8000 GH-129386: Add `test.support.reset_code` (GH-129486) · python/cpython@674befb · GitHub
[go: up one dir, main page]

Skip to content

Commit 674befb

Browse files
authored
GH-129386: Add test.support.reset_code (GH-129486)
1 parent e3eba8c commit 674befb

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

Lib/test/support/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"BrokenIter",
6767
"in_systemd_nspawn_sync_suppressed",
6868
"run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
69+
"reset_code",
6970
]
7071

7172

@@ -1286,6 +1287,12 @@ def requires_specialization_ft(test):
12861287
_opcode.ENABLE_SPECIALIZATION_FT, "requires specialization")(test)
12871288

12881289

1290+
def reset_code(f: types.FunctionType) -> types.FunctionType:
1291+
"""Clear all specializations, local instrumentation, and JIT code for the given function."""
1292+
f.__code__ = f.__code__.replace()
1293+
return f
1294+
1295+
12891296
#=======================================================================
12901297
# Check for the presence of docstrings.
12911298

Lib/test/test_capi/test_opt.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import _opcode
1010

1111
from test.support import (script_helper, requires_specialization,
12-
import_helper, Py_GIL_DISABLED, requires_jit_enabled)
12+
import_helper, Py_GIL_DISABLED, requires_jit_enabled,
13+
reset_code)
1314

1415
_testinternalcapi = import_helper.import_module("_testinternalcapi")
1516

@@ -19,11 +20,11 @@
1920
@contextlib.contextmanager
2021
def clear_executors(func):
2122
# Clear executors in func before and after running a block
22-
func.__code__ = func.__code__.replace()
23+
reset_code(func)
2324
try:
2425
yield
2526
finally:
26-
func.__code__ = func.__code__.replace()
27+
reset_code(func)
2728

2829

2930
def get_first_executor(func):

Lib/test/test_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616
from test.support import (captured_stdout, requires_debug_ranges,
1717
requires_specialization, cpython_only,
18-
os_helper, import_helper)
18+
os_helper, import_helper, reset_code)
1919
from test.support.bytecode_helper import BytecodeTestCase
2020

2121

@@ -1356,7 +1356,7 @@ def f():
13561356
self.code_quicken(f)
13571357
else:
13581358
# "copy" the code to un-quicken it:
1359-
f.__code__ = f.__code__.replace()
1359+
reset_code(f)
13601360
for instruction in _unroll_caches_as_Instructions(dis.get_instructions(
13611361
f, show_caches=True, adaptive=adaptive
13621362
), show_caches=True):

Lib/test/test_embed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self):
391391
import importlib._bootstrap
392392
import opcode
393393
import test.test_dis
394+
import test.support
394395
395396
def is_specialized(f):
396397
for instruction in dis.get_instructions(f, adaptive=True):
@@ -409,7 +410,7 @@ def is_specialized(f):
409410
func = importlib._bootstrap._handle_fromlist
410411
411412
# "copy" the code to un-specialize it:
412-
func.__code__ = func.__code__.replace()
413+
test.support.reset_code(func)
413414
414415
assert not is_specialized(func), "specialized instructions found"
415416

Lib/test/test_opcache.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77
from test.support import (threading_helper, check_impl_detail,
88
6D40 requires_specialization, requires_specialization_ft,
9-
cpython_only, requires_jit_disabled)
9+
cpython_only, requires_jit_disabled, reset_code)
1010
from test.support.import_helper import import_module
1111

1212
# Skip this module on other interpreters, it is cpython specific:
@@ -579,9 +579,9 @@ def assert_races_do_not_crash(
579579
# Reset:
580580
if check_items:
581581
for item in items:
582-
item.__code__ = item.__code__.replace()
582+
reset_code(item)
583583
else:
584-
read.__code__ = read.__code__.replace()
584+
reset_code(read)
585585
# Specialize:
586586
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
587587
read(items)
@@ -1552,6 +1552,7 @@ def test_store_attr_instance_value(self):
15521552
class C:
15531553
pass
15541554

1555+
@reset_code
15551556
def set_value(n):
15561557
c = C()
15571558
for i in range(n):
@@ -1577,6 +1578,7 @@ class C:
15771578
for i in range(_testinternalcapi.SHARED_KEYS_MAX_SIZE - 1):
15781579
setattr(c, f"_{i}", None)
15791580

1581+
@reset_code
15801582
def set_value(n):
15811583
for i in range(n):
15821584
c.x = i
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``test.support.reset_code``, which can be used to reset various
2+
bytecode-level optimizations and local instrumentation for a function.

0 commit comments

Comments
 (0)
0