[pypy-commit] pypy default: Issue #870: ctypes.byref(.., offset)

arigo pypy.commits at gmail.com
Fri Jul 8 13:22:16 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r85612:36aba877dfa1
Date: 2016-07-08 14:35 +0200
http://bitbucket.org/pypy/pypy/changeset/36aba877dfa1/

Log:	Issue #870: ctypes.byref(.., offset)

diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -199,10 +199,13 @@
     return tp._alignmentofinstances()
 
 @builtinify
-def byref(cdata):
+def byref(cdata, offset=0):
     # "pointer" is imported at the end of this module to avoid circular
     # imports
-    return pointer(cdata)
+    ptr = pointer(cdata)
+    if offset != 0:
+        ptr._buffer[0] += offset
+    return ptr
 
 def cdata_from_address(self, address):
     # fix the address: turn it into as unsigned, in case it's a negative number
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
@@ -265,3 +265,10 @@
         class A(object):
             _byref = byref
         A._byref(c_int(5))
+
+    def test_byref_with_offset(self):
+        c = c_int()
+        d = byref(c)
+        base = cast(d, c_void_p).value
+        for i in [0, 1, 4, 1444, -10293]:
+            assert cast(byref(c, i), c_void_p).value == base + i


More information about the pypy-commit mailing list