ast.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * This file exposes PyAST_Validate interface to check the integrity
  3. * of the given abstract syntax tree (potentially constructed manually).
  4. */
  5. #include "Python.h"
  6. #include "pycore_ast.h" // asdl_stmt_seq
  7. #include "pycore_pystate.h" // _PyThreadState_GET()
  8. #include <assert.h>
  9. #include <stdbool.h>
  10. struct validator {
  11. int recursion_depth; /* current recursion depth */
  12. int recursion_limit; /* recursion limit */
  13. };
  14. static int validate_stmts(struct validator *, asdl_stmt_seq *);
  15. static int validate_exprs(struct validator *, asdl_expr_seq *, expr_context_ty, int);
  16. static int validate_patterns(struct validator *, asdl_pattern_seq *, int);
  17. static int validate_type_params(struct validator *, asdl_type_param_seq *);
  18. static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
  19. static int validate_stmt(struct validator *, stmt_ty);
  20. static int validate_expr(struct validator *, expr_ty, expr_context_ty);
  21. static int validate_pattern(struct validator *, pattern_ty, int);
  22. static int validate_typeparam(struct validator *, type_param_ty);
  23. #define VALIDATE_POSITIONS(node) \
  24. if (node->lineno > node->end_lineno) { \
  25. PyErr_Format(PyExc_ValueError, \
  26. "AST node line range (%d, %d) is not valid", \
  27. node->lineno, node->end_lineno); \
  28. return 0; \
  29. } \
  30. if ((node->lineno < 0 && node->end_lineno != node->lineno) || \
  31. (node->col_offset < 0 && node->col_offset != node->end_col_offset)) { \
  32. PyErr_Format(PyExc_ValueError, \
  33. "AST node column range (%d, %d) for line range (%d, %d) is not valid", \
  34. node->col_offset, node->end_col_offset, node->lineno, node->end_lineno); \
  35. return 0; \
  36. } \
  37. if (node->lineno == node->end_lineno && node->col_offset > node->end_col_offset) { \
  38. PyErr_Format(PyExc_ValueError, \
  39. "line %d, column %d-%d is not a valid range", \
  40. node->lineno, node->col_offset, node->end_col_offset); \
  41. return 0; \
  42. }
  43. static int
  44. validate_name(PyObject *name)
  45. {
  46. assert(!PyErr_Occurred());
  47. assert(PyUnicode_Check(name));
  48. static const char * const forbidden[] = {
  49. "None",
  50. "True",
  51. "False",
  52. NULL
  53. };
  54. for (int i = 0; forbidden[i] != NULL; i++) {
  55. if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
  56. PyErr_Format(PyExc_ValueError, "identifier field can't represent '%s' constant", forbidden[i]);
  57. return 0;
  58. }
  59. }
  60. return 1;
  61. }
  62. static int
  63. validate_comprehension(struct validator *state, asdl_comprehension_seq *gens)
  64. {
  65. assert(!PyErr_Occurred());
  66. if (!asdl_seq_LEN(gens)) {
  67. PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
  68. return 0;
  69. }
  70. for (Py_ssize_t i = 0; i < asdl_seq_LEN(gens); i++) {
  71. comprehension_ty comp = asdl_seq_GET(gens, i);
  72. if (!validate_expr(state, comp->target, Store) ||
  73. !validate_expr(state, comp->iter, Load) ||
  74. !validate_exprs(state, comp->ifs, Load, 0))
  75. return 0;
  76. }
  77. return 1;
  78. }
  79. static int
  80. validate_keywords(struct validator *state, asdl_keyword_seq *keywords)
  81. {
  82. assert(!PyErr_Occurred());
  83. for (Py_ssize_t i = 0; i < asdl_seq_LEN(keywords); i++)
  84. if (!validate_expr(state, (asdl_seq_GET(keywords, i))->value, Load))
  85. return 0;
  86. return 1;
  87. }
  88. static int
  89. validate_args(struct validator *state, asdl_arg_seq *args)
  90. {
  91. assert(!PyErr_Occurred());
  92. for (Py_ssize_t i = 0; i < asdl_seq_LEN(args); i++) {
  93. arg_ty arg = asdl_seq_GET(args, i);
  94. VALIDATE_POSITIONS(arg);
  95. if (arg->annotation && !validate_expr(state, arg->annotation, Load))
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. static const char *
  101. expr_context_name(expr_context_ty ctx)
  102. {
  103. switch (ctx) {
  104. case Load:
  105. return "Load";
  106. case Store:
  107. return "Store";
  108. case Del:
  109. return "Del";
  110. // No default case so compiler emits warning for unhandled cases
  111. }
  112. Py_UNREACHABLE();
  113. }
  114. static int
  115. validate_arguments(struct validator *state, arguments_ty args)
  116. {
  117. assert(!PyErr_Occurred());
  118. if (!validate_args(state, args->posonlyargs) || !validate_args(state, args->args)) {
  119. return 0;
  120. }
  121. if (args->vararg && args->vararg->annotation
  122. && !validate_expr(state, args->vararg->annotation, Load)) {
  123. return 0;
  124. }
  125. if (!validate_args(state, args->kwonlyargs))
  126. return 0;
  127. if (args->kwarg && args->kwarg->annotation
  128. && !validate_expr(state, args->kwarg->annotation, Load)) {
  129. return 0;
  130. }
  131. if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
  132. PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
  133. return 0;
  134. }
  135. if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
  136. PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
  137. "kw_defaults on arguments");
  138. return 0;
  139. }
  140. return validate_exprs(state, args->defaults, Load, 0) && validate_exprs(state, args->kw_defaults, Load, 1);
  141. }
  142. static int
  143. validate_constant(struct validator *state, PyObject *value)
  144. {
  145. assert(!PyErr_Occurred());
  146. if (value == Py_None || value == Py_Ellipsis)
  147. return 1;
  148. if (PyLong_CheckExact(value)
  149. || PyFloat_CheckExact(value)
  150. || PyComplex_CheckExact(value)
  151. || PyBool_Check(value)
  152. || PyUnicode_CheckExact(value)
  153. || PyBytes_CheckExact(value))
  154. return 1;
  155. if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
  156. if (++state->recursion_depth > state->recursion_limit) {
  157. PyErr_SetString(PyExc_RecursionError,
  158. "maximum recursion depth exceeded during compilation");
  159. return 0;
  160. }
  161. PyObject *it = PyObject_GetIter(value);
  162. if (it == NULL)
  163. return 0;
  164. while (1) {
  165. PyObject *item = PyIter_Next(it);
  166. if (item == NULL) {
  167. if (PyErr_Occurred()) {
  168. Py_DECREF(it);
  169. return 0;
  170. }
  171. break;
  172. }
  173. if (!validate_constant(state, item)) {
  174. Py_DECREF(it);
  175. Py_DECREF(item);
  176. return 0;
  177. }
  178. Py_DECREF(item);
  179. }
  180. Py_DECREF(it);
  181. --state->recursion_depth;
  182. return 1;
  183. }
  184. if (!PyErr_Occurred()) {
  185. PyErr_Format(PyExc_TypeError,
  186. "got an invalid type in Constant: %s",
  187. _PyType_Name(Py_TYPE(value)));
  188. }
  189. return 0;
  190. }
  191. static int
  192. validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
  193. {
  194. assert(!PyErr_Occurred());
  195. VALIDATE_POSITIONS(exp);
  196. int ret = -1;
  197. if (++state->recursion_depth > state->recursion_limit) {
  198. PyErr_SetString(PyExc_RecursionError,
  199. "maximum recursion depth exceeded during compilation");
  200. return 0;
  201. }
  202. int check_ctx = 1;
  203. expr_context_ty actual_ctx;
  204. /* First check expression context. */
  205. switch (exp->kind) {
  206. case Attribute_kind:
  207. actual_ctx = exp->v.Attribute.ctx;
  208. break;
  209. case Subscript_kind:
  210. actual_ctx = exp->v.Subscript.ctx;
  211. break;
  212. case Starred_kind:
  213. actual_ctx = exp->v.Starred.ctx;
  214. break;
  215. case Name_kind:
  216. if (!validate_name(exp->v.Name.id)) {
  217. return 0;
  218. }
  219. actual_ctx = exp->v.Name.ctx;
  220. break;
  221. case List_kind:
  222. actual_ctx = exp->v.List.ctx;
  223. break;
  224. case Tuple_kind:
  225. actual_ctx = exp->v.Tuple.ctx;
  226. break;
  227. default:
  228. if (ctx != Load) {
  229. PyErr_Format(PyExc_ValueError, "expression which can't be "
  230. "assigned to in %s context", expr_context_name(ctx));
  231. return 0;
  232. }
  233. check_ctx = 0;
  234. /* set actual_ctx to prevent gcc warning */
  235. actual_ctx = 0;
  236. }
  237. if (check_ctx && actual_ctx != ctx) {
  238. PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
  239. expr_context_name(ctx), expr_context_name(actual_ctx));
  240. return 0;
  241. }
  242. /* Now validate expression. */
  243. switch (exp->kind) {
  244. case BoolOp_kind:
  245. if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
  246. PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
  247. return 0;
  248. }
  249. ret = validate_exprs(state, exp->v.BoolOp.values, Load, 0);
  250. break;
  251. case BinOp_kind:
  252. ret = validate_expr(state, exp->v.BinOp.left, Load) &&
  253. validate_expr(state, exp->v.BinOp.right, Load);
  254. break;
  255. case UnaryOp_kind:
  256. ret = validate_expr(state, exp->v.UnaryOp.operand, Load);
  257. break;
  258. case Lambda_kind:
  259. ret = validate_arguments(state, exp->v.Lambda.args) &&
  260. validate_expr(state, exp->v.Lambda.body, Load);
  261. break;
  262. case IfExp_kind:
  263. ret = validate_expr(state, exp->v.IfExp.test, Load) &&
  264. validate_expr(state, exp->v.IfExp.body, Load) &&
  265. validate_expr(state, exp->v.IfExp.orelse, Load);
  266. break;
  267. case Dict_kind:
  268. if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
  269. PyErr_SetString(PyExc_ValueError,
  270. "Dict doesn't have the same number of keys as values");
  271. return 0;
  272. }
  273. /* null_ok=1 for keys expressions to allow dict unpacking to work in
  274. dict literals, i.e. ``{**{a:b}}`` */
  275. ret = validate_exprs(state, exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
  276. validate_exprs(state, exp->v.Dict.values, Load, /*null_ok=*/ 0);
  277. break;
  278. case Set_kind:
  279. ret = validate_exprs(state, exp->v.Set.elts, Load, 0);
  280. break;
  281. #define COMP(NAME) \
  282. case NAME ## _kind: \
  283. ret = validate_comprehension(state, exp->v.NAME.generators) && \
  284. validate_expr(state, exp->v.NAME.elt, Load); \
  285. break;
  286. COMP(ListComp)
  287. COMP(SetComp)
  288. COMP(GeneratorExp)
  289. #undef COMP
  290. case DictComp_kind:
  291. ret = validate_comprehension(state, exp->v.DictComp.generators) &&
  292. validate_expr(state, exp->v.DictComp.key, Load) &&
  293. validate_expr(state, exp->v.DictComp.value, Load);
  294. break;
  295. case Yield_kind:
  296. ret = !exp->v.Yield.value || validate_expr(state, exp->v.Yield.value, Load);
  297. break;
  298. case YieldFrom_kind:
  299. ret = validate_expr(state, exp->v.YieldFrom.value, Load);
  300. break;
  301. case Await_kind:
  302. ret = validate_expr(state, exp->v.Await.value, Load);
  303. break;
  304. case Compare_kind:
  305. if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
  306. PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
  307. return 0;
  308. }
  309. if (asdl_seq_LEN(exp->v.Compare.comparators) !=
  310. asdl_seq_LEN(exp->v.Compare.ops)) {
  311. PyErr_SetString(PyExc_ValueError, "Compare has a different number "
  312. "of comparators and operands");
  313. return 0;
  314. }
  315. ret = validate_exprs(state, exp->v.Compare.comparators, Load, 0) &&
  316. validate_expr(state, exp->v.Compare.left, Load);
  317. break;
  318. case Call_kind:
  319. ret = validate_expr(state, exp->v.Call.func, Load) &&
  320. validate_exprs(state, exp->v.Call.args, Load, 0) &&
  321. validate_keywords(state, exp->v.Call.keywords);
  322. break;
  323. case Constant_kind:
  324. if (!validate_constant(state, exp->v.Constant.value)) {
  325. return 0;
  326. }
  327. ret = 1;
  328. break;
  329. case JoinedStr_kind:
  330. ret = validate_exprs(state, exp->v.JoinedStr.values, Load, 0);
  331. break;
  332. case FormattedValue_kind:
  333. if (validate_expr(state, exp->v.FormattedValue.value, Load) == 0)
  334. return 0;
  335. if (exp->v.FormattedValue.format_spec) {
  336. ret = validate_expr(state, exp->v.FormattedValue.format_spec, Load);
  337. break;
  338. }
  339. ret = 1;
  340. break;
  341. case Attribute_kind:
  342. ret = validate_expr(state, exp->v.Attribute.value, Load);
  343. break;
  344. case Subscript_kind:
  345. ret = validate_expr(state, exp->v.Subscript.slice, Load) &&
  346. validate_expr(state, exp->v.Subscript.value, Load);
  347. break;
  348. case Starred_kind:
  349. ret = validate_expr(state, exp->v.Starred.value, ctx);
  350. break;
  351. case Slice_kind:
  352. ret = (!exp->v.Slice.lower || validate_expr(state, exp->v.Slice.lower, Load)) &&
  353. (!exp->v.Slice.upper || validate_expr(state, exp->v.Slice.upper, Load)) &&
  354. (!exp->v.Slice.step || validate_expr(state, exp->v.Slice.step, Load));
  355. break;
  356. case List_kind:
  357. ret = validate_exprs(state, exp->v.List.elts, ctx, 0);
  358. break;
  359. case Tuple_kind:
  360. ret = validate_exprs(state, exp->v.Tuple.elts, ctx, 0);
  361. break;
  362. case NamedExpr_kind:
  363. if (exp->v.NamedExpr.target->kind != Name_kind) {
  364. PyErr_SetString(PyExc_TypeError,
  365. "NamedExpr target must be a Name");
  366. return 0;
  367. }
  368. ret = validate_expr(state, exp->v.NamedExpr.value, Load);
  369. break;
  370. /* This last case doesn't have any checking. */
  371. case Name_kind:
  372. ret = 1;
  373. break;
  374. // No default case so compiler emits warning for unhandled cases
  375. }
  376. if (ret < 0) {
  377. PyErr_SetString(PyExc_SystemError, "unexpected expression");
  378. ret = 0;
  379. }
  380. state->recursion_depth--;
  381. return ret;
  382. }
  383. // Note: the ensure_literal_* functions are only used to validate a restricted
  384. // set of non-recursive literals that have already been checked with
  385. // validate_expr, so they don't accept the validator state
  386. static int
  387. ensure_literal_number(expr_ty exp, bool allow_real, bool allow_imaginary)
  388. {
  389. assert(exp->kind == Constant_kind);
  390. PyObject *value = exp->v.Constant.value;
  391. return (allow_real && PyFloat_CheckExact(value)) ||
  392. (allow_real && PyLong_CheckExact(value)) ||
  393. (allow_imaginary && PyComplex_CheckExact(value));
  394. }
  395. static int
  396. ensure_literal_negative(expr_ty exp, bool allow_real, bool allow_imaginary)
  397. {
  398. assert(exp->kind == UnaryOp_kind);
  399. // Must be negation ...
  400. if (exp->v.UnaryOp.op != USub) {
  401. return 0;
  402. }
  403. // ... of a constant ...
  404. expr_ty operand = exp->v.UnaryOp.operand;
  405. if (operand->kind != Constant_kind) {
  406. return 0;
  407. }
  408. // ... number
  409. return ensure_literal_number(operand, allow_real, allow_imaginary);
  410. }
  411. static int
  412. ensure_literal_complex(expr_ty exp)
  413. {
  414. assert(exp->kind == BinOp_kind);
  415. expr_ty left = exp->v.BinOp.left;
  416. expr_ty right = exp->v.BinOp.right;
  417. // Ensure op is addition or subtraction
  418. if (exp->v.BinOp.op != Add && exp->v.BinOp.op != Sub) {
  419. return 0;
  420. }
  421. // Check LHS is a real number (potentially signed)
  422. switch (left->kind)
  423. {
  424. case Constant_kind:
  425. if (!ensure_literal_number(left, /*real=*/true, /*imaginary=*/false)) {
  426. return 0;
  427. }
  428. break;
  429. case UnaryOp_kind:
  430. if (!ensure_literal_negative(left, /*real=*/true, /*imaginary=*/false)) {
  431. return 0;
  432. }
  433. break;
  434. default:
  435. return 0;
  436. }
  437. // Check RHS is an imaginary number (no separate sign allowed)
  438. switch (right->kind)
  439. {
  440. case Constant_kind:
  441. if (!ensure_literal_number(right, /*real=*/false, /*imaginary=*/true)) {
  442. return 0;
  443. }
  444. break;
  445. default:
  446. return 0;
  447. }
  448. return 1;
  449. }
  450. static int
  451. validate_pattern_match_value(struct validator *state, expr_ty exp)
  452. {
  453. assert(!PyErr_Occurred());
  454. if (!validate_expr(state, exp, Load)) {
  455. return 0;
  456. }
  457. switch (exp->kind)
  458. {
  459. case Constant_kind:
  460. /* Ellipsis and immutable sequences are not allowed.
  461. For True, False and None, MatchSingleton() should
  462. be used */
  463. if (!validate_expr(state, exp, Load)) {
  464. return 0;
  465. }
  466. PyObject *literal = exp->v.Constant.value;
  467. if (PyLong_CheckExact(literal) || PyFloat_CheckExact(literal) ||
  468. PyBytes_CheckExact(literal) || PyComplex_CheckExact(literal) ||
  469. PyUnicode_CheckExact(literal)) {
  470. return 1;
  471. }
  472. PyErr_SetString(PyExc_ValueError,
  473. "unexpected constant inside of a literal pattern");
  474. return 0;
  475. case Attribute_kind:
  476. // Constants and attribute lookups are always permitted
  477. return 1;
  478. case UnaryOp_kind:
  479. // Negated numbers are permitted (whether real or imaginary)
  480. // Compiler will complain if AST folding doesn't create a constant
  481. if (ensure_literal_negative(exp, /*real=*/true, /*imaginary=*/true)) {
  482. return 1;
  483. }
  484. break;
  485. case BinOp_kind:
  486. // Complex literals are permitted
  487. // Compiler will complain if AST folding doesn't create a constant
  488. if (ensure_literal_complex(exp)) {
  489. return 1;
  490. }
  491. break;
  492. case JoinedStr_kind:
  493. // Handled in the later stages
  494. return 1;
  495. default:
  496. break;
  497. }
  498. PyErr_SetString(PyExc_ValueError,
  499. "patterns may only match literals and attribute lookups");
  500. return 0;
  501. }
  502. static int
  503. validate_capture(PyObject *name)
  504. {
  505. assert(!PyErr_Occurred());
  506. if (_PyUnicode_EqualToASCIIString(name, "_")) {
  507. PyErr_Format(PyExc_ValueError, "can't capture name '_' in patterns");
  508. return 0;
  509. }
  510. return validate_name(name);
  511. }
  512. static int
  513. validate_pattern(struct validator *state, pattern_ty p, int star_ok)
  514. {
  515. assert(!PyErr_Occurred());
  516. VALIDATE_POSITIONS(p);
  517. int ret = -1;
  518. if (++state->recursion_depth > state->recursion_limit) {
  519. PyErr_SetString(PyExc_RecursionError,
  520. "maximum recursion depth exceeded during compilation");
  521. return 0;
  522. }
  523. switch (p->kind) {
  524. case MatchValue_kind:
  525. ret = validate_pattern_match_value(state, p->v.MatchValue.value);
  526. break;
  527. case MatchSingleton_kind:
  528. ret = p->v.MatchSingleton.value == Py_None || PyBool_Check(p->v.MatchSingleton.value);
  529. if (!ret) {
  530. PyErr_SetString(PyExc_ValueError,
  531. "MatchSingleton can only contain True, False and None");
  532. }
  533. break;
  534. case MatchSequence_kind:
  535. ret = validate_patterns(state, p->v.MatchSequence.patterns, /*star_ok=*/1);
  536. break;
  537. case MatchMapping_kind:
  538. if (asdl_seq_LEN(p->v.MatchMapping.keys) != asdl_seq_LEN(p->v.MatchMapping.patterns)) {
  539. PyErr_SetString(PyExc_ValueError,
  540. "MatchMapping doesn't have the same number of keys as patterns");
  541. ret = 0;
  542. break;
  543. }
  544. if (p->v.MatchMapping.rest && !validate_capture(p->v.MatchMapping.rest)) {
  545. ret = 0;
  546. break;
  547. }
  548. asdl_expr_seq *keys = p->v.MatchMapping.keys;
  549. for (Py_ssize_t i = 0; i < asdl_seq_LEN(keys); i++) {
  550. expr_ty key = asdl_seq_GET(keys, i);
  551. if (key->kind == Constant_kind) {
  552. PyObject *literal = key->v.Constant.value;
  553. if (literal == Py_None || PyBool_Check(literal)) {
  554. /* validate_pattern_match_value will ensure the key
  555. doesn't contain True, False and None but it is
  556. syntactically valid, so we will pass those on in
  557. a special case. */
  558. continue;
  559. }
  560. }
  561. if (!validate_pattern_match_value(state, key)) {
  562. ret = 0;
  563. break;
  564. }
  565. }
  566. if (ret == 0) {
  567. break;
  568. }
  569. ret = validate_patterns(state, p->v.MatchMapping.patterns, /*star_ok=*/0);
  570. break;
  571. case MatchClass_kind:
  572. if (asdl_seq_LEN(p->v.MatchClass.kwd_attrs) != asdl_seq_LEN(p->v.MatchClass.kwd_patterns)) {
  573. PyErr_SetString(PyExc_ValueError,
  574. "MatchClass doesn't have the same number of keyword attributes as patterns");
  575. ret = 0;
  576. break;
  577. }
  578. if (!validate_expr(state, p->v.MatchClass.cls, Load)) {
  579. ret = 0;
  580. break;
  581. }
  582. expr_ty cls = p->v.MatchClass.cls;
  583. while (1) {
  584. if (cls->kind == Name_kind) {
  585. break;
  586. }
  587. else if (cls->kind == Attribute_kind) {
  588. cls = cls->v.Attribute.value;
  589. continue;
  590. }
  591. else {
  592. PyErr_SetString(PyExc_ValueError,
  593. "MatchClass cls field can only contain Name or Attribute nodes.");
  594. ret = 0;
  595. break;
  596. }
  597. }
  598. if (ret == 0) {
  599. break;
  600. }
  601. for (Py_ssize_t i = 0; i < asdl_seq_LEN(p->v.MatchClass.kwd_attrs); i++) {
  602. PyObject *identifier = asdl_seq_GET(p->v.MatchClass.kwd_attrs, i);
  603. if (!validate_name(identifier)) {
  604. ret = 0;
  605. break;
  606. }
  607. }
  608. if (ret == 0) {
  609. break;
  610. }
  611. if (!validate_patterns(state, p->v.MatchClass.patterns, /*star_ok=*/0)) {
  612. ret = 0;
  613. break;
  614. }
  615. ret = validate_patterns(state, p->v.MatchClass.kwd_patterns, /*star_ok=*/0);
  616. break;
  617. case MatchStar_kind:
  618. if (!star_ok) {
  619. PyErr_SetString(PyExc_ValueError, "can't use MatchStar here");
  620. ret = 0;
  621. break;
  622. }
  623. ret = p->v.MatchStar.name == NULL || validate_capture(p->v.MatchStar.name);
  624. break;
  625. case MatchAs_kind:
  626. if (p->v.MatchAs.name && !validate_capture(p->v.MatchAs.name)) {
  627. ret = 0;
  628. break;
  629. }
  630. if (p->v.MatchAs.pattern == NULL) {
  631. ret = 1;
  632. }
  633. else if (p->v.MatchAs.name == NULL) {
  634. PyErr_SetString(PyExc_ValueError,
  635. "MatchAs must specify a target name if a pattern is given");
  636. ret = 0;
  637. }
  638. else {
  639. ret = validate_pattern(state, p->v.MatchAs.pattern, /*star_ok=*/0);
  640. }
  641. break;
  642. case MatchOr_kind:
  643. if (asdl_seq_LEN(p->v.MatchOr.patterns) < 2) {
  644. PyErr_SetString(PyExc_ValueError,
  645. "MatchOr requires at least 2 patterns");
  646. ret = 0;
  647. break;
  648. }
  649. ret = validate_patterns(state, p->v.MatchOr.patterns, /*star_ok=*/0);
  650. break;
  651. // No default case, so the compiler will emit a warning if new pattern
  652. // kinds are added without being handled here
  653. }
  654. if (ret < 0) {
  655. PyErr_SetString(PyExc_SystemError, "unexpected pattern");
  656. ret = 0;
  657. }
  658. state->recursion_depth--;
  659. return ret;
  660. }
  661. static int
  662. _validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
  663. {
  664. if (asdl_seq_LEN(seq))
  665. return 1;
  666. PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
  667. return 0;
  668. }
  669. #define validate_nonempty_seq(seq, what, owner) _validate_nonempty_seq((asdl_seq*)seq, what, owner)
  670. static int
  671. validate_assignlist(struct validator *state, asdl_expr_seq *targets, expr_context_ty ctx)
  672. {
  673. assert(!PyErr_Occurred());
  674. return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
  675. validate_exprs(state, targets, ctx, 0);
  676. }
  677. static int
  678. validate_body(struct validator *state, asdl_stmt_seq *body, const char *owner)
  679. {
  680. assert(!PyErr_Occurred());
  681. return validate_nonempty_seq(body, "body", owner) && validate_stmts(state, body);
  682. }
  683. static int
  684. validate_stmt(struct validator *state, stmt_ty stmt)
  685. {
  686. assert(!PyErr_Occurred());
  687. VALIDATE_POSITIONS(stmt);
  688. int ret = -1;
  689. if (++state->recursion_depth > state->recursion_limit) {
  690. PyErr_SetString(PyExc_RecursionError,
  691. "maximum recursion depth exceeded during compilation");
  692. return 0;
  693. }
  694. switch (stmt->kind) {
  695. case FunctionDef_kind:
  696. ret = validate_body(state, stmt->v.FunctionDef.body, "FunctionDef") &&
  697. validate_type_params(state, stmt->v.FunctionDef.type_params) &&
  698. validate_arguments(state, stmt->v.FunctionDef.args) &&
  699. validate_exprs(state, stmt->v.FunctionDef.decorator_list, Load, 0) &&
  700. (!stmt->v.FunctionDef.returns ||
  701. validate_expr(state, stmt->v.FunctionDef.returns, Load));
  702. break;
  703. case ClassDef_kind:
  704. ret = validate_body(state, stmt->v.ClassDef.body, "ClassDef") &&
  705. validate_type_params(state, stmt->v.ClassDef.type_params) &&
  706. validate_exprs(state, stmt->v.ClassDef.bases, Load, 0) &&
  707. validate_keywords(state, stmt->v.ClassDef.keywords) &&
  708. validate_exprs(state, stmt->v.ClassDef.decorator_list, Load, 0);
  709. break;
  710. case Return_kind:
  711. ret = !stmt->v.Return.value || validate_expr(state, stmt->v.Return.value, Load);
  712. break;
  713. case Delete_kind:
  714. ret = validate_assignlist(state, stmt->v.Delete.targets, Del);
  715. break;
  716. case Assign_kind:
  717. ret = validate_assignlist(state, stmt->v.Assign.targets, Store) &&
  718. validate_expr(state, stmt->v.Assign.value, Load);
  719. break;
  720. case AugAssign_kind:
  721. ret = validate_expr(state, stmt->v.AugAssign.target, Store) &&
  722. validate_expr(state, stmt->v.AugAssign.value, Load);
  723. break;
  724. case AnnAssign_kind:
  725. if (stmt->v.AnnAssign.target->kind != Name_kind &&
  726. stmt->v.AnnAssign.simple) {
  727. PyErr_SetString(PyExc_TypeError,
  728. "AnnAssign with simple non-Name target");
  729. return 0;
  730. }
  731. ret = validate_expr(state, stmt->v.AnnAssign.target, Store) &&
  732. (!stmt->v.AnnAssign.value ||
  733. validate_expr(state, stmt->v.AnnAssign.value, Load)) &&
  734. validate_expr(state, stmt->v.AnnAssign.annotation, Load);
  735. break;
  736. case TypeAlias_kind:
  737. if (stmt->v.TypeAlias.name->kind != Name_kind) {
  738. PyErr_SetString(PyExc_TypeError,
  739. "TypeAlias with non-Name name");
  740. return 0;
  741. }
  742. ret = validate_expr(state, stmt->v.TypeAlias.name, Store) &&
  743. validate_type_params(state, stmt->v.TypeAlias.type_params) &&
  744. validate_expr(state, stmt->v.TypeAlias.value, Load);
  745. break;
  746. case For_kind:
  747. ret = validate_expr(state, stmt->v.For.target, Store) &&
  748. validate_expr(state, stmt->v.For.iter, Load) &&
  749. validate_body(state, stmt->v.For.body, "For") &&
  750. validate_stmts(state, stmt->v.For.orelse);
  751. break;
  752. case AsyncFor_kind:
  753. ret = validate_expr(state, stmt->v.AsyncFor.target, Store) &&
  754. validate_expr(state, stmt->v.AsyncFor.iter, Load) &&
  755. validate_body(state, stmt->v.AsyncFor.body, "AsyncFor") &&
  756. validate_stmts(state, stmt->v.AsyncFor.orelse);
  757. break;
  758. case While_kind:
  759. ret = validate_expr(state, stmt->v.While.test, Load) &&
  760. validate_body(state, stmt->v.While.body, "While") &&
  761. validate_stmts(state, stmt->v.While.orelse);
  762. break;
  763. case If_kind:
  764. ret = validate_expr(state, stmt->v.If.test, Load) &&
  765. validate_body(state, stmt->v.If.body, "If") &&
  766. validate_stmts(state, stmt->v.If.orelse);
  767. break;
  768. case With_kind:
  769. if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
  770. return 0;
  771. for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
  772. withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
  773. if (!validate_expr(state, item->context_expr, Load) ||
  774. (item->optional_vars && !validate_expr(state, item->optional_vars, Store)))
  775. return 0;
  776. }
  777. ret = validate_body(state, stmt->v.With.body, "With");
  778. break;
  779. case AsyncWith_kind:
  780. if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
  781. return 0;
  782. for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
  783. withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
  784. if (!validate_expr(state, item->context_expr, Load) ||
  785. (item->optional_vars && !validate_expr(state, item->optional_vars, Store)))
  786. return 0;
  787. }
  788. ret = validate_body(state, stmt->v.AsyncWith.body, "AsyncWith");
  789. break;
  790. case Match_kind:
  791. if (!validate_expr(state, stmt->v.Match.subject, Load)
  792. || !validate_nonempty_seq(stmt->v.Match.cases, "cases", "Match")) {
  793. return 0;
  794. }
  795. for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.Match.cases); i++) {
  796. match_case_ty m = asdl_seq_GET(stmt->v.Match.cases, i);
  797. if (!validate_pattern(state, m->pattern, /*star_ok=*/0)
  798. || (m->guard && !validate_expr(state, m->guard, Load))
  799. || !validate_body(state, m->body, "match_case")) {
  800. return 0;
  801. }
  802. }
  803. ret = 1;
  804. break;
  805. case Raise_kind:
  806. if (stmt->v.Raise.exc) {
  807. ret = validate_expr(state, stmt->v.Raise.exc, Load) &&
  808. (!stmt->v.Raise.cause || validate_expr(state, stmt->v.Raise.cause, Load));
  809. break;
  810. }
  811. if (stmt->v.Raise.cause) {
  812. PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
  813. return 0;
  814. }
  815. ret = 1;
  816. break;
  817. case Try_kind:
  818. if (!validate_body(state, stmt->v.Try.body, "Try"))
  819. return 0;
  820. if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
  821. !asdl_seq_LEN(stmt->v.Try.finalbody)) {
  822. PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
  823. return 0;
  824. }
  825. if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
  826. asdl_seq_LEN(stmt->v.Try.orelse)) {
  827. PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
  828. return 0;
  829. }
  830. for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
  831. excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
  832. VALIDATE_POSITIONS(handler);
  833. if ((handler->v.ExceptHandler.type &&
  834. !validate_expr(state, handler->v.ExceptHandler.type, Load)) ||
  835. !validate_body(state, handler->v.ExceptHandler.body, "ExceptHandler"))
  836. return 0;
  837. }
  838. ret = (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
  839. validate_stmts(state, stmt->v.Try.finalbody)) &&
  840. (!asdl_seq_LEN(stmt->v.Try.orelse) ||
  841. validate_stmts(state, stmt->v.Try.orelse));
  842. break;
  843. case TryStar_kind:
  844. if (!validate_body(state, stmt->v.TryStar.body, "TryStar"))
  845. return 0;
  846. if (!asdl_seq_LEN(stmt->v.TryStar.handlers) &&
  847. !asdl_seq_LEN(stmt->v.TryStar.finalbody)) {
  848. PyErr_SetString(PyExc_ValueError, "TryStar has neither except handlers nor finalbody");
  849. return 0;
  850. }
  851. if (!asdl_seq_LEN(stmt->v.TryStar.handlers) &&
  852. asdl_seq_LEN(stmt->v.TryStar.orelse)) {
  853. PyErr_SetString(PyExc_ValueError, "TryStar has orelse but no except handlers");
  854. return 0;
  855. }
  856. for (Py_ssize_t i = 0; i < asdl_seq_LEN(stmt->v.TryStar.handlers); i++) {
  857. excepthandler_ty handler = asdl_seq_GET(stmt->v.TryStar.handlers, i);
  858. if ((handler->v.ExceptHandler.type &&
  859. !validate_expr(state, handler->v.ExceptHandler.type, Load)) ||
  860. !validate_body(state, handler->v.ExceptHandler.body, "ExceptHandler"))
  861. return 0;
  862. }
  863. ret = (!asdl_seq_LEN(stmt->v.TryStar.finalbody) ||
  864. validate_stmts(state, stmt->v.TryStar.finalbody)) &&
  865. (!asdl_seq_LEN(stmt->v.TryStar.orelse) ||
  866. validate_stmts(state, stmt->v.TryStar.orelse));
  867. break;
  868. case Assert_kind:
  869. ret = validate_expr(state, stmt->v.Assert.test, Load) &&
  870. (!stmt->v.Assert.msg || validate_expr(state, stmt->v.Assert.msg, Load));
  871. break;
  872. case Import_kind:
  873. ret = validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
  874. break;
  875. case ImportFrom_kind:
  876. if (stmt->v.ImportFrom.level < 0) {
  877. PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
  878. return 0;
  879. }
  880. ret = validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
  881. break;
  882. case Global_kind:
  883. ret = validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
  884. break;
  885. case Nonlocal_kind:
  886. ret = validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
  887. break;
  888. case Expr_kind:
  889. ret = validate_expr(state, stmt->v.Expr.value, Load);
  890. break;
  891. case AsyncFunctionDef_kind:
  892. ret = validate_body(state, stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
  893. validate_type_params(state, stmt->v.AsyncFunctionDef.type_params) &&
  894. validate_arguments(state, stmt->v.AsyncFunctionDef.args) &&
  895. validate_exprs(state, stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
  896. (!stmt->v.AsyncFunctionDef.returns ||
  897. validate_expr(state, stmt->v.AsyncFunctionDef.returns, Load));
  898. break;
  899. case Pass_kind:
  900. case Break_kind:
  901. case Continue_kind:
  902. ret = 1;
  903. break;
  904. // No default case so compiler emits warning for unhandled cases
  905. }
  906. if (ret < 0) {
  907. PyErr_SetString(PyExc_SystemError, "unexpected statement");
  908. ret = 0;
  909. }
  910. state->recursion_depth--;
  911. return ret;
  912. }
  913. static int
  914. validate_stmts(struct validator *state, asdl_stmt_seq *seq)
  915. {
  916. assert(!PyErr_Occurred());
  917. for (Py_ssize_t i = 0; i < asdl_seq_LEN(seq); i++) {
  918. stmt_ty stmt = asdl_seq_GET(seq, i);
  919. if (stmt) {
  920. if (!validate_stmt(state, stmt))
  921. return 0;
  922. }
  923. else {
  924. PyErr_SetString(PyExc_ValueError,
  925. "None disallowed in statement list");
  926. return 0;
  927. }
  928. }
  929. return 1;
  930. }
  931. static int
  932. validate_exprs(struct validator *state, asdl_expr_seq *exprs, expr_context_ty ctx, int null_ok)
  933. {
  934. assert(!PyErr_Occurred());
  935. for (Py_ssize_t i = 0; i < asdl_seq_LEN(exprs); i++) {
  936. expr_ty expr = asdl_seq_GET(exprs, i);
  937. if (expr) {
  938. if (!validate_expr(state, expr, ctx))
  939. return 0;
  940. }
  941. else if (!null_ok) {
  942. PyErr_SetString(PyExc_ValueError,
  943. "None disallowed in expression list");
  944. return 0;
  945. }
  946. }
  947. return 1;
  948. }
  949. static int
  950. validate_patterns(struct validator *state, asdl_pattern_seq *patterns, int star_ok)
  951. {
  952. assert(!PyErr_Occurred());
  953. for (Py_ssize_t i = 0; i < asdl_seq_LEN(patterns); i++) {
  954. pattern_ty pattern = asdl_seq_GET(patterns, i);
  955. if (!validate_pattern(state, pattern, star_ok)) {
  956. return 0;
  957. }
  958. }
  959. return 1;
  960. }
  961. static int
  962. validate_typeparam(struct validator *state, type_param_ty tp)
  963. {
  964. VALIDATE_POSITIONS(tp);
  965. int ret = -1;
  966. switch (tp->kind) {
  967. case TypeVar_kind:
  968. ret = validate_name(tp->v.TypeVar.name) &&
  969. (!tp->v.TypeVar.bound ||
  970. validate_expr(state, tp->v.TypeVar.bound, Load));
  971. break;
  972. case ParamSpec_kind:
  973. ret = validate_name(tp->v.ParamSpec.name);
  974. break;
  975. case TypeVarTuple_kind:
  976. ret = validate_name(tp->v.TypeVarTuple.name);
  977. break;
  978. }
  979. return ret;
  980. }
  981. static int
  982. validate_type_params(struct validator *state, asdl_type_param_seq *tps)
  983. {
  984. Py_ssize_t i;
  985. for (i = 0; i < asdl_seq_LEN(tps); i++) {
  986. type_param_ty tp = asdl_seq_GET(tps, i);
  987. if (tp) {
  988. if (!validate_typeparam(state, tp))
  989. return 0;
  990. }
  991. }
  992. return 1;
  993. }
  994. int
  995. _PyAST_Validate(mod_ty mod)
  996. {
  997. assert(!PyErr_Occurred());
  998. int res = -1;
  999. struct validator state;
  1000. PyThreadState *tstate;
  1001. int starting_recursion_depth;
  1002. /* Setup recursion depth check counters */
  1003. tstate = _PyThreadState_GET();
  1004. if (!tstate) {
  1005. return 0;
  1006. }
  1007. /* Be careful here to prevent overflow. */
  1008. int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
  1009. starting_recursion_depth = recursion_depth;
  1010. state.recursion_depth = starting_recursion_depth;
  1011. state.recursion_limit = C_RECURSION_LIMIT;
  1012. switch (mod->kind) {
  1013. case Module_kind:
  1014. res = validate_stmts(&state, mod->v.Module.body);
  1015. break;
  1016. case Interactive_kind:
  1017. res = validate_stmts(&state, mod->v.Interactive.body);
  1018. break;
  1019. case Expression_kind:
  1020. res = validate_expr(&state, mod->v.Expression.body, Load);
  1021. break;
  1022. case FunctionType_kind:
  1023. res = validate_exprs(&state, mod->v.FunctionType.argtypes, Load, /*null_ok=*/0) &&
  1024. validate_expr(&state, mod->v.FunctionType.returns, Load);
  1025. break;
  1026. // No default case so compiler emits warning for unhandled cases
  1027. }
  1028. if (res < 0) {
  1029. PyErr_SetString(PyExc_SystemError, "impossible module node");
  1030. return 0;
  1031. }
  1032. /* Check that the recursion depth counting balanced correctly */
  1033. if (res && state.recursion_depth != starting_recursion_depth) {
  1034. PyErr_Format(PyExc_SystemError,
  1035. "AST validator recursion depth mismatch (before=%d, after=%d)",
  1036. starting_recursion_depth, state.recursion_depth);
  1037. return 0;
  1038. }
  1039. return res;
  1040. }
  1041. PyObject *
  1042. _PyAST_GetDocString(asdl_stmt_seq *body)
  1043. {
  1044. if (!asdl_seq_LEN(body)) {
  1045. return NULL;
  1046. }
  1047. stmt_ty st = asdl_seq_GET(body, 0);
  1048. if (st->kind != Expr_kind) {
  1049. return NULL;
  1050. }
  1051. expr_ty e = st->v.Expr.value;
  1052. if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
  1053. return e->v.Constant.value;
  1054. }
  1055. return NULL;
  1056. }