mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
bpo-37001: Makes symtable.symtable have parity with compile for input (#13483)
* Makes symtable.symtable have parity for accepted datatypes for source code as compile() * Add NEWS blurb
This commit is contained in:
parent
ab0716ed1e
commit
415406999d
7 changed files with 111 additions and 79 deletions
|
@ -687,55 +687,6 @@ builtin_chr_impl(PyObject *module, int i)
|
|||
}
|
||||
|
||||
|
||||
static const char *
|
||||
source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
|
||||
{
|
||||
const char *str;
|
||||
Py_ssize_t size;
|
||||
Py_buffer view;
|
||||
|
||||
*cmd_copy = NULL;
|
||||
if (PyUnicode_Check(cmd)) {
|
||||
cf->cf_flags |= PyCF_IGNORE_COOKIE;
|
||||
str = PyUnicode_AsUTF8AndSize(cmd, &size);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
}
|
||||
else if (PyBytes_Check(cmd)) {
|
||||
str = PyBytes_AS_STRING(cmd);
|
||||
size = PyBytes_GET_SIZE(cmd);
|
||||
}
|
||||
else if (PyByteArray_Check(cmd)) {
|
||||
str = PyByteArray_AS_STRING(cmd);
|
||||
size = PyByteArray_GET_SIZE(cmd);
|
||||
}
|
||||
else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
|
||||
/* Copy to NUL-terminated buffer. */
|
||||
*cmd_copy = PyBytes_FromStringAndSize(
|
||||
(const char *)view.buf, view.len);
|
||||
PyBuffer_Release(&view);
|
||||
if (*cmd_copy == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
str = PyBytes_AS_STRING(*cmd_copy);
|
||||
size = PyBytes_GET_SIZE(*cmd_copy);
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"%s() arg 1 must be a %s object",
|
||||
funcname, what);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strlen(str) != (size_t)size) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"source code string cannot contain null bytes");
|
||||
Py_CLEAR(*cmd_copy);
|
||||
return NULL;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
compile as builtin_compile
|
||||
|
||||
|
@ -855,7 +806,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
|
|||
goto finally;
|
||||
}
|
||||
|
||||
str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
|
||||
str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
|
||||
if (str == NULL)
|
||||
goto error;
|
||||
|
||||
|
@ -991,7 +942,7 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
|
|||
|
||||
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
|
||||
cf.cf_feature_version = PY_MINOR_VERSION;
|
||||
str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
|
||||
str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -1083,7 +1034,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
|
|||
PyCompilerFlags cf;
|
||||
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
|
||||
cf.cf_feature_version = PY_MINOR_VERSION;
|
||||
str = source_as_string(source, "exec",
|
||||
str = _Py_SourceAsString(source, "exec",
|
||||
"string, bytes or code", &cf,
|
||||
&source_copy);
|
||||
if (str == NULL)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue