[Python-checkins] bpo-39481: Make weakref and WeakSet generic (GH-19497)

Ethan Smith webhook-mailer at python.org
Tue Apr 14 00:54:44 EDT 2020


https://github.com/python/cpython/commit/8ef875028a3644a329c87ce420a73793e315143f
commit: 8ef875028a3644a329c87ce420a73793e315143f
branch: master
author: Ethan Smith <ethan at ethanhs.me>
committer: GitHub <noreply at github.com>
date: 2020-04-13T21:54:40-07:00
summary:

bpo-39481: Make weakref and WeakSet generic (GH-19497)

files:
M Lib/_weakrefset.py
M Lib/test/test_genericalias.py
M Objects/weakrefobject.c

diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py
index 7a84823622ee7..b267780f0ced7 100644
--- a/Lib/_weakrefset.py
+++ b/Lib/_weakrefset.py
@@ -3,6 +3,7 @@
 # by abc.py to load everything else at startup.
 
 from _weakref import ref
+from types import GenericAlias
 
 __all__ = ['WeakSet']
 
@@ -197,3 +198,5 @@ def isdisjoint(self, other):
 
     def __repr__(self):
         return repr(self.data)
+
+    __class_getitem__ = classmethod(GenericAlias)
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index 770aeef45105b..686df17d6a961 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -33,6 +33,7 @@
 from urllib.parse import SplitResult, ParseResult
 from unittest.case import _AssertRaisesContext
 from queue import Queue, SimpleQueue
+from weakref import WeakSet, ReferenceType, ref
 import typing
 
 from typing import TypeVar
@@ -73,6 +74,7 @@ def test_subscriptable(self):
                   Array, LibraryLoader,
                   SplitResult, ParseResult,
                   ValueProxy, ApplyResult,
+                  WeakSet, ReferenceType, ref,
                   ShareableList, SimpleQueue,
                   Future, _WorkItem,
                   Morsel,
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 1e6697b729c9c..dd9b789823512 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -362,6 +362,12 @@ static PyMemberDef weakref_members[] = {
     {NULL} /* Sentinel */
 };
 
+static PyMethodDef weakref_methods[] = {
+    {"__class_getitem__",    (PyCFunction)Py_GenericAlias,
+    METH_O|METH_CLASS,       PyDoc_STR("See PEP 585")},
+    {NULL} /* Sentinel */
+};
+
 PyTypeObject
 _PyWeakref_RefType = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -392,7 +398,7 @@ _PyWeakref_RefType = {
     0,                          /*tp_weaklistoffset*/
     0,                          /*tp_iter*/
     0,                          /*tp_iternext*/
-    0,                          /*tp_methods*/
+    weakref_methods,            /*tp_methods*/
     weakref_members,            /*tp_members*/
     0,                          /*tp_getset*/
     0,                          /*tp_base*/



More information about the Python-checkins mailing list