]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-33308: Fix a crash in the parser module when convert an ST object. (GH...
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 19 Apr 2018 06:16:43 +0000 (09:16 +0300)
committerGitHub <noreply@github.com>
Thu, 19 Apr 2018 06:16:43 +0000 (09:16 +0300)
Converting with line_info=False and col_info=True crashed before.
(cherry picked from commit e5362eaa75a154c6e91c5b1c47719d0a0f5ca48b)

Lib/test/test_parser.py
Misc/NEWS.d/next/Library/2018-04-18-19-12-25.bpo-33308.fW75xi.rst [new file with mode: 0644]
Modules/parsermodule.c

index 34b9cbb8c1636822536273d28c5016122f0c976f..73974a96f037bf24742792e6febec82ae6e8ce58 100644 (file)
@@ -263,21 +263,19 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
         # 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),
@@ -293,8 +291,25 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
             (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])
 
 
 #
diff --git a/Misc/NEWS.d/next/Library/2018-04-18-19-12-25.bpo-33308.fW75xi.rst b/Misc/NEWS.d/next/Library/2018-04-18-19-12-25.bpo-33308.fW75xi.rst
new file mode 100644 (file)
index 0000000..586004a
--- /dev/null
@@ -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``.
index 79555428d1b62fdca18f7926e3b63822384fa9e9..fcc618d5d90a6f25d12be6260cc99e22c8155984 100644 (file)
@@ -121,10 +121,10 @@ node2tuple(node *n,                     /* node to convert               */
         if (result != NULL) {
             (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
             (void) addelem(result, 1, PyString_FromString(STR(n)));
-            if (lineno == 1)
+            if (lineno)
                 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
-            if (col_offset == 1)
-                (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
+            if (col_offset)
+                (void) addelem(result, 2 + lineno, PyInt_FromLong(n->n_col_offset));
         }
         return (result);
     }