8000 gh-99633: _contextvars: Change private types to reduce casting · rhansen/cpython@1e02793 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e02793

Browse files
committed
pythongh-99633: _contextvars: Change private types to reduce casting
Change `PyObject *` to/from `PyContext *` to reduce the amount of casting and improve readability.
1 parent f3f17b3 commit 1e02793

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

Include/internal/pycore_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct {
2424

2525
struct _pycontextobject {
2626
PyObject_HEAD
27-
PyContext *ctx_prev;
27+
PyObject *ctx_prev;
2828
PyHamtObject *ctx_vars;
2929
PyObject *ctx_weakreflist;
3030
int ctx_entered;

Python/context.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,15 @@ context_switched(PyThreadState *ts)
194194

195195

196196
static int
197-
_PyContext_Enter(PyThreadState *ts, PyObject *octx)
197+
_PyContext_Enter(PyThreadState *ts, PyContext *ctx)
198198
{
199-
ENSURE_Context(octx, -1)
200-
PyContext *ctx = (PyContext *)octx;
201-
202199
if (ctx->ctx_entered) {
203200
_PyErr_Format(ts, PyExc_RuntimeError,
204201
"cannot enter context: %R is already entered", ctx);
205202
return -1;
206203
}
207204

208-
ctx->ctx_prev = (PyContext *)ts->context; /* borrow */
205+
ctx->ctx_prev = ts->context; /* borrow */
209206
ctx->ctx_entered = 1;
210207

211208
ts->context = Py_NewRef(ctx);
@@ -219,16 +216,14 @@ PyContext_Enter(PyObject *octx)
219216
{
220217
PyThreadState *ts = _PyThreadState_GET();
221218
assert(ts != NULL);
222-
return _PyContext_Enter(ts, octx);
219+
ENSURE_Context(octx, -1)
220+
return _PyContext_Enter(ts, (PyContext *)octx);
223221
}
224222

225223

226224
static int
227-
_PyContext_Exit(PyThreadState *ts, PyObject *octx)
225+
_PyContext_Exit(PyThreadState *ts, PyContext *ctx)
228226
{
229-
ENSURE_Context(octx, -1)
230-
PyContext *ctx = (PyContext *)octx;
231-
232227
if (!ctx->ctx_entered) {
233228
PyErr_Format(PyExc_RuntimeError,
234229
"cannot exit context: %R has not been entered", ctx);
@@ -243,7 +238,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
243238
return -1;
244239
}
245240

246-
Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
241+
Py_SETREF(ts->context, ctx->ctx_prev);
247242

248243
ctx->ctx_prev = NULL;
249244
ctx->ctx_entered = 0;
@@ -256,7 +251,8 @@ PyContext_Exit(PyObject *octx)
256251
{
257252
PyThreadState *ts = _PyThreadState_GET();
258253
assert(ts != NULL);
259-
return _PyContext_Exit(ts, octx);
254+
ENSURE_Context(octx, -1)
255+
return _PyContext_Exit(ts, (PyContext *)octx);
260256
}
261257

262258

@@ -715,14 +711,14 @@ context_run(PyContext *self, PyObject *const *args,
715711
return NULL;
716712
}
717713

718-
if (_PyContext_Enter(ts, (PyObject *)self)) {
714+
if (_PyContext_Enter(ts, self)) {
719715
return NULL;
720716
}
721717

722718
PyObject *call_result = _PyObject_VectorcallTstate(
723719
ts, args[0], args + 1, nargs - 1, kwnames);
724720

725-
if (_PyContext_Exit(ts, (PyObject *)self)) {
721+
if (_PyContext_Exit(ts, self)) {
726722
Py_XDECREF(call_result);
727723
return NULL;
728724
}

0 commit comments

Comments
 (0)
0