@@ -1350,19 +1350,16 @@ _channel_recv(_channels *channels, int64_t id)
1350
1350
_PyCrossInterpreterData * data = _channel_next (chan , PyInterpreterState_GetID (interp ));
1351
1351
PyThread_release_lock (mutex );
1352
1352
if (data == NULL ) {
1353
- if (!PyErr_Occurred ()) {
1354
- PyErr_Format (ChannelEmptyError , "channel %" PRId64 " is empty" , id );
1355
- }
1356
1353
return NULL ;
1357
1354
}
1358
1355
1359
1356
// Convert the data back to an object.
1360
1357
PyObject * obj = _PyCrossInterpreterData_NewObject (data );
1358
+ _PyCrossInterpreterData_Release (data );
1359
+ PyMem_Free (data );
1361
1360
if (obj == NULL ) {
1362
1361
return NULL ;
1363
1362
}
1364
- _PyCrossInterpreterData_Release (data );
1365
- PyMem_Free (data );
1366
1363
1367
1364
return obj ;
1368
1365
}
@@ -2351,20 +2348,37 @@ Add the object's data to the channel's queue.");
2351
2348
static PyObject *
2352
2349
channel_recv (PyObject * self , PyObject * args , PyObject * kwds )
2353
2350
{
2354
- static char * kwlist [] = {"cid" , NULL };
2351
+ static char * kwlist [] = {"cid" , "default" , NULL };
2355
2352
int64_t cid ;
2356
- if (!PyArg_ParseTupleAndKeywords (args , kwds , "O&:channel_recv" , kwlist ,
2357
- channel_id_converter , & cid )) {
2353
+ PyObject * dflt = NULL ;
2354
+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "O&|O:channel_recv" , kwlist ,
2355
+ channel_id_converter , & cid , & dflt )) {
2358
2356
return NULL ;
2359
2357
}
2358
+ Py_XINCREF (dflt );
2360
2359
2361
- return _channel_recv (& _globals .channels , cid );
2360
+ PyObject * obj = _channel_recv (& _globals .channels , cid );
2361
+ if (obj != NULL ) {
2362
+ Py_XDECREF (dflt );
2363
+ return obj ;
2364
+ } else if (PyErr_Occurred ()) {
2365
+ Py_XDECREF (dflt );
2366
+ return NULL ;
2367
+ } else if (dflt != NULL ) {
2368
+ return dflt ;
2369
+ } else {
2370
+ PyErr_Format (ChannelEmptyError , "channel %" PRId64 " is empty" , cid );
2371
+ return NULL ;
2372
+ }
2362
2373
}
2363
2374
2364
2375
PyDoc_STRVAR (channel_recv_doc ,
2365
- "channel_recv(cid) -> obj\n\
2376
+ "channel_recv(cid, [default]) -> obj\n\
2377
+ \n\
2378
+ Return a new object from the data at the front of the channel's queue.\n\
2366
2379
\n\
2367
- Return a new object from the data at the from of the channel's queue." );
2380
+ If there is nothing to receive then raise ChannelEmptyError, unless\n\
2381
+ a default value is provided. In that case return it." );
2368
2382
2369
2383
static PyObject *
2370
2384
channel_close (PyObject * self , PyObject * args , PyObject * kwds )
0 commit comments