mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
gh-130139: always check ast node type in ast.parse() with ast input (#130140)
This commit is contained in:
parent
2e8044a4f7
commit
c9b1bf302c
8 changed files with 73 additions and 32 deletions
42
Python/Python-ast.c
generated
42
Python/Python-ast.c
generated
|
@ -18161,11 +18161,35 @@ 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;
|
||||
|
||||
struct ast_state *state = get_ast_state();
|
||||
if (state == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
PyObject *req_type[3];
|
||||
req_type[0] = state->Module_type;
|
||||
req_type[1] = state->Expression_type;
|
||||
req_type[2] = state->Interactive_type;
|
||||
|
||||
assert(0 <= mode && mode <= 2);
|
||||
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;
|
||||
}
|
||||
|
@ -18175,19 +18199,7 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *req_type[3];
|
||||
req_type[0] = state->Module_type;
|
||||
req_type[1] = state->Expression_type;
|
||||
req_type[2] = state->Interactive_type;
|
||||
|
||||
assert(0 <= mode && mode <= 2);
|
||||
|
||||
isinstance = PyObject_IsInstance(ast, req_type[mode]);
|
||||
if (isinstance == -1)
|
||||
return NULL;
|
||||
if (!isinstance) {
|
||||
PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
|
||||
req_name[mode], _PyType_Name(Py_TYPE(ast)));
|
||||
if (PyAst_CheckMode(ast, mode) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue