pep-567: More additions (#510)

* Add Token.MISSING and Token.old_value
* Change the signature of PyContextVar_Reset
This commit is contained in:
Yury Selivanov 2017-12-13 12:25:17 -05:00 committed by GitHub
parent 3b59266d43
commit 75fe7fe8b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 8 deletions

View File

@ -160,8 +160,14 @@ restore the ``ContextVar`` to its previous value, or remove it from
the context if the variable was not set before. It can be created the context if the variable was not set before. It can be created
only by calling ``ContextVar.set()``. only by calling ``ContextVar.set()``.
For debug and introspection purposes it has a read-only attribute For debug and introspection purposes it has:
``Token.var`` pointing to the variable that created the token.
* a read-only attribute ``Token.var`` pointing to the variable
that created the token;
* a read-only attribute ``Token.old_value`` set to the value the
variable had before the ``set()`` call, or to ``Token.MISSING``
if the variable wasn't set before.
Having the ``ContextVar.set()`` method returning a ``Token`` object Having the ``ContextVar.set()`` method returning a ``Token`` object
and the ``ContextVar.reset(token)`` method, allows context variables and the ``ContextVar.reset(token)`` method, allows context variables
@ -286,7 +292,7 @@ C API
3. ``PyContextToken * PyContextVar_Set(PyContextVar *, PyObject *)``: 3. ``PyContextToken * PyContextVar_Set(PyContextVar *, PyObject *)``:
set the value of the variable in the current context. set the value of the variable in the current context.
4. ``PyContextVar_Reset(PyContextToken *)``: 4. ``PyContextVar_Reset(PyContextVar *, PyContextToken *)``:
reset the value of the context variable. reset the value of the context variable.
5. ``PyContext * PyContext_New()``: create a new empty context. 5. ``PyContext * PyContext_New()``: create a new empty context.
@ -414,7 +420,7 @@ points to a ``_ContextData`` object::
try: try:
old_value = data.get(self) old_value = data.get(self)
except KeyError: except KeyError:
old_value = _NO_VALUE old_value = Token.MISSING
ts.context_data = data.set(self, value) ts.context_data = data.set(self, value)
return Token(self, old_value) return Token(self, old_value)
@ -423,7 +429,7 @@ points to a ``_ContextData`` object::
if token._used: if token._used:
return return
if token._old_value is _NO_VALUE: if token._old_value is Token.MISSING:
ts.context_data = data.delete(token._var) ts.context_data = data.delete(token._var)
else: else:
ts.context_data = data.set(token._var, ts.context_data = data.set(token._var,
@ -434,6 +440,8 @@ points to a ``_ContextData`` object::
class Token: class Token:
MISSING = object()
def __init__(self, var, old_value): def __init__(self, var, old_value):
self._var = var self._var = var
self._old_value = old_value self._old_value = old_value
@ -443,9 +451,9 @@ points to a ``_ContextData`` object::
def var(self): def var(self):
return self._var return self._var
@property
(The ``_NO_VALUE`` is an internal marker object that will not be def old_value(self):
part of the public API.) return self._old_value
Implementation Notes Implementation Notes