summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-04-29 16:30:48 (GMT)
committerGitHub <noreply@github.com>2024-04-29 16:30:48 (GMT)
commit444ac0b7a64ff6b6caba9c2731bd33151ce18ad1 (patch)
tree572fd300a05e5a177da821c2afc371e3d2122f4a /Modules
parent51c70de998ead35674bf4b2b236e9ce8e89d17b4 (diff)
downloadcpython-444ac0b7a64ff6b6caba9c2731bd33151ce18ad1.zip
cpython-444ac0b7a64ff6b6caba9c2731bd33151ce18ad1.tar.gz
cpython-444ac0b7a64ff6b6caba9c2731bd33151ce18ad1.tar.bz2
gh-118285: Fix signatures of operator.{attrgetter,itemgetter,methodcaller} instances (GH-118316)
* Allow to specify the signature of custom callable instances of extension type by the __text_signature__ attribute. * Specify signatures of operator.attrgetter, operator.itemgetter, and operator.methodcaller instances.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_operator.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Modules/_operator.c b/Modules/_operator.c
index 1f6496d..306d450 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -966,6 +966,18 @@ static struct PyMethodDef operator_methods[] = {
};
+
+static PyObject *
+text_signature(PyObject *self, void *Py_UNUSED(ignored))
+{
+ return PyUnicode_FromString("(obj, /)");
+}
+
+static PyGetSetDef common_getset[] = {
+ {"__text_signature__", text_signature, (setter)NULL},
+ {NULL}
+};
+
/* itemgetter object **********************************************************/
typedef struct {
@@ -1171,6 +1183,7 @@ static PyType_Slot itemgetter_type_slots[] = {
{Py_tp_clear, itemgetter_clear},
{Py_tp_methods, itemgetter_methods},
{Py_tp_members, itemgetter_members},
+ {Py_tp_getset, common_getset},
{Py_tp_new, itemgetter_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_repr, itemgetter_repr},
@@ -1528,6 +1541,7 @@ static PyType_Slot attrgetter_type_slots[] = {
{Py_tp_clear, attrgetter_clear},
{Py_tp_methods, attrgetter_methods},
{Py_tp_members, attrgetter_members},
+ {Py_tp_getset, common_getset},
{Py_tp_new, attrgetter_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_repr, attrgetter_repr},
@@ -1863,6 +1877,7 @@ static PyType_Slot methodcaller_type_slots[] = {
{Py_tp_clear, methodcaller_clear},
{Py_tp_methods, methodcaller_methods},
{Py_tp_members, methodcaller_members},
+ {Py_tp_getset, common_getset},
{Py_tp_new, methodcaller_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_repr, methodcaller_repr},