]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43933: Show frame.f_lineno as None, rather than -1, if there is no line number...
authorMark Shannon <mark@hotpy.org>
Thu, 29 Apr 2021 18:28:50 +0000 (19:28 +0100)
committerGitHub <noreply@github.com>
Thu, 29 Apr 2021 18:28:50 +0000 (19:28 +0100)
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2021-04-29-13-11-44.bpo-43933.mvoV6O.rst [new file with mode: 0644]
Objects/frameobject.c

index 590935cb6cd624b54f84a0aad80b7e24703fd576..3810108e356637f715d0ab2ab5a0bb5dd9d2cdff 100644 (file)
@@ -2081,7 +2081,10 @@ class PEP626Tests(unittest.TestCase):
             while t.tb_next:
                 t = t.tb_next
             frame = t.tb_frame
-            self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
+            if line is None:
+                self.assertEqual(frame.f_lineno, line)
+            else:
+                self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
 
     def test_lineno_after_raise_simple(self):
         def simple():
@@ -2153,6 +2156,12 @@ class PEP626Tests(unittest.TestCase):
                 pass
         self.lineno_after_raise(after_with, 2)
 
+    def test_missing_lineno_shows_as_none(self):
+        def f():
+            1/0
+        self.lineno_after_raise(f, 1)
+        f.__code__ = f.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
+        self.lineno_after_raise(f, None)
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-29-13-11-44.bpo-43933.mvoV6O.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-29-13-11-44.bpo-43933.mvoV6O.rst
new file mode 100644 (file)
index 0000000..8d11a8c
--- /dev/null
@@ -0,0 +1,3 @@
+If the current position in a frame has no line number then set the f_lineno
+attribute to None, instead of -1, to conform to PEP 626. This should not
+normally be possible, but might occur in some unusual circumstances.
index b0487c2b68811bab8d3c503460bf47beafc45a9a..5920ed86fd923083af5648fd7f692c2fc32062e5 100644 (file)
@@ -53,7 +53,13 @@ PyFrame_GetLineNumber(PyFrameObject *f)
 static PyObject *
 frame_getlineno(PyFrameObject *f, void *closure)
 {
-    return PyLong_FromLong(PyFrame_GetLineNumber(f));
+    int lineno = PyFrame_GetLineNumber(f);
+    if (lineno < 0) {
+        Py_RETURN_NONE;
+    }
+    else {
+        return PyLong_FromLong(lineno);
+    }
 }
 
 static PyObject *