Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-38880: List interpreters associated with a channel end#17323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ec5aa60ee78133433a5b064c16612c5c6df7f439fad86ae97c529c88168ebf32a7b5084d6ac6ea44c2d2215300b9b124db76621c7d4c51bff3c2b34af10d1fac09b6c8e698640546702ad511efa1b7c3ad9b3e27f4990b0c7dbb046202ecda7cf61b357101ffaca1df75e791f79f5d355481e8279be8a0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added the ability to list interpreters associated with channel ends in the internal subinterpreters module. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -536,7 +536,7 @@ _channelend_find(_channelend *first, int64_t interp, _channelend **pprev) | ||
| typedef struct _channelassociations{ | ||
| // Note that the list entries are never removed for interpreter | ||
| // for which the channel is closed. This should be a problem in | ||
| // for which the channel is closed. This should not be a problem in | ||
| // practice. Also, a channel isn't automatically closed when an | ||
| // interpreter is destroyed. | ||
| int64_t numsendopen; | ||
| @@ -1177,11 +1177,6 @@ _channels_list_all(_channels *channels, int64_t *count) | ||
| { | ||
| int64_t *cids = NULL; | ||
| PyThread_acquire_lock(channels->mutex, WAIT_LOCK); | ||
| int64_t numopen = channels->numopen; | ||
| if (numopen >= PY_SSIZE_T_MAX){ | ||
| PyErr_SetString(PyExc_RuntimeError, "too many channels open"); | ||
| goto done; | ||
| } | ||
| int64_t *ids = PyMem_NEW(int64_t, (Py_ssize_t)(channels->numopen)); | ||
| if (ids == NULL){ | ||
| goto done; | ||
| @@ -1393,6 +1388,24 @@ _channel_close(_channels *channels, int64_t id, int end, int force) | ||
| return _channels_close(channels, id, NULL, end, force); | ||
| } | ||
| static int | ||
| _channel_is_associated(_channels *channels, int64_t cid, int64_t interp, | ||
| int send) | ||
| { | ||
| _PyChannelState *chan = _channels_lookup(channels, cid, NULL); | ||
| if (chan == NULL){ | ||
| return -1; | ||
| } else if (send && chan->closing != NULL){ | ||
| PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid); | ||
| return -1; | ||
| } | ||
| _channelend *end = _channelend_find(send ? chan->ends->send : chan->ends->recv, | ||
| interp, NULL); | ||
| return (end != NULL && end->open); | ||
| } | ||
| /* ChannelID class */ | ||
| static PyTypeObject ChannelIDtype; | ||
| @@ -2323,6 +2336,68 @@ PyDoc_STRVAR(channel_list_all_doc, | ||
| \n\ | ||
| Return the list of all IDs for active channels."); | ||
| static PyObject * | ||
| channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds) | ||
| { | ||
| static char *kwlist[] ={"cid", "send", NULL}; | ||
| int64_t cid; /* Channel ID */ | ||
| int send = 0; /* Send or receive end? */ | ||
| int64_t id; | ||
| PyObject *ids, *id_obj; | ||
| PyInterpreterState *interp; | ||
| if (!PyArg_ParseTupleAndKeywords( | ||
| args, kwds, "O&$p:channel_list_interpreters", | ||
| kwlist, channel_id_converter, &cid, &send)){ | ||
| return NULL; | ||
| } | ||
| ids = PyList_New(0); | ||
| if (ids == NULL){ | ||
| goto except; | ||
LewisGaul marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| interp = PyInterpreterState_Head(); | ||
| while (interp != NULL){ | ||
| id = PyInterpreterState_GetID(interp); | ||
| assert(id >= 0); | ||
| int res = _channel_is_associated(&_globals.channels, cid, id, send); | ||
| if (res < 0){ | ||
| goto except; | ||
LewisGaul marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| if (res){ | ||
| id_obj = _PyInterpreterState_GetIDObject(interp); | ||
| if (id_obj == NULL){ | ||
| goto except; | ||
| } | ||
| res = PyList_Insert(ids, 0, id_obj); | ||
ericsnowcurrently marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Py_DECREF(id_obj); | ||
| if (res < 0){ | ||
| goto except; | ||
| } | ||
| } | ||
| interp = PyInterpreterState_Next(interp); | ||
| } | ||
| goto finally; | ||
| except: | ||
| Py_XDECREF(ids); | ||
| ids = NULL; | ||
| finally: | ||
| return ids; | ||
| } | ||
| PyDoc_STRVAR(channel_list_interpreters_doc, | ||
| "channel_list_interpreters(cid, *, send) -> [id]\n\ | ||
| \n\ | ||
| Return the list of all interpreter IDs associated with an end of the channel.\n\ | ||
| \n\ | ||
| The 'send' argument should be a boolean indicating whether to use the send or\n\ | ||
| receive end."); | ||
| static PyObject * | ||
| channel_send(PyObject *self, PyObject *args, PyObject *kwds) | ||
| { | ||
| @@ -2476,6 +2551,8 @@ static PyMethodDef module_functions[] ={ | ||
| METH_VARARGS | METH_KEYWORDS, channel_destroy_doc}, | ||
| {"channel_list_all", channel_list_all, | ||
| METH_NOARGS, channel_list_all_doc}, | ||
| {"channel_list_interpreters", (PyCFunction)(void(*)(void))channel_list_interpreters, | ||
LewisGaul marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| METH_VARARGS | METH_KEYWORDS, channel_list_interpreters_doc}, | ||
| {"channel_send", (PyCFunction)(void(*)(void))channel_send, | ||
| METH_VARARGS | METH_KEYWORDS, channel_send_doc}, | ||
| {"channel_recv", (PyCFunction)(void(*)(void))channel_recv, | ||
Uh oh!
There was an error while loading. Please reload this page.