[pypy-commit] pypy cffi_dlopen_unicode: Test on Posix; avoid using utf-8 and use the filesystem encoding instead

arigo pypy.commits at gmail.com
Sun Oct 14 02:32:16 EDT 2018


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi_dlopen_unicode
Changeset: r95212:8c1e8b2f18b2
Date: 2018-10-14 08:16 +0200
http://bitbucket.org/pypy/pypy/changeset/8c1e8b2f18b2/

Log:	Test on Posix; avoid using utf-8 and use the filesystem encoding
	instead

diff --git a/pypy/module/_cffi_backend/cdlopen.py b/pypy/module/_cffi_backend/cdlopen.py
--- a/pypy/module/_cffi_backend/cdlopen.py
+++ b/pypy/module/_cffi_backend/cdlopen.py
@@ -26,16 +26,16 @@
         if WIN32 and space.isinstance_w(w_filename, space.w_unicode):
             fname = space.unicode_w(w_filename)
             with rffi.scoped_unicode2wcharp(fname) as ll_libname:
-                fname = fname.encode('utf-8')
                 try:
                     handle = dlopenU(ll_libname, flags)
                 except DLOpenError as e:
-                    raise wrap_dlopenerror(space, e, fname)
+                    w_repr = space.repr(w_filename)
+                    raise wrap_dlopenerror(space, e, space.text_w(w_repr))
         else:
             if space.is_none(w_filename):
                 fname = None
             elif space.isinstance_w(w_filename, space.w_unicode):
-                fname = space.unicode_w(w_filename).encode('utf-8')
+                fname = space.fsencode_w(w_filename)
             else:
                 fname = space.text_w(w_filename)
             with rffi.scoped_str2charp(fname) as ll_libname:
diff --git a/pypy/module/_cffi_backend/libraryobj.py b/pypy/module/_cffi_backend/libraryobj.py
--- a/pypy/module/_cffi_backend/libraryobj.py
+++ b/pypy/module/_cffi_backend/libraryobj.py
@@ -28,16 +28,16 @@
         if WIN32 and space.isinstance_w(w_filename, space.w_unicode):
             fname = space.unicode_w(w_filename)
             with rffi.scoped_unicode2wcharp(fname) as ll_libname:
-                fname = fname.encode('utf-8')
                 try:
                     handle = dlopenU(ll_libname, flags)
                 except DLOpenError as e:
-                    raise wrap_dlopenerror(space, e, fname)
+                    w_repr = space.repr(w_filename)
+                    raise wrap_dlopenerror(space, e, space.text_w(w_repr))
         else:
             if space.is_none(w_filename):
                 fname = None
             elif space.isinstance_w(w_filename, space.w_unicode):
-                fname = space.unicode_w(w_filename).encode('utf-8')
+                fname = space.fsencode_w(w_filename)
             else:
                 fname = space.text_w(w_filename)
             with rffi.scoped_str2charp(fname) as ll_libname:
diff --git a/pypy/module/_cffi_backend/test/test_re_python.py b/pypy/module/_cffi_backend/test/test_re_python.py
--- a/pypy/module/_cffi_backend/test/test_re_python.py
+++ b/pypy/module/_cffi_backend/test/test_re_python.py
@@ -1,5 +1,5 @@
 import py
-import sys, shutil
+import sys, shutil, os
 from rpython.tool.udir import udir
 from pypy.interpreter.gateway import interp2app
 from pypy.module._cffi_backend.newtype import _clean_cache
@@ -46,8 +46,15 @@
         outputfilename = ffiplatform.compile(str(tmpdir), ext)
         cls.w_extmod = space.wrap(outputfilename)
         if WIN32:
-            # non-windows need utf8 locales to deal with unicode filenames
-            outputfileUname = unicode(udir.join(u'load\u03betest.dll'))
+            unicode_name = u'load\u03betest.dll'
+        else:
+            unicode_name = u'load_caf\xe9' + os.path.splitext(outputfilename)[1]
+            try:
+                unicode_name.encode(sys.getfilesystemencoding())
+            except UnicodeEncodeError:
+                unicode_name = None      # skip the test
+        if unicode_name is not None:
+            outputfileUname = os.path.join(unicode(udir), unicode_name)
             shutil.copyfile(outputfilename, outputfileUname)
             cls.w_extmodU = space.wrap(outputfileUname)
         #mod.tmpdir = tmpdir
@@ -119,12 +126,13 @@
         assert type(lib.add42) is _cffi_backend.FFI.CData
 
     def test_dlopen_unicode(self):
-        if getattr(self, 'extmodU', None):
-            import _cffi_backend
-            self.fix_path()
-            from re_python_pysrc import ffi
-            lib = ffi.dlopen(self.extmodU)
-            assert lib.add42(-10) == 32
+        if not getattr(self, 'extmodU', None):
+            skip("no unicode file name")
+        import _cffi_backend
+        self.fix_path()
+        from re_python_pysrc import ffi
+        lib = ffi.dlopen(self.extmodU)
+        assert lib.add42(-10) == 32
 
     def test_dlclose(self):
         import _cffi_backend


More information about the pypy-commit mailing list