Skip to content

Commit 802acf7

Browse files
committed
Add sys.exc_info
1 parent a0abe08 commit 802acf7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

tests/snippets/sysmod.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ def demo(x):
3838
demo(5)
3939
sys.settrace(None)
4040

41+
assert sys.exc_info() == (None, None, None)
42+
43+
try:
44+
1/0
45+
except ZeroDivisionError as exc:
46+
exc_info = sys.exc_info()
47+
assert exc_info[0] == type(exc) == ZeroDivisionError
48+
assert exc_info[1] == exc

vm/src/sysmodule.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::{env, mem};
44
use crate::frame::FrameRef;
55
use crate::function::{OptionalArg, PyFuncArgs};
66
use crate::obj::objstr::PyStringRef;
7-
use crate::pyobject::{IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult};
7+
use crate::pyobject::{
8+
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, TypeProtocol,
9+
};
810
use crate::version;
911
use crate::vm::{PySettings, VirtualMachine};
1012

@@ -149,6 +151,17 @@ fn sys_intern(value: PyStringRef, _vm: &VirtualMachine) -> PyStringRef {
149151
value
150152
}
151153

154+
fn sys_exc_info(vm: &VirtualMachine) -> PyResult {
155+
Ok(vm.ctx.new_tuple(match vm.current_exception() {
156+
Some(exception) => vec![
157+
exception.class().into_object(),
158+
exception.clone(),
159+
vm.get_none(),
160+
],
161+
None => vec![vm.get_none(), vm.get_none(), vm.get_none()],
162+
}))
163+
}
164+
152165
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectRef) {
153166
let ctx = &vm.ctx;
154167

@@ -309,6 +322,7 @@ settrace() -- set the global debug tracing function
309322
"setprofile" => ctx.new_rustfunc(sys_setprofile),
310323
"settrace" => ctx.new_rustfunc(sys_settrace),
311324
"version" => vm.new_str(version::get_version()),
325+
"exc_info" => ctx.new_rustfunc(sys_exc_info),
312326
});
313327

314328
modules.set_item("sys", module.clone(), vm).unwrap();

0 commit comments

Comments
 (0)