Skip to content

Commit d958d91

Browse files
committed
PyObjectWeak is PyRef<PyWeak>
1 parent c2bb72f commit d958d91

8 files changed

Lines changed: 30 additions & 28 deletions

File tree

stdlib/src/ssl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod _ssl {
3030
},
3131
socket::{self, PySocket},
3232
vm::{
33-
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef},
33+
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef, PyWeak},
3434
convert::{ToPyException, ToPyObject},
3535
exceptions,
3636
function::{
@@ -39,7 +39,7 @@ mod _ssl {
3939
stdlib::os::PyPathLike,
4040
types::Constructor,
4141
utils::{Either, ToCString},
42-
PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
42+
PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
4343
},
4444
};
4545
use crossbeam_utils::atomic::AtomicCell;
@@ -890,7 +890,7 @@ mod _ssl {
890890
stream: PyRwLock<ssl::SslStream<SocketStream>>,
891891
socket_type: SslServerOrClient,
892892
server_hostname: Option<PyStrRef>,
893-
owner: PyRwLock<Option<PyObjectWeak>>,
893+
owner: PyRwLock<Option<PyRef<PyWeak>>>,
894894
}
895895

896896
impl fmt::Debug for PySslSocket {

vm/src/builtins/pytype.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
mappingproxy::PyMappingProxy, object, pyunion, PyClassMethod, PyDictRef, PyList,
3-
PyStaticMethod, PyStr, PyStrRef, PyTuple, PyTupleRef,
3+
PyStaticMethod, PyStr, PyStrRef, PyTuple, PyTupleRef, PyWeak,
44
};
55
use crate::common::{
66
ascii,
@@ -11,7 +11,7 @@ use crate::{
1111
function::{FuncArgs, KwArgs, OptionalArg},
1212
pyclass::{PyClassImpl, StaticType},
1313
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
14-
AsObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
14+
AsObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
1515
};
1616
use itertools::Itertools;
1717
use std::{
@@ -29,7 +29,7 @@ pub struct PyType {
2929
pub base: Option<PyTypeRef>,
3030
pub bases: Vec<PyTypeRef>,
3131
pub mro: Vec<PyTypeRef>,
32-
pub subclasses: PyRwLock<Vec<PyObjectWeak>>,
32+
pub subclasses: PyRwLock<Vec<PyRef<PyWeak>>>,
3333
pub attributes: PyRwLock<PyAttributes>,
3434
pub slots: PyTypeSlots,
3535
}

vm/src/builtins/weakproxy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use super::{PyStrRef, PyTypeRef};
1+
use super::{PyStrRef, PyTypeRef, PyWeak};
22
use crate::{
33
function::OptionalArg,
44
pyclass::PyClassImpl,
55
types::{Constructor, SetAttr},
6-
PyContext, PyObjectRef, PyObjectWeak, PyResult, PyValue, VirtualMachine,
6+
PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
77
};
88

99
#[pyclass(module = false, name = "weakproxy")]
1010
#[derive(Debug)]
1111
pub struct PyWeakProxy {
12-
weak: PyObjectWeak,
12+
weak: PyRef<PyWeak>,
1313
}
1414

1515
impl PyValue for PyWeakProxy {

vm/src/builtins/weakref.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ impl Constructor for PyWeak {
4343
vm: &VirtualMachine,
4444
) -> PyResult {
4545
let weak = referent.downgrade_with_typ(callback.into_option(), cls, vm)?;
46-
let pyref_weak: PyRef<PyWeak> = weak.into();
47-
Ok(pyref_weak.into())
46+
Ok(weak.into())
4847
}
4948
}
5049

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub use self::convert::{TryFromBorrowedObject, TryFromObject};
9090
// pyobject items
9191
pub use self::pyobject::{AsObject, PyContext, PyMethod, PyRefExact, PyResult, PyValue};
9292
// pyobjectrc items
93-
pub use self::pyobject::{PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef};
93+
pub use self::pyobject::{PyObject, PyObjectRef, PyObjectView, PyRef, PyWeakRef};
9494
pub use self::types::PyStructSequence;
9595
pub use self::vm::{InitParameter, Interpreter, PySettings, VirtualMachine};
9696

vm/src/pyobjectrc.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl WeakRefList {
147147
cls_is_weakref: bool,
148148
callback: Option<PyObjectRef>,
149149
dict: Option<PyDictRef>,
150-
) -> PyObjectWeak {
150+
) -> PyRef<PyWeak> {
151151
let is_generic = cls_is_weakref && callback.is_none();
152152
let inner_ptr = self.inner.get_or_init(|| {
153153
Box::new(PyMutex::new(WeakListInner {
@@ -250,7 +250,7 @@ impl WeakRefList {
250250
Box::from_raw(ptr.as_ptr());
251251
}
252252

253-
fn get_weak_references(&self) -> Vec<PyObjectWeak> {
253+
fn get_weak_references(&self) -> Vec<PyRef<PyWeak>> {
254254
let inner = match self.try_lock() {
255255
Some(inner) => inner,
256256
None => return vec![],
@@ -361,9 +361,7 @@ impl Drop for PyWeak {
361361
}
362362
}
363363

364-
pub type PyObjectWeak = PyRef<PyWeak>;
365-
366-
impl PyObjectWeak {
364+
impl PyRef<PyWeak> {
367365
#[inline(always)]
368366
pub fn upgrade(&self) -> Option<PyObjectRef> {
369367
PyWeak::upgrade(self)
@@ -562,7 +560,7 @@ impl PyObject {
562560
callback: Option<PyObjectRef>,
563561
// a reference to weakref_type **specifically**
564562
typ: PyTypeRef,
565-
) -> Option<PyObjectWeak> {
563+
) -> Option<PyRef<PyWeak>> {
566564
self.weak_ref_list()
567565
.map(|wrl| wrl.add(self, typ, true, callback, None))
568566
}
@@ -572,7 +570,7 @@ impl PyObject {
572570
callback: Option<PyObjectRef>,
573571
typ: PyTypeRef,
574572
vm: &VirtualMachine,
575-
) -> PyResult<PyObjectWeak> {
573+
) -> PyResult<PyRef<PyWeak>> {
576574
let dict = if typ
577575
.slots
578576
.flags
@@ -597,11 +595,11 @@ impl PyObject {
597595
&self,
598596
callback: Option<PyObjectRef>,
599597
vm: &VirtualMachine,
600-
) -> PyResult<PyObjectWeak> {
598+
) -> PyResult<PyRef<PyWeak>> {
601599
self.downgrade_with_typ(callback, vm.ctx.types.weakref_type.clone(), vm)
602600
}
603601

604-
pub fn get_weak_references(&self) -> Option<Vec<PyObjectWeak>> {
602+
pub fn get_weak_references(&self) -> Option<Vec<PyRef<PyWeak>>> {
605603
self.weak_ref_list().map(|wrl| wrl.get_weak_references())
606604
}
607605

@@ -1006,7 +1004,7 @@ where
10061004

10071005
#[repr(transparent)]
10081006
pub struct PyWeakRef<T: PyObjectPayload> {
1009-
weak: PyObjectWeak,
1007+
weak: PyRef<PyWeak>,
10101008
_marker: PhantomData<T>,
10111009
}
10121010

vm/src/stdlib/weakref.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ pub(crate) use _weakref::make_module;
88

99
#[pymodule]
1010
mod _weakref {
11-
use crate::builtins::{PyDictRef, PyTypeRef, PyWeak};
12-
use crate::{PyObjectRef, PyRef, PyResult, VirtualMachine};
11+
use crate::{
12+
builtins::{PyDictRef, PyTypeRef, PyWeak},
13+
PyObjectRef, PyResult, VirtualMachine,
14+
};
1315

1416
#[pyattr(name = "ref")]
1517
fn ref_(vm: &VirtualMachine) -> PyTypeRef {
@@ -40,7 +42,7 @@ mod _weakref {
4042
#[pyfunction]
4143
fn getweakrefs(obj: PyObjectRef) -> Vec<PyObjectRef> {
4244
match obj.get_weak_references() {
43-
Some(v) => v.into_iter().map(|weak| PyRef::from(weak).into()).collect(),
45+
Some(v) => v.into_iter().map(Into::into).collect(),
4446
None => vec![],
4547
}
4648
}

wasm/lib/src/vm_class.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::{
55
};
66
use js_sys::{Object, TypeError};
77
use rustpython_vm::{
8+
builtins::PyWeak,
89
compile::{self, Mode},
910
scope::Scope,
10-
InitParameter, Interpreter, PyObjectRef, PyObjectWeak, PyResult, PySettings, PyValue,
11-
VirtualMachine,
11+
InitParameter, Interpreter, PyObjectRef, PyRef, PyResult, PySettings, PyValue, VirtualMachine,
1212
};
1313
use std::{
1414
cell::RefCell,
@@ -197,7 +197,10 @@ impl WASMVirtualMachine {
197197
STORED_VMS.with(|cell| cell.borrow().contains_key(&self.id))
198198
}
199199

200-
pub(crate) fn push_held_rc(&self, obj: PyObjectRef) -> Result<PyResult<PyObjectWeak>, JsValue> {
200+
pub(crate) fn push_held_rc(
201+
&self,
202+
obj: PyObjectRef,
203+
) -> Result<PyResult<PyRef<PyWeak>>, JsValue> {
201204
self.with_vm(|vm, stored_vm| {
202205
let weak = obj.downgrade(None, vm)?;
203206
stored_vm.held_objects.borrow_mut().push(obj);

0 commit comments

Comments
 (0)