changeset 49729:7236f11db0c3

demandimport: ensure lazyloaderex sets loader attributes (issue6725) Adds test capturing missed expectation.
author Jason R. Coombs <jaraco@jaraco.com>
date Wed, 02 Nov 2022 09:34:03 -0400
parents 8a8005025d1c
children 266bb5c86f4b
files hgdemandimport/demandimportpy3.py tests/test-demandimport.py
diffstat 2 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgdemandimport/demandimportpy3.py	Tue Aug 30 09:59:53 2022 -0400
+++ b/hgdemandimport/demandimportpy3.py	Wed Nov 02 09:34:03 2022 -0400
@@ -41,6 +41,10 @@
         """Make the module load lazily."""
         with tracing.log('demandimport %s', module):
             if _deactivated or module.__name__ in ignores:
+                # Reset the loader on the module as super() does (issue6725)
+                module.__spec__.loader = self.loader
+                module.__loader__ = self.loader
+
                 self.loader.exec_module(module)
             else:
                 super().exec_module(module)
--- a/tests/test-demandimport.py	Tue Aug 30 09:59:53 2022 -0400
+++ b/tests/test-demandimport.py	Wed Nov 02 09:34:03 2022 -0400
@@ -234,3 +234,11 @@
 zipfileimp = __import__('ftplib', globals(), locals(), ['unknownattr'])
 assert f(zipfileimp) == "<module 'ftplib' from '?'>", f(zipfileimp)
 assert not util.safehasattr(zipfileimp, 'unknownattr')
+
+
+# test deactivation for issue6725
+del sys.modules['telnetlib']
+with demandimport.deactivated():
+    import telnetlib
+assert telnetlib.__loader__ == telnetlib.__spec__.loader
+assert telnetlib.__loader__.get_resource_reader