Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78ae1fb

Browse files
committed
Merged revisions 87968,87971-87975 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r87968 | antoine.pitrou | 2011-01-12 21:46:37 +0100 (mer., 12 janv. 2011) | 4 lines Fix the expected memory use of utf-8 encoding. Also, release the one reference to a huge object even when an exception is raised. ........ r87971 | antoine.pitrou | 2011-01-12 22:19:59 +0100 (mer., 12 janv. 2011) | 3 lines Make test skipping message nicer, and remove the rather useless "overhead" parameter. ........ r87972 | antoine.pitrou | 2011-01-12 22:40:20 +0100 (mer., 12 janv. 2011) | 3 lines Fix @bigmemtest when no limit is given by the user (oops) ........ r87973 | antoine.pitrou | 2011-01-12 22:50:44 +0100 (mer., 12 janv. 2011) | 3 lines More informative skip message in @bigaddrspace ........ r87974 | antoine.pitrou | 2011-01-12 22:58:39 +0100 (mer., 12 janv. 2011) | 3 lines A better message again ........ r87975 | antoine.pitrou | 2011-01-12 23:02:45 +0100 (mer., 12 janv. 2011) | 3 lines Fix test_bigaddrspace (some tests didn't trigger the expected MemoryError) ........
1 parent 39be2a5 commit 78ae1fb

3 files changed

Lines changed: 68 additions & 52 deletions

File tree

Lib/test/support.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,12 @@ def set_memlimit(limit):
784784
raise ValueError('Memory limit %r too low to be useful' % (limit,))
785785
max_memuse = memlimit
786786

787-
def bigmemtest(minsize, memuse, overhead=5*_1M):
787+
def bigmemtest(minsize, memuse):
788788
"""Decorator for bigmem tests.
789789
790790
'minsize' is the minimum useful size for the test (in arbitrary,
791791
test-interpreted units.) 'memuse' is the number of 'bytes per size' for
792-
the test, or a good estimate of it. 'overhead' specifies fixed overhead,
793-
independent of the testsize, and defaults to 5Mb.
792+
the test, or a good estimate of it.
794793
795794
The decorator tries to guess a good value for 'size' and passes it to
796795
the decorated test function. If minsize * memuse is more than the
@@ -802,62 +801,57 @@ def wrapper(self):
802801
# Retrieve values in case someone decided to adjust them
803802
minsize = wrapper.minsize
804803
memuse = wrapper.memuse
805-
overhead = wrapper.overhead
806804
if not max_memuse:
807805
# If max_memuse is 0 (the default),
808806
# we still want to run the tests with size set to a few kb,
809807
# to make sure they work. We still want to avoid using
810808
# too much memory, though, but we do that noisily.
811809
maxsize = 5147
812-
self.assertFalse(maxsize * memuse + overhead > 20 * _1M)
810+
self.assertFalse(maxsize * memuse > 20 * _1M)
813811
else:
814-
maxsize = int((max_memuse - overhead) / memuse)
812+
maxsize = int(max_memuse / memuse)
815813
if maxsize < minsize:
816-
# Really ought to print 'test skipped' or something
817-
if verbose:
818-
sys.stderr.write("Skipping %s because of memory "
819-
"constraint\n" % (f.__name__,))
820-
return
821-
# Try to keep some breathing room in memory use
822-
maxsize = max(maxsize - 50 * _1M, minsize)
814+
raise unittest.SkipTest(
815+
"not enough memory: %.1fG minimum needed"
816+
% (minsize * memuse / (1024 ** 3)))
823817
return f(self, maxsize)
824818
wrapper.minsize = minsize
825819
wrapper.memuse = memuse
826-
wrapper.overhead = overhead
827820
return wrapper
828821
return decorator
829822

830-
def precisionbigmemtest(size, memuse, overhead=5*_1M):
823+
def precisionbigmemtest(size, memuse):
831824
def decorator(f):
832825
def wrapper(self):
833826
size = wrapper.size
834827
memuse = wrapper.memuse
835-
overhead = wrapper.overhead
836828
if not real_max_memuse:
837829
maxsize = 5147
838830
else:
839831
maxsize = size
840832

841833
if real_max_memuse and real_max_memuse < maxsize * memuse:
842-
if verbose:
843-
sys.stderr.write("Skipping %s because of memory "
844-
"constraint\n" % (f.__name__,))
845-
return
834+
raise unittest.SkipTest(
835+
"not enough memory: %.1fG minimum needed"
836+
% (size * memuse / (1024 ** 3)))
846837

847838
return f(self, maxsize)
848839
wrapper.size = size
849840
wrapper.memuse = memuse
850-
wrapper.overhead = overhead
851841
return wrapper
852842
return decorator
853843

854844
def bigaddrspacetest(f):
855845
"""Decorator for tests that fill the address space."""
856846
def wrapper(self):
857847
if max_memuse < MAX_Py_ssize_t:
858-
if verbose:
859-
sys.stderr.write("Skipping %s because of memory "
860-
"constraint\n" % (f.__name__,))
848+
if MAX_Py_ssize_t >= 2**63 - 1 and max_memuse >= 2**31:
849+
raise unittest.SkipTest(
850+
"not enough memory: try a 32-bit build instead")
851+
else:
852+
raise unittest.SkipTest(
853+
"not enough memory: %.1fG minimum needed"
854+
% (MAX_Py_ssize_t / (1024 ** 3)))
861855
else:
862856
return f(self)
863857
return wrapper

Lib/test/test_bigaddrspace.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,34 @@ def test_concat(self):
2323
# Allocate a bytestring that's near the maximum size allowed by
2424
# the address space, and then try to build a new, larger one through
2525
# concatenation.
26-
x = b"x" * (MAX_Py_ssize_t - 128)
27-
self.assertRaises(OverflowError, operator.add, x, b"x" * 128)
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
2831

2932
@bigaddrspacetest
3033
def test_optimized_concat(self):
31-
x = b"x" * (MAX_Py_ssize_t - 128)
34+
try:
35+
x = b"x" * (MAX_Py_ssize_t - 128)
3236

33-
with self.assertRaises(OverflowError) as cm:
34-
# this statement uses a fast path in ceval.c
35-
x = x + b"x" * 128
37+
with self.assertRaises(OverflowError) as cm:
38+
# this statement used a fast path in ceval.c
39+
x = x + b"x" * 128
3640

37-
with self.assertRaises(OverflowError) as cm:
38-
# this statement uses a fast path in ceval.c
39-
x += b"x" * 128
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
4046

4147
@bigaddrspacetest
4248
def test_repeat(self):
43-
x = b"x" * (MAX_Py_ssize_t - 128)
44-
self.assertRaises(OverflowError, operator.mul, x, 128)
49+
try:
50+
x = b"x" * (MAX_Py_ssize_t - 128)
51+
self.assertRaises(OverflowError, operator.mul, x, 128)
52+
finally:
53+
x = None
4554

4655

4756
class StrTest(unittest.TestCase):
@@ -50,28 +59,37 @@ class StrTest(unittest.TestCase):
5059

5160
@bigaddrspacetest
5261
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)
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
5870

5971
@bigaddrspacetest
6072
def test_optimized_concat(self):
61-
x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
73+
try:
74+
x = "x" * int(MAX_Py_ssize_t // (1.1 * self.unicodesize))
6275

63-
with self.assertRaises(MemoryError) as cm:
64-
# this statement uses a fast path in ceval.c
65-
x = x + x
76+
with self.assertRaises(MemoryError) as cm:
77+
# this statement uses a fast path in ceval.c
78+
x = x + x
6679

67-
with self.assertRaises(MemoryError) as cm:
68-
# this statement uses a fast path in ceval.c
69-
x += x
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
7085

7186
@bigaddrspacetest
7287
def test_repeat(self):
73-
x = "x" * (MAX_Py_ssize_t // (2 * self.unicodesize))
74-
self.assertRaises(MemoryError, operator.mul, x, 2)
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
7593

7694

7795
def test_main():

Lib/test/test_bigmem.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,11 @@ def basic_encode_test(self, size, enc, c='.', expectedsize=None):
564564
if expectedsize is None:
565565
expectedsize = size
566566

567-
s = c * size
568-
self.assertEqual(len(s.encode(enc)), expectedsize)
567+
try:
568+
s = c * size
569+
self.assertEqual(len(s.encode(enc)), expectedsize)
570+
finally:
571+
s = None
569572

570573
def setUp(self):
571574
# HACK: adjust memory use of tests inherited from BaseStrTest
@@ -586,7 +589,8 @@ def tearDown(self):
586589
for name, memuse in self._adjusted.items():
587590
getattr(type(self), name).memuse = memuse
588591

589-
@bigmemtest(minsize=_2G + 2, memuse=character_size + 1)
592+
# the utf8 encoder preallocates big time (4x the number of characters)
593+
@bigmemtest(minsize=_2G + 2, memuse=character_size + 4)
590594
def test_encode(self, size):
591595
return self.basic_encode_test(size, 'utf-8')
592596

0 commit comments

Comments
 (0)