Repository: python/cpython
Pull request: gh-121554: remove unnecessary internal functions in compile.c
Fixes #121554.

<!-- gh-issue-number: gh-121554 -->
* Issue: gh-121554
<!-- /gh-issue-number -->


Problem statement:
remove  _PyCompile_OpcodeHas* functions
There are a bunch of functions in compile.c that expose data from pycore_opcode_metadata.h as private C API. These are only used in Modules/_opcode.c.

However, Modules/_opcode.c can just include  pycore_opcode_metadata.h and pycore_opcode_utils.h, and access this data directly. 


<!-- gh-linked-prs -->
### Linked PRs
* gh-121555
<!-- /gh-linked-prs -->

Hints from issue discussion:

Commit message: Regen configure

Patch to review:
diff --git a/Include/internal/pycore_compile.h b/Include/internal/pycore_compile.h
index a1ac034e3e44af..325243e6a64e1f 100644
--- a/Include/internal/pycore_compile.h
+++ b/Include/internal/pycore_compile.h
@@ -76,15 +76,6 @@ int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
 
 
 // Export for '_opcode' extension module
-PyAPI_FUNC(int) _PyCompile_OpcodeIsValid(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasArg(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasConst(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasName(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasJump(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasFree(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasLocal(int opcode);
-PyAPI_FUNC(int) _PyCompile_OpcodeHasExc(int opcode);
-
 PyAPI_FUNC(PyObject*) _PyCompile_GetUnaryIntrinsicName(int index);
 PyAPI_FUNC(PyObject*) _PyCompile_GetBinaryIntrinsicName(int index);
 
diff --git a/Modules/Setup.bootstrap.in b/Modules/Setup.bootstrap.in
index aa4e60e272653b..4dcc0f55176d0e 100644
--- a/Modules/Setup.bootstrap.in
+++ b/Modules/Setup.bootstrap.in
@@ -30,6 +30,7 @@ _weakref _weakref.c
 _abc _abc.c
 _functools _functoolsmodule.c
 _locale _localemodule.c
+_opcode _opcode.c
 _operator _operator.c
 _stat _stat.c
 _symtable symtablemodule.c
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index 78b979698fcd75..dfc75077650df8 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -36,7 +36,6 @@
 @MODULE__HEAPQ_TRUE@_heapq _heapqmodule.c
 @MODULE__JSON_TRUE@_json _json.c
 @MODULE__LSPROF_TRUE@_lsprof _lsprof.c rotatingtree.c
-@MODULE__OPCODE_TRUE@_opcode _opcode.c
 @MODULE__PICKLE_TRUE@_pickle _pickle.c
 @MODULE__QUEUE_TRUE@_queue _queuemodule.c
 @MODULE__RANDOM_TRUE@_random _randommodule.c
diff --git a/Modules/_opcode.c b/Modules/_opcode.c
index 67643641bea861..eef0b3c7a08a74 100644
--- a/Modules/_opcode.c
+++ b/Modules/_opcode.c
@@ -10,6 +10,8 @@
 #include "pycore_compile.h"
 #include "pycore_intrinsics.h"
 #include "pycore_optimizer.h"     // _Py_GetExecutor()
+#include "pycore_opcode_metadata.h" // IS_VALID_OPCODE, OPCODE_HAS_*, etc
+#include "pycore_opcode_utils.h"
 
 /*[clinic input]
 module _opcode
@@ -81,7 +83,7 @@ static int
 _opcode_is_valid_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=b0d918ea1d073f65 input=fe23e0aa194ddae0]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode);
+    return IS_VALID_OPCODE(opcode);
 }
 
 /*[clinic input]
@@ -97,8 +99,7 @@ static int
 _opcode_has_arg_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=7a062d3b2dcc0815 input=93d878ba6361db5f]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasArg(opcode);
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_ARG(opcode);
 }
 
 /*[clinic input]
@@ -114,8 +115,7 @@ static int
 _opcode_has_const_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=c646d5027c634120 input=a6999e4cf13f9410]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasConst(opcode);
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_CONST(opcode);
 }
 
 /*[clinic input]
@@ -131,8 +131,7 @@ static int
 _opcode_has_name_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=b49a83555c2fa517 input=448aa5e4bcc947ba]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasName(opcode);
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_NAME(opcode);
 }
 
 /*[clinic input]
@@ -148,9 +147,7 @@ static int
 _opcode_has_jump_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=e9c583c669f1c46a input=35f711274357a0c3]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasJump(opcode);
-
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_JUMP(opcode);
 }
 
 /*[clinic input]
@@ -171,9 +168,7 @@ static int
 _opcode_has_free_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=d81ae4d79af0ee26 input=117dcd5c19c1139b]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasFree(opcode);
-
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_FREE(opcode);
 }
 
 /*[clinic input]
@@ -189,8 +184,7 @@ static int
 _opcode_has_local_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=da5a8616b7a5097b input=9a798ee24aaef49d]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasLocal(opcode);
+    return IS_VALID_OPCODE(opcode) && OPCODE_HAS_LOCAL(opcode);
 }
 
 /*[clinic input]
@@ -206,8 +200,7 @@ static int
 _opcode_has_exc_impl(PyObject *module, int opcode)
 /*[clinic end generated code: output=41b68dff0ec82a52 input=db0e4bdb9bf13fa5]*/
 {
-    return _PyCompile_OpcodeIsValid(opcode) &&
-           _PyCompile_OpcodeHasExc(opcode);
+    return IS_VALID_OPCODE(opcode) && IS_BLOCK_PUSH_OPCODE(opcode);
 }
 
 /*[clinic input]
@@ -425,7 +418,7 @@ opcode_functions[] =  {
 };
 
 int
-_opcode_exec(PyObject *m) {
+_Py_opcode_exec(PyObject *m) {
     if (PyModule_AddIntMacro(m, ENABLE_SPECIALIZATION) < 0) {
         return -1;
     }
@@ -433,7 +426,7 @@ _opcode_exec(PyObject *m) {
 }
 
 static PyModuleDef_Slot module_slots[] = {
-    {Py_mod_exec, _opcode_exec},
+    {Py_mod_exec, _Py_opcode_exec},
     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
     {Py_mod_gil, Py_MOD_GIL_NOT_USED},
     {0, NULL}
diff --git a/Python/compile.c b/Python/compile.c
index fa70ca101c40e5..e31fa1fdf0f2d0 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -659,54 +659,6 @@ compiler_set_qualname(struct compiler *c)
     return SUCCESS;
 }
 
-int
-_PyCompile_OpcodeIsValid(int opcode)
-{
-    return IS_VALID_OPCODE(opcode);
-}
-
-int
-_PyCompile_OpcodeHasArg(int opcode)
-{
-    return OPCODE_HAS_ARG(opcode);
-}
-
-int
-_PyCompile_OpcodeHasConst(int opcode)
-{
-    return OPCODE_HAS_CONST(opcode);
-}
-
-int
-_PyCompile_OpcodeHasName(int opcode)
-{
-    return OPCODE_HAS_NAME(opcode);
-}
-
-int
-_PyCompile_OpcodeHasJump(int opcode)
-{
-    return OPCODE_HAS_JUMP(opcode);
-}
-
-int
-_PyCompile_OpcodeHasFree(int opcode)
-{
-    return OPCODE_HAS_FREE(opcode);
-}
-
-int
-_PyCompile_OpcodeHasLocal(int opcode)
-{
-    return OPCODE_HAS_LOCAL(opcode);
-}
-
-int
-_PyCompile_OpcodeHasExc(int opcode)
-{
-    return IS_BLOCK_PUSH_OPCODE(opcode);
-}
-
 static int
 codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
 {
diff --git a/configure b/configure
index 131ca5f7f897a7..bbfa805883cac5 100755
--- a/configure
+++ b/configure
@@ -795,8 +795,6 @@ MODULE__POSIXSUBPROCESS_FALSE
 MODULE__POSIXSUBPROCESS_TRUE
 MODULE__PICKLE_FALSE
 MODULE__PICKLE_TRUE
-MODULE__OPCODE_FALSE
-MODULE__OPCODE_TRUE
 MODULE__LSPROF_FALSE
 MODULE__LSPROF_TRUE
 MODULE__JSON_FALSE
@@ -29231,28 +29229,6 @@ then :
 
 
 
-fi
-
-
-        if test "$py_cv_module__opcode" != "n/a"
-then :
-  py_cv_module__opcode=yes
-fi
-   if test "$py_cv_module__opcode" = yes; then
-  MODULE__OPCODE_TRUE=
-  MODULE__OPCODE_FALSE='#'
-else
-  MODULE__OPCODE_TRUE='#'
-  MODULE__OPCODE_FALSE=
-fi
-
-  as_fn_append MODULE_BLOCK "MODULE__OPCODE_STATE=$py_cv_module__opcode$as_nl"
-  if test "x$py_cv_module__opcode" = xyes
-then :
-
-
-
-
 fi
 
 
@@ -31784,10 +31760,6 @@ if test -z "${MODULE__LSPROF_TRUE}" && test -z "${MODULE__LSPROF_FALSE}"; then
   as_fn_error $? "conditional \"MODULE__LSPROF\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
-if test -z "${MODULE__OPCODE_TRUE}" && test -z "${MODULE__OPCODE_FALSE}"; then
-  as_fn_error $? "conditional \"MODULE__OPCODE\" was never defined.
-Usually this means the macro was only invoked conditionally." "$LINENO" 5
-fi
 if test -z "${MODULE__PICKLE_TRUE}" && test -z "${MODULE__PICKLE_FALSE}"; then
   as_fn_error $? "conditional \"MODULE__PICKLE\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
diff --git a/configure.ac b/configure.ac
index 705f8752597b96..87c4df20818808 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7689,7 +7689,6 @@ PY_STDLIB_MOD_SIMPLE([_csv])
 PY_STDLIB_MOD_SIMPLE([_heapq])
 PY_STDLIB_MOD_SIMPLE([_json])
 PY_STDLIB_MOD_SIMPLE([_lsprof])
-PY_STDLIB_MOD_SIMPLE([_opcode])
 PY_STDLIB_MOD_SIMPLE([_pickle])
 PY_STDLIB_MOD_SIMPLE([_posixsubprocess])
 PY_STDLIB_MOD_SIMPLE([_queue])


Code review comments:
diff_hunk: @@ -425,15 +418,15 @@ opcode_functions[] =  {
 };
 
 int
-_opcode_exec(PyObject *m) {
+_Py_opcode_exec(PyObject *m) {
path: Modules/_opcode.c
text: @user1:
Why this name change? It looks to me it's only used in L429, right?

@author:
smelly was complaining about this.

@user1:
Ah, it's probably because it's not declared as `static`. It should be a file local function.

@author:
I'll try that.