8000 Remove some unncessary dependencies by coolreader18 · Pull Request #5641 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Remove some unncessary dependencies #5641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 11 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ chrono = "0.4.39"
criterion = { version = "0.3.5", features = ["html_reports"] }
crossbeam-utils = "0.8.21"
flame = "0.2.2"
getrandom = "0.3"
getrandom = { version = "0.3", features = ["std"] }
glob = "0.3"
hex = "0.4.3"
indexmap = { version = "2.2.6", features = ["std"] }
Expand All @@ -185,6 +185,7 @@ paste = "1.0.15"
proc-macro2 = "1.0.93"
quote = "1.0.38"
rand = "0.9"
rand_core = { version = "0.9", features = ["os_rng"] }
rustix = { version = "0.38", features = ["event"] }
rustyline = "15.0.0"
serde = { version = "1.0.133", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ascii = { workspace = true }
bitflags = { workspace = true }
bstr = { workspace = true }
cfg-if = { workspace = true }
getrandom = { workspace = true }
itertools = { workspace = true }
libc = { workspace = true }
malachite-bigint = { workspace = true }
Expand All @@ -28,12 +29,11 @@ memchr = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, optional = true }
rand = { workspace = true }
unicode_names2 = { workspace = true }

lock_api = "0.4"
radium = "0.7"
siphasher = "0.3"
siphasher = "1"
volatile = "0.3"

[target.'cfg(windows)'.dependencies]
Expand Down
9 changes: 0 additions & 9 deletions common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ impl BuildHasher for HashSecret {
}
}

impl rand::distr::Distribution<HashSecret> for rand::distr::StandardUniform {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> HashSecret {
HashSecret {
k0: rng.random(),
k1: rng.random(),
}
}
}

impl HashSecret {
pub fn new(seed: u32) -> Self {
let mut buf = [0u8; 16];
Expand Down
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod int;
pub mod linked_list;
pub mod lock;
pub mod os;
pub mod rand;
pub mod rc;
pub mod refcount;
pub mod static_cell;
Expand Down
13 changes: 13 additions & 0 deletions common/src/rand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Get `N` bytes of random data.
///
/// This function is mildly expensive to call, as it fetches random data
/// directly from the OS entropy source.
///
/// # Panics
///
/// Panics if the OS entropy source returns an error.
pub fn os_random<const N: usize>() -> [u8; N] {
let mut buf = [0u8; N];
getrandom::fill(&mut buf).unwrap();
buf
}
3 changes: 3 additions & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
rand = { workspace = true }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ puruspe = "0.4.0"
xml-rs = "0.8.14"

# random
rand = { workspace = true }
rand_core = { workspace = true }
mt19937 = "3.1"

# Crypto:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod _random {
use malachite_bigint::{BigInt, BigUint, Sign};
use mt19937::MT19937;
use num_traits::{Signed, Zero};
use rand::{RngCore, SeedableRng};
use rand_core::{RngCore, SeedableRng};
use rustpython_vm::types::DefaultConstructor;

#[pyattr]
Expand Down
3 changes: 2 additions & 1 deletion stdlib/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ mod _uuid {
fn get_node_id() -> [u8; 6] {
match get_mac_address() {
Ok(Some(_ma)) => get_mac_address().unwrap().unwrap().bytes(),
_ => rand::random::<[u8; 6]>(),
// os_random is expensive, but this is only ever called once
_ => rustpython_common::rand::os_random::<6>(),
}
}

Expand Down
2 changes: 0 additions & 2 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ num_enum = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
paste = { workspace = true }
rand = { workspace = true }
serde = { workspace = true, optional = true }
static_assertions = { workspace = true }
strum = { workspace = true }
Expand Down Expand Up @@ -160,7 +159,6 @@ getrandom = { workspace = true }
[build-dependencies]
glob = { workspace = true }
itertools = { workspace = true }
rustc_version = "0.4.0"

[lints]
workspace = true
12 changes: 7 additions & 5 deletions vm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ fn main() {
);
println!("cargo:rustc-env=RUSTPYTHON_GIT_TAG={}", git_tag());
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
println!(
"cargo:rustc-env=RUSTC_VERSION={}",
rustc_version::version().unwrap()
);
println!("cargo:rustc-env=RUSTC_VERSION={}", rustc_version());

println!(
"cargo:rustc-env=RUSTPYTHON_TARGET_TRIPLE={}",
Expand Down Expand Up @@ -61,7 +58,12 @@ fn git(args: &[&str]) -> String {
command("git", args)
}

fn command(cmd: &str, args: &[&str]) -> String {
fn rustc_version() -> String {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
command(rustc, &["-V"])
}
Comment on lines +61 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree about rand to reduce runtime efficiency, but do we really need to remove rustc_version? We only use it on build step and we don't need to take this extra code to maintain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also pulls in the semver crate to let you do semantic version matching, which we fully don't use at all. I do agree that a build dependency is not a very big deal, but the part of rust_version we use is basically exactly equivalent to this code, and we already have the command function here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually gonna switch this to use rustc_version::VersionMeta.short_version_string instead of the plain 1.X.Y version (it's rustc 1.85.0 (4d91de4e4 2025-02-17) which is more similar to what pypy has: [PyPy 7.3.18 with GCC 14.2.1 20250128]) and then I realized that short_version_string is literally just the output of rustc -V, with no parsing of rustc -Vv needed at all.


fn command(cmd: impl AsRef<std::ffi::OsStr>, args: &[&str]) -> String {
match Command::new(cmd).args(args).output() {
Ok(output) => match String::from_utf8(output.stdout) {
Ok(s) => s,
Expand Down
3 changes: 2 additions & 1 deletion vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub(crate) fn init_importlib_package(vm: &VirtualMachine, importlib: PyObjectRef
let mut magic = get_git_revision().into_bytes();
magic.truncate(4);
if magic.len() != 4 {
magic = rand::random::<[u8; 4]>().to_vec();
// os_random is expensive, but this is only ever called once
magic = rustpython_common::rand::os_random::<4>().to_vec();
}
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?;
Expand Down
6 changes: 2 additions & 4 deletions vm/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn get_version() -> String {
get_version_number(),
get_build_info(),
env!("CARGO_PKG_VERSION"),
get_compiler()
COMPILER,
)
}

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

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

pub fn get_build_info() -> String {
// See: https://reproducible-builds.org/docs/timestamps/
Expand Down
3 changes: 2 additions & 1 deletion vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub struct PyGlobalState {
pub fn process_hash_secret_seed() -> u32 {
use std::sync::OnceLock;
static SEED: OnceLock<u32> = OnceLock::new();
*SEED.get_or_init(rand::random)
// os_random is expensive, but this is only ever called once
*SEED.get_or_init(|| u32::from_ne_bytes(rustpython_common::rand::os_random()))
}

impl VirtualMachine {
Expand Down
Loading
0