Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

python: verify wants the original's exception, not a panic (#24)

chessMan62ec364924a1
diff --git a/src/python.rs b/src/python.rs
index 6c8e35d..6984b01 100644
--- a/src/python.rs
+++ b/src/python.rs
@@ -669,6 +669,32 @@ mod tests {
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
}
+ #[test]
+ fn verify_wants_the_same_exception_not_a_panic() {
+ let Some(python) = ["python3", "python"]
+ .into_iter()
+ .find(|p| Command::new(p).arg("--version").output().is_ok_and(|o| o.status.success()))
+ else {
+ eprintln!("no python on PATH, skipping");
+ return;
+ };
+ let runner = std::env::temp_dir().join(format!("fa-runner-raises-{}.py", std::process::id()));
+ fs::write(&runner, RUNNER).unwrap();
+ let check = "import runpy, sys\n\
+ ns = runpy.run_path(sys.argv[1])\n\
+ raises_like, best_pass = ns['raises_like'], ns['best_pass']\n\
+ class PanicException(BaseException): pass\n\
+ assert raises_like(ValueError('x'), 'ValueError')\n\
+ assert not raises_like(KeyError('x'), 'ValueError')\n\
+ assert not raises_like(PanicException('index out of bounds'), 'ValueError')\n\
+ assert not raises_like(None, 'ValueError')\n\
+ def panics(*a):\n raise PanicException('boom')\n\
+ assert best_pass(panics, [((), {})], 0.0) >= 0\n";
+ let out = Command::new(python).args(["-I", "-c", check]).arg(&runner).output().unwrap();
+ let _ = fs::remove_file(&runner);
+ assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
+ }
+
#[test]
fn spots_python() {
assert!(is_python("python"));
diff --git a/src/python/runner.py b/src/python/runner.py
index 63ec08c..2fdec83 100644
--- a/src/python/runner.py
+++ b/src/python/runner.py
@@ -486,7 +486,7 @@ def cmd_verify(job, _argv):
except BaseException as e: # Rust panics arrive as PanicException (a BaseException)
got, err = None, e
if "raises" in case:
- ok, expected = err is not None, "raises " + case["raises"]
+ ok, expected = raises_like(err, case["raises"]), "raises " + case["raises"]
else:
want = dec(case["result"])
ok, expected = err is None and same(want, got), show(want)
@@ -710,6 +710,12 @@ def derive(original, cases, budget, limit):
return derived
+def raises_like(err, name):
+ """The port raised the exception the original did. A Rust panic (PanicException, a BaseException)
+ never counts: the app's except clauses won't catch it."""
+ return isinstance(err, Exception) and type(err).__name__ == name
+
+
def best_pass(fn, calls, budget):
"""Fastest time to replay every recorded call once, within a time budget."""
best, spent, passes = float("inf"), 0.0, 0
@@ -718,7 +724,9 @@ def best_pass(fn, calls, budget):
for args, kwargs in calls:
try:
fn(*args, **kwargs)
- except Exception:
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except BaseException: # Rust panics arrive as PanicException
pass
took = time.perf_counter() - start
best, spent, passes = min(best, took), spent + took, passes + 1