| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -304,8 +304,6 @@ def test_pickle_for_empty_array(self): | |||
| 304 | 304 | self.assertEqual(a.x, b.x) | |
| 305 | 305 | self.assertEqual(type(a), type(b)) | |
| 306 | 306 | ||
| 307 | - # TODO: RUSTPYTHON | ||
| 308 | - @unittest.expectedFailure | ||
| 309 | 307 | def test_iterator_pickle(self): | |
| 310 | 308 | orig = array.array(self.typecode, self.example) | |
| 311 | 309 | data = list(orig) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,15 +6,15 @@ mod array { | |||
| 6 | 6 | common::{ | |
| 7 | 7 | atomic::{self, AtomicUsize}, | |
| 8 | 8 | lock::{ | |
| 9 | - PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyRwLock, PyRwLockReadGuard, | ||
| 10 | - PyRwLockWriteGuard, | ||
| 9 | + PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock, | ||
| 10 | + PyRwLockReadGuard, PyRwLockWriteGuard, | ||
| 11 | 11 | }, | |
| 12 | 12 | str::wchar_t, | |
| 13 | 13 | }, | |
| 14 | 14 | vm::{ | |
| 15 | 15 | builtins::{ | |
| 16 | - PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt, PyIntRef, PyList, | ||
| 17 | - PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef, | ||
| 16 | + PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt, | ||
| 17 | + PyIntRef, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef, | ||
| 18 | 18 | }, | |
| 19 | 19 | class_or_notimplemented, | |
| 20 | 20 | convert::{ToPyObject, ToPyResult, TryFromObject}, | |
@@ -1258,9 +1258,8 @@ mod array { | |||
| 1258 | 1258 | ||
| 1259 | 1259 | impl Iterable for PyArray { | |
| 1260 | 1260 | fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult { | |
| 1261 | - Ok(PyArrayIterator { | ||
| 1262 | - position: AtomicUsize::new(0), | ||
| 1263 | - array: zelf, | ||
| 1261 | + Ok(PyArrayIter { | ||
| 1262 | + internal: PyMutex::new(PositionIterInternal::new(zelf, 0)), | ||
| 1264 | 1263 | } | |
| 1265 | 1264 | .into_pyobject(vm)) | |
| 1266 | 1265 | } | |
@@ -1278,24 +1277,36 @@ mod array { | |||
| 1278 | 1277 | #[pyattr] | |
| 1279 | 1278 | #[pyclass(name = "arrayiterator")] | |
| 1280 | 1279 | #[derive(Debug, PyPayload)] | |
| 1281 | - pub struct PyArrayIterator { | ||
| 1282 | - position: AtomicUsize, | ||
| 1283 | - array: PyArrayRef, | ||
| 1280 | + pub struct PyArrayIter { | ||
| 1281 | + internal: PyMutex<PositionIterInternal<PyArrayRef>>, | ||
| 1284 | 1282 | } | |
| 1285 | 1283 | ||
| 1286 | - #[pyimpl(with(IterNext))] | ||
| 1287 | - impl PyArrayIterator {} | ||
| 1284 | + #[pyimpl(with(IterNext), flags(HAS_DICT))] | ||
| 1285 | + impl PyArrayIter { | ||
| 1286 | + #[pymethod(magic)] | ||
| 1287 | + fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { | ||
| 1288 | + self.internal | ||
| 1289 | + .lock() | ||
| 1290 | + .set_state(state, |obj, pos| pos.min(obj.len()), vm) | ||
| 1291 | + } | ||
| 1288 | 1292 | ||
| 1289 | - impl IterNextIterable for PyArrayIterator {} | ||
| 1290 | - impl IterNext for PyArrayIterator { | ||
| 1293 | + #[pymethod(magic)] | ||
| 1294 | + fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef { | ||
| 1295 | + self.internal | ||
| 1296 | + .lock() | ||
| 1297 | + .builtins_iter_reduce(|x| x.clone().into(), vm) | ||
| 1298 | + } | ||
| 1299 | + } | ||
| 1300 | + | ||
| 1301 | + impl IterNextIterable for PyArrayIter {} | ||
| 1302 | + impl IterNext for PyArrayIter { | ||
| 1291 | 1303 | fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> { | |
| 1292 | - let pos = zelf.position.fetch_add(1, atomic::Ordering::SeqCst); | ||
| 1293 | - let r = if let Some(item) = zelf.array.read().get(pos, vm) { | ||
| 1294 | - PyIterReturn::Return(item?) | ||
| 1295 | - } else { | ||
| 1296 | - PyIterReturn::StopIteration(None) | ||
| 1297 | - }; | ||
| 1298 | - Ok(r) | ||
| 1304 | + zelf.internal.lock().next(|array, pos| { | ||
| 1305 | + Ok(match array.read().get(pos, vm) { | ||
| 1306 | + Some(item) => PyIterReturn::Return(item?), | ||
| 1307 | + None => PyIterReturn::StopIteration(None), | ||
| 1308 | + }) | ||
| 1309 | + }) | ||
| 1299 | 1310 | } | |
| 1300 | 1311 | } | |
| 1301 | 1312 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments