OLD | NEW |
1 # As a test suite for the os module, this is woefully inadequate, but this | 1 # As a test suite for the os module, this is woefully inadequate, but this |
2 # does add tests for a few functions which have been determined to be more | 2 # does add tests for a few functions which have been determined to be more |
3 # portable than they had been thought to be. | 3 # portable than they had been thought to be. |
4 | 4 |
5 import asynchat | 5 import asynchat |
6 import asyncore | 6 import asyncore |
7 import codecs | 7 import codecs |
8 import contextlib | 8 import contextlib |
9 import decimal | 9 import decimal |
10 import errno | 10 import errno |
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 | 649 |
650 def _reference(self): | 650 def _reference(self): |
651 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} | 651 return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"} |
652 | 652 |
653 def _empty_mapping(self): | 653 def _empty_mapping(self): |
654 os.environ.clear() | 654 os.environ.clear() |
655 return os.environ | 655 return os.environ |
656 | 656 |
657 # Bug 1110478 | 657 # Bug 1110478 |
658 @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') | 658 @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') |
| 659 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
659 def test_update2(self): | 660 def test_update2(self): |
660 os.environ.clear() | 661 os.environ.clear() |
661 os.environ.update(HELLO="World") | 662 os.environ.update(HELLO="World") |
662 with os.popen("/bin/sh -c 'echo $HELLO'") as popen: | 663 with os.popen("/bin/sh -c 'echo $HELLO'") as popen: |
663 value = popen.read().strip() | 664 value = popen.read().strip() |
664 self.assertEqual(value, "World") | 665 self.assertEqual(value, "World") |
665 | 666 |
666 @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') | 667 @unittest.skipUnless(os.path.exists('/bin/sh'), 'requires /bin/sh') |
| 668 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
667 def test_os_popen_iter(self): | 669 def test_os_popen_iter(self): |
668 with os.popen( | 670 with os.popen( |
669 "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: | 671 "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: |
670 it = iter(popen) | 672 it = iter(popen) |
671 self.assertEqual(next(it), "line1\n") | 673 self.assertEqual(next(it), "line1\n") |
672 self.assertEqual(next(it), "line2\n") | 674 self.assertEqual(next(it), "line2\n") |
673 self.assertEqual(next(it), "line3\n") | 675 self.assertEqual(next(it), "line3\n") |
674 self.assertRaises(StopIteration, next, it) | 676 self.assertRaises(StopIteration, next, it) |
675 | 677 |
676 # Verify environ keys and values from the OS are of the | 678 # Verify environ keys and values from the OS are of the |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1358 os.execv = mock_execv | 1360 os.execv = mock_execv |
1359 os.execve = mock_execve | 1361 os.execve = mock_execve |
1360 if defpath is not None: | 1362 if defpath is not None: |
1361 os.defpath = defpath | 1363 os.defpath = defpath |
1362 yield calls | 1364 yield calls |
1363 finally: | 1365 finally: |
1364 os.execv = orig_execv | 1366 os.execv = orig_execv |
1365 os.execve = orig_execve | 1367 os.execve = orig_execve |
1366 os.defpath = orig_defpath | 1368 os.defpath = orig_defpath |
1367 | 1369 |
| 1370 @unittest.skipUnless(hasattr(os, 'execv'), "os module doesn't provide execvpe()"
) |
1368 class ExecTests(unittest.TestCase): | 1371 class ExecTests(unittest.TestCase): |
1369 @unittest.skipIf(USING_LINUXTHREADS, | 1372 @unittest.skipIf(USING_LINUXTHREADS, |
1370 "avoid triggering a linuxthreads bug: see issue #4970") | 1373 "avoid triggering a linuxthreads bug: see issue #4970") |
1371 def test_execvpe_with_bad_program(self): | 1374 def test_execvpe_with_bad_program(self): |
1372 self.assertRaises(OSError, os.execvpe, 'no such app-', | 1375 self.assertRaises(OSError, os.execvpe, 'no such app-', |
1373 ['no such app-'], None) | 1376 ['no such app-'], None) |
1374 | 1377 |
1375 def test_execvpe_with_bad_arglist(self): | 1378 def test_execvpe_with_bad_arglist(self): |
1376 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) | 1379 self.assertRaises(ValueError, os.execvpe, 'notepad', [], None) |
1377 | 1380 |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1619 self.assertRaises(OverflowError, os.setegid, 1<<32) | 1622 self.assertRaises(OverflowError, os.setegid, 1<<32) |
1620 | 1623 |
1621 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') | 1624 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') |
1622 def test_setreuid(self): | 1625 def test_setreuid(self): |
1623 if os.getuid() != 0: | 1626 if os.getuid() != 0: |
1624 self.assertRaises(OSError, os.setreuid, 0, 0) | 1627 self.assertRaises(OSError, os.setreuid, 0, 0) |
1625 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) | 1628 self.assertRaises(OverflowError, os.setreuid, 1<<32, 0) |
1626 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) | 1629 self.assertRaises(OverflowError, os.setreuid, 0, 1<<32) |
1627 | 1630 |
1628 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') | 1631 @unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()') |
| 1632 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
1629 def test_setreuid_neg1(self): | 1633 def test_setreuid_neg1(self): |
1630 # Needs to accept -1. We run this in a subprocess to avoid | 1634 # Needs to accept -1. We run this in a subprocess to avoid |
1631 # altering the test runner's process state (issue8045). | 1635 # altering the test runner's process state (issue8045). |
1632 subprocess.check_call([ | 1636 subprocess.check_call([ |
1633 sys.executable, '-c', | 1637 sys.executable, '-c', |
1634 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) | 1638 'import os,sys;os.setreuid(-1,-1);sys.exit(0)']) |
1635 | 1639 |
1636 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') | 1640 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') |
| 1641 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
1637 def test_setregid(self): | 1642 def test_setregid(self): |
1638 if os.getuid() != 0 and not HAVE_WHEEL_GROUP: | 1643 if os.getuid() != 0 and not HAVE_WHEEL_GROUP: |
1639 self.assertRaises(OSError, os.setregid, 0, 0) | 1644 self.assertRaises(OSError, os.setregid, 0, 0) |
1640 self.assertRaises(OverflowError, os.setregid, 1<<32, 0) | 1645 self.assertRaises(OverflowError, os.setregid, 1<<32, 0) |
1641 self.assertRaises(OverflowError, os.setregid, 0, 1<<32) | 1646 self.assertRaises(OverflowError, os.setregid, 0, 1<<32) |
1642 | 1647 |
1643 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') | 1648 @unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()') |
| 1649 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
1644 def test_setregid_neg1(self): | 1650 def test_setregid_neg1(self): |
1645 # Needs to accept -1. We run this in a subprocess to avoid | 1651 # Needs to accept -1. We run this in a subprocess to avoid |
1646 # altering the test runner's process state (issue8045). | 1652 # altering the test runner's process state (issue8045). |
1647 subprocess.check_call([ | 1653 subprocess.check_call([ |
1648 sys.executable, '-c', | 1654 sys.executable, '-c', |
1649 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) | 1655 'import os,sys;os.setregid(-1,-1);sys.exit(0)']) |
1650 | 1656 |
1651 @unittest.skipIf(sys.platform == "win32", "Posix specific tests") | 1657 @unittest.skipIf(sys.platform == "win32", "Posix specific tests") |
1652 class Pep383Tests(unittest.TestCase): | 1658 class Pep383Tests(unittest.TestCase): |
1653 def setUp(self): | 1659 def setUp(self): |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2090 (hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET'))), | 2096 (hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET'))), |
2091 'test requires a tty and either Windows or nl_langinfo(CODESET)') | 2097 'test requires a tty and either Windows or nl_langinfo(CODESET)') |
2092 def test_device_encoding(self): | 2098 def test_device_encoding(self): |
2093 encoding = os.device_encoding(0) | 2099 encoding = os.device_encoding(0) |
2094 self.assertIsNotNone(encoding) | 2100 self.assertIsNotNone(encoding) |
2095 self.assertTrue(codecs.lookup(encoding)) | 2101 self.assertTrue(codecs.lookup(encoding)) |
2096 | 2102 |
2097 | 2103 |
2098 class PidTests(unittest.TestCase): | 2104 class PidTests(unittest.TestCase): |
2099 @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid") | 2105 @unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid") |
| 2106 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
2100 def test_getppid(self): | 2107 def test_getppid(self): |
2101 p = subprocess.Popen([sys.executable, '-c', | 2108 p = subprocess.Popen([sys.executable, '-c', |
2102 'import os; print(os.getppid())'], | 2109 'import os; print(os.getppid())'], |
2103 stdout=subprocess.PIPE) | 2110 stdout=subprocess.PIPE) |
2104 stdout, _ = p.communicate() | 2111 stdout, _ = p.communicate() |
2105 # We are the parent of our subprocess | 2112 # We are the parent of our subprocess |
2106 self.assertEqual(int(stdout), os.getpid()) | 2113 self.assertEqual(int(stdout), os.getpid()) |
2107 | 2114 |
2108 def test_waitpid(self): | 2115 def test_waitpid(self): |
2109 args = [sys.executable, '-c', 'pass'] | 2116 args = [sys.executable, '-c', 'pass'] |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2563 except OSError as e: | 2570 except OSError as e: |
2564 if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY
): | 2571 if sys.platform == "win32" or e.errno in (errno.EINVAL, errno.ENOTTY
): |
2565 # Under win32 a generic OSError can be thrown if the | 2572 # Under win32 a generic OSError can be thrown if the |
2566 # handle cannot be retrieved | 2573 # handle cannot be retrieved |
2567 self.skipTest("failed to query terminal size") | 2574 self.skipTest("failed to query terminal size") |
2568 raise | 2575 raise |
2569 | 2576 |
2570 self.assertGreaterEqual(size.columns, 0) | 2577 self.assertGreaterEqual(size.columns, 0) |
2571 self.assertGreaterEqual(size.lines, 0) | 2578 self.assertGreaterEqual(size.lines, 0) |
2572 | 2579 |
| 2580 @unittest.skipUnless(hasattr(subprocess, 'Popen'), "test requires subprocess
.Popen()") |
2573 def test_stty_match(self): | 2581 def test_stty_match(self): |
2574 """Check if stty returns the same results | 2582 """Check if stty returns the same results |
2575 | 2583 |
2576 stty actually tests stdin, so get_terminal_size is invoked on | 2584 stty actually tests stdin, so get_terminal_size is invoked on |
2577 stdin explicitly. If stty succeeded, then get_terminal_size() | 2585 stdin explicitly. If stty succeeded, then get_terminal_size() |
2578 should work too. | 2586 should work too. |
2579 """ | 2587 """ |
2580 try: | 2588 try: |
2581 size = subprocess.check_output(['stty', 'size']).decode().split() | 2589 size = subprocess.check_output(['stty', 'size']).decode().split() |
2582 except (FileNotFoundError, subprocess.CalledProcessError): | 2590 except (FileNotFoundError, subprocess.CalledProcessError): |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3078 support.gc_collect() | 3086 support.gc_collect() |
3079 # exhausted iterator | 3087 # exhausted iterator |
3080 iterator = os.scandir(self.path) | 3088 iterator = os.scandir(self.path) |
3081 list(iterator) | 3089 list(iterator) |
3082 with self.check_no_resource_warning(): | 3090 with self.check_no_resource_warning(): |
3083 del iterator | 3091 del iterator |
3084 | 3092 |
3085 | 3093 |
3086 if __name__ == "__main__": | 3094 if __name__ == "__main__": |
3087 unittest.main() | 3095 unittest.main() |
OLD | NEW |