Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: py3.6-wordcode
Changeset: r94613:c6b57116d338
Date: 2018-05-18 11:28 +0200
http://bitbucket.org/pypy/pypy/changeset/c6b57116d338/

Log:    fix co_firstlineno when annotations are present

diff --git a/pypy/interpreter/astcompiler/codegen.py 
b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -307,11 +307,10 @@
         # if the scope contained an annotated variable assignemt,
         # this will emit the requisite SETUP_ANNOTATIONS
         if self.scope.contains_annotated and not isinstance(self, 
AbstractFunctionCodeGenerator):
-            self.emit_op(ops.SETUP_ANNOTATIONS)
+            return self.emit_op(ops.SETUP_ANNOTATIONS)
 
     def visit_Module(self, mod):
-        if not self._handle_body(mod.body):
-            self.first_lineno = self.lineno = 1
+        self._handle_body(mod.body)
 
     def visit_Interactive(self, mod):
         self.interactive = True
@@ -1685,6 +1684,12 @@
                                      symbols, compile_info, qualname=None)
 
     def _compile(self, tree):
+        if isinstance(tree, ast.Module):
+            if tree.body:
+                self.first_lineno = tree.body[0].lineno
+            else:
+                self.first_lineno = self.lineno = 1
+
         self._maybe_setup_annotations()
         tree.walkabout(self)
 
diff --git a/pypy/interpreter/test/test_annotations.py 
b/pypy/interpreter/test/test_annotations.py
--- a/pypy/interpreter/test/test_annotations.py
+++ b/pypy/interpreter/test/test_annotations.py
@@ -132,3 +132,11 @@
             ''')
         """)
 
+    def test_lineno(self):
+        s = """
+
+a: int
+        """
+        c = compile(s, "f", "exec")
+        assert c.co_firstlineno == 3
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to