Skip to content

Commit a7dd8b0

Browse files
committed
Migrate constants and functions to helper
1 parent 7c58504 commit a7dd8b0

File tree

1 file changed

+1
-132
lines changed

1 file changed

+1
-132
lines changed

Lib/test/support/__init__.py

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@
8383
# io
8484
"record_original_stdout", "get_original_stdout", "captured_stdout",
8585
"captured_stdin", "captured_stderr",
86-
# filesystem
87-
"TESTFN", "SAVEDCWD", "unlink", "rmtree", "temp_cwd", "findfile",
88-
"create_empty_file", "can_symlink", "fs_is_case_insensitive",
8986
# unittest
9087
"is_resource_enabled", "requires", "requires_freebsd_version",
9188
"requires_linux_version", "requires_mac_ver",
@@ -279,135 +276,6 @@ def _force_run(path, func, *args):
279276
os.chmod(path, stat.S_IRWXU)
280277
return func(*args)
281278

282-
if sys.platform.startswith("win"):
283-
def _waitfor(func, pathname, waitall=False):
284-
# Perform the operation
285-
func(pathname)
286-
# Now setup the wait loop
287-
if waitall:
288-
dirname = pathname
289-
else:
290-
dirname, name = os.path.split(pathname)
291-
dirname = dirname or '.'
292-
# Check for `pathname` to be removed from the filesystem.
293-
# The exponential backoff of the timeout amounts to a total
294-
# of ~1 second after which the deletion is probably an error
295-
# anyway.
296-
# Testing on an i7@4.3GHz shows that usually only 1 iteration is
297-
# required when contention occurs.
298-
timeout = 0.001
299-
while timeout < 1.0:
300-
# Note we are only testing for the existence of the file(s) in
301-
# the contents of the directory regardless of any security or
302-
# access rights. If we have made it this far, we have sufficient
303-
# permissions to do that much using Python's equivalent of the
304-
# Windows API FindFirstFile.
305-
# Other Windows APIs can fail or give incorrect results when
306-
# dealing with files that are pending deletion.
307-
L = os.listdir(dirname)
308-
if not (L if waitall else name in L):
309-
return
310-
# Increase the timeout and try again
311-
time.sleep(timeout)
312-
timeout *= 2
313-
warnings.warn('tests may fail, delete still pending for ' + pathname,
314-
RuntimeWarning, stacklevel=4)
315-
316-
def _unlink(filename):
317-
# XXX RUSTPYTHON: on ci, unlink() raises PermissionError when target doesn't exist.
318-
# Might also happen locally, but not sure
319-
if not os.path.exists(filename):
320-
return
321-
_waitfor(os.unlink, filename)
322-
323-
def _rmdir(dirname):
324-
# XXX RUSTPYTHON: on ci, unlink() raises PermissionError when target doesn't exist.
325-
# Might also happen locally, but not sure
326-
if not os.path.exists(dirname):
327-
return
328-
_waitfor(os.rmdir, dirname)
329-
330-
def _rmtree(path):
331-
# XXX RUSTPYTHON: on ci, unlink() raises PermissionError when target doesn't exist.
332-
# Might also happen locally, but not sure
333-
if not os.path.exists(path):
334-
return
335-
def _rmtree_inner(path):
336-
for name in _force_run(path, os.listdir, path):
337-
fullname = os.path.join(path, name)
338-
try:
339-
mode = os.lstat(fullname).st_mode
340-
except OSError as exc:
341-
print("os_helper.rmtree(): os.lstat(%r) failed with %s" % (fullname, exc),
342-
file=sys.__stderr__)
343-
mode = 0
344-
if stat.S_ISDIR(mode):
345-
_waitfor(_rmtree_inner, fullname, waitall=True)
346-
_force_run(fullname, os.rmdir, fullname)
347-
else:
348-
_force_run(fullname, os.unlink, fullname)
349-
_waitfor(_rmtree_inner, path, waitall=True)
350-
_waitfor(lambda p: _force_run(p, os.rmdir, p), path)
351-
352-
def _longpath(path):
353-
try:
354-
import ctypes
355-
except ImportError:
356-
# No ctypes means we can't expands paths.
357-
pass
358-
else:
359-
buffer = ctypes.create_unicode_buffer(len(path) * 2)
360-
length = ctypes.windll.kernel32.GetLongPathNameW(path, buffer,
361-
len(buffer))
362-
if length:
363-
return buffer[:length]
364-
return path
365-
else:
366-
_unlink = os.unlink
367-
_rmdir = os.rmdir
368-
369-
def _rmtree(path):
370-
try:
371-
shutil.rmtree(path)
372-
return
373-
except OSError:
374-
pass
375-
376-
def _rmtree_inner(path):
377-
for name in _force_run(path, os.listdir, path):
378-
fullname = os.path.join(path, name)
379-
try:
380-
mode = os.lstat(fullname).st_mode
381-
except OSError:
382-
mode = 0
383-
if stat.S_ISDIR(mode):
384-
_rmtree_inner(fullname)
385-
_force_run(path, os.rmdir, fullname)
386-
else:
387-
_force_run(path, os.unlink, fullname)
388-
_rmtree_inner(path)
389-
os.rmdir(path)
390-
391-
def _longpath(path):
392-
return path
393-
394-
def unlink(filename):
395-
try:
396-
_unlink(filename)
397-
except (FileNotFoundError, NotADirectoryError):
398-
pass
399-
400-
def rmdir(dirname):
401-
try:
402-
_rmdir(dirname)
403-
except FileNotFoundError:
404-
pass
405-
406-
def rmtree(path):
407-
try:
408-
_rmtree(path)
409-
except FileNotFoundError:
410-
pass
411279

412280
# Check whether a gui is actually available
413281
def _is_gui_available():
@@ -2245,6 +2113,7 @@ def skip_unless_bind_unix_socket(test):
22452113
except OSError as e:
22462114
_bind_nix_socket_error = e
22472115
finally:
2116+
from .os_helper import unlink
22482117
unlink(path)
22492118
if _bind_nix_socket_error:
22502119
msg = 'Requires a functional unix bind(): %s' % _bind_nix_socket_error

0 commit comments

Comments
 (0)