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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ def __next__(self):

self.assertRaises(ValueError, {}.update, [(1, 2, 3)])

# TODO: RUSTPYTHON
@unittest.expectedFailure
Comment thread
dieterplex marked this conversation as resolved.
def test_fromkeys(self):
self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
d = {}
Expand Down
27 changes: 18 additions & 9 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
set::PySetInner, IterStatus, PositionIterInternal, PyBaseExceptionRef, PyGenericAlias, PySet,
PyStrRef, PyTupleRef, PyTypeRef,
PyStrRef, PyTupleRef, PyType, PyTypeRef,
};
use crate::{
builtins::{iter::builtins_iter, PyTuple},
Expand All @@ -9,8 +9,8 @@ use crate::{
function::{ArgIterable, FuncArgs, IntoPyObject, KwArgs, OptionalArg},
protocol::{PyIterIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
types::{
AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable,
Iterable, PyComparisonOp, Unconstructible, Unhashable,
AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
vm::{ReprGuard, VirtualMachine},
IdProtocol,
Expand Down Expand Up @@ -150,14 +150,23 @@ impl PyDict {
iterable: ArgIterable,
value: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
let dict = DictContentType::default();
) -> PyResult {
let value = value.unwrap_or_none(vm);
for elem in iterable.iter(vm)? {
let elem = elem?;
dict.insert(vm, elem, value.clone())?;
let d = PyType::call(&class, ().into(), vm)?;
match d.downcast_exact::<PyDict>(vm) {
Ok(pydict) => {
for key in iterable.iter(vm)? {
pydict.setitem(key?, value.clone(), vm)?;
}
Ok(pydict.into_pyobject(vm))
}
Err(pyobj) => {
for key in iterable.iter(vm)? {
pyobj.set_item(key?, value.clone(), vm)?;
}
Ok(pyobj)
}
}
PyDict { entries: dict }.into_ref_with_type(vm, class)
}

#[pymethod(magic)]
Expand Down