Exemple #1
0
 def test_size_of_zlib_compress_obj(self):
     # zlib compress objects allocate a lot of extra buffers, we want to
     # track that. Note that we are approximating it, because we don't
     # actually inspect the C attributes. But it is a closer approximation
     # than not doing this.
     c = zlib.compressobj()
     self.assertTrue(_scanner.size_of(c) > 256000)
     self.assertEqual(0, _scanner.size_of(c) % _scanner._word_size)
 def test_size_of_zlib_compress_obj(self):
     # zlib compress objects allocate a lot of extra buffers, we want to
     # track that. Note that we are approximating it, because we don't
     # actually inspect the C attributes. But it is a closer approximation
     # than not doing this.
     c = zlib.compressobj()
     self.assertTrue(_scanner.size_of(c) > 256000)
     self.assertEqual(0, _scanner.size_of(c) % _scanner._word_size)
Exemple #3
0
 def test_bytes(self):
     mystr = b'a string'
     self.assertDumpBytes(
         '{"address": %d, "type": "%s", "size": %d, "len": 8'
         ', "value": "a string", "refs": []}\n' %
         (id(mystr), bytes.__name__, _scanner.size_of(mystr)), mystr)
     mystr = b'a \\str/with"control'
     self.assertDumpBytes(
         '{"address": %d, "type": "%s", "size": %d, "len": 19'
         ', "value": "a \\\\str\\/with\\"control", "refs": []}\n' %
         (id(mystr), bytes.__name__, _scanner.size_of(mystr)), mystr)
Exemple #4
0
 def test_str(self):
     mystr = 'a string'
     self.assertDumpText(
         '{"address": %d, "type": "str", "size": %d, "len": 8'
         ', "value": "a string", "refs": []}\n' %
         (id(mystr), _scanner.size_of(mystr)), mystr)
     mystr = 'a \\str/with"control'
     self.assertDumpText(
         '{"address": %d, "type": "str", "size": %d, "len": 19'
         ', "value": "a \\\\str\\/with\\"control", "refs": []}\n' %
         (id(mystr), _scanner.size_of(mystr)), mystr)
Exemple #5
0
 def test_bool(self):
     a = True
     b = False
     self.assertDumpText(
         '{"address": %d, "type": "bool", "size": %d'
         ', "value": "True", "refs": []}\n' % (id(a), _scanner.size_of(a)),
         a)
     self.assertDumpText(
         '{"address": %d, "type": "bool", "size": %d'
         ', "value": "False", "refs": []}\n' % (id(b), _scanner.size_of(b)),
         b)
 def test_bool(self):
     a = True
     b = False
     self.assertDumpText(
         '{"address": %d, "type": "bool", "size": %d'
         ', "value": "True", "refs": []}\n'
         % (id(a), _scanner.size_of(a)), a)
     self.assertDumpText(
         '{"address": %d, "type": "bool", "size": %d'
         ', "value": "False", "refs": []}\n'
         % (id(b), _scanner.size_of(b)), b)
 def test_str(self):
     mystr = 'a string'
     self.assertDumpText(
         '{"address": %d, "type": "str", "size": %d, "len": 8'
         ', "value": "a string", "refs": []}\n'
         % (id(mystr), _scanner.size_of(mystr)),
         mystr)
     mystr = 'a \\str/with"control'
     self.assertDumpText(
         '{"address": %d, "type": "str", "size": %d, "len": 19'
         ', "value": "a \\\\str\\/with\\"control", "refs": []}\n'
         % (id(mystr), _scanner.size_of(mystr)),
         mystr)
 def test_unicode(self):
     myu = u'a \xb5nicode'
     self.assertDumpText(
         '{"address": %d, "type": "unicode", "size": %d'
         ', "len": 9, "value": "a \\u00b5nicode", "refs": []}\n' % (
             id(myu), _scanner.size_of(myu)),
         myu)
Exemple #9
0
def _py_dump_json_obj(obj):
    klass = getattr(obj, '__class__', None)
    if klass is None:
        # This is an old style class
        klass = type(obj)
    content = [('{"address": %d'
                ', "type": %s'
                ', "size": %d') %
               (id(obj), _str_to_json(klass.__name__), _scanner.size_of(obj))]
    name = getattr(obj, '__name__', None)
    if name is not None:
        content.append(', "name": %s' % (_str_to_json(name), ))
    if getattr(obj, '__len__', None) is not None:
        content.append(', "len": %s' % (len(obj), ))
    if isinstance(obj, bytes):
        content.append(', "value": %s' % (_bytes_to_json(obj[:100]), ))
    elif isinstance(obj, six.text_type):
        content.append(', "value": %s' % (_text_to_json(obj[:100]), ))
    elif obj is True:
        content.append(', "value": "True"')
    elif obj is False:
        content.append(', "value": "False"')
    elif isinstance(obj, int):
        content.append(', "value": %d' % (obj, ))
    elif isinstance(obj, types.FrameType):
        content.append(', "value": "%s"' % (obj.f_code.co_name, ))
    content.append(', "refs": [')
    ref_strs = []
    for ref in gc.get_referents(obj):
        ref_strs.append('%d' % (id(ref), ))
    content.append(', '.join(ref_strs))
    content.append(']')
    content.append('}\n')
    return ''.join(content).encode('UTF-8')
Exemple #10
0
 def test_unicode(self):
     myu = 'a \xb5nicode'
     self.assertDumpText(
         '{"address": %d, "type": "unicode", "size": %d'
         ', "len": 9, "value": "a \\u00b5nicode", "refs": []}\n' % (
             id(myu), _scanner.size_of(myu)),
         myu)
Exemple #11
0
 def test_unicode(self):
     myu = u'a \xb5nicode'
     self.assertDumpBytes(
         '{"address": %d, "type": "%s", "size": %d'
         ', "len": 9, "value": "a \\u00b5nicode", "refs": []}\n' % (
             id(myu), six.text_type.__name__, _scanner.size_of(myu)),
         myu)
Exemple #12
0
 def test_tuple(self):
     a = object()
     b = object()
     t = (a, b)
     self.assertDumpText(
         '{"address": %d, "type": "tuple", "size": %d'
         ', "len": 2, "refs": [%d, %d]}\n' %
         (id(t), _scanner.size_of(t), id(b), id(a)), t)
Exemple #13
0
 def test_tuple(self):
     a = object()
     b = object()
     t = (a, b)
     self.assertDumpText(
         '{"address": %d, "type": "tuple", "size": %d'
         ', "len": 2, "refs": [%d, %d]}\n'
         % (id(t), _scanner.size_of(t), id(b), id(a)), t)
Exemple #14
0
def _py_dump_json_obj(obj):
    klass = getattr(obj, '__class__', None)
    if klass is None:
        # This is an old style class
        klass = type(obj)
    content = [(
        '{"address": %d'
        ', "type": %s'
        ', "size": %d'
        ) % (id(obj), _string_to_json(klass.__name__),
             _scanner.size_of(obj))
        ]
    name = getattr(obj, '__name__', None)
    if name is not None:
        content.append(', "name": %s' % (_string_to_json(name),))
    if getattr(obj, '__len__', None) is not None:
        content.append(', "len": %s' % (len(obj),))
    if isinstance(obj, str):
        content.append(', "value": %s' % (_string_to_json(obj[:100]),))
    elif isinstance(obj, unicode):
        content.append(', "value": %s' % (_unicode_to_json(obj[:100]),))
    elif obj is True:
        content.append(', "value": "True"')
    elif obj is False:
        content.append(', "value": "False"')
    elif isinstance(obj, int):
        content.append(', "value": %d' % (obj,))
    elif isinstance(obj, types.FrameType):
        content.append(', "value": "%s"' % (obj.f_code.co_name,))
    first = True
    content.append(', "refs": [')
    ref_strs = []
    for ref in gc.get_referents(obj):
        ref_strs.append('%d' % (id(ref),))
    content.append(', '.join(ref_strs))
    content.append(']')
    content.append('}\n')
    return ''.join(content)
Exemple #15
0
 def test_module(self):
     m = _scanner
     self.assertDumpText(
         '{"address": %d, "type": "module", "size": %d'
         ', "name": "meliae._scanner", "refs": [%d]}\n' %
         (id(m), _scanner.size_of(m), id(m.__dict__)), m)
Exemple #16
0
 def test_obj(self):
     obj = object()
     self.assertDumpText(
         '{"address": %d, "type": "object", "size": %d, "refs": []}\n' %
         (id(obj), _scanner.size_of(obj)), obj)
Exemple #17
0
 def test_size_of_zlib_decompress_obj(self):
     d = zlib.decompressobj()
     self.assertTrue(_scanner.size_of(d) > 30000)
     self.assertEqual(0, _scanner.size_of(d) % _scanner._word_size)
Exemple #18
0
 def test_size_of_zlib_decompress_obj(self):
     d = zlib.decompressobj()
     self.assertTrue(_scanner.size_of(d) > 30000)
     self.assertEqual(0, _scanner.size_of(d) % _scanner._word_size)
Exemple #19
0
 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))
Exemple #20
0
 def test_obj(self):
     obj = object()
     self.assertDumpText(
         '{"address": %d, "type": "object", "size": %d, "refs": []}\n'
         % (id(obj), _scanner.size_of(obj)), obj)
Exemple #21
0
 def test_module(self):
     m = _scanner
     self.assertDumpText(
         '{"address": %d, "type": "module", "size": %d'
         ', "name": "meliae._scanner", "refs": [%d]}\n'
         % (id(m), _scanner.size_of(m), id(m.__dict__)), m)
Exemple #22
0
 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))
Exemple #23
0
 def assertSizeOf(self, obj):
     self.assertEqual(sys.getsizeof(obj), _scanner.size_of(obj))