mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +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
|
@ -1231,21 +1231,77 @@ PyCompileString(const char *str, const char *filename, int start)
|
|||
return Py_CompileStringFlags(str, filename, start, NULL);
|
||||
}
|
||||
|
||||
const char *
|
||||
_Py_SourceAsString(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;
|
||||
}
|
||||
|
||||
struct symtable *
|
||||
Py_SymtableStringObject(const char *str, PyObject *filename, int start)
|
||||
{
|
||||
PyCompilerFlags flags;
|
||||
|
||||
flags.cf_flags = 0;
|
||||
flags.cf_feature_version = PY_MINOR_VERSION;
|
||||
return _Py_SymtableStringObjectFlags(str, filename, start, &flags);
|
||||
}
|
||||
|
||||
struct symtable *
|
||||
_Py_SymtableStringObjectFlags(const char *str, PyObject *filename, int start, PyCompilerFlags *flags)
|
||||
{
|
||||
struct symtable *st;
|
||||
mod_ty mod;
|
||||
PyCompilerFlags flags;
|
||||
PyArena *arena;
|
||||
|
||||
arena = PyArena_New();
|
||||
if (arena == NULL)
|
||||
return NULL;
|
||||
|
||||
flags.cf_flags = 0;
|
||||
flags.cf_feature_version = PY_MINOR_VERSION;
|
||||
mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
|
||||
mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
|
||||
if (mod == NULL) {
|
||||
PyArena_Free(arena);
|
||||
return NULL;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue