Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add HAS_WEAKREF to _asyncio Future/Task, rename weakref helpers
- Add HAS_WEAKREF flag to PyFuture and PyTask (matches CPython)
- Rename subtype_getweakref/setweakref to subtype_get_weakref/set_weakref
  to fix cspell unknown word lint
  • Loading branch information
youknowone committed Mar 5, 2026
commit 75d8a3a60c30dcecf2b439c27e7cf2a7ce13ac9b
4 changes: 2 additions & 2 deletions crates/stdlib/src/_asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub(crate) mod _asyncio {
}

#[pyclass(
flags(BASETYPE, HAS_DICT),
flags(BASETYPE, HAS_DICT, HAS_WEAKREF),
with(Constructor, Initializer, Destructor, Representable, Iterable)
)]
impl PyFuture {
Expand Down Expand Up @@ -1169,7 +1169,7 @@ pub(crate) mod _asyncio {
}

#[pyclass(
flags(BASETYPE, HAS_DICT),
flags(BASETYPE, HAS_DICT, HAS_WEAKREF),
with(Constructor, Initializer, Destructor, Representable, Iterable)
)]
impl PyTask {
Expand Down
12 changes: 6 additions & 6 deletions crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,8 +1981,8 @@ impl Constructor for PyType {
let descriptor = vm.ctx.new_getset(
"__weakref__",
&typ,
subtype_getweakref,
subtype_setweakref,
subtype_get_weakref,
subtype_set_weakref,
);
typ.attributes
.write()
Expand Down Expand Up @@ -2426,15 +2426,15 @@ fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -
}
}

// subtype_getweakref
fn subtype_getweakref(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
// subtype_get_weakref
fn subtype_get_weakref(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
// Return the first weakref in the weakref list, or None
let weakref = obj.get_weakrefs();
Ok(weakref.unwrap_or_else(|| vm.ctx.none()))
}

// subtype_setweakref: __weakref__ is read-only
fn subtype_setweakref(obj: PyObjectRef, _value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
// subtype_set_weakref: __weakref__ is read-only
fn subtype_set_weakref(obj: PyObjectRef, _value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Err(vm.new_attribute_error(format!(
"attribute '__weakref__' of '{}' objects is not writable",
obj.class().name()
Expand Down
7 changes: 6 additions & 1 deletion crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ unsafe impl Link for GcLink {
/// Extension fields for objects that need dict, weakref list, or member slots.
/// Allocated as a prefix before PyInner when needed (prefix allocation pattern).
/// Access via `PyInner::ext_ref()` using negative offset from the object pointer.
#[repr(C)]
///
/// align(8) ensures size_of::<ObjExt>() is always a multiple of 8,
/// so the offset from Layout::extend equals size_of::<ObjExt>() for any
/// PyInner<T> alignment (important on wasm32 where pointers are 4 bytes
/// but some payloads like PyWeak have align 8 due to i64 fields).
#[repr(C, align(8))]
pub(super) struct ObjExt {
pub(super) dict: Option<InstanceDict>,
pub(super) weak_list: WeakRefList,
Expand Down