-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
GH-95589: Dont crash when subclassing extension classes with multiple inheritance #96028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1b39c88
b994446
bca93af
2ce1e9f
21b50cf
0be2b91
c930792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -677,21 +677,43 @@ def test_heaptype_with_custom_metaclass(self): | |
|
||
def test_multiple_inheritance_ctypes_with_weakref_or_dict(self): | ||
|
||
class Both1(_testcapi.HeapCTypeWithWeakref, _testcapi.HeapCTypeWithDict): | ||
with self.assertRaises(TypeError): | ||
class Both1(_testcapi.HeapCTypeWithWeakref, _testcapi.HeapCTypeWithDict): | ||
pass | ||
with self.assertRaises(TypeError): | ||
class Both2(_testcapi.HeapCTypeWithDict, _testcapi.HeapCTypeWithWeakref): | ||
pass | ||
|
||
def test_multiple_inheritance_ctypes_with_weakref_or_dict_and_other_builtin(self): | ||
|
||
with self.assertRaises(TypeError): | ||
class C1(_testcapi.HeapCTypeWithDict, list): | ||
pass | ||
|
||
with self.assertRaises(TypeError): | ||
class C2(_testcapi.HeapCTypeWithWeakref, list): | ||
pass | ||
|
||
class C3(_testcapi.HeapCTypeWithManagedDict, list): | ||
pass | ||
class Both2(_testcapi.HeapCTypeWithDict, _testcapi.HeapCTypeWithWeakref): | ||
class C4(_testcapi.HeapCTypeWithManagedWeakref, list): | ||
pass | ||
|
||
for cls in (_testcapi.HeapCTypeWithDict, _testcapi.HeapCTypeWithDict2, | ||
_testcapi.HeapCTypeWithWeakref, _testcapi.HeapCTypeWithWeakref2): | ||
for cls2 in (_testcapi.HeapCTypeWithDict, _testcapi.HeapCTypeWithDict2, | ||
_testcapi.HeapCTypeWithWeakref, _testcapi.HeapCTypeWithWeakref2): | ||
if cls is not cls2: | ||
class S(cls, cls2): | ||
pass | ||
class B1(Both1, cls): | ||
inst = C3() | ||
inst.append(0) | ||
str(inst.__dict__) | ||
|
||
inst = C4() | ||
inst.append(0) | ||
str(inst.__weakref__) | ||
|
||
for cls in (_testcapi.HeapCTypeWithManagedDict, _testcapi.HeapCTypeWithManagedWeakref): | ||
for cls2 in (_testcapi.HeapCTypeWithDict, _testcapi.HeapCTypeWithWeakref): | ||
class S(cls, cls2): | ||
pass | ||
class B1(C3, cls): | ||
pass | ||
class B2(Both1, cls): | ||
class B2(C4, cls): | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also assert that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't change the mro. That's determined by the C3 algorithm. https://en.wikipedia.org/wiki/C3_linearization |
||
|
||
def test_pytype_fromspec_with_repeated_slots(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Extensions classes that set ``tp_dictoffset`` and ``tp_weaklistoffset`` | ||
lose the support for multiple inheritance, but are now safe. Extension | ||
classes should use :const:`Py_TPFLAGS_MANAGED_DICT` and | ||
:const:`Py_TPFLAGS_MANAGED_WEAKREF` instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the extension docs be updated to provide examples of using the new API? (Probably as a separate PR...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they should. I also plan to add a new file documenting the new object layout.