Exemplo n.º 1
0
 def test_sizeof(self):
     basesize = support.calcobjsize('P2nN2Pn')
     check = self.check_sizeof
     self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
     check(io.BytesIO(), basesize )
     check(io.BytesIO(b'a'), basesize + 1 + 1 )
     check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
Exemplo n.º 2
0
 def test_sizeof(self):
     basesize = support.calcobjsize('P2n2Pn')
     check = self.check_sizeof
     self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
     check(io.BytesIO(), basesize )
     n = 1000  # use a variable to prevent constant folding
     check(io.BytesIO(b'a' * n), basesize + sys.getsizeof(b'a' * n))
Exemplo n.º 3
0
 def test_sizeof(self):
     basesize = support.calcobjsize('P2n2Pn')
     check = self.check_sizeof
     self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
     check(io.BytesIO(), basesize)
     n = 1000  # use a variable to prevent constant folding
     check(io.BytesIO(b'a' * n), basesize + sys.getsizeof(b'a' * n))
Exemplo n.º 4
0
    def test_sizeof(self):
        def XXXROUNDUP(n):
            if n <= 1:
                return n
            if n <= 128:
                return (n + 3) & ~3
            return 1 << (n - 1).bit_length()

        basesize = support.calcobjsize('Pii')
        nodesize = struct.calcsize('hP3iP0h')
        def sizeofchildren(node):
            if node is None:
                return 0
            res = 0
            hasstr = len(node) > 1 and isinstance(node[-1], str)
            if hasstr:
                res += len(node[-1]) + 1
            children = node[1:-1] if hasstr else node[1:]
            if children:
                res += XXXROUNDUP(len(children)) * nodesize
                for child in children:
                    res += sizeofchildren(child)
            return res

        def check_st_sizeof(st):
            self.check_sizeof(st, basesize + nodesize +
                                  sizeofchildren(st.totuple()))

        check_st_sizeof(parser.expr('2 + 3'))
        check_st_sizeof(parser.expr('2 + 3 + 4'))
        check_st_sizeof(parser.suite('x = 2 + 3'))
        check_st_sizeof(parser.suite(''))
        check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-'))
        check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']'))
Exemplo n.º 5
0
 def test_sizeof(self):
     BLOCKLEN = 62
     basesize = support.calcobjsize("2P4nlP")
     blocksize = struct.calcsize("2P%dP" % BLOCKLEN)
     self.assertEqual(object.__sizeof__(deque()), basesize)
     check = self.check_sizeof
     check(deque(), basesize + blocksize)
     check(deque("a"), basesize + blocksize)
     check(deque("a" * (BLOCKLEN // 2)), basesize + blocksize)
     check(deque("a" * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize)
     check(deque("a" * (42 * BLOCKLEN)), basesize + 43 * blocksize)
Exemplo n.º 6
0
 def test_sizeof(self):
     BLOCKLEN = 62
     basesize = support.calcobjsize('2P4PlP')
     blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
     self.assertEqual(object.__sizeof__(deque()), basesize)
     check = self.check_sizeof
     check(deque(), basesize + blocksize)
     check(deque('a'), basesize + blocksize)
     check(deque('a' * (BLOCKLEN // 2)), basesize + blocksize)
     check(deque('a' * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize)
     check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize)
Exemplo n.º 7
0
 def test_sizeof(self):
     BLOCKLEN = 62
     basesize = support.calcobjsize('2P4nlP')
     blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
     self.assertEqual(object.__sizeof__(deque()), basesize)
     check = self.check_sizeof
     check(deque(), basesize + blocksize)
     check(deque('a'), basesize + blocksize)
     check(deque('a' * (BLOCKLEN - 1)), basesize + blocksize)
     check(deque('a' * BLOCKLEN), basesize + 2 * blocksize)
     check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize)
Exemplo n.º 8
0
 def test_pickler(self):
     basesize = support.calcobjsize('5P2n3i2n3iP')
     p = _pickle.Pickler(io.BytesIO())
     self.assertEqual(object.__sizeof__(p), basesize)
     MT_size = struct.calcsize('3nP0n')
     ME_size = struct.calcsize('Pn0P')
     check = self.check_sizeof
     check(
         p,
         basesize + MT_size + 8 * ME_size + sys.getsizeof(b'x' * 4096))
     for i in range(6):
         p.dump(chr(i))
     check(p, basesize + MT_size + 32 * ME_size + 0)
Exemplo n.º 9
0
        def test_unpickler(self):
            basesize = support.calcobjsize('2P2n2P 2P2n2i5P 2P3n6P2n2i')
            unpickler = _pickle.Unpickler
            P = struct.calcsize('P')  # Size of memo table entry.
            n = struct.calcsize('n')  # Size of mark table entry.
            check = self.check_sizeof
            for encoding in 'ASCII', 'UTF-16', 'latin-1':
                for errors in 'strict', 'replace':
                    u = unpickler(io.BytesIO(),
                                  encoding=encoding,
                                  errors=errors)
                    self.assertEqual(object.__sizeof__(u), basesize)
                    check(
                        u,
                        basesize + 32 * P +  # Minimal memo table size.
                        len(encoding) + 1 + len(errors) + 1)

            stdsize = basesize + len('ASCII') + 1 + len('strict') + 1

            def check_unpickler(data, memo_size, marks_size):
                dump = pickle.dumps(data)
                u = unpickler(io.BytesIO(dump),
                              encoding='ASCII',
                              errors='strict')
                u.load()
                check(u, stdsize + memo_size * P + marks_size * n)

            check_unpickler(0, 32, 0)
            # 20 is minimal non-empty mark stack size.
            check_unpickler([0] * 100, 32, 20)
            # 128 is memo table size required to save references to 100 objects.
            check_unpickler([chr(i) for i in range(100)], 128, 20)

            def recurse(deep):
                data = 0
                for i in range(deep):
                    data = [data, data]
                return data

            check_unpickler(recurse(0), 32, 0)
            check_unpickler(recurse(1), 32, 20)
            check_unpickler(recurse(20), 32, 20)
            check_unpickler(recurse(50), 64, 60)
            check_unpickler(recurse(100), 128, 140)

            u = unpickler(io.BytesIO(pickle.dumps('a', 0)),
                          encoding='ASCII',
                          errors='strict')
            u.load()
            check(u, stdsize + 32 * P + 2 + 1)
Exemplo n.º 10
0
 def test_pickler(self):
     basesize = support.calcobjsize('7P2n3i2n3i2P')
     p = _pickle.Pickler(io.BytesIO())
     self.assertEqual(object.__sizeof__(p), basesize)
     MT_size = struct.calcsize('3nP0n')
     ME_size = struct.calcsize('Pn0P')
     check = self.check_sizeof
     check(p, basesize +
         MT_size + 8 * ME_size +  # Minimal memo table size.
         sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
     for i in range(6):
         p.dump(chr(i))
     check(p, basesize +
         MT_size + 32 * ME_size +  # Size of memo table required to
                                   # save references to 6 objects.
         0)  # Write buffer is cleared after every dump().
Exemplo n.º 11
0
 def test_pickler(self):
     basesize = support.calcobjsize('5P2n3i2n3iP')
     p = _pickle.Pickler(io.BytesIO())
     self.assertEqual(object.__sizeof__(p), basesize)
     MT_size = struct.calcsize('3nP0n')
     ME_size = struct.calcsize('Pn0P')
     check = self.check_sizeof
     check(p, basesize +
         MT_size + 8 * ME_size +  # Minimal memo table size.
         sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
     for i in range(6):
         p.dump(chr(i))
     check(p, basesize +
         MT_size + 32 * ME_size +  # Size of memo table required to
                                   # save references to 6 objects.
         0)  # Write buffer is cleared after every dump().
Exemplo n.º 12
0
        def test_unpickler(self):
            basesize = support.calcobjsize('2Pn2P 2P2n2i5P 2P3n6P2n2i')
            unpickler = _pickle.Unpickler
            P = struct.calcsize('P')
            n = struct.calcsize('n')
            check = self.check_sizeof
            for encoding in ('ASCII', 'UTF-16', 'latin-1'):
                for errors in ('strict', 'replace'):
                    u = unpickler(io.BytesIO(),
                                  encoding=encoding,
                                  errors=errors)
                    self.assertEqual(object.__sizeof__(u), basesize)
                    check(
                        u, basesize + 32 * P + len(encoding) + 1 +
                        len(errors) + 1)
            stdsize = basesize + len('ASCII') + 1 + len('strict') + 1

            def check_unpickler(data, memo_size, marks_size):
                dump = pickle.dumps(data)
                u = unpickler(io.BytesIO(dump),
                              encoding='ASCII',
                              errors='strict')
                u.load()
                check(u, stdsize + memo_size * P + marks_size * n)

            check_unpickler(0, 32, 0)
            check_unpickler([0] * 100, 32, 20)
            check_unpickler([chr(i) for i in range(100)], 128, 20)

            def recurse(deep):
                data = 0
                for i in range(deep):
                    data = [data, data]
                return data

            check_unpickler(recurse(0), 32, 0)
            check_unpickler(recurse(1), 32, 20)
            check_unpickler(recurse(20), 32, 58)
            check_unpickler(recurse(50), 64, 58)
            check_unpickler(recurse(100), 128, 134)
            u = unpickler(io.BytesIO(pickle.dumps('a', 0)),
                          encoding='ASCII',
                          errors='strict')
            u.load()
            check(u, stdsize + 32 * P + 2 + 1)
Exemplo n.º 13
0
        def test_unpickler(self):
            basesize = support.calcobjsize('2Pn2P 2P2n2i5P 2P3n6P2n2i')
            unpickler = _pickle.Unpickler
            P = struct.calcsize('P')  # Size of memo table entry.
            n = struct.calcsize('n')  # Size of mark table entry.
            check = self.check_sizeof
            for encoding in 'ASCII', 'UTF-16', 'latin-1':
                for errors in 'strict', 'replace':
                    u = unpickler(io.BytesIO(),
                                  encoding=encoding, errors=errors)
                    self.assertEqual(object.__sizeof__(u), basesize)
                    check(u, basesize +
                             32 * P +  # Minimal memo table size.
                             len(encoding) + 1 + len(errors) + 1)

            stdsize = basesize + len('ASCII') + 1 + len('strict') + 1
            def check_unpickler(data, memo_size, marks_size):
                dump = pickle.dumps(data)
                u = unpickler(io.BytesIO(dump),
                              encoding='ASCII', errors='strict')
                u.load()
                check(u, stdsize + memo_size * P + marks_size * n)

            check_unpickler(0, 32, 0)
            # 20 is minimal non-empty mark stack size.
            check_unpickler([0] * 100, 32, 20)
            # 128 is memo table size required to save references to 100 objects.
            check_unpickler([chr(i) for i in range(100)], 128, 20)
            def recurse(deep):
                data = 0
                for i in range(deep):
                    data = [data, data]
                return data
            check_unpickler(recurse(0), 32, 0)
            check_unpickler(recurse(1), 32, 20)
            check_unpickler(recurse(20), 32, 58)
            check_unpickler(recurse(50), 64, 58)
            check_unpickler(recurse(100), 128, 134)

            u = unpickler(io.BytesIO(pickle.dumps('a', 0)),
                          encoding='ASCII', errors='strict')
            u.load()
            check(u, stdsize + 32 * P + 2 + 1)
Exemplo n.º 14
0
 def check_sizeof(self, format_str, number_of_codes):
     # The size of 'PyStructObject'
     totalsize = support.calcobjsize('2n3P')
     # The size taken up by the 'formatcode' dynamic array
     totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
     support.check_sizeof(self, struct.Struct(format_str), totalsize)
Exemplo n.º 15
0
 def setUp(self):
     self.elementsize = support.calcobjsize('5P')
     # extra
     self.extra = struct.calcsize('PiiP4P')
Exemplo n.º 16
0
 def check_sizeof(self, format_str, number_of_codes):
     # The size of 'PyStructObject'
     totalsize = support.calcobjsize('2n3P')
     # The size taken up by the 'formatcode' dynamic array
     totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
     support.check_sizeof(self, struct.Struct(format_str), totalsize)
Exemplo n.º 17
0
 def setUp(self):
     self.elementsize = support.calcobjsize("5P")
     # extra
     self.extra = struct.calcsize("PnnP4P")
Exemplo n.º 18
0
 def setUp(self):
     self.elementsize = support.calcobjsize('5P')
     # extra
     self.extra = struct.calcsize('PnnP4P')
Exemplo n.º 19
0
 def check_sizeof(self, format_str, number_of_codes):
     totalsize = support.calcobjsize('2n3P')
     totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
     support.check_sizeof(self, struct.Struct(format_str), totalsize)