2005-10-21 03:59:25 +08:00
|
|
|
/*
|
2020-10-11 01:14:59 +08:00
|
|
|
* This file exposes PyAST_Validate interface to check the integrity
|
|
|
|
* of the given abstract syntax tree (potentially constructed manually).
|
2005-10-21 03:59:25 +08:00
|
|
|
*/
|
|
|
|
#include "Python.h"
|
2021-03-24 03:47:40 +08:00
|
|
|
#include "pycore_ast.h" // asdl_stmt_seq
|
2021-04-25 18:38:00 +08:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
2005-10-21 03:59:25 +08:00
|
|
|
|
|
|
|
#include <assert.h>
|
2021-04-29 13:58:44 +08:00
|
|
|
#include <stdbool.h>
|
2019-01-12 15:46:50 +08:00
|
|
|
|
2021-04-25 18:38:00 +08:00
|
|
|
struct validator {
|
|
|
|
int recursion_depth; /* current recursion depth */
|
|
|
|
int recursion_limit; /* recursion limit */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int validate_stmts(struct validator *, asdl_stmt_seq *);
|
|
|
|
static int validate_exprs(struct validator *, asdl_expr_seq*, expr_context_ty, int);
|
2020-09-17 02:42:00 +08:00
|
|
|
static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
|
2021-04-25 18:38:00 +08:00
|
|
|
static int validate_stmt(struct validator *, stmt_ty);
|
|
|
|
static int validate_expr(struct validator *, expr_ty, expr_context_ty);
|
2021-04-29 13:58:44 +08:00
|
|
|
static int validate_pattern(struct validator *, pattern_ty);
|
2011-08-10 05:15:04 +08:00
|
|
|
|
2020-06-06 20:44:16 +08:00
|
|
|
static int
|
|
|
|
validate_name(PyObject *name)
|
|
|
|
{
|
|
|
|
assert(PyUnicode_Check(name));
|
|
|
|
static const char * const forbidden[] = {
|
|
|
|
"None",
|
|
|
|
"True",
|
|
|
|
"False",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
for (int i = 0; forbidden[i] != NULL; i++) {
|
|
|
|
if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:15:04 +08:00
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_comprehension(struct validator *state, asdl_comprehension_seq *gens)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2011-08-10 05:15:04 +08:00
|
|
|
if (!asdl_seq_LEN(gens)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < asdl_seq_LEN(gens); i++) {
|
|
|
|
comprehension_ty comp = asdl_seq_GET(gens, i);
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, comp->target, Store) ||
|
|
|
|
!validate_expr(state, comp->iter, Load) ||
|
|
|
|
!validate_exprs(state, comp->ifs, Load, 0))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_keywords(struct validator *state, asdl_keyword_seq *keywords)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2011-08-10 05:15:04 +08:00
|
|
|
for (i = 0; i < asdl_seq_LEN(keywords); i++)
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, (asdl_seq_GET(keywords, i))->value, Load))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_args(struct validator *state, asdl_arg_seq *args)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2011-08-10 05:15:04 +08:00
|
|
|
for (i = 0; i < asdl_seq_LEN(args); i++) {
|
|
|
|
arg_ty arg = asdl_seq_GET(args, i);
|
2021-04-25 18:38:00 +08:00
|
|
|
if (arg->annotation && !validate_expr(state, arg->annotation, Load))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
expr_context_name(expr_context_ty ctx)
|
|
|
|
{
|
|
|
|
switch (ctx) {
|
|
|
|
case Load:
|
|
|
|
return "Load";
|
|
|
|
case Store:
|
|
|
|
return "Store";
|
|
|
|
case Del:
|
|
|
|
return "Del";
|
2021-04-29 13:58:44 +08:00
|
|
|
// No default case so compiler emits warning for unhandled cases
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
2021-04-29 13:58:44 +08:00
|
|
|
Py_UNREACHABLE();
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_arguments(struct validator *state, arguments_ty args)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_args(state, args->posonlyargs) || !validate_args(state, args->args)) {
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
2019-04-29 20:36:57 +08:00
|
|
|
}
|
2013-03-19 01:48:58 +08:00
|
|
|
if (args->vararg && args->vararg->annotation
|
2021-04-25 18:38:00 +08:00
|
|
|
&& !validate_expr(state, args->vararg->annotation, Load)) {
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_args(state, args->kwonlyargs))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
2015-03-18 22:02:06 +08:00
|
|
|
if (args->kwarg && args->kwarg->annotation
|
2021-04-25 18:38:00 +08:00
|
|
|
&& !validate_expr(state, args->kwarg->annotation, Load)) {
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-31 21:09:49 +08:00
|
|
|
if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
|
2011-08-10 05:15:04 +08:00
|
|
|
PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
|
|
|
|
"kw_defaults on arguments");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
return validate_exprs(state, args->defaults, Load, 0) && validate_exprs(state, args->kw_defaults, Load, 1);
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 07:40:57 +08:00
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_constant(struct validator *state, PyObject *value)
|
2016-01-26 07:40:57 +08:00
|
|
|
{
|
|
|
|
if (value == Py_None || value == Py_Ellipsis)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (PyLong_CheckExact(value)
|
|
|
|
|| PyFloat_CheckExact(value)
|
|
|
|
|| PyComplex_CheckExact(value)
|
|
|
|
|| PyBool_Check(value)
|
|
|
|
|| PyUnicode_CheckExact(value)
|
|
|
|
|| PyBytes_CheckExact(value))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
|
2021-04-25 18:38:00 +08:00
|
|
|
if (++state->recursion_depth > state->recursion_limit) {
|
|
|
|
PyErr_SetString(PyExc_RecursionError,
|
|
|
|
"maximum recursion depth exceeded during compilation");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-26 07:40:57 +08:00
|
|
|
|
2021-04-25 18:38:00 +08:00
|
|
|
PyObject *it = PyObject_GetIter(value);
|
2016-01-26 07:40:57 +08:00
|
|
|
if (it == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
PyObject *item = PyIter_Next(it);
|
|
|
|
if (item == NULL) {
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(it);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_constant(state, item)) {
|
2016-01-26 07:40:57 +08:00
|
|
|
Py_DECREF(it);
|
2016-01-27 07:11:47 +08:00
|
|
|
Py_DECREF(item);
|
2016-01-26 07:40:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-27 07:11:47 +08:00
|
|
|
Py_DECREF(item);
|
2016-01-26 07:40:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(it);
|
2021-04-25 18:38:00 +08:00
|
|
|
--state->recursion_depth;
|
2016-01-26 07:40:57 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-19 19:32:28 +08:00
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"got an invalid type in Constant: %s",
|
|
|
|
_PyType_Name(Py_TYPE(value)));
|
|
|
|
}
|
2016-01-26 07:40:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:15:04 +08:00
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2021-04-29 13:58:44 +08:00
|
|
|
int ret = -1;
|
2021-04-25 18:38:00 +08:00
|
|
|
if (++state->recursion_depth > state->recursion_limit) {
|
|
|
|
PyErr_SetString(PyExc_RecursionError,
|
|
|
|
"maximum recursion depth exceeded during compilation");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-10 05:15:04 +08:00
|
|
|
int check_ctx = 1;
|
|
|
|
expr_context_ty actual_ctx;
|
|
|
|
|
|
|
|
/* First check expression context. */
|
|
|
|
switch (exp->kind) {
|
|
|
|
case Attribute_kind:
|
|
|
|
actual_ctx = exp->v.Attribute.ctx;
|
|
|
|
break;
|
|
|
|
case Subscript_kind:
|
|
|
|
actual_ctx = exp->v.Subscript.ctx;
|
|
|
|
break;
|
|
|
|
case Starred_kind:
|
|
|
|
actual_ctx = exp->v.Starred.ctx;
|
|
|
|
break;
|
|
|
|
case Name_kind:
|
2020-06-06 20:44:16 +08:00
|
|
|
if (!validate_name(exp->v.Name.id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-10 05:15:04 +08:00
|
|
|
actual_ctx = exp->v.Name.ctx;
|
|
|
|
break;
|
|
|
|
case List_kind:
|
|
|
|
actual_ctx = exp->v.List.ctx;
|
|
|
|
break;
|
|
|
|
case Tuple_kind:
|
|
|
|
actual_ctx = exp->v.Tuple.ctx;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (ctx != Load) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "expression which can't be "
|
|
|
|
"assigned to in %s context", expr_context_name(ctx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
check_ctx = 0;
|
2015-03-18 22:02:06 +08:00
|
|
|
/* set actual_ctx to prevent gcc warning */
|
|
|
|
actual_ctx = 0;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
if (check_ctx && actual_ctx != ctx) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
|
|
|
|
expr_context_name(ctx), expr_context_name(actual_ctx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now validate expression. */
|
|
|
|
switch (exp->kind) {
|
|
|
|
case BoolOp_kind:
|
|
|
|
if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.BoolOp.values, Load, 0);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case BinOp_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.BinOp.left, Load) &&
|
|
|
|
validate_expr(state, exp->v.BinOp.right, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case UnaryOp_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.UnaryOp.operand, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Lambda_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_arguments(state, exp->v.Lambda.args) &&
|
|
|
|
validate_expr(state, exp->v.Lambda.body, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case IfExp_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.IfExp.test, Load) &&
|
|
|
|
validate_expr(state, exp->v.IfExp.body, Load) &&
|
|
|
|
validate_expr(state, exp->v.IfExp.orelse, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Dict_kind:
|
|
|
|
if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"Dict doesn't have the same number of keys as values");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-02 04:10:49 +08:00
|
|
|
/* null_ok=1 for keys expressions to allow dict unpacking to work in
|
|
|
|
dict literals, i.e. ``{**{a:b}}`` */
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
|
|
|
|
validate_exprs(state, exp->v.Dict.values, Load, /*null_ok=*/ 0);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Set_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.Set.elts, Load, 0);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
#define COMP(NAME) \
|
|
|
|
case NAME ## _kind: \
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_comprehension(state, exp->v.NAME.generators) && \
|
|
|
|
validate_expr(state, exp->v.NAME.elt, Load); \
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
COMP(ListComp)
|
|
|
|
COMP(SetComp)
|
|
|
|
COMP(GeneratorExp)
|
|
|
|
#undef COMP
|
|
|
|
case DictComp_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_comprehension(state, exp->v.DictComp.generators) &&
|
|
|
|
validate_expr(state, exp->v.DictComp.key, Load) &&
|
|
|
|
validate_expr(state, exp->v.DictComp.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Yield_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = !exp->v.Yield.value || validate_expr(state, exp->v.Yield.value, Load);
|
|
|
|
break;
|
2012-01-14 21:58:23 +08:00
|
|
|
case YieldFrom_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.YieldFrom.value, Load);
|
|
|
|
break;
|
2015-05-12 10:57:16 +08:00
|
|
|
case Await_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.Await.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Compare_kind:
|
|
|
|
if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (asdl_seq_LEN(exp->v.Compare.comparators) !=
|
|
|
|
asdl_seq_LEN(exp->v.Compare.ops)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Compare has a different number "
|
|
|
|
"of comparators and operands");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.Compare.comparators, Load, 0) &&
|
|
|
|
validate_expr(state, exp->v.Compare.left, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Call_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.Call.func, Load) &&
|
|
|
|
validate_exprs(state, exp->v.Call.args, Load, 0) &&
|
|
|
|
validate_keywords(state, exp->v.Call.keywords);
|
|
|
|
break;
|
2016-01-26 07:40:57 +08:00
|
|
|
case Constant_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_constant(state, exp->v.Constant.value)) {
|
2016-01-26 07:40:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = 1;
|
|
|
|
break;
|
2015-09-20 02:51:32 +08:00
|
|
|
case JoinedStr_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.JoinedStr.values, Load, 0);
|
|
|
|
break;
|
2015-09-20 02:51:32 +08:00
|
|
|
case FormattedValue_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
if (validate_expr(state, exp->v.FormattedValue.value, Load) == 0)
|
2015-09-20 02:51:32 +08:00
|
|
|
return 0;
|
2021-04-25 18:38:00 +08:00
|
|
|
if (exp->v.FormattedValue.format_spec) {
|
|
|
|
ret = validate_expr(state, exp->v.FormattedValue.format_spec, Load);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Attribute_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.Attribute.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Subscript_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.Subscript.slice, Load) &&
|
|
|
|
validate_expr(state, exp->v.Subscript.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Starred_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.Starred.value, ctx);
|
|
|
|
break;
|
2020-03-11 00:52:34 +08:00
|
|
|
case Slice_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = (!exp->v.Slice.lower || validate_expr(state, exp->v.Slice.lower, Load)) &&
|
|
|
|
(!exp->v.Slice.upper || validate_expr(state, exp->v.Slice.upper, Load)) &&
|
|
|
|
(!exp->v.Slice.step || validate_expr(state, exp->v.Slice.step, Load));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case List_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.List.elts, ctx, 0);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Tuple_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_exprs(state, exp->v.Tuple.elts, ctx, 0);
|
|
|
|
break;
|
2019-03-18 21:51:53 +08:00
|
|
|
case NamedExpr_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, exp->v.NamedExpr.value, Load);
|
|
|
|
break;
|
2018-09-27 22:42:37 +08:00
|
|
|
/* This last case doesn't have any checking. */
|
2011-08-10 05:15:04 +08:00
|
|
|
case Name_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = 1;
|
|
|
|
break;
|
2021-04-29 13:58:44 +08:00
|
|
|
// No default case so compiler emits warning for unhandled cases
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2021-04-25 18:38:00 +08:00
|
|
|
PyErr_SetString(PyExc_SystemError, "unexpected expression");
|
2021-04-29 13:58:44 +08:00
|
|
|
ret = 0;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
state->recursion_depth--;
|
|
|
|
return ret;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:58:44 +08:00
|
|
|
|
|
|
|
// Note: the ensure_literal_* functions are only used to validate a restricted
|
|
|
|
// set of non-recursive literals that have already been checked with
|
|
|
|
// validate_expr, so they don't accept the validator state
|
|
|
|
static int
|
|
|
|
ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
|
|
|
|
{
|
|
|
|
assert(exp->kind == Constant_kind);
|
|
|
|
PyObject *value = exp->v.Constant.value;
|
|
|
|
return (allow_real && PyFloat_CheckExact(value)) ||
|
|
|
|
(allow_real && PyLong_CheckExact(value)) ||
|
|
|
|
(allow_imaginary && PyComplex_CheckExact(value));
|
|
|
|
}
|
|
|
|
|
2021-02-27 06:51:55 +08:00
|
|
|
static int
|
2021-04-29 13:58:44 +08:00
|
|
|
ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
|
2021-02-27 06:51:55 +08:00
|
|
|
{
|
2021-04-29 13:58:44 +08:00
|
|
|
assert(exp->kind == UnaryOp_kind);
|
|
|
|
// Must be negation ...
|
|
|
|
if (exp->v.UnaryOp.op != USub) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// ... of a constant ...
|
|
|
|
expr_ty operand = exp->v.UnaryOp.operand;
|
|
|
|
if (operand->kind != Constant_kind) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// ... number
|
|
|
|
return ensure_literal_number(operand, allow_real, allow_imaginary);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ensure_literal_complex(expr_ty exp)
|
|
|
|
{
|
|
|
|
assert(exp->kind == BinOp_kind);
|
|
|
|
expr_ty left = exp->v.BinOp.left;
|
|
|
|
expr_ty right = exp->v.BinOp.right;
|
|
|
|
// Ensure op is addition or subtraction
|
|
|
|
if (exp->v.BinOp.op != Add && exp->v.BinOp.op != Sub) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Check LHS is a real number (potentially signed)
|
|
|
|
switch (left->kind)
|
|
|
|
{
|
|
|
|
case Constant_kind:
|
|
|
|
if (!ensure_literal_number(left, /*real=*/true, /*imaginary=*/false)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UnaryOp_kind:
|
|
|
|
if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Check RHS is an imaginary number (no separate sign allowed)
|
|
|
|
switch (right->kind)
|
|
|
|
{
|
|
|
|
case Constant_kind:
|
|
|
|
if (!ensure_literal_number(right, /*real=*/false, /*imaginary=*/true)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-27 06:51:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:58:44 +08:00
|
|
|
static int
|
|
|
|
validate_pattern_match_value(struct validator *state, expr_ty exp)
|
|
|
|
{
|
|
|
|
if (!validate_expr(state, exp, Load)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (exp->kind)
|
|
|
|
{
|
|
|
|
case Constant_kind:
|
|
|
|
case Attribute_kind:
|
|
|
|
// Constants and attribute lookups are always permitted
|
|
|
|
return 1;
|
|
|
|
case UnaryOp_kind:
|
|
|
|
// Negated numbers are permitted (whether real or imaginary)
|
|
|
|
// Compiler will complain if AST folding doesn't create a constant
|
|
|
|
if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BinOp_kind:
|
|
|
|
// Complex literals are permitted
|
|
|
|
// Compiler will complain if AST folding doesn't create a constant
|
|
|
|
if (ensure_literal_complex(exp)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
"patterns may only match literals and attribute lookups");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
validate_pattern(struct validator *state, pattern_ty p)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
if (++state->recursion_depth > state->recursion_limit) {
|
|
|
|
PyErr_SetString(PyExc_RecursionError,
|
|
|
|
"maximum recursion depth exceeded during compilation");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Coming soon: https://bugs.python.org/issue43897 (thanks Batuhan)!
|
|
|
|
// TODO: Ensure no subnodes use "_" as an ordinary identifier
|
|
|
|
switch (p->kind) {
|
|
|
|
case MatchValue_kind:
|
|
|
|
ret = validate_pattern_match_value(state, p->v.MatchValue.value);
|
|
|
|
break;
|
|
|
|
case MatchSingleton_kind:
|
|
|
|
// TODO: Check constant is specifically None, True, or False
|
|
|
|
ret = validate_constant(state, p->v.MatchSingleton.value);
|
|
|
|
break;
|
|
|
|
case MatchSequence_kind:
|
|
|
|
// TODO: Validate all subpatterns
|
|
|
|
// return validate_patterns(state, p->v.MatchSequence.patterns);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case MatchMapping_kind:
|
|
|
|
// TODO: check "rest" target name is valid
|
|
|
|
if (asdl_seq_LEN(p->v.MatchMapping.keys) != asdl_seq_LEN(p->v.MatchMapping.patterns)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"MatchMapping doesn't have the same number of keys as patterns");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// null_ok=0 for key expressions, as rest-of-mapping is captured in "rest"
|
|
|
|
// TODO: replace with more restrictive expression validator, as per MatchValue above
|
|
|
|
if (!validate_exprs(state, p->v.MatchMapping.keys, Load, /*null_ok=*/ 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// TODO: Validate all subpatterns
|
|
|
|
// ret = validate_patterns(state, p->v.MatchMapping.patterns);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case MatchClass_kind:
|
|
|
|
if (asdl_seq_LEN(p->v.MatchClass.kwd_attrs) != asdl_seq_LEN(p->v.MatchClass.kwd_patterns)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"MatchClass doesn't have the same number of keyword attributes as patterns");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// TODO: Restrict cls lookup to being a name or attribute
|
|
|
|
if (!validate_expr(state, p->v.MatchClass.cls, Load)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// TODO: Validate all subpatterns
|
|
|
|
// return validate_patterns(state, p->v.MatchClass.patterns) &&
|
|
|
|
// validate_patterns(state, p->v.MatchClass.kwd_patterns);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case MatchStar_kind:
|
|
|
|
// TODO: check target name is valid
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case MatchAs_kind:
|
|
|
|
// TODO: check target name is valid
|
|
|
|
if (p->v.MatchAs.pattern == NULL) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else if (p->v.MatchAs.name == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"MatchAs must specify a target name if a pattern is given");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = validate_pattern(state, p->v.MatchAs.pattern);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MatchOr_kind:
|
|
|
|
// TODO: Validate all subpatterns
|
|
|
|
// return validate_patterns(state, p->v.MatchOr.patterns);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
// No default case, so the compiler will emit a warning if new pattern
|
|
|
|
// kinds are added without being handled here
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "unexpected pattern");
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
state->recursion_depth--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:15:04 +08:00
|
|
|
static int
|
2020-09-17 02:42:00 +08:00
|
|
|
_validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
|
|
|
if (asdl_seq_LEN(seq))
|
|
|
|
return 1;
|
|
|
|
PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-17 02:42:00 +08:00
|
|
|
#define validate_nonempty_seq(seq, what, owner) _validate_nonempty_seq((asdl_seq*)seq, what, owner)
|
2011-08-10 05:15:04 +08:00
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_assignlist(struct validator *state, asdl_expr_seq *targets, expr_context_ty ctx)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
|
|
|
return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_exprs(state, targets, ctx, 0);
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_body(struct validator *state, asdl_stmt_seq *body, const char *owner)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2021-04-25 18:38:00 +08:00
|
|
|
return validate_nonempty_seq(body, "body", owner) && validate_stmts(state, body);
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_stmt(struct validator *state, stmt_ty stmt)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2021-04-29 13:58:44 +08:00
|
|
|
int ret = -1;
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2021-04-25 18:38:00 +08:00
|
|
|
if (++state->recursion_depth > state->recursion_limit) {
|
|
|
|
PyErr_SetString(PyExc_RecursionError,
|
|
|
|
"maximum recursion depth exceeded during compilation");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-10 05:15:04 +08:00
|
|
|
switch (stmt->kind) {
|
|
|
|
case FunctionDef_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_body(state, stmt->v.FunctionDef.body, "FunctionDef") &&
|
|
|
|
validate_arguments(state, stmt->v.FunctionDef.args) &&
|
|
|
|
validate_exprs(state, stmt->v.FunctionDef.decorator_list, Load, 0) &&
|
2011-08-10 05:15:04 +08:00
|
|
|
(!stmt->v.FunctionDef.returns ||
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_expr(state, stmt->v.FunctionDef.returns, Load));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case ClassDef_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_body(state, stmt->v.ClassDef.body, "ClassDef") &&
|
|
|
|
validate_exprs(state, stmt->v.ClassDef.bases, Load, 0) &&
|
|
|
|
validate_keywords(state, stmt->v.ClassDef.keywords) &&
|
|
|
|
validate_exprs(state, stmt->v.ClassDef.decorator_list, Load, 0);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Return_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = !stmt->v.Return.value || validate_expr(state, stmt->v.Return.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Delete_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_assignlist(state, stmt->v.Delete.targets, Del);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Assign_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_assignlist(state, stmt->v.Assign.targets, Store) &&
|
|
|
|
validate_expr(state, stmt->v.Assign.value, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case AugAssign_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.AugAssign.target, Store) &&
|
|
|
|
validate_expr(state, stmt->v.AugAssign.value, Load);
|
|
|
|
break;
|
2016-09-09 11:50:03 +08:00
|
|
|
case AnnAssign_kind:
|
|
|
|
if (stmt->v.AnnAssign.target->kind != Name_kind &&
|
|
|
|
stmt->v.AnnAssign.simple) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"AnnAssign with simple non-Name target");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.AnnAssign.target, Store) &&
|
2016-09-09 11:50:03 +08:00
|
|
|
(!stmt->v.AnnAssign.value ||
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_expr(state, stmt->v.AnnAssign.value, Load)) &&
|
|
|
|
validate_expr(state, stmt->v.AnnAssign.annotation, Load);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case For_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.For.target, Store) &&
|
|
|
|
validate_expr(state, stmt->v.For.iter, Load) &&
|
|
|
|
validate_body(state, stmt->v.For.body, "For") &&
|
|
|
|
validate_stmts(state, stmt->v.For.orelse);
|
|
|
|
break;
|
2015-05-12 10:57:16 +08:00
|
|
|
case AsyncFor_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.AsyncFor.target, Store) &&
|
|
|
|
validate_expr(state, stmt->v.AsyncFor.iter, Load) &&
|
|
|
|
validate_body(state, stmt->v.AsyncFor.body, "AsyncFor") &&
|
|
|
|
validate_stmts(state, stmt->v.AsyncFor.orelse);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case While_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.While.test, Load) &&
|
|
|
|
validate_body(state, stmt->v.While.body, "While") &&
|
|
|
|
validate_stmts(state, stmt->v.While.orelse);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case If_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.If.test, Load) &&
|
|
|
|
validate_body(state, stmt->v.If.body, "If") &&
|
|
|
|
validate_stmts(state, stmt->v.If.orelse);
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case With_kind:
|
|
|
|
if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
|
|
|
|
withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, item->context_expr, Load) ||
|
|
|
|
(item->optional_vars && !validate_expr(state, item->optional_vars, Store)))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_body(state, stmt->v.With.body, "With");
|
|
|
|
break;
|
2015-05-12 10:57:16 +08:00
|
|
|
case AsyncWith_kind:
|
|
|
|
if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
|
|
|
|
withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, item->context_expr, Load) ||
|
|
|
|
(item->optional_vars && !validate_expr(state, item->optional_vars, Store)))
|
2015-05-12 10:57:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_body(state, stmt->v.AsyncWith.body, "AsyncWith");
|
|
|
|
break;
|
2021-02-27 06:51:55 +08:00
|
|
|
case Match_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, stmt->v.Match.subject, Load)
|
2021-02-27 06:51:55 +08:00
|
|
|
|| !validate_nonempty_seq(stmt->v.Match.cases, "cases", "Match")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < asdl_seq_LEN(stmt->v.Match.cases); i++) {
|
|
|
|
match_case_ty m = asdl_seq_GET(stmt->v.Match.cases, i);
|
2021-04-29 13:58:44 +08:00
|
|
|
if (!validate_pattern(state, m->pattern)
|
2021-04-25 18:38:00 +08:00
|
|
|
|| (m->guard && !validate_expr(state, m->guard, Load))
|
|
|
|
|| !validate_body(state, m->body, "match_case")) {
|
2021-02-27 06:51:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = 1;
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Raise_kind:
|
|
|
|
if (stmt->v.Raise.exc) {
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.Raise.exc, Load) &&
|
|
|
|
(!stmt->v.Raise.cause || validate_expr(state, stmt->v.Raise.cause, Load));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
if (stmt->v.Raise.cause) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = 1;
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Try_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_body(state, stmt->v.Try.body, "Try"))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
|
|
|
|
!asdl_seq_LEN(stmt->v.Try.finalbody)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
|
|
|
|
asdl_seq_LEN(stmt->v.Try.orelse)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
|
|
|
|
excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
|
|
|
|
if ((handler->v.ExceptHandler.type &&
|
2021-04-25 18:38:00 +08:00
|
|
|
!validate_expr(state, handler->v.ExceptHandler.type, Load)) ||
|
|
|
|
!validate_body(state, handler->v.ExceptHandler.body, "ExceptHandler"))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
|
|
|
|
validate_stmts(state, stmt->v.Try.finalbody)) &&
|
2011-08-10 05:15:04 +08:00
|
|
|
(!asdl_seq_LEN(stmt->v.Try.orelse) ||
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_stmts(state, stmt->v.Try.orelse));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Assert_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.Assert.test, Load) &&
|
|
|
|
(!stmt->v.Assert.msg || validate_expr(state, stmt->v.Assert.msg, Load));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Import_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case ImportFrom_kind:
|
2016-06-28 02:39:12 +08:00
|
|
|
if (stmt->v.ImportFrom.level < 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Global_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Nonlocal_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Expr_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_expr(state, stmt->v.Expr.value, Load);
|
|
|
|
break;
|
2015-05-12 10:57:16 +08:00
|
|
|
case AsyncFunctionDef_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = validate_body(state, stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
|
|
|
|
validate_arguments(state, stmt->v.AsyncFunctionDef.args) &&
|
|
|
|
validate_exprs(state, stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
|
2015-05-12 10:57:16 +08:00
|
|
|
(!stmt->v.AsyncFunctionDef.returns ||
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_expr(state, stmt->v.AsyncFunctionDef.returns, Load));
|
|
|
|
break;
|
2011-08-10 05:15:04 +08:00
|
|
|
case Pass_kind:
|
|
|
|
case Break_kind:
|
|
|
|
case Continue_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
ret = 1;
|
|
|
|
break;
|
2021-04-29 13:58:44 +08:00
|
|
|
// No default case so compiler emits warning for unhandled cases
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2011-08-10 05:15:04 +08:00
|
|
|
PyErr_SetString(PyExc_SystemError, "unexpected statement");
|
2021-04-29 13:58:44 +08:00
|
|
|
ret = 0;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
state->recursion_depth--;
|
|
|
|
return ret;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_stmts(struct validator *state, asdl_stmt_seq *seq)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2011-08-10 05:15:04 +08:00
|
|
|
for (i = 0; i < asdl_seq_LEN(seq); i++) {
|
|
|
|
stmt_ty stmt = asdl_seq_GET(seq, i);
|
|
|
|
if (stmt) {
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_stmt(state, stmt))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"None disallowed in statement list");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-04-25 18:38:00 +08:00
|
|
|
validate_exprs(struct validator *state, asdl_expr_seq *exprs, expr_context_ty ctx, int null_ok)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2018-11-22 21:45:16 +08:00
|
|
|
Py_ssize_t i;
|
2011-08-10 05:15:04 +08:00
|
|
|
for (i = 0; i < asdl_seq_LEN(exprs); i++) {
|
|
|
|
expr_ty expr = asdl_seq_GET(exprs, i);
|
|
|
|
if (expr) {
|
2021-04-25 18:38:00 +08:00
|
|
|
if (!validate_expr(state, expr, ctx))
|
2011-08-10 05:15:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (!null_ok) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"None disallowed in expression list");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-18 22:02:06 +08:00
|
|
|
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-25 18:38:00 +08:00
|
|
|
/* See comments in symtable.c. */
|
|
|
|
#define COMPILER_STACK_FRAME_SCALE 3
|
|
|
|
|
2011-08-10 05:15:04 +08:00
|
|
|
int
|
2021-03-18 21:57:49 +08:00
|
|
|
_PyAST_Validate(mod_ty mod)
|
2011-08-10 05:15:04 +08:00
|
|
|
{
|
2021-04-29 13:58:44 +08:00
|
|
|
int res = -1;
|
2021-04-25 18:38:00 +08:00
|
|
|
struct validator state;
|
|
|
|
PyThreadState *tstate;
|
|
|
|
int recursion_limit = Py_GetRecursionLimit();
|
|
|
|
int starting_recursion_depth;
|
|
|
|
|
|
|
|
/* Setup recursion depth check counters */
|
|
|
|
tstate = _PyThreadState_GET();
|
|
|
|
if (!tstate) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Be careful here to prevent overflow. */
|
|
|
|
starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
|
|
|
|
tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
|
|
|
|
state.recursion_depth = starting_recursion_depth;
|
|
|
|
state.recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
|
|
|
|
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
|
2011-08-10 05:15:04 +08:00
|
|
|
|
|
|
|
switch (mod->kind) {
|
|
|
|
case Module_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
res = validate_stmts(&state, mod->v.Module.body);
|
2011-08-10 05:15:04 +08:00
|
|
|
break;
|
|
|
|
case Interactive_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
res = validate_stmts(&state, mod->v.Interactive.body);
|
2011-08-10 05:15:04 +08:00
|
|
|
break;
|
|
|
|
case Expression_kind:
|
2021-04-25 18:38:00 +08:00
|
|
|
res = validate_expr(&state, mod->v.Expression.body, Load);
|
2011-08-10 05:15:04 +08:00
|
|
|
break;
|
2021-04-29 13:58:44 +08:00
|
|
|
case FunctionType_kind:
|
|
|
|
res = validate_exprs(&state, mod->v.FunctionType.argtypes, Load, /*null_ok=*/0) &&
|
|
|
|
validate_expr(&state, mod->v.FunctionType.returns, Load);
|
2011-08-10 05:15:04 +08:00
|
|
|
break;
|
2021-04-29 13:58:44 +08:00
|
|
|
// No default case so compiler emits warning for unhandled cases
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "impossible module node");
|
|
|
|
return 0;
|
2011-08-10 05:15:04 +08:00
|
|
|
}
|
2021-04-25 18:38:00 +08:00
|
|
|
|
|
|
|
/* Check that the recursion depth counting balanced correctly */
|
|
|
|
if (res && state.recursion_depth != starting_recursion_depth) {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"AST validator recursion depth mismatch (before=%d, after=%d)",
|
|
|
|
starting_recursion_depth, state.recursion_depth);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-10 05:15:04 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-05-30 15:56:16 +08:00
|
|
|
PyObject *
|
2020-09-17 02:42:00 +08:00
|
|
|
_PyAST_GetDocString(asdl_stmt_seq *body)
|
2018-05-30 15:56:16 +08:00
|
|
|
{
|
|
|
|
if (!asdl_seq_LEN(body)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-11 01:14:59 +08:00
|
|
|
stmt_ty st = asdl_seq_GET(body, 0);
|
2018-05-30 15:56:16 +08:00
|
|
|
if (st->kind != Expr_kind) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
expr_ty e = st->v.Expr.value;
|
|
|
|
if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
|
|
|
|
return e->v.Constant.value;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|