FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implemented __reduce__ method for types.SimpleNamespace by LucaSforza · Pull Request #4975 · RustPython/RustPython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 0 additions & 2 deletions Lib/test/test_types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,6 @@ class Spam(types.SimpleNamespace):
self.assertIs(type(spam), Spam)
self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickle(self):
ns = types.SimpleNamespace(breakfast="spam", lunch="spam")

Expand Down
20 changes: 16 additions & 4 deletions vm/src/builtins/namespace.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::{PyType, PyTypeRef};
use super::{tuple::IntoPyTuple, PyTupleRef, PyType, PyTypeRef};
use crate::{
builtins::PyDict,
class::PyClassImpl,
function::{FuncArgs, PyComparisonValue},
recursion::ReprGuard,
types::{Comparable, Constructor, Initializer, PyComparisonOp, Representable},
AsObject, Context, Py, PyObject, PyPayload, PyRef, PyResult, VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

/// A simple attribute-based namespace.
///
/// SimpleNamespace(**kwargs)
#[pyclass(module = false, name = "SimpleNamespace")]
#[pyclass(module = "types", name = "SimpleNamespace")]
#[derive(Debug)]
pub struct PyNamespace {}

Expand Down Expand Up @@ -43,7 +43,19 @@ impl PyNamespace {
flags(BASETYPE, HAS_DICT),
with(Constructor, Initializer, Comparable, Representable)
)]
impl PyNamespace {}
impl PyNamespace {
#[pymethod(magic)]
fn reduce(zelf: PyObjectRef, vm: &VirtualMachine) -> PyTupleRef {
let dict = zelf.as_object().dict().unwrap();
let obj = zelf.as_object().to_owned();
let result: (PyObjectRef, PyObjectRef, PyObjectRef) = (
obj.class().to_owned().into(),
vm.new_tuple(()).into(),
dict.into(),
);
result.into_pytuple(vm)
}
}

impl Initializer for PyNamespace {
type Args = FuncArgs;
Expand Down

Back | FazBrowse Home | New Git URL