| 1 | n/a | """ |
|---|
| 2 | n/a | These tests are meant to exercise that requests to create objects bigger |
|---|
| 3 | n/a | than what the address space allows are properly met with an OverflowError |
|---|
| 4 | n/a | (rather than crash weirdly). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Primarily, this means 32-bit builds with at least 2 GB of available memory. |
|---|
| 7 | n/a | You need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to |
|---|
| 8 | n/a | be enabled. |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | from test import support |
|---|
| 12 | n/a | from test.support import bigaddrspacetest, MAX_Py_ssize_t |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | import unittest |
|---|
| 15 | n/a | import operator |
|---|
| 16 | n/a | import sys |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | class BytesTest(unittest.TestCase): |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | @bigaddrspacetest |
|---|
| 22 | n/a | def test_concat(self): |
|---|
| 23 | n/a | # Allocate a bytestring that's near the maximum size allowed by |
|---|
| 24 | n/a | # the address space, and then try to build a new, larger one through |
|---|
| 25 | n/a | # concatenation. |
|---|
| 26 | n/a | try: |
|---|
| 27 | n/a | x = b"x" * (MAX_Py_ssize_t - 128) |
|---|
| 28 | n/a | self.assertRaises(OverflowError, operator.add, x, b"x" * 128) |
|---|
| 29 | n/a | finally: |
|---|
| 30 | n/a | x = None |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | @bigaddrspacetest |
|---|
| 33 | n/a | def test_optimized_concat(self): |
|---|
| 34 | n/a | try: |
|---|
| 35 | n/a | x = b"x" * (MAX_Py_ssize_t - 128) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | with self.assertRaises(OverflowError) as cm: |
|---|
| 38 | n/a | # this statement used a fast path in ceval.c |
|---|
| 39 | n/a | x = x + b"x" * 128 |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | with self.assertRaises(OverflowError) as cm: |
|---|
| 42 | n/a | # this statement used a fast path in ceval.c |
|---|
| 43 | n/a | x += b"x" * 128 |
|---|
| 44 | n/a | finally: |
|---|
| 45 | n/a | x = None |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | @bigaddrspacetest |
|---|
| 48 | n/a | def test_repeat(self): |
|---|
| 49 | n/a | try: |
|---|
| 50 | n/a | x = b"x" * (MAX_Py_ssize_t - 128) |
|---|
| 51 | n/a | self.assertRaises(OverflowError, operator.mul, x, 128) |
|---|
| 52 | n/a | finally: |
|---|
| 53 | n/a | x = None |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | class StrTest(unittest.TestCase): |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | unicodesize = 2 if sys.maxunicode < 65536 else 4 |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | @bigaddrspacetest |
|---|
| 61 | n/a | def test_concat(self): |
|---|
| 62 | n/a | try: |
|---|
| 63 | n/a | # Create a string that would fill almost the address space |
|---|
| 64 | n/a | x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize)) |
|---|
| 65 | n/a | # Unicode objects trigger MemoryError in case an operation that's |
|---|
| 66 | n/a | # going to cause a size overflow is executed |
|---|
| 67 | n/a | self.assertRaises(MemoryError, operator.add, x, x) |
|---|
| 68 | n/a | finally: |
|---|
| 69 | n/a | x = None |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | @bigaddrspacetest |
|---|
| 72 | n/a | def test_optimized_concat(self): |
|---|
| 73 | n/a | try: |
|---|
| 74 | n/a | x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize)) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | with self.assertRaises(MemoryError) as cm: |
|---|
| 77 | n/a | # this statement uses a fast path in ceval.c |
|---|
| 78 | n/a | x = x + x |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | with self.assertRaises(MemoryError) as cm: |
|---|
| 81 | n/a | # this statement uses a fast path in ceval.c |
|---|
| 82 | n/a | x += x |
|---|
| 83 | n/a | finally: |
|---|
| 84 | n/a | x = None |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | @bigaddrspacetest |
|---|
| 87 | n/a | def test_repeat(self): |
|---|
| 88 | n/a | try: |
|---|
| 89 | n/a | x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize)) |
|---|
| 90 | n/a | self.assertRaises(MemoryError, operator.mul, x, 2) |
|---|
| 91 | n/a | finally: |
|---|
| 92 | n/a | x = None |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_main(): |
|---|
| 96 | n/a | support.run_unittest(BytesTest, StrTest) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | if __name__ == '__main__': |
|---|
| 99 | n/a | if len(sys.argv) > 1: |
|---|
| 100 | n/a | support.set_memlimit(sys.argv[1]) |
|---|
| 101 | n/a | test_main() |
|---|