Skip to content

Commit

Permalink
00381: pythongh-92597: Ensure that AST nodes without explicit end pos…
Browse files Browse the repository at this point in the history
…itions can be compiled

(cherry picked from commit 705eaec)
  • Loading branch information
pablogsal authored and hroncok committed May 31, 2022
1 parent c8e5f22 commit 1aa94b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
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.

0 comments on commit 1aa94b2

Please sign in to comment.