]> granicus.if.org Git - python/commitdiff
bpo-36974: add some assertions for PEP 590 (GH-13682)
authorJeroen Demeyer <J.Demeyer@UGent.be>
Sun, 2 Jun 2019 23:57:22 +0000 (01:57 +0200)
committerPetr Viktorin <encukou@gmail.com>
Sun, 2 Jun 2019 23:57:22 +0000 (01:57 +0200)
Objects/typeobject.c

index beb0ddd82400145f171b7a963caa7ae7b512d180..006df8d1f090d5c395b245deb1b8219406e5b465 100644 (file)
@@ -5233,6 +5233,20 @@ PyType_Ready(PyTypeObject *type)
     _PyObject_ASSERT((PyObject *)type,
                      (type->tp_flags & Py_TPFLAGS_READYING) == 0);
 
+    /* Consistency checks for PEP 590:
+     * - Py_TPFLAGS_METHOD_DESCRIPTOR requires tp_descr_get
+     * - _Py_TPFLAGS_HAVE_VECTORCALL requires tp_call and
+     *   tp_vectorcall_offset > 0
+     * To avoid mistakes, we require this before inheriting.
+     */
+    if (type->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR) {
+        _PyObject_ASSERT((PyObject *)type, type->tp_descr_get != NULL);
+    }
+    if (type->tp_flags & _Py_TPFLAGS_HAVE_VECTORCALL) {
+        _PyObject_ASSERT((PyObject *)type, type->tp_vectorcall_offset > 0);
+        _PyObject_ASSERT((PyObject *)type, type->tp_call != NULL);
+    }
+
     type->tp_flags |= Py_TPFLAGS_READYING;
 
 #ifdef Py_TRACE_REFS