summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorDino Viehland <dinoviehland@gmail.com>2019-05-28 23:21:17 (GMT)
committerGitHub <noreply@github.com>2019-05-28 23:21:17 (GMT)
commit415406999d7c09af9f3dcacfb4578b9e97b2ce77 (patch)
tree99aba9596d3532bb75913ac7c1c8dda8aebd9f91 /Python
parentab0716ed1ea2957396054730afbb80c1825f9786 (diff)
downloadcpython-415406999d7c09af9f3dcacfb4578b9e97b2ce77.zip
cpython-415406999d7c09af9f3dcacfb4578b9e97b2ce77.tar.gz
cpython-415406999d7c09af9f3dcacfb4578b9e97b2ce77.tar.bz2
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
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c55
-rw-r--r--Python/pythonrun.c64
2 files changed, 63 insertions, 56 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 5d58085..065ad95 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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)
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index ace9f2f..784c15b 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -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;