Skip to content

Commit fc8693d

Browse files
bpo-33308: Fix a crash in the parser module when convert an ST object. (GH-6519)
Converting with line_info=False and col_info=True crashed before. (cherry picked from commit e5362ea) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1957e7b commit fc8693d

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Lib/test/test_parser.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,19 @@ def test_position(self):
322322
# An absolutely minimal test of position information. Better
323323
# tests would be a big project.
324324
code = "def f(x):\n return x + 1"
325-
st1 = parser.suite(code)
326-
st2 = st1.totuple(line_info=1, col_info=1)
325+
st = parser.suite(code)
327326

328327
def walk(tree):
329328
node_type = tree[0]
330329
next = tree[1]
331-
if isinstance(next, tuple):
330+
if isinstance(next, (tuple, list)):
332331
for elt in tree[1:]:
333332
for x in walk(elt):
334333
yield x
335334
else:
336335
yield tree
337336

338-
terminals = list(walk(st2))
339-
self.assertEqual([
337+
expected = [
340338
(1, 'def', 1, 0),
341339
(1, 'f', 1, 4),
342340
(7, '(', 1, 5),
@@ -352,8 +350,25 @@ def walk(tree):
352350
(4, '', 2, 16),
353351
(6, '', 2, -1),
354352
(4, '', 2, -1),
355-
(0, '', 2, -1)],
356-
terminals)
353+
(0, '', 2, -1),
354+
]
355+
356+
self.assertEqual(list(walk(st.totuple(line_info=True, col_info=True))),
357+
expected)
358+
self.assertEqual(list(walk(st.totuple())),
359+
[(t, n) for t, n, l, c in expected])
360+
self.assertEqual(list(walk(st.totuple(line_info=True))),
361+
[(t, n, l) for t, n, l, c in expected])
362+
self.assertEqual(list(walk(st.totuple(col_info=True))),
363+
[(t, n, c) for t, n, l, c in expected])
364+
self.assertEqual(list(walk(st.tolist(line_info=True, col_info=True))),
365+
[list(x) for x in expected])
366+
self.assertEqual(list(walk(parser.st2tuple(st, line_info=True,
367+
col_info=True))),
368+
expected)
369+
self.assertEqual(list(walk(parser.st2list(st, line_info=True,
370+
col_info=True))),
371+
[list(x) for x in expected])
357372

358373
def test_extended_unpacking(self):
359374
self.check_suite("*a = y")
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,18 @@ node2tuple(node *n, /* node to convert */
136136
goto error;
137137
(void) addelem(result, 1, w);
138138

139-
if (lineno == 1) {
139+
if (lineno) {
140140
w = PyLong_FromLong(n->n_lineno);
141141
if (w == NULL)
142142
goto error;
143143
(void) addelem(result, 2, w);
144144
}
145145

146-
if (col_offset == 1) {
146+
if (col_offset) {
147147
w = PyLong_FromLong(n->n_col_offset);
148148
if (w == NULL)
149149
goto error;
150-
(void) addelem(result, 3, w);
150+
(void) addelem(result, 2 + lineno, w);
151151
}
152152
}
153153
else {

0 commit comments

Comments
 (0)