fcntl: various improvements (#3680)

* fcntl: mark positional-only args

* fcntl: use overload for fcntl.fcntl

The comment about depending on the type of arg seems incorrect
bf501353a0

I checked the docs and examples, CPython implementation and CPython tests, but
I might be missing something

* fcntl: use overload for fcntl.ioctl

Based off of docs and examples

* fcntl: type buffers better

Follows the approach in #2610
This commit is contained in:
Shantanu
2020-02-21 21:31:58 -08:00
committed by GitHub
parent af2767d9d5
commit 1b7eadce95

View File

@@ -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: ...