Skip to content

Commit 437a819

Browse files
authored
Merge pull request RustPython#1970 from youknowone/sub-1962
non-subprocess stuff from RustPython#1962
2 parents ca7d9b6 + ec1c9f9 commit 437a819

File tree

5 files changed

+356
-17
lines changed

5 files changed

+356
-17
lines changed

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ schannel = "0.1"
122122

123123
[target.'cfg(windows)'.dependencies.winapi]
124124
version = "0.3"
125-
features = ["winsock2", "handleapi", "ws2def", "std", "winbase", "wincrypt", "fileapi"]
125+
features = ["winsock2", "handleapi", "ws2def", "std", "winbase", "wincrypt", "fileapi", "wincon"]
126126

127127
[target.'cfg(target_arch = "wasm32")'.dependencies]
128128
wasm-bindgen = "0.2"

vm/src/function.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ impl<T> From<HashMap<String, T>> for KwArgs<T> {
274274
KwArgs(map)
275275
}
276276
}
277+
impl<T> Default for KwArgs<T> {
278+
fn default() -> Self {
279+
KwArgs(HashMap::new())
280+
}
281+
}
277282

278283
impl<T> FromArgs for KwArgs<T>
279284
where

vm/src/obj/objdict.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::exceptions::PyBaseExceptionRef;
1010
use crate::function::{KwArgs, OptionalArg, PyFuncArgs};
1111
use crate::pyobject::{
1212
IdProtocol, IntoPyObject, ItemProtocol, PyAttributes, PyClassImpl, PyContext, PyIterable,
13-
PyObjectRef, PyRef, PyResult, PyValue,
13+
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
1414
};
1515
use crate::vm::{ReprGuard, VirtualMachine};
1616

@@ -39,6 +39,7 @@ impl PyValue for PyDict {
3939
}
4040

4141
// Python dict methods:
42+
#[allow(clippy::len_without_is_empty)]
4243
#[pyimpl(flags(BASETYPE))]
4344
impl PyDictRef {
4445
#[pyslot]
@@ -179,7 +180,7 @@ impl PyDictRef {
179180
}
180181

181182
#[pymethod(magic)]
182-
fn len(self) -> usize {
183+
pub fn len(self) -> usize {
183184
self.entries.len()
184185
}
185186

@@ -531,6 +532,11 @@ impl Iterator for DictIter {
531532
None => None,
532533
}
533534
}
535+
536+
fn size_hint(&self) -> (usize, Option<usize>) {
537+
let l = self.dict.entries.len_from_entry_index(self.position);
538+
(l, Some(l))
539+
}
534540
}
535541

536542
macro_rules! dict_iterator {
@@ -675,3 +681,26 @@ pub(crate) fn init(context: &PyContext) {
675681
PyDictItems::extend_class(context, &context.types.dictitems_type);
676682
PyDictItemIterator::extend_class(context, &context.types.dictitemiterator_type);
677683
}
684+
685+
pub struct PyMapping {
686+
dict: PyDictRef,
687+
}
688+
689+
impl TryFromObject for PyMapping {
690+
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
691+
let dict = vm.ctx.new_dict();
692+
PyDictRef::merge(
693+
&dict.entries,
694+
OptionalArg::Present(obj),
695+
KwArgs::default(),
696+
vm,
697+
)?;
698+
Ok(PyMapping { dict })
699+
}
700+
}
701+
702+
impl PyMapping {
703+
pub fn into_dict(self) -> PyDictRef {
704+
self.dict
705+
}
706+
}

vm/src/pyobject.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,12 @@ impl<T> PyIterable<T> {
980980
},
981981
)?;
982982

983+
let lenhint = objiter::length_hint(vm, iter_obj.clone())?;
984+
983985
Ok(PyIterator {
984986
vm,
985987
obj: iter_obj,
988+
lenhint,
986989
_item: std::marker::PhantomData,
987990
})
988991
}
@@ -991,6 +994,7 @@ impl<T> PyIterable<T> {
991994
pub struct PyIterator<'a, T> {
992995
vm: &'a VirtualMachine,
993996
obj: PyObjectRef,
997+
lenhint: Option<usize>,
994998
_item: std::marker::PhantomData<T>,
995999
}
9961000

@@ -1012,6 +1016,10 @@ where
10121016
}
10131017
}
10141018
}
1019+
1020+
fn size_hint(&self) -> (usize, Option<usize>) {
1021+
(self.lenhint.unwrap_or(0), self.lenhint)
1022+
}
10151023
}
10161024

10171025
impl<T> TryFromObject for PyIterable<T>

0 commit comments

Comments
 (0)