_queuemodule.c.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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(simplequeue_new__doc__,
  9. "SimpleQueue()\n"
  10. "--\n"
  11. "\n"
  12. "Simple, unbounded, reentrant FIFO queue.");
  13. static PyObject *
  14. simplequeue_new_impl(PyTypeObject *type);
  15. static PyObject *
  16. simplequeue_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  17. {
  18. PyObject *return_value = NULL;
  19. PyTypeObject *base_tp = simplequeue_get_state_by_type(type)->SimpleQueueType;
  20. if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
  21. !_PyArg_NoPositional("SimpleQueue", args)) {
  22. goto exit;
  23. }
  24. if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
  25. !_PyArg_NoKeywords("SimpleQueue", kwargs)) {
  26. goto exit;
  27. }
  28. return_value = simplequeue_new_impl(type);
  29. exit:
  30. return return_value;
  31. }
  32. PyDoc_STRVAR(_queue_SimpleQueue_put__doc__,
  33. "put($self, /, item, block=True, timeout=None)\n"
  34. "--\n"
  35. "\n"
  36. "Put the item on the queue.\n"
  37. "\n"
  38. "The optional \'block\' and \'timeout\' arguments are ignored, as this method\n"
  39. "never blocks. They are provided for compatibility with the Queue class.");
  40. #define _QUEUE_SIMPLEQUEUE_PUT_METHODDEF \
  41. {"put", _PyCFunction_CAST(_queue_SimpleQueue_put), METH_FASTCALL|METH_KEYWORDS, _queue_SimpleQueue_put__doc__},
  42. static PyObject *
  43. _queue_SimpleQueue_put_impl(simplequeueobject *self, PyObject *item,
  44. int block, PyObject *timeout);
  45. static PyObject *
  46. _queue_SimpleQueue_put(simplequeueobject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  47. {
  48. PyObject *return_value = NULL;
  49. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  50. #define NUM_KEYWORDS 3
  51. static struct {
  52. PyGC_Head _this_is_not_used;
  53. PyObject_VAR_HEAD
  54. PyObject *ob_item[NUM_KEYWORDS];
  55. } _kwtuple = {
  56. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  57. .ob_item = { &_Py_ID(item), &_Py_ID(block), &_Py_ID(timeout), },
  58. };
  59. #undef NUM_KEYWORDS
  60. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  61. #else // !Py_BUILD_CORE
  62. # define KWTUPLE NULL
  63. #endif // !Py_BUILD_CORE
  64. static const char * const _keywords[] = {"item", "block", "timeout", NULL};
  65. static _PyArg_Parser _parser = {
  66. .keywords = _keywords,
  67. .fname = "put",
  68. .kwtuple = KWTUPLE,
  69. };
  70. #undef KWTUPLE
  71. PyObject *argsbuf[3];
  72. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  73. PyObject *item;
  74. int block = 1;
  75. PyObject *timeout = Py_None;
  76. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
  77. if (!args) {
  78. goto exit;
  79. }
  80. item = args[0];
  81. if (!noptargs) {
  82. goto skip_optional_pos;
  83. }
  84. if (args[1]) {
  85. block = PyObject_IsTrue(args[1]);
  86. if (block < 0) {
  87. goto exit;
  88. }
  89. if (!--noptargs) {
  90. goto skip_optional_pos;
  91. }
  92. }
  93. timeout = args[2];
  94. skip_optional_pos:
  95. return_value = _queue_SimpleQueue_put_impl(self, item, block, timeout);
  96. exit:
  97. return return_value;
  98. }
  99. PyDoc_STRVAR(_queue_SimpleQueue_put_nowait__doc__,
  100. "put_nowait($self, /, item)\n"
  101. "--\n"
  102. "\n"
  103. "Put an item into the queue without blocking.\n"
  104. "\n"
  105. "This is exactly equivalent to `put(item)` and is only provided\n"
  106. "for compatibility with the Queue class.");
  107. #define _QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF \
  108. {"put_nowait", _PyCFunction_CAST(_queue_SimpleQueue_put_nowait), METH_FASTCALL|METH_KEYWORDS, _queue_SimpleQueue_put_nowait__doc__},
  109. static PyObject *
  110. _queue_SimpleQueue_put_nowait_impl(simplequeueobject *self, PyObject *item);
  111. static PyObject *
  112. _queue_SimpleQueue_put_nowait(simplequeueobject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  113. {
  114. PyObject *return_value = NULL;
  115. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  116. #define NUM_KEYWORDS 1
  117. static struct {
  118. PyGC_Head _this_is_not_used;
  119. PyObject_VAR_HEAD
  120. PyObject *ob_item[NUM_KEYWORDS];
  121. } _kwtuple = {
  122. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  123. .ob_item = { &_Py_ID(item), },
  124. };
  125. #undef NUM_KEYWORDS
  126. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  127. #else // !Py_BUILD_CORE
  128. # define KWTUPLE NULL
  129. #endif // !Py_BUILD_CORE
  130. static const char * const _keywords[] = {"item", NULL};
  131. static _PyArg_Parser _parser = {
  132. .keywords = _keywords,
  133. .fname = "put_nowait",
  134. .kwtuple = KWTUPLE,
  135. };
  136. #undef KWTUPLE
  137. PyObject *argsbuf[1];
  138. PyObject *item;
  139. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  140. if (!args) {
  141. goto exit;
  142. }
  143. item = args[0];
  144. return_value = _queue_SimpleQueue_put_nowait_impl(self, item);
  145. exit:
  146. return return_value;
  147. }
  148. PyDoc_STRVAR(_queue_SimpleQueue_get__doc__,
  149. "get($self, /, block=True, timeout=None)\n"
  150. "--\n"
  151. "\n"
  152. "Remove and return an item from the queue.\n"
  153. "\n"
  154. "If optional args \'block\' is true and \'timeout\' is None (the default),\n"
  155. "block if necessary until an item is available. If \'timeout\' is\n"
  156. "a non-negative number, it blocks at most \'timeout\' seconds and raises\n"
  157. "the Empty exception if no item was available within that time.\n"
  158. "Otherwise (\'block\' is false), return an item if one is immediately\n"
  159. "available, else raise the Empty exception (\'timeout\' is ignored\n"
  160. "in that case).");
  161. #define _QUEUE_SIMPLEQUEUE_GET_METHODDEF \
  162. {"get", _PyCFunction_CAST(_queue_SimpleQueue_get), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _queue_SimpleQueue_get__doc__},
  163. static PyObject *
  164. _queue_SimpleQueue_get_impl(simplequeueobject *self, PyTypeObject *cls,
  165. int block, PyObject *timeout_obj);
  166. static PyObject *
  167. _queue_SimpleQueue_get(simplequeueobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  168. {
  169. PyObject *return_value = NULL;
  170. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  171. #define NUM_KEYWORDS 2
  172. static struct {
  173. PyGC_Head _this_is_not_used;
  174. PyObject_VAR_HEAD
  175. PyObject *ob_item[NUM_KEYWORDS];
  176. } _kwtuple = {
  177. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  178. .ob_item = { &_Py_ID(block), &_Py_ID(timeout), },
  179. };
  180. #undef NUM_KEYWORDS
  181. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  182. #else // !Py_BUILD_CORE
  183. # define KWTUPLE NULL
  184. #endif // !Py_BUILD_CORE
  185. static const char * const _keywords[] = {"block", "timeout", NULL};
  186. static _PyArg_Parser _parser = {
  187. .keywords = _keywords,
  188. .fname = "get",
  189. .kwtuple = KWTUPLE,
  190. };
  191. #undef KWTUPLE
  192. PyObject *argsbuf[2];
  193. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  194. int block = 1;
  195. PyObject *timeout_obj = Py_None;
  196. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
  197. if (!args) {
  198. goto exit;
  199. }
  200. if (!noptargs) {
  201. goto skip_optional_pos;
  202. }
  203. if (args[0]) {
  204. block = PyObject_IsTrue(args[0]);
  205. if (block < 0) {
  206. goto exit;
  207. }
  208. if (!--noptargs) {
  209. goto skip_optional_pos;
  210. }
  211. }
  212. timeout_obj = args[1];
  213. skip_optional_pos:
  214. return_value = _queue_SimpleQueue_get_impl(self, cls, block, timeout_obj);
  215. exit:
  216. return return_value;
  217. }
  218. PyDoc_STRVAR(_queue_SimpleQueue_get_nowait__doc__,
  219. "get_nowait($self, /)\n"
  220. "--\n"
  221. "\n"
  222. "Remove and return an item from the queue without blocking.\n"
  223. "\n"
  224. "Only get an item if one is immediately available. Otherwise\n"
  225. "raise the Empty exception.");
  226. #define _QUEUE_SIMPLEQUEUE_GET_NOWAIT_METHODDEF \
  227. {"get_nowait", _PyCFunction_CAST(_queue_SimpleQueue_get_nowait), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _queue_SimpleQueue_get_nowait__doc__},
  228. static PyObject *
  229. _queue_SimpleQueue_get_nowait_impl(simplequeueobject *self,
  230. PyTypeObject *cls);
  231. static PyObject *
  232. _queue_SimpleQueue_get_nowait(simplequeueobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  233. {
  234. if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
  235. PyErr_SetString(PyExc_TypeError, "get_nowait() takes no arguments");
  236. return NULL;
  237. }
  238. return _queue_SimpleQueue_get_nowait_impl(self, cls);
  239. }
  240. PyDoc_STRVAR(_queue_SimpleQueue_empty__doc__,
  241. "empty($self, /)\n"
  242. "--\n"
  243. "\n"
  244. "Return True if the queue is empty, False otherwise (not reliable!).");
  245. #define _QUEUE_SIMPLEQUEUE_EMPTY_METHODDEF \
  246. {"empty", (PyCFunction)_queue_SimpleQueue_empty, METH_NOARGS, _queue_SimpleQueue_empty__doc__},
  247. static int
  248. _queue_SimpleQueue_empty_impl(simplequeueobject *self);
  249. static PyObject *
  250. _queue_SimpleQueue_empty(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
  251. {
  252. PyObject *return_value = NULL;
  253. int _return_value;
  254. _return_value = _queue_SimpleQueue_empty_impl(self);
  255. if ((_return_value == -1) && PyErr_Occurred()) {
  256. goto exit;
  257. }
  258. return_value = PyBool_FromLong((long)_return_value);
  259. exit:
  260. return return_value;
  261. }
  262. PyDoc_STRVAR(_queue_SimpleQueue_qsize__doc__,
  263. "qsize($self, /)\n"
  264. "--\n"
  265. "\n"
  266. "Return the approximate size of the queue (not reliable!).");
  267. #define _QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF \
  268. {"qsize", (PyCFunction)_queue_SimpleQueue_qsize, METH_NOARGS, _queue_SimpleQueue_qsize__doc__},
  269. static Py_ssize_t
  270. _queue_SimpleQueue_qsize_impl(simplequeueobject *self);
  271. static PyObject *
  272. _queue_SimpleQueue_qsize(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
  273. {
  274. PyObject *return_value = NULL;
  275. Py_ssize_t _return_value;
  276. _return_value = _queue_SimpleQueue_qsize_impl(self);
  277. if ((_return_value == -1) && PyErr_Occurred()) {
  278. goto exit;
  279. }
  280. return_value = PyLong_FromSsize_t(_return_value);
  281. exit:
  282. return return_value;
  283. }
  284. /*[clinic end generated code: output=78816f171ecc4422 input=a9049054013a1b77]*/