From 3b8121f2143b5e8a1f6fc0bef9dff382c2aedbc1 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 17 Jun 2022 19:32:52 +0100 Subject: [PATCH 1/3] GH-93249: relax overly struct assertion on bounds->ar_start --- Objects/codeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 8ac4e9914f8887..dc27cdb7c96183 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -818,7 +818,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); From 8dde6c655597994c84e7003517f9546aaab0c0c9 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 17 Jun 2022 23:15:32 +0100 Subject: [PATCH 2/3] add test --- Lib/test/test_traceback.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 07472e29df45c3..722c265a6a8a51 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4,6 +4,7 @@ from io import StringIO import linecache import sys +import types import inspect import unittest import re @@ -1128,7 +1129,7 @@ def test_print_exception_bad_type_python(self): 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 @@ def exc(): 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): # From c2138e1345566a126b1952bc32483796e37fe3ed Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 20 Jun 2022 15:24:48 +0100 Subject: [PATCH 3/3] update previous_code_delta for case of ar_start == 0 --- Objects/codeobject.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index dc27cdb7c96183..b90a7e1e5d23fe 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -775,6 +775,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--;