Skip to content

Commit c2dbdca

Browse files
committed
Remove public PyContextVar_GetChanged() API.
Use a private API instead (ctx._get_changed() is the Python-level API).
1 parent 74482e3 commit c2dbdca

11 files changed

Lines changed: 90 additions & 156 deletions

File tree

Doc/c-api/contextvars.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -182,27 +182,3 @@ Context variable functions:
182182
Reset the state of the *var* context variable to that it was in before
183183
:c:func:`PyContextVar_Set` that returned the *token* was called.
184184
This function returns ``0`` on success and ``-1`` on error.
185-
186-
.. c:function:: int PyContextVar_GetChanged(PyObject *var, PyObject *default_value, PyObject **value, int *changed)
187-
188-
Like :c:func:`PyContextVar_Get`, but also reports whether the variable was
189-
changed in the current context scope. This combines a value lookup with a
190-
change check in a single HAMT lookup.
191-
192-
Returns ``-1`` if an error has occurred during lookup, and ``0`` if no
193-
error occurred, whether or not a value was found.
194-
195-
On success, *\*value* is set following the same rules as
196-
:c:func:`PyContextVar_Get`. *\*changed* is set to ``1`` if the variable
197-
was changed (via :c:func:`PyContextVar_Set`) in the current context scope
198-
(i.e. within the current :meth:`~contextvars.Context.run` call) with a
199-
value that is a different object than the inherited one. Otherwise
200-
*\*changed* is set to ``0``. If the value was not found, *\*changed* is
201-
always ``0``.
202-
203-
If the current context was never entered (no :meth:`~contextvars.Context.run`
204-
is active), all existing bindings are considered "changed".
205-
206-
Except for ``NULL``, the function returns a new reference via *\*value*.
207-
208-
.. versionadded:: 3.15

Doc/data/refcounts.dat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,6 @@ PyContextVar_Reset:int:::
400400
PyContextVar_Reset:PyObject*:var:0:
401401
PyContextVar_Reset:PyObject*:token:-1:
402402

403-
PyContextVar_GetChanged:int:::
404-
PyContextVar_GetChanged:PyObject*:var:0:
405-
PyContextVar_GetChanged:PyObject*:default_value:0:
406-
PyContextVar_GetChanged:PyObject**:value:+1:???
407-
PyContextVar_GetChanged:int*:changed::
408-
409403
PyCFunction_New:PyObject*::+1:
410404
PyCFunction_New:PyMethodDef*:ml::
411405
PyCFunction_New:PyObject*:self:+1:

Doc/library/contextvars.rst

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -119,41 +119,6 @@ Context Variables
119119

120120
The same *token* cannot be used twice.
121121

122-
.. method:: get_changed([default])
123-
124-
Like :meth:`ContextVar.get`, but returns a tuple ``(value, changed)``
125-
where *changed* indicates whether the variable was changed in the
126-
current context scope.
127-
128-
A variable is considered "changed" if :meth:`ContextVar.set` has been
129-
called on it within the current :meth:`Context.run` call with a value
130-
that is a different object than the inherited one. Variables inherited
131-
unchanged from a parent context scope are not considered "changed".
132-
If no :meth:`Context.run` is active, all existing bindings are
133-
considered "changed". When the value comes from a default, *changed*
134-
is always ``False``.
135-
136-
This is useful when a context variable holds a mutable object that
137-
needs to be copied on first access in a new context scope to ensure
138-
modifications are local to that scope::
139-
140-
_ctx_var = ContextVar('ctx_var')
141-
142-
def get_ctx():
143-
try:
144-
ctx, changed = _ctx_var.get_changed()
145-
except LookupError:
146-
ctx = default_context()
147-
_ctx_var.set(ctx)
148-
return ctx
149-
150-
if not changed:
151-
ctx = ctx.copy()
152-
_ctx_var.set(ctx)
153-
return ctx
154-
155-
.. versionadded:: 3.15
156-
157122

158123
.. class:: Token
159124

Include/cpython/context.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,6 @@ PyAPI_FUNC(PyObject *) PyContextVar_Set(PyObject *var, PyObject *value);
100100
PyAPI_FUNC(int) PyContextVar_Reset(PyObject *var, PyObject *token);
101101

102102

103-
/* Get a value for the variable and check if it was changed.
104-
105-
Like PyContextVar_Get, but also reports whether the variable was
106-
changed in the current context scope via a single HAMT lookup.
107-
108-
Returns -1 if an error occurred during lookup.
109-
110-
Returns 0 if no error occurred. In this case:
111-
112-
- *value will be set the same as for PyContextVar_Get.
113-
- *changed will be set to 1 if the variable was changed in the
114-
current context scope, 0 otherwise. If the variable was not
115-
found, *changed is always 0.
116-
117-
'*value' will be a new ref, if not NULL.
118-
*/
119-
PyAPI_FUNC(int) PyContextVar_GetChanged(
120-
PyObject *var, PyObject *default_value, PyObject **value, int *changed);
121-
122-
123103
#ifdef __cplusplus
124104
}
125105
#endif

Include/internal/pycore_context.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,24 @@ PyAPI_FUNC(PyObject*) _PyContext_NewHamtForTests(void);
5959
PyAPI_FUNC(int) _PyContext_Enter(PyThreadState *ts, PyObject *octx);
6060
PyAPI_FUNC(int) _PyContext_Exit(PyThreadState *ts, PyObject *octx);
6161

62+
/* Get a value for the variable and check if it was changed.
63+
64+
Like PyContextVar_Get, but also reports whether the variable was
65+
changed in the current context scope via a single HAMT lookup.
66+
67+
Returns -1 if an error occurred during lookup.
68+
69+
Returns 0 if no error occurred. In this case:
70+
71+
- *value will be set the same as for PyContextVar_Get.
72+
- *changed will be set to 1 if the variable was changed in the
73+
current context scope, 0 otherwise. If the variable was not
74+
found, *changed is always 0.
75+
76+
'*value' will be a new ref, if not NULL.
77+
*/
78+
PyAPI_FUNC(int) _PyContextVar_GetChanged(
79+
PyObject *var, PyObject *default_value, PyObject **value, int *changed);
80+
6281

6382
#endif /* !Py_INTERNAL_CONTEXT_H */

Lib/_pydecimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def getcontext():
361361
New contexts are copies of DefaultContext.
362362
"""
363363
try:
364-
context, changed = _current_context_var.get_changed()
364+
context, changed = _current_context_var._get_changed()
365365
except LookupError:
366366
context = Context()
367367
_current_context_var.set(context)

0 commit comments

Comments
 (0)