FunctionArguments.c 12 KB

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