Skip to content

Commit df5bca4

Browse files
committed
Add OSError.__reduce__
1 parent 5f04da0 commit df5bca4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

extra_tests/snippets/builtin_exceptions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ class SubError(MyError):
273273
assert w.filename == None
274274
assert w.filename2 == None
275275
assert str(w) == ""
276+
x = pickle.loads(pickle.dumps(w, 4))
277+
assert type(w) == type(x)
278+
assert w == x
276279

277280
w = OSError(0)
278281
assert w.errno == None
@@ -281,6 +284,9 @@ class SubError(MyError):
281284
assert w.filename == None
282285
assert w.filename2 == None
283286
assert str(w) == "0"
287+
x = pickle.loads(pickle.dumps(w, 4))
288+
assert type(w) == type(x)
289+
assert w == x
284290

285291
w = OSError('foo')
286292
assert w.errno == None
@@ -289,6 +295,9 @@ class SubError(MyError):
289295
assert w.filename == None
290296
assert w.filename2 == None
291297
assert str(w) == "foo"
298+
x = pickle.loads(pickle.dumps(w, 4))
299+
assert type(w) == type(x)
300+
assert w == x
292301

293302
w = OSError('a', 'b', 'c', 'd', 'e', 'f')
294303
assert w.errno == None
@@ -297,6 +306,9 @@ class SubError(MyError):
297306
assert w.filename == None
298307
assert w.filename2 == None
299308
assert str(w) == "('a', 'b', 'c', 'd', 'e', 'f')"
309+
x = pickle.loads(pickle.dumps(w, 4))
310+
assert type(w) == type(x)
311+
assert w == x
300312

301313
# Custom `__new__` and `__init__`:
302314
assert ImportError.__init__.__qualname__ == 'ImportError.__init__'

vm/src/exceptions.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use self::types::{PyBaseException, PyBaseExceptionRef};
2+
use crate::builtins::tuple::IntoPyTuple;
23
use crate::common::lock::PyRwLock;
34
use crate::{
45
builtins::{
@@ -773,6 +774,7 @@ impl ExceptionZoo {
773774
// second exception filename
774775
"filename2" => ctx.none(),
775776
"__str__" => ctx.new_method("__str__", excs.os_error, os_error_str),
777+
"__reduce__" => ctx.new_method("__reduce__", excs.os_error, os_error_reduce),
776778
});
777779
// TODO: this isn't really accurate
778780
#[cfg(windows)]
@@ -907,6 +909,42 @@ fn os_error_str(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyResult<PyStrR
907909
}
908910
}
909911

912+
fn os_error_reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {
913+
let args = exc.args();
914+
let obj = exc.as_object().to_owned();
915+
let mut result: Vec<PyObjectRef> = vec![obj.class().clone().into()];
916+
917+
if args.len() >= 2 && args.len() <= 5 {
918+
// SAFETY: len() == 2 is checked so get_arg 1 or 2 won't panic
919+
let errno = exc.get_arg(0).unwrap();
920+
let msg = exc.get_arg(1).unwrap();
921+
922+
if let Ok(filename) = obj.get_attr("filename", vm) {
923+
if !vm.is_none(&filename) {
924+
let mut args_reduced: Vec<PyObjectRef> = vec![errno, msg, filename];
925+
926+
if let Ok(filename2) = obj.get_attr("filename2", vm) {
927+
if !vm.is_none(&filename2) {
928+
args_reduced.push(filename2);
929+
}
930+
}
931+
result.push(args_reduced.into_pytuple(vm).into());
932+
} else {
933+
result.push(vm.new_tuple((errno, msg)).into());
934+
}
935+
} else {
936+
result.push(vm.new_tuple((errno, msg)).into());
937+
}
938+
} else {
939+
result.push(args.into());
940+
}
941+
942+
if let Some(dict) = obj.dict().filter(|x| !x.is_empty()) {
943+
result.push(dict.into());
944+
}
945+
result.into_pytuple(vm)
946+
}
947+
910948
fn system_exit_code(exc: PyBaseExceptionRef) -> Option<PyObjectRef> {
911949
exc.args.read().first().map(|code| {
912950
match_class!(match code {

0 commit comments

Comments
 (0)