summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-04-25 17:08:54 (GMT)
committerGitHub <noreply@github.com>2023-04-25 17:08:54 (GMT)
commit93a9535d54d6f645ad2bfadf30f723c781e0fdd0 (patch)
tree02b234c5c53ca2732216121611e1dac29c1da2c3
parent026c3e9cc466a4b7157fc337bf6800a4d144c3ac (diff)
downloadcpython-93a9535d54d6f645ad2bfadf30f723c781e0fdd0.zip
cpython-93a9535d54d6f645ad2bfadf30f723c781e0fdd0.tar.gz
cpython-93a9535d54d6f645ad2bfadf30f723c781e0fdd0.tar.bz2
[3.11] gh-101517: fix line number propagation in code generated for except* (#103550) (#103816)
Manual backport of https://github.com/python/cpython/pull/103550. <!-- gh-issue-number: gh-101517 --> * Issue: gh-101517 <!-- /gh-issue-number --> --------- Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
-rw-r--r--Lib/bdb.py2
-rw-r--r--Lib/test/test_bdb.py3
-rw-r--r--Lib/test/test_pdb.py19
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2023-04-14-22-35-23.gh-issue-101517.5EqM-S.rst1
-rw-r--r--Python/compile.c38
5 files changed, 48 insertions, 15 deletions
diff --git a/Lib/bdb.py b/Lib/bdb.py
index 7f9b095..0f3eec6 100644
--- a/Lib/bdb.py
+++ b/Lib/bdb.py
@@ -574,6 +574,8 @@ class Bdb:
line = linecache.getline(filename, lineno, frame.f_globals)
if line:
s += lprefix + line.strip()
+ else:
+ s += f'{lprefix}Warning: lineno is None'
return s
# The following methods can be called by clients to use
diff --git a/Lib/test/test_bdb.py b/Lib/test/test_bdb.py
index 042c2da..c2df18c 100644
--- a/Lib/test/test_bdb.py
+++ b/Lib/test/test_bdb.py
@@ -1206,7 +1206,8 @@ class IssuesTestCase(BaseTestCase):
class TestRegressions(unittest.TestCase):
def test_format_stack_entry_no_lineno(self):
# See gh-101517
- Bdb().format_stack_entry((sys._getframe(), None))
+ self.assertIn('Warning: lineno is None',
+ Bdb().format_stack_entry((sys._getframe(), None)))
if __name__ == "__main__":
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index e96dc7f..1a0fbf4 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1653,6 +1653,25 @@ def test_pdb_issue_gh_101673():
(Pdb) continue
"""
+def test_pdb_issue_gh_101517():
+ """See GH-101517
+
+ Make sure pdb doesn't crash when the exception is caught in a try/except* block
+
+ >>> def test_function():
+ ... try:
+ ... raise KeyError
+ ... except* Exception as e:
+ ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+
+ >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
+ ... 'continue'
+ ... ]):
+ ... test_function()
+ > <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(4)test_function()
+ -> except* Exception as e:
+ (Pdb) continue
+ """
@support.requires_subprocess()
class PdbTestCase(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-14-22-35-23.gh-issue-101517.5EqM-S.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-14-22-35-23.gh-issue-101517.5EqM-S.rst
new file mode 100644
index 0000000..730c6cd
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-14-22-35-23.gh-issue-101517.5EqM-S.rst
@@ -0,0 +1 @@
+Fix bug in line numbers of instructions emitted for :keyword:`except* <except_star>`.
diff --git a/Python/compile.c b/Python/compile.c
index 17d1df2..2170e82 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3613,12 +3613,10 @@ compiler_try_except(struct compiler *c, stmt_ty s)
[orig, res, exc] <evaluate E1>
[orig, res, exc, E1] CHECK_EG_MATCH
- [orig, red, rest/exc, match?] COPY 1
- [orig, red, rest/exc, match?, match?] POP_JUMP_IF_NOT_NONE H1
- [orig, red, exc, None] POP_TOP
- [orig, red, exc] JUMP L2
+ [orig, res, rest/exc, match?] COPY 1
+ [orig, res, rest/exc, match?, match?] POP_JUMP_IF_NONE C1
- [orig, res, rest, match] H1: <assign to V1> (or POP if no V1)
+ [orig, res, rest, match] <assign to V1> (or POP if no V1)
[orig, res, rest] SETUP_FINALLY R1
[orig, res, rest] <code for S1>
@@ -3626,8 +3624,14 @@ compiler_try_except(struct compiler *c, stmt_ty s)
[orig, res, rest, i, v] R1: LIST_APPEND 3 ) exc raised in except* body - add to res
[orig, res, rest, i] POP
+ [orig, res, rest] JUMP LE2
- [orig, res, rest] L2: <evaluate E2>
+ [orig, res, rest] L2: NOP ) for lineno
+ [orig, res, rest] JUMP LE2
+
+ [orig, res, rest/exc, None] C1: POP
+
+ [orig, res, rest] LE2: <evaluate E2>
.............................etc.......................
[orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
@@ -3700,8 +3704,12 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
if (except == NULL) {
return 0;
}
- basicblock *handle_match = compiler_new_block(c);
- if (handle_match == NULL) {
+ basicblock *except_with_error = compiler_new_block(c);
+ if (except_with_error == NULL) {
+ return 0;
+ }
+ basicblock *no_match = compiler_new_block(c);
+ if (no_match == NULL) {
return 0;
}
if (i == 0) {
@@ -3725,13 +3733,9 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
VISIT(c, expr, handler->v.ExceptHandler.type);
ADDOP(c, CHECK_EG_MATCH);
ADDOP_I(c, COPY, 1);
- ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, handle_match);
- ADDOP(c, POP_TOP); // match
- ADDOP_JUMP(c, JUMP, except);
+ ADDOP_JUMP(c, POP_JUMP_IF_NONE, no_match);
}
- compiler_use_next_block(c, handle_match);
-
basicblock *cleanup_end = compiler_new_block(c);
if (cleanup_end == NULL) {
return 0;
@@ -3793,8 +3797,14 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
ADDOP_I(c, LIST_APPEND, 3); // exc
ADDOP(c, POP_TOP); // lasti
- ADDOP_JUMP(c, JUMP, except);
+ ADDOP_JUMP(c, JUMP, except_with_error);
compiler_use_next_block(c, except);
+ ADDOP(c, NOP); // to hold a propagated location info
+ ADDOP_JUMP(c, JUMP, except_with_error);
+ compiler_use_next_block(c, no_match);
+ ADDOP(c, POP_TOP); // match (None)
+
+ compiler_use_next_block(c, except_with_error);
if (i == n - 1) {
/* Add exc to the list (if not None it's the unhandled part of the EG) */