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