blob: d383440356ca774c2b4ff97bb0c3ca28cd1ea0fd [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
6Primarily, this means 32-bit builds with at least 2 GB of available memory.
7You 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.
26 x = b"x" * (MAX_Py_ssize_t - 128)
27 self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028
29 @bigaddrspacetest
30 def test_optimized_concat(self):
Antoine Pitrou99c48302010-11-04 20:48:37 +000031 x = b"x" * (MAX_Py_ssize_t - 128)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032
Antoine Pitrou99c48302010-11-04 20:48:37 +000033 with self.assertRaises(OverflowError) as cm:
34 # this statement uses a fast path in ceval.c
35 x = x + b"x" * 128
36
37 with self.assertRaises(OverflowError) as cm:
38 # this statement uses a fast path in ceval.c
39 x += b"x" * 128
40
41 @bigaddrspacetest
42 def test_repeat(self):
43 x = b"x" * (MAX_Py_ssize_t - 128)
44 self.assertRaises(OverflowError, operator.mul, x, 128)
45
46
47class StrTest(unittest.TestCase):
48
49 unicodesize = 2 if sys.maxunicode < 65536 else 4
50
51 @bigaddrspacetest
52 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)
58
59 @bigaddrspacetest
60 def test_optimized_concat(self):
61 x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
62
63 with self.assertRaises(MemoryError) as cm:
64 # this statement uses a fast path in ceval.c
65 x = x + x
66
67 with self.assertRaises(MemoryError) as cm:
68 # this statement uses a fast path in ceval.c
69 x += x
70
71 @bigaddrspacetest
72 def test_repeat(self):
73 x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
74 self.assertRaises(MemoryError, operator.mul, x, 2)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
76
77def test_main():
Antoine Pitrou99c48302010-11-04 20:48:37 +000078 support.run_unittest(BytesTest, StrTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079
80if __name__ == '__main__':
81 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000082 support.set_memlimit(sys.argv[1])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 test_main()