def test_flatsize(failf=None, stdf=None): '''Compare the results of **flatsize()** without using ``sys.getsizeof()`` with the accurate sizes returned by ``sys.getsizeof()``. Return the total number of tests and number of unexpected failures. Expect differences for sequences as dicts, lists, sets, tuples, etc. While this is no proof for the accuracy of **flatsize()** on Python builds without ``sys.getsizeof()``, it does provide some evidence that function **flatsize()** produces reasonable and usable results. ''' t, g, e = [], asizeof._getsizeof, 0 if g: for v in asizeof._values(asizeof._typedefs): t.append(v.type) try: # creating one instance if v.type.__module__ not in ('io',): # avoid 3.0 RuntimeWarning t.append(v.type()) except Exception: # ignore errors pass t.extend(({1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}, [1, 2, 3, 4, 5, 6, 7, 8], ['1', '2', '3'], [0] * 100, '12345678', 'x' * 1001, (1, 2, 3, 4, 5, 6, 7, 8), ('1', '2', '3'), (0,) * 100, asizeof._Slots((1, 2, 3, 4, 5, 6, 7, 8)), asizeof._Slots(('1', '2', '3')), asizeof._Slots((0,) * 100), 0, 1 << 8, 1 << 16, 1 << 32, 1 << 64, 1 << 128, complex(0, 1), True, False)) asizeof._getsizeof = None # zap _getsizeof for flatsize() for o in t: a = asizeof.flatsize(o) s = sys.getsizeof(o, 0) # 0 as default if a != s: if isinstance(o, (dict, list, set, frozenset, tuple, bytes)): # flatsize() approximates length of sequences x = ', expected failure' elif (isinstance(o, (type, bool, asizeof.ABCMeta)) and sys.version_info >= (3, 0)): x = ', expected failure' elif isinstance(o, str) and sys.version_info >= (3, 3): x = ', expected failure' elif isinstance(o, deque) and sys.version_info >= (3, 4): x = ', expected failure' else: x = ', %r' % asizeof._typedefof(o) e += 1 if failf: # report failure failf('%s vs %s for %s: %s', a, s, asizeof._nameof(type(o)), _repr(o)) if stdf: asizeof._printf('flatsize() %s vs sys.getsizeof() %s for %s: %s%s', a, s, asizeof._nameof(type(o)), _repr(o), x, file=stdf) asizeof._getsizeof = g # restore return len(t), e
def test_iterator(self): '''Test iterator examples''' self._printf('%sasizeof(%s, code=%s) ... %s', os.linesep, '<iterator>', False, '-iter[ator]') o = iter('0123456789') e = iter('') d = iter({}) i = iter(asizeof._items({1:1})) k = iter(asizeof._keys({2:2, 3:3})) v = iter(asizeof._values({4:4, 5:5, 6:6})) l = iter([]) t = iter(()) asizeof.asizesof(o, e, d, i, k, v, l, t, limit=0, code=False, stats=1) asizeof.asizesof(o, e, d, i, k, v, l, t, limit=9, code=False, stats=1)
def test_flatsize(failf=None, stdf=None): '''Compare the results of **flatsize()** without using ``sys.getsizeof()`` with the accurate sizes returned by ``sys.getsizeof()``. Return the total number of tests and number of unexpected failures. Expect differences for sequences as dicts, lists, sets, tuples, etc. While this is no proof for the accuracy of **flatsize()** on Python builds without ``sys.getsizeof()``, it does provide some evidence that function **flatsize()** produces reasonable and usable results. ''' t, g, e = [], asizeof._getsizeof, 0 if g: for v in asizeof._values(asizeof._typedefs): t.append(v.type) try: # creating one instance if v.type.__module__ not in ( 'io', ): # avoid 3.0 RuntimeWarning t.append(v.type()) except Exception: # ignore errors pass t.extend( ({ 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8 }, [1, 2, 3, 4, 5, 6, 7, 8], ['1', '2', '3'], [0] * 100, '12345678', 'x' * 1001, (1, 2, 3, 4, 5, 6, 7, 8), ('1', '2', '3'), (0, ) * 100, asizeof._Slots((1, 2, 3, 4, 5, 6, 7, 8)), asizeof._Slots(('1', '2', '3')), asizeof._Slots( (0, ) * 100), 0, 1 << 8, 1 << 16, 1 << 32, 1 << 64, 1 << 128, complex(0, 1), True, False)) asizeof._getsizeof = None # zap _getsizeof for flatsize() for o in t: a = asizeof.flatsize(o) s = sys.getsizeof(o, 0) # 0 as default if a != s: if isinstance(o, (dict, list, set, frozenset, tuple, bytes)): # flatsize() approximates length of sequences x = ', expected failure' elif (isinstance(o, (type, bool, asizeof.ABCMeta)) and sys.version_info >= (3, 0)): x = ', expected failure' elif isinstance(o, str) and sys.version_info >= (3, 3): x = ', expected failure' elif isinstance(o, deque) and sys.version_info >= (3, 4): x = ', expected failure' else: x = ', %r' % asizeof._typedefof(o) e += 1 if failf: # report failure failf('%s vs %s for %s: %s', a, s, asizeof._nameof(type(o)), _repr(o)) if stdf: asizeof._printf( 'flatsize() %s vs sys.getsizeof() %s for %s: %s%s', a, s, asizeof._nameof(type(o)), _repr(o), x, file=stdf) asizeof._getsizeof = g # restore return len(t), e