Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 1 | /* Generator object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 4 | #include "pycore_ceval.h" /* _PyEval_EvalFrame() */ |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 5 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 6 | #include "pycore_pystate.h" |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 7 | #include "frameobject.h" |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9 | #include "opcode.h" |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 10 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 11 | static PyObject *gen_close(PyGenObject *, PyObject *); |
| 12 | static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *); |
| 13 | static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *); |
| 14 | |
| 15 | static char *NON_INIT_CORO_MSG = "can't send non-None value to a " |
| 16 | "just-started coroutine"; |
| 17 | |
| 18 | static char *ASYNC_GEN_IGNORED_EXIT_MSG = |
| 19 | "async generator ignored GeneratorExit"; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 20 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 21 | static inline int |
| 22 | exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg) |
| 23 | { |
| 24 | Py_VISIT(exc_state->exc_type); |
| 25 | Py_VISIT(exc_state->exc_value); |
| 26 | Py_VISIT(exc_state->exc_traceback); |
| 27 | return 0; |
| 28 | } |
| 29 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 30 | static int |
| 31 | gen_traverse(PyGenObject *gen, visitproc visit, void *arg) |
| 32 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | Py_VISIT((PyObject *)gen->gi_frame); |
| 34 | Py_VISIT(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 35 | Py_VISIT(gen->gi_name); |
| 36 | Py_VISIT(gen->gi_qualname); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 37 | /* No need to visit cr_origin, because it's just tuples/str/int, so can't |
| 38 | participate in a reference cycle. */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 39 | return exc_state_traverse(&gen->gi_exc_state, visit, arg); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 42 | void |
| 43 | _PyGen_Finalize(PyObject *self) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 44 | { |
| 45 | PyGenObject *gen = (PyGenObject *)self; |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 46 | PyObject *res = NULL; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 47 | PyObject *error_type, *error_value, *error_traceback; |
| 48 | |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 49 | if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 50 | /* Generator isn't paused, so no need to close */ |
| 51 | return; |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 52 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 53 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 54 | if (PyAsyncGen_CheckExact(self)) { |
| 55 | PyAsyncGenObject *agen = (PyAsyncGenObject*)self; |
| 56 | PyObject *finalizer = agen->ag_finalizer; |
| 57 | if (finalizer && !agen->ag_closed) { |
| 58 | /* Save the current exception, if any. */ |
| 59 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 60 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 61 | res = PyObject_CallOneArg(finalizer, self); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 62 | |
| 63 | if (res == NULL) { |
| 64 | PyErr_WriteUnraisable(self); |
| 65 | } else { |
| 66 | Py_DECREF(res); |
| 67 | } |
| 68 | /* Restore the saved exception. */ |
| 69 | PyErr_Restore(error_type, error_value, error_traceback); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 74 | /* Save the current exception, if any. */ |
| 75 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 76 | |
Benjamin Peterson | 2f40ed4 | 2016-09-05 10:14:54 -0700 | [diff] [blame] | 77 | /* If `gen` is a coroutine, and if it was never awaited on, |
| 78 | issue a RuntimeWarning. */ |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 79 | if (gen->gi_code != NULL && |
| 80 | ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE && |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 81 | gen->gi_frame->f_lasti == -1) |
| 82 | { |
| 83 | _PyErr_WarnUnawaitedCoroutine((PyObject *)gen); |
Benjamin Peterson | 2f40ed4 | 2016-09-05 10:14:54 -0700 | [diff] [blame] | 84 | } |
| 85 | else { |
| 86 | res = gen_close(gen, NULL); |
| 87 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 88 | |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 89 | if (res == NULL) { |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 90 | if (PyErr_Occurred()) { |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 91 | PyErr_WriteUnraisable(self); |
Yury Selivanov | 2a2270d | 2018-01-29 14:31:47 -0500 | [diff] [blame] | 92 | } |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 93 | } |
| 94 | else { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 95 | Py_DECREF(res); |
Benjamin Peterson | b88db87 | 2016-09-07 08:46:59 -0700 | [diff] [blame] | 96 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 97 | |
| 98 | /* Restore the saved exception. */ |
| 99 | PyErr_Restore(error_type, error_value, error_traceback); |
| 100 | } |
| 101 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 102 | static inline void |
| 103 | exc_state_clear(_PyErr_StackItem *exc_state) |
| 104 | { |
| 105 | PyObject *t, *v, *tb; |
| 106 | t = exc_state->exc_type; |
| 107 | v = exc_state->exc_value; |
| 108 | tb = exc_state->exc_traceback; |
| 109 | exc_state->exc_type = NULL; |
| 110 | exc_state->exc_value = NULL; |
| 111 | exc_state->exc_traceback = NULL; |
| 112 | Py_XDECREF(t); |
| 113 | Py_XDECREF(v); |
| 114 | Py_XDECREF(tb); |
| 115 | } |
| 116 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 117 | static void |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 118 | gen_dealloc(PyGenObject *gen) |
| 119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | PyObject *self = (PyObject *) gen; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | _PyObject_GC_UNTRACK(gen); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | if (gen->gi_weakreflist != NULL) |
| 125 | PyObject_ClearWeakRefs(self); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 127 | _PyObject_GC_TRACK(self); |
| 128 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 129 | if (PyObject_CallFinalizerFromDealloc(self)) |
| 130 | return; /* resurrected. :( */ |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 131 | |
| 132 | _PyObject_GC_UNTRACK(self); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 133 | if (PyAsyncGen_CheckExact(gen)) { |
| 134 | /* We have to handle this case for asynchronous generators |
| 135 | right here, because this code has to be between UNTRACK |
| 136 | and GC_Del. */ |
| 137 | Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer); |
| 138 | } |
Benjamin Peterson | bdddb11 | 2016-09-05 10:39:57 -0700 | [diff] [blame] | 139 | if (gen->gi_frame != NULL) { |
| 140 | gen->gi_frame->f_gen = NULL; |
| 141 | Py_CLEAR(gen->gi_frame); |
| 142 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 143 | if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) { |
| 144 | Py_CLEAR(((PyCoroObject *)gen)->cr_origin); |
| 145 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | Py_CLEAR(gen->gi_code); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 147 | Py_CLEAR(gen->gi_name); |
| 148 | Py_CLEAR(gen->gi_qualname); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 149 | exc_state_clear(&gen->gi_exc_state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | PyObject_GC_Del(gen); |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static PyObject * |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 154 | gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 155 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 156 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 158 | PyObject *result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 159 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 160 | if (gen->gi_running) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 161 | const char *msg = "generator already executing"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 162 | if (PyCoro_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 163 | msg = "coroutine already executing"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 164 | } |
| 165 | else if (PyAsyncGen_CheckExact(gen)) { |
| 166 | msg = "async generator already executing"; |
| 167 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 168 | PyErr_SetString(PyExc_ValueError, msg); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 171 | if (f == NULL || f->f_stacktop == NULL) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 172 | if (PyCoro_CheckExact(gen) && !closing) { |
| 173 | /* `gen` is an exhausted coroutine: raise an error, |
| 174 | except when called from gen_close(), which should |
| 175 | always be a silent method. */ |
| 176 | PyErr_SetString( |
| 177 | PyExc_RuntimeError, |
| 178 | "cannot reuse already awaited coroutine"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 179 | } |
| 180 | else if (arg && !exc) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 181 | /* `gen` is an exhausted generator: |
| 182 | only set exception if called from send(). */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 183 | if (PyAsyncGen_CheckExact(gen)) { |
| 184 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 185 | } |
| 186 | else { |
| 187 | PyErr_SetNone(PyExc_StopIteration); |
| 188 | } |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 189 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 193 | if (f->f_lasti == -1) { |
| 194 | if (arg && arg != Py_None) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 195 | const char *msg = "can't send non-None value to a " |
| 196 | "just-started generator"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 197 | if (PyCoro_CheckExact(gen)) { |
| 198 | msg = NON_INIT_CORO_MSG; |
| 199 | } |
| 200 | else if (PyAsyncGen_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 201 | msg = "can't send non-None value to a " |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 202 | "just-started async generator"; |
| 203 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 204 | PyErr_SetString(PyExc_TypeError, msg); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | } else { |
| 208 | /* Push arg onto the frame's value stack */ |
| 209 | result = arg ? arg : Py_None; |
| 210 | Py_INCREF(result); |
| 211 | *(f->f_stacktop++) = result; |
| 212 | } |
| 213 | |
| 214 | /* Generators always return to their most recent caller, not |
| 215 | * necessarily their creator. */ |
| 216 | Py_XINCREF(tstate->frame); |
| 217 | assert(f->f_back == NULL); |
| 218 | f->f_back = tstate->frame; |
| 219 | |
| 220 | gen->gi_running = 1; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 221 | gen->gi_exc_state.previous_item = tstate->exc_info; |
| 222 | tstate->exc_info = &gen->gi_exc_state; |
Victor Stinner | b9e6812 | 2019-11-14 12:20:46 +0100 | [diff] [blame] | 223 | result = _PyEval_EvalFrame(tstate, f, exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 224 | tstate->exc_info = gen->gi_exc_state.previous_item; |
| 225 | gen->gi_exc_state.previous_item = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 226 | gen->gi_running = 0; |
| 227 | |
| 228 | /* Don't keep the reference to f_back any longer than necessary. It |
| 229 | * may keep a chain of frames alive or it could create a reference |
| 230 | * cycle. */ |
| 231 | assert(f->f_back == tstate->frame); |
| 232 | Py_CLEAR(f->f_back); |
| 233 | |
| 234 | /* If the generator just returned (as opposed to yielding), signal |
| 235 | * that the generator is exhausted. */ |
| 236 | if (result && f->f_stacktop == NULL) { |
| 237 | if (result == Py_None) { |
| 238 | /* Delay exception instantiation if we can */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 239 | if (PyAsyncGen_CheckExact(gen)) { |
| 240 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 241 | } |
| 242 | else { |
| 243 | PyErr_SetNone(PyExc_StopIteration); |
| 244 | } |
| 245 | } |
| 246 | else { |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 247 | /* Async generators cannot return anything but None */ |
| 248 | assert(!PyAsyncGen_CheckExact(gen)); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 249 | _PyGen_SetStopIterationValue(result); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 250 | } |
| 251 | Py_CLEAR(result); |
| 252 | } |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 253 | else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 254 | const char *msg = "generator raised StopIteration"; |
| 255 | if (PyCoro_CheckExact(gen)) { |
| 256 | msg = "coroutine raised StopIteration"; |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 257 | } |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 258 | else if PyAsyncGen_CheckExact(gen) { |
| 259 | msg = "async generator raised StopIteration"; |
Yury Selivanov | 6833339 | 2015-05-22 11:16:47 -0400 | [diff] [blame] | 260 | } |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 261 | _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); |
| 262 | |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 263 | } |
Yury Selivanov | 43c47fe | 2018-01-26 15:24:24 -0500 | [diff] [blame] | 264 | else if (!result && PyAsyncGen_CheckExact(gen) && |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 265 | PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) |
| 266 | { |
| 267 | /* code in `gen` raised a StopAsyncIteration error: |
| 268 | raise a RuntimeError. |
| 269 | */ |
| 270 | const char *msg = "async generator raised StopAsyncIteration"; |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 271 | _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 272 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 273 | |
| 274 | if (!result || f->f_stacktop == NULL) { |
| 275 | /* generator can't be rerun, so release the frame */ |
| 276 | /* first clean reference cycle through stored exception traceback */ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 277 | exc_state_clear(&gen->gi_exc_state); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 278 | gen->gi_frame->f_gen = NULL; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 279 | gen->gi_frame = NULL; |
| 280 | Py_DECREF(f); |
| 281 | } |
| 282 | |
| 283 | return result; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 286 | PyDoc_STRVAR(send_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | "send(arg) -> send 'arg' into generator,\n\ |
| 288 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 289 | |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 290 | PyObject * |
| 291 | _PyGen_Send(PyGenObject *gen, PyObject *arg) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 292 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 293 | return gen_send_ex(gen, arg, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | PyDoc_STRVAR(close_doc, |
Benjamin Peterson | ab3da29 | 2012-05-03 18:44:09 -0400 | [diff] [blame] | 297 | "close() -> raise GeneratorExit inside generator."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 298 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 299 | /* |
| 300 | * This helper function is used by gen_close and gen_throw to |
| 301 | * close a subiterator being delegated to by yield-from. |
| 302 | */ |
| 303 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 304 | static int |
| 305 | gen_close_iter(PyObject *yf) |
| 306 | { |
| 307 | PyObject *retval = NULL; |
| 308 | _Py_IDENTIFIER(close); |
| 309 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 310 | if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 311 | retval = gen_close((PyGenObject *)yf, NULL); |
| 312 | if (retval == NULL) |
| 313 | return -1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 314 | } |
| 315 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 316 | PyObject *meth; |
| 317 | if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) { |
| 318 | PyErr_WriteUnraisable(yf); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 319 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 320 | if (meth) { |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 321 | retval = _PyObject_CallNoArg(meth); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 322 | Py_DECREF(meth); |
| 323 | if (retval == NULL) |
| 324 | return -1; |
| 325 | } |
| 326 | } |
| 327 | Py_XDECREF(retval); |
| 328 | return 0; |
| 329 | } |
| 330 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 331 | PyObject * |
| 332 | _PyGen_yf(PyGenObject *gen) |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 333 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 334 | PyObject *yf = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 335 | PyFrameObject *f = gen->gi_frame; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 336 | |
| 337 | if (f && f->f_stacktop) { |
| 338 | PyObject *bytecode = f->f_code->co_code; |
| 339 | unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode); |
| 340 | |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 341 | if (f->f_lasti < 0) { |
| 342 | /* Return immediately if the frame didn't start yet. YIELD_FROM |
| 343 | always come after LOAD_CONST: a code object should not start |
| 344 | with YIELD_FROM */ |
| 345 | assert(code[0] != YIELD_FROM); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 349 | if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM) |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 350 | return NULL; |
| 351 | yf = f->f_stacktop[-1]; |
| 352 | Py_INCREF(yf); |
| 353 | } |
| 354 | |
| 355 | return yf; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 356 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 357 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 358 | static PyObject * |
| 359 | gen_close(PyGenObject *gen, PyObject *args) |
| 360 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 361 | PyObject *retval; |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 362 | PyObject *yf = _PyGen_yf(gen); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 363 | int err = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 364 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 365 | if (yf) { |
| 366 | gen->gi_running = 1; |
| 367 | err = gen_close_iter(yf); |
| 368 | gen->gi_running = 0; |
| 369 | Py_DECREF(yf); |
| 370 | } |
| 371 | if (err == 0) |
| 372 | PyErr_SetNone(PyExc_GeneratorExit); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 373 | retval = gen_send_ex(gen, Py_None, 1, 1); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 374 | if (retval) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 375 | const char *msg = "generator ignored GeneratorExit"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 376 | if (PyCoro_CheckExact(gen)) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 377 | msg = "coroutine ignored GeneratorExit"; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 378 | } else if (PyAsyncGen_CheckExact(gen)) { |
| 379 | msg = ASYNC_GEN_IGNORED_EXIT_MSG; |
| 380 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 381 | Py_DECREF(retval); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 382 | PyErr_SetString(PyExc_RuntimeError, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 385 | if (PyErr_ExceptionMatches(PyExc_StopIteration) |
| 386 | || PyErr_ExceptionMatches(PyExc_GeneratorExit)) { |
| 387 | PyErr_Clear(); /* ignore these errors */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 388 | Py_RETURN_NONE; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 389 | } |
| 390 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 393 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 394 | PyDoc_STRVAR(throw_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | "throw(typ[,val[,tb]]) -> raise exception in generator,\n\ |
| 396 | return next yielded value or raise StopIteration."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 397 | |
| 398 | static PyObject * |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 399 | _gen_throw(PyGenObject *gen, int close_on_genexit, |
| 400 | PyObject *typ, PyObject *val, PyObject *tb) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 401 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 402 | PyObject *yf = _PyGen_yf(gen); |
Nick Coghlan | 5b0dac1 | 2012-06-17 15:45:11 +1000 | [diff] [blame] | 403 | _Py_IDENTIFIER(throw); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 404 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 405 | if (yf) { |
| 406 | PyObject *ret; |
| 407 | int err; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 408 | if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) && |
| 409 | close_on_genexit |
| 410 | ) { |
| 411 | /* Asynchronous generators *should not* be closed right away. |
| 412 | We have to allow some awaits to work it through, hence the |
| 413 | `close_on_genexit` parameter here. |
| 414 | */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 415 | gen->gi_running = 1; |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 416 | err = gen_close_iter(yf); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 417 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 418 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 419 | if (err < 0) |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 420 | return gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 421 | goto throw_here; |
| 422 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 423 | if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { |
| 424 | /* `yf` is a generator or a coroutine. */ |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 425 | gen->gi_running = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 426 | /* Close the generator that we are currently iterating with |
| 427 | 'yield from' or awaiting on with 'await'. */ |
| 428 | ret = _gen_throw((PyGenObject *)yf, close_on_genexit, |
| 429 | typ, val, tb); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 430 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 431 | } else { |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 432 | /* `yf` is an iterator or a coroutine-like object. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 433 | PyObject *meth; |
| 434 | if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) { |
| 435 | Py_DECREF(yf); |
| 436 | return NULL; |
| 437 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 438 | if (meth == NULL) { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 439 | Py_DECREF(yf); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 440 | goto throw_here; |
| 441 | } |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 442 | gen->gi_running = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 443 | ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 444 | gen->gi_running = 0; |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 445 | Py_DECREF(meth); |
| 446 | } |
| 447 | Py_DECREF(yf); |
| 448 | if (!ret) { |
| 449 | PyObject *val; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 450 | /* Pop subiterator from stack */ |
| 451 | ret = *(--gen->gi_frame->f_stacktop); |
| 452 | assert(ret == yf); |
| 453 | Py_DECREF(ret); |
| 454 | /* Termination repetition of YIELD_FROM */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 455 | assert(gen->gi_frame->f_lasti >= 0); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 456 | gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 457 | if (_PyGen_FetchStopIterationValue(&val) == 0) { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 458 | ret = gen_send_ex(gen, val, 0, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 459 | Py_DECREF(val); |
| 460 | } else { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 461 | ret = gen_send_ex(gen, Py_None, 1, 0); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | throw_here: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | /* First, check the traceback argument, replacing None with |
| 469 | NULL. */ |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 470 | if (tb == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | tb = NULL; |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
| 474 | PyErr_SetString(PyExc_TypeError, |
| 475 | "throw() third argument must be a traceback object"); |
| 476 | return NULL; |
| 477 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | Py_INCREF(typ); |
| 480 | Py_XINCREF(val); |
| 481 | Py_XINCREF(tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 482 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 483 | if (PyExceptionClass_Check(typ)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | PyErr_NormalizeException(&typ, &val, &tb); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | else if (PyExceptionInstance_Check(typ)) { |
| 487 | /* Raising an instance. The value should be a dummy. */ |
| 488 | if (val && val != Py_None) { |
| 489 | PyErr_SetString(PyExc_TypeError, |
| 490 | "instance exception may not have a separate value"); |
| 491 | goto failed_throw; |
| 492 | } |
| 493 | else { |
| 494 | /* Normalize to raise <class>, <instance> */ |
| 495 | Py_XDECREF(val); |
| 496 | val = typ; |
| 497 | typ = PyExceptionInstance_Class(typ); |
| 498 | Py_INCREF(typ); |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 499 | |
Benjamin Peterson | 9d9141f | 2011-10-19 16:57:40 -0400 | [diff] [blame] | 500 | if (tb == NULL) |
Antoine Pitrou | 551ba20 | 2011-10-18 16:40:50 +0200 | [diff] [blame] | 501 | /* Returns NULL if there's no traceback */ |
| 502 | tb = PyException_GetTraceback(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | else { |
| 506 | /* Not something you can raise. throw() fails. */ |
| 507 | PyErr_Format(PyExc_TypeError, |
| 508 | "exceptions must be classes or instances " |
| 509 | "deriving from BaseException, not %s", |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 510 | Py_TYPE(typ)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | goto failed_throw; |
| 512 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | PyErr_Restore(typ, val, tb); |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 515 | return gen_send_ex(gen, Py_None, 1, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 516 | |
| 517 | failed_throw: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | /* Didn't use our arguments, so restore their original refcounts */ |
| 519 | Py_DECREF(typ); |
| 520 | Py_XDECREF(val); |
| 521 | Py_XDECREF(tb); |
| 522 | return NULL; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
| 526 | static PyObject * |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 527 | gen_throw(PyGenObject *gen, PyObject *args) |
| 528 | { |
| 529 | PyObject *typ; |
| 530 | PyObject *tb = NULL; |
| 531 | PyObject *val = NULL; |
| 532 | |
| 533 | if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) { |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | return _gen_throw(gen, 1, typ, val, tb); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | static PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 542 | gen_iternext(PyGenObject *gen) |
| 543 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 544 | return gen_send_ex(gen, NULL, 0, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 547 | /* |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 548 | * Set StopIteration with specified value. Value can be arbitrary object |
| 549 | * or NULL. |
| 550 | * |
| 551 | * Returns 0 if StopIteration is set and -1 if any other exception is set. |
| 552 | */ |
| 553 | int |
| 554 | _PyGen_SetStopIterationValue(PyObject *value) |
| 555 | { |
| 556 | PyObject *e; |
| 557 | |
| 558 | if (value == NULL || |
Yury Selivanov | b7c9150 | 2017-03-12 15:53:07 -0400 | [diff] [blame] | 559 | (!PyTuple_Check(value) && !PyExceptionInstance_Check(value))) |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 560 | { |
| 561 | /* Delay exception instantiation if we can */ |
| 562 | PyErr_SetObject(PyExc_StopIteration, value); |
| 563 | return 0; |
| 564 | } |
| 565 | /* Construct an exception instance manually with |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 566 | * PyObject_CallOneArg and pass it to PyErr_SetObject. |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 567 | * |
| 568 | * We do this to handle a situation when "value" is a tuple, in which |
| 569 | * case PyErr_SetObject would set the value of StopIteration to |
| 570 | * the first element of the tuple. |
| 571 | * |
| 572 | * (See PyErr_SetObject/_PyErr_CreateException code for details.) |
| 573 | */ |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 574 | e = PyObject_CallOneArg(PyExc_StopIteration, value); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 575 | if (e == NULL) { |
| 576 | return -1; |
| 577 | } |
| 578 | PyErr_SetObject(PyExc_StopIteration, e); |
| 579 | Py_DECREF(e); |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 584 | * If StopIteration exception is set, fetches its 'value' |
| 585 | * attribute if any, otherwise sets pvalue to None. |
| 586 | * |
| 587 | * Returns 0 if no exception or StopIteration is set. |
| 588 | * If any other exception is set, returns -1 and leaves |
| 589 | * pvalue unchanged. |
| 590 | */ |
| 591 | |
| 592 | int |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 593 | _PyGen_FetchStopIterationValue(PyObject **pvalue) |
| 594 | { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 595 | PyObject *et, *ev, *tb; |
| 596 | PyObject *value = NULL; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 597 | |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 598 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 599 | PyErr_Fetch(&et, &ev, &tb); |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 600 | if (ev) { |
| 601 | /* exception will usually be normalised already */ |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 602 | if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 603 | value = ((PyStopIterationObject *)ev)->value; |
| 604 | Py_INCREF(value); |
| 605 | Py_DECREF(ev); |
Serhiy Storchaka | 24411f8 | 2016-11-06 18:44:42 +0200 | [diff] [blame] | 606 | } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) { |
| 607 | /* Avoid normalisation and take ev as value. |
| 608 | * |
| 609 | * Normalization is required if the value is a tuple, in |
| 610 | * that case the value of StopIteration would be set to |
| 611 | * the first element of the tuple. |
| 612 | * |
| 613 | * (See _PyErr_CreateException code for details.) |
| 614 | */ |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 615 | value = ev; |
| 616 | } else { |
| 617 | /* normalisation required */ |
| 618 | PyErr_NormalizeException(&et, &ev, &tb); |
Serhiy Storchaka | 08d230a | 2015-05-22 11:02:49 +0300 | [diff] [blame] | 619 | if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) { |
Antoine Pitrou | 7403e91 | 2015-04-26 18:46:40 +0200 | [diff] [blame] | 620 | PyErr_Restore(et, ev, tb); |
| 621 | return -1; |
| 622 | } |
| 623 | value = ((PyStopIterationObject *)ev)->value; |
| 624 | Py_INCREF(value); |
| 625 | Py_DECREF(ev); |
| 626 | } |
| 627 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 628 | Py_XDECREF(et); |
| 629 | Py_XDECREF(tb); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 630 | } else if (PyErr_Occurred()) { |
| 631 | return -1; |
| 632 | } |
| 633 | if (value == NULL) { |
| 634 | value = Py_None; |
Amaury Forgeot d'Arc | e557da8 | 2012-01-13 21:06:12 +0100 | [diff] [blame] | 635 | Py_INCREF(value); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 636 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 637 | *pvalue = value; |
| 638 | return 0; |
| 639 | } |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 640 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 641 | static PyObject * |
| 642 | gen_repr(PyGenObject *gen) |
| 643 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 644 | return PyUnicode_FromFormat("<generator object %S at %p>", |
| 645 | gen->gi_qualname, gen); |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 648 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 649 | gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 650 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 651 | Py_INCREF(op->gi_name); |
| 652 | return op->gi_name; |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 655 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 656 | gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 657 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 658 | /* Not legal to del gen.gi_name or to set it to anything |
| 659 | * other than a string object. */ |
| 660 | if (value == NULL || !PyUnicode_Check(value)) { |
| 661 | PyErr_SetString(PyExc_TypeError, |
| 662 | "__name__ must be set to a string object"); |
| 663 | return -1; |
| 664 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 665 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 666 | Py_XSETREF(op->gi_name, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 671 | gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 672 | { |
| 673 | Py_INCREF(op->gi_qualname); |
| 674 | return op->gi_qualname; |
| 675 | } |
| 676 | |
| 677 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 678 | gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 679 | { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 680 | /* Not legal to del gen.__qualname__ or to set it to anything |
| 681 | * other than a string object. */ |
| 682 | if (value == NULL || !PyUnicode_Check(value)) { |
| 683 | PyErr_SetString(PyExc_TypeError, |
| 684 | "__qualname__ must be set to a string object"); |
| 685 | return -1; |
| 686 | } |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 687 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 688 | Py_XSETREF(op->gi_qualname, value); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 689 | return 0; |
| 690 | } |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 691 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 692 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 693 | gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored)) |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 694 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 695 | PyObject *yf = _PyGen_yf(gen); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 696 | if (yf == NULL) |
| 697 | Py_RETURN_NONE; |
| 698 | return yf; |
| 699 | } |
| 700 | |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 701 | static PyGetSetDef gen_getsetlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 702 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 703 | PyDoc_STR("name of the generator")}, |
| 704 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 705 | PyDoc_STR("qualified name of the generator")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 706 | {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL, |
| 707 | PyDoc_STR("object being iterated by yield from, or None")}, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 708 | {NULL} /* Sentinel */ |
Alexandre Vassalotti | e9f305f | 2008-05-16 04:39:54 +0000 | [diff] [blame] | 709 | }; |
| 710 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 711 | static PyMemberDef gen_memberlist[] = { |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 712 | {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY}, |
| 713 | {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY}, |
| 714 | {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | {NULL} /* Sentinel */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 718 | static PyMethodDef gen_methods[] = { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 719 | {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc}, |
| 721 | {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc}, |
| 722 | {NULL, NULL} /* Sentinel */ |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 723 | }; |
| 724 | |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 725 | PyTypeObject PyGen_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 727 | "generator", /* tp_name */ |
| 728 | sizeof(PyGenObject), /* tp_basicsize */ |
| 729 | 0, /* tp_itemsize */ |
| 730 | /* methods */ |
| 731 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 732 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | 0, /* tp_getattr */ |
| 734 | 0, /* tp_setattr */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 735 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | (reprfunc)gen_repr, /* tp_repr */ |
| 737 | 0, /* tp_as_number */ |
| 738 | 0, /* tp_as_sequence */ |
| 739 | 0, /* tp_as_mapping */ |
| 740 | 0, /* tp_hash */ |
| 741 | 0, /* tp_call */ |
| 742 | 0, /* tp_str */ |
| 743 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 744 | 0, /* tp_setattro */ |
| 745 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 746 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | 0, /* tp_doc */ |
| 748 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 749 | 0, /* tp_clear */ |
| 750 | 0, /* tp_richcompare */ |
| 751 | offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 752 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | (iternextfunc)gen_iternext, /* tp_iternext */ |
| 754 | gen_methods, /* tp_methods */ |
| 755 | gen_memberlist, /* tp_members */ |
| 756 | gen_getsetlist, /* tp_getset */ |
| 757 | 0, /* tp_base */ |
| 758 | 0, /* tp_dict */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | 0, /* tp_descr_get */ |
| 761 | 0, /* tp_descr_set */ |
| 762 | 0, /* tp_dictoffset */ |
| 763 | 0, /* tp_init */ |
| 764 | 0, /* tp_alloc */ |
| 765 | 0, /* tp_new */ |
| 766 | 0, /* tp_free */ |
| 767 | 0, /* tp_is_gc */ |
| 768 | 0, /* tp_bases */ |
| 769 | 0, /* tp_mro */ |
| 770 | 0, /* tp_cache */ |
| 771 | 0, /* tp_subclasses */ |
| 772 | 0, /* tp_weaklist */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 773 | 0, /* tp_del */ |
| 774 | 0, /* tp_version_tag */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 775 | _PyGen_Finalize, /* tp_finalize */ |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 778 | static PyObject * |
| 779 | gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, |
| 780 | PyObject *name, PyObject *qualname) |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 781 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 782 | PyGenObject *gen = PyObject_GC_New(PyGenObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | if (gen == NULL) { |
| 784 | Py_DECREF(f); |
| 785 | return NULL; |
| 786 | } |
| 787 | gen->gi_frame = f; |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 788 | f->f_gen = (PyObject *) gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | Py_INCREF(f->f_code); |
| 790 | gen->gi_code = (PyObject *)(f->f_code); |
| 791 | gen->gi_running = 0; |
| 792 | gen->gi_weakreflist = NULL; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 793 | gen->gi_exc_state.exc_type = NULL; |
| 794 | gen->gi_exc_state.exc_value = NULL; |
| 795 | gen->gi_exc_state.exc_traceback = NULL; |
| 796 | gen->gi_exc_state.previous_item = NULL; |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 797 | if (name != NULL) |
| 798 | gen->gi_name = name; |
| 799 | else |
| 800 | gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; |
| 801 | Py_INCREF(gen->gi_name); |
| 802 | if (qualname != NULL) |
| 803 | gen->gi_qualname = qualname; |
| 804 | else |
| 805 | gen->gi_qualname = gen->gi_name; |
| 806 | Py_INCREF(gen->gi_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | _PyObject_GC_TRACK(gen); |
| 808 | return (PyObject *)gen; |
Martin v. Löwis | e440e47 | 2004-06-01 15:22:42 +0000 | [diff] [blame] | 809 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 810 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 811 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 812 | PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 813 | { |
| 814 | return gen_new_with_qualname(&PyGen_Type, f, name, qualname); |
| 815 | } |
| 816 | |
| 817 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 818 | PyGen_New(PyFrameObject *f) |
| 819 | { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 820 | return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL); |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 823 | /* Coroutine Object */ |
| 824 | |
| 825 | typedef struct { |
| 826 | PyObject_HEAD |
| 827 | PyCoroObject *cw_coroutine; |
| 828 | } PyCoroWrapper; |
| 829 | |
| 830 | static int |
| 831 | gen_is_coroutine(PyObject *o) |
| 832 | { |
| 833 | if (PyGen_CheckExact(o)) { |
| 834 | PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code; |
| 835 | if (code->co_flags & CO_ITERABLE_COROUTINE) { |
| 836 | return 1; |
| 837 | } |
| 838 | } |
| 839 | return 0; |
| 840 | } |
| 841 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 842 | /* |
| 843 | * This helper function returns an awaitable for `o`: |
| 844 | * - `o` if `o` is a coroutine-object; |
| 845 | * - `type(o)->tp_as_async->am_await(o)` |
| 846 | * |
| 847 | * Raises a TypeError if it's not possible to return |
| 848 | * an awaitable and returns NULL. |
| 849 | */ |
| 850 | PyObject * |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 851 | _PyCoro_GetAwaitableIter(PyObject *o) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 852 | { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 853 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 854 | PyTypeObject *ot; |
| 855 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 856 | if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) { |
| 857 | /* 'o' is a coroutine. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 858 | Py_INCREF(o); |
| 859 | return o; |
| 860 | } |
| 861 | |
| 862 | ot = Py_TYPE(o); |
| 863 | if (ot->tp_as_async != NULL) { |
| 864 | getter = ot->tp_as_async->am_await; |
| 865 | } |
| 866 | if (getter != NULL) { |
| 867 | PyObject *res = (*getter)(o); |
| 868 | if (res != NULL) { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 869 | if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) { |
| 870 | /* __await__ must return an *iterator*, not |
| 871 | a coroutine or another awaitable (see PEP 492) */ |
| 872 | PyErr_SetString(PyExc_TypeError, |
| 873 | "__await__() returned a coroutine"); |
| 874 | Py_CLEAR(res); |
| 875 | } else if (!PyIter_Check(res)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 876 | PyErr_Format(PyExc_TypeError, |
| 877 | "__await__() returned non-iterator " |
| 878 | "of type '%.100s'", |
| 879 | Py_TYPE(res)->tp_name); |
| 880 | Py_CLEAR(res); |
| 881 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 882 | } |
| 883 | return res; |
| 884 | } |
| 885 | |
| 886 | PyErr_Format(PyExc_TypeError, |
| 887 | "object %.100s can't be used in 'await' expression", |
| 888 | ot->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 889 | return NULL; |
| 890 | } |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 891 | |
| 892 | static PyObject * |
| 893 | coro_repr(PyCoroObject *coro) |
| 894 | { |
| 895 | return PyUnicode_FromFormat("<coroutine object %S at %p>", |
| 896 | coro->cr_qualname, coro); |
| 897 | } |
| 898 | |
| 899 | static PyObject * |
| 900 | coro_await(PyCoroObject *coro) |
| 901 | { |
| 902 | PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type); |
| 903 | if (cw == NULL) { |
| 904 | return NULL; |
| 905 | } |
| 906 | Py_INCREF(coro); |
| 907 | cw->cw_coroutine = coro; |
| 908 | _PyObject_GC_TRACK(cw); |
| 909 | return (PyObject *)cw; |
| 910 | } |
| 911 | |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 912 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 913 | coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored)) |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 914 | { |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 915 | PyObject *yf = _PyGen_yf((PyGenObject *) coro); |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 916 | if (yf == NULL) |
| 917 | Py_RETURN_NONE; |
| 918 | return yf; |
| 919 | } |
| 920 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 921 | static PyGetSetDef coro_getsetlist[] = { |
| 922 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 923 | PyDoc_STR("name of the coroutine")}, |
| 924 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 925 | PyDoc_STR("qualified name of the coroutine")}, |
Yury Selivanov | e13f8f3 | 2015-07-03 00:23:30 -0400 | [diff] [blame] | 926 | {"cr_await", (getter)coro_get_cr_await, NULL, |
| 927 | PyDoc_STR("object being awaited on, or None")}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 928 | {NULL} /* Sentinel */ |
| 929 | }; |
| 930 | |
| 931 | static PyMemberDef coro_memberlist[] = { |
| 932 | {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY}, |
| 933 | {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY}, |
| 934 | {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY}, |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 935 | {"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin), READONLY}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 936 | {NULL} /* Sentinel */ |
| 937 | }; |
| 938 | |
| 939 | PyDoc_STRVAR(coro_send_doc, |
| 940 | "send(arg) -> send 'arg' into coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 941 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 942 | |
| 943 | PyDoc_STRVAR(coro_throw_doc, |
| 944 | "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\ |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 945 | return next iterated value or raise StopIteration."); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 946 | |
| 947 | PyDoc_STRVAR(coro_close_doc, |
| 948 | "close() -> raise GeneratorExit inside coroutine."); |
| 949 | |
| 950 | static PyMethodDef coro_methods[] = { |
| 951 | {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc}, |
| 952 | {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc}, |
| 953 | {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc}, |
| 954 | {NULL, NULL} /* Sentinel */ |
| 955 | }; |
| 956 | |
| 957 | static PyAsyncMethods coro_as_async = { |
| 958 | (unaryfunc)coro_await, /* am_await */ |
| 959 | 0, /* am_aiter */ |
| 960 | 0 /* am_anext */ |
| 961 | }; |
| 962 | |
| 963 | PyTypeObject PyCoro_Type = { |
| 964 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 965 | "coroutine", /* tp_name */ |
| 966 | sizeof(PyCoroObject), /* tp_basicsize */ |
| 967 | 0, /* tp_itemsize */ |
| 968 | /* methods */ |
| 969 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 970 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 971 | 0, /* tp_getattr */ |
| 972 | 0, /* tp_setattr */ |
| 973 | &coro_as_async, /* tp_as_async */ |
| 974 | (reprfunc)coro_repr, /* tp_repr */ |
| 975 | 0, /* tp_as_number */ |
| 976 | 0, /* tp_as_sequence */ |
| 977 | 0, /* tp_as_mapping */ |
| 978 | 0, /* tp_hash */ |
| 979 | 0, /* tp_call */ |
| 980 | 0, /* tp_str */ |
| 981 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 982 | 0, /* tp_setattro */ |
| 983 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 984 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 985 | 0, /* tp_doc */ |
| 986 | (traverseproc)gen_traverse, /* tp_traverse */ |
| 987 | 0, /* tp_clear */ |
| 988 | 0, /* tp_richcompare */ |
| 989 | offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */ |
| 990 | 0, /* tp_iter */ |
| 991 | 0, /* tp_iternext */ |
| 992 | coro_methods, /* tp_methods */ |
| 993 | coro_memberlist, /* tp_members */ |
| 994 | coro_getsetlist, /* tp_getset */ |
| 995 | 0, /* tp_base */ |
| 996 | 0, /* tp_dict */ |
| 997 | 0, /* tp_descr_get */ |
| 998 | 0, /* tp_descr_set */ |
| 999 | 0, /* tp_dictoffset */ |
| 1000 | 0, /* tp_init */ |
| 1001 | 0, /* tp_alloc */ |
| 1002 | 0, /* tp_new */ |
| 1003 | 0, /* tp_free */ |
| 1004 | 0, /* tp_is_gc */ |
| 1005 | 0, /* tp_bases */ |
| 1006 | 0, /* tp_mro */ |
| 1007 | 0, /* tp_cache */ |
| 1008 | 0, /* tp_subclasses */ |
| 1009 | 0, /* tp_weaklist */ |
| 1010 | 0, /* tp_del */ |
| 1011 | 0, /* tp_version_tag */ |
| 1012 | _PyGen_Finalize, /* tp_finalize */ |
| 1013 | }; |
| 1014 | |
| 1015 | static void |
| 1016 | coro_wrapper_dealloc(PyCoroWrapper *cw) |
| 1017 | { |
| 1018 | _PyObject_GC_UNTRACK((PyObject *)cw); |
| 1019 | Py_CLEAR(cw->cw_coroutine); |
| 1020 | PyObject_GC_Del(cw); |
| 1021 | } |
| 1022 | |
| 1023 | static PyObject * |
| 1024 | coro_wrapper_iternext(PyCoroWrapper *cw) |
| 1025 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 1026 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | static PyObject * |
| 1030 | coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg) |
| 1031 | { |
Yury Selivanov | 77c9681 | 2016-02-13 17:59:05 -0500 | [diff] [blame] | 1032 | return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | static PyObject * |
| 1036 | coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args) |
| 1037 | { |
| 1038 | return gen_throw((PyGenObject *)cw->cw_coroutine, args); |
| 1039 | } |
| 1040 | |
| 1041 | static PyObject * |
| 1042 | coro_wrapper_close(PyCoroWrapper *cw, PyObject *args) |
| 1043 | { |
| 1044 | return gen_close((PyGenObject *)cw->cw_coroutine, args); |
| 1045 | } |
| 1046 | |
| 1047 | static int |
| 1048 | coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg) |
| 1049 | { |
| 1050 | Py_VISIT((PyObject *)cw->cw_coroutine); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static PyMethodDef coro_wrapper_methods[] = { |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 1055 | {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, |
| 1056 | {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc}, |
| 1057 | {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc}, |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1058 | {NULL, NULL} /* Sentinel */ |
| 1059 | }; |
| 1060 | |
| 1061 | PyTypeObject _PyCoroWrapper_Type = { |
| 1062 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1063 | "coroutine_wrapper", |
| 1064 | sizeof(PyCoroWrapper), /* tp_basicsize */ |
| 1065 | 0, /* tp_itemsize */ |
| 1066 | (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1067 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1068 | 0, /* tp_getattr */ |
| 1069 | 0, /* tp_setattr */ |
| 1070 | 0, /* tp_as_async */ |
| 1071 | 0, /* tp_repr */ |
| 1072 | 0, /* tp_as_number */ |
| 1073 | 0, /* tp_as_sequence */ |
| 1074 | 0, /* tp_as_mapping */ |
| 1075 | 0, /* tp_hash */ |
| 1076 | 0, /* tp_call */ |
| 1077 | 0, /* tp_str */ |
| 1078 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1079 | 0, /* tp_setattro */ |
| 1080 | 0, /* tp_as_buffer */ |
| 1081 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1082 | "A wrapper object implementing __await__ for coroutines.", |
| 1083 | (traverseproc)coro_wrapper_traverse, /* tp_traverse */ |
| 1084 | 0, /* tp_clear */ |
| 1085 | 0, /* tp_richcompare */ |
| 1086 | 0, /* tp_weaklistoffset */ |
| 1087 | PyObject_SelfIter, /* tp_iter */ |
| 1088 | (iternextfunc)coro_wrapper_iternext, /* tp_iternext */ |
| 1089 | coro_wrapper_methods, /* tp_methods */ |
| 1090 | 0, /* tp_members */ |
| 1091 | 0, /* tp_getset */ |
| 1092 | 0, /* tp_base */ |
| 1093 | 0, /* tp_dict */ |
| 1094 | 0, /* tp_descr_get */ |
| 1095 | 0, /* tp_descr_set */ |
| 1096 | 0, /* tp_dictoffset */ |
| 1097 | 0, /* tp_init */ |
| 1098 | 0, /* tp_alloc */ |
| 1099 | 0, /* tp_new */ |
Yury Selivanov | 33499b7 | 2016-11-08 19:19:28 -0500 | [diff] [blame] | 1100 | 0, /* tp_free */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1101 | }; |
| 1102 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1103 | static PyObject * |
| 1104 | compute_cr_origin(int origin_depth) |
| 1105 | { |
| 1106 | PyFrameObject *frame = PyEval_GetFrame(); |
| 1107 | /* First count how many frames we have */ |
| 1108 | int frame_count = 0; |
| 1109 | for (; frame && frame_count < origin_depth; ++frame_count) { |
| 1110 | frame = frame->f_back; |
| 1111 | } |
| 1112 | |
| 1113 | /* Now collect them */ |
| 1114 | PyObject *cr_origin = PyTuple_New(frame_count); |
Alexey Izbyshev | 8fdd331 | 2018-08-25 10:15:23 +0300 | [diff] [blame] | 1115 | if (cr_origin == NULL) { |
| 1116 | return NULL; |
| 1117 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1118 | frame = PyEval_GetFrame(); |
| 1119 | for (int i = 0; i < frame_count; ++i) { |
| 1120 | PyObject *frameinfo = Py_BuildValue( |
| 1121 | "OiO", |
| 1122 | frame->f_code->co_filename, |
| 1123 | PyFrame_GetLineNumber(frame), |
| 1124 | frame->f_code->co_name); |
| 1125 | if (!frameinfo) { |
| 1126 | Py_DECREF(cr_origin); |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | PyTuple_SET_ITEM(cr_origin, i, frameinfo); |
| 1130 | frame = frame->f_back; |
| 1131 | } |
| 1132 | |
| 1133 | return cr_origin; |
| 1134 | } |
| 1135 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1136 | PyObject * |
| 1137 | PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 1138 | { |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1139 | PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname); |
| 1140 | if (!coro) { |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1144 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1145 | int origin_depth = tstate->coroutine_origin_tracking_depth; |
| 1146 | |
| 1147 | if (origin_depth == 0) { |
| 1148 | ((PyCoroObject *)coro)->cr_origin = NULL; |
| 1149 | } else { |
| 1150 | PyObject *cr_origin = compute_cr_origin(origin_depth); |
Zackery Spytz | 062a57b | 2018-11-18 09:45:57 -0700 | [diff] [blame] | 1151 | ((PyCoroObject *)coro)->cr_origin = cr_origin; |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1152 | if (!cr_origin) { |
| 1153 | Py_DECREF(coro); |
| 1154 | return NULL; |
| 1155 | } |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | return coro; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1159 | } |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1160 | |
| 1161 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1162 | /* ========= Asynchronous Generators ========= */ |
| 1163 | |
| 1164 | |
| 1165 | typedef enum { |
| 1166 | AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */ |
| 1167 | AWAITABLE_STATE_ITER, /* being iterated */ |
| 1168 | AWAITABLE_STATE_CLOSED, /* closed */ |
| 1169 | } AwaitableState; |
| 1170 | |
| 1171 | |
| 1172 | typedef struct { |
| 1173 | PyObject_HEAD |
| 1174 | PyAsyncGenObject *ags_gen; |
| 1175 | |
| 1176 | /* Can be NULL, when in the __anext__() mode |
| 1177 | (equivalent of "asend(None)") */ |
| 1178 | PyObject *ags_sendval; |
| 1179 | |
| 1180 | AwaitableState ags_state; |
| 1181 | } PyAsyncGenASend; |
| 1182 | |
| 1183 | |
| 1184 | typedef struct { |
| 1185 | PyObject_HEAD |
| 1186 | PyAsyncGenObject *agt_gen; |
| 1187 | |
| 1188 | /* Can be NULL, when in the "aclose()" mode |
| 1189 | (equivalent of "athrow(GeneratorExit)") */ |
| 1190 | PyObject *agt_args; |
| 1191 | |
| 1192 | AwaitableState agt_state; |
| 1193 | } PyAsyncGenAThrow; |
| 1194 | |
| 1195 | |
| 1196 | typedef struct { |
| 1197 | PyObject_HEAD |
| 1198 | PyObject *agw_val; |
| 1199 | } _PyAsyncGenWrappedValue; |
| 1200 | |
| 1201 | |
| 1202 | #ifndef _PyAsyncGen_MAXFREELIST |
| 1203 | #define _PyAsyncGen_MAXFREELIST 80 |
| 1204 | #endif |
| 1205 | |
| 1206 | /* Freelists boost performance 6-10%; they also reduce memory |
| 1207 | fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend |
| 1208 | are short-living objects that are instantiated for every |
| 1209 | __anext__ call. |
| 1210 | */ |
| 1211 | |
| 1212 | static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST]; |
| 1213 | static int ag_value_freelist_free = 0; |
| 1214 | |
| 1215 | static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST]; |
| 1216 | static int ag_asend_freelist_free = 0; |
| 1217 | |
| 1218 | #define _PyAsyncGenWrappedValue_CheckExact(o) \ |
| 1219 | (Py_TYPE(o) == &_PyAsyncGenWrappedValue_Type) |
| 1220 | |
| 1221 | #define PyAsyncGenASend_CheckExact(o) \ |
| 1222 | (Py_TYPE(o) == &_PyAsyncGenASend_Type) |
| 1223 | |
| 1224 | |
| 1225 | static int |
| 1226 | async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg) |
| 1227 | { |
| 1228 | Py_VISIT(gen->ag_finalizer); |
| 1229 | return gen_traverse((PyGenObject*)gen, visit, arg); |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | static PyObject * |
| 1234 | async_gen_repr(PyAsyncGenObject *o) |
| 1235 | { |
| 1236 | return PyUnicode_FromFormat("<async_generator object %S at %p>", |
| 1237 | o->ag_qualname, o); |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | static int |
| 1242 | async_gen_init_hooks(PyAsyncGenObject *o) |
| 1243 | { |
| 1244 | PyThreadState *tstate; |
| 1245 | PyObject *finalizer; |
| 1246 | PyObject *firstiter; |
| 1247 | |
| 1248 | if (o->ag_hooks_inited) { |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | o->ag_hooks_inited = 1; |
| 1253 | |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1254 | tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1255 | |
| 1256 | finalizer = tstate->async_gen_finalizer; |
| 1257 | if (finalizer) { |
| 1258 | Py_INCREF(finalizer); |
| 1259 | o->ag_finalizer = finalizer; |
| 1260 | } |
| 1261 | |
| 1262 | firstiter = tstate->async_gen_firstiter; |
| 1263 | if (firstiter) { |
| 1264 | PyObject *res; |
| 1265 | |
| 1266 | Py_INCREF(firstiter); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1267 | res = PyObject_CallOneArg(firstiter, (PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1268 | Py_DECREF(firstiter); |
| 1269 | if (res == NULL) { |
| 1270 | return 1; |
| 1271 | } |
| 1272 | Py_DECREF(res); |
| 1273 | } |
| 1274 | |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | static PyObject * |
| 1280 | async_gen_anext(PyAsyncGenObject *o) |
| 1281 | { |
| 1282 | if (async_gen_init_hooks(o)) { |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | return async_gen_asend_new(o, NULL); |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | static PyObject * |
| 1290 | async_gen_asend(PyAsyncGenObject *o, PyObject *arg) |
| 1291 | { |
| 1292 | if (async_gen_init_hooks(o)) { |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | return async_gen_asend_new(o, arg); |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | static PyObject * |
| 1300 | async_gen_aclose(PyAsyncGenObject *o, PyObject *arg) |
| 1301 | { |
| 1302 | if (async_gen_init_hooks(o)) { |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | return async_gen_athrow_new(o, NULL); |
| 1306 | } |
| 1307 | |
| 1308 | static PyObject * |
| 1309 | async_gen_athrow(PyAsyncGenObject *o, PyObject *args) |
| 1310 | { |
| 1311 | if (async_gen_init_hooks(o)) { |
| 1312 | return NULL; |
| 1313 | } |
| 1314 | return async_gen_athrow_new(o, args); |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | static PyGetSetDef async_gen_getsetlist[] = { |
| 1319 | {"__name__", (getter)gen_get_name, (setter)gen_set_name, |
| 1320 | PyDoc_STR("name of the async generator")}, |
| 1321 | {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname, |
| 1322 | PyDoc_STR("qualified name of the async generator")}, |
| 1323 | {"ag_await", (getter)coro_get_cr_await, NULL, |
| 1324 | PyDoc_STR("object being awaited on, or None")}, |
| 1325 | {NULL} /* Sentinel */ |
| 1326 | }; |
| 1327 | |
| 1328 | static PyMemberDef async_gen_memberlist[] = { |
| 1329 | {"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY}, |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1330 | {"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async), |
| 1331 | READONLY}, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1332 | {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY}, |
| 1333 | {NULL} /* Sentinel */ |
| 1334 | }; |
| 1335 | |
| 1336 | PyDoc_STRVAR(async_aclose_doc, |
| 1337 | "aclose() -> raise GeneratorExit inside generator."); |
| 1338 | |
| 1339 | PyDoc_STRVAR(async_asend_doc, |
| 1340 | "asend(v) -> send 'v' in generator."); |
| 1341 | |
| 1342 | PyDoc_STRVAR(async_athrow_doc, |
| 1343 | "athrow(typ[,val[,tb]]) -> raise exception in generator."); |
| 1344 | |
| 1345 | static PyMethodDef async_gen_methods[] = { |
| 1346 | {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc}, |
| 1347 | {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc}, |
| 1348 | {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc}, |
| 1349 | {NULL, NULL} /* Sentinel */ |
| 1350 | }; |
| 1351 | |
| 1352 | |
| 1353 | static PyAsyncMethods async_gen_as_async = { |
| 1354 | 0, /* am_await */ |
| 1355 | PyObject_SelfIter, /* am_aiter */ |
| 1356 | (unaryfunc)async_gen_anext /* am_anext */ |
| 1357 | }; |
| 1358 | |
| 1359 | |
| 1360 | PyTypeObject PyAsyncGen_Type = { |
| 1361 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1362 | "async_generator", /* tp_name */ |
| 1363 | sizeof(PyAsyncGenObject), /* tp_basicsize */ |
| 1364 | 0, /* tp_itemsize */ |
| 1365 | /* methods */ |
| 1366 | (destructor)gen_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1367 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1368 | 0, /* tp_getattr */ |
| 1369 | 0, /* tp_setattr */ |
| 1370 | &async_gen_as_async, /* tp_as_async */ |
| 1371 | (reprfunc)async_gen_repr, /* tp_repr */ |
| 1372 | 0, /* tp_as_number */ |
| 1373 | 0, /* tp_as_sequence */ |
| 1374 | 0, /* tp_as_mapping */ |
| 1375 | 0, /* tp_hash */ |
| 1376 | 0, /* tp_call */ |
| 1377 | 0, /* tp_str */ |
| 1378 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1379 | 0, /* tp_setattro */ |
| 1380 | 0, /* tp_as_buffer */ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 1381 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1382 | 0, /* tp_doc */ |
| 1383 | (traverseproc)async_gen_traverse, /* tp_traverse */ |
| 1384 | 0, /* tp_clear */ |
| 1385 | 0, /* tp_richcompare */ |
| 1386 | offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */ |
| 1387 | 0, /* tp_iter */ |
| 1388 | 0, /* tp_iternext */ |
| 1389 | async_gen_methods, /* tp_methods */ |
| 1390 | async_gen_memberlist, /* tp_members */ |
| 1391 | async_gen_getsetlist, /* tp_getset */ |
| 1392 | 0, /* tp_base */ |
| 1393 | 0, /* tp_dict */ |
| 1394 | 0, /* tp_descr_get */ |
| 1395 | 0, /* tp_descr_set */ |
| 1396 | 0, /* tp_dictoffset */ |
| 1397 | 0, /* tp_init */ |
| 1398 | 0, /* tp_alloc */ |
| 1399 | 0, /* tp_new */ |
| 1400 | 0, /* tp_free */ |
| 1401 | 0, /* tp_is_gc */ |
| 1402 | 0, /* tp_bases */ |
| 1403 | 0, /* tp_mro */ |
| 1404 | 0, /* tp_cache */ |
| 1405 | 0, /* tp_subclasses */ |
| 1406 | 0, /* tp_weaklist */ |
| 1407 | 0, /* tp_del */ |
| 1408 | 0, /* tp_version_tag */ |
| 1409 | _PyGen_Finalize, /* tp_finalize */ |
| 1410 | }; |
| 1411 | |
| 1412 | |
| 1413 | PyObject * |
| 1414 | PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname) |
| 1415 | { |
| 1416 | PyAsyncGenObject *o; |
| 1417 | o = (PyAsyncGenObject *)gen_new_with_qualname( |
| 1418 | &PyAsyncGen_Type, f, name, qualname); |
| 1419 | if (o == NULL) { |
| 1420 | return NULL; |
| 1421 | } |
| 1422 | o->ag_finalizer = NULL; |
| 1423 | o->ag_closed = 0; |
| 1424 | o->ag_hooks_inited = 0; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1425 | o->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1426 | return (PyObject*)o; |
| 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | int |
| 1431 | PyAsyncGen_ClearFreeLists(void) |
| 1432 | { |
| 1433 | int ret = ag_value_freelist_free + ag_asend_freelist_free; |
| 1434 | |
| 1435 | while (ag_value_freelist_free) { |
| 1436 | _PyAsyncGenWrappedValue *o; |
| 1437 | o = ag_value_freelist[--ag_value_freelist_free]; |
| 1438 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1439 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | while (ag_asend_freelist_free) { |
| 1443 | PyAsyncGenASend *o; |
| 1444 | o = ag_asend_freelist[--ag_asend_freelist_free]; |
| 1445 | assert(Py_TYPE(o) == &_PyAsyncGenASend_Type); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1446 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | return ret; |
| 1450 | } |
| 1451 | |
| 1452 | void |
Victor Stinner | bed4817 | 2019-08-27 00:12:32 +0200 | [diff] [blame] | 1453 | _PyAsyncGen_Fini(void) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1454 | { |
| 1455 | PyAsyncGen_ClearFreeLists(); |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | static PyObject * |
| 1460 | async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result) |
| 1461 | { |
| 1462 | if (result == NULL) { |
| 1463 | if (!PyErr_Occurred()) { |
| 1464 | PyErr_SetNone(PyExc_StopAsyncIteration); |
| 1465 | } |
| 1466 | |
| 1467 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) |
| 1468 | || PyErr_ExceptionMatches(PyExc_GeneratorExit) |
| 1469 | ) { |
| 1470 | gen->ag_closed = 1; |
| 1471 | } |
| 1472 | |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1473 | gen->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1474 | return NULL; |
| 1475 | } |
| 1476 | |
| 1477 | if (_PyAsyncGenWrappedValue_CheckExact(result)) { |
| 1478 | /* async yield */ |
Serhiy Storchaka | 60e49aa | 2016-11-06 18:47:03 +0200 | [diff] [blame] | 1479 | _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1480 | Py_DECREF(result); |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1481 | gen->ag_running_async = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1482 | return NULL; |
| 1483 | } |
| 1484 | |
| 1485 | return result; |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | /* ---------- Async Generator ASend Awaitable ------------ */ |
| 1490 | |
| 1491 | |
| 1492 | static void |
| 1493 | async_gen_asend_dealloc(PyAsyncGenASend *o) |
| 1494 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1495 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1496 | Py_CLEAR(o->ags_gen); |
| 1497 | Py_CLEAR(o->ags_sendval); |
| 1498 | if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) { |
| 1499 | assert(PyAsyncGenASend_CheckExact(o)); |
| 1500 | ag_asend_freelist[ag_asend_freelist_free++] = o; |
| 1501 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1502 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1506 | static int |
| 1507 | async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg) |
| 1508 | { |
| 1509 | Py_VISIT(o->ags_gen); |
| 1510 | Py_VISIT(o->ags_sendval); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1514 | |
| 1515 | static PyObject * |
| 1516 | async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg) |
| 1517 | { |
| 1518 | PyObject *result; |
| 1519 | |
| 1520 | if (o->ags_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1521 | PyErr_SetString( |
| 1522 | PyExc_RuntimeError, |
| 1523 | "cannot reuse already awaited __anext__()/asend()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1524 | return NULL; |
| 1525 | } |
| 1526 | |
| 1527 | if (o->ags_state == AWAITABLE_STATE_INIT) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1528 | if (o->ags_gen->ag_running_async) { |
| 1529 | PyErr_SetString( |
| 1530 | PyExc_RuntimeError, |
| 1531 | "anext(): asynchronous generator is already running"); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1535 | if (arg == NULL || arg == Py_None) { |
| 1536 | arg = o->ags_sendval; |
| 1537 | } |
| 1538 | o->ags_state = AWAITABLE_STATE_ITER; |
| 1539 | } |
| 1540 | |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1541 | o->ags_gen->ag_running_async = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1542 | result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0); |
| 1543 | result = async_gen_unwrap_value(o->ags_gen, result); |
| 1544 | |
| 1545 | if (result == NULL) { |
| 1546 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1547 | } |
| 1548 | |
| 1549 | return result; |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | static PyObject * |
| 1554 | async_gen_asend_iternext(PyAsyncGenASend *o) |
| 1555 | { |
| 1556 | return async_gen_asend_send(o, NULL); |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | static PyObject * |
| 1561 | async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args) |
| 1562 | { |
| 1563 | PyObject *result; |
| 1564 | |
| 1565 | if (o->ags_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1566 | PyErr_SetString( |
| 1567 | PyExc_RuntimeError, |
| 1568 | "cannot reuse already awaited __anext__()/asend()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1569 | return NULL; |
| 1570 | } |
| 1571 | |
| 1572 | result = gen_throw((PyGenObject*)o->ags_gen, args); |
| 1573 | result = async_gen_unwrap_value(o->ags_gen, result); |
| 1574 | |
| 1575 | if (result == NULL) { |
| 1576 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1577 | } |
| 1578 | |
| 1579 | return result; |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | static PyObject * |
| 1584 | async_gen_asend_close(PyAsyncGenASend *o, PyObject *args) |
| 1585 | { |
| 1586 | o->ags_state = AWAITABLE_STATE_CLOSED; |
| 1587 | Py_RETURN_NONE; |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | static PyMethodDef async_gen_asend_methods[] = { |
| 1592 | {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc}, |
| 1593 | {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc}, |
| 1594 | {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc}, |
| 1595 | {NULL, NULL} /* Sentinel */ |
| 1596 | }; |
| 1597 | |
| 1598 | |
| 1599 | static PyAsyncMethods async_gen_asend_as_async = { |
| 1600 | PyObject_SelfIter, /* am_await */ |
| 1601 | 0, /* am_aiter */ |
| 1602 | 0 /* am_anext */ |
| 1603 | }; |
| 1604 | |
| 1605 | |
| 1606 | PyTypeObject _PyAsyncGenASend_Type = { |
| 1607 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1608 | "async_generator_asend", /* tp_name */ |
| 1609 | sizeof(PyAsyncGenASend), /* tp_basicsize */ |
| 1610 | 0, /* tp_itemsize */ |
| 1611 | /* methods */ |
| 1612 | (destructor)async_gen_asend_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1613 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1614 | 0, /* tp_getattr */ |
| 1615 | 0, /* tp_setattr */ |
| 1616 | &async_gen_asend_as_async, /* tp_as_async */ |
| 1617 | 0, /* tp_repr */ |
| 1618 | 0, /* tp_as_number */ |
| 1619 | 0, /* tp_as_sequence */ |
| 1620 | 0, /* tp_as_mapping */ |
| 1621 | 0, /* tp_hash */ |
| 1622 | 0, /* tp_call */ |
| 1623 | 0, /* tp_str */ |
| 1624 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1625 | 0, /* tp_setattro */ |
| 1626 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1627 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1628 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1629 | (traverseproc)async_gen_asend_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1630 | 0, /* tp_clear */ |
| 1631 | 0, /* tp_richcompare */ |
| 1632 | 0, /* tp_weaklistoffset */ |
| 1633 | PyObject_SelfIter, /* tp_iter */ |
| 1634 | (iternextfunc)async_gen_asend_iternext, /* tp_iternext */ |
| 1635 | async_gen_asend_methods, /* tp_methods */ |
| 1636 | 0, /* tp_members */ |
| 1637 | 0, /* tp_getset */ |
| 1638 | 0, /* tp_base */ |
| 1639 | 0, /* tp_dict */ |
| 1640 | 0, /* tp_descr_get */ |
| 1641 | 0, /* tp_descr_set */ |
| 1642 | 0, /* tp_dictoffset */ |
| 1643 | 0, /* tp_init */ |
| 1644 | 0, /* tp_alloc */ |
| 1645 | 0, /* tp_new */ |
| 1646 | }; |
| 1647 | |
| 1648 | |
| 1649 | static PyObject * |
| 1650 | async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval) |
| 1651 | { |
| 1652 | PyAsyncGenASend *o; |
| 1653 | if (ag_asend_freelist_free) { |
| 1654 | ag_asend_freelist_free--; |
| 1655 | o = ag_asend_freelist[ag_asend_freelist_free]; |
| 1656 | _Py_NewReference((PyObject *)o); |
| 1657 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1658 | o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1659 | if (o == NULL) { |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | Py_INCREF(gen); |
| 1665 | o->ags_gen = gen; |
| 1666 | |
| 1667 | Py_XINCREF(sendval); |
| 1668 | o->ags_sendval = sendval; |
| 1669 | |
| 1670 | o->ags_state = AWAITABLE_STATE_INIT; |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1671 | |
| 1672 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1673 | return (PyObject*)o; |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | /* ---------- Async Generator Value Wrapper ------------ */ |
| 1678 | |
| 1679 | |
| 1680 | static void |
| 1681 | async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o) |
| 1682 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1683 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1684 | Py_CLEAR(o->agw_val); |
| 1685 | if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) { |
| 1686 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
| 1687 | ag_value_freelist[ag_value_freelist_free++] = o; |
| 1688 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1689 | PyObject_GC_Del(o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1694 | static int |
| 1695 | async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o, |
| 1696 | visitproc visit, void *arg) |
| 1697 | { |
| 1698 | Py_VISIT(o->agw_val); |
| 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1703 | PyTypeObject _PyAsyncGenWrappedValue_Type = { |
| 1704 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1705 | "async_generator_wrapped_value", /* tp_name */ |
| 1706 | sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */ |
| 1707 | 0, /* tp_itemsize */ |
| 1708 | /* methods */ |
| 1709 | (destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1710 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1711 | 0, /* tp_getattr */ |
| 1712 | 0, /* tp_setattr */ |
| 1713 | 0, /* tp_as_async */ |
| 1714 | 0, /* tp_repr */ |
| 1715 | 0, /* tp_as_number */ |
| 1716 | 0, /* tp_as_sequence */ |
| 1717 | 0, /* tp_as_mapping */ |
| 1718 | 0, /* tp_hash */ |
| 1719 | 0, /* tp_call */ |
| 1720 | 0, /* tp_str */ |
| 1721 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1722 | 0, /* tp_setattro */ |
| 1723 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1724 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1725 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1726 | (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1727 | 0, /* tp_clear */ |
| 1728 | 0, /* tp_richcompare */ |
| 1729 | 0, /* tp_weaklistoffset */ |
| 1730 | 0, /* tp_iter */ |
| 1731 | 0, /* tp_iternext */ |
| 1732 | 0, /* tp_methods */ |
| 1733 | 0, /* tp_members */ |
| 1734 | 0, /* tp_getset */ |
| 1735 | 0, /* tp_base */ |
| 1736 | 0, /* tp_dict */ |
| 1737 | 0, /* tp_descr_get */ |
| 1738 | 0, /* tp_descr_set */ |
| 1739 | 0, /* tp_dictoffset */ |
| 1740 | 0, /* tp_init */ |
| 1741 | 0, /* tp_alloc */ |
| 1742 | 0, /* tp_new */ |
| 1743 | }; |
| 1744 | |
| 1745 | |
| 1746 | PyObject * |
| 1747 | _PyAsyncGenValueWrapperNew(PyObject *val) |
| 1748 | { |
| 1749 | _PyAsyncGenWrappedValue *o; |
| 1750 | assert(val); |
| 1751 | |
| 1752 | if (ag_value_freelist_free) { |
| 1753 | ag_value_freelist_free--; |
| 1754 | o = ag_value_freelist[ag_value_freelist_free]; |
| 1755 | assert(_PyAsyncGenWrappedValue_CheckExact(o)); |
| 1756 | _Py_NewReference((PyObject*)o); |
| 1757 | } else { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1758 | o = PyObject_GC_New(_PyAsyncGenWrappedValue, |
| 1759 | &_PyAsyncGenWrappedValue_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1760 | if (o == NULL) { |
| 1761 | return NULL; |
| 1762 | } |
| 1763 | } |
| 1764 | o->agw_val = val; |
| 1765 | Py_INCREF(val); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1766 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1767 | return (PyObject*)o; |
| 1768 | } |
| 1769 | |
| 1770 | |
| 1771 | /* ---------- Async Generator AThrow awaitable ------------ */ |
| 1772 | |
| 1773 | |
| 1774 | static void |
| 1775 | async_gen_athrow_dealloc(PyAsyncGenAThrow *o) |
| 1776 | { |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1777 | _PyObject_GC_UNTRACK((PyObject *)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1778 | Py_CLEAR(o->agt_gen); |
| 1779 | Py_CLEAR(o->agt_args); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 1780 | PyObject_GC_Del(o); |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | static int |
| 1785 | async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg) |
| 1786 | { |
| 1787 | Py_VISIT(o->agt_gen); |
| 1788 | Py_VISIT(o->agt_args); |
| 1789 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | |
| 1793 | static PyObject * |
| 1794 | async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) |
| 1795 | { |
| 1796 | PyGenObject *gen = (PyGenObject*)o->agt_gen; |
| 1797 | PyFrameObject *f = gen->gi_frame; |
| 1798 | PyObject *retval; |
| 1799 | |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1800 | if (o->agt_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1801 | PyErr_SetString( |
| 1802 | PyExc_RuntimeError, |
| 1803 | "cannot reuse already awaited aclose()/athrow()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1804 | return NULL; |
| 1805 | } |
| 1806 | |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1807 | if (f == NULL || f->f_stacktop == NULL) { |
| 1808 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1809 | PyErr_SetNone(PyExc_StopIteration); |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1813 | if (o->agt_state == AWAITABLE_STATE_INIT) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1814 | if (o->agt_gen->ag_running_async) { |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1815 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1816 | if (o->agt_args == NULL) { |
| 1817 | PyErr_SetString( |
| 1818 | PyExc_RuntimeError, |
| 1819 | "aclose(): asynchronous generator is already running"); |
| 1820 | } |
| 1821 | else { |
| 1822 | PyErr_SetString( |
| 1823 | PyExc_RuntimeError, |
| 1824 | "athrow(): asynchronous generator is already running"); |
| 1825 | } |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1829 | if (o->agt_gen->ag_closed) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1830 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1831 | PyErr_SetNone(PyExc_StopAsyncIteration); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1832 | return NULL; |
| 1833 | } |
| 1834 | |
| 1835 | if (arg != Py_None) { |
| 1836 | PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG); |
| 1837 | return NULL; |
| 1838 | } |
| 1839 | |
| 1840 | o->agt_state = AWAITABLE_STATE_ITER; |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1841 | o->agt_gen->ag_running_async = 1; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1842 | |
| 1843 | if (o->agt_args == NULL) { |
| 1844 | /* aclose() mode */ |
| 1845 | o->agt_gen->ag_closed = 1; |
| 1846 | |
| 1847 | retval = _gen_throw((PyGenObject *)gen, |
| 1848 | 0, /* Do not close generator when |
| 1849 | PyExc_GeneratorExit is passed */ |
| 1850 | PyExc_GeneratorExit, NULL, NULL); |
| 1851 | |
| 1852 | if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) { |
| 1853 | Py_DECREF(retval); |
| 1854 | goto yield_close; |
| 1855 | } |
| 1856 | } else { |
| 1857 | PyObject *typ; |
| 1858 | PyObject *tb = NULL; |
| 1859 | PyObject *val = NULL; |
| 1860 | |
| 1861 | if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3, |
| 1862 | &typ, &val, &tb)) { |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | retval = _gen_throw((PyGenObject *)gen, |
| 1867 | 0, /* Do not close generator when |
| 1868 | PyExc_GeneratorExit is passed */ |
| 1869 | typ, val, tb); |
| 1870 | retval = async_gen_unwrap_value(o->agt_gen, retval); |
| 1871 | } |
| 1872 | if (retval == NULL) { |
| 1873 | goto check_error; |
| 1874 | } |
| 1875 | return retval; |
| 1876 | } |
| 1877 | |
| 1878 | assert(o->agt_state == AWAITABLE_STATE_ITER); |
| 1879 | |
| 1880 | retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0); |
| 1881 | if (o->agt_args) { |
| 1882 | return async_gen_unwrap_value(o->agt_gen, retval); |
| 1883 | } else { |
| 1884 | /* aclose() mode */ |
| 1885 | if (retval) { |
| 1886 | if (_PyAsyncGenWrappedValue_CheckExact(retval)) { |
| 1887 | Py_DECREF(retval); |
| 1888 | goto yield_close; |
| 1889 | } |
| 1890 | else { |
| 1891 | return retval; |
| 1892 | } |
| 1893 | } |
| 1894 | else { |
| 1895 | goto check_error; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | yield_close: |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1900 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1901 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1902 | PyErr_SetString( |
| 1903 | PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); |
| 1904 | return NULL; |
| 1905 | |
| 1906 | check_error: |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1907 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1908 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | 52698c7 | 2018-06-07 20:31:26 -0400 | [diff] [blame] | 1909 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) || |
| 1910 | PyErr_ExceptionMatches(PyExc_GeneratorExit)) |
| 1911 | { |
Yury Selivanov | 41782e4 | 2016-11-16 18:16:17 -0500 | [diff] [blame] | 1912 | if (o->agt_args == NULL) { |
| 1913 | /* when aclose() is called we don't want to propagate |
Yury Selivanov | 52698c7 | 2018-06-07 20:31:26 -0400 | [diff] [blame] | 1914 | StopAsyncIteration or GeneratorExit; just raise |
| 1915 | StopIteration, signalling that this 'aclose()' await |
| 1916 | is done. |
| 1917 | */ |
Yury Selivanov | 41782e4 | 2016-11-16 18:16:17 -0500 | [diff] [blame] | 1918 | PyErr_Clear(); |
| 1919 | PyErr_SetNone(PyExc_StopIteration); |
| 1920 | } |
| 1921 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1922 | return NULL; |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | static PyObject * |
| 1927 | async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args) |
| 1928 | { |
| 1929 | PyObject *retval; |
| 1930 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1931 | if (o->agt_state == AWAITABLE_STATE_CLOSED) { |
Andrew Svetlov | a96e06d | 2020-01-21 00:49:30 +0200 | [diff] [blame] | 1932 | PyErr_SetString( |
| 1933 | PyExc_RuntimeError, |
| 1934 | "cannot reuse already awaited aclose()/athrow()"); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | retval = gen_throw((PyGenObject*)o->agt_gen, args); |
| 1939 | if (o->agt_args) { |
| 1940 | return async_gen_unwrap_value(o->agt_gen, retval); |
| 1941 | } else { |
| 1942 | /* aclose() mode */ |
| 1943 | if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) { |
Yury Selivanov | fc4a044 | 2019-09-29 22:59:11 -0700 | [diff] [blame] | 1944 | o->agt_gen->ag_running_async = 0; |
Nathaniel J. Smith | 925dc7f | 2020-02-13 00:15:38 -0800 | [diff] [blame^] | 1945 | o->agt_state = AWAITABLE_STATE_CLOSED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1946 | Py_DECREF(retval); |
| 1947 | PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); |
| 1948 | return NULL; |
| 1949 | } |
Vincent Michel | 8e0de2a | 2019-11-19 05:53:52 -0800 | [diff] [blame] | 1950 | if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) || |
| 1951 | PyErr_ExceptionMatches(PyExc_GeneratorExit)) |
| 1952 | { |
| 1953 | /* when aclose() is called we don't want to propagate |
| 1954 | StopAsyncIteration or GeneratorExit; just raise |
| 1955 | StopIteration, signalling that this 'aclose()' await |
| 1956 | is done. |
| 1957 | */ |
| 1958 | PyErr_Clear(); |
| 1959 | PyErr_SetNone(PyExc_StopIteration); |
| 1960 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1961 | return retval; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | static PyObject * |
| 1967 | async_gen_athrow_iternext(PyAsyncGenAThrow *o) |
| 1968 | { |
| 1969 | return async_gen_athrow_send(o, Py_None); |
| 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | static PyObject * |
| 1974 | async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args) |
| 1975 | { |
| 1976 | o->agt_state = AWAITABLE_STATE_CLOSED; |
| 1977 | Py_RETURN_NONE; |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | static PyMethodDef async_gen_athrow_methods[] = { |
| 1982 | {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc}, |
| 1983 | {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc}, |
| 1984 | {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc}, |
| 1985 | {NULL, NULL} /* Sentinel */ |
| 1986 | }; |
| 1987 | |
| 1988 | |
| 1989 | static PyAsyncMethods async_gen_athrow_as_async = { |
| 1990 | PyObject_SelfIter, /* am_await */ |
| 1991 | 0, /* am_aiter */ |
| 1992 | 0 /* am_anext */ |
| 1993 | }; |
| 1994 | |
| 1995 | |
| 1996 | PyTypeObject _PyAsyncGenAThrow_Type = { |
| 1997 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1998 | "async_generator_athrow", /* tp_name */ |
| 1999 | sizeof(PyAsyncGenAThrow), /* tp_basicsize */ |
| 2000 | 0, /* tp_itemsize */ |
| 2001 | /* methods */ |
| 2002 | (destructor)async_gen_athrow_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2003 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2004 | 0, /* tp_getattr */ |
| 2005 | 0, /* tp_setattr */ |
| 2006 | &async_gen_athrow_as_async, /* tp_as_async */ |
| 2007 | 0, /* tp_repr */ |
| 2008 | 0, /* tp_as_number */ |
| 2009 | 0, /* tp_as_sequence */ |
| 2010 | 0, /* tp_as_mapping */ |
| 2011 | 0, /* tp_hash */ |
| 2012 | 0, /* tp_call */ |
| 2013 | 0, /* tp_str */ |
| 2014 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2015 | 0, /* tp_setattro */ |
| 2016 | 0, /* tp_as_buffer */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2017 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2018 | 0, /* tp_doc */ |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2019 | (traverseproc)async_gen_athrow_traverse, /* tp_traverse */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2020 | 0, /* tp_clear */ |
| 2021 | 0, /* tp_richcompare */ |
| 2022 | 0, /* tp_weaklistoffset */ |
| 2023 | PyObject_SelfIter, /* tp_iter */ |
| 2024 | (iternextfunc)async_gen_athrow_iternext, /* tp_iternext */ |
| 2025 | async_gen_athrow_methods, /* tp_methods */ |
| 2026 | 0, /* tp_members */ |
| 2027 | 0, /* tp_getset */ |
| 2028 | 0, /* tp_base */ |
| 2029 | 0, /* tp_dict */ |
| 2030 | 0, /* tp_descr_get */ |
| 2031 | 0, /* tp_descr_set */ |
| 2032 | 0, /* tp_dictoffset */ |
| 2033 | 0, /* tp_init */ |
| 2034 | 0, /* tp_alloc */ |
| 2035 | 0, /* tp_new */ |
| 2036 | }; |
| 2037 | |
| 2038 | |
| 2039 | static PyObject * |
| 2040 | async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args) |
| 2041 | { |
| 2042 | PyAsyncGenAThrow *o; |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2043 | o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2044 | if (o == NULL) { |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | o->agt_gen = gen; |
| 2048 | o->agt_args = args; |
| 2049 | o->agt_state = AWAITABLE_STATE_INIT; |
| 2050 | Py_INCREF(gen); |
| 2051 | Py_XINCREF(args); |
Yury Selivanov | 29310c4 | 2016-11-08 19:46:22 -0500 | [diff] [blame] | 2052 | _PyObject_GC_TRACK((PyObject*)o); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2053 | return (PyObject*)o; |
| 2054 | } |