1010 - os.extsep is the extension separator (always '.')
1111 - os.altsep is the alternate pathname separator (None or '/')
1212 - os.pathsep is the component separator used in $PATH etc
13- - os.linesep is the line separator in text files ('\r' or '\ n' or '\r\n')
13+ - os.linesep is the line separator in text files ('\n' or '\r\n')
1414 - os.defpath is the default search path for executables
1515 - os.devnull is the file path of the null device ('/dev/null', etc.)
1616
@@ -64,6 +64,10 @@ def _get_exports_list(module):
6464 from posix import _have_functions
6565 except ImportError :
6666 pass
67+ try :
68+ from posix import _create_environ
69+ except ImportError :
70+ pass
6771
6872 import posix
6973 __all__ .extend (_get_exports_list (posix ))
@@ -88,6 +92,10 @@ def _get_exports_list(module):
8892 from nt import _have_functions
8993 except ImportError :
9094 pass
95+ try :
96+ from nt import _create_environ
97+ except ImportError :
98+ pass
9199
92100else :
93101 raise ImportError ('no os specific module found' )
@@ -366,61 +374,45 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
366374 # minor reason when (say) a thousand readable directories are still
367375 # left to visit.
368376 try :
369- scandir_it = scandir (top )
377+ with scandir (top ) as entries :
378+ for entry in entries :
379+ try :
380+ if followlinks is _walk_symlinks_as_files :
381+ is_dir = entry .is_dir (follow_symlinks = False ) and not entry .is_junction ()
382+ else :
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
388+
389+ if is_dir :
390+ dirs .append (entry .name )
391+ else :
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 )
370411 except OSError as error :
371412 if onerror is not None :
372413 onerror (error )
373414 continue
374415
375- cont = False
376- with scandir_it :
377- while True :
378- try :
379- try :
380- entry = next (scandir_it )
381- except StopIteration :
382- break
383- except OSError as error :
384- if onerror is not None :
385- onerror (error )
386- cont = True
387- break
388-
389- try :
390- if followlinks is _walk_symlinks_as_files :
391- is_dir = entry .is_dir (follow_symlinks = False ) and not entry .is_junction ()
392- else :
393- is_dir = entry .is_dir ()
394- except OSError :
395- # If is_dir() raises an OSError, consider the entry not to
396- # be a directory, same behaviour as os.path.isdir().
397- is_dir = False
398-
399- if is_dir :
400- dirs .append (entry .name )
401- else :
402- nondirs .append (entry .name )
403-
404- if not topdown and is_dir :
405- # Bottom-up: traverse into sub-directory, but exclude
406- # symlinks to directories if followlinks is False
407- if followlinks :
408- walk_into = True
409- else :
410- try :
411- is_symlink = entry .is_symlink ()
412- except OSError :
413- # If is_symlink() raises an OSError, consider the
414- # entry not to be a symbolic link, same behaviour
415- # as os.path.islink().
416- is_symlink = False
417- walk_into = not is_symlink
418-
419- if walk_into :
420- walk_dirs .append (entry .path )
421- if cont :
422- continue
423-
424416 if topdown :
425417 # Yield before sub-directory traversal if going top down
426418 yield top , dirs , nondirs
@@ -774,7 +766,7 @@ def __ror__(self, other):
774766 new .update (self )
775767 return new
776768
777- def _createenviron ():
769+ def _create_environ_mapping ():
778770 if name == 'nt' :
779771 # Where Env Var Names Must Be UPPERCASE
780772 def check_str (value ):
@@ -804,9 +796,24 @@ def decode(value):
804796 encode , decode )
805797
806798# unicode environ
807- environ = _createenviron ()
808- del _createenviron
799+ environ = _create_environ_mapping ()
800+ del _create_environ_mapping
801+
802+
803+ if _exists ("_create_environ" ):
804+ def reload_environ ():
805+ data = _create_environ ()
806+ if name == 'nt' :
807+ encodekey = environ .encodekey
808+ data = {encodekey (key ): value
809+ for key , value in data .items ()}
810+
811+ # modify in-place to keep os.environb in sync
812+ env_data = environ ._data
813+ env_data .clear ()
814+ env_data .update (data )
809815
816+ __all__ .append ("reload_environ" )
810817
811818def getenv (key , default = None ):
812819 """Get an environment variable, return None if it doesn't exist.
0 commit comments