summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorJeroen Demeyer <J.Demeyer@UGent.be>2019-05-28 12:42:53 (GMT)
committerPetr Viktorin <encukou@gmail.com>2019-05-28 12:42:53 (GMT)
commiteb65e2443ac21739baf6d373abc7b4638b9d6927 (patch)
tree2197fa4322a60cbe077dfb8c03e0287cd3baabd9 /Objects
parent0811f2d81a12a3415dc2cb2744b41520c48d4db5 (diff)
downloadcpython-eb65e2443ac21739baf6d373abc7b4638b9d6927.zip
cpython-eb65e2443ac21739baf6d373abc7b4638b9d6927.tar.gz
cpython-eb65e2443ac21739baf6d373abc7b4638b9d6927.tar.bz2
bpo-36922: implement PEP-590 Py_TPFLAGS_METHOD_DESCRIPTOR (GH-13338)
Co-authored-by: Mark Shannon <mark@hotpy.org>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/descrobject.c6
-rw-r--r--Objects/funcobject.c3
-rw-r--r--Objects/object.c3
-rw-r--r--Objects/typeobject.c11
4 files changed, 17 insertions, 6 deletions
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 0db8057..6c99f9b 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -556,7 +556,8 @@ PyTypeObject PyMethodDescr_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
0, /* tp_doc */
descr_traverse, /* tp_traverse */
0, /* tp_clear */
@@ -705,7 +706,8 @@ PyTypeObject PyWrapperDescr_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
0, /* tp_doc */
descr_traverse, /* tp_traverse */
0, /* tp_clear */
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 09b94c2..fb7abfa 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -663,7 +663,8 @@ PyTypeObject PyFunction_Type = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
func_new__doc__, /* tp_doc */
(traverseproc)func_traverse, /* tp_traverse */
(inquiry)func_clear, /* tp_clear */
diff --git a/Objects/object.c b/Objects/object.c
index 270716f..87dba98 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1155,8 +1155,7 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
descr = _PyType_Lookup(tp, name);
if (descr != NULL) {
Py_INCREF(descr);
- if (PyFunction_Check(descr) ||
- (Py_TYPE(descr) == &PyMethodDescr_Type)) {
+ if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
meth_found = 1;
} else {
f = descr->ob_type->tp_descr_get;
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index fc809d3..06e045b 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4950,7 +4950,7 @@ static void
inherit_special(PyTypeObject *type, PyTypeObject *base)
{
- /* Copying basicsize is connected to the GC flags */
+ /* Copying tp_traverse and tp_clear is connected to the GC flags */
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
(base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
(!type->tp_traverse && !type->tp_clear)) {
@@ -5165,6 +5165,15 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
}
{
COPYSLOT(tp_descr_get);
+ /* Inherit Py_TPFLAGS_METHOD_DESCRIPTOR if tp_descr_get was inherited,
+ * but only for extension types */
+ if (base->tp_descr_get &&
+ type->tp_descr_get == base->tp_descr_get &&
+ !(type->tp_flags & Py_TPFLAGS_HEAPTYPE) &&
+ (base->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR))
+ {
+ type->tp_flags |= Py_TPFLAGS_METHOD_DESCRIPTOR;
+ }
COPYSLOT(tp_descr_set);
COPYSLOT(tp_dictoffset);
COPYSLOT(tp_init);