Commit 4d6e0d2f authored by Walter Dörwald's avatar Walter Dörwald

Port test_resource.py to unittest.

parent 5224ee14
import os import unittest
import resource from test import test_support
from test.test_support import TESTFN
import os, resource
# This test is checking a few specific problem spots. RLIMIT_FSIZE
# should be RLIM_INFINITY, which will be a really big number on a # This test is checking a few specific problem spots with the resource module.
# platform with large file support. On these platforms, we need to
# test that the get/setrlimit functions properly convert the number to class ResourceTest(unittest.TestCase):
# a C long long and that the conversion doesn't raise an error. def test_fsize_ismax(self):
try: try:
cur, max = resource.getrlimit(resource.RLIMIT_FSIZE) (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
except AttributeError: except AttributeError:
pass pass
else: else:
print resource.RLIM_INFINITY == max # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) # number on a platform with large file support. On these platforms,
# we need to test that the get/setrlimit functions properly convert
# Now check to see what happens when the RLIMIT_FSIZE is small. Some # the number to a C long long and that the conversion doesn't raise
# versions of Python were terminated by an uncaught SIGXFSZ, but # an error.
# pythonrun.c has been fixed to ignore that exception. If so, the self.assertEqual(resource.RLIM_INFINITY, max)
# write() should return EFBIG when the limit is exceeded. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
# At least one platform has an unlimited RLIMIT_FSIZE and attempts to def test_fsize_enforced(self):
# change it raise ValueError instead. try:
(cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
try: except AttributeError:
try: pass
resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) else:
limit_set = 1 # Check to see what happens when the RLIMIT_FSIZE is small. Some
except ValueError: # versions of Python were terminated by an uncaught SIGXFSZ, but
limit_set = 0 # pythonrun.c has been fixed to ignore that exception. If so, the
f = open(TESTFN, "wb") # write() should return EFBIG when the limit is exceeded.
f.write("X" * 1024)
try: # At least one platform has an unlimited RLIMIT_FSIZE and attempts
f.write("Y") # to change it raise ValueError instead.
f.flush() try:
except IOError: try:
if not limit_set: resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
raise limit_set = True
f.close() except ValueError:
os.unlink(TESTFN) limit_set = False
finally: f = open(test_support.TESTFN, "wb")
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) f.write("X" * 1024)
try:
# And be sure that setrlimit is checking for really large values f.write("Y")
too_big = 10L**50 f.flush()
try: except IOError:
resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) if not limit_set:
except (OverflowError, ValueError): raise
pass f.close()
try: os.unlink(test_support.TESTFN)
resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) finally:
except (OverflowError, ValueError): resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
pass
def test_fsize_toobig(self):
# Be sure that setrlimit is checking for really large values
too_big = 10L**50
try:
(cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
except AttributeError:
pass
else:
try:
resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
except (OverflowError, ValueError):
pass
try:
resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
except (OverflowError, ValueError):
pass
def test_main(verbose=None):
test_support.run_unittest(ResourceTest)
if __name__ == "__main__":
test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment