Skip to content

Commit 7ae0244

Browse files
committed
Make byte* iterators atomic to fix a shared list/bytearray test
1 parent a10936c commit 7ae0244

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

Lib/test/list_tests.py

-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,6 @@ def __iter__(self):
565565
raise KeyboardInterrupt
566566
self.assertRaises(KeyboardInterrupt, list, F())
567567

568-
# TODO: RUSTPYTHON
569-
@unittest.expectedFailure
570568
def test_exhausted_iterator(self):
571569
a = self.type2test([1, 2, 3])
572570
exhit = iter(a)

vm/src/obj/objbytearray.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementation of the python bytearray object.
2-
use std::cell::Cell;
2+
use crossbeam_utils::atomic::AtomicCell;
33
use std::convert::TryFrom;
44
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
55

@@ -150,7 +150,7 @@ impl PyByteArray {
150150
#[pymethod(name = "__iter__")]
151151
fn iter(zelf: PyRef<Self>) -> PyByteArrayIterator {
152152
PyByteArrayIterator {
153-
position: Cell::new(0),
153+
position: AtomicCell::new(0),
154154
bytearray: zelf,
155155
}
156156
}
@@ -605,7 +605,7 @@ impl PyByteArray {
605605
#[pyclass]
606606
#[derive(Debug)]
607607
pub struct PyByteArrayIterator {
608-
position: Cell<usize>,
608+
position: AtomicCell<usize>,
609609
bytearray: PyByteArrayRef,
610610
}
611611

@@ -619,9 +619,9 @@ impl PyValue for PyByteArrayIterator {
619619
impl PyByteArrayIterator {
620620
#[pymethod(name = "__next__")]
621621
fn next(&self, vm: &VirtualMachine) -> PyResult<u8> {
622-
if self.position.get() < self.bytearray.borrow_value().len() {
623-
let ret = self.bytearray.borrow_value().elements[self.position.get()];
624-
self.position.set(self.position.get() + 1);
622+
let bytearr = self.bytearray.borrow_value();
623+
let pos = self.position.fetch_add(1);
624+
if let Some(&ret) = bytearr.elements.get(pos) {
625625
Ok(ret)
626626
} else {
627627
Err(objiter::new_stop_iteration(vm))

vm/src/obj/objbytes.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::Cell;
1+
use crossbeam_utils::atomic::AtomicCell;
22
use std::mem::size_of;
33
use std::ops::Deref;
44

@@ -144,7 +144,7 @@ impl PyBytes {
144144
#[pymethod(name = "__iter__")]
145145
fn iter(zelf: PyRef<Self>) -> PyBytesIterator {
146146
PyBytesIterator {
147-
position: Cell::new(0),
147+
position: AtomicCell::new(0),
148148
bytes: zelf,
149149
}
150150
}
@@ -484,7 +484,7 @@ impl PyBytes {
484484
#[pyclass]
485485
#[derive(Debug)]
486486
pub struct PyBytesIterator {
487-
position: Cell<usize>,
487+
position: AtomicCell<usize>,
488488
bytes: PyBytesRef,
489489
}
490490

@@ -498,9 +498,8 @@ impl PyValue for PyBytesIterator {
498498
impl PyBytesIterator {
499499
#[pymethod(name = "__next__")]
500500
fn next(&self, vm: &VirtualMachine) -> PyResult<u8> {
501-
if self.position.get() < self.bytes.inner.len() {
502-
let ret = self.bytes[self.position.get()];
503-
self.position.set(self.position.get() + 1);
501+
let pos = self.position.fetch_add(1);
502+
if let Some(&ret) = self.bytes.get_value().get(pos) {
504503
Ok(ret)
505504
} else {
506505
Err(objiter::new_stop_iteration(vm))

0 commit comments

Comments
 (0)