Skip to content

Commit

Permalink
pythongh-92597: Ensure that AST nodes without explicit end positions …
Browse files Browse the repository at this point in the history
…can be compiled
  • Loading branch information
pablogsal committed May 30, 2022
1 parent 5893b5d commit df8934d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
19 changes: 19 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@ 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.Assign(
targets=[
ast.Name(
id='a',
ctx=ast.Store(),
lineno=10,
col_offset=10)
],
value=ast.Constant(
value=1,
lineno=10,
col_offset=40),
lineno=10,
col_offset=10)],
type_ignores=[])
compile(tree, "<string>", "exec")

def test_slice(self):
slc = ast.parse("x[::]").body[0].value.slice
Expand Down
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 df8934d

Please sign in to comment.