14adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport unittest
24adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaofrom test import test_support
34adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoimport time
44adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
54adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoresource = test_support.import_module('resource')
64adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
74adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao# This test is checking a few specific problem spots with the resource module.
84adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
94adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoclass ResourceTest(unittest.TestCase):
104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_args(self):
124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.getrlimit)
134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.getrlimit, 42, 42)
144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.setrlimit)
154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_fsize_ismax(self):
184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except AttributeError:
214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # number on a platform with large file support.  On these platforms,
254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # we need to test that the get/setrlimit functions properly convert
264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # the number to a C long long and that the conversion doesn't raise
274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # an error.
284adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            self.assertEqual(resource.RLIM_INFINITY, max)
294adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
304adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
314adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_fsize_enforced(self):
324adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
334adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
344adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except AttributeError:
354adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
364adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
374adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # Check to see what happens when the RLIMIT_FSIZE is small.  Some
384adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # versions of Python were terminated by an uncaught SIGXFSZ, but
394adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # pythonrun.c has been fixed to ignore that exception.  If so, the
404adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # write() should return EFBIG when the limit is exceeded.
414adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
424adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # At least one platform has an unlimited RLIMIT_FSIZE and attempts
434adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            # to change it raise ValueError instead.
444adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
454adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                try:
464adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
474adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    limit_set = True
484adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                except ValueError:
494adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    limit_set = False
504adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                f = open(test_support.TESTFN, "wb")
514adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                try:
524adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    f.write("X" * 1024)
534adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    try:
544adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        f.write("Y")
554adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        f.flush()
564adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # On some systems (e.g., Ubuntu on hppa) the flush()
574adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # doesn't always cause the exception, but the close()
584adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # does eventually.  Try flushing several times in
594adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # an attempt to ensure the file is really synced and
604adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # the exception raised.
614adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        for i in range(5):
624adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            time.sleep(.1)
634adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            f.flush()
644adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    except IOError:
654adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        if not limit_set:
664adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                            raise
674adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if limit_set:
684adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # Close will attempt to flush the byte we wrote
694adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        # Restore limit first to avoid getting a spurious error
704adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
714adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                finally:
724adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    f.close()
734adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            finally:
744adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                if limit_set:
754adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
764adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                test_support.unlink(test_support.TESTFN)
774adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
784adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_fsize_toobig(self):
794adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # Be sure that setrlimit is checking for really large values
804adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        too_big = 10L**50
814adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
824adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
834adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except AttributeError:
844adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
854adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
864adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
874adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
884adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except (OverflowError, ValueError):
894adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                pass
904adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            try:
914adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
924adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            except (OverflowError, ValueError):
934adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                pass
944adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
954adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_getrusage(self):
964adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.getrusage)
974adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        self.assertRaises(TypeError, resource.getrusage, 42, 42)
984adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        usageself = resource.getrusage(resource.RUSAGE_SELF)
994adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
1004adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        # May not be available on all systems.
1014adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
1024adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            usageboth = resource.getrusage(resource.RUSAGE_BOTH)
1034adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except (ValueError, AttributeError):
1044adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
1054adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1064adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    # Issue 6083: Reference counting bug
1074adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    def test_setrusage_refcount(self):
1084adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        try:
1094adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            limits = resource.getrlimit(resource.RLIMIT_CPU)
1104adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        except AttributeError:
1114adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            pass
1124adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao        else:
1134adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            class BadSequence:
1144adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                def __len__(self):
1154adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    return 2
1164adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                def __getitem__(self, key):
1174adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    if key in (0, 1):
1184adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                        return len(tuple(range(1000000)))
1194adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao                    raise IndexError
1204adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1214adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao            resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
1224adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1234adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaodef test_main(verbose=None):
1244adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test_support.run_unittest(ResourceTest)
1254adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao
1264adfde8bc82dd39f59e0445588c3e599ada477dJosh Gaoif __name__ == "__main__":
1274adfde8bc82dd39f59e0445588c3e599ada477dJosh Gao    test_main()
128