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