Skip to content
Prev Previous commit
Next Next commit
Use exact type check for builtins_dict cache
downcast_ref::<PyDict>() matches dict subclasses, causing
get_chain_exact to bypass custom __getitem__ overrides.
Use downcast_ref_if_exact to only fast-path exact dict types.
  • Loading branch information
youknowone committed Mar 1, 2026
commit b1cf47c83202b3d84a146d95fcdee536a20cf9ea
12 changes: 6 additions & 6 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,15 @@ impl Frame {

impl Py<Frame> {
#[inline(always)]
fn with_exec<R>(&self, f: impl FnOnce(ExecutingFrame<'_>) -> R) -> R {
fn with_exec<R>(&self, vm: &VirtualMachine, f: impl FnOnce(ExecutingFrame<'_>) -> R) -> R {
let mut state = self.state.lock();
let exec = ExecutingFrame {
code: &self.code,
fastlocals: &self.fastlocals,
locals: &self.locals,
globals: &self.globals,
builtins: &self.builtins,
builtins_dict: self.builtins.downcast_ref::<PyDict>(),
builtins_dict: self.builtins.downcast_ref_if_exact::<PyDict>(vm),
lasti: &self.lasti,
object: self,
state: &mut state,
Expand All @@ -454,15 +454,15 @@ impl Py<Frame> {

// #[cfg_attr(feature = "flame-it", flame("Frame"))]
pub fn run(&self, vm: &VirtualMachine) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| exec.run(vm))
self.with_exec(vm, |mut exec| exec.run(vm))
}

pub(crate) fn resume(
&self,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| {
self.with_exec(vm, |mut exec| {
if let Some(value) = value {
exec.push_value(value)
}
Expand All @@ -477,7 +477,7 @@ impl Py<Frame> {
exc_val: PyObjectRef,
exc_tb: PyObjectRef,
) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| exec.gen_throw(vm, exc_type, exc_val, exc_tb))
self.with_exec(vm, |mut exec| exec.gen_throw(vm, exc_type, exc_val, exc_tb))
}

pub fn yield_from_target(&self) -> Option<PyObjectRef> {
Expand All @@ -490,7 +490,7 @@ impl Py<Frame> {
locals: &self.locals,
globals: &self.globals,
builtins: &self.builtins,
builtins_dict: self.builtins.downcast_ref::<PyDict>(),
builtins_dict: None,
lasti: &self.lasti,
object: self,
state: &mut state,
Expand Down