Files
hse-2026/homework/07-kv-sharding/tests/src/python_seed.rs
T

33 lines
1.2 KiB
Rust
Raw Normal View History

2026-11-07 22:19:18 +03:00
//! Set Python's hash seed before the interpreter (including the validator) starts.
use std::env;
use std::process::Command;
pub fn configure(seed: u64) {
// Python accepts only 32-bit hash seeds; the simulator keeps the full u64 seed.
let hash_seed = (seed as u32).to_string();
if env::var("PYTHONHASHSEED").as_deref() == Ok(hash_seed.as_str()) {
return;
}
// Restart with an inherited environment instead of changing it at runtime:
// on Windows, Rust and CPython can use different copies of the environment.
let mut command = Command::new(env::current_exe().expect("cannot locate test executable"));
command
.args(env::args_os().skip(1))
.env("PYTHONHASHSEED", hash_seed);
// Preserve the PID so timeout/signals still reach the test process directly.
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
let error = command.exec();
panic!("cannot restart tests with PYTHONHASHSEED: {error}");
}
#[cfg(not(unix))]
{
let status = command
.status()
.expect("cannot restart tests with PYTHONHASHSEED");
std::process::exit(status.code().unwrap_or(1));
}
}