Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,27 @@ class Weak(object):
self.assertEqual(r(), None)
del r

def test_weakrefs_from_secondary_base(self):
# gh-95710: weak references are supported if any base supports them,
# even if the primary base is a variable-size type.
import weakref
class Weak:
pass
for base, arg in (int, 42), (tuple, (1, 2)), (bytes, b'ab'):
with self.subTest(base=base):
class C(base, Weak):
pass
c = C(arg)
r = weakref.ref(c)
self.assertIs(r(), c)
self.assertEqual(c, arg)
del c
support.gc_collect()
self.assertIsNone(r())
class D(base):
pass
self.assertRaises(TypeError, weakref.ref, D(arg))

def test_properties(self):
# Testing property...
class C(object):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Instances of a class that inherits from a variable-size built-in type
(such as :class:`int` or :class:`tuple`)
and from a class supporting weak references
now support weak references too.
2 changes: 2 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4357,6 +4357,8 @@ type_new_slots(type_new_ctx *ctx, PyObject *dict)
if (ctx->may_add_weak && ctx->base->tp_itemsize == 0) {
ctx->add_weak++;
}
/* Secondary bases may provide weakrefs */
type_new_slots_bases(ctx);
}
else {
/* Have slots */
Expand Down
Loading