Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add vectorcall for attrgetter objects
  • Loading branch information
sweeneyde committed Aug 19, 2021
commit 4033d273ce51a70ecaac95429fc0a6f2b1a89efd
49 changes: 41 additions & 8 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,6 @@ Return a callable object that fetches the given item(s) from its operand.\n\
After f = itemgetter(2), the call f(r) returns r[2].\n\
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");


static PyType_Slot itemgetter_type_slots[] = {
{Py_tp_doc, (void *)itemgetter_doc},
{Py_tp_dealloc, itemgetter_dealloc},
Expand Down Expand Up @@ -1177,8 +1176,15 @@ typedef struct {
PyObject_HEAD
Py_ssize_t nattrs;
PyObject *attr;
vectorcallfunc vectorcall;
} attrgetterobject;

// Forward declarations
static PyObject *
attrgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *);
static PyObject *
attrgetter_call_impl(attrgetterobject *, PyObject *);

/* AC 3.5: treats first argument as an iterable, otherwise uses *args */
static PyObject *
attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Expand Down Expand Up @@ -1222,7 +1228,7 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
kind = PyUnicode_KIND(item);
data = PyUnicode_DATA(item);

/* check whethere the string is dotted */
/* check whether the string is dotted */
dot_count = 0;
for (char_idx = 0; char_idx < item_len; ++char_idx) {
if (PyUnicode_READ(kind, data, char_idx) == '.')
Expand Down Expand Up @@ -1288,6 +1294,7 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

ag->attr = attr;
ag->nattrs = nattrs;
ag->vectorcall = (vectorcallfunc)attrgetter_vectorcall;

PyObject_GC_Track(ag);
return (PyObject *)ag;
Expand Down Expand Up @@ -1354,16 +1361,36 @@ dotted_getattr(PyObject *obj, PyObject *attr)
static PyObject *
attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
{
PyObject *obj, *result;
Py_ssize_t i, nattrs=ag->nattrs;

if (!_PyArg_NoKeywords("attrgetter", kw))
return NULL;
if (!_PyArg_CheckPositional("attrgetter", PyTuple_GET_SIZE(args), 1, 1))
return NULL;
obj = PyTuple_GET_ITEM(args, 0);
if (ag->nattrs == 1) /* ag->attr is always a tuple */
return attrgetter_call_impl(ag, PyTuple_GET_ITEM(args, 0));
}

static PyObject *
attrgetter_vectorcall(PyObject *ag, PyObject *const *args, size_t nargsf, PyObject *kwnames)
{
if (!_PyArg_NoKwnames("attrgetter", kwnames)) {
return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("attrgetter", nargs, 1, 1)) {
return NULL;
}
return attrgetter_call_impl((attrgetterobject *)ag, args[0]);
}

static PyObject *
attrgetter_call_impl(attrgetterobject *ag, PyObject *obj)
{
PyObject *result;
Py_ssize_t i, nattrs=ag->nattrs;

if (ag->nattrs == 1) {
/* ag->attr is always a tuple */
return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
}

assert(PyTuple_Check(ag->attr));
assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Expand Down Expand Up @@ -1472,6 +1499,11 @@ static PyMethodDef attrgetter_methods[] = {
{NULL}
};

static PyMemberDef attrgetter_members[] = {
{"__vectorcalloffset__", T_PYSSIZET, offsetof(attrgetterobject, vectorcall), READONLY},
Comment thread
methane marked this conversation as resolved.
{NULL} /* Sentinel*/
};

PyDoc_STRVAR(attrgetter_doc,
"attrgetter(attr, ...) --> attrgetter object\n\
\n\
Expand All @@ -1488,6 +1520,7 @@ static PyType_Slot attrgetter_type_slots[] = {
{Py_tp_traverse, attrgetter_traverse},
{Py_tp_clear, attrgetter_clear},
{Py_tp_methods, attrgetter_methods},
{Py_tp_members, attrgetter_members},
{Py_tp_new, attrgetter_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_repr, attrgetter_repr},
Expand All @@ -1499,7 +1532,7 @@ static PyType_Spec attrgetter_type_spec = {
.basicsize = sizeof(attrgetterobject),
.itemsize = 0,
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_VECTORCALL),
.slots = attrgetter_type_slots,
};

Expand Down