From f835bb87182cdc78c2dd4bbbbdb182ada9cbf72f Mon Sep 17 00:00:00 2001 From: phi Date: Wed, 31 Jul 2024 00:02:36 +0900 Subject: [PATCH] fix: path to string --- src/timeout_executor/executor.py | 2 +- src/timeout_executor/result.py | 13 +++---------- src/timeout_executor/subprocess.py | 27 ++++++++++++--------------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/timeout_executor/executor.py b/src/timeout_executor/executor.py index e55dac8..e38fb02 100644 --- a/src/timeout_executor/executor.py +++ b/src/timeout_executor/executor.py @@ -75,7 +75,7 @@ def _command(self, stacklevel: int = 2) -> list[str]: def _dump_args( self, output_file: Path | anyio.Path, *args: P.args, **kwargs: P.kwargs ) -> bytes: - input_args = (self._func, args, kwargs, output_file) + input_args = (self._func, args, kwargs, str(output_file)) logger.debug("%r before dump input args", self) input_args_as_bytes = cloudpickle.dumps(input_args) logger.debug( diff --git a/src/timeout_executor/result.py b/src/timeout_executor/result.py index 22a6f34..b9ab889 100644 --- a/src/timeout_executor/result.py +++ b/src/timeout_executor/result.py @@ -46,14 +46,8 @@ def __init__( self._executor_args.input_file, self._executor_args.output_file, ) - - if not isinstance(input_file, anyio.Path): - input_file = anyio.Path(input_file) - self._input = input_file - - if not isinstance(output_file, anyio.Path): - output_file = anyio.Path(output_file) - self._output = output_file + self._input = anyio.Path(input_file) + self._output = anyio.Path(output_file) @property def _func_name(self) -> str: @@ -149,8 +143,7 @@ async def wait_process( process: subprocess.Popen[str], timeout: float, input_file: Path | anyio.Path ) -> None: wait_func = partial(sync_to_async(process.wait), timeout) - if not isinstance(input_file, anyio.Path): - input_file = anyio.Path(input_file) + input_file = anyio.Path(input_file) try: with anyio.fail_after(timeout): diff --git a/src/timeout_executor/subprocess.py b/src/timeout_executor/subprocess.py index e2e016e..325db5a 100644 --- a/src/timeout_executor/subprocess.py +++ b/src/timeout_executor/subprocess.py @@ -5,20 +5,20 @@ from collections.abc import Awaitable, Coroutine from os import environ from pathlib import Path -from typing import Any, Callable +from typing import TYPE_CHECKING, Any, Callable -import anyio import cloudpickle -from typing_extensions import ParamSpec, TypeAlias, TypeVar from timeout_executor.const import TIMEOUT_EXECUTOR_INPUT_FILE -from timeout_executor.serde import dumps_error -__all__ = [] +if TYPE_CHECKING: + from typing_extensions import ParamSpec, TypeAlias, TypeVar + + P = ParamSpec("P") + T = TypeVar("T", infer_variance=True) + AnyAwaitable: TypeAlias = "Awaitable[T] | Coroutine[Any, Any, T]" -P = ParamSpec("P") -T = TypeVar("T", infer_variance=True) -AnyAwaitable: TypeAlias = "Awaitable[T] | Coroutine[Any, Any, T]" +__all__ = [] def run_in_subprocess() -> None: @@ -32,16 +32,13 @@ def run_in_subprocess() -> None: def dumps_value(value: Any) -> bytes: if isinstance(value, BaseException): + from timeout_executor.serde import dumps_error + return dumps_error(value) return cloudpickle.dumps(value) -def output_to_file( - file: Path | anyio.Path, -) -> Callable[[Callable[P, T]], Callable[P, T]]: - if isinstance(file, anyio.Path): - file = file._path # noqa: SLF001 - +def output_to_file(file: str) -> Callable[[Callable[P, T]], Callable[P, T]]: def wrapper(func: Callable[P, T]) -> Callable[P, T]: def inner(*args: P.args, **kwargs: P.kwargs) -> T: dump = b"" @@ -55,7 +52,7 @@ def inner(*args: P.args, **kwargs: P.kwargs) -> T: dump = dumps_value(result) return result finally: - with file.open("wb+") as file_io: + with open(file, "wb+") as file_io: # noqa: PTH123 file_io.write(dump) return inner