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