greenlet.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
  2. /* Format with:
  3. * clang-format -i --style=file src/greenlet/greenlet.c
  4. *
  5. *
  6. * Fix missing braces with:
  7. * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
  8. */
  9. #define GREENLET_MODULE
  10. #include "greenlet.h"
  11. #include "structmember.h"
  12. #ifdef __clang__
  13. # pragma clang diagnostic push
  14. # pragma clang diagnostic ignored "-Wunused-parameter"
  15. # pragma clang diagnostic ignored "-Wmissing-field-initializers"
  16. #endif
  17. /***********************************************************
  18. A PyGreenlet is a range of C stack addresses that must be
  19. saved and restored in such a way that the full range of the
  20. stack contains valid data when we switch to it.
  21. Stack layout for a greenlet:
  22. | ^^^ |
  23. | older data |
  24. | |
  25. stack_stop . |_______________|
  26. . | |
  27. . | greenlet data |
  28. . | in stack |
  29. . * |_______________| . . _____________ stack_copy + stack_saved
  30. . | | | |
  31. . | data | |greenlet data|
  32. . | unrelated | | saved |
  33. . | to | | in heap |
  34. stack_start . | this | . . |_____________| stack_copy
  35. | greenlet |
  36. | |
  37. | newer data |
  38. | vvv |
  39. Note that a greenlet's stack data is typically partly at its correct
  40. place in the stack, and partly saved away in the heap, but always in
  41. the above configuration: two blocks, the more recent one in the heap
  42. and the older one still in the stack (either block may be empty).
  43. Greenlets are chained: each points to the previous greenlet, which is
  44. the one that owns the data currently in the C stack above my
  45. stack_stop. The currently running greenlet is the first element of
  46. this chain. The main (initial) greenlet is the last one. Greenlets
  47. whose stack is entirely in the heap can be skipped from the chain.
  48. The chain is not related to execution order, but only to the order
  49. in which bits of C stack happen to belong to greenlets at a particular
  50. point in time.
  51. The main greenlet doesn't have a stack_stop: it is responsible for the
  52. complete rest of the C stack, and we don't know where it begins. We
  53. use (char*) -1, the largest possible address.
  54. States:
  55. stack_stop == NULL && stack_start == NULL: did not start yet
  56. stack_stop != NULL && stack_start == NULL: already finished
  57. stack_stop != NULL && stack_start != NULL: active
  58. The running greenlet's stack_start is undefined but not NULL.
  59. ***********************************************************/
  60. /*** global state ***/
  61. /* In the presence of multithreading, this is a bit tricky:
  62. - ts_current always store a reference to a greenlet, but it is
  63. not really the current greenlet after a thread switch occurred.
  64. - each *running* greenlet uses its run_info field to know which
  65. thread it is attached to. A greenlet can only run in the thread
  66. where it was created. This run_info is a ref to tstate->dict.
  67. - the thread state dict is used to save and restore ts_current,
  68. using the dictionary key 'ts_curkey'.
  69. */
  70. extern PyTypeObject PyGreenlet_Type;
  71. #if PY_VERSION_HEX >= 0x030700A3
  72. # define GREENLET_PY37 1
  73. #else
  74. # define GREENLET_PY37 0
  75. #endif
  76. #if PY_VERSION_HEX >= 0x30A00B1
  77. /*
  78. Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
  79. See https://github.com/python/cpython/pull/25276
  80. We have to save and restore this as well.
  81. */
  82. #define TSTATE_USE_TRACING(tstate) (tstate->cframe->use_tracing)
  83. #define GREENLET_USE_CFRAME 1
  84. #else
  85. #define TSTATE_USE_TRACING(tstate) (tstate->use_tracing)
  86. #define GREENLET_USE_CFRAME 0
  87. #endif
  88. #ifndef Py_SET_REFCNT
  89. /* Py_REFCNT and Py_SIZE macros are converted to functions
  90. https://bugs.python.org/issue39573 */
  91. # define Py_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt)
  92. #endif
  93. #ifndef _Py_DEC_REFTOTAL
  94. /* _Py_DEC_REFTOTAL macro has been removed from Python 3.9 by:
  95. https://github.com/python/cpython/commit/49932fec62c616ec88da52642339d83ae719e924
  96. */
  97. # ifdef Py_REF_DEBUG
  98. # define _Py_DEC_REFTOTAL _Py_RefTotal--
  99. # else
  100. # define _Py_DEC_REFTOTAL
  101. # endif
  102. #endif
  103. /* Weak reference to the switching-to greenlet during the slp switch */
  104. static PyGreenlet* volatile ts_target = NULL;
  105. /* Strong reference to the switching from greenlet after the switch */
  106. static PyGreenlet* volatile ts_origin = NULL;
  107. /* Strong reference to the current greenlet in this thread state */
  108. static PyGreenlet* volatile ts_current = NULL;
  109. /* NULL if error, otherwise args tuple to pass around during slp switch */
  110. static PyObject* volatile ts_passaround_args = NULL;
  111. static PyObject* volatile ts_passaround_kwargs = NULL;
  112. /* Used internally in ``g_switchstack()`` */
  113. #if GREENLET_USE_CFRAME
  114. static int volatile ts__g_switchstack_use_tracing = 0;
  115. #endif
  116. /***********************************************************/
  117. /* Thread-aware routines, switching global variables when needed */
  118. #define STATE_OK \
  119. (ts_current->run_info == PyThreadState_GET()->dict || \
  120. !green_updatecurrent())
  121. static PyObject* ts_curkey;
  122. static PyObject* ts_delkey;
  123. static PyObject* ts_tracekey;
  124. static PyObject* ts_event_switch;
  125. static PyObject* ts_event_throw;
  126. static PyObject* PyExc_GreenletError;
  127. static PyObject* PyExc_GreenletExit;
  128. static PyObject* ts_empty_tuple;
  129. static PyObject* ts_empty_dict;
  130. #define GREENLET_GC_FLAGS Py_TPFLAGS_HAVE_GC
  131. #define GREENLET_tp_alloc PyType_GenericAlloc
  132. #define GREENLET_tp_free PyObject_GC_Del
  133. #define GREENLET_tp_traverse green_traverse
  134. #define GREENLET_tp_clear green_clear
  135. #define GREENLET_tp_is_gc green_is_gc
  136. static void
  137. green_clear_exc(PyGreenlet* g)
  138. {
  139. #if GREENLET_PY37
  140. g->exc_info = NULL;
  141. g->exc_state.exc_type = NULL;
  142. g->exc_state.exc_value = NULL;
  143. g->exc_state.exc_traceback = NULL;
  144. g->exc_state.previous_item = NULL;
  145. #else
  146. g->exc_type = NULL;
  147. g->exc_value = NULL;
  148. g->exc_traceback = NULL;
  149. #endif
  150. }
  151. static PyGreenlet*
  152. green_create_main(void)
  153. {
  154. PyGreenlet* gmain;
  155. PyObject* dict = PyThreadState_GetDict();
  156. if (dict == NULL) {
  157. if (!PyErr_Occurred()) {
  158. PyErr_NoMemory();
  159. }
  160. return NULL;
  161. }
  162. /* create the main greenlet for this thread */
  163. gmain = (PyGreenlet*)PyType_GenericAlloc(&PyGreenlet_Type, 0);
  164. if (gmain == NULL) {
  165. return NULL;
  166. }
  167. gmain->stack_start = (char*)1;
  168. gmain->stack_stop = (char*)-1;
  169. /* GetDict() returns a borrowed reference. Make it strong. */
  170. gmain->run_info = dict;
  171. Py_INCREF(dict);
  172. return gmain;
  173. }
  174. static int
  175. green_updatecurrent(void)
  176. {
  177. PyObject *exc, *val, *tb;
  178. PyThreadState* tstate;
  179. PyGreenlet* current;
  180. PyGreenlet* previous;
  181. PyObject* deleteme;
  182. green_updatecurrent_restart:
  183. /* save current exception */
  184. PyErr_Fetch(&exc, &val, &tb);
  185. /* get ts_current from the active tstate */
  186. tstate = PyThreadState_GET();
  187. if (tstate->dict &&
  188. (current = (PyGreenlet*)PyDict_GetItem(tstate->dict, ts_curkey))) {
  189. /* found -- remove it, to avoid keeping a ref */
  190. Py_INCREF(current);
  191. PyDict_DelItem(tstate->dict, ts_curkey);
  192. }
  193. else {
  194. /* first time we see this tstate */
  195. current = green_create_main();
  196. if (current == NULL) {
  197. Py_XDECREF(exc);
  198. Py_XDECREF(val);
  199. Py_XDECREF(tb);
  200. return -1;
  201. }
  202. }
  203. assert(current->run_info == tstate->dict);
  204. green_updatecurrent_retry:
  205. /* update ts_current as soon as possible, in case of nested switches */
  206. Py_INCREF(current);
  207. previous = ts_current;
  208. ts_current = current;
  209. /* save ts_current as the current greenlet of its own thread */
  210. if (PyDict_SetItem(previous->run_info, ts_curkey, (PyObject*)previous)) {
  211. Py_DECREF(previous);
  212. Py_DECREF(current);
  213. Py_XDECREF(exc);
  214. Py_XDECREF(val);
  215. Py_XDECREF(tb);
  216. return -1;
  217. }
  218. Py_DECREF(previous);
  219. /* green_dealloc() cannot delete greenlets from other threads, so
  220. it stores them in the thread dict; delete them now. */
  221. deleteme = PyDict_GetItem(tstate->dict, ts_delkey);
  222. if (deleteme != NULL) {
  223. /* The only reference to these greenlets should be in this list, so
  224. clearing the list should let them be deleted again, triggering
  225. calls to green_dealloc() in the correct thread. This may run
  226. arbitrary Python code?
  227. */
  228. PyList_SetSlice(deleteme, 0, INT_MAX, NULL);
  229. }
  230. if (ts_current != current) {
  231. /* some Python code executed above and there was a thread switch,
  232. * so ts_current points to some other thread again. We need to
  233. * delete ts_curkey (it's likely there) and retry. */
  234. PyDict_DelItem(tstate->dict, ts_curkey);
  235. goto green_updatecurrent_retry;
  236. }
  237. /* release an extra reference */
  238. Py_DECREF(current);
  239. /* restore current exception */
  240. PyErr_Restore(exc, val, tb);
  241. /* thread switch could happen during PyErr_Restore, in that
  242. case there's nothing to do except restart from scratch. */
  243. if (ts_current->run_info != tstate->dict) {
  244. goto green_updatecurrent_restart;
  245. }
  246. return 0;
  247. }
  248. static PyObject*
  249. green_statedict(PyGreenlet* g)
  250. {
  251. while (!PyGreenlet_STARTED(g)) {
  252. g = g->parent;
  253. if (g == NULL) {
  254. /* garbage collected greenlet in chain */
  255. return NULL;
  256. }
  257. }
  258. return g->run_info;
  259. }
  260. /***********************************************************/
  261. /* Some functions must not be inlined:
  262. * slp_restore_state, when inlined into slp_switch might cause
  263. it to restore stack over its own local variables
  264. * slp_save_state, when inlined would add its own local
  265. variables to the saved stack, wasting space
  266. * slp_switch, cannot be inlined for obvious reasons
  267. * g_initialstub, when inlined would receive a pointer into its
  268. own stack frame, leading to incomplete stack save/restore
  269. */
  270. #if defined(__GNUC__) && \
  271. (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
  272. # define GREENLET_NOINLINE_SUPPORTED
  273. # define GREENLET_NOINLINE(name) __attribute__((noinline)) name
  274. #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
  275. # define GREENLET_NOINLINE_SUPPORTED
  276. # define GREENLET_NOINLINE(name) __declspec(noinline) name
  277. #endif
  278. #ifdef GREENLET_NOINLINE_SUPPORTED
  279. /* add forward declarations */
  280. static void GREENLET_NOINLINE(slp_restore_state)(void);
  281. static int GREENLET_NOINLINE(slp_save_state)(char*);
  282. # if !(defined(MS_WIN64) && defined(_M_X64))
  283. static int GREENLET_NOINLINE(slp_switch)(void);
  284. # endif
  285. static int GREENLET_NOINLINE(g_initialstub)(void*);
  286. # define GREENLET_NOINLINE_INIT() \
  287. do { \
  288. } while (0)
  289. #else
  290. /* force compiler to call functions via pointers */
  291. static void (*slp_restore_state)(void);
  292. static int (*slp_save_state)(char*);
  293. static int (*slp_switch)(void);
  294. static int (*g_initialstub)(void*);
  295. # define GREENLET_NOINLINE(name) cannot_inline_##name
  296. # define GREENLET_NOINLINE_INIT() \
  297. do { \
  298. slp_restore_state = GREENLET_NOINLINE(slp_restore_state); \
  299. slp_save_state = GREENLET_NOINLINE(slp_save_state); \
  300. slp_switch = GREENLET_NOINLINE(slp_switch); \
  301. g_initialstub = GREENLET_NOINLINE(g_initialstub); \
  302. } while (0)
  303. #endif
  304. /*
  305. * the following macros are spliced into the OS/compiler
  306. * specific code, in order to simplify maintenance.
  307. */
  308. #define SLP_SAVE_STATE(stackref, stsizediff) \
  309. stackref += STACK_MAGIC; \
  310. if (slp_save_state((char*)stackref)) \
  311. return -1; \
  312. if (!PyGreenlet_ACTIVE(ts_target)) \
  313. return 1; \
  314. stsizediff = ts_target->stack_start - (char*)stackref
  315. #define SLP_RESTORE_STATE() slp_restore_state()
  316. #define SLP_EVAL
  317. #define slp_switch GREENLET_NOINLINE(slp_switch)
  318. #include "slp_platformselect.h"
  319. #undef slp_switch
  320. #ifndef STACK_MAGIC
  321. # error \
  322. "greenlet needs to be ported to this platform, or taught how to detect your compiler properly."
  323. #endif /* !STACK_MAGIC */
  324. #ifdef EXTERNAL_ASM
  325. /* CCP addition: Make these functions, to be called from assembler.
  326. * The token include file for the given platform should enable the
  327. * EXTERNAL_ASM define so that this is included.
  328. */
  329. intptr_t
  330. slp_save_state_asm(intptr_t* ref)
  331. {
  332. intptr_t diff;
  333. SLP_SAVE_STATE(ref, diff);
  334. return diff;
  335. }
  336. void
  337. slp_restore_state_asm(void)
  338. {
  339. SLP_RESTORE_STATE();
  340. }
  341. extern int
  342. slp_switch(void);
  343. #endif
  344. /***********************************************************/
  345. static int
  346. g_save(PyGreenlet* g, char* stop)
  347. {
  348. /* Save more of g's stack into the heap -- at least up to 'stop'
  349. g->stack_stop |________|
  350. | |
  351. | __ stop . . . . .
  352. | | ==> . .
  353. |________| _______
  354. | | | |
  355. | | | |
  356. g->stack_start | | |_______| g->stack_copy
  357. */
  358. intptr_t sz1 = g->stack_saved;
  359. intptr_t sz2 = stop - g->stack_start;
  360. assert(g->stack_start != NULL);
  361. if (sz2 > sz1) {
  362. char* c = (char*)PyMem_Realloc(g->stack_copy, sz2);
  363. if (!c) {
  364. PyErr_NoMemory();
  365. return -1;
  366. }
  367. memcpy(c + sz1, g->stack_start + sz1, sz2 - sz1);
  368. g->stack_copy = c;
  369. g->stack_saved = sz2;
  370. }
  371. return 0;
  372. }
  373. static void GREENLET_NOINLINE(slp_restore_state)(void)
  374. {
  375. PyGreenlet* g = ts_target;
  376. PyGreenlet* owner = ts_current;
  377. #ifdef SLP_BEFORE_RESTORE_STATE
  378. SLP_BEFORE_RESTORE_STATE();
  379. #endif
  380. /* Restore the heap copy back into the C stack */
  381. if (g->stack_saved != 0) {
  382. memcpy(g->stack_start, g->stack_copy, g->stack_saved);
  383. PyMem_Free(g->stack_copy);
  384. g->stack_copy = NULL;
  385. g->stack_saved = 0;
  386. }
  387. if (owner->stack_start == NULL) {
  388. owner = owner->stack_prev; /* greenlet is dying, skip it */
  389. }
  390. while (owner && owner->stack_stop <= g->stack_stop) {
  391. owner = owner->stack_prev; /* find greenlet with more stack */
  392. }
  393. g->stack_prev = owner;
  394. }
  395. static int GREENLET_NOINLINE(slp_save_state)(char* stackref)
  396. {
  397. /* must free all the C stack up to target_stop */
  398. char* target_stop = ts_target->stack_stop;
  399. PyGreenlet* owner = ts_current;
  400. assert(owner->stack_saved == 0);
  401. if (owner->stack_start == NULL) {
  402. owner = owner->stack_prev; /* not saved if dying */
  403. }
  404. else {
  405. owner->stack_start = stackref;
  406. }
  407. #ifdef SLP_BEFORE_SAVE_STATE
  408. SLP_BEFORE_SAVE_STATE();
  409. #endif
  410. while (owner->stack_stop < target_stop) {
  411. /* ts_current is entierely within the area to free */
  412. if (g_save(owner, owner->stack_stop)) {
  413. return -1; /* XXX */
  414. }
  415. owner = owner->stack_prev;
  416. }
  417. if (owner != ts_target) {
  418. if (g_save(owner, target_stop)) {
  419. return -1; /* XXX */
  420. }
  421. }
  422. return 0;
  423. }
  424. /**
  425. Perform a stack switch according to some global variables
  426. that must be set before calling this function. Those variables
  427. are:
  428. - ts_current: current greenlet (holds a reference)
  429. - ts_target: greenlet to switch to (weak reference)
  430. - ts_passaround_args: NULL if PyErr_Occurred(),
  431. else a tuple of args sent to ts_target (holds a reference)
  432. - ts_passaround_kwargs: switch kwargs (holds a reference)
  433. Because the stack switch happens in this function, this function can't use
  434. its own stack (local) variables, set before the switch, and then accessed after the
  435. switch. Global variables beginning with ``ts__g_switchstack`` are used
  436. internally instead.
  437. On return results are passed via global variables as well:
  438. - ts_origin: originating greenlet (holds a reference)
  439. - ts_current: current greenlet (holds a reference)
  440. - ts_passaround_args: NULL if PyErr_Occurred(),
  441. else a tuple of args sent to ts_current (holds a reference)
  442. - ts_passaround_kwargs: switch kwargs (holds a reference)
  443. It is very important that stack switch is 'atomic', i.e. no
  444. calls into other Python code allowed (except very few that
  445. are safe), because global variables are very fragile.
  446. */
  447. static int
  448. g_switchstack(void)
  449. {
  450. int err;
  451. { /* save state */
  452. PyGreenlet* current = ts_current;
  453. PyThreadState* tstate = PyThreadState_GET();
  454. current->recursion_depth = tstate->recursion_depth;
  455. current->top_frame = tstate->frame;
  456. #if GREENLET_PY37
  457. current->context = tstate->context;
  458. #endif
  459. #if GREENLET_PY37
  460. current->exc_info = tstate->exc_info;
  461. current->exc_state = tstate->exc_state;
  462. #else
  463. current->exc_type = tstate->exc_type;
  464. current->exc_value = tstate->exc_value;
  465. current->exc_traceback = tstate->exc_traceback;
  466. #endif
  467. #if GREENLET_USE_CFRAME
  468. /*
  469. IMPORTANT: ``cframe`` is a pointer into the STACK.
  470. Thus, because the call to ``slp_switch()``
  471. changes the contents of the stack, you cannot read from
  472. ``ts_current->cframe`` after that call and necessarily
  473. get the same values you get from reading it here. Anything
  474. you need to restore from now to then must be saved
  475. in a global variable (because we can't use stack variables
  476. here either).
  477. */
  478. current->cframe = tstate->cframe;
  479. ts__g_switchstack_use_tracing = tstate->cframe->use_tracing;
  480. #endif
  481. }
  482. err = slp_switch();
  483. if (err < 0) { /* error */
  484. PyGreenlet* current = ts_current;
  485. current->top_frame = NULL;
  486. #if GREENLET_PY37
  487. green_clear_exc(current);
  488. #else
  489. current->exc_type = NULL;
  490. current->exc_value = NULL;
  491. current->exc_traceback = NULL;
  492. #endif
  493. assert(ts_origin == NULL);
  494. ts_target = NULL;
  495. }
  496. else {
  497. PyGreenlet* target = ts_target;
  498. PyGreenlet* origin = ts_current;
  499. PyThreadState* tstate = PyThreadState_GET();
  500. tstate->recursion_depth = target->recursion_depth;
  501. tstate->frame = target->top_frame;
  502. target->top_frame = NULL;
  503. #if GREENLET_PY37
  504. tstate->context = target->context;
  505. target->context = NULL;
  506. /* Incrementing this value invalidates the contextvars cache,
  507. which would otherwise remain valid across switches */
  508. tstate->context_ver++;
  509. #endif
  510. #if GREENLET_PY37
  511. tstate->exc_state = target->exc_state;
  512. tstate->exc_info =
  513. target->exc_info ? target->exc_info : &tstate->exc_state;
  514. #else
  515. tstate->exc_type = target->exc_type;
  516. tstate->exc_value = target->exc_value;
  517. tstate->exc_traceback = target->exc_traceback;
  518. #endif
  519. green_clear_exc(target);
  520. #if GREENLET_USE_CFRAME
  521. tstate->cframe = target->cframe;
  522. /*
  523. If we were tracing, we need to keep tracing.
  524. There should never be the possibility of hitting the
  525. root_cframe here. See note above about why we can't
  526. just copy this from ``origin->cframe->use_tracing``.
  527. */
  528. tstate->cframe->use_tracing = ts__g_switchstack_use_tracing;
  529. #endif
  530. assert(ts_origin == NULL);
  531. Py_INCREF(target);
  532. ts_current = target;
  533. ts_origin = origin;
  534. ts_target = NULL;
  535. }
  536. return err;
  537. }
  538. static int
  539. g_calltrace(PyObject* tracefunc, PyObject* event, PyGreenlet* origin,
  540. PyGreenlet* target)
  541. {
  542. PyObject* retval;
  543. PyObject *exc_type, *exc_val, *exc_tb;
  544. PyThreadState* tstate;
  545. PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
  546. tstate = PyThreadState_GET();
  547. tstate->tracing++;
  548. TSTATE_USE_TRACING(tstate) = 0;
  549. retval = PyObject_CallFunction(tracefunc, "O(OO)", event, origin, target);
  550. tstate->tracing--;
  551. TSTATE_USE_TRACING(tstate) =
  552. (tstate->tracing <= 0 &&
  553. ((tstate->c_tracefunc != NULL) || (tstate->c_profilefunc != NULL)));
  554. if (retval == NULL) {
  555. /* In case of exceptions trace function is removed */
  556. if (PyDict_GetItem(tstate->dict, ts_tracekey)) {
  557. PyDict_DelItem(tstate->dict, ts_tracekey);
  558. }
  559. Py_XDECREF(exc_type);
  560. Py_XDECREF(exc_val);
  561. Py_XDECREF(exc_tb);
  562. return -1;
  563. }
  564. else {
  565. Py_DECREF(retval);
  566. }
  567. PyErr_Restore(exc_type, exc_val, exc_tb);
  568. return 0;
  569. }
  570. static PyObject*
  571. g_switch(PyGreenlet* target, PyObject* args, PyObject* kwargs)
  572. {
  573. /* _consumes_ a reference to the args tuple and kwargs dict,
  574. and return a new tuple reference */
  575. int err = 0;
  576. PyObject* run_info;
  577. /* check ts_current */
  578. if (!STATE_OK) {
  579. Py_XDECREF(args);
  580. Py_XDECREF(kwargs);
  581. return NULL;
  582. }
  583. run_info = green_statedict(target);
  584. if (run_info == NULL || run_info != ts_current->run_info) {
  585. Py_XDECREF(args);
  586. Py_XDECREF(kwargs);
  587. PyErr_SetString(PyExc_GreenletError,
  588. run_info ?
  589. "cannot switch to a different thread" :
  590. "cannot switch to a garbage collected greenlet");
  591. return NULL;
  592. }
  593. ts_passaround_args = args;
  594. ts_passaround_kwargs = kwargs;
  595. /* find the real target by ignoring dead greenlets,
  596. and if necessary starting a greenlet. */
  597. while (target) {
  598. if (PyGreenlet_ACTIVE(target)) {
  599. ts_target = target;
  600. err = g_switchstack();
  601. break;
  602. }
  603. if (!PyGreenlet_STARTED(target)) {
  604. void* dummymarker;
  605. ts_target = target;
  606. err = g_initialstub(&dummymarker);
  607. if (err == 1) {
  608. continue; /* retry the switch */
  609. }
  610. break;
  611. }
  612. target = target->parent;
  613. }
  614. /* For a very short time, immediately after the 'atomic'
  615. g_switchstack() call, global variables are in a known state.
  616. We need to save everything we need, before it is destroyed
  617. by calls into arbitrary Python code. */
  618. args = ts_passaround_args;
  619. ts_passaround_args = NULL;
  620. kwargs = ts_passaround_kwargs;
  621. ts_passaround_kwargs = NULL;
  622. if (err < 0) {
  623. /* Turn switch errors into switch throws */
  624. assert(ts_origin == NULL);
  625. Py_CLEAR(kwargs);
  626. Py_CLEAR(args);
  627. }
  628. else {
  629. PyGreenlet* origin;
  630. PyGreenlet* current;
  631. PyObject* tracefunc;
  632. origin = ts_origin;
  633. ts_origin = NULL;
  634. current = ts_current;
  635. if ((tracefunc = PyDict_GetItem(current->run_info, ts_tracekey)) != NULL) {
  636. Py_INCREF(tracefunc);
  637. if (g_calltrace(tracefunc,
  638. args ? ts_event_switch : ts_event_throw,
  639. origin,
  640. current) < 0) {
  641. /* Turn trace errors into switch throws */
  642. Py_CLEAR(kwargs);
  643. Py_CLEAR(args);
  644. }
  645. Py_DECREF(tracefunc);
  646. }
  647. Py_DECREF(origin);
  648. }
  649. /* We need to figure out what values to pass to the target greenlet
  650. based on the arguments that have been passed to greenlet.switch(). If
  651. switch() was just passed an arg tuple, then we'll just return that.
  652. If only keyword arguments were passed, then we'll pass the keyword
  653. argument dict. Otherwise, we'll create a tuple of (args, kwargs) and
  654. return both. */
  655. if (kwargs == NULL) {
  656. return args;
  657. }
  658. else if (PyDict_Size(kwargs) == 0) {
  659. Py_DECREF(kwargs);
  660. return args;
  661. }
  662. else if (PySequence_Length(args) == 0) {
  663. Py_DECREF(args);
  664. return kwargs;
  665. }
  666. else {
  667. PyObject* tuple = PyTuple_New(2);
  668. if (tuple == NULL) {
  669. Py_DECREF(args);
  670. Py_DECREF(kwargs);
  671. return NULL;
  672. }
  673. PyTuple_SET_ITEM(tuple, 0, args);
  674. PyTuple_SET_ITEM(tuple, 1, kwargs);
  675. return tuple;
  676. }
  677. }
  678. static PyObject*
  679. g_handle_exit(PyObject* result)
  680. {
  681. if (result == NULL && PyErr_ExceptionMatches(PyExc_GreenletExit)) {
  682. /* catch and ignore GreenletExit */
  683. PyObject *exc, *val, *tb;
  684. PyErr_Fetch(&exc, &val, &tb);
  685. if (val == NULL) {
  686. Py_INCREF(Py_None);
  687. val = Py_None;
  688. }
  689. result = val;
  690. Py_DECREF(exc);
  691. Py_XDECREF(tb);
  692. }
  693. if (result != NULL) {
  694. /* package the result into a 1-tuple */
  695. PyObject* r = result;
  696. result = PyTuple_New(1);
  697. if (result) {
  698. PyTuple_SET_ITEM(result, 0, r);
  699. }
  700. else {
  701. Py_DECREF(r);
  702. }
  703. }
  704. return result;
  705. }
  706. static int GREENLET_NOINLINE(g_initialstub)(void* mark)
  707. {
  708. int err;
  709. PyObject *o, *run;
  710. PyObject *exc, *val, *tb;
  711. PyObject* run_info;
  712. PyGreenlet* self = ts_target;
  713. PyObject* args = ts_passaround_args;
  714. PyObject* kwargs = ts_passaround_kwargs;
  715. #if GREENLET_USE_CFRAME
  716. /*
  717. See green_new(). This is a stack-allocated variable used
  718. while *self* is in PyObject_Call().
  719. We want to defer copying the state info until we're sure
  720. we need it and are in a stable place to do so.
  721. */
  722. CFrame trace_info;
  723. #endif
  724. /* save exception in case getattr clears it */
  725. PyErr_Fetch(&exc, &val, &tb);
  726. /* self.run is the object to call in the new greenlet */
  727. run = PyObject_GetAttrString((PyObject*)self, "run");
  728. if (run == NULL) {
  729. Py_XDECREF(exc);
  730. Py_XDECREF(val);
  731. Py_XDECREF(tb);
  732. return -1;
  733. }
  734. /* restore saved exception */
  735. PyErr_Restore(exc, val, tb);
  736. /* recheck the state in case getattr caused thread switches */
  737. if (!STATE_OK) {
  738. Py_DECREF(run);
  739. return -1;
  740. }
  741. /* recheck run_info in case greenlet reparented anywhere above */
  742. run_info = green_statedict(self);
  743. if (run_info == NULL || run_info != ts_current->run_info) {
  744. Py_DECREF(run);
  745. PyErr_SetString(PyExc_GreenletError,
  746. run_info ?
  747. "cannot switch to a different thread" :
  748. "cannot switch to a garbage collected greenlet");
  749. return -1;
  750. }
  751. /* by the time we got here another start could happen elsewhere,
  752. * that means it should now be a regular switch
  753. */
  754. if (PyGreenlet_STARTED(self)) {
  755. Py_DECREF(run);
  756. ts_passaround_args = args;
  757. ts_passaround_kwargs = kwargs;
  758. return 1;
  759. }
  760. #if GREENLET_USE_CFRAME
  761. /* OK, we need it, we're about to switch greenlets, save the state. */
  762. trace_info = *PyThreadState_GET()->cframe;
  763. /* Make the target greenlet refer to the stack value. */
  764. self->cframe = &trace_info;
  765. /*
  766. And restore the link to the previous frame so this one gets
  767. unliked appropriately.
  768. */
  769. self->cframe->previous = &PyThreadState_GET()->root_cframe;
  770. #endif
  771. /* start the greenlet */
  772. self->stack_start = NULL;
  773. self->stack_stop = (char*)mark;
  774. if (ts_current->stack_start == NULL) {
  775. /* ts_current is dying */
  776. self->stack_prev = ts_current->stack_prev;
  777. }
  778. else {
  779. self->stack_prev = ts_current;
  780. }
  781. self->top_frame = NULL;
  782. green_clear_exc(self);
  783. self->recursion_depth = PyThreadState_GET()->recursion_depth;
  784. /* restore arguments in case they are clobbered */
  785. ts_target = self;
  786. ts_passaround_args = args;
  787. ts_passaround_kwargs = kwargs;
  788. /* perform the initial switch */
  789. err = g_switchstack();
  790. /* returns twice!
  791. The 1st time with ``err == 1``: we are in the new greenlet
  792. The 2nd time with ``err <= 0``: back in the caller's greenlet
  793. */
  794. if (err == 1) {
  795. /* in the new greenlet */
  796. PyGreenlet* origin;
  797. PyObject* tracefunc;
  798. PyObject* result;
  799. PyGreenlet* parent;
  800. self->stack_start = (char*)1; /* running */
  801. /* grab origin while we still can */
  802. origin = ts_origin;
  803. ts_origin = NULL;
  804. /* now use run_info to store the statedict */
  805. o = self->run_info;
  806. self->run_info = green_statedict(self->parent);
  807. Py_INCREF(self->run_info);
  808. Py_XDECREF(o);
  809. if ((tracefunc = PyDict_GetItem(self->run_info, ts_tracekey)) != NULL) {
  810. Py_INCREF(tracefunc);
  811. if (g_calltrace(tracefunc,
  812. args ? ts_event_switch : ts_event_throw,
  813. origin,
  814. self) < 0) {
  815. /* Turn trace errors into switch throws */
  816. Py_CLEAR(kwargs);
  817. Py_CLEAR(args);
  818. }
  819. Py_DECREF(tracefunc);
  820. }
  821. Py_DECREF(origin);
  822. if (args == NULL) {
  823. /* pending exception */
  824. result = NULL;
  825. }
  826. else {
  827. /* call g.run(*args, **kwargs) */
  828. result = PyObject_Call(run, args, kwargs);
  829. Py_DECREF(args);
  830. Py_XDECREF(kwargs);
  831. }
  832. Py_DECREF(run);
  833. result = g_handle_exit(result);
  834. /* jump back to parent */
  835. self->stack_start = NULL; /* dead */
  836. for (parent = self->parent; parent != NULL; parent = parent->parent) {
  837. result = g_switch(parent, result, NULL);
  838. /* Return here means switch to parent failed,
  839. * in which case we throw *current* exception
  840. * to the next parent in chain.
  841. */
  842. assert(result == NULL);
  843. }
  844. /* We ran out of parents, cannot continue */
  845. PyErr_WriteUnraisable((PyObject*)self);
  846. Py_FatalError("greenlets cannot continue");
  847. }
  848. /* back in the parent */
  849. if (err < 0) {
  850. /* start failed badly, restore greenlet state */
  851. self->stack_start = NULL;
  852. self->stack_stop = NULL;
  853. self->stack_prev = NULL;
  854. }
  855. return err;
  856. }
  857. /***********************************************************/
  858. static PyObject*
  859. green_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
  860. {
  861. PyObject* o =
  862. PyBaseObject_Type.tp_new(type, ts_empty_tuple, ts_empty_dict);
  863. if (o != NULL) {
  864. if (!STATE_OK) {
  865. Py_DECREF(o);
  866. return NULL;
  867. }
  868. Py_INCREF(ts_current);
  869. ((PyGreenlet*)o)->parent = ts_current;
  870. #if GREENLET_USE_CFRAME
  871. /*
  872. The PyThreadState->cframe pointer usually points to memory on the
  873. stack, alloceted in a call into PyEval_EvalFrameDefault.
  874. Initially, before any evaluation begins, it points to the initial
  875. PyThreadState object's ``root_cframe`` object, which is statically
  876. allocated for the lifetime of the thread.
  877. A greenlet can last for longer than a call to
  878. PyEval_EvalFrameDefault, so we can't set its ``cframe`` pointer to
  879. be the current ``PyThreadState->cframe``; nor could we use one from
  880. the greenlet parent for the same reason. Yet a further no: we can't
  881. allocate one scoped to the greenlet and then destroy it when the
  882. greenlet is deallocated, because inside the interpreter the CFrame
  883. objects form a linked list, and that too can result in accessing
  884. memory beyond its dynamic lifetime (if the greenlet doesn't actually
  885. finish before it dies, its entry could still be in the list).
  886. Using the ``root_cframe`` is problematic, though, because its
  887. members are never modified by the interpreter and are set to 0,
  888. meaning that its ``use_tracing`` flag is never updated. We don't
  889. want to modify that value in the ``root_cframe`` ourself: it
  890. *shouldn't* matter much because we should probably never get back to
  891. the point where that's the only cframe on the stack; even if it did
  892. matter, the major consequence of an incorrect value for
  893. ``use_tracing`` is that if its true the interpreter does some extra
  894. work --- however, it's just good code hygiene.
  895. Our solution: before a greenlet runs, after its initial creation,
  896. it uses the ``root_cframe`` just to have something to put there.
  897. However, once the greenlet is actually switched to for the first
  898. time, ``g_initialstub`` (which doesn't actually "return" while the
  899. greenlet is running) stores a new CFrame on its local stack, and
  900. copies the appropriate values from the currently running CFrame;
  901. this is then made the CFrame for the newly-minted greenlet.
  902. ``g_initialstub`` then proceeds to call ``glet.run()``, which
  903. results in ``PyEval_...`` adding the CFrame to the list. Switches
  904. continue as normal. Finally, when the greenlet finishes, the call to
  905. ``glet.run()`` returns and the CFrame is taken out of the linked
  906. list and the stack value is now unused and free to expire.
  907. */
  908. ((PyGreenlet*)o)->cframe = &PyThreadState_GET()->root_cframe;
  909. #endif
  910. }
  911. return o;
  912. }
  913. static int
  914. green_setrun(PyGreenlet* self, PyObject* nrun, void* c);
  915. static int
  916. green_setparent(PyGreenlet* self, PyObject* nparent, void* c);
  917. static int
  918. green_init(PyGreenlet* self, PyObject* args, PyObject* kwargs)
  919. {
  920. PyObject* run = NULL;
  921. PyObject* nparent = NULL;
  922. static char* kwlist[] = {"run", "parent", 0};
  923. if (!PyArg_ParseTupleAndKeywords(
  924. args, kwargs, "|OO:green", kwlist, &run, &nparent)) {
  925. return -1;
  926. }
  927. if (run != NULL) {
  928. if (green_setrun(self, run, NULL)) {
  929. return -1;
  930. }
  931. }
  932. if (nparent != NULL && nparent != Py_None) {
  933. return green_setparent(self, nparent, NULL);
  934. }
  935. return 0;
  936. }
  937. static int
  938. kill_greenlet(PyGreenlet* self)
  939. {
  940. /* Cannot raise an exception to kill the greenlet if
  941. it is not running in the same thread! */
  942. if (self->run_info == PyThreadState_GET()->dict) {
  943. /* The dying greenlet cannot be a parent of ts_current
  944. because the 'parent' field chain would hold a
  945. reference */
  946. PyObject* result;
  947. PyGreenlet* oldparent;
  948. PyGreenlet* tmp;
  949. if (!STATE_OK) {
  950. return -1;
  951. }
  952. oldparent = self->parent;
  953. self->parent = ts_current;
  954. Py_INCREF(self->parent);
  955. /* Send the greenlet a GreenletExit exception. */
  956. PyErr_SetNone(PyExc_GreenletExit);
  957. result = g_switch(self, NULL, NULL);
  958. tmp = self->parent;
  959. self->parent = oldparent;
  960. Py_XDECREF(tmp);
  961. if (result == NULL) {
  962. return -1;
  963. }
  964. Py_DECREF(result);
  965. return 0;
  966. }
  967. else {
  968. /* Not the same thread! Temporarily save the greenlet
  969. into its thread's ts_delkey list. */
  970. PyObject* lst;
  971. lst = PyDict_GetItem(self->run_info, ts_delkey);
  972. if (lst == NULL) {
  973. lst = PyList_New(0);
  974. if (lst == NULL
  975. || PyDict_SetItem(self->run_info, ts_delkey, lst) < 0) {
  976. return -1;
  977. }
  978. /* PyDict_SetItem now holds a strong reference. PyList_New also
  979. returned a fresh reference. We need to DECREF it now and let
  980. the dictionary keep sole ownership. Frow now on, we're working
  981. with a borrowed reference that will go away when the thread
  982. dies. */
  983. Py_DECREF(lst);
  984. }
  985. if (PyList_Append(lst, (PyObject*)self) < 0) {
  986. return -1;
  987. }
  988. if (!STATE_OK) { /* to force ts_delkey to be reconsidered */
  989. return -1;
  990. }
  991. return 0;
  992. }
  993. }
  994. static int
  995. green_traverse(PyGreenlet* self, visitproc visit, void* arg)
  996. {
  997. /* We must only visit referenced objects, i.e. only objects
  998. Py_INCREF'ed by this greenlet (directly or indirectly):
  999. - stack_prev is not visited: holds previous stack pointer, but it's not
  1000. referenced
  1001. - frames are not visited: alive greenlets are not garbage collected
  1002. anyway */
  1003. Py_VISIT((PyObject*)self->parent);
  1004. Py_VISIT(self->run_info);
  1005. #if GREENLET_PY37
  1006. Py_VISIT(self->context);
  1007. #endif
  1008. #if GREENLET_PY37
  1009. Py_VISIT(self->exc_state.exc_type);
  1010. Py_VISIT(self->exc_state.exc_value);
  1011. Py_VISIT(self->exc_state.exc_traceback);
  1012. #else
  1013. Py_VISIT(self->exc_type);
  1014. Py_VISIT(self->exc_value);
  1015. Py_VISIT(self->exc_traceback);
  1016. #endif
  1017. Py_VISIT(self->dict);
  1018. return 0;
  1019. }
  1020. static int
  1021. green_is_gc(PyGreenlet* self)
  1022. {
  1023. /* Main greenlet can be garbage collected since it can only
  1024. become unreachable if the underlying thread exited.
  1025. Active greenlet cannot be garbage collected, however. */
  1026. if (PyGreenlet_MAIN(self) || !PyGreenlet_ACTIVE(self)) {
  1027. return 1;
  1028. }
  1029. return 0;
  1030. }
  1031. static int
  1032. green_clear(PyGreenlet* self)
  1033. {
  1034. /* Greenlet is only cleared if it is about to be collected.
  1035. Since active greenlets are not garbage collectable, we can
  1036. be sure that, even if they are deallocated during clear,
  1037. nothing they reference is in unreachable or finalizers,
  1038. so even if it switches we are relatively safe. */
  1039. Py_CLEAR(self->parent);
  1040. Py_CLEAR(self->run_info);
  1041. #if GREENLET_PY37
  1042. Py_CLEAR(self->context);
  1043. #endif
  1044. #if GREENLET_PY37
  1045. Py_CLEAR(self->exc_state.exc_type);
  1046. Py_CLEAR(self->exc_state.exc_value);
  1047. Py_CLEAR(self->exc_state.exc_traceback);
  1048. #else
  1049. Py_CLEAR(self->exc_type);
  1050. Py_CLEAR(self->exc_value);
  1051. Py_CLEAR(self->exc_traceback);
  1052. #endif
  1053. Py_CLEAR(self->dict);
  1054. return 0;
  1055. }
  1056. static void
  1057. green_dealloc(PyGreenlet* self)
  1058. {
  1059. PyObject *error_type, *error_value, *error_traceback;
  1060. Py_ssize_t refcnt;
  1061. PyObject_GC_UnTrack(self);
  1062. if (PyGreenlet_ACTIVE(self) && self->run_info != NULL &&
  1063. !PyGreenlet_MAIN(self)) {
  1064. /* Hacks hacks hacks copied from instance_dealloc() */
  1065. /* Temporarily resurrect the greenlet. */
  1066. assert(Py_REFCNT(self) == 0);
  1067. Py_SET_REFCNT(self, 1);
  1068. /* Save the current exception, if any. */
  1069. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  1070. if (kill_greenlet(self) < 0) {
  1071. PyErr_WriteUnraisable((PyObject*)self);
  1072. /* XXX what else should we do? */
  1073. }
  1074. /* Check for no resurrection must be done while we keep
  1075. * our internal reference, otherwise PyFile_WriteObject
  1076. * causes recursion if using Py_INCREF/Py_DECREF
  1077. */
  1078. if (Py_REFCNT(self) == 1 && PyGreenlet_ACTIVE(self)) {
  1079. /* Not resurrected, but still not dead!
  1080. XXX what else should we do? we complain. */
  1081. PyObject* f = PySys_GetObject("stderr");
  1082. Py_INCREF(self); /* leak! */
  1083. if (f != NULL) {
  1084. PyFile_WriteString("GreenletExit did not kill ", f);
  1085. PyFile_WriteObject((PyObject*)self, f, 0);
  1086. PyFile_WriteString("\n", f);
  1087. }
  1088. }
  1089. /* Restore the saved exception. */
  1090. PyErr_Restore(error_type, error_value, error_traceback);
  1091. /* Undo the temporary resurrection; can't use DECREF here,
  1092. * it would cause a recursive call.
  1093. */
  1094. assert(Py_REFCNT(self) > 0);
  1095. refcnt = Py_REFCNT(self) - 1;
  1096. Py_SET_REFCNT(self, refcnt);
  1097. if (refcnt != 0) {
  1098. /* Resurrected! */
  1099. _Py_NewReference((PyObject*)self);
  1100. Py_SET_REFCNT(self, refcnt);
  1101. /* Better to use tp_finalizer slot (PEP 442)
  1102. * and call ``PyObject_CallFinalizerFromDealloc``,
  1103. * but that's only supported in Python 3.4+; see
  1104. * Modules/_io/iobase.c for an example.
  1105. *
  1106. * The following approach is copied from iobase.c in CPython 2.7.
  1107. * (along with much of this function in general). Here's their
  1108. * comment:
  1109. *
  1110. * When called from a heap type's dealloc, the type will be
  1111. * decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
  1112. if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) {
  1113. Py_INCREF(Py_TYPE(self));
  1114. }
  1115. PyObject_GC_Track((PyObject*)self);
  1116. _Py_DEC_REFTOTAL;
  1117. #ifdef COUNT_ALLOCS
  1118. --Py_TYPE(self)->tp_frees;
  1119. --Py_TYPE(self)->tp_allocs;
  1120. #endif /* COUNT_ALLOCS */
  1121. return;
  1122. }
  1123. }
  1124. if (self->weakreflist != NULL) {
  1125. PyObject_ClearWeakRefs((PyObject*)self);
  1126. }
  1127. Py_CLEAR(self->parent);
  1128. Py_CLEAR(self->run_info);
  1129. #if GREENLET_PY37
  1130. Py_CLEAR(self->context);
  1131. #endif
  1132. #if GREENLET_PY37
  1133. Py_CLEAR(self->exc_state.exc_type);
  1134. Py_CLEAR(self->exc_state.exc_value);
  1135. Py_CLEAR(self->exc_state.exc_traceback);
  1136. #else
  1137. Py_CLEAR(self->exc_type);
  1138. Py_CLEAR(self->exc_value);
  1139. Py_CLEAR(self->exc_traceback);
  1140. #endif
  1141. Py_CLEAR(self->dict);
  1142. Py_TYPE(self)->tp_free((PyObject*)self);
  1143. }
  1144. static PyObject*
  1145. single_result(PyObject* results)
  1146. {
  1147. if (results != NULL && PyTuple_Check(results) &&
  1148. PyTuple_GET_SIZE(results) == 1) {
  1149. PyObject* result = PyTuple_GET_ITEM(results, 0);
  1150. Py_INCREF(result);
  1151. Py_DECREF(results);
  1152. return result;
  1153. }
  1154. else {
  1155. return results;
  1156. }
  1157. }
  1158. static PyObject*
  1159. throw_greenlet(PyGreenlet* self, PyObject* typ, PyObject* val, PyObject* tb)
  1160. {
  1161. /* Note: _consumes_ a reference to typ, val, tb */
  1162. PyObject* result = NULL;
  1163. PyErr_Restore(typ, val, tb);
  1164. if (PyGreenlet_STARTED(self) && !PyGreenlet_ACTIVE(self)) {
  1165. /* dead greenlet: turn GreenletExit into a regular return */
  1166. result = g_handle_exit(result);
  1167. }
  1168. return single_result(g_switch(self, result, NULL));
  1169. }
  1170. PyDoc_STRVAR(
  1171. green_switch_doc,
  1172. "switch(*args, **kwargs)\n"
  1173. "\n"
  1174. "Switch execution to this greenlet.\n"
  1175. "\n"
  1176. "If this greenlet has never been run, then this greenlet\n"
  1177. "will be switched to using the body of ``self.run(*args, **kwargs)``.\n"
  1178. "\n"
  1179. "If the greenlet is active (has been run, but was switch()'ed\n"
  1180. "out before leaving its run function), then this greenlet will\n"
  1181. "be resumed and the return value to its switch call will be\n"
  1182. "None if no arguments are given, the given argument if one\n"
  1183. "argument is given, or the args tuple and keyword args dict if\n"
  1184. "multiple arguments are given.\n"
  1185. "\n"
  1186. "If the greenlet is dead, or is the current greenlet then this\n"
  1187. "function will simply return the arguments using the same rules as\n"
  1188. "above.\n");
  1189. static PyObject*
  1190. green_switch(PyGreenlet* self, PyObject* args, PyObject* kwargs)
  1191. {
  1192. Py_INCREF(args);
  1193. Py_XINCREF(kwargs);
  1194. return single_result(g_switch(self, args, kwargs));
  1195. }
  1196. PyDoc_STRVAR(
  1197. green_throw_doc,
  1198. "Switches execution to this greenlet, but immediately raises the\n"
  1199. "given exception in this greenlet. If no argument is provided, the "
  1200. "exception\n"
  1201. "defaults to `greenlet.GreenletExit`. The normal exception\n"
  1202. "propagation rules apply, as described for `switch`. Note that calling "
  1203. "this\n"
  1204. "method is almost equivalent to the following::\n"
  1205. "\n"
  1206. " def raiser():\n"
  1207. " raise typ, val, tb\n"
  1208. " g_raiser = greenlet(raiser, parent=g)\n"
  1209. " g_raiser.switch()\n"
  1210. "\n"
  1211. "except that this trick does not work for the\n"
  1212. "`greenlet.GreenletExit` exception, which would not propagate\n"
  1213. "from ``g_raiser`` to ``g``.\n");
  1214. static PyObject*
  1215. green_throw(PyGreenlet* self, PyObject* args)
  1216. {
  1217. PyObject* typ = PyExc_GreenletExit;
  1218. PyObject* val = NULL;
  1219. PyObject* tb = NULL;
  1220. if (!PyArg_ParseTuple(args, "|OOO:throw", &typ, &val, &tb)) {
  1221. return NULL;
  1222. }
  1223. /* First, check the traceback argument, replacing None, with NULL */
  1224. if (tb == Py_None) {
  1225. tb = NULL;
  1226. }
  1227. else if (tb != NULL && !PyTraceBack_Check(tb)) {
  1228. PyErr_SetString(PyExc_TypeError,
  1229. "throw() third argument must be a traceback object");
  1230. return NULL;
  1231. }
  1232. Py_INCREF(typ);
  1233. Py_XINCREF(val);
  1234. Py_XINCREF(tb);
  1235. if (PyExceptionClass_Check(typ)) {
  1236. PyErr_NormalizeException(&typ, &val, &tb);
  1237. }
  1238. else if (PyExceptionInstance_Check(typ)) {
  1239. /* Raising an instance. The value should be a dummy. */
  1240. if (val && val != Py_None) {
  1241. PyErr_SetString(
  1242. PyExc_TypeError,
  1243. "instance exception may not have a separate value");
  1244. goto failed_throw;
  1245. }
  1246. else {
  1247. /* Normalize to raise <class>, <instance> */
  1248. Py_XDECREF(val);
  1249. val = typ;
  1250. typ = PyExceptionInstance_Class(typ);
  1251. Py_INCREF(typ);
  1252. }
  1253. }
  1254. else {
  1255. /* Not something you can raise. throw() fails. */
  1256. PyErr_Format(PyExc_TypeError,
  1257. "exceptions must be classes, or instances, not %s",
  1258. Py_TYPE(typ)->tp_name);
  1259. goto failed_throw;
  1260. }
  1261. return throw_greenlet(self, typ, val, tb);
  1262. failed_throw:
  1263. /* Didn't use our arguments, so restore their original refcounts */
  1264. Py_DECREF(typ);
  1265. Py_XDECREF(val);
  1266. Py_XDECREF(tb);
  1267. return NULL;
  1268. }
  1269. static int
  1270. green_bool(PyGreenlet* self)
  1271. {
  1272. return PyGreenlet_ACTIVE(self);
  1273. }
  1274. static PyObject*
  1275. green_getdict(PyGreenlet* self, void* c)
  1276. {
  1277. if (self->dict == NULL) {
  1278. self->dict = PyDict_New();
  1279. if (self->dict == NULL) {
  1280. return NULL;
  1281. }
  1282. }
  1283. Py_INCREF(self->dict);
  1284. return self->dict;
  1285. }
  1286. static int
  1287. green_setdict(PyGreenlet* self, PyObject* val, void* c)
  1288. {
  1289. PyObject* tmp;
  1290. if (val == NULL) {
  1291. PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
  1292. return -1;
  1293. }
  1294. if (!PyDict_Check(val)) {
  1295. PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
  1296. return -1;
  1297. }
  1298. tmp = self->dict;
  1299. Py_INCREF(val);
  1300. self->dict = val;
  1301. Py_XDECREF(tmp);
  1302. return 0;
  1303. }
  1304. static int
  1305. _green_not_dead(PyGreenlet* self)
  1306. {
  1307. return PyGreenlet_ACTIVE(self) || !PyGreenlet_STARTED(self);
  1308. }
  1309. static PyObject*
  1310. green_getdead(PyGreenlet* self, void* c)
  1311. {
  1312. if (_green_not_dead(self)) {
  1313. Py_RETURN_FALSE;
  1314. }
  1315. else {
  1316. Py_RETURN_TRUE;
  1317. }
  1318. }
  1319. static PyObject*
  1320. green_get_stack_saved(PyGreenlet* self, void* c)
  1321. {
  1322. return PyLong_FromSsize_t(self->stack_saved);
  1323. }
  1324. static PyObject*
  1325. green_getrun(PyGreenlet* self, void* c)
  1326. {
  1327. if (PyGreenlet_STARTED(self) || self->run_info == NULL) {
  1328. PyErr_SetString(PyExc_AttributeError, "run");
  1329. return NULL;
  1330. }
  1331. Py_INCREF(self->run_info);
  1332. return self->run_info;
  1333. }
  1334. static int
  1335. green_setrun(PyGreenlet* self, PyObject* nrun, void* c)
  1336. {
  1337. PyObject* o;
  1338. if (PyGreenlet_STARTED(self)) {
  1339. PyErr_SetString(PyExc_AttributeError,
  1340. "run cannot be set "
  1341. "after the start of the greenlet");
  1342. return -1;
  1343. }
  1344. o = self->run_info;
  1345. self->run_info = nrun;
  1346. Py_XINCREF(nrun);
  1347. Py_XDECREF(o);
  1348. return 0;
  1349. }
  1350. static PyObject*
  1351. green_getparent(PyGreenlet* self, void* c)
  1352. {
  1353. PyObject* result = self->parent ? (PyObject*)self->parent : Py_None;
  1354. Py_INCREF(result);
  1355. return result;
  1356. }
  1357. static int
  1358. green_setparent(PyGreenlet* self, PyObject* nparent, void* c)
  1359. {
  1360. PyGreenlet* p;
  1361. PyObject* run_info = NULL;
  1362. if (nparent == NULL) {
  1363. PyErr_SetString(PyExc_AttributeError, "can't delete attribute");
  1364. return -1;
  1365. }
  1366. if (!PyGreenlet_Check(nparent)) {
  1367. PyErr_SetString(PyExc_TypeError, "parent must be a greenlet");
  1368. return -1;
  1369. }
  1370. for (p = (PyGreenlet*)nparent; p; p = p->parent) {
  1371. if (p == self) {
  1372. PyErr_SetString(PyExc_ValueError, "cyclic parent chain");
  1373. return -1;
  1374. }
  1375. run_info = PyGreenlet_ACTIVE(p) ? p->run_info : NULL;
  1376. }
  1377. if (run_info == NULL) {
  1378. PyErr_SetString(PyExc_ValueError,
  1379. "parent must not be garbage collected");
  1380. return -1;
  1381. }
  1382. if (PyGreenlet_STARTED(self) && self->run_info != run_info) {
  1383. PyErr_SetString(PyExc_ValueError,
  1384. "parent cannot be on a different thread");
  1385. return -1;
  1386. }
  1387. p = self->parent;
  1388. self->parent = (PyGreenlet*)nparent;
  1389. Py_INCREF(nparent);
  1390. Py_XDECREF(p);
  1391. return 0;
  1392. }
  1393. #ifdef Py_CONTEXT_H
  1394. # define GREENLET_NO_CONTEXTVARS_REASON "This build of greenlet"
  1395. #else
  1396. # define GREENLET_NO_CONTEXTVARS_REASON "This Python interpreter"
  1397. #endif
  1398. static PyObject*
  1399. green_getcontext(PyGreenlet* self, void* c)
  1400. {
  1401. #if GREENLET_PY37
  1402. PyThreadState* tstate = PyThreadState_GET();
  1403. PyObject* result;
  1404. if (!STATE_OK) {
  1405. return NULL;
  1406. }
  1407. if (PyGreenlet_ACTIVE(self) && self->top_frame == NULL) {
  1408. /* Currently running greenlet: context is stored in the thread state,
  1409. not the greenlet object. */
  1410. if (self == ts_current) {
  1411. result = tstate->context;
  1412. }
  1413. else {
  1414. PyErr_SetString(PyExc_ValueError,
  1415. "cannot get context of a "
  1416. "greenlet that is running in a different thread");
  1417. return NULL;
  1418. }
  1419. }
  1420. else {
  1421. /* Greenlet is not running: just return context. */
  1422. result = self->context;
  1423. }
  1424. if (result == NULL) {
  1425. result = Py_None;
  1426. }
  1427. Py_INCREF(result);
  1428. return result;
  1429. #else
  1430. PyErr_SetString(PyExc_AttributeError,
  1431. GREENLET_NO_CONTEXTVARS_REASON
  1432. " does not support context variables");
  1433. return NULL;
  1434. #endif
  1435. }
  1436. static int
  1437. green_setcontext(PyGreenlet* self, PyObject* nctx, void* c)
  1438. {
  1439. #if GREENLET_PY37
  1440. PyThreadState* tstate;
  1441. PyObject* octx = NULL;
  1442. if (!STATE_OK) {
  1443. return -1;
  1444. }
  1445. if (nctx == NULL) {
  1446. PyErr_SetString(PyExc_AttributeError, "can't delete attribute");
  1447. return -1;
  1448. }
  1449. if (nctx == Py_None) {
  1450. /* "Empty context" is stored as NULL, not None. */
  1451. nctx = NULL;
  1452. }
  1453. else if (!PyContext_CheckExact(nctx)) {
  1454. PyErr_SetString(PyExc_TypeError,
  1455. "greenlet context must be a "
  1456. "contextvars.Context or None");
  1457. return -1;
  1458. }
  1459. tstate = PyThreadState_GET();
  1460. if (PyGreenlet_ACTIVE(self) && self->top_frame == NULL) {
  1461. /* Currently running greenlet: context is stored in the thread state,
  1462. not the greenlet object. */
  1463. if (self == ts_current) {
  1464. octx = tstate->context;
  1465. tstate->context = nctx;
  1466. tstate->context_ver++;
  1467. Py_XINCREF(nctx);
  1468. }
  1469. else {
  1470. PyErr_SetString(PyExc_ValueError,
  1471. "cannot set context of a "
  1472. "greenlet that is running in a different thread");
  1473. return -1;
  1474. }
  1475. }
  1476. else {
  1477. /* Greenlet is not running: just set context. */
  1478. octx = self->context;
  1479. self->context = nctx;
  1480. Py_XINCREF(nctx);
  1481. }
  1482. Py_XDECREF(octx);
  1483. return 0;
  1484. #else
  1485. PyErr_SetString(PyExc_AttributeError,
  1486. GREENLET_NO_CONTEXTVARS_REASON
  1487. " does not support context variables");
  1488. return -1;
  1489. #endif
  1490. }
  1491. #undef GREENLET_NO_CONTEXTVARS_REASON
  1492. static PyObject*
  1493. green_getframe(PyGreenlet* self, void* c)
  1494. {
  1495. PyObject* result = self->top_frame ? (PyObject*)self->top_frame : Py_None;
  1496. Py_INCREF(result);
  1497. return result;
  1498. }
  1499. static PyObject*
  1500. green_getstate(PyGreenlet* self)
  1501. {
  1502. PyErr_Format(PyExc_TypeError,
  1503. "cannot serialize '%s' object",
  1504. Py_TYPE(self)->tp_name);
  1505. return NULL;
  1506. }
  1507. static PyObject*
  1508. green_repr(PyGreenlet* self)
  1509. {
  1510. /*
  1511. Return a string like
  1512. <greenlet.greenlet at 0xdeadbeef [current][active started]|dead main>
  1513. The handling of greenlets across threads is not super good.
  1514. We mostly use the internal definitions of these terms, but they
  1515. generally should make sense to users as well.
  1516. */
  1517. PyObject* result;
  1518. int never_started = !PyGreenlet_STARTED(self) && !PyGreenlet_ACTIVE(self);
  1519. if (!STATE_OK) {
  1520. return NULL;
  1521. }
  1522. #if PY_MAJOR_VERSION >= 3
  1523. # define GNative_FromFormat PyUnicode_FromFormat
  1524. #else
  1525. # define GNative_FromFormat PyString_FromFormat
  1526. #endif
  1527. if (_green_not_dead(self)) {
  1528. /* XXX: The otid= is almost useless becasue you can't correlate it to
  1529. any thread identifier exposed to Python. We could use
  1530. PyThreadState_GET()->thread_id, but we'd need to save that in the
  1531. greenlet, or save the whole PyThreadState object itself.
  1532. As it stands, its only useful for identifying greenlets from the same thread.
  1533. */
  1534. result = GNative_FromFormat(
  1535. "<%s object at %p (otid=%p)%s%s%s%s>",
  1536. Py_TYPE(self)->tp_name,
  1537. self,
  1538. self->run_info,
  1539. ts_current == self
  1540. ? " current"
  1541. : (PyGreenlet_STARTED(self) ? " suspended" : ""),
  1542. PyGreenlet_ACTIVE(self) ? " active" : "",
  1543. never_started ? " pending" : " started",
  1544. PyGreenlet_MAIN(self) ? " main" : ""
  1545. );
  1546. }
  1547. else {
  1548. /* main greenlets never really appear dead. */
  1549. result = GNative_FromFormat(
  1550. "<%s object at %p (otid=%p) dead>",
  1551. Py_TYPE(self)->tp_name,
  1552. self,
  1553. self->run_info
  1554. );
  1555. }
  1556. #undef GNative_FromFormat
  1557. return result;
  1558. }
  1559. /*****************************************************************************
  1560. * C interface
  1561. *
  1562. * These are exported using the CObject API
  1563. */
  1564. static PyGreenlet*
  1565. PyGreenlet_GetCurrent(void)
  1566. {
  1567. if (!STATE_OK) {
  1568. return NULL;
  1569. }
  1570. Py_INCREF(ts_current);
  1571. return ts_current;
  1572. }
  1573. static int
  1574. PyGreenlet_SetParent(PyGreenlet* g, PyGreenlet* nparent)
  1575. {
  1576. if (!PyGreenlet_Check(g)) {
  1577. PyErr_SetString(PyExc_TypeError, "parent must be a greenlet");
  1578. return -1;
  1579. }
  1580. return green_setparent((PyGreenlet*)g, (PyObject*)nparent, NULL);
  1581. }
  1582. static PyGreenlet*
  1583. PyGreenlet_New(PyObject* run, PyGreenlet* parent)
  1584. {
  1585. /* XXX: Why doesn't this call green_new()? There's some duplicate
  1586. code. */
  1587. PyGreenlet* g = NULL;
  1588. g = (PyGreenlet*)PyType_GenericAlloc(&PyGreenlet_Type, 0);
  1589. if (g == NULL) {
  1590. return NULL;
  1591. }
  1592. if (run != NULL) {
  1593. Py_INCREF(run);
  1594. g->run_info = run;
  1595. }
  1596. if (parent != NULL) {
  1597. if (PyGreenlet_SetParent(g, parent)) {
  1598. Py_DECREF(g);
  1599. return NULL;
  1600. }
  1601. }
  1602. else {
  1603. if ((g->parent = PyGreenlet_GetCurrent()) == NULL) {
  1604. Py_DECREF(g);
  1605. return NULL;
  1606. }
  1607. }
  1608. #if GREENLET_USE_CFRAME
  1609. g->cframe = &PyThreadState_GET()->root_cframe;
  1610. #endif
  1611. return g;
  1612. }
  1613. static PyObject*
  1614. PyGreenlet_Switch(PyGreenlet* g, PyObject* args, PyObject* kwargs)
  1615. {
  1616. PyGreenlet* self = (PyGreenlet*)g;
  1617. if (!PyGreenlet_Check(self)) {
  1618. PyErr_BadArgument();
  1619. return NULL;
  1620. }
  1621. if (args == NULL) {
  1622. args = Py_BuildValue("()");
  1623. }
  1624. else {
  1625. Py_INCREF(args);
  1626. }
  1627. if (kwargs != NULL && PyDict_Check(kwargs)) {
  1628. Py_INCREF(kwargs);
  1629. }
  1630. else {
  1631. kwargs = NULL;
  1632. }
  1633. return single_result(g_switch(self, args, kwargs));
  1634. }
  1635. static PyObject*
  1636. PyGreenlet_Throw(PyGreenlet* self, PyObject* typ, PyObject* val, PyObject* tb)
  1637. {
  1638. if (!PyGreenlet_Check(self)) {
  1639. PyErr_BadArgument();
  1640. return NULL;
  1641. }
  1642. Py_INCREF(typ);
  1643. Py_XINCREF(val);
  1644. Py_XINCREF(tb);
  1645. return throw_greenlet(self, typ, val, tb);
  1646. }
  1647. /** End C API ****************************************************************/
  1648. static PyMethodDef green_methods[] = {
  1649. {"switch",
  1650. (PyCFunction)green_switch,
  1651. METH_VARARGS | METH_KEYWORDS,
  1652. green_switch_doc},
  1653. {"throw", (PyCFunction)green_throw, METH_VARARGS, green_throw_doc},
  1654. {"__getstate__", (PyCFunction)green_getstate, METH_NOARGS, NULL},
  1655. {NULL, NULL} /* sentinel */
  1656. };
  1657. static PyGetSetDef green_getsets[] = {
  1658. {"__dict__", (getter)green_getdict, (setter)green_setdict, /*XXX*/ NULL},
  1659. {"run", (getter)green_getrun, (setter)green_setrun, /*XXX*/ NULL},
  1660. {"parent", (getter)green_getparent, (setter)green_setparent, /*XXX*/ NULL},
  1661. {"gr_frame", (getter)green_getframe, NULL, /*XXX*/ NULL},
  1662. {"gr_context",
  1663. (getter)green_getcontext,
  1664. (setter)green_setcontext,
  1665. /*XXX*/ NULL},
  1666. {"dead", (getter)green_getdead, NULL, /*XXX*/ NULL},
  1667. {"_stack_saved", (getter)green_get_stack_saved, NULL, /*XXX*/ NULL},
  1668. {NULL}};
  1669. static PyNumberMethods green_as_number = {
  1670. NULL, /* nb_add */
  1671. NULL, /* nb_subtract */
  1672. NULL, /* nb_multiply */
  1673. #if PY_MAJOR_VERSION < 3
  1674. NULL, /* nb_divide */
  1675. #endif
  1676. NULL, /* nb_remainder */
  1677. NULL, /* nb_divmod */
  1678. NULL, /* nb_power */
  1679. NULL, /* nb_negative */
  1680. NULL, /* nb_positive */
  1681. NULL, /* nb_absolute */
  1682. (inquiry)green_bool, /* nb_bool */
  1683. };
  1684. PyTypeObject PyGreenlet_Type = {
  1685. PyVarObject_HEAD_INIT(NULL, 0)
  1686. "greenlet.greenlet", /* tp_name */
  1687. sizeof(PyGreenlet), /* tp_basicsize */
  1688. 0, /* tp_itemsize */
  1689. /* methods */
  1690. (destructor)green_dealloc, /* tp_dealloc */
  1691. 0, /* tp_print */
  1692. 0, /* tp_getattr */
  1693. 0, /* tp_setattr */
  1694. 0, /* tp_compare */
  1695. (reprfunc)green_repr, /* tp_repr */
  1696. &green_as_number, /* tp_as _number*/
  1697. 0, /* tp_as _sequence*/
  1698. 0, /* tp_as _mapping*/
  1699. 0, /* tp_hash */
  1700. 0, /* tp_call */
  1701. 0, /* tp_str */
  1702. 0, /* tp_getattro */
  1703. 0, /* tp_setattro */
  1704. 0, /* tp_as_buffer*/
  1705. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  1706. GREENLET_GC_FLAGS, /* tp_flags */
  1707. "greenlet(run=None, parent=None) -> greenlet\n\n"
  1708. "Creates a new greenlet object (without running it).\n\n"
  1709. " - *run* -- The callable to invoke.\n"
  1710. " - *parent* -- The parent greenlet. The default is the current "
  1711. "greenlet.", /* tp_doc */
  1712. (traverseproc)GREENLET_tp_traverse, /* tp_traverse */
  1713. (inquiry)GREENLET_tp_clear, /* tp_clear */
  1714. 0, /* tp_richcompare */
  1715. offsetof(PyGreenlet, weakreflist), /* tp_weaklistoffset */
  1716. 0, /* tp_iter */
  1717. 0, /* tp_iternext */
  1718. green_methods, /* tp_methods */
  1719. 0, /* tp_members */
  1720. green_getsets, /* tp_getset */
  1721. 0, /* tp_base */
  1722. 0, /* tp_dict */
  1723. 0, /* tp_descr_get */
  1724. 0, /* tp_descr_set */
  1725. offsetof(PyGreenlet, dict), /* tp_dictoffset */
  1726. (initproc)green_init, /* tp_init */
  1727. GREENLET_tp_alloc, /* tp_alloc */
  1728. green_new, /* tp_new */
  1729. GREENLET_tp_free, /* tp_free */
  1730. (inquiry)GREENLET_tp_is_gc, /* tp_is_gc */
  1731. };
  1732. PyDoc_STRVAR(mod_getcurrent_doc,
  1733. "getcurrent() -> greenlet\n"
  1734. "\n"
  1735. "Returns the current greenlet (i.e. the one which called this "
  1736. "function).\n");
  1737. static PyObject*
  1738. mod_getcurrent(PyObject* self)
  1739. {
  1740. if (!STATE_OK) {
  1741. return NULL;
  1742. }
  1743. Py_INCREF(ts_current);
  1744. return (PyObject*)ts_current;
  1745. }
  1746. PyDoc_STRVAR(mod_settrace_doc,
  1747. "settrace(callback) -> object\n"
  1748. "\n"
  1749. "Sets a new tracing function and returns the previous one.\n");
  1750. static PyObject*
  1751. mod_settrace(PyObject* self, PyObject* args)
  1752. {
  1753. int err;
  1754. PyObject* previous;
  1755. PyObject* tracefunc;
  1756. PyGreenlet* current;
  1757. if (!PyArg_ParseTuple(args, "O", &tracefunc)) {
  1758. return NULL;
  1759. }
  1760. if (!STATE_OK) {
  1761. return NULL;
  1762. }
  1763. current = ts_current;
  1764. previous = PyDict_GetItem(current->run_info, ts_tracekey);
  1765. if (previous == NULL) {
  1766. previous = Py_None;
  1767. }
  1768. Py_INCREF(previous);
  1769. if (tracefunc == Py_None) {
  1770. err = previous != Py_None ?
  1771. PyDict_DelItem(current->run_info, ts_tracekey) :
  1772. 0;
  1773. }
  1774. else {
  1775. err = PyDict_SetItem(current->run_info, ts_tracekey, tracefunc);
  1776. }
  1777. if (err < 0) {
  1778. Py_CLEAR(previous);
  1779. }
  1780. return previous;
  1781. }
  1782. PyDoc_STRVAR(mod_gettrace_doc,
  1783. "gettrace() -> object\n"
  1784. "\n"
  1785. "Returns the currently set tracing function, or None.\n");
  1786. static PyObject*
  1787. mod_gettrace(PyObject* self)
  1788. {
  1789. PyObject* tracefunc;
  1790. if (!STATE_OK) {
  1791. return NULL;
  1792. }
  1793. tracefunc = PyDict_GetItem(ts_current->run_info, ts_tracekey);
  1794. if (tracefunc == NULL) {
  1795. tracefunc = Py_None;
  1796. }
  1797. Py_INCREF(tracefunc);
  1798. return tracefunc;
  1799. }
  1800. static PyMethodDef GreenMethods[] = {
  1801. {"getcurrent",
  1802. (PyCFunction)mod_getcurrent,
  1803. METH_NOARGS,
  1804. mod_getcurrent_doc},
  1805. {"settrace", (PyCFunction)mod_settrace, METH_VARARGS, mod_settrace_doc},
  1806. {"gettrace", (PyCFunction)mod_gettrace, METH_NOARGS, mod_gettrace_doc},
  1807. {NULL, NULL} /* Sentinel */
  1808. };
  1809. static char* copy_on_greentype[] = {
  1810. "getcurrent", "error", "GreenletExit", "settrace", "gettrace", NULL};
  1811. #if PY_MAJOR_VERSION >= 3
  1812. # define INITERROR return NULL
  1813. static struct PyModuleDef greenlet_module_def = {
  1814. PyModuleDef_HEAD_INIT,
  1815. "greenlet._greenlet",
  1816. NULL,
  1817. -1,
  1818. GreenMethods,
  1819. };
  1820. PyMODINIT_FUNC
  1821. PyInit__greenlet(void)
  1822. #else
  1823. # define INITERROR return
  1824. PyMODINIT_FUNC
  1825. init_greenlet(void)
  1826. #endif
  1827. {
  1828. PyObject* m = NULL;
  1829. char** p = NULL;
  1830. PyObject* c_api_object;
  1831. static void* _PyGreenlet_API[PyGreenlet_API_pointers];
  1832. GREENLET_NOINLINE_INIT();
  1833. #if PY_MAJOR_VERSION >= 3
  1834. m = PyModule_Create(&greenlet_module_def);
  1835. #else
  1836. m = Py_InitModule("greenlet._greenlet", GreenMethods);
  1837. #endif
  1838. if (m == NULL) {
  1839. INITERROR;
  1840. }
  1841. #if PY_MAJOR_VERSION >= 3
  1842. # define Greenlet_Intern PyUnicode_InternFromString
  1843. #else
  1844. # define Greenlet_Intern PyString_InternFromString
  1845. #endif
  1846. ts_curkey = Greenlet_Intern("__greenlet_ts_curkey");
  1847. ts_delkey = Greenlet_Intern("__greenlet_ts_delkey");
  1848. ts_tracekey = Greenlet_Intern("__greenlet_ts_tracekey");
  1849. ts_event_switch = Greenlet_Intern("switch");
  1850. ts_event_throw = Greenlet_Intern("throw");
  1851. #undef Greenlet_Intern
  1852. if (ts_curkey == NULL || ts_delkey == NULL) {
  1853. INITERROR;
  1854. }
  1855. if (PyType_Ready(&PyGreenlet_Type) < 0) {
  1856. INITERROR;
  1857. }
  1858. PyExc_GreenletError = PyErr_NewException("greenlet.error", NULL, NULL);
  1859. if (PyExc_GreenletError == NULL) {
  1860. INITERROR;
  1861. }
  1862. PyExc_GreenletExit =
  1863. PyErr_NewException("greenlet.GreenletExit", PyExc_BaseException, NULL);
  1864. if (PyExc_GreenletExit == NULL) {
  1865. INITERROR;
  1866. }
  1867. ts_empty_tuple = PyTuple_New(0);
  1868. if (ts_empty_tuple == NULL) {
  1869. INITERROR;
  1870. }
  1871. ts_empty_dict = PyDict_New();
  1872. if (ts_empty_dict == NULL) {
  1873. INITERROR;
  1874. }
  1875. ts_current = green_create_main();
  1876. if (ts_current == NULL) {
  1877. INITERROR;
  1878. }
  1879. Py_INCREF(&PyGreenlet_Type);
  1880. PyModule_AddObject(m, "greenlet", (PyObject*)&PyGreenlet_Type);
  1881. Py_INCREF(PyExc_GreenletError);
  1882. PyModule_AddObject(m, "error", PyExc_GreenletError);
  1883. Py_INCREF(PyExc_GreenletExit);
  1884. PyModule_AddObject(m, "GreenletExit", PyExc_GreenletExit);
  1885. PyModule_AddObject(m, "GREENLET_USE_GC", PyBool_FromLong(1));
  1886. PyModule_AddObject(m, "GREENLET_USE_TRACING", PyBool_FromLong(1));
  1887. PyModule_AddObject(
  1888. m, "GREENLET_USE_CONTEXT_VARS", PyBool_FromLong(GREENLET_PY37));
  1889. /* also publish module-level data as attributes of the greentype. */
  1890. /* XXX: Why? */
  1891. for (p = copy_on_greentype; *p; p++) {
  1892. PyObject* o = PyObject_GetAttrString(m, *p);
  1893. if (!o) {
  1894. continue;
  1895. }
  1896. PyDict_SetItemString(PyGreenlet_Type.tp_dict, *p, o);
  1897. Py_DECREF(o);
  1898. }
  1899. /*
  1900. * Expose C API
  1901. */
  1902. /* types */
  1903. _PyGreenlet_API[PyGreenlet_Type_NUM] = (void*)&PyGreenlet_Type;
  1904. /* exceptions */
  1905. _PyGreenlet_API[PyExc_GreenletError_NUM] = (void*)PyExc_GreenletError;
  1906. _PyGreenlet_API[PyExc_GreenletExit_NUM] = (void*)PyExc_GreenletExit;
  1907. /* methods */
  1908. _PyGreenlet_API[PyGreenlet_New_NUM] = (void*)PyGreenlet_New;
  1909. _PyGreenlet_API[PyGreenlet_GetCurrent_NUM] = (void*)PyGreenlet_GetCurrent;
  1910. _PyGreenlet_API[PyGreenlet_Throw_NUM] = (void*)PyGreenlet_Throw;
  1911. _PyGreenlet_API[PyGreenlet_Switch_NUM] = (void*)PyGreenlet_Switch;
  1912. _PyGreenlet_API[PyGreenlet_SetParent_NUM] = (void*)PyGreenlet_SetParent;
  1913. /* XXX: Note that our module name is ``greenlet._greenlet``, but for
  1914. backwards compatibility with existing C code, we need the _C_API to
  1915. be directly in greenlet.
  1916. */
  1917. c_api_object =
  1918. PyCapsule_New((void*)_PyGreenlet_API, "greenlet._C_API", NULL);
  1919. if (c_api_object != NULL) {
  1920. PyModule_AddObject(m, "_C_API", c_api_object);
  1921. }
  1922. #if PY_MAJOR_VERSION >= 3
  1923. return m;
  1924. #endif
  1925. }
  1926. #ifdef __clang__
  1927. # pragma clang diagnostic pop
  1928. #endif