diff -r fc882d42cd21 -r 7df15564bf86 Lib/test/test_bigaddrspace.py --- a/Lib/test/test_bigaddrspace.py Thu Nov 04 18:06:58 2010 +0100 +++ b/Lib/test/test_bigaddrspace.py Thu Nov 04 19:35:42 2010 +0100 @@ -6,39 +6,66 @@ import sys -class StrTest(unittest.TestCase): +class ByteTest(unittest.TestCase): @bigaddrspacetest def test_concat(self): - s1 = 'x' * MAX_Py_ssize_t - self.assertRaises(OverflowError, operator.add, s1, '?') + # this is a "dirty trick" - since just multiplying b"x" with + # MAX_Py_ssize_t generates an OverflowError before actually + # testing the operator.add, we multiply b"x" for a value 'a bit + # less than MAX_Py_ssize_t, and then readd the missing 'bit' later + s1 = b"x" * (MAX_Py_ssize_t - 128) + self.assertRaises(OverflowError, operator.add, s1, b"x" * 128) @bigaddrspacetest def test_optimized_concat(self): - x = 'x' * MAX_Py_ssize_t - try: - x = x + '?' # this statement uses a fast path in ceval.c - except OverflowError: - pass - else: - self.fail("should have raised OverflowError") - try: - x += '?' # this statement uses a fast path in ceval.c - except OverflowError: - pass - else: - self.fail("should have raised OverflowError") - self.assertEquals(len(x), MAX_Py_ssize_t) + x = b"x" * (MAX_Py_ssize_t - 128) - ### the following test is pending a patch - # (http://mail.python.org/pipermail/python-dev/2006-July/067774.html) - #@bigaddrspacetest - #def test_repeat(self): - # self.assertRaises(OverflowError, operator.mul, 'x', MAX_Py_ssize_t + 1) + with self.assertRaises(OverflowError) as cm: + x = x + b"x" * 128 # this statement uses a fast path in ceval.c + + with self.assertRaises(OverflowError) as cm: + x += b"x" * 128 # this statement uses a fast path in ceval.c + + @bigaddrspacetest + def test_repeat(self): + s1 = b"x" * (MAX_Py_ssize_t - 128) + self.assertRaises(OverflowError, operator.mul, s1, 128) + + +class StrTest(unittest.TestCase): + + # we need to know if we're using UCS-2 or UCS-4, so the + # length of unicode sequences + unicodesize = 2 if sys.maxunicode < 65536 else 4 + + @bigaddrspacetest + def test_concat(self): + # we create a string, the half the size of the string that + # would fill the memory + s1 = "x" * (MAX_Py_ssize_t//(2*self.unicodesize)) + # Unicode objects trigger MemoryError in case an operation that's + # going to cause an overflow is executed + self.assertRaises(MemoryError, operator.add, s1, s1) + + @bigaddrspacetest + def test_optimized_concat(self): + x = "x" * (MAX_Py_ssize_t//(2*self.unicodesize)) + + with self.assertRaises(MemoryError) as cm: + x = x + x # this statement uses a fast path in ceval.c + + with self.assertRaises(MemoryError) as cm: + x += x # this statement uses a fast path in ceval.c + + @bigaddrspacetest + def test_repeat(self): + s1 = "x" * (MAX_Py_ssize_t//(2*self.unicodesize)) + self.assertRaises(MemoryError, operator.mul, s1, 2) def test_main(): - support.run_unittest(StrTest) + support.run_unittest(ByteTest, StrTest) if __name__ == '__main__': if len(sys.argv) > 1: