Skip to content

gh-133644: remove deprecated PyImport_ImportModuleNoBlock #133655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Doc/c-api/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ Importing Modules
This is a wrapper around :c:func:`PyImport_Import()` which takes a
:c:expr:`const char *` as an argument instead of a :c:expr:`PyObject *`.

.. c:function:: PyObject* PyImport_ImportModuleNoBlock(const char *name)

This function is a deprecated alias of :c:func:`PyImport_ImportModule`.

.. versionchanged:: 3.3
This function used to fail immediately when the import lock was held
by another thread. In Python 3.3 though, the locking scheme switched
to per-module locks for most purposes, so this function's special
behaviour isn't needed anymore.

.. deprecated-removed:: 3.13 3.15
Use :c:func:`PyImport_ImportModule` instead.


.. c:function:: PyObject* PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)

Expand Down
3 changes: 0 additions & 3 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,6 @@ PyImport_ImportModuleLevelObject:PyObject*:locals:0:???
PyImport_ImportModuleLevelObject:PyObject*:fromlist:0:???
PyImport_ImportModuleLevelObject:int:level::

PyImport_ImportModuleNoBlock:PyObject*::+1:
PyImport_ImportModuleNoBlock:const char*:name::

PyImport_ReloadModule:PyObject*::+1:
PyImport_ReloadModule:PyObject*:m:0:

Expand Down
1 change: 0 additions & 1 deletion Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Doc/deprecations/c-api-pending-removal-in-3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pending removal in Python 3.15
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* The bundled copy of ``libmpdecimal``.
* The :c:func:`PyImport_ImportModuleNoBlock`:
* The :c:func:`!PyImport_ImportModuleNoBlock`:
Use :c:func:`PyImport_ImportModule` instead.
* :c:func:`PyWeakref_GetObject` and :c:func:`PyWeakref_GET_OBJECT`:
Use :c:func:`PyWeakref_GetRef` instead. The `pythoncapi-compat project
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/2.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3043,7 +3043,7 @@ Changes to Python's build process and to the C API include:

* Importing modules simultaneously in two different threads no longer
deadlocks; it will now raise an :exc:`ImportError`. A new API
function, :c:func:`PyImport_ImportModuleNoBlock`, will look for a
function, :c:func:`!PyImport_ImportModuleNoBlock`, will look for a
module in ``sys.modules`` first, then try to import it after
acquiring an import lock. If the import lock is held by another
thread, an :exc:`ImportError` is raised.
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ to the C API.
* :c:func:`!PyNumber_Coerce`, :c:func:`!PyNumber_CoerceEx`,
:c:func:`!PyMember_Get`, and :c:func:`!PyMember_Set` C APIs are removed.

* New C API :c:func:`PyImport_ImportModuleNoBlock`, works like
* New C API :c:func:`!PyImport_ImportModuleNoBlock`, works like
:c:func:`PyImport_ImportModule` but won't block on the import lock
(returning an error instead).

Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ Deprecated C APIs
which return a :term:`borrowed reference`.
(Soft deprecated as part of :pep:`667`.)

* Deprecate the :c:func:`PyImport_ImportModuleNoBlock` function,
* Deprecate the :c:func:`!PyImport_ImportModuleNoBlock` function,
which is just an alias to :c:func:`PyImport_ImportModule` since Python 3.3.
(Contributed by Victor Stinner in :gh:`105396`.)

Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,5 @@ Deprecated C APIs
Removed C APIs
--------------

* :c:func:`!PyImport_ImportModuleNoBlock`: deprecated alias
of :c:func:`PyImport_ImportModule`.
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ Previous versions of CPython have always relied on a global import lock.
This led to unexpected annoyances, such as deadlocks when importing a module
would trigger code execution in a different thread as a side-effect.
Clumsy workarounds were sometimes employed, such as the
:c:func:`PyImport_ImportModuleNoBlock` C API function.
:c:func:`!PyImport_ImportModuleNoBlock` C API function.

In Python 3.3, importing a module takes a per-module lock. This correctly
serializes importation of a given module from multiple threads (preventing
Expand Down
3 changes: 0 additions & 3 deletions Include/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ PyAPI_FUNC(PyObject *) PyImport_AddModuleRef(
PyAPI_FUNC(PyObject *) PyImport_ImportModule(
const char *name /* UTF-8 encoded string */
);
Py_DEPRECATED(3.13) PyAPI_FUNC(PyObject *) PyImport_ImportModuleNoBlock(
const char *name /* UTF-8 encoded string */
);
PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevel(
const char *name, /* UTF-8 encoded string */
PyObject *globals,
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_importmodule(self):
# CRASHES importmodule(NULL)

def test_importmodulenoblock(self):
# Test deprecated PyImport_ImportModuleNoBlock()
# Test deprecated (stable ABI only) PyImport_ImportModuleNoBlock()
importmodulenoblock = _testlimitedcapi.PyImport_ImportModuleNoBlock
with check_warnings(('', DeprecationWarning)):
self.check_import_func(importmodulenoblock)
Expand Down
2 changes: 1 addition & 1 deletion Misc/NEWS.d/3.11.0a4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ no-op now.
.. nonce: Lq2_gR
.. section: C API

Replaced deprecated usage of :c:func:`PyImport_ImportModuleNoBlock` with
Replaced deprecated usage of :c:func:`!PyImport_ImportModuleNoBlock` with
:c:func:`PyImport_ImportModule` in stdlib modules. Patch by Kumar Aditya.

..
Expand Down
2 changes: 1 addition & 1 deletion Misc/NEWS.d/3.13.0a1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6538,7 +6538,7 @@ to hide implementation details. Patch by Victor Stinner.
.. nonce: FQJG5B
.. section: C API

Deprecate the :c:func:`PyImport_ImportModuleNoBlock` function which is just
Deprecate the :c:func:`!PyImport_ImportModuleNoBlock` function which is just
an alias to :c:func:`PyImport_ImportModule` since Python 3.3. Patch by
Victor Stinner.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove deprecated alias :c:func:`!PyImport_ImportModuleNoBlock` of
:c:func:`PyImport_ImportModule`. Patch by Bénédikt Tran.
1 change: 1 addition & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@
added = '3.2'
[function.PyImport_ImportModuleNoBlock]
added = '3.2'
abi_only = true
[function.PyImport_ReloadModule]
added = '3.2'
[function.PyInterpreterState_Clear]
Expand Down
9 changes: 4 additions & 5 deletions Modules/_testlimitedcapi/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,19 @@ pyimport_importmodule(PyObject *Py_UNUSED(module), PyObject *args)
}


/* Test PyImport_ImportModuleNoBlock() */
/* Test PyImport_ImportModuleNoBlock() (removed in 3.15) */
static PyObject *
pyimport_importmodulenoblock(PyObject *Py_UNUSED(module), PyObject *args)
{
// Get the function from the stable ABI.
PyAPI_FUNC(PyObject *) PyImport_ImportModuleNoBlock(const char *name);

const char *name;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
return NULL;
}

_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
return PyImport_ImportModuleNoBlock(name);
_Py_COMP_DIAG_POP
}


Expand Down
4 changes: 3 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3434,8 +3434,10 @@ PyImport_ImportModule(const char *name)
* ImportError instead of blocking.
*
* Returns the module object with incremented ref count.
*
* Removed in 3.15, but kept for stable ABI compatibility.
*/
PyObject *
PyAPI_FUNC(PyObject *)
PyImport_ImportModuleNoBlock(const char *name)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
Expand Down
Loading