From a948db02f3294593fa7164bbfc34e2ff466f73ad Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 23 Jun 2025 15:34:25 +0300 Subject: [PATCH 1/5] Fix `shared` type in `_interpreters` It is a `dict[str, Any]` or `None`: ```python >>> import _interpreters >>> i = _interpreters.create() >>> _interpreters.exec(i, 'print(1)', shared=False) Traceback (most recent call last): File "", line 1, in _interpreters.exec(i, 'print(1)', shared=False) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: _interpreters.exec() argument 3 must be dict, not bool ``` Non-`str` keys: ```python >>> _interpreters.exec(i, 'print(1)', shared={1: 1}) Traceback (most recent call last): File "", line 1, in _interpreters.exec(i, 'print(1)', shared={1: 1}) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: bad argument type for built-in operation ``` Real sharing: ```python >>> _interpreters.exec(i, 'print(1)', shared={'a': 1}) 1 ``` Some objects can't be shared. Currently only a few can be. But, I think that restricting this type yet is not a good idea, because more can be added in the future. Also, `(1, 2)` is ok, `(1, object())` is not --- stdlib/_interpreters.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/_interpreters.pyi b/stdlib/_interpreters.pyi index caa1115e9d3d..c87350f7e582 100644 --- a/stdlib/_interpreters.pyi +++ b/stdlib/_interpreters.pyi @@ -1,9 +1,10 @@ import types from collections.abc import Callable, Mapping -from typing import Final, Literal, SupportsIndex +from typing import Any, Final, Literal, SupportsIndex from typing_extensions import TypeAlias _Configs: TypeAlias = Literal["default", "isolated", "legacy", "empty", ""] +_SharedDict: TypeAlias = dict[str, Any] # any object can be shared class InterpreterError(Exception): ... class InterpreterNotFoundError(InterpreterError): ... @@ -22,7 +23,7 @@ def is_running(id: SupportsIndex, *, restrict: bool = False) -> bool: ... def get_config(id: SupportsIndex, *, restrict: bool = False) -> types.SimpleNamespace: ... def whence(id: SupportsIndex) -> int: ... def exec( - id: SupportsIndex, code: str | types.CodeType | Callable[[], object], shared: bool | None = None, *, restrict: bool = False + id: SupportsIndex, code: str | types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False ) -> None | types.SimpleNamespace: ... def call( id: SupportsIndex, @@ -33,10 +34,10 @@ def call( restrict: bool = False, ) -> object: ... def run_string( - id: SupportsIndex, script: str | types.CodeType | Callable[[], object], shared: bool | None = None, *, restrict: bool = False + id: SupportsIndex, script: str | types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False ) -> None: ... def run_func( - id: SupportsIndex, func: types.CodeType | Callable[[], object], shared: bool | None = None, *, restrict: bool = False + id: SupportsIndex, func: types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False ) -> None: ... def set___main___attrs(id: SupportsIndex, updates: Mapping[str, object], *, restrict: bool = False) -> None: ... def incref(id: SupportsIndex, *, implieslink: bool = False, restrict: bool = False) -> None: ... From aeb776f2bc9e2c33815c4687cb7f5a6bc3010ac6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 12:36:16 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/_interpreters.pyi | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/stdlib/_interpreters.pyi b/stdlib/_interpreters.pyi index c87350f7e582..f0f57f30963d 100644 --- a/stdlib/_interpreters.pyi +++ b/stdlib/_interpreters.pyi @@ -23,7 +23,11 @@ def is_running(id: SupportsIndex, *, restrict: bool = False) -> bool: ... def get_config(id: SupportsIndex, *, restrict: bool = False) -> types.SimpleNamespace: ... def whence(id: SupportsIndex) -> int: ... def exec( - id: SupportsIndex, code: str | types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False + id: SupportsIndex, + code: str | types.CodeType | Callable[[], object], + shared: _SharedDict | None = None, + *, + restrict: bool = False, ) -> None | types.SimpleNamespace: ... def call( id: SupportsIndex, @@ -34,7 +38,11 @@ def call( restrict: bool = False, ) -> object: ... def run_string( - id: SupportsIndex, script: str | types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False + id: SupportsIndex, + script: str | types.CodeType | Callable[[], object], + shared: _SharedDict | None = None, + *, + restrict: bool = False, ) -> None: ... def run_func( id: SupportsIndex, func: types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False From 7cd1c87ed7697768906990c045505b2ed0eda7fb Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 23 Jun 2025 15:40:57 +0300 Subject: [PATCH 3/5] Update _interpreters.pyi --- stdlib/_interpreters.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/_interpreters.pyi b/stdlib/_interpreters.pyi index f0f57f30963d..972f38e9e748 100644 --- a/stdlib/_interpreters.pyi +++ b/stdlib/_interpreters.pyi @@ -4,7 +4,7 @@ from typing import Any, Final, Literal, SupportsIndex from typing_extensions import TypeAlias _Configs: TypeAlias = Literal["default", "isolated", "legacy", "empty", ""] -_SharedDict: TypeAlias = dict[str, Any] # any object can be shared +_SharedDict: TypeAlias = dict[str, Any] # many objects can be shared class InterpreterError(Exception): ... class InterpreterNotFoundError(InterpreterError): ... From 73b9f9284709f840cf367314694d8356cd99a482 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 23 Jun 2025 17:36:27 +0300 Subject: [PATCH 4/5] Update stdlib/_interpreters.pyi Co-authored-by: Brian Schubert --- stdlib/_interpreters.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/_interpreters.pyi b/stdlib/_interpreters.pyi index 972f38e9e748..add3d1615ef4 100644 --- a/stdlib/_interpreters.pyi +++ b/stdlib/_interpreters.pyi @@ -47,7 +47,7 @@ def run_string( def run_func( id: SupportsIndex, func: types.CodeType | Callable[[], object], shared: _SharedDict | None = None, *, restrict: bool = False ) -> None: ... -def set___main___attrs(id: SupportsIndex, updates: Mapping[str, object], *, restrict: bool = False) -> None: ... +def set___main___attrs(id: SupportsIndex, updates: _SharedDict, *, restrict: bool = False) -> None: ... def incref(id: SupportsIndex, *, implieslink: bool = False, restrict: bool = False) -> None: ... def decref(id: SupportsIndex, *, restrict: bool = False) -> None: ... def is_shareable(obj: object) -> bool: ... From cc6cb1272758f0ae5e8054d41a1460dc132ac4af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:38:59 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/_interpreters.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/_interpreters.pyi b/stdlib/_interpreters.pyi index add3d1615ef4..ad8eccbe3328 100644 --- a/stdlib/_interpreters.pyi +++ b/stdlib/_interpreters.pyi @@ -1,5 +1,5 @@ import types -from collections.abc import Callable, Mapping +from collections.abc import Callable from typing import Any, Final, Literal, SupportsIndex from typing_extensions import TypeAlias