[pypy-commit] pypy freelibrary: export FreeLibrary, non-closing LoadLibrary from rwin32

mattip pypy.commits at gmail.com
Wed Jun 19 18:46:40 EDT 2019


Author: Matti Picus <matti.picus at gmail.com>
Branch: freelibrary
Changeset: r96830:ec338ff9ef03
Date: 2019-06-20 01:44 +0300
http://bitbucket.org/pypy/pypy/changeset/ec338ff9ef03/

Log:	export FreeLibrary, non-closing LoadLibrary from rwin32

diff --git a/lib_pypy/_ctypes/__init__.py b/lib_pypy/_ctypes/__init__.py
--- a/lib_pypy/_ctypes/__init__.py
+++ b/lib_pypy/_ctypes/__init__.py
@@ -16,7 +16,7 @@
 import os as _os
 
 if _os.name in ("nt", "ce"):
-    from _rawffi import FormatError
+    from _rawffi import FormatError, FreeLibrary, LoadLibrary
     from _rawffi import check_HRESULT as _check_HRESULT
 
     try: from __pypy__ import builtinify
@@ -34,7 +34,6 @@
 
     del builtinify
 
-    LoadLibrary = dlopen
 
 from _rawffi import FUNCFLAG_STDCALL, FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI
 from _rawffi import FUNCFLAG_USE_ERRNO, FUNCFLAG_USE_LASTERROR
diff --git a/pypy/module/_rawffi/__init__.py b/pypy/module/_rawffi/__init__.py
--- a/pypy/module/_rawffi/__init__.py
+++ b/pypy/module/_rawffi/__init__.py
@@ -31,6 +31,9 @@
         'SegfaultException'  : 'space.new_exception_class("_rawffi.SegfaultException")',
         'exit'               : 'interp_exit.exit',
     }
+    if sys.platform == 'win32':
+        interpleveldefs['LoadLibrary'] = 'interp_rawffi.LoadLibrary'
+        interpleveldefs['FreeLibrary'] = 'interp_rawffi.FreeLibrary'
 
     appleveldefs = {
     }
diff --git a/pypy/module/_rawffi/interp_rawffi.py b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -658,11 +658,26 @@
 if sys.platform == 'win32':
     # see also
     # https://bitbucket.org/pypy/pypy/issue/1944/ctypes-on-windows-getlasterror
+
     def get_last_error(space):
         return space.newint(rwin32.GetLastError_alt_saved())
     @unwrap_spec(error=int)
     def set_last_error(space, error):
         rwin32.SetLastError_alt_saved(error)
+
+    @unwrap_spec(name='fsencode_or_none')
+    def LoadLibrary(space, name):
+        with rffi.scoped_str2charp(libname) as ll_libname:
+            try:
+                return dlopen(ll_libname)
+            except DLOpenError as e:
+                raise wrap_dlopenerror(space, e, name or "<None>")
+            except OSError as e:
+                raise wrap_oserror(space, e)
+
+    def FreeLibrary(space, w_handle):
+        return rwin32.FreeLibrary(space.int_w(w_handle))
+        
 else:
     # always have at least a dummy version of these functions
     # (https://bugs.pypy.org/issue1242)
diff --git a/pypy/module/_rawffi/test/test__rawffi.py b/pypy/module/_rawffi/test/test__rawffi.py
--- a/pypy/module/_rawffi/test/test__rawffi.py
+++ b/pypy/module/_rawffi/test/test__rawffi.py
@@ -288,11 +288,15 @@
         if not self.iswin32:
             skip("win32 specific")
         import _rawffi
-        lib = _rawffi.CDLL(self.lib_name)
-        # This will call the ordinal function numbered 1
-        # my compiler seems to order them alphabetically:
-        # AAA_first_ordinal_function
-        assert lib.ptr(1, [], 'i')()[0] == 42
+        lib = _rawffi.LoadLibrary(self.lib_name)
+        try:
+            # This will call the ordinal function numbered 1
+            # my compiler seems to order them alphabetically:
+            # AAA_first_ordinal_function
+            assert lib.ptr(1, [], 'i')()[0] == 42
+        finally:
+            assert _rawffi.FreeLibrary(lib) == 0
+        
 
     def test_getchar(self):
         import _rawffi


More information about the pypy-commit mailing list