From 33c9d86af4dc1df04cf1b38a0102fe7e121173ec Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 10 Aug 2020 21:10:46 +1000 Subject: [PATCH 1/2] Change load_module to exec_module --- setuptools/command/bdist_egg.py | 7 ++++--- setuptools/command/build_ext.py | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 7af3165cd5..4be1545789 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -55,11 +55,12 @@ def write_stub(resource, pyfile): _stub_template = textwrap.dedent(""" def __bootstrap__(): global __bootstrap__, __loader__, __file__ - import sys, pkg_resources - from importlib.machinery import ExtensionFileLoader + import sys, pkg_resources, importlib.util __file__ = pkg_resources.resource_filename(__name__, %r) __loader__ = None; del __bootstrap__, __loader__ - ExtensionFileLoader(__name__,__file__).load_module() + spec = importlib.util.spec_from_file_location(__name__,__file__) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) __bootstrap__() """).lstrip() with open(pyfile, 'w') as f: diff --git a/setuptools/command/build_ext.py b/setuptools/command/build_ext.py index 0eb29adc8b..89a0e328f9 100644 --- a/setuptools/command/build_ext.py +++ b/setuptools/command/build_ext.py @@ -254,8 +254,8 @@ def write_stub(self, output_dir, ext, compile=False): '\n'.join([ "def __bootstrap__():", " global __bootstrap__, __file__, __loader__", - " import sys, os, pkg_resources" + if_dl(", dl"), - " from importlib.machinery import ExtensionFileLoader", + " import sys, os, pkg_resources, importlib.util" + + if_dl(", dl"), " __file__ = pkg_resources.resource_filename" "(__name__,%r)" % os.path.basename(ext._file_name), @@ -267,8 +267,10 @@ def write_stub(self, output_dir, ext, compile=False): " try:", " os.chdir(os.path.dirname(__file__))", if_dl(" sys.setdlopenflags(dl.RTLD_NOW)"), - " ExtensionFileLoader(__name__,", - " __file__).load_module()", + " spec = importlib.util.spec_from_file_location(", + " __name__, __file__)", + " mod = importlib.util.module_from_spec(spec)", + " spec.loader.exec_module(mod)", " finally:", if_dl(" sys.setdlopenflags(old_flags)"), " os.chdir(old_dir)", From 3af54b4b0e31c3f56bbb229cdba58ce8a70fa38b Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 12 Aug 2020 16:22:34 -0400 Subject: [PATCH 2/2] Update changelog. --- changelog.d/2297.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/2297.misc.rst diff --git a/changelog.d/2297.misc.rst b/changelog.d/2297.misc.rst new file mode 100644 index 0000000000..40340324fb --- /dev/null +++ b/changelog.d/2297.misc.rst @@ -0,0 +1 @@ +Once again, in stubs prefer exec_module to the deprecated load_module.