Skip to content

Commit 2e92edb

Browse files
authored
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().
1 parent f6d2bb1 commit 2e92edb

File tree

8 files changed

+46
-22
lines changed

8 files changed

+46
-22
lines changed

Include/cpython/import.h

-20
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@
44

55
PyMODINIT_FUNC PyInit__imp(void);
66

7-
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
8-
9-
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(_Py_Identifier *name);
10-
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
11-
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
12-
13-
PyAPI_FUNC(void) _PyImport_AcquireLock(PyInterpreterState *interp);
14-
PyAPI_FUNC(int) _PyImport_ReleaseLock(PyInterpreterState *interp);
15-
16-
PyAPI_FUNC(int) _PyImport_FixupBuiltin(
17-
PyObject *mod,
18-
const char *name, /* UTF-8 encoded string */
19-
PyObject *modules
20-
);
21-
PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *,
22-
PyObject *, PyObject *);
23-
247
struct _inittab {
258
const char *name; /* ASCII encoded string */
269
PyObject* (*initfunc)(void);
@@ -41,6 +24,3 @@ struct _frozen {
4124
collection of frozen modules: */
4225

4326
PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;
44-
45-
PyAPI_DATA(PyObject *) _PyImport_GetModuleAttr(PyObject *, PyObject *);
46-
PyAPI_DATA(PyObject *) _PyImport_GetModuleAttrString(const char *, const char *);

Include/internal/pycore_import.h

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ extern "C" {
77

88
#include "pycore_time.h" // _PyTime_t
99

10+
extern int _PyImport_IsInitialized(PyInterpreterState *);
11+
12+
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(_Py_Identifier *name);
13+
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
14+
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
15+
16+
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
17+
extern int _PyImport_ReleaseLock(PyInterpreterState *interp);
18+
19+
extern int _PyImport_FixupBuiltin(
20+
PyObject *mod,
21+
const char *name, /* UTF-8 encoded string */
22+
PyObject *modules
23+
);
24+
extern int _PyImport_FixupExtensionObject(PyObject*, PyObject *,
25+
PyObject *, PyObject *);
26+
27+
PyAPI_DATA(PyObject *) _PyImport_GetModuleAttr(PyObject *, PyObject *);
28+
PyAPI_DATA(PyObject *) _PyImport_GetModuleAttrString(const char *, const char *);
29+
1030

1131
struct _import_runtime_state {
1232
/* The builtin modules (defined in config.c). */

Modules/_elementtree.c

+5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
*--------------------------------------------------------------------
1212
*/
1313

14+
#ifndef Py_BUILD_CORE_BUILTIN
15+
# define Py_BUILD_CORE_MODULE 1
16+
#endif
17+
1418
#include "Python.h"
19+
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
1520
#include "structmember.h" // PyMemberDef
1621
#include "expat.h"
1722
#include "pyexpat.h"

Modules/_sqlite/connection.c

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "blob.h"
3434
#include "prepare_protocol.h"
3535
#include "util.h"
36+
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
3637
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
3738

3839
#include <stdbool.h>

Modules/_sqlite/module.c

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24+
#ifndef Py_BUILD_CORE_BUILTIN
25+
# define Py_BUILD_CORE_MODULE 1
26+
#endif
27+
2428
#include "connection.h"
2529
#include "statement.h"
2630
#include "cursor.h"
@@ -29,6 +33,8 @@
2933
#include "row.h"
3034
#include "blob.h"
3135

36+
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
37+
3238
#if SQLITE_VERSION_NUMBER < 3015002
3339
#error "SQLite 3.15.2 or higher required"
3440
#endif

Modules/_testcapimodule.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1267,9 +1267,15 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
12671267
if (ret != -1 || match == 0)
12681268
goto error;
12691269

1270+
PyObject *mod_io = PyImport_ImportModule("_io");
1271+
if (mod_io == NULL) {
1272+
return NULL;
1273+
}
1274+
12701275
/* bytesiobuf_getbuffer() */
1271-
PyTypeObject *type = (PyTypeObject *)_PyImport_GetModuleAttrString(
1272-
"_io", "_BytesIOBuffer");
1276+
PyTypeObject *type = (PyTypeObject *)PyObject_GetAttrString(
1277+
mod_io, "_BytesIOBuffer");
1278+
Py_DECREF(mod_io);
12731279
if (type == NULL) {
12741280
return NULL;
12751281
}

Modules/cjkcodecs/cjkcodecs.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "Python.h"
1515
#include "multibytecodec.h"
16+
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
1617

1718

1819
/* a unicode "undefined" code point */

Modules/pyexpat.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
#ifndef Py_BUILD_CORE_BUILTIN
2+
# define Py_BUILD_CORE_MODULE 1
3+
#endif
4+
15
#include "Python.h"
6+
#include "pycore_import.h" // _PyImport_SetModule()
27
#include <ctype.h>
38

49
#include "structmember.h" // PyMemberDef

0 commit comments

Comments
 (0)