context.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. #include "Python.h"
  2. #include "pycore_call.h" // _PyObject_VectorcallTstate()
  3. #include "pycore_context.h"
  4. #include "pycore_gc.h" // _PyObject_GC_MAY_BE_TRACKED()
  5. #include "pycore_hamt.h"
  6. #include "pycore_initconfig.h" // _PyStatus_OK()
  7. #include "pycore_object.h"
  8. #include "pycore_pyerrors.h"
  9. #include "pycore_pystate.h" // _PyThreadState_GET()
  10. #include "structmember.h" // PyMemberDef
  11. #include "clinic/context.c.h"
  12. /*[clinic input]
  13. module _contextvars
  14. [clinic start generated code]*/
  15. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a0955718c8b8cea6]*/
  16. #define ENSURE_Context(o, err_ret) \
  17. if (!PyContext_CheckExact(o)) { \
  18. PyErr_SetString(PyExc_TypeError, \
  19. "an instance of Context was expected"); \
  20. return err_ret; \
  21. }
  22. #define ENSURE_ContextVar(o, err_ret) \
  23. if (!PyContextVar_CheckExact(o)) { \
  24. PyErr_SetString(PyExc_TypeError, \
  25. "an instance of ContextVar was expected"); \
  26. return err_ret; \
  27. }
  28. #define ENSURE_ContextToken(o, err_ret) \
  29. if (!PyContextToken_CheckExact(o)) { \
  30. PyErr_SetString(PyExc_TypeError, \
  31. "an instance of Token was expected"); \
  32. return err_ret; \
  33. }
  34. /////////////////////////// Context API
  35. static PyContext *
  36. context_new_empty(void);
  37. static PyContext *
  38. context_new_from_vars(PyHamtObject *vars);
  39. static inline PyContext *
  40. context_get(void);
  41. static PyContextToken *
  42. token_new(PyContext *ctx, PyContextVar *var, PyObject *val);
  43. static PyContextVar *
  44. contextvar_new(PyObject *name, PyObject *def);
  45. static int
  46. contextvar_set(PyContextVar *var, PyObject *val);
  47. static int
  48. contextvar_del(PyContextVar *var);
  49. #if PyContext_MAXFREELIST > 0
  50. static struct _Py_context_state *
  51. get_context_state(void)
  52. {
  53. PyInterpreterState *interp = _PyInterpreterState_GET();
  54. return &interp->context;
  55. }
  56. #endif
  57. PyObject *
  58. _PyContext_NewHamtForTests(void)
  59. {
  60. return (PyObject *)_PyHamt_New();
  61. }
  62. PyObject *
  63. PyContext_New(void)
  64. {
  65. return (PyObject *)context_new_empty();
  66. }
  67. PyObject *
  68. PyContext_Copy(PyObject * octx)
  69. {
  70. ENSURE_Context(octx, NULL)
  71. PyContext *ctx = (PyContext *)octx;
  72. return (PyObject *)context_new_from_vars(ctx->ctx_vars);
  73. }
  74. PyObject *
  75. PyContext_CopyCurrent(void)
  76. {
  77. PyContext *ctx = context_get();
  78. if (ctx == NULL) {
  79. return NULL;
  80. }
  81. return (PyObject *)context_new_from_vars(ctx->ctx_vars);
  82. }
  83. static int
  84. _PyContext_Enter(PyThreadState *ts, PyObject *octx)
  85. {
  86. ENSURE_Context(octx, -1)
  87. PyContext *ctx = (PyContext *)octx;
  88. if (ctx->ctx_entered) {
  89. _PyErr_Format(ts, PyExc_RuntimeError,
  90. "cannot enter context: %R is already entered", ctx);
  91. return -1;
  92. }
  93. ctx->ctx_prev = (PyContext *)ts->context; /* borrow */
  94. ctx->ctx_entered = 1;
  95. ts->context = Py_NewRef(ctx);
  96. ts->context_ver++;
  97. return 0;
  98. }
  99. int
  100. PyContext_Enter(PyObject *octx)
  101. {
  102. PyThreadState *ts = _PyThreadState_GET();
  103. assert(ts != NULL);
  104. return _PyContext_Enter(ts, octx);
  105. }
  106. static int
  107. _PyContext_Exit(PyThreadState *ts, PyObject *octx)
  108. {
  109. ENSURE_Context(octx, -1)
  110. PyContext *ctx = (PyContext *)octx;
  111. if (!ctx->ctx_entered) {
  112. PyErr_Format(PyExc_RuntimeError,
  113. "cannot exit context: %R has not been entered", ctx);
  114. return -1;
  115. }
  116. if (ts->context != (PyObject *)ctx) {
  117. /* Can only happen if someone misuses the C API */
  118. PyErr_SetString(PyExc_RuntimeError,
  119. "cannot exit context: thread state references "
  120. "a different context object");
  121. return -1;
  122. }
  123. Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
  124. ts->context_ver++;
  125. ctx->ctx_prev = NULL;
  126. ctx->ctx_entered = 0;
  127. return 0;
  128. }
  129. int
  130. PyContext_Exit(PyObject *octx)
  131. {
  132. PyThreadState *ts = _PyThreadState_GET();
  133. assert(ts != NULL);
  134. return _PyContext_Exit(ts, octx);
  135. }
  136. PyObject *
  137. PyContextVar_New(const char *name, PyObject *def)
  138. {
  139. PyObject *pyname = PyUnicode_FromString(name);
  140. if (pyname == NULL) {
  141. return NULL;
  142. }
  143. PyContextVar *var = contextvar_new(pyname, def);
  144. Py_DECREF(pyname);
  145. return (PyObject *)var;
  146. }
  147. int
  148. PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
  149. {
  150. ENSURE_ContextVar(ovar, -1)
  151. PyContextVar *var = (PyContextVar *)ovar;
  152. PyThreadState *ts = _PyThreadState_GET();
  153. assert(ts != NULL);
  154. if (ts->context == NULL) {
  155. goto not_found;
  156. }
  157. if (var->var_cached != NULL &&
  158. var->var_cached_tsid == ts->id &&
  159. var->var_cached_tsver == ts->context_ver)
  160. {
  161. *val = var->var_cached;
  162. goto found;
  163. }
  164. assert(PyContext_CheckExact(ts->context));
  165. PyHamtObject *vars = ((PyContext *)ts->context)->ctx_vars;
  166. PyObject *found = NULL;
  167. int res = _PyHamt_Find(vars, (PyObject*)var, &found);
  168. if (res < 0) {
  169. goto error;
  170. }
  171. if (res == 1) {
  172. assert(found != NULL);
  173. var->var_cached = found; /* borrow */
  174. var->var_cached_tsid = ts->id;
  175. var->var_cached_tsver = ts->context_ver;
  176. *val = found;
  177. goto found;
  178. }
  179. not_found:
  180. if (def == NULL) {
  181. if (var->var_default != NULL) {
  182. *val = var->var_default;
  183. goto found;
  184. }
  185. *val = NULL;
  186. goto found;
  187. }
  188. else {
  189. *val = def;
  190. goto found;
  191. }
  192. found:
  193. Py_XINCREF(*val);
  194. return 0;
  195. error:
  196. *val = NULL;
  197. return -1;
  198. }
  199. PyObject *
  200. PyContextVar_Set(PyObject *ovar, PyObject *val)
  201. {
  202. ENSURE_ContextVar(ovar, NULL)
  203. PyContextVar *var = (PyContextVar *)ovar;
  204. if (!PyContextVar_CheckExact(var)) {
  205. PyErr_SetString(
  206. PyExc_TypeError, "an instance of ContextVar was expected");
  207. return NULL;
  208. }
  209. PyContext *ctx = context_get();
  210. if (ctx == NULL) {
  211. return NULL;
  212. }
  213. PyObject *old_val = NULL;
  214. int found = _PyHamt_Find(ctx->ctx_vars, (PyObject *)var, &old_val);
  215. if (found < 0) {
  216. return NULL;
  217. }
  218. Py_XINCREF(old_val);
  219. PyContextToken *tok = token_new(ctx, var, old_val);
  220. Py_XDECREF(old_val);
  221. if (contextvar_set(var, val)) {
  222. Py_DECREF(tok);
  223. return NULL;
  224. }
  225. return (PyObject *)tok;
  226. }
  227. int
  228. PyContextVar_Reset(PyObject *ovar, PyObject *otok)
  229. {
  230. ENSURE_ContextVar(ovar, -1)
  231. ENSURE_ContextToken(otok, -1)
  232. PyContextVar *var = (PyContextVar *)ovar;
  233. PyContextToken *tok = (PyContextToken *)otok;
  234. if (tok->tok_used) {
  235. PyErr_Format(PyExc_RuntimeError,
  236. "%R has already been used once", tok);
  237. return -1;
  238. }
  239. if (var != tok->tok_var) {
  240. PyErr_Format(PyExc_ValueError,
  241. "%R was created by a different ContextVar", tok);
  242. return -1;
  243. }
  244. PyContext *ctx = context_get();
  245. if (ctx != tok->tok_ctx) {
  246. PyErr_Format(PyExc_ValueError,
  247. "%R was created in a different Context", tok);
  248. return -1;
  249. }
  250. tok->tok_used = 1;
  251. if (tok->tok_oldval == NULL) {
  252. return contextvar_del(var);
  253. }
  254. else {
  255. return contextvar_set(var, tok->tok_oldval);
  256. }
  257. }
  258. /////////////////////////// PyContext
  259. /*[clinic input]
  260. class _contextvars.Context "PyContext *" "&PyContext_Type"
  261. [clinic start generated code]*/
  262. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bdf87f8e0cb580e8]*/
  263. static inline PyContext *
  264. _context_alloc(void)
  265. {
  266. PyContext *ctx;
  267. #if PyContext_MAXFREELIST > 0
  268. struct _Py_context_state *state = get_context_state();
  269. #ifdef Py_DEBUG
  270. // _context_alloc() must not be called after _PyContext_Fini()
  271. assert(state->numfree != -1);
  272. #endif
  273. if (state->numfree) {
  274. state->numfree--;
  275. ctx = state->freelist;
  276. state->freelist = (PyContext *)ctx->ctx_weakreflist;
  277. OBJECT_STAT_INC(from_freelist);
  278. ctx->ctx_weakreflist = NULL;
  279. _Py_NewReference((PyObject *)ctx);
  280. }
  281. else
  282. #endif
  283. {
  284. ctx = PyObject_GC_New(PyContext, &PyContext_Type);
  285. if (ctx == NULL) {
  286. return NULL;
  287. }
  288. }
  289. ctx->ctx_vars = NULL;
  290. ctx->ctx_prev = NULL;
  291. ctx->ctx_entered = 0;
  292. ctx->ctx_weakreflist = NULL;
  293. return ctx;
  294. }
  295. static PyContext *
  296. context_new_empty(void)
  297. {
  298. PyContext *ctx = _context_alloc();
  299. if (ctx == NULL) {
  300. return NULL;
  301. }
  302. ctx->ctx_vars = _PyHamt_New();
  303. if (ctx->ctx_vars == NULL) {
  304. Py_DECREF(ctx);
  305. return NULL;
  306. }
  307. _PyObject_GC_TRACK(ctx);
  308. return ctx;
  309. }
  310. static PyContext *
  311. context_new_from_vars(PyHamtObject *vars)
  312. {
  313. PyContext *ctx = _context_alloc();
  314. if (ctx == NULL) {
  315. return NULL;
  316. }
  317. ctx->ctx_vars = (PyHamtObject*)Py_NewRef(vars);
  318. _PyObject_GC_TRACK(ctx);
  319. return ctx;
  320. }
  321. static inline PyContext *
  322. context_get(void)
  323. {
  324. PyThreadState *ts = _PyThreadState_GET();
  325. assert(ts != NULL);
  326. PyContext *current_ctx = (PyContext *)ts->context;
  327. if (current_ctx == NULL) {
  328. current_ctx = context_new_empty();
  329. if (current_ctx == NULL) {
  330. return NULL;
  331. }
  332. ts->context = (PyObject *)current_ctx;
  333. }
  334. return current_ctx;
  335. }
  336. static int
  337. context_check_key_type(PyObject *key)
  338. {
  339. if (!PyContextVar_CheckExact(key)) {
  340. // abort();
  341. PyErr_Format(PyExc_TypeError,
  342. "a ContextVar key was expected, got %R", key);
  343. return -1;
  344. }
  345. return 0;
  346. }
  347. static PyObject *
  348. context_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  349. {
  350. if (PyTuple_Size(args) || (kwds != NULL && PyDict_Size(kwds))) {
  351. PyErr_SetString(
  352. PyExc_TypeError, "Context() does not accept any arguments");
  353. return NULL;
  354. }
  355. return PyContext_New();
  356. }
  357. static int
  358. context_tp_clear(PyContext *self)
  359. {
  360. Py_CLEAR(self->ctx_prev);
  361. Py_CLEAR(self->ctx_vars);
  362. return 0;
  363. }
  364. static int
  365. context_tp_traverse(PyContext *self, visitproc visit, void *arg)
  366. {
  367. Py_VISIT(self->ctx_prev);
  368. Py_VISIT(self->ctx_vars);
  369. return 0;
  370. }
  371. static void
  372. context_tp_dealloc(PyContext *self)
  373. {
  374. _PyObject_GC_UNTRACK(self);
  375. if (self->ctx_weakreflist != NULL) {
  376. PyObject_ClearWeakRefs((PyObject*)self);
  377. }
  378. (void)context_tp_clear(self);
  379. #if PyContext_MAXFREELIST > 0
  380. struct _Py_context_state *state = get_context_state();
  381. #ifdef Py_DEBUG
  382. // _context_alloc() must not be called after _PyContext_Fini()
  383. assert(state->numfree != -1);
  384. #endif
  385. if (state->numfree < PyContext_MAXFREELIST) {
  386. state->numfree++;
  387. self->ctx_weakreflist = (PyObject *)state->freelist;
  388. state->freelist = self;
  389. OBJECT_STAT_INC(to_freelist);
  390. }
  391. else
  392. #endif
  393. {
  394. Py_TYPE(self)->tp_free(self);
  395. }
  396. }
  397. static PyObject *
  398. context_tp_iter(PyContext *self)
  399. {
  400. return _PyHamt_NewIterKeys(self->ctx_vars);
  401. }
  402. static PyObject *
  403. context_tp_richcompare(PyObject *v, PyObject *w, int op)
  404. {
  405. if (!PyContext_CheckExact(v) || !PyContext_CheckExact(w) ||
  406. (op != Py_EQ && op != Py_NE))
  407. {
  408. Py_RETURN_NOTIMPLEMENTED;
  409. }
  410. int res = _PyHamt_Eq(
  411. ((PyContext *)v)->ctx_vars, ((PyContext *)w)->ctx_vars);
  412. if (res < 0) {
  413. return NULL;
  414. }
  415. if (op == Py_NE) {
  416. res = !res;
  417. }
  418. if (res) {
  419. Py_RETURN_TRUE;
  420. }
  421. else {
  422. Py_RETURN_FALSE;
  423. }
  424. }
  425. static Py_ssize_t
  426. context_tp_len(PyContext *self)
  427. {
  428. return _PyHamt_Len(self->ctx_vars);
  429. }
  430. static PyObject *
  431. context_tp_subscript(PyContext *self, PyObject *key)
  432. {
  433. if (context_check_key_type(key)) {
  434. return NULL;
  435. }
  436. PyObject *val = NULL;
  437. int found = _PyHamt_Find(self->ctx_vars, key, &val);
  438. if (found < 0) {
  439. return NULL;
  440. }
  441. if (found == 0) {
  442. PyErr_SetObject(PyExc_KeyError, key);
  443. return NULL;
  444. }
  445. return Py_NewRef(val);
  446. }
  447. static int
  448. context_tp_contains(PyContext *self, PyObject *key)
  449. {
  450. if (context_check_key_type(key)) {
  451. return -1;
  452. }
  453. PyObject *val = NULL;
  454. return _PyHamt_Find(self->ctx_vars, key, &val);
  455. }
  456. /*[clinic input]
  457. _contextvars.Context.get
  458. key: object
  459. default: object = None
  460. /
  461. Return the value for `key` if `key` has the value in the context object.
  462. If `key` does not exist, return `default`. If `default` is not given,
  463. return None.
  464. [clinic start generated code]*/
  465. static PyObject *
  466. _contextvars_Context_get_impl(PyContext *self, PyObject *key,
  467. PyObject *default_value)
  468. /*[clinic end generated code: output=0c54aa7664268189 input=c8eeb81505023995]*/
  469. {
  470. if (context_check_key_type(key)) {
  471. return NULL;
  472. }
  473. PyObject *val = NULL;
  474. int found = _PyHamt_Find(self->ctx_vars, key, &val);
  475. if (found < 0) {
  476. return NULL;
  477. }
  478. if (found == 0) {
  479. return Py_NewRef(default_value);
  480. }
  481. return Py_NewRef(val);
  482. }
  483. /*[clinic input]
  484. _contextvars.Context.items
  485. Return all variables and their values in the context object.
  486. The result is returned as a list of 2-tuples (variable, value).
  487. [clinic start generated code]*/
  488. static PyObject *
  489. _contextvars_Context_items_impl(PyContext *self)
  490. /*[clinic end generated code: output=fa1655c8a08502af input=00db64ae379f9f42]*/
  491. {
  492. return _PyHamt_NewIterItems(self->ctx_vars);
  493. }
  494. /*[clinic input]
  495. _contextvars.Context.keys
  496. Return a list of all variables in the context object.
  497. [clinic start generated code]*/
  498. static PyObject *
  499. _contextvars_Context_keys_impl(PyContext *self)
  500. /*[clinic end generated code: output=177227c6b63ec0e2 input=114b53aebca3449c]*/
  501. {
  502. return _PyHamt_NewIterKeys(self->ctx_vars);
  503. }
  504. /*[clinic input]
  505. _contextvars.Context.values
  506. Return a list of all variables' values in the context object.
  507. [clinic start generated code]*/
  508. static PyObject *
  509. _contextvars_Context_values_impl(PyContext *self)
  510. /*[clinic end generated code: output=d286dabfc8db6dde input=ce8075d04a6ea526]*/
  511. {
  512. return _PyHamt_NewIterValues(self->ctx_vars);
  513. }
  514. /*[clinic input]
  515. _contextvars.Context.copy
  516. Return a shallow copy of the context object.
  517. [clinic start generated code]*/
  518. static PyObject *
  519. _contextvars_Context_copy_impl(PyContext *self)
  520. /*[clinic end generated code: output=30ba8896c4707a15 input=ebafdbdd9c72d592]*/
  521. {
  522. return (PyObject *)context_new_from_vars(self->ctx_vars);
  523. }
  524. static PyObject *
  525. context_run(PyContext *self, PyObject *const *args,
  526. Py_ssize_t nargs, PyObject *kwnames)
  527. {
  528. PyThreadState *ts = _PyThreadState_GET();
  529. if (nargs < 1) {
  530. _PyErr_SetString(ts, PyExc_TypeError,
  531. "run() missing 1 required positional argument");
  532. return NULL;
  533. }
  534. if (_PyContext_Enter(ts, (PyObject *)self)) {
  535. return NULL;
  536. }
  537. PyObject *call_result = _PyObject_VectorcallTstate(
  538. ts, args[0], args + 1, nargs - 1, kwnames);
  539. if (_PyContext_Exit(ts, (PyObject *)self)) {
  540. return NULL;
  541. }
  542. return call_result;
  543. }
  544. static PyMethodDef PyContext_methods[] = {
  545. _CONTEXTVARS_CONTEXT_GET_METHODDEF
  546. _CONTEXTVARS_CONTEXT_ITEMS_METHODDEF
  547. _CONTEXTVARS_CONTEXT_KEYS_METHODDEF
  548. _CONTEXTVARS_CONTEXT_VALUES_METHODDEF
  549. _CONTEXTVARS_CONTEXT_COPY_METHODDEF
  550. {"run", _PyCFunction_CAST(context_run), METH_FASTCALL | METH_KEYWORDS, NULL},
  551. {NULL, NULL}
  552. };
  553. static PySequenceMethods PyContext_as_sequence = {
  554. 0, /* sq_length */
  555. 0, /* sq_concat */
  556. 0, /* sq_repeat */
  557. 0, /* sq_item */
  558. 0, /* sq_slice */
  559. 0, /* sq_ass_item */
  560. 0, /* sq_ass_slice */
  561. (objobjproc)context_tp_contains, /* sq_contains */
  562. 0, /* sq_inplace_concat */
  563. 0, /* sq_inplace_repeat */
  564. };
  565. static PyMappingMethods PyContext_as_mapping = {
  566. (lenfunc)context_tp_len, /* mp_length */
  567. (binaryfunc)context_tp_subscript, /* mp_subscript */
  568. };
  569. PyTypeObject PyContext_Type = {
  570. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  571. "_contextvars.Context",
  572. sizeof(PyContext),
  573. .tp_methods = PyContext_methods,
  574. .tp_as_mapping = &PyContext_as_mapping,
  575. .tp_as_sequence = &PyContext_as_sequence,
  576. .tp_iter = (getiterfunc)context_tp_iter,
  577. .tp_dealloc = (destructor)context_tp_dealloc,
  578. .tp_getattro = PyObject_GenericGetAttr,
  579. .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
  580. .tp_richcompare = context_tp_richcompare,
  581. .tp_traverse = (traverseproc)context_tp_traverse,
  582. .tp_clear = (inquiry)context_tp_clear,
  583. .tp_new = context_tp_new,
  584. .tp_weaklistoffset = offsetof(PyContext, ctx_weakreflist),
  585. .tp_hash = PyObject_HashNotImplemented,
  586. };
  587. /////////////////////////// ContextVar
  588. static int
  589. contextvar_set(PyContextVar *var, PyObject *val)
  590. {
  591. var->var_cached = NULL;
  592. PyThreadState *ts = _PyThreadState_GET();
  593. PyContext *ctx = context_get();
  594. if (ctx == NULL) {
  595. return -1;
  596. }
  597. PyHamtObject *new_vars = _PyHamt_Assoc(
  598. ctx->ctx_vars, (PyObject *)var, val);
  599. if (new_vars == NULL) {
  600. return -1;
  601. }
  602. Py_SETREF(ctx->ctx_vars, new_vars);
  603. var->var_cached = val; /* borrow */
  604. var->var_cached_tsid = ts->id;
  605. var->var_cached_tsver = ts->context_ver;
  606. return 0;
  607. }
  608. static int
  609. contextvar_del(PyContextVar *var)
  610. {
  611. var->var_cached = NULL;
  612. PyContext *ctx = context_get();
  613. if (ctx == NULL) {
  614. return -1;
  615. }
  616. PyHamtObject *vars = ctx->ctx_vars;
  617. PyHamtObject *new_vars = _PyHamt_Without(vars, (PyObject *)var);
  618. if (new_vars == NULL) {
  619. return -1;
  620. }
  621. if (vars == new_vars) {
  622. Py_DECREF(new_vars);
  623. PyErr_SetObject(PyExc_LookupError, (PyObject *)var);
  624. return -1;
  625. }
  626. Py_SETREF(ctx->ctx_vars, new_vars);
  627. return 0;
  628. }
  629. static Py_hash_t
  630. contextvar_generate_hash(void *addr, PyObject *name)
  631. {
  632. /* Take hash of `name` and XOR it with the object's addr.
  633. The structure of the tree is encoded in objects' hashes, which
  634. means that sufficiently similar hashes would result in tall trees
  635. with many Collision nodes. Which would, in turn, result in slower
  636. get and set operations.
  637. The XORing helps to ensure that:
  638. (1) sequentially allocated ContextVar objects have
  639. different hashes;
  640. (2) context variables with equal names have
  641. different hashes.
  642. */
  643. Py_hash_t name_hash = PyObject_Hash(name);
  644. if (name_hash == -1) {
  645. return -1;
  646. }
  647. Py_hash_t res = _Py_HashPointer(addr) ^ name_hash;
  648. return res == -1 ? -2 : res;
  649. }
  650. static PyContextVar *
  651. contextvar_new(PyObject *name, PyObject *def)
  652. {
  653. if (!PyUnicode_Check(name)) {
  654. PyErr_SetString(PyExc_TypeError,
  655. "context variable name must be a str");
  656. return NULL;
  657. }
  658. PyContextVar *var = PyObject_GC_New(PyContextVar, &PyContextVar_Type);
  659. if (var == NULL) {
  660. return NULL;
  661. }
  662. var->var_hash = contextvar_generate_hash(var, name);
  663. if (var->var_hash == -1) {
  664. Py_DECREF(var);
  665. return NULL;
  666. }
  667. var->var_name = Py_NewRef(name);
  668. var->var_default = Py_XNewRef(def);
  669. var->var_cached = NULL;
  670. var->var_cached_tsid = 0;
  671. var->var_cached_tsver = 0;
  672. if (_PyObject_GC_MAY_BE_TRACKED(name) ||
  673. (def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
  674. {
  675. PyObject_GC_Track(var);
  676. }
  677. return var;
  678. }
  679. /*[clinic input]
  680. class _contextvars.ContextVar "PyContextVar *" "&PyContextVar_Type"
  681. [clinic start generated code]*/
  682. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=445da935fa8883c3]*/
  683. static PyObject *
  684. contextvar_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  685. {
  686. static char *kwlist[] = {"", "default", NULL};
  687. PyObject *name;
  688. PyObject *def = NULL;
  689. if (!PyArg_ParseTupleAndKeywords(
  690. args, kwds, "O|$O:ContextVar", kwlist, &name, &def))
  691. {
  692. return NULL;
  693. }
  694. return (PyObject *)contextvar_new(name, def);
  695. }
  696. static int
  697. contextvar_tp_clear(PyContextVar *self)
  698. {
  699. Py_CLEAR(self->var_name);
  700. Py_CLEAR(self->var_default);
  701. self->var_cached = NULL;
  702. self->var_cached_tsid = 0;
  703. self->var_cached_tsver = 0;
  704. return 0;
  705. }
  706. static int
  707. contextvar_tp_traverse(PyContextVar *self, visitproc visit, void *arg)
  708. {
  709. Py_VISIT(self->var_name);
  710. Py_VISIT(self->var_default);
  711. return 0;
  712. }
  713. static void
  714. contextvar_tp_dealloc(PyContextVar *self)
  715. {
  716. PyObject_GC_UnTrack(self);
  717. (void)contextvar_tp_clear(self);
  718. Py_TYPE(self)->tp_free(self);
  719. }
  720. static Py_hash_t
  721. contextvar_tp_hash(PyContextVar *self)
  722. {
  723. return self->var_hash;
  724. }
  725. static PyObject *
  726. contextvar_tp_repr(PyContextVar *self)
  727. {
  728. _PyUnicodeWriter writer;
  729. _PyUnicodeWriter_Init(&writer);
  730. if (_PyUnicodeWriter_WriteASCIIString(
  731. &writer, "<ContextVar name=", 17) < 0)
  732. {
  733. goto error;
  734. }
  735. PyObject *name = PyObject_Repr(self->var_name);
  736. if (name == NULL) {
  737. goto error;
  738. }
  739. if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
  740. Py_DECREF(name);
  741. goto error;
  742. }
  743. Py_DECREF(name);
  744. if (self->var_default != NULL) {
  745. if (_PyUnicodeWriter_WriteASCIIString(&writer, " default=", 9) < 0) {
  746. goto error;
  747. }
  748. PyObject *def = PyObject_Repr(self->var_default);
  749. if (def == NULL) {
  750. goto error;
  751. }
  752. if (_PyUnicodeWriter_WriteStr(&writer, def) < 0) {
  753. Py_DECREF(def);
  754. goto error;
  755. }
  756. Py_DECREF(def);
  757. }
  758. PyObject *addr = PyUnicode_FromFormat(" at %p>", self);
  759. if (addr == NULL) {
  760. goto error;
  761. }
  762. if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) {
  763. Py_DECREF(addr);
  764. goto error;
  765. }
  766. Py_DECREF(addr);
  767. return _PyUnicodeWriter_Finish(&writer);
  768. error:
  769. _PyUnicodeWriter_Dealloc(&writer);
  770. return NULL;
  771. }
  772. /*[clinic input]
  773. _contextvars.ContextVar.get
  774. default: object = NULL
  775. /
  776. Return a value for the context variable for the current context.
  777. If there is no value for the variable in the current context, the method will:
  778. * return the value of the default argument of the method, if provided; or
  779. * return the default value for the context variable, if it was created
  780. with one; or
  781. * raise a LookupError.
  782. [clinic start generated code]*/
  783. static PyObject *
  784. _contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value)
  785. /*[clinic end generated code: output=0746bd0aa2ced7bf input=30aa2ab9e433e401]*/
  786. {
  787. if (!PyContextVar_CheckExact(self)) {
  788. PyErr_SetString(
  789. PyExc_TypeError, "an instance of ContextVar was expected");
  790. return NULL;
  791. }
  792. PyObject *val;
  793. if (PyContextVar_Get((PyObject *)self, default_value, &val) < 0) {
  794. return NULL;
  795. }
  796. if (val == NULL) {
  797. PyErr_SetObject(PyExc_LookupError, (PyObject *)self);
  798. return NULL;
  799. }
  800. return val;
  801. }
  802. /*[clinic input]
  803. _contextvars.ContextVar.set
  804. value: object
  805. /
  806. Call to set a new value for the context variable in the current context.
  807. The required value argument is the new value for the context variable.
  808. Returns a Token object that can be used to restore the variable to its previous
  809. value via the `ContextVar.reset()` method.
  810. [clinic start generated code]*/
  811. static PyObject *
  812. _contextvars_ContextVar_set(PyContextVar *self, PyObject *value)
  813. /*[clinic end generated code: output=446ed5e820d6d60b input=c0a6887154227453]*/
  814. {
  815. return PyContextVar_Set((PyObject *)self, value);
  816. }
  817. /*[clinic input]
  818. _contextvars.ContextVar.reset
  819. token: object
  820. /
  821. Reset the context variable.
  822. The variable is reset to the value it had before the `ContextVar.set()` that
  823. created the token was used.
  824. [clinic start generated code]*/
  825. static PyObject *
  826. _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token)
  827. /*[clinic end generated code: output=d4ee34d0742d62ee input=ebe2881e5af4ffda]*/
  828. {
  829. if (!PyContextToken_CheckExact(token)) {
  830. PyErr_Format(PyExc_TypeError,
  831. "expected an instance of Token, got %R", token);
  832. return NULL;
  833. }
  834. if (PyContextVar_Reset((PyObject *)self, token)) {
  835. return NULL;
  836. }
  837. Py_RETURN_NONE;
  838. }
  839. static PyMemberDef PyContextVar_members[] = {
  840. {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY},
  841. {NULL}
  842. };
  843. static PyMethodDef PyContextVar_methods[] = {
  844. _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
  845. _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF
  846. _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF
  847. {"__class_getitem__", Py_GenericAlias,
  848. METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  849. {NULL, NULL}
  850. };
  851. PyTypeObject PyContextVar_Type = {
  852. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  853. "_contextvars.ContextVar",
  854. sizeof(PyContextVar),
  855. .tp_methods = PyContextVar_methods,
  856. .tp_members = PyContextVar_members,
  857. .tp_dealloc = (destructor)contextvar_tp_dealloc,
  858. .tp_getattro = PyObject_GenericGetAttr,
  859. .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
  860. .tp_traverse = (traverseproc)contextvar_tp_traverse,
  861. .tp_clear = (inquiry)contextvar_tp_clear,
  862. .tp_new = contextvar_tp_new,
  863. .tp_free = PyObject_GC_Del,
  864. .tp_hash = (hashfunc)contextvar_tp_hash,
  865. .tp_repr = (reprfunc)contextvar_tp_repr,
  866. };
  867. /////////////////////////// Token
  868. static PyObject * get_token_missing(void);
  869. /*[clinic input]
  870. class _contextvars.Token "PyContextToken *" "&PyContextToken_Type"
  871. [clinic start generated code]*/
  872. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=338a5e2db13d3f5b]*/
  873. static PyObject *
  874. token_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  875. {
  876. PyErr_SetString(PyExc_RuntimeError,
  877. "Tokens can only be created by ContextVars");
  878. return NULL;
  879. }
  880. static int
  881. token_tp_clear(PyContextToken *self)
  882. {
  883. Py_CLEAR(self->tok_ctx);
  884. Py_CLEAR(self->tok_var);
  885. Py_CLEAR(self->tok_oldval);
  886. return 0;
  887. }
  888. static int
  889. token_tp_traverse(PyContextToken *self, visitproc visit, void *arg)
  890. {
  891. Py_VISIT(self->tok_ctx);
  892. Py_VISIT(self->tok_var);
  893. Py_VISIT(self->tok_oldval);
  894. return 0;
  895. }
  896. static void
  897. token_tp_dealloc(PyContextToken *self)
  898. {
  899. PyObject_GC_UnTrack(self);
  900. (void)token_tp_clear(self);
  901. Py_TYPE(self)->tp_free(self);
  902. }
  903. static PyObject *
  904. token_tp_repr(PyContextToken *self)
  905. {
  906. _PyUnicodeWriter writer;
  907. _PyUnicodeWriter_Init(&writer);
  908. if (_PyUnicodeWriter_WriteASCIIString(&writer, "<Token", 6) < 0) {
  909. goto error;
  910. }
  911. if (self->tok_used) {
  912. if (_PyUnicodeWriter_WriteASCIIString(&writer, " used", 5) < 0) {
  913. goto error;
  914. }
  915. }
  916. if (_PyUnicodeWriter_WriteASCIIString(&writer, " var=", 5) < 0) {
  917. goto error;
  918. }
  919. PyObject *var = PyObject_Repr((PyObject *)self->tok_var);
  920. if (var == NULL) {
  921. goto error;
  922. }
  923. if (_PyUnicodeWriter_WriteStr(&writer, var) < 0) {
  924. Py_DECREF(var);
  925. goto error;
  926. }
  927. Py_DECREF(var);
  928. PyObject *addr = PyUnicode_FromFormat(" at %p>", self);
  929. if (addr == NULL) {
  930. goto error;
  931. }
  932. if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) {
  933. Py_DECREF(addr);
  934. goto error;
  935. }
  936. Py_DECREF(addr);
  937. return _PyUnicodeWriter_Finish(&writer);
  938. error:
  939. _PyUnicodeWriter_Dealloc(&writer);
  940. return NULL;
  941. }
  942. static PyObject *
  943. token_get_var(PyContextToken *self, void *Py_UNUSED(ignored))
  944. {
  945. return Py_NewRef(self->tok_var);;
  946. }
  947. static PyObject *
  948. token_get_old_value(PyContextToken *self, void *Py_UNUSED(ignored))
  949. {
  950. if (self->tok_oldval == NULL) {
  951. return get_token_missing();
  952. }
  953. return Py_NewRef(self->tok_oldval);
  954. }
  955. static PyGetSetDef PyContextTokenType_getsetlist[] = {
  956. {"var", (getter)token_get_var, NULL, NULL},
  957. {"old_value", (getter)token_get_old_value, NULL, NULL},
  958. {NULL}
  959. };
  960. static PyMethodDef PyContextTokenType_methods[] = {
  961. {"__class_getitem__", Py_GenericAlias,
  962. METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  963. {NULL}
  964. };
  965. PyTypeObject PyContextToken_Type = {
  966. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  967. "_contextvars.Token",
  968. sizeof(PyContextToken),
  969. .tp_methods = PyContextTokenType_methods,
  970. .tp_getset = PyContextTokenType_getsetlist,
  971. .tp_dealloc = (destructor)token_tp_dealloc,
  972. .tp_getattro = PyObject_GenericGetAttr,
  973. .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
  974. .tp_traverse = (traverseproc)token_tp_traverse,
  975. .tp_clear = (inquiry)token_tp_clear,
  976. .tp_new = token_tp_new,
  977. .tp_free = PyObject_GC_Del,
  978. .tp_hash = PyObject_HashNotImplemented,
  979. .tp_repr = (reprfunc)token_tp_repr,
  980. };
  981. static PyContextToken *
  982. token_new(PyContext *ctx, PyContextVar *var, PyObject *val)
  983. {
  984. PyContextToken *tok = PyObject_GC_New(PyContextToken, &PyContextToken_Type);
  985. if (tok == NULL) {
  986. return NULL;
  987. }
  988. tok->tok_ctx = (PyContext*)Py_NewRef(ctx);
  989. tok->tok_var = (PyContextVar*)Py_NewRef(var);
  990. tok->tok_oldval = Py_XNewRef(val);
  991. tok->tok_used = 0;
  992. PyObject_GC_Track(tok);
  993. return tok;
  994. }
  995. /////////////////////////// Token.MISSING
  996. static PyObject *
  997. context_token_missing_tp_repr(PyObject *self)
  998. {
  999. return PyUnicode_FromString("<Token.MISSING>");
  1000. }
  1001. static void
  1002. context_token_missing_tp_dealloc(_PyContextTokenMissing *Py_UNUSED(self))
  1003. {
  1004. #ifdef Py_DEBUG
  1005. /* The singleton is statically allocated. */
  1006. _Py_FatalRefcountError("deallocating the token missing singleton");
  1007. #else
  1008. return;
  1009. #endif
  1010. }
  1011. PyTypeObject _PyContextTokenMissing_Type = {
  1012. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1013. "Token.MISSING",
  1014. sizeof(_PyContextTokenMissing),
  1015. .tp_dealloc = (destructor)context_token_missing_tp_dealloc,
  1016. .tp_getattro = PyObject_GenericGetAttr,
  1017. .tp_flags = Py_TPFLAGS_DEFAULT,
  1018. .tp_repr = context_token_missing_tp_repr,
  1019. };
  1020. static PyObject *
  1021. get_token_missing(void)
  1022. {
  1023. return Py_NewRef(&_Py_SINGLETON(context_token_missing));
  1024. }
  1025. ///////////////////////////
  1026. void
  1027. _PyContext_ClearFreeList(PyInterpreterState *interp)
  1028. {
  1029. #if PyContext_MAXFREELIST > 0
  1030. struct _Py_context_state *state = &interp->context;
  1031. for (; state->numfree; state->numfree--) {
  1032. PyContext *ctx = state->freelist;
  1033. state->freelist = (PyContext *)ctx->ctx_weakreflist;
  1034. ctx->ctx_weakreflist = NULL;
  1035. PyObject_GC_Del(ctx);
  1036. }
  1037. #endif
  1038. }
  1039. void
  1040. _PyContext_Fini(PyInterpreterState *interp)
  1041. {
  1042. _PyContext_ClearFreeList(interp);
  1043. #if defined(Py_DEBUG) && PyContext_MAXFREELIST > 0
  1044. struct _Py_context_state *state = &interp->context;
  1045. state->numfree = -1;
  1046. #endif
  1047. }
  1048. PyStatus
  1049. _PyContext_Init(PyInterpreterState *interp)
  1050. {
  1051. if (!_Py_IsMainInterpreter(interp)) {
  1052. return _PyStatus_OK();
  1053. }
  1054. PyObject *missing = get_token_missing();
  1055. if (PyDict_SetItemString(
  1056. _PyType_GetDict(&PyContextToken_Type), "MISSING", missing))
  1057. {
  1058. Py_DECREF(missing);
  1059. return _PyStatus_ERR("can't init context types");
  1060. }
  1061. Py_DECREF(missing);
  1062. return _PyStatus_OK();
  1063. }