[pypy-commit] pypy py3.5: while I am touching that code: make the error offset more precise when there are

cfbolz pypy.commits at gmail.com
Mon Jan 29 08:47:00 EST 2018


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.5
Changeset: r93712:095d5a88a186
Date: 2018-01-29 14:45 +0100
http://bitbucket.org/pypy/pypy/changeset/095d5a88a186/

Log:	while I am touching that code: make the error offset more precise
	when there are several bytes literals after each other

diff --git a/pypy/interpreter/astcompiler/fstring.py b/pypy/interpreter/astcompiler/fstring.py
--- a/pypy/interpreter/astcompiler/fstring.py
+++ b/pypy/interpreter/astcompiler/fstring.py
@@ -342,10 +342,11 @@
     encoding = astbuilder.compile_info.encoding
     joined_pieces = []
     fmode = False
-    try:
-        for i in range(atom_node.num_children()):
+    for i in range(atom_node.num_children()):
+        child = atom_node.get_child(i)
+        try:
             w_next = parsestring.parsestr(
-                    space, encoding, atom_node.get_child(i).get_value())
+                    space, encoding, child.get_value())
             if not isinstance(w_next, parsestring.W_FString):
                 add_constant_string(astbuilder, joined_pieces, w_next,
                                     atom_node)
@@ -353,20 +354,20 @@
                 parse_f_string(astbuilder, joined_pieces, w_next, atom_node)
                 fmode = True
 
-    except error.OperationError as e:
-        if e.match(space, space.w_UnicodeError):
-            kind = '(unicode error) '
-        elif e.match(space, space.w_ValueError):
-            kind = '(value error) '
-        elif e.match(space, space.w_SyntaxError):
-            kind = ''
-        else:
-            raise
-        # Unicode/ValueError/SyntaxError (without position information) in
-        # literal: turn into SyntaxError with position information
-        e.normalize_exception(space)
-        errmsg = space.text_w(space.str(e.get_w_value(space)))
-        raise astbuilder.error('%s%s' % (kind, errmsg), atom_node)
+        except error.OperationError as e:
+            if e.match(space, space.w_UnicodeError):
+                kind = '(unicode error) '
+            elif e.match(space, space.w_ValueError):
+                kind = '(value error) '
+            elif e.match(space, space.w_SyntaxError):
+                kind = ''
+            else:
+                raise
+            # Unicode/ValueError/SyntaxError (without position information) in
+            # literal: turn into SyntaxError with position information
+            e.normalize_exception(space)
+            errmsg = space.text_w(space.str(e.get_w_value(space)))
+            raise astbuilder.error('%s%s' % (kind, errmsg), child)
 
     if not fmode and len(joined_pieces) == 1:   # <= the common path
         return joined_pieces[0]   # ast.Str, Bytes or FormattedValue
diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -1024,6 +1024,12 @@
         raises(SyntaxError, compile, b"#\x00\nx=5#\xfd\n", "dummy", "exec",
                PyCF_ACCEPT_NULL_BYTES)
 
+    def test_correct_offset_in_many_bytes(self):
+        excinfo = raises(SyntaxError, compile, b'# coding: utf-8\nx = b"a" b"c" b"\xfd"\n',
+               "dummy", "exec")
+        assert excinfo.value.lineno == 2
+        assert excinfo.value.offset == 14
+
     def test_dict_and_set_literal_order(self):
         x = 1
         l1 = list({1:'a', 3:'b', 2:'c', 4:'d'})


More information about the pypy-commit mailing list