Skip to content
Merged
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 view
Prev Previous commit
Next Next commit
Token.reset() checks the context too now
  • Loading branch information
Yury Selivanov
Yury Selivanov committed Jan 22, 2018
commit e299b4c3e224e0c687e2f4cfc56e0cff62811e95
1 change: 1 addition & 0 deletions Include/internal/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct _pycontextvarobject {

struct _pycontexttokenobject {
PyObject_HEAD
PyContext *tok_ctx;
PyContextVar *tok_var;
PyObject *tok_oldval;
int tok_used;
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ def fun():

ctx.run(fun)

@isolated_context
def test_context_getset_4(self):
c = contextvars.ContextVar('c', default=42)
ctx = contextvars.Context()

tok = ctx.run(c.set, 1)

with self.assertRaisesRegex(ValueError, 'different Context'):
c.reset(tok)

def test_context_copy_1(self):
ctx1 = contextvars.Context()
c = contextvars.ContextVar('c', default=42)
Expand Down
18 changes: 15 additions & 3 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static inline PyContext *
context_copy(void);

static PyContextToken *
token_new(PyContextVar *var, PyObject *val);
token_new(PyContext *ctx, PyContextVar *var, PyObject *val);

static PyContextVar *
contextvar_new(PyObject *name, PyObject *def);
Expand Down Expand Up @@ -211,7 +211,7 @@ PyContextVar_Set(PyContextVar *var, PyObject *val)
}

Py_XINCREF(old_val);
PyContextToken *tok = token_new(var, old_val);
PyContextToken *tok = token_new(ctx, var, old_val);
Py_XDECREF(old_val);

if (contextvar_set(var, val)) {
Expand All @@ -232,6 +232,13 @@ PyContextVar_Reset(PyContextVar *var, PyContextToken *tok)
return -1;
}

PyContext *ctx = context_get();
if (ctx != tok->tok_ctx) {
PyErr_SetString(PyExc_ValueError,
"Token was created in a different Context");
return -1;
}

if (tok->tok_used) {
return 0;
}
Expand Down Expand Up @@ -971,6 +978,7 @@ token_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
token_tp_clear(PyContextToken *self)
{
Py_CLEAR(self->tok_ctx);
Py_CLEAR(self->tok_var);
Py_CLEAR(self->tok_oldval);
return 0;
Expand All @@ -979,6 +987,7 @@ token_tp_clear(PyContextToken *self)
static int
token_tp_traverse(PyContextToken *self, visitproc visit, void *arg)
{
Py_VISIT(self->tok_ctx);
Py_VISIT(self->tok_var);
Py_VISIT(self->tok_oldval);
return 0;
Expand Down Expand Up @@ -1032,13 +1041,16 @@ PyTypeObject PyContextToken_Type = {
};

static PyContextToken *
token_new(PyContextVar *var, PyObject *val)
token_new(PyContext *ctx, PyContextVar *var, PyObject *val)
{
PyContextToken *tok = PyObject_GC_New(PyContextToken, &PyContextToken_Type);
if (tok == NULL) {
return NULL;
}

Py_INCREF(ctx);
tok->tok_ctx = ctx;

Py_INCREF(var);
tok->tok_var = var;

Expand Down