Skip to content

Commit 2bd8ff0

Browse files
authored
Merge pull request RustPython#5257 from youknowone/os-posix
Update os/posix/posixpath from CPython 3.12.2
2 parents 153ec28 + 5c9d6d4 commit 2bd8ff0

7 files changed

Lines changed: 1030 additions & 392 deletions

File tree

‎Lib/genericpath.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import stat
1111

1212
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
13-
'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
13+
'getsize', 'isdir', 'isfile', 'islink', 'samefile', 'sameopenfile',
1414
'samestat']
1515

1616

@@ -48,6 +48,18 @@ def isdir(s):
4848
return stat.S_ISDIR(st.st_mode)
4949

5050

51+
# Is a path a symbolic link?
52+
# This will always return false on systems where os.lstat doesn't exist.
53+
54+
def islink(path):
55+
"""Test whether a path is a symbolic link"""
56+
try:
57+
st = os.lstat(path)
58+
except (OSError, ValueError, AttributeError):
59+
return False
60+
return stat.S_ISLNK(st.st_mode)
61+
62+
5163
def getsize(filename):
5264
"""Return the size of a file, reported by os.stat()."""
5365
return os.stat(filename).st_size

‎Lib/os.py‎

Lines changed: 96 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
288288
dirpath, dirnames, filenames
289289
290290
dirpath is a string, the path to the directory. dirnames is a list of
291-
the names of the subdirectories in dirpath (excluding '.' and '..').
291+
the names of the subdirectories in dirpath (including symlinks to directories,
292+
and excluding '.' and '..').
292293
filenames is a list of the names of the non-directory files in dirpath.
293294
Note that the names in the lists are just names, with no path components.
294295
To get a full path (which begins with top) to a file or directory in
@@ -331,97 +332,103 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
331332
import os
332333
from os.path import join, getsize
333334
for root, dirs, files in os.walk('python/Lib/email'):
334-
print(root, "consumes", end="")
335-
print(sum(getsize(join(root, name)) for name in files), end="")
335+
print(root, "consumes ")
336+
print(sum(getsize(join(root, name)) for name in files), end=" ")
336337
print("bytes in", len(files), "non-directory files")
337338
if 'CVS' in dirs:
338339
dirs.remove('CVS') # don't visit CVS directories
339340
340341
"""
341342
sys.audit("os.walk", top, topdown, onerror, followlinks)
342-
return _walk(fspath(top), topdown, onerror, followlinks)
343-
344-
def _walk(top, topdown, onerror, followlinks):
345-
dirs = []
346-
nondirs = []
347-
walk_dirs = []
348-
349-
# We may not have read permission for top, in which case we can't
350-
# get a list of the files the directory contains. os.walk
351-
# always suppressed the exception then, rather than blow up for a
352-
# minor reason when (say) a thousand readable directories are still
353-
# left to visit. That logic is copied here.
354-
try:
355-
# Note that scandir is global in this module due
356-
# to earlier import-*.
357-
scandir_it = scandir(top)
358-
except OSError as error:
359-
if onerror is not None:
360-
onerror(error)
361-
return
362343

363-
with scandir_it:
364-
while True:
365-
try:
344+
stack = [fspath(top)]
345+
islink, join = path.islink, path.join
346+
while stack:
347+
top = stack.pop()
348+
if isinstance(top, tuple):
349+
yield top
350+
continue
351+
352+
dirs = []
353+
nondirs = []
354+
walk_dirs = []
355+
356+
# We may not have read permission for top, in which case we can't
357+
# get a list of the files the directory contains.
358+
# We suppress the exception here, rather than blow up for a
359+
# minor reason when (say) a thousand readable directories are still
360+
# left to visit.
361+
try:
362+
scandir_it = scandir(top)
363+
except OSError as error:
364+
if onerror is not None:
365+
onerror(error)
366+
continue
367+
368+
cont = False
369+
with scandir_it:
370+
while True:
366371
try:
367-
entry = next(scandir_it)
368-
except StopIteration:
372+
try:
373+
entry = next(scandir_it)
374+
except StopIteration:
375+
break
376+
except OSError as error:
377+
if onerror is not None:
378+
onerror(error)
379+
cont = True
369380
break
370-
except OSError as error:
371-
if onerror is not None:
372-
onerror(error)
373-
return
374-
375-
try:
376-
is_dir = entry.is_dir()
377-
except OSError:
378-
# If is_dir() raises an OSError, consider that the entry is not
379-
# a directory, same behaviour than os.path.isdir().
380-
is_dir = False
381381

382-
if is_dir:
383-
dirs.append(entry.name)
384-
else:
385-
nondirs.append(entry.name)
382+
try:
383+
is_dir = entry.is_dir()
384+
except OSError:
385+
# If is_dir() raises an OSError, consider the entry not to
386+
# be a directory, same behaviour as os.path.isdir().
387+
is_dir = False
386388

387-
if not topdown and is_dir:
388-
# Bottom-up: recurse into sub-directory, but exclude symlinks to
389-
# directories if followlinks is False
390-
if followlinks:
391-
walk_into = True
389+
if is_dir:
390+
dirs.append(entry.name)
392391
else:
393-
try:
394-
is_symlink = entry.is_symlink()
395-
except OSError:
396-
# If is_symlink() raises an OSError, consider that the
397-
# entry is not a symbolic link, same behaviour than
398-
# os.path.islink().
399-
is_symlink = False
400-
walk_into = not is_symlink
401-
402-
if walk_into:
403-
walk_dirs.append(entry.path)
404-
405-
# Yield before recursion if going top down
406-
if topdown:
407-
yield top, dirs, nondirs
408-
409-
# Recurse into sub-directories
410-
islink, join = path.islink, path.join
411-
for dirname in dirs:
412-
new_path = join(top, dirname)
413-
# Issue #23605: os.path.islink() is used instead of caching
414-
# entry.is_symlink() result during the loop on os.scandir() because
415-
# the caller can replace the directory entry during the "yield"
416-
# above.
417-
if followlinks or not islink(new_path):
418-
yield from _walk(new_path, topdown, onerror, followlinks)
419-
else:
420-
# Recurse into sub-directories
421-
for new_path in walk_dirs:
422-
yield from _walk(new_path, topdown, onerror, followlinks)
423-
# Yield after recursion if going bottom up
424-
yield top, dirs, nondirs
392+
nondirs.append(entry.name)
393+
394+
if not topdown and is_dir:
395+
# Bottom-up: traverse into sub-directory, but exclude
396+
# symlinks to directories if followlinks is False
397+
if followlinks:
398+
walk_into = True
399+
else:
400+
try:
401+
is_symlink = entry.is_symlink()
402+
except OSError:
403+
# If is_symlink() raises an OSError, consider the
404+
# entry not to be a symbolic link, same behaviour
405+
# as os.path.islink().
406+
is_symlink = False
407+
walk_into = not is_symlink
408+
409+
if walk_into:
410+
walk_dirs.append(entry.path)
411+
if cont:
412+
continue
413+
414+
if topdown:
415+
# Yield before sub-directory traversal if going top down
416+
yield top, dirs, nondirs
417+
# Traverse into sub-directories
418+
for dirname in reversed(dirs):
419+
new_path = join(top, dirname)
420+
# bpo-23605: os.path.islink() is used instead of caching
421+
# entry.is_symlink() result during the loop on os.scandir() because
422+
# the caller can replace the directory entry during the "yield"
423+
# above.
424+
if followlinks or not islink(new_path):
425+
stack.append(new_path)
426+
else:
427+
# Yield after sub-directory traversal if going bottom up
428+
stack.append((top, dirs, nondirs))
429+
# Traverse into sub-directories
430+
for new_path in reversed(walk_dirs):
431+
stack.append(new_path)
425432

426433
__all__.append("walk")
427434

@@ -461,13 +468,12 @@ def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=
461468
dirs.remove('CVS') # don't visit CVS directories
462469
"""
463470
sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd)
464-
if not isinstance(top, int) or not hasattr(top, '__index__'):
465-
top = fspath(top)
471+
top = fspath(top)
466472
# Note: To guard against symlink races, we use the standard
467473
# lstat()/open()/fstat() trick.
468474
if not follow_symlinks:
469475
orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
470-
topfd = open(top, O_RDONLY, dir_fd=dir_fd)
476+
topfd = open(top, O_RDONLY | O_NONBLOCK, dir_fd=dir_fd)
471477
try:
472478
if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and
473479
path.samestat(orig_st, stat(topfd)))):
@@ -516,7 +522,7 @@ def _fwalk(topfd, toppath, isbytes, topdown, onerror, follow_symlinks):
516522
assert entries is not None
517523
name, entry = name
518524
orig_st = entry.stat(follow_symlinks=False)
519-
dirfd = open(name, O_RDONLY, dir_fd=topfd)
525+
dirfd = open(name, O_RDONLY | O_NONBLOCK, dir_fd=topfd)
520526
except OSError as err:
521527
if onerror is not None:
522528
onerror(err)
@@ -704,9 +710,11 @@ def __len__(self):
704710
return len(self._data)
705711

706712
def __repr__(self):
707-
return 'environ({{{}}})'.format(', '.join(
708-
('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value))
709-
for key, value in self._data.items())))
713+
formatted_items = ", ".join(
714+
f"{self.decodekey(key)!r}: {self.decodevalue(value)!r}"
715+
for key, value in self._data.items()
716+
)
717+
return f"environ({{{formatted_items}}})"
710718

711719
def copy(self):
712720
return dict(self)
@@ -980,7 +988,7 @@ def popen(cmd, mode="r", buffering=-1):
980988
raise ValueError("invalid mode %r" % mode)
981989
if buffering == 0 or buffering is None:
982990
raise ValueError("popen() does not support unbuffered streams")
983-
import subprocess, io
991+
import subprocess
984992
if mode == "r":
985993
proc = subprocess.Popen(cmd,
986994
shell=True, text=True,

0 commit comments

Comments
 (0)