Example #1
0
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
Example #2
0
def get_named_refs(obj):
    """
    Return all named referents of `obj`. Reuse functionality from asizeof.
    Does not return referents without a name, e.g. objects in a list.
    TODO: Move this to a different module, probably asizeof.
    """
    refs = []
    v = _typedefof(obj)
    if v:
        v = v.refs
        if v and _callable(v):
            for ref in v(obj, True):
                try:
                    refs.append((ref.name, ref.ref))
                except AttributeError:
                    pass
    return refs
Example #3
0
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._typedefs.copy().values():
            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