gh-104799: Default missing lists in AST to the empty list (#104834)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Jelle Zijlstra 2023-06-01 18:39:39 -07:00 committed by GitHub
parent 37498fc950
commit 77d2579586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 400 additions and 239 deletions

View File

@ -1591,6 +1591,8 @@ class ASTValidatorTests(unittest.TestCase):
f = ast.FunctionDef("x", a, [ast.Pass()], [], f = ast.FunctionDef("x", a, [ast.Pass()], [],
ast.Name("x", ast.Store()), None, []) ast.Name("x", ast.Store()), None, [])
self.stmt(f, "must have Load context") self.stmt(f, "must have Load context")
f = ast.FunctionDef("x", ast.arguments(), [ast.Pass()])
self.stmt(f)
def fac(args): def fac(args):
return ast.FunctionDef("x", args, [ast.Pass()], [], None, None, []) return ast.FunctionDef("x", args, [ast.Pass()], [], None, None, [])
self._check_arguments(fac, self.stmt) self._check_arguments(fac, self.stmt)

View File

@ -0,0 +1,4 @@
Attributes of :mod:`ast` nodes that are lists now default to the empty list
if omitted. This means that some code that previously raised
:exc:`TypeError` when the AST node was used will now proceed with the empty
list instead. Patch by Jelle Zijlstra.

View File

@ -632,29 +632,38 @@ class Obj2ModVisitor(PickleVisitor):
self.emit(line % field.name, depth) self.emit(line % field.name, depth)
self.emit("return 1;", depth+1) self.emit("return 1;", depth+1)
self.emit("}", depth) self.emit("}", depth)
if not field.opt: if field.seq:
self.emit("if (tmp == NULL) {", depth) self.emit("if (tmp == NULL) {", depth)
message = "required field \\\"%s\\\" missing from %s" % (field.name, name) self.emit("tmp = PyList_New(0);", depth+1)
format = "PyErr_SetString(PyExc_TypeError, \"%s\");" self.emit("if (tmp == NULL) {", depth+1)
self.emit(format % message, depth+1, reflow=False) self.emit("return 1;", depth+2)
self.emit("return 1;", depth+1) self.emit("}", depth+1)
self.emit("}", depth)
self.emit("{", depth)
else: else:
self.emit("if (tmp == NULL || tmp == Py_None) {", depth) if not field.opt:
self.emit("Py_CLEAR(tmp);", depth+1) self.emit("if (tmp == NULL) {", depth)
if self.isNumeric(field): message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
if field.name in self.attribute_special_defaults: format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
self.emit( self.emit(format % message, depth+1, reflow=False)
"%s = %s;" % (field.name, self.attribute_special_defaults[field.name]), self.emit("return 1;", depth+1)
depth+1,
)
else:
self.emit("%s = 0;" % field.name, depth+1)
elif not self.isSimpleType(field):
self.emit("%s = NULL;" % field.name, depth+1)
else: else:
raise TypeError("could not determine the default value for %s" % field.name) self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
self.emit("}", depth) self.emit("Py_CLEAR(tmp);", depth+1)
self.emit("else {", depth) if self.isNumeric(field):
if field.name in self.attribute_special_defaults:
self.emit(
"%s = %s;" % (field.name, self.attribute_special_defaults[field.name]),
depth+1,
)
else:
self.emit("%s = 0;" % field.name, depth+1)
elif not self.isSimpleType(field):
self.emit("%s = NULL;" % field.name, depth+1)
else:
raise TypeError("could not determine the default value for %s" % field.name)
self.emit("}", depth)
self.emit("else {", depth)
self.emit("int res;", depth+1) self.emit("int res;", depth+1)
if field.seq: if field.seq:

584
Python/Python-ast.c generated

File diff suppressed because it is too large Load Diff