mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Improve defaultdict some more
This commit is contained in:
4 files changed
+38
-8
No files matched your search
@@ -1,7 +1,11 @@
|
||||
from reprlib import recursive_repr as _recursive_repr
|
||||
|
||||
class defaultdict(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if len(args) >= 1:
|
||||
default_factory = args[0]
|
||||
if default_factory is not None and not callable(default_factory):
|
||||
raise TypeError("first argument must be callable or None")
|
||||
args = args[1:]
|
||||
else:
|
||||
default_factory = None
|
||||
@@ -9,13 +13,30 @@ class defaultdict(dict):
|
||||
self.default_factory = default_factory
|
||||
|
||||
def __missing__(self, key):
|
||||
if self.default_factory:
|
||||
if self.default_factory is not None:
|
||||
val = self.default_factory()
|
||||
else:
|
||||
raise KeyError(key)
|
||||
self[key] = val
|
||||
return val
|
||||
|
||||
def __repr__(self):
|
||||
return f"defaultdict({self.default_factory}, {dict.__repr__(self)})"
|
||||
@_recursive_repr()
|
||||
def __repr_factory(factory):
|
||||
return repr(factory)
|
||||
|
||||
def __repr__(self):
|
||||
return f"{type(self).__name__}({defaultdict.__repr_factory(self.default_factory)}, {dict.__repr__(self)})"
|
||||
|
||||
def copy(self):
|
||||
return type(self)(self.default_factory, self)
|
||||
|
||||
__copy__ = copy
|
||||
|
||||
def __reduce__(self):
|
||||
if self.default_factory is not None:
|
||||
args = self.default_factory,
|
||||
else:
|
||||
args = ()
|
||||
return type(self), args, None, None, iter(self.items())
|
||||
|
||||
defaultdict.__module__ = 'collections'
|
||||
@@ -714,8 +714,6 @@ class TestCopy(unittest.TestCase):
|
||||
self.assertEqual(x.foo, y.foo)
|
||||
self.assertIsNot(x.foo, y.foo)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_deepcopy_dict_subclass(self):
|
||||
class C(dict):
|
||||
def __init__(self, d=None):
|
||||
|
||||
@@ -63,7 +63,7 @@ def reduce_2(obj):
|
||||
state = getstate()
|
||||
|
||||
listitems = iter(obj) if isinstance(obj, list) else None
|
||||
dictitems = obj.iteritems() if isinstance(obj, dict) else None
|
||||
dictitems = iter(obj.items()) if isinstance(obj, dict) else None
|
||||
|
||||
newobj = copyreg.__newobj__
|
||||
|
||||
|
||||
@@ -449,9 +449,20 @@ impl PyBoundMethod {
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let funcname = if let Some(qname) =
|
||||
vm.get_attribute_opt(self.function.clone(), "__qualname__")?
|
||||
{
|
||||
Some(qname)
|
||||
} else if let Some(name) = vm.get_attribute_opt(self.function.clone(), "__qualname__")? {
|
||||
Some(name)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let funcname: Option<PyStrRef> = funcname.and_then(|o| o.downcast().ok());
|
||||
Ok(format!(
|
||||
"<bound method of {}>",
|
||||
vm.to_repr(&self.object)?.borrow_value()
|
||||
"<bound method {} of {}>",
|
||||
funcname.as_ref().map_or("?", |s| s.borrow_value()),
|
||||
vm.to_repr(&self.object)?.borrow_value(),
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user