Skip to content

Commit 1034477

Browse files
authored
unit test fix - only allow catching exceptions - test_catch_non_BaseException (RustPython#5249)
* test_baseexception.test_catch_non_BaseException * enabling test_except_star_invalid_exception_type
1 parent 49cfcd8 commit 1034477

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎Lib/test/test_baseexception.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ def test_raise_string(self):
196196
# Raising a string raises TypeError.
197197
self.raise_fails("spam")
198198

199-
# TODO: RUSTPYTHON
200-
@unittest.expectedFailure
201199
def test_catch_non_BaseException(self):
202200
# Trying to catch an object that does not inherit from BaseException
203201
# is not allowed.

‎Lib/test/test_exceptions.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,8 +2613,6 @@ def test_incorrect_constructor(self):
26132613

26142614

26152615
class TestInvalidExceptionMatcher(unittest.TestCase):
2616-
# TODO: RUSTPYTHON
2617-
@unittest.expectedFailure
26182616
def test_except_star_invalid_exception_type(self):
26192617
with self.assertRaises(TypeError):
26202618
try:

‎vm/src/frame.rs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,27 @@ impl ExecutingFrame<'_> {
18361836
bytecode::TestOperator::IsNot => !a.is(&b),
18371837
bytecode::TestOperator::In => self._in(vm, a, &b)?,
18381838
bytecode::TestOperator::NotIn => self._not_in(vm, a, &b)?,
1839-
bytecode::TestOperator::ExceptionMatch => a.is_instance(&b, vm)?,
1839+
bytecode::TestOperator::ExceptionMatch => {
1840+
if let Some(tuple_of_exceptions) = b.downcast_ref::<PyTuple>() {
1841+
for exception in tuple_of_exceptions.iter() {
1842+
if !exception
1843+
.is_subclass(vm.ctx.exceptions.base_exception_type.into(), vm)?
1844+
{
1845+
return Err(vm.new_type_error(
1846+
"catching classes that do not inherit from BaseException is not allowed"
1847+
.to_owned(),
1848+
));
1849+
}
1850+
}
1851+
} else if !b.is_subclass(vm.ctx.exceptions.base_exception_type.into(), vm)? {
1852+
return Err(vm.new_type_error(
1853+
"catching classes that do not inherit from BaseException is not allowed"
1854+
.to_owned(),
1855+
));
1856+
}
1857+
1858+
a.is_instance(&b, vm)?
1859+
}
18401860
};
18411861

18421862
self.push_value(vm.ctx.new_bool(value).into());

0 commit comments

Comments
 (0)