Skip to content

Commit 30aeb8b

Browse files
authored
Introduce PyAtomicRef and impl for PyObject.typ (RustPython#3828)
* introduce PyAtomicRef * refactor PyObject.typ use PyAtomicRef * add documents * add swap_to_temporary_refs * replace AtomicCell with PyAtomic * fix name conflict
1 parent 4be533a commit 30aeb8b

34 files changed

+158
-138
lines changed

stdlib/src/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ mod array {
11281128
return Self::reduce(zelf, vm);
11291129
}
11301130
let array = zelf.read();
1131-
let cls = zelf.class().clone();
1131+
let cls = zelf.class().to_owned();
11321132
let typecode = vm.ctx.new_str(array.typecode_str());
11331133
let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
11341134
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
@@ -1148,7 +1148,7 @@ mod array {
11481148
vm: &VirtualMachine,
11491149
) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
11501150
let array = zelf.read();
1151-
let cls = zelf.class().clone();
1151+
let cls = zelf.class().to_owned();
11521152
let typecode = vm.ctx.new_str(array.typecode_str());
11531153
let values = if array.typecode() == 'u' {
11541154
let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?;

stdlib/src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod math {
55
use crate::vm::{
66
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef, PyStrInterned},
77
function::{ArgIntoFloat, ArgIterable, Either, OptionalArg, PosArgs},
8-
identifier, AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
8+
identifier, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
99
};
1010
use num_bigint::BigInt;
1111
use num_traits::{One, Signed, Zero};

vm/src/anystr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
builtins::{PyIntRef, PyTupleRef},
33
cformat::CFormatString,
44
function::OptionalOption,
5-
AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
5+
PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
66
};
77
use num_traits::{cast::ToPrimitive, sign::Signed};
88
use std::str::FromStr;

vm/src/builtins/builtinfunc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl GetDescriptor for PyBuiltinMethod {
194194
Ok(obj) => obj,
195195
Err(result) => return result,
196196
};
197-
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
197+
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
198198
zelf.into()
199199
} else {
200200
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()

vm/src/builtins/bytearray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ impl PyByteArray {
684684
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
685685
let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).to_pyobject(vm);
686686
(
687-
zelf.class().clone(),
687+
zelf.class().to_owned(),
688688
PyTuple::new_ref(vec![bytes], &vm.ctx),
689689
zelf.as_object().dict(),
690690
)

vm/src/builtins/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl PyBytes {
545545
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
546546
let bytes = PyBytes::from(zelf.inner.elements.clone()).to_pyobject(vm);
547547
(
548-
zelf.class().clone(),
548+
zelf.class().to_owned(),
549549
PyTuple::new_ref(vec![bytes], &vm.ctx),
550550
zelf.as_object().dict(),
551551
)

vm/src/builtins/classmethod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl GetDescriptor for PyClassMethod {
5454
vm: &VirtualMachine,
5555
) -> PyResult {
5656
let (zelf, _obj) = Self::_unwrap(zelf, obj, vm)?;
57-
let cls = cls.unwrap_or_else(|| _obj.class().clone().into());
57+
let cls = cls.unwrap_or_else(|| _obj.class().to_owned().into());
5858
let call_descr_get: PyResult<PyObjectRef> = zelf.callable.lock().get_attr("__get__", vm);
5959
match call_descr_get {
6060
Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()),

vm/src/builtins/enumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl PyEnumerate {
6060
#[pymethod(magic)]
6161
fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyIter, BigInt)) {
6262
(
63-
zelf.class().clone(),
63+
zelf.class().to_owned(),
6464
(zelf.iterator.clone(), zelf.counter.read().clone()),
6565
)
6666
}

vm/src/builtins/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl GetDescriptor for PyFunction {
435435
vm: &VirtualMachine,
436436
) -> PyResult {
437437
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
438-
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
438+
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
439439
zelf.into()
440440
} else {
441441
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()

vm/src/builtins/object.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl PyBaseObject {
261261

262262
#[pygetset(name = "__class__")]
263263
fn get_class(obj: PyObjectRef) -> PyTypeRef {
264-
obj.class().clone()
264+
obj.class().to_owned()
265265
}
266266

267267
#[pygetset(name = "__class__", setter)]
@@ -270,7 +270,7 @@ impl PyBaseObject {
270270
match value.downcast::<PyType>() {
271271
Ok(cls) => {
272272
// FIXME(#1979) cls instances might have a payload
273-
*instance.class_lock().write() = cls;
273+
instance.set_class(cls, vm);
274274
Ok(())
275275
}
276276
Err(value) => {
@@ -311,7 +311,7 @@ impl PyBaseObject {
311311
let __reduce__ = identifier!(vm, __reduce__);
312312
if let Some(reduce) = vm.get_attribute_opt(obj.clone(), __reduce__)? {
313313
let object_reduce = vm.ctx.types.object_type.get_attr(__reduce__).unwrap();
314-
let typ_obj: PyObjectRef = obj.class().clone().into();
314+
let typ_obj: PyObjectRef = obj.class().to_owned().into();
315315
let class_reduce = typ_obj.get_attr(__reduce__, vm)?;
316316
if !class_reduce.is(&object_reduce) {
317317
return vm.invoke(&reduce, ());

0 commit comments

Comments
 (0)