[pypy-svn] r74944 - in pypy/trunk/pypy/rlib: . test

getxsick at codespeak.net getxsick at codespeak.net
Mon May 31 00:57:20 CEST 2010


Author: getxsick
Date: Mon May 31 00:57:19 2010
New Revision: 74944

Added:
   pypy/trunk/pypy/rlib/rdynload.py
   pypy/trunk/pypy/rlib/test/test_rdynload.py
Modified:
   pypy/trunk/pypy/rlib/libffi.py
   pypy/trunk/pypy/rlib/test/test_libffi.py
Log:
move dl* functions to pypy.rlib.rdynload


Modified: pypy/trunk/pypy/rlib/libffi.py
==============================================================================
--- pypy/trunk/pypy/rlib/libffi.py	(original)
+++ pypy/trunk/pypy/rlib/libffi.py	Mon May 31 00:57:19 2010
@@ -1,5 +1,4 @@
-
-""" Various rpython-level functions for dlopen and libffi wrapping
+""" Libffi wrapping
 """
 
 from pypy.rpython.tool import rffi_platform
@@ -7,15 +6,17 @@
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.rlib.rarithmetic import intmask, r_uint
 from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.rmmap import alloc
+from pypy.rlib.rdynload import dlopen, dlclose, dlsym, dlsym_byordinal
+from pypy.rlib.rdynload import DLOpenError
 from pypy.tool.autopath import pypydir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.rlib.rmmap import alloc
+from pypy.translator.platform import platform
 import py
 import os
 import sys
 import ctypes.util
 
-from pypy.translator.platform import platform
 
 # maaaybe isinstance here would be better. Think
 _MSVC = platform.name == "msvc"
@@ -55,18 +56,13 @@
     if _MINGW:
         includes = ['windows.h', 'ffi.h']
     else:
-        includes = ['dlfcn.h', 'ffi.h']
+        includes = ['ffi.h']
 
     if _MAC_OS:
         pre_include_bits = ['#define MACOSX']
     else: 
         pre_include_bits = []
 
-    if _FREEBSD_7 or _MINGW:
-        libraries = []
-    else:
-        libraries = ['dl']
-
     def find_libffi_a():
         dirlist = platform.library_dirs_for_libffi_a()
         for dir in dirlist:
@@ -77,10 +73,11 @@
 
     if hasattr(platform, 'library_dirs_for_libffi_a'):
         # platforms on which we want static linking
+        libraries = []
         link_files = [find_libffi_a()]
     else:
         # platforms on which we want dynamic linking
-        libraries = ['ffi'] + libraries
+        libraries = ['ffi']
         link_files = []
 
     eci = ExternalCompilationInfo(
@@ -115,10 +112,6 @@
 class CConfig:
     _compilation_info_ = eci
 
-    RTLD_LOCAL = rffi_platform.DefinedConstantInteger('RTLD_LOCAL')
-    RTLD_GLOBAL = rffi_platform.DefinedConstantInteger('RTLD_GLOBAL')
-    RTLD_NOW = rffi_platform.DefinedConstantInteger('RTLD_NOW')
-
     FFI_OK = rffi_platform.ConstantInteger('FFI_OK')
     FFI_BAD_TYPEDEF = rffi_platform.ConstantInteger('FFI_BAD_TYPEDEF')
     FFI_DEFAULT_ABI = rffi_platform.ConstantInteger('FFI_DEFAULT_ABI')
@@ -216,61 +209,8 @@
 def winexternal(name, args, result):
     return rffi.llexternal(name, args, result, compilation_info=eci, calling_conv='win')
 
-class DLOpenError(Exception):
-    def __init__(self, msg):
-        self.msg = msg
-    def __str__(self):
-        return repr(self.msg)
-
 
 if not _WIN32:
-    c_dlopen = external('dlopen', [rffi.CCHARP, rffi.INT], rffi.VOIDP)
-    c_dlclose = external('dlclose', [rffi.VOIDP], rffi.INT)
-    c_dlerror = external('dlerror', [], rffi.CCHARP)
-    c_dlsym = external('dlsym', [rffi.VOIDP, rffi.CCHARP], rffi.VOIDP)
-
-    RTLD_LOCAL = cConfig.RTLD_LOCAL
-    RTLD_GLOBAL = cConfig.RTLD_GLOBAL
-    RTLD_NOW = cConfig.RTLD_NOW
-
-    def dlerror():
-        # XXX this would never work on top of ll2ctypes, because
-        # ctypes are calling dlerror itself, unsure if I can do much in this
-        # area (nor I would like to)
-        res = c_dlerror()
-        if not res:
-            return ""
-        return rffi.charp2str(res)
-
-    def dlopen(name, mode=-1):
-        """ Wrapper around C-level dlopen
-        """
-        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:
-            err = dlerror()
-            raise DLOpenError(err)
-        return res
-
-    dlclose = c_dlclose
-
-    def dlsym(libhandle, name):
-        """ Wrapper around C-level dlsym
-        """
-        res = c_dlsym(libhandle, name)
-        if not res:
-            raise KeyError(name)
-        # XXX rffi.cast here...
-        return res
-
-    def dlsym_byordinal(handle, index):
-        # Never called
-        raise KeyError(index)
-
     def check_fficall_result(result, flags):
         pass # No check
     
@@ -281,36 +221,6 @@
         return libc_name
 
 if _WIN32:
-    def dlopen(name):
-        res = rwin32.LoadLibrary(name)
-        if not res:
-            err = rwin32.GetLastError()
-            raise DLOpenError(rwin32.FormatError(err))
-        return res
-
-    def dlclose(handle):
-        res = rwin32.FreeLibrary(handle)
-        if res:
-            return -1
-        else:
-            return 0
-
-    def dlsym(handle, name):
-        res = rwin32.GetProcAddress(handle, name)
-        if not res:
-            raise KeyError(name)
-        # XXX rffi.cast here...
-        return res
-
-    def dlsym_byordinal(handle, index):
-        # equivalent to MAKEINTRESOURCEA
-        intresource = rffi.cast(rffi.CCHARP, r_uint(index) & 0xFFFF)
-        res = rwin32.GetProcAddress(handle, intresource)
-        if not res:
-            raise KeyError(name)
-        # XXX rffi.cast here...
-        return res
-    
     def check_fficall_result(result, flags):
         if result == 0:
             return

Added: pypy/trunk/pypy/rlib/rdynload.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/rlib/rdynload.py	Mon May 31 00:57:19 2010
@@ -0,0 +1,150 @@
+""" Various rpython-level functions for dlopen
+"""
+
+from pypy.rpython.tool import rffi_platform
+from pypy.rpython.lltypesystem import rffi
+from pypy.rlib.rarithmetic import r_uint
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.translator.platform import platform
+
+import sys
+
+# maaaybe isinstance here would be better. Think
+_MSVC = platform.name == "msvc"
+_MINGW = platform.name == "mingw32"
+_WIN32 = _MSVC or _MINGW
+_MAC_OS = platform.name == "darwin"
+_FREEBSD_7 = platform.name == "freebsd7"
+
+if _WIN32:
+    from pypy.rlib import rwin32
+
+if _MINGW:
+    includes = ['windows.h']
+else:
+    includes = ['dlfcn.h']
+
+if _MAC_OS:
+    pre_include_bits = ['#define MACOSX']
+else: 
+    pre_include_bits = []
+
+if _FREEBSD_7 or _MINGW: # no need of library or not supported?
+    libraries = []
+elif _MSVC:
+    libraries = ['kernel32'] # not sure if needed
+else:
+    libraries = ['dl']
+
+eci = ExternalCompilationInfo(
+    pre_include_bits = pre_include_bits,
+    includes = includes,
+    libraries = libraries,
+)
+
+class CConfig:
+    _compilation_info_ = eci
+
+    RTLD_LOCAL = rffi_platform.DefinedConstantInteger('RTLD_LOCAL')
+    RTLD_GLOBAL = rffi_platform.DefinedConstantInteger('RTLD_GLOBAL')
+    RTLD_NOW = rffi_platform.DefinedConstantInteger('RTLD_NOW')
+
+class cConfig:
+    pass
+
+for k, v in rffi_platform.configure(CConfig).items():
+    setattr(cConfig, k, v)
+
+def external(name, args, result, **kwds):
+    return rffi.llexternal(name, args, result, compilation_info=eci, **kwds)
+
+def winexternal(name, args, result):
+    return rffi.llexternal(name, args, result, compilation_info=eci, calling_conv='win')
+
+class DLOpenError(Exception):
+    def __init__(self, msg):
+        self.msg = msg
+    def __str__(self):
+        return repr(self.msg)
+
+
+if not _WIN32:
+    c_dlopen = external('dlopen', [rffi.CCHARP, rffi.INT], rffi.VOIDP)
+    c_dlclose = external('dlclose', [rffi.VOIDP], rffi.INT)
+    c_dlerror = external('dlerror', [], rffi.CCHARP)
+    c_dlsym = external('dlsym', [rffi.VOIDP, rffi.CCHARP], rffi.VOIDP)
+
+    RTLD_LOCAL = cConfig.RTLD_LOCAL
+    RTLD_GLOBAL = cConfig.RTLD_GLOBAL
+    RTLD_NOW = cConfig.RTLD_NOW
+
+    def dlerror():
+        # XXX this would never work on top of ll2ctypes, because
+        # ctypes are calling dlerror itself, unsure if I can do much in this
+        # area (nor I would like to)
+        res = c_dlerror()
+        if not res:
+            return ""
+        return rffi.charp2str(res)
+
+    def dlopen(name, mode=-1):
+        """ Wrapper around C-level dlopen
+        """
+        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:
+            err = dlerror()
+            raise DLOpenError(err)
+        return res
+
+    dlclose = c_dlclose
+
+    def dlsym(libhandle, name):
+        """ Wrapper around C-level dlsym
+        """
+        res = c_dlsym(libhandle, name)
+        if not res:
+            raise KeyError(name)
+        # XXX rffi.cast here...
+        return res
+
+    def dlsym_byordinal(handle, index):
+        # Never called
+        raise KeyError(index)
+
+if _WIN32:
+    def dlopen(name):
+        res = rwin32.LoadLibrary(name)
+        if not res:
+            err = rwin32.GetLastError()
+            raise DLOpenError(rwin32.FormatError(err))
+        return res
+
+    def dlclose(handle):
+        res = rwin32.FreeLibrary(handle)
+        if res:
+            return -1
+        else:
+            return 0
+
+    def dlsym(handle, name):
+        res = rwin32.GetProcAddress(handle, name)
+        if not res:
+            raise KeyError(name)
+        # XXX rffi.cast here...
+        return res
+
+    def dlsym_byordinal(handle, index):
+        # equivalent to MAKEINTRESOURCEA
+        intresource = rffi.cast(rffi.CCHARP, r_uint(index) & 0xFFFF)
+        res = rwin32.GetProcAddress(handle, intresource)
+        if not res:
+            raise KeyError(name)
+        # XXX rffi.cast here...
+        return res
+    
+    LoadLibrary = rwin32.LoadLibrary

Modified: pypy/trunk/pypy/rlib/test/test_libffi.py
==============================================================================
--- pypy/trunk/pypy/rlib/test/test_libffi.py	(original)
+++ pypy/trunk/pypy/rlib/test/test_libffi.py	Mon May 31 00:57:19 2010
@@ -1,15 +1,14 @@
 
-""" Tests of libffi wrappers and dl* friends
+""" Tests of libffi wrapper
 """
 
-from pypy.rpython.test.test_llinterp import interpret
 from pypy.translator.c.test.test_genc import compile
 from pypy.rlib.libffi import *
 from pypy.rlib.objectmodel import keepalive_until_here
 from pypy.rpython.lltypesystem.ll2ctypes import ALLOCATED
 from pypy.rpython.lltypesystem import rffi, lltype
-import os, sys
 import py
+import sys
 import time
 
 def setup_module(mod):
@@ -19,14 +18,10 @@
         ffistruct = globals()[name]
         rffi.cast(rffi.VOIDP, ffistruct)
 
-class TestDLOperations:
+class TestLibffi:
     def setup_method(self, meth):
         ALLOCATED.clear()
 
-    def test_dlopen(self):
-        py.test.raises(DLOpenError, "dlopen(rffi.str2charp('xxxxxxxxxxxx'))")
-        assert dlopen(rffi.str2charp(get_libc_name()))
-        
     def get_libc(self):
         return CDLL(get_libc_name())
     

Added: pypy/trunk/pypy/rlib/test/test_rdynload.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/rlib/test/test_rdynload.py	Mon May 31 00:57:19 2010
@@ -0,0 +1,16 @@
+from pypy.rlib.rdynload import *
+from pypy.rlib.libffi import get_libc_name
+from pypy.rpython.lltypesystem import rffi, lltype
+import py
+
+class TestDLOperations:
+    def test_dlopen(self):
+        py.test.raises(DLOpenError, "dlopen(rffi.str2charp('xxxxxxxxxxxx'))")
+        assert dlopen(rffi.str2charp(get_libc_name()))
+
+    def test_dlsym(self):
+        lib = dlopen(rffi.str2charp(get_libc_name()))
+        handle = rffi.cast(lltype.Ptr(lltype.FuncType([lltype.Signed],
+                           lltype.Signed)), dlsym(lib, 'abs'))
+        assert 1 == handle(1)
+        assert 1 == handle(-1)



More information about the Pypy-commit mailing list