Skip to content

Commit

Permalink
pythongh-104212: Explain how to port imp code to importlib
Browse files Browse the repository at this point in the history
Explain in What's New in Python 3.12 how to port existing code using
the removed imp to the importlib module.
  • Loading branch information
vstinner committed Jun 19, 2023
1 parent 4426279 commit ff274e9
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,37 @@ Removed
* The :mod:`!imp` module has been removed. (Contributed by Barry Warsaw in
:gh:`98040`.)

* Replace ``imp.new_module(name)`` with ``types.ModuleType(name)``.
* Replace removed ``imp`` functions with :mod:`importlib` functions:

================================= =====================================
imp importlib
================================= =====================================
``imp.NullImporter`` Insert ``None`` into ``sys.path_importer_cache``
``imp.cache_from_source()`` :func:`importlib.util.cache_from_source`
``imp.find_module()`` :func:`importlib.util.find_spec`
``imp.get_magic()`` :attr:`importlib.util.MAGIC_NUMBER`
``imp.get_suffixes()`` :attr:`importlib.machinery.SOURCE_SUFFIXES`, :attr:`importlib.machinery.EXTENSION_SUFFIXES`, and :attr:`importlib.machinery.BYTECODE_SUFFIXES`
``imp.get_tag()`` :attr:`sys.implementation.cache_tag`
``imp.load_module()`` :func:`importlib.import_module`
``imp.new_module(name)`` ``types.ModuleType(name)``.
``imp.reload()`` :func:`importlib.reload`
``imp.source_from_cache()`` :func:`importlib.util.source_from_cache`

* Removed ``imp`` functions and attributes with no replacements:

* undocumented functions:

* ``imp.init_builtin()``
* ``imp.load_compiled()``
* ``imp.load_dynamic()``
* ``imp.load_package()``
* ``imp.load_source()``

* ``imp.lock_held()``, ``imp.acquire_lock()``, ``imp.release_lock()``:
the locking scheme has changed in Python 3.3 to per-module locks.
* :func:`find_module` constants: ``SEARCH_ERROR``, ``PY_SOURCE``,
``PY_COMPILED``, ``C_EXTENSION``, ``PY_RESOURCE``, ``PKG_DIRECTORY``,
``C_BUILTIN``, ``PY_FROZEN``, ``PY_CODERESOURCE``, ``IMP_HOOK``.

* Removed the ``suspicious`` rule from the documentation Makefile, and
removed ``Doc/tools/rstlint.py``, both in favor of `sphinx-lint
Expand Down

0 comments on commit ff274e9

Please sign in to comment.