Skip to content

Commit 8352e32

Browse files
gh-79512: Fixed names and __module__ value of weakref classes (GH-93719)
Classes ReferenceType, ProxyType and CallableProxyType have now correct atrtributes __module__, __name__ and __qualname__. It makes them (types, not instances) pickleable.
1 parent 6fd4c8e commit 8352e32

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Lib/test/test_weakref.py

+11
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,17 @@ def test_atexit(self):
21542154
self.assertTrue(b'ZeroDivisionError' in err)
21552155

21562156

2157+
class ModuleTestCase(unittest.TestCase):
2158+
def test_names(self):
2159+
for name in ('ReferenceType', 'ProxyType', 'CallableProxyType',
2160+
'WeakMethod', 'WeakSet', 'WeakKeyDictionary', 'WeakValueDictionary'):
2161+
obj = getattr(weakref, name)
2162+
if name != 'WeakSet':
2163+
self.assertEqual(obj.__module__, 'weakref')
2164+
self.assertEqual(obj.__name__, name)
2165+
self.assertEqual(obj.__qualname__, name)
2166+
2167+
21572168
libreftest = """ Doctest for examples in the library reference: weakref.rst
21582169
21592170
>>> from test.support import gc_collect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed names and ``__module__`` value of :mod:`weakref` classes
2+
:class:`~weakref.ReferenceType`, :class:`~weakref.ProxyType`,
3+
:class:`~weakref.CallableProxyType`. It makes them pickleable.

Objects/weakrefobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static PyMethodDef weakref_methods[] = {
371371
PyTypeObject
372372
_PyWeakref_RefType = {
373373
PyVarObject_HEAD_INIT(&PyType_Type, 0)
374-
.tp_name = "weakref",
374+
.tp_name = "weakref.ReferenceType",
375375
.tp_basicsize = sizeof(PyWeakReference),
376376
.tp_dealloc = weakref_dealloc,
377377
.tp_vectorcall_offset = offsetof(PyWeakReference, vectorcall),
@@ -719,7 +719,7 @@ static PyMappingMethods proxy_as_mapping = {
719719
PyTypeObject
720720
_PyWeakref_ProxyType = {
721721
PyVarObject_HEAD_INIT(&PyType_Type, 0)
722-
"weakproxy",
722+
"weakref.ProxyType",
723723
sizeof(PyWeakReference),
724724
0,
725725
/* methods */
@@ -754,7 +754,7 @@ _PyWeakref_ProxyType = {
754754
PyTypeObject
755755
_PyWeakref_CallableProxyType = {
756756
PyVarObject_HEAD_INIT(&PyType_Type, 0)
757-
"weakcallableproxy",
757+
"weakref.CallableProxyType",
758758
sizeof(PyWeakReference),
759759
0,
760760
/* methods */

0 commit comments

Comments
 (0)