Skip to content

Commit 2ca4ab8

Browse files
[3.10] bpo-45307: Restore private C API function _PyImport_FindExtensionObject() (GH-28594)
py2exe and PyOxidizer rely on this API. It will be removed in Python 3.11. Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 3397e31 commit 2ca4ab8

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Include/cpython/import.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
1313
PyAPI_FUNC(void) _PyImport_AcquireLock(void);
1414
PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
1515

16+
/* Obsolete since 3.5, will be removed in 3.11. */
17+
Py_DEPRECATED(3.10) PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObject(PyObject *, PyObject *);
18+
1619
PyAPI_FUNC(int) _PyImport_FixupBuiltin(
1720
PyObject *mod,
1821
const char *name, /* UTF-8 encoded string */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore the private C API function :func:`_PyImport_FindExtensionObject`. It
2+
will be removed in Python 3.11.

Python/import.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,23 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
556556
return mod;
557557
}
558558

559+
PyObject *
560+
_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
561+
{
562+
PyThreadState *tstate = _PyThreadState_GET();
563+
PyObject *mod = import_find_extension(tstate, name, filename);
564+
if (mod) {
565+
PyObject *ref = PyWeakref_NewRef(mod, NULL);
566+
Py_DECREF(mod);
567+
if (ref == NULL) {
568+
return NULL;
569+
}
570+
mod = PyWeakref_GetObject(ref);
571+
Py_DECREF(ref);
572+
}
573+
return mod; /* borrowed reference */
574+
}
575+
559576

560577
/* Get the module object corresponding to a module name.
561578
First check the modules dictionary if there's one there,

0 commit comments

Comments
 (0)