Skip to content

Commit 14264a9

Browse files
Support a timeout arg.
1 parent 32f1556 commit 14264a9

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

Lib/test/support/interpreters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def send(self, obj):
208208
209209
This blocks until the object is received.
210210
"""
211-
_channels.send(self._id, obj, wait=True)
211+
_channels.send(self._id, obj, blocking=True)
212212

213213
def send_nowait(self, obj):
214214
"""Send the object to the channel's receiving end.
@@ -219,7 +219,7 @@ def send_nowait(self, obj):
219219
# XXX Note that at the moment channel_send() only ever returns
220220
# None. This should be fixed when channel_send_wait() is added.
221221
# See bpo-32604 and gh-19829.
222-
return _channels.send(self._id, obj, wait=False)
222+
return _channels.send(self._id, obj, blocking=False)
223223

224224
def send_buffer(self, obj):
225225
"""Send the object's buffer to the channel's receiving end.

Modules/_xxinterpchannelsmodule.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,27 +2521,32 @@ static PyObject *
25212521
channel_send(PyObject *self, PyObject *args, PyObject *kwds)
25222522
{
25232523
// XXX Add a timeout arg.
2524-
static char *kwlist[] = {"cid", "obj", "wait", NULL};
2525-
int64_t cid;
2524+
static char *kwlist[] = {"cid", "obj", "blocking", "timeout", NULL};
25262525
struct channel_id_converter_data cid_data = {
25272526
.module = self,
25282527
};
25292528
PyObject *obj;
2530-
int wait = 1;
2531-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O|$p:channel_send", kwlist,
2529+
int blocking = 1;
2530+
PyObject *timeout_obj = NULL;
2531+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O|$pL:channel_send", kwlist,
25322532
channel_id_converter, &cid_data, &obj,
2533-
&wait)) {
2533+
&blocking, &timeout_obj)) {
2534+
return NULL;
2535+
}
2536+
2537+
int64_t cid = cid_data.cid;
2538+
PY_TIMEOUT_T timeout;
2539+
if (PyThread_ParseTimeoutArg(timeout_obj, blocking, &timeout) < 0) {
25342540
return NULL;
25352541
}
2536-
cid = cid_data.cid;
25372542

2538-
if (wait) {
2543+
if (blocking) {
25392544
PyThread_type_lock mutex = PyThread_allocate_lock();
25402545
if (mutex == NULL) {
25412546
PyErr_NoMemory();
25422547
return NULL;
25432548
}
2544-
PyThread_acquire_lock(mutex, WAIT_LOCK);
2549+
PyThread_acquire_lock(mutex, NOWAIT_LOCK);
25452550

25462551
/* Queue up the object. */
25472552
int err = _channel_send(&_globals.channels, cid, obj, mutex);
@@ -2551,7 +2556,6 @@ channel_send(PyObject *self, PyObject *args, PyObject *kwds)
25512556
}
25522557

25532558
/* Wait until the object is received. */
2554-
PY_TIMEOUT_T timeout = -1;
25552559
if (wait_for_lock(mutex, timeout) < 0) {
25562560
return NULL;
25572561
}
@@ -2568,35 +2572,40 @@ channel_send(PyObject *self, PyObject *args, PyObject *kwds)
25682572
}
25692573

25702574
PyDoc_STRVAR(channel_send_doc,
2571-
"channel_send(cid, obj, wait=True)\n\
2575+
"channel_send(cid, obj, blocking=True)\n\
25722576
\n\
25732577
Add the object's data to the channel's queue.\n\
25742578
By default this waits for the object to be received.");
25752579

25762580
static PyObject *
25772581
channel_send_buffer(PyObject *self, PyObject *args, PyObject *kwds)
25782582
{
2579-
static char *kwlist[] = {"cid", "obj", "wait", NULL};
2580-
int64_t cid;
2583+
static char *kwlist[] = {"cid", "obj", "blocking", "timeout", NULL};
25812584
struct channel_id_converter_data cid_data = {
25822585
.module = self,
25832586
};
25842587
PyObject *obj;
2585-
int wait = 1;
2588+
int blocking = 1;
2589+
PyObject *timeout_obj = NULL;
25862590
if (!PyArg_ParseTupleAndKeywords(args, kwds,
2587-
"O&O|$p:channel_send_buffer", kwlist,
2591+
"O&O|$pO:channel_send_buffer", kwlist,
25882592
channel_id_converter, &cid_data, &obj,
2589-
&wait)) {
2593+
&blocking, &timeout_obj)) {
2594+
return NULL;
2595+
}
2596+
2597+
int64_t cid = cid_data.cid;
2598+
PY_TIMEOUT_T timeout;
2599+
if (PyThread_ParseTimeoutArg(timeout_obj, blocking, &timeout) < 0) {
25902600
return NULL;
25912601
}
2592-
cid = cid_data.cid;
25932602

25942603
PyObject *tempobj = PyMemoryView_FromObject(obj);
25952604
if (tempobj == NULL) {
25962605
return NULL;
25972606
}
25982607

2599-
if (wait) {
2608+
if (blocking) {
26002609
PyThread_type_lock mutex = PyThread_allocate_lock();
26012610
if (mutex == NULL) {
26022611
Py_DECREF(tempobj);
@@ -2614,7 +2623,6 @@ channel_send_buffer(PyObject *self, PyObject *args, PyObject *kwds)
26142623
}
26152624

26162625
/* Wait until the buffer is received. */
2617-
PY_TIMEOUT_T timeout = -1;
26182626
if (wait_for_lock(mutex, timeout) < 0) {
26192627
return NULL;
26202628
}
@@ -2632,7 +2640,7 @@ channel_send_buffer(PyObject *self, PyObject *args, PyObject *kwds)
26322640
}
26332641

26342642
PyDoc_STRVAR(channel_send_buffer_doc,
2635-
"channel_send_buffer(cid, obj, wait=True)\n\
2643+
"channel_send_buffer(cid, obj, blocking=True)\n\
26362644
\n\
26372645
Add the object's buffer to the channel's queue.\n\
26382646
By default this waits for the object to be received.");

0 commit comments

Comments
 (0)