[pypy-svn] r54797 - in pypy/dist/pypy: lib/_ctypes lib/ctypes module/_rawffi rpython/lltypesystem

afa at codespeak.net afa at codespeak.net
Fri May 16 19:50:47 CEST 2008


Author: afa
Date: Fri May 16 19:50:43 2008
New Revision: 54797

Modified:
   pypy/dist/pypy/lib/_ctypes/__init__.py
   pypy/dist/pypy/lib/_ctypes/builtin.py
   pypy/dist/pypy/lib/ctypes/__init__.py
   pypy/dist/pypy/module/_rawffi/__init__.py
   pypy/dist/pypy/module/_rawffi/interp_rawffi.py
   pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
Log:
On win32, ctypes now works on top of py.py!

    bin\py.py --withmod-_rawffi

can import ctypes, and basic operations seem to work.
I Hope I did not break other platforms...

Todo: write a version of ll2ctypes.get_libc_name that does not
depend on CPython ctypes.


Modified: pypy/dist/pypy/lib/_ctypes/__init__.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/__init__.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/__init__.py	Fri May 16 19:50:43 2008
@@ -11,9 +11,15 @@
      set_conversion_mode, _wstring_at_addr
 from _ctypes.union import Union
 
+import os as _os
+
+if _os.name in ("nt", "ce"):
+    from _rawffi import FormatError
+    from _rawffi import check_HRESULT as _check_HRESULT
+
+from _rawffi import FUNCFLAG_STDCALL, FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI
+
 __version__ = '1.0.2'
 #XXX platform dependant?
 RTLD_LOCAL = 0
 RTLD_GLOBAL = 256
-FUNCFLAG_CDECL = 1
-FUNCFLAG_PYTHONAPI = 4

Modified: pypy/dist/pypy/lib/_ctypes/builtin.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/builtin.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/builtin.py	Fri May 16 19:50:43 2008
@@ -5,8 +5,8 @@
     encoding = 'ascii'
     errors = 'strict'
 
-_memmove_addr = ('memmove', 'libc.so.6')
-_memset_addr = ('memset', 'libc.so.6')
+_memmove_addr = ('memmove', _rawffi.libc_name)
+_memset_addr = ('memset', _rawffi.libc_name)
 
 def _string_at_addr(addr, lgt):
     # address here can be almost anything

Modified: pypy/dist/pypy/lib/ctypes/__init__.py
==============================================================================
--- pypy/dist/pypy/lib/ctypes/__init__.py	(original)
+++ pypy/dist/pypy/lib/ctypes/__init__.py	Fri May 16 19:50:43 2008
@@ -106,7 +106,6 @@
         return CFunctionType
 
 if _os.name in ("nt", "ce"):
-    from _ctypes import LoadLibrary as _dlopen
     from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
     if _os.name == "ce":
         # 'ce' doesn't have the stdcall calling convention
@@ -127,9 +126,7 @@
     if WINFUNCTYPE.__doc__:
         WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
 
-elif _os.name == "posix":
-    from _ctypes import dlopen as _dlopen
-
+from _ctypes import dlopen as _dlopen
 from _ctypes import sizeof, byref, addressof, alignment, resize
 from _ctypes import _SimpleCData
 

Modified: pypy/dist/pypy/module/_rawffi/__init__.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/__init__.py	(original)
+++ pypy/dist/pypy/module/_rawffi/__init__.py	Fri May 16 19:50:43 2008
@@ -30,3 +30,24 @@
     appleveldefs = {
         'SegfaultException'  : 'error.SegfaultException',
     }
+
+    def buildloaders(cls):
+        from pypy.module._rawffi import interp_rawffi
+
+        if hasattr(interp_rawffi, 'FormatError'):
+            Module.interpleveldefs['FormatError'] = 'interp_rawffi.FormatError'
+        if hasattr(interp_rawffi, 'check_HRESULT'):
+            Module.interpleveldefs['check_HRESULT'] = 'interp_rawffi.check_HRESULT'
+
+        from pypy.rlib import libffi
+        for name in ['FUNCFLAG_STDCALL', 'FUNCFLAG_CDECL', 'FUNCFLAG_PYTHONAPI',
+                     ]:
+            if hasattr(libffi, name):
+                Module.interpleveldefs[name] = "space.wrap(%r)" % getattr(libffi, name)
+
+        # Name of C runtime library
+        from pypy.rpython.lltypesystem.ll2ctypes import get_libc_name
+        Module.interpleveldefs['libc_name'] = "space.wrap(%r)" % get_libc_name()
+                
+        super(Module, cls).buildloaders()
+    buildloaders = classmethod(buildloaders)

Modified: pypy/dist/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/dist/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/dist/pypy/module/_rawffi/interp_rawffi.py	Fri May 16 19:50:43 2008
@@ -2,13 +2,18 @@
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable, \
      Arguments
 from pypy.interpreter.error import OperationError, wrap_oserror
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, NoneNotWrapped
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 
 from pypy.rlib.libffi import *
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rlib.unroll import unrolling_iterable
 
+try:
+    from pypy.rlib import rwin32
+except:
+    rwin32 = None
+
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.rarithmetic import intmask, r_uint, r_singlefloat
 from pypy.module._rawffi.tracker import tracker
@@ -452,3 +457,13 @@
     s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, address), maxlength)
     return space.wrap(s)
 charp2rawstring.unwrap_spec = [ObjSpace, r_uint, int]
+
+def FormatError(space, code):
+    return space.wrap(rwin32.FormatError(code))
+FormatError.unwrap_spec = [ObjSpace, int]
+
+def check_HRESULT(space, hresult):
+    if rwin32.FAILED(hresult):
+        raise OperationError(space.w_WindowsError, space.wrap(hresult))
+    return space.wrap(hresult)
+check_HRESULT.unwrap_spec = [ObjSpace, int]

Modified: pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/ll2ctypes.py	Fri May 16 19:50:43 2008
@@ -518,30 +518,33 @@
 # __________ the standard C library __________
 
 if ctypes:
-    if sys.platform == 'win32':
-        # Parses sys.version and deduces the version of the compiler
-        import distutils.msvccompiler
-        version = distutils.msvccompiler.get_build_version()
-        if version is None:
-            # This logic works with official builds of Python.
-            if sys.version_info < (2, 4):
-                clibname = 'msvcrt'
-            else:
-                clibname = 'msvcr71'
-        else:
-            if version <= 6:
-                clibname = 'msvcrt'
+    def get_libc_name():
+        if sys.platform == 'win32':
+            # Parses sys.version and deduces the version of the compiler
+            import distutils.msvccompiler
+            version = distutils.msvccompiler.get_build_version()
+            if version is None:
+                # This logic works with official builds of Python.
+                if sys.version_info < (2, 4):
+                    clibname = 'msvcrt'
+                else:
+                    clibname = 'msvcr71'
             else:
-                clibname = 'msvcr%d' % (version * 10)
+                if version <= 6:
+                    clibname = 'msvcrt'
+                else:
+                    clibname = 'msvcr%d' % (version * 10)
+
+            # If python was built with in debug mode
+            import imp
+            if imp.get_suffixes()[0][0] == '_d.pyd':
+                clibname += 'd'
 
-        # If python was built with in debug mode
-        import imp
-        if imp.get_suffixes()[0][0] == '_d.pyd':
-            clibname += 'd'
-
-        standard_c_lib = ctypes.cdll.LoadLibrary(clibname+'.dll')
-    else:
-        standard_c_lib = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
+            return clibname+'.dll'
+        else:
+            return ctypes.util.find_library('c')
+        
+    standard_c_lib = ctypes.cdll.LoadLibrary(get_libc_name())
 
 # ____________________________________________
 



More information about the Pypy-commit mailing list