Rev 129: Start some direct testing of the Proxy object. in http://bazaar.launchpad.net/~jameinel/meliae/mem-object-collection
John Arbash Meinel
john at arbash-meinel.com
Mon Dec 28 00:55:02 GMT 2009
At http://bazaar.launchpad.net/~jameinel/meliae/mem-object-collection
------------------------------------------------------------
revno: 129
revision-id: john at arbash-meinel.com-20091228005448-hm6ll74g7cw7slw2
parent: john at arbash-meinel.com-20091228003801-31hv17bjdpbmikv5
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: mem-object-collection
timestamp: Sun 2009-12-27 18:54:48 -0600
message:
Start some direct testing of the Proxy object.
-------------- next part --------------
=== modified file 'meliae/_loader.pyx'
--- a/meliae/_loader.pyx 2009-12-28 00:38:01 +0000
+++ b/meliae/_loader.pyx 2009-12-28 00:54:48 +0000
@@ -156,31 +156,20 @@
these on-the-fly.
"""
- # Note that it is possible for the table to be mutated while this object is
- # live, which is why we don't hold a direct pointer to the _MemObject, and
- # instead go through a table lookup for all attributes. An alternative
- # option would be to hold a copy-by-value, and only go back to the original
- # object in __set__ functions.
- # Alternatively, we could keep track of all proxy objects returned, and
- # point them to the new locations when things move.
- # We may want to keep track of them anyway, so that we don't create 20
- # proxy objects for the same address. TBD
-
cdef MemObjectCollection collection
cdef public object address
+ cdef _MemObject *_obj
+ cdef object __weakref__
def __init__(self, address, collection):
self.address = address
self.collection = collection
+ self._obj = NULL
cdef _MemObject *_get_obj(self) except NULL:
- cdef _MemObject **slot
-
- slot = self.collection._lookup(self.address)
- if slot[0] == NULL or slot[0] == _dummy:
- raise RuntimeError('we failed to lookup the obj at address %d'
- % (self.address,))
- return slot[0]
+ if self._obj is NULL:
+ raise RuntimeError('_MemObjectProxy was deleted underneath it.')
+ return self._obj
property type_str:
"""The type of this object."""
@@ -318,21 +307,26 @@
def __getitem__(self, at):
cdef _MemObject **slot
+ cdef _MemObjectProxy proxy
if isinstance(at, _MemObjectProxy):
address = at.address
+ proxy = at
else:
address = at
+ proxy = None
slot = self._lookup(address)
if slot[0] == NULL or slot[0] == _dummy:
raise KeyError('address %s not present' % (at,))
- if at is address:
- return _MemObjectProxy(address, self)
- return at
+ if proxy is None:
+ proxy = _MemObjectProxy(address, self)
+ proxy._obj = slot[0]
+ return proxy
def __delitem__(self, at):
cdef _MemObject **slot
+ cdef _MemObjectProxy proxy
if isinstance(at, _MemObjectProxy):
address = at.address
=== modified file 'meliae/tests/test__loader.py'
--- a/meliae/tests/test__loader.py 2009-12-28 00:38:01 +0000
+++ b/meliae/tests/test__loader.py 2009-12-28 00:54:48 +0000
@@ -202,3 +202,24 @@
mop = moc[1024]
self.assertEqual(1024, mop.address)
self.assertEqual(1124, mop.size)
+
+
+class Test_MemObjectProxy(tests.TestCase):
+
+ def setUp(self):
+ super(Test_MemObjectProxy, self).setUp()
+ self.moc = _loader.MemObjectCollection()
+ self.moc.add(1024, 'bar', 200)
+ self.moc.add(0, 'foo', 100)
+ self.moc.add(255, 'baz', 300)
+ del self.moc[1024]
+
+ def test_basic_proxy(self):
+ mop = self.moc[0]
+ self.assertEqual(0, mop.address)
+ self.assertEqual('foo', mop.type_str)
+ self.assertEqual(100, mop.size)
+ mop.size = 1024
+ self.assertEqual(1024, mop.size)
+ self.assertEqual(1024, self.moc[0].size)
+ self.assertEqual(0, len(mop))
More information about the bazaar-commits
mailing list