Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 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 GB 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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
| 12 | from test.support import bigaddrspacetest, MAX_Py_ssize_t |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | |
| 14 | import unittest |
| 15 | import operator |
| 16 | import sys |
| 17 | |
| 18 | |
Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 19 | class BytesTest(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | |
| 21 | @bigaddrspacetest |
| 22 | def test_concat(self): |
Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 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 | x = b"x" * (MAX_Py_ssize_t - 128) |
| 27 | self.assertRaises(OverflowError, operator.add, x, b"x" * 128) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | |
| 29 | @bigaddrspacetest |
| 30 | def test_optimized_concat(self): |
Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 31 | x = b"x" * (MAX_Py_ssize_t - 128) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 33 | 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 | |
| 47 | class 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def test_main(): |
Antoine Pitrou | 99c4830 | 2010-11-04 20:48:37 +0000 | [diff] [blame] | 78 | support.run_unittest(BytesTest, StrTest) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | |
| 80 | if __name__ == '__main__': |
| 81 | if len(sys.argv) > 1: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 82 | support.set_memlimit(sys.argv[1]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 83 | test_main() |