Skip to content

Commit 807e126

Browse files
committed
pyclass: no need to try inherit base dict and weaklist
1 parent 90e8ef7 commit 807e126

File tree

6 files changed

+112
-119
lines changed

6 files changed

+112
-119
lines changed

pyo3-macros-backend/src/pyclass.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,17 +664,13 @@ impl<'a> PyClassImplsBuilder<'a> {
664664
let attr = self.attr;
665665
let dict = if attr.has_dict {
666666
quote! { _pyo3::pyclass_slots::PyClassDictSlot }
667-
} else if attr.has_extends {
668-
quote! { <Self::BaseType as _pyo3::class::impl_::PyClassBaseType>::Dict }
669667
} else {
670668
quote! { _pyo3::pyclass_slots::PyClassDummySlot }
671669
};
672670

673671
// insert space for weak ref
674672
let weakref = if attr.has_weaklist {
675673
quote! { _pyo3::pyclass_slots::PyClassWeakRefSlot }
676-
} else if attr.has_extends {
677-
quote! { <Self::BaseType as _pyo3::class::impl_::PyClassBaseType>::WeakRef }
678674
} else {
679675
quote! { _pyo3::pyclass_slots::PyClassDummySlot }
680676
};

src/class/impl_.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,6 @@ impl<T: Send, U: PyClassBaseType> PyClassThreadChecker<T> for ThreadCheckerInher
760760

761761
/// Trait denoting that this class is suitable to be used as a base type for PyClass.
762762
pub trait PyClassBaseType: Sized {
763-
type Dict;
764-
type WeakRef;
765763
type LayoutAsBase: PyCellLayout<Self>;
766764
type BaseNativeType;
767765
type ThreadChecker: PyClassThreadChecker<Self>;
@@ -770,8 +768,6 @@ pub trait PyClassBaseType: Sized {
770768

771769
/// All PyClasses can be used as a base type.
772770
impl<T: PyClass> PyClassBaseType for T {
773-
type Dict = T::Dict;
774-
type WeakRef = T::WeakRef;
775771
type LayoutAsBase = crate::pycell::PyCell<T>;
776772
type BaseNativeType = T::BaseNativeType;
777773
type ThreadChecker = T::ThreadChecker;

src/types/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ macro_rules! pyobject_native_type_sized {
197197
unsafe impl $crate::type_object::PyLayout<$name> for $layout {}
198198
impl $crate::type_object::PySizedLayout<$name> for $layout {}
199199
impl<'a, $($generics,)*> $crate::class::impl_::PyClassBaseType for $name {
200-
type Dict = $crate::pyclass_slots::PyClassDummySlot;
201-
type WeakRef = $crate::pyclass_slots::PyClassDummySlot;
202200
type LayoutAsBase = $crate::pycell::PyCellBase<$layout>;
203201
type BaseNativeType = $name;
204202
type ThreadChecker = $crate::class::impl_::ThreadCheckerStub<$crate::PyObject>;

tests/test_class_basics.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,115 @@ fn test_tuple_struct_class() {
343343
assert_eq!(instance.borrow(py).0, 1234);
344344
});
345345
}
346+
347+
348+
#[pyclass(dict, subclass)]
349+
struct DunderDictSupport {}
350+
351+
#[test]
352+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
353+
fn dunder_dict_support() {
354+
let gil = Python::acquire_gil();
355+
let py = gil.python();
356+
let inst = PyCell::new(py, DunderDictSupport {}).unwrap();
357+
py_run!(
358+
py,
359+
inst,
360+
r#"
361+
inst.a = 1
362+
assert inst.a == 1
363+
"#
364+
);
365+
}
366+
367+
// Accessing inst.__dict__ only supported in limited API from Python 3.10
368+
#[test]
369+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
370+
fn access_dunder_dict() {
371+
let gil = Python::acquire_gil();
372+
let py = gil.python();
373+
let inst = PyCell::new(py, DunderDictSupport {}).unwrap();
374+
py_run!(
375+
py,
376+
inst,
377+
r#"
378+
inst.a = 1
379+
assert inst.__dict__ == {'a': 1}
380+
"#
381+
);
382+
}
383+
384+
// If the base class has dict support, child class also has dict
385+
#[pyclass(extends=DunderDictSupport)]
386+
struct InheritDict {
387+
_value: usize,
388+
}
389+
390+
#[test]
391+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
392+
fn inherited_dict() {
393+
let gil = Python::acquire_gil();
394+
let py = gil.python();
395+
let inst = PyCell::new(py, (InheritDict { _value: 0 }, DunderDictSupport {})).unwrap();
396+
py_run!(
397+
py,
398+
inst,
399+
r#"
400+
inst.a = 1
401+
assert inst.a == 1
402+
"#
403+
);
404+
}
405+
406+
#[pyclass(weakref, dict)]
407+
struct WeakRefDunderDictSupport {}
408+
409+
#[test]
410+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
411+
fn weakref_dunder_dict_support() {
412+
let gil = Python::acquire_gil();
413+
let py = gil.python();
414+
let inst = PyCell::new(py, WeakRefDunderDictSupport {}).unwrap();
415+
py_run!(
416+
py,
417+
inst,
418+
"import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1"
419+
);
420+
}
421+
422+
423+
424+
#[pyclass(weakref, subclass)]
425+
struct WeakRefSupport {}
426+
427+
#[test]
428+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
429+
fn weakref_support() {
430+
let gil = Python::acquire_gil();
431+
let py = gil.python();
432+
let inst = PyCell::new(py, WeakRefSupport {}).unwrap();
433+
py_run!(
434+
py,
435+
inst,
436+
"import weakref; assert weakref.ref(inst)() is inst"
437+
);
438+
}
439+
440+
// If the base class has weakref support, child class also has weakref.
441+
#[pyclass(extends=WeakRefSupport)]
442+
struct InheritWeakRef {
443+
_value: usize,
444+
}
445+
446+
#[test]
447+
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
448+
fn inherited_weakref() {
449+
let gil = Python::acquire_gil();
450+
let py = gil.python();
451+
let inst = PyCell::new(py, (InheritWeakRef { _value: 0 }, WeakRefSupport {})).unwrap();
452+
py_run!(
453+
py,
454+
inst,
455+
"import weakref; assert weakref.ref(inst)() is inst"
456+
);
457+
}

tests/test_gc.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -148,41 +148,6 @@ fn gc_integration2() {
148148
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
149149
}
150150

151-
#[pyclass(weakref, subclass)]
152-
struct WeakRefSupport {}
153-
154-
#[test]
155-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
156-
fn weakref_support() {
157-
let gil = Python::acquire_gil();
158-
let py = gil.python();
159-
let inst = PyCell::new(py, WeakRefSupport {}).unwrap();
160-
py_run!(
161-
py,
162-
inst,
163-
"import weakref; assert weakref.ref(inst)() is inst"
164-
);
165-
}
166-
167-
// If the base class has weakref support, child class also has weakref.
168-
#[pyclass(extends=WeakRefSupport)]
169-
struct InheritWeakRef {
170-
_value: usize,
171-
}
172-
173-
#[test]
174-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
175-
fn inherited_weakref() {
176-
let gil = Python::acquire_gil();
177-
let py = gil.python();
178-
let inst = PyCell::new(py, (InheritWeakRef { _value: 0 }, WeakRefSupport {})).unwrap();
179-
py_run!(
180-
py,
181-
inst,
182-
"import weakref; assert weakref.ref(inst)() is inst"
183-
);
184-
}
185-
186151
#[pyclass(subclass)]
187152
struct BaseClassWithDrop {
188153
data: Option<Arc<AtomicBool>>,

tests/test_pyproto.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -349,80 +349,6 @@ fn test_cls_impl() {
349349
py_assert!(py, ob, "ob[100:200:1] == 'slice'");
350350
}
351351

352-
#[pyclass(dict, subclass)]
353-
struct DunderDictSupport {}
354-
355-
#[test]
356-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
357-
fn dunder_dict_support() {
358-
let gil = Python::acquire_gil();
359-
let py = gil.python();
360-
let inst = PyCell::new(py, DunderDictSupport {}).unwrap();
361-
py_run!(
362-
py,
363-
inst,
364-
r#"
365-
inst.a = 1
366-
assert inst.a == 1
367-
"#
368-
);
369-
}
370-
371-
// Accessing inst.__dict__ only supported in limited API from Python 3.10
372-
#[test]
373-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
374-
fn access_dunder_dict() {
375-
let gil = Python::acquire_gil();
376-
let py = gil.python();
377-
let inst = PyCell::new(py, DunderDictSupport {}).unwrap();
378-
py_run!(
379-
py,
380-
inst,
381-
r#"
382-
inst.a = 1
383-
assert inst.__dict__ == {'a': 1}
384-
"#
385-
);
386-
}
387-
388-
// If the base class has dict support, child class also has dict
389-
#[pyclass(extends=DunderDictSupport)]
390-
struct InheritDict {
391-
_value: usize,
392-
}
393-
394-
#[test]
395-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
396-
fn inherited_dict() {
397-
let gil = Python::acquire_gil();
398-
let py = gil.python();
399-
let inst = PyCell::new(py, (InheritDict { _value: 0 }, DunderDictSupport {})).unwrap();
400-
py_run!(
401-
py,
402-
inst,
403-
r#"
404-
inst.a = 1
405-
assert inst.a == 1
406-
"#
407-
);
408-
}
409-
410-
#[pyclass(weakref, dict)]
411-
struct WeakRefDunderDictSupport {}
412-
413-
#[test]
414-
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_9)), ignore)]
415-
fn weakref_dunder_dict_support() {
416-
let gil = Python::acquire_gil();
417-
let py = gil.python();
418-
let inst = PyCell::new(py, WeakRefDunderDictSupport {}).unwrap();
419-
py_run!(
420-
py,
421-
inst,
422-
"import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1"
423-
);
424-
}
425-
426352
#[pyclass]
427353
struct ClassWithGetAttr {
428354
#[pyo3(get, set)]

0 commit comments

Comments
 (0)