From aa05ebabb98f0b953ef30d6f688f9b5030ac84e6 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 1 Aug 2023 13:16:08 +0100 Subject: [PATCH 1/5] gh-105481: ENABLE_SPECIALIZATION does not need to be generated by the build script --- Include/internal/pycore_code.h | 2 ++ Include/opcode.h | 2 -- Lib/opcode.py | 4 +--- Modules/_opcode.c | 9 +++++++++ Tools/build/generate_opcode_h.py | 5 ----- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index bcdf8645c8557c..ee1b85187cbab6 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -229,6 +229,8 @@ extern void _PyLineTable_InitAddressRange( extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range); extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range); +#define ENABLE_SPECIALIZATION 1 + /* Specialization functions */ extern void _Py_Specialize_LoadSuperAttr(PyObject *global_super, PyObject *cls, diff --git a/Include/opcode.h b/Include/opcode.h index 697520937d9055..eb4bb85e5b667d 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -256,8 +256,6 @@ extern "C" { #define NB_INPLACE_TRUE_DIVIDE 24 #define NB_INPLACE_XOR 25 -/* Defined in Lib/opcode.py */ -#define ENABLE_SPECIALIZATION 1 #ifdef __cplusplus } diff --git a/Lib/opcode.py b/Lib/opcode.py index bed922399821a6..1b0e4db21baa38 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -16,12 +16,10 @@ # The build uses older versions of Python which do not have _opcode_metadata if sys.version_info[:2] >= (3, 13): from _opcode_metadata import _specializations, _specialized_instructions + from _opcode import ENABLE_SPECIALIZATION cmp_op = ('<', '<=', '==', '!=', '>', '>=') - -ENABLE_SPECIALIZATION = True - def is_pseudo(op): return op >= MIN_PSEUDO_OPCODE and op <= MAX_PSEUDO_OPCODE diff --git a/Modules/_opcode.c b/Modules/_opcode.c index b8d95a048ec2c6..ad0fa736f82767 100644 --- a/Modules/_opcode.c +++ b/Modules/_opcode.c @@ -292,7 +292,16 @@ opcode_functions[] = { {NULL, NULL, 0, NULL} }; +int +_opcode_exec(PyObject *m) { + if (PyModule_AddIntMacro(m, ENABLE_SPECIALIZATION) < 0) { + return -1; + } + return 0; +} + static PyModuleDef_Slot module_slots[] = { + {Py_mod_exec, _opcode_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {0, NULL} }; diff --git a/Tools/build/generate_opcode_h.py b/Tools/build/generate_opcode_h.py index 19e5eab822a235..2259dad77869f8 100644 --- a/Tools/build/generate_opcode_h.py +++ b/Tools/build/generate_opcode_h.py @@ -73,7 +73,6 @@ def main(opcode_py, opname = opcode['opname'] is_pseudo = opcode['is_pseudo'] - ENABLE_SPECIALIZATION = opcode["ENABLE_SPECIALIZATION"] MIN_PSEUDO_OPCODE = opcode["MIN_PSEUDO_OPCODE"] MAX_PSEUDO_OPCODE = opcode["MAX_PSEUDO_OPCODE"] MIN_INSTRUMENTED_OPCODE = opcode["MIN_INSTRUMENTED_OPCODE"] @@ -141,10 +140,6 @@ def main(opcode_py, for i, (op, _) in enumerate(opcode["_nb_ops"]): fobj.write(DEFINE.format(op, i)) - fobj.write("\n") - fobj.write("/* Defined in Lib/opcode.py */\n") - fobj.write(f"#define ENABLE_SPECIALIZATION {int(ENABLE_SPECIALIZATION)}") - iobj.write("\n") iobj.write(f"\nextern const char *const _PyOpcode_OpName[{NUM_OPCODES}];\n") iobj.write("\n#ifdef NEED_OPCODE_TABLES\n") From 8489449ec1d925c6e9edc273aad1da6358ef4523 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 1 Aug 2023 14:58:17 +0100 Subject: [PATCH 2/5] opcode.py doesn't need ENABLE_SPECIALIZATION --- Lib/opcode.py | 1 - Lib/test/support/__init__.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/opcode.py b/Lib/opcode.py index 1b0e4db21baa38..51432ab1fab3f1 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -16,7 +16,6 @@ # The build uses older versions of Python which do not have _opcode_metadata if sys.version_info[:2] >= (3, 13): from _opcode_metadata import _specializations, _specialized_instructions - from _opcode import ENABLE_SPECIALIZATION cmp_op = ('<', '<=', '==', '!=', '>', '>=') diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 3b332f49951f0c..2a59911e54d6b9 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -6,7 +6,7 @@ import contextlib import functools import getpass -import opcode +import _opcode import os import re import stat @@ -1092,7 +1092,7 @@ def requires_limited_api(test): def requires_specialization(test): return unittest.skipUnless( - opcode.ENABLE_SPECIALIZATION, "requires specialization")(test) + _opcode.ENABLE_SPECIALIZATION, "requires specialization")(test) def _filter_suite(suite, pred): """Recursively filter test cases in a suite based on a predicate.""" From 4e300fc86c6875cf5233a54a9ceec5c3273ea08f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 1 Aug 2023 15:17:30 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-08-01-15-17-20.gh-issue-105481.vMbmj_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-01-15-17-20.gh-issue-105481.vMbmj_.rst diff --git a/Misc/NEWS.d/next/Library/2023-08-01-15-17-20.gh-issue-105481.vMbmj_.rst b/Misc/NEWS.d/next/Library/2023-08-01-15-17-20.gh-issue-105481.vMbmj_.rst new file mode 100644 index 00000000000000..153c18a6f00953 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-01-15-17-20.gh-issue-105481.vMbmj_.rst @@ -0,0 +1 @@ +:data:`opcode.ENABLE_SPECIALIZATION` (which was added in 3.12 but never documented or intended for external usage) is moved to :data:`_opcode.ENABLE_SPECIALIZATION` where tests can access it. From bf9bb58dd5155152a2d189282515f9c397664595 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 1 Aug 2023 17:08:26 +0100 Subject: [PATCH 4/5] add what's new entry --- Doc/whatsnew/3.13.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 8fb4e6cfdf14c7..ab21215192487c 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -117,6 +117,13 @@ and only logged in :ref:`Python Development Mode ` or on :ref:`Python built on debug mode `. (Contributed by Victor Stinner in :gh:`62948`.) +opcode +------ + +* Move :data:`opcode.ENABLE_SPECIALIZATION` to :data:`_opcode.ENABLE_SPECIALIZATION`. + This field was added in 3.12, it was never documented and is not intended for + external usage. (Contributed by Irit Katriel in :gh:`105481`.) + pathlib ------- From 1c6d8d0c79fb22d048b8c0e9fd8ddc9f9184d6a6 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 1 Aug 2023 17:21:56 +0100 Subject: [PATCH 5/5] fix doc formatting --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index ab21215192487c..22c1e03470f5dc 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -120,7 +120,7 @@ built on debug mode `. opcode ------ -* Move :data:`opcode.ENABLE_SPECIALIZATION` to :data:`_opcode.ENABLE_SPECIALIZATION`. +* Move ``opcode.ENABLE_SPECIALIZATION`` to ``_opcode.ENABLE_SPECIALIZATION``. This field was added in 3.12, it was never documented and is not intended for external usage. (Contributed by Irit Katriel in :gh:`105481`.)