Skip to content

Commit a80b61e

Browse files
committed
Avoid importing unix local sandboxes on Windows
1 parent e80d2d2 commit a80b61e

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/agents/sandbox/sandboxes/__init__.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
execution environments (e.g. Docker, local Unix).
66
"""
77

8-
from .unix_local import (
9-
UnixLocalSandboxClient,
10-
UnixLocalSandboxClientOptions,
11-
UnixLocalSandboxSession,
12-
UnixLocalSandboxSessionState,
13-
)
8+
import sys
9+
10+
_HAS_UNIX_LOCAL = False
11+
12+
if sys.platform != "win32":
13+
# The Unix local backend depends on Unix-only stdlib modules like fcntl and termios.
14+
from .unix_local import (
15+
UnixLocalSandboxClient,
16+
UnixLocalSandboxClientOptions,
17+
UnixLocalSandboxSession,
18+
UnixLocalSandboxSessionState,
19+
)
20+
21+
_HAS_UNIX_LOCAL = True
1422

1523
try:
1624
from .docker import ( # noqa: F401
@@ -25,12 +33,17 @@
2533
# Docker is an optional extra; keep base imports working without it.
2634
_HAS_DOCKER = False
2735

28-
__all__ = [
29-
"UnixLocalSandboxClient",
30-
"UnixLocalSandboxClientOptions",
31-
"UnixLocalSandboxSession",
32-
"UnixLocalSandboxSessionState",
33-
]
36+
__all__: list[str] = []
37+
38+
if _HAS_UNIX_LOCAL:
39+
__all__.extend(
40+
[
41+
"UnixLocalSandboxClient",
42+
"UnixLocalSandboxClientOptions",
43+
"UnixLocalSandboxSession",
44+
"UnixLocalSandboxSessionState",
45+
]
46+
)
3447

3548
if _HAS_DOCKER:
3649
__all__.extend(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import importlib
4+
import sys
5+
6+
7+
def test_sandboxes_package_import_skips_unix_local_on_windows(monkeypatch) -> None:
8+
original_module = sys.modules.get("agents.sandbox.sandboxes")
9+
sandbox_package = importlib.import_module("agents.sandbox")
10+
11+
monkeypatch.setattr(sys, "platform", "win32")
12+
sys.modules.pop("agents.sandbox.sandboxes", None)
13+
14+
try:
15+
sandboxes = importlib.import_module("agents.sandbox.sandboxes")
16+
assert sandboxes.__name__ == "agents.sandbox.sandboxes"
17+
assert "UnixLocalSandboxClient" not in sandboxes.__all__
18+
assert not hasattr(sandboxes, "UnixLocalSandboxClient")
19+
finally:
20+
sys.modules.pop("agents.sandbox.sandboxes", None)
21+
if original_module is not None:
22+
sys.modules["agents.sandbox.sandboxes"] = original_module
23+
setattr(sandbox_package, "sandboxes", original_module)
24+
else:
25+
try:
26+
delattr(sandbox_package, "sandboxes")
27+
except AttributeError:
28+
pass

0 commit comments

Comments
 (0)