8000 gh-99633: Add context manager support to `contextvars.Context` by rhansen · Pull Request #99634 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99633: Add context manager support to contextvars.Context #99634

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff vi 8000 ew
Prev Previous commit
Next Next commit
gh-99633: _contextvars: Change private types to reduce casting
Change `PyObject *` to/from `PyContext *` to reduce the amount of
casting and improve readability.
  • Loading branch information
rhansen committed Oct 10, 2024
commit 1e02793f0c265a1f1cc4b96ca36d141c7c78fe06
2 changes: 1 addition & 1 deletion Include/internal/pycore_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct {

struct _pycontextobject {
PyObject_HEAD
PyContext *ctx_prev;
PyObject *ctx_prev;
PyHamtObject *ctx_vars;
PyObject *ctx_weakreflist;
int ctx_entered;
Expand Down
24 changes: 10 additions & 14 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,15 @@ context_switched(PyThreadState *ts)


static int
_PyContext_Enter(PyThreadState *ts, PyObject *octx)
_PyContext_Enter(PyThreadState *ts, PyContext *ctx)
{
ENSURE_Context(octx, -1)
PyContext *ctx = (PyContext *)octx;

if (ctx->ctx_entered) {
_PyErr_Format(ts, PyExc_RuntimeError,
"cannot enter context: %R is already entered", ctx);
return -1;
}

ctx->ctx_prev = (PyContext *)ts->context; /* borrow */
ctx->ctx_prev = ts->context; /* borrow */
ctx->ctx_entered = 1;

ts->context = Py_NewRef(ctx);
Expand All @@ -219,16 +216,14 @@ PyContext_Enter(PyObject *octx)
{
PyThreadState *ts = _PyThreadState_GET();
assert(ts != NULL);
return _PyContext_Enter(ts, octx);
ENSURE_Context(octx, -1)
return _PyContext_Enter(ts, (PyContext *)octx);
}


static int
_PyContext_Exit(PyThreadState *ts, PyObject *octx)
_PyContext_Exit(PyThreadState *ts, PyContext *ctx)
{
ENSURE_Context(octx, -1)
PyContext *ctx = (PyContext *)octx;

if (!ctx->ctx_entered) {
PyErr_Format(PyExc_RuntimeError,
"cannot exit context: %R has not been entered", ctx);
Expand All @@ -243,7 +238,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
return -1;
}

Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
Py_SETREF(ts->context, ctx->ctx_prev);

ctx->ctx_prev = NULL;
ctx->ctx_entered = 0;
Expand All @@ -256,7 +251,8 @@ PyContext_Exit(PyObject *octx)
{
PyThreadState *ts = _PyThreadState_GET();
assert(ts != NULL);
return _PyContext_Exit(ts, octx);
ENSURE_Context(octx, -1)
return _PyContext_Exit(ts, (PyContext *)octx);
}


Expand Down Expand Up @@ -715,14 +711,14 @@ context_run(PyContext *self, PyObject *const *args,
return NULL;
}

if (_PyContext_Enter(ts, (PyObject *)self)) {
if (_PyContext_Enter(ts, self)) {
return NULL;
}

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

if (_PyContext_Exit(ts, (PyObject *)self)) {
if (_PyContext_Exit(ts, self)) {
Py_XDECREF(call_result);
return NULL;
}
Expand Down
0