Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 21 additions & 3 deletions stdlib/@tests/test_cases/multiprocessing/check_ctypes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from __future__ import annotations

from ctypes import c_char, c_float, c_int
from multiprocessing import Array, Value
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray, SynchronizedString
import ctypes
from ctypes import Structure, c_char, c_float, c_int
from multiprocessing import Array, Value, sharedctypes
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray, SynchronizedBase, SynchronizedString
from typing import Any
from typing_extensions import assert_type


class Point(Structure):
_fields_ = [("x", c_int), ("y", c_int)]


string = Array(c_char, 12)
assert_type(string, SynchronizedString)
assert_type(string.value, bytes)
Expand All @@ -17,3 +24,14 @@
field = Value(c_float, 0.0)
assert_type(field, Synchronized[float])
field.value = 1.2

assert_type(Value("i", 0), Synchronized[Any])
assert_type(Value(c_int, 0, lock=False), c_int)
assert_type(Value(Point), SynchronizedBase[Point])
assert_type(Array(c_int, 3, lock=False), ctypes.Array[c_int])

assert_type(sharedctypes.Value(c_int, 0), Synchronized[int])
assert_type(sharedctypes.Value("i", 0), Synchronized[Any])
assert_type(sharedctypes.Value(c_int, 0, lock=False), c_int)
assert_type(sharedctypes.Value(Point), SynchronizedBase[Point])
assert_type(sharedctypes.Array(c_int, 3, lock=False), ctypes.Array[c_int])
10 changes: 5 additions & 5 deletions stdlib/multiprocessing/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from multiprocessing import popen_fork, popen_forkserver, popen_spawn_posix, pop
from multiprocessing.managers import SyncManager
from multiprocessing.pool import Pool as _Pool
from multiprocessing.process import BaseProcess
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray, SynchronizedString
from multiprocessing.sharedctypes import Synchronized, SynchronizedArray, SynchronizedBase, SynchronizedString
from typing import Any, ClassVar, Literal, TypeAlias, TypeVar, overload

if sys.platform != "win32":
Expand Down Expand Up @@ -90,18 +90,18 @@ class BaseContext:
self, typecode_or_type: type[_SimpleCData[_T]], *args: Any, lock: Literal[True] | _LockLike = True
) -> Synchronized[_T]: ...
@overload
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[False]) -> Synchronized[_CT]: ...
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[False]) -> _CT: ...
@overload
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[True] | _LockLike = True) -> Synchronized[_CT]: ...
def Value(self, typecode_or_type: type[_CT], *args: Any, lock: Literal[True] | _LockLike = True) -> SynchronizedBase[_CT]: ...
@overload
def Value(self, typecode_or_type: str, *args: Any, lock: Literal[True] | _LockLike = True) -> Synchronized[Any]: ...
@overload
def Value(self, typecode_or_type: str | type[_CData], *args: Any, lock: bool | _LockLike = True) -> Any: ...

@overload
def Array(
self, typecode_or_type: type[_SimpleCData[_T]], size_or_initializer: int | Sequence[Any], *, lock: Literal[False]
) -> SynchronizedArray[_T]: ...
self, typecode_or_type: type[_CT], size_or_initializer: int | Sequence[Any], *, lock: Literal[False]
) -> ctypes.Array[_CT]: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contradicts the types in .sharedctypes. I don't know which is correct, but it should be consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR both modules return ctypes.Array[_CT] for lock=False. The sharedctypes.pyi change is further down in the diff (on main it returned _CT, which was also wrong). They have to agree, since BaseContext.Array/Value just forward to the sharedctypes functions (context.py). At runtime, Array(c_int, 3, lock=False) returns a c_int_Array_3 instance from both.

@overload
def Array(
self, typecode_or_type: type[c_char], size_or_initializer: int | Sequence[Any], *, lock: Literal[True] | _LockLike = True
Expand Down
8 changes: 6 additions & 2 deletions stdlib/multiprocessing/sharedctypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ def RawArray(typecode_or_type: str, size_or_initializer: int | Sequence[Any]) ->
@overload
def Value(typecode_or_type: type[_CT], *args: Any, lock: Literal[False], ctx: BaseContext | None = None) -> _CT: ...
@overload
def Value(
typecode_or_type: type[_SimpleCData[_T]], *args: Any, lock: Literal[True] | _LockLike = True, ctx: BaseContext | None = None
) -> Synchronized[_T]: ...
@overload
def Value(
typecode_or_type: type[_CT], *args: Any, lock: Literal[True] | _LockLike = True, ctx: BaseContext | None = None
) -> SynchronizedBase[_CT]: ...
@overload
def Value(
typecode_or_type: str, *args: Any, lock: Literal[True] | _LockLike = True, ctx: BaseContext | None = None
) -> SynchronizedBase[Any]: ...
) -> Synchronized[Any]: ...
@overload
def Value(
typecode_or_type: str | type[_CData], *args: Any, lock: bool | _LockLike = True, ctx: BaseContext | None = None
Expand All @@ -40,7 +44,7 @@ def Value(
@overload
def Array(
typecode_or_type: type[_CT], size_or_initializer: int | Sequence[Any], *, lock: Literal[False], ctx: BaseContext | None = None
) -> _CT: ...
) -> ctypes.Array[_CT]: ...
@overload
def Array(
typecode_or_type: type[c_char],
Expand Down
Loading