From 701629fb4d7028d780821a28291ab5e0e4f1a466 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 21 Jun 2023 01:01:52 +0200 Subject: [PATCH 1/2] gh-104212: Explain how to port imp.load_source() Explain how to port removed imp "load" functions to Python 3.13 in What's New in Python 3.12. --- Doc/whatsnew/3.12.rst | 79 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 1a50bce0b65e0b..40d47c20e01abe 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -1386,15 +1386,78 @@ Removed ``imp.source_from_cache()`` :func:`importlib.util.source_from_cache` ================================= ======================================= - * Removed :mod:`!imp` functions and attributes with no replacements: - - * undocumented functions: + * 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 + + * Replace ``imp.load_compiled()`` with:: + + import importlib.util + import importlib.machinery + + def load_compiled(modname, filename): + loader = importlib.machinery.SourcelessFileLoader(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 + + * Replace ``imp.load_package()`` with:: + + import sys + import os.path + import importlib + + def load_package(name, path): + old_path = list(sys.path) + try: + sys.path.insert(0, os.path.dirname(path)) + return importlib.import_module(name) + finally: + sys.path.clear() + sys.path.extend(old_path) + + * Replace ``imp.load_dynamic()`` with:: + + import importlib.machinery + import importlib.util + + def load_dynamic(name, filename): + loader = importlib.machinery.ExtensionFileLoader(name, filename) + spec = importlib.util.spec_from_loader(name, 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 + + * Replace ``imp.init_builtin()`` with:: + + import importlib.machinery + import importlib.util + + def init_builtin(name): + spec = importlib.machinery.BuiltinImporter.find_spec(name) + if spec is None: + raise ImportError(f'no built-in module named: {name!r}') + return importlib.util.module_from_spec(spec) - * ``imp.init_builtin()`` - * ``imp.load_compiled()`` - * ``imp.load_dynamic()`` - * ``imp.load_package()`` - * ``imp.load_source()`` + * Removed :mod:`!imp` functions and attributes with no replacements: * ``imp.lock_held()``, ``imp.acquire_lock()``, ``imp.release_lock()``: the locking scheme has changed in Python 3.3 to per-module locks. From 66dba52b6ee148a6d87162f19fda687f883c5d13 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 21 Jun 2023 01:19:54 +0200 Subject: [PATCH 2/2] move load_dynamic before --- Doc/whatsnew/3.12.rst | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 40d47c20e01abe..d5ba755a19300a 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -1416,21 +1416,6 @@ Removed loader.exec_module(module) return module - * Replace ``imp.load_package()`` with:: - - import sys - import os.path - import importlib - - def load_package(name, path): - old_path = list(sys.path) - try: - sys.path.insert(0, os.path.dirname(path)) - return importlib.import_module(name) - finally: - sys.path.clear() - sys.path.extend(old_path) - * Replace ``imp.load_dynamic()`` with:: import importlib.machinery @@ -1446,6 +1431,21 @@ Removed loader.exec_module(module) return module + * Replace ``imp.load_package()`` with:: + + import sys + import os.path + import importlib + + def load_package(name, path): + old_path = list(sys.path) + try: + sys.path.insert(0, os.path.dirname(path)) + return importlib.import_module(name) + finally: + sys.path.clear() + sys.path.extend(old_path) + * Replace ``imp.init_builtin()`` with:: import importlib.machinery