From c72a82b2b67c177b7475bff9b27118d6cfb61ae5 Mon Sep 17 00:00:00 2001 From: phi Date: Thu, 26 Sep 2024 13:47:07 +0900 Subject: [PATCH] fix: env var --- src/tests/executor/test_executor.py | 17 ++++++++++++++++- src/timeout_executor/executor.py | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/tests/executor/test_executor.py b/src/tests/executor/test_executor.py index 5e13cce..9d198e6 100644 --- a/src/tests/executor/test_executor.py +++ b/src/tests/executor/test_executor.py @@ -1,6 +1,8 @@ from __future__ import annotations +import os import time +import uuid from collections.abc import Awaitable from itertools import product from typing import Any @@ -9,7 +11,7 @@ import pytest from tests.executor.base import BaseExecutorTest -from timeout_executor import AsyncResult +from timeout_executor import AsyncResult, TimeoutExecutor TEST_SIZE = 3 @@ -156,3 +158,16 @@ def awaitable_func() -> Awaitable[Any]: result = await result.delay() assert isinstance(result, str) assert result == expect + + +def test_environment_variable(): + key, value = "E" + uuid.uuid4().hex.upper(), str(uuid.uuid4()) + os.environ[key] = value + + def func() -> bool: + import os + + return os.environ.get(key) == value + + result = TimeoutExecutor(1).apply(func) + assert result.result() is True diff --git a/src/timeout_executor/executor.py b/src/timeout_executor/executor.py index b746a89..5d87206 100644 --- a/src/timeout_executor/executor.py +++ b/src/timeout_executor/executor.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import shlex import subprocess import sys @@ -135,7 +136,8 @@ def _create_process( logger.debug("%r before create new process", self, stacklevel=stacklevel) process = subprocess.Popen( # noqa: S603 command, - env={ + env=os.environ + | { TIMEOUT_EXECUTOR_INPUT_FILE: str(input_file), TIMEOUT_EXECUTOR_INIT_FILE: "" if init_file is None else str(init_file), },