Skip to content

Commit 24d9956

Browse files
coolreader18youknowone
authored andcommitted
Remove some unncessary dependencies
1 parent 696dcea commit 24d9956

File tree

15 files changed

+49
-62
lines changed

15 files changed

+49
-62
lines changed

Cargo.lock

+11-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ chrono = "0.4.39"
158158
criterion = { version = "0.3.5", features = ["html_reports"] }
159159
crossbeam-utils = "0.8.21"
160160
flame = "0.2.2"
161-
getrandom = "0.3"
161+
getrandom = { version = "0.3", features = ["std"] }
162162
glob = "0.3"
163163
hex = "0.4.3"
164164
indexmap = { version = "2.2.6", features = ["std"] }
@@ -185,6 +185,7 @@ paste = "1.0.15"
185185
proc-macro2 = "1.0.93"
186186
quote = "1.0.38"
187187
rand = "0.9"
188+
rand_core = { version = "0.9", features = ["os_rng"] }
188189
rustix = { version = "0.38", features = ["event"] }
189190
rustyline = "15.0.0"
190191
serde = { version = "1.0.133", default-features = false }

common/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ascii = { workspace = true }
1919
bitflags = { workspace = true }
2020
bstr = { workspace = true }
2121
cfg-if = { workspace = true }
22+
getrandom = { workspace = true }
2223
itertools = { workspace = true }
2324
libc = { workspace = true }
2425
malachite-bigint = { workspace = true }
@@ -28,12 +29,11 @@ memchr = { workspace = true }
2829
num-traits = { workspace = true }
2930
once_cell = { workspace = true }
3031
parking_lot = { workspace = true, optional = true }
31-
rand = { workspace = true }
3232
unicode_names2 = { workspace = true }
3333

3434
lock_api = "0.4"
3535
radium = "0.7"
36-
siphasher = "0.3"
36+
siphasher = "1"
3737
volatile = "0.3"
3838

3939
[target.'cfg(windows)'.dependencies]

common/src/hash.rs

-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ impl BuildHasher for HashSecret {
3737
}
3838
}
3939

40-
impl rand::distr::Distribution<HashSecret> for rand::distr::StandardUniform {
41-
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> HashSecret {
42-
HashSecret {
43-
k0: rng.random(),
44-
k1: rng.random(),
45-
}
46-
}
47-
}
48-
4940
impl HashSecret {
5041
pub fn new(seed: u32) -> Self {
5142
let mut buf = [0u8; 16];

common/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod int;
2323
pub mod linked_list;
2424
pub mod lock;
2525
pub mod os;
26+
pub mod rand;
2627
pub mod rc;
2728
pub mod refcount;
2829
pub mod static_cell;

common/src/rand.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// Get `N` bytes of random data.
2+
///
3+
/// This function is mildly expensive to call, as it fetches random data
4+
/// directly from the OS entropy source.
5+
///
6+
/// # Panics
7+
///
8+
/// Panics if the OS entropy source returns an error.
9+
pub fn os_random<const N: usize>() -> [u8; N] {
10+
let mut buf = [0u8; N];
11+
getrandom::fill(&mut buf).unwrap();
12+
buf
13+
}

compiler/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ ruff_source_file = { workspace = true }
1919
ruff_text_size = { workspace = true }
2020
thiserror = { workspace = true }
2121

22+
[dev-dependencies]
23+
rand = { workspace = true }
24+
2225
[lints]
2326
workspace = true

stdlib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ puruspe = "0.4.0"
5050
xml-rs = "0.8.14"
5151

5252
# random
53-
rand = { workspace = true }
53+
rand_core = { workspace = true }
5454
mt19937 = "3.1"
5555

5656
# Crypto:

stdlib/src/random.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod _random {
1616
use malachite_bigint::{BigInt, BigUint, Sign};
1717
use mt19937::MT19937;
1818
use num_traits::{Signed, Zero};
19-
use rand::{RngCore, SeedableRng};
19+
use rand_core::{RngCore, SeedableRng};
2020
use rustpython_vm::types::DefaultConstructor;
2121

2222
#[pyattr]

stdlib/src/uuid.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ mod _uuid {
1010
fn get_node_id() -> [u8; 6] {
1111
match get_mac_address() {
1212
Ok(Some(_ma)) => get_mac_address().unwrap().unwrap().bytes(),
13-
_ => rand::random::<[u8; 6]>(),
13+
// os_random is expensive, but this is only ever called once
14+
_ => rustpython_common::rand::os_random::<6>(),
1415
}
1516
}
1617

vm/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ num_enum = { workspace = true }
6868
once_cell = { workspace = true }
6969
parking_lot = { workspace = true }
7070
paste = { workspace = true }
71-
rand = { workspace = true }
7271
serde = { workspace = true, optional = true }
7372
static_assertions = { workspace = true }
7473
strum = { workspace = true }
@@ -160,7 +159,6 @@ getrandom = { workspace = true }
160159
[build-dependencies]
161160
glob = { workspace = true }
162161
itertools = { workspace = true }
163-
rustc_version = "0.4.0"
164162

165163
[lints]
166164
workspace = true

vm/build.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ fn main() {
2020
);
2121
println!("cargo:rustc-env=RUSTPYTHON_GIT_TAG={}", git_tag());
2222
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
23-
println!(
24-
"cargo:rustc-env=RUSTC_VERSION={}",
25-
rustc_version::version().unwrap()
26-
);
23+
println!("cargo:rustc-env=RUSTC_VERSION={}", rustc_version());
2724

2825
println!(
2926
"cargo:rustc-env=RUSTPYTHON_TARGET_TRIPLE={}",
@@ -61,7 +58,12 @@ fn git(args: &[&str]) -> String {
6158
command("git", args)
6259
}
6360

64-
fn command(cmd: &str, args: &[&str]) -> String {
61+
fn rustc_version() -> String {
62+
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
63+
command(rustc, &["-V"])
64+
}
65+
66+
fn command(cmd: impl AsRef<std::ffi::OsStr>, args: &[&str]) -> String {
6567
match Command::new(cmd).args(args).output() {
6668
Ok(output) => match String::from_utf8(output.stdout) {
6769
Ok(s) => s,

vm/src/import.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub(crate) fn init_importlib_package(vm: &VirtualMachine, importlib: PyObjectRef
4949
let mut magic = get_git_revision().into_bytes();
5050
magic.truncate(4);
5151
if magic.len() != 4 {
52-
magic = rand::random::<[u8; 4]>().to_vec();
52+
// os_random is expensive, but this is only ever called once
53+
magic = rustpython_common::rand::os_random::<4>().to_vec();
5354
}
5455
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
5556
importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?;

vm/src/version.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn get_version() -> String {
2121
get_version_number(),
2222
get_build_info(),
2323
env!("CARGO_PKG_VERSION"),
24-
get_compiler()
24+
COMPILER,
2525
)
2626
}
2727

@@ -33,9 +33,7 @@ pub fn get_winver_number() -> String {
3333
format!("{MAJOR}.{MINOR}")
3434
}
3535

36-
pub fn get_compiler() -> String {
37-
format!("rustc {}", env!("RUSTC_VERSION"))
38-
}
36+
const COMPILER: &str = env!("RUSTC_VERSION");
3937

4038
pub fn get_build_info() -> String {
4139
// See: https://reproducible-builds.org/docs/timestamps/

vm/src/vm/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ pub struct PyGlobalState {
109109
pub fn process_hash_secret_seed() -> u32 {
110110
use std::sync::OnceLock;
111111
static SEED: OnceLock<u32> = OnceLock::new();
112-
*SEED.get_or_init(rand::random)
112+
// os_random is expensive, but this is only ever called once
113+
*SEED.get_or_init(|| u32::from_ne_bytes(rustpython_common::rand::os_random()))
113114
}
114115

115116
impl VirtualMachine {

0 commit comments

Comments
 (0)