Skip to content

Commit f54b80f

Browse files
committed
Add Lib/test/test_bigaddrspace.py from CPython v3.12.0
1 parent 87cf891 commit f54b80f

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Lib/test/test_bigaddrspace.py

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

0 commit comments

Comments
 (0)