gh-133371: Don't optimize `LOAD_FAST` instructions whose local is killed by `DELETE_FAST` by mpage · Pull Request #133383 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
24 changes: 24 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dis
import gc
from itertools import combinations, product
import opcode
import sys
Expand Down Expand Up @@ -2472,6 +2473,13 @@ def test_unoptimized_if_support_killed(self):
]
self.check(insts, insts)

insts = [
Comment thread
mpage marked this conversation as resolved.
("LOAD_FAST", 0, 1),
("DELETE_FAST", 0, 2),
("POP_TOP", None, 3),
]
self.check(insts, insts)

def test_unoptimized_if_aliased(self):
insts = [
("LOAD_FAST", 0, 1),
Expand Down Expand Up @@ -2606,6 +2614,22 @@ def test_send(self):
]
self.cfg_optimization_test(insts, expected, consts=[None])

def test_del_in_finally(self):
# This loads `obj` onto the stack, executes `del obj`, then returns the
# `obj` from the stack. See gh-133371 for more details.
def create_obj():
obj = [42]
try:
return obj
finally:
del obj

obj = create_obj()
# The crash in the linked issue happens while running GC during
# interpreter finalization, so run it here manually.
gc.collect()
self.assertEqual(obj, [42])



if __name__ == "__main__":
Expand Down
5 changes: 5 additions & 0 deletions Python/flowgraph.c