Skip to content

os: improve bytes handling #9072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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: ...
Expand Down