[pypy-svn] r77246 - pypy/branch/fast-forward/lib_pypy/_ctypes
afa at codespeak.net
afa at codespeak.net
Tue Sep 21 19:07:54 CEST 2010
Author: afa
Date: Tue Sep 21 19:07:52 2010
New Revision: 77246
Modified:
pypy/branch/fast-forward/lib_pypy/_ctypes/__init__.py
pypy/branch/fast-forward/lib_pypy/_ctypes/pointer.py
Log:
ctypes.POINTER was rewritten in C "for performance".
For pypy, we simply copy the old python code...
Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/__init__.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/__init__.py (original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/__init__.py Tue Sep 21 19:07:52 2010
@@ -3,6 +3,7 @@
ArgumentError, COMError
from _ctypes.primitive import _SimpleCData
from _ctypes.pointer import _Pointer, _cast_addr
+from _ctypes.pointer import POINTER, pointer, _pointer_type_cache
from _ctypes.function import CFuncPtr
from _ctypes.dll import dlopen as LoadLibrary
from _ctypes.structure import Structure
Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/pointer.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/pointer.py (original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/pointer.py Tue Sep 21 19:07:52 2010
@@ -5,6 +5,9 @@
from _ctypes.array import Array, array_get_slice_params, array_slice_getitem,\
array_slice_setitem
+# This cache maps types to pointers to them.
+_pointer_type_cache = {}
+
DEFAULT_VALUE = object()
class PointerType(_CDataMeta):
@@ -136,3 +139,26 @@
result._buffer[0] = obj._buffer[0]
return result
+
+def POINTER(cls):
+ try:
+ return _pointer_type_cache[cls]
+ except KeyError:
+ pass
+ if type(cls) is str:
+ klass = type(_Pointer)("LP_%s" % cls,
+ (_Pointer,),
+ {})
+ _pointer_type_cache[id(klass)] = klass
+ return klass
+ else:
+ name = "LP_%s" % cls.__name__
+ klass = type(_Pointer)(name,
+ (_Pointer,),
+ {'_type_': cls})
+ _pointer_type_cache[cls] = klass
+ return klass
+
+def pointer(inst):
+ return POINTER(type(inst))(inst)
+
More information about the Pypy-commit
mailing list