Skip to content

Commit e467fd5

Browse files
authored
multiprocessing: add sharedctypes, fix some stubtest cases (#4282)
1 parent 564a822 commit e467fd5

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

stdlib/3/multiprocessing/__init__.pyi

+15-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import Any, Callable, Iterable, Optional, List, Union, Sequence, Tup
33
from typing_extensions import Literal
44
from ctypes import _CData
55
from logging import Logger
6-
from multiprocessing import connection, pool, spawn, synchronize
6+
from multiprocessing import connection, pool, sharedctypes, spawn, synchronize
77
from multiprocessing.context import (
88
AuthenticationError as AuthenticationError,
99
BaseContext,
@@ -26,7 +26,6 @@ if sys.version_info >= (3, 8):
2626
if sys.platform != "win32":
2727
from multiprocessing.context import ForkContext, ForkServerContext
2828

29-
3029
# N.B. The functions below are generated at runtime by partially applying
3130
# multiprocessing.context.BaseContext's methods, so the two signatures should
3231
# be identical (modulo self).
@@ -50,30 +49,21 @@ def Pool(processes: Optional[int] = ...,
5049
initargs: Iterable[Any] = ...,
5150
maxtasksperchild: Optional[int] = ...) -> pool.Pool: ...
5251

53-
class Array:
54-
value: Any = ...
55-
56-
def __init__(self, typecode_or_type: Union[str, Type[_CData]], size_or_initializer: Union[int, Sequence[Any]], *, lock: Union[bool, _LockLike] = ...) -> None: ...
57-
def acquire(self) -> bool: ...
58-
def release(self) -> bool: ...
59-
def get_lock(self) -> _LockLike: ...
60-
def get_obj(self) -> Any: ...
61-
62-
@overload
63-
def __getitem__(self, key: int) -> Any: ...
64-
@overload
65-
def __getitem__(self, key: slice) -> List[Any]: ...
66-
def __getslice__(self, start: int, stop: int) -> Any: ...
67-
def __setitem__(self, key: int, value: Any) -> None: ...
68-
52+
# Functions Array and Value are copied from context.pyi.
53+
# See https://github.com/python/typeshed/blob/ac234f25927634e06d9c96df98d72d54dd80dfc4/stdlib/2and3/turtle.pyi#L284-L291
54+
# for rationale
55+
def Array(
56+
typecode_or_type: Any,
57+
size_or_initializer: Union[int, Sequence[Any]],
58+
*,
59+
lock: bool = ...
60+
) -> sharedctypes._Array: ...
6961

70-
class Value():
71-
value: Any = ...
72-
def __init__(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> None: ...
73-
def get_lock(self) -> _LockLike: ...
74-
def get_obj(self) -> Any: ...
75-
def acquire(self) -> bool: ...
76-
def release(self) -> bool: ...
62+
def Value(
63+
typecode_or_type: Any,
64+
*args: Any,
65+
lock: bool = ...
66+
) -> sharedctypes._Value: ...
7767

7868
# ----- multiprocessing function stubs -----
7969
def allow_connection_pickling() -> None: ...

stdlib/3/multiprocessing/context.pyi

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from logging import Logger
22
import multiprocessing
3+
from multiprocessing import sharedctypes
34
from multiprocessing import synchronize
45
from multiprocessing import queues
56
from multiprocessing.process import BaseProcess
@@ -73,23 +74,21 @@ class BaseContext(object):
7374
def RawArray(self, typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]]) -> Any: ...
7475
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
7576
# how to handle the ctype
76-
# TODO: change return to Value once a stub exists in multiprocessing.sharedctypes
7777
def Value(
7878
self,
7979
typecode_or_type: Any,
8080
*args: Any,
8181
lock: bool = ...
82-
) -> Any: ...
82+
) -> sharedctypes._Value: ...
8383
# TODO: typecode_or_type param is a ctype with a base class of _SimpleCData or array.typecode Need to figure out
8484
# how to handle the ctype
85-
# TODO: change return to Array once a stub exists in multiprocessing.sharedctypes
8685
def Array(
8786
self,
8887
typecode_or_type: Any,
8988
size_or_initializer: Union[int, Sequence[Any]],
9089
*,
9190
lock: bool = ...
92-
) -> Any: ...
91+
) -> sharedctypes._Array: ...
9392
def freeze_support(self) -> None: ...
9493
def get_logger(self) -> Logger: ...
9594
def log_to_stderr(self, level: Optional[str] = ...) -> Logger: ...
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from ctypes import _CData
2+
from typing import Any, List, Optional, Sequence, Type, Union, overload
3+
4+
from multiprocessing.synchronize import _LockLike
5+
from multiprocessing.context import BaseContext
6+
7+
class _Array:
8+
value: Any = ...
9+
10+
def __init__(self, typecode_or_type: Union[str, Type[_CData]], size_or_initializer: Union[int, Sequence[Any]], *, lock: Union[bool, _LockLike] = ...) -> None: ...
11+
def acquire(self) -> bool: ...
12+
def release(self) -> bool: ...
13+
def get_lock(self) -> _LockLike: ...
14+
def get_obj(self) -> Any: ...
15+
16+
@overload
17+
def __getitem__(self, key: int) -> Any: ...
18+
@overload
19+
def __getitem__(self, key: slice) -> List[Any]: ...
20+
def __getslice__(self, start: int, stop: int) -> Any: ...
21+
def __setitem__(self, key: int, value: Any) -> None: ...
22+
23+
class _Value():
24+
value: Any = ...
25+
def __init__(self, typecode_or_type: Union[str, Type[_CData]], *args: Any, lock: Union[bool, _LockLike] = ...) -> None: ...
26+
def get_lock(self) -> _LockLike: ...
27+
def get_obj(self) -> Any: ...
28+
def acquire(self) -> bool: ...
29+
def release(self) -> bool: ...
30+
31+
def Array(
32+
typecode_or_type: Union[str, Type[_CData]],
33+
size_or_initializer: Union[int, Sequence[Any]],
34+
*,
35+
lock: Union[bool, _LockLike] = ...,
36+
ctx: Optional[BaseContext] = ...) -> _Array: ...
37+
38+
def Value(
39+
typecode_or_type: Union[str, Type[_CData]],
40+
*args: Any,
41+
lock: Union[bool, _LockLike] = ...,
42+
ctx: Optional[BaseContext] = ...) -> _Value: ...

0 commit comments

Comments
 (0)