Skip to content

Commit d988c0b

Browse files
[2.7] bpo-33308: Fix a crash in the parser module when convert an ST object. (GH-6519) (GH-6532)
Converting with line_info=False and col_info=True crashed before. (cherry picked from commit e5362ea)
1 parent afc768d commit d988c0b

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Lib/test/test_parser.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,19 @@ def test_position(self):
263263
# An absolutely minimal test of position information. Better
264264
# tests would be a big project.
265265
code = "def f(x):\n return x + 1"
266-
st1 = parser.suite(code)
267-
st2 = st1.totuple(line_info=1, col_info=1)
266+
st = parser.suite(code)
268267

269268
def walk(tree):
270269
node_type = tree[0]
271270
next = tree[1]
272-
if isinstance(next, tuple):
271+
if isinstance(next, (tuple, list)):
273272
for elt in tree[1:]:
274273
for x in walk(elt):
275274
yield x
276275
else:
277276
yield tree
278277

279-
terminals = list(walk(st2))
280-
self.assertEqual([
278+
expected = [
281279
(1, 'def', 1, 0),
282280
(1, 'f', 1, 4),
283281
(7, '(', 1, 5),
@@ -293,8 +291,25 @@ def walk(tree):
293291
(4, '', 2, 16),
294292
(6, '', 2, -1),
295293
(4, '', 2, -1),
296-
(0, '', 2, -1)],
297-
terminals)
294+
(0, '', 2, -1),
295+
]
296+
297+
self.assertEqual(list(walk(st.totuple(line_info=True, col_info=True))),
298+
expected)
299+
self.assertEqual(list(walk(st.totuple())),
300+
[(t, n) for t, n, l, c in expected])
301+
self.assertEqual(list(walk(st.totuple(line_info=True))),
302+
[(t, n, l) for t, n, l, c in expected])
303+
self.assertEqual(list(walk(st.totuple(col_info=True))),
304+
[(t, n, c) for t, n, l, c in expected])
305+
self.assertEqual(list(walk(st.tolist(line_info=True, col_info=True))),
306+
[list(x) for x in expected])
307+
self.assertEqual(list(walk(parser.st2tuple(st, line_info=True,
308+
col_info=True))),
309+
expected)
310+
self.assertEqual(list(walk(parser.st2list(st, line_info=True,
311+
col_info=True))),
312+
[list(x) for x in expected])
298313

299314

300315
#
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a crash in the :mod:`parser` module when converting an ST object to a
2+
tree of tuples or lists with ``line_info=False`` and ``col_info=True``.

Modules/parsermodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ node2tuple(node *n, /* node to convert */
121121
if (result != NULL) {
122122
(void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
123123
(void) addelem(result, 1, PyString_FromString(STR(n)));
124-
if (lineno == 1)
124+
if (lineno)
125125
(void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
126-
if (col_offset == 1)
127-
(void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
126+
if (col_offset)
127+
(void) addelem(result, 2 + lineno, PyInt_FromLong(n->n_col_offset));
128128
}
129129
return (result);
130130
}

0 commit comments

Comments
 (0)