test_resource.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import unittest
  2. from test import test_support
  3. import time
  4. resource = test_support.import_module('resource')
  5. # This test is checking a few specific problem spots with the resource module.
  6. class ResourceTest(unittest.TestCase):
  7. def test_args(self):
  8. self.assertRaises(TypeError, resource.getrlimit)
  9. self.assertRaises(TypeError, resource.getrlimit, 42, 42)
  10. self.assertRaises(TypeError, resource.setrlimit)
  11. self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
  12. def test_fsize_ismax(self):
  13. try:
  14. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  15. except AttributeError:
  16. self.skipTest('RLIMIT_FSIZE not available')
  17. # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
  18. # number on a platform with large file support. On these platforms,
  19. # we need to test that the get/setrlimit functions properly convert
  20. # the number to a C long long and that the conversion doesn't raise
  21. # an error.
  22. self.assertEqual(resource.RLIM_INFINITY, max)
  23. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  24. def test_fsize_enforced(self):
  25. try:
  26. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  27. except AttributeError:
  28. self.skipTest('RLIMIT_FSIZE not available')
  29. # Check to see what happens when the RLIMIT_FSIZE is small. Some
  30. # versions of Python were terminated by an uncaught SIGXFSZ, but
  31. # pythonrun.c has been fixed to ignore that exception. If so, the
  32. # write() should return EFBIG when the limit is exceeded.
  33. # At least one platform has an unlimited RLIMIT_FSIZE and attempts
  34. # to change it raise ValueError instead.
  35. try:
  36. try:
  37. resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
  38. limit_set = True
  39. except ValueError:
  40. limit_set = False
  41. f = open(test_support.TESTFN, "wb")
  42. try:
  43. f.write("X" * 1024)
  44. try:
  45. f.write("Y")
  46. f.flush()
  47. # On some systems (e.g., Ubuntu on hppa) the flush()
  48. # doesn't always cause the exception, but the close()
  49. # does eventually. Try flushing several times in
  50. # an attempt to ensure the file is really synced and
  51. # the exception raised.
  52. for i in range(5):
  53. time.sleep(.1)
  54. f.flush()
  55. except IOError:
  56. if not limit_set:
  57. raise
  58. if limit_set:
  59. # Close will attempt to flush the byte we wrote
  60. # Restore limit first to avoid getting a spurious error
  61. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  62. finally:
  63. f.close()
  64. finally:
  65. if limit_set:
  66. resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
  67. test_support.unlink(test_support.TESTFN)
  68. def test_fsize_toobig(self):
  69. # Be sure that setrlimit is checking for really large values
  70. too_big = 10L**50
  71. try:
  72. (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
  73. except AttributeError:
  74. self.skipTest('RLIMIT_FSIZE not available')
  75. try:
  76. resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
  77. except (OverflowError, ValueError):
  78. pass
  79. try:
  80. resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
  81. except (OverflowError, ValueError):
  82. pass
  83. def test_getrusage(self):
  84. self.assertRaises(TypeError, resource.getrusage)
  85. self.assertRaises(TypeError, resource.getrusage, 42, 42)
  86. usageself = resource.getrusage(resource.RUSAGE_SELF)
  87. usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
  88. # May not be available on all systems.
  89. try:
  90. usageboth = resource.getrusage(resource.RUSAGE_BOTH)
  91. except (ValueError, AttributeError):
  92. pass
  93. # Issue 6083: Reference counting bug
  94. def test_setrusage_refcount(self):
  95. try:
  96. limits = resource.getrlimit(resource.RLIMIT_CPU)
  97. except AttributeError:
  98. self.skipTest('RLIMIT_CPU not available')
  99. class BadSequence:
  100. def __len__(self):
  101. return 2
  102. def __getitem__(self, key):
  103. if key in (0, 1):
  104. return len(tuple(range(1000000)))
  105. raise IndexError
  106. resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
  107. def test_main(verbose=None):
  108. test_support.run_unittest(ResourceTest)
  109. if __name__ == "__main__":
  110. test_main()