dictobject.c.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(dict_fromkeys__doc__,
  9. "fromkeys($type, iterable, value=None, /)\n"
  10. "--\n"
  11. "\n"
  12. "Create a new dictionary with keys from iterable and values set to value.");
  13. #define DICT_FROMKEYS_METHODDEF \
  14. {"fromkeys", _PyCFunction_CAST(dict_fromkeys), METH_FASTCALL|METH_CLASS, dict_fromkeys__doc__},
  15. static PyObject *
  16. dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value);
  17. static PyObject *
  18. dict_fromkeys(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
  19. {
  20. PyObject *return_value = NULL;
  21. PyObject *iterable;
  22. PyObject *value = Py_None;
  23. if (!_PyArg_CheckPositional("fromkeys", nargs, 1, 2)) {
  24. goto exit;
  25. }
  26. iterable = args[0];
  27. if (nargs < 2) {
  28. goto skip_optional;
  29. }
  30. value = args[1];
  31. skip_optional:
  32. return_value = dict_fromkeys_impl(type, iterable, value);
  33. exit:
  34. return return_value;
  35. }
  36. PyDoc_STRVAR(dict___contains____doc__,
  37. "__contains__($self, key, /)\n"
  38. "--\n"
  39. "\n"
  40. "True if the dictionary has the specified key, else False.");
  41. #define DICT___CONTAINS___METHODDEF \
  42. {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
  43. PyDoc_STRVAR(dict_get__doc__,
  44. "get($self, key, default=None, /)\n"
  45. "--\n"
  46. "\n"
  47. "Return the value for key if key is in the dictionary, else default.");
  48. #define DICT_GET_METHODDEF \
  49. {"get", _PyCFunction_CAST(dict_get), METH_FASTCALL, dict_get__doc__},
  50. static PyObject *
  51. dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value);
  52. static PyObject *
  53. dict_get(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
  54. {
  55. PyObject *return_value = NULL;
  56. PyObject *key;
  57. PyObject *default_value = Py_None;
  58. if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
  59. goto exit;
  60. }
  61. key = args[0];
  62. if (nargs < 2) {
  63. goto skip_optional;
  64. }
  65. default_value = args[1];
  66. skip_optional:
  67. return_value = dict_get_impl(self, key, default_value);
  68. exit:
  69. return return_value;
  70. }
  71. PyDoc_STRVAR(dict_setdefault__doc__,
  72. "setdefault($self, key, default=None, /)\n"
  73. "--\n"
  74. "\n"
  75. "Insert key with a value of default if key is not in the dictionary.\n"
  76. "\n"
  77. "Return the value for key if key is in the dictionary, else default.");
  78. #define DICT_SETDEFAULT_METHODDEF \
  79. {"setdefault", _PyCFunction_CAST(dict_setdefault), METH_FASTCALL, dict_setdefault__doc__},
  80. static PyObject *
  81. dict_setdefault_impl(PyDictObject *self, PyObject *key,
  82. PyObject *default_value);
  83. static PyObject *
  84. dict_setdefault(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
  85. {
  86. PyObject *return_value = NULL;
  87. PyObject *key;
  88. PyObject *default_value = Py_None;
  89. if (!_PyArg_CheckPositional("setdefault", nargs, 1, 2)) {
  90. goto exit;
  91. }
  92. key = args[0];
  93. if (nargs < 2) {
  94. goto skip_optional;
  95. }
  96. default_value = args[1];
  97. skip_optional:
  98. return_value = dict_setdefault_impl(self, key, default_value);
  99. exit:
  100. return return_value;
  101. }
  102. PyDoc_STRVAR(dict_pop__doc__,
  103. "pop($self, key, default=<unrepresentable>, /)\n"
  104. "--\n"
  105. "\n"
  106. "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
  107. "\n"
  108. "If the key is not found, return the default if given; otherwise,\n"
  109. "raise a KeyError.");
  110. #define DICT_POP_METHODDEF \
  111. {"pop", _PyCFunction_CAST(dict_pop), METH_FASTCALL, dict_pop__doc__},
  112. static PyObject *
  113. dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value);
  114. static PyObject *
  115. dict_pop(PyDictObject *self, PyObject *const *args, Py_ssize_t nargs)
  116. {
  117. PyObject *return_value = NULL;
  118. PyObject *key;
  119. PyObject *default_value = NULL;
  120. if (!_PyArg_CheckPositional("pop", nargs, 1, 2)) {
  121. goto exit;
  122. }
  123. key = args[0];
  124. if (nargs < 2) {
  125. goto skip_optional;
  126. }
  127. default_value = args[1];
  128. skip_optional:
  129. return_value = dict_pop_impl(self, key, default_value);
  130. exit:
  131. return return_value;
  132. }
  133. PyDoc_STRVAR(dict_popitem__doc__,
  134. "popitem($self, /)\n"
  135. "--\n"
  136. "\n"
  137. "Remove and return a (key, value) pair as a 2-tuple.\n"
  138. "\n"
  139. "Pairs are returned in LIFO (last-in, first-out) order.\n"
  140. "Raises KeyError if the dict is empty.");
  141. #define DICT_POPITEM_METHODDEF \
  142. {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, dict_popitem__doc__},
  143. static PyObject *
  144. dict_popitem_impl(PyDictObject *self);
  145. static PyObject *
  146. dict_popitem(PyDictObject *self, PyObject *Py_UNUSED(ignored))
  147. {
  148. return dict_popitem_impl(self);
  149. }
  150. PyDoc_STRVAR(dict___reversed____doc__,
  151. "__reversed__($self, /)\n"
  152. "--\n"
  153. "\n"
  154. "Return a reverse iterator over the dict keys.");
  155. #define DICT___REVERSED___METHODDEF \
  156. {"__reversed__", (PyCFunction)dict___reversed__, METH_NOARGS, dict___reversed____doc__},
  157. static PyObject *
  158. dict___reversed___impl(PyDictObject *self);
  159. static PyObject *
  160. dict___reversed__(PyDictObject *self, PyObject *Py_UNUSED(ignored))
  161. {
  162. return dict___reversed___impl(self);
  163. }
  164. /*[clinic end generated code: output=c0064abbea6091c5 input=a9049054013a1b77]*/