os: improve bytes handling (#9072)

This commit is contained in:
Jelle Zijlstra
2022-11-06 21:32:46 -08:00
committed by GitHub
parent 340eb1264b
commit 68924e00e5

View File

@@ -503,11 +503,14 @@ if sys.platform != "win32":
def getenvb(key: bytes) -> bytes | None: ...
@overload
def getenvb(key: bytes, default: _T) -> bytes | _T: ...
def putenv(__name: StrOrBytesPath, __value: StrOrBytesPath) -> None: ...
def unsetenv(__name: StrOrBytesPath) -> None: ...
def putenv(__name: bytes | str, __value: bytes | str) -> None: ...
else:
def putenv(__name: str, __value: str) -> None: ...
if sys.platform != "win32" or sys.version_info >= (3, 9):
def unsetenv(__name: bytes | str) -> None: ...
if sys.version_info >= (3, 9):
def unsetenv(__name: str) -> None: ...
_Opener: TypeAlias = Callable[[str, int], int]
@@ -622,7 +625,7 @@ if sys.platform != "win32":
def posix_fadvise(__fd: int, __offset: int, __length: int, __advice: int) -> None: ...
def pread(__fd: int, __length: int, __offset: int) -> bytes: ...
def pwrite(__fd: int, __buffer: bytes, __offset: int) -> int: ...
def pwrite(__fd: int, __buffer: ReadableBuffer, __offset: int) -> int: ...
# In CI, stubtest sometimes reports that these are available on MacOS, sometimes not
def preadv(__fd: int, __buffers: SupportsLenAndGetItem[WriteableBuffer], __offset: int, __flags: int = ...) -> int: ...
def pwritev(__fd: int, __buffers: SupportsLenAndGetItem[ReadableBuffer], __offset: int, __flags: int = ...) -> int: ...
@@ -641,8 +644,8 @@ if sys.platform != "win32":
in_fd: int,
offset: int,
count: int,
headers: Sequence[bytes] = ...,
trailers: Sequence[bytes] = ...,
headers: Sequence[ReadableBuffer] = ...,
trailers: Sequence[ReadableBuffer] = ...,
flags: int = ...,
) -> int: ... # FreeBSD and Mac OS X only
def readv(__fd: int, __buffers: SupportsLenAndGetItem[WriteableBuffer]) -> int: ...
@@ -671,7 +674,7 @@ if sys.platform != "win32":
def tcsetpgrp(__fd: int, __pgid: int) -> None: ...
def ttyname(__fd: int) -> str: ...
def write(__fd: int, __data: bytes) -> int: ...
def write(__fd: int, __data: ReadableBuffer) -> int: ...
def access(
path: _FdOrAnyPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
) -> bool: ...
@@ -775,14 +778,19 @@ if sys.platform != "win32":
) -> Iterator[tuple[str, list[str], list[str], int]]: ...
@overload
def fwalk(
top: bytes, topdown: bool = ..., onerror: _OnError | None = ..., *, follow_symlinks: bool = ..., dir_fd: int | None = ...
top: BytesPath,
topdown: bool = ...,
onerror: _OnError | None = ...,
*,
follow_symlinks: bool = ...,
dir_fd: int | None = ...,
) -> Iterator[tuple[bytes, list[bytes], list[bytes], int]]: ...
if sys.platform == "linux":
def getxattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
def listxattr(path: _FdOrAnyPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
def removexattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
def setxattr(
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: bytes, flags: int = ..., *, follow_symlinks: bool = ...
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: ReadableBuffer, flags: int = ..., *, follow_symlinks: bool = ...
) -> None: ...
def abort() -> NoReturn: ...
@@ -810,6 +818,10 @@ _ExecVArgs: TypeAlias = (
| list[str | PathLike[Any]]
| list[bytes | str | PathLike[Any]]
)
# Depending on the OS, the keys and values are passed either to
# PyUnicode_FSDecoder (which accepts str | ReadableBuffer) or to
# PyUnicode_FSConverter (which accepts StrOrBytesPath). For simplicity,
# we limit to str | bytes.
_ExecEnv: TypeAlias = Mapping[bytes, bytes | str] | Mapping[str, bytes | str]
def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ...