|
| 1 | +from test.support import os_helper |
1 | 2 | from test.test_importlib import util
|
2 | 3 |
|
3 | 4 | importlib = util.import_importlib('importlib')
|
@@ -153,6 +154,28 @@ def test_deleted_cwd(self):
|
153 | 154 | # Do not want FileNotFoundError raised.
|
154 | 155 | self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
|
155 | 156 |
|
| 157 | + @os_helper.skip_unless_working_chmod |
| 158 | + def test_permission_error_cwd(self): |
| 159 | + # gh-115911: Test that an unreadable CWD does not break imports, in |
| 160 | + # particular during early stages of interpreter startup. |
| 161 | + with ( |
| 162 | + os_helper.temp_dir() as new_dir, |
| 163 | + os_helper.save_mode(new_dir), |
| 164 | + os_helper.change_cwd(new_dir), |
| 165 | + util.import_state(path=['']), |
| 166 | + ): |
| 167 | + # chmod() is done here (inside the 'with' block) because the order |
| 168 | + # of teardown operations cannot be the reverse of setup order. See |
| 169 | + # https://github.com/python/cpython/pull/116131#discussion_r1739649390 |
| 170 | + try: |
| 171 | + os.chmod(new_dir, 0o000) |
| 172 | + except OSError: |
| 173 | + self.skipTest("platform does not allow " |
| 174 | + "changing mode of the cwd") |
| 175 | + |
| 176 | + # Do not want PermissionError raised. |
| 177 | + self.assertIsNone(self.machinery.PathFinder.find_spec('whatever')) |
| 178 | + |
156 | 179 | def test_invalidate_caches_finders(self):
|
157 | 180 | # Finders with an invalidate_caches() method have it called.
|
158 | 181 | class FakeFinder:
|
|
0 commit comments