[pypy-svn] r22521 - in pypy/dist/pypy/rpython/memory: . test
rxe at codespeak.net
rxe at codespeak.net
Mon Jan 23 14:53:00 CET 2006
Author: rxe
Date: Mon Jan 23 14:52:58 2006
New Revision: 22521
Modified:
pypy/dist/pypy/rpython/memory/lladdress.py
pypy/dist/pypy/rpython/memory/lltypelayout.py
pypy/dist/pypy/rpython/memory/test/test_address.py
Log:
(cfbolz, rxe)
Added convert_offset_to_int() and fixed up address.__add__ to deal with OffsetsOf.
Modified: pypy/dist/pypy/rpython/memory/lladdress.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lladdress.py (original)
+++ pypy/dist/pypy/rpython/memory/lladdress.py Mon Jan 23 14:52:58 2006
@@ -5,6 +5,8 @@
from pypy.rpython.lltypesystem import lltype
class address(object):
+ #XXX should be _address!
+
def __new__(cls, intaddress=0):
if intaddress == 0:
null = cls.__dict__.get("NULL")
@@ -22,13 +24,18 @@
return self.intaddress
def __add__(self, offset):
- assert isinstance(offset, int)
- return address(self.intaddress + offset)
+ from pypy.rpython.memory.lltypelayout import convert_offset_to_int
+ if isinstance(offset, int):
+ return address(self.intaddress + offset)
+ else:
+ assert isinstance(offset, llmemory.OffsetOf)
+ return address(self.intaddress + convert_offset_to_int(offset))
def __sub__(self, other):
if isinstance(other, int):
return address(self.intaddress - other)
else:
+ assert isinstance(other, address)
return self.intaddress - other.intaddress
def __cmp__(self, other):
Modified: pypy/dist/pypy/rpython/memory/lltypelayout.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/lltypelayout.py (original)
+++ pypy/dist/pypy/rpython/memory/lltypelayout.py Mon Jan 23 14:52:58 2006
@@ -87,7 +87,14 @@
else:
return fixedsize + i * varsize
-
+def convert_offset_to_int(offset):
+ TYPE = offset.TYPE
+ res = 0
+ for fld in offset.fldnames:
+ layout = get_layout(TYPE)
+ res += layout[fld]
+ TYPE = getattr(TYPE, fld)
+ return res
# _____________________________________________________________________________
# the following functions are used to find contained pointers
Modified: pypy/dist/pypy/rpython/memory/test/test_address.py
==============================================================================
--- pypy/dist/pypy/rpython/memory/test/test_address.py (original)
+++ pypy/dist/pypy/rpython/memory/test/test_address.py Mon Jan 23 14:52:58 2006
@@ -12,6 +12,7 @@
from pypy.rpython.lltypesystem.llmemory import Address
from pypy.rpython.memory.simulator import MemorySimulatorError
from pypy.rpython.memory.test.test_llinterpsim import interpret
+from pypy.rpython.lltypesystem import lltype
class TestAddressAnnotation(object):
def test_null(self):
@@ -411,3 +412,11 @@
assert get_py_object(addr1.address[0])(0) == 1
assert (addr1 + 10).signed[0] == 42
assert (addr1 + 20).char[0] == "a"
+
+ def test_add_offsetofs(self):
+ from pypy.rpython.lltypesystem.llmemory import offsetof
+ S = lltype.GcStruct("struct", ('a', lltype.Signed), ('b', lltype.Signed))
+ addr = raw_malloc(100)
+ (addr + offsetof(S, 'b')).signed[0] = 42
+ assert (addr + offsetof(S, 'b')).signed[0] == 42
+
More information about the Pypy-commit
mailing list