8000 os: improve bytes handling (#9072) · python/typeshed@68924e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68924e0

Browse files
os: improve bytes handling (#9072)
1 parent 340eb12 commit 68924e0

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

stdlib/os/__init__.pyi

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,14 @@ if sys.platform != "win32":
503503
def getenvb(key: bytes) -> bytes | None: ...
504504
@overload
505505
def getenvb(key: bytes, default: _T) -> bytes | _T: ...
506+
def putenv(__name: StrOrBytesPath, __value: StrOrBytesPath) -> None: ...
507+
def unsetenv(__name: StrOrBytesPath) -> None: ...
506508

507-
def putenv(__name: bytes | str, __value: bytes | str) -> None: ...
509+
else:
510+
def putenv(__name: str, __value: str) -> None: ...
508511

509-
if sys.platform != "win32" or sys.version_info >= (3, 9):
510-
def unsetenv(__name: bytes | str) -> None: ...
512+
if sys.version_info >= (3, 9):
513+
def unsetenv(__name: str) -> None: ...
511514

512515
_Opener: TypeAlias = Callable[[str, int], int]
513516

@@ -622,7 +625,7 @@ if sys.platform != "win32":
622625
def posix_fadvise(__fd: int, __offset: int, __length: int, __advice: int) -> None: ...
623626

624627
def pread(__fd: int, __length: int, __offset: int) -> bytes: ...
625-
def pwrite(__fd: int, __buffer: bytes, __offset: int) -> int: ...
628+
def pwrite(__fd: int, __buffer: ReadableBuffer, __offset: int) -> int: ...
626629
# In CI, stubtest sometimes reports that these are available on MacOS, sometimes not
627630
def preadv(__fd: int, __buffers: SupportsLenAndGetItem[WriteableBuffer], __offset: int, __flags: int = ...) -> int: ...
628631
def pwritev(__fd: int, __buffers: SupportsLenAndGetItem[ReadableBuffer], __offset: int, __flags: int = ...) -> int: ...
@@ -641,8 +644,8 @@ if sys.platform != "win32":
641644
in_fd: int,
642645
offset: int,
643646
count: int,
644-
headers: Sequence[bytes] = ...,
645-
trailers: Sequence[bytes] = ...,
647+
headers: Sequence[ReadableBuffer] = ...,
648+
trailers: Sequence[ReadableBuffer] = ...,
646649
flags: int = ...,
647650
) -> int: ... # FreeBSD and Mac OS X only
648651
def readv(__fd: int, __buffers: SupportsLenAndGetItem[WriteableBuffer]) -> int: ...
@@ -671,7 +674,7 @@ if sys.platform != "win32":
671674
def tcsetpgrp(__fd: int, __pgid: int) -> None: ...
672675
def ttyname(__fd: int) -> str: ...
673676

674-
def write(__fd: int, __data: bytes) -> int: ...
677+
def write(__fd: int, __data: ReadableBuffer) -> int: ...
675678
def access(
676679
path: _FdOrAnyPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
677680
) -> bool: ...
@@ -775,14 +778,19 @@ if sys.platform != "win32":
775778
) -> Iterator[tuple[str, list[str], list[str], int]]: ...
776779
@overload
777780
def fwalk(
778-
top: bytes, topdown: bool = ..., onerror: _OnError | None = ..., *, follow_symlinks: bool = ..., dir_fd: int | None = ...
781+
top: BytesPath,
782+
topdown: bool = ...,
783+
onerror: _OnError | None = ...,
784+
*,
785+
follow_symlinks: bool = ...,
786+
dir_fd: int | None = ...,
779787
) -> Iterator[tuple[bytes, list[bytes], list[bytes], int]]: ...
780788
if sys.platform == "linux":
781789
def getxattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
782790
def listxattr(path: _FdOrAnyPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
783791
def removexattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
784792
def setxattr(
785-
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: bytes, flags: int = ..., *, follow_symlinks: bool = ...
793+
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: ReadableBuffer, flags: int = ..., *, follow_symlinks: bool = ...
786794
) -> None: ...
787795

788796
def abort() -> NoReturn: ...
@@ -810,6 +818,10 @@ _ExecVArgs: TypeAlias = (
810818
| list[str | PathLike[Any]]
811819
| list[bytes | str | PathLike[Any]]
812820
)
821+
# Depending on the OS, the keys and values are passed either to
822+
# PyUnicode_FSDecoder (which accepts str | ReadableBuffer) or to
823+
# PyUnicode_FSConverter (which accepts StrOrBytesPath). For simplicity,
824+
# we limit to str | bytes.
813825
_ExecEnv: TypeAlias = Mapping[bytes, bytes | str] | Mapping[str, bytes | str]
814826

815827
def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ...

0 commit comments

Comments
 (0)
0