summaryrefslogtreecommitdiffstats
path: root/Python/Python-ast.c
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2025-02-16 13:32:39 (GMT)
committerGitHub <noreply@github.com>2025-02-16 13:32:39 (GMT)
commitc9b1bf302ce67031e0309a9b0ea55ecdd68ed682 (patch)
tree65fc48a20bef7d7921502bc5c1e7859b41c70871 /Python/Python-ast.c
parent2e8044a4f74f5fc19e5249139c171403aff0118d (diff)
downloadcpython-c9b1bf302ce67031e0309a9b0ea55ecdd68ed682.zip
cpython-c9b1bf302ce67031e0309a9b0ea55ecdd68ed682.tar.gz
cpython-c9b1bf302ce67031e0309a9b0ea55ecdd68ed682.tar.bz2
gh-130139: always check ast node type in ast.parse() with ast input (#130140)
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r--Python/Python-ast.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 7038e3c..4adf72a 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -18161,18 +18161,13 @@ PyObject* PyAST_mod2obj(mod_ty t)
}
/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
-mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
+int PyAst_CheckMode(PyObject *ast, int mode)
{
const char * const req_name[] = {"Module", "Expression", "Interactive"};
- int isinstance;
-
- if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
- return NULL;
- }
struct ast_state *state = get_ast_state();
if (state == NULL) {
- return NULL;
+ return -1;
}
PyObject *req_type[3];
@@ -18181,13 +18176,30 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
req_type[2] = state->Interactive_type;
assert(0 <= mode && mode <= 2);
-
- isinstance = PyObject_IsInstance(ast, req_type[mode]);
- if (isinstance == -1)
- return NULL;
+ int isinstance = PyObject_IsInstance(ast, req_type[mode]);
+ if (isinstance == -1) {
+ return -1;
+ }
if (!isinstance) {
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
req_name[mode], _PyType_Name(Py_TYPE(ast)));
+ return -1;
+ }
+ return 0;
+}
+
+mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
+{
+ if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
+ return NULL;
+ }
+
+ struct ast_state *state = get_ast_state();
+ if (state == NULL) {
+ return NULL;
+ }
+
+ if (PyAst_CheckMode(ast, mode) < 0) {
return NULL;
}