]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-93249: relax overly strict assertion on bounds->ar_start (GH-93961)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 20 Jun 2022 16:13:39 +0000 (17:13 +0100)
committerGitHub <noreply@github.com>
Mon, 20 Jun 2022 16:13:39 +0000 (17:13 +0100)
Lib/test/test_traceback.py
Objects/codeobject.c

index 07472e29df45c322109a89cc87b3e647168b7c13..722c265a6a8a519730e075971cd5fb123a3f9007 100644 (file)
@@ -4,6 +4,7 @@ from collections import namedtuple
 from io import StringIO
 import linecache
 import sys
+import types
 import inspect
 import unittest
 import re
@@ -1128,7 +1129,7 @@ boundaries = re.compile(
 class BaseExceptionReportingTests:
 
     def get_exception(self, exception_or_callable):
-        if isinstance(exception_or_callable, Exception):
+        if isinstance(exception_or_callable, BaseException):
             return exception_or_callable
         try:
             exception_or_callable()
@@ -1850,6 +1851,31 @@ class BaseExceptionReportingTests:
         report = self.get_report(exc)
         self.assertEqual(report, expected)
 
+    def test_KeyboardInterrupt_at_first_line_of_frame(self):
+        # see GH-93249
+        def f():
+            return sys._getframe()
+
+        tb_next = None
+        frame = f()
+        lasti = 0
+        lineno = f.__code__.co_firstlineno
+        tb = types.TracebackType(tb_next, frame, lasti, lineno)
+
+        exc = KeyboardInterrupt()
+        exc.__traceback__ = tb
+
+        expected = (f'Traceback (most recent call last):\n'
+                    f'  File "{__file__}", line {lineno}, in f\n'
+                    f'    def f():\n'
+                    f'\n'
+                    f'KeyboardInterrupt\n')
+
+        report = self.get_report(exc)
+        # remove trailing writespace:
+        report = '\n'.join([l.rstrip() for l in report.split('\n')])
+        self.assertEqual(report, expected)
+
 
 class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
     #
index c1f696dc5723b709ad8681fdb0e8e0e8a8c4af09..707de11d47922f4dda29307df864c1b830eaf925 100644 (file)
@@ -826,6 +826,11 @@ next_code_delta(PyCodeAddressRange *bounds)
 static int
 previous_code_delta(PyCodeAddressRange *bounds)
 {
+    if (bounds->ar_start == 0) {
+        // If we looking at the first entry, the
+        // "previous" entry has an implicit length of 1.
+        return 1;
+    }
     const uint8_t *ptr = bounds->opaque.lo_next-1;
     while (((*ptr) & 128) == 0) {
         ptr--;
@@ -869,7 +874,7 @@ static void
 retreat(PyCodeAddressRange *bounds)
 {
     ASSERT_VALID_BOUNDS(bounds);
-    assert(bounds->ar_start > 0);
+    assert(bounds->ar_start >= 0);
     do {
         bounds->opaque.lo_next--;
     } while (((*bounds->opaque.lo_next) & 128) == 0);