Skip to content

Commit ea4e001

Browse files
committed
fix EG subclass
1 parent 3557eb5 commit ea4e001

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

crates/codegen/src/compile.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,13 +2233,14 @@ impl Compiler {
22332233
emit!(self, Instruction::CheckEgMatch);
22342234
// Stack: [orig, list, new_rest, match]
22352235

2236-
// Check if match is not None
2236+
// Check if match is not None (use identity check, not truthiness)
22372237
// CopyItem is 1-indexed: CopyItem(1) = TOS, CopyItem(2) = second from top
22382238
emit!(self, Instruction::CopyItem { index: 1 });
2239-
emit!(self, Instruction::ToBool);
2239+
self.emit_load_const(ConstantData::None);
2240+
emit!(self, Instruction::IsOp(bytecode::Invert::No)); // is None?
22402241
emit!(
22412242
self,
2242-
Instruction::PopJumpIfFalse {
2243+
Instruction::PopJumpIfTrue {
22432244
target: no_match_block
22442245
}
22452246
);
@@ -2369,9 +2370,10 @@ impl Compiler {
23692370
);
23702371
// Stack: [result] (exception to reraise or None)
23712372

2372-
// CopyItem(1) copies TOS
2373+
// Check if result is not None (use identity check, not truthiness)
23732374
emit!(self, Instruction::CopyItem { index: 1 });
2374-
emit!(self, Instruction::ToBool);
2375+
self.emit_load_const(ConstantData::None);
2376+
emit!(self, Instruction::IsOp(bytecode::Invert::Yes)); // is not None?
23752377
emit!(
23762378
self,
23772379
Instruction::PopJumpIfTrue {

crates/vm/src/exceptions.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,7 +2459,7 @@ fn check_except_star_type_valid(match_type: &PyObjectRef, vm: &VirtualMachine) -
24592459
// If it's a tuple, check each element
24602460
if let Ok(tuple) = match_type.clone().downcast::<PyTuple>() {
24612461
for item in tuple.iter() {
2462-
check_one(&item)?;
2462+
check_one(item)?;
24632463
}
24642464
} else {
24652465
check_one(match_type)?;
@@ -2510,6 +2510,13 @@ pub fn exception_group_match(
25102510
// Check for partial match if it's an exception group
25112511
if exc_value.fast_isinstance(vm.ctx.exceptions.base_exception_group) {
25122512
let pair = vm.call_method(exc_value, "split", (match_type.clone(),))?;
2513+
if !pair.class().is(vm.ctx.types.tuple_type) {
2514+
return Err(vm.new_type_error(format!(
2515+
"{}.split must return a tuple, not {}",
2516+
exc_value.class().name(),
2517+
pair.class().name()
2518+
)));
2519+
}
25132520
let pair_tuple: PyTupleRef = pair.try_into_value(vm)?;
25142521
if pair_tuple.len() < 2 {
25152522
return Err(vm.new_type_error(format!(
@@ -2528,7 +2535,7 @@ pub fn exception_group_match(
25282535
}
25292536

25302537
/// Prepare exception for reraise in except* block.
2531-
/// Implements _PyExc_PrepReraiseStar from Objects/exceptions.c
2538+
/// Implements _PyExc_PrepReraiseStar
25322539
pub fn prep_reraise_star(orig: PyObjectRef, excs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
25332540
use crate::builtins::PyList;
25342541

@@ -2633,11 +2640,11 @@ fn collect_exception_group_leaf_ids(
26332640
}
26342641

26352642
// Recurse into exception group's exceptions
2636-
if let Ok(excs_attr) = exc.get_attr("exceptions", vm) {
2637-
if let Ok(tuple) = excs_attr.downcast::<PyTuple>() {
2638-
for e in tuple.iter() {
2639-
collect_exception_group_leaf_ids(&e, leaf_ids, vm);
2640-
}
2643+
if let Ok(excs_attr) = exc.get_attr("exceptions", vm)
2644+
&& let Ok(tuple) = excs_attr.downcast::<PyTuple>()
2645+
{
2646+
for e in tuple.iter() {
2647+
collect_exception_group_leaf_ids(e, leaf_ids, vm);
26412648
}
26422649
}
26432650
}
@@ -2689,7 +2696,7 @@ fn split_by_leaf_ids(
26892696

26902697
let mut matched = Vec::new();
26912698
for e in tuple.iter() {
2692-
let m = split_by_leaf_ids(&e, leaf_ids, vm)?;
2699+
let m = split_by_leaf_ids(e, leaf_ids, vm)?;
26932700
if !vm.is_none(&m) {
26942701
matched.push(m);
26952702
}

crates/vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
33
builtins::{
4-
PyBaseExceptionRef, PyBaseException, PyCode, PyCoroutine, PyDict, PyDictRef, PyGenerator, PyList, PySet,
5-
PySlice, PyStr, PyStrInterned, PyStrRef, PyTraceback, PyType,
4+
PyBaseException, PyBaseExceptionRef, PyCode, PyCoroutine, PyDict, PyDictRef, PyGenerator,
5+
PyList, PySet, PySlice, PyStr, PyStrInterned, PyStrRef, PyTraceback, PyType,
66
asyncgenerator::PyAsyncGenWrappedValue,
77
function::{PyCell, PyCellRef, PyFunction},
88
tuple::{PyTuple, PyTupleRef},

0 commit comments

Comments
 (0)