| 118 | return Token(ctx, self, old_value) |
| 119 | |
| 120 | def reset(self, token): |
| 121 | if token._used: |
| 122 | raise RuntimeError("Token has already been used once") |
| 123 | |
| 124 | if token._var is not self: |
| 125 | raise ValueError( |
| 126 | "Token was created by a different ContextVar") |
| 127 | |
| 128 | if token._context is not _get_context(): |
| 129 | raise ValueError( |
| 130 | "Token was created in a different Context") |
| 131 | |
| 132 | ctx = token._context |
| 133 | if token._old_value is Token.MISSING: |
| 134 | ctx._data = ctx._data.delete(token._var) |
| 135 | else: |
| 136 | ctx._data = ctx._data.set(token._var, token._old_value) |
| 137 | |
| 138 | token._used = True |
| 139 | |
| 140 | def __repr__(self): |
| 141 | r = '<ContextVar name={!r}'.format(self.name) |