blob: c86735a7ed12efee4cf94778d80e83f83b58ba2a [file] [log] [blame]
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00001import os
2import resource
3
Neal Norwitz209ea392008-01-27 20:08:04 +00004from test.test_support import TESTFN, unlink
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00005
6# This test is checking a few specific problem spots. RLIMIT_FSIZE
7# should be RLIM_INFINITY, which will be a really big number on a
8# platform with large file support. On these platforms, we need to
9# test that the get/setrlimit functions properly convert the number to
10# a C long long and that the conversion doesn't raise an error.
11
12try:
13 cur, max = resource.getrlimit(resource.RLIMIT_FSIZE)
14except AttributeError:
15 pass
16else:
17 print resource.RLIM_INFINITY == max
18 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
19
20# Now check to see what happens when the RLIMIT_FSIZE is small. Some
21# versions of Python were terminated by an uncaught SIGXFSZ, but
22# pythonrun.c has been fixed to ignore that exception. If so, the
23# write() should return EFBIG when the limit is exceeded.
24
Jason Tishlere4a070a2002-12-12 18:13:36 +000025# At least one platform has an unlimited RLIMIT_FSIZE and attempts to
26# change it raise ValueError instead.
27
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000028try:
Jason Tishlere4a070a2002-12-12 18:13:36 +000029 try:
30 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
31 limit_set = 1
32 except ValueError:
33 limit_set = 0
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000034 f = open(TESTFN, "wb")
35 f.write("X" * 1024)
Neal Norwitz249cbe72008-01-28 01:33:23 +000036 f.flush()
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000037 try:
38 f.write("Y")
39 f.flush()
Neal Norwitzb03528c2008-01-27 01:23:50 +000040 # On some systems (e.g., Ubuntu on hppa) the flush()
Neal Norwitz61792342008-01-27 05:02:56 +000041 # doesn't always cause the exception, but the close()
Neal Norwitz209ea392008-01-27 20:08:04 +000042 # does eventually. Try flushing several times in an attempt
Neal Norwitz61792342008-01-27 05:02:56 +000043 # to ensure the file is really synced and the exception raised.
44 for i in range(5):
Neal Norwitz209ea392008-01-27 20:08:04 +000045 time.sleep(.1)
46 f.flush()
Neal Norwitz249cbe72008-01-28 01:33:23 +000047 f.close()
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000048 except IOError:
Jason Tishlere4a070a2002-12-12 18:13:36 +000049 if not limit_set:
50 raise
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000051 f.close()
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000052finally:
Neal Norwitz209ea392008-01-27 20:08:04 +000053 if limit_set:
54 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
55 unlink(TESTFN)
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000056
57# And be sure that setrlimit is checking for really large values
58too_big = 10L**50
59try:
60 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
Jason Tishlere4a070a2002-12-12 18:13:36 +000061except (OverflowError, ValueError):
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000062 pass
63try:
64 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
Jason Tishlere4a070a2002-12-12 18:13:36 +000065except (OverflowError, ValueError):
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000066 pass