1 //////////////////// ArgTypeTest.proto ////////////////////
2
3
4 #define __Pyx_ArgTypeTest(obj, type, none_allowed, name, exact) \
5 ((likely((Py_TYPE(obj) == type) | (none_allowed && (obj == Py_None)))) ? 1 : \
6 __Pyx__ArgTypeTest(obj, type, name, exact))
7
8 static int __Pyx__ArgTypeTest(PyObject *obj, PyTypeObject *type, const char *name, int exact); /*proto*/
9
10 //////////////////// ArgTypeTest ////////////////////
11
__Pyx__ArgTypeTest(PyObject * obj,PyTypeObject * type,const char * name,int exact)12 static int __Pyx__ArgTypeTest(PyObject *obj, PyTypeObject *type, const char *name, int exact)
13 {
14 if (unlikely(!type)) {
15 PyErr_SetString(PyExc_SystemError, "Missing type object");
16 return 0;
17 }
18 else if (exact) {
19 #if PY_MAJOR_VERSION == 2
20 if ((type == &PyBaseString_Type) && likely(__Pyx_PyBaseString_CheckExact(obj))) return 1;
21 #endif
22 }
23 else {
24 if (likely(__Pyx_TypeCheck(obj, type))) return 1;
25 }
26 PyErr_Format(PyExc_TypeError,
27 "Argument '%.200s' has incorrect type (expected %.200s, got %.200s)",
28 name, type->tp_name, Py_TYPE(obj)->tp_name);
29 return 0;
30 }
31
32 //////////////////// RaiseArgTupleInvalid.proto ////////////////////
33
34 static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
35 Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
36
37 //////////////////// RaiseArgTupleInvalid ////////////////////
38
39 // __Pyx_RaiseArgtupleInvalid raises the correct exception when too
40 // many or too few positional arguments were found. This handles
41 // Py_ssize_t formatting correctly.
42
__Pyx_RaiseArgtupleInvalid(const char * func_name,int exact,Py_ssize_t num_min,Py_ssize_t num_max,Py_ssize_t num_found)43 static void __Pyx_RaiseArgtupleInvalid(
44 const char* func_name,
45 int exact,
46 Py_ssize_t num_min,
47 Py_ssize_t num_max,
48 Py_ssize_t num_found)
49 {
50 Py_ssize_t num_expected;
51 const char *more_or_less;
52
53 if (num_found < num_min) {
54 num_expected = num_min;
55 more_or_less = "at least";
56 } else {
57 num_expected = num_max;
58 more_or_less = "at most";
59 }
60 if (exact) {
61 more_or_less = "exactly";
62 }
63 PyErr_Format(PyExc_TypeError,
64 "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)",
65 func_name, more_or_less, num_expected,
66 (num_expected == 1) ? "" : "s", num_found);
67 }
68
69
70 //////////////////// RaiseKeywordRequired.proto ////////////////////
71
72 static void __Pyx_RaiseKeywordRequired(const char* func_name, PyObject* kw_name); /*proto*/
73
74 //////////////////// RaiseKeywordRequired ////////////////////
75
__Pyx_RaiseKeywordRequired(const char * func_name,PyObject * kw_name)76 static void __Pyx_RaiseKeywordRequired(const char* func_name, PyObject* kw_name) {
77 PyErr_Format(PyExc_TypeError,
78 #if PY_MAJOR_VERSION >= 3
79 "%s() needs keyword-only argument %U", func_name, kw_name);
80 #else
81 "%s() needs keyword-only argument %s", func_name,
82 PyString_AS_STRING(kw_name));
83 #endif
84 }
85
86
87 //////////////////// RaiseDoubleKeywords.proto ////////////////////
88
89 static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/
90
91 //////////////////// RaiseDoubleKeywords ////////////////////
92
__Pyx_RaiseDoubleKeywordsError(const char * func_name,PyObject * kw_name)93 static void __Pyx_RaiseDoubleKeywordsError(
94 const char* func_name,
95 PyObject* kw_name)
96 {
97 PyErr_Format(PyExc_TypeError,
98 #if PY_MAJOR_VERSION >= 3
99 "%s() got multiple values for keyword argument '%U'", func_name, kw_name);
100 #else
101 "%s() got multiple values for keyword argument '%s'", func_name,
102 PyString_AsString(kw_name));
103 #endif
104 }
105
106
107 //////////////////// RaiseMappingExpected.proto ////////////////////
108
109 static void __Pyx_RaiseMappingExpectedError(PyObject* arg); /*proto*/
110
111 //////////////////// RaiseMappingExpected ////////////////////
112
__Pyx_RaiseMappingExpectedError(PyObject * arg)113 static void __Pyx_RaiseMappingExpectedError(PyObject* arg) {
114 PyErr_Format(PyExc_TypeError, "'%.200s' object is not a mapping", Py_TYPE(arg)->tp_name);
115 }
116
117
118 //////////////////// KeywordStringCheck.proto ////////////////////
119
120 static int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); /*proto*/
121
122 //////////////////// KeywordStringCheck ////////////////////
123
124 // __Pyx_CheckKeywordStrings raises an error if non-string keywords
125 // were passed to a function, or if any keywords were passed to a
126 // function that does not accept them.
127
__Pyx_CheckKeywordStrings(PyObject * kwdict,const char * function_name,int kw_allowed)128 static int __Pyx_CheckKeywordStrings(
129 PyObject *kwdict,
130 const char* function_name,
131 int kw_allowed)
132 {
133 PyObject* key = 0;
134 Py_ssize_t pos = 0;
135 #if CYTHON_COMPILING_IN_PYPY
136 /* PyPy appears to check keywords at call time, not at unpacking time => not much to do here */
137 if (!kw_allowed && PyDict_Next(kwdict, &pos, &key, 0))
138 goto invalid_keyword;
139 return 1;
140 #else
141 while (PyDict_Next(kwdict, &pos, &key, 0)) {
142 #if PY_MAJOR_VERSION < 3
143 if (unlikely(!PyString_Check(key)))
144 #endif
145 if (unlikely(!PyUnicode_Check(key)))
146 goto invalid_keyword_type;
147 }
148 if ((!kw_allowed) && unlikely(key))
149 goto invalid_keyword;
150 return 1;
151 invalid_keyword_type:
152 PyErr_Format(PyExc_TypeError,
153 "%.200s() keywords must be strings", function_name);
154 return 0;
155 #endif
156 invalid_keyword:
157 PyErr_Format(PyExc_TypeError,
158 #if PY_MAJOR_VERSION < 3
159 "%.200s() got an unexpected keyword argument '%.200s'",
160 function_name, PyString_AsString(key));
161 #else
162 "%s() got an unexpected keyword argument '%U'",
163 function_name, key);
164 #endif
165 return 0;
166 }
167
168
169 //////////////////// ParseKeywords.proto ////////////////////
170
171 static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
172 PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
173 const char* function_name); /*proto*/
174
175 //////////////////// ParseKeywords ////////////////////
176 //@requires: RaiseDoubleKeywords
177
178 // __Pyx_ParseOptionalKeywords copies the optional/unknown keyword
179 // arguments from the kwds dict into kwds2. If kwds2 is NULL, unknown
180 // keywords will raise an invalid keyword error.
181 //
182 // Three kinds of errors are checked: 1) non-string keywords, 2)
183 // unexpected keywords and 3) overlap with positional arguments.
184 //
185 // If num_posargs is greater 0, it denotes the number of positional
186 // arguments that were passed and that must therefore not appear
187 // amongst the keywords as well.
188 //
189 // This method does not check for required keyword arguments.
190
__Pyx_ParseOptionalKeywords(PyObject * kwds,PyObject ** argnames[],PyObject * kwds2,PyObject * values[],Py_ssize_t num_pos_args,const char * function_name)191 static int __Pyx_ParseOptionalKeywords(
192 PyObject *kwds,
193 PyObject **argnames[],
194 PyObject *kwds2,
195 PyObject *values[],
196 Py_ssize_t num_pos_args,
197 const char* function_name)
198 {
199 PyObject *key = 0, *value = 0;
200 Py_ssize_t pos = 0;
201 PyObject*** name;
202 PyObject*** first_kw_arg = argnames + num_pos_args;
203
204 while (PyDict_Next(kwds, &pos, &key, &value)) {
205 name = first_kw_arg;
206 while (*name && (**name != key)) name++;
207 if (*name) {
208 values[name-argnames] = value;
209 continue;
210 }
211
212 name = first_kw_arg;
213 #if PY_MAJOR_VERSION < 3
214 if (likely(PyString_Check(key))) {
215 while (*name) {
216 if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))
217 && _PyString_Eq(**name, key)) {
218 values[name-argnames] = value;
219 break;
220 }
221 name++;
222 }
223 if (*name) continue;
224 else {
225 // not found after positional args, check for duplicate
226 PyObject*** argname = argnames;
227 while (argname != first_kw_arg) {
228 if ((**argname == key) || (
229 (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))
230 && _PyString_Eq(**argname, key))) {
231 goto arg_passed_twice;
232 }
233 argname++;
234 }
235 }
236 } else
237 #endif
238 if (likely(PyUnicode_Check(key))) {
239 while (*name) {
240 int cmp = (**name == key) ? 0 :
241 #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
242 (__Pyx_PyUnicode_GET_LENGTH(**name) != __Pyx_PyUnicode_GET_LENGTH(key)) ? 1 :
243 #endif
244 // In Py2, we may need to convert the argument name from str to unicode for comparison.
245 PyUnicode_Compare(**name, key);
246 if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
247 if (cmp == 0) {
248 values[name-argnames] = value;
249 break;
250 }
251 name++;
252 }
253 if (*name) continue;
254 else {
255 // not found after positional args, check for duplicate
256 PyObject*** argname = argnames;
257 while (argname != first_kw_arg) {
258 int cmp = (**argname == key) ? 0 :
259 #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
260 (__Pyx_PyUnicode_GET_LENGTH(**argname) != __Pyx_PyUnicode_GET_LENGTH(key)) ? 1 :
261 #endif
262 // need to convert argument name from bytes to unicode for comparison
263 PyUnicode_Compare(**argname, key);
264 if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
265 if (cmp == 0) goto arg_passed_twice;
266 argname++;
267 }
268 }
269 } else
270 goto invalid_keyword_type;
271
272 if (kwds2) {
273 if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
274 } else {
275 goto invalid_keyword;
276 }
277 }
278 return 0;
279 arg_passed_twice:
280 __Pyx_RaiseDoubleKeywordsError(function_name, key);
281 goto bad;
282 invalid_keyword_type:
283 PyErr_Format(PyExc_TypeError,
284 "%.200s() keywords must be strings", function_name);
285 goto bad;
286 invalid_keyword:
287 PyErr_Format(PyExc_TypeError,
288 #if PY_MAJOR_VERSION < 3
289 "%.200s() got an unexpected keyword argument '%.200s'",
290 function_name, PyString_AsString(key));
291 #else
292 "%s() got an unexpected keyword argument '%U'",
293 function_name, key);
294 #endif
295 bad:
296 return -1;
297 }
298
299
300 //////////////////// MergeKeywords.proto ////////////////////
301
302 static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping); /*proto*/
303
304 //////////////////// MergeKeywords ////////////////////
305 //@requires: RaiseDoubleKeywords
306 //@requires: Optimize.c::dict_iter
307
__Pyx_MergeKeywords(PyObject * kwdict,PyObject * source_mapping)308 static int __Pyx_MergeKeywords(PyObject *kwdict, PyObject *source_mapping) {
309 PyObject *iter, *key = NULL, *value = NULL;
310 int source_is_dict, result;
311 Py_ssize_t orig_length, ppos = 0;
312
313 iter = __Pyx_dict_iterator(source_mapping, 0, PYIDENT("items"), &orig_length, &source_is_dict);
314 if (unlikely(!iter)) {
315 // slow fallback: try converting to dict, then iterate
316 PyObject *args;
317 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
318 PyErr_Clear();
319 args = PyTuple_Pack(1, source_mapping);
320 if (likely(args)) {
321 PyObject *fallback = PyObject_Call((PyObject*)&PyDict_Type, args, NULL);
322 Py_DECREF(args);
323 if (likely(fallback)) {
324 iter = __Pyx_dict_iterator(fallback, 1, PYIDENT("items"), &orig_length, &source_is_dict);
325 Py_DECREF(fallback);
326 }
327 }
328 if (unlikely(!iter)) goto bad;
329 }
330
331 while (1) {
332 result = __Pyx_dict_iter_next(iter, orig_length, &ppos, &key, &value, NULL, source_is_dict);
333 if (unlikely(result < 0)) goto bad;
334 if (!result) break;
335
336 if (unlikely(PyDict_Contains(kwdict, key))) {
337 __Pyx_RaiseDoubleKeywordsError("function", key);
338 result = -1;
339 } else {
340 result = PyDict_SetItem(kwdict, key, value);
341 }
342 Py_DECREF(key);
343 Py_DECREF(value);
344 if (unlikely(result < 0)) goto bad;
345 }
346 Py_XDECREF(iter);
347 return 0;
348
349 bad:
350 Py_XDECREF(iter);
351 return -1;
352 }
353