10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport time
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoresource = test_support.import_module('resource')
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This test is checking a few specific problem spots with the resource module.
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ResourceTest(unittest.TestCase):
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_args(self):
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.getrlimit)
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.getrlimit, 42, 42)
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.setrlimit)
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_fsize_ismax(self):
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # number on a platform with large file support.  On these platforms,
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # we need to test that the get/setrlimit functions properly convert
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # the number to a C long long and that the conversion doesn't raise
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # an error.
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(resource.RLIM_INFINITY, max)
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_fsize_enforced(self):
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Check to see what happens when the RLIMIT_FSIZE is small.  Some
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # versions of Python were terminated by an uncaught SIGXFSZ, but
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # pythonrun.c has been fixed to ignore that exception.  If so, the
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # write() should return EFBIG when the limit is exceeded.
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # At least one platform has an unlimited RLIMIT_FSIZE and attempts
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # to change it raise ValueError instead.
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    limit_set = True
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except ValueError:
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    limit_set = False
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                f = open(test_support.TESTFN, "wb")
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    f.write("X" * 1024)
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    try:
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        f.write("Y")
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        f.flush()
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # On some systems (e.g., Ubuntu on hppa) the flush()
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # doesn't always cause the exception, but the close()
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # does eventually.  Try flushing several times in
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # an attempt to ensure the file is really synced and
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # the exception raised.
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        for i in range(5):
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            time.sleep(.1)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            f.flush()
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    except IOError:
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        if not limit_set:
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            raise
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if limit_set:
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # Close will attempt to flush the byte we wrote
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # Restore limit first to avoid getting a spurious error
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                finally:
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    f.close()
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if limit_set:
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                test_support.unlink(test_support.TESTFN)
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_fsize_toobig(self):
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Be sure that setrlimit is checking for really large values
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        too_big = 10L**50
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except (OverflowError, ValueError):
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except (OverflowError, ValueError):
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_getrusage(self):
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.getrusage)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, resource.getrusage, 42, 42)
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        usageself = resource.getrusage(resource.RUSAGE_SELF)
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # May not be available on all systems.
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            usageboth = resource.getrusage(resource.RUSAGE_BOTH)
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except (ValueError, AttributeError):
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Issue 6083: Reference counting bug
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_setrusage_refcount(self):
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            limits = resource.getrlimit(resource.RLIMIT_CPU)
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class BadSequence:
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                def __len__(self):
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return 2
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                def __getitem__(self, key):
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if key in (0, 1):
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return len(tuple(range(1000000)))
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise IndexError
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(verbose=None):
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_support.run_unittest(ResourceTest)
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
128