Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +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 | 020e09a | 2010-11-04 20:50:00 +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 | 020e09a | 2010-11-04 20:50:00 +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. |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 31 | |
| 32 | @bigaddrspacetest |
| 33 | def test_optimized_concat(self): |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 34 | try: |
| 35 | x = b"x" * (MAX_Py_ssize_t - 128) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 37 | with self.assertRaises(OverflowError) as cm: |
| 38 | # this statement used a fast path in ceval.c |
| 39 | x = x + b"x" * 128 |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 46 | |
| 47 | @bigaddrspacetest |
| 48 | def test_repeat(self): |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 49 | try: |
| 50 | x = b"x" * (MAX_Py_ssize_t - 128) |
| 51 | self.assertRaises(OverflowError, operator.mul, x, 128) |
| 52 | finally: |
| 53 | x = None |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | class StrTest(unittest.TestCase): |
| 57 | |
| 58 | unicodesize = 2 if sys.maxunicode < 65536 else 4 |
| 59 | |
| 60 | @bigaddrspacetest |
| 61 | def test_concat(self): |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 70 | |
| 71 | @bigaddrspacetest |
| 72 | def test_optimized_concat(self): |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 73 | try: |
| 74 | x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize)) |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 76 | with self.assertRaises(MemoryError) as cm: |
| 77 | # this statement uses a fast path in ceval.c |
| 78 | x = x + x |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 85 | |
| 86 | @bigaddrspacetest |
| 87 | def test_repeat(self): |
Antoine Pitrou | 78ae1fb | 2011-01-14 20:19:57 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def test_main(): |
Antoine Pitrou | 020e09a | 2010-11-04 20:50:00 +0000 | [diff] [blame] | 96 | support.run_unittest(BytesTest, StrTest) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 97 | |
| 98 | if __name__ == '__main__': |
| 99 | if len(sys.argv) > 1: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 100 | support.set_memlimit(sys.argv[1]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 101 | test_main() |