@@ -83,6 +83,18 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start)
83
83
return (PyObject * )en ;
84
84
}
85
85
86
+ static int check_keyword (PyObject * kwnames , int index ,
87
+ const char * name )
88
+ {
89
+ PyObject * kw = PyTuple_GET_ITEM (kwnames , index );
90
+ if (!_PyUnicode_EqualToASCIIString (kw , name )) {
91
+ PyErr_Format (PyExc_TypeError ,
92
+ "'%S' is an invalid keyword argument for enumerate()" , kw );
93
+ return 0 ;
94
+ }
95
+ return 1 ;
96
+ }
97
+
86
98
// TODO: Use AC when bpo-43447 is supported
87
99
static PyObject *
88
100
enumerate_vectorcall (PyObject * type , PyObject * const * args ,
@@ -91,31 +103,46 @@ enumerate_vectorcall(PyObject *type, PyObject *const *args,
91
103
PyTypeObject * tp = _PyType_CAST (type );
92
104
Py_ssize_t nargs = PyVectorcall_NARGS (nargsf );
93
105
Py_ssize_t nkwargs = 0 ;
94
- if (nargs == 0 ) {
95
- PyErr_SetString (PyExc_TypeError ,
96
- "enumerate() missing required argument 'iterable'" );
97
- return NULL ;
98
- }
99
106
if (kwnames != NULL ) {
100
107
nkwargs = PyTuple_GET_SIZE (kwnames );
101
108
}
102
109
110
+ // Manually implement enumerate(iterable, start=...)
103
111
if (nargs + nkwargs == 2 ) {
104
112
if (nkwargs == 1 ) {
105
- PyObject * kw = PyTuple_GET_ITEM (kwnames , 0 );
106
- if (!_PyUnicode_EqualToASCIIString (kw , "start" )) {
107
- PyErr_Format (PyExc_TypeError ,
108
- "'%S' is an invalid keyword argument for enumerate()" , kw );
113
+ if (!check_keyword (kwnames , 0 , "start" )) {
114
+ return NULL ;
115
+ }
116
+ } else if (nkwargs == 2 ) {
117
+ PyObject * kw0 = PyTuple_GET_ITEM (kwnames , 0 );
118
+ if (_PyUnicode_EqualToASCIIString (kw0 , "start" )) {
119
+ if (!check_keyword (kwnames , 1 , "iterable" )) {
120
+ return NULL ;
121
+ }
122
+ return enum_new_impl (tp , args [1 ], args [0 ]);
123
+ }
124
+ if (!check_keyword (kwnames , 0 , "iterable" ) ||
125
+ !check_keyword (kwnames , 1 , "start" )) {
109
126
return NULL ;
110
127
}
128
+
111
129
}
112
130
return enum_new_impl (tp , args [0 ], args [1 ]);
113
131
}
114
132
115
- if (nargs == 1 && nkwargs == 0 ) {
133
+ if (nargs + nkwargs == 1 ) {
134
+ if (nkwargs == 1 && !check_keyword (kwnames , 0 , "iterable" )) {
135
+ return NULL ;
136
+ }
116
137
return enum_new_impl (tp , args [0 ], NULL );
117
138
}
118
139
140
+ if (nargs == 0 ) {
141
+ PyErr_SetString (PyExc_TypeError ,
142
+ "enumerate() missing required argument 'iterable'" );
143
+ return NULL ;
144
+ }
145
+
119
146
PyErr_Format (PyExc_TypeError ,
120
147
"enumerate() takes at most 2 arguments (%d given)" , nargs + nkwargs );
121
148
return NULL ;
0 commit comments