This commit is contained in:
blueloveTH 2024-01-02 16:52:00 +08:00
parent c6ec028730
commit 5ec1622887
3 changed files with 22 additions and 5 deletions

View File

@ -1551,14 +1551,14 @@ void add_module_traceback(VM* vm){
PyObject* mod = vm->new_module("traceback");
vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
if(vm->_last_exception==nullptr) vm->ValueError("no exception");
Exception& e = CAST(Exception&, vm->_last_exception);
Exception& e = _CAST(Exception&, vm->_last_exception);
vm->stdout_write(e.summary());
return vm->None;
});
vm->bind_func<0>(mod, "format_exc", [](VM* vm, ArgsView args) {
if(vm->_last_exception==nullptr) vm->ValueError("no exception");
Exception& e = CAST(Exception&, vm->_last_exception);
Exception& e = _CAST(Exception&, vm->_last_exception);
return VAR(e.summary());
});
}

View File

@ -38,7 +38,7 @@ assert True
def f():
try:
raise KeyError('foo')
except A: # will fail to catch
except IndexError: # will fail to catch
exit(1)
except:
pass
@ -52,9 +52,9 @@ def f1():
try:
a = {1: 2, 3: 4}
x = a[0]
except A:
except RuntimeError:
exit(1)
except B:
except IndexError:
exit(1)
exit(1)
@ -94,8 +94,24 @@ try:
except IndexError as e:
exit(1)
except Exception as e:
assert type(e) is KeyError
assert str(e) == '2'
assert repr(e).startswith('KeyError(')
except:
exit(1)
class MyException(Exception):
pass
class MyException2(MyException):
pass
try:
raise MyException2
except MyException as e:
ok = True
except Exception:
exit(1)
assert ok

View File

@ -153,6 +153,7 @@ try:
except TypeError:
pass
class ArithmeticError(Exception): pass
class BadCompare:
def __eq__(self, other):