[pypy-svn] r56176 - pypy/dist/pypy/rlib

xoraxax at codespeak.net xoraxax at codespeak.net
Sun Jun 29 23:53:49 CEST 2008


Author: xoraxax
Date: Sun Jun 29 23:53:47 2008
New Revision: 56176

Modified:
   pypy/dist/pypy/rlib/libffi.py
Log:
A few small changes in libffi.py:
  * Introduce a DEBUG flag to write dlerror() msgs to stderr.
  * Add a mode parameter to dlopen().
  * Remove CDLL.ll_libname.
  * Add a new parameter to CDLL.__init__ to disable the dlclose() call
    in __del__.



Modified: pypy/dist/pypy/rlib/libffi.py
==============================================================================
--- pypy/dist/pypy/rlib/libffi.py	(original)
+++ pypy/dist/pypy/rlib/libffi.py	Sun Jun 29 23:53:47 2008
@@ -12,6 +12,9 @@
 import py
 import os
 
+
+DEBUG = False # writes dlerror() messages to stderr
+
 _MS_WINDOWS = os.name == "nt"
 
 if _MS_WINDOWS:
@@ -186,16 +189,23 @@
             return ""
         return rffi.charp2str(res)
 
-    def dlopen(name):
+    def dlopen(name, mode=-1):
         """ Wrapper around C-level dlopen
         """
-        if RTLD_LOCAL is not None:
-            mode = RTLD_LOCAL | RTLD_NOW
-        else:
-            mode = RTLD_NOW
+        if mode == -1:
+            if RTLD_LOCAL is not None:
+                mode = RTLD_LOCAL | RTLD_NOW
+            else:
+                mode = RTLD_NOW
         res = c_dlopen(name, rffi.cast(rffi.INT, mode))
         if not res:
-            raise OSError(-1, dlerror())
+            err = dlerror()
+            # because the message would be lost in a translated program (OSError only has an errno),
+            # we offer a way to write it to stderr
+            if DEBUG:
+                import os
+                os.write(2, err)
+            raise OSError(-1, err)
         return res
 
     dlclose = c_dlclose
@@ -514,20 +524,18 @@
 
 class CDLL:
     flags = FUNCFLAG_CDECL
-    
-    def __init__(self, libname):
-        self.ll_libname = lltype.nullptr(rffi.CCHARP.TO)
+
+    def __init__(self, libname, unload_on_finalization=True):
+        self.unload_on_finalization = unload_on_finalization
         self.lib = lltype.nullptr(rffi.CCHARP.TO)
-        self.ll_libname = rffi.str2charp(libname)
-        self.lib = dlopen(self.ll_libname)
+        ll_libname = rffi.str2charp(libname)
+        self.lib = dlopen(ll_libname)
+        lltype.free(ll_libname, flavor='raw')
 
     def __del__(self):
-        if self.lib:
+        if self.lib and self.unload_on_finalization:
             dlclose(self.lib)
             self.lib = lltype.nullptr(rffi.CCHARP.TO)
-        if self.ll_libname:
-            lltype.free(self.ll_libname, flavor='raw')
-            self.ll_libname = lltype.nullptr(rffi.CCHARP.TO)
 
     def getpointer(self, name, argtypes, restype):
         # these arguments are already casted to proper ffi



More information about the Pypy-commit mailing list