test_bigaddrspace.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Primarily, this means 32-bit builds with at least 2 GiB of available memory.
  6. You need to pass the -M option to regrtest (e.g. "-M 2.1G") for tests to
  7. be enabled.
  8. """
  9. from test import support
  10. from test.support import bigaddrspacetest, MAX_Py_ssize_t
  11. import unittest
  12. import operator
  13. import sys
  14. class BytesTest(unittest.TestCase):
  15. @bigaddrspacetest
  16. def test_concat(self):
  17. # Allocate a bytestring that's near the maximum size allowed by
  18. # the address space, and then try to build a new, larger one through
  19. # concatenation.
  20. try:
  21. x = b"x" * (MAX_Py_ssize_t - 128)
  22. self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
  23. finally:
  24. x = None
  25. @bigaddrspacetest
  26. def test_optimized_concat(self):
  27. try:
  28. x = b"x" * (MAX_Py_ssize_t - 128)
  29. with self.assertRaises(OverflowError) as cm:
  30. # this statement used a fast path in ceval.c
  31. x = x + b"x" * 128
  32. with self.assertRaises(OverflowError) as cm:
  33. # this statement used a fast path in ceval.c
  34. x += b"x" * 128
  35. finally:
  36. x = None
  37. @bigaddrspacetest
  38. def test_repeat(self):
  39. try:
  40. x = b"x" * (MAX_Py_ssize_t - 128)
  41. self.assertRaises(OverflowError, operator.mul, x, 128)
  42. finally:
  43. x = None
  44. class StrTest(unittest.TestCase):
  45. unicodesize = 4
  46. @bigaddrspacetest
  47. def test_concat(self):
  48. try:
  49. # Create a string that would fill almost the address space
  50. x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
  51. # Unicode objects trigger MemoryError in case an operation that's
  52. # going to cause a size overflow is executed
  53. self.assertRaises(MemoryError, operator.add, x, x)
  54. finally:
  55. x = None
  56. @bigaddrspacetest
  57. def test_optimized_concat(self):
  58. try:
  59. x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
  60. with self.assertRaises(MemoryError) as cm:
  61. # this statement uses a fast path in ceval.c
  62. x = x + x
  63. with self.assertRaises(MemoryError) as cm:
  64. # this statement uses a fast path in ceval.c
  65. x += x
  66. finally:
  67. x = None
  68. @bigaddrspacetest
  69. def test_repeat(self):
  70. try:
  71. x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
  72. self.assertRaises(MemoryError, operator.mul, x, 2)
  73. finally:
  74. x = None
  75. if __name__ == '__main__':
  76. if len(sys.argv) > 1:
  77. support.set_memlimit(sys.argv[1])
  78. unittest.main()