|
21 | 21 |
|
22 | 22 | try:
|
23 | 23 | import posix
|
| 24 | + nt = None |
24 | 25 | except ImportError:
|
25 |
| - import nt as posix |
| 26 | + import nt |
| 27 | + posix = nt |
26 | 28 |
|
27 | 29 | try:
|
28 | 30 | import pwd
|
@@ -935,6 +937,116 @@ def test_utime(self):
|
935 | 937 | posix.utime(os_helper.TESTFN, (int(now), int(now)))
|
936 | 938 | posix.utime(os_helper.TESTFN, (now, now))
|
937 | 939 |
|
| 940 | + def check_chmod(self, chmod_func, target, **kwargs): |
| 941 | + mode = os.stat(target).st_mode |
| 942 | + try: |
| 943 | + chmod_func(target, mode & ~stat.S_IWRITE, **kwargs) |
| 944 | + self.assertEqual(os.stat(target).st_mode, mode & ~stat.S_IWRITE) |
| 945 | + if stat.S_ISREG(mode): |
| 946 | + try: |
| 947 | + with open(target, 'wb+'): |
| 948 | + pass |
| 949 | + except PermissionError: |
| 950 | + pass |
| 951 | + chmod_func(target, mode | stat.S_IWRITE, **kwargs) |
| 952 | + self.assertEqual(os.stat(target).st_mode, mode | stat.S_IWRITE) |
| 953 | + if stat.S_ISREG(mode): |
| 954 | + with open(target, 'wb+'): |
| 955 | + pass |
| 956 | + finally: |
| 957 | + posix.chmod(target, mode) |
| 958 | + |
| 959 | + def test_chmod_file(self): |
| 960 | + self.check_chmod(posix.chmod, os_helper.TESTFN) |
| 961 | + |
| 962 | + def tempdir(self): |
| 963 | + target = os_helper.TESTFN + 'd' |
| 964 | + posix.mkdir(target) |
| 965 | + self.addCleanup(posix.rmdir, target) |
| 966 | + return target |
| 967 | + |
| 968 | + def test_chmod_dir(self): |
| 969 | + target = self.tempdir() |
| 970 | + self.check_chmod(posix.chmod, target) |
| 971 | + |
| 972 | + @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()') |
| 973 | + def test_lchmod_file(self): |
| 974 | + self.check_chmod(posix.lchmod, os_helper.TESTFN) |
| 975 | + self.check_chmod(posix.chmod, os_helper.TESTFN, follow_symlinks=False) |
| 976 | + |
| 977 | + @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()') |
| 978 | + def test_lchmod_dir(self): |
| 979 | + target = self.tempdir() |
| 980 | + self.check_chmod(posix.lchmod, target) |
| 981 | + self.check_chmod(posix.chmod, target, follow_symlinks=False) |
| 982 | + |
| 983 | + def check_chmod_link(self, chmod_func, target, link, **kwargs): |
| 984 | + target_mode = os.stat(target).st_mode |
| 985 | + link_mode = os.lstat(link).st_mode |
| 986 | + try: |
| 987 | + chmod_func(link, target_mode & ~stat.S_IWRITE, **kwargs) |
| 988 | + self.assertEqual(os.stat(target).st_mode, target_mode & ~stat.S_IWRITE) |
| 989 | + self.assertEqual(os.lstat(link).st_mode, link_mode) |
| 990 | + chmod_func(link, target_mode | stat.S_IWRITE) |
| 991 | + self.assertEqual(os.stat(target).st_mode, target_mode | stat.S_IWRITE) |
| 992 | + self.assertEqual(os.lstat(link).st_mode, link_mode) |
| 993 | + finally: |
| 994 | + posix.chmod(target, target_mode) |
| 995 | + |
| 996 | + def check_lchmod_link(self, chmod_func, target, link, **kwargs): |
| 997 | + target_mode = os.stat(target).st_mode |
| 998 | + link_mode = os.lstat(link).st_mode |
| 999 | + chmod_func(link, link_mode & ~stat.S_IWRITE, **kwargs) |
| 1000 | + self.assertEqual(os.stat(target).st_mode, target_mode) |
| 1001 | + self.assertEqual(os.lstat(link).st_mode, link_mode & ~stat.S_IWRITE) |
| 1002 | + chmod_func(link, link_mode | stat.S_IWRITE) |
| 1003 | + self.assertEqual(os.stat(target).st_mode, target_mode) |
| 1004 | + self.assertEqual(os.lstat(link).st_mode, link_mode | stat.S_IWRITE) |
| 1005 | + |
| 1006 | + @os_helper.skip_unless_symlink |
| 1007 | + def test_chmod_file_symlink(self): |
| 1008 | + target = os_helper.TESTFN |
| 1009 | + link = os_helper.TESTFN + '-link' |
| 1010 | + os.symlink(target, link) |
| 1011 | + self.addCleanup(posix.unlink, link) |
| 1012 | + if os.name == 'nt': |
| 1013 | + self.check_lchmod_link(posix.chmod, target, link) |
| 1014 | + else: |
| 1015 | + self.check_chmod_link(posix.chmod, target, link) |
| 1016 | + self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True) |
| 1017 | + |
| 1018 | + @os_helper.skip_unless_symlink |
| 1019 | + def test_chmod_dir_symlink(self): |
| 1020 | + target = self.tempdir() |
| 1021 | + link = os_helper.TESTFN + '-link' |
| 1022 | + os.symlink(target, link, target_is_directory=True) |
| 1023 | + self.addCleanup(posix.unlink, link) |
| 1024 | + if os.name == 'nt': |
| 1025 | + self.check_lchmod_link(posix.chmod, target, link) |
| 1026 | + else: |
| 1027 | + self.check_chmod_link(posix.chmod, target, link) |
| 1028 | + self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True) |
| 1029 | + |
| 1030 | + @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()') |
| 1031 | + @os_helper.skip_unless_symlink |
| 1032 | + def test_lchmod_file_symlink(self): |
| 1033 | + target = os_helper.TESTFN |
| 1034 | + link = os_helper.TESTFN + '-link' |
| 1035 | + os.symlink(target, link) |
| 1036 | + self.addCleanup(posix.unlink, link) |
| 1037 | + self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False) |
| 1038 | + self.check_lchmod_link(posix.lchmod, target, link) |
| 1039 | + |
| 1040 | + @unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()') |
| 1041 | + @os_helper.skip_unless_symlink |
| 1042 | + def test_lchmod_dir_symlink(self): |
| 1043 | + target = self.tempdir() |
| 1044 | + link = os_helper.TESTFN + '-link' |
| 1045 | + os.symlink(target, link) |
| 1046 | + self.addCleanup(posix.unlink, link) |
| 1047 | + self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False) |
| 1048 | + self.check_lchmod_link(posix.lchmod, target, link) |
| 1049 | + |
938 | 1050 | def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
|
939 | 1051 | st = os.stat(target_file)
|
940 | 1052 | self.assertTrue(hasattr(st, 'st_flags'))
|
|
0 commit comments