[pypy-commit] pypy cffi_dlopen_unicode: copy unicode handling to _cffi_backend.libraryobj.W_Library

mattip pypy.commits at gmail.com
Mon Oct 8 02:41:43 EDT 2018


Author: Matti Picus <matti.picus at gmail.com>
Branch: cffi_dlopen_unicode
Changeset: r95187:295154c77400
Date: 2018-10-08 09:36 +0300
http://bitbucket.org/pypy/pypy/changeset/295154c77400/

Log:	copy unicode handling to _cffi_backend.libraryobj.W_Library

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
@@ -1,4 +1,5 @@
 from __future__ import with_statement
+import sys
 
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import oefmt
@@ -12,20 +13,42 @@
 from pypy.module._cffi_backend.cdataobj import W_CData
 from pypy.module._cffi_backend.ctypeobj import W_CType
 
+if sys.platform == 'win32':
+    from rpython.rlib.rdynload import dlopenU
+    WIN32 = True
+else:
+    WIN32 = False
+
 
 class W_Library(W_Root):
     _immutable_ = True
 
-    def __init__(self, space, filename, flags):
+    def __init__(self, space, w_filename, flags):
         self.space = space
-        with rffi.scoped_str2charp(filename) as ll_libname:
-            if filename is None:
-                filename = "<None>"
-            try:
-                self.handle = dlopen(ll_libname, flags)
-            except DLOpenError as e:
-                raise wrap_dlopenerror(space, e, filename)
-        self.name = filename
+        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)
+        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')
+            else:
+                fname = space.text_w(w_filename)
+            with rffi.scoped_str2charp(fname) as ll_libname:
+                if fname is None:
+                    fname = "<None>"
+                try:
+                    handle = dlopen(ll_libname, flags)
+                except DLOpenError as e:
+                    raise wrap_dlopenerror(space, e, fname)
+        self.handle = handle
+        self.name = fname
         self.register_finalizer(space)
 
     def _finalize_(self):
@@ -104,7 +127,7 @@
 W_Library.typedef.acceptable_as_base_class = False
 
 
- at unwrap_spec(filename="fsencode_or_none", flags=int)
-def load_library(space, filename, flags=0):
-    lib = W_Library(space, filename, flags)
+ at unwrap_spec(flags=int)
+def load_library(space, w_filename, flags=0):
+    lib = W_Library(space, w_filename, flags)
     return lib


More information about the pypy-commit mailing list