]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92114: Improve error message for types with __class_getitem__ = None (GH-92115)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 2 May 2022 05:29:49 +0000 (08:29 +0300)
committerGitHub <noreply@github.com>
Mon, 2 May 2022 05:29:49 +0000 (08:29 +0300)
Lib/test/test_genericclass.py
Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst [new file with mode: 0644]
Objects/abstract.c

index 27420d4f2bad54dc3967c86694a6067f3a60e546..d8bb37f69e18a1761580c3ab2e9d71872bb7ef90 100644 (file)
@@ -220,6 +220,7 @@ class TestClassGetitem(unittest.TestCase):
                 return None
         with self.assertRaises(TypeError):
             C_too_few[int]
+
         class C_too_many:
             def __class_getitem__(cls, one, two):
                 return None
@@ -232,16 +233,23 @@ class TestClassGetitem(unittest.TestCase):
                 return None
         with self.assertRaises(TypeError):
             C()[int]
+
         class E: ...
         e = E()
         e.__class_getitem__ = lambda cls, item: 'This will not work'
         with self.assertRaises(TypeError):
             e[int]
+
         class C_not_callable:
             __class_getitem__ = "Surprise!"
         with self.assertRaises(TypeError):
             C_not_callable[int]
 
+        class C_is_none(tuple):
+            __class_getitem__ = None
+        with self.assertRaisesRegex(TypeError, "C_is_none"):
+            C_is_none[int]
+
     def test_class_getitem_metaclass(self):
         class Meta(type):
             def __class_getitem__(cls, item):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-01-16-40-07.gh-issue-92114.5xTlLt.rst
new file mode 100644 (file)
index 0000000..a999245
--- /dev/null
@@ -0,0 +1,2 @@
+Improve error message when subscript a type with ``__class_getitem__`` set
+to ``None``.
index cfb0edcab0e5d34e2482f4a5dfe1fc80bb38c6d2..9034737235681872132286496a73b5e21f04bcca 100644 (file)
@@ -185,11 +185,12 @@ PyObject_GetItem(PyObject *o, PyObject *key)
         if (_PyObject_LookupAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) {
             return NULL;
         }
-        if (meth) {
+        if (meth && meth != Py_None) {
             result = PyObject_CallOneArg(meth, key);
             Py_DECREF(meth);
             return result;
         }
+        Py_XDECREF(meth);
         PyErr_Format(PyExc_TypeError, "type '%.200s' is not subscriptable",
                      ((PyTypeObject *)o)->tp_name);
         return NULL;