mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Issue #22896: Avoid to use PyObject_AsCharBuffer(), PyObject_AsReadBuffer()
and PyObject_AsWriteBuffer().
This commit is contained in:
parent
b0ef78535a
commit
4fdb68491e
17 changed files with 327 additions and 335 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue