@@ -28,6 +28,16 @@ module _operator
2828[clinic start generated code]*/
2929/*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/
3030
31+ /*[clinic input]
32+ class _operator.itemgetter "itemgetterobject *" "type"
33+ [clinic start generated code]*/
34+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=419d278df02ea948]*/
35+
36+ /*[clinic input]
37+ class _operator.attrgetter "attrgetterobject *" "type"
38+ [clinic start generated code]*/
39+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6efaf5380908852]*/
40+
3141PyDoc_STRVAR (operator_doc ,
3242"Operator interface.\n\
3343\n\
@@ -1029,34 +1039,45 @@ itemgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *);
10291039static PyObject *
10301040itemgetter_call_impl (itemgetterobject * , PyObject * );
10311041
1032- /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
1042+ /*[clinic input]
1043+ @vectorcall
1044+ @classmethod
1045+ _operator.itemgetter.__new__ as itemgetter_type_new
1046+ *items: array
1047+ [clinic start generated code]*/
1048+
10331049static PyObject *
1034- itemgetter_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1050+ itemgetter_type_new_impl (PyTypeObject * type , PyObject * const * items ,
1051+ Py_ssize_t items_length )
1052+ /*[clinic end generated code: output=b449203c8dbfacf8 input=af4aec330543c134]*/
10351053{
10361054 itemgetterobject * ig ;
10371055 PyObject * item ;
1038- Py_ssize_t nitems ;
10391056 Py_ssize_t index ;
10401057
1041- if (!_PyArg_NoKeywords ("itemgetter" , kwds ))
1058+ if (items_length < 1 ) {
1059+ PyErr_SetString (PyExc_TypeError , "itemgetter expected 1 argument, got 0" );
10421060 return NULL ;
1043-
1044- nitems = PyTuple_GET_SIZE (args );
1045- if (nitems <= 1 ) {
1046- if (!PyArg_UnpackTuple (args , "itemgetter" , 1 , 1 , & item ))
1061+ }
1062+ if (items_length == 1 ) {
1063+ item = Py_NewRef (items [0 ]);
1064+ }
1065+ else {
1066+ item = PyTuple_FromArray (items , items_length );
1067+ if (item == NULL ) {
10471068 return NULL ;
1048- } else {
1049- item = args ;
1069+ }
10501070 }
10511071 _operator_state * state = _PyType_GetModuleState (type );
10521072 /* create itemgetterobject structure */
10531073 ig = PyObject_GC_New (itemgetterobject , (PyTypeObject * ) state -> itemgetter_type );
10541074 if (ig == NULL ) {
1075+ Py_DECREF (item );
10551076 return NULL ;
10561077 }
10571078
1058- ig -> item = Py_NewRef ( item ) ;
1059- ig -> nitems = nitems ;
1079+ ig -> item = item ;
1080+ ig -> nitems = items_length ;
10601081 ig -> index = -1 ;
10611082 if (PyLong_CheckExact (item )) {
10621083 index = PyLong_AsSsize_t (item );
@@ -1225,7 +1246,8 @@ static PyType_Slot itemgetter_type_slots[] = {
12251246 {Py_tp_methods , itemgetter_methods },
12261247 {Py_tp_members , itemgetter_members },
12271248 {Py_tp_getset , common_getset },
1228- {Py_tp_new , itemgetter_new },
1249+ {Py_tp_new , itemgetter_type_new },
1250+ {Py_tp_vectorcall , itemgetter_type_vectorcall },
12291251 {Py_tp_getattro , PyObject_GenericGetAttr },
12301252 {Py_tp_repr , itemgetter_repr },
12311253 {0 , 0 }
@@ -1257,31 +1279,35 @@ attrgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *);
12571279static PyObject *
12581280attrgetter_call_impl (attrgetterobject * , PyObject * );
12591281
1260- /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
1282+ /*[clinic input]
1283+ @vectorcall
1284+ @classmethod
1285+ _operator.attrgetter.__new__ as attrgetter_type_new
1286+ *attrs: array
1287+ [clinic start generated code]*/
1288+
12611289static PyObject *
1262- attrgetter_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1290+ attrgetter_type_new_impl (PyTypeObject * type , PyObject * const * attrs ,
1291+ Py_ssize_t attrs_length )
1292+ /*[clinic end generated code: output=5d014e36ea7aba23 input=b3a21a0100fbeba8]*/
12631293{
12641294 attrgetterobject * ag ;
12651295 PyObject * attr ;
1266- Py_ssize_t nattrs , idx , char_idx ;
1296+ Py_ssize_t idx , char_idx ;
12671297
1268- if (!_PyArg_NoKeywords ("attrgetter" , kwds ))
1298+ if (attrs_length < 1 ) {
1299+ PyErr_SetString (PyExc_TypeError , "attrgetter expected 1 argument, got 0" );
12691300 return NULL ;
1270-
1271- nattrs = PyTuple_GET_SIZE (args );
1272- if (nattrs <= 1 ) {
1273- if (!PyArg_UnpackTuple (args , "attrgetter" , 1 , 1 , & attr ))
1274- return NULL ;
12751301 }
12761302
1277- attr = PyTuple_New (nattrs );
1303+ attr = PyTuple_New (attrs_length );
12781304 if (attr == NULL )
12791305 return NULL ;
12801306
12811307 /* prepare attr while checking args */
12821308 PyInterpreterState * interp = _PyInterpreterState_GET ();
1283- for (idx = 0 ; idx < nattrs ; ++ idx ) {
1284- PyObject * item = PyTuple_GET_ITEM ( args , idx ) ;
1309+ for (idx = 0 ; idx < attrs_length ; ++ idx ) {
1310+ PyObject * item = attrs [ idx ] ;
12851311 int dot_count ;
12861312
12871313 if (!PyUnicode_Check (item )) {
@@ -1359,7 +1385,7 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13591385 }
13601386
13611387 ag -> attr = attr ;
1362- ag -> nattrs = nattrs ;
1388+ ag -> nattrs = attrs_length ;
13631389 ag -> vectorcall = attrgetter_vectorcall ;
13641390
13651391 PyObject_GC_Track (ag );
@@ -1592,7 +1618,8 @@ static PyType_Slot attrgetter_type_slots[] = {
15921618 {Py_tp_methods , attrgetter_methods },
15931619 {Py_tp_members , attrgetter_members },
15941620 {Py_tp_getset , common_getset },
1595- {Py_tp_new , attrgetter_new },
1621+ {Py_tp_new , attrgetter_type_new },
1622+ {Py_tp_vectorcall , attrgetter_type_vectorcall },
15961623 {Py_tp_getattro , PyObject_GenericGetAttr },
15971624 {Py_tp_repr , attrgetter_repr },
15981625 {0 , 0 }
0 commit comments