From cff3f1f86dfc8765f1dffda70383064df8b2b265 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Tue, 22 Oct 2019 18:27:23 +0300 Subject: [PATCH] Enable pyi-stubs for `os` (PY-23258, PY-21395, PY-21394, PY-21397, PY-17420, PY-28984, PY-27584) GitOrigin-RevId: 34ecf07bc68276e62315d01f3c3347f79026ec65 --- .../helpers/python-skeletons/os/__init__.py | 1291 ----------------- python/helpers/python-skeletons/os/path.py | 293 ---- .../helpers/typeshed/stdlib/2/os/__init__.pyi | 368 +++++ python/helpers/typeshed/stdlib/2/os/path.pyi | 177 +++ .../helpers/typeshed/stdlib/2/os2emxpath.pyi | 177 +++ python/helpers/typeshed/stdlib/2/posix.pyi | 201 +++ .../typeshed/stdlib/2and3/genericpath.pyi | 21 + .../helpers/typeshed/stdlib/2and3/macpath.pyi | 171 +++ .../helpers/typeshed/stdlib/2and3/ntpath.pyi | 177 +++ .../typeshed/stdlib/2and3/posixpath.pyi | 177 +++ .../helpers/typeshed/stdlib/3/os/__init__.pyi | 663 +++++++++ python/helpers/typeshed/stdlib/3/os/path.pyi | 177 +++ python/helpers/typeshed/stdlib/3/posix.pyi | 131 ++ .../src/com/jetbrains/python/PyNames.java | 1 + .../typing/PyTypingTypeProvider.java | 17 - .../documentation/PyTypeModelBuilder.java | 31 +- .../jetbrains/python/psi/types/PyABCUtil.java | 2 +- .../completion/PythonCompletionTest.java | 11 - python/src/META-INF/python-core-common.xml | 1 - .../stdlib/PyStdlibModuleMembersProvider.java | 32 - .../stdlib/PyStdlibTypeProvider.java | 2 +- python/testData/MockSdk3.7/Lib/genericpath.py | 151 -- python/testData/MockSdk3.7/Lib/ntpath.py | 669 --------- python/testData/MockSdk3.7/Lib/os.py | 1078 -------------- python/testData/completion/osPath/a.after.py | 3 - python/testData/completion/osPath/a.py | 3 - python/testData/completion/osPath/ntpath.py | 1 - python/testData/completion/osPath/os.py | 1 - .../testData/completion/osPath/posixpath.py | 1 - .../OsPopenClose/a.py | 2 - .../OsPopenClose/os.py | 6 - ....py => PathLikePassedToStdlibFunctions.py} | 8 +- .../PathLikePassedToStdlibFunctions/os.py | 30 - .../posixpath.py | 2 - .../osAttributesFromPosixPathAndNTPath/a.py | 2 - .../lib/ntpath.py | 0 .../lib/os.py | 4 - .../lib/posixpath.py | 0 .../python/PyMultiFileResolveTest.java | 23 - ...3NoneFunctionAssignmentInspectionTest.java | 5 - .../Py3TypeCheckerInspectionTest.java | 2 +- .../quickFixes/PyAddImportQuickFixTest.java | 27 +- .../jetbrains/python/tools/PyTypeShedSync.kts | 7 + 43 files changed, 2497 insertions(+), 3649 deletions(-) delete mode 100644 python/helpers/python-skeletons/os/__init__.py delete mode 100644 python/helpers/python-skeletons/os/path.py create mode 100644 python/helpers/typeshed/stdlib/2/os/__init__.pyi create mode 100644 python/helpers/typeshed/stdlib/2/os/path.pyi create mode 100644 python/helpers/typeshed/stdlib/2/os2emxpath.pyi create mode 100644 python/helpers/typeshed/stdlib/2/posix.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/genericpath.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/macpath.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/ntpath.pyi create mode 100644 python/helpers/typeshed/stdlib/2and3/posixpath.pyi create mode 100644 python/helpers/typeshed/stdlib/3/os/__init__.pyi create mode 100644 python/helpers/typeshed/stdlib/3/os/path.pyi create mode 100644 python/helpers/typeshed/stdlib/3/posix.pyi delete mode 100644 python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibModuleMembersProvider.java delete mode 100644 python/testData/MockSdk3.7/Lib/genericpath.py delete mode 100644 python/testData/MockSdk3.7/Lib/ntpath.py delete mode 100644 python/testData/MockSdk3.7/Lib/os.py delete mode 100644 python/testData/completion/osPath/a.after.py delete mode 100644 python/testData/completion/osPath/a.py delete mode 100644 python/testData/completion/osPath/ntpath.py delete mode 100644 python/testData/completion/osPath/os.py delete mode 100644 python/testData/completion/osPath/posixpath.py delete mode 100644 python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/a.py delete mode 100644 python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/os.py rename python/testData/inspections/PyTypeCheckerInspection/{PathLikePassedToStdlibFunctions/a.py => PathLikePassedToStdlibFunctions.py} (66%) delete mode 100644 python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/os.py delete mode 100644 python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/posixpath.py delete mode 100644 python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/a.py delete mode 100644 python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/ntpath.py delete mode 100644 python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/os.py delete mode 100644 python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/posixpath.py diff --git a/python/helpers/python-skeletons/os/__init__.py b/python/helpers/python-skeletons/os/__init__.py deleted file mode 100644 index 058383bad3db..000000000000 --- a/python/helpers/python-skeletons/os/__init__.py +++ /dev/null @@ -1,1291 +0,0 @@ -"""Skeleton for 'os' stdlib module.""" - - -from __future__ import unicode_literals -import io -import os -import subprocess -import sys - - -error = OSError - - -def ctermid(): - """Return the filename corresponding to the controlling terminal of the - process. - - :rtype: string - """ - return '' - - -def getegid(): - """Return the effective group id of the current process. - - :rtype: int - """ - return 0 - - -def geteuid(): - """Return the current process's effective user id. - - :rtype: int - """ - return 0 - - -def getgid(): - """Return the real group id of the current process. - - :rtype: int - """ - return 0 - - -def getgroups(): - """Return list of supplemental group ids associated with the current - process. - - :rtype: list[int] - """ - return [] - - -if sys.version_info >= (2, 7): - def initgroups(username, gid): - """Call the system initgroups() to initialize the group access list - with all of the groups of which the specified username is a member, - plus the specified group id. - - :type username: string - :type gid: int - :rtype: None - """ - pass - - -def getlogin(): - """Return the name of the user logged in on the controlling terminal of the - process. - - :rtype: string - """ - return '' - - -def getpgid(pid): - """Return the process group id of the process with process id pid. - - :type pid: int - :rtype: int - """ - return 0 - - -def getpgrp(): - """Return the id of the current process group. - - :rtype: int - """ - return 0 - - -def getpid(): - """Return the current process id. - - :rtype: int - """ - return 0 - - -def getppid(): - """Return the parent's process id. - - :rtype: int - """ - return 0 - - -if sys.version_info >= (2, 7): - def getresuid(): - """Return a tuple (ruid, euid, suid) denoting the current process's - real, effective, and saved user ids. - - :rtype: (int, int, int) - """ - return 0, 0, 0 - - def getresgid(): - """Return a tuple (rgid, egid, sgid) denoting the current process's - real, effective, and saved group ids. - - :rtype: (int, int, int) - """ - return 0, 0, 0 - - -def getuid(): - """Return the current process's user id. - - :rtype: int - """ - return 0 - - -def getenv(varname, value=None): - """Return the value of the environment variable varname if it exists, or - value if it doesn't. - - :type varname: string - :type value: T - :rtype: string | T - """ - pass - - -def putenv(varname, value): - """Set the environment variable named varname to the string value. - - :type varname: string - :rtype: None - """ - pass - - -def setegid(egid): - """Set the current process's effective group id. - - :type egid: int - :rtype: None - """ - pass - - -def seteuid(euid): - """Set the current process's effective user id. - - :type euid: int - :rtype: None - """ - pass - - -def setgid(gid): - """Set the current process' group id. - - :type gid: int - :rtype: None - """ - pass - - -def setgroups(groups): - """Set the list of supplemental group ids associated with the current - process to groups. - - :type groups: collections.Iterable[int] - :rtype: None - """ - pass - - -def setpgid(pid, pgrp): - """Call the system call setpgid() to set the process group id of the - process with id pid to the process group with id pgrp. - - :type pid: int - :type pgrp: int - :rtype: None - """ - pass - - -def setregid(rgid, egid): - """Set the current process's real and effective group ids. - - :type rgid: int - :type egid: int - :rtype: None - """ - pass - - -if sys.version_info >= (2, 7): - def setresgid(rgid, egid, sgid): - """Set the current process's real, effective, and saved group ids. - - :type rgid: int - :type egid: int - :type sgid: int - :rtype: None - """ - pass - - def setresuid(ruid, euid, suid): - """Set the current process's real, effective, and saved user ids. - - :type ruid: int - :type euid: int - :type suid: int - :rtype None - """ - pass - - -def setreuid(ruid, euid): - """Set the current process's real and effective user ids. - - :type ruid: int - :type euid: int - :rtype None - """ - pass - - -def setsid(): - """Call the system call getsid(). - - :rtype: None - """ - pass - - -def setuid(uid): - """Set the current process's user id. - - :type uid: int - :rtype: None - """ - pass - - -def strerror(code): - """Return the error message corresponding to the error code in code. - - :type code: int - :rtype: string - """ - return '' - - -def umask(mask): - """Set the current numeric umask and return the previous umask. - - :type mask: int - :rtype: int - """ - return 0 - - -def uname(): - """Return a 5-tuple containing information identifying the current - operating system. - - :rtype: (string, string, string, string, string) - """ - return '', '', '', '', '' - - -def unsetenv(varname): - """Unset (delete) the environment variable named varname. - - :type varname: string - :rtype: None - """ - pass - - -def fdopen(fd, mode='r', bufsize=-1): - """Return an open file object connected to the file descriptor fd. - - :type fd: int - :type mode: string - :type bufsize: int - :rtype: file - """ - return file() - - -def popen(command, mode='r', bufsize=-1): - """Open a pipe to or from command. - - :type command: string - :type mode: string - :type bufsize: int - :rtype: os._wrap_close - """ - pass - - -class _wrap_close(io.TextIOWrapper[unicode]): - def __init__(self, stream, proc): - """ - :type stream: io.TextIOWrapper[unicode] - :type proc: subprocess.Popen - """ - pass - - def close(self): - """ - :rtype: int | None - """ - pass - - def __enter__(self): - """ - :rtype: os._wrap_close - """ - pass - - def __exit__(self, *args): - pass - - def __iter__(self): - """ - :rtype: collections.Iterator[unicode] - """ - pass - - -def tmpfile(): - """Return a new file object opened in update mode (w+b). - - :rtype: io.FileIO[bytes] - """ - pass - - -def popen2(cmd, mode='r', bufsize=-1): - """Execute cmd as a sub-process and return the file objects (child_stdin, - child_stdout). - - :type cmd: string - :type mode: string - :type bufsize: int - :rtype: (io.FileIO[bytes], io.FileIO[bytes]) - """ - pass - - -def popen3(cmd, mode='r', bufsize=-1): - """Execute cmd as a sub-process and return the file objects (child_stdin, - child_stdout, child_stderr). - - :type cmd: string - :type mode: string - :type bufsize: int - :rtype: (io.FileIO[bytes], io.FileIO[bytes], io.FileIO[bytes]) - """ - pass - - -def popen4(cmd, mode='r', bufsize=-1): - """Execute cmd as a sub-process and return the file objects (child_stdin, - child_stdout_and_stderr). - - :type cmd: string - :type mode: string - :type bufsize: int - :rtype: (io.FileIO[bytes], io.FileIO[bytes]) - """ - pass - - -def close(fd): - """Close file descriptor fd. - - :type fd: int - :rtype: None - """ - pass - - -if sys.version_info >= (2, 6): - def closerange(fd_low, fd_high): - """Close all file descriptors from fd_low (inclusive) to fd_high - (exclusive), ignoring errors. - - :type fd_low: int - :type fd_high: int - :rtype: None - """ - pass - - -def dup(fd): - """Return a duplicate of file descriptor fd. - - :type fd: int - :rtype: int - """ - return 0 - - -def dup2(fd, fd2): - """Duplicate file descriptor fd to fd2, closing the latter first if - necessary. - - :type fd: int - :type fd2: int - :rtype: None - """ - pass - - -if sys.version_info >= (2, 6): - def fchmod(fd, mode): - """Change the mode of the file given by fd to the numeric mode. - - :type fd: int - :type mode: int - :rtype: None - """ - pass - - def fchown(fd, uid, gid): - """Change the owner and group id of the file given by fd to the numeric - uid and gid. - - :type fd: int - :type uid: int - :type gid: int - :rtype: None - """ - pass - - -def fdatasync(fd): - """Force write of file with filedescriptor fd to disk. - - :type fd: int - :rtype: None - """ - pass - - -def fpathconf(fd, name): - """Return system configuration information relevant to an open file. - - :type fd: int - :type name: string | int - """ - pass - - -def fstat(fd): - """Return status for file descriptor fd, like stat(). - - :type fd: int - :rtype: os.stat_result - """ - pass - - -def fstatvfs(fd): - """Return information about the filesystem containing the file associated - with file descriptor fd, like statvfs(). - - :type fd: int - :rtype: os.statvfs_result - """ - pass - - -def fsync(fd): - """Force write of file with filedescriptor fd to disk. - - :type fd: int - :rtype: None - """ - pass - - -def ftruncate(fd, length): - """Truncate the file corresponding to file descriptor fd, so that it is at - most length bytes in size. - - :type fd: int - :type length: numbers.Integral - :rtype: None - """ - pass - - -def isatty(fd): - """Return True if the file descriptor fd is open and connected to a - tty(-like) device, else False. - - :type fd: int - :rtype: bool - """ - return False - - -def lseek(fd, pos, how): - """Set the current position of file descriptor fd to position pos, modified - by how. - - :type fd: int - :type pos: numbers.Integral - :type how: int - :rtype: None - """ - pass - - -def open(file, flags, mode=0o777): - """Open the file file and set various flags according to flags and possibly - its mode according to mode. - - :type file: string - :type flags: int - :type mode: int - :rtype: int - """ - return 0 - - -def openpty(): - """Open a new pseudo-terminal pair. - - :rtype: (int, int) - """ - return 0, 0 - - -def pipe(): - """Create a pipe. - - :rtype: (int, int) - """ - return 0, 0 - - -def read(fd, n): - """Read at most n bytes from file descriptor fd. - - :type fd: int - :type n: numbers.Integral - :rtype: bytes - """ - pass - - -def tcgetpgrp(fd): - """Return the process group associated with the terminal given by fd. - - :type fd: int - :rtype: int - """ - return 0 - - -def tcsetpgrp(fd, pg): - """Set the process group associated with the terminal given by fd to pg. - - :type fd: int - :type pg: int - :rtype: None - """ - pass - - -def ttyname(fd): - """Return a string which specifies the terminal device associated with file - descriptor fd. - - :type fd: int - :rtype: string - """ - return '' - - -def write(fd, str): - """Write the string str to file descriptor fd. Return the number of bytes - actually written. - - :type fd: int - :type str: bytes - :rtype: int - """ - return 0 - - -def access(path, mode): - """Use the real uid/gid to test for access to path. - - :type path: bytes | unicode - :type mode: int - :rtype: bool - """ - return False - - -def chdir(path): - """Change the current working directory to path. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def fchdir(fd): - """Change the current working directory to the directory represented by the - file descriptor fd. - - :type fd: int - :rtype: None - """ - pass - - -def getcwd(): - """Return a string representing the current working directory. - - :rtype: string - """ - return '' - - -if sys.version_info < (3, 0): - def getcwdu(): - """Return a Unicode object representing the current working directory. - - :rtype: unicode - """ - return '' - - -def chflags(path, flags): - """Set the flags of path to the numeric flags. - - :type path: bytes | unicode - :type flags: int - :rtype: None - """ - pass - - -def chroot(path): - """Change the root directory of the current process to path. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def chmod(path, mode): - """Change the mode of path to the numeric mode. - - :type path: bytes | unicode - :type mode: int - :rtype: None - """ - pass - - -def chown(path, uid, gid): - """Change the owner and group id of path to the numeric uid and gid. - - :type path: bytes | unicode - :type uid: int - :type gid: int - :rtype: None - """ - pass - - -def lchflags(path, flags): - """Set the flags of path to the numeric flags, like chflags(), but do not - follow symbolic links. - - :type path: bytes | unicode - :type flags: int - :rtype: None - """ - pass - - -def lchmod(path, mode): - """Change the mode of path to the numeric mode. If path is a symlink, this - affects the symlink rather than the target. - - :type path: bytes | unicode - :type mode: int - :rtype: None - """ - pass - - -def lchown(path, uid, gid): - """Change the owner and group id of path to the numeric uid and gid. This - function will not follow symbolic links. - - :type path: bytes | unicode - :type uid: int - :type gid: int - :rtype: None - """ - pass - - -def link(source, link_name): - """Create a hard link pointing to source named link_name. - - :type source: bytes | unicode - :type link_name: bytes | unicode - :rtype: None - """ - pass - - -def listdir(path): - """Return a list containing the names of the entries in the directory given - by path. - - :type path: T <= bytes | unicode - :rtype: list[T] - """ - return [] - - -def lstat(path): - """Perform the equivalent of an lstat() system call on the given path. - Similar to stat(), but does not follow symbolic links. - - :type path: bytes | unicode - :rtype: os.stat_result - """ - pass - - -def mkfifo(path, mode=0o666): - """Create a FIFO (a named pipe) named path with numeric mode mode. - - :type path: bytes | unicode - :type mode: int - :rtype: None - """ - pass - - -def mknod(filename, mode=0o600, device=0): - """Create a filesystem node (file, device special file or named pipe) named - filename. - - :type filename: bytes | unicode - :type mode: int - :type device: int - :rtype: None - """ - pass - - -def major(device): - """Extract the device major number from a raw device number (usually the - st_dev or st_rdev field from stat). - - :type device: int - :rtype: int - """ - return 0 - - -def minor(device): - """Extract the device minor number from a raw device number (usually the - st_dev or st_rdev field from stat). - - :type device: int - :rtype: int - """ - return 0 - - -def makedev(major, minor): - """Compose a raw device number from the major and minor device numbers. - - :type major: int - :type minor: int - :rtype: int - """ - return 0 - - -def mkdir(path, mode=0o777): - """Create a directory named path with numeric mode mode. - - :type path: bytes | unicode - :type mode: int - :rtype: None - """ - pass - - -def makedirs(path, mode=0o777, exist_ok=False): - """Recursive directory creation function. - - :type path: bytes | unicode - :type mode: int - :type exist_ok: int - :rtype: None - """ - pass - - -def pathconf(path, name): - """Return system configuration information relevant to a named file. - - :type path: bytes | unicode - :type name: int | string - """ - pass - - -def readlink(path): - """Return a string representing the path to which the symbolic link points. - - :type path: T <= bytes | unicode - :rtype: T - """ - return path - - -def remove(path): - """Remove (delete) the file path. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def removedirs(path): - """Remove directories recursively. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def rename(src, dst): - """Rename the file or directory src to dst. - - :type src: bytes | unicode - :type dst: bytes | unicode - :rtype: None - """ - pass - - -def renames(old, new): - """Recursive directory or file renaming function. - - :type old: bytes | unicode - :type new: bytes | unicode - :rtype: None - """ - pass - - -def rmdir(path): - """Remove (delete) the directory path. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def stat(path, dir_fd=None, follow_symlinks=True): - """Perform the equivalent of a stat() system call on the given path. - - :type path: bytes | unicode | int - :type dir_fd: int | None - :type follow_symlinks: bool | None - :rtype: os.stat_result - """ - pass - - -def stat_float_times(newvalue=None): - """Determine whether stat_result represents time stamps as float objects. - - :type newvalue: bool | None - :rtype: bool - """ - return False - - -def statvfs(path): - """Perform a statvfs() system call on the given path. - - :type path: bytes | unicode - :rtype: os.statvfs_result - """ - pass - - -def symlink(source, link_name, target_is_directory=False, dir_fd=None): - """Create a symbolic link pointing to source named link_name. - - :type source: bytes | unicode - :type link_name: bytes| unicode - :type target_is_directory: bool - :type dir_fd: int | None - :rtype: None - """ - pass - - -def tempnam(dir=None, prefix=None): - """Return a unique path name that is reasonable for creating a temporary - file. - - :type dir: bytes | unicode - :type prefix: bytes | unicode - :rtype: string - """ - return '' - - -def tmpnam(): - """Return a unique path name that is reasonable for creating a temporary - file. - - :rtype: string - """ - return '' - - -def unlink(path): - """Remove (delete) the file path. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def utime(path, times): - """Set the access and modified times of the file specified by path. - - :type path: bytes | unicode - :type times: (numbers.Real, numbers.Real) | None - :rtype: None - """ - pass - - -def walk(top, topdown=True, onerror=None, followlinks=False): - """Generate the file names in a directory tree by walking the tree either - top-down or bottom-up. - - :type top: T <= bytes | unicode - :type topdown: bool - :type onerror: ((Exception) -> None) | None - :rtype: collections.Iterator[(T, list[T], list[T])] - """ - return [] - - -def execl(path, *args): - """Execute a new program, replacing the current process. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def execle(path, *args): - """Execute a new program, replacing the current process. - - :type path: bytes | unicode - :rtype: None - """ - pass - - -def execlp(file, *args): - """Execute a new program, replacing the current process. - - :type file: bytes | unicode - :rtype: None - """ - pass - - -def execlpe(file, *args): - """Execute a new program, replacing the current process. - - :type file: bytes | unicode - :rtype: None - """ - pass - - -def execv(path, args): - """Execute a new program, replacing the current process. - - :type path: bytes | unicode - :type args: collections.Iterable - :rtype: None - """ - pass - - -def execve(path, args, env): - """Execute a new program, replacing the current process. - - :type path: bytes | unicode - :type args: collections.Iterable - :type env: collections.Mapping - :rtype: None - """ - pass - - -def execvp(file, args): - """Execute a new program, replacing the current process. - - :type file: bytes | unicode - :type args: collections.Iterable - :rtype: None - """ - pass - - -def execvpe(file, args, env): - """Execute a new program, replacing the current process. - - :type file: bytes | unicode - :type args: collections.Iterable - :type env: collections.Mapping - :rtype: None - """ - pass - - -def _exit(n): - """Exit the process with status n, without calling cleanup handlers, - flushing stdio buffers, etc. - - :type n: int - :rtype: None - """ - pass - - -def fork(): - """Fork a child process. - - :rtype: int - """ - return 0 - - -def forkpty(): - """Fork a child process, using a new pseudo-terminal as the child's - controlling terminal. - - :rtype: (int, int) - """ - return 0, 0 - - -def kill(pid, sig): - """Send signal sig to the process pid. - - :type pid: int - :type sig: int - :rtype: None - """ - pass - - -def killpg(pgid, sig): - """Send the signal sig to the process group pgid. - - :type pgid: int - :type sig: int - :rtype: None - """ - pass - - -def nice(increment): - """Add increment to the process's "niceness". - - :type increment: int - :rtype: int - """ - return 0 - - -def plock(op): - """Lock program segments into memory. - - :rtype: None - """ - pass - - -def spawnl(mode, path, *args): - """Execute the program path in a new process. - - :type mode: int - :type path: bytes | unicode - :rtype: int - """ - return 0 - - -def spawnle(mode, path, *args): - """Execute the program path in a new process. - - :type mode: int - :type path: bytes | unicode - :rtype: int - """ - return 0 - - -def spawnlp(mode, file, *args): - """Execute the program path in a new process. - - :type mode: int - :type file: bytes | unicode - :rtype: int - """ - return 0 - - -def spawnlpe(mode, file, *args): - """Execute the program path in a new process. - - :type mode: int - :type file: bytes | unicode - :rtype: int - """ - return 0 - - -def spawnv(mode, path, args): - """Execute the program path in a new process. - - :type mode: int - :type path: bytes | unicode - :type args: collections.Iterable - :rtype: int - """ - return 0 - - -def spawnve(mode, path, args, env): - """Execute the program path in a new process. - - :type mode: int - :type path: bytes | unicode - :type args: collections.Iterable - :type env: collections.Mapping - :rtype: int - """ - return 0 - - -def spawnvp(mode, file, args): - """Execute the program path in a new process. - - :type mode: int - :type file: bytes | unicode - :type args: collections.Iterable - :rtype: int - """ - return 0 - - -def spawnvpe(mode, file, args, env): - """Execute the program path in a new process. - - :type mode: int - :type file: bytes | unicode - :type args: collections.Iterable - :type env: collections.Mapping - :rtype: int - """ - return 0 - - -def system(command): - """Execute the command (a string) in a subshell. - - :type command: bytes | unicode - :rtype: int - """ - return 0 - - -def times(): - """Return a 5-tuple of floating point numbers indicating accumulated - (processor or other) times, in seconds. - - :rtype: (float, float, float, float, float) - """ - return 0.0, 0.0, 0.0, 0.0, 0.0 - - -def wait(): - """Wait for completion of a child process, and return a tuple containing - its pid and exit status indication - - :rtype: (int, int) - """ - return 0, 0 - - -def waitpid(pid, options): - """Wait for completion of a child process given by process id pid, and - return a tuple containing its process id and exit status indication. - - :type pid: int - :type options: int - :rtype: (int, int) - """ - return 0, 0 - - -def wait3(options): - """Similar to waitpid(), except no process id argument is given and a - 3-element tuple containing the child's process id, exit status indication, - and resource usage information is returned. - - :type options: int - :rtype: (int, int, resource.struct_rusage) - """ - pass - - -def wait4(pid, options): - """Similar to waitpid(), except a 3-element tuple, containing the child's - process id, exit status indication, and resource usage information is - returned. - - :type pid: int - :type options: int - :rtype: (int, int, resource.struct_rusage) - """ - pass - - -def urandom(n): - """Return a string of n random bytes suitable for cryptographic use. - - :type n: int - :rtype: bytes - """ - return b'' \ No newline at end of file diff --git a/python/helpers/python-skeletons/os/path.py b/python/helpers/python-skeletons/os/path.py deleted file mode 100644 index 0e0c93409994..000000000000 --- a/python/helpers/python-skeletons/os/path.py +++ /dev/null @@ -1,293 +0,0 @@ -"""Skeleton for 'os.path' stdlib module.""" - - -import sys -import os - - -def abspath(path): - """Return a normalized absolutized version of the pathname path. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def basename(path): - """Return the base name of pathname path. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def commonprefix(list): - """Return the longest path prefix (taken character-by-character) that is a - prefix of all paths in list. - - :type list: collections.Iterable[bytes | unicode | os.PathLike] - :rtype: bytes | unicode - """ - pass - - -def dirname(path): - """Return the directory name of pathname path. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def exists(path): - """Return True if path refers to an existing path. Returns False for broken - symbolic links. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def lexists(path): - """Return True if path refers to an existing path. Returns True for broken - symbolic links. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def expanduser(path): - """On Unix and Windows, return the argument with an initial component of ~ - or ~user replaced by that user's home directory. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def expandvars(path): - """Return the argument with environment variables expanded. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def getatime(path): - """Return the time of last access of path. - - :type path: bytes | unicode | os.PathLike - :rtype: float - """ - return 0.0 - - -def getmtime(path): - """Return the time of last modification of path. - - :type path: bytes | unicode | os.PathLike - :rtype: float - """ - return 0.0 - - -def getctime(path): - """Return the system's ctime. - - :type path: bytes | unicode | os.PathLike - :rtype: float - """ - return 0.0 - - -def getsize(path): - """Return the size, in bytes, of path. - - :type path: bytes | unicode | os.PathLike - :rtype: int - """ - return 0 - - -def isabs(path): - """Return True if path is an absolute pathname. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def isfile(path): - """Return True if path is an existing regular file. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def isdir(path): - """Return True if path is an existing directory. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def islink(path): - """Return True if path refers to a directory entry that is a symbolic link. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def ismount(path): - """Return True if pathname path is a mount point: a point in a file system - where a different file system has been mounted. - - :type path: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def join(path, *paths): - """Join one or more path components intelligently. - - :type path: bytes | unicode | os.PathLike - :type paths: collections.Iterable[bytes | unicode | os.PathLike] - :rtype: bytes | unicode - """ - return path - - -def normcase(path): - """Normalize the case of a pathname. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def normpath(path): - """Normalize a pathname by collapsing redundant separators and up-level - references. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def realpath(path): - """Return the canonical path of the specified filename, eliminating any - symbolic links encountered in the path. - - :type path: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def relpath(path, start=os.curdir): - """Return a relative filepath to path either from the current directory or - from an optional start directory. - - :type path: bytes | unicode | os.PathLike - :type start: bytes | unicode | os.PathLike - :rtype: bytes | unicode - """ - return path - - -def samefile(path1, path2): - """Return True if both pathname arguments refer to the same file or - directory. - - :type path1: bytes | unicode | os.PathLike - :type path2: bytes | unicode | os.PathLike - :rtype: bool - """ - return False - - -def sameopenfile(fp1, fp2): - """Return True if the file descriptors fp1 and fp2 refer to the same file. - - :type fp1: int - :type fp2: int - :rtype: bool - """ - return False - - -def samestat(stat1, stat2): - """Return True if the stat tuples stat1 and stat2 refer to the same file. - - :type stat1: os.stat_result | tuple - :type stat2: os.stat_result | tuple - :rtype: bool - """ - return False - - -def split(path): - """Split the pathname path into a pair, (head, tail). - - :type path: bytes | unicode | os.PathLike - :rtype: (bytes | unicode, bytes | unicode) - """ - return path, path - - -def splitdrive(path): - """Split the pathname path into a pair (drive, tail). - - :type path: bytes | unicode | os.PathLike - :rtype: (bytes | unicode, bytes | unicode) - """ - return path, path - - -def splitext(path): - """Split the pathname path into a pair (root, ext). - - :type path: bytes | unicode | os.PathLike - :rtype: (bytes | unicode, bytes | unicode) - """ - return path, path - - -def splitunc(path): - """Split the pathname path into a pair (unc, rest). - - :type path: bytes | unicode | os.PathLike - :rtype: (bytes | unicode, bytes | unicode) - """ - return path, path - - -if sys.version_info < (3, 0): - def walk(path, visit, arg): - """Calls the function visit with arguments (arg, dirname, names) for - each directory in the directory tree rooted at path. - - :type path: T <= bytes | unicode - :type visit: (V, T, list[T]) -> None - :type arg: V - :rtype: None - """ - pass diff --git a/python/helpers/typeshed/stdlib/2/os/__init__.pyi b/python/helpers/typeshed/stdlib/2/os/__init__.pyi new file mode 100644 index 000000000000..63407c289e88 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2/os/__init__.pyi @@ -0,0 +1,368 @@ +# Stubs for os +# Ron Murawski + +from builtins import OSError as error +from io import TextIOWrapper as _TextIOWrapper +from posix import listdir as listdir, stat_result as stat_result # TODO: use this, see https://github.com/python/mypy/issues/3078 +import sys +from typing import ( + Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr, + Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple, NoReturn, TypeVar +) +from . import path as path + +_T = TypeVar('_T') + +# ----- os variables ----- + +if sys.version_info >= (3, 2): + supports_bytes_environ: bool + +if sys.version_info >= (3, 3): + supports_dir_fd: Set[Callable[..., Any]] + supports_fd: Set[Callable[..., Any]] + supports_effective_ids: Set[Callable[..., Any]] + supports_follow_symlinks: Set[Callable[..., Any]] + +SEEK_SET: int +SEEK_CUR: int +SEEK_END: int + +O_RDONLY: int +O_WRONLY: int +O_RDWR: int +O_APPEND: int +O_CREAT: int +O_EXCL: int +O_TRUNC: int +# We don't use sys.platform for O_* flags to denote platform-dependent APIs because some codes, +# including tests for mypy, use a more finer way than sys.platform before using these APIs +# See https://github.com/python/typeshed/pull/2286 for discussions +O_DSYNC: int # Unix only +O_RSYNC: int # Unix only +O_SYNC: int # Unix only +O_NDELAY: int # Unix only +O_NONBLOCK: int # Unix only +O_NOCTTY: int # Unix only +O_SHLOCK: int # Unix only +O_EXLOCK: int # Unix only +O_BINARY: int # Windows only +O_NOINHERIT: int # Windows only +O_SHORT_LIVED: int # Windows only +O_TEMPORARY: int # Windows only +O_RANDOM: int # Windows only +O_SEQUENTIAL: int # Windows only +O_TEXT: int # Windows only +O_ASYNC: int # Gnu extension if in C library +O_DIRECT: int # Gnu extension if in C library +O_DIRECTORY: int # Gnu extension if in C library +O_NOFOLLOW: int # Gnu extension if in C library +O_NOATIME: int # Gnu extension if in C library +O_LARGEFILE: int # Gnu extension if in C library + +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +linesep: str +devnull: str +name: str + +F_OK: int +R_OK: int +W_OK: int +X_OK: int + +class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): + def copy(self) -> Dict[AnyStr, AnyStr]: ... + def __delitem__(self, key: AnyStr) -> None: ... + def __getitem__(self, key: AnyStr) -> AnyStr: ... + def __setitem__(self, key: AnyStr, value: AnyStr) -> None: ... + def __iter__(self) -> Iterator[AnyStr]: ... + def __len__(self) -> int: ... + +environ: _Environ[str] +if sys.version_info >= (3, 2): + environb: _Environ[bytes] + +if sys.platform != 'win32': + # Unix only + confstr_names: Dict[str, int] + pathconf_names: Dict[str, int] + sysconf_names: Dict[str, int] + + EX_OK: int + EX_USAGE: int + EX_DATAERR: int + EX_NOINPUT: int + EX_NOUSER: int + EX_NOHOST: int + EX_UNAVAILABLE: int + EX_SOFTWARE: int + EX_OSERR: int + EX_OSFILE: int + EX_CANTCREAT: int + EX_IOERR: int + EX_TEMPFAIL: int + EX_PROTOCOL: int + EX_NOPERM: int + EX_CONFIG: int + EX_NOTFOUND: int + +P_NOWAIT: int +P_NOWAITO: int +P_WAIT: int +if sys.platform == 'win32': + P_DETACH: int + P_OVERLAY: int + +# wait()/waitpid() options +if sys.platform != 'win32': + WNOHANG: int # Unix only + WCONTINUED: int # some Unix systems + WUNTRACED: int # Unix only + +TMP_MAX: int # Undocumented, but used by tempfile + +# ----- os classes (structures) ----- +if sys.version_info >= (3, 6): + from builtins import _PathLike as PathLike # See comment in builtins + +_PathType = path._PathType + +_StatVFS = NamedTuple('_StatVFS', [('f_bsize', int), ('f_frsize', int), ('f_blocks', int), + ('f_bfree', int), ('f_bavail', int), ('f_files', int), + ('f_ffree', int), ('f_favail', int), ('f_flag', int), + ('f_namemax', int)]) + +def getlogin() -> str: ... +def getpid() -> int: ... +def getppid() -> int: ... +def strerror(code: int) -> str: ... +def umask(mask: int) -> int: ... + +if sys.platform != 'win32': + def ctermid() -> str: ... + def getegid() -> int: ... + def geteuid() -> int: ... + def getgid() -> int: ... + def getgroups() -> List[int]: ... # Unix only, behaves differently on Mac + def initgroups(username: str, gid: int) -> None: ... + def getpgid(pid: int) -> int: ... + def getpgrp() -> int: ... + def getresuid() -> Tuple[int, int, int]: ... + def getresgid() -> Tuple[int, int, int]: ... + def getuid() -> int: ... + def setegid(egid: int) -> None: ... + def seteuid(euid: int) -> None: ... + def setgid(gid: int) -> None: ... + def setgroups(groups: Sequence[int]) -> None: ... + def setpgrp() -> None: ... + def setpgid(pid: int, pgrp: int) -> None: ... + def setregid(rgid: int, egid: int) -> None: ... + def setresgid(rgid: int, egid: int, sgid: int) -> None: ... + def setresuid(ruid: int, euid: int, suid: int) -> None: ... + def setreuid(ruid: int, euid: int) -> None: ... + def getsid(pid: int) -> int: ... + def setsid() -> None: ... + def setuid(uid: int) -> None: ... + def uname() -> Tuple[str, str, str, str, str]: ... + +@overload +def getenv(key: Text) -> Optional[str]: ... +@overload +def getenv(key: Text, default: _T) -> Union[str, _T]: ... +def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... +def unsetenv(key: Union[bytes, Text]) -> None: ... + +def fdopen(fd: int, *args, **kwargs) -> IO[Any]: ... +def close(fd: int) -> None: ... +def closerange(fd_low: int, fd_high: int) -> None: ... +def dup(fd: int) -> int: ... +def dup2(fd: int, fd2: int) -> None: ... +def fstat(fd: int) -> Any: ... +def fsync(fd: int) -> None: ... +def lseek(fd: int, pos: int, how: int) -> int: ... +def open(file: _PathType, flags: int, mode: int = ...) -> int: ... +def pipe() -> Tuple[int, int]: ... +def read(fd: int, n: int) -> bytes: ... +def write(fd: int, string: Union[bytes, buffer]) -> int: ... +def access(path: _PathType, mode: int) -> bool: ... +def chdir(path: _PathType) -> None: ... +def fchdir(fd: int) -> None: ... +def getcwd() -> str: ... +def getcwdu() -> unicode: ... +def chmod(path: _PathType, mode: int) -> None: ... +def link(src: _PathType, link_name: _PathType) -> None: ... +def lstat(path: _PathType) -> Any: ... +def mknod(filename: _PathType, mode: int = ..., device: int = ...) -> None: ... +def major(device: int) -> int: ... +def minor(device: int) -> int: ... +def makedev(major: int, minor: int) -> int: ... +def mkdir(path: _PathType, mode: int = ...) -> None: ... +def makedirs(path: _PathType, mode: int = ...) -> None: ... +def readlink(path: AnyStr) -> AnyStr: ... +def remove(path: _PathType) -> None: ... +def removedirs(path: _PathType) -> None: ... +def rename(src: _PathType, dst: _PathType) -> None: ... +def renames(old: _PathType, new: _PathType) -> None: ... +def rmdir(path: _PathType) -> None: ... +def stat(path: _PathType) -> Any: ... +@overload +def stat_float_times() -> bool: ... +@overload +def stat_float_times(newvalue: bool) -> None: ... +def symlink(source: _PathType, link_name: _PathType) -> None: ... +def unlink(path: _PathType) -> None: ... +# TODO: add ns, dir_fd, follow_symlinks argument +if sys.version_info >= (3, 0): + def utime(path: _PathType, times: Optional[Tuple[float, float]] = ...) -> None: ... +else: + def utime(path: _PathType, times: Optional[Tuple[float, float]]) -> None: ... + +if sys.platform != 'win32': + # Unix only + def fchmod(fd: int, mode: int) -> None: ... + def fchown(fd: int, uid: int, gid: int) -> None: ... + if sys.platform != 'darwin': + def fdatasync(fd: int) -> None: ... # Unix only, not Mac + def fpathconf(fd: int, name: Union[str, int]) -> int: ... + def fstatvfs(fd: int) -> _StatVFS: ... + def ftruncate(fd: int, length: int) -> None: ... + def isatty(fd: int) -> bool: ... + def openpty() -> Tuple[int, int]: ... # some flavors of Unix + def tcgetpgrp(fd: int) -> int: ... + def tcsetpgrp(fd: int, pg: int) -> None: ... + def ttyname(fd: int) -> str: ... + def chflags(path: _PathType, flags: int) -> None: ... + def chroot(path: _PathType) -> None: ... + def chown(path: _PathType, uid: int, gid: int) -> None: ... + def lchflags(path: _PathType, flags: int) -> None: ... + def lchmod(path: _PathType, mode: int) -> None: ... + def lchown(path: _PathType, uid: int, gid: int) -> None: ... + def mkfifo(path: _PathType, mode: int = ...) -> None: ... + def pathconf(path: _PathType, name: Union[str, int]) -> int: ... + def statvfs(path: _PathType) -> _StatVFS: ... + +if sys.version_info >= (3, 6): + def walk(top: Union[AnyStr, PathLike[AnyStr]], topdown: bool = ..., + onerror: Optional[Callable[[OSError], Any]] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... +else: + def walk(top: AnyStr, topdown: bool = ..., onerror: Optional[Callable[[OSError], Any]] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... + +def abort() -> NoReturn: ... +# These are defined as execl(file, *args) but the first *arg is mandatory. +def execl(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ... +def execlp(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ... + +# These are: execle(file, *args, env) but env is pulled from the last element of the args. +def execle(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ... +def execlpe(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ... + +# The docs say `args: tuple or list of strings` +# The implementation enforces tuple or list so we can't use Sequence. +_ExecVArgs = Union[Tuple[Union[bytes, Text], ...], List[bytes], List[Text], List[Union[bytes, Text]]] +def execv(path: _PathType, args: _ExecVArgs) -> NoReturn: ... +def execve(path: _PathType, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ... +def execvp(file: _PathType, args: _ExecVArgs) -> NoReturn: ... +def execvpe(file: _PathType, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ... + +def _exit(n: int) -> NoReturn: ... +def kill(pid: int, sig: int) -> None: ... + +if sys.platform != 'win32': + # Unix only + def fork() -> int: ... + def forkpty() -> Tuple[int, int]: ... # some flavors of Unix + def killpg(pgid: int, sig: int) -> None: ... + def nice(increment: int) -> int: ... + def plock(op: int) -> None: ... # ???op is int? + +if sys.version_info >= (3, 0): + class popen(_TextIOWrapper): + # TODO 'b' modes or bytes command not accepted? + def __init__(self, command: str, mode: str = ..., + bufsize: int = ...) -> None: ... + def close(self) -> Any: ... # may return int +else: + def popen(command: str, *args, **kwargs) -> IO[Any]: ... + def popen2(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... + def popen3(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any], IO[Any]]: ... + def popen4(cmd: str, *args, **kwargs) -> Tuple[IO[Any], IO[Any]]: ... + +def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... +def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text], + *args: Any) -> int: ... # Imprecise sig +def spawnv(mode: int, path: _PathType, args: List[Union[bytes, Text]]) -> int: ... +def spawnve(mode: int, path: _PathType, args: List[Union[bytes, Text]], + env: Mapping[str, str]) -> int: ... +def system(command: _PathType) -> int: ... +def times() -> Tuple[float, float, float, float, float]: ... +def waitpid(pid: int, options: int) -> Tuple[int, int]: ... +def urandom(n: int) -> bytes: ... + +if sys.platform == 'win32': + def startfile(path: _PathType, operation: Optional[str] = ...) -> None: ... +else: + # Unix only + def spawnlp(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... + def spawnlpe(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature + def spawnvp(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ... + def spawnvpe(mode: int, file: _PathType, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ... + def wait() -> Tuple[int, int]: ... + def wait3(options: int) -> Tuple[int, int, Any]: ... + def wait4(pid: int, options: int) -> Tuple[int, int, Any]: ... + def WCOREDUMP(status: int) -> bool: ... + def WIFCONTINUED(status: int) -> bool: ... + def WIFSTOPPED(status: int) -> bool: ... + def WIFSIGNALED(status: int) -> bool: ... + def WIFEXITED(status: int) -> bool: ... + def WEXITSTATUS(status: int) -> int: ... + def WSTOPSIG(status: int) -> int: ... + def WTERMSIG(status: int) -> int: ... + def confstr(name: Union[str, int]) -> Optional[str]: ... + def getloadavg() -> Tuple[float, float, float]: ... + def sysconf(name: Union[str, int]) -> int: ... + +if sys.version_info >= (3, 0): + def sched_getaffinity(id: int) -> Set[int]: ... +if sys.version_info >= (3, 3): + class waitresult: + si_pid: int + def waitid(idtype: int, id: int, options: int) -> waitresult: ... + +if sys.version_info < (3, 0): + def tmpfile() -> IO[Any]: ... + def tmpnam() -> str: ... + def tempnam(dir: str = ..., prefix: str = ...) -> str: ... + +P_ALL: int +WEXITED: int +WNOWAIT: int + +if sys.version_info >= (3, 3): + if sys.platform != 'win32': + # Unix only + def sync() -> None: ... + + def truncate(path: Union[_PathType, int], length: int) -> None: ... # Unix only up to version 3.4 + + def fwalk(top: AnyStr = ..., topdown: bool = ..., + onerror: Callable = ..., *, follow_symlinks: bool = ..., + dir_fd: int = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], List[AnyStr], int]]: ... + + terminal_size = NamedTuple('terminal_size', [('columns', int), ('lines', int)]) + def get_terminal_size(fd: int = ...) -> terminal_size: ... + +if sys.version_info >= (3, 4): + def cpu_count() -> Optional[int]: ... diff --git a/python/helpers/typeshed/stdlib/2/os/path.pyi b/python/helpers/typeshed/stdlib/2/os/path.pyi new file mode 100644 index 000000000000..42409c0e8721 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2/os/path.pyi @@ -0,0 +1,177 @@ +# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent! +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +_T = TypeVar('_T') + +if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] +else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + +# ----- os.path variables ----- +supports_unicode_filenames: bool +# aliases (also in os) +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +devnull: str + +# ----- os.path function stubs ----- +if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + +else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + +if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... +elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + +# NOTE: Empty lists results in '' (str) regardless of contained type. +# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes +# So, fall back to Any +def commonprefix(list: Sequence[_PathType]) -> Any: ... + +if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... +else: + def exists(path: _PathType) -> bool: ... +def lexists(path: _PathType) -> bool: ... + +# These return float if os.stat_float_times() == True, +# but int is a subclass of float. +def getatime(path: _PathType) -> float: ... +def getmtime(path: _PathType) -> float: ... +def getctime(path: _PathType) -> float: ... + +def getsize(path: _PathType) -> int: ... +def isabs(path: _PathType) -> bool: ... +def isfile(path: _PathType) -> bool: ... +def isdir(path: _PathType) -> bool: ... +def islink(path: _PathType) -> bool: ... +def ismount(path: _PathType) -> bool: ... + +if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... +elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... +else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + +@overload +def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... +@overload +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + +def samefile(path1: _PathType, path2: _PathType) -> bool: ... +def sameopenfile(fp1: int, fp2: int) -> bool: ... +def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + +if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... +else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + +if sys.platform == 'win32': + def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # deprecated + +if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/2/os2emxpath.pyi b/python/helpers/typeshed/stdlib/2/os2emxpath.pyi new file mode 100644 index 000000000000..42409c0e8721 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2/os2emxpath.pyi @@ -0,0 +1,177 @@ +# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent! +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +_T = TypeVar('_T') + +if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] +else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + +# ----- os.path variables ----- +supports_unicode_filenames: bool +# aliases (also in os) +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +devnull: str + +# ----- os.path function stubs ----- +if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + +else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + +if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... +elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + +# NOTE: Empty lists results in '' (str) regardless of contained type. +# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes +# So, fall back to Any +def commonprefix(list: Sequence[_PathType]) -> Any: ... + +if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... +else: + def exists(path: _PathType) -> bool: ... +def lexists(path: _PathType) -> bool: ... + +# These return float if os.stat_float_times() == True, +# but int is a subclass of float. +def getatime(path: _PathType) -> float: ... +def getmtime(path: _PathType) -> float: ... +def getctime(path: _PathType) -> float: ... + +def getsize(path: _PathType) -> int: ... +def isabs(path: _PathType) -> bool: ... +def isfile(path: _PathType) -> bool: ... +def isdir(path: _PathType) -> bool: ... +def islink(path: _PathType) -> bool: ... +def ismount(path: _PathType) -> bool: ... + +if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... +elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... +else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + +@overload +def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... +@overload +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + +def samefile(path1: _PathType, path2: _PathType) -> bool: ... +def sameopenfile(fp1: int, fp2: int) -> bool: ... +def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + +if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... +else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + +if sys.platform == 'win32': + def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # deprecated + +if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/2/posix.pyi b/python/helpers/typeshed/stdlib/2/posix.pyi new file mode 100644 index 000000000000..6cfd8520a15f --- /dev/null +++ b/python/helpers/typeshed/stdlib/2/posix.pyi @@ -0,0 +1,201 @@ +from typing import AnyStr, Dict, List, Mapping, Tuple, Union, Sequence, IO, Optional, TypeVar + +error = OSError + +confstr_names: Dict[str, int] +environ: Dict[str, str] +pathconf_names: Dict[str, int] +sysconf_names: Dict[str, int] + +_T = TypeVar("_T") + +EX_CANTCREAT: int +EX_CONFIG: int +EX_DATAERR: int +EX_IOERR: int +EX_NOHOST: int +EX_NOINPUT: int +EX_NOPERM: int +EX_NOUSER: int +EX_OK: int +EX_OSERR: int +EX_OSFILE: int +EX_PROTOCOL: int +EX_SOFTWARE: int +EX_TEMPFAIL: int +EX_UNAVAILABLE: int +EX_USAGE: int +F_OK: int +NGROUPS_MAX: int +O_APPEND: int +O_ASYNC: int +O_CREAT: int +O_DIRECT: int +O_DIRECTORY: int +O_DSYNC: int +O_EXCL: int +O_LARGEFILE: int +O_NDELAY: int +O_NOATIME: int +O_NOCTTY: int +O_NOFOLLOW: int +O_NONBLOCK: int +O_RDONLY: int +O_RDWR: int +O_RSYNC: int +O_SYNC: int +O_TRUNC: int +O_WRONLY: int +R_OK: int +TMP_MAX: int +WCONTINUED: int +WNOHANG: int +WUNTRACED: int +W_OK: int +X_OK: int + +def WCOREDUMP(status: int) -> bool: ... +def WEXITSTATUS(status: int) -> bool: ... +def WIFCONTINUED(status: int) -> bool: ... +def WIFEXITED(status: int) -> bool: ... +def WIFSIGNALED(status: int) -> bool: ... +def WIFSTOPPED(status: int) -> bool: ... +def WSTOPSIG(status: int) -> bool: ... +def WTERMSIG(status: int) -> bool: ... + +class stat_result(object): + n_fields: int + n_sequence_fields: int + n_unnamed_fields: int + st_mode: int + st_ino: int + st_dev: int + st_nlink: int + st_uid: int + st_gid: int + st_size: int + st_atime: int + st_mtime: int + st_ctime: int + +class statvfs_result(object): + n_fields: int + n_sequence_fields: int + n_unnamed_fields: int + f_bsize: int + f_frsize: int + f_blocks: int + f_bfree: int + f_bavail: int + f_files: int + f_ffree: int + f_favail: int + f_flag: int + f_namemax: int + +def _exit(status: int) -> None: ... +def abort() -> None: ... +def access(path: unicode, mode: int) -> bool: ... +def chdir(path: unicode) -> None: ... +def chmod(path: unicode, mode: int) -> None: ... +def chown(path: unicode, uid: int, gid: int) -> None: ... +def chroot(path: unicode) -> None: ... +def close(fd: int) -> None: ... +def closerange(fd_low: int, fd_high: int) -> None: ... +def confstr(name: Union[str, int]) -> str: ... +def ctermid() -> str: ... +def dup(fd: int) -> int: ... +def dup2(fd: int, fd2: int) -> None: ... +def execv(path: str, args: Sequence[str], env: Mapping[str, str]) -> None: ... +def execve(path: str, args: Sequence[str], env: Mapping[str, str]) -> None: ... +def fchdir(fd: int) -> None: ... +def fchmod(fd: int, mode: int) -> None: ... +def fchown(fd: int, uid: int, gid: int) -> None: ... +def fdatasync(fd: int) -> None: ... +def fdopen(fd: int, mode: str = ..., bufsize: int = ...) -> IO[str]: ... +def fork() -> int: ... +def forkpty() -> Tuple[int, int]: ... +def fpathconf(fd: int, name: str) -> None: ... +def fstat(fd: int) -> stat_result: ... +def fstatvfs(fd: int) -> statvfs_result: ... +def fsync(fd: int) -> None: ... +def ftruncate(fd: int, length: int) -> None: ... +def getcwd() -> str: ... +def getcwdu() -> unicode: ... +def getegid() -> int: ... +def geteuid() -> int: ... +def getgid() -> int: ... +def getgroups() -> List[int]: ... +def getloadavg() -> Tuple[float, float, float]: ... +def getlogin() -> str: ... +def getpgid(pid: int) -> int: ... +def getpgrp() -> int: ... +def getpid() -> int: ... +def getppid() -> int: ... +def getresgid() -> Tuple[int, int, int]: ... +def getresuid() -> Tuple[int, int, int]: ... +def getsid(pid: int) -> int: ... +def getuid() -> int: ... +def initgroups(username: str, gid: int) -> None: ... +def isatty(fd: int) -> bool: ... +def kill(pid: int, sig: int) -> None: ... +def killpg(pgid: int, sig: int) -> None: ... +def lchown(path: unicode, uid: int, gid: int) -> None: ... +def link(source: unicode, link_name: str) -> None: ... +def listdir(path: AnyStr) -> List[AnyStr]: ... +def lseek(fd: int, pos: int, how: int) -> None: ... +def lstat(path: unicode) -> stat_result: ... +def major(device: int) -> int: ... +def makedev(major: int, minor: int) -> int: ... +def minor(device: int) -> int: ... +def mkdir(path: unicode, mode: int = ...) -> None: ... +def mkfifo(path: unicode, mode: int = ...) -> None: ... +def mknod(filename: unicode, mode: int = ..., device: int = ...) -> None: ... +def nice(increment: int) -> int: ... +def open(file: unicode, flags: int, mode: int = ...) -> int: ... +def openpty() -> Tuple[int, int]: ... +def pathconf(path: unicode, name: str) -> str: ... +def pipe() -> Tuple[int, int]: ... +def popen(command: str, mode: str = ..., bufsize: int = ...) -> IO[str]: ... +def putenv(varname: str, value: str) -> None: ... +def read(fd: int, n: int) -> str: ... +def readlink(path: _T) -> _T: ... +def remove(path: unicode) -> None: ... +def rename(src: unicode, dst: unicode) -> None: ... +def rmdir(path: unicode) -> None: ... +def setegid(egid: int) -> None: ... +def seteuid(euid: int) -> None: ... +def setgid(gid: int) -> None: ... +def setgroups(groups: Sequence[int]) -> None: ... +def setpgid(pid: int, pgrp: int) -> None: ... +def setpgrp() -> None: ... +def setregid(rgid: int, egid: int) -> None: ... +def setresgid(rgid: int, egid: int, sgid: int) -> None: ... +def setresuid(ruid: int, euid: int, suid: int) -> None: ... +def setreuid(ruid: int, euid: int) -> None: ... +def setsid() -> None: ... +def setuid(pid: int) -> None: ... +def stat(path: unicode) -> stat_result: ... +def statvfs(path: unicode) -> statvfs_result: ... +def stat_float_times(fd: int) -> None: ... +def strerror(code: int) -> str: ... +def symlink(source: unicode, link_name: unicode) -> None: ... +def sysconf(name: Union[str, int]) -> int: ... +def system(command: unicode) -> int: ... +def tcgetpgrp(fd: int) -> int: ... +def tcsetpgrp(fd: int, pg: int) -> None: ... +def times() -> Tuple[float, float, float, float, float]: ... +def tmpfile() -> IO[str]: ... +def ttyname(fd: int) -> str: ... +def umask(mask: int) -> int: ... +def uname() -> Tuple[str, str, str, str, str]: ... +def unlink(path: unicode) -> None: ... +def unsetenv(varname: str) -> None: ... +def urandom(n: int) -> str: ... +def utime(path: unicode, times: Optional[Tuple[int, int]]) -> None: ... +def wait() -> int: ... +_r = Tuple[float, float, int, int, int, int, int, int, int, int, int, int, int, int, int, int] +def wait3(options: int) -> Tuple[int, int, _r]: ... +def wait4(pid: int, options: int) -> Tuple[int, int, _r]: ... +def waitpid(pid: int, options: int) -> int: ... +def write(fd: int, str: str) -> int: ... diff --git a/python/helpers/typeshed/stdlib/2and3/genericpath.pyi b/python/helpers/typeshed/stdlib/2and3/genericpath.pyi new file mode 100644 index 000000000000..e0b1c6de208c --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/genericpath.pyi @@ -0,0 +1,21 @@ +from typing import Sequence, AnyStr, Text +import sys + +if sys.version_info >= (3, 0): + def commonprefix(m: Sequence[str]) -> str: ... +else: + def commonprefix(m: Sequence[AnyStr]) -> AnyStr: ... + +def exists(path: Text) -> bool: ... +def isfile(path: Text) -> bool: ... +def isdir(s: Text) -> bool: ... +def getsize(filename: Text) -> int: ... +def getmtime(filename: Text) -> float: ... +def getatime(filename: Text) -> float: ... +def getctime(filename: Text) -> float: ... + + +if sys.version_info >= (3, 4): + def samestat(s1: str, s2: str) -> int: ... + def samefile(f1: str, f2: str) -> int: ... + def sameopenfile(fp1: str, fp2: str) -> int: ... diff --git a/python/helpers/typeshed/stdlib/2and3/macpath.pyi b/python/helpers/typeshed/stdlib/2and3/macpath.pyi new file mode 100644 index 000000000000..324fc7b31a11 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/macpath.pyi @@ -0,0 +1,171 @@ +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +if sys.version_info < (3, 8): + _T = TypeVar('_T') + + if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] + else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + + # ----- os.path variables ----- + supports_unicode_filenames: bool + # aliases (also in os) + curdir: str + pardir: str + sep: str + altsep: Optional[str] + extsep: str + pathsep: str + defpath: str + devnull: str + + # ----- os.path function stubs ----- + if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + + else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + + if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... + elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + + # NOTE: Empty lists results in '' (str) regardless of contained type. + # Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes + # So, fall back to Any + def commonprefix(list: Sequence[_PathType]) -> Any: ... + + if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... + else: + def exists(path: _PathType) -> bool: ... + def lexists(path: _PathType) -> bool: ... + + # These return float if os.stat_float_times() == True, + # but int is a subclass of float. + def getatime(path: _PathType) -> float: ... + def getmtime(path: _PathType) -> float: ... + def getctime(path: _PathType) -> float: ... + + def getsize(path: _PathType) -> int: ... + def isabs(path: _PathType) -> bool: ... + def isfile(path: _PathType) -> bool: ... + def isdir(path: _PathType) -> bool: ... + def islink(path: _PathType) -> bool: ... + def ismount(path: _PathType) -> bool: ... + + if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... + elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... + else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + + @overload + def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... + @overload + def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + + def samefile(path1: _PathType, path2: _PathType) -> bool: ... + def sameopenfile(fp1: int, fp2: int) -> bool: ... + def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + + if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + + if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/2and3/ntpath.pyi b/python/helpers/typeshed/stdlib/2and3/ntpath.pyi new file mode 100644 index 000000000000..42409c0e8721 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/ntpath.pyi @@ -0,0 +1,177 @@ +# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent! +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +_T = TypeVar('_T') + +if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] +else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + +# ----- os.path variables ----- +supports_unicode_filenames: bool +# aliases (also in os) +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +devnull: str + +# ----- os.path function stubs ----- +if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + +else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + +if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... +elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + +# NOTE: Empty lists results in '' (str) regardless of contained type. +# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes +# So, fall back to Any +def commonprefix(list: Sequence[_PathType]) -> Any: ... + +if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... +else: + def exists(path: _PathType) -> bool: ... +def lexists(path: _PathType) -> bool: ... + +# These return float if os.stat_float_times() == True, +# but int is a subclass of float. +def getatime(path: _PathType) -> float: ... +def getmtime(path: _PathType) -> float: ... +def getctime(path: _PathType) -> float: ... + +def getsize(path: _PathType) -> int: ... +def isabs(path: _PathType) -> bool: ... +def isfile(path: _PathType) -> bool: ... +def isdir(path: _PathType) -> bool: ... +def islink(path: _PathType) -> bool: ... +def ismount(path: _PathType) -> bool: ... + +if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... +elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... +else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + +@overload +def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... +@overload +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + +def samefile(path1: _PathType, path2: _PathType) -> bool: ... +def sameopenfile(fp1: int, fp2: int) -> bool: ... +def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + +if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... +else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + +if sys.platform == 'win32': + def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # deprecated + +if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/2and3/posixpath.pyi b/python/helpers/typeshed/stdlib/2and3/posixpath.pyi new file mode 100644 index 000000000000..42409c0e8721 --- /dev/null +++ b/python/helpers/typeshed/stdlib/2and3/posixpath.pyi @@ -0,0 +1,177 @@ +# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent! +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +_T = TypeVar('_T') + +if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] +else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + +# ----- os.path variables ----- +supports_unicode_filenames: bool +# aliases (also in os) +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +devnull: str + +# ----- os.path function stubs ----- +if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + +else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + +if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... +elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + +# NOTE: Empty lists results in '' (str) regardless of contained type. +# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes +# So, fall back to Any +def commonprefix(list: Sequence[_PathType]) -> Any: ... + +if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... +else: + def exists(path: _PathType) -> bool: ... +def lexists(path: _PathType) -> bool: ... + +# These return float if os.stat_float_times() == True, +# but int is a subclass of float. +def getatime(path: _PathType) -> float: ... +def getmtime(path: _PathType) -> float: ... +def getctime(path: _PathType) -> float: ... + +def getsize(path: _PathType) -> int: ... +def isabs(path: _PathType) -> bool: ... +def isfile(path: _PathType) -> bool: ... +def isdir(path: _PathType) -> bool: ... +def islink(path: _PathType) -> bool: ... +def ismount(path: _PathType) -> bool: ... + +if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... +elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... +else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + +@overload +def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... +@overload +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + +def samefile(path1: _PathType, path2: _PathType) -> bool: ... +def sameopenfile(fp1: int, fp2: int) -> bool: ... +def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + +if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... +else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + +if sys.platform == 'win32': + def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # deprecated + +if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/3/os/__init__.pyi b/python/helpers/typeshed/stdlib/3/os/__init__.pyi new file mode 100644 index 000000000000..6d7eef7ebe83 --- /dev/null +++ b/python/helpers/typeshed/stdlib/3/os/__init__.pyi @@ -0,0 +1,663 @@ +# Stubs for os +# Ron Murawski + +from io import TextIOWrapper as _TextIOWrapper +from posix import listdir as listdir, times_result +import sys +from typing import ( + Mapping, MutableMapping, Dict, List, Any, Tuple, Iterable, Iterator, NoReturn, overload, Union, AnyStr, + Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, TypeVar, ContextManager +) + +# Re-exported names from other modules. +from builtins import OSError as error +from . import path as path + +_T = TypeVar('_T') + +# ----- os variables ----- + +supports_bytes_environ: bool + +supports_dir_fd: Set[Callable[..., Any]] +supports_fd: Set[Callable[..., Any]] +supports_effective_ids: Set[Callable[..., Any]] +supports_follow_symlinks: Set[Callable[..., Any]] + +if sys.platform != 'win32': + # Unix only + PRIO_PROCESS: int + PRIO_PGRP: int + PRIO_USER: int + + F_LOCK: int + F_TLOCK: int + F_ULOCK: int + F_TEST: int + + POSIX_FADV_NORMAL: int + POSIX_FADV_SEQUENTIAL: int + POSIX_FADV_RANDOM: int + POSIX_FADV_NOREUSE: int + POSIX_FADV_WILLNEED: int + POSIX_FADV_DONTNEED: int + + SF_NODISKIO: int + SF_MNOWAIT: int + SF_SYNC: int + + XATTR_SIZE_MAX: int # Linux only + XATTR_CREATE: int # Linux only + XATTR_REPLACE: int # Linux only + + P_PID: int + P_PGID: int + P_ALL: int + + WEXITED: int + WSTOPPED: int + WNOWAIT: int + + CLD_EXITED: int + CLD_DUMPED: int + CLD_TRAPPED: int + CLD_CONTINUED: int + + SCHED_OTHER: int # some flavors of Unix + SCHED_BATCH: int # some flavors of Unix + SCHED_IDLE: int # some flavors of Unix + SCHED_SPORADIC: int # some flavors of Unix + SCHED_FIFO: int # some flavors of Unix + SCHED_RR: int # some flavors of Unix + SCHED_RESET_ON_FORK: int # some flavors of Unix + +RTLD_LAZY: int +RTLD_NOW: int +RTLD_GLOBAL: int +RTLD_LOCAL: int +RTLD_NODELETE: int +RTLD_NOLOAD: int +RTLD_DEEPBIND: int + +SEEK_SET: int +SEEK_CUR: int +SEEK_END: int +if sys.platform != 'win32': + SEEK_DATA: int # some flavors of Unix + SEEK_HOLE: int # some flavors of Unix + +O_RDONLY: int +O_WRONLY: int +O_RDWR: int +O_APPEND: int +O_CREAT: int +O_EXCL: int +O_TRUNC: int +# We don't use sys.platform for O_* flags to denote platform-dependent APIs because some codes, +# including tests for mypy, use a more finer way than sys.platform before using these APIs +# See https://github.com/python/typeshed/pull/2286 for discussions +O_DSYNC: int # Unix only +O_RSYNC: int # Unix only +O_SYNC: int # Unix only +O_NDELAY: int # Unix only +O_NONBLOCK: int # Unix only +O_NOCTTY: int # Unix only +O_CLOEXEC: int # Unix only +O_SHLOCK: int # Unix only +O_EXLOCK: int # Unix only +O_BINARY: int # Windows only +O_NOINHERIT: int # Windows only +O_SHORT_LIVED: int # Windows only +O_TEMPORARY: int # Windows only +O_RANDOM: int # Windows only +O_SEQUENTIAL: int # Windows only +O_TEXT: int # Windows only +O_ASYNC: int # Gnu extension if in C library +O_DIRECT: int # Gnu extension if in C library +O_DIRECTORY: int # Gnu extension if in C library +O_NOFOLLOW: int # Gnu extension if in C library +O_NOATIME: int # Gnu extension if in C library +O_PATH: int # Gnu extension if in C library +O_TMPFILE: int # Gnu extension if in C library +O_LARGEFILE: int # Gnu extension if in C library + +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +linesep: str +devnull: str +name: str + +F_OK: int +R_OK: int +W_OK: int +X_OK: int + +class _Environ(MutableMapping[AnyStr, AnyStr], Generic[AnyStr]): + def copy(self) -> Dict[AnyStr, AnyStr]: ... + def __delitem__(self, key: AnyStr) -> None: ... + def __getitem__(self, key: AnyStr) -> AnyStr: ... + def __setitem__(self, key: AnyStr, value: AnyStr) -> None: ... + def __iter__(self) -> Iterator[AnyStr]: ... + def __len__(self) -> int: ... + +environ: _Environ[str] +environb: _Environ[bytes] + +if sys.platform != 'win32': + confstr_names: Dict[str, int] + pathconf_names: Dict[str, int] + sysconf_names: Dict[str, int] + + EX_OK: int + EX_USAGE: int + EX_DATAERR: int + EX_NOINPUT: int + EX_NOUSER: int + EX_NOHOST: int + EX_UNAVAILABLE: int + EX_SOFTWARE: int + EX_OSERR: int + EX_OSFILE: int + EX_CANTCREAT: int + EX_IOERR: int + EX_TEMPFAIL: int + EX_PROTOCOL: int + EX_NOPERM: int + EX_CONFIG: int + EX_NOTFOUND: int + +P_NOWAIT: int +P_NOWAITO: int +P_WAIT: int +if sys.platform == 'win32': + P_DETACH: int + P_OVERLAY: int + +# wait()/waitpid() options +if sys.platform != 'win32': + WNOHANG: int # Unix only + WCONTINUED: int # some Unix systems + WUNTRACED: int # Unix only + +TMP_MAX: int # Undocumented, but used by tempfile + +# ----- os classes (structures) ----- +class stat_result: + # For backward compatibility, the return value of stat() is also + # accessible as a tuple of at least 10 integers giving the most important + # (and portable) members of the stat structure, in the order st_mode, + # st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, + # st_ctime. More items may be added at the end by some implementations. + + st_mode: int # protection bits, + st_ino: int # inode number, + st_dev: int # device, + st_nlink: int # number of hard links, + st_uid: int # user id of owner, + st_gid: int # group id of owner, + st_size: int # size of file, in bytes, + st_atime: float # time of most recent access, + st_mtime: float # time of most recent content modification, + st_ctime: float # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) + st_atime_ns: int # time of most recent access, in nanoseconds + st_mtime_ns: int # time of most recent content modification in nanoseconds + st_ctime_ns: int # platform dependent (time of most recent metadata change on Unix, or the time of creation on Windows) in nanoseconds + if sys.version_info >= (3, 8) and sys.platform == "win32": + st_reparse_tag: int + + def __getitem__(self, i: int) -> int: ... + + # not documented + def __init__(self, tuple: Tuple[int, ...]) -> None: ... + + # On some Unix systems (such as Linux), the following attributes may also + # be available: + st_blocks: int # number of blocks allocated for file + st_blksize: int # filesystem blocksize + st_rdev: int # type of device if an inode device + st_flags: int # user defined flags for file + + # On other Unix systems (such as FreeBSD), the following attributes may be + # available (but may be only filled out if root tries to use them): + st_gen: int # file generation number + st_birthtime: int # time of file creation + + # On Mac OS systems, the following attributes may also be available: + st_rsize: int + st_creator: int + st_type: int + +if sys.version_info >= (3, 6): + from builtins import _PathLike as PathLike # See comment in builtins + +_PathType = path._PathType +_FdOrPathType = Union[int, _PathType] + +if sys.version_info >= (3, 6): + class DirEntry(PathLike[AnyStr]): + # This is what the scandir interator yields + # The constructor is hidden + + name: AnyStr + path: AnyStr + def inode(self) -> int: ... + def is_dir(self, *, follow_symlinks: bool = ...) -> bool: ... + def is_file(self, *, follow_symlinks: bool = ...) -> bool: ... + def is_symlink(self) -> bool: ... + def stat(self, *, follow_symlinks: bool = ...) -> stat_result: ... + + def __fspath__(self) -> AnyStr: ... +else: + class DirEntry(Generic[AnyStr]): + # This is what the scandir interator yields + # The constructor is hidden + + name: AnyStr + path: AnyStr + def inode(self) -> int: ... + def is_dir(self, *, follow_symlinks: bool = ...) -> bool: ... + def is_file(self, *, follow_symlinks: bool = ...) -> bool: ... + def is_symlink(self) -> bool: ... + def stat(self, *, follow_symlinks: bool = ...) -> stat_result: ... + + +if sys.platform != 'win32': + class statvfs_result: # Unix only + f_bsize: int + f_frsize: int + f_blocks: int + f_bfree: int + f_bavail: int + f_files: int + f_ffree: int + f_favail: int + f_flag: int + f_namemax: int + +# ----- os function stubs ----- +if sys.version_info >= (3, 6): + def fsencode(filename: Union[str, bytes, PathLike[Any]]) -> bytes: ... +else: + def fsencode(filename: Union[str, bytes]) -> bytes: ... + +if sys.version_info >= (3, 6): + def fsdecode(filename: Union[str, bytes, PathLike[Any]]) -> str: ... +else: + def fsdecode(filename: Union[str, bytes]) -> str: ... + +if sys.version_info >= (3, 6): + @overload + def fspath(path: str) -> str: ... + @overload + def fspath(path: bytes) -> bytes: ... + @overload + def fspath(path: PathLike[Any]) -> Any: ... + +def get_exec_path(env: Optional[Mapping[str, str]] = ...) -> List[str]: ... +# NOTE: get_exec_path(): returns List[bytes] when env not None +def getlogin() -> str: ... +def getpid() -> int: ... +def getppid() -> int: ... +def strerror(code: int) -> str: ... +def umask(mask: int) -> int: ... + +if sys.platform != 'win32': + # Unix only + def ctermid() -> str: ... + def getegid() -> int: ... + def geteuid() -> int: ... + def getgid() -> int: ... + def getgrouplist(user: str, gid: int) -> List[int]: ... + def getgroups() -> List[int]: ... # Unix only, behaves differently on Mac + def initgroups(username: str, gid: int) -> None: ... + def getpgid(pid: int) -> int: ... + def getpgrp() -> int: ... + def getpriority(which: int, who: int) -> int: ... + def setpriority(which: int, who: int, priority: int) -> None: ... + def getresuid() -> Tuple[int, int, int]: ... + def getresgid() -> Tuple[int, int, int]: ... + def getuid() -> int: ... + def setegid(egid: int) -> None: ... + def seteuid(euid: int) -> None: ... + def setgid(gid: int) -> None: ... + def setgroups(groups: Sequence[int]) -> None: ... + def setpgrp() -> None: ... + def setpgid(pid: int, pgrp: int) -> None: ... + def setregid(rgid: int, egid: int) -> None: ... + def setresgid(rgid: int, egid: int, sgid: int) -> None: ... + def setresuid(ruid: int, euid: int, suid: int) -> None: ... + def setreuid(ruid: int, euid: int) -> None: ... + def getsid(pid: int) -> int: ... + def setsid() -> None: ... + def setuid(uid: int) -> None: ... + from posix import uname_result + def uname() -> uname_result: ... + +@overload +def getenv(key: Text) -> Optional[str]: ... +@overload +def getenv(key: Text, default: _T) -> Union[str, _T]: ... +def getenvb(key: bytes, default: bytes = ...) -> bytes: ... +def putenv(key: Union[bytes, Text], value: Union[bytes, Text]) -> None: ... +def unsetenv(key: Union[bytes, Text]) -> None: ... + +# Return IO or TextIO +def fdopen(fd: int, mode: str = ..., buffering: int = ..., encoding: Optional[str] = ..., + errors: str = ..., newline: str = ..., closefd: bool = ...) -> Any: ... +def close(fd: int) -> None: ... +def closerange(fd_low: int, fd_high: int) -> None: ... +def device_encoding(fd: int) -> Optional[str]: ... +def dup(fd: int) -> int: ... +if sys.version_info >= (3, 7): + def dup2(fd: int, fd2: int, inheritable: bool = ...) -> int: ... +else: + def dup2(fd: int, fd2: int, inheritable: bool = ...) -> None: ... +def fstat(fd: int) -> stat_result: ... +def fsync(fd: int) -> None: ... +def lseek(fd: int, pos: int, how: int) -> int: ... +def open(file: _PathType, flags: int, mode: int = ..., *, dir_fd: Optional[int] = ...) -> int: ... +def pipe() -> Tuple[int, int]: ... +def read(fd: int, n: int) -> bytes: ... + +if sys.platform != 'win32': + # Unix only + def fchmod(fd: int, mode: int) -> None: ... + def fchown(fd: int, uid: int, gid: int) -> None: ... + def fdatasync(fd: int) -> None: ... # Unix only, not Mac + def fpathconf(fd: int, name: Union[str, int]) -> int: ... + def fstatvfs(fd: int) -> statvfs_result: ... + def ftruncate(fd: int, length: int) -> None: ... + def get_blocking(fd: int) -> bool: ... + def set_blocking(fd: int, blocking: bool) -> None: ... + def isatty(fd: int) -> bool: ... + def lockf(__fd: int, __cmd: int, __length: int) -> None: ... + def openpty() -> Tuple[int, int]: ... # some flavors of Unix + def pipe2(flags: int) -> Tuple[int, int]: ... # some flavors of Unix + def posix_fallocate(fd: int, offset: int, length: int) -> None: ... + def posix_fadvise(fd: int, offset: int, length: int, advice: int) -> None: ... + def pread(fd: int, buffersize: int, offset: int) -> bytes: ... + def pwrite(fd: int, string: bytes, offset: int) -> int: ... + @overload + def sendfile(__out_fd: int, __in_fd: int, offset: Optional[int], count: int) -> int: ... + @overload + def sendfile(__out_fd: int, __in_fd: int, offset: int, count: int, + headers: Sequence[bytes] = ..., trailers: Sequence[bytes] = ..., flags: int = ...) -> int: ... # FreeBSD and Mac OS X only + def readv(fd: int, buffers: Sequence[bytearray]) -> int: ... + def writev(fd: int, buffers: Sequence[bytes]) -> int: ... + +terminal_size = NamedTuple('terminal_size', [('columns', int), ('lines', int)]) +def get_terminal_size(fd: int = ...) -> terminal_size: ... + +def get_inheritable(fd: int) -> bool: ... +def set_inheritable(fd: int, inheritable: bool) -> None: ... + +if sys.platform != 'win32': + # Unix only + def tcgetpgrp(fd: int) -> int: ... + def tcsetpgrp(fd: int, pg: int) -> None: ... + def ttyname(fd: int) -> str: ... +def write(fd: int, string: bytes) -> int: ... +def access( + path: _FdOrPathType, + mode: int, + *, + dir_fd: Optional[int] = ..., + effective_ids: bool = ..., + follow_symlinks: bool = ..., +) -> bool: ... +def chdir(path: _FdOrPathType) -> None: ... +def fchdir(fd: int) -> None: ... +def getcwd() -> str: ... +def getcwdb() -> bytes: ... +def chmod(path: _FdOrPathType, mode: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ... +if sys.platform != 'win32': + def chflags(path: _PathType, flags: int, follow_symlinks: bool = ...) -> None: ... # some flavors of Unix + def chown(path: _FdOrPathType, uid: int, gid: int, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> None: ... # Unix only +if sys.platform != 'win32': + # Unix only + def chroot(path: _PathType) -> None: ... + def lchflags(path: _PathType, flags: int) -> None: ... + def lchmod(path: _PathType, mode: int) -> None: ... + def lchown(path: _PathType, uid: int, gid: int) -> None: ... +def link( + src: _PathType, + link_name: _PathType, + *, + src_dir_fd: Optional[int] = ..., + dst_dir_fd: Optional[int] = ..., + follow_symlinks: bool = ..., +) -> None: ... + +def lstat(path: _PathType, *, dir_fd: Optional[int] = ...) -> stat_result: ... +def mkdir(path: _PathType, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ... +if sys.platform != 'win32': + def mkfifo(path: _PathType, mode: int = ..., *, dir_fd: Optional[int] = ...) -> None: ... # Unix only +def makedirs(name: _PathType, mode: int = ..., exist_ok: bool = ...) -> None: ... +def mknod(path: _PathType, mode: int = ..., device: int = ..., *, dir_fd: Optional[int] = ...) -> None: ... +def major(device: int) -> int: ... +def minor(device: int) -> int: ... +def makedev(major: int, minor: int) -> int: ... +if sys.platform != 'win32': + def pathconf(path: _FdOrPathType, name: Union[str, int]) -> int: ... # Unix only +if sys.version_info >= (3, 6): + def readlink(path: Union[AnyStr, PathLike[AnyStr]], *, dir_fd: Optional[int] = ...) -> AnyStr: ... +else: + def readlink(path: AnyStr, *, dir_fd: Optional[int] = ...) -> AnyStr: ... +def remove(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ... +def removedirs(name: _PathType) -> None: ... +def rename(src: _PathType, dst: _PathType, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ... +def renames(old: _PathType, new: _PathType) -> None: ... +def replace(src: _PathType, dst: _PathType, *, src_dir_fd: Optional[int] = ..., dst_dir_fd: Optional[int] = ...) -> None: ... +def rmdir(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ... +if sys.version_info >= (3, 7): + class _ScandirIterator(Iterator[DirEntry[AnyStr]], ContextManager[_ScandirIterator[AnyStr]]): + def __next__(self) -> DirEntry[AnyStr]: ... + def close(self) -> None: ... + @overload + def scandir() -> _ScandirIterator[str]: ... + @overload + def scandir(path: int) -> _ScandirIterator[str]: ... + @overload + def scandir(path: Union[AnyStr, PathLike[AnyStr]]) -> _ScandirIterator[AnyStr]: ... +elif sys.version_info >= (3, 6): + class _ScandirIterator(Iterator[DirEntry[AnyStr]], ContextManager[_ScandirIterator[AnyStr]]): + def __next__(self) -> DirEntry[AnyStr]: ... + def close(self) -> None: ... + @overload + def scandir() -> _ScandirIterator[str]: ... + @overload + def scandir(path: Union[AnyStr, PathLike[AnyStr]]) -> _ScandirIterator[AnyStr]: ... +else: + @overload + def scandir() -> Iterator[DirEntry[str]]: ... + @overload + def scandir(path: AnyStr) -> Iterator[DirEntry[AnyStr]]: ... +def stat(path: _FdOrPathType, *, dir_fd: Optional[int] = ..., follow_symlinks: bool = ...) -> stat_result: ... +if sys.version_info < (3, 7): + @overload + def stat_float_times() -> bool: ... + @overload + def stat_float_times(__newvalue: bool) -> None: ... +if sys.platform != 'win32': + def statvfs(path: _FdOrPathType) -> statvfs_result: ... # Unix only +def symlink( + source: _PathType, + link_name: _PathType, + target_is_directory: bool = ..., + *, + dir_fd: Optional[int] = ..., +) -> None: ... +if sys.platform != 'win32': + def sync() -> None: ... # Unix only +def truncate(path: _FdOrPathType, length: int) -> None: ... # Unix only up to version 3.4 +def unlink(path: _PathType, *, dir_fd: Optional[int] = ...) -> None: ... +def utime( + path: _FdOrPathType, + times: Optional[Union[Tuple[int, int], Tuple[float, float]]] = ..., + *, + ns: Tuple[int, int] = ..., + dir_fd: Optional[int] = ..., + follow_symlinks: bool = ..., +) -> None: ... + +_OnError = Callable[[OSError], Any] + +if sys.version_info >= (3, 6): + def walk(top: Union[AnyStr, PathLike[AnyStr]], topdown: bool = ..., + onerror: Optional[_OnError] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... +else: + def walk(top: AnyStr, topdown: bool = ..., onerror: Optional[_OnError] = ..., + followlinks: bool = ...) -> Iterator[Tuple[AnyStr, List[AnyStr], + List[AnyStr]]]: ... +if sys.platform != 'win32': + if sys.version_info >= (3, 7): + @overload + def fwalk(top: Union[str, PathLike[str]] = ..., topdown: bool = ..., + onerror: Optional[_OnError] = ..., *, follow_symlinks: bool = ..., + dir_fd: Optional[int] = ...) -> Iterator[Tuple[str, List[str], List[str], int]]: ... + @overload + def fwalk(top: bytes, topdown: bool = ..., + onerror: Optional[_OnError] = ..., *, follow_symlinks: bool = ..., + dir_fd: Optional[int] = ...) -> Iterator[Tuple[bytes, List[bytes], List[bytes], int]]: ... + elif sys.version_info >= (3, 6): + def fwalk(top: Union[str, PathLike[str]] = ..., topdown: bool = ..., + onerror: Optional[_OnError] = ..., *, follow_symlinks: bool = ..., + dir_fd: Optional[int] = ...) -> Iterator[Tuple[str, List[str], List[str], int]]: ... + else: + def fwalk(top: str = ..., topdown: bool = ..., + onerror: Optional[_OnError] = ..., *, follow_symlinks: bool = ..., + dir_fd: Optional[int] = ...) -> Iterator[Tuple[str, List[str], List[str], int]]: ... + def getxattr(path: _FdOrPathType, attribute: _PathType, *, follow_symlinks: bool = ...) -> bytes: ... # Linux only + def listxattr(path: _FdOrPathType, *, follow_symlinks: bool = ...) -> List[str]: ... # Linux only + def removexattr(path: _FdOrPathType, attribute: _PathType, *, follow_symlinks: bool = ...) -> None: ... # Linux only + def setxattr(path: _FdOrPathType, attribute: _PathType, value: bytes, flags: int = ..., *, + follow_symlinks: bool = ...) -> None: ... # Linux only + +def abort() -> NoReturn: ... +# These are defined as execl(file, *args) but the first *arg is mandatory. +def execl(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ... +def execlp(file: _PathType, __arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> NoReturn: ... + +# These are: execle(file, *args, env) but env is pulled from the last element of the args. +def execle(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ... +def execlpe(file: _PathType, __arg0: Union[bytes, Text], *args: Any) -> NoReturn: ... + +# The docs say `args: tuple or list of strings` +# The implementation enforces tuple or list so we can't use Sequence. +_ExecVArgs = Union[Tuple[Union[bytes, Text], ...], List[bytes], List[Text], List[Union[bytes, Text]]] +def execv(path: _PathType, args: _ExecVArgs) -> NoReturn: ... +def execve(path: _FdOrPathType, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ... +def execvp(file: _PathType, args: _ExecVArgs) -> NoReturn: ... +def execvpe(file: _PathType, args: _ExecVArgs, env: Mapping[str, str]) -> NoReturn: ... + +def _exit(n: int) -> NoReturn: ... +def kill(pid: int, sig: int) -> None: ... +if sys.platform != 'win32': + # Unix only + def fork() -> int: ... + def forkpty() -> Tuple[int, int]: ... # some flavors of Unix + def killpg(pgid: int, sig: int) -> None: ... + def nice(increment: int) -> int: ... + def plock(op: int) -> None: ... # ???op is int? + +class _wrap_close(_TextIOWrapper): + def close(self) -> Optional[int]: ... # type: ignore +def popen(command: str, mode: str = ..., buffering: int = ...) -> _wrap_close: ... + +def spawnl(mode: int, path: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... +def spawnle(mode: int, path: _PathType, arg0: Union[bytes, Text], + *args: Any) -> int: ... # Imprecise sig +def spawnv(mode: int, path: _PathType, args: List[Union[bytes, Text]]) -> int: ... +def spawnve(mode: int, path: _PathType, args: List[Union[bytes, Text]], + env: Mapping[str, str]) -> int: ... +def system(command: _PathType) -> int: ... +def times() -> times_result: ... +def waitpid(pid: int, options: int) -> Tuple[int, int]: ... + +if sys.platform == 'win32': + def startfile(path: _PathType, operation: Optional[str] = ...) -> None: ... +else: + # Unix only + def spawnlp(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Union[bytes, Text]) -> int: ... + def spawnlpe(mode: int, file: _PathType, arg0: Union[bytes, Text], *args: Any) -> int: ... # Imprecise signature + def spawnvp(mode: int, file: _PathType, args: List[Union[bytes, Text]]) -> int: ... + def spawnvpe(mode: int, file: _PathType, args: List[Union[bytes, Text]], env: Mapping[str, str]) -> int: ... + def wait() -> Tuple[int, int]: ... # Unix only + from posix import waitid_result + def waitid(idtype: int, ident: int, options: int) -> waitid_result: ... + def wait3(options: int) -> Tuple[int, int, Any]: ... + def wait4(pid: int, options: int) -> Tuple[int, int, Any]: ... + def WCOREDUMP(status: int) -> bool: ... + def WIFCONTINUED(status: int) -> bool: ... + def WIFSTOPPED(status: int) -> bool: ... + def WIFSIGNALED(status: int) -> bool: ... + def WIFEXITED(status: int) -> bool: ... + def WEXITSTATUS(status: int) -> int: ... + def WSTOPSIG(status: int) -> int: ... + def WTERMSIG(status: int) -> int: ... + +if sys.platform != 'win32': + from posix import sched_param + def sched_get_priority_min(policy: int) -> int: ... # some flavors of Unix + def sched_get_priority_max(policy: int) -> int: ... # some flavors of Unix + def sched_setscheduler(pid: int, policy: int, param: sched_param) -> None: ... # some flavors of Unix + def sched_getscheduler(pid: int) -> int: ... # some flavors of Unix + def sched_setparam(pid: int, param: sched_param) -> None: ... # some flavors of Unix + def sched_getparam(pid: int) -> sched_param: ... # some flavors of Unix + def sched_rr_get_interval(pid: int) -> float: ... # some flavors of Unix + def sched_yield() -> None: ... # some flavors of Unix + def sched_setaffinity(pid: int, mask: Iterable[int]) -> None: ... # some flavors of Unix + def sched_getaffinity(pid: int) -> Set[int]: ... # some flavors of Unix + +def cpu_count() -> Optional[int]: ... +if sys.platform != 'win32': + # Unix only + def confstr(name: Union[str, int]) -> Optional[str]: ... + def getloadavg() -> Tuple[float, float, float]: ... + def sysconf(name: Union[str, int]) -> int: ... +if sys.version_info >= (3, 6): + def getrandom(size: int, flags: int = ...) -> bytes: ... + def urandom(size: int) -> bytes: ... +else: + def urandom(n: int) -> bytes: ... + +if sys.version_info >= (3, 7): + def register_at_fork(func: Callable[..., object], when: str) -> None: ... + +if sys.version_info >= (3, 8): + if sys.platform == "win32": + class _AddedDllDirectory: + path: Optional[str] + def close(self) -> None: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, *args: Any) -> None: ... + def add_dll_directory(path: str) -> _AddedDllDirectory: ... + if sys.platform == "linux": + MFD_CLOEXEC: int + MFD_ALLOW_SEALING: int + MFD_HUGETLB: int + MFD_HUGE_SHIFT: int + MFD_HUGE_MASK: int + MFD_HUGE_64KB: int + MFD_HUGE_512KB: int + MFD_HUGE_1MB: int + MFD_HUGE_2MB: int + MFD_HUGE_8MB: int + MFD_HUGE_16MB: int + MFD_HUGE_32MB: int + MFD_HUGE_256MB: int + MFD_HUGE_512MB: int + MFD_HUGE_1GB: int + MFD_HUGE_2GB: int + MFD_HUGE_16GB: int + def memfd_create(name: str, flags: int = ...) -> int: ... diff --git a/python/helpers/typeshed/stdlib/3/os/path.pyi b/python/helpers/typeshed/stdlib/3/os/path.pyi new file mode 100644 index 000000000000..42409c0e8721 --- /dev/null +++ b/python/helpers/typeshed/stdlib/3/os/path.pyi @@ -0,0 +1,177 @@ +# NB: path.pyi and stdlib/2 and stdlib/3 must remain consistent! +# Stubs for os.path +# Ron Murawski + +import os +import sys +from typing import overload, List, Any, AnyStr, Sequence, Tuple, TypeVar, Union, Text, Callable, Optional + +_T = TypeVar('_T') + +if sys.version_info >= (3, 6): + from builtins import _PathLike + _PathType = Union[bytes, Text, _PathLike] + _StrPath = Union[Text, _PathLike[Text]] + _BytesPath = Union[bytes, _PathLike[bytes]] +else: + _PathType = Union[bytes, Text] + _StrPath = Text + _BytesPath = bytes + +# ----- os.path variables ----- +supports_unicode_filenames: bool +# aliases (also in os) +curdir: str +pardir: str +sep: str +if sys.platform == 'win32': + altsep: str +else: + altsep: Optional[str] +extsep: str +pathsep: str +defpath: str +devnull: str + +# ----- os.path function stubs ----- +if sys.version_info >= (3, 6): + # Overloads are necessary to work around python/mypy#3644. + @overload + def abspath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def abspath(path: AnyStr) -> AnyStr: ... + @overload + def basename(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def basename(path: AnyStr) -> AnyStr: ... + @overload + def dirname(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def dirname(path: AnyStr) -> AnyStr: ... + @overload + def expanduser(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expanduser(path: AnyStr) -> AnyStr: ... + @overload + def expandvars(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def expandvars(path: AnyStr) -> AnyStr: ... + @overload + def normcase(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normcase(path: AnyStr) -> AnyStr: ... + @overload + def normpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + @overload + def realpath(path: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(path: AnyStr) -> AnyStr: ... + else: + @overload + def realpath(filename: _PathLike[AnyStr]) -> AnyStr: ... + @overload + def realpath(filename: AnyStr) -> AnyStr: ... + +else: + def abspath(path: AnyStr) -> AnyStr: ... + def basename(path: AnyStr) -> AnyStr: ... + def dirname(path: AnyStr) -> AnyStr: ... + def expanduser(path: AnyStr) -> AnyStr: ... + def expandvars(path: AnyStr) -> AnyStr: ... + def normcase(path: AnyStr) -> AnyStr: ... + def normpath(path: AnyStr) -> AnyStr: ... + if sys.platform == 'win32': + def realpath(path: AnyStr) -> AnyStr: ... + else: + def realpath(filename: AnyStr) -> AnyStr: ... + +if sys.version_info >= (3, 6): + # In reality it returns str for sequences of _StrPath and bytes for sequences + # of _BytesPath, but mypy does not accept such a signature. + def commonpath(paths: Sequence[_PathType]) -> Any: ... +elif sys.version_info >= (3, 5): + def commonpath(paths: Sequence[AnyStr]) -> AnyStr: ... + +# NOTE: Empty lists results in '' (str) regardless of contained type. +# Also, in Python 2 mixed sequences of Text and bytes results in either Text or bytes +# So, fall back to Any +def commonprefix(list: Sequence[_PathType]) -> Any: ... + +if sys.version_info >= (3, 3): + def exists(path: Union[_PathType, int]) -> bool: ... +else: + def exists(path: _PathType) -> bool: ... +def lexists(path: _PathType) -> bool: ... + +# These return float if os.stat_float_times() == True, +# but int is a subclass of float. +def getatime(path: _PathType) -> float: ... +def getmtime(path: _PathType) -> float: ... +def getctime(path: _PathType) -> float: ... + +def getsize(path: _PathType) -> int: ... +def isabs(path: _PathType) -> bool: ... +def isfile(path: _PathType) -> bool: ... +def isdir(path: _PathType) -> bool: ... +def islink(path: _PathType) -> bool: ... +def ismount(path: _PathType) -> bool: ... + +if sys.version_info < (3, 0): + # Make sure signatures are disjunct, and allow combinations of bytes and unicode. + # (Since Python 2 allows that, too) + # Note that e.g. os.path.join("a", "b", "c", "d", u"e") will still result in + # a type error. + @overload + def join(__p1: bytes, *p: bytes) -> bytes: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: bytes, __p4: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: bytes, __p3: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: bytes, __p2: Text, *p: _PathType) -> Text: ... + @overload + def join(__p1: Text, *p: _PathType) -> Text: ... +elif sys.version_info >= (3, 6): + # Mypy complains that the signatures overlap (same for relpath below), but things seem to behave correctly anyway. + @overload + def join(path: _StrPath, *paths: _StrPath) -> Text: ... + @overload + def join(path: _BytesPath, *paths: _BytesPath) -> bytes: ... +else: + def join(path: AnyStr, *paths: AnyStr) -> AnyStr: ... + +@overload +def relpath(path: _BytesPath, start: Optional[_BytesPath] = ...) -> bytes: ... +@overload +def relpath(path: _StrPath, start: Optional[_StrPath] = ...) -> Text: ... + +def samefile(path1: _PathType, path2: _PathType) -> bool: ... +def sameopenfile(fp1: int, fp2: int) -> bool: ... +def samestat(stat1: os.stat_result, stat2: os.stat_result) -> bool: ... + +if sys.version_info >= (3, 6): + @overload + def split(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: _PathLike[AnyStr]) -> Tuple[AnyStr, AnyStr]: ... + @overload + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... +else: + def split(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitdrive(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + def splitext(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... + +if sys.platform == 'win32': + def splitunc(path: AnyStr) -> Tuple[AnyStr, AnyStr]: ... # deprecated + +if sys.version_info < (3,): + def walk(path: AnyStr, visit: Callable[[_T, AnyStr, List[AnyStr]], Any], arg: _T) -> None: ... diff --git a/python/helpers/typeshed/stdlib/3/posix.pyi b/python/helpers/typeshed/stdlib/3/posix.pyi new file mode 100644 index 000000000000..fe9782354c94 --- /dev/null +++ b/python/helpers/typeshed/stdlib/3/posix.pyi @@ -0,0 +1,131 @@ +# Stubs for posix + +# NOTE: These are incomplete! + +import sys +from typing import List, NamedTuple, Optional, overload + +from os import stat_result as stat_result + +if sys.version_info >= (3, 6): + from builtins import _PathLike # See comment in builtins + +uname_result = NamedTuple('uname_result', [ + ('sysname', str), + ('nodename', str), + ('release', str), + ('version', str), + ('machine', str), +]) + +times_result = NamedTuple('times_result', [ + ('user', float), + ('system', float), + ('children_user', float), + ('children_system', float), + ('elapsed', float), +]) + +waitid_result = NamedTuple('waitid_result', [ + ('si_pid', int), + ('si_uid', int), + ('si_signo', int), + ('si_status', int), + ('si_code', int), +]) + +sched_param = NamedTuple('sched_param', [ + ('sched_priority', int), +]) + + +EX_CANTCREAT: int +EX_CONFIG: int +EX_DATAERR: int +EX_IOERR: int +EX_NOHOST: int +EX_NOINPUT: int +EX_NOPERM: int +EX_NOTFOUND: int +EX_NOUSER: int +EX_OK: int +EX_OSERR: int +EX_OSFILE: int +EX_PROTOCOL: int +EX_SOFTWARE: int +EX_TEMPFAIL: int +EX_UNAVAILABLE: int +EX_USAGE: int + +F_OK: int +R_OK: int +W_OK: int +X_OK: int + +if sys.version_info >= (3, 6): + GRND_NONBLOCK: int + GRND_RANDOM: int +NGROUPS_MAX: int + +O_APPEND: int +O_ACCMODE: int +O_ASYNC: int +O_CREAT: int +O_DIRECT: int +O_DIRECTORY: int +O_DSYNC: int +O_EXCL: int +O_LARGEFILE: int +O_NDELAY: int +O_NOATIME: int +O_NOCTTY: int +O_NOFOLLOW: int +O_NONBLOCK: int +O_RDONLY: int +O_RDWR: int +O_RSYNC: int +O_SYNC: int +O_TRUNC: int +O_WRONLY: int + +ST_APPEND: int +ST_MANDLOCK: int +ST_NOATIME: int +ST_NODEV: int +ST_NODIRATIME: int +ST_NOEXEC: int +ST_NOSUID: int +ST_RDONLY: int +ST_RELATIME: int +ST_SYNCHRONOUS: int +ST_WRITE: int + +TMP_MAX: int +WCONTINUED: int +WCOREDUMP: int +WEXITSTATUS: int +WIFCONTINUED: int +WIFEXITED: int +WIFSIGNALED: int +WIFSTOPPED: int +WNOHANG: int +WSTOPSIG: int +WTERMSIG: int +WUNTRACED: int + +if sys.version_info >= (3, 6): + @overload + def listdir(path: Optional[str] = ...) -> List[str]: ... + @overload + def listdir(path: bytes) -> List[bytes]: ... + @overload + def listdir(path: int) -> List[str]: ... + @overload + def listdir(path: _PathLike[str]) -> List[str]: ... +else: + @overload + def listdir(path: Optional[str] = ...) -> List[str]: ... + @overload + def listdir(path: bytes) -> List[bytes]: ... + @overload + def listdir(path: int) -> List[str]: ... diff --git a/python/python-psi-api/src/com/jetbrains/python/PyNames.java b/python/python-psi-api/src/com/jetbrains/python/PyNames.java index 50dc8929fcbf..565e11dad42e 100644 --- a/python/python-psi-api/src/com/jetbrains/python/PyNames.java +++ b/python/python-psi-api/src/com/jetbrains/python/PyNames.java @@ -158,6 +158,7 @@ public class PyNames { public static final String ABC_SET = "Set"; public static final String ABC_MUTABLE_SET = "MutableSet"; public static final String PATH_LIKE = "PathLike"; + public static final String BUILTIN_PATH_LIKE = "_PathLike"; public static final String AWAITABLE = "Awaitable"; public static final String ASYNC_ITERABLE = "AsyncIterable"; diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/typing/PyTypingTypeProvider.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/typing/PyTypingTypeProvider.java index 0535014daf12..86a1c947f194 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/typing/PyTypingTypeProvider.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/typing/PyTypingTypeProvider.java @@ -1441,17 +1441,6 @@ public class PyTypingTypeProvider extends PyTypeProviderBase { continue; } } - if (isBuiltinPathLike(element)) { - // see https://github.com/python/typeshed/commit/41561f11c7b06368aebe512acf69d8010662266d - // or comment in typeshed/stdlib/3/builtins.pyi near _PathLike class - final QualifiedName osPathLikeQName = QualifiedName.fromComponents("os", PyNames.PATH_LIKE); - final PsiElement osPathLike = - PyResolveImportUtil.resolveTopLevelMember(osPathLikeQName, PyResolveImportUtil.fromFoothold(element)); - if (osPathLike != null) { - elements.add(Pair.create(null, osPathLike)); - continue; - } - } if (element != null) { elements.add(Pair.create(null, element)); } @@ -1473,12 +1462,6 @@ public class PyTypingTypeProvider extends PyTypeProviderBase { return Collections.singletonList(expression); } - private static boolean isBuiltinPathLike(@Nullable PsiElement element) { - return element instanceof PyClass && - PyBuiltinCache.getInstance(element).isBuiltin(element) && - ("_" + PyNames.PATH_LIKE).equals(((PyClass)element).getName()); - } - @NotNull public static Collection resolveToQualifiedNames(@NotNull PyExpression expression, @NotNull TypeEvalContext context) { final Set names = Sets.newLinkedHashSet(); diff --git a/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java b/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java index 4513c7e1f0d5..b0a92ff0a013 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java +++ b/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java @@ -72,12 +72,12 @@ public class PyTypeModelBuilder { } } - static class CollectionOf extends TypeModel { - private final String collectionName; + private static class CollectionOf extends TypeModel { + private final TypeModel collectionType; private final List elementTypes; - private CollectionOf(String collectionName, List elementTypes) { - this.collectionName = collectionName; + private CollectionOf(TypeModel collectionType, List elementTypes) { + this.collectionType = collectionType; this.elementTypes = elementTypes; } @@ -270,14 +270,14 @@ public class PyTypeModelBuilder { result = new TupleType(elementModels, tupleType.isHomogeneous()); } else if (type instanceof PyCollectionType) { - final String name = type.getName(); - final List elementTypes = ((PyCollectionType)type).getElementTypes(); + final PyCollectionType asCollection = (PyCollectionType)type; final List elementModels = new ArrayList<>(); - for (PyType elementType : elementTypes) { + for (PyType elementType : asCollection.getElementTypes()) { elementModels.add(build(elementType, true)); } if (!elementModels.isEmpty()) { - result = new CollectionOf(name, elementModels); + final TypeModel collectionType = build(new PyClassTypeImpl(asCollection.getPyClass(), asCollection.isDefinition()), false); + result = new CollectionOf(collectionType, elementModels); } } else if (type instanceof PyUnionType && allowUnions) { @@ -304,6 +304,9 @@ public class PyTypeModelBuilder { else if (type instanceof PyGenericType) { result = new GenericType(type.getName()); } + else if (type != null && type.isBuiltin() && PyNames.BUILTIN_PATH_LIKE.equals(type.getName())) { + result = new NamedType(PyNames.PATH_LIKE); + } if (result == null) { result = NamedType.nameOrAny(type); } @@ -483,6 +486,7 @@ public class PyTypeModelBuilder { private abstract static class TypeNameVisitor implements TypeVisitor { private int myDepth = 0; private final static int MAX_DEPTH = 6; + private boolean switchBuiltinToTyping = false; @Override public void oneOf(OneOf oneOf) { @@ -522,7 +526,7 @@ public class PyTypeModelBuilder { } final boolean allTypeParamsAreAny = ContainerUtil.and(collectionOf.elementTypes, t -> t == NamedType.ANY); if (allTypeParamsAreAny) { - name(collectionOf.collectionName); + collectionOf.collectionType.accept(this); } else { typingGenericFormat(collectionOf); @@ -531,8 +535,11 @@ public class PyTypeModelBuilder { } protected void typingGenericFormat(CollectionOf collectionOf) { - final String name = collectionOf.collectionName; - addType(PyTypingTypeProvider.TYPING_COLLECTION_CLASSES.getOrDefault(name, name)); + final boolean prevSwitchBuiltinToTyping = switchBuiltinToTyping; + switchBuiltinToTyping = true; + collectionOf.collectionType.accept(this); + switchBuiltinToTyping = prevSwitchBuiltinToTyping; + if (!collectionOf.elementTypes.isEmpty()) { add("["); processList(collectionOf.elementTypes); @@ -544,7 +551,7 @@ public class PyTypeModelBuilder { @Override public void name(String name) { - addType(name); + addType(switchBuiltinToTyping ? PyTypingTypeProvider.TYPING_COLLECTION_CLASSES.getOrDefault(name, name) : name); } @Override diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/types/PyABCUtil.java b/python/python-psi-impl/src/com/jetbrains/python/psi/types/PyABCUtil.java index 10041326e459..c2026847f9e8 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/types/PyABCUtil.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/types/PyABCUtil.java @@ -100,7 +100,7 @@ public class PyABCUtil { if (PyNames.AWAITABLE.equals(superClassName)) { return hasMethod(subClass, PyNames.DUNDER_AWAIT, inherited, context); } - if (PyNames.PATH_LIKE.equals(superClassName)) { + if (PyNames.BUILTIN_PATH_LIKE.equals(superClassName)) { return hasMethod(subClass, PyNames.FSPATH, inherited, context); } return false; diff --git a/python/python-psi-impl/test/com/jetbrains/python/completion/PythonCompletionTest.java b/python/python-psi-impl/test/com/jetbrains/python/completion/PythonCompletionTest.java index f594e052a701..33cc278bb5e0 100644 --- a/python/python-psi-impl/test/com/jetbrains/python/completion/PythonCompletionTest.java +++ b/python/python-psi-impl/test/com/jetbrains/python/completion/PythonCompletionTest.java @@ -854,17 +854,6 @@ public class PythonCompletionTest extends PyTestCase { assertSameElements(suggested, "VAR", "subpkg1"); } - - // PY-14519 - public void testOsPath() { - myFixture.copyDirectoryToProject(getTestName(true), ""); - myFixture.configureByFile("a.py"); - myFixture.completeBasic(); - final List suggested = myFixture.getLookupElementStrings(); - assertNotNull(suggested); - assertContainsElements(suggested, "path"); - } - // PY-14331 public void testExcludedTopLevelPackage() { myFixture.copyDirectoryToProject(getTestName(true), ""); diff --git a/python/src/META-INF/python-core-common.xml b/python/src/META-INF/python-core-common.xml index ca16ba9fbbb3..32788e1e2f08 100644 --- a/python/src/META-INF/python-core-common.xml +++ b/python/src/META-INF/python-core-common.xml @@ -714,7 +714,6 @@ - diff --git a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibModuleMembersProvider.java b/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibModuleMembersProvider.java deleted file mode 100644 index 415c8a411888..000000000000 --- a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibModuleMembersProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.jetbrains.python.codeInsight.stdlib; - -import com.intellij.openapi.util.SystemInfo; -import com.intellij.psi.PsiElement; -import com.intellij.psi.util.QualifiedName; -import com.jetbrains.python.codeInsight.PyCustomMember; -import com.jetbrains.python.psi.PyFile; -import com.jetbrains.python.psi.resolve.ResolveImportUtil; -import com.jetbrains.python.psi.types.PyModuleMembersProvider; -import com.jetbrains.python.psi.types.TypeEvalContext; -import org.jetbrains.annotations.NotNull; - -import java.util.Collection; -import java.util.Collections; - -/** - * @author yole - */ -public class PyStdlibModuleMembersProvider extends PyModuleMembersProvider { - - @Override - @NotNull - protected Collection getMembersByQName(@NotNull PyFile module, @NotNull String qName, @NotNull TypeEvalContext context) { - if (qName.equals("os")) { - final String pathModuleName = SystemInfo.isWindows ? "ntpath" : "posixpath"; - final PsiElement path = ResolveImportUtil.resolveModuleInRoots(QualifiedName.fromDottedString(pathModuleName), module); - return Collections.singletonList(new PyCustomMember("path", path)); - } - return Collections.emptyList(); - } -} diff --git a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java b/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java index 7c6fa4fba5e9..61dbc8a4c6f4 100644 --- a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java @@ -28,7 +28,7 @@ import static com.jetbrains.python.psi.PyUtil.as; public class PyStdlibTypeProvider extends PyTypeProviderBase { @NotNull - private static final Set OPEN_FUNCTIONS = ImmutableSet.of("os.fdopen", "posix.fdopen", "nt.fdopen"); + private static final Set OPEN_FUNCTIONS = ImmutableSet.of("os.fdopen", "posix.fdopen"); @Nullable public static PyStdlibTypeProvider getInstance() { diff --git a/python/testData/MockSdk3.7/Lib/genericpath.py b/python/testData/MockSdk3.7/Lib/genericpath.py deleted file mode 100644 index 303b3b349a9f..000000000000 --- a/python/testData/MockSdk3.7/Lib/genericpath.py +++ /dev/null @@ -1,151 +0,0 @@ -""" -Path operations common to more than one OS -Do not use directly. The OS specific modules import the appropriate -functions from this module themselves. -""" -import os -import stat - -__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', - 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile', - 'samestat'] - - -# Does a path exist? -# This is false for dangling symbolic links on systems that support them. -def exists(path): - """Test whether a path exists. Returns False for broken symbolic links""" - try: - os.stat(path) - except OSError: - return False - return True - - -# This follows symbolic links, so both islink() and isdir() can be true -# for the same path on systems that support symlinks -def isfile(path): - """Test whether a path is a regular file""" - try: - st = os.stat(path) - except OSError: - return False - return stat.S_ISREG(st.st_mode) - - -# Is a path a directory? -# This follows symbolic links, so both islink() and isdir() -# can be true for the same path on systems that support symlinks -def isdir(s): - """Return true if the pathname refers to an existing directory.""" - try: - st = os.stat(s) - except OSError: - return False - return stat.S_ISDIR(st.st_mode) - - -def getsize(filename): - """Return the size of a file, reported by os.stat().""" - return os.stat(filename).st_size - - -def getmtime(filename): - """Return the last modification time of a file, reported by os.stat().""" - return os.stat(filename).st_mtime - - -def getatime(filename): - """Return the last access time of a file, reported by os.stat().""" - return os.stat(filename).st_atime - - -def getctime(filename): - """Return the metadata change time of a file, reported by os.stat().""" - return os.stat(filename).st_ctime - - -# Return the longest prefix of all list elements. -def commonprefix(m): - "Given a list of pathnames, returns the longest common leading component" - if not m: return '' - # Some people pass in a list of pathname parts to operate in an OS-agnostic - # fashion; don't try to translate in that case as that's an abuse of the - # API and they are already doing what they need to be OS-agnostic and so - # they most likely won't be using an os.PathLike object in the sublists. - if not isinstance(m[0], (list, tuple)): - m = tuple(map(os.fspath, m)) - s1 = min(m) - s2 = max(m) - for i, c in enumerate(s1): - if c != s2[i]: - return s1[:i] - return s1 - -# Are two stat buffers (obtained from stat, fstat or lstat) -# describing the same file? -def samestat(s1, s2): - """Test whether two stat buffers reference the same file""" - return (s1.st_ino == s2.st_ino and - s1.st_dev == s2.st_dev) - - -# Are two filenames really pointing to the same file? -def samefile(f1, f2): - """Test whether two pathnames reference the same actual file""" - s1 = os.stat(f1) - s2 = os.stat(f2) - return samestat(s1, s2) - - -# Are two open files really referencing the same file? -# (Not necessarily the same file descriptor!) -def sameopenfile(fp1, fp2): - """Test whether two open file objects reference the same file""" - s1 = os.fstat(fp1) - s2 = os.fstat(fp2) - return samestat(s1, s2) - - -# Split a path in root and extension. -# The extension is everything starting at the last dot in the last -# pathname component; the root is everything before that. -# It is always true that root + ext == p. - -# Generic implementation of splitext, to be parametrized with -# the separators -def _splitext(p, sep, altsep, extsep): - """Split the extension from a pathname. - - Extension is everything from the last dot to the end, ignoring - leading dots. Returns "(root, ext)"; ext may be empty.""" - # NOTE: This code must work for text and bytes strings. - - sepIndex = p.rfind(sep) - if altsep: - altsepIndex = p.rfind(altsep) - sepIndex = max(sepIndex, altsepIndex) - - dotIndex = p.rfind(extsep) - if dotIndex > sepIndex: - # skip all leading dots - filenameIndex = sepIndex + 1 - while filenameIndex < dotIndex: - if p[filenameIndex:filenameIndex+1] != extsep: - return p[:dotIndex], p[dotIndex:] - filenameIndex += 1 - - return p, p[:0] - -def _check_arg_types(funcname, *args): - hasstr = hasbytes = False - for s in args: - if isinstance(s, str): - hasstr = True - elif isinstance(s, bytes): - hasbytes = True - else: - raise TypeError('%s() argument must be str or bytes, not %r' % - (funcname, s.__class__.__name__)) from None - if hasstr and hasbytes: - raise TypeError("Can't mix strings and bytes in path components") from None diff --git a/python/testData/MockSdk3.7/Lib/ntpath.py b/python/testData/MockSdk3.7/Lib/ntpath.py deleted file mode 100644 index f0e03a2f496a..000000000000 --- a/python/testData/MockSdk3.7/Lib/ntpath.py +++ /dev/null @@ -1,669 +0,0 @@ -# Module 'ntpath' -- common operations on WinNT/Win95 pathnames -"""Common pathname manipulations, WindowsNT/95 version. - -Instead of importing this module directly, import os and refer to this -module as os.path. -""" - -# strings representing various path-related bits and pieces -# These are primarily for export; internally, they are hardcoded. -# Should be set before imports for resolving cyclic dependency. -curdir = '.' -pardir = '..' -extsep = '.' -sep = '\\' -pathsep = ';' -altsep = '/' -defpath = '.;C:\\bin' -devnull = 'nul' - -import os -import sys -import stat -import genericpath -from genericpath import * - -__all__ = ["normcase","isabs","join","splitdrive","split","splitext", - "basename","dirname","commonprefix","getsize","getmtime", - "getatime","getctime", "islink","exists","lexists","isdir","isfile", - "ismount", "expanduser","expandvars","normpath","abspath", - "curdir","pardir","sep","pathsep","defpath","altsep", - "extsep","devnull","realpath","supports_unicode_filenames","relpath", - "samefile", "sameopenfile", "samestat", "commonpath"] - -def _get_bothseps(path): - if isinstance(path, bytes): - return b'\\/' - else: - return '\\/' - -# Normalize the case of a pathname and map slashes to backslashes. -# Other normalizations (such as optimizing '../' away) are not done -# (this is done by normpath). - -def normcase(s): - """Normalize case of pathname. - - Makes all characters lowercase and all slashes into backslashes.""" - s = os.fspath(s) - try: - if isinstance(s, bytes): - return s.replace(b'/', b'\\').lower() - else: - return s.replace('/', '\\').lower() - except (TypeError, AttributeError): - if not isinstance(s, (bytes, str)): - raise TypeError("normcase() argument must be str or bytes, " - "not %r" % s.__class__.__name__) from None - raise - - -# Return whether a path is absolute. -# Trivial in Posix, harder on Windows. -# For Windows it is absolute if it starts with a slash or backslash (current -# volume), or if a pathname after the volume-letter-and-colon or UNC-resource -# starts with a slash or backslash. - -def isabs(s): - """Test whether a path is absolute""" - s = os.fspath(s) - s = splitdrive(s)[1] - return len(s) > 0 and s[0] in _get_bothseps(s) - - -# Join two (or more) paths. -def join(path, *paths): - path = os.fspath(path) - if isinstance(path, bytes): - sep = b'\\' - seps = b'\\/' - colon = b':' - else: - sep = '\\' - seps = '\\/' - colon = ':' - try: - if not paths: - path[:0] + sep #23780: Ensure compatible data type even if p is null. - result_drive, result_path = splitdrive(path) - for p in map(os.fspath, paths): - p_drive, p_path = splitdrive(p) - if p_path and p_path[0] in seps: - # Second path is absolute - if p_drive or not result_drive: - result_drive = p_drive - result_path = p_path - continue - elif p_drive and p_drive != result_drive: - if p_drive.lower() != result_drive.lower(): - # Different drives => ignore the first path entirely - result_drive = p_drive - result_path = p_path - continue - # Same drive in different case - result_drive = p_drive - # Second path is relative to the first - if result_path and result_path[-1] not in seps: - result_path = result_path + sep - result_path = result_path + p_path - ## add separator between UNC and non-absolute path - if (result_path and result_path[0] not in seps and - result_drive and result_drive[-1:] != colon): - return result_drive + sep + result_path - return result_drive + result_path - except (TypeError, AttributeError, BytesWarning): - genericpath._check_arg_types('join', path, *paths) - raise - - -# Split a path in a drive specification (a drive letter followed by a -# colon) and the path specification. -# It is always true that drivespec + pathspec == p -def splitdrive(p): - """Split a pathname into drive/UNC sharepoint and relative path specifiers. - Returns a 2-tuple (drive_or_unc, path); either part may be empty. - - If you assign - result = splitdrive(p) - It is always true that: - result[0] + result[1] == p - - If the path contained a drive letter, drive_or_unc will contain everything - up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir") - - If the path contained a UNC path, the drive_or_unc will contain the host name - and share up to but not including the fourth directory separator character. - e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir") - - Paths cannot contain both a drive letter and a UNC path. - - """ - p = os.fspath(p) - if len(p) >= 2: - if isinstance(p, bytes): - sep = b'\\' - altsep = b'/' - colon = b':' - else: - sep = '\\' - altsep = '/' - colon = ':' - normp = p.replace(altsep, sep) - if (normp[0:2] == sep*2) and (normp[2:3] != sep): - # is a UNC path: - # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path - # \\machine\mountpoint\directory\etc\... - # directory ^^^^^^^^^^^^^^^ - index = normp.find(sep, 2) - if index == -1: - return p[:0], p - index2 = normp.find(sep, index + 1) - # a UNC path can't have two slashes in a row - # (after the initial two) - if index2 == index + 1: - return p[:0], p - if index2 == -1: - index2 = len(p) - return p[:index2], p[index2:] - if normp[1:2] == colon: - return p[:2], p[2:] - return p[:0], p - - -# Split a path in head (everything up to the last '/') and tail (the -# rest). After the trailing '/' is stripped, the invariant -# join(head, tail) == p holds. -# The resulting head won't end in '/' unless it is the root. - -def split(p): - """Split a pathname. - - Return tuple (head, tail) where tail is everything after the final slash. - Either part may be empty.""" - p = os.fspath(p) - seps = _get_bothseps(p) - d, p = splitdrive(p) - # set i to index beyond p's last slash - i = len(p) - while i and p[i-1] not in seps: - i -= 1 - head, tail = p[:i], p[i:] # now tail has no slashes - # remove trailing slashes from head, unless it's all slashes - head = head.rstrip(seps) or head - return d + head, tail - - -# Split a path in root and extension. -# The extension is everything starting at the last dot in the last -# pathname component; the root is everything before that. -# It is always true that root + ext == p. - -def splitext(p): - p = os.fspath(p) - if isinstance(p, bytes): - return genericpath._splitext(p, b'\\', b'/', b'.') - else: - return genericpath._splitext(p, '\\', '/', '.') -splitext.__doc__ = genericpath._splitext.__doc__ - - -# Return the tail (basename) part of a path. - -def basename(p): - """Returns the final component of a pathname""" - return split(p)[1] - - -# Return the head (dirname) part of a path. - -def dirname(p): - """Returns the directory component of a pathname""" - return split(p)[0] - -# Is a path a symbolic link? -# This will always return false on systems where os.lstat doesn't exist. - -def islink(path): - """Test whether a path is a symbolic link. - This will always return false for Windows prior to 6.0. - """ - try: - st = os.lstat(path) - except (OSError, AttributeError): - return False - return stat.S_ISLNK(st.st_mode) - -# Being true for dangling symbolic links is also useful. - -def lexists(path): - """Test whether a path exists. Returns True for broken symbolic links""" - try: - st = os.lstat(path) - except OSError: - return False - return True - -# Is a path a mount point? -# Any drive letter root (eg c:\) -# Any share UNC (eg \\server\share) -# Any volume mounted on a filesystem folder -# -# No one method detects all three situations. Historically we've lexically -# detected drive letter roots and share UNCs. The canonical approach to -# detecting mounted volumes (querying the reparse tag) fails for the most -# common case: drive letter roots. The alternative which uses GetVolumePathName -# fails if the drive letter is the result of a SUBST. -try: - from nt import _getvolumepathname -except ImportError: - _getvolumepathname = None -def ismount(path): - """Test whether a path is a mount point (a drive root, the root of a - share, or a mounted volume)""" - path = os.fspath(path) - seps = _get_bothseps(path) - path = abspath(path) - root, rest = splitdrive(path) - if root and root[0] in seps: - return (not rest) or (rest in seps) - if rest in seps: - return True - - if _getvolumepathname: - return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps) - else: - return False - - -# Expand paths beginning with '~' or '~user'. -# '~' means $HOME; '~user' means that user's home directory. -# If the path doesn't begin with '~', or if the user or $HOME is unknown, -# the path is returned unchanged (leaving error reporting to whatever -# function is called with the expanded path as argument). -# See also module 'glob' for expansion of *, ? and [...] in pathnames. -# (A function should also be defined to do full *sh-style environment -# variable expansion.) - -def expanduser(path): - """Expand ~ and ~user constructs. - - If user or $HOME is unknown, do nothing.""" - path = os.fspath(path) - if isinstance(path, bytes): - tilde = b'~' - else: - tilde = '~' - if not path.startswith(tilde): - return path - i, n = 1, len(path) - while i < n and path[i] not in _get_bothseps(path): - i += 1 - - if 'HOME' in os.environ: - userhome = os.environ['HOME'] - elif 'USERPROFILE' in os.environ: - userhome = os.environ['USERPROFILE'] - elif not 'HOMEPATH' in os.environ: - return path - else: - try: - drive = os.environ['HOMEDRIVE'] - except KeyError: - drive = '' - userhome = join(drive, os.environ['HOMEPATH']) - - if isinstance(path, bytes): - userhome = os.fsencode(userhome) - - if i != 1: #~user - userhome = join(dirname(userhome), path[1:i]) - - return userhome + path[i:] - - -# Expand paths containing shell variable substitutions. -# The following rules apply: -# - no expansion within single quotes -# - '$$' is translated into '$' -# - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% -# - ${varname} is accepted. -# - $varname is accepted. -# - %varname% is accepted. -# - varnames can be made out of letters, digits and the characters '_-' -# (though is not verified in the ${varname} and %varname% cases) -# XXX With COMMAND.COM you can use any characters in a variable name, -# XXX except '^|<>='. - -def expandvars(path): - """Expand shell variables of the forms $var, ${var} and %var%. - - Unknown variables are left unchanged.""" - path = os.fspath(path) - if isinstance(path, bytes): - if b'$' not in path and b'%' not in path: - return path - import string - varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii') - quote = b'\'' - percent = b'%' - brace = b'{' - rbrace = b'}' - dollar = b'$' - environ = getattr(os, 'environb', None) - else: - if '$' not in path and '%' not in path: - return path - import string - varchars = string.ascii_letters + string.digits + '_-' - quote = '\'' - percent = '%' - brace = '{' - rbrace = '}' - dollar = '$' - environ = os.environ - res = path[:0] - index = 0 - pathlen = len(path) - while index < pathlen: - c = path[index:index+1] - if c == quote: # no expansion within single quotes - path = path[index + 1:] - pathlen = len(path) - try: - index = path.index(c) - res += c + path[:index + 1] - except ValueError: - res += c + path - index = pathlen - 1 - elif c == percent: # variable or '%' - if path[index + 1:index + 2] == percent: - res += c - index += 1 - else: - path = path[index+1:] - pathlen = len(path) - try: - index = path.index(percent) - except ValueError: - res += percent + path - index = pathlen - 1 - else: - var = path[:index] - try: - if environ is None: - value = os.fsencode(os.environ[os.fsdecode(var)]) - else: - value = environ[var] - except KeyError: - value = percent + var + percent - res += value - elif c == dollar: # variable or '$$' - if path[index + 1:index + 2] == dollar: - res += c - index += 1 - elif path[index + 1:index + 2] == brace: - path = path[index+2:] - pathlen = len(path) - try: - index = path.index(rbrace) - except ValueError: - res += dollar + brace + path - index = pathlen - 1 - else: - var = path[:index] - try: - if environ is None: - value = os.fsencode(os.environ[os.fsdecode(var)]) - else: - value = environ[var] - except KeyError: - value = dollar + brace + var + rbrace - res += value - else: - var = path[:0] - index += 1 - c = path[index:index + 1] - while c and c in varchars: - var += c - index += 1 - c = path[index:index + 1] - try: - if environ is None: - value = os.fsencode(os.environ[os.fsdecode(var)]) - else: - value = environ[var] - except KeyError: - value = dollar + var - res += value - if c: - index -= 1 - else: - res += c - index += 1 - return res - - -# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B. -# Previously, this function also truncated pathnames to 8+3 format, -# but as this module is called "ntpath", that's obviously wrong! - -def normpath(path): - """Normalize path, eliminating double slashes, etc.""" - path = os.fspath(path) - if isinstance(path, bytes): - sep = b'\\' - altsep = b'/' - curdir = b'.' - pardir = b'..' - special_prefixes = (b'\\\\.\\', b'\\\\?\\') - else: - sep = '\\' - altsep = '/' - curdir = '.' - pardir = '..' - special_prefixes = ('\\\\.\\', '\\\\?\\') - if path.startswith(special_prefixes): - # in the case of paths with these prefixes: - # \\.\ -> device names - # \\?\ -> literal paths - # do not do any normalization, but return the path unchanged - return path - path = path.replace(altsep, sep) - prefix, path = splitdrive(path) - - # collapse initial backslashes - if path.startswith(sep): - prefix += sep - path = path.lstrip(sep) - - comps = path.split(sep) - i = 0 - while i < len(comps): - if not comps[i] or comps[i] == curdir: - del comps[i] - elif comps[i] == pardir: - if i > 0 and comps[i-1] != pardir: - del comps[i-1:i+1] - i -= 1 - elif i == 0 and prefix.endswith(sep): - del comps[i] - else: - i += 1 - else: - i += 1 - # If the path is now empty, substitute '.' - if not prefix and not comps: - comps.append(curdir) - return prefix + sep.join(comps) - -def _abspath_fallback(path): - """Return the absolute version of a path as a fallback function in case - `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for - more. - - """ - - path = os.fspath(path) - if not isabs(path): - if isinstance(path, bytes): - cwd = os.getcwdb() - else: - cwd = os.getcwd() - path = join(cwd, path) - return normpath(path) - -# Return an absolute path. -try: - from nt import _getfullpathname - -except ImportError: # not running on Windows - mock up something sensible - abspath = _abspath_fallback - -else: # use native Windows method on Windows - def abspath(path): - """Return the absolute version of a path.""" - try: - return _getfullpathname(path) - except OSError: - return _abspath_fallback(path) - -# realpath is a no-op on systems without islink support -realpath = abspath -# Win9x family and earlier have no Unicode filename support. -supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and - sys.getwindowsversion()[3] >= 2) - -def relpath(path, start=None): - """Return a relative version of a path""" - path = os.fspath(path) - if isinstance(path, bytes): - sep = b'\\' - curdir = b'.' - pardir = b'..' - else: - sep = '\\' - curdir = '.' - pardir = '..' - - if start is None: - start = curdir - - if not path: - raise ValueError("no path specified") - - start = os.fspath(start) - try: - start_abs = abspath(normpath(start)) - path_abs = abspath(normpath(path)) - start_drive, start_rest = splitdrive(start_abs) - path_drive, path_rest = splitdrive(path_abs) - if normcase(start_drive) != normcase(path_drive): - raise ValueError("path is on mount %r, start on mount %r" % ( - path_drive, start_drive)) - - start_list = [x for x in start_rest.split(sep) if x] - path_list = [x for x in path_rest.split(sep) if x] - # Work out how much of the filepath is shared by start and path. - i = 0 - for e1, e2 in zip(start_list, path_list): - if normcase(e1) != normcase(e2): - break - i += 1 - - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) - except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning): - genericpath._check_arg_types('relpath', path, start) - raise - - -# Return the longest common sub-path of the sequence of paths given as input. -# The function is case-insensitive and 'separator-insensitive', i.e. if the -# only difference between two paths is the use of '\' versus '/' as separator, -# they are deemed to be equal. -# -# However, the returned path will have the standard '\' separator (even if the -# given paths had the alternative '/' separator) and will have the case of the -# first path given in the sequence. Additionally, any trailing separator is -# stripped from the returned path. - -def commonpath(paths): - """Given a sequence of path names, returns the longest common sub-path.""" - - if not paths: - raise ValueError('commonpath() arg is an empty sequence') - - paths = tuple(map(os.fspath, paths)) - if isinstance(paths[0], bytes): - sep = b'\\' - altsep = b'/' - curdir = b'.' - else: - sep = '\\' - altsep = '/' - curdir = '.' - - try: - drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths] - split_paths = [p.split(sep) for d, p in drivesplits] - - try: - isabs, = set(p[:1] == sep for d, p in drivesplits) - except ValueError: - raise ValueError("Can't mix absolute and relative paths") from None - - # Check that all drive letters or UNC paths match. The check is made only - # now otherwise type errors for mixing strings and bytes would not be - # caught. - if len(set(d for d, p in drivesplits)) != 1: - raise ValueError("Paths don't have the same drive") - - drive, path = splitdrive(paths[0].replace(altsep, sep)) - common = path.split(sep) - common = [c for c in common if c and c != curdir] - - split_paths = [[c for c in s if c and c != curdir] for s in split_paths] - s1 = min(split_paths) - s2 = max(split_paths) - for i, c in enumerate(s1): - if c != s2[i]: - common = common[:i] - break - else: - common = common[:len(s1)] - - prefix = drive + sep if isabs else drive - return prefix + sep.join(common) - except (TypeError, AttributeError): - genericpath._check_arg_types('commonpath', *paths) - raise - - -# determine if two files are in fact the same file -try: - # GetFinalPathNameByHandle is available starting with Windows 6.0. - # Windows XP and non-Windows OS'es will mock _getfinalpathname. - if sys.getwindowsversion()[:2] >= (6, 0): - from nt import _getfinalpathname - else: - raise ImportError -except (AttributeError, ImportError): - # On Windows XP and earlier, two files are the same if their absolute - # pathnames are the same. - # Non-Windows operating systems fake this method with an XP - # approximation. - def _getfinalpathname(f): - return normcase(abspath(f)) - - -try: - # The genericpath.isdir implementation uses os.stat and checks the mode - # attribute to tell whether or not the path is a directory. - # This is overkill on Windows - just pass the path to GetFileAttributes - # and check the attribute from there. - from nt import _isdir as isdir -except ImportError: - # Use genericpath.isdir as imported above. - pass diff --git a/python/testData/MockSdk3.7/Lib/os.py b/python/testData/MockSdk3.7/Lib/os.py deleted file mode 100644 index 499e6285618f..000000000000 --- a/python/testData/MockSdk3.7/Lib/os.py +++ /dev/null @@ -1,1078 +0,0 @@ -r"""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). -""" - -#' -import abc -import sys -import stat as st - -_names = sys.builtin_module_names - -# Note: more names are added to __all__ later. -__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep", - "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR", - "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen", - "popen", "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] != '_'] - -# Any new dependencies of the os module and/or 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 _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) # fstat always works - _add("HAVE_FTRUNCATE", "truncate") - _add("HAVE_FUTIMENS", "utime") - _add("HAVE_FUTIMES", "utime") - _add("HAVE_FPATHCONF", "pathconf") - if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3 - _add("HAVE_FSTATVFS", "statvfs") - supports_fd = _set - - _set = set() - _add("HAVE_FACCESSAT", "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 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") - _add("HAVE_FCHOWNAT", "chown") - _add("HAVE_FSTATAT", "stat") - _add("HAVE_LCHFLAGS", "chflags") - _add("HAVE_LCHMOD", "chmod") - if _exists("lchown"): # mac os x10.3 - _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 - - -# 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 -SEEK_SET = 0 -SEEK_CUR = 1 -SEEK_END = 2 - -# Super directory utilities. -# (Inspired by Eric Raymond; the doc strings are mostly his) - -def makedirs(name, mode=0o777, exist_ok=False): - """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. - - """ - 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: - # Defeats race condition when another thread created the path - pass - cdir = curdir - if isinstance(tail, bytes): - cdir = bytes(curdir, 'ASCII') - if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists - return - try: - mkdir(name, 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 exist_ok or not path.isdir(name): - raise - -def removedirs(name): - """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. - - """ - 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): - """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. - - """ - 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): - """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 (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 is ineffective, 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", end="") - 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 - - """ - top = fspath(top) - dirs = [] - nondirs = [] - walk_dirs = [] - - # We may not have read permission for top, in which case we can't - # get a list of the files the directory contains. os.walk - # always suppressed the exception then, rather than blow up for a - # minor reason when (say) a thousand readable directories are still - # left to visit. That logic is copied here. - try: - # Note that scandir is global in this module due - # to earlier import-*. - scandir_it = scandir(top) - except OSError as error: - if onerror is not None: - onerror(error) - return - - 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) - return - - try: - is_dir = entry.is_dir() - except OSError: - # If is_dir() raises an OSError, consider that the entry is not - # a directory, same behaviour than 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: recurse 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 that the - # entry is not a symbolic link, same behaviour than - # os.path.islink(). - is_symlink = False - walk_into = not is_symlink - - if walk_into: - walk_dirs.append(entry.path) - - # Yield before recursion if going top down - if topdown: - yield top, dirs, nondirs - - # Recurse into sub-directories - islink, join = path.islink, path.join - for dirname in dirs: - new_path = join(top, dirname) - # Issue #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. - if followlinks or not islink(new_path): - yield from walk(new_path, topdown, onerror, followlinks) - else: - # Recurse into sub-directories - for new_path in walk_dirs: - yield from walk(new_path, topdown, onerror, followlinks) - # Yield after recursion if going bottom up - yield top, dirs, nondirs - -__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): - """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 - """ - if not isinstance(top, int) or not hasattr(top, '__index__'): - top = fspath(top) - # Note: To guard against symlink races, we use the standard - # lstat()/open()/fstat() trick. - 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): - # 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. - - 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: - # Add dangling symlinks, ignore disappeared files - 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") - -# Make sure os.environ exists, at least -try: - environ -except NameError: - environ = {} - -def execl(file, *args): - """execl(file, *args) - - Execute the executable file with argument list args, replacing the - current process. """ - execv(file, args) - -def execle(file, *args): - """execle(file, *args, env) - - Execute the executable file with argument list args and - environment env, replacing the current process. """ - env = args[-1] - execve(file, args[:-1], env) - -def execlp(file, *args): - """execlp(file, *args) - - Execute the executable file (which is searched for along $PATH) - with argument list args, replacing the current process. """ - execvp(file, args) - -def execlpe(file, *args): - """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. """ - env = args[-1] - execvpe(file, args[:-1], env) - -def execvp(file, args): - """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) - -def execvpe(file, args, env): - """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. """ - _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): - """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. - import warnings - - if env is None: - env = environ - - # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a - # BytesWarning when using python -b or python -bb: ignore the warning - 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) - - -# Change environ to automatically call putenv(), unsetenv if they exist. -from _collections_abc import MutableMapping - -class _Environ(MutableMapping): - def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue, putenv, unsetenv): - self.encodekey = encodekey - self.decodekey = decodekey - self.encodevalue = encodevalue - self.decodevalue = decodevalue - self.putenv = putenv - self.unsetenv = unsetenv - self._data = data - - def __getitem__(self, key): - try: - value = self._data[self.encodekey(key)] - except KeyError: - # raise KeyError with the original key value - raise KeyError(key) from None - return self.decodevalue(value) - - def __setitem__(self, key, value): - key = self.encodekey(key) - value = self.encodevalue(value) - self.putenv(key, value) - self._data[key] = value - - def __delitem__(self, key): - encodedkey = self.encodekey(key) - self.unsetenv(encodedkey) - try: - del self._data[encodedkey] - except KeyError: - # raise KeyError with the original key value - raise KeyError(key) from None - - def __iter__(self): - # list() from dict object is an atomic operation - keys = list(self._data) - for key in keys: - yield self.decodekey(key) - - def __len__(self): - return len(self._data) - - def __repr__(self): - return 'environ({{{}}})'.format(', '.join( - ('{!r}: {!r}'.format(self.decodekey(key), self.decodevalue(value)) - for key, value in self._data.items()))) - - def copy(self): - return dict(self) - - def setdefault(self, key, value): - if key not in self: - self[key] = value - return self[key] - -try: - _putenv = putenv -except NameError: - _putenv = lambda key, value: None -else: - if "putenv" not in __all__: - __all__.append("putenv") - -try: - _unsetenv = unsetenv -except NameError: - _unsetenv = lambda key: _putenv(key, "") -else: - if "unsetenv" not in __all__: - __all__.append("unsetenv") - -def _createenviron(): - if name == 'nt': - # Where Env Var Names Must Be UPPERCASE - 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: - # Where Env Var Names Can Be Mixed Case - 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, - _putenv, _unsetenv) - -# unicode environ -environ = _createenviron() -del _createenviron - - -def getenv(key, default=None): - """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.""" - 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 - - # bytes environ - environb = _Environ(environ._data, - _check_bytes, bytes, - _check_bytes, bytes, - _putenv, _unsetenv) - del _check_bytes - - def getenvb(key, default=None): - """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.get(key, default) - - __all__.extend(("environb", "getenvb")) - -def _fscodec(): - encoding = sys.getfilesystemencoding() - errors = sys.getfilesystemencodeerrors() - - def fsencode(filename): - """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). - """ - filename = fspath(filename) # Does type-checking of `filename`. - if isinstance(filename, str): - return filename.encode(encoding, errors) - else: - return filename - - def fsdecode(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). - """ - filename = fspath(filename) # Does type-checking of `filename`. - if isinstance(filename, bytes): - return filename.decode(encoding, errors) - else: - return filename - - return fsencode, fsdecode - -fsencode, fsdecode = _fscodec() -del _fscodec - -# Supply spawn*() (probably only for Unix) -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"]) - - # 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*()? - - def _spawnvef(mode, file, args, env, func): - # Internal helper; func is the exec*() function to use - 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: - # Child - try: - if env is None: - func(file, args) - else: - func(file, args, env) - except: - _exit(127) - else: - # Parent - if mode == P_NOWAIT: - return pid # Caller is responsible for waiting! - while 1: - wpid, sts = waitpid(pid, 0) - if WIFSTOPPED(sts): - continue - elif WIFSIGNALED(sts): - return -WTERMSIG(sts) - elif WIFEXITED(sts): - return WEXITSTATUS(sts) - else: - raise OSError("Not stopped, signaled or exited???") - - def spawnv(mode, file, args): - """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. """ - return _spawnvef(mode, file, args, None, execv) - - def spawnve(mode, file, args, env): - """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. """ - return _spawnvef(mode, file, args, env, execve) - - # Note: spawnvp[e] isn't currently supported on Windows - - def spawnvp(mode, file, args): - """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. """ - return _spawnvef(mode, file, args, None, execvp) - - def spawnvpe(mode, file, args, env): - """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. """ - return _spawnvef(mode, file, args, env, execvpe) - - - __all__.extend(["spawnv", "spawnve", "spawnvp", "spawnvpe"]) - - -if _exists("spawnv"): - # These aren't supplied by the basic Windows code - # but can be easily implemented in Python - - def spawnl(mode, file, *args): - """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. """ - return spawnv(mode, file, args) - - def spawnle(mode, file, *args): - """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. """ - env = args[-1] - return spawnve(mode, file, args[:-1], env) - - - __all__.extend(["spawnl", "spawnle"]) - - -if _exists("spawnvp"): - # At the moment, Windows doesn't implement spawnvp[e], - # so it won't have spawnlp[e] either. - def spawnlp(mode, file, *args): - """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. """ - return spawnvp(mode, file, args) - - def spawnlpe(mode, file, *args): - """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. """ - env = args[-1] - return spawnvpe(mode, file, args[:-1], env) - - - __all__.extend(["spawnlp", "spawnlpe"]) - - -# Supply os.popen() -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, io - if mode == "r": - proc = subprocess.Popen(cmd, - shell=True, - stdout=subprocess.PIPE, - bufsize=buffering) - return _wrap_close(io.TextIOWrapper(proc.stdout), proc) - else: - proc = subprocess.Popen(cmd, - shell=True, - stdin=subprocess.PIPE, - bufsize=buffering) - return _wrap_close(io.TextIOWrapper(proc.stdin), proc) - -# Helper for popen() -- a proxy for a file whose close waits for the process -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 # Shift left to match old behavior - 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) - -# Supply os.fdopen() -def fdopen(fd, *args, **kwargs): - if not isinstance(fd, int): - raise TypeError("invalid fd type (%s, expected integer)" % type(fd)) - import io - return io.open(fd, *args, **kwargs) - - -# For testing purposes, make sure the function is available when the C -# implementation exists. -def _fspath(path): - """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. - """ - if isinstance(path, (str, bytes)): - return path - - # Work from the object's type to match method resolution of other magic - # methods. - 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__) - 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 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 PathLike(abc.ABC): - - """Abstract base class for implementing the file system path protocol.""" - - @abc.abstractmethod - def __fspath__(self): - """Return the file system path representation of the object.""" - raise NotImplementedError - - @classmethod - def __subclasshook__(cls, subclass): - return hasattr(subclass, '__fspath__') diff --git a/python/testData/completion/osPath/a.after.py b/python/testData/completion/osPath/a.after.py deleted file mode 100644 index 2e927028da70..000000000000 --- a/python/testData/completion/osPath/a.after.py +++ /dev/null @@ -1,3 +0,0 @@ -import os - -os.path diff --git a/python/testData/completion/osPath/a.py b/python/testData/completion/osPath/a.py deleted file mode 100644 index d0ceb4315866..000000000000 --- a/python/testData/completion/osPath/a.py +++ /dev/null @@ -1,3 +0,0 @@ -import os - -os.pat diff --git a/python/testData/completion/osPath/ntpath.py b/python/testData/completion/osPath/ntpath.py deleted file mode 100644 index 8b137891791f..000000000000 --- a/python/testData/completion/osPath/ntpath.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/python/testData/completion/osPath/os.py b/python/testData/completion/osPath/os.py deleted file mode 100644 index 8b137891791f..000000000000 --- a/python/testData/completion/osPath/os.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/python/testData/completion/osPath/posixpath.py b/python/testData/completion/osPath/posixpath.py deleted file mode 100644 index 8b137891791f..000000000000 --- a/python/testData/completion/osPath/posixpath.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/a.py b/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/a.py deleted file mode 100644 index d46a5cac74a6..000000000000 --- a/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/a.py +++ /dev/null @@ -1,2 +0,0 @@ -from os import popen -ret = popen("non-existent-command").close() \ No newline at end of file diff --git a/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/os.py b/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/os.py deleted file mode 100644 index be3e93c6ac47..000000000000 --- a/python/testData/inspections/PyNoneFunctionAssignmentInspection/OsPopenClose/os.py +++ /dev/null @@ -1,6 +0,0 @@ -def popen(command, mode='r', bufsize=-1): - pass - -class _wrap_close: - def close(self): - pass \ No newline at end of file diff --git a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/a.py b/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions.py similarity index 66% rename from python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/a.py rename to python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions.py index df433c5f3332..e121e37effa1 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/a.py +++ b/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions.py @@ -30,14 +30,14 @@ b = B() open(b) -os.fspath(b) # TODO fail after enabling pyi-stubs for `os` module -os.fsencode(b) # TODO fail after enabling pyi-stubs for `os` module -os.fsdecode(b) # TODO fail after enabling pyi-stubs for `os` module +os.fspath(b) +os.fsencode(b) +os.fsdecode(b) Path(b) PurePath(b) # TODO update after supporting versioning in pyi-stubs -os.path.abspath(b) +os.path.abspath(b) # pathlib.PurePath diff --git a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/os.py b/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/os.py deleted file mode 100644 index 4ca9470b3dc8..000000000000 --- a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/os.py +++ /dev/null @@ -1,30 +0,0 @@ -import abc -import posixpath as path - - -def _fscodec(): - pass - - -fsencode, fsdecode = _fscodec() - - -def _fspath(path): - pass - - -fspath = _fspath - - -class PathLike(abc.ABC): - - """Abstract base class for implementing the file system path protocol.""" - - @abc.abstractmethod - def __fspath__(self): - """Return the file system path representation of the object.""" - raise NotImplementedError - - @classmethod - def __subclasshook__(cls, subclass): - return hasattr(subclass, '__fspath__') \ No newline at end of file diff --git a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/posixpath.py b/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/posixpath.py deleted file mode 100644 index 0432fd6b9f4c..000000000000 --- a/python/testData/inspections/PyTypeCheckerInspection/PathLikePassedToStdlibFunctions/posixpath.py +++ /dev/null @@ -1,2 +0,0 @@ -def abspath(path): - pass \ No newline at end of file diff --git a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/a.py b/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/a.py deleted file mode 100644 index be866eaa1966..000000000000 --- a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/a.py +++ /dev/null @@ -1,2 +0,0 @@ -from os.path import join -# \ No newline at end of file diff --git a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/ntpath.py b/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/ntpath.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/os.py b/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/os.py deleted file mode 100644 index 8217b070f3eb..000000000000 --- a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/os.py +++ /dev/null @@ -1,4 +0,0 @@ -if a: - import posixpath as path -elif b: - import ntpath as path \ No newline at end of file diff --git a/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/posixpath.py b/python/testData/resolve/multiFile/osAttributesFromPosixPathAndNTPath/lib/posixpath.py deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/python/testSrc/com/jetbrains/python/PyMultiFileResolveTest.java b/python/testSrc/com/jetbrains/python/PyMultiFileResolveTest.java index aba809348836..f39f947cd1f1 100644 --- a/python/testSrc/com/jetbrains/python/PyMultiFileResolveTest.java +++ b/python/testSrc/com/jetbrains/python/PyMultiFileResolveTest.java @@ -23,7 +23,6 @@ import com.jetbrains.python.sdk.PythonSdkUtil; import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -597,28 +596,6 @@ public class PyMultiFileResolveTest extends PyMultiFileResolveTestCase { assertSameElements(doMultiResolveAndGetFileUrls("pkg1/pkg2/mod1.py"), "pkg1/pkg2/__init__.py"); } - // PY-28764 - public void testOsAttributesFromPosixPathAndNTPath() { - myFixture.copyDirectoryToProject(getTestName(true), ""); - - runWithSourceRoots( - Collections.singletonList(myFixture.findFileInTempDir("lib")), - () -> { - final PsiReference reference = PyResolveTestCase.findReferenceByMarker(myFixture.configureByFile("a.py")); - assertInstanceOf(reference, PsiPolyVariantReference.class); - - final List elements = PyUtil.multiResolveTopPriority((PsiPolyVariantReference)reference); - assertEquals(1, elements.size()); - - final PsiElement element = elements.get(0); - assertInstanceOf(element, PyFile.class); - - final VirtualFile file = ((PyFile)element).getVirtualFile(); - assertEquals("ntpath.py", file.getName()); - } - ); - } - // EA-121262 public void testIncompleteFromImport() { assertUnresolved(); diff --git a/python/testSrc/com/jetbrains/python/inspections/Py3NoneFunctionAssignmentInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/Py3NoneFunctionAssignmentInspectionTest.java index 2c2f292f9e71..7b46ea7b17cb 100644 --- a/python/testSrc/com/jetbrains/python/inspections/Py3NoneFunctionAssignmentInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/Py3NoneFunctionAssignmentInspectionTest.java @@ -28,11 +28,6 @@ public class Py3NoneFunctionAssignmentInspectionTest extends PyInspectionTestCas return ourPy3Descriptor; } - // PY-21040 - public void testOsPopenClose() { - doMultiFileTest(); - } - // PY-28729 public void testGenericSubstitutedWithNone() { doTestByText( diff --git a/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java index 9dc3830446d2..5abb510f091d 100644 --- a/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java @@ -149,7 +149,7 @@ public class Py3TypeCheckerInspectionTest extends PyInspectionTestCase { // PY-20769 public void testPathLikePassedToStdlibFunctions() { - doMultiFileTest(); + doTest(); } // PY-21048 diff --git a/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java b/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java index 6ee7eb78bf51..a4a070c46245 100644 --- a/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/quickFixes/PyAddImportQuickFixTest.java @@ -155,7 +155,17 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase { // PY-20976 public void testCombinedElementOrdering() { - doTestProposedImportsOrdering("path", "path from sys", "first.path", "first.second.path()", "os.path", "first._third.path"); + runWithAdditionalFileInLibDir( + "os/__init__.py", + "", + (__) -> + runWithAdditionalFileInLibDir( + "os/path.py", + "", + (___) -> doTestProposedImportsOrdering("path", + "path from sys", "first.path", "first.second.path()", "os.path", "first._third.path") + ) + ); } // PY-20976 @@ -163,7 +173,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase { runWithAdditionalFileInLibDir( "sys.py", "path = 10", - (__) -> doTestProposedImportsOrdering("path", "pkg.path", "sys.path", "os.path") + (__) -> doTestProposedImportsOrdering("path", "pkg.path", "sys.path") ); } @@ -172,7 +182,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase { runWithAdditionalFileInLibDir( "sys.py", "path = 10", - (__) -> doTestProposedImportsOrdering("path", "first.second.path", "sys.path", "os.path", "_private.path") + (__) -> doTestProposedImportsOrdering("path", "first.second.path", "sys.path", "_private.path") ); } @@ -193,7 +203,16 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase { // PY-20976 public void testOrderingWithExistingImport() { - doTestProposedImportsOrdering("path", "path from sys", "src.path", "os.path"); + runWithAdditionalFileInLibDir( + "os/__init__.py", + "", + (__) -> + runWithAdditionalFileInLibDir( + "os/path.py", + "", + (___) -> doTestProposedImportsOrdering("path", "path from sys", "src.path", "os.path") + ) + ); } private void doTestProposedImportsOrdering(@NotNull String text, @NotNull String... expected) { diff --git a/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts b/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts index 27d60f8a98d6..f30969523142 100644 --- a/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts +++ b/python/tools/src/com/jetbrains/python/tools/PyTypeShedSync.kts @@ -43,15 +43,22 @@ val whiteList = setOf( "email", "exceptions", "functools", + "genericpath", "io", "itertools", "logging", + "macpath", "math", "mock", "multiprocessing", + "ntpath", "numbers", "pathlib", "queue", + "os", + "os2emxpath", + "posix", + "posixpath", "re", "shutil", "signal",