Skip to content

Commit ac17d9e

Browse files
Fix the test for read-only tempdir. Reduce the number of attempts even more.
1 parent 5753c73 commit ac17d9e

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
if hasattr(_os, 'O_BINARY'):
5858
_bin_openflags |= _os.O_BINARY
5959

60-
TMP_MAX = 100
60+
TMP_MAX = 20
6161

6262
# This variable _was_ unused for legacy reasons, see issue 10354.
6363
# But as of 3.5 we actually use it at runtime so changing it would

Lib/test/test_tempfile.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,36 @@ def _mock_candidate_names(*names):
330330
class TestBadTempdir:
331331
def test_read_only_directory(self):
332332
with _inside_empty_temp_dir():
333-
oldmode = mode = os.stat(tempfile.tempdir).st_mode
334-
mode &= ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
335-
os.chmod(tempfile.tempdir, mode)
333+
probe = os.path.join(tempfile.tempdir, 'probe')
334+
if os.name == 'nt':
335+
cmd = ['icacls', tempfile.tempdir, '/deny', 'Everyone:(W)']
336+
stdout = None if support.verbose > 1 else subprocess.DEVNULL
337+
subprocess.run(cmd, check=True, stdout=stdout)
338+
else:
339+
oldmode = mode = os.stat(tempfile.tempdir).st_mode
340+
mode &= ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
341+
mode = stat.S_IREAD
342+
os.chmod(tempfile.tempdir, mode)
336343
try:
337-
if os.access(tempfile.tempdir, os.W_OK):
344+
# Check that the directory is read-only.
345+
try:
346+
os.mkdir(probe)
347+
except PermissionError:
348+
pass
349+
else:
350+
os.rmdir(probe)
338351
self.skipTest("can't set the directory read-only")
352+
# gh-66305: Now it takes a split second, but previously
353+
# it took about 10 days on Windows.
339354
with self.assertRaises(PermissionError):
340355
self.make_temp()
341-
self.assertEqual(os.listdir(tempfile.tempdir), [])
342356
finally:
343-
os.chmod(tempfile.tempdir, oldmode)
357+
if os.name == 'nt':
358+
cmd = ['icacls', tempfile.tempdir, '/grant:r', 'Everyone:(M)']
359+
subprocess.run(cmd, check=True, stdout=stdout)
360+
else:
361+
os.chmod(tempfile.tempdir, oldmode)
362+
self.assertEqual(os.listdir(tempfile.tempdir), [])
344363

345364
def test_nonexisting_directory(self):
346365
with _inside_empty_temp_dir():

0 commit comments

Comments
 (0)