context.c.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(_contextvars_Context_get__doc__,
  9. "get($self, key, default=None, /)\n"
  10. "--\n"
  11. "\n"
  12. "Return the value for `key` if `key` has the value in the context object.\n"
  13. "\n"
  14. "If `key` does not exist, return `default`. If `default` is not given,\n"
  15. "return None.");
  16. #define _CONTEXTVARS_CONTEXT_GET_METHODDEF \
  17. {"get", _PyCFunction_CAST(_contextvars_Context_get), METH_FASTCALL, _contextvars_Context_get__doc__},
  18. static PyObject *
  19. _contextvars_Context_get_impl(PyContext *self, PyObject *key,
  20. PyObject *default_value);
  21. static PyObject *
  22. _contextvars_Context_get(PyContext *self, PyObject *const *args, Py_ssize_t nargs)
  23. {
  24. PyObject *return_value = NULL;
  25. PyObject *key;
  26. PyObject *default_value = Py_None;
  27. if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
  28. goto exit;
  29. }
  30. key = args[0];
  31. if (nargs < 2) {
  32. goto skip_optional;
  33. }
  34. default_value = args[1];
  35. skip_optional:
  36. return_value = _contextvars_Context_get_impl(self, key, default_value);
  37. exit:
  38. return return_value;
  39. }
  40. PyDoc_STRVAR(_contextvars_Context_items__doc__,
  41. "items($self, /)\n"
  42. "--\n"
  43. "\n"
  44. "Return all variables and their values in the context object.\n"
  45. "\n"
  46. "The result is returned as a list of 2-tuples (variable, value).");
  47. #define _CONTEXTVARS_CONTEXT_ITEMS_METHODDEF \
  48. {"items", (PyCFunction)_contextvars_Context_items, METH_NOARGS, _contextvars_Context_items__doc__},
  49. static PyObject *
  50. _contextvars_Context_items_impl(PyContext *self);
  51. static PyObject *
  52. _contextvars_Context_items(PyContext *self, PyObject *Py_UNUSED(ignored))
  53. {
  54. return _contextvars_Context_items_impl(self);
  55. }
  56. PyDoc_STRVAR(_contextvars_Context_keys__doc__,
  57. "keys($self, /)\n"
  58. "--\n"
  59. "\n"
  60. "Return a list of all variables in the context object.");
  61. #define _CONTEXTVARS_CONTEXT_KEYS_METHODDEF \
  62. {"keys", (PyCFunction)_contextvars_Context_keys, METH_NOARGS, _contextvars_Context_keys__doc__},
  63. static PyObject *
  64. _contextvars_Context_keys_impl(PyContext *self);
  65. static PyObject *
  66. _contextvars_Context_keys(PyContext *self, PyObject *Py_UNUSED(ignored))
  67. {
  68. return _contextvars_Context_keys_impl(self);
  69. }
  70. PyDoc_STRVAR(_contextvars_Context_values__doc__,
  71. "values($self, /)\n"
  72. "--\n"
  73. "\n"
  74. "Return a list of all variables\' values in the context object.");
  75. #define _CONTEXTVARS_CONTEXT_VALUES_METHODDEF \
  76. {"values", (PyCFunction)_contextvars_Context_values, METH_NOARGS, _contextvars_Context_values__doc__},
  77. static PyObject *
  78. _contextvars_Context_values_impl(PyContext *self);
  79. static PyObject *
  80. _contextvars_Context_values(PyContext *self, PyObject *Py_UNUSED(ignored))
  81. {
  82. return _contextvars_Context_values_impl(self);
  83. }
  84. PyDoc_STRVAR(_contextvars_Context_copy__doc__,
  85. "copy($self, /)\n"
  86. "--\n"
  87. "\n"
  88. "Return a shallow copy of the context object.");
  89. #define _CONTEXTVARS_CONTEXT_COPY_METHODDEF \
  90. {"copy", (PyCFunction)_contextvars_Context_copy, METH_NOARGS, _contextvars_Context_copy__doc__},
  91. static PyObject *
  92. _contextvars_Context_copy_impl(PyContext *self);
  93. static PyObject *
  94. _contextvars_Context_copy(PyContext *self, PyObject *Py_UNUSED(ignored))
  95. {
  96. return _contextvars_Context_copy_impl(self);
  97. }
  98. PyDoc_STRVAR(_contextvars_ContextVar_get__doc__,
  99. "get($self, default=<unrepresentable>, /)\n"
  100. "--\n"
  101. "\n"
  102. "Return a value for the context variable for the current context.\n"
  103. "\n"
  104. "If there is no value for the variable in the current context, the method will:\n"
  105. " * return the value of the default argument of the method, if provided; or\n"
  106. " * return the default value for the context variable, if it was created\n"
  107. " with one; or\n"
  108. " * raise a LookupError.");
  109. #define _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF \
  110. {"get", _PyCFunction_CAST(_contextvars_ContextVar_get), METH_FASTCALL, _contextvars_ContextVar_get__doc__},
  111. static PyObject *
  112. _contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value);
  113. static PyObject *
  114. _contextvars_ContextVar_get(PyContextVar *self, PyObject *const *args, Py_ssize_t nargs)
  115. {
  116. PyObject *return_value = NULL;
  117. PyObject *default_value = NULL;
  118. if (!_PyArg_CheckPositional("get", nargs, 0, 1)) {
  119. goto exit;
  120. }
  121. if (nargs < 1) {
  122. goto skip_optional;
  123. }
  124. default_value = args[0];
  125. skip_optional:
  126. return_value = _contextvars_ContextVar_get_impl(self, default_value);
  127. exit:
  128. return return_value;
  129. }
  130. PyDoc_STRVAR(_contextvars_ContextVar_set__doc__,
  131. "set($self, value, /)\n"
  132. "--\n"
  133. "\n"
  134. "Call to set a new value for the context variable in the current context.\n"
  135. "\n"
  136. "The required value argument is the new value for the context variable.\n"
  137. "\n"
  138. "Returns a Token object that can be used to restore the variable to its previous\n"
  139. "value via the `ContextVar.reset()` method.");
  140. #define _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF \
  141. {"set", (PyCFunction)_contextvars_ContextVar_set, METH_O, _contextvars_ContextVar_set__doc__},
  142. PyDoc_STRVAR(_contextvars_ContextVar_reset__doc__,
  143. "reset($self, token, /)\n"
  144. "--\n"
  145. "\n"
  146. "Reset the context variable.\n"
  147. "\n"
  148. "The variable is reset to the value it had before the `ContextVar.set()` that\n"
  149. "created the token was used.");
  150. #define _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF \
  151. {"reset", (PyCFunction)_contextvars_ContextVar_reset, METH_O, _contextvars_ContextVar_reset__doc__},
  152. /*[clinic end generated code: output=0c94d4b919500438 input=a9049054013a1b77]*/