Author: Matti Picus <matti.pi...@gmail.com>
Branch: 
Changeset: r98203:4454c23fcc36
Date: 2019-12-01 18:41 +0200
http://bitbucket.org/pypy/pypy/changeset/4454c23fcc36/

Log:    test, fix for importing with unicode in sys.path (issue 3112)

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -448,14 +448,19 @@
             return w_loader
 
 def _getimporter(space, w_pathitem):
-    # the function 'imp._getimporter' is a pypy-only extension
+    # 'imp._getimporter' is somewhat like CPython's get_path_importer
     w_path_importer_cache = space.sys.get("path_importer_cache")
     w_importer = space.finditem(w_path_importer_cache, w_pathitem)
     if w_importer is None:
         space.setitem(w_path_importer_cache, w_pathitem, space.w_None)
         for w_hook in space.unpackiterable(space.sys.get("path_hooks")):
+            w_pathbytes = w_pathitem
+            if space.isinstance_w(w_pathitem, space.w_unicode):
+                from pypy.module.sys.interp_encoding import 
getfilesystemencoding
+                w_pathbytes = space.call_method(space.w_unicode, 'encode',
+                                     w_pathitem, getfilesystemencoding(space))
             try:
-                w_importer = space.call_function(w_hook, w_pathitem)
+                w_importer = space.call_function(w_hook, w_pathbytes)
             except OperationError as e:
                 if not e.match(space, space.w_ImportError):
                     raise
diff --git a/pypy/module/imp/test/test_app.py b/pypy/module/imp/test/test_app.py
--- a/pypy/module/imp/test/test_app.py
+++ b/pypy/module/imp/test/test_app.py
@@ -4,7 +4,8 @@
 
 class AppTestImpModule:
     spaceconfig = {
-        'usemodules': ['binascii', 'imp', 'itertools', 'time', 'struct'],
+        'usemodules': ['binascii', 'imp', 'itertools', 'time', 'struct',
+                       'zipimport'],
     }
 
     def setup_class(cls):
@@ -246,3 +247,14 @@
         assert marshal.loads == 42
 
         marshal.loads = old
+
+    def test_unicode_in_sys_path(self):
+        # issue 3112: when _getimporter calls
+        # for x in sys.path: for h in sys.path_hooks: h(x)
+        # make sure x is properly encoded
+        import sys
+        import zipimport #  installs a sys.path_hook
+        if sys.getfilesystemencoding().lower() == 'utf-8':
+            sys.path.insert(0, u'\xef')
+        with raises(ImportError):
+            import impossible_module
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to