Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 61 additions & 54 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- os.extsep is the extension separator (always '.')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.linesep is the line separator in text files ('\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)

Expand Down Expand Up @@ -64,6 +64,10 @@ def _get_exports_list(module):
from posix import _have_functions
except ImportError:
pass
try:
from posix import _create_environ
except ImportError:
pass

import posix
__all__.extend(_get_exports_list(posix))
Expand All @@ -88,6 +92,10 @@ def _get_exports_list(module):
from nt import _have_functions
except ImportError:
pass
try:
from nt import _create_environ
except ImportError:
pass

else:
raise ImportError('no os specific module found')
Expand Down Expand Up @@ -366,61 +374,45 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
# minor reason when (say) a thousand readable directories are still
# left to visit.
try:
scandir_it = scandir(top)
with scandir(top) as entries:
for entry in entries:
try:
if followlinks is _walk_symlinks_as_files:
is_dir = entry.is_dir(follow_symlinks=False) and not entry.is_junction()
else:
is_dir = entry.is_dir()
except OSError:
# If is_dir() raises an OSError, consider the entry not to
# be a directory, same behaviour as os.path.isdir().
is_dir = False

if is_dir:
dirs.append(entry.name)
else:
nondirs.append(entry.name)

if not topdown and is_dir:
# Bottom-up: traverse into sub-directory, but exclude
# symlinks to directories if followlinks is False
if followlinks:
walk_into = True
else:
try:
is_symlink = entry.is_symlink()
except OSError:
# If is_symlink() raises an OSError, consider the
# entry not to be a symbolic link, same behaviour
# as os.path.islink().
is_symlink = False
walk_into = not is_symlink

if walk_into:
walk_dirs.append(entry.path)
except OSError as error:
if onerror is not None:
onerror(error)
continue

cont = False
with scandir_it:
while True:
try:
try:
entry = next(scandir_it)
except StopIteration:
break
except OSError as error:
if onerror is not None:
onerror(error)
cont = True
break

try:
if followlinks is _walk_symlinks_as_files:
is_dir = entry.is_dir(follow_symlinks=False) and not entry.is_junction()
else:
is_dir = entry.is_dir()
except OSError:
# If is_dir() raises an OSError, consider the entry not to
# be a directory, same behaviour as os.path.isdir().
is_dir = False

if is_dir:
dirs.append(entry.name)
else:
nondirs.append(entry.name)

if not topdown and is_dir:
# Bottom-up: traverse into sub-directory, but exclude
# symlinks to directories if followlinks is False
if followlinks:
walk_into = True
else:
try:
is_symlink = entry.is_symlink()
except OSError:
# If is_symlink() raises an OSError, consider the
# entry not to be a symbolic link, same behaviour
# as os.path.islink().
is_symlink = False
walk_into = not is_symlink

if walk_into:
walk_dirs.append(entry.path)
if cont:
continue

if topdown:
# Yield before sub-directory traversal if going top down
yield top, dirs, nondirs
Expand Down Expand Up @@ -774,7 +766,7 @@ def __ror__(self, other):
new.update(self)
return new

def _createenviron():
def _create_environ_mapping():
if name == 'nt':
# Where Env Var Names Must Be UPPERCASE
def check_str(value):
Expand Down Expand Up @@ -804,9 +796,24 @@ def decode(value):
encode, decode)

# unicode environ
environ = _createenviron()
del _createenviron
environ = _create_environ_mapping()
del _create_environ_mapping


if _exists("_create_environ"):
def reload_environ():
data = _create_environ()
if name == 'nt':
encodekey = environ.encodekey
data = {encodekey(key): value
for key, value in data.items()}

# modify in-place to keep os.environb in sync
env_data = environ._data
env_data.clear()
env_data.update(data)

__all__.append("reload_environ")

def getenv(key, default=None):
"""Get an environment variable, return None if it doesn't exist.
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_genericpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def test_exists_fd(self):
os.close(w)
self.assertFalse(self.pathmodule.exists(r))

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_exists_bool(self):
for fd in False, True:
with self.assertWarnsRegex(RuntimeWarning,
Expand Down
Loading
Loading