]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104212: Explain how to port imp.load_source() (#105978)
authorVictor Stinner <vstinner@python.org>
Wed, 21 Jun 2023 22:18:31 +0000 (00:18 +0200)
committerGitHub <noreply@github.com>
Wed, 21 Jun 2023 22:18:31 +0000 (00:18 +0200)
Explain how to port removed imp.load_source() to importlib in What's
New in Python 3.12.

Doc/whatsnew/3.12.rst

index 1a50bce0b65e0bfcf7434e188b2c6c19621f119a..d8a236080c0056078907bad9094e4552dc56b34a 100644 (file)
@@ -1386,6 +1386,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:
@@ -1394,7 +1409,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.