Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-92597: Ensure that AST nodes without explicit end positions can be…
… compiled (GH-93359)

(cherry picked from commit 705eaec)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
  • Loading branch information
pablogsal authored and miss-islington committed May 31, 2022
commit a3156c6d570f841c7a17f5aaaa9bf319e6d3cdc8
8 changes: 8 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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.