Skip to content

Commit

Permalink
fix: path to string
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Jul 30, 2024
1 parent 0adffe5 commit f835bb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/timeout_executor/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 3 additions & 10 deletions src/timeout_executor/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
27 changes: 12 additions & 15 deletions src/timeout_executor/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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""
Expand All @@ -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
Expand Down

0 comments on commit f835bb8

Please sign in to comment.