Skip to content

Commit 7ba2e8f

Browse files
Add current interpreter info.
1 parent f897d61 commit 7ba2e8f

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Modules/_xxinterpchannelsmodule.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,11 @@ struct channel_info {
20982098
struct {
20992099
// 1: closed; -1: closing
21002100
int closed;
2101+
struct {
2102+
// 1: associated; -1: released
2103+
int send;
2104+
int recv;
2105+
} cur;
21012106
} status;
21022107
Py_ssize_t count;
21032108
};
@@ -2108,6 +2113,13 @@ _channel_get_info(_channels *channels, int64_t cid, struct channel_info *info)
21082113
int err = 0;
21092114
*info = (struct channel_info){0};
21102115

2116+
// Get the current interpreter.
2117+
PyInterpreterState *interp = _get_current_interp();
2118+
if (interp == NULL) {
2119+
return -1;
2120+
}
2121+
Py_ssize_t interpid = PyInterpreterState_GetID(interp);
2122+
21112123
// Hold the global lock until we're done.
21122124
PyThread_acquire_lock(channels->mutex, WAIT_LOCK);
21132125

@@ -2140,6 +2152,22 @@ _channel_get_info(_channels *channels, int64_t cid, struct channel_info *info)
21402152
// Get the number of queued objects.
21412153
info->count = chan->queue->count;
21422154

2155+
// Get the current ends status.
2156+
_channelend *send = _channelend_find(chan->ends->send, interpid, NULL);
2157+
if (send == NULL) {
2158+
info->status.cur.send = 0;
2159+
}
2160+
else {
2161+
info->status.cur.send = send->open ? 1 : -1;
2162+
}
2163+
_channelend *recv = _channelend_find(chan->ends->recv, interpid, NULL);
2164+
if (recv == NULL) {
2165+
info->status.cur.recv = 0;
2166+
}
2167+
else {
2168+
info->status.cur.recv = recv->open ? 1 : -1;
2169+
}
2170+
21432171
finally:
21442172
PyThread_release_lock(channels->mutex);
21452173
return err;
@@ -2155,14 +2183,18 @@ static PyStructSequence_Field channel_info_fields[] = {
21552183
{"closing", "send is closed, recv is non-empty"},
21562184
{"closed", "both ends are closed"},
21572185
{"count", "queued objects"},
2186+
{"send_associated", "current interpreter is bound to the send end"},
2187+
{"send_released", "current interpreter *was* bound to the send end"},
2188+
{"recv_associated", "current interpreter is bound to the recv end"},
2189+
{"recv_released", "current interpreter *was* bound to the recv end"},
21582190
{0}
21592191
};
21602192

21612193
static PyStructSequence_Desc channel_info_desc = {
21622194
.name = "ChannelInfo",
21632195
.doc = channel_info_doc,
21642196
.fields = channel_info_fields,
2165-
.n_in_sequence = 4,
2197+
.n_in_sequence = 8,
21662198
};
21672199

21682200
static PyObject *
@@ -2196,6 +2228,10 @@ new_channel_info(PyObject *mod, struct channel_info *info)
21962228
SET_BOOL(info->status.closed == -1);
21972229
SET_BOOL(info->status.closed == 1);
21982230
SET_COUNT(info->count);
2231+
SET_BOOL(info->status.cur.send == 1);
2232+
SET_BOOL(info->status.cur.send == -1);
2233+
SET_BOOL(info->status.cur.recv == 1);
2234+
SET_BOOL(info->status.cur.recv == -1);
21992235
#undef SET_COUNT
22002236
#undef SET_BOOL
22012237
assert(!PyErr_Occurred());

0 commit comments

Comments
 (0)