diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-02 23:21:08 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-02 23:21:08 (GMT) |
commit | 4fdb68491e8b2d044c9173babf625bbb815c39d1 (patch) | |
tree | 652360cd0b400bb3077d6106bee5f02bbea30c61 /Python | |
parent | b0ef78535a94b6b368a8b9935525cb3162c670d4 (diff) | |
download | cpython-4fdb68491e8b2d044c9173babf625bbb815c39d1.zip cpython-4fdb68491e8b2d044c9173babf625bbb815c39d1.tar.gz cpython-4fdb68491e8b2d044c9173babf625bbb815c39d1.tar.bz2 |
Issue #22896: Avoid to use PyObject_AsCharBuffer(), PyObject_AsReadBuffer()
and PyObject_AsWriteBuffer().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index d905ba2..4b4f979 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -559,10 +559,10 @@ PyDoc_STRVAR(chr_doc, Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); -static char * -source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf) +static const char * +source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, Py_buffer *view) { - char *str; + const char *str; Py_ssize_t size; if (PyUnicode_Check(cmd)) { @@ -571,19 +571,21 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf) if (str == NULL) return NULL; } - else if (!PyObject_CheckReadBuffer(cmd)) { + else if (PyObject_GetBuffer(cmd, view, PyBUF_SIMPLE) == 0) { + str = (const char *)view->buf; + size = view->len; + } + else { PyErr_Format(PyExc_TypeError, "%s() arg 1 must be a %s object", funcname, what); return NULL; } - else if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) { - return NULL; - } if (strlen(str) != size) { PyErr_SetString(PyExc_TypeError, "source code string cannot contain null bytes"); + PyBuffer_Release(view); return NULL; } return str; @@ -592,7 +594,8 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf) static PyObject * builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) { - char *str; + Py_buffer view = {NULL, NULL}; + const char *str; PyObject *filename; char *startstr; int mode = -1; @@ -678,11 +681,12 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) goto finally; } - str = source_as_string(cmd, "compile", "string, bytes or AST", &cf); + str = source_as_string(cmd, "compile", "string, bytes or AST", &cf, &view); if (str == NULL) goto error; result = Py_CompileStringObject(str, filename, start[mode], &cf, optimize); + PyBuffer_Release(&view); goto finally; error: @@ -752,7 +756,8 @@ builtin_eval(PyObject *self, PyObject *args) { PyObject *cmd, *result, *tmp = NULL; PyObject *globals = Py_None, *locals = Py_None; - char *str; + Py_buffer view = {NULL, NULL}; + const char *str; PyCompilerFlags cf; if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals)) @@ -801,7 +806,7 @@ builtin_eval(PyObject *self, PyObject *args) } cf.cf_flags = PyCF_SOURCE_IS_UTF8; - str = source_as_string(cmd, "eval", "string, bytes or code", &cf); + str = source_as_string(cmd, "eval", "string, bytes or code", &cf, &view); if (str == NULL) return NULL; @@ -810,6 +815,7 @@ builtin_eval(PyObject *self, PyObject *args) (void)PyEval_MergeCompilerFlags(&cf); result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); + PyBuffer_Release(&view); Py_XDECREF(tmp); return result; } @@ -876,11 +882,12 @@ builtin_exec(PyObject *self, PyObject *args) v = PyEval_EvalCode(prog, globals, locals); } else { - char *str; + Py_buffer view = {NULL, NULL}; + const char *str; PyCompilerFlags cf; cf.cf_flags = PyCF_SOURCE_IS_UTF8; str = source_as_string(prog, "exec", - "string, bytes or code", &cf); + "string, bytes or code", &cf, &view); if (str == NULL) return NULL; if (PyEval_MergeCompilerFlags(&cf)) @@ -888,6 +895,7 @@ builtin_exec(PyObject *self, PyObject *args) locals, &cf); else v = PyRun_String(str, Py_file_input, globals, locals); + PyBuffer_Release(&view); } if (v == NULL) return NULL; |