Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
reimpl origname
  • Loading branch information
youknowone committed Dec 31, 2025
commit 72f314dfa62973092e1e492e0ab484fe0de33195
39 changes: 11 additions & 28 deletions crates/compiler-core/src/frozen.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::bytecode::*;
use crate::marshal::{self, Read, ReadBorrowed, Write};

/// A frozen module. Holds a frozen code object and whether it is part of a package.
/// The `origname` type is generic to allow either static names (runtime) or
/// borrowed/owned names during compile-time freezing.
/// A frozen module. Holds a frozen code object and whether it is part of a package
#[derive(Copy, Clone)]
pub struct FrozenModule<B = &'static [u8], N = &'static str> {
pub struct FrozenModule<B = &'static [u8]> {
pub code: FrozenCodeObject<B>,
pub package: bool,
pub origname: N,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -58,7 +55,7 @@ impl<B: AsRef<[u8]> + ?Sized> FrozenLib<B> {
}

impl<'a, B: AsRef<[u8]> + ?Sized> IntoIterator for &'a FrozenLib<B> {
type Item = (&'a str, FrozenModule<&'a [u8], &'a str>);
type Item = (&'a str, FrozenModule<&'a [u8]>);
type IntoIter = FrozenModulesIter<'a>;

fn into_iter(self) -> Self::IntoIter {
Expand All @@ -72,7 +69,7 @@ pub struct FrozenModulesIter<'a> {
}

impl<'a> Iterator for FrozenModulesIter<'a> {
type Item = (&'a str, FrozenModule<&'a [u8], &'a str>);
type Item = (&'a str, FrozenModule<&'a [u8]>);

fn next(&mut self) -> Option<Self::Item> {
if self.remaining > 0 {
Expand All @@ -93,30 +90,21 @@ impl ExactSizeIterator for FrozenModulesIter<'_> {}

fn read_entry<'a>(
rdr: &mut &'a [u8],
) -> Result<(&'a str, FrozenModule<&'a [u8], &'a str>), marshal::MarshalError> {
) -> Result<(&'a str, FrozenModule<&'a [u8]>), marshal::MarshalError> {
let len = rdr.read_u32()?;
let name = rdr.read_str_borrow(len)?;
let len = rdr.read_u32()?;
let code_slice = rdr.read_slice_borrow(len)?;
let code = FrozenCodeObject { bytes: code_slice };
let package = rdr.read_u8()? != 0;
let len = rdr.read_u32()?;
let origname = rdr.read_str_borrow(len)?;
Ok((
name,
FrozenModule {
code,
package,
origname,
},
))
Ok((name, FrozenModule { code, package }))
}

impl FrozenLib<Vec<u8>> {
/// Encode the given iterator of frozen modules into a compressed vector of bytes
pub fn encode<'a, I, B: AsRef<[u8]>, N: AsRef<str>>(lib: I) -> Self
pub fn encode<'a, I, B: AsRef<[u8]>>(lib: I) -> Self
where
I: IntoIterator<Item = (&'a str, FrozenModule<B, N>), IntoIter: ExactSizeIterator + Clone>,
I: IntoIterator<Item = (&'a str, FrozenModule<B>), IntoIter: ExactSizeIterator + Clone>,
{
let iter = lib.into_iter();
let mut bytes = Vec::new();
Expand All @@ -125,23 +113,18 @@ impl FrozenLib<Vec<u8>> {
}
}

fn write_lib<'a, B: AsRef<[u8]>, N: AsRef<str>>(
fn write_lib<'a, B: AsRef<[u8]>>(
buf: &mut Vec<u8>,
lib: impl ExactSizeIterator<Item = (&'a str, FrozenModule<B, N>)>,
lib: impl ExactSizeIterator<Item = (&'a str, FrozenModule<B>)>,
) {
marshal::write_len(buf, lib.len());
for (name, module) in lib {
write_entry(buf, name, module);
}
}

fn write_entry<N: AsRef<str>>(
buf: &mut Vec<u8>,
name: &str,
module: FrozenModule<impl AsRef<[u8]>, N>,
) {
fn write_entry(buf: &mut Vec<u8>, name: &str, module: FrozenModule<impl AsRef<[u8]>>) {
marshal::write_vec(buf, name.as_bytes());
marshal::write_vec(buf, module.code.bytes.as_ref());
buf.write_u8(module.package as u8);
marshal::write_vec(buf, module.origname.as_ref().as_bytes());
}
21 changes: 6 additions & 15 deletions crates/derive-impl/src/compile_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ enum CompilationSourceKind {
struct CompiledModule {
code: CodeObject,
package: bool,
origname: String,
}

struct CompilationSource {
Expand Down Expand Up @@ -92,17 +91,12 @@ impl CompilationSource {
mode,
compiler,
),
_ => {
let origname = module_name.clone();
let code = self.compile_single(mode, module_name, compiler)?;
Ok(hashmap! {
origname.clone() => CompiledModule {
code,
package: false,
origname,
},
})
}
_ => Ok(hashmap! {
module_name.clone() => CompiledModule {
code: self.compile_single(mode, module_name, compiler)?,
package: false,
},
}),
}
}

Expand Down Expand Up @@ -226,13 +220,11 @@ impl CompilationSource {
Err(e) => return Err(e),
};

let origname = module_name.clone();
code_map.insert(
module_name,
CompiledModule {
code,
package: is_init,
origname,
},
);
}
Expand Down Expand Up @@ -376,7 +368,6 @@ pub fn impl_py_freeze(
let v = frozen::FrozenModule {
code: frozen::FrozenCodeObject::encode(&v.code),
package: v.package,
origname: &*v.origname,
};
(&**k, v)
}));
Expand Down
5 changes: 3 additions & 2 deletions crates/vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
builtins::{PyCode, list, traceback::PyTraceback},
exceptions::types::PyBaseException,
scope::Scope,
vm::{VirtualMachine, thread},
vm::{VirtualMachine, resolve_frozen_alias, thread},
};

pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult<PyObjectRef> {
Expand Down Expand Up @@ -77,7 +77,8 @@ pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
})?;
let module = import_code_obj(vm, module_name, vm.ctx.new_code(frozen.code), false)?;
debug_assert!(module.get_attr(identifier!(vm, __name__), vm).is_ok());
module.set_attr("__origname__", vm.ctx.new_str(frozen.origname), vm)?;
let origname = resolve_frozen_alias(module_name);
module.set_attr("__origname__", vm.ctx.new_str(origname), vm)?;
Ok(module)
}

Expand Down
4 changes: 3 additions & 1 deletion crates/vm/src/stdlib/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::frozen::FrozenModule;
use crate::{VirtualMachine, builtins::PyBaseExceptionRef};
pub(crate) use _imp::make_module;

pub use crate::vm::resolve_frozen_alias;

#[cfg(feature = "threading")]
#[pymodule(sub)]
mod lock {
Expand Down Expand Up @@ -191,7 +193,7 @@ mod _imp {
Err(e) => return Err(e.to_pyexception(name.as_str(), vm)),
};

let origname = vm.ctx.new_str(info.origname);
let origname = vm.ctx.new_str(super::resolve_frozen_alias(name.as_str()));
Ok(Some((None, info.package, origname)))
}

Expand Down
29 changes: 10 additions & 19 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,15 @@ impl AsRef<Context> for VirtualMachine {
}
}

use std::sync::OnceLock;

static FROZEN_ORIGNAME_ALIASES: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
/// Resolve frozen module alias to its original name.
/// Returns the original module name if an alias exists, otherwise returns the input name.
pub fn resolve_frozen_alias(name: &str) -> &str {
match name {
"_frozen_importlib" => "importlib._bootstrap",
"_frozen_importlib_external" => "importlib._bootstrap_external",
_ => name,
}
}

fn core_frozen_inits() -> impl Iterator<Item = (&'static str, FrozenModule)> {
let iter = core::iter::empty();
Expand Down Expand Up @@ -1040,22 +1046,7 @@ fn core_frozen_inits() -> impl Iterator<Item = (&'static str, FrozenModule)> {
crate_name = "rustpython_compiler_core"
);

let aliases = FROZEN_ORIGNAME_ALIASES.get_or_init(|| {
HashMap::from([
("_frozen_importlib", "importlib._bootstrap"),
(
"_frozen_importlib_external",
"importlib._bootstrap_external",
),
])
});

iter.map(|(name, mut module)| {
if let Some(origname) = aliases.get(name) {
module.origname = *origname;
}
(name, module)
})
iter
}

#[test]
Expand Down
Loading