Skip to content

Commit

Permalink
pythongh-104212: Explain how to port imp.load_source() (pythonGH-105978)
Browse files Browse the repository at this point in the history
Explain how to port removed imp.load_source() to importlib in What's
New in Python 3.12.
(cherry picked from commit 18a7c86)

Co-authored-by: Victor Stinner <vstinner@python.org>
  • Loading branch information
vstinner authored and miss-islington committed Jun 25, 2023
1 parent f955ed9 commit 5db404b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,21 @@ Removed
``imp.source_from_cache()`` :func:`importlib.util.source_from_cache`
================================= =======================================

* Replace ``imp.load_source()`` with::

import importlib.util
import importlib.machinery

def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module

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

* undocumented functions:
Expand All @@ -1393,7 +1408,6 @@ Removed
* ``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.
Expand Down

0 comments on commit 5db404b

Please sign in to comment.