Skip to content

Commit c0a18a6

Browse files
committed
gh-142462: Ensure kw_defaults is always populated in AST arguments
Normalize AST arguments construction to always allocate kwonlyargs/kw_defaults (with length parity) and assert the invariant.
1 parent 02c085d commit c0a18a6

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Normalize AST arguments construction to always allocate
2+
kwonlyargs/kw_defaults (with length parity) and assert the invariant.

Parser/asdl_c.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,22 @@ def emit(s, depth=0, reflow=True):
437437
emit("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
438438
emit("if (!p)", 1)
439439
emit("return NULL;", 2)
440+
# Special-case normalization for the arguments product to guarantee
441+
# non-NULL kwonlyargs/kw_defaults with matched lengths.
442+
if name == "arguments":
443+
emit("if (kwonlyargs == NULL) {", 1)
444+
emit("kwonlyargs = _Py_asdl_arg_seq_new(0, arena);", 2)
445+
emit("if (!kwonlyargs) {", 2)
446+
emit("return NULL;", 3)
447+
emit("}", 2)
448+
emit("}", 1)
449+
emit("if (kw_defaults == NULL) {", 1)
450+
emit("kw_defaults = _Py_asdl_expr_seq_new(asdl_seq_LEN(kwonlyargs), arena);", 2)
451+
emit("if (!kw_defaults) {", 2)
452+
emit("return NULL;", 3)
453+
emit("}", 2)
454+
emit("}", 1)
455+
emit("assert(asdl_seq_LEN(kw_defaults) == asdl_seq_LEN(kwonlyargs));", 1)
440456
if union:
441457
self.emit_body_union(name, args, attrs)
442458
else:

Python/Python-ast.c

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)