Skip to content

Commit 867b645

Browse files
debug: fix diagnostic import
Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
1 parent 0e5802d commit 867b645

1 file changed

Lines changed: 85 additions & 29 deletions

File tree

‎diag_test.py‎

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,92 @@
1-
import sys, time, traceback
2-
print("PYTHON:", sys.version, flush=True)
3-
import codesafe
4-
print("codesafe imported:", codesafe.__version__, flush=True)
5-
from codesafe import safe_eval, UnsafeExpressionError, EvaluationTimeoutError
1+
"""Temporary CI diagnostic: exercise the paths most likely to be
2+
version/platform sensitive and print full tracebacks."""
3+
import platform
4+
import sys
5+
import traceback
6+
7+
8+
def section(name):
9+
print(f"\n=== {name} ===", flush=True)
10+
11+
12+
def show(label, fn):
13+
try:
14+
value = fn()
15+
print(f" OK {label}: {value!r}"[:200], flush=True)
16+
return True
17+
except BaseException as exc: # noqa: BLE001 - diagnostics
18+
print(f" FAIL {label}: {type(exc).__name__}: {exc}", flush=True)
19+
traceback.print_exc()
20+
print(f"::error title={label}::{type(exc).__name__}: {exc}", flush=True)
21+
return False
22+
23+
24+
def main():
25+
section("environment")
26+
print(f" python: {sys.version}", flush=True)
27+
print(f" platform: {platform.platform()}", flush=True)
28+
import multiprocessing
29+
print(f" start method: {multiprocessing.get_start_method(allow_none=True)}", flush=True)
30+
try:
31+
print(f" available: {multiprocessing.get_all_start_methods()}", flush=True)
32+
except Exception as exc:
33+
print(f" get_all_start_methods failed: {exc}", flush=True)
34+
35+
section("import")
36+
ok = show("import codesafe", lambda: __import__("codesafe").__version__)
37+
if not ok:
38+
return 1
39+
from codesafe import EvaluationTimeoutError, safe_eval
40+
41+
section("basic evaluation")
42+
show("arithmetic", lambda: safe_eval("1 + 1"))
43+
show("attribute", lambda: safe_eval("'hi'.upper()", allow_attributes=True))
44+
show("container", lambda: safe_eval("[1, 2]", allow_objects=True))
45+
show("float", lambda: safe_eval("1.5"))
46+
show("bytes", lambda: safe_eval("b'x'"))
47+
show("none", lambda: safe_eval("None"))
48+
49+
section("result boundary")
50+
show("lambda rejected", lambda: _expect(RuntimeError, lambda: safe_eval("lambda: 1")))
51+
52+
section("timeout / process handling")
53+
show("timeout", lambda: _expect(
54+
EvaluationTimeoutError,
55+
lambda: safe_eval("f()", allowed_vars={"f": _spin},
56+
allowed_function_calls=["f"], timeout=0.3)))
57+
show("after timeout", lambda: safe_eval("1 + 1"))
58+
59+
section("encryption")
60+
def _crypto():
61+
from codesafe import decrypt, encrypt, generate_key
62+
key = generate_key()
63+
return decrypt(encrypt("print(1)", key=key), key=key)
64+
show("roundtrip", _crypto)
65+
66+
section("subprocess/pickle behaviour")
67+
def _probe_pickle():
68+
import pickle
69+
70+
class Local:
71+
pass
72+
pickle.dumps(Local())
73+
return "local class pickled"
74+
show("local class picklable (spawn precondition)", _probe_pickle)
75+
return 0
76+
677

778
def _spin():
879
while True:
980
pass
1081

11-
checks = []
12-
def chk(name, fn):
13-
t0 = time.perf_counter()
14-
try:
15-
fn()
16-
checks.append((name, "PASS", (time.perf_counter()-t0)*1000))
17-
except Exception as e:
18-
checks.append((name, f"FAIL {type(e).__name__}: {str(e)[:120]}", (time.perf_counter()-t0)*1000))
19-
20-
chk("basic", lambda: safe_eval("1+1"))
21-
chk("attr", lambda: safe_eval("'hi'.upper()", allow_attributes=True))
22-
chk("container", lambda: safe_eval("[1,2]", allow_objects=True))
23-
chk("float", lambda: safe_eval("1.5"))
24-
chk("bytes", lambda: safe_eval("b'x'"))
25-
def _expect(exc, fn):
82+
83+
def _expect(exc_type, fn):
2684
try:
2785
fn()
28-
except exc:
29-
return
30-
raise AssertionError(f"expected {exc.__name__}")
31-
32-
chk("timeout", lambda: _expect(EvaluationTimeoutError, lambda: safe_eval("f()", allowed_vars={"f": _spin}, allowed_function_calls=["f"], timeout=0.3)))
33-
chk("lambda-reject", lambda: _expect(RuntimeError, lambda: safe_eval("lambda: 1")))
34-
for name, status, ms in checks:
35-
print(f"{status:8} {ms:8.1f}ms {name}", flush=True)
36-
sys.exit(0 if all(s == "PASS" for _, s, _ in checks) else 1)
86+
except exc_type as exc:
87+
return f"raised {type(exc).__name__}"
88+
raise AssertionError(f"expected {exc_type.__name__}")
89+
90+
91+
if __name__ == "__main__":
92+
sys.exit(main())

0 commit comments

Comments
 (0)