From 2cbbaf163dc4917743f174915aac0979aff01763 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 23 Apr 2026 11:00:28 +0300 Subject: [PATCH 01/21] Remove unused rust impl for formatting dis output --- crates/codegen/src/ir.rs | 3 +- crates/compiler-core/src/bytecode.rs | 72 ---- .../compiler-core/src/bytecode/instruction.rs | 353 +----------------- crates/vm/src/builtins/code.rs | 6 - 4 files changed, 6 insertions(+), 428 deletions(-) diff --git a/crates/codegen/src/ir.rs b/crates/codegen/src/ir.rs index 18d878c4901..e44332fbe39 100644 --- a/crates/codegen/src/ir.rs +++ b/crates/codegen/src/ir.rs @@ -5010,8 +5010,7 @@ impl CodeInfo { } else { OpArg::new(ins.target.0) }; - let instr_display = instr.display(display_arg, self); - eprint!("{instr_display}: {depth} {effect:+} => "); + eprint!("{display_arg:?}: {depth} {effect:+} => "); } let new_depth = depth.checked_add_signed(effect).ok_or({ if effect < 0 { diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index eeef411acbe..6daee678aec 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -1136,65 +1136,6 @@ impl CodeObject { label_targets } - fn display_inner( - &self, - f: &mut fmt::Formatter<'_>, - expand_code_objects: bool, - level: usize, - ) -> fmt::Result { - let label_targets = self.label_targets(); - let line_digits = (3).max(self.locations.last().unwrap().0.line.digits().get()); - let offset_digits = (4).max(1 + self.instructions.len().ilog10() as usize); - let mut last_line = OneIndexed::MAX; - let mut arg_state = OpArgState::default(); - for (offset, &instruction) in self.instructions.iter().enumerate() { - let (instruction, arg) = arg_state.get(instruction); - // optional line number - let line = self.locations[offset].0.line; - if line != last_line { - if last_line != OneIndexed::MAX { - writeln!(f)?; - } - last_line = line; - write!(f, "{line:line_digits$}")?; - } else { - for _ in 0..line_digits { - write!(f, " ")?; - } - } - write!(f, " ")?; - - // level indent - for _ in 0..level { - write!(f, " ")?; - } - - // arrow and offset - let arrow = if label_targets.contains(&Label::from_u32(offset as u32)) { - ">>" - } else { - " " - }; - write!(f, "{arrow} {offset:offset_digits$} ")?; - - // instruction - instruction.fmt_dis(arg, f, self, expand_code_objects, 21, level)?; - writeln!(f)?; - } - Ok(()) - } - - /// Recursively display this CodeObject - pub fn display_expand_code_objects(&self) -> impl fmt::Display + '_ { - struct Display<'a, C: Constant>(&'a CodeObject); - impl fmt::Display for Display<'_, C> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.display_inner(f, true, 1) - } - } - Display(self) - } - /// Map this CodeObject to one that holds a Bag::Constant pub fn map_bag(self, bag: Bag) -> CodeObject { let map_names = |names: Box<[C::Name]>| { @@ -1264,19 +1205,6 @@ impl CodeObject { } } -impl fmt::Display for CodeObject { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.display_inner(f, false, 1)?; - for constant in &*self.constants { - if let BorrowedConstant::Code { code } = constant.borrow_constant() { - writeln!(f, "\nDisassembly of {code:?}")?; - code.fmt(f)?; - } - } - Ok(()) - } -} - pub trait InstrDisplayContext { type Constant: Constant; diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 5aec1cced96..8b7373e633f 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -1,14 +1,11 @@ use core::{fmt, marker::PhantomData}; use crate::{ - bytecode::{ - BorrowedConstant, Constant, InstrDisplayContext, - oparg::{ - self, BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator, - ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr, - LoadSuperAttr, MakeFunctionFlag, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind, - SpecialMethod, UnpackExArgs, - }, + bytecode::oparg::{ + self, BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator, + ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr, + LoadSuperAttr, MakeFunctionFlag, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind, + SpecialMethod, UnpackExArgs, }, marshal::MarshalError, }; @@ -1019,309 +1016,6 @@ impl InstructionMetadata for Instruction { // In CPython 3.14 the metadata-based stack_effect is the same for both // fallthrough and branch paths for all real instructions. // Only pseudo-instructions (SETUP_*) differ — see PseudoInstruction. - - #[allow(clippy::too_many_arguments)] - fn fmt_dis( - &self, - arg: OpArg, - f: &mut fmt::Formatter<'_>, - ctx: &impl InstrDisplayContext, - expand_code_objects: bool, - pad: usize, - level: usize, - ) -> fmt::Result { - macro_rules! w { - ($variant:ident) => { - write!(f, stringify!($variant)) - }; - ($variant:ident, $map:ident = $arg_marker:expr) => {{ - let arg = $arg_marker.get(arg); - write!(f, "{:pad$}({}, {})", stringify!($variant), arg, $map(arg)) - }}; - ($variant:ident, $arg_marker:expr) => { - write!(f, "{:pad$}({})", stringify!($variant), $arg_marker.get(arg)) - }; - ($variant:ident, ?$arg_marker:expr) => { - write!( - f, - "{:pad$}({:?})", - stringify!($variant), - $arg_marker.get(arg) - ) - }; - } - - let varname = |var_num: oparg::VarNum| ctx.get_varname(var_num); - let name = |i: u32| ctx.get_name(i as usize); - let cell_name = |i: oparg::VarNum| ctx.get_localsplus_name(i); - - let fmt_const = |op: &str, - arg: OpArg, - f: &mut fmt::Formatter<'_>, - consti: &Arg| - -> fmt::Result { - let value = ctx.get_constant(consti.get(arg)); - match value.borrow_constant() { - BorrowedConstant::Code { code } if expand_code_objects => { - write!(f, "{op:pad$}({code:?}):")?; - code.display_inner(f, true, level + 1)?; - Ok(()) - } - c => { - write!(f, "{op:pad$}(")?; - c.fmt_display(f)?; - write!(f, ")") - } - } - }; - - match self { - Self::BinarySlice => w!(BINARY_SLICE), - Self::BinaryOp { op } => write!(f, "{:pad$}({})", "BINARY_OP", op.get(arg)), - Self::BinaryOpInplaceAddUnicode => w!(BINARY_OP_INPLACE_ADD_UNICODE), - Self::BuildList { count } => w!(BUILD_LIST, count), - Self::BuildMap { count } => w!(BUILD_MAP, count), - Self::BuildSet { count } => w!(BUILD_SET, count), - Self::BuildSlice { argc } => w!(BUILD_SLICE, ?argc), - Self::BuildString { count } => w!(BUILD_STRING, count), - Self::BuildTuple { count } => w!(BUILD_TUPLE, count), - Self::Call { argc } => w!(CALL, argc), - Self::CallFunctionEx => w!(CALL_FUNCTION_EX), - Self::CallKw { argc } => w!(CALL_KW, argc), - Self::CallIntrinsic1 { func } => w!(CALL_INTRINSIC_1, ?func), - Self::CallIntrinsic2 { func } => w!(CALL_INTRINSIC_2, ?func), - Self::Cache => w!(CACHE), - Self::CheckEgMatch => w!(CHECK_EG_MATCH), - Self::CheckExcMatch => w!(CHECK_EXC_MATCH), - Self::CleanupThrow => w!(CLEANUP_THROW), - Self::CompareOp { opname } => { - let op = opname.get(arg); - if u32::from(arg) & oparg::COMPARE_OP_BOOL_MASK != 0 { - write!(f, "{:pad$}(bool({}))", "COMPARE_OP", op) - } else { - write!(f, "{:pad$}({})", "COMPARE_OP", op) - } - } - Self::ContainsOp { invert } => w!(CONTAINS_OP, ?invert), - Self::ConvertValue { oparg } => { - let oparg = oparg.get(arg); - write!(f, "{:pad$} {} ({})", "CONVERT_VALUE", oparg.as_u8(), oparg) - } - Self::Copy { i } => w!(COPY, i), - Self::CopyFreeVars { n } => w!(COPY_FREE_VARS, n), - Self::DeleteAttr { namei } => w!(DELETE_ATTR, name = namei), - Self::DeleteDeref { i } => w!(DELETE_DEREF, cell_name = i), - Self::DeleteFast { var_num } => w!(DELETE_FAST, varname = var_num), - Self::DeleteGlobal { namei } => w!(DELETE_GLOBAL, name = namei), - Self::DeleteName { namei } => w!(DELETE_NAME, name = namei), - Self::DeleteSubscr => w!(DELETE_SUBSCR), - Self::DictMerge { i } => w!(DICT_MERGE, i), - Self::DictUpdate { i } => w!(DICT_UPDATE, i), - Self::EndAsyncFor => w!(END_ASYNC_FOR), - Self::EndSend => w!(END_SEND), - Self::ExtendedArg => w!(EXTENDED_ARG, Arg::::marker()), - Self::ExitInitCheck => w!(EXIT_INIT_CHECK), - Self::ForIter { delta } => w!(FOR_ITER, delta), - Self::FormatSimple => w!(FORMAT_SIMPLE), - Self::FormatWithSpec => w!(FORMAT_WITH_SPEC), - Self::GetAIter => w!(GET_AITER), - Self::GetANext => w!(GET_ANEXT), - Self::GetAwaitable { r#where } => w!(GET_AWAITABLE, r#where), - Self::Reserved => w!(RESERVED), - Self::GetIter => w!(GET_ITER), - Self::GetLen => w!(GET_LEN), - Self::ImportFrom { namei } => w!(IMPORT_FROM, name = namei), - Self::ImportName { namei } => w!(IMPORT_NAME, name = namei), - Self::InterpreterExit => w!(INTERPRETER_EXIT), - Self::IsOp { invert } => w!(IS_OP, ?invert), - Self::JumpBackward { delta } => w!(JUMP_BACKWARD, delta), - Self::JumpBackwardNoInterrupt { delta } => w!(JUMP_BACKWARD_NO_INTERRUPT, delta), - Self::JumpForward { delta } => w!(JUMP_FORWARD, delta), - Self::ListAppend { i } => w!(LIST_APPEND, i), - Self::ListExtend { i } => w!(LIST_EXTEND, i), - Self::LoadAttr { namei } => { - let oparg = namei.get(arg); - let oparg_u32 = u32::from(oparg); - let attr_name = name(oparg.name_idx()); - if oparg.is_method() { - write!( - f, - "{:pad$}({}, {}, method=true)", - "LOAD_ATTR", oparg_u32, attr_name - ) - } else { - write!(f, "{:pad$}({}, {})", "LOAD_ATTR", oparg_u32, attr_name) - } - } - Self::LoadBuildClass => w!(LOAD_BUILD_CLASS), - Self::LoadCommonConstant { idx } => w!(LOAD_COMMON_CONSTANT, ?idx), - Self::LoadFromDictOrDeref { i } => w!(LOAD_FROM_DICT_OR_DEREF, cell_name = i), - Self::LoadConst { consti } => fmt_const("LOAD_CONST", arg, f, consti), - Self::LoadSmallInt { i } => w!(LOAD_SMALL_INT, i), - Self::LoadDeref { i } => w!(LOAD_DEREF, cell_name = i), - Self::LoadFast { var_num } => w!(LOAD_FAST, varname = var_num), - Self::LoadFastAndClear { var_num } => w!(LOAD_FAST_AND_CLEAR, varname = var_num), - Self::LoadFastBorrow { var_num } => w!(LOAD_FAST_BORROW, varname = var_num), - Self::LoadFastCheck { var_num } => w!(LOAD_FAST_CHECK, varname = var_num), - Self::LoadFastLoadFast { var_nums } => { - let oparg = var_nums.get(arg); - let (idx1, idx2) = oparg.indexes(); - let name1 = varname(idx1); - let name2 = varname(idx2); - write!(f, "{:pad$}({}, {})", "LOAD_FAST_LOAD_FAST", name1, name2) - } - Self::LoadFastBorrowLoadFastBorrow { var_nums } => { - let oparg = var_nums.get(arg); - let (idx1, idx2) = oparg.indexes(); - let name1 = varname(idx1); - let name2 = varname(idx2); - write!( - f, - "{:pad$}({}, {})", - "LOAD_FAST_BORROW_LOAD_FAST_BORROW", name1, name2 - ) - } - Self::LoadFromDictOrGlobals { i } => w!(LOAD_FROM_DICT_OR_GLOBALS, name = i), - Self::LoadGlobal { namei } => { - let oparg = namei.get(arg); - let name_idx = oparg >> 1; - if (oparg & 1) != 0 { - write!( - f, - "{:pad$}({}, NULL + {})", - "LOAD_GLOBAL", - oparg, - name(name_idx) - ) - } else { - write!(f, "{:pad$}({}, {})", "LOAD_GLOBAL", oparg, name(name_idx)) - } - } - Self::LoadGlobalBuiltin => { - let oparg = u32::from(arg); - let name_idx = oparg >> 1; - if (oparg & 1) != 0 { - write!( - f, - "{:pad$}({}, NULL + {})", - "LOAD_GLOBAL_BUILTIN", - oparg, - name(name_idx) - ) - } else { - write!( - f, - "{:pad$}({}, {})", - "LOAD_GLOBAL_BUILTIN", - oparg, - name(name_idx) - ) - } - } - Self::LoadGlobalModule => { - let oparg = u32::from(arg); - let name_idx = oparg >> 1; - if (oparg & 1) != 0 { - write!( - f, - "{:pad$}({}, NULL + {})", - "LOAD_GLOBAL_MODULE", - oparg, - name(name_idx) - ) - } else { - write!( - f, - "{:pad$}({}, {})", - "LOAD_GLOBAL_MODULE", - oparg, - name(name_idx) - ) - } - } - Self::LoadLocals => w!(LOAD_LOCALS), - Self::LoadName { namei } => w!(LOAD_NAME, name = namei), - Self::LoadSpecial { method } => w!(LOAD_SPECIAL, method), - Self::LoadSuperAttr { namei } => { - let oparg = namei.get(arg); - write!( - f, - "{:pad$}({}, {}, method={}, class={})", - "LOAD_SUPER_ATTR", - u32::from(oparg), - name(oparg.name_idx()), - oparg.is_load_method(), - oparg.has_class() - ) - } - Self::MakeCell { i } => w!(MAKE_CELL, cell_name = i), - Self::MakeFunction => w!(MAKE_FUNCTION), - Self::MapAdd { i } => w!(MAP_ADD, i), - Self::MatchClass { count } => w!(MATCH_CLASS, count), - Self::MatchKeys => w!(MATCH_KEYS), - Self::MatchMapping => w!(MATCH_MAPPING), - Self::MatchSequence => w!(MATCH_SEQUENCE), - Self::Nop => w!(NOP), - Self::NotTaken => w!(NOT_TAKEN), - Self::PopExcept => w!(POP_EXCEPT), - Self::PopJumpIfFalse { delta } => w!(POP_JUMP_IF_FALSE, delta), - Self::PopJumpIfNone { delta } => w!(POP_JUMP_IF_NONE, delta), - Self::PopJumpIfNotNone { delta } => w!(POP_JUMP_IF_NOT_NONE, delta), - Self::PopJumpIfTrue { delta } => w!(POP_JUMP_IF_TRUE, delta), - Self::PopTop => w!(POP_TOP), - Self::EndFor => w!(END_FOR), - Self::PopIter => w!(POP_ITER), - Self::PushExcInfo => w!(PUSH_EXC_INFO), - Self::PushNull => w!(PUSH_NULL), - Self::RaiseVarargs { argc } => w!(RAISE_VARARGS, ?argc), - Self::Reraise { depth } => w!(RERAISE, depth), - Self::Resume { context } => w!(RESUME, context), - Self::ReturnValue => w!(RETURN_VALUE), - Self::ReturnGenerator => w!(RETURN_GENERATOR), - Self::Send { delta } => w!(SEND, delta), - Self::SetAdd { i } => w!(SET_ADD, i), - Self::SetFunctionAttribute { flag } => w!(SET_FUNCTION_ATTRIBUTE, ?flag), - Self::SetupAnnotations => w!(SETUP_ANNOTATIONS), - Self::SetUpdate { i } => w!(SET_UPDATE, i), - Self::StoreAttr { namei } => w!(STORE_ATTR, name = namei), - Self::StoreDeref { i } => w!(STORE_DEREF, cell_name = i), - Self::StoreFast { var_num } => w!(STORE_FAST, varname = var_num), - Self::StoreFastLoadFast { var_nums } => { - let oparg = var_nums.get(arg); - let (store_idx, load_idx) = oparg.indexes(); - write!(f, "STORE_FAST_LOAD_FAST")?; - write!(f, " ({}, {})", store_idx, load_idx) - } - Self::StoreFastStoreFast { var_nums } => { - let oparg = var_nums.get(arg); - let (idx1, idx2) = oparg.indexes(); - write!( - f, - "{:pad$}({}, {})", - "STORE_FAST_STORE_FAST", - varname(idx1), - varname(idx2) - ) - } - Self::StoreGlobal { namei } => w!(STORE_GLOBAL, name = namei), - Self::StoreName { namei } => w!(STORE_NAME, name = namei), - Self::StoreSlice => w!(STORE_SLICE), - Self::StoreSubscr => w!(STORE_SUBSCR), - Self::Swap { i } => w!(SWAP, i), - Self::ToBool => w!(TO_BOOL), - Self::UnpackEx { counts } => w!(UNPACK_EX, counts), - Self::UnpackSequence { count } => w!(UNPACK_SEQUENCE, count), - Self::WithExceptStart => w!(WITH_EXCEPT_START), - Self::UnaryInvert => w!(UNARY_INVERT), - Self::UnaryNegative => w!(UNARY_NEGATIVE), - Self::UnaryNot => w!(UNARY_NOT), - Self::YieldValue { arg } => w!(YIELD_VALUE, arg), - Self::GetYieldFromIter => w!(GET_YIELD_FROM_ITER), - Self::BuildTemplate => w!(BUILD_TEMPLATE), - Self::BuildInterpolation { format } => w!(BUILD_INTERPOLATION, format), - _ => w!(RUSTPYTHON_PLACEHOLDER), - } - } } define_opcodes!( @@ -1419,18 +1113,6 @@ impl InstructionMetadata for PseudoInstruction { fn is_unconditional_jump(&self) -> bool { matches!(self, Self::Jump { .. } | Self::JumpNoInterrupt { .. }) } - - fn fmt_dis( - &self, - _arg: OpArg, - _f: &mut fmt::Formatter<'_>, - _ctx: &impl InstrDisplayContext, - _expand_code_objects: bool, - _pad: usize, - _level: usize, - ) -> fmt::Result { - unimplemented!() - } } #[derive(Clone, Copy, Debug)] @@ -1514,16 +1196,6 @@ impl InstructionMetadata for AnyInstruction { inst_either!(fn stack_effect_jump(&self, oparg: u32) -> i32); inst_either!(fn stack_effect_info(&self, oparg: u32) -> StackEffect); - - inst_either!(fn fmt_dis( - &self, - arg: OpArg, - f: &mut fmt::Formatter<'_>, - ctx: &impl InstrDisplayContext, - expand_code_objects: bool, - pad: usize, - level: usize - ) -> fmt::Result); } impl AnyInstruction { @@ -1722,21 +1394,6 @@ pub trait InstructionMetadata { fn stack_effect_jump(&self, oparg: u32) -> i32 { self.stack_effect(oparg) } - - #[allow(clippy::too_many_arguments)] - fn fmt_dis( - &self, - arg: OpArg, - f: &mut fmt::Formatter<'_>, - ctx: &impl InstrDisplayContext, - expand_code_objects: bool, - pad: usize, - level: usize, - ) -> fmt::Result; - - fn display(&self, arg: OpArg, ctx: &impl InstrDisplayContext) -> impl fmt::Display { - fmt::from_fn(move |f| self.fmt_dis(arg, f, ctx, false, 0, 0)) - } } #[derive(Copy, Clone)] diff --git a/crates/vm/src/builtins/code.rs b/crates/vm/src/builtins/code.rs index dd5cb14f84b..ffdf27ba270 100644 --- a/crates/vm/src/builtins/code.rs +++ b/crates/vm/src/builtins/code.rs @@ -1462,12 +1462,6 @@ impl PyCode { } } -impl fmt::Display for PyCode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - (**self).fmt(f) - } -} - impl ToPyObject for CodeObject { fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx.new_code(self).into() From 774bdcd918b4ba702717c1cc1ea854bb91ac8848 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 23 Apr 2026 22:15:28 +0300 Subject: [PATCH 02/21] remove `examples/dis.rs` --- examples/dis.rs | 90 ------------------------------------------------- 1 file changed, 90 deletions(-) delete mode 100644 examples/dis.rs diff --git a/examples/dis.rs b/examples/dis.rs deleted file mode 100644 index 0b6190dde3c..00000000000 --- a/examples/dis.rs +++ /dev/null @@ -1,90 +0,0 @@ -//! This an example usage of the rustpython_compiler crate. -//! This program reads, parses, and compiles a file you provide -//! to RustPython bytecode, and then displays the output in the -//! `dis.dis` format. -//! -//! example usage: -//! $ cargo run --release --example dis demo*.py - -#[macro_use] -extern crate log; - -use core::error::Error; -use lexopt::ValueExt; -use rustpython_compiler as compiler; -use std::fs; -use std::path::{Path, PathBuf}; - -fn main() -> Result<(), lexopt::Error> { - env_logger::init(); - - let mut scripts = vec![]; - let mut mode = compiler::Mode::Exec; - let mut expand_code_objects = true; - let mut optimize = 0; - - let mut parser = lexopt::Parser::from_env(); - while let Some(arg) = parser.next()? { - use lexopt::Arg::*; - match arg { - Long("help") | Short('h') => { - let bin_name = parser.bin_name().unwrap_or("dis"); - println!( - "usage: {bin_name} [-m,--mode=exec|single|eval] [-x,--no-expand] [-O]" - ); - println!( - "Compiles and disassembles python script files for viewing their bytecode." - ); - return Ok(()); - } - Value(x) => scripts.push(PathBuf::from(x)), - Long("mode") | Short('m') => { - mode = parser - .value()? - .parse_with(|s| s.parse::().map_err(|e| e.to_string()))? - } - Long("no-expand") | Short('x') => expand_code_objects = false, - Short('O') => optimize += 1, - _ => return Err(arg.unexpected()), - } - } - - if scripts.is_empty() { - return Err("expected at least one argument".into()); - } - - let opts = compiler::CompileOpts { - optimize, - debug_ranges: true, - }; - - for script in &scripts { - if script.exists() && script.is_file() { - let res = display_script(script, mode, opts, expand_code_objects); - if let Err(e) = res { - error!("Error while compiling {script:?}: {e}"); - } - } else { - eprintln!("{script:?} is not a file."); - } - } - - Ok(()) -} - -fn display_script( - path: &Path, - mode: compiler::Mode, - opts: compiler::CompileOpts, - expand_code_objects: bool, -) -> Result<(), Box> { - let source = fs::read_to_string(path)?; - let code = compiler::compile(&source, mode, &path.to_string_lossy(), opts)?; - println!("{}:", path.display()); - if expand_code_objects { - println!("{}", code.display_expand_code_objects()); - } else { - println!("{code}"); - } - Ok(()) -} From ad9feb09899fa5920fbc61be8e8805ca7f27f535 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 23 Apr 2026 22:18:36 +0300 Subject: [PATCH 03/21] Added tests --- crates/codegen/Cargo.toml | 1 - crates/codegen/src/compile.rs | 100 ------- ...k_attribute_and_subscript_expressions.snap | 67 ----- ...en__compile__tests__const_bool_not_op.snap | 9 - ...nt_true_if_pass_keeps_line_anchor_nop.snap | 10 - ...thon_codegen__compile__tests__if_ands.snap | 8 - ...hon_codegen__compile__tests__if_mixed.snap | 8 - ...ython_codegen__compile__tests__if_ors.snap | 10 - ...degen__compile__tests__nested_bool_op.snap | 35 --- ...pile__tests__nested_double_async_with.snap | 258 ------------------ ...ompiler_core__compile__tests__if_ands.snap | 14 - ...mpiler_core__compile__tests__if_mixed.snap | 16 -- ...compiler_core__compile__tests__if_ors.snap | 14 - crates/stdlib/Cargo.toml | 5 + crates/stdlib/src/_opcode.rs | 148 ++++++++++ ...k_attribute_and_subscript_expressions.snap | 52 ++++ ...n_stdlib___opcode__tests__const_no_op.snap | 10 + ...nt_true_if_pass_keeps_line_anchor_nop.snap | 10 + ...ython_stdlib___opcode__tests__if_ands.snap | 8 + ...thon_stdlib___opcode__tests__if_mixed.snap | 8 + ...python_stdlib___opcode__tests__if_ors.snap | 10 + ...tdlib___opcode__tests__nested_bool_op.snap | 24 ++ ...code__tests__nested_double_async_with.snap | 183 +++++++++++++ 23 files changed, 458 insertions(+), 550 deletions(-) delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__constant_true_if_pass_keeps_line_anchor_nop.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_bool_op.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap delete mode 100644 crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__const_no_op.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__constant_true_if_pass_keeps_line_anchor_nop.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ands.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_mixed.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ors.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_bool_op.snap create mode 100644 crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap diff --git a/crates/codegen/Cargo.toml b/crates/codegen/Cargo.toml index 78065962fff..4f32fcc3c3f 100644 --- a/crates/codegen/Cargo.toml +++ b/crates/codegen/Cargo.toml @@ -33,7 +33,6 @@ unicode_names2 = { workspace = true } [dev-dependencies] ruff_python_parser = { workspace = true } -insta = { workspace = true } [lints] workspace = true diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index e79226cf141..0035a6c924c 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -11647,26 +11647,6 @@ def f(f, oldcls, newcls): }) } - macro_rules! assert_dis_snapshot { - ($value:expr) => { - insta::assert_snapshot!( - insta::internals::AutoName, - $value.display_expand_code_objects().to_string(), - stringify!($value) - ) - }; - } - - #[test] - fn test_if_ors() { - assert_dis_snapshot!(compile_exec( - "\ -if True or False or False: - pass -" - )); - } - #[test] fn test_trace_assert_true_try_pair() { let trace = compile_exec_late_cfg_trace( @@ -11901,44 +11881,6 @@ def f(self): ); } - #[test] - fn test_if_ands() { - assert_dis_snapshot!(compile_exec( - "\ -if True and False and False: - pass -" - )); - } - - #[test] - fn test_if_mixed() { - assert_dis_snapshot!(compile_exec( - "\ -if (True and False) or (False and True): - pass -" - )); - } - - #[test] - fn test_nested_bool_op() { - assert_dis_snapshot!(compile_exec( - "\ -x = Test() and False or False -" - )); - } - - #[test] - fn test_const_bool_not_op() { - assert_dis_snapshot!(compile_exec_optimized( - "\ -x = not True -" - )); - } - #[test] fn test_plain_constant_bool_op_folds_to_selected_operand() { let code = compile_exec( @@ -12171,24 +12113,6 @@ def or_false(x): } } - #[test] - fn test_nested_double_async_with() { - assert_dis_snapshot!(compile_exec( - "\ -async def test(): - for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')): - with self.subTest(type=type(stop_exc)): - try: - async with egg(): - raise stop_exc - except Exception as ex: - self.assertIs(ex, stop_exc) - else: - self.fail(f'{stop_exc} was suppressed') -" - )); - } - #[test] fn test_scope_exit_instructions_keep_line_numbers() { let code = compile_exec( @@ -13157,20 +13081,6 @@ def f(x, y): assert_eq!(u8::from(call_arg), 0); } - #[test] - fn test_bare_function_annotations_check_attribute_and_subscript_expressions() { - assert_dis_snapshot!(compile_exec( - "\ -def f(one: int): - int.new_attr: int - [list][0].new_attr: [int, str] - my_lst = [1] - my_lst[one]: int - return my_lst -" - )); - } - #[test] fn test_non_simple_bare_name_annotation_does_not_create_local_binding() { let code = compile_exec( @@ -13203,16 +13113,6 @@ def f2bad(): ); } - #[test] - fn test_constant_true_if_pass_keeps_line_anchor_nop() { - assert_dis_snapshot!(compile_exec( - "\ -if 1: - pass -" - )); - } - #[test] fn test_negative_constant_binop_folds_after_unary_folding() { let code = compile_exec( diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap deleted file mode 100644 index 840f4397d75..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap +++ /dev/null @@ -1,67 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 12138 -expression: "compile_exec(\"\\\ndef f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_CONST (): 1 0 RESUME (0) - 1 LOAD_FAST_BORROW (0, format) - 2 LOAD_SMALL_INT (2) - >> 3 COMPARE_OP (>) - 4 CACHE - 5 POP_JUMP_IF_FALSE (3) - 6 CACHE - 7 NOT_TAKEN - 8 LOAD_COMMON_CONSTANT (NotImplementedError) - 9 RAISE_VARARGS (Raise) - 10 LOAD_CONST ("one") - 11 LOAD_GLOBAL (0, int) - 12 CACHE - 13 CACHE - 14 CACHE - 15 CACHE - 16 BUILD_MAP (1) - 17 RETURN_VALUE - - 2 MAKE_FUNCTION - 3 LOAD_CONST (): 1 0 RESUME (0) - - 2 1 LOAD_GLOBAL (0, int) - 2 CACHE - 3 CACHE - 4 CACHE - 5 CACHE - 6 POP_TOP - - 3 7 LOAD_GLOBAL (2, list) - 8 CACHE - 9 CACHE - 10 CACHE - 11 CACHE - 12 BUILD_LIST (1) - 13 LOAD_SMALL_INT (0) - 14 BINARY_OP ([]) - 15 CACHE - 16 CACHE - 17 CACHE - 18 CACHE - 19 CACHE - 20 POP_TOP - - 4 21 LOAD_SMALL_INT (1) - 22 BUILD_LIST (1) - 23 STORE_FAST (1, my_lst) - - 5 24 LOAD_FAST_BORROW (1, my_lst) - 25 POP_TOP - 26 LOAD_FAST_BORROW (0, one) - 27 POP_TOP - - 6 28 LOAD_FAST_BORROW (1, my_lst) - 29 RETURN_VALUE - - 4 MAKE_FUNCTION - 5 SET_FUNCTION_ATTRIBUTE(Annotate) - 6 STORE_NAME (0, f) - 7 LOAD_CONST (None) - 8 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap deleted file mode 100644 index f9a74c2055c..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap +++ /dev/null @@ -1,9 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -expression: "compile_exec_optimized(\"\\\nx = not True\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_CONST (False) - 2 STORE_NAME (0, x) - 3 LOAD_CONST (None) - 4 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__constant_true_if_pass_keeps_line_anchor_nop.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__constant_true_if_pass_keeps_line_anchor_nop.snap deleted file mode 100644 index a600a829863..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__constant_true_if_pass_keeps_line_anchor_nop.snap +++ /dev/null @@ -1,10 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 12222 -expression: "compile_exec(\"\\\nif 1:\n pass\n\")" ---- - 1 0 RESUME (0) - 1 NOP - - 2 2 LOAD_CONST (None) - 3 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap deleted file mode 100644 index 8e9bb5d25f4..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 11413 -expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_CONST (None) - 2 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap deleted file mode 100644 index f7df6f4f3ee..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 11423 -expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_CONST (None) - 2 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap deleted file mode 100644 index f38d3c2c593..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap +++ /dev/null @@ -1,10 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 11039 -expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")" ---- - 1 0 RESUME (0) - 1 NOP - - 2 2 LOAD_CONST (None) - 3 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_bool_op.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_bool_op.snap deleted file mode 100644 index 3ad96c56454..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_bool_op.snap +++ /dev/null @@ -1,35 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 11769 -expression: "compile_exec(\"\\\nx = Test() and False or False\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_NAME (0, Test) - 2 PUSH_NULL - >> 3 CALL (0) - 4 CACHE - 5 CACHE - 6 CACHE - 7 COPY (1) - 8 TO_BOOL - 9 CACHE - 10 CACHE - >> 11 CACHE - 12 POP_JUMP_IF_FALSE (11) - 13 CACHE - 14 NOT_TAKEN - 15 POP_TOP - 16 LOAD_CONST (False) - 17 COPY (1) - 18 TO_BOOL - 19 CACHE - 20 CACHE - 21 CACHE - 22 POP_JUMP_IF_TRUE (3) - 23 CACHE - 24 NOT_TAKEN - 25 POP_TOP - 26 LOAD_CONST (False) - 27 STORE_NAME (1, x) - 28 LOAD_CONST (None) - 29 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap deleted file mode 100644 index 27ae2ae18bc..00000000000 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ /dev/null @@ -1,258 +0,0 @@ ---- -source: crates/codegen/src/compile.rs -assertion_line: 11847 -expression: "compile_exec(\"\\\nasync def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\")" ---- - 1 0 RESUME (0) - 1 LOAD_CONST (): 1 0 RETURN_GENERATOR - 1 POP_TOP - >> 2 RESUME (0) - - 2 >> 3 LOAD_GLOBAL (1, NULL + StopIteration) - >> 4 CACHE - >> 5 CACHE - 6 CACHE - 7 CACHE - >> 8 LOAD_CONST ("spam") - 9 CALL (1) - >> 10 CACHE - 11 CACHE - 12 CACHE - 13 LOAD_GLOBAL (3, NULL + StopAsyncIteration) - 14 CACHE - 15 CACHE - 16 CACHE - 17 CACHE - 18 LOAD_CONST ("ham") - 19 CALL (1) - 20 CACHE - 21 CACHE - 22 CACHE - 23 BUILD_TUPLE (2) - 24 GET_ITER - 25 FOR_ITER (71) - 26 CACHE - 27 STORE_FAST (0, stop_exc) - - 3 28 LOAD_GLOBAL (4, self) - 29 CACHE - 30 CACHE - 31 CACHE - >> 32 CACHE - 33 LOAD_ATTR (7, subTest, method=true) - 34 CACHE - 35 CACHE - 36 CACHE - 37 CACHE - 38 CACHE - 39 CACHE - 40 CACHE - 41 CACHE - 42 CACHE - 43 LOAD_GLOBAL (9, NULL + type) - 44 CACHE - >> 45 CACHE - 46 CACHE - 47 CACHE - 48 LOAD_FAST_BORROW (0, stop_exc) - 49 CALL (1) - 50 CACHE - 51 CACHE - 52 CACHE - 53 LOAD_CONST (("type")) - 54 CALL_KW (1) - 55 CACHE - 56 CACHE - 57 CACHE - 58 COPY (1) - 59 LOAD_SPECIAL (__exit__) - 60 SWAP (2) - 61 SWAP (3) - 62 LOAD_SPECIAL (__enter__) - 63 CALL (0) - 64 CACHE - 65 CACHE - 66 CACHE - 67 POP_TOP - - 4 68 NOP - - 5 69 LOAD_GLOBAL (11, NULL + egg) - 70 CACHE - >> 71 CACHE - 72 CACHE - 73 CACHE - 74 CALL (0) - 75 CACHE - 76 CACHE - 77 CACHE - 78 COPY (1) - 79 LOAD_SPECIAL (__aexit__) - 80 SWAP (2) - 81 SWAP (3) - 82 LOAD_SPECIAL (__aenter__) - 83 CALL (0) - 84 CACHE - 85 CACHE - 86 CACHE - 87 GET_AWAITABLE (1) - 88 LOAD_CONST (None) - 89 SEND (3) - 90 CACHE - 91 YIELD_VALUE (1) - 92 RESUME (3) - 93 JUMP_BACKWARD_NO_INTERRUPT(5) - 94 END_SEND - 95 POP_TOP - - 6 96 LOAD_FAST_BORROW (0, stop_exc) - 97 RAISE_VARARGS (Raise) - - 2 98 END_FOR - 99 POP_ITER - 100 LOAD_CONST (None) - 101 RETURN_VALUE - - 5 102 CLEANUP_THROW - 103 JUMP_BACKWARD_NO_INTERRUPT(10) - 104 PUSH_EXC_INFO - 105 WITH_EXCEPT_START - 106 GET_AWAITABLE (2) - 107 LOAD_CONST (None) - 108 SEND (4) - 109 CACHE - 110 YIELD_VALUE (1) - 111 RESUME (3) - 112 JUMP_BACKWARD_NO_INTERRUPT(5) - 113 CLEANUP_THROW - 114 END_SEND - 115 TO_BOOL - 116 CACHE - 117 CACHE - 118 CACHE - 119 POP_JUMP_IF_TRUE (2) - 120 CACHE - 121 NOT_TAKEN - 122 RERAISE (2) - 123 POP_TOP - 124 POP_EXCEPT - 125 POP_TOP - 126 POP_TOP - 127 POP_TOP - 128 JUMP_FORWARD (3) - 129 COPY (3) - 130 POP_EXCEPT - 131 RERAISE (1) - 132 NOP - - 10 133 LOAD_GLOBAL (4, self) - 134 CACHE - 135 CACHE - 136 CACHE - 137 CACHE - 138 LOAD_ATTR (13, fail, method=true) - 139 CACHE - 140 CACHE - 141 CACHE - 142 CACHE - 143 CACHE - 144 CACHE - 145 CACHE - 146 CACHE - 147 CACHE - 148 LOAD_FAST (0, stop_exc) - 149 FORMAT_SIMPLE - 150 LOAD_CONST (" was suppressed") - 151 BUILD_STRING (2) - 152 CALL (1) - 153 CACHE - 154 CACHE - 155 CACHE - 156 POP_TOP - 157 JUMP_FORWARD (45) - 158 PUSH_EXC_INFO - - 7 159 LOAD_GLOBAL (14, Exception) - 160 CACHE - 161 CACHE - 162 CACHE - 163 CACHE - 164 CHECK_EXC_MATCH - 165 POP_JUMP_IF_FALSE (32) - 166 CACHE - 167 NOT_TAKEN - 168 STORE_FAST (1, ex) - - 8 169 LOAD_GLOBAL (4, self) - 170 CACHE - 171 CACHE - 172 CACHE - 173 CACHE - 174 LOAD_ATTR (17, assertIs, method=true) - 175 CACHE - 176 CACHE - 177 CACHE - 178 CACHE - 179 CACHE - 180 CACHE - 181 CACHE - 182 CACHE - 183 CACHE - 184 LOAD_FAST_LOAD_FAST (ex, stop_exc) - 185 CALL (2) - 186 CACHE - 187 CACHE - >> 188 CACHE - 189 POP_TOP - 190 POP_EXCEPT - 191 LOAD_CONST (None) - 192 STORE_FAST (1, ex) - 193 DELETE_FAST (1, ex) - 194 JUMP_FORWARD (8) - 195 LOAD_CONST (None) - 196 STORE_FAST (1, ex) - 197 DELETE_FAST (1, ex) - 198 RERAISE (1) - - 7 199 RERAISE (0) - 200 COPY (3) - 201 POP_EXCEPT - 202 RERAISE (1) - - 3 203 LOAD_CONST (None) - 204 LOAD_CONST (None) - >> 205 LOAD_CONST (None) - 206 CALL (3) - 207 CACHE - 208 CACHE - 209 CACHE - 210 POP_TOP - 211 JUMP_BACKWARD (188) - 212 CACHE - 213 PUSH_EXC_INFO - 214 WITH_EXCEPT_START - 215 TO_BOOL - 216 CACHE - 217 CACHE - 218 CACHE - 219 POP_JUMP_IF_TRUE (2) - 220 CACHE - 221 NOT_TAKEN - 222 RERAISE (2) - 223 POP_TOP - 224 POP_EXCEPT - 225 POP_TOP - 226 POP_TOP - 227 POP_TOP - 228 JUMP_BACKWARD (205) - 229 CACHE - 230 COPY (3) - 231 POP_EXCEPT - 232 RERAISE (1) - 233 CALL_INTRINSIC_1 (StopIterationError) - 234 RERAISE (1) - - 2 MAKE_FUNCTION - 3 STORE_NAME (0, test) - 4 LOAD_CONST (None) - 5 RETURN_VALUE diff --git a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap b/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap deleted file mode 100644 index bc88cf2349c..00000000000 --- a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ands.snap +++ /dev/null @@ -1,14 +0,0 @@ ---- -source: compiler/src/compile.rs -expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")" ---- - 1 0 LoadConst (True) - 1 PopJumpIfFalse (6) - 2 LoadConst (False) - 3 PopJumpIfFalse (6) - 4 LoadConst (False) - 5 PopJumpIfFalse (6) - - 2 >> 6 LoadConst (None) - 7 ReturnValue - diff --git a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap b/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap deleted file mode 100644 index b19cbb119d9..00000000000 --- a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_mixed.snap +++ /dev/null @@ -1,16 +0,0 @@ ---- -source: compiler/src/compile.rs -expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")" ---- - 1 0 LoadConst (True) - 1 PopJumpIfFalse (4) - 2 LoadConst (False) - 3 PopJumpIfTrue (8) - >> 4 LoadConst (False) - 5 PopJumpIfFalse (8) - 6 LoadConst (True) - 7 PopJumpIfFalse (8) - - 2 >> 8 LoadConst (None) - 9 ReturnValue - diff --git a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap b/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap deleted file mode 100644 index 3d1f5a1d6f0..00000000000 --- a/crates/codegen/src/snapshots/rustpython_compiler_core__compile__tests__if_ors.snap +++ /dev/null @@ -1,14 +0,0 @@ ---- -source: compiler/src/compile.rs -expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")" ---- - 1 0 LoadConst (True) - 1 PopJumpIfTrue (6) - 2 LoadConst (False) - 3 PopJumpIfTrue (6) - 4 LoadConst (False) - 5 PopJumpIfFalse (6) - - 2 >> 6 LoadConst (None) - 7 ReturnValue - diff --git a/crates/stdlib/Cargo.toml b/crates/stdlib/Cargo.toml index e969114d164..ea2d0aea8d3 100644 --- a/crates/stdlib/Cargo.toml +++ b/crates/stdlib/Cargo.toml @@ -165,5 +165,10 @@ features = [ [target.'cfg(target_os = "macos")'.dependencies] system-configuration = "0.7.0" +[dev-dependencies] +insta = { workspace = true } +rustpython-pylib = { workspace = true, features = [ "freeze-stdlib" ] } + + [lints] workspace = true diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index 3f46608ad8d..37a16a06c25 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -255,3 +255,151 @@ mod _opcode { vm.ctx.none() } } + +#[cfg(test)] +mod tests { + use crate::vm::{self, compiler::Mode}; + + macro_rules! assert_dis_snapshot { + ($value:expr) => { + insta::assert_snapshot!( + insta::internals::AutoName, + dis($value.trim()), + stringify!($value).trim() + ) + }; + } + + /// Returns the [`dis.dis`](https://docs.python.org/3/library/dis.html#dis.dis) output. + /// + /// # Notes + /// + /// Memory addresses in the output are replaced with `0xdeadbeef` for consistency. + fn dis(source: &str) -> String { + let fname = String::from(""); + + let builder = vm::Interpreter::builder(Default::default()); + let stdlib_defs = crate::stdlib_module_defs(&builder.ctx); + let interp = builder + .add_native_modules(&stdlib_defs) + .add_frozen_modules(rustpython_pylib::FROZEN_STDLIB) + .build(); + + interp.enter(|vm| { + let scope = vm.new_scope_with_builtins(); + let code_obj = vm + .compile(source, Mode::Exec, fname.clone()) + .map_err(|err| vm.new_syntax_error(&err, Some(source))) + .unwrap(); + scope.globals.set_item("code", code_obj.into(), vm).unwrap(); + + let py_source = r#" +import dis +import io +import re +import sys + +old_stdout = sys.stdout +sys.stdout = buf = io.StringIO() +dis.dis(code) +sys.stdout = old_stdout + +tmp_output = buf.getvalue() + +# constant mem address +output = re.sub(r'(0xdeadbeef', tmp_output) +"#; + + let py_code_obj = vm + .compile(py_source, Mode::Exec, fname) + .map_err(|err| vm.new_syntax_error(&err, Some(py_source))) + .unwrap(); + + vm.run_code_obj(py_code_obj, scope.clone()).unwrap(); + let py_output = scope.globals.get_item("output", vm).unwrap(); + py_output.str(vm).unwrap().to_string() + }) + } + + #[test] + fn test_if_ors() { + assert_dis_snapshot!( + " +if True or False or False: + pass +" + ) + } + + #[test] + fn test_if_ands() { + assert_dis_snapshot!( + " +if True and False and False: + pass +" + ) + } + + #[test] + fn test_if_mixed() { + assert_dis_snapshot!( + " +if (True and False) or (False and True): + pass +" + ) + } + + #[test] + fn test_nested_bool_op() { + assert_dis_snapshot!("x = Test() and False or False") + } + + #[test] + fn test_const_no_op() { + assert_dis_snapshot!("x = not True") + } + + #[test] + fn test_constant_true_if_pass_keeps_line_anchor_nop() { + assert_dis_snapshot!( + " +if 1: + pass +" + ) + } + + #[test] + fn test_nested_double_async_with() { + assert_dis_snapshot!( + " +async def test(): + for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')): + with self.subTest(type=type(stop_exc)): + try: + async with egg(): + raise stop_exc + except Exception as ex: + self.assertIs(ex, stop_exc) + else: + self.fail(f'{stop_exc} was suppressed') +" + ) + } + + #[test] + fn test_bare_function_annotations_check_attribute_and_subscript_expressions() { + assert_dis_snapshot!( + " +def f(one: int): + int.new_attr: int + [list][0].new_attr: [int, str] + my_lst = [1] + my_lst[one]: int + return my_lst +" + ) + } +} diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap new file mode 100644 index 00000000000..91b65bcc1c9 --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap @@ -0,0 +1,52 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "def f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n" +--- + 0 RESUME 0 + + 1 LOAD_CONST 0 (", line 1>) + MAKE_FUNCTION + LOAD_CONST 1 (", line 1>) + MAKE_FUNCTION + SET_FUNCTION_ATTRIBUTE 16 (annotate) + STORE_NAME 0 (f) + LOAD_CONST 2 (None) + RETURN_VALUE + +Disassembly of ", line 1>: + 1 RESUME 0 + LOAD_FAST_BORROW 0 + LOAD_SMALL_INT 2 + COMPARE_OP 132 (>) + POP_JUMP_IF_FALSE 3 (to L1) + NOT_TAKEN + LOAD_COMMON_CONSTANT 1 (NotImplementedError) + RAISE_VARARGS 1 + L1: LOAD_CONST 1 ('one') + LOAD_GLOBAL 0 (int) + BUILD_MAP 1 + RETURN_VALUE + +Disassembly of ", line 1>: + 1 RESUME 0 + + 2 LOAD_GLOBAL 0 (int) + POP_TOP + + 3 LOAD_GLOBAL 2 (list) + BUILD_LIST 1 + LOAD_SMALL_INT 0 + BINARY_OP 26 ([]) + POP_TOP + + 4 LOAD_SMALL_INT 1 + BUILD_LIST 1 + STORE_FAST 1 (my_lst) + + 5 LOAD_FAST_BORROW 1 + POP_TOP + LOAD_FAST_BORROW 0 + POP_TOP + + 6 LOAD_FAST_BORROW 1 + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__const_no_op.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__const_no_op.snap new file mode 100644 index 00000000000..347e58767ae --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__const_no_op.snap @@ -0,0 +1,10 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: x = not True +--- + 0 RESUME 0 + + 1 LOAD_CONST 2 (False) + STORE_NAME 0 (x) + LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__constant_true_if_pass_keeps_line_anchor_nop.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__constant_true_if_pass_keeps_line_anchor_nop.snap new file mode 100644 index 00000000000..02e2473501d --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__constant_true_if_pass_keeps_line_anchor_nop.snap @@ -0,0 +1,10 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "if 1:\n pass" +--- + 0 RESUME 0 + + 1 NOP + + 2 LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ands.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ands.snap new file mode 100644 index 00000000000..b5957dda5e5 --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ands.snap @@ -0,0 +1,8 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "if True and False and False:\n pass" +--- + 0 RESUME 0 + + 1 LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_mixed.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_mixed.snap new file mode 100644 index 00000000000..f8976b8c6e5 --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_mixed.snap @@ -0,0 +1,8 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "if (True and False) or (False and True):\n pass" +--- + 0 RESUME 0 + + 1 LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ors.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ors.snap new file mode 100644 index 00000000000..f8cc3a1f28f --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__if_ors.snap @@ -0,0 +1,10 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "if True or False or False:\n pass" +--- + 0 RESUME 0 + + 1 NOP + + 2 LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_bool_op.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_bool_op.snap new file mode 100644 index 00000000000..c0e3659487b --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_bool_op.snap @@ -0,0 +1,24 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: x = Test() and False or False +--- + 0 RESUME 0 + + 1 LOAD_NAME 0 (Test) + PUSH_NULL + CALL 0 + COPY 1 + TO_BOOL + POP_JUMP_IF_FALSE 11 (to L1) + NOT_TAKEN + POP_TOP + LOAD_CONST 0 (False) + COPY 1 + TO_BOOL + POP_JUMP_IF_TRUE 3 (to L2) + NOT_TAKEN + L1: POP_TOP + LOAD_CONST 0 (False) + L2: STORE_NAME 1 (x) + LOAD_CONST 1 (None) + RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap new file mode 100644 index 00000000000..27cf08bb8d7 --- /dev/null +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap @@ -0,0 +1,183 @@ +--- +source: crates/stdlib/src/_opcode.rs +expression: "async def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n" +--- + 0 RESUME 0 + + 1 LOAD_CONST 0 (", line 1>) + MAKE_FUNCTION + STORE_NAME 0 (test) + LOAD_CONST 1 (None) + RETURN_VALUE + +Disassembly of ", line 1>: + 1 RETURN_GENERATOR + POP_TOP + RESUME 0 + + 2 L1: LOAD_GLOBAL 1 (StopIteration + NULL) + LOAD_CONST 0 ('spam') + CALL 1 + LOAD_GLOBAL 3 (StopAsyncIteration + NULL) + LOAD_CONST 1 ('ham') + CALL 1 + BUILD_TUPLE 2 + GET_ITER + FOR_ITER 71 (to L9) + STORE_FAST 0 (stop_exc) + + 3 LOAD_GLOBAL 4 (self) + LOAD_ATTR 7 (subTest + NULL|self) + LOAD_GLOBAL 9 (type + NULL) + LOAD_FAST_BORROW 0 + CALL 1 + LOAD_CONST 2 (('type',)) + CALL_KW 1 + COPY 1 + LOAD_SPECIAL 1 (__exit__) + SWAP 2 + SWAP 3 + LOAD_SPECIAL 0 (__enter__) + CALL 0 + L2: POP_TOP + + 4 L3: NOP + + 5 L4: LOAD_GLOBAL 11 (egg + NULL) + CALL 0 + COPY 1 + LOAD_SPECIAL 3 (__aexit__) + SWAP 2 + SWAP 3 + LOAD_SPECIAL 2 (__aenter__) + CALL 0 + GET_AWAITABLE 1 + LOAD_CONST 3 (None) + SEND 3 (to L7) + L5: YIELD_VALUE 1 + L6: RESUME 3 + JUMP_BACKWARD_NO_INTERRUPT 5 + L7: END_SEND + L8: POP_TOP + + 6 LOAD_FAST_BORROW 0 + RAISE_VARARGS 1 + + 2 L9: END_FOR + POP_ITER + LOAD_CONST 3 (None) + RETURN_VALUE + + 5 L10: CLEANUP_THROW + JUMP_BACKWARD_NO_INTERRUPT 10 + L11: PUSH_EXC_INFO + WITH_EXCEPT_START + GET_AWAITABLE 2 + LOAD_CONST 3 (None) + SEND 4 (to L15) + L12: YIELD_VALUE 1 + L13: RESUME 3 + JUMP_BACKWARD_NO_INTERRUPT 5 + L14: CLEANUP_THROW + L15: END_SEND + TO_BOOL + POP_JUMP_IF_TRUE 2 (to L16) + NOT_TAKEN + RERAISE 2 + L16: POP_TOP + L17: POP_EXCEPT + POP_TOP + POP_TOP + POP_TOP + JUMP_FORWARD 3 + L18: COPY 3 + POP_EXCEPT + RERAISE 1 + L19: NOP + + 10 L20: LOAD_GLOBAL 4 (self) + LOAD_ATTR 13 (fail + NULL|self) + LOAD_FAST 0 (stop_exc) + FORMAT_SIMPLE + LOAD_CONST 4 (' was suppressed') + BUILD_STRING 2 + CALL 1 + POP_TOP + JUMP_FORWARD 45 + + -- L21: PUSH_EXC_INFO + + 7 LOAD_GLOBAL 14 (Exception) + CHECK_EXC_MATCH + POP_JUMP_IF_FALSE 32 (to L25) + NOT_TAKEN + STORE_FAST 1 (ex) + + 8 L22: LOAD_GLOBAL 4 (self) + LOAD_ATTR 17 (assertIs + NULL|self) + LOAD_FAST_LOAD_FAST 16 (ex, stop_exc) + CALL 2 + POP_TOP + L23: POP_EXCEPT + LOAD_CONST 3 (None) + STORE_FAST 1 (ex) + DELETE_FAST 1 (ex) + JUMP_FORWARD 8 + + -- L24: LOAD_CONST 3 (None) + STORE_FAST 1 (ex) + DELETE_FAST 1 (ex) + RERAISE 1 + + 7 L25: RERAISE 0 + + -- L26: COPY 3 + POP_EXCEPT + RERAISE 1 + + 3 L27: LOAD_CONST 3 (None) + LOAD_CONST 3 (None) + LOAD_CONST 3 (None) + CALL 3 + POP_TOP + JUMP_BACKWARD 188 + L28: PUSH_EXC_INFO + WITH_EXCEPT_START + TO_BOOL + POP_JUMP_IF_TRUE 2 (to L29) + NOT_TAKEN + RERAISE 2 + L29: POP_TOP + L30: POP_EXCEPT + POP_TOP + POP_TOP + POP_TOP + JUMP_BACKWARD 205 + L31: COPY 3 + POP_EXCEPT + RERAISE 1 + + -- L32: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) + RERAISE 1 +ExceptionTable: + L1 to L2 -> L32 [0] lasti + L2 to L3 -> L28 [3] lasti + L4 to L5 -> L21 [3] + L5 to L6 -> L10 [7] + L6 to L8 -> L21 [3] + L8 to L9 -> L11 [5] lasti + L9 to L10 -> L32 [0] lasti + L10 to L11 -> L21 [3] + L11 to L12 -> L18 [7] lasti + L12 to L13 -> L14 [10] + L13 to L17 -> L18 [7] lasti + L17 to L19 -> L21 [3] + L20 to L21 -> L28 [3] lasti + L21 to L22 -> L26 [4] lasti + L22 to L23 -> L24 [4] lasti + L23 to L24 -> L28 [3] lasti + L24 to L26 -> L26 [4] lasti + L26 to L27 -> L28 [3] lasti + L27 to L28 -> L32 [0] lasti + L28 to L30 -> L31 [5] lasti + L30 to L32 -> L32 [0] lasti From 256252c7836460ae604c5d4cb17905792ced897a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 28 Apr 2026 10:25:59 +0300 Subject: [PATCH 04/21] Update lock --- Cargo.lock | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 033d32ea286..b755cb08c5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3158,7 +3158,6 @@ dependencies = [ "ahash", "bitflags 2.11.0", "indexmap", - "insta", "itertools 0.14.0", "log", "malachite-bigint", @@ -3427,6 +3426,7 @@ dependencies = [ "icu_normalizer", "icu_properties", "indexmap", + "insta", "itertools 0.14.0", "libc", "liblzma", @@ -3464,6 +3464,7 @@ dependencies = [ "rustpython-common", "rustpython-derive", "rustpython-host_env", + "rustpython-pylib", "rustpython-ruff_python_ast", "rustpython-ruff_python_parser", "rustpython-ruff_source_file", From 7e8938d78ad59458e96d2100acfd1aaf38d40693 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:26:49 +0300 Subject: [PATCH 05/21] Try to set snapshot dir --- crates/stdlib/src/_opcode.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index 37a16a06c25..ae632eb4db0 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -262,11 +262,13 @@ mod tests { macro_rules! assert_dis_snapshot { ($value:expr) => { - insta::assert_snapshot!( - insta::internals::AutoName, - dis($value.trim()), - stringify!($value).trim() - ) + insta::with_settings!({snapshot_path => "./snapshots"}, { + insta::assert_snapshot!( + insta::internals::AutoName, + dis($value.trim()), + stringify!($value).trim() + ) + }) }; } From 693ab8c036120ac6ea005e2979635887357f860f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:27:52 +0300 Subject: [PATCH 06/21] Remove verbose flag --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1633336b0fc..7506d3e17ae 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -77,7 +77,7 @@ jobs: run: cargo clippy ${{ env.CARGO_ARGS }} --workspace --all-targets ${{ env.WORKSPACE_EXCLUDES }} -- -Dwarnings - name: run rust tests - run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --verbose --features threading ${{ env.CARGO_ARGS }} + run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }} - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} From ce8b0c8e4fd4345dadf6202f2630b8f7284fd857 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 28 Apr 2026 15:20:21 +0300 Subject: [PATCH 07/21] Regenerate snapshots after #7711 --- ...k_attribute_and_subscript_expressions.snap | 10 +- ...code__tests__nested_double_async_with.snap | 140 +++++++++--------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap index 91b65bcc1c9..30958043d0f 100644 --- a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap @@ -1,6 +1,6 @@ --- source: crates/stdlib/src/_opcode.rs -expression: "def f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n" +expression: "\"\ndef f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n\"" --- 0 RESUME 0 @@ -15,7 +15,7 @@ expression: "def f(one: int):\n int.new_attr: int\n [list][0].new_attr: [i Disassembly of ", line 1>: 1 RESUME 0 - LOAD_FAST_BORROW 0 + LOAD_FAST_BORROW 0 (format) LOAD_SMALL_INT 2 COMPARE_OP 132 (>) POP_JUMP_IF_FALSE 3 (to L1) @@ -43,10 +43,10 @@ Disassembly of ", line 1>: BUILD_LIST 1 STORE_FAST 1 (my_lst) - 5 LOAD_FAST_BORROW 1 + 5 LOAD_FAST_BORROW 1 (my_lst) POP_TOP - LOAD_FAST_BORROW 0 + LOAD_FAST_BORROW 0 (one) POP_TOP - 6 LOAD_FAST_BORROW 1 + 6 LOAD_FAST_BORROW 1 (my_lst) RETURN_VALUE diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap index 27cf08bb8d7..fa30ce0178d 100644 --- a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap @@ -1,6 +1,6 @@ --- source: crates/stdlib/src/_opcode.rs -expression: "async def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n" +expression: "\"\nasync def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\"" --- 0 RESUME 0 @@ -23,13 +23,13 @@ Disassembly of ", line 1>: CALL 1 BUILD_TUPLE 2 GET_ITER - FOR_ITER 71 (to L9) + L2: FOR_ITER 71 (to L11) STORE_FAST 0 (stop_exc) 3 LOAD_GLOBAL 4 (self) LOAD_ATTR 7 (subTest + NULL|self) LOAD_GLOBAL 9 (type + NULL) - LOAD_FAST_BORROW 0 + LOAD_FAST_BORROW 0 (stop_exc) CALL 1 LOAD_CONST 2 (('type',)) CALL_KW 1 @@ -39,11 +39,11 @@ Disassembly of ", line 1>: SWAP 3 LOAD_SPECIAL 0 (__enter__) CALL 0 - L2: POP_TOP + L3: POP_TOP - 4 L3: NOP + 4 L4: NOP - 5 L4: LOAD_GLOBAL 11 (egg + NULL) + 5 L5: LOAD_GLOBAL 11 (egg + NULL) CALL 0 COPY 1 LOAD_SPECIAL 3 (__aexit__) @@ -53,49 +53,49 @@ Disassembly of ", line 1>: CALL 0 GET_AWAITABLE 1 LOAD_CONST 3 (None) - SEND 3 (to L7) - L5: YIELD_VALUE 1 - L6: RESUME 3 - JUMP_BACKWARD_NO_INTERRUPT 5 - L7: END_SEND - L8: POP_TOP - - 6 LOAD_FAST_BORROW 0 + L6: SEND 3 (to L9) + L7: YIELD_VALUE 1 + L8: RESUME 3 + JUMP_BACKWARD_NO_INTERRUPT 5 (to L6) + L9: END_SEND + L10: POP_TOP + + 6 LOAD_FAST_BORROW 0 (stop_exc) RAISE_VARARGS 1 - 2 L9: END_FOR + 2 L11: END_FOR POP_ITER LOAD_CONST 3 (None) RETURN_VALUE - 5 L10: CLEANUP_THROW - JUMP_BACKWARD_NO_INTERRUPT 10 - L11: PUSH_EXC_INFO + 5 L12: CLEANUP_THROW + JUMP_BACKWARD_NO_INTERRUPT 10 (to L9) + L13: PUSH_EXC_INFO WITH_EXCEPT_START GET_AWAITABLE 2 LOAD_CONST 3 (None) - SEND 4 (to L15) - L12: YIELD_VALUE 1 - L13: RESUME 3 - JUMP_BACKWARD_NO_INTERRUPT 5 - L14: CLEANUP_THROW - L15: END_SEND + L14: SEND 4 (to L18) + L15: YIELD_VALUE 1 + L16: RESUME 3 + JUMP_BACKWARD_NO_INTERRUPT 5 (to L14) + L17: CLEANUP_THROW + L18: END_SEND TO_BOOL - POP_JUMP_IF_TRUE 2 (to L16) + POP_JUMP_IF_TRUE 2 (to L19) NOT_TAKEN RERAISE 2 - L16: POP_TOP - L17: POP_EXCEPT + L19: POP_TOP + L20: POP_EXCEPT POP_TOP POP_TOP POP_TOP - JUMP_FORWARD 3 - L18: COPY 3 + JUMP_FORWARD 3 (to L22) + L21: COPY 3 POP_EXCEPT RERAISE 1 - L19: NOP + L22: NOP - 10 L20: LOAD_GLOBAL 4 (self) + 10 L23: LOAD_GLOBAL 4 (self) LOAD_ATTR 13 (fail + NULL|self) LOAD_FAST 0 (stop_exc) FORMAT_SIMPLE @@ -103,81 +103,81 @@ Disassembly of ", line 1>: BUILD_STRING 2 CALL 1 POP_TOP - JUMP_FORWARD 45 + JUMP_FORWARD 45 (to L30) - -- L21: PUSH_EXC_INFO + -- L24: PUSH_EXC_INFO 7 LOAD_GLOBAL 14 (Exception) CHECK_EXC_MATCH - POP_JUMP_IF_FALSE 32 (to L25) + POP_JUMP_IF_FALSE 32 (to L28) NOT_TAKEN STORE_FAST 1 (ex) - 8 L22: LOAD_GLOBAL 4 (self) + 8 L25: LOAD_GLOBAL 4 (self) LOAD_ATTR 17 (assertIs + NULL|self) LOAD_FAST_LOAD_FAST 16 (ex, stop_exc) CALL 2 POP_TOP - L23: POP_EXCEPT + L26: POP_EXCEPT LOAD_CONST 3 (None) STORE_FAST 1 (ex) DELETE_FAST 1 (ex) - JUMP_FORWARD 8 + JUMP_FORWARD 8 (to L30) - -- L24: LOAD_CONST 3 (None) + -- L27: LOAD_CONST 3 (None) STORE_FAST 1 (ex) DELETE_FAST 1 (ex) RERAISE 1 - 7 L25: RERAISE 0 + 7 L28: RERAISE 0 - -- L26: COPY 3 + -- L29: COPY 3 POP_EXCEPT RERAISE 1 - 3 L27: LOAD_CONST 3 (None) + 3 L30: LOAD_CONST 3 (None) LOAD_CONST 3 (None) LOAD_CONST 3 (None) CALL 3 POP_TOP - JUMP_BACKWARD 188 - L28: PUSH_EXC_INFO + JUMP_BACKWARD 188 (to L2) + L31: PUSH_EXC_INFO WITH_EXCEPT_START TO_BOOL - POP_JUMP_IF_TRUE 2 (to L29) + POP_JUMP_IF_TRUE 2 (to L32) NOT_TAKEN RERAISE 2 - L29: POP_TOP - L30: POP_EXCEPT + L32: POP_TOP + L33: POP_EXCEPT POP_TOP POP_TOP POP_TOP - JUMP_BACKWARD 205 - L31: COPY 3 + JUMP_BACKWARD 205 (to L2) + L34: COPY 3 POP_EXCEPT RERAISE 1 - -- L32: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) + -- L35: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) RERAISE 1 ExceptionTable: - L1 to L2 -> L32 [0] lasti - L2 to L3 -> L28 [3] lasti - L4 to L5 -> L21 [3] - L5 to L6 -> L10 [7] - L6 to L8 -> L21 [3] - L8 to L9 -> L11 [5] lasti - L9 to L10 -> L32 [0] lasti - L10 to L11 -> L21 [3] - L11 to L12 -> L18 [7] lasti - L12 to L13 -> L14 [10] - L13 to L17 -> L18 [7] lasti - L17 to L19 -> L21 [3] - L20 to L21 -> L28 [3] lasti - L21 to L22 -> L26 [4] lasti - L22 to L23 -> L24 [4] lasti - L23 to L24 -> L28 [3] lasti - L24 to L26 -> L26 [4] lasti - L26 to L27 -> L28 [3] lasti - L27 to L28 -> L32 [0] lasti - L28 to L30 -> L31 [5] lasti - L30 to L32 -> L32 [0] lasti + L1 to L3 -> L35 [0] lasti + L3 to L4 -> L31 [3] lasti + L5 to L7 -> L24 [3] + L7 to L8 -> L12 [7] + L8 to L10 -> L24 [3] + L10 to L11 -> L13 [5] lasti + L11 to L12 -> L35 [0] lasti + L12 to L13 -> L24 [3] + L13 to L15 -> L21 [7] lasti + L15 to L16 -> L17 [10] + L16 to L20 -> L21 [7] lasti + L20 to L22 -> L24 [3] + L23 to L24 -> L31 [3] lasti + L24 to L25 -> L29 [4] lasti + L25 to L26 -> L27 [4] lasti + L26 to L27 -> L31 [3] lasti + L27 to L29 -> L29 [4] lasti + L29 to L30 -> L31 [3] lasti + L30 to L31 -> L35 [0] lasti + L31 to L33 -> L34 [5] lasti + L33 to L35 -> L35 [0] lasti From d9751c9b55febb59a45b9cff9d66e203b2a504d8 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 28 Apr 2026 18:21:51 +0300 Subject: [PATCH 08/21] Revert "Bump insta from 1.46.3 to 1.47.2 (#7706)" This reverts commit e6d9ea6bfea2ade954842aceb07ac7e566dfe8bb. --- Cargo.lock | 11 ++++++----- Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b755cb08c5f..72d44aeeb3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,13 +637,14 @@ dependencies = [ [[package]] name = "console" -version = "0.16.3" +version = "0.15.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d64e8af5551369d19cf50138de61f1c42074ab970f74e99be916646777f8fc87" +checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" dependencies = [ "encode_unicode", "libc", - "windows-sys 0.61.2", + "once_cell", + "windows-sys 0.59.0", ] [[package]] @@ -1621,9 +1622,9 @@ dependencies = [ [[package]] name = "insta" -version = "1.47.2" +version = "1.46.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b4a6248eb93a4401ed2f37dfe8ea592d3cf05b7cf4f8efa867b6895af7e094e" +checksum = "e82db8c87c7f1ccecb34ce0c24399b8a73081427f3c7c50a5d597925356115e4" dependencies = [ "console", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 3262749b72c..92f466e34d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -191,7 +191,7 @@ glob = "0.3" hex = "0.4.3" hexf-parse = "0.2.1" indexmap = { version = "2.14.0", features = ["std"] } -insta = "1.47" +insta = "1.46" itertools = "0.14.0" is-macro = "0.3.7" junction = "1.4.2" From b0ec4a0ec7cc5b16463a3b81916f7b8a52061474 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 18:29:31 +0300 Subject: [PATCH 09/21] Debug info --- .github/workflows/ci.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7506d3e17ae..8dd2ba31b87 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -122,6 +122,12 @@ jobs: PYTHONPATH: scripts if: runner.os == 'Linux' + - name: debug + if: ${{ (runner.os == 'Linux') && !cancelled() }} + run: | + git status + git diff --name-only + cargo_check: if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }} name: cargo check From dc4c8573e97b91fd104f25f9099f0446384a6aab Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 19:38:30 +0300 Subject: [PATCH 10/21] Show diff as well --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8dd2ba31b87..17c7e441d6b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -127,6 +127,7 @@ jobs: run: | git status git diff --name-only + git diff cargo_check: if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }} From bae530c1ca8ed8d6d7c159024a411b79ec87c8ca Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 19:54:06 +0300 Subject: [PATCH 11/21] Show debug faster --- .github/workflows/ci.yaml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 17c7e441d6b..fbbfcc951fa 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -74,11 +74,19 @@ jobs: uses: ./.github/actions/install-macos-deps - name: run clippy + if: false run: cargo clippy ${{ env.CARGO_ARGS }} --workspace --all-targets ${{ env.WORKSPACE_EXCLUDES }} -- -Dwarnings - name: run rust tests run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }} + - name: debug + if: ${{ (runner.os == 'Linux') && !cancelled() }} + run: | + git status + git diff --name-only + git diff + - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} @@ -122,12 +130,6 @@ jobs: PYTHONPATH: scripts if: runner.os == 'Linux' - - name: debug - if: ${{ (runner.os == 'Linux') && !cancelled() }} - run: | - git status - git diff --name-only - git diff cargo_check: if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }} From 7a77e6518d76f7a43ddca3eb6b03b10c63a492ea Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 19:56:02 +0300 Subject: [PATCH 12/21] CI: true env --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fbbfcc951fa..c89bd9e262d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,6 +34,7 @@ env: CARGO_PROFILE_RELEASE_DEBUG: 0 CARGO_TERM_COLOR: always FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true # TODO: Remove on 2026/06/02 + CI: true jobs: rust_tests: From 58c8d53814377223317f118b15b554ab29b40b5c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:09:17 +0300 Subject: [PATCH 13/21] Recert CI --- .github/workflows/ci.yaml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c89bd9e262d..1633336b0fc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,7 +34,6 @@ env: CARGO_PROFILE_RELEASE_DEBUG: 0 CARGO_TERM_COLOR: always FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true # TODO: Remove on 2026/06/02 - CI: true jobs: rust_tests: @@ -75,18 +74,10 @@ jobs: uses: ./.github/actions/install-macos-deps - name: run clippy - if: false run: cargo clippy ${{ env.CARGO_ARGS }} --workspace --all-targets ${{ env.WORKSPACE_EXCLUDES }} -- -Dwarnings - name: run rust tests - run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }} - - - name: debug - if: ${{ (runner.os == 'Linux') && !cancelled() }} - run: | - git status - git diff --name-only - git diff + run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --verbose --features threading ${{ env.CARGO_ARGS }} - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} @@ -131,7 +122,6 @@ jobs: PYTHONPATH: scripts if: runner.os == 'Linux' - cargo_check: if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }} name: cargo check From a3f690ec750e2f4aa8ebcc115f3101507b0c050f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:09:56 +0300 Subject: [PATCH 14/21] Add `CI: true` in ci emv --- .github/workflows/ci.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1633336b0fc..6b0962add47 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,6 +34,7 @@ env: CARGO_PROFILE_RELEASE_DEBUG: 0 CARGO_TERM_COLOR: always FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true # TODO: Remove on 2026/06/02 + CI: true jobs: rust_tests: @@ -77,7 +78,7 @@ jobs: run: cargo clippy ${{ env.CARGO_ARGS }} --workspace --all-targets ${{ env.WORKSPACE_EXCLUDES }} -- -Dwarnings - name: run rust tests - run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --verbose --features threading ${{ env.CARGO_ARGS }} + run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }} - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} From 182b4f5c6642c246d5c853f423da21778c4690b1 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:10:42 +0300 Subject: [PATCH 15/21] Reapply "Bump insta from 1.46.3 to 1.47.2 (#7706)" This reverts commit 693ca8cbe4d7885a81162a9be31e8bb567db885a. --- Cargo.lock | 11 +++++------ Cargo.toml | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72d44aeeb3d..b755cb08c5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,14 +637,13 @@ dependencies = [ [[package]] name = "console" -version = "0.15.11" +version = "0.16.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" +checksum = "d64e8af5551369d19cf50138de61f1c42074ab970f74e99be916646777f8fc87" dependencies = [ "encode_unicode", "libc", - "once_cell", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1622,9 +1621,9 @@ dependencies = [ [[package]] name = "insta" -version = "1.46.3" +version = "1.47.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e82db8c87c7f1ccecb34ce0c24399b8a73081427f3c7c50a5d597925356115e4" +checksum = "7b4a6248eb93a4401ed2f37dfe8ea592d3cf05b7cf4f8efa867b6895af7e094e" dependencies = [ "console", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 92f466e34d5..3262749b72c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -191,7 +191,7 @@ glob = "0.3" hex = "0.4.3" hexf-parse = "0.2.1" indexmap = { version = "2.14.0", features = ["std"] } -insta = "1.46" +insta = "1.47" itertools = "0.14.0" is-macro = "0.3.7" junction = "1.4.2" From c235e6ce338a2837b6e779aaf7c895a7ca837556 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 30 Apr 2026 12:39:10 +0300 Subject: [PATCH 16/21] simplify macro --- crates/stdlib/src/_opcode.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index ae632eb4db0..8b27ad36500 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -262,13 +262,7 @@ mod tests { macro_rules! assert_dis_snapshot { ($value:expr) => { - insta::with_settings!({snapshot_path => "./snapshots"}, { - insta::assert_snapshot!( - insta::internals::AutoName, - dis($value.trim()), - stringify!($value).trim() - ) - }) + insta::assert_snapshot!(dis($value)) }; } From 42353fa49a95780f3d54a135f92c02cb1598686f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 30 Apr 2026 12:39:35 +0300 Subject: [PATCH 17/21] trim on function side --- crates/stdlib/src/_opcode.rs | 40 +++++++++++-------- ...k_attribute_and_subscript_expressions.snap | 10 ++--- ...code__tests__nested_double_async_with.snap | 6 +-- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index 8b27ad36500..513aadafc57 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -272,7 +272,7 @@ mod tests { /// /// Memory addresses in the output are replaced with `0xdeadbeef` for consistency. fn dis(source: &str) -> String { - let fname = String::from(""); + let fname = String::from(""); let builder = vm::Interpreter::builder(Default::default()); let stdlib_defs = crate::stdlib_module_defs(&builder.ctx); @@ -284,7 +284,7 @@ mod tests { interp.enter(|vm| { let scope = vm.new_scope_with_builtins(); let code_obj = vm - .compile(source, Mode::Exec, fname.clone()) + .compile(source.trim(), Mode::Exec, fname.clone()) .map_err(|err| vm.new_syntax_error(&err, Some(source))) .unwrap(); scope.globals.set_item("code", code_obj.into(), vm).unwrap(); @@ -320,57 +320,65 @@ output = re.sub(r'(0xdeadbeef', tmp #[test] fn test_if_ors() { assert_dis_snapshot!( - " + r#" if True or False or False: pass -" +"# ) } #[test] fn test_if_ands() { assert_dis_snapshot!( - " + r#" if True and False and False: pass -" +"# ) } #[test] fn test_if_mixed() { assert_dis_snapshot!( - " + r#" if (True and False) or (False and True): pass -" +"# ) } #[test] fn test_nested_bool_op() { - assert_dis_snapshot!("x = Test() and False or False") + assert_dis_snapshot!( + r#" +x = Test() and False or False +"# + ) } #[test] fn test_const_no_op() { - assert_dis_snapshot!("x = not True") + assert_dis_snapshot!( + r#" +x = not True +"# + ) } #[test] fn test_constant_true_if_pass_keeps_line_anchor_nop() { assert_dis_snapshot!( - " + r#" if 1: pass -" +"# ) } #[test] fn test_nested_double_async_with() { assert_dis_snapshot!( - " + r#" async def test(): for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')): with self.subTest(type=type(stop_exc)): @@ -381,21 +389,21 @@ async def test(): self.assertIs(ex, stop_exc) else: self.fail(f'{stop_exc} was suppressed') -" +"# ) } #[test] fn test_bare_function_annotations_check_attribute_and_subscript_expressions() { assert_dis_snapshot!( - " + r#" def f(one: int): int.new_attr: int [list][0].new_attr: [int, str] my_lst = [1] my_lst[one]: int return my_lst -" +"# ) } } diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap index 30958043d0f..3274352b920 100644 --- a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__bare_function_annotations_check_attribute_and_subscript_expressions.snap @@ -1,19 +1,19 @@ --- source: crates/stdlib/src/_opcode.rs -expression: "\"\ndef f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n\"" +expression: "dis(r#\"\ndef f(one: int):\n int.new_attr: int\n [list][0].new_attr: [int, str]\n my_lst = [1]\n my_lst[one]: int\n return my_lst\n\"#)" --- 0 RESUME 0 - 1 LOAD_CONST 0 (", line 1>) + 1 LOAD_CONST 0 (", line 1>) MAKE_FUNCTION - LOAD_CONST 1 (", line 1>) + LOAD_CONST 1 (", line 1>) MAKE_FUNCTION SET_FUNCTION_ATTRIBUTE 16 (annotate) STORE_NAME 0 (f) LOAD_CONST 2 (None) RETURN_VALUE -Disassembly of ", line 1>: +Disassembly of ", line 1>: 1 RESUME 0 LOAD_FAST_BORROW 0 (format) LOAD_SMALL_INT 2 @@ -27,7 +27,7 @@ Disassembly of ", line 1 BUILD_MAP 1 RETURN_VALUE -Disassembly of ", line 1>: +Disassembly of ", line 1>: 1 RESUME 0 2 LOAD_GLOBAL 0 (int) diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap index fa30ce0178d..e08ecabae07 100644 --- a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap @@ -1,16 +1,16 @@ --- source: crates/stdlib/src/_opcode.rs -expression: "\"\nasync def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\"" +expression: "dis(r#\"\nasync def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\"#)" --- 0 RESUME 0 - 1 LOAD_CONST 0 (", line 1>) + 1 LOAD_CONST 0 (", line 1>) MAKE_FUNCTION STORE_NAME 0 (test) LOAD_CONST 1 (None) RETURN_VALUE -Disassembly of ", line 1>: +Disassembly of ", line 1>: 1 RETURN_GENERATOR POP_TOP RESUME 0 From 2450d694cff996fc20b8400829bcf83deb0c13c3 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 1 May 2026 12:08:50 +0300 Subject: [PATCH 18/21] Force insta workspace root --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6b0962add47..726aa9fbfe1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -79,6 +79,8 @@ jobs: - name: run rust tests run: cargo test --workspace ${{ env.WORKSPACE_EXCLUDES }} --features threading ${{ env.CARGO_ARGS }} + env: + INSTA_WORKSPACE_ROOT: ${{ github.workspace }} - name: check compilation without threading run: cargo check ${{ env.CARGO_ARGS }} From ad2b24f869be76aa08c4b70827621cf08abfeb2d Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Mon, 4 May 2026 15:09:57 +0300 Subject: [PATCH 19/21] fix merge --- crates/compiler-core/src/bytecode/instruction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 65c6ba59f1e..21869601598 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -76,7 +76,7 @@ macro_rules! define_opcodes { impl ::core::fmt::Display for $opcode_name { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - write!(f, "{}", self.name()) + self.name().fmt(f) } } From 5c3aa0d7d3386bacde142c1355685fb6359e8426 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 5 May 2026 10:23:22 +0300 Subject: [PATCH 20/21] fixing bad merge --- crates/codegen/src/compile.rs | 100 ---------------------------------- 1 file changed, 100 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index debec3acd25..33c295a5e24 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -12267,26 +12267,6 @@ def f(sys, os, file): }) } - macro_rules! assert_dis_snapshot { - ($value:expr) => { - insta::assert_snapshot!( - insta::internals::AutoName, - $value.display_expand_code_objects().to_string(), - stringify!($value) - ) - }; - } - - #[test] - fn test_if_ors() { - assert_dis_snapshot!(compile_exec( - "\ -if True or False or False: - pass -" - )); - } - #[test] fn test_trace_assert_true_try_pair() { let trace = compile_exec_late_cfg_trace( @@ -12521,44 +12501,6 @@ def f(self): ); } - #[test] - fn test_if_ands() { - assert_dis_snapshot!(compile_exec( - "\ -if True and False and False: - pass -" - )); - } - - #[test] - fn test_if_mixed() { - assert_dis_snapshot!(compile_exec( - "\ -if (True and False) or (False and True): - pass -" - )); - } - - #[test] - fn test_nested_bool_op() { - assert_dis_snapshot!(compile_exec( - "\ -x = Test() and False or False -" - )); - } - - #[test] - fn test_const_bool_not_op() { - assert_dis_snapshot!(compile_exec_optimized( - "\ -x = not True -" - )); - } - #[test] fn test_plain_constant_bool_op_folds_to_selected_operand() { let code = compile_exec( @@ -12832,24 +12774,6 @@ def f(self, mod): ); } - #[test] - fn test_nested_double_async_with() { - assert_dis_snapshot!(compile_exec( - "\ -async def test(): - for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')): - with self.subTest(type=type(stop_exc)): - try: - async with egg(): - raise stop_exc - except Exception as ex: - self.assertIs(ex, stop_exc) - else: - self.fail(f'{stop_exc} was suppressed') -" - )); - } - #[test] fn test_scope_exit_instructions_keep_line_numbers() { let code = compile_exec( @@ -13998,20 +13922,6 @@ def f(expected_ns, namespace): } } - #[test] - fn test_bare_function_annotations_check_attribute_and_subscript_expressions() { - assert_dis_snapshot!(compile_exec( - "\ -def f(one: int): - int.new_attr: int - [list][0].new_attr: [int, str] - my_lst = [1] - my_lst[one]: int - return my_lst -" - )); - } - #[test] fn test_non_simple_bare_name_annotation_does_not_create_local_binding() { let code = compile_exec( @@ -14044,16 +13954,6 @@ def f2bad(): ); } - #[test] - fn test_constant_true_if_pass_keeps_line_anchor_nop() { - assert_dis_snapshot!(compile_exec( - "\ -if 1: - pass -" - )); - } - #[test] fn test_negative_constant_binop_folds_after_unary_folding() { let code = compile_exec( From 15d212fd8c83f56898ce77e283fb2402046802fa Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 5 May 2026 10:30:16 +0300 Subject: [PATCH 21/21] regen snapshot --- ...code__tests__nested_double_async_with.snap | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap index e08ecabae07..04684cb6c52 100644 --- a/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap +++ b/crates/stdlib/src/snapshots/rustpython_stdlib___opcode__tests__nested_double_async_with.snap @@ -69,33 +69,33 @@ Disassembly of ", line 1>: RETURN_VALUE 5 L12: CLEANUP_THROW - JUMP_BACKWARD_NO_INTERRUPT 10 (to L9) - L13: PUSH_EXC_INFO + L13: JUMP_BACKWARD_NO_INTERRUPT 10 (to L9) + L14: PUSH_EXC_INFO WITH_EXCEPT_START GET_AWAITABLE 2 LOAD_CONST 3 (None) - L14: SEND 4 (to L18) - L15: YIELD_VALUE 1 - L16: RESUME 3 - JUMP_BACKWARD_NO_INTERRUPT 5 (to L14) - L17: CLEANUP_THROW - L18: END_SEND + L15: SEND 4 (to L19) + L16: YIELD_VALUE 1 + L17: RESUME 3 + JUMP_BACKWARD_NO_INTERRUPT 5 (to L15) + L18: CLEANUP_THROW + L19: END_SEND TO_BOOL - POP_JUMP_IF_TRUE 2 (to L19) + POP_JUMP_IF_TRUE 2 (to L20) NOT_TAKEN RERAISE 2 - L19: POP_TOP - L20: POP_EXCEPT + L20: POP_TOP + L21: POP_EXCEPT POP_TOP POP_TOP POP_TOP - JUMP_FORWARD 3 (to L22) - L21: COPY 3 + JUMP_FORWARD 3 (to L23) + L22: COPY 3 POP_EXCEPT RERAISE 1 - L22: NOP + L23: NOP - 10 L23: LOAD_GLOBAL 4 (self) + 10 L24: LOAD_GLOBAL 4 (self) LOAD_ATTR 13 (fail + NULL|self) LOAD_FAST 0 (stop_exc) FORMAT_SIMPLE @@ -103,81 +103,81 @@ Disassembly of ", line 1>: BUILD_STRING 2 CALL 1 POP_TOP - JUMP_FORWARD 45 (to L30) + JUMP_FORWARD 45 (to L31) - -- L24: PUSH_EXC_INFO + -- L25: PUSH_EXC_INFO 7 LOAD_GLOBAL 14 (Exception) CHECK_EXC_MATCH - POP_JUMP_IF_FALSE 32 (to L28) + POP_JUMP_IF_FALSE 32 (to L29) NOT_TAKEN STORE_FAST 1 (ex) - 8 L25: LOAD_GLOBAL 4 (self) + 8 L26: LOAD_GLOBAL 4 (self) LOAD_ATTR 17 (assertIs + NULL|self) LOAD_FAST_LOAD_FAST 16 (ex, stop_exc) CALL 2 POP_TOP - L26: POP_EXCEPT + L27: POP_EXCEPT LOAD_CONST 3 (None) STORE_FAST 1 (ex) DELETE_FAST 1 (ex) - JUMP_FORWARD 8 (to L30) + JUMP_FORWARD 8 (to L31) - -- L27: LOAD_CONST 3 (None) + -- L28: LOAD_CONST 3 (None) STORE_FAST 1 (ex) DELETE_FAST 1 (ex) RERAISE 1 - 7 L28: RERAISE 0 + 7 L29: RERAISE 0 - -- L29: COPY 3 + -- L30: COPY 3 POP_EXCEPT RERAISE 1 - 3 L30: LOAD_CONST 3 (None) + 3 L31: LOAD_CONST 3 (None) LOAD_CONST 3 (None) LOAD_CONST 3 (None) CALL 3 POP_TOP JUMP_BACKWARD 188 (to L2) - L31: PUSH_EXC_INFO + L32: PUSH_EXC_INFO WITH_EXCEPT_START TO_BOOL - POP_JUMP_IF_TRUE 2 (to L32) + POP_JUMP_IF_TRUE 2 (to L33) NOT_TAKEN RERAISE 2 - L32: POP_TOP - L33: POP_EXCEPT + L33: POP_TOP + L34: POP_EXCEPT POP_TOP POP_TOP POP_TOP JUMP_BACKWARD 205 (to L2) - L34: COPY 3 + L35: COPY 3 POP_EXCEPT RERAISE 1 - -- L35: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) + -- L36: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) RERAISE 1 ExceptionTable: - L1 to L3 -> L35 [0] lasti - L3 to L4 -> L31 [3] lasti - L5 to L7 -> L24 [3] + L1 to L3 -> L36 [0] lasti + L3 to L4 -> L32 [3] lasti + L5 to L7 -> L25 [3] L7 to L8 -> L12 [7] - L8 to L10 -> L24 [3] - L10 to L11 -> L13 [5] lasti - L11 to L12 -> L35 [0] lasti - L12 to L13 -> L24 [3] - L13 to L15 -> L21 [7] lasti - L15 to L16 -> L17 [10] - L16 to L20 -> L21 [7] lasti - L20 to L22 -> L24 [3] - L23 to L24 -> L31 [3] lasti - L24 to L25 -> L29 [4] lasti - L25 to L26 -> L27 [4] lasti - L26 to L27 -> L31 [3] lasti - L27 to L29 -> L29 [4] lasti - L29 to L30 -> L31 [3] lasti - L30 to L31 -> L35 [0] lasti - L31 to L33 -> L34 [5] lasti - L33 to L35 -> L35 [0] lasti + L8 to L10 -> L25 [3] + L10 to L11 -> L14 [5] lasti + L11 to L12 -> L36 [0] lasti + L12 to L13 -> L25 [3] + L14 to L16 -> L22 [7] lasti + L16 to L17 -> L18 [10] + L17 to L21 -> L22 [7] lasti + L21 to L23 -> L25 [3] + L24 to L25 -> L32 [3] lasti + L25 to L26 -> L30 [4] lasti + L26 to L27 -> L28 [4] lasti + L27 to L28 -> L32 [3] lasti + L28 to L30 -> L30 [4] lasti + L30 to L31 -> L32 [3] lasti + L31 to L32 -> L36 [0] lasti + L32 to L34 -> L35 [5] lasti + L34 to L36 -> L36 [0] lasti