Author: Antonio Cuni <[email protected]>
Branch: dummy-importlib2
Changeset: r98341:37239125314a
Date: 2019-12-20 01:19 +0100
http://bitbucket.org/pypy/pypy/changeset/37239125314a/

Log:    tweak the code until it works: remove the references to lonepycfiles
        and make MODULES_THAT_ALWAYS_SHADOW a global var since it's no
        longer on the space

diff --git a/pypy/module/_dummy_importlib/interp_import.py 
b/pypy/module/_dummy_importlib/interp_import.py
--- a/pypy/module/_dummy_importlib/interp_import.py
+++ b/pypy/module/_dummy_importlib/interp_import.py
@@ -1,5 +1,10 @@
 """
-Implementation of the interpreter-level default import logic.
+This is mostly a copy&paste from pypy/module/imp/importing.py in the
+default branch, adapted to work on pypy3.  This module is NOT meant to be
+translated and probably the logic is slightly different than the real logic
+needed for Python3.  However, since it is written at interp-level, it is much
+faster than _frozen_importlib which is written at applevel, which makes
+running tests much faster.
 """
 
 import sys, os, stat
@@ -37,6 +42,15 @@
 # split the two usages again.
 DEFAULT_SOABI = 'pypy-%d%d' % PYPY_VERSION[:2]
 
+# on default this is an attribute of space defined in baseobjspace.py: here
+# it's no longer on the space, we we copied&pasted this from baseobjspace
+MODULES_THAT_ALWAYS_SHADOW = dict.fromkeys([
+    '__builtin__', '__pypy__', '_ast', '_codecs', '_sre', '_warnings',
+    '_weakref', 'errno', 'exceptions', 'gc', 'imp', 'marshal',
+    'posix', 'nt', 'pwd', 'signal', 'sys', 'thread', 'zipimport',
+], None)
+
+
 @specialize.memo()
 def get_so_extension(space):
     if space.config.objspace.soabi is not None:
@@ -77,8 +91,6 @@
     init = os.path.join(filepart, "__init__")
     if path_exists(init + ".py"):
         return True
-    if space.config.objspace.lonepycfiles and path_exists(init + ".pyc"):
-        return True
     return False
 
 def find_modtype(space, filepart):
@@ -97,18 +109,6 @@
         if file_exists(pyfile):
             return PY_SOURCE, ".pyw", "U"
 
-    # The .py file does not exist.  By default on PyPy, lonepycfiles
-    # is False: if a .py file does not exist, we don't even try to
-    # look for a lone .pyc file.
-    # The "imp" module does not respect this, and is allowed to find
-    # lone .pyc files.
-    # check the .pyc file
-    if space.config.objspace.lonepycfiles:
-        pycfile = filepart + ".pyc"
-        if file_exists(pycfile):
-            # existing .pyc file
-            return PY_COMPILED, ".pyc", "rb"
-
     if has_so_extension(space):
         so_extension = get_so_extension(space)
         pydfile = filepart + so_extension
@@ -520,6 +520,7 @@
     find_module=interp2app(W_NullImporter.find_module_w),
     )
 
+
 class FindInfo:
     def __init__(self, modtype, filename, stream,
                  suffix="", filemode="", w_loader=None):
@@ -556,7 +557,7 @@
             # could possibly be; a "pseudo-extension module" does not, and
             # is only loaded at the point in sys.path where we find
             # '.../lib_pypy/__extensions__'.
-            if modulename in space.MODULES_THAT_ALWAYS_SHADOW:
+            if modulename in MODULES_THAT_ALWAYS_SHADOW:
                 return delayed_builtin
             w_lib_extensions = space.sys.get_state(space).w_lib_extensions
         w_path = space.sys.get('path')
diff --git a/pypy/module/_dummy_importlib/moduledef.py 
b/pypy/module/_dummy_importlib/moduledef.py
--- a/pypy/module/_dummy_importlib/moduledef.py
+++ b/pypy/module/_dummy_importlib/moduledef.py
@@ -1,4 +1,5 @@
 from pypy.interpreter.mixedmodule import MixedModule
+from pypy.interpreter.gateway import interp2app
 from pypy.module._dummy_importlib import interp_import
 
 class Module(MixedModule):
@@ -11,7 +12,8 @@
     def install(self):
         """NOT_RPYTHON"""
         super(Module, self).install()
-        self.w_import = self.space.wrap(interp_import.importhook)
+        self.w_import = self.space.wrap(
+            interp2app(interp_import.importhook, app_name='__dummy_import__'))
 
     def startup(self, space):
         """Copy our __import__ to builtins."""
diff --git a/pypy/module/_dummy_importlib/test/test__dummy_importlib.py 
b/pypy/module/_dummy_importlib/test/test__dummy_importlib.py
--- a/pypy/module/_dummy_importlib/test/test__dummy_importlib.py
+++ b/pypy/module/_dummy_importlib/test/test__dummy_importlib.py
@@ -8,3 +8,16 @@
     space = gettestobjspace(usemodules=['_frozen_importlib'])
     assert not space.config.objspace.usemodules._dummy_importlib
     assert space.config.objspace.usemodules._frozen_importlib
+
+
+class AppTestDummyImportlib:
+
+    def test_import_builtin(self):
+        import sys
+        import operator
+        assert sys.modules['operator'] is operator
+        assert operator.add(1, 2) == 3
+
+    def test_import_from_sys_path(self):
+        import keyword # this is a module from lib-python
+        assert keyword.iskeyword('def')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to