diff options
author | mpage <mpage@meta.com> | 2024-12-19 21:03:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-19 21:03:14 (GMT) |
commit | 255762c09fe518757bb3e8ce1bb6e5d8eec9f466 (patch) | |
tree | 179d8f0893a0550d2f73359da6e20d4df05e01e2 /Lib | |
parent | e163e8d4e1a9844b8615ef38b9917b887a377948 (diff) | |
download | cpython-255762c09fe518757bb3e8ce1bb6e5d8eec9f466.zip cpython-255762c09fe518757bb3e8ce1bb6e5d8eec9f466.tar.gz cpython-255762c09fe518757bb3e8ce1bb6e5d8eec9f466.tar.bz2 |
gh-127274: Defer nested methods (#128012)
Methods (functions defined in class scope) are likely to be cleaned
up by the GC anyway.
Add a new code flag, `CO_METHOD`, that is set for functions defined
in a class scope. Use that when deciding to defer functions.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/dis.py | 1 | ||||
-rw-r--r-- | Lib/inspect.py | 1 | ||||
-rw-r--r-- | Lib/test/test_monitoring.py | 9 | ||||
-rw-r--r-- | Lib/test/test_opcache.py | 11 |
4 files changed, 9 insertions, 13 deletions
@@ -162,6 +162,7 @@ COMPILER_FLAG_NAMES = { 256: "ITERABLE_COROUTINE", 512: "ASYNC_GENERATOR", 0x4000000: "HAS_DOCSTRING", + 0x8000000: "METHOD", } def pretty_flags(flags): diff --git a/Lib/inspect.py b/Lib/inspect.py index b7d8271..5b7c4df 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -57,6 +57,7 @@ __all__ = [ "CO_VARARGS", "CO_VARKEYWORDS", "CO_HAS_DOCSTRING", + "CO_METHOD", "ClassFoundException", "ClosureVars", "EndOfBlock", diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 087ac8d..32b3a6a 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -850,12 +850,6 @@ class ReturnRecorder: def __call__(self, code, offset, val): self.events.append(("return", code.co_name, val)) -# gh-127274: CALL_ALLOC_AND_ENTER_INIT will only cache __init__ methods that -# are deferred. We only defer functions defined at the top-level. -class ValueErrorRaiser: - def __init__(self): - raise ValueError() - class ExceptionMonitoringTest(CheckEvents): @@ -1054,6 +1048,9 @@ class ExceptionMonitoringTest(CheckEvents): @requires_specialization_ft def test_no_unwind_for_shim_frame(self): + class ValueErrorRaiser: + def __init__(self): + raise ValueError() def f(): try: diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py index ad0b0c4..ba111b5 100644 --- a/Lib/test/test_opcache.py +++ b/Lib/test/test_opcache.py @@ -493,13 +493,6 @@ class TestLoadMethodCache(unittest.TestCase): self.assertFalse(f()) -# gh-127274: CALL_ALLOC_AND_ENTER_INIT will only cache __init__ methods that -# are deferred. We only defer functions defined at the top-level. -class MyClass: - def __init__(self): - pass - - class InitTakesArg: def __init__(self, arg): self.arg = arg @@ -536,6 +529,10 @@ class TestCallCache(TestBase): @disabling_optimizer @requires_specialization_ft def test_assign_init_code(self): + class MyClass: + def __init__(self): + pass + def instantiate(): return MyClass() |