Skip to content

Commit a1d9c64

Browse files
authored
Implemented __reduce__ method for types.SimpleNamespace (RustPython#4975)
1 parent 1aeaf4a commit a1d9c64

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Lib/test/test_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,8 +1793,6 @@ class Spam(types.SimpleNamespace):
17931793
self.assertIs(type(spam), Spam)
17941794
self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})
17951795

1796-
# TODO: RUSTPYTHON
1797-
@unittest.expectedFailure
17981796
def test_pickle(self):
17991797
ns = types.SimpleNamespace(breakfast="spam", lunch="spam")
18001798

vm/src/builtins/namespace.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use super::{PyType, PyTypeRef};
1+
use super::{tuple::IntoPyTuple, PyTupleRef, PyType, PyTypeRef};
22
use crate::{
33
builtins::PyDict,
44
class::PyClassImpl,
55
function::{FuncArgs, PyComparisonValue},
66
recursion::ReprGuard,
77
types::{Comparable, Constructor, Initializer, PyComparisonOp, Representable},
8-
AsObject, Context, Py, PyObject, PyPayload, PyRef, PyResult, VirtualMachine,
8+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
1010

1111
/// A simple attribute-based namespace.
1212
///
1313
/// SimpleNamespace(**kwargs)
14-
#[pyclass(module = false, name = "SimpleNamespace")]
14+
#[pyclass(module = "types", name = "SimpleNamespace")]
1515
#[derive(Debug)]
1616
pub struct PyNamespace {}
1717

@@ -43,7 +43,19 @@ impl PyNamespace {
4343
flags(BASETYPE, HAS_DICT),
4444
with(Constructor, Initializer, Comparable, Representable)
4545
)]
46-
impl PyNamespace {}
46+
impl PyNamespace {
47+
#[pymethod(magic)]
48+
fn reduce(zelf: PyObjectRef, vm: &VirtualMachine) -> PyTupleRef {
49+
let dict = zelf.as_object().dict().unwrap();
50+
let obj = zelf.as_object().to_owned();
51+
let result: (PyObjectRef, PyObjectRef, PyObjectRef) = (
52+
obj.class().to_owned().into(),
53+
vm.new_tuple(()).into(),
54+
dict.into(),
55+
);
56+
result.into_pytuple(vm)
57+
}
58+
}
4759

4860
impl Initializer for PyNamespace {
4961
type Args = FuncArgs;

0 commit comments

Comments
 (0)