Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] gh-92597: Ensure that AST nodes without explicit end positions can be compiled (GH-93359) #93397

Merged
merged 1 commit into from May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_ast.py
Expand Up @@ -362,6 +362,14 @@ def test_invalid_position_information(self):
with self.assertRaises(ValueError):
compile(tree, '<string>', 'exec')

def test_compilation_of_ast_nodes_with_default_end_position_values(self):
tree = ast.Module(body=[
ast.Import(names=[ast.alias(name='builtins', lineno=1, col_offset=0)], lineno=1, col_offset=0),
ast.Import(names=[ast.alias(name='traceback', lineno=0, col_offset=0)], lineno=0, col_offset=1)
], type_ignores=[])

# Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
compile(tree, "<string>", "exec")

def test_slice(self):
slc = ast.parse("x[::]").body[0].value.slice
Expand Down
@@ -0,0 +1,2 @@
Ensure that custom :mod:`ast` nodes without explicit end positions can be
compiled. Patch by Pablo Galindo.
14 changes: 13 additions & 1 deletion Parser/asdl_c.py
Expand Up @@ -488,6 +488,12 @@ def visitProduct(self, prod, name):


class Obj2ModVisitor(PickleVisitor):

attribute_special_defaults = {
"end_lineno": "lineno",
"end_col_offset": "col_offset",
}

@contextmanager
def recursive_call(self, node, level):
self.emit('if (_Py_EnterRecursiveCall(" while traversing \'%s\' node")) {' % node, level, reflow=False)
Expand Down Expand Up @@ -637,7 +643,13 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
self.emit("Py_CLEAR(tmp);", depth+1)
if self.isNumeric(field):
self.emit("%s = 0;" % field.name, depth+1)
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:
Expand Down
24 changes: 12 additions & 12 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.