ยปCore Development>Code coverage>Lib/test/test_bigaddrspace.py

Python code coverage for Lib/test/test_bigaddrspace.py

#countcontent
1n/a"""
2n/aThese tests are meant to exercise that requests to create objects bigger
3n/athan what the address space allows are properly met with an OverflowError
4n/a(rather than crash weirdly).
5n/a
6n/aPrimarily, this means 32-bit builds with at least 2 GB of available memory.
7n/aYou need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to
8n/abe enabled.
9n/a"""
10n/a
11n/afrom test import support
12n/afrom test.support import bigaddrspacetest, MAX_Py_ssize_t
13n/a
14n/aimport unittest
15n/aimport operator
16n/aimport sys
17n/a
18n/a
19n/aclass BytesTest(unittest.TestCase):
20n/a
21n/a @bigaddrspacetest
22n/a def test_concat(self):
23n/a # Allocate a bytestring that's near the maximum size allowed by
24n/a # the address space, and then try to build a new, larger one through
25n/a # concatenation.
26n/a try:
27n/a x = b"x" * (MAX_Py_ssize_t - 128)
28n/a self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
29n/a finally:
30n/a x = None
31n/a
32n/a @bigaddrspacetest
33n/a def test_optimized_concat(self):
34n/a try:
35n/a x = b"x" * (MAX_Py_ssize_t - 128)
36n/a
37n/a with self.assertRaises(OverflowError) as cm:
38n/a # this statement used a fast path in ceval.c
39n/a x = x + b"x" * 128
40n/a
41n/a with self.assertRaises(OverflowError) as cm:
42n/a # this statement used a fast path in ceval.c
43n/a x += b"x" * 128
44n/a finally:
45n/a x = None
46n/a
47n/a @bigaddrspacetest
48n/a def test_repeat(self):
49n/a try:
50n/a x = b"x" * (MAX_Py_ssize_t - 128)
51n/a self.assertRaises(OverflowError, operator.mul, x, 128)
52n/a finally:
53n/a x = None
54n/a
55n/a
56n/aclass StrTest(unittest.TestCase):
57n/a
58n/a unicodesize = 2 if sys.maxunicode < 65536 else 4
59n/a
60n/a @bigaddrspacetest
61n/a def test_concat(self):
62n/a try:
63n/a # Create a string that would fill almost the address space
64n/a x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
65n/a # Unicode objects trigger MemoryError in case an operation that's
66n/a # going to cause a size overflow is executed
67n/a self.assertRaises(MemoryError, operator.add, x, x)
68n/a finally:
69n/a x = None
70n/a
71n/a @bigaddrspacetest
72n/a def test_optimized_concat(self):
73n/a try:
74n/a x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
75n/a
76n/a with self.assertRaises(MemoryError) as cm:
77n/a # this statement uses a fast path in ceval.c
78n/a x = x + x
79n/a
80n/a with self.assertRaises(MemoryError) as cm:
81n/a # this statement uses a fast path in ceval.c
82n/a x += x
83n/a finally:
84n/a x = None
85n/a
86n/a @bigaddrspacetest
87n/a def test_repeat(self):
88n/a try:
89n/a x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
90n/a self.assertRaises(MemoryError, operator.mul, x, 2)
91n/a finally:
92n/a x = None
93n/a
94n/a
95n/adef test_main():
96n/a support.run_unittest(BytesTest, StrTest)
97n/a
98n/aif __name__ == '__main__':
99n/a if len(sys.argv) > 1:
100n/a support.set_memlimit(sys.argv[1])
101n/a test_main()