diff --git a/stdlib/3/fcntl.pyi b/stdlib/3/fcntl.pyi index 6f9bdf232..b9df14753 100644 --- a/stdlib/3/fcntl.pyi +++ b/stdlib/3/fcntl.pyi @@ -1,7 +1,13 @@ # Stubs for fcntl +from array import array from io import IOBase -from typing import Any, IO, Union +from typing import IO, Any, Union, overload from _types import FileDescriptorLike +import sys +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal FASYNC: int FD_CLOEXEC: int @@ -76,20 +82,39 @@ LOCK_SH: int LOCK_UN: int LOCK_WRITE: int -# TODO All these return either int or bytes depending on the value of -# cmd (not on the type of arg). -def fcntl(fd: FileDescriptorLike, - cmd: int, - arg: Union[int, bytes] = ...) -> Any: ... -# TODO This function accepts any object supporting a buffer interface, -# as arg, is there a better way to express this than bytes? -def ioctl(fd: FileDescriptorLike, - request: int, - arg: Union[int, bytes] = ..., - mutate_flag: bool = ...) -> Any: ... -def flock(fd: FileDescriptorLike, operation: int) -> None: ... -def lockf(fd: FileDescriptorLike, - cmd: int, - len: int = ..., - start: int = ..., - whence: int = ...) -> Any: ... +@overload +def fcntl(__fd: FileDescriptorLike, + __cmd: int, + __arg: int = ...) -> int: ... +@overload +def fcntl(__fd: FileDescriptorLike, + __cmd: int, + __arg: bytes) -> bytes: ... +_ReadOnlyBuffer = bytes +_WritableBuffer = Union[bytearray, memoryview, array] +@overload +def ioctl(__fd: FileDescriptorLike, + __request: int, + __arg: int = ..., + __mutate_flag: bool = ...) -> int: ... +@overload +def ioctl(__fd: FileDescriptorLike, + __request: int, + __arg: _WritableBuffer, + __mutate_flag: Literal[True] = ...) -> int: ... +@overload +def ioctl(__fd: FileDescriptorLike, + __request: int, + __arg: _WritableBuffer, + __mutate_flag: Literal[False]) -> bytes: ... +@overload +def ioctl(__fd: FileDescriptorLike, + __request: int, + __arg: _ReadOnlyBuffer, + __mutate_flag: bool) -> bytes: ... +def flock(__fd: FileDescriptorLike, __operation: int) -> None: ... +def lockf(__fd: FileDescriptorLike, + __cmd: int, + __len: int = ..., + __start: int = ..., + __whence: int = ...) -> Any: ...