Skip to content

[3.7] bpo-33308: Fix a crash in the parser module when convert an ST object. (GH-6519) #6526

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

Merged
merged 1 commit into from
Apr 19, 2018
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
29 changes: 22 additions & 7 deletions Lib/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,19 @@ def test_position(self):
# An absolutely minimal test of position information. Better
# tests would be a big project.
code = "def f(x):\n return x + 1"
st1 = parser.suite(code)
st2 = st1.totuple(line_info=1, col_info=1)
st = parser.suite(code)

def walk(tree):
node_type = tree[0]
next = tree[1]
if isinstance(next, tuple):
if isinstance(next, (tuple, list)):
for elt in tree[1:]:
for x in walk(elt):
yield x
else:
yield tree

terminals = list(walk(st2))
self.assertEqual([
expected = [
(1, 'def', 1, 0),
(1, 'f', 1, 4),
(7, '(', 1, 5),
Expand All @@ -352,8 +350,25 @@ def walk(tree):
(4, '', 2, 16),
(6, '', 2, -1),
(4, '', 2, -1),
(0, '', 2, -1)],
terminals)
(0, '', 2, -1),
]

self.assertEqual(list(walk(st.totuple(line_info=True, col_info=True))),
expected)
self.assertEqual(list(walk(st.totuple())),
[(t, n) for t, n, l, c in expected])
self.assertEqual(list(walk(st.totuple(line_info=True))),
[(t, n, l) for t, n, l, c in expected])
self.assertEqual(list(walk(st.totuple(col_info=True))),
[(t, n, c) for t, n, l, c in expected])
self.assertEqual(list(walk(st.tolist(line_info=True, col_info=True))),
[list(x) for x in expected])
self.assertEqual(list(walk(parser.st2tuple(st, line_info=True,
col_info=True))),
expected)
self.assertEqual(list(walk(parser.st2list(st, line_info=True,
col_info=True))),
[list(x) for x in expected])

def test_extended_unpacking(self):
self.check_suite("*a = y")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a crash in the :mod:`parser` module when converting an ST object to a
tree of tuples or lists with ``line_info=False`` and ``col_info=True``.
6 changes: 3 additions & 3 deletions Modules/parsermodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ node2tuple(node *n, /* node to convert */
goto error;
(void) addelem(result, 1, w);

if (lineno == 1) {
if (lineno) {
w = PyLong_FromLong(n->n_lineno);
if (w == NULL)
goto error;
(void) addelem(result, 2, w);
}

if (col_offset == 1) {
if (col_offset) {
w = PyLong_FromLong(n->n_col_offset);
if (w == NULL)
goto error;
(void) addelem(result, 3, w);
(void) addelem(result, 2 + lineno, w);
}
}
else {
Expand Down