Update test_os from CPython 3.10.6
@@ -10,8 +10,11 @@ import fnmatch
import fractions
import itertools
import locale
-# TODO: RUSTPYTHON
-# import mmap
+# XXX: RUSTPYTHON
+try:
+ import mmap
+except ImportError:
+ pass
import os
import pickle
import select
@@ -24,6 +27,7 @@ import subprocess
import sys
import sysconfig
import tempfile
+import textwrap
import threading
import time
import types
@@ -2889,6 +2893,48 @@ class Win32NtTests(unittest.TestCase):
self.assertEqual(0, handle_delta)
+ def test_stat_unlink_race(self):
+ # bpo-46785: the implementation of os.stat() falls back to reading
+ # the parent directory if CreateFileW() fails with a permission
+ # error. If reading the parent directory fails because the file or
+ # directory are subsequently unlinked, or because the volume or
+ # share are no longer available, then the original permission error
+ # should not be restored.
+ filename = os_helper.TESTFN
+ self.addCleanup(os_helper.unlink, filename)
+ deadline = time.time() + 5
+ command = textwrap.dedent("""\
+ import os
+ import sys
+ import time
+
+ filename = sys.argv[1]
+ deadline = float(sys.argv[2])
+
+ while time.time() < deadline:
+ try:
+ with open(filename, "w") as f:
+ pass
+ except OSError:
+ pass
+ try:
+ os.remove(filename)
+ except OSError:
+ pass
+ """)
+
+ with subprocess.Popen([sys.executable, '-c', command, filename, str(deadline)]) as proc:
+ while time.time() < deadline:
+ try:
+ os.stat(filename)
+ except FileNotFoundError as e:
+ assert e.winerror == 2 # ERROR_FILE_NOT_FOUND
+ try:
+ proc.wait(1)
+ except subprocess.TimeoutExpired:
+ proc.terminate()
+
+
@os_helper.skip_unless_symlink
class NonLocalSymlinkTests(unittest.TestCase):
