Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e28513

Browse files
committed
Fix test_bigaddrspace (some tests didn't trigger the expected MemoryError)
1 parent 98c62bd commit 1e28513

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

Lib/test/test_bigaddrspace.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,34 @@ def test_concat(self):
2323
# Allocate a bytestring that's near the maximum size allowed by
2424
# the address space, and then try to build a new, larger one through
2525
# concatenation.
26-
x = b"x" * (MAX_Py_ssize_t - 128)
27-
self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
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
2831

2932
@bigaddrspacetest
3033
def test_optimized_concat(self):
31-
x = b"x" * (MAX_Py_ssize_t - 128)
34+
try:
35+
x = b"x" * (MAX_Py_ssize_t - 128)
3236

33-
with self.assertRaises(OverflowError) as cm:
34-
# this statement uses a fast path in ceval.c
35-
x = x + b"x" * 128
37+
with self.assertRaises(OverflowError) as cm:
38+
# this statement used a fast path in ceval.c
39+
x = x + b"x" * 128
3640

37-
with self.assertRaises(OverflowError) as cm:
38-
# this statement uses a fast path in ceval.c
39-
x += b"x" * 128
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
4046

4147
@bigaddrspacetest
4248
def test_repeat(self):
43-
x = b"x" * (MAX_Py_ssize_t - 128)
44-
self.assertRaises(OverflowError, operator.mul, x, 128)
49+
try:
50+
x = b"x" * (MAX_Py_ssize_t - 128)
51+
self.assertRaises(OverflowError, operator.mul, x, 128)
52+
finally:
53+
x = None
4554

4655

4756
class StrTest(unittest.TestCase):
@@ -50,28 +59,37 @@ class StrTest(unittest.TestCase):
5059

5160
@bigaddrspacetest
5261
def test_concat(self):
53-
# Create a string half the size that would fill the address space
54-
x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
55-
# Unicode objects trigger MemoryError in case an operation that's
56-
# going to cause a size overflow is executed
57-
self.assertRaises(MemoryError, operator.add, x, x)
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
5870

5971
@bigaddrspacetest
6072
def test_optimized_concat(self):
61-
x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
73+
try:
74+
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
6275

63-
with self.assertRaises(MemoryError) as cm:
64-
# this statement uses a fast path in ceval.c
65-
x = x + x
76+
with self.assertRaises(MemoryError) as cm:
77+
# this statement uses a fast path in ceval.c
78+
x = x + x
6679

67-
with self.assertRaises(MemoryError) as cm:
68-
# this statement uses a fast path in ceval.c
69-
x += x
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
7085

7186
@bigaddrspacetest
7287
def test_repeat(self):
73-
x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
74-
self.assertRaises(MemoryError, operator.mul, x, 2)
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
7593

7694

7795
def test_main():

0 commit comments

Comments
 (0)