FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-98442: fix locations of with statement's cleanup instructions (GH-120763) by miss-islington · Pull Request #120786 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 33 additions & 0 deletions Lib/test/test_compile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,39 @@ def test_lambda_return_position(self):
self.assertGreaterEqual(end_col, start_col)
self.assertLessEqual(end_col, code_end)

def test_return_in_with_positions(self):
# See gh-98442
def f():
with xyz:
1
2
3
4
return R

# All instructions should have locations on a single line
for instr in dis.get_instructions(f):
start_line, end_line, _, _ = instr.positions
self.assertEqual(start_line, end_line)

# Expect three load None instructions for the no-exception __exit__ call,
# and one RETURN_VALUE.
# They should all have the locations of the context manager ('xyz').

load_none = [instr for instr in dis.get_instructions(f) if
instr.opname == 'LOAD_CONST' and instr.argval is None]
return_value = [instr for instr in dis.get_instructions(f) if
instr.opname == 'RETURN_VALUE']

self.assertEqual(len(load_none), 3)
self.assertEqual(len(return_value), 1)
for instr in load_none + return_value:
start_line, end_line, start_col, end_col = instr.positions
self.assertEqual(start_line, f.__code__.co_firstlineno + 1)
self.assertEqual(end_line, f.__code__.co_firstlineno + 1)
self.assertEqual(start_col, 17)
self.assertEqual(end_col, 20)


class TestStaticAttributes(unittest.TestCase):

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix too wide source locations of the cleanup instructions of a with
statement.
4 changes: 3 additions & 1 deletion Python/compile.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
struct fblockinfo {
enum fblocktype fb_type;
jump_target_label fb_block;
location fb_loc;
/* (optional) type-specific exit or cleanup block */
jump_target_label fb_exit;
/* (optional) additional information required for unwinding */
Expand Down Expand Up @@ -1433,6 +1434,7 @@ compiler_push_fblock(struct compiler *c, location loc,
f = &c->u->u_fblock[c->u->u_nfblocks++];
f->fb_type = t;
f->fb_block = block_label;
f->fb_loc = loc;
f->fb_exit = exit;
f->fb_datum = datum;
return SUCCESS;
Expand Down Expand Up @@ -1560,7 +1562,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc,

case WITH:
case ASYNC_WITH:
*ploc = LOC((stmt_ty)info->fb_datum);
*ploc = info->fb_loc;
ADDOP(c, *ploc, POP_BLOCK);
if (preserve_tos) {
ADDOP_I(c, *ploc, SWAP, 2);
Expand Down

Back | FazBrowse Home | New Git URL