summaryrefslogtreecommitdiffstats
path: root/Doc/whatsnew
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-06-25 15:14:18 (GMT)
committerGitHub <noreply@github.com>2023-06-25 15:14:18 (GMT)
commite9366df3ecb360882020131def3f5c400ae54ba8 (patch)
tree7b06b9620c22db0df73629c661da34870a8750bc /Doc/whatsnew
parentf955ed9da74e50f3901fbefb2e9a9309563077c0 (diff)
downloadcpython-e9366df3ecb360882020131def3f5c400ae54ba8.zip
cpython-e9366df3ecb360882020131def3f5c400ae54ba8.tar.gz
cpython-e9366df3ecb360882020131def3f5c400ae54ba8.tar.bz2
[3.12] gh-104212: Explain how to port imp.load_source() (GH-105978) (#106083)
gh-104212: Explain how to port imp.load_source() (GH-105978) Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12. (cherry picked from commit 18a7c86697493510993e43bafe8bd4046928bec5) Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r--Doc/whatsnew/3.12.rst16
1 files changed, 15 insertions, 1 deletions
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index ed4657c..e06faf3 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -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:
@@ -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.