[Python-checkins] gh-106320: Remove private _PyImport C API functions (#106383)

vstinner webhook-mailer at python.org
Mon Jul 3 19:02:10 EDT 2023


https://github.com/python/cpython/commit/2e92edbf6de9578b30cca8e48c4bfb2ba71ae97a
commit: 2e92edbf6de9578b30cca8e48c4bfb2ba71ae97a
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-07-03T23:02:07Z
summary:

gh-106320: Remove private _PyImport C API functions (#106383)

* Remove private _PyImport C API functions: move them to the internal
  C API (pycore_import.h).
* No longer export most of these private functions.
* _testcapi avoids private _PyImport_GetModuleAttrString().

files:
M Include/cpython/import.h
M Include/internal/pycore_import.h
M Modules/_elementtree.c
M Modules/_sqlite/connection.c
M Modules/_sqlite/module.c
M Modules/_testcapimodule.c
M Modules/cjkcodecs/cjkcodecs.h
M Modules/pyexpat.c

diff --git a/Include/cpython/import.h b/Include/cpython/import.h
index 2bca4ade4c4f2..cdfdd15bfa48d 100644
--- a/Include/cpython/import.h
+++ b/Include/cpython/import.h
@@ -4,23 +4,6 @@
 
 PyMODINIT_FUNC PyInit__imp(void);
 
-PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
-
-PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(_Py_Identifier *name);
-PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
-PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
-
-PyAPI_FUNC(void) _PyImport_AcquireLock(PyInterpreterState *interp);
-PyAPI_FUNC(int) _PyImport_ReleaseLock(PyInterpreterState *interp);
-
-PyAPI_FUNC(int) _PyImport_FixupBuiltin(
-    PyObject *mod,
-    const char *name,            /* UTF-8 encoded string */
-    PyObject *modules
-    );
-PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *,
-                                               PyObject *, PyObject *);
-
 struct _inittab {
     const char *name;           /* ASCII encoded string */
     PyObject* (*initfunc)(void);
@@ -41,6 +24,3 @@ struct _frozen {
    collection of frozen modules: */
 
 PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;
-
-PyAPI_DATA(PyObject *) _PyImport_GetModuleAttr(PyObject *, PyObject *);
-PyAPI_DATA(PyObject *) _PyImport_GetModuleAttrString(const char *, const char *);
diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h
index ee93f7d99d915..457a654aff464 100644
--- a/Include/internal/pycore_import.h
+++ b/Include/internal/pycore_import.h
@@ -7,6 +7,26 @@ extern "C" {
 
 #include "pycore_time.h"          // _PyTime_t
 
+extern int _PyImport_IsInitialized(PyInterpreterState *);
+
+PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(_Py_Identifier *name);
+PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
+PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
+
+extern void _PyImport_AcquireLock(PyInterpreterState *interp);
+extern int _PyImport_ReleaseLock(PyInterpreterState *interp);
+
+extern int _PyImport_FixupBuiltin(
+    PyObject *mod,
+    const char *name,            /* UTF-8 encoded string */
+    PyObject *modules
+    );
+extern int _PyImport_FixupExtensionObject(PyObject*, PyObject *,
+                                          PyObject *, PyObject *);
+
+PyAPI_DATA(PyObject *) _PyImport_GetModuleAttr(PyObject *, PyObject *);
+PyAPI_DATA(PyObject *) _PyImport_GetModuleAttrString(const char *, const char *);
+
 
 struct _import_runtime_state {
     /* The builtin modules (defined in config.c). */
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 48280690a707a..3e742e067e7db 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -11,7 +11,12 @@
  *--------------------------------------------------------------------
  */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
+#include "pycore_import.h"        // _PyImport_GetModuleAttrString()
 #include "structmember.h"         // PyMemberDef
 #include "expat.h"
 #include "pyexpat.h"
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 967ba2812080e..d71cef14779e5 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -33,6 +33,7 @@
 #include "blob.h"
 #include "prepare_protocol.h"
 #include "util.h"
+#include "pycore_import.h"        // _PyImport_GetModuleAttrString()
 #include "pycore_weakref.h"       // _PyWeakref_IS_DEAD()
 
 #include <stdbool.h>
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index ea4d8c58b7ee0..368e581b4f335 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -21,6 +21,10 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "connection.h"
 #include "statement.h"
 #include "cursor.h"
@@ -29,6 +33,8 @@
 #include "row.h"
 #include "blob.h"
 
+#include "pycore_import.h"        // _PyImport_GetModuleAttrString()
+
 #if SQLITE_VERSION_NUMBER < 3015002
 #error "SQLite 3.15.2 or higher required"
 #endif
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index ce1131743eb2a..d1044b5445202 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1267,9 +1267,15 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
     if (ret != -1 || match == 0)
         goto error;
 
+    PyObject *mod_io = PyImport_ImportModule("_io");
+    if (mod_io == NULL) {
+        return NULL;
+    }
+
     /* bytesiobuf_getbuffer() */
-    PyTypeObject *type = (PyTypeObject *)_PyImport_GetModuleAttrString(
-            "_io", "_BytesIOBuffer");
+    PyTypeObject *type = (PyTypeObject *)PyObject_GetAttrString(
+            mod_io, "_BytesIOBuffer");
+    Py_DECREF(mod_io);
     if (type == NULL) {
         return NULL;
     }
diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h
index 97290aac3ba43..ee588785e7403 100644
--- a/Modules/cjkcodecs/cjkcodecs.h
+++ b/Modules/cjkcodecs/cjkcodecs.h
@@ -13,6 +13,7 @@
 
 #include "Python.h"
 #include "multibytecodec.h"
+#include "pycore_import.h"        // _PyImport_GetModuleAttrString()
 
 
 /* a unicode "undefined" code point */
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index e3333fff00b2b..28915359fb49e 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1,4 +1,9 @@
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
+#include "pycore_import.h"        // _PyImport_SetModule()
 #include <ctype.h>
 
 #include "structmember.h"         // PyMemberDef



More information about the Python-checkins mailing list