From 0f24b183121e95e38bf7cfa677607c4dfd2ba610 Mon Sep 17 00:00:00 2001 From: hyezzang Date: Mon, 11 Jul 2022 19:19:18 +0900 Subject: [PATCH 1/8] Add arrayiter __reduce__ Co-authored-by: Yaminyam --- stdlib/src/array.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index b7fd8502b39..7a99cf82791 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -6,15 +6,15 @@ mod array { common::{ atomic::{self, AtomicUsize}, lock::{ - PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard, - PyRwLockWriteGuard, + PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock, + PyRwLockReadGuard, PyRwLockWriteGuard, }, str::wchar_t, }, vm::{ builtins::{ - PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt, PyIntRef, PyList, - PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef, + PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt, + PyIntRef, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef, }, class_or_notimplemented, convert::{ToPyObject, ToPyResult, TryFromObject}, @@ -1260,7 +1260,8 @@ mod array { fn iter(zelf: PyRef, vm: &VirtualMachine) -> PyResult { Ok(PyArrayIterator { position: AtomicUsize::new(0), - array: zelf, + array: zelf.clone(), + internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)), } .into_pyobject(vm)) } @@ -1281,10 +1282,19 @@ mod array { pub struct PyArrayIterator { position: AtomicUsize, array: PyArrayRef, + internal: PyMutex>, } #[pyimpl(with(IterNext))] - impl PyArrayIterator {} + + impl PyArrayIterator { + #[pymethod(magic)] + fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { + self.internal + .lock() + .builtins_iter_reduce(|x| x.clone().into(), vm) + } + } impl IterNextIterable for PyArrayIterator {} impl IterNext for PyArrayIterator { From ab284c87fcc45966541940ffa3c38feba24c4d5c Mon Sep 17 00:00:00 2001 From: hyezzang Date: Mon, 11 Jul 2022 19:55:56 +0900 Subject: [PATCH 2/8] update test_iterator_pickle Co-authored-by: Yaminyam --- Lib/test/test_array.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index f7405098b1c..b68d85fbd40 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -284,8 +284,6 @@ def test_pickle_for_empty_array(self): self.assertEqual(a.x, b.x) self.assertEqual(type(a), type(b)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_iterator_pickle(self): orig = array.array(self.typecode, self.example) data = list(orig) From 821f3e7aabe2fab6049be8656842645fdbaafbd2 Mon Sep 17 00:00:00 2001 From: hyezzang Date: Tue, 12 Jul 2022 21:32:52 +0900 Subject: [PATCH 3/8] fix position value to struct value --- stdlib/src/array.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 7a99cf82791..81ec41aa6d4 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1290,9 +1290,13 @@ mod array { impl PyArrayIterator { #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { - self.internal + let tuple = self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into(), vm) + .builtins_iter_reduce(|x| x.clone().into(), vm); + let func = tuple[0].clone(); + let obj = tuple[1].clone(); + let pos = self.position.load(atomic::Ordering::SeqCst); + vm.new_tuple((func, obj, pos,)) } } From 1a526aff47095940c3b0ba7fc0eb34f265793414 Mon Sep 17 00:00:00 2001 From: siontama Date: Sat, 16 Jul 2022 16:42:00 +0900 Subject: [PATCH 4/8] Fix: Modify iter next to be implemented only internal --- stdlib/src/array.rs | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 81ec41aa6d4..5c11544b330 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1258,9 +1258,7 @@ mod array { impl Iterable for PyArray { fn iter(zelf: PyRef, vm: &VirtualMachine) -> PyResult { - Ok(PyArrayIterator { - position: AtomicUsize::new(0), - array: zelf.clone(), + Ok(PyArrayIter { internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)), } .into_pyobject(vm)) @@ -1279,37 +1277,31 @@ mod array { #[pyattr] #[pyclass(name = "arrayiterator")] #[derive(Debug, PyPayload)] - pub struct PyArrayIterator { - position: AtomicUsize, - array: PyArrayRef, + pub struct PyArrayIter { internal: PyMutex>, } - #[pyimpl(with(IterNext))] - - impl PyArrayIterator { + #[pyimpl(with(IterNext), flags(HAS_DICT))] + impl PyArrayIter { #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { - let tuple = self.internal + self.internal .lock() - .builtins_iter_reduce(|x| x.clone().into(), vm); - let func = tuple[0].clone(); - let obj = tuple[1].clone(); - let pos = self.position.load(atomic::Ordering::SeqCst); - vm.new_tuple((func, obj, pos,)) + .builtins_iter_reduce(|x| x.clone().into(), vm) } } - impl IterNextIterable for PyArrayIterator {} - impl IterNext for PyArrayIterator { + impl IterNextIterable for PyArrayIter {} + impl IterNext for PyArrayIter { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { - let pos = zelf.position.fetch_add(1, atomic::Ordering::SeqCst); - let r = if let Some(item) = zelf.array.read().get(pos, vm) { - PyIterReturn::Return(item?) - } else { - PyIterReturn::StopIteration(None) - }; - Ok(r) + zelf.internal.lock().next(|array, pos| { + let r = if let Some(item) = array.read().get(pos, vm) { + PyIterReturn::Return(item?) + } else { + PyIterReturn::StopIteration(None) + }; + Ok(r) + }) } } From 50aebefa4c8d2e3fc796ae75f5efccb47c08789a Mon Sep 17 00:00:00 2001 From: Sion Kang <31057849+Yaminyam@users.noreply.github.com> Date: Sat, 16 Jul 2022 16:53:52 +0900 Subject: [PATCH 5/8] Update stdlib/src/array.rs Co-authored-by: Snowapril --- stdlib/src/array.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 5c11544b330..33aa9800fd7 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1295,12 +1295,10 @@ mod array { impl IterNext for PyArrayIter { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { zelf.internal.lock().next(|array, pos| { - let r = if let Some(item) = array.read().get(pos, vm) { - PyIterReturn::Return(item?) - } else { - PyIterReturn::StopIteration(None) - }; - Ok(r) + Ok(match array.read().get(pos, vm) { + Some(item) => PyIterReturn::Return(item?), + None => PyIterReturn::StopIteration(None), + }) }) } } From 2417d3e81363aeae37c8a51bd8594fef59c63b61 Mon Sep 17 00:00:00 2001 From: siontama Date: Sat, 16 Jul 2022 17:19:14 +0900 Subject: [PATCH 6/8] Feat: impl `PyArrayIter.setstate` --- stdlib/src/array.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 33aa9800fd7..2c31b73511d 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1283,6 +1283,13 @@ mod array { #[pyimpl(with(IterNext), flags(HAS_DICT))] impl PyArrayIter { + #[pymethod(magic)] + fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + self.internal + .lock() + .set_state(state, |obj, pos| pos.min(obj.len()), vm) + } + #[pymethod(magic)] fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { self.internal From cdd937cebbbb118f2a43a44a533e6458156b3f72 Mon Sep 17 00:00:00 2001 From: siontama Date: Sat, 16 Jul 2022 17:20:13 +0900 Subject: [PATCH 7/8] Refactor: convert indent from tab to space --- stdlib/src/array.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 2c31b73511d..ff4ba29188f 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1301,12 +1301,12 @@ mod array { impl IterNextIterable for PyArrayIter {} impl IterNext for PyArrayIter { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { - zelf.internal.lock().next(|array, pos| { - Ok(match array.read().get(pos, vm) { + zelf.internal.lock().next(|array, pos| { + Ok(match array.read().get(pos, vm) { Some(item) => PyIterReturn::Return(item?), None => PyIterReturn::StopIteration(None), }) - }) + }) } } From 2bf61aa60a5c2c3a84b8eb9fc4c8d2f1236d6d94 Mon Sep 17 00:00:00 2001 From: siontama Date: Sat, 16 Jul 2022 17:40:40 +0900 Subject: [PATCH 8/8] Refactor: delete unnecessary clones --- stdlib/src/array.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index ff4ba29188f..6dc7fb1bdfb 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -1259,7 +1259,7 @@ mod array { impl Iterable for PyArray { fn iter(zelf: PyRef, vm: &VirtualMachine) -> PyResult { Ok(PyArrayIter { - internal: PyMutex::new(PositionIterInternal::new(zelf.clone(), 0)), + internal: PyMutex::new(PositionIterInternal::new(zelf, 0)), } .into_pyobject(vm)) }