odictobject.c.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(OrderedDict_fromkeys__doc__,
  9. "fromkeys($type, /, iterable, value=None)\n"
  10. "--\n"
  11. "\n"
  12. "Create a new ordered dictionary with keys from iterable and values set to value.");
  13. #define ORDEREDDICT_FROMKEYS_METHODDEF \
  14. {"fromkeys", _PyCFunction_CAST(OrderedDict_fromkeys), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, OrderedDict_fromkeys__doc__},
  15. static PyObject *
  16. OrderedDict_fromkeys_impl(PyTypeObject *type, PyObject *seq, PyObject *value);
  17. static PyObject *
  18. OrderedDict_fromkeys(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  19. {
  20. PyObject *return_value = NULL;
  21. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  22. #define NUM_KEYWORDS 2
  23. static struct {
  24. PyGC_Head _this_is_not_used;
  25. PyObject_VAR_HEAD
  26. PyObject *ob_item[NUM_KEYWORDS];
  27. } _kwtuple = {
  28. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  29. .ob_item = { &_Py_ID(iterable), &_Py_ID(value), },
  30. };
  31. #undef NUM_KEYWORDS
  32. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  33. #else // !Py_BUILD_CORE
  34. # define KWTUPLE NULL
  35. #endif // !Py_BUILD_CORE
  36. static const char * const _keywords[] = {"iterable", "value", NULL};
  37. static _PyArg_Parser _parser = {
  38. .keywords = _keywords,
  39. .fname = "fromkeys",
  40. .kwtuple = KWTUPLE,
  41. };
  42. #undef KWTUPLE
  43. PyObject *argsbuf[2];
  44. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  45. PyObject *seq;
  46. PyObject *value = Py_None;
  47. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  48. if (!args) {
  49. goto exit;
  50. }
  51. seq = args[0];
  52. if (!noptargs) {
  53. goto skip_optional_pos;
  54. }
  55. value = args[1];
  56. skip_optional_pos:
  57. return_value = OrderedDict_fromkeys_impl(type, seq, value);
  58. exit:
  59. return return_value;
  60. }
  61. PyDoc_STRVAR(OrderedDict_setdefault__doc__,
  62. "setdefault($self, /, key, default=None)\n"
  63. "--\n"
  64. "\n"
  65. "Insert key with a value of default if key is not in the dictionary.\n"
  66. "\n"
  67. "Return the value for key if key is in the dictionary, else default.");
  68. #define ORDEREDDICT_SETDEFAULT_METHODDEF \
  69. {"setdefault", _PyCFunction_CAST(OrderedDict_setdefault), METH_FASTCALL|METH_KEYWORDS, OrderedDict_setdefault__doc__},
  70. static PyObject *
  71. OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
  72. PyObject *default_value);
  73. static PyObject *
  74. OrderedDict_setdefault(PyODictObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  75. {
  76. PyObject *return_value = NULL;
  77. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  78. #define NUM_KEYWORDS 2
  79. static struct {
  80. PyGC_Head _this_is_not_used;
  81. PyObject_VAR_HEAD
  82. PyObject *ob_item[NUM_KEYWORDS];
  83. } _kwtuple = {
  84. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  85. .ob_item = { &_Py_ID(key), &_Py_ID(default), },
  86. };
  87. #undef NUM_KEYWORDS
  88. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  89. #else // !Py_BUILD_CORE
  90. # define KWTUPLE NULL
  91. #endif // !Py_BUILD_CORE
  92. static const char * const _keywords[] = {"key", "default", NULL};
  93. static _PyArg_Parser _parser = {
  94. .keywords = _keywords,
  95. .fname = "setdefault",
  96. .kwtuple = KWTUPLE,
  97. };
  98. #undef KWTUPLE
  99. PyObject *argsbuf[2];
  100. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  101. PyObject *key;
  102. PyObject *default_value = Py_None;
  103. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  104. if (!args) {
  105. goto exit;
  106. }
  107. key = args[0];
  108. if (!noptargs) {
  109. goto skip_optional_pos;
  110. }
  111. default_value = args[1];
  112. skip_optional_pos:
  113. return_value = OrderedDict_setdefault_impl(self, key, default_value);
  114. exit:
  115. return return_value;
  116. }
  117. PyDoc_STRVAR(OrderedDict_pop__doc__,
  118. "pop($self, /, key, default=<unrepresentable>)\n"
  119. "--\n"
  120. "\n"
  121. "od.pop(key[,default]) -> v, remove specified key and return the corresponding value.\n"
  122. "\n"
  123. "If the key is not found, return the default if given; otherwise,\n"
  124. "raise a KeyError.");
  125. #define ORDEREDDICT_POP_METHODDEF \
  126. {"pop", _PyCFunction_CAST(OrderedDict_pop), METH_FASTCALL|METH_KEYWORDS, OrderedDict_pop__doc__},
  127. static PyObject *
  128. OrderedDict_pop_impl(PyODictObject *self, PyObject *key,
  129. PyObject *default_value);
  130. static PyObject *
  131. OrderedDict_pop(PyODictObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  132. {
  133. PyObject *return_value = NULL;
  134. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  135. #define NUM_KEYWORDS 2
  136. static struct {
  137. PyGC_Head _this_is_not_used;
  138. PyObject_VAR_HEAD
  139. PyObject *ob_item[NUM_KEYWORDS];
  140. } _kwtuple = {
  141. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  142. .ob_item = { &_Py_ID(key), &_Py_ID(default), },
  143. };
  144. #undef NUM_KEYWORDS
  145. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  146. #else // !Py_BUILD_CORE
  147. # define KWTUPLE NULL
  148. #endif // !Py_BUILD_CORE
  149. static const char * const _keywords[] = {"key", "default", NULL};
  150. static _PyArg_Parser _parser = {
  151. .keywords = _keywords,
  152. .fname = "pop",
  153. .kwtuple = KWTUPLE,
  154. };
  155. #undef KWTUPLE
  156. PyObject *argsbuf[2];
  157. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  158. PyObject *key;
  159. PyObject *default_value = NULL;
  160. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  161. if (!args) {
  162. goto exit;
  163. }
  164. key = args[0];
  165. if (!noptargs) {
  166. goto skip_optional_pos;
  167. }
  168. default_value = args[1];
  169. skip_optional_pos:
  170. return_value = OrderedDict_pop_impl(self, key, default_value);
  171. exit:
  172. return return_value;
  173. }
  174. PyDoc_STRVAR(OrderedDict_popitem__doc__,
  175. "popitem($self, /, last=True)\n"
  176. "--\n"
  177. "\n"
  178. "Remove and return a (key, value) pair from the dictionary.\n"
  179. "\n"
  180. "Pairs are returned in LIFO order if last is true or FIFO order if false.");
  181. #define ORDEREDDICT_POPITEM_METHODDEF \
  182. {"popitem", _PyCFunction_CAST(OrderedDict_popitem), METH_FASTCALL|METH_KEYWORDS, OrderedDict_popitem__doc__},
  183. static PyObject *
  184. OrderedDict_popitem_impl(PyODictObject *self, int last);
  185. static PyObject *
  186. OrderedDict_popitem(PyODictObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  187. {
  188. PyObject *return_value = NULL;
  189. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  190. #define NUM_KEYWORDS 1
  191. static struct {
  192. PyGC_Head _this_is_not_used;
  193. PyObject_VAR_HEAD
  194. PyObject *ob_item[NUM_KEYWORDS];
  195. } _kwtuple = {
  196. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  197. .ob_item = { &_Py_ID(last), },
  198. };
  199. #undef NUM_KEYWORDS
  200. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  201. #else // !Py_BUILD_CORE
  202. # define KWTUPLE NULL
  203. #endif // !Py_BUILD_CORE
  204. static const char * const _keywords[] = {"last", NULL};
  205. static _PyArg_Parser _parser = {
  206. .keywords = _keywords,
  207. .fname = "popitem",
  208. .kwtuple = KWTUPLE,
  209. };
  210. #undef KWTUPLE
  211. PyObject *argsbuf[1];
  212. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  213. int last = 1;
  214. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  215. if (!args) {
  216. goto exit;
  217. }
  218. if (!noptargs) {
  219. goto skip_optional_pos;
  220. }
  221. last = PyObject_IsTrue(args[0]);
  222. if (last < 0) {
  223. goto exit;
  224. }
  225. skip_optional_pos:
  226. return_value = OrderedDict_popitem_impl(self, last);
  227. exit:
  228. return return_value;
  229. }
  230. PyDoc_STRVAR(OrderedDict_move_to_end__doc__,
  231. "move_to_end($self, /, key, last=True)\n"
  232. "--\n"
  233. "\n"
  234. "Move an existing element to the end (or beginning if last is false).\n"
  235. "\n"
  236. "Raise KeyError if the element does not exist.");
  237. #define ORDEREDDICT_MOVE_TO_END_METHODDEF \
  238. {"move_to_end", _PyCFunction_CAST(OrderedDict_move_to_end), METH_FASTCALL|METH_KEYWORDS, OrderedDict_move_to_end__doc__},
  239. static PyObject *
  240. OrderedDict_move_to_end_impl(PyODictObject *self, PyObject *key, int last);
  241. static PyObject *
  242. OrderedDict_move_to_end(PyODictObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  243. {
  244. PyObject *return_value = NULL;
  245. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  246. #define NUM_KEYWORDS 2
  247. static struct {
  248. PyGC_Head _this_is_not_used;
  249. PyObject_VAR_HEAD
  250. PyObject *ob_item[NUM_KEYWORDS];
  251. } _kwtuple = {
  252. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  253. .ob_item = { &_Py_ID(key), &_Py_ID(last), },
  254. };
  255. #undef NUM_KEYWORDS
  256. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  257. #else // !Py_BUILD_CORE
  258. # define KWTUPLE NULL
  259. #endif // !Py_BUILD_CORE
  260. static const char * const _keywords[] = {"key", "last", NULL};
  261. static _PyArg_Parser _parser = {
  262. .keywords = _keywords,
  263. .fname = "move_to_end",
  264. .kwtuple = KWTUPLE,
  265. };
  266. #undef KWTUPLE
  267. PyObject *argsbuf[2];
  268. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  269. PyObject *key;
  270. int last = 1;
  271. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  272. if (!args) {
  273. goto exit;
  274. }
  275. key = args[0];
  276. if (!noptargs) {
  277. goto skip_optional_pos;
  278. }
  279. last = PyObject_IsTrue(args[1]);
  280. if (last < 0) {
  281. goto exit;
  282. }
  283. skip_optional_pos:
  284. return_value = OrderedDict_move_to_end_impl(self, key, last);
  285. exit:
  286. return return_value;
  287. }
  288. /*[clinic end generated code: output=76d85a9162d62ca8 input=a9049054013a1b77]*/