Skip to content

Commit

Permalink
Merge branch 'main' into superopt
Browse files Browse the repository at this point in the history
* main:
  pythongh-101517: fix line number propagation in code generated for except* (python#103550)
  pythongh-103780: Use patch instead of mock in asyncio unix events test (python#103782)
  • Loading branch information
carljm committed Apr 24, 2023
2 parents 19b8025 + 1c01f8d commit 0de5bc6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
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
Expand Down
9 changes: 4 additions & 5 deletions Lib/test/test_asyncio/test_unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1712,11 +1712,11 @@ class PolicyTests(unittest.TestCase):
def create_policy(self):
return asyncio.DefaultEventLoopPolicy()

def test_get_default_child_watcher(self):
@mock.patch('asyncio.unix_events.can_use_pidfd')
def test_get_default_child_watcher(self, m_can_use_pidfd):
m_can_use_pidfd.return_value = False
policy = self.create_policy()
self.assertIsNone(policy._watcher)
unix_events.can_use_pidfd = mock.Mock()
unix_events.can_use_pidfd.return_value = False
with self.assertWarns(DeprecationWarning):
watcher = policy.get_child_watcher()
self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher)
Expand All @@ -1725,10 +1725,9 @@ def test_get_default_child_watcher(self):
with self.assertWarns(DeprecationWarning):
self.assertIs(watcher, policy.get_child_watcher())

m_can_use_pidfd.return_value = True
policy = self.create_policy()
self.assertIsNone(policy._watcher)
unix_events.can_use_pidfd = mock.Mock()
unix_events.can_use_pidfd.return_value = True
with self.assertWarns(DeprecationWarning):
watcher = policy.get_child_watcher()
self.assertIsInstance(watcher, asyncio.PidfdChildWatcher)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,8 @@ def main():
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__":
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,8 @@ def test_pdb_issue_gh_101517():
... 'continue'
... ]):
... test_function()
--Return--
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(None)test_function()->None
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(5)test_function()
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) continue
"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in line numbers of instructions emitted for :keyword:`except* <except_star>`.
32 changes: 20 additions & 12 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3067,20 +3067,24 @@ compiler_try_except(struct compiler *c, stmt_ty s)
[orig, res, exc] <evaluate E1>
[orig, res, exc, E1] CHECK_EG_MATCH
[orig, res, rest/exc, match?] COPY 1
[orig, res, rest/exc, match?, match?] POP_JUMP_IF_NOT_NONE H1
[orig, res, exc, None] POP_TOP
[orig, res, exc] JUMP L2
[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>
[orig, res, rest] JUMP L2
[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)
Expand Down Expand Up @@ -3136,7 +3140,8 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
location loc = LOC(handler);
NEW_JUMP_TARGET_LABEL(c, next_except);
except = next_except;
NEW_JUMP_TARGET_LABEL(c, handle_match);
NEW_JUMP_TARGET_LABEL(c, except_with_error);
NEW_JUMP_TARGET_LABEL(c, no_match);
if (i == 0) {
/* create empty list for exceptions raised/reraise in the except* blocks */
/*
Expand All @@ -3154,13 +3159,9 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
VISIT(c, expr, handler->v.ExceptHandler.type);
ADDOP(c, loc, CHECK_EG_MATCH);
ADDOP_I(c, loc, COPY, 1);
ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match);
ADDOP(c, loc, POP_TOP); // match
ADDOP_JUMP(c, loc, JUMP, except);
ADDOP_JUMP(c, loc, POP_JUMP_IF_NONE, no_match);
}

USE_LABEL(c, handle_match);

NEW_JUMP_TARGET_LABEL(c, cleanup_end);
NEW_JUMP_TARGET_LABEL(c, cleanup_body);

Expand Down Expand Up @@ -3219,9 +3220,16 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
/* add exception raised to the res list */
ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc
ADDOP(c, NO_LOCATION, POP_TOP); // lasti
ADDOP_JUMP(c, NO_LOCATION, JUMP, except);
ADDOP_JUMP(c, NO_LOCATION, JUMP, except_with_error);

USE_LABEL(c, except);
ADDOP(c, NO_LOCATION, NOP); // to hold a propagated location info
ADDOP_JUMP(c, NO_LOCATION, JUMP, except_with_error);

USE_LABEL(c, no_match);
ADDOP(c, loc, POP_TOP); // match (None)

USE_LABEL(c, except_with_error);

if (i == n - 1) {
/* Add exc to the list (if not None it's the unhandled part of the EG) */
Expand Down

0 comments on commit 0de5bc6

Please sign in to comment.