blob: b639f68c2347bf72f0e7dff814e2d5b14843c2db [file] [log] [blame]
Antoine Pitrou99c48302010-11-04 20:48:37 +00001"""
2These tests are meant to exercise that requests to create objects bigger
3than what the address space allows are properly met with an OverflowError
4(rather than crash weirdly).
5
Victor Stinner8c663fd2017-11-08 14:44:44 -08006Primarily, this means 32-bit builds with at least 2 GiB of available memory.
Antoine Pitrou99c48302010-11-04 20:48:37 +00007You need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to
8be enabled.
9"""
10
Benjamin Petersonee8712c2008-05-20 21:35:26 +000011from test import support
12from test.support import bigaddrspacetest, MAX_Py_ssize_t
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013
14import unittest
15import operator
16import sys
17
18
Antoine Pitrou99c48302010-11-04 20:48:37 +000019class BytesTest(unittest.TestCase):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020
21 @bigaddrspacetest
22 def test_concat(self):
Antoine Pitrou99c48302010-11-04 20:48:37 +000023 # 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.
Antoine Pitrou1e285132011-01-12 22:02:45 +000026 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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031
32 @bigaddrspacetest
33 def test_optimized_concat(self):
Antoine Pitrou1e285132011-01-12 22:02:45 +000034 try:
35 x = b"x" * (MAX_Py_ssize_t - 128)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
Antoine Pitrou1e285132011-01-12 22:02:45 +000037 with self.assertRaises(OverflowError) as cm:
38 # this statement used a fast path in ceval.c
39 x = x + b"x" * 128
Antoine Pitrou99c48302010-11-04 20:48:37 +000040
Antoine Pitrou1e285132011-01-12 22:02:45 +000041 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
Antoine Pitrou99c48302010-11-04 20:48:37 +000046
47 @bigaddrspacetest
48 def test_repeat(self):
Antoine Pitrou1e285132011-01-12 22:02:45 +000049 try:
50 x = b"x" * (MAX_Py_ssize_t - 128)
51 self.assertRaises(OverflowError, operator.mul, x, 128)
52 finally:
53 x = None
Antoine Pitrou99c48302010-11-04 20:48:37 +000054
55
56class StrTest(unittest.TestCase):
57
58 unicodesize = 2 if sys.maxunicode < 65536 else 4
59
60 @bigaddrspacetest
61 def test_concat(self):
Antoine Pitrou1e285132011-01-12 22:02:45 +000062 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
Antoine Pitrou99c48302010-11-04 20:48:37 +000070
71 @bigaddrspacetest
72 def test_optimized_concat(self):
Antoine Pitrou1e285132011-01-12 22:02:45 +000073 try:
74 x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
Antoine Pitrou99c48302010-11-04 20:48:37 +000075
Antoine Pitrou1e285132011-01-12 22:02:45 +000076 with self.assertRaises(MemoryError) as cm:
77 # this statement uses a fast path in ceval.c
78 x = x + x
Antoine Pitrou99c48302010-11-04 20:48:37 +000079
Antoine Pitrou1e285132011-01-12 22:02:45 +000080 with self.assertRaises(MemoryError) as cm:
81 # this statement uses a fast path in ceval.c
82 x += x
83 finally:
84 x = None
Antoine Pitrou99c48302010-11-04 20:48:37 +000085
86 @bigaddrspacetest
87 def test_repeat(self):
Antoine Pitrou1e285132011-01-12 22:02:45 +000088 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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093
94
95def test_main():
Antoine Pitrou99c48302010-11-04 20:48:37 +000096 support.run_unittest(BytesTest, StrTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097
98if __name__ == '__main__':
99 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000100 support.set_memlimit(sys.argv[1])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101 test_main()