import abc import sys import stat as st from collectionsabc import checkmethods genericalias typelistint names sys builtinmodulenames note more names are added to all later all altsep curdir pardir sep pathsep linesep defpath name path devnull seekset seekcur seekend fsencode fsdecode getexecpath fdopen extsep def existsname return name in globals def getexportslistmodule try return listmodule all except attributeerror return n for n in dirmodule if n0 any new dependencies of the os module andor changes in path separator requires updating importlib as well if posix in names name posix linesep n from posix import try from posix import exit all append exit except importerror pass import posixpath as path try from posix import havefunctions except importerror pass import posix all extendgetexportslistposix del posix elif nt in names name nt linesep rn from nt import try from nt import exit all append exit except importerror pass import ntpath as path import nt all extendgetexportslistnt del nt try from nt import havefunctions except importerror pass else raise importerror no os specific module found sys modules os path path from os path import curdir pardir sep pathsep defpath extsep altsep devnull del names if existshavefunctions globals globals def addstr fn if fn in globals and str in havefunctions set addglobalsfn set set addhavefaccessat access addhavefchmodat chmod addhavefchownat chown addhavefstatat stat addhavefutimesat utime addhavelinkat link addhavemkdirat mkdir addhavemkfifoat mkfifo addhavemknodat mknod addhaveopenat open addhavereadlinkat readlink addhaverenameat rename addhavesymlinkat symlink addhaveunlinkat unlink addhaveunlinkat rmdir addhaveutimensat utime supportsdirfd set set set addhavefaccessat access supportseffectiveids set set set addhavefchdir chdir addhavefchmod chmod addhavefchown chown addhavefdopendir listdir addhavefdopendir scandir addhavefexecve execve set addstat fstat always works addhaveftruncate truncate addhavefutimens utime addhavefutimes utime addhavefpathconf pathconf if existsstatvfs and existsfstatvfs mac os x10 3 addhavefstatvfs statvfs supportsfd set set set addhavefaccessat access some platforms don t support lchmod often the function exists anyway as a stub that always returns enosup or perhaps eopnotsupp no i don t know why that s a good design configure will detect this and reject itso havelchmod still won t be defined on such platforms this is very helpful however sometimes platforms without a working lchmod do have fchmodat examples linux kernel 3 2 with glibc 2 15 openindiana 3 x and fchmodat has a flag that theoretically makes it behave like lchmod so in theory it would be a suitable replacement for lchmod but when lchmod doesn t work fchmodat s flag doesn t work either sadly configure isn t sophisticated enough to detect this conditionit only determines whether or not fchmodat minimally works therefore we simply ignore fchmodat when deciding whether or not os chmod supports followsymlinks just checking lchmod is sufficient after allif you have a working fchmodat your lchmod almost certainly works too addhavefchmodat chmod addhavefchownat chown addhavefstatat stat addhavelchflags chflags addhavelchmod chmod if existslchown mac os x10 3 addhavelchown chown addhavelinkat link addhavelutimes utime addhavelstat stat addhavefstatat stat addhaveutimensat utime addmswindows stat supportsfollowsymlinks set del set del havefunctions del globals del add python uses fixed values for the seek constants they are mapped to native constants if necessary in posixmodule c other possible seek values are directly imported from posixmodule c seekset 0 seekcur 1 seekend 2 super directory utilities inspired by eric raymond the doc strings are mostly his def makedirsname mode0o777 existokfalse head tail path splitname if not tail head tail path splithead if head and tail and not path existshead try makedirshead existokexistok except fileexistserror defeats race condition when another thread created the path pass cdir curdir if isinstancetail bytes cdir bytescurdir ascii if tail cdir xxxnewdir exists if xxxnewdir exists return try mkdirname mode except oserror cannot rely on checking for eexist since the operating system could give priority to other errors like eacces or erofs if not existok or not path isdirname raise def removedirsname rmdirname head tail path splitname if not tail head tail path splithead while head and tail try rmdirhead except oserror break head tail path splithead def renamesold new head tail path splitnew if head and tail and not path existshead makedirshead renameold new head tail path splitold if head and tail try removedirshead except oserror pass all extendmakedirs removedirs renames def walktop topdowntrue onerrornone followlinksfalse sys auditos walk top topdown onerror followlinks stack fspathtop islink join path islink path join while stack top stack pop if isinstancetop tuple yield top continue dirs nondirs walkdirs we may not have read permission for top in which case we can t get a list of the files the directory contains we suppress the exception here rather than blow up for a minor reason when say a thousand readable directories are still left to visit try scandirit scandirtop except oserror as error if onerror is not none onerrorerror continue cont false with scandirit while true try try entry nextscandirit except stopiteration break except oserror as error if onerror is not none onerrorerror cont true break try isdir entry isdir except oserror if isdir raises an oserror consider the entry not to be a directory same behaviour as os path isdir isdir false if isdir dirs appendentry name else nondirs appendentry name if not topdown and isdir bottomup traverse into subdirectory but exclude symlinks to directories if followlinks is false if followlinks walkinto true else try issymlink entry issymlink except oserror if issymlink raises an oserror consider the entry not to be a symbolic link same behaviour as os path islink issymlink false walkinto not issymlink if walkinto walkdirs appendentry path if cont continue if topdown yield before subdirectory traversal if going top down yield top dirs nondirs traverse into subdirectories for dirname in reverseddirs newpath jointop dirname bpo23605 os path islink is used instead of caching entry issymlink result during the loop on os scandir because the caller can replace the directory entry during the yield above if followlinks or not islinknewpath stack appendnewpath else yield after subdirectory traversal if going bottom up stack appendtop dirs nondirs traverse into subdirectories for newpath in reversedwalkdirs stack appendnewpath all appendwalk if open stat supportsdirfd and scandir stat supportsfd def fwalktop topdowntrue onerrornone followsymlinksfalse dirfdnone sys auditos fwalk top topdown onerror followsymlinks dirfd top fspathtop note to guard against symlink races we use the standard lstatopenfstat trick if not followsymlinks origst stattop followsymlinksfalse dirfddirfd topfd opentop ordonly dirfddirfd try if followsymlinks or st sisdirorigst stmode and path samestatorigst stattopfd yield from fwalktopfd top isinstancetop bytes topdown onerror followsymlinks finally closetopfd def fwalktopfd toppath isbytes topdown onerror followsymlinks note this uses odepth of the directory tree file descriptors if necessary it can be adapted to only require o1 fds see issue 13734 scandirit scandirtopfd dirs nondirs entries none if topdown or followsymlinks else for entry in scandirit name entry name if isbytes name fsencodename try if entry isdir dirs appendname if entries is not none entries appendentry else nondirs appendname except oserror try add dangling symlinks ignore disappeared files if entry issymlink nondirs appendname except oserror pass if topdown yield toppath dirs nondirs topfd for name in dirs if entries is none else zipdirs entries try if not followsymlinks if topdown origst statname dirfdtopfd followsymlinksfalse else assert entries is not none name entry name origst entry statfollowsymlinksfalse dirfd openname ordonly dirfdtopfd except oserror as err if onerror is not none onerrorerr continue try if followsymlinks or path samestatorigst statdirfd dirpath path jointoppath name yield from fwalkdirfd dirpath isbytes topdown onerror followsymlinks finally closedirfd if not topdown yield toppath dirs nondirs topfd all appendfwalk def execlfile args execlefile args env execute the executable file with argument list args and environment env replacing the current process env args1 execvefile args 1 env def execlpfile args execlpefile args env execute the executable file which is searched for along path with argument list args and environment env replacing the current process env args1 execvpefile args 1 env def execvpfile args execvpefile args env execute the executable file which is searched for along path with argument list args and environment env replacing the current process args may be a list or tuple of strings execvpefile args env all extendexecl execle execlp execlpe execvp execvpe def execvpefile args envnone if env is not none execfunc execve argrest args env else execfunc execv argrest args env environ if path dirnamefile execfuncfile argrest return savedexc none pathlist getexecpathenv if name nt file fsencodefile pathlist mapfsencode pathlist for dir in pathlist fullname path joindir file try execfuncfullname argrest except filenotfounderror notadirectoryerror as e lastexc e except oserror as e lastexc e if savedexc is none savedexc e if savedexc is not none raise savedexc raise lastexc def getexecpathenvnone use a local import instead of a global import to limit the number of modules loaded at startup the os module is always loaded at startup by python it may also avoid a bootstrap issue import warnings if env is none env environ b path get path and path getb path emit a byteswarning when using python b or python bb ignore the warning with warnings catchwarnings warnings simplefilterignore byteswarning try pathlist env get path except typeerror pathlist none if supportsbytesenviron try pathlistb envb path except keyerror typeerror pass else if pathlist is not none raise valueerror env cannot contain path and b path keys pathlist pathlistb if pathlist is not none and isinstancepathlist bytes pathlist fsdecodepathlist if pathlist is none pathlist defpath return pathlist splitpathsep change environ to automatically call putenv and unsetenv from collectionsabc import mutablemapping mapping class environmutablemapping def initself data encodekey decodekey encodevalue decodevalue self encodekey encodekey self decodekey decodekey self encodevalue encodevalue self decodevalue decodevalue self data data def getitemself key try value self dataself encodekeykey except keyerror raise keyerror with the original key value raise keyerrorkey from none return self decodevaluevalue def setitemself key value key self encodekeykey value self encodevaluevalue putenvkey value self datakey value def delitemself key encodedkey self encodekeykey unsetenvencodedkey try del self dataencodedkey except keyerror raise keyerror with the original key value raise keyerrorkey from none def iterself list from dict object is an atomic operation keys listself data for key in keys yield self decodekeykey def lenself return lenself data def reprself formatteditems join fself decodekeykey r self decodevaluevalue r for key value in self data items return fenvironformatteditems def copyself return dictself def setdefaultself key value if key not in self selfkey value return selfkey def iorself other self updateother return self def orself other if not isinstanceother mapping return notimplemented new dictself new updateother return new def rorself other if not isinstanceother mapping return notimplemented new dictother new updateself return new def createenviron if name nt where env var names must be uppercase def checkstrvalue if not isinstancevalue str raise typeerrorstr expected not s typevalue name return value encode checkstr decode str def encodekeykey return encodekey upper data for key value in environ items dataencodekeykey value else where env var names can be mixed case encoding sys getfilesystemencoding def encodevalue if not isinstancevalue str raise typeerrorstr expected not s typevalue name return value encodeencoding surrogateescape def decodevalue return value decodeencoding surrogateescape encodekey encode data environ return environdata encodekey decode encode decode unicode environ environ createenviron del createenviron def getenvkey defaultnone bytes environ get an environment variable return none if it doesn t exist the optional second argument can specify an alternate default key default and the result are bytes return environb getkey default all extendenvironb getenvb def fscodec encoding sys getfilesystemencoding errors sys getfilesystemencodeerrors def fsencodefilename filename fspathfilename does typechecking of filename if isinstancefilename str return filename encodeencoding errors else return filename def fsdecodefilename filename fspathfilename does typechecking of filename if isinstancefilename bytes return filename decodeencoding errors else return filename return fsencode fsdecode fsencode fsdecode fscodec del fscodec supply spawn probably only for unix if existsfork and not existsspawnv and existsexecv pwait 0 pnowait pnowaito 1 all extendpwait pnowait pnowaito xxx should we support pdetach i suppose it could fork2 and close the std io streams also poverlay is the same as execv def spawnvefmode file args env func internal helper func is the exec function to use if not isinstanceargs tuple list raise typeerror argv must be a tuple or a list if not args or not args0 raise valueerror argv first element cannot be empty pid fork if not pid child try if env is none funcfile args else funcfile args env except exit127 else parent if mode pnowait return pid caller is responsible for waiting while 1 wpid sts waitpidpid 0 if wifstoppedsts continue return waitstatustoexitcodests def spawnvmode file args spawnvemode file args env integer execute file with arguments from args in a subprocess with the specified environment if mode pnowait return the pid of the process if mode pwait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it return spawnvefmode file args env execve note spawnvpe isn t currently supported on windows def spawnvpmode file args spawnvpemode file args env integer execute file which is looked for along path with arguments from args in a subprocess with the supplied environment if mode pnowait return the pid of the process if mode pwait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it return spawnvefmode file args env execvpe all extendspawnv spawnve spawnvp spawnvpe if existsspawnv these aren t supplied by the basic windows code but can be easily implemented in python def spawnlmode file args spawnlemode file args env integer execute file with arguments from args in a subprocess with the supplied environment if mode pnowait return the pid of the process if mode pwait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it env args1 return spawnvemode file args 1 env all extendspawnl spawnle if existsspawnvp at the moment windows doesn t implement spawnvpe so it won t have spawnlpe either def spawnlpmode file args spawnlpemode file args env integer execute file which is looked for along path with arguments from args in a subprocess with the supplied environment if mode pnowait return the pid of the process if mode pwait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it env args1 return spawnvpemode file args 1 env all extendspawnlp spawnlpe vxworks has no user space shell provided as a result running command in a shell can t be supported if sys platform vxworks supply os popen def popencmd moder buffering1 if not isinstancecmd str raise typeerrorinvalid cmd type s expected string typecmd if mode not in r w raise valueerrorinvalid mode r mode if buffering 0 or buffering is none raise valueerrorpopen does not support unbuffered streams import subprocess if mode r proc subprocess popencmd shelltrue texttrue stdoutsubprocess pipe bufsizebuffering return wrapcloseproc stdout proc else proc subprocess popencmd shelltrue texttrue stdinsubprocess pipe bufsizebuffering return wrapcloseproc stdin proc helper for popen a proxy for a file whose close waits for the process class wrapclose def initself stream proc self stream stream self proc proc def closeself self stream close returncode self proc wait if returncode 0 return none if name nt return returncode else return returncode 8 shift left to match old behavior def enterself return self def exitself args self close def getattrself name return getattrself stream name def iterself return iterself stream all appendpopen supply os fdopen def fdopenfd moder buffering1 encodingnone args kwargs if not isinstancefd int raise typeerrorinvalid fd type s expected integer typefd import io if b not in mode encoding io textencodingencoding return io openfd mode buffering encoding args kwargs for testing purposes make sure the function is available when the c implementation exists def fspathpath if isinstancepath str bytes return path work from the object s type to match method resolution of other magic methods pathtype typepath try pathrepr pathtype fspathpath except attributeerror if hasattrpathtype fspath raise else raise typeerrorexpected str bytes or os pathlike object not pathtype name except typeerror if pathtype fspath is none raise typeerrorexpected str bytes or os pathlike object not pathtype name from none else raise if isinstancepathrepr str bytes return pathrepr else raise typeerrorexpected fspath to return str or bytes not formatpathtype name typepathrepr name if there is no c implementation make the pure python version the implementation as transparently as possible if not exists fspath fspath fspath fspath name fspath class pathlikeabc abc return the file system path representation of the object raise notimplementederror classmethod def subclasshookcls subclass if cls is pathlike return checkmethodssubclass fspath return notimplemented classgetitem classmethodgenericalias if name nt class addeddlldirectory def initself path cookie removedlldirectory self path path self cookie cookie self removedlldirectory removedlldirectory def closeself self removedlldirectoryself cookie self path none def enterself return self def exitself args self close def reprself if self path return addeddlldirectory r formatself path return addeddlldirectory def adddlldirectorypath import nt cookie nt adddlldirectorypath return addeddlldirectory path cookie nt removedlldirectory if exists schedgetaffinity and sys getcpucountconfig 0 def processcpucount return lenschedgetaffinity0 else just an alias to cpucount same docstring processcpucount cpucount os routines for nt or posix depending on what system we re on this exports all functions from posix or nt e g unlink stat etc os path is either posixpath or ntpath os name is either posix or nt os curdir is a string representing the current directory always os pardir is a string representing the parent directory always os sep is the or a most common pathname separator or 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 defpath is the default search path for executables os devnull is the file path of the null device dev null etc programs that import and use os stand a better chance of being portable between different platforms of course they must then only use functions that are defined by all platforms e g unlink and opendir and leave all pathname manipulation to os path e g split and join note more names are added to __all__ later any new dependencies of the os module and or changes in path separator requires updating importlib as well fstat always works mac os x10 3 some platforms don t support lchmod often the function exists anyway as a stub that always returns enosup or perhaps eopnotsupp no i don t know why that s a good design configure will detect this and reject it so have_lchmod still won t be defined on such platforms this is very helpful however sometimes platforms without a working lchmod do have fchmodat examples linux kernel 3 2 with glibc 2 15 openindiana 3 x and fchmodat has a flag that theoretically makes it behave like lchmod so in theory it would be a suitable replacement for lchmod but when lchmod doesn t work fchmodat s flag doesn t work either sadly configure isn t sophisticated enough to detect this condition it only determines whether or not fchmodat minimally works therefore we simply ignore fchmodat when deciding whether or not os chmod supports follow_symlinks just checking lchmod is sufficient after all if you have a working fchmodat your lchmod almost certainly works too _add have_fchmodat chmod mac os x10 3 python uses fixed values for the seek_ constants they are mapped to native constants if necessary in posixmodule c other possible seek values are directly imported from posixmodule c super directory utilities inspired by eric raymond the doc strings are mostly his makedirs name mode 0o777 exist_ok false super mkdir create a leaf directory and all intermediate ones works like mkdir except that any intermediate path segment not just the rightmost will be created if it does not exist if the target directory already exists raise an oserror if exist_ok is false otherwise no exception is raised this is recursive defeats race condition when another thread created the path xxx newdir exists if xxx newdir exists cannot rely on checking for eexist since the operating system could give priority to other errors like eacces or erofs removedirs name super rmdir remove a leaf directory and all empty intermediate ones works like rmdir except that if the leaf directory is successfully removed directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs errors during this latter phase are ignored they generally mean that a directory was not empty renames old new super rename create directories as necessary and delete any left empty works like rename except creation of any intermediate directories needed to make the new pathname good is attempted first after the rename directories corresponding to rightmost path segments of the old name will be pruned until either the whole path is consumed or a nonempty directory is found note this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file directory tree generator for each directory in the directory tree rooted at top including top itself but excluding and yields a 3 tuple dirpath dirnames filenames dirpath is a string the path to the directory dirnames is a list of the names of the subdirectories in dirpath including symlinks to directories and excluding and filenames is a list of the names of the non directory files in dirpath note that the names in the lists are just names with no path components to get a full path which begins with top to a file or directory in dirpath do os path join dirpath name if optional arg topdown is true or not specified the triple for a directory is generated before the triples for any of its subdirectories directories are generated top down if topdown is false the triple for a directory is generated after the triples for all of its subdirectories directories are generated bottom up when topdown is true the caller can modify the dirnames list in place e g via del or slice assignment and walk will only recurse into the subdirectories whose names remain in dirnames this can be used to prune the search or to impose a specific order of visiting modifying dirnames when topdown is false has no effect on the behavior of os walk since the directories in dirnames have already been generated by the time dirnames itself is generated no matter the value of topdown the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated by default errors from the os scandir call are ignored if optional arg onerror is specified it should be a function it will be called with one argument an oserror instance it can report the error to continue with the walk or raise the exception to abort the walk note that the filename is available as the filename attribute of the exception object by default os walk does not follow symbolic links to subdirectories on systems that support them in order to get this functionality set the optional argument followlinks to true caution if you pass a relative pathname for top don t change the current working directory between resumptions of walk walk never changes the current directory and assumes that the client doesn t either example import os from os path import join getsize for root dirs files in os walk python lib email print root consumes print sum getsize join root name for name in files end print bytes in len files non directory files if cvs in dirs dirs remove cvs don t visit cvs directories we may not have read permission for top in which case we can t get a list of the files the directory contains we suppress the exception here rather than blow up for a minor reason when say a thousand readable directories are still left to visit if is_dir raises an oserror consider the entry not to be a directory same behaviour as os path isdir bottom up traverse into sub directory but exclude symlinks to directories if followlinks is false if is_symlink raises an oserror consider the entry not to be a symbolic link same behaviour as os path islink yield before sub directory traversal if going top down traverse into sub directories bpo 23605 os path islink is used instead of caching entry is_symlink result during the loop on os scandir because the caller can replace the directory entry during the yield above yield after sub directory traversal if going bottom up traverse into sub directories directory tree generator this behaves exactly like walk except that it yields a 4 tuple dirpath dirnames filenames dirfd dirpath dirnames and filenames are identical to walk output and dirfd is a file descriptor referring to the directory dirpath the advantage of fwalk over walk is that it s safe against symlink races when follow_symlinks is false if dir_fd is not none it should be a file descriptor open to a directory and top should be relative top will then be relative to that directory dir_fd is always supported for fwalk caution since fwalk yields file descriptors those are only valid until the next iteration step so you should dup them if you want to keep them for a longer period example import os for root dirs files rootfd in os fwalk python lib email print root consumes end print sum os stat name dir_fd rootfd st_size for name in files end print bytes in len files non directory files if cvs in dirs dirs remove cvs don t visit cvs directories note to guard against symlink races we use the standard lstat open fstat trick note this uses o depth of the directory tree file descriptors if necessary it can be adapted to only require o 1 fds see issue 13734 add dangling symlinks ignore disappeared files execl file args execute the executable file with argument list args replacing the current process execle file args env execute the executable file with argument list args and environment env replacing the current process execlp file args execute the executable file which is searched for along path with argument list args replacing the current process execlpe file args env execute the executable file which is searched for along path with argument list args and environment env replacing the current process execvp file args execute the executable file which is searched for along path with argument list args replacing the current process args may be a list or tuple of strings execvpe file args env execute the executable file which is searched for along path with argument list args and environment env replacing the current process args may be a list or tuple of strings returns the sequence of directories that will be searched for the named executable similar to a shell when launching a process env must be an environment variable dict or none if env is none os environ will be used use a local import instead of a global import to limit the number of modules loaded at startup the os module is always loaded at startup by python it may also avoid a bootstrap issue b path get path and path get b path emit a byteswarning when using python b or python bb ignore the warning change environ to automatically call putenv and unsetenv raise keyerror with the original key value raise keyerror with the original key value list from dict object is an atomic operation where env var names must be uppercase where env var names can be mixed case unicode environ get an environment variable return none if it doesn t exist the optional second argument can specify an alternate default key default and the result are str bytes environ get an environment variable return none if it doesn t exist the optional second argument can specify an alternate default key default and the result are bytes encode filename an os pathlike bytes or str to the filesystem encoding with surrogateescape error handler return bytes unchanged on windows use strict error handler if the file system encoding is mbcs which is the default encoding does type checking of filename decode filename an os pathlike bytes or str from the filesystem encoding with surrogateescape error handler return str unchanged on windows use strict error handler if the file system encoding is mbcs which is the default encoding does type checking of filename supply spawn probably only for unix xxx should we support p_detach i suppose it could fork 2 and close the std i o streams also p_overlay is the same as execv internal helper func is the exec function to use child parent caller is responsible for waiting spawnv mode file args integer execute file with arguments from args in a subprocess if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it spawnve mode file args env integer execute file with arguments from args in a subprocess with the specified environment if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it note spawnvp e isn t currently supported on windows spawnvp mode file args integer execute file which is looked for along path with arguments from args in a subprocess if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it spawnvpe mode file args env integer execute file which is looked for along path with arguments from args in a subprocess with the supplied environment if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it these aren t supplied by the basic windows code but can be easily implemented in python spawnl mode file args integer execute file with arguments from args in a subprocess if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it spawnle mode file args env integer execute file with arguments from args in a subprocess with the supplied environment if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it at the moment windows doesn t implement spawnvp e so it won t have spawnlp e either spawnlp mode file args integer execute file which is looked for along path with arguments from args in a subprocess with the supplied environment if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it spawnlpe mode file args env integer execute file which is looked for along path with arguments from args in a subprocess with the supplied environment if mode p_nowait return the pid of the process if mode p_wait return the process s exit code if it exits normally otherwise return sig where sig is the signal that killed it vxworks has no user space shell provided as a result running command in a shell can t be supported supply os popen helper for popen a proxy for a file whose close waits for the process shift left to match old behavior supply os fdopen for testing purposes make sure the function is available when the c implementation exists return the path representation of a path like object if str or bytes is passed in it is returned unchanged otherwise the os pathlike interface is used to get the path representation if the path representation is not str or bytes typeerror is raised if the provided path is not str bytes or os pathlike typeerror is raised work from the object s type to match method resolution of other magic methods if there is no c implementation make the pure python version the implementation as transparently as possible abstract base class for implementing the file system path protocol return the file system path representation of the object add a path to the dll search path this search path is used when resolving dependencies for imported extension modules the module itself is resolved through sys path and also by ctypes remove the directory by calling close on the returned object or using it in a with statement get the number of cpus of the current process return the number of logical cpus usable by the calling thread of the current process return none if indeterminable just an alias to cpu_count same docstring
r


import abc
import sys
import stat as st

from _collections_abc import _check_methods

GenericAlias = type(list[int])

_names = sys.builtin_module_names


__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
           "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
           "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen",
           "extsep"]

def _exists(name):
    return name in globals()

def _get_exports_list(module):
    try:
        return list(module.__all__)
    except AttributeError:
        return [n for n in dir(module) if n[0] != '_']



if 'posix' in _names:
    name = 'posix'
    linesep = '\n'
    from posix import *
    try:
        from posix import _exit
        __all__.append('_exit')
    except ImportError:
        pass
    import posixpath as path

    try:
        from posix import _have_functions
    except ImportError:
        pass

    import posix
    __all__.extend(_get_exports_list(posix))
    del posix

elif 'nt' in _names:
    name = 'nt'
    linesep = '\r\n'
    from nt import *
    try:
        from nt import _exit
        __all__.append('_exit')
    except ImportError:
        pass
    import ntpath as path

    import nt
    __all__.extend(_get_exports_list(nt))
    del nt

    try:
        from nt import _have_functions
    except ImportError:
        pass

else:
    raise ImportError('no os specific module found')

sys.modules['os.path'] = path
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
    devnull)

del _names


if _exists("_have_functions"):
    _globals = globals()
    def _add(str, fn):
        if (fn in _globals) and (str in _have_functions):
            _set.add(_globals[fn])

    _set = set()
    _add("HAVE_FACCESSAT",  "access")
    _add("HAVE_FCHMODAT",   "chmod")
    _add("HAVE_FCHOWNAT",   "chown")
    _add("HAVE_FSTATAT",    "stat")
    _add("HAVE_FUTIMESAT",  "utime")
    _add("HAVE_LINKAT",     "link")
    _add("HAVE_MKDIRAT",    "mkdir")
    _add("HAVE_MKFIFOAT",   "mkfifo")
    _add("HAVE_MKNODAT",    "mknod")
    _add("HAVE_OPENAT",     "open")
    _add("HAVE_READLINKAT", "readlink")
    _add("HAVE_RENAMEAT",   "rename")
    _add("HAVE_SYMLINKAT",  "symlink")
    _add("HAVE_UNLINKAT",   "unlink")
    _add("HAVE_UNLINKAT",   "rmdir")
    _add("HAVE_UTIMENSAT",  "utime")
    supports_dir_fd = _set

    _set = set()
    _add("HAVE_FACCESSAT",  "access")
    supports_effective_ids = _set

    _set = set()
    _add("HAVE_FCHDIR",     "chdir")
    _add("HAVE_FCHMOD",     "chmod")
    _add("HAVE_FCHOWN",     "chown")
    _add("HAVE_FDOPENDIR",  "listdir")
    _add("HAVE_FDOPENDIR",  "scandir")
    _add("HAVE_FEXECVE",    "execve")
    _set.add(stat) 
    _add("HAVE_FTRUNCATE",  "truncate")
    _add("HAVE_FUTIMENS",   "utime")
    _add("HAVE_FUTIMES",    "utime")
    _add("HAVE_FPATHCONF",  "pathconf")
    if _exists("statvfs") and _exists("fstatvfs"): 
        _add("HAVE_FSTATVFS", "statvfs")
    supports_fd = _set

    _set = set()
    _add("HAVE_FACCESSAT",  "access")
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    _add("HAVE_FCHOWNAT",   "chown")
    _add("HAVE_FSTATAT",    "stat")
    _add("HAVE_LCHFLAGS",   "chflags")
    _add("HAVE_LCHMOD",     "chmod")
    if _exists("lchown"): 
        _add("HAVE_LCHOWN", "chown")
    _add("HAVE_LINKAT",     "link")
    _add("HAVE_LUTIMES",    "utime")
    _add("HAVE_LSTAT",      "stat")
    _add("HAVE_FSTATAT",    "stat")
    _add("HAVE_UTIMENSAT",  "utime")
    _add("MS_WINDOWS",      "stat")
    supports_follow_symlinks = _set

    del _set
    del _have_functions
    del _globals
    del _add





SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2




def makedirs(name, mode=0o777, exist_ok=False):
    
    head, tail = path.split(name)
    if not tail:
        head, tail = path.split(head)
    if head and tail and not path.exists(head):
        try:
            makedirs(head, exist_ok=exist_ok)
        except FileExistsError:
            
            pass
        cdir = curdir
        if isinstance(tail, bytes):
            cdir = bytes(curdir, 'ASCII')
        if tail == cdir:           
            return
    try:
        mkdir(name, mode)
    except OSError:
        
        
        if not exist_ok or not path.isdir(name):
            raise

def removedirs(name):
    
    rmdir(name)
    head, tail = path.split(name)
    if not tail:
        head, tail = path.split(head)
    while head and tail:
        try:
            rmdir(head)
        except OSError:
            break
        head, tail = path.split(head)

def renames(old, new):
    
    head, tail = path.split(new)
    if head and tail and not path.exists(head):
        makedirs(head)
    rename(old, new)
    head, tail = path.split(old)
    if head and tail:
        try:
            removedirs(head)
        except OSError:
            pass

__all__.extend(["makedirs", "removedirs", "renames"])

def walk(top, topdown=True, onerror=None, followlinks=False):
    
    sys.audit("os.walk", top, topdown, onerror, followlinks)

    stack = [fspath(top)]
    islink, join = path.islink, path.join
    while stack:
        top = stack.pop()
        if isinstance(top, tuple):
            yield top
            continue

        dirs = []
        nondirs = []
        walk_dirs = []

        
        
        
        
        
        try:
            scandir_it = scandir(top)
        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:
                    is_dir = entry.is_dir()
                except OSError:
                    
                    
                    is_dir = False

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

                if not topdown and is_dir:
                    
                    
                    if followlinks:
                        walk_into = True
                    else:
                        try:
                            is_symlink = entry.is_symlink()
                        except OSError:
                            
                            
                            
                            is_symlink = False
                        walk_into = not is_symlink

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

        if topdown:
            
            yield top, dirs, nondirs
            
            for dirname in reversed(dirs):
                new_path = join(top, dirname)
                
                
                
                
                if followlinks or not islink(new_path):
                    stack.append(new_path)
        else:
            
            stack.append((top, dirs, nondirs))
            
            for new_path in reversed(walk_dirs):
                stack.append(new_path)

__all__.append("walk")

if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd:

    def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None):
        
        sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd)
        top = fspath(top)
        
        
        if not follow_symlinks:
            orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
        topfd = open(top, O_RDONLY, dir_fd=dir_fd)
        try:
            if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and
                                    path.samestat(orig_st, stat(topfd)))):
                yield from _fwalk(topfd, top, isinstance(top, bytes),
                                  topdown, onerror, follow_symlinks)
        finally:
            close(topfd)

    def _fwalk(topfd, toppath, isbytes, topdown, onerror, follow_symlinks):
        
        
        

        scandir_it = scandir(topfd)
        dirs = []
        nondirs = []
        entries = None if topdown or follow_symlinks else []
        for entry in scandir_it:
            name = entry.name
            if isbytes:
                name = fsencode(name)
            try:
                if entry.is_dir():
                    dirs.append(name)
                    if entries is not None:
                        entries.append(entry)
                else:
                    nondirs.append(name)
            except OSError:
                try:
                    
                    if entry.is_symlink():
                        nondirs.append(name)
                except OSError:
                    pass

        if topdown:
            yield toppath, dirs, nondirs, topfd

        for name in dirs if entries is None else zip(dirs, entries):
            try:
                if not follow_symlinks:
                    if topdown:
                        orig_st = stat(name, dir_fd=topfd, follow_symlinks=False)
                    else:
                        assert entries is not None
                        name, entry = name
                        orig_st = entry.stat(follow_symlinks=False)
                dirfd = open(name, O_RDONLY, dir_fd=topfd)
            except OSError as err:
                if onerror is not None:
                    onerror(err)
                continue
            try:
                if follow_symlinks or path.samestat(orig_st, stat(dirfd)):
                    dirpath = path.join(toppath, name)
                    yield from _fwalk(dirfd, dirpath, isbytes,
                                      topdown, onerror, follow_symlinks)
            finally:
                close(dirfd)

        if not topdown:
            yield toppath, dirs, nondirs, topfd

    __all__.append("fwalk")

def execl(file, *args):
    
    execv(file, args)

def execle(file, *args):
    
    env = args[-1]
    execve(file, args[:-1], env)

def execlp(file, *args):
    
    execvp(file, args)

def execlpe(file, *args):
    
    env = args[-1]
    execvpe(file, args[:-1], env)

def execvp(file, args):
    
    _execvpe(file, args)

def execvpe(file, args, env):
    
    _execvpe(file, args, env)

__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])

def _execvpe(file, args, env=None):
    if env is not None:
        exec_func = execve
        argrest = (args, env)
    else:
        exec_func = execv
        argrest = (args,)
        env = environ

    if path.dirname(file):
        exec_func(file, *argrest)
        return
    saved_exc = None
    path_list = get_exec_path(env)
    if name != 'nt':
        file = fsencode(file)
        path_list = map(fsencode, path_list)
    for dir in path_list:
        fullname = path.join(dir, file)
        try:
            exec_func(fullname, *argrest)
        except (FileNotFoundError, NotADirectoryError) as e:
            last_exc = e
        except OSError as e:
            last_exc = e
            if saved_exc is None:
                saved_exc = e
    if saved_exc is not None:
        raise saved_exc
    raise last_exc


def get_exec_path(env=None):
    
    
    
    
    import warnings

    if env is None:
        env = environ

    
    
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", BytesWarning)

        try:
            path_list = env.get('PATH')
        except TypeError:
            path_list = None

        if supports_bytes_environ:
            try:
                path_listb = env[b'PATH']
            except (KeyError, TypeError):
                pass
            else:
                if path_list is not None:
                    raise ValueError(
                        "env cannot contain 'PATH' and b'PATH' keys")
                path_list = path_listb

            if path_list is not None and isinstance(path_list, bytes):
                path_list = fsdecode(path_list)

    if path_list is None:
        path_list = defpath
    return path_list.split(pathsep)



from _collections_abc import MutableMapping, Mapping

class _Environ(MutableMapping):
    def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue):
        self.encodekey = encodekey
        self.decodekey = decodekey
        self.encodevalue = encodevalue
        self.decodevalue = decodevalue
        self._data = data

    def __getitem__(self, key):
        try:
            value = self._data[self.encodekey(key)]
        except KeyError:
            
            raise KeyError(key) from None
        return self.decodevalue(value)

    def __setitem__(self, key, value):
        key = self.encodekey(key)
        value = self.encodevalue(value)
        putenv(key, value)
        self._data[key] = value

    def __delitem__(self, key):
        encodedkey = self.encodekey(key)
        unsetenv(encodedkey)
        try:
            del self._data[encodedkey]
        except KeyError:
            
            raise KeyError(key) from None

    def __iter__(self):
        
        keys = list(self._data)
        for key in keys:
            yield self.decodekey(key)

    def __len__(self):
        return len(self._data)

    def __repr__(self):
        formatted_items = ", ".join(
            f"{self.decodekey(key)!r}: {self.decodevalue(value)!r}"
            for key, value in self._data.items()
        )
        return f"environ({{{formatted_items}}})"

    def copy(self):
        return dict(self)

    def setdefault(self, key, value):
        if key not in self:
            self[key] = value
        return self[key]

    def __ior__(self, other):
        self.update(other)
        return self

    def __or__(self, other):
        if not isinstance(other, Mapping):
            return NotImplemented
        new = dict(self)
        new.update(other)
        return new

    def __ror__(self, other):
        if not isinstance(other, Mapping):
            return NotImplemented
        new = dict(other)
        new.update(self)
        return new

def _createenviron():
    if name == 'nt':
        
        def check_str(value):
            if not isinstance(value, str):
                raise TypeError("str expected, not %s" % type(value).__name__)
            return value
        encode = check_str
        decode = str
        def encodekey(key):
            return encode(key).upper()
        data = {}
        for key, value in environ.items():
            data[encodekey(key)] = value
    else:
        
        encoding = sys.getfilesystemencoding()
        def encode(value):
            if not isinstance(value, str):
                raise TypeError("str expected, not %s" % type(value).__name__)
            return value.encode(encoding, 'surrogateescape')
        def decode(value):
            return value.decode(encoding, 'surrogateescape')
        encodekey = encode
        data = environ
    return _Environ(data,
        encodekey, decode,
        encode, decode)


environ = _createenviron()
del _createenviron


def getenv(key, default=None):
    
    return environ.get(key, default)

supports_bytes_environ = (name != 'nt')
__all__.extend(("getenv", "supports_bytes_environ"))

if supports_bytes_environ:
    def _check_bytes(value):
        if not isinstance(value, bytes):
            raise TypeError("bytes expected, not %s" % type(value).__name__)
        return value

    
    environb = _Environ(environ._data,
        _check_bytes, bytes,
        _check_bytes, bytes)
    del _check_bytes

    def getenvb(key, default=None):
        
        return environb.get(key, default)

    __all__.extend(("environb", "getenvb"))

def _fscodec():
    encoding = sys.getfilesystemencoding()
    errors = sys.getfilesystemencodeerrors()

    def fsencode(filename):
        
        filename = fspath(filename)  
        if isinstance(filename, str):
            return filename.encode(encoding, errors)
        else:
            return filename

    def fsdecode(filename):
        
        filename = fspath(filename)  
        if isinstance(filename, bytes):
            return filename.decode(encoding, errors)
        else:
            return filename

    return fsencode, fsdecode

fsencode, fsdecode = _fscodec()
del _fscodec


if _exists("fork") and not _exists("spawnv") and _exists("execv"):

    P_WAIT = 0
    P_NOWAIT = P_NOWAITO = 1

    __all__.extend(["P_WAIT", "P_NOWAIT", "P_NOWAITO"])

    
    
    

    def _spawnvef(mode, file, args, env, func):
        
        if not isinstance(args, (tuple, list)):
            raise TypeError('argv must be a tuple or a list')
        if not args or not args[0]:
            raise ValueError('argv first element cannot be empty')
        pid = fork()
        if not pid:
            
            try:
                if env is None:
                    func(file, args)
                else:
                    func(file, args, env)
            except:
                _exit(127)
        else:
            
            if mode == P_NOWAIT:
                return pid 
            while 1:
                wpid, sts = waitpid(pid, 0)
                if WIFSTOPPED(sts):
                    continue

                return waitstatus_to_exitcode(sts)

    def spawnv(mode, file, args):
        
        return _spawnvef(mode, file, args, None, execv)

    def spawnve(mode, file, args, env):
        
        return _spawnvef(mode, file, args, env, execve)

    

    def spawnvp(mode, file, args):
        
        return _spawnvef(mode, file, args, None, execvp)

    def spawnvpe(mode, file, args, env):
        
        return _spawnvef(mode, file, args, env, execvpe)


    __all__.extend(["spawnv", "spawnve", "spawnvp", "spawnvpe"])


if _exists("spawnv"):
    
    

    def spawnl(mode, file, *args):
        
        return spawnv(mode, file, args)

    def spawnle(mode, file, *args):
        
        env = args[-1]
        return spawnve(mode, file, args[:-1], env)


    __all__.extend(["spawnl", "spawnle"])


if _exists("spawnvp"):
    
    
    def spawnlp(mode, file, *args):
        
        return spawnvp(mode, file, args)

    def spawnlpe(mode, file, *args):
        
        env = args[-1]
        return spawnvpe(mode, file, args[:-1], env)


    __all__.extend(["spawnlp", "spawnlpe"])



if sys.platform != 'vxworks':
    
    def popen(cmd, mode="r", buffering=-1):
        if not isinstance(cmd, str):
            raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
        if mode not in ("r", "w"):
            raise ValueError("invalid mode %r" % mode)
        if buffering == 0 or buffering is None:
            raise ValueError("popen() does not support unbuffered streams")
        import subprocess
        if mode == "r":
            proc = subprocess.Popen(cmd,
                                    shell=True, text=True,
                                    stdout=subprocess.PIPE,
                                    bufsize=buffering)
            return _wrap_close(proc.stdout, proc)
        else:
            proc = subprocess.Popen(cmd,
                                    shell=True, text=True,
                                    stdin=subprocess.PIPE,
                                    bufsize=buffering)
            return _wrap_close(proc.stdin, proc)

    
    class _wrap_close:
        def __init__(self, stream, proc):
            self._stream = stream
            self._proc = proc
        def close(self):
            self._stream.close()
            returncode = self._proc.wait()
            if returncode == 0:
                return None
            if name == 'nt':
                return returncode
            else:
                return returncode << 8  
        def __enter__(self):
            return self
        def __exit__(self, *args):
            self.close()
        def __getattr__(self, name):
            return getattr(self._stream, name)
        def __iter__(self):
            return iter(self._stream)

    __all__.append("popen")


def fdopen(fd, mode="r", buffering=-1, encoding=None, *args, **kwargs):
    if not isinstance(fd, int):
        raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
    import io
    if "b" not in mode:
        encoding = io.text_encoding(encoding)
    return io.open(fd, mode, buffering, encoding, *args, **kwargs)




def _fspath(path):
    
    if isinstance(path, (str, bytes)):
        return path

    
    
    path_type = type(path)
    try:
        path_repr = path_type.__fspath__(path)
    except AttributeError:
        if hasattr(path_type, '__fspath__'):
            raise
        else:
            raise TypeError("expected str, bytes or os.PathLike object, "
                            "not " + path_type.__name__)
    except TypeError:
        if path_type.__fspath__ is None:
            raise TypeError("expected str, bytes or os.PathLike object, "
                            "not " + path_type.__name__) from None
        else:
            raise
    if isinstance(path_repr, (str, bytes)):
        return path_repr
    else:
        raise TypeError("expected {}.__fspath__() to return str or bytes, "
                        "not {}".format(path_type.__name__,
                                        type(path_repr).__name__))



if not _exists('fspath'):
    fspath = _fspath
    fspath.__name__ = "fspath"


class PathLike(abc.ABC):

    

    __slots__ = ()

    @abc.abstractmethod
    def __fspath__(self):
        
        raise NotImplementedError

    @classmethod
    def __subclasshook__(cls, subclass):
        if cls is PathLike:
            return _check_methods(subclass, '__fspath__')
        return NotImplemented

    __class_getitem__ = classmethod(GenericAlias)


if name == 'nt':
    class _AddedDllDirectory:
        def __init__(self, path, cookie, remove_dll_directory):
            self.path = path
            self._cookie = cookie
            self._remove_dll_directory = remove_dll_directory
        def close(self):
            self._remove_dll_directory(self._cookie)
            self.path = None
        def __enter__(self):
            return self
        def __exit__(self, *args):
            self.close()
        def __repr__(self):
            if self.path:
                return "<AddedDllDirectory({!r})>".format(self.path)
            return "<AddedDllDirectory()>"

    def add_dll_directory(path):
        
        import nt
        cookie = nt._add_dll_directory(path)
        return _AddedDllDirectory(
            path,
            cookie,
            nt._remove_dll_directory
        )


if _exists('sched_getaffinity') and sys._get_cpu_count_config() < 0:
    def process_cpu_count():
        
        return len(sched_getaffinity(0))
else:
    
    process_cpu_count = cpu_count