Rev 160: Add __sizeof__ to IntSet and IDSet. in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk
John Arbash Meinel
john at arbash-meinel.com
Wed Jul 28 21:36:56 BST 2010
At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk
------------------------------------------------------------
revno: 160
revision-id: john at arbash-meinel.com-20100728203640-8ng11q4olwxl2gyh
parent: john at arbash-meinel.com-20100728202728-jkg83mq76uih962e
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Wed 2010-07-28 15:36:40 -0500
message:
Add __sizeof__ to IntSet and IDSet.
-------------- next part --------------
=== modified file 'CHANGES.txt'
--- a/CHANGES.txt 2010-07-20 16:08:27 +0000
+++ b/CHANGES.txt 2010-07-28 20:36:40 +0000
@@ -10,6 +10,10 @@
:0.3: (not released yet)
+* Add ``__sizeof__`` members to a lot of the core classes (IntSet,
+ etc.) (John Arbash Meinel)
+
+
Meliae 0.2.1
############
=== modified file 'meliae/_intset.pyx'
--- a/meliae/_intset.pyx 2010-06-30 17:29:31 +0000
+++ b/meliae/_intset.pyx 2010-07-28 20:36:40 +0000
@@ -65,6 +65,12 @@
def __len__(self):
return self._count
+ def __sizeof__(self):
+ my_size = (sizeof(Py_ssize_t)*2 + sizeof(int) + sizeof(int_type*))
+ if self._array != NULL:
+ my_size += (sizeof(int_type) * (self._mask + 1))
+ return my_size
+
def _peek_array(self):
cdef Py_ssize_t i, size
if self._array == NULL:
=== modified file 'meliae/tests/test__intset.py'
--- a/meliae/tests/test__intset.py 2010-01-29 22:19:38 +0000
+++ b/meliae/tests/test__intset.py 2010-07-28 20:36:40 +0000
@@ -18,6 +18,7 @@
from meliae import (
_intset,
+ _scanner,
tests,
)
@@ -108,6 +109,24 @@
# Not supported yet... KnownFailure
pass
+ def assertSizeOf(self, num_words, obj, extra_size=0, has_gc=True):
+ expected_size = extra_size + num_words * _scanner._word_size
+ if has_gc:
+ expected_size += _scanner._gc_head_size
+ self.assertEqual(expected_size, _scanner.size_of(obj))
+
+ def test__sizeof__(self):
+ # The intset class should report a size
+ iset = self._set_type([])
+ # I'm a bit leery of has_gc=False, as I think some versions of pyrex
+ # will put the object into GC even though it doesn't have any 'object'
+ # references...
+ # We could do something with a double-entry check
+ self.assertSizeOf(3, iset, extra_size=4, has_gc=False)
+ iset.add(12345)
+ # Min allocation is 256 entries
+ self.assertSizeOf(3+256, iset, extra_size=4, has_gc=False)
+
class TestIDSet(TestIntSet):
More information about the bazaar-commits
mailing list