From e91ac80bb413d740ba4e1699581857647fa7a7d2 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 13:51:35 +0300 Subject: [PATCH 01/11] add reduce for chain --- vm/src/stdlib/itertools.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index e4b47afae5..94402fb81f 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -60,6 +60,23 @@ mod decl { fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias { PyGenericAlias::new(cls, args, vm) } + + #[pymethod(magic)] + fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + let source = zelf.source.read().clone(); + let active = zelf.active.read().clone(); + match source { + Some(source) => { + match active { + Some(active) => { + Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone(), (source, active)))) + }, + None => Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone(), (source, )))) + } + }, + None => Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone()))) + } + } } impl IterNextIterable for PyItertoolsChain {} impl IterNext for PyItertoolsChain { From 05ce6f88e0f0eaaf747d51cbb112a640966fe2ba Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 14:13:16 +0300 Subject: [PATCH 02/11] fix formatting issues --- vm/src/stdlib/itertools.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 12327328a2..aafa97bd7f 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -18,6 +18,7 @@ mod decl { VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; + use num_bigint::BigInt; use num_traits::{Signed, ToPrimitive}; use std::fmt; @@ -42,7 +43,7 @@ mod decl { .map(Into::into) } - #[pyclassmethod] + #[pyclassmethod] fn from_iterable( cls: PyTypeRef, source: PyObjectRef, @@ -64,16 +65,17 @@ mod decl { fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { let source = zelf.source.read().clone(); let active = zelf.active.read().clone(); + let cls = zelf.class().clone(); match source { Some(source) => { match active { Some(active) => { - Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone(), (source, active)))) + Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) }, - None => Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone(), (source, )))) + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, )))) } }, - None => Ok(vm.new_tuple((zelf.class().clone(), vm.ctx.empty_tuple.clone()))) + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))) } } } @@ -163,7 +165,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyIter, PyIter)) { ( - zelf.class().to_owned(), + zelf.class().clone(), (zelf.data.clone(), zelf.selectors.clone()), ) } @@ -234,7 +236,7 @@ mod decl { // return Py_BuildValue("0(00)", Py_TYPE(lz), lz->long_cnt, lz->long_step); #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef,)) { - (zelf.class().to_owned(), (zelf.cur.read().clone(),)) + (zelf.class().clone(), (zelf.cur.read().clone(),)) } #[pymethod(magic)] @@ -359,7 +361,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - let cls = zelf.class().to_owned(); + let cls = zelf.class().clone(); Ok(match zelf.times { Some(ref times) => vm.new_tuple((cls, (zelf.object.clone(), *times.read()))), None => vm.new_tuple((cls, (zelf.object.clone(),))), @@ -426,7 +428,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { ( - zelf.class().to_owned(), + zelf.class().clone(), (zelf.function.clone(), zelf.iterable.clone()), ) } @@ -487,11 +489,11 @@ mod decl { #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] impl PyItertoolsTakewhile { #[pymethod(magic)] - fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { + fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { ( - zelf.class().to_owned(), + zelf.class().clone(), (zelf.predicate.clone(), zelf.iterable.clone()), - zelf.stop_flag.load() as _, + (if zelf.stop_flag.load() { 1 } else { 0 }).into(), ) } #[pymethod(magic)] @@ -568,11 +570,11 @@ mod decl { #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] impl PyItertoolsDropwhile { #[pymethod(magic)] - fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { + fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { ( - zelf.class().to_owned(), + zelf.class().clone(), (zelf.predicate.clone().into(), zelf.iterable.clone()), - (zelf.start_flag.load() as _), + (if zelf.start_flag.load() { 1 } else { 0 }).into(), ) } #[pymethod(magic)] @@ -965,7 +967,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { ( - zelf.class().to_owned(), + zelf.class().clone(), (zelf.predicate.clone(), zelf.iterable.clone()), ) } @@ -1453,7 +1455,7 @@ mod decl { // Increment the current index which we know is not at its // maximum. Then set all to the right to the same value. for j in idx as usize..r { - indices[j] = index; + indices[j as usize] = index as usize; } } From 7aff198bb32fe660bb439fe5b987f5f750358f5d Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 14:29:07 +0300 Subject: [PATCH 03/11] change some clone() calls to to_owned() to fix mismatched type errors --- vm/src/stdlib/itertools.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index aafa97bd7f..a6fb9aa444 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -65,7 +65,7 @@ mod decl { fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { let source = zelf.source.read().clone(); let active = zelf.active.read().clone(); - let cls = zelf.class().clone(); + let cls = zelf.class().to_owned(); match source { Some(source) => { match active { @@ -165,8 +165,8 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyIter, PyIter)) { ( - zelf.class().clone(), - (zelf.data.clone(), zelf.selectors.clone()), + zelf.class().to_owned(), + (zelf.data.to_owned(), zelf.selectors.clone()), ) } } @@ -179,7 +179,7 @@ mod decl { PyIterReturn::Return(obj) => obj, PyIterReturn::StopIteration(v) => return Ok(PyIterReturn::StopIteration(v)), }; - let verdict = sel_obj.clone().try_to_bool(vm)?; + let verdict = sel_obj.to_owned().try_to_bool(vm)?; let data_obj = zelf.data.next(vm)?; if verdict { @@ -236,7 +236,7 @@ mod decl { // return Py_BuildValue("0(00)", Py_TYPE(lz), lz->long_cnt, lz->long_step); #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef,)) { - (zelf.class().clone(), (zelf.cur.read().clone(),)) + (zelf.class().to_owned(), (zelf.cur.read().clone(),)) } #[pymethod(magic)] @@ -361,7 +361,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - let cls = zelf.class().clone(); + let cls = zelf.class().to_owned(); Ok(match zelf.times { Some(ref times) => vm.new_tuple((cls, (zelf.object.clone(), *times.read()))), None => vm.new_tuple((cls, (zelf.object.clone(),))), @@ -428,7 +428,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { ( - zelf.class().clone(), + zelf.class().to_owned(), (zelf.function.clone(), zelf.iterable.clone()), ) } @@ -491,7 +491,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { ( - zelf.class().clone(), + zelf.class().to_owned(), (zelf.predicate.clone(), zelf.iterable.clone()), (if zelf.stop_flag.load() { 1 } else { 0 }).into(), ) @@ -572,7 +572,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { ( - zelf.class().clone(), + zelf.class().to_owned(), (zelf.predicate.clone().into(), zelf.iterable.clone()), (if zelf.start_flag.load() { 1 } else { 0 }).into(), ) @@ -967,7 +967,7 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter)) { ( - zelf.class().clone(), + zelf.class().to_owned(), (zelf.predicate.clone(), zelf.iterable.clone()), ) } From bda0f4e4b82c1bb709bf5b0fb7f2a2dbc33c4c2d Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 14:30:34 +0300 Subject: [PATCH 04/11] fix formatting issues --- vm/src/stdlib/itertools.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index a6fb9aa444..1e76b6ab34 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -43,7 +43,7 @@ mod decl { .map(Into::into) } - #[pyclassmethod] + #[pyclassmethod] fn from_iterable( cls: PyTypeRef, source: PyObjectRef, @@ -63,19 +63,17 @@ mod decl { #[pymethod(magic)] fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - let source = zelf.source.read().clone(); + let source = zelf.source.read().clone(); let active = zelf.active.read().clone(); let cls = zelf.class().to_owned(); match source { - Some(source) => { - match active { - Some(active) => { - Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) - }, - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, )))) + Some(source) => match active { + Some(active) => { + Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) } + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), }, - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))) + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), } } } From 1e4e266822d12a7748d76dea7c9984415ea75322 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 16:24:26 +0300 Subject: [PATCH 05/11] add HAS_DICT flag to chain --- vm/src/stdlib/itertools.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 1e76b6ab34..7b154ff0ad 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -30,7 +30,7 @@ mod decl { active: PyRwLock>, } - #[pyclass(with(IterNext), flags(BASETYPE))] + #[pyclass(with(IterNext), flags(BASETYPE, HAS_DICT))] impl PyItertoolsChain { #[pyslot] fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { @@ -69,7 +69,23 @@ mod decl { match source { Some(source) => match active { Some(active) => { - Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) + Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), ((source, active))))) + } + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), + }, + None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), + } + } + + #[pymethod(magic)] + fn reduce_ex(zelf: PyRef, _proto: usize, vm: &VirtualMachine) -> PyResult { + let source = zelf.source.read().clone(); + let active = zelf.active.read().clone(); + let cls = zelf.class().to_owned(); + match source { + Some(source) => match active { + Some(active) => { + Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), ((source, active))))) } None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), }, From c3986354cc145e22ed9c3be582021c0a81e125a0 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 16:28:09 +0300 Subject: [PATCH 06/11] fix formatting issues --- vm/src/stdlib/itertools.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 7b154ff0ad..6f1fe828e5 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -78,7 +78,11 @@ mod decl { } #[pymethod(magic)] - fn reduce_ex(zelf: PyRef, _proto: usize, vm: &VirtualMachine) -> PyResult { + fn reduce_ex( + zelf: PyRef, + _proto: usize, + vm: &VirtualMachine, + ) -> PyResult { let source = zelf.source.read().clone(); let active = zelf.active.read().clone(); let cls = zelf.class().to_owned(); From 9609b2ea4e37b99148bd32470ddeb4d7f44712bc Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 16:34:14 +0300 Subject: [PATCH 07/11] fix clippy issues --- vm/src/stdlib/itertools.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 6f1fe828e5..1d11c94a8c 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -69,7 +69,7 @@ mod decl { match source { Some(source) => match active { Some(active) => { - Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), ((source, active))))) + Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) } None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), }, From 33de7b7bcfe30e33c71fa92c1bbd16dd06cc64bd Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Tue, 18 Oct 2022 19:23:49 +0300 Subject: [PATCH 08/11] remove reduce_ex for now --- vm/src/stdlib/itertools.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 1d11c94a8c..1d65fc920e 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -76,26 +76,6 @@ mod decl { None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), } } - - #[pymethod(magic)] - fn reduce_ex( - zelf: PyRef, - _proto: usize, - vm: &VirtualMachine, - ) -> PyResult { - let source = zelf.source.read().clone(); - let active = zelf.active.read().clone(); - let cls = zelf.class().to_owned(); - match source { - Some(source) => match active { - Some(active) => { - Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), ((source, active))))) - } - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), - }, - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), - } - } } impl IterNextIterable for PyItertoolsChain {} impl IterNext for PyItertoolsChain { From 1f38cd3f0b9e499c44374793678791797312cb4c Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Wed, 19 Oct 2022 16:27:45 +0300 Subject: [PATCH 09/11] add setstate for chain --- Lib/test/test_itertools.py | 5 +---- vm/src/stdlib/itertools.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 53cc26355b..f40cee143f 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -181,8 +181,6 @@ def test_chain_from_iterable(self): self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd')) self.assertRaises(TypeError, list, chain.from_iterable([2, 3])) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_chain_reducible(self): for oper in [copy.deepcopy] + picklecopiers: it = chain('abc', 'def') @@ -195,8 +193,7 @@ def test_chain_reducible(self): self.assertRaises(TypeError, list, oper(chain(2, 3))) for proto in range(pickle.HIGHEST_PROTOCOL + 1): self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef')) - # TODO: RUSTPYTHON - @unittest.expectedFailure + def test_chain_setstate(self): self.assertRaises(TypeError, chain().__setstate__, ()) self.assertRaises(TypeError, chain().__setstate__, []) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 1d65fc920e..c007dc6dbc 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -76,6 +76,35 @@ mod decl { None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), } } + + #[pymethod(magic)] + fn setstate(zelf: PyRef, state: PyTupleRef, vm: &VirtualMachine) -> PyResult<()> { + let args = state.as_slice(); + if args.is_empty() { + let msg = String::from("function takes at leat 1 arguments (0 given)"); + return Err(vm.new_type_error(msg)); + } + if args.len() > 2 { + let msg = format!("function takes at most 2 arguments ({} given)", args.len()); + return Err(vm.new_type_error(msg)); + } + let source = &args[0]; + if args.len() == 1 { + if !PyIter::check(source.as_ref()) { + return Err(vm.new_type_error(String::from("Arguments must be iterators."))); + } + *zelf.source.write() = source.to_owned().try_into_value(vm)?; + return Ok(()); + } + let active = &args[1]; + + if !PyIter::check(source.as_ref()) || !PyIter::check(active.as_ref()) { + return Err(vm.new_type_error(String::from("Arguments must be iterators."))); + } + *zelf.source.write() = source.to_owned().try_into_value(vm)?; + *zelf.active.write() = active.to_owned().try_into_value(vm)?; + Ok(()) + } } impl IterNextIterable for PyItertoolsChain {} impl IterNext for PyItertoolsChain { From d0b56543a3c4ac760bb3aa7db8811a53369cb6b1 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Thu, 20 Oct 2022 13:25:05 +0300 Subject: [PATCH 10/11] switch back the to_owned() calls and remove flag changes (see commit 7aff198) --- vm/src/stdlib/itertools.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 98eca875a4..f078648412 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -18,7 +18,6 @@ mod decl { VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; - use num_bigint::BigInt; use num_traits::{Signed, ToPrimitive}; use std::fmt; @@ -193,7 +192,7 @@ mod decl { fn reduce(zelf: PyRef) -> (PyTypeRef, (PyIter, PyIter)) { ( zelf.class().to_owned(), - (zelf.data.to_owned(), zelf.selectors.clone()), + (zelf.data.clone(), zelf.selectors.clone()), ) } } @@ -206,7 +205,7 @@ mod decl { PyIterReturn::Return(obj) => obj, PyIterReturn::StopIteration(v) => return Ok(PyIterReturn::StopIteration(v)), }; - let verdict = sel_obj.to_owned().try_to_bool(vm)?; + let verdict = sel_obj.clone().try_to_bool(vm)?; let data_obj = zelf.data.next(vm)?; if verdict { @@ -516,11 +515,11 @@ mod decl { #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] impl PyItertoolsTakewhile { #[pymethod(magic)] - fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { + fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { ( zelf.class().to_owned(), (zelf.predicate.clone(), zelf.iterable.clone()), - (if zelf.stop_flag.load() { 1 } else { 0 }).into(), + zelf.stop_flag.load() as _, ) } #[pymethod(magic)] @@ -597,11 +596,11 @@ mod decl { #[pyclass(with(IterNext, Constructor), flags(BASETYPE))] impl PyItertoolsDropwhile { #[pymethod(magic)] - fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) { + fn reduce(zelf: PyRef) -> (PyTypeRef, (PyObjectRef, PyIter), u32) { ( zelf.class().to_owned(), (zelf.predicate.clone().into(), zelf.iterable.clone()), - (if zelf.start_flag.load() { 1 } else { 0 }).into(), + (zelf.start_flag.load() as _), ) } #[pymethod(magic)] @@ -1482,7 +1481,7 @@ mod decl { // Increment the current index which we know is not at its // maximum. Then set all to the right to the same value. for j in idx as usize..r { - indices[j as usize] = index as usize; + indices[j] = index; } } From e32d4de484754b2cc9a90ab23d6e76393135f845 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Fri, 21 Oct 2022 08:14:25 +0300 Subject: [PATCH 11/11] fix styling and lock issues --- vm/src/stdlib/itertools.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index f078648412..09a3f26344 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -65,15 +65,15 @@ mod decl { let source = zelf.source.read().clone(); let active = zelf.active.read().clone(); let cls = zelf.class().to_owned(); - match source { + let empty_tuple = vm.ctx.empty_tuple.clone(); + let reduced = match source { Some(source) => match active { - Some(active) => { - Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active)))) - } - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))), + Some(active) => vm.new_tuple((cls, empty_tuple, (source, active))), + None => vm.new_tuple((cls, empty_tuple, (source,))), }, - None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))), - } + None => vm.new_tuple((cls, empty_tuple)), + }; + Ok(reduced) } #[pymethod(magic)] @@ -100,8 +100,10 @@ mod decl { if !PyIter::check(source.as_ref()) || !PyIter::check(active.as_ref()) { return Err(vm.new_type_error(String::from("Arguments must be iterators."))); } - *zelf.source.write() = source.to_owned().try_into_value(vm)?; - *zelf.active.write() = active.to_owned().try_into_value(vm)?; + let mut source_lock = zelf.source.write(); + let mut active_lock = zelf.active.write(); + *source_lock = source.to_owned().try_into_value(vm)?; + *active_lock = active.to_owned().try_into_value(vm)?; Ok(()) } }