33 lines
1.2 KiB
Rust
33 lines
1.2 KiB
Rust
//! 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));
|
||
|
|
}
|
||
|
|
}
|