Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9 | /* enable more aggressive intra-module optimizations, where available */ |
| 10 | #define PY_LOCAL_AGGRESSIVE |
| 11 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 13 | #include "pycore_object.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 14 | #include "pycore_pystate.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 15 | #include "pycore_tupleobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 17 | #include "code.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 18 | #include "dictobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | #include "frameobject.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 20 | #include "opcode.h" |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 21 | #include "pydtrace.h" |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 22 | #include "setobject.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 23 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
| 26 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 27 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 28 | /* For debugging the interpreter: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | #define LLTRACE 1 /* Low-level trace feature */ |
| 30 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 33 | /* Private API for the LOAD_METHOD opcode. */ |
| 34 | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **); |
| 35 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 36 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 38 | /* Forward declarations */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 39 | Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, |
| 40 | PyObject *); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 41 | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 43 | #ifdef LLTRACE |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 44 | static int lltrace; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 45 | static int prtrace(PyObject *, const char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 46 | #endif |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 47 | static int call_trace(Py_tracefunc, PyObject *, |
| 48 | PyThreadState *, PyFrameObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | int, PyObject *); |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 50 | static int call_trace_protected(Py_tracefunc, PyObject *, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 51 | PyThreadState *, PyFrameObject *, |
| 52 | int, PyObject *); |
| 53 | static void call_exc_trace(Py_tracefunc, PyObject *, |
| 54 | PyThreadState *, PyFrameObject *); |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 55 | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 56 | PyThreadState *, PyFrameObject *, |
| 57 | int *, int *, int *); |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 58 | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
| 59 | static void dtrace_function_entry(PyFrameObject *); |
| 60 | static void dtrace_function_return(PyFrameObject *); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 63 | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, |
| 64 | PyObject *); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 65 | static PyObject * import_from(PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 66 | static int import_all_from(PyObject *, PyObject *); |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 67 | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 68 | static void format_exc_unbound(PyCodeObject *co, int oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 69 | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 70 | PyFrameObject *, const _Py_CODEUNIT *); |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 71 | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 72 | static int check_args_iterable(PyObject *func, PyObject *vararg); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 73 | static void format_kwargs_error(PyObject *func, PyObject *kwargs); |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 74 | static void format_awaitable_error(PyTypeObject *, int); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 75 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 76 | #define NAME_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | "name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 78 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 80 | #define UNBOUNDFREE_ERROR_MSG \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | "free variable '%.200s' referenced before assignment" \ |
| 82 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 84 | /* Dynamic execution profile */ |
| 85 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 86 | #ifdef DXPAIRS |
| 87 | static long dxpairs[257][256]; |
| 88 | #define dxp dxpairs[256] |
| 89 | #else |
| 90 | static long dxp[256]; |
| 91 | #endif |
| 92 | #endif |
| 93 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 94 | #define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request) |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 95 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 96 | /* This can set eval_breaker to 0 even though gil_drop_request became |
| 97 | 1. We believe this is all right because the eval loop will release |
| 98 | the GIL eventually anyway. */ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 99 | #define COMPUTE_EVAL_BREAKER() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | _Py_atomic_store_relaxed( \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 101 | &_PyRuntime.ceval.eval_breaker, \ |
Benjamin Peterson | d2be5b4 | 2010-09-10 22:47:02 +0000 | [diff] [blame] | 102 | GIL_REQUEST | \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 103 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 104 | _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \ |
| 105 | _PyRuntime.ceval.pending.async_exc) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 106 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 107 | #define SET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 109 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 110 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 112 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 113 | #define RESET_GIL_DROP_REQUEST() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | do { \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 115 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 116 | COMPUTE_EVAL_BREAKER(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 118 | |
Jeffrey Yasskin | 3937083 | 2010-05-03 19:29:34 +0000 | [diff] [blame] | 119 | /* Pending calls are only modified under pending_lock */ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 120 | #define SIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | do { \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 122 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \ |
| 123 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 125 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 126 | #define UNSIGNAL_PENDING_CALLS() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | do { \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 128 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \ |
| 129 | COMPUTE_EVAL_BREAKER(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 131 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 132 | #define SIGNAL_PENDING_SIGNALS() \ |
| 133 | do { \ |
| 134 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 135 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 136 | } while (0) |
| 137 | |
| 138 | #define UNSIGNAL_PENDING_SIGNALS() \ |
| 139 | do { \ |
| 140 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 141 | COMPUTE_EVAL_BREAKER(); \ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 142 | } while (0) |
| 143 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 144 | #define SIGNAL_ASYNC_EXC() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | do { \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 146 | _PyRuntime.ceval.pending.async_exc = 1; \ |
| 147 | _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 149 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 150 | #define UNSIGNAL_ASYNC_EXC() \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 151 | do { \ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 152 | _PyRuntime.ceval.pending.async_exc = 0; \ |
| 153 | COMPUTE_EVAL_BREAKER(); \ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 154 | } while (0) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 155 | |
| 156 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 157 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 158 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 159 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 160 | #include "pythread.h" |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 161 | #include "ceval_gil.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 162 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 163 | int |
| 164 | PyEval_ThreadsInitialized(void) |
| 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | return gil_created(); |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 169 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 170 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (gil_created()) |
| 173 | return; |
Inada Naoki | 001fee1 | 2019-02-20 10:00:09 +0900 | [diff] [blame] | 174 | PyThread_init_thread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | create_gil(); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 176 | take_gil(_PyThreadState_GET()); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 177 | |
| 178 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
| 179 | if (_PyRuntime.ceval.pending.lock == NULL) { |
| 180 | return Py_FatalError("Can't initialize threads for pending calls"); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 181 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 184 | void |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 185 | _PyEval_FiniThreads(void) |
| 186 | { |
| 187 | if (!gil_created()) |
| 188 | return; |
| 189 | destroy_gil(); |
| 190 | assert(!gil_created()); |
| 191 | } |
| 192 | |
| 193 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 194 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 195 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 196 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | if (tstate == NULL) |
| 198 | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
| 199 | take_gil(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 203 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | /* This function must succeed when the current thread state is NULL. |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 206 | We therefore avoid PyThreadState_Get() which dumps a fatal error |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | in debug mode. |
| 208 | */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 209 | drop_gil(_PyThreadState_GET()); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 213 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | if (tstate == NULL) |
| 216 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
| 217 | /* Check someone has called PyEval_InitThreads() to create the lock */ |
| 218 | assert(gil_created()); |
| 219 | take_gil(tstate); |
| 220 | if (PyThreadState_Swap(tstate) != NULL) |
| 221 | Py_FatalError( |
| 222 | "PyEval_AcquireThread: non-NULL old thread state"); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 226 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | if (tstate == NULL) |
| 229 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 230 | if (PyThreadState_Swap(NULL) != tstate) |
| 231 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
| 232 | drop_gil(tstate); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 233 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 235 | /* This function is called from PyOS_AfterFork_Child to destroy all threads |
| 236 | * which are not running in the child process, and clear internal locks |
| 237 | * which might be held by those threads. |
| 238 | */ |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 239 | |
| 240 | void |
| 241 | PyEval_ReInitThreads(void) |
| 242 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 243 | PyThreadState *current_tstate = _PyThreadState_GET(); |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | if (!gil_created()) |
| 246 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | recreate_gil(); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 248 | take_gil(current_tstate); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 249 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 250 | _PyRuntime.ceval.pending.lock = PyThread_allocate_lock(); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 251 | if (_PyRuntime.ceval.pending.lock == NULL) { |
| 252 | Py_FatalError("Can't initialize threads for pending calls"); |
| 253 | } |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 255 | /* Destroy all threads except the current one */ |
| 256 | _PyThreadState_DeleteExcept(current_tstate); |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 257 | } |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 259 | /* This function is used to signal that async exceptions are waiting to be |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 260 | raised. */ |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 261 | |
| 262 | void |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 263 | _PyEval_SignalAsyncExc(void) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 264 | { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 265 | SIGNAL_ASYNC_EXC(); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 266 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 268 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 269 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 272 | if (tstate == NULL) |
| 273 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 274 | assert(gil_created()); |
| 275 | drop_gil(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 280 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | if (tstate == NULL) |
| 283 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 284 | assert(gil_created()); |
| 285 | |
| 286 | int err = errno; |
| 287 | take_gil(tstate); |
| 288 | /* _Py_Finalizing is protected by the GIL */ |
| 289 | if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) { |
| 290 | drop_gil(tstate); |
| 291 | PyThread_exit_thread(); |
| 292 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | } |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 294 | errno = err; |
| 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 300 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 301 | signal handlers or Mac I/O completion routines) can schedule calls |
| 302 | to a function to be called synchronously. |
| 303 | The synchronous function is called with one void* argument. |
| 304 | It should return 0 for success or -1 for failure -- failure should |
| 305 | be accompanied by an exception. |
| 306 | |
| 307 | If registry succeeds, the registry function returns 0; if it fails |
| 308 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 309 | an exception condition). |
| 310 | |
| 311 | Note that because registry may occur from within signal handlers, |
| 312 | or other asynchronous events, calling malloc() is unsafe! |
| 313 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 314 | Any thread can schedule pending calls, but only the main thread |
| 315 | will execute them. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 316 | There is no facility to schedule calls to a particular thread, but |
| 317 | that should be easy to change, should that ever be required. In |
| 318 | that case, the static variables here should go into the python |
| 319 | threadstate. |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 320 | */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 322 | void |
| 323 | _PyEval_SignalReceived(void) |
| 324 | { |
| 325 | /* bpo-30703: Function called when the C signal handler of Python gets a |
| 326 | signal. We cannot queue a callback using Py_AddPendingCall() since |
| 327 | that function is not async-signal-safe. */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 328 | SIGNAL_PENDING_SIGNALS(); |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 331 | /* Push one item onto the queue while holding the lock. */ |
| 332 | static int |
| 333 | _push_pending_call(int (*func)(void *), void *arg) |
| 334 | { |
| 335 | int i = _PyRuntime.ceval.pending.last; |
| 336 | int j = (i + 1) % NPENDINGCALLS; |
| 337 | if (j == _PyRuntime.ceval.pending.first) { |
| 338 | return -1; /* Queue full */ |
| 339 | } |
| 340 | _PyRuntime.ceval.pending.calls[i].func = func; |
| 341 | _PyRuntime.ceval.pending.calls[i].arg = arg; |
| 342 | _PyRuntime.ceval.pending.last = j; |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* Pop one item off the queue while holding the lock. */ |
| 347 | static void |
| 348 | _pop_pending_call(int (**func)(void *), void **arg) |
| 349 | { |
| 350 | int i = _PyRuntime.ceval.pending.first; |
| 351 | if (i == _PyRuntime.ceval.pending.last) { |
| 352 | return; /* Queue empty */ |
| 353 | } |
| 354 | |
| 355 | *func = _PyRuntime.ceval.pending.calls[i].func; |
| 356 | *arg = _PyRuntime.ceval.pending.calls[i].arg; |
| 357 | _PyRuntime.ceval.pending.first = (i + 1) % NPENDINGCALLS; |
| 358 | } |
| 359 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 360 | /* This implementation is thread-safe. It allows |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 361 | scheduling to be made from any thread, and even from an executing |
| 362 | callback. |
| 363 | */ |
| 364 | |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 365 | int |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 366 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 367 | { |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 368 | PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 369 | int result = _push_pending_call(func, arg); |
Eric Snow | 8479a34 | 2019-03-08 23:44:33 -0700 | [diff] [blame] | 370 | PyThread_release_lock(_PyRuntime.ceval.pending.lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | /* signal main loop */ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 373 | SIGNAL_PENDING_CALLS(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | return result; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 377 | static int |
| 378 | handle_signals(void) |
| 379 | { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 380 | /* Only handle signals on main thread. PyEval_InitThreads must |
| 381 | * have been called already. |
| 382 | */ |
| 383 | if (PyThread_get_thread_ident() != _PyRuntime.main_thread) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | } |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Ensure that the thread isn't currently running some other |
| 388 | * interpreter. |
| 389 | */ |
| 390 | if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) { |
| 391 | return 0; |
| 392 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 393 | |
| 394 | UNSIGNAL_PENDING_SIGNALS(); |
Eric Snow | 64d6cc8 | 2019-02-23 15:40:43 -0700 | [diff] [blame] | 395 | if (_PyErr_CheckSignals() < 0) { |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 396 | SIGNAL_PENDING_SIGNALS(); /* We're not done yet */ |
| 397 | return -1; |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 403 | make_pending_calls(void) |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 404 | { |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 405 | static int busy = 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 406 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 407 | /* only service pending calls on main thread */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 408 | if (PyThread_get_thread_ident() != _PyRuntime.main_thread) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | /* don't perform recursive pending calls */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 413 | if (busy) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | return 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 415 | } |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 416 | busy = 1; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 417 | /* unsignal before starting to call callbacks, so that any callback |
| 418 | added in-between re-signals */ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 419 | UNSIGNAL_PENDING_CALLS(); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 420 | int res = 0; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | /* perform a bounded number of calls, in case of recursion */ |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 423 | for (int i=0; i<NPENDINGCALLS; i++) { |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 424 | int (*func)(void *) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | void *arg = NULL; |
| 426 | |
| 427 | /* pop one item off the queue while holding the lock */ |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 428 | PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 429 | _pop_pending_call(&func, &arg); |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 430 | PyThread_release_lock(_PyRuntime.ceval.pending.lock); |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 431 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 432 | /* having released the lock, perform the callback */ |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 433 | if (func == NULL) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 434 | break; |
Eric Snow | 5be45a6 | 2019-03-08 22:47:07 -0700 | [diff] [blame] | 435 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 436 | res = func(arg); |
| 437 | if (res) { |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 438 | goto error; |
| 439 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | } |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 441 | |
Charles-François Natali | f23339a | 2011-07-23 18:15:43 +0200 | [diff] [blame] | 442 | busy = 0; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 443 | return res; |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 444 | |
| 445 | error: |
| 446 | busy = 0; |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 447 | SIGNAL_PENDING_CALLS(); |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 448 | return res; |
| 449 | } |
| 450 | |
| 451 | /* Py_MakePendingCalls() is a simple wrapper for the sake |
| 452 | of backward-compatibility. */ |
| 453 | int |
| 454 | Py_MakePendingCalls(void) |
| 455 | { |
| 456 | assert(PyGILState_Check()); |
| 457 | |
| 458 | /* Python signal handler doesn't really queue a callback: it only signals |
| 459 | that a signal was received, see _PyEval_SignalReceived(). */ |
| 460 | int res = handle_signals(); |
| 461 | if (res != 0) { |
| 462 | return res; |
| 463 | } |
| 464 | |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 465 | res = make_pending_calls(); |
| 466 | if (res != 0) { |
| 467 | return res; |
| 468 | } |
| 469 | |
| 470 | return 0; |
Benjamin Peterson | e5bf383 | 2009-01-17 23:43:58 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 473 | /* The interpreter's recursion limit */ |
| 474 | |
Hye-Shik Chang | b6fa281 | 2005-04-04 15:49:02 +0000 | [diff] [blame] | 475 | #ifndef Py_DEFAULT_RECURSION_LIMIT |
| 476 | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 477 | #endif |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 478 | |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 479 | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 480 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 481 | void |
| 482 | _PyEval_Initialize(struct _ceval_runtime_state *state) |
| 483 | { |
| 484 | state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
| 485 | _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
| 486 | _gil_initialize(&state->gil); |
| 487 | } |
| 488 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 489 | int |
| 490 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 491 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 492 | return _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 495 | void |
| 496 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 497 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 498 | _PyRuntime.ceval.recursion_limit = new_limit; |
| 499 | _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 502 | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
| 503 | if the recursion_depth reaches _Py_CheckRecursionLimit. |
| 504 | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
| 505 | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
| 506 | Without USE_STACKCHECK, there is no need for this. */ |
| 507 | int |
Serhiy Storchaka | 5fa22fc | 2015-06-21 16:26:28 +0300 | [diff] [blame] | 508 | _Py_CheckRecursiveCall(const char *where) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 509 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 510 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 511 | int recursion_limit = _PyRuntime.ceval.recursion_limit; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 512 | |
| 513 | #ifdef USE_STACKCHECK |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 514 | tstate->stackcheck_counter = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | if (PyOS_CheckStack()) { |
| 516 | --tstate->recursion_depth; |
| 517 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 518 | return -1; |
| 519 | } |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 520 | /* Needed for ABI backwards-compatibility (see bpo-31857) */ |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 521 | _Py_CheckRecursionLimit = recursion_limit; |
pdox | 1896793 | 2017-10-25 23:03:01 -0700 | [diff] [blame] | 522 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | if (tstate->recursion_critical) |
| 524 | /* Somebody asked that we don't check for recursion. */ |
| 525 | return 0; |
| 526 | if (tstate->overflowed) { |
| 527 | if (tstate->recursion_depth > recursion_limit + 50) { |
| 528 | /* Overflowing while handling an overflow. Give up. */ |
| 529 | Py_FatalError("Cannot recover from stack overflow."); |
| 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | if (tstate->recursion_depth > recursion_limit) { |
| 534 | --tstate->recursion_depth; |
| 535 | tstate->overflowed = 1; |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 536 | PyErr_Format(PyExc_RecursionError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | "maximum recursion depth exceeded%s", |
| 538 | where); |
| 539 | return -1; |
| 540 | } |
| 541 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 544 | static int do_raise(PyObject *, PyObject *); |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 545 | static int unpack_iterable(PyObject *, int, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 546 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 547 | #define _Py_TracingPossible _PyRuntime.ceval.tracing_possible |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 550 | PyObject * |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 551 | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return PyEval_EvalCodeEx(co, |
| 554 | globals, locals, |
| 555 | (PyObject **)NULL, 0, |
| 556 | (PyObject **)NULL, 0, |
| 557 | (PyObject **)NULL, 0, |
| 558 | NULL, NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | /* Interpreter main loop */ |
| 563 | |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 564 | PyObject * |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 565 | PyEval_EvalFrame(PyFrameObject *f) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | /* This is for backward compatibility with extension modules that |
| 567 | used this API; core interpreter code should call |
| 568 | PyEval_EvalFrameEx() */ |
| 569 | return PyEval_EvalFrameEx(f, 0); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 573 | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 574 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 575 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 576 | return interp->eval_frame(f, throwflag); |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 579 | PyObject* _Py_HOT_FUNCTION |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 580 | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
| 581 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 582 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | int lastopcode = 0; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 584 | #endif |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 585 | PyObject **stack_pointer; /* Next free slot in value stack */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 586 | const _Py_CODEUNIT *next_instr; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 587 | int opcode; /* Current opcode */ |
| 588 | int oparg; /* Current opcode argument, if any */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 589 | PyObject **fastlocals, **freevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | PyObject *retval = NULL; /* Return value */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 591 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 592 | _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | PyCodeObject *co; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | /* when tracing we set things up so that |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | not (instr_lb <= current_bytecode_offset < instr_ub) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | is true when the line being executed has changed. The |
| 600 | initial values are such as to make this false the first |
| 601 | time it is tested. */ |
| 602 | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 603 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 604 | const _Py_CODEUNIT *first_instr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | PyObject *names; |
| 606 | PyObject *consts; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 607 | |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 608 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 609 | _Py_IDENTIFIER(__ltrace__); |
Brett Cannon | 368b4b7 | 2012-04-02 12:17:59 -0400 | [diff] [blame] | 610 | #endif |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 611 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 612 | /* Computed GOTOs, or |
| 613 | the-optimization-commonly-but-improperly-known-as-"threaded code" |
| 614 | using gcc's labels-as-values extension |
| 615 | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
| 616 | |
| 617 | The traditional bytecode evaluation loop uses a "switch" statement, which |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | decent compilers will optimize as a single indirect branch instruction |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 619 | combined with a lookup table of jump addresses. However, since the |
| 620 | indirect jump instruction is shared by all opcodes, the CPU will have a |
| 621 | hard time making the right prediction for where to jump next (actually, |
| 622 | it will be always wrong except in the uncommon case of a sequence of |
| 623 | several identical opcodes). |
| 624 | |
| 625 | "Threaded code" in contrast, uses an explicit jump table and an explicit |
| 626 | indirect jump instruction at the end of each opcode. Since the jump |
| 627 | instruction is at a different address for each opcode, the CPU will make a |
| 628 | separate prediction for each of these instructions, which is equivalent to |
| 629 | predicting the second opcode of each opcode pair. These predictions have |
| 630 | a much better chance to turn out valid, especially in small bytecode loops. |
| 631 | |
| 632 | A mispredicted branch on a modern CPU flushes the whole pipeline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | can cost several CPU cycles (depending on the pipeline depth), |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 634 | and potentially many more instructions (depending on the pipeline width). |
| 635 | A correctly predicted branch, however, is nearly free. |
| 636 | |
| 637 | At the time of this writing, the "threaded code" version is up to 15-20% |
| 638 | faster than the normal "switch" version, depending on the compiler and the |
| 639 | CPU architecture. |
| 640 | |
| 641 | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
| 642 | because it would render the measurements invalid. |
| 643 | |
| 644 | |
| 645 | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
| 646 | indirect jumps by sharing them between all opcodes. Such optimizations |
| 647 | can be disabled on gcc by using the -fno-gcse flag (or possibly |
| 648 | -fno-crossjumping). |
| 649 | */ |
| 650 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 651 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 652 | #undef USE_COMPUTED_GOTOS |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 653 | #define USE_COMPUTED_GOTOS 0 |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 654 | #endif |
| 655 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 656 | #ifdef HAVE_COMPUTED_GOTOS |
| 657 | #ifndef USE_COMPUTED_GOTOS |
| 658 | #define USE_COMPUTED_GOTOS 1 |
| 659 | #endif |
| 660 | #else |
| 661 | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 662 | #error "Computed gotos are not supported on this compiler." |
| 663 | #endif |
| 664 | #undef USE_COMPUTED_GOTOS |
| 665 | #define USE_COMPUTED_GOTOS 0 |
| 666 | #endif |
| 667 | |
| 668 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 669 | /* Import the static jump table */ |
| 670 | #include "opcode_targets.h" |
| 671 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 672 | #define TARGET(op) \ |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 673 | op: \ |
| 674 | TARGET_##op |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 676 | #define DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | { \ |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 678 | if (!_Py_atomic_load_relaxed(eval_breaker)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | FAST_DISPATCH(); \ |
| 680 | } \ |
| 681 | continue; \ |
| 682 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 683 | |
| 684 | #ifdef LLTRACE |
| 685 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 687 | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 689 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 690 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | } \ |
| 692 | goto fast_next_opcode; \ |
| 693 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 694 | #else |
| 695 | #define FAST_DISPATCH() \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | { \ |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 697 | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | f->f_lasti = INSTR_OFFSET(); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 699 | NEXTOPARG(); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 700 | goto *opcode_targets[opcode]; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | } \ |
| 702 | goto fast_next_opcode; \ |
| 703 | } |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 704 | #endif |
| 705 | |
| 706 | #else |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 707 | #define TARGET(op) op |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 708 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 709 | #define DISPATCH() continue |
| 710 | #define FAST_DISPATCH() goto fast_next_opcode |
| 711 | #endif |
| 712 | |
| 713 | |
Neal Norwitz | a81d220 | 2002-07-14 00:27:26 +0000 | [diff] [blame] | 714 | /* Tuple access macros */ |
| 715 | |
| 716 | #ifndef Py_DEBUG |
| 717 | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
| 718 | #else |
| 719 | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
| 720 | #endif |
| 721 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 722 | /* Code access macros */ |
| 723 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 724 | /* The integer overflow is checked by an assertion below. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 725 | #define INSTR_OFFSET() \ |
| 726 | (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 727 | #define NEXTOPARG() do { \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 728 | _Py_CODEUNIT word = *next_instr; \ |
| 729 | opcode = _Py_OPCODE(word); \ |
| 730 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 731 | next_instr++; \ |
| 732 | } while (0) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 733 | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
| 734 | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 735 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 736 | /* OpCode prediction macros |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | Some opcodes tend to come in pairs thus making it possible to |
| 738 | predict the second code when the first is run. For example, |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 739 | COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | Verifying the prediction costs a single high-speed test of a register |
| 742 | variable against a constant. If the pairing was good, then the |
| 743 | processor's own internal branch predication has a high likelihood of |
| 744 | success, resulting in a nearly zero-overhead transition to the |
| 745 | next opcode. A successful prediction saves a trip through the eval-loop |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 746 | including its unpredictable switch-case branch. Combined with the |
| 747 | processor's internal branch prediction, a successful PREDICT has the |
| 748 | effect of making the two opcodes run as if they were a single new opcode |
| 749 | with the bodies combined. |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 750 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 751 | If collecting opcode statistics, your choices are to either keep the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | predictions turned-on and interpret the results as if some opcodes |
| 753 | had been combined or turn-off predictions so that the opcode frequency |
| 754 | counter updates for both opcodes. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 755 | |
| 756 | Opcode prediction is disabled with threaded code, since the latter allows |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | the CPU to record separate branch prediction information for each |
| 758 | opcode. |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 759 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 760 | */ |
| 761 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 762 | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | #define PREDICT(op) if (0) goto PRED_##op |
Raymond Hettinger | a721698 | 2004-02-08 19:59:27 +0000 | [diff] [blame] | 764 | #else |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 765 | #define PREDICT(op) \ |
| 766 | do{ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 767 | _Py_CODEUNIT word = *next_instr; \ |
| 768 | opcode = _Py_OPCODE(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 769 | if (opcode == op){ \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 770 | oparg = _Py_OPARG(word); \ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 771 | next_instr++; \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 772 | goto PRED_##op; \ |
| 773 | } \ |
| 774 | } while(0) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 775 | #endif |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 776 | #define PREDICTED(op) PRED_##op: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 777 | |
Raymond Hettinger | f606f87 | 2003-03-16 03:11:04 +0000 | [diff] [blame] | 778 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 779 | /* Stack manipulation macros */ |
| 780 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 781 | /* The stack can grow at most MAXINT deep, as co_nlocals and |
| 782 | co_stacksize are ints. */ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 783 | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
| 784 | #define EMPTY() (STACK_LEVEL() == 0) |
| 785 | #define TOP() (stack_pointer[-1]) |
| 786 | #define SECOND() (stack_pointer[-2]) |
| 787 | #define THIRD() (stack_pointer[-3]) |
| 788 | #define FOURTH() (stack_pointer[-4]) |
| 789 | #define PEEK(n) (stack_pointer[-(n)]) |
| 790 | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
| 791 | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
| 792 | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
| 793 | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
| 794 | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
| 795 | #define BASIC_STACKADJ(n) (stack_pointer += n) |
| 796 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 797 | #define BASIC_POP() (*--stack_pointer) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 799 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 801 | lltrace && prtrace(TOP(), "push")); \ |
| 802 | assert(STACK_LEVEL() <= co->co_stacksize); } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 804 | BASIC_POP()) |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 805 | #define STACK_GROW(n) do { \ |
| 806 | assert(n >= 0); \ |
| 807 | (void)(BASIC_STACKADJ(n), \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 808 | lltrace && prtrace(TOP(), "stackadj")); \ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 809 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 810 | } while (0) |
| 811 | #define STACK_SHRINK(n) do { \ |
| 812 | assert(n >= 0); \ |
| 813 | (void)(lltrace && prtrace(TOP(), "stackadj")); \ |
| 814 | (void)(BASIC_STACKADJ(-n)); \ |
| 815 | assert(STACK_LEVEL() <= co->co_stacksize); \ |
| 816 | } while (0) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 817 | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 818 | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
| 819 | *--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 820 | #else |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 821 | #define PUSH(v) BASIC_PUSH(v) |
| 822 | #define POP() BASIC_POP() |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 823 | #define STACK_GROW(n) BASIC_STACKADJ(n) |
| 824 | #define STACK_SHRINK(n) BASIC_STACKADJ(-n) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 825 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 826 | #endif |
| 827 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 828 | /* Local variable macros */ |
| 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | cfbf1a3 | 2002-03-28 20:17:52 +0000 | [diff] [blame] | 831 | |
| 832 | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
| 833 | then store the new value; it must copy the old value to a temporary |
| 834 | value, then store the new value, and then DECREF the temporary value. |
| 835 | This is because it is possible that during the DECREF the frame is |
| 836 | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
| 837 | variable would be pointing to already-freed memory. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 839 | GETLOCAL(i) = value; \ |
| 840 | Py_XDECREF(tmp); } while (0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 841 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 842 | |
| 843 | #define UNWIND_BLOCK(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | while (STACK_LEVEL() > (b)->b_level) { \ |
| 845 | PyObject *v = POP(); \ |
| 846 | Py_XDECREF(v); \ |
| 847 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 848 | |
| 849 | #define UNWIND_EXCEPT_HANDLER(b) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 850 | do { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | PyObject *type, *value, *traceback; \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 852 | _PyErr_StackItem *exc_info; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
| 854 | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
| 855 | value = POP(); \ |
| 856 | Py_XDECREF(value); \ |
| 857 | } \ |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 858 | exc_info = tstate->exc_info; \ |
| 859 | type = exc_info->exc_type; \ |
| 860 | value = exc_info->exc_value; \ |
| 861 | traceback = exc_info->exc_traceback; \ |
| 862 | exc_info->exc_type = POP(); \ |
| 863 | exc_info->exc_value = POP(); \ |
| 864 | exc_info->exc_traceback = POP(); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | Py_XDECREF(type); \ |
| 866 | Py_XDECREF(value); \ |
| 867 | Py_XDECREF(traceback); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 868 | } while(0) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 869 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 870 | /* Start of code */ |
| 871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | /* push frame */ |
| 873 | if (Py_EnterRecursiveCall("")) |
| 874 | return NULL; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | if (tstate->use_tracing) { |
| 879 | if (tstate->c_tracefunc != NULL) { |
| 880 | /* tstate->c_tracefunc, if defined, is a |
| 881 | function that will be called on *every* entry |
| 882 | to a code block. Its return value, if not |
| 883 | None, is a function that will be called at |
| 884 | the start of each executed line of code. |
| 885 | (Actually, the function must return itself |
| 886 | in order to continue tracing.) The trace |
| 887 | functions are called with three arguments: |
| 888 | a pointer to the current frame, a string |
| 889 | indicating why the function is called, and |
| 890 | an argument which depends on the situation. |
| 891 | The global trace function is also called |
| 892 | whenever an exception is detected. */ |
| 893 | if (call_trace_protected(tstate->c_tracefunc, |
| 894 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 895 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | /* Trace function raised an error */ |
| 897 | goto exit_eval_frame; |
| 898 | } |
| 899 | } |
| 900 | if (tstate->c_profilefunc != NULL) { |
| 901 | /* Similar for c_profilefunc, except it needn't |
| 902 | return itself and isn't called for "line" events */ |
| 903 | if (call_trace_protected(tstate->c_profilefunc, |
| 904 | tstate->c_profileobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 905 | tstate, f, PyTrace_CALL, Py_None)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | /* Profile function raised an error */ |
| 907 | goto exit_eval_frame; |
| 908 | } |
| 909 | } |
| 910 | } |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 911 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 912 | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
| 913 | dtrace_function_entry(f); |
| 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | co = f->f_code; |
| 916 | names = co->co_names; |
| 917 | consts = co->co_consts; |
| 918 | fastlocals = f->f_localsplus; |
| 919 | freevars = f->f_localsplus + co->co_nlocals; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 920 | assert(PyBytes_Check(co->co_code)); |
| 921 | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 922 | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
| 923 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
| 924 | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 925 | /* |
| 926 | f->f_lasti refers to the index of the last instruction, |
| 927 | unless it's -1 in which case next_instr should be first_instr. |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 928 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 929 | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 930 | multiple values. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | When the PREDICT() macros are enabled, some opcode pairs follow in |
| 933 | direct succession without updating f->f_lasti. A successful |
| 934 | prediction effectively links the two codes together as if they |
| 935 | were a single new opcode; accordingly,f->f_lasti will point to |
| 936 | the first code in the pair (for instance, GET_ITER followed by |
| 937 | FOR_ITER is effectively a single opcode and f->f_lasti will point |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 938 | to the beginning of the combined pair.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 940 | assert(f->f_lasti >= -1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 941 | next_instr = first_instr; |
| 942 | if (f->f_lasti >= 0) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 943 | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
| 944 | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 945 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | stack_pointer = f->f_stacktop; |
| 947 | assert(stack_pointer != NULL); |
| 948 | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 949 | f->f_executing = 1; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 950 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 951 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 952 | #ifdef LLTRACE |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 953 | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 954 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 955 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 956 | if (throwflag) /* support for generator.throw() */ |
| 957 | goto error; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 958 | |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 959 | #ifdef Py_DEBUG |
| 960 | /* PyEval_EvalFrameEx() must not be called with an exception set, |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 961 | because it can clear it (directly or indirectly) and so the |
Martin Panter | 9955a37 | 2015-10-07 10:26:23 +0000 | [diff] [blame] | 962 | caller loses its exception */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 963 | assert(!PyErr_Occurred()); |
| 964 | #endif |
| 965 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 966 | main_loop: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | for (;;) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
| 969 | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 970 | assert(!PyErr_Occurred()); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | /* Do periodic things. Doing this every time through |
| 973 | the loop would add too much overhead, so we do it |
| 974 | only every Nth instruction. We also do it if |
| 975 | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
| 976 | event needs attention (e.g. a signal handler or |
| 977 | async I/O handler); see Py_AddPendingCall() and |
| 978 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 979 | |
Eric Snow | 7bda9de | 2019-03-08 17:25:54 -0700 | [diff] [blame] | 980 | if (_Py_atomic_load_relaxed(eval_breaker)) { |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 981 | opcode = _Py_OPCODE(*next_instr); |
| 982 | if (opcode == SETUP_FINALLY || |
| 983 | opcode == SETUP_WITH || |
| 984 | opcode == BEFORE_ASYNC_WITH || |
| 985 | opcode == YIELD_FROM) { |
| 986 | /* Few cases where we skip running signal handlers and other |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 987 | pending calls: |
Serhiy Storchaka | 3f4d90d | 2018-07-09 15:40:14 +0300 | [diff] [blame] | 988 | - If we're about to enter the 'with:'. It will prevent |
| 989 | emitting a resource warning in the common idiom |
| 990 | 'with open(path) as file:'. |
| 991 | - If we're about to enter the 'async with:'. |
| 992 | - If we're about to enter the 'try:' of a try/finally (not |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 993 | *very* useful, but might help in some cases and it's |
| 994 | traditional) |
| 995 | - If we're resuming a chain of nested 'yield from' or |
| 996 | 'await' calls, then each frame is parked with YIELD_FROM |
| 997 | as its next opcode. If the user hit control-C we want to |
| 998 | wait until we've reached the innermost frame before |
| 999 | running the signal handler and raising KeyboardInterrupt |
| 1000 | (see bpo-30039). |
| 1001 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | goto fast_next_opcode; |
| 1003 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1004 | |
| 1005 | if (_Py_atomic_load_relaxed( |
| 1006 | &_PyRuntime.ceval.signals_pending)) |
| 1007 | { |
| 1008 | if (handle_signals() != 0) { |
| 1009 | goto error; |
| 1010 | } |
| 1011 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1012 | if (_Py_atomic_load_relaxed( |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 1013 | &_PyRuntime.ceval.pending.calls_to_do)) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1014 | { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 1015 | if (make_pending_calls() != 0) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1016 | goto error; |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1017 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | } |
Eric Snow | fdf282d | 2019-01-11 14:26:55 -0700 | [diff] [blame] | 1019 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1020 | if (_Py_atomic_load_relaxed( |
| 1021 | &_PyRuntime.ceval.gil_drop_request)) |
| 1022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | /* Give another thread a chance */ |
| 1024 | if (PyThreadState_Swap(NULL) != tstate) |
| 1025 | Py_FatalError("ceval: tstate mix-up"); |
| 1026 | drop_gil(tstate); |
| 1027 | |
| 1028 | /* Other threads may run now */ |
| 1029 | |
| 1030 | take_gil(tstate); |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* Check if we should make a quick exit. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1033 | if (_Py_IsFinalizing() && |
| 1034 | !_Py_CURRENTLY_FINALIZING(tstate)) |
| 1035 | { |
Benjamin Peterson | 17548dd | 2014-06-16 22:59:07 -0700 | [diff] [blame] | 1036 | drop_gil(tstate); |
| 1037 | PyThread_exit_thread(); |
| 1038 | } |
| 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | if (PyThreadState_Swap(tstate) != NULL) |
| 1041 | Py_FatalError("ceval: orphan tstate"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | } |
| 1043 | /* Check for asynchronous exceptions. */ |
| 1044 | if (tstate->async_exc != NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1045 | PyObject *exc = tstate->async_exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | tstate->async_exc = NULL; |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 1047 | UNSIGNAL_ASYNC_EXC(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1048 | PyErr_SetNone(exc); |
| 1049 | Py_DECREF(exc); |
| 1050 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | fast_next_opcode: |
| 1055 | f->f_lasti = INSTR_OFFSET(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1056 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1057 | if (PyDTrace_LINE_ENABLED()) |
| 1058 | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
| 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | /* line-by-line tracing support */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | if (_Py_TracingPossible && |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 1063 | tstate->c_tracefunc != NULL && !tstate->tracing) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1064 | int err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | /* see maybe_call_line_trace |
| 1066 | for expository comments */ |
| 1067 | f->f_stacktop = stack_pointer; |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | err = maybe_call_line_trace(tstate->c_tracefunc, |
| 1070 | tstate->c_traceobj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1071 | tstate, f, |
| 1072 | &instr_lb, &instr_ub, &instr_prev); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | /* Reload possibly changed frame fields */ |
| 1074 | JUMPTO(f->f_lasti); |
| 1075 | if (f->f_stacktop != NULL) { |
| 1076 | stack_pointer = f->f_stacktop; |
| 1077 | f->f_stacktop = NULL; |
| 1078 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1079 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | /* trace function raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1081 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | } |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | /* Extract opcode and argument */ |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 1085 | |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 1086 | NEXTOPARG(); |
Stefan Krah | b7e1010 | 2010-06-23 18:42:39 +0000 | [diff] [blame] | 1087 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1088 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1089 | #ifdef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | dxpairs[lastopcode][opcode]++; |
| 1091 | lastopcode = opcode; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1092 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | dxp[opcode]++; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 1094 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1095 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1096 | #ifdef LLTRACE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | if (lltrace) { |
| 1100 | if (HAS_ARG(opcode)) { |
| 1101 | printf("%d: %d, %d\n", |
| 1102 | f->f_lasti, opcode, oparg); |
| 1103 | } |
| 1104 | else { |
| 1105 | printf("%d: %d\n", |
| 1106 | f->f_lasti, opcode); |
| 1107 | } |
| 1108 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1109 | #endif |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | /* BEWARE! |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1114 | It is essential that any operation that fails must goto error |
| 1115 | and that all operation that succeed call [FAST_]DISPATCH() ! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1116 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1117 | case TARGET(NOP): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | FAST_DISPATCH(); |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1119 | } |
Raymond Hettinger | 9c18e81 | 2004-06-21 16:31:15 +0000 | [diff] [blame] | 1120 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1121 | case TARGET(LOAD_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1122 | PyObject *value = GETLOCAL(oparg); |
| 1123 | if (value == NULL) { |
| 1124 | format_exc_check_arg(PyExc_UnboundLocalError, |
| 1125 | UNBOUNDLOCAL_ERROR_MSG, |
| 1126 | PyTuple_GetItem(co->co_varnames, oparg)); |
| 1127 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1129 | Py_INCREF(value); |
| 1130 | PUSH(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1134 | case TARGET(LOAD_CONST): { |
| 1135 | PREDICTED(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1136 | PyObject *value = GETITEM(consts, oparg); |
| 1137 | Py_INCREF(value); |
| 1138 | PUSH(value); |
| 1139 | FAST_DISPATCH(); |
| 1140 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1141 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1142 | case TARGET(STORE_FAST): { |
| 1143 | PREDICTED(STORE_FAST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1144 | PyObject *value = POP(); |
| 1145 | SETLOCAL(oparg, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1147 | } |
Neil Schemenauer | 6354386 | 2002-02-17 19:10:14 +0000 | [diff] [blame] | 1148 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1149 | case TARGET(POP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1150 | PyObject *value = POP(); |
| 1151 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1153 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1154 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1155 | case TARGET(ROT_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1156 | PyObject *top = TOP(); |
| 1157 | PyObject *second = SECOND(); |
| 1158 | SET_TOP(second); |
| 1159 | SET_SECOND(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1161 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1162 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1163 | case TARGET(ROT_THREE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1164 | PyObject *top = TOP(); |
| 1165 | PyObject *second = SECOND(); |
| 1166 | PyObject *third = THIRD(); |
| 1167 | SET_TOP(second); |
| 1168 | SET_SECOND(third); |
| 1169 | SET_THIRD(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1171 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1172 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1173 | case TARGET(ROT_FOUR): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1174 | PyObject *top = TOP(); |
| 1175 | PyObject *second = SECOND(); |
| 1176 | PyObject *third = THIRD(); |
| 1177 | PyObject *fourth = FOURTH(); |
| 1178 | SET_TOP(second); |
| 1179 | SET_SECOND(third); |
| 1180 | SET_THIRD(fourth); |
| 1181 | SET_FOURTH(top); |
| 1182 | FAST_DISPATCH(); |
| 1183 | } |
| 1184 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1185 | case TARGET(DUP_TOP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1186 | PyObject *top = TOP(); |
| 1187 | Py_INCREF(top); |
| 1188 | PUSH(top); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1190 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1191 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1192 | case TARGET(DUP_TOP_TWO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1193 | PyObject *top = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1194 | PyObject *second = SECOND(); |
Benjamin Peterson | f208df3 | 2012-10-12 11:37:56 -0400 | [diff] [blame] | 1195 | Py_INCREF(top); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1196 | Py_INCREF(second); |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1197 | STACK_GROW(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1198 | SET_TOP(top); |
| 1199 | SET_SECOND(second); |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 1200 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1201 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1202 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1203 | case TARGET(UNARY_POSITIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1204 | PyObject *value = TOP(); |
| 1205 | PyObject *res = PyNumber_Positive(value); |
| 1206 | Py_DECREF(value); |
| 1207 | SET_TOP(res); |
| 1208 | if (res == NULL) |
| 1209 | goto error; |
| 1210 | DISPATCH(); |
| 1211 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1212 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1213 | case TARGET(UNARY_NEGATIVE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1214 | PyObject *value = TOP(); |
| 1215 | PyObject *res = PyNumber_Negative(value); |
| 1216 | Py_DECREF(value); |
| 1217 | SET_TOP(res); |
| 1218 | if (res == NULL) |
| 1219 | goto error; |
| 1220 | DISPATCH(); |
| 1221 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1222 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1223 | case TARGET(UNARY_NOT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1224 | PyObject *value = TOP(); |
| 1225 | int err = PyObject_IsTrue(value); |
| 1226 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | if (err == 0) { |
| 1228 | Py_INCREF(Py_True); |
| 1229 | SET_TOP(Py_True); |
| 1230 | DISPATCH(); |
| 1231 | } |
| 1232 | else if (err > 0) { |
| 1233 | Py_INCREF(Py_False); |
| 1234 | SET_TOP(Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | DISPATCH(); |
| 1236 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1237 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1238 | goto error; |
| 1239 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1240 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1241 | case TARGET(UNARY_INVERT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1242 | PyObject *value = TOP(); |
| 1243 | PyObject *res = PyNumber_Invert(value); |
| 1244 | Py_DECREF(value); |
| 1245 | SET_TOP(res); |
| 1246 | if (res == NULL) |
| 1247 | goto error; |
| 1248 | DISPATCH(); |
| 1249 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1250 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1251 | case TARGET(BINARY_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1252 | PyObject *exp = POP(); |
| 1253 | PyObject *base = TOP(); |
| 1254 | PyObject *res = PyNumber_Power(base, exp, Py_None); |
| 1255 | Py_DECREF(base); |
| 1256 | Py_DECREF(exp); |
| 1257 | SET_TOP(res); |
| 1258 | if (res == NULL) |
| 1259 | goto error; |
| 1260 | DISPATCH(); |
| 1261 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1262 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1263 | case TARGET(BINARY_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1264 | PyObject *right = POP(); |
| 1265 | PyObject *left = TOP(); |
| 1266 | PyObject *res = PyNumber_Multiply(left, right); |
| 1267 | Py_DECREF(left); |
| 1268 | Py_DECREF(right); |
| 1269 | SET_TOP(res); |
| 1270 | if (res == NULL) |
| 1271 | goto error; |
| 1272 | DISPATCH(); |
| 1273 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1274 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1275 | case TARGET(BINARY_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1276 | PyObject *right = POP(); |
| 1277 | PyObject *left = TOP(); |
| 1278 | PyObject *res = PyNumber_MatrixMultiply(left, right); |
| 1279 | Py_DECREF(left); |
| 1280 | Py_DECREF(right); |
| 1281 | SET_TOP(res); |
| 1282 | if (res == NULL) |
| 1283 | goto error; |
| 1284 | DISPATCH(); |
| 1285 | } |
| 1286 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1287 | case TARGET(BINARY_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1288 | PyObject *divisor = POP(); |
| 1289 | PyObject *dividend = TOP(); |
| 1290 | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
| 1291 | Py_DECREF(dividend); |
| 1292 | Py_DECREF(divisor); |
| 1293 | SET_TOP(quotient); |
| 1294 | if (quotient == NULL) |
| 1295 | goto error; |
| 1296 | DISPATCH(); |
| 1297 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1298 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1299 | case TARGET(BINARY_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1300 | PyObject *divisor = POP(); |
| 1301 | PyObject *dividend = TOP(); |
| 1302 | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
| 1303 | Py_DECREF(dividend); |
| 1304 | Py_DECREF(divisor); |
| 1305 | SET_TOP(quotient); |
| 1306 | if (quotient == NULL) |
| 1307 | goto error; |
| 1308 | DISPATCH(); |
| 1309 | } |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1310 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1311 | case TARGET(BINARY_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1312 | PyObject *divisor = POP(); |
| 1313 | PyObject *dividend = TOP(); |
Martijn Pieters | d7e6433 | 2017-02-23 13:38:04 +0000 | [diff] [blame] | 1314 | PyObject *res; |
| 1315 | if (PyUnicode_CheckExact(dividend) && ( |
| 1316 | !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { |
| 1317 | // fast path; string formatting, but not if the RHS is a str subclass |
| 1318 | // (see issue28598) |
| 1319 | res = PyUnicode_Format(dividend, divisor); |
| 1320 | } else { |
| 1321 | res = PyNumber_Remainder(dividend, divisor); |
| 1322 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1323 | Py_DECREF(divisor); |
| 1324 | Py_DECREF(dividend); |
| 1325 | SET_TOP(res); |
| 1326 | if (res == NULL) |
| 1327 | goto error; |
| 1328 | DISPATCH(); |
| 1329 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1330 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1331 | case TARGET(BINARY_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1332 | PyObject *right = POP(); |
| 1333 | PyObject *left = TOP(); |
| 1334 | PyObject *sum; |
Victor Stinner | d65f42a | 2016-10-20 12:18:10 +0200 | [diff] [blame] | 1335 | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
| 1336 | CPython using bytecode, it is simply worthless. |
| 1337 | See http://bugs.python.org/issue21955 and |
| 1338 | http://bugs.python.org/issue10044 for the discussion. In short, |
| 1339 | no patch shown any impact on a realistic benchmark, only a minor |
| 1340 | speedup on microbenchmarks. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1341 | if (PyUnicode_CheckExact(left) && |
| 1342 | PyUnicode_CheckExact(right)) { |
| 1343 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1344 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1345 | } |
| 1346 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1347 | sum = PyNumber_Add(left, right); |
| 1348 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1349 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1350 | Py_DECREF(right); |
| 1351 | SET_TOP(sum); |
| 1352 | if (sum == NULL) |
| 1353 | goto error; |
| 1354 | DISPATCH(); |
| 1355 | } |
| 1356 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1357 | case TARGET(BINARY_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1358 | PyObject *right = POP(); |
| 1359 | PyObject *left = TOP(); |
| 1360 | PyObject *diff = PyNumber_Subtract(left, right); |
| 1361 | Py_DECREF(right); |
| 1362 | Py_DECREF(left); |
| 1363 | SET_TOP(diff); |
| 1364 | if (diff == NULL) |
| 1365 | goto error; |
| 1366 | DISPATCH(); |
| 1367 | } |
| 1368 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1369 | case TARGET(BINARY_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1370 | PyObject *sub = POP(); |
| 1371 | PyObject *container = TOP(); |
| 1372 | PyObject *res = PyObject_GetItem(container, sub); |
| 1373 | Py_DECREF(container); |
| 1374 | Py_DECREF(sub); |
| 1375 | SET_TOP(res); |
| 1376 | if (res == NULL) |
| 1377 | goto error; |
| 1378 | DISPATCH(); |
| 1379 | } |
| 1380 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1381 | case TARGET(BINARY_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1382 | PyObject *right = POP(); |
| 1383 | PyObject *left = TOP(); |
| 1384 | PyObject *res = PyNumber_Lshift(left, right); |
| 1385 | Py_DECREF(left); |
| 1386 | Py_DECREF(right); |
| 1387 | SET_TOP(res); |
| 1388 | if (res == NULL) |
| 1389 | goto error; |
| 1390 | DISPATCH(); |
| 1391 | } |
| 1392 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1393 | case TARGET(BINARY_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1394 | PyObject *right = POP(); |
| 1395 | PyObject *left = TOP(); |
| 1396 | PyObject *res = PyNumber_Rshift(left, right); |
| 1397 | Py_DECREF(left); |
| 1398 | Py_DECREF(right); |
| 1399 | SET_TOP(res); |
| 1400 | if (res == NULL) |
| 1401 | goto error; |
| 1402 | DISPATCH(); |
| 1403 | } |
| 1404 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1405 | case TARGET(BINARY_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1406 | PyObject *right = POP(); |
| 1407 | PyObject *left = TOP(); |
| 1408 | PyObject *res = PyNumber_And(left, right); |
| 1409 | Py_DECREF(left); |
| 1410 | Py_DECREF(right); |
| 1411 | SET_TOP(res); |
| 1412 | if (res == NULL) |
| 1413 | goto error; |
| 1414 | DISPATCH(); |
| 1415 | } |
| 1416 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1417 | case TARGET(BINARY_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1418 | PyObject *right = POP(); |
| 1419 | PyObject *left = TOP(); |
| 1420 | PyObject *res = PyNumber_Xor(left, right); |
| 1421 | Py_DECREF(left); |
| 1422 | Py_DECREF(right); |
| 1423 | SET_TOP(res); |
| 1424 | if (res == NULL) |
| 1425 | goto error; |
| 1426 | DISPATCH(); |
| 1427 | } |
| 1428 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1429 | case TARGET(BINARY_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1430 | PyObject *right = POP(); |
| 1431 | PyObject *left = TOP(); |
| 1432 | PyObject *res = PyNumber_Or(left, right); |
| 1433 | Py_DECREF(left); |
| 1434 | Py_DECREF(right); |
| 1435 | SET_TOP(res); |
| 1436 | if (res == NULL) |
| 1437 | goto error; |
| 1438 | DISPATCH(); |
| 1439 | } |
| 1440 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1441 | case TARGET(LIST_APPEND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1442 | PyObject *v = POP(); |
| 1443 | PyObject *list = PEEK(oparg); |
| 1444 | int err; |
| 1445 | err = PyList_Append(list, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1447 | if (err != 0) |
| 1448 | goto error; |
| 1449 | PREDICT(JUMP_ABSOLUTE); |
| 1450 | DISPATCH(); |
| 1451 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1452 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1453 | case TARGET(SET_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1454 | PyObject *v = POP(); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 1455 | PyObject *set = PEEK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1456 | int err; |
| 1457 | err = PySet_Add(set, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1459 | if (err != 0) |
| 1460 | goto error; |
| 1461 | PREDICT(JUMP_ABSOLUTE); |
| 1462 | DISPATCH(); |
| 1463 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1464 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1465 | case TARGET(INPLACE_POWER): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1466 | PyObject *exp = POP(); |
| 1467 | PyObject *base = TOP(); |
| 1468 | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
| 1469 | Py_DECREF(base); |
| 1470 | Py_DECREF(exp); |
| 1471 | SET_TOP(res); |
| 1472 | if (res == NULL) |
| 1473 | goto error; |
| 1474 | DISPATCH(); |
| 1475 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1476 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1477 | case TARGET(INPLACE_MULTIPLY): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1478 | PyObject *right = POP(); |
| 1479 | PyObject *left = TOP(); |
| 1480 | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
| 1481 | Py_DECREF(left); |
| 1482 | Py_DECREF(right); |
| 1483 | SET_TOP(res); |
| 1484 | if (res == NULL) |
| 1485 | goto error; |
| 1486 | DISPATCH(); |
| 1487 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1488 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1489 | case TARGET(INPLACE_MATRIX_MULTIPLY): { |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1490 | PyObject *right = POP(); |
| 1491 | PyObject *left = TOP(); |
| 1492 | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
| 1493 | Py_DECREF(left); |
| 1494 | Py_DECREF(right); |
| 1495 | SET_TOP(res); |
| 1496 | if (res == NULL) |
| 1497 | goto error; |
| 1498 | DISPATCH(); |
| 1499 | } |
| 1500 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1501 | case TARGET(INPLACE_TRUE_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1502 | PyObject *divisor = POP(); |
| 1503 | PyObject *dividend = TOP(); |
| 1504 | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
| 1505 | Py_DECREF(dividend); |
| 1506 | Py_DECREF(divisor); |
| 1507 | SET_TOP(quotient); |
| 1508 | if (quotient == NULL) |
| 1509 | goto error; |
| 1510 | DISPATCH(); |
| 1511 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1512 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1513 | case TARGET(INPLACE_FLOOR_DIVIDE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1514 | PyObject *divisor = POP(); |
| 1515 | PyObject *dividend = TOP(); |
| 1516 | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
| 1517 | Py_DECREF(dividend); |
| 1518 | Py_DECREF(divisor); |
| 1519 | SET_TOP(quotient); |
| 1520 | if (quotient == NULL) |
| 1521 | goto error; |
| 1522 | DISPATCH(); |
| 1523 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1524 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1525 | case TARGET(INPLACE_MODULO): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1526 | PyObject *right = POP(); |
| 1527 | PyObject *left = TOP(); |
| 1528 | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
| 1529 | Py_DECREF(left); |
| 1530 | Py_DECREF(right); |
| 1531 | SET_TOP(mod); |
| 1532 | if (mod == NULL) |
| 1533 | goto error; |
| 1534 | DISPATCH(); |
| 1535 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1536 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1537 | case TARGET(INPLACE_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1538 | PyObject *right = POP(); |
| 1539 | PyObject *left = TOP(); |
| 1540 | PyObject *sum; |
| 1541 | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
| 1542 | sum = unicode_concatenate(left, right, f, next_instr); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1543 | /* unicode_concatenate consumed the ref to left */ |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1544 | } |
| 1545 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1546 | sum = PyNumber_InPlaceAdd(left, right); |
| 1547 | Py_DECREF(left); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 1548 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1549 | Py_DECREF(right); |
| 1550 | SET_TOP(sum); |
| 1551 | if (sum == NULL) |
| 1552 | goto error; |
| 1553 | DISPATCH(); |
| 1554 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1555 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1556 | case TARGET(INPLACE_SUBTRACT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1557 | PyObject *right = POP(); |
| 1558 | PyObject *left = TOP(); |
| 1559 | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
| 1560 | Py_DECREF(left); |
| 1561 | Py_DECREF(right); |
| 1562 | SET_TOP(diff); |
| 1563 | if (diff == NULL) |
| 1564 | goto error; |
| 1565 | DISPATCH(); |
| 1566 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1567 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1568 | case TARGET(INPLACE_LSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1569 | PyObject *right = POP(); |
| 1570 | PyObject *left = TOP(); |
| 1571 | PyObject *res = PyNumber_InPlaceLshift(left, right); |
| 1572 | Py_DECREF(left); |
| 1573 | Py_DECREF(right); |
| 1574 | SET_TOP(res); |
| 1575 | if (res == NULL) |
| 1576 | goto error; |
| 1577 | DISPATCH(); |
| 1578 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1579 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1580 | case TARGET(INPLACE_RSHIFT): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1581 | PyObject *right = POP(); |
| 1582 | PyObject *left = TOP(); |
| 1583 | PyObject *res = PyNumber_InPlaceRshift(left, right); |
| 1584 | Py_DECREF(left); |
| 1585 | Py_DECREF(right); |
| 1586 | SET_TOP(res); |
| 1587 | if (res == NULL) |
| 1588 | goto error; |
| 1589 | DISPATCH(); |
| 1590 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1591 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1592 | case TARGET(INPLACE_AND): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1593 | PyObject *right = POP(); |
| 1594 | PyObject *left = TOP(); |
| 1595 | PyObject *res = PyNumber_InPlaceAnd(left, right); |
| 1596 | Py_DECREF(left); |
| 1597 | Py_DECREF(right); |
| 1598 | SET_TOP(res); |
| 1599 | if (res == NULL) |
| 1600 | goto error; |
| 1601 | DISPATCH(); |
| 1602 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1603 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1604 | case TARGET(INPLACE_XOR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1605 | PyObject *right = POP(); |
| 1606 | PyObject *left = TOP(); |
| 1607 | PyObject *res = PyNumber_InPlaceXor(left, right); |
| 1608 | Py_DECREF(left); |
| 1609 | Py_DECREF(right); |
| 1610 | SET_TOP(res); |
| 1611 | if (res == NULL) |
| 1612 | goto error; |
| 1613 | DISPATCH(); |
| 1614 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1615 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1616 | case TARGET(INPLACE_OR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1617 | PyObject *right = POP(); |
| 1618 | PyObject *left = TOP(); |
| 1619 | PyObject *res = PyNumber_InPlaceOr(left, right); |
| 1620 | Py_DECREF(left); |
| 1621 | Py_DECREF(right); |
| 1622 | SET_TOP(res); |
| 1623 | if (res == NULL) |
| 1624 | goto error; |
| 1625 | DISPATCH(); |
| 1626 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1627 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1628 | case TARGET(STORE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1629 | PyObject *sub = TOP(); |
| 1630 | PyObject *container = SECOND(); |
| 1631 | PyObject *v = THIRD(); |
| 1632 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1633 | STACK_SHRINK(3); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1634 | /* container[sub] = v */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1635 | err = PyObject_SetItem(container, sub, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1637 | Py_DECREF(container); |
| 1638 | Py_DECREF(sub); |
| 1639 | if (err != 0) |
| 1640 | goto error; |
| 1641 | DISPATCH(); |
| 1642 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1643 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1644 | case TARGET(DELETE_SUBSCR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1645 | PyObject *sub = TOP(); |
| 1646 | PyObject *container = SECOND(); |
| 1647 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 1648 | STACK_SHRINK(2); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1649 | /* del container[sub] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1650 | err = PyObject_DelItem(container, sub); |
| 1651 | Py_DECREF(container); |
| 1652 | Py_DECREF(sub); |
| 1653 | if (err != 0) |
| 1654 | goto error; |
| 1655 | DISPATCH(); |
| 1656 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1657 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1658 | case TARGET(PRINT_EXPR): { |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1659 | _Py_IDENTIFIER(displayhook); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1660 | PyObject *value = POP(); |
Victor Stinner | cab75e3 | 2013-11-06 22:38:37 +0100 | [diff] [blame] | 1661 | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
Benjamin Peterson | fe1bcb6 | 2012-10-12 11:40:01 -0400 | [diff] [blame] | 1662 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1663 | if (hook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | PyErr_SetString(PyExc_RuntimeError, |
| 1665 | "lost sys.displayhook"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1666 | Py_DECREF(value); |
| 1667 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | } |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 1669 | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1670 | Py_DECREF(value); |
| 1671 | if (res == NULL) |
| 1672 | goto error; |
| 1673 | Py_DECREF(res); |
| 1674 | DISPATCH(); |
| 1675 | } |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1676 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1677 | case TARGET(RAISE_VARARGS): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1678 | PyObject *cause = NULL, *exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | switch (oparg) { |
| 1680 | case 2: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1681 | cause = POP(); /* cause */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1682 | /* fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | case 1: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1684 | exc = POP(); /* exc */ |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1685 | /* fall through */ |
| 1686 | case 0: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1687 | if (do_raise(exc, cause)) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1688 | goto exception_unwind; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1689 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | default: |
| 1692 | PyErr_SetString(PyExc_SystemError, |
| 1693 | "bad RAISE_VARARGS oparg"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1696 | goto error; |
| 1697 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1698 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1699 | case TARGET(RETURN_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | retval = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1701 | assert(f->f_iblock == 0); |
| 1702 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1703 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1704 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1705 | case TARGET(GET_AITER): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1706 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1707 | PyObject *iter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1708 | PyObject *obj = TOP(); |
| 1709 | PyTypeObject *type = Py_TYPE(obj); |
| 1710 | |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1711 | if (type->tp_as_async != NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1712 | getter = type->tp_as_async->am_aiter; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1713 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1714 | |
| 1715 | if (getter != NULL) { |
| 1716 | iter = (*getter)(obj); |
| 1717 | Py_DECREF(obj); |
| 1718 | if (iter == NULL) { |
| 1719 | SET_TOP(NULL); |
| 1720 | goto error; |
| 1721 | } |
| 1722 | } |
| 1723 | else { |
| 1724 | SET_TOP(NULL); |
| 1725 | PyErr_Format( |
| 1726 | PyExc_TypeError, |
| 1727 | "'async for' requires an object with " |
| 1728 | "__aiter__ method, got %.100s", |
| 1729 | type->tp_name); |
| 1730 | Py_DECREF(obj); |
| 1731 | goto error; |
| 1732 | } |
| 1733 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1734 | if (Py_TYPE(iter)->tp_as_async == NULL || |
| 1735 | Py_TYPE(iter)->tp_as_async->am_anext == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1736 | |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1737 | SET_TOP(NULL); |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1738 | PyErr_Format( |
| 1739 | PyExc_TypeError, |
| 1740 | "'async for' received an object from __aiter__ " |
| 1741 | "that does not implement __anext__: %.100s", |
| 1742 | Py_TYPE(iter)->tp_name); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1743 | Py_DECREF(iter); |
| 1744 | goto error; |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1745 | } |
| 1746 | |
Yury Selivanov | faa135a | 2017-10-06 02:08:57 -0400 | [diff] [blame] | 1747 | SET_TOP(iter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1748 | DISPATCH(); |
| 1749 | } |
| 1750 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1751 | case TARGET(GET_ANEXT): { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 1752 | unaryfunc getter = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1753 | PyObject *next_iter = NULL; |
| 1754 | PyObject *awaitable = NULL; |
| 1755 | PyObject *aiter = TOP(); |
| 1756 | PyTypeObject *type = Py_TYPE(aiter); |
| 1757 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1758 | if (PyAsyncGen_CheckExact(aiter)) { |
| 1759 | awaitable = type->tp_as_async->am_anext(aiter); |
| 1760 | if (awaitable == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1761 | goto error; |
| 1762 | } |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1763 | } else { |
| 1764 | if (type->tp_as_async != NULL){ |
| 1765 | getter = type->tp_as_async->am_anext; |
| 1766 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1767 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1768 | if (getter != NULL) { |
| 1769 | next_iter = (*getter)(aiter); |
| 1770 | if (next_iter == NULL) { |
| 1771 | goto error; |
| 1772 | } |
| 1773 | } |
| 1774 | else { |
| 1775 | PyErr_Format( |
| 1776 | PyExc_TypeError, |
| 1777 | "'async for' requires an iterator with " |
| 1778 | "__anext__ method, got %.100s", |
| 1779 | type->tp_name); |
| 1780 | goto error; |
| 1781 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1782 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1783 | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
| 1784 | if (awaitable == NULL) { |
Yury Selivanov | 398ff91 | 2017-03-02 22:20:00 -0500 | [diff] [blame] | 1785 | _PyErr_FormatFromCause( |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1786 | PyExc_TypeError, |
| 1787 | "'async for' received an invalid object " |
| 1788 | "from __anext__: %.100s", |
| 1789 | Py_TYPE(next_iter)->tp_name); |
| 1790 | |
| 1791 | Py_DECREF(next_iter); |
| 1792 | goto error; |
| 1793 | } else { |
| 1794 | Py_DECREF(next_iter); |
| 1795 | } |
| 1796 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1797 | |
| 1798 | PUSH(awaitable); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1799 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1800 | DISPATCH(); |
| 1801 | } |
| 1802 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1803 | case TARGET(GET_AWAITABLE): { |
| 1804 | PREDICTED(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1805 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1806 | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1807 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 1808 | if (iter == NULL) { |
| 1809 | format_awaitable_error(Py_TYPE(iterable), |
| 1810 | _Py_OPCODE(next_instr[-2])); |
| 1811 | } |
| 1812 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1813 | Py_DECREF(iterable); |
| 1814 | |
Yury Selivanov | c724bae | 2016-03-02 11:30:46 -0500 | [diff] [blame] | 1815 | if (iter != NULL && PyCoro_CheckExact(iter)) { |
| 1816 | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
| 1817 | if (yf != NULL) { |
| 1818 | /* `iter` is a coroutine object that is being |
| 1819 | awaited, `yf` is a pointer to the current awaitable |
| 1820 | being awaited on. */ |
| 1821 | Py_DECREF(yf); |
| 1822 | Py_CLEAR(iter); |
| 1823 | PyErr_SetString( |
| 1824 | PyExc_RuntimeError, |
| 1825 | "coroutine is being awaited already"); |
| 1826 | /* The code below jumps to `error` if `iter` is NULL. */ |
| 1827 | } |
| 1828 | } |
| 1829 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1830 | SET_TOP(iter); /* Even if it's NULL */ |
| 1831 | |
| 1832 | if (iter == NULL) { |
| 1833 | goto error; |
| 1834 | } |
| 1835 | |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 1836 | PREDICT(LOAD_CONST); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1837 | DISPATCH(); |
| 1838 | } |
| 1839 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1840 | case TARGET(YIELD_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1841 | PyObject *v = POP(); |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1842 | PyObject *receiver = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1843 | int err; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1844 | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
| 1845 | retval = _PyGen_Send((PyGenObject *)receiver, v); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1846 | } else { |
Benjamin Peterson | 302e790 | 2012-03-20 23:17:04 -0400 | [diff] [blame] | 1847 | _Py_IDENTIFIER(send); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1848 | if (v == Py_None) |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1849 | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1850 | else |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1851 | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1852 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1853 | Py_DECREF(v); |
| 1854 | if (retval == NULL) { |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1855 | PyObject *val; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1856 | if (tstate->c_tracefunc != NULL |
| 1857 | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 1858 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Nick Coghlan | c40bc09 | 2012-06-17 15:15:49 +1000 | [diff] [blame] | 1859 | err = _PyGen_FetchStopIterationValue(&val); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1860 | if (err < 0) |
| 1861 | goto error; |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 1862 | Py_DECREF(receiver); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1863 | SET_TOP(val); |
| 1864 | DISPATCH(); |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1865 | } |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 1866 | /* receiver remains on stack, retval is value to be yielded */ |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1867 | f->f_stacktop = stack_pointer; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1868 | /* and repeat... */ |
Victor Stinner | f7d199f | 2016-11-24 22:33:01 +0100 | [diff] [blame] | 1869 | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 1870 | f->f_lasti -= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1871 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1872 | } |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1873 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1874 | case TARGET(YIELD_VALUE): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | retval = POP(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1876 | |
| 1877 | if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 1878 | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
| 1879 | Py_DECREF(retval); |
| 1880 | if (w == NULL) { |
| 1881 | retval = NULL; |
| 1882 | goto error; |
| 1883 | } |
| 1884 | retval = w; |
| 1885 | } |
| 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | f->f_stacktop = stack_pointer; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1888 | goto return_or_yield; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1889 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1890 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1891 | case TARGET(POP_EXCEPT): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1892 | PyObject *type, *value, *traceback; |
| 1893 | _PyErr_StackItem *exc_info; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1894 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1895 | if (b->b_type != EXCEPT_HANDLER) { |
| 1896 | PyErr_SetString(PyExc_SystemError, |
| 1897 | "popped block is not an except handler"); |
| 1898 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1900 | assert(STACK_LEVEL() >= (b)->b_level + 3 && |
| 1901 | STACK_LEVEL() <= (b)->b_level + 4); |
| 1902 | exc_info = tstate->exc_info; |
| 1903 | type = exc_info->exc_type; |
| 1904 | value = exc_info->exc_value; |
| 1905 | traceback = exc_info->exc_traceback; |
| 1906 | exc_info->exc_type = POP(); |
| 1907 | exc_info->exc_value = POP(); |
| 1908 | exc_info->exc_traceback = POP(); |
| 1909 | Py_XDECREF(type); |
| 1910 | Py_XDECREF(value); |
| 1911 | Py_XDECREF(traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1913 | } |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 1914 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1915 | case TARGET(POP_BLOCK): { |
| 1916 | PREDICTED(POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1917 | PyFrame_BlockPop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 1919 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1920 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1921 | case TARGET(POP_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1922 | /* If oparg is 0 at the top of the stack are 1 or 6 values: |
| 1923 | Either: |
| 1924 | - TOP = NULL or an integer |
| 1925 | or: |
| 1926 | - (TOP, SECOND, THIRD) = exc_info() |
| 1927 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 1928 | |
| 1929 | If oparg is 1 the value for 'return' was additionally pushed |
| 1930 | at the top of the stack. |
| 1931 | */ |
| 1932 | PyObject *res = NULL; |
| 1933 | if (oparg) { |
| 1934 | res = POP(); |
| 1935 | } |
| 1936 | PyObject *exc = POP(); |
| 1937 | if (exc == NULL || PyLong_CheckExact(exc)) { |
| 1938 | Py_XDECREF(exc); |
| 1939 | } |
| 1940 | else { |
| 1941 | Py_DECREF(exc); |
| 1942 | Py_DECREF(POP()); |
| 1943 | Py_DECREF(POP()); |
| 1944 | |
| 1945 | PyObject *type, *value, *traceback; |
| 1946 | _PyErr_StackItem *exc_info; |
| 1947 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 1948 | if (b->b_type != EXCEPT_HANDLER) { |
| 1949 | PyErr_SetString(PyExc_SystemError, |
| 1950 | "popped block is not an except handler"); |
| 1951 | Py_XDECREF(res); |
| 1952 | goto error; |
| 1953 | } |
| 1954 | assert(STACK_LEVEL() == (b)->b_level + 3); |
| 1955 | exc_info = tstate->exc_info; |
| 1956 | type = exc_info->exc_type; |
| 1957 | value = exc_info->exc_value; |
| 1958 | traceback = exc_info->exc_traceback; |
| 1959 | exc_info->exc_type = POP(); |
| 1960 | exc_info->exc_value = POP(); |
| 1961 | exc_info->exc_traceback = POP(); |
| 1962 | Py_XDECREF(type); |
| 1963 | Py_XDECREF(value); |
| 1964 | Py_XDECREF(traceback); |
| 1965 | } |
| 1966 | if (oparg) { |
| 1967 | PUSH(res); |
| 1968 | } |
| 1969 | DISPATCH(); |
| 1970 | } |
| 1971 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1972 | case TARGET(CALL_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1973 | PyObject *ret = PyLong_FromLong(INSTR_OFFSET()); |
| 1974 | if (ret == NULL) { |
| 1975 | goto error; |
| 1976 | } |
| 1977 | PUSH(ret); |
| 1978 | JUMPBY(oparg); |
| 1979 | FAST_DISPATCH(); |
| 1980 | } |
| 1981 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1982 | case TARGET(BEGIN_FINALLY): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1983 | /* Push NULL onto the stack for using it in END_FINALLY, |
| 1984 | POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH. |
| 1985 | */ |
| 1986 | PUSH(NULL); |
| 1987 | FAST_DISPATCH(); |
| 1988 | } |
| 1989 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 1990 | case TARGET(END_FINALLY): { |
| 1991 | PREDICTED(END_FINALLY); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1992 | /* At the top of the stack are 1 or 6 values: |
| 1993 | Either: |
| 1994 | - TOP = NULL or an integer |
| 1995 | or: |
| 1996 | - (TOP, SECOND, THIRD) = exc_info() |
| 1997 | - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
| 1998 | */ |
| 1999 | PyObject *exc = POP(); |
| 2000 | if (exc == NULL) { |
| 2001 | FAST_DISPATCH(); |
| 2002 | } |
| 2003 | else if (PyLong_CheckExact(exc)) { |
| 2004 | int ret = _PyLong_AsInt(exc); |
| 2005 | Py_DECREF(exc); |
| 2006 | if (ret == -1 && PyErr_Occurred()) { |
| 2007 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2009 | JUMPTO(ret); |
| 2010 | FAST_DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2011 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2012 | else { |
| 2013 | assert(PyExceptionClass_Check(exc)); |
| 2014 | PyObject *val = POP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2015 | PyObject *tb = POP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2016 | PyErr_Restore(exc, val, tb); |
| 2017 | goto exception_unwind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2019 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2020 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2021 | case TARGET(END_ASYNC_FOR): { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2022 | PyObject *exc = POP(); |
| 2023 | assert(PyExceptionClass_Check(exc)); |
| 2024 | if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { |
| 2025 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 2026 | assert(b->b_type == EXCEPT_HANDLER); |
| 2027 | Py_DECREF(exc); |
| 2028 | UNWIND_EXCEPT_HANDLER(b); |
| 2029 | Py_DECREF(POP()); |
| 2030 | JUMPBY(oparg); |
| 2031 | FAST_DISPATCH(); |
| 2032 | } |
| 2033 | else { |
| 2034 | PyObject *val = POP(); |
| 2035 | PyObject *tb = POP(); |
| 2036 | PyErr_Restore(exc, val, tb); |
| 2037 | goto exception_unwind; |
| 2038 | } |
| 2039 | } |
| 2040 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2041 | case TARGET(LOAD_BUILD_CLASS): { |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2042 | _Py_IDENTIFIER(__build_class__); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2043 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2044 | PyObject *bc; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2045 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2046 | bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2047 | if (bc == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2048 | if (!PyErr_Occurred()) { |
| 2049 | PyErr_SetString(PyExc_NameError, |
| 2050 | "__build_class__ not found"); |
| 2051 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2052 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2053 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2054 | Py_INCREF(bc); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2055 | } |
| 2056 | else { |
| 2057 | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
| 2058 | if (build_class_str == NULL) |
Serhiy Storchaka | 70b72f0 | 2016-11-08 23:12:46 +0200 | [diff] [blame] | 2059 | goto error; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2060 | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
| 2061 | if (bc == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2062 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2063 | PyErr_SetString(PyExc_NameError, |
| 2064 | "__build_class__ not found"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2065 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2066 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2068 | PUSH(bc); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2069 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2070 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2071 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2072 | case TARGET(STORE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2073 | PyObject *name = GETITEM(names, oparg); |
| 2074 | PyObject *v = POP(); |
| 2075 | PyObject *ns = f->f_locals; |
| 2076 | int err; |
| 2077 | if (ns == NULL) { |
| 2078 | PyErr_Format(PyExc_SystemError, |
| 2079 | "no locals found when storing %R", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2081 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2083 | if (PyDict_CheckExact(ns)) |
| 2084 | err = PyDict_SetItem(ns, name, v); |
| 2085 | else |
| 2086 | err = PyObject_SetItem(ns, name, v); |
| 2087 | Py_DECREF(v); |
| 2088 | if (err != 0) |
| 2089 | goto error; |
| 2090 | DISPATCH(); |
| 2091 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2092 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2093 | case TARGET(DELETE_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2094 | PyObject *name = GETITEM(names, oparg); |
| 2095 | PyObject *ns = f->f_locals; |
| 2096 | int err; |
| 2097 | if (ns == NULL) { |
| 2098 | PyErr_Format(PyExc_SystemError, |
| 2099 | "no locals when deleting %R", name); |
| 2100 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2102 | err = PyObject_DelItem(ns, name); |
| 2103 | if (err != 0) { |
| 2104 | format_exc_check_arg(PyExc_NameError, |
| 2105 | NAME_ERROR_MSG, |
| 2106 | name); |
| 2107 | goto error; |
| 2108 | } |
| 2109 | DISPATCH(); |
| 2110 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2111 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2112 | case TARGET(UNPACK_SEQUENCE): { |
| 2113 | PREDICTED(UNPACK_SEQUENCE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2114 | PyObject *seq = POP(), *item, **items; |
| 2115 | if (PyTuple_CheckExact(seq) && |
| 2116 | PyTuple_GET_SIZE(seq) == oparg) { |
| 2117 | items = ((PyTupleObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2119 | item = items[oparg]; |
| 2120 | Py_INCREF(item); |
| 2121 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2123 | } else if (PyList_CheckExact(seq) && |
| 2124 | PyList_GET_SIZE(seq) == oparg) { |
| 2125 | items = ((PyListObject *)seq)->ob_item; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | while (oparg--) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2127 | item = items[oparg]; |
| 2128 | Py_INCREF(item); |
| 2129 | PUSH(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2131 | } else if (unpack_iterable(seq, oparg, -1, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | stack_pointer + oparg)) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2133 | STACK_GROW(oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | } else { |
| 2135 | /* unpack_iterable() raised an exception */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2136 | Py_DECREF(seq); |
| 2137 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2139 | Py_DECREF(seq); |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2140 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 2142 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2143 | case TARGET(UNPACK_EX): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2144 | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
| 2145 | PyObject *seq = POP(); |
| 2146 | |
| 2147 | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
| 2148 | stack_pointer + totalargs)) { |
| 2149 | stack_pointer += totalargs; |
| 2150 | } else { |
| 2151 | Py_DECREF(seq); |
| 2152 | goto error; |
| 2153 | } |
| 2154 | Py_DECREF(seq); |
| 2155 | DISPATCH(); |
| 2156 | } |
| 2157 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2158 | case TARGET(STORE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2159 | PyObject *name = GETITEM(names, oparg); |
| 2160 | PyObject *owner = TOP(); |
| 2161 | PyObject *v = SECOND(); |
| 2162 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2163 | STACK_SHRINK(2); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2164 | err = PyObject_SetAttr(owner, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2166 | Py_DECREF(owner); |
| 2167 | if (err != 0) |
| 2168 | goto error; |
| 2169 | DISPATCH(); |
| 2170 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2171 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2172 | case TARGET(DELETE_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2173 | PyObject *name = GETITEM(names, oparg); |
| 2174 | PyObject *owner = POP(); |
| 2175 | int err; |
| 2176 | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
| 2177 | Py_DECREF(owner); |
| 2178 | if (err != 0) |
| 2179 | goto error; |
| 2180 | DISPATCH(); |
| 2181 | } |
| 2182 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2183 | case TARGET(STORE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2184 | PyObject *name = GETITEM(names, oparg); |
| 2185 | PyObject *v = POP(); |
| 2186 | int err; |
| 2187 | err = PyDict_SetItem(f->f_globals, name, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2188 | Py_DECREF(v); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2189 | if (err != 0) |
| 2190 | goto error; |
| 2191 | DISPATCH(); |
| 2192 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2193 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2194 | case TARGET(DELETE_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2195 | PyObject *name = GETITEM(names, oparg); |
| 2196 | int err; |
| 2197 | err = PyDict_DelItem(f->f_globals, name); |
| 2198 | if (err != 0) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2199 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2200 | format_exc_check_arg( |
| 2201 | PyExc_NameError, NAME_ERROR_MSG, name); |
| 2202 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2203 | goto error; |
Benjamin Peterson | 00f86f2 | 2012-10-10 14:10:33 -0400 | [diff] [blame] | 2204 | } |
| 2205 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2206 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2207 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2208 | case TARGET(LOAD_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2209 | PyObject *name = GETITEM(names, oparg); |
| 2210 | PyObject *locals = f->f_locals; |
| 2211 | PyObject *v; |
| 2212 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | PyErr_Format(PyExc_SystemError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2214 | "no locals when loading %R", name); |
| 2215 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2217 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2218 | v = PyDict_GetItemWithError(locals, name); |
| 2219 | if (v != NULL) { |
| 2220 | Py_INCREF(v); |
| 2221 | } |
| 2222 | else if (PyErr_Occurred()) { |
| 2223 | goto error; |
| 2224 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | } |
| 2226 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2227 | v = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2228 | if (v == NULL) { |
Benjamin Peterson | 9272279 | 2012-12-15 12:51:05 -0500 | [diff] [blame] | 2229 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2230 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | PyErr_Clear(); |
| 2232 | } |
| 2233 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2234 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2235 | v = PyDict_GetItemWithError(f->f_globals, name); |
| 2236 | if (v != NULL) { |
| 2237 | Py_INCREF(v); |
| 2238 | } |
| 2239 | else if (PyErr_Occurred()) { |
| 2240 | goto error; |
| 2241 | } |
| 2242 | else { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2243 | if (PyDict_CheckExact(f->f_builtins)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2244 | v = PyDict_GetItemWithError(f->f_builtins, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2245 | if (v == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2246 | if (!PyErr_Occurred()) { |
| 2247 | format_exc_check_arg( |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2248 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2249 | NAME_ERROR_MSG, name); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2250 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2251 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2252 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2253 | Py_INCREF(v); |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2254 | } |
| 2255 | else { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2256 | v = PyObject_GetItem(f->f_builtins, name); |
| 2257 | if (v == NULL) { |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2258 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2259 | format_exc_check_arg( |
| 2260 | PyExc_NameError, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2261 | NAME_ERROR_MSG, name); |
| 2262 | goto error; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2263 | } |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2264 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2267 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2269 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2270 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2271 | case TARGET(LOAD_GLOBAL): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2272 | PyObject *name = GETITEM(names, oparg); |
| 2273 | PyObject *v; |
Victor Stinner | b0b2242 | 2012-04-19 00:57:45 +0200 | [diff] [blame] | 2274 | if (PyDict_CheckExact(f->f_globals) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2275 | && PyDict_CheckExact(f->f_builtins)) |
| 2276 | { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2277 | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2278 | (PyDictObject *)f->f_builtins, |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2279 | name); |
| 2280 | if (v == NULL) { |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2281 | if (!_PyErr_OCCURRED()) { |
| 2282 | /* _PyDict_LoadGlobal() returns NULL without raising |
| 2283 | * an exception if the key doesn't exist */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2284 | format_exc_check_arg(PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2285 | NAME_ERROR_MSG, name); |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2286 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2287 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2289 | Py_INCREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2291 | else { |
| 2292 | /* Slow-path if globals or builtins is not a dict */ |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2293 | |
| 2294 | /* namespace 1: globals */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2295 | v = PyObject_GetItem(f->f_globals, name); |
| 2296 | if (v == NULL) { |
Victor Stinner | 60a1d3c | 2015-11-05 13:55:20 +0100 | [diff] [blame] | 2297 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2298 | goto error; |
| 2299 | PyErr_Clear(); |
| 2300 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 2301 | /* namespace 2: builtins */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2302 | v = PyObject_GetItem(f->f_builtins, name); |
| 2303 | if (v == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2304 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2305 | format_exc_check_arg( |
| 2306 | PyExc_NameError, |
Ezio Melotti | 04a2955 | 2013-03-03 15:12:44 +0200 | [diff] [blame] | 2307 | NAME_ERROR_MSG, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2308 | goto error; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2312 | PUSH(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2314 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2315 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2316 | case TARGET(DELETE_FAST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2317 | PyObject *v = GETLOCAL(oparg); |
| 2318 | if (v != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | SETLOCAL(oparg, NULL); |
| 2320 | DISPATCH(); |
| 2321 | } |
| 2322 | format_exc_check_arg( |
| 2323 | PyExc_UnboundLocalError, |
| 2324 | UNBOUNDLOCAL_ERROR_MSG, |
| 2325 | PyTuple_GetItem(co->co_varnames, oparg) |
| 2326 | ); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2327 | goto error; |
| 2328 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2329 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2330 | case TARGET(DELETE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2331 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 2332 | PyObject *oldobj = PyCell_GET(cell); |
| 2333 | if (oldobj != NULL) { |
| 2334 | PyCell_SET(cell, NULL); |
| 2335 | Py_DECREF(oldobj); |
Benjamin Peterson | 00ebe2c | 2010-09-10 22:02:31 +0000 | [diff] [blame] | 2336 | DISPATCH(); |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2337 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2338 | format_exc_unbound(co, oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2339 | goto error; |
| 2340 | } |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2341 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2342 | case TARGET(LOAD_CLOSURE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2343 | PyObject *cell = freevars[oparg]; |
| 2344 | Py_INCREF(cell); |
| 2345 | PUSH(cell); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2347 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2348 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2349 | case TARGET(LOAD_CLASSDEREF): { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2350 | PyObject *name, *value, *locals = f->f_locals; |
Victor Stinner | d3dfd0e | 2013-05-16 23:48:01 +0200 | [diff] [blame] | 2351 | Py_ssize_t idx; |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2352 | assert(locals); |
| 2353 | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
| 2354 | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
| 2355 | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
| 2356 | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
| 2357 | if (PyDict_CheckExact(locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2358 | value = PyDict_GetItemWithError(locals, name); |
| 2359 | if (value != NULL) { |
| 2360 | Py_INCREF(value); |
| 2361 | } |
| 2362 | else if (PyErr_Occurred()) { |
| 2363 | goto error; |
| 2364 | } |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2365 | } |
| 2366 | else { |
| 2367 | value = PyObject_GetItem(locals, name); |
Victor Stinner | e20310f | 2015-11-05 13:56:58 +0100 | [diff] [blame] | 2368 | if (value == NULL) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2369 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2370 | goto error; |
| 2371 | PyErr_Clear(); |
| 2372 | } |
| 2373 | } |
| 2374 | if (!value) { |
| 2375 | PyObject *cell = freevars[oparg]; |
| 2376 | value = PyCell_GET(cell); |
| 2377 | if (value == NULL) { |
| 2378 | format_exc_unbound(co, oparg); |
| 2379 | goto error; |
| 2380 | } |
| 2381 | Py_INCREF(value); |
| 2382 | } |
| 2383 | PUSH(value); |
| 2384 | DISPATCH(); |
| 2385 | } |
| 2386 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2387 | case TARGET(LOAD_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2388 | PyObject *cell = freevars[oparg]; |
| 2389 | PyObject *value = PyCell_GET(cell); |
| 2390 | if (value == NULL) { |
| 2391 | format_exc_unbound(co, oparg); |
| 2392 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2394 | Py_INCREF(value); |
| 2395 | PUSH(value); |
| 2396 | DISPATCH(); |
| 2397 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2398 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2399 | case TARGET(STORE_DEREF): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2400 | PyObject *v = POP(); |
| 2401 | PyObject *cell = freevars[oparg]; |
Raymond Hettinger | b2b1543 | 2016-11-11 04:32:11 -0800 | [diff] [blame] | 2402 | PyObject *oldobj = PyCell_GET(cell); |
| 2403 | PyCell_SET(cell, v); |
| 2404 | Py_XDECREF(oldobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2405 | DISPATCH(); |
| 2406 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2407 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2408 | case TARGET(BUILD_STRING): { |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 2409 | PyObject *str; |
| 2410 | PyObject *empty = PyUnicode_New(0, 0); |
| 2411 | if (empty == NULL) { |
| 2412 | goto error; |
| 2413 | } |
| 2414 | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
| 2415 | Py_DECREF(empty); |
| 2416 | if (str == NULL) |
| 2417 | goto error; |
| 2418 | while (--oparg >= 0) { |
| 2419 | PyObject *item = POP(); |
| 2420 | Py_DECREF(item); |
| 2421 | } |
| 2422 | PUSH(str); |
| 2423 | DISPATCH(); |
| 2424 | } |
| 2425 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2426 | case TARGET(BUILD_TUPLE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2427 | PyObject *tup = PyTuple_New(oparg); |
| 2428 | if (tup == NULL) |
| 2429 | goto error; |
| 2430 | while (--oparg >= 0) { |
| 2431 | PyObject *item = POP(); |
| 2432 | PyTuple_SET_ITEM(tup, oparg, item); |
| 2433 | } |
| 2434 | PUSH(tup); |
| 2435 | DISPATCH(); |
| 2436 | } |
| 2437 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2438 | case TARGET(BUILD_LIST): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2439 | PyObject *list = PyList_New(oparg); |
| 2440 | if (list == NULL) |
| 2441 | goto error; |
| 2442 | while (--oparg >= 0) { |
| 2443 | PyObject *item = POP(); |
| 2444 | PyList_SET_ITEM(list, oparg, item); |
| 2445 | } |
| 2446 | PUSH(list); |
| 2447 | DISPATCH(); |
| 2448 | } |
| 2449 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2450 | case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL): |
| 2451 | case TARGET(BUILD_TUPLE_UNPACK): |
| 2452 | case TARGET(BUILD_LIST_UNPACK): { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2453 | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2454 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2455 | PyObject *sum = PyList_New(0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2456 | PyObject *return_value; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2457 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2458 | if (sum == NULL) |
| 2459 | goto error; |
| 2460 | |
| 2461 | for (i = oparg; i > 0; i--) { |
| 2462 | PyObject *none_val; |
| 2463 | |
| 2464 | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
| 2465 | if (none_val == NULL) { |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2466 | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 2467 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2468 | { |
| 2469 | check_args_iterable(PEEK(1 + oparg), PEEK(i)); |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 2470 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2471 | Py_DECREF(sum); |
| 2472 | goto error; |
| 2473 | } |
| 2474 | Py_DECREF(none_val); |
| 2475 | } |
| 2476 | |
| 2477 | if (convert_to_tuple) { |
| 2478 | return_value = PyList_AsTuple(sum); |
| 2479 | Py_DECREF(sum); |
| 2480 | if (return_value == NULL) |
| 2481 | goto error; |
| 2482 | } |
| 2483 | else { |
| 2484 | return_value = sum; |
| 2485 | } |
| 2486 | |
| 2487 | while (oparg--) |
| 2488 | Py_DECREF(POP()); |
| 2489 | PUSH(return_value); |
| 2490 | DISPATCH(); |
| 2491 | } |
| 2492 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2493 | case TARGET(BUILD_SET): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2494 | PyObject *set = PySet_New(NULL); |
| 2495 | int err = 0; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2496 | int i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2497 | if (set == NULL) |
| 2498 | goto error; |
Raymond Hettinger | 4c483ad | 2016-09-08 14:45:40 -0700 | [diff] [blame] | 2499 | for (i = oparg; i > 0; i--) { |
| 2500 | PyObject *item = PEEK(i); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2501 | if (err == 0) |
| 2502 | err = PySet_Add(set, item); |
| 2503 | Py_DECREF(item); |
| 2504 | } |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2505 | STACK_SHRINK(oparg); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2506 | if (err != 0) { |
| 2507 | Py_DECREF(set); |
| 2508 | goto error; |
| 2509 | } |
| 2510 | PUSH(set); |
| 2511 | DISPATCH(); |
| 2512 | } |
| 2513 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2514 | case TARGET(BUILD_SET_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2515 | Py_ssize_t i; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2516 | PyObject *sum = PySet_New(NULL); |
| 2517 | if (sum == NULL) |
| 2518 | goto error; |
| 2519 | |
| 2520 | for (i = oparg; i > 0; i--) { |
| 2521 | if (_PySet_Update(sum, PEEK(i)) < 0) { |
| 2522 | Py_DECREF(sum); |
| 2523 | goto error; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | while (oparg--) |
| 2528 | Py_DECREF(POP()); |
| 2529 | PUSH(sum); |
| 2530 | DISPATCH(); |
| 2531 | } |
| 2532 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2533 | case TARGET(BUILD_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2534 | Py_ssize_t i; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2535 | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2536 | if (map == NULL) |
| 2537 | goto error; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2538 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2539 | int err; |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2540 | PyObject *key = PEEK(2*i); |
| 2541 | PyObject *value = PEEK(2*i - 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2542 | err = PyDict_SetItem(map, key, value); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2543 | if (err != 0) { |
| 2544 | Py_DECREF(map); |
| 2545 | goto error; |
| 2546 | } |
| 2547 | } |
Benjamin Peterson | d5d77aa | 2015-07-05 10:37:25 -0500 | [diff] [blame] | 2548 | |
| 2549 | while (oparg--) { |
| 2550 | Py_DECREF(POP()); |
| 2551 | Py_DECREF(POP()); |
| 2552 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2553 | PUSH(map); |
| 2554 | DISPATCH(); |
| 2555 | } |
| 2556 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2557 | case TARGET(SETUP_ANNOTATIONS): { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2558 | _Py_IDENTIFIER(__annotations__); |
| 2559 | int err; |
| 2560 | PyObject *ann_dict; |
| 2561 | if (f->f_locals == NULL) { |
| 2562 | PyErr_Format(PyExc_SystemError, |
| 2563 | "no locals found when setting up annotations"); |
| 2564 | goto error; |
| 2565 | } |
| 2566 | /* check if __annotations__ in locals()... */ |
| 2567 | if (PyDict_CheckExact(f->f_locals)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2568 | ann_dict = _PyDict_GetItemIdWithError(f->f_locals, |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2569 | &PyId___annotations__); |
| 2570 | if (ann_dict == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2571 | if (PyErr_Occurred()) { |
| 2572 | goto error; |
| 2573 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2574 | /* ...if not, create a new one */ |
| 2575 | ann_dict = PyDict_New(); |
| 2576 | if (ann_dict == NULL) { |
| 2577 | goto error; |
| 2578 | } |
| 2579 | err = _PyDict_SetItemId(f->f_locals, |
| 2580 | &PyId___annotations__, ann_dict); |
| 2581 | Py_DECREF(ann_dict); |
| 2582 | if (err != 0) { |
| 2583 | goto error; |
| 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | else { |
| 2588 | /* do the same if locals() is not a dict */ |
| 2589 | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
| 2590 | if (ann_str == NULL) { |
Serhiy Storchaka | 4678b2f | 2016-11-08 23:13:36 +0200 | [diff] [blame] | 2591 | goto error; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 2592 | } |
| 2593 | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
| 2594 | if (ann_dict == NULL) { |
| 2595 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2596 | goto error; |
| 2597 | } |
| 2598 | PyErr_Clear(); |
| 2599 | ann_dict = PyDict_New(); |
| 2600 | if (ann_dict == NULL) { |
| 2601 | goto error; |
| 2602 | } |
| 2603 | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
| 2604 | Py_DECREF(ann_dict); |
| 2605 | if (err != 0) { |
| 2606 | goto error; |
| 2607 | } |
| 2608 | } |
| 2609 | else { |
| 2610 | Py_DECREF(ann_dict); |
| 2611 | } |
| 2612 | } |
| 2613 | DISPATCH(); |
| 2614 | } |
| 2615 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2616 | case TARGET(BUILD_CONST_KEY_MAP): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2617 | Py_ssize_t i; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 2618 | PyObject *map; |
| 2619 | PyObject *keys = TOP(); |
| 2620 | if (!PyTuple_CheckExact(keys) || |
| 2621 | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
| 2622 | PyErr_SetString(PyExc_SystemError, |
| 2623 | "bad BUILD_CONST_KEY_MAP keys argument"); |
| 2624 | goto error; |
| 2625 | } |
| 2626 | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
| 2627 | if (map == NULL) { |
| 2628 | goto error; |
| 2629 | } |
| 2630 | for (i = oparg; i > 0; i--) { |
| 2631 | int err; |
| 2632 | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
| 2633 | PyObject *value = PEEK(i + 1); |
| 2634 | err = PyDict_SetItem(map, key, value); |
| 2635 | if (err != 0) { |
| 2636 | Py_DECREF(map); |
| 2637 | goto error; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | Py_DECREF(POP()); |
| 2642 | while (oparg--) { |
| 2643 | Py_DECREF(POP()); |
| 2644 | } |
| 2645 | PUSH(map); |
| 2646 | DISPATCH(); |
| 2647 | } |
| 2648 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2649 | case TARGET(BUILD_MAP_UNPACK): { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 2650 | Py_ssize_t i; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 2651 | PyObject *sum = PyDict_New(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2652 | if (sum == NULL) |
| 2653 | goto error; |
| 2654 | |
| 2655 | for (i = oparg; i > 0; i--) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2656 | PyObject *arg = PEEK(i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2657 | if (PyDict_Update(sum, arg) < 0) { |
| 2658 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 2659 | PyErr_Format(PyExc_TypeError, |
Berker Peksag | 8e9045d | 2016-10-02 13:08:25 +0300 | [diff] [blame] | 2660 | "'%.200s' object is not a mapping", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2661 | arg->ob_type->tp_name); |
| 2662 | } |
| 2663 | Py_DECREF(sum); |
| 2664 | goto error; |
| 2665 | } |
| 2666 | } |
| 2667 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 2668 | while (oparg--) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2669 | Py_DECREF(POP()); |
| 2670 | PUSH(sum); |
| 2671 | DISPATCH(); |
| 2672 | } |
| 2673 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2674 | case TARGET(BUILD_MAP_UNPACK_WITH_CALL): { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2675 | Py_ssize_t i; |
| 2676 | PyObject *sum = PyDict_New(); |
| 2677 | if (sum == NULL) |
| 2678 | goto error; |
| 2679 | |
| 2680 | for (i = oparg; i > 0; i--) { |
| 2681 | PyObject *arg = PEEK(i); |
| 2682 | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2683 | Py_DECREF(sum); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 2684 | format_kwargs_error(PEEK(2 + oparg), arg); |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2685 | goto error; |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | while (oparg--) |
| 2690 | Py_DECREF(POP()); |
| 2691 | PUSH(sum); |
| 2692 | DISPATCH(); |
| 2693 | } |
| 2694 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2695 | case TARGET(MAP_ADD): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2696 | PyObject *key = TOP(); |
| 2697 | PyObject *value = SECOND(); |
| 2698 | PyObject *map; |
| 2699 | int err; |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2700 | STACK_SHRINK(2); |
Raymond Hettinger | 4186222 | 2016-10-15 19:03:06 -0700 | [diff] [blame] | 2701 | map = PEEK(oparg); /* dict */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2702 | assert(PyDict_CheckExact(map)); |
Martin Panter | 95f53c1 | 2016-07-18 08:23:26 +0000 | [diff] [blame] | 2703 | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2704 | Py_DECREF(value); |
| 2705 | Py_DECREF(key); |
| 2706 | if (err != 0) |
| 2707 | goto error; |
| 2708 | PREDICT(JUMP_ABSOLUTE); |
| 2709 | DISPATCH(); |
| 2710 | } |
| 2711 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2712 | case TARGET(LOAD_ATTR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2713 | PyObject *name = GETITEM(names, oparg); |
| 2714 | PyObject *owner = TOP(); |
| 2715 | PyObject *res = PyObject_GetAttr(owner, name); |
| 2716 | Py_DECREF(owner); |
| 2717 | SET_TOP(res); |
| 2718 | if (res == NULL) |
| 2719 | goto error; |
| 2720 | DISPATCH(); |
| 2721 | } |
| 2722 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2723 | case TARGET(COMPARE_OP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2724 | PyObject *right = POP(); |
| 2725 | PyObject *left = TOP(); |
| 2726 | PyObject *res = cmp_outcome(oparg, left, right); |
| 2727 | Py_DECREF(left); |
| 2728 | Py_DECREF(right); |
| 2729 | SET_TOP(res); |
| 2730 | if (res == NULL) |
| 2731 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | PREDICT(POP_JUMP_IF_FALSE); |
| 2733 | PREDICT(POP_JUMP_IF_TRUE); |
| 2734 | DISPATCH(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2735 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2736 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2737 | case TARGET(IMPORT_NAME): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2738 | PyObject *name = GETITEM(names, oparg); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2739 | PyObject *fromlist = POP(); |
| 2740 | PyObject *level = TOP(); |
| 2741 | PyObject *res; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 2742 | res = import_name(f, name, fromlist, level); |
| 2743 | Py_DECREF(level); |
| 2744 | Py_DECREF(fromlist); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2745 | SET_TOP(res); |
| 2746 | if (res == NULL) |
| 2747 | goto error; |
| 2748 | DISPATCH(); |
| 2749 | } |
| 2750 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2751 | case TARGET(IMPORT_STAR): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2752 | PyObject *from = POP(), *locals; |
| 2753 | int err; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2754 | if (PyFrame_FastToLocalsWithError(f) < 0) { |
| 2755 | Py_DECREF(from); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2756 | goto error; |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2757 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 2758 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2759 | locals = f->f_locals; |
| 2760 | if (locals == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | PyErr_SetString(PyExc_SystemError, |
| 2762 | "no locals found during 'import *'"); |
Matthias Bussonnier | 160edb4 | 2017-02-25 21:58:05 -0800 | [diff] [blame] | 2763 | Py_DECREF(from); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2764 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2766 | err = import_all_from(locals, from); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | PyFrame_LocalsToFast(f, 0); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2768 | Py_DECREF(from); |
| 2769 | if (err != 0) |
| 2770 | goto error; |
| 2771 | DISPATCH(); |
| 2772 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2773 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2774 | case TARGET(IMPORT_FROM): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2775 | PyObject *name = GETITEM(names, oparg); |
| 2776 | PyObject *from = TOP(); |
| 2777 | PyObject *res; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2778 | res = import_from(from, name); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2779 | PUSH(res); |
| 2780 | if (res == NULL) |
| 2781 | goto error; |
| 2782 | DISPATCH(); |
| 2783 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2784 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2785 | case TARGET(JUMP_FORWARD): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | JUMPBY(oparg); |
| 2787 | FAST_DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2788 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2789 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2790 | case TARGET(POP_JUMP_IF_FALSE): { |
| 2791 | PREDICTED(POP_JUMP_IF_FALSE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2792 | PyObject *cond = POP(); |
| 2793 | int err; |
| 2794 | if (cond == Py_True) { |
| 2795 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | FAST_DISPATCH(); |
| 2797 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2798 | if (cond == Py_False) { |
| 2799 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | JUMPTO(oparg); |
| 2801 | FAST_DISPATCH(); |
| 2802 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2803 | err = PyObject_IsTrue(cond); |
| 2804 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2805 | if (err > 0) |
Adrian Wielgosik | 50c2850 | 2017-06-23 13:35:41 -0700 | [diff] [blame] | 2806 | ; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | else if (err == 0) |
| 2808 | JUMPTO(oparg); |
| 2809 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2810 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2812 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2813 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2814 | case TARGET(POP_JUMP_IF_TRUE): { |
| 2815 | PREDICTED(POP_JUMP_IF_TRUE); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2816 | PyObject *cond = POP(); |
| 2817 | int err; |
| 2818 | if (cond == Py_False) { |
| 2819 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | FAST_DISPATCH(); |
| 2821 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2822 | if (cond == Py_True) { |
| 2823 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | JUMPTO(oparg); |
| 2825 | FAST_DISPATCH(); |
| 2826 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2827 | err = PyObject_IsTrue(cond); |
| 2828 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2829 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | JUMPTO(oparg); |
| 2831 | } |
| 2832 | else if (err == 0) |
| 2833 | ; |
| 2834 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2835 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2837 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2838 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2839 | case TARGET(JUMP_IF_FALSE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2840 | PyObject *cond = TOP(); |
| 2841 | int err; |
| 2842 | if (cond == Py_True) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2843 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2844 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | FAST_DISPATCH(); |
| 2846 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2847 | if (cond == Py_False) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | JUMPTO(oparg); |
| 2849 | FAST_DISPATCH(); |
| 2850 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2851 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | if (err > 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2853 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2854 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | } |
| 2856 | else if (err == 0) |
| 2857 | JUMPTO(oparg); |
| 2858 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2859 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2861 | } |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2862 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2863 | case TARGET(JUMP_IF_TRUE_OR_POP): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2864 | PyObject *cond = TOP(); |
| 2865 | int err; |
| 2866 | if (cond == Py_False) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2867 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2868 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | FAST_DISPATCH(); |
| 2870 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2871 | if (cond == Py_True) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | JUMPTO(oparg); |
| 2873 | FAST_DISPATCH(); |
| 2874 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2875 | err = PyObject_IsTrue(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | if (err > 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | JUMPTO(oparg); |
| 2878 | } |
| 2879 | else if (err == 0) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2880 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2881 | Py_DECREF(cond); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | } |
| 2883 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2884 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2886 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2887 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2888 | case TARGET(JUMP_ABSOLUTE): { |
| 2889 | PREDICTED(JUMP_ABSOLUTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | JUMPTO(oparg); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2891 | #if FAST_LOOPS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | /* Enabling this path speeds-up all while and for-loops by bypassing |
| 2893 | the per-loop checks for signals. By default, this should be turned-off |
| 2894 | because it prevents detection of a control-break in tight loops like |
| 2895 | "while 1: pass". Compile with this option turned-on when you need |
| 2896 | the speed-up and do not need break checking inside tight loops (ones |
| 2897 | that contain only instructions ending with FAST_DISPATCH). |
| 2898 | */ |
| 2899 | FAST_DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2900 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | DISPATCH(); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2902 | #endif |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2903 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2904 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2905 | case TARGET(GET_ITER): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | /* before: [obj]; after [getiter(obj)] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2907 | PyObject *iterable = TOP(); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2908 | PyObject *iter = PyObject_GetIter(iterable); |
| 2909 | Py_DECREF(iterable); |
| 2910 | SET_TOP(iter); |
| 2911 | if (iter == NULL) |
| 2912 | goto error; |
| 2913 | PREDICT(FOR_ITER); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2914 | PREDICT(CALL_FUNCTION); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2915 | DISPATCH(); |
| 2916 | } |
| 2917 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2918 | case TARGET(GET_YIELD_FROM_ITER): { |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2919 | /* before: [obj]; after [getiter(obj)] */ |
| 2920 | PyObject *iterable = TOP(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2921 | PyObject *iter; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 2922 | if (PyCoro_CheckExact(iterable)) { |
| 2923 | /* `iterable` is a coroutine */ |
| 2924 | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
| 2925 | /* and it is used in a 'yield from' expression of a |
| 2926 | regular generator. */ |
| 2927 | Py_DECREF(iterable); |
| 2928 | SET_TOP(NULL); |
| 2929 | PyErr_SetString(PyExc_TypeError, |
| 2930 | "cannot 'yield from' a coroutine object " |
| 2931 | "in a non-coroutine generator"); |
| 2932 | goto error; |
| 2933 | } |
| 2934 | } |
| 2935 | else if (!PyGen_CheckExact(iterable)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2936 | /* `iterable` is not a generator. */ |
| 2937 | iter = PyObject_GetIter(iterable); |
| 2938 | Py_DECREF(iterable); |
| 2939 | SET_TOP(iter); |
| 2940 | if (iter == NULL) |
| 2941 | goto error; |
| 2942 | } |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2943 | PREDICT(LOAD_CONST); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2944 | DISPATCH(); |
| 2945 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2946 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2947 | case TARGET(FOR_ITER): { |
| 2948 | PREDICTED(FOR_ITER); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2950 | PyObject *iter = TOP(); |
| 2951 | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
| 2952 | if (next != NULL) { |
| 2953 | PUSH(next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | PREDICT(STORE_FAST); |
| 2955 | PREDICT(UNPACK_SEQUENCE); |
| 2956 | DISPATCH(); |
| 2957 | } |
| 2958 | if (PyErr_Occurred()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2959 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2960 | goto error; |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 2961 | else if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 2962 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | PyErr_Clear(); |
| 2964 | } |
| 2965 | /* iterator ended normally */ |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 2966 | STACK_SHRINK(1); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2967 | Py_DECREF(iter); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2968 | JUMPBY(oparg); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 2969 | PREDICT(POP_BLOCK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2971 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2972 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2973 | case TARGET(SETUP_FINALLY): { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | /* NOTE: If you add any new block-setup opcodes that |
| 2975 | are not try/except/finally handlers, you may need |
| 2976 | to update the PyGen_NeedsFinalizing() function. |
| 2977 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2978 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2979 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | STACK_LEVEL()); |
| 2981 | DISPATCH(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 2982 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2983 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 2984 | case TARGET(BEFORE_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2985 | _Py_IDENTIFIER(__aexit__); |
| 2986 | _Py_IDENTIFIER(__aenter__); |
| 2987 | |
| 2988 | PyObject *mgr = TOP(); |
| 2989 | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
| 2990 | *enter; |
| 2991 | PyObject *res; |
| 2992 | if (exit == NULL) |
| 2993 | goto error; |
| 2994 | SET_TOP(exit); |
| 2995 | enter = special_lookup(mgr, &PyId___aenter__); |
| 2996 | Py_DECREF(mgr); |
| 2997 | if (enter == NULL) |
| 2998 | goto error; |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 2999 | res = _PyObject_CallNoArg(enter); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3000 | Py_DECREF(enter); |
| 3001 | if (res == NULL) |
| 3002 | goto error; |
| 3003 | PUSH(res); |
Serhiy Storchaka | da9c513 | 2016-06-27 18:58:57 +0300 | [diff] [blame] | 3004 | PREDICT(GET_AWAITABLE); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3005 | DISPATCH(); |
| 3006 | } |
| 3007 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3008 | case TARGET(SETUP_ASYNC_WITH): { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3009 | PyObject *res = POP(); |
| 3010 | /* Setup the finally block before pushing the result |
| 3011 | of __aenter__ on the stack. */ |
| 3012 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3013 | STACK_LEVEL()); |
| 3014 | PUSH(res); |
| 3015 | DISPATCH(); |
| 3016 | } |
| 3017 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3018 | case TARGET(SETUP_WITH): { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 3019 | _Py_IDENTIFIER(__exit__); |
| 3020 | _Py_IDENTIFIER(__enter__); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3021 | PyObject *mgr = TOP(); |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3022 | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3023 | PyObject *res; |
Raymond Hettinger | a3fec15 | 2016-11-21 17:24:23 -0800 | [diff] [blame] | 3024 | if (enter == NULL) |
| 3025 | goto error; |
| 3026 | exit = special_lookup(mgr, &PyId___exit__); |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3027 | if (exit == NULL) { |
| 3028 | Py_DECREF(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3029 | goto error; |
Raymond Hettinger | 64e2f9a | 2016-11-22 11:50:40 -0800 | [diff] [blame] | 3030 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3031 | SET_TOP(exit); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3032 | Py_DECREF(mgr); |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 3033 | res = _PyObject_CallNoArg(enter); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3034 | Py_DECREF(enter); |
| 3035 | if (res == NULL) |
| 3036 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | /* Setup the finally block before pushing the result |
| 3038 | of __enter__ on the stack. */ |
| 3039 | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
| 3040 | STACK_LEVEL()); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3041 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3042 | PUSH(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | DISPATCH(); |
| 3044 | } |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3045 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3046 | case TARGET(WITH_CLEANUP_START): { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3047 | /* At the top of the stack are 1 or 6 values indicating |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | how/why we entered the finally clause: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3049 | - TOP = NULL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | - (TOP, SECOND, THIRD) = exc_info() |
| 3051 | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3052 | Below them is EXIT, the context.__exit__ or context.__aexit__ |
| 3053 | bound method. |
| 3054 | In the first case, we must call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | EXIT(None, None, None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3056 | otherwise we must call |
| 3057 | EXIT(TOP, SECOND, THIRD) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3058 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3059 | In the first case, we remove EXIT from the |
| 3060 | stack, leaving TOP, and push TOP on the stack. |
| 3061 | Otherwise we shift the bottom 3 values of the |
| 3062 | stack down, replace the empty spot with NULL, and push |
| 3063 | None on the stack. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 3064 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3065 | Finally we push the result of the call. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3067 | PyObject *stack[3]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | PyObject *exit_func; |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3069 | PyObject *exc, *val, *tb, *res; |
| 3070 | |
| 3071 | val = tb = Py_None; |
| 3072 | exc = TOP(); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3073 | if (exc == NULL) { |
costypetrisor | 8ed317f | 2018-07-31 20:55:14 +0000 | [diff] [blame] | 3074 | STACK_SHRINK(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | exit_func = TOP(); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3076 | SET_TOP(exc); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3077 | exc = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | } |
| 3079 | else { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3080 | assert(PyExceptionClass_Check(exc)); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3081 | PyObject *tp2, *exc2, *tb2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | PyTryBlock *block; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3083 | val = SECOND(); |
| 3084 | tb = THIRD(); |
| 3085 | tp2 = FOURTH(); |
| 3086 | exc2 = PEEK(5); |
| 3087 | tb2 = PEEK(6); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | exit_func = PEEK(7); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3089 | SET_VALUE(7, tb2); |
| 3090 | SET_VALUE(6, exc2); |
| 3091 | SET_VALUE(5, tp2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
| 3093 | SET_FOURTH(NULL); |
| 3094 | /* We just shifted the stack down, so we have |
| 3095 | to tell the except handler block that the |
| 3096 | values are lower than it expects. */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3097 | assert(f->f_iblock > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | block = &f->f_blockstack[f->f_iblock - 1]; |
| 3099 | assert(block->b_type == EXCEPT_HANDLER); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3100 | assert(block->b_level > 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | block->b_level--; |
| 3102 | } |
Victor Stinner | 842cfff | 2016-12-01 14:45:31 +0100 | [diff] [blame] | 3103 | |
| 3104 | stack[0] = exc; |
| 3105 | stack[1] = val; |
| 3106 | stack[2] = tb; |
| 3107 | res = _PyObject_FastCall(exit_func, stack, 3); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | Py_DECREF(exit_func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3109 | if (res == NULL) |
| 3110 | goto error; |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3111 | |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3112 | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3113 | PUSH(exc); |
| 3114 | PUSH(res); |
| 3115 | PREDICT(WITH_CLEANUP_FINISH); |
| 3116 | DISPATCH(); |
| 3117 | } |
| 3118 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3119 | case TARGET(WITH_CLEANUP_FINISH): { |
| 3120 | PREDICTED(WITH_CLEANUP_FINISH); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3121 | /* TOP = the result of calling the context.__exit__ bound method |
| 3122 | SECOND = either None or exception type |
| 3123 | |
| 3124 | If SECOND is None below is NULL or the return address, |
| 3125 | otherwise below are 7 values representing an exception. |
| 3126 | */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3127 | PyObject *res = POP(); |
| 3128 | PyObject *exc = POP(); |
| 3129 | int err; |
| 3130 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3131 | if (exc != Py_None) |
| 3132 | err = PyObject_IsTrue(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | else |
| 3134 | err = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3135 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3136 | Py_DECREF(res); |
Nick Coghlan | baaadbf | 2015-05-13 15:54:02 +1000 | [diff] [blame] | 3137 | Py_DECREF(exc); |
Amaury Forgeot d'Arc | 10b24e8 | 2008-12-10 23:49:33 +0000 | [diff] [blame] | 3138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | if (err < 0) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3140 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | else if (err > 0) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3142 | /* There was an exception and a True return. |
| 3143 | * We must manually unwind the EXCEPT_HANDLER block |
| 3144 | * which was created when the exception was caught, |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 3145 | * otherwise the stack will be in an inconsistent state. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3146 | */ |
| 3147 | PyTryBlock *b = PyFrame_BlockPop(f); |
| 3148 | assert(b->b_type == EXCEPT_HANDLER); |
| 3149 | UNWIND_EXCEPT_HANDLER(b); |
| 3150 | PUSH(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | } |
| 3152 | PREDICT(END_FINALLY); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3153 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | } |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3155 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3156 | case TARGET(LOAD_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3157 | /* Designed to work in tamdem with CALL_METHOD. */ |
| 3158 | PyObject *name = GETITEM(names, oparg); |
| 3159 | PyObject *obj = TOP(); |
| 3160 | PyObject *meth = NULL; |
| 3161 | |
| 3162 | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
| 3163 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3164 | if (meth == NULL) { |
| 3165 | /* Most likely attribute wasn't found. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3166 | goto error; |
| 3167 | } |
| 3168 | |
| 3169 | if (meth_found) { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3170 | /* We can bypass temporary bound method object. |
| 3171 | meth is unbound method and obj is self. |
Victor Stinner | a8cb515 | 2017-01-18 14:12:51 +0100 | [diff] [blame] | 3172 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3173 | meth | self | arg1 | ... | argN |
| 3174 | */ |
| 3175 | SET_TOP(meth); |
| 3176 | PUSH(obj); // self |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3177 | } |
| 3178 | else { |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3179 | /* meth is not an unbound method (but a regular attr, or |
| 3180 | something was returned by a descriptor protocol). Set |
| 3181 | the second element of the stack to NULL, to signal |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3182 | CALL_METHOD that it's not a method call. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3183 | |
| 3184 | NULL | meth | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3185 | */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3186 | SET_TOP(NULL); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3187 | Py_DECREF(obj); |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3188 | PUSH(meth); |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3189 | } |
| 3190 | DISPATCH(); |
| 3191 | } |
| 3192 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3193 | case TARGET(CALL_METHOD): { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3194 | /* Designed to work in tamdem with LOAD_METHOD. */ |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3195 | PyObject **sp, *res, *meth; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3196 | |
| 3197 | sp = stack_pointer; |
| 3198 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3199 | meth = PEEK(oparg + 2); |
| 3200 | if (meth == NULL) { |
| 3201 | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
| 3202 | a method call. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3203 | |
| 3204 | Stack layout: |
| 3205 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3206 | ... | NULL | callable | arg1 | ... | argN |
| 3207 | ^- TOP() |
| 3208 | ^- (-oparg) |
| 3209 | ^- (-oparg-1) |
| 3210 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3211 | |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 3212 | `callable` will be POPed by call_function. |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3213 | NULL will will be POPed manually later. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3214 | */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3215 | res = call_function(&sp, oparg, NULL); |
| 3216 | stack_pointer = sp; |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3217 | (void)POP(); /* POP the NULL. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3218 | } |
| 3219 | else { |
| 3220 | /* This is a method call. Stack layout: |
| 3221 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3222 | ... | method | self | arg1 | ... | argN |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3223 | ^- TOP() |
| 3224 | ^- (-oparg) |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3225 | ^- (-oparg-1) |
| 3226 | ^- (-oparg-2) |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3227 | |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3228 | `self` and `method` will be POPed by call_function. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3229 | We'll be passing `oparg + 1` to call_function, to |
INADA Naoki | 015bce6 | 2017-01-16 17:23:30 +0900 | [diff] [blame] | 3230 | make it accept the `self` as a first argument. |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3231 | */ |
| 3232 | res = call_function(&sp, oparg + 1, NULL); |
| 3233 | stack_pointer = sp; |
| 3234 | } |
| 3235 | |
| 3236 | PUSH(res); |
| 3237 | if (res == NULL) |
| 3238 | goto error; |
| 3239 | DISPATCH(); |
| 3240 | } |
| 3241 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3242 | case TARGET(CALL_FUNCTION): { |
| 3243 | PREDICTED(CALL_FUNCTION); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3244 | PyObject **sp, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3246 | res = call_function(&sp, oparg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3247 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3248 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3249 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3250 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3251 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3252 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3254 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3255 | case TARGET(CALL_FUNCTION_KW): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3256 | PyObject **sp, *res, *names; |
| 3257 | |
| 3258 | names = POP(); |
| 3259 | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | sp = stack_pointer; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3261 | res = call_function(&sp, oparg, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | stack_pointer = sp; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3263 | PUSH(res); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3264 | Py_DECREF(names); |
| 3265 | |
| 3266 | if (res == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3267 | goto error; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3268 | } |
| 3269 | DISPATCH(); |
| 3270 | } |
| 3271 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3272 | case TARGET(CALL_FUNCTION_EX): { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3273 | PyObject *func, *callargs, *kwargs = NULL, *result; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3274 | if (oparg & 0x01) { |
| 3275 | kwargs = POP(); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3276 | if (!PyDict_CheckExact(kwargs)) { |
| 3277 | PyObject *d = PyDict_New(); |
| 3278 | if (d == NULL) |
| 3279 | goto error; |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3280 | if (_PyDict_MergeEx(d, kwargs, 2) < 0) { |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3281 | Py_DECREF(d); |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 3282 | format_kwargs_error(SECOND(), kwargs); |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3283 | Py_DECREF(kwargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3284 | goto error; |
| 3285 | } |
| 3286 | Py_DECREF(kwargs); |
| 3287 | kwargs = d; |
| 3288 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3289 | assert(PyDict_CheckExact(kwargs)); |
| 3290 | } |
| 3291 | callargs = POP(); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3292 | func = TOP(); |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3293 | if (!PyTuple_CheckExact(callargs)) { |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 3294 | if (check_args_iterable(func, callargs) < 0) { |
Victor Stinner | eece222 | 2016-09-12 11:16:37 +0200 | [diff] [blame] | 3295 | Py_DECREF(callargs); |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3296 | goto error; |
| 3297 | } |
| 3298 | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
| 3299 | if (callargs == NULL) { |
| 3300 | goto error; |
| 3301 | } |
| 3302 | } |
Serhiy Storchaka | 63dc548 | 2016-09-22 19:41:20 +0300 | [diff] [blame] | 3303 | assert(PyTuple_CheckExact(callargs)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3304 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3305 | result = do_call_core(func, callargs, kwargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3306 | Py_DECREF(func); |
| 3307 | Py_DECREF(callargs); |
| 3308 | Py_XDECREF(kwargs); |
| 3309 | |
| 3310 | SET_TOP(result); |
| 3311 | if (result == NULL) { |
| 3312 | goto error; |
| 3313 | } |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3314 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3316 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3317 | case TARGET(MAKE_FUNCTION): { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3318 | PyObject *qualname = POP(); |
| 3319 | PyObject *codeobj = POP(); |
| 3320 | PyFunctionObject *func = (PyFunctionObject *) |
| 3321 | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 3322 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3323 | Py_DECREF(codeobj); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3324 | Py_DECREF(qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3325 | if (func == NULL) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3326 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3328 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3329 | if (oparg & 0x08) { |
| 3330 | assert(PyTuple_CheckExact(TOP())); |
| 3331 | func ->func_closure = POP(); |
| 3332 | } |
| 3333 | if (oparg & 0x04) { |
| 3334 | assert(PyDict_CheckExact(TOP())); |
| 3335 | func->func_annotations = POP(); |
| 3336 | } |
| 3337 | if (oparg & 0x02) { |
| 3338 | assert(PyDict_CheckExact(TOP())); |
| 3339 | func->func_kwdefaults = POP(); |
| 3340 | } |
| 3341 | if (oparg & 0x01) { |
| 3342 | assert(PyTuple_CheckExact(TOP())); |
| 3343 | func->func_defaults = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 3345 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 3346 | PUSH((PyObject *)func); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3347 | DISPATCH(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3349 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3350 | case TARGET(BUILD_SLICE): { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3351 | PyObject *start, *stop, *step, *slice; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | if (oparg == 3) |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3353 | step = POP(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | else |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3355 | step = NULL; |
| 3356 | stop = POP(); |
| 3357 | start = TOP(); |
| 3358 | slice = PySlice_New(start, stop, step); |
| 3359 | Py_DECREF(start); |
| 3360 | Py_DECREF(stop); |
| 3361 | Py_XDECREF(step); |
| 3362 | SET_TOP(slice); |
| 3363 | if (slice == NULL) |
| 3364 | goto error; |
| 3365 | DISPATCH(); |
| 3366 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3367 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3368 | case TARGET(FORMAT_VALUE): { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3369 | /* Handles f-string value formatting. */ |
| 3370 | PyObject *result; |
| 3371 | PyObject *fmt_spec; |
| 3372 | PyObject *value; |
| 3373 | PyObject *(*conv_fn)(PyObject *); |
| 3374 | int which_conversion = oparg & FVC_MASK; |
| 3375 | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
| 3376 | |
| 3377 | fmt_spec = have_fmt_spec ? POP() : NULL; |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3378 | value = POP(); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3379 | |
| 3380 | /* See if any conversion is specified. */ |
| 3381 | switch (which_conversion) { |
| 3382 | case FVC_STR: conv_fn = PyObject_Str; break; |
| 3383 | case FVC_REPR: conv_fn = PyObject_Repr; break; |
| 3384 | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
| 3385 | |
| 3386 | /* Must be 0 (meaning no conversion), since only four |
| 3387 | values are allowed by (oparg & FVC_MASK). */ |
| 3388 | default: conv_fn = NULL; break; |
| 3389 | } |
| 3390 | |
| 3391 | /* If there's a conversion function, call it and replace |
| 3392 | value with that result. Otherwise, just use value, |
| 3393 | without conversion. */ |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3394 | if (conv_fn != NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3395 | result = conv_fn(value); |
| 3396 | Py_DECREF(value); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3397 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3398 | Py_XDECREF(fmt_spec); |
| 3399 | goto error; |
| 3400 | } |
| 3401 | value = result; |
| 3402 | } |
| 3403 | |
| 3404 | /* If value is a unicode object, and there's no fmt_spec, |
| 3405 | then we know the result of format(value) is value |
| 3406 | itself. In that case, skip calling format(). I plan to |
| 3407 | move this optimization in to PyObject_Format() |
| 3408 | itself. */ |
| 3409 | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
| 3410 | /* Do nothing, just transfer ownership to result. */ |
| 3411 | result = value; |
| 3412 | } else { |
| 3413 | /* Actually call format(). */ |
| 3414 | result = PyObject_Format(value, fmt_spec); |
| 3415 | Py_DECREF(value); |
| 3416 | Py_XDECREF(fmt_spec); |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3417 | if (result == NULL) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3418 | goto error; |
Eric V. Smith | eb588a1 | 2016-02-05 18:26:20 -0500 | [diff] [blame] | 3419 | } |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3420 | } |
| 3421 | |
Eric V. Smith | 135d5f4 | 2016-02-05 18:23:08 -0500 | [diff] [blame] | 3422 | PUSH(result); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3423 | DISPATCH(); |
| 3424 | } |
| 3425 | |
Benjamin Peterson | ddd1949 | 2018-09-16 22:38:02 -0700 | [diff] [blame] | 3426 | case TARGET(EXTENDED_ARG): { |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 3427 | int oldoparg = oparg; |
| 3428 | NEXTOPARG(); |
| 3429 | oparg |= oldoparg << 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | goto dispatch_opcode; |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3431 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 3432 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3433 | |
Antoine Pitrou | 042b128 | 2010-08-13 21:15:58 +0000 | [diff] [blame] | 3434 | #if USE_COMPUTED_GOTOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | _unknown_opcode: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 3436 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | default: |
| 3438 | fprintf(stderr, |
| 3439 | "XXX lineno: %d, opcode: %d\n", |
| 3440 | PyFrame_GetLineNumber(f), |
| 3441 | opcode); |
| 3442 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3443 | goto error; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | } /* switch */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3446 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3447 | /* This should never be reached. Every opcode should end with DISPATCH() |
| 3448 | or goto error. */ |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3449 | Py_UNREACHABLE(); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3450 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3451 | error: |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3452 | /* Double-check exception status. */ |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3453 | #ifdef NDEBUG |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3454 | if (!PyErr_Occurred()) |
| 3455 | PyErr_SetString(PyExc_SystemError, |
| 3456 | "error return without exception set"); |
Victor Stinner | 365b693 | 2013-07-12 00:11:58 +0200 | [diff] [blame] | 3457 | #else |
| 3458 | assert(PyErr_Occurred()); |
| 3459 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3460 | |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3461 | /* Log traceback info. */ |
| 3462 | PyTraceBack_Here(f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3463 | |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3464 | if (tstate->c_tracefunc != NULL) |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3465 | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 3466 | tstate, f); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3467 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3468 | exception_unwind: |
| 3469 | /* Unwind stacks if an exception occurred */ |
| 3470 | while (f->f_iblock > 0) { |
| 3471 | /* Pop the current block. */ |
| 3472 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3474 | if (b->b_type == EXCEPT_HANDLER) { |
| 3475 | UNWIND_EXCEPT_HANDLER(b); |
| 3476 | continue; |
| 3477 | } |
| 3478 | UNWIND_BLOCK(b); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3479 | if (b->b_type == SETUP_FINALLY) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | PyObject *exc, *val, *tb; |
| 3481 | int handler = b->b_handler; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3482 | _PyErr_StackItem *exc_info = tstate->exc_info; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | /* Beware, this invalidates all b->b_* fields */ |
| 3484 | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3485 | PUSH(exc_info->exc_traceback); |
| 3486 | PUSH(exc_info->exc_value); |
| 3487 | if (exc_info->exc_type != NULL) { |
| 3488 | PUSH(exc_info->exc_type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | } |
| 3490 | else { |
| 3491 | Py_INCREF(Py_None); |
| 3492 | PUSH(Py_None); |
| 3493 | } |
| 3494 | PyErr_Fetch(&exc, &val, &tb); |
| 3495 | /* Make the raw exception data |
| 3496 | available to the handler, |
| 3497 | so a program can emulate the |
| 3498 | Python main loop. */ |
| 3499 | PyErr_NormalizeException( |
| 3500 | &exc, &val, &tb); |
Victor Stinner | 7eab0d0 | 2013-07-15 21:16:27 +0200 | [diff] [blame] | 3501 | if (tb != NULL) |
| 3502 | PyException_SetTraceback(val, tb); |
| 3503 | else |
| 3504 | PyException_SetTraceback(val, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | Py_INCREF(exc); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3506 | exc_info->exc_type = exc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | Py_INCREF(val); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 3508 | exc_info->exc_value = val; |
| 3509 | exc_info->exc_traceback = tb; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | if (tb == NULL) |
| 3511 | tb = Py_None; |
| 3512 | Py_INCREF(tb); |
| 3513 | PUSH(tb); |
| 3514 | PUSH(val); |
| 3515 | PUSH(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | JUMPTO(handler); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3517 | /* Resume normal execution */ |
| 3518 | goto main_loop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | } |
| 3520 | } /* unwind stack */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3521 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3522 | /* End the loop as we still have an error */ |
| 3523 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | /* Pop remaining stack entries. */ |
| 3527 | while (!EMPTY()) { |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 3528 | PyObject *o = POP(); |
| 3529 | Py_XDECREF(o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3530 | } |
Guido van Rossum | 35974fb | 2001-12-06 21:28:18 +0000 | [diff] [blame] | 3531 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3532 | assert(retval == NULL); |
| 3533 | assert(PyErr_Occurred()); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3534 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3535 | return_or_yield: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | if (tstate->use_tracing) { |
Benjamin Peterson | 51f4616 | 2013-01-23 08:38:47 -0500 | [diff] [blame] | 3537 | if (tstate->c_tracefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3538 | if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
| 3539 | tstate, f, PyTrace_RETURN, retval)) { |
| 3540 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | } |
| 3542 | } |
| 3543 | if (tstate->c_profilefunc) { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3544 | if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, |
| 3545 | tstate, f, PyTrace_RETURN, retval)) { |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3546 | Py_CLEAR(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 3550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | /* pop frame */ |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3552 | exit_eval_frame: |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 3553 | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
| 3554 | dtrace_function_return(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3555 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 3556 | f->f_executing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3557 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3558 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3559 | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3562 | static void |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3563 | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
| 3564 | { |
| 3565 | int err; |
| 3566 | Py_ssize_t len = PyList_GET_SIZE(names); |
| 3567 | PyObject *name_str, *comma, *tail, *tmp; |
| 3568 | |
| 3569 | assert(PyList_CheckExact(names)); |
| 3570 | assert(len >= 1); |
| 3571 | /* Deal with the joys of natural language. */ |
| 3572 | switch (len) { |
| 3573 | case 1: |
| 3574 | name_str = PyList_GET_ITEM(names, 0); |
| 3575 | Py_INCREF(name_str); |
| 3576 | break; |
| 3577 | case 2: |
| 3578 | name_str = PyUnicode_FromFormat("%U and %U", |
| 3579 | PyList_GET_ITEM(names, len - 2), |
| 3580 | PyList_GET_ITEM(names, len - 1)); |
| 3581 | break; |
| 3582 | default: |
| 3583 | tail = PyUnicode_FromFormat(", %U, and %U", |
| 3584 | PyList_GET_ITEM(names, len - 2), |
| 3585 | PyList_GET_ITEM(names, len - 1)); |
Benjamin Peterson | d1ab608 | 2012-06-01 11:18:22 -0700 | [diff] [blame] | 3586 | if (tail == NULL) |
| 3587 | return; |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3588 | /* Chop off the last two objects in the list. This shouldn't actually |
| 3589 | fail, but we can't be too careful. */ |
| 3590 | err = PyList_SetSlice(names, len - 2, len, NULL); |
| 3591 | if (err == -1) { |
| 3592 | Py_DECREF(tail); |
| 3593 | return; |
| 3594 | } |
| 3595 | /* Stitch everything up into a nice comma-separated list. */ |
| 3596 | comma = PyUnicode_FromString(", "); |
| 3597 | if (comma == NULL) { |
| 3598 | Py_DECREF(tail); |
| 3599 | return; |
| 3600 | } |
| 3601 | tmp = PyUnicode_Join(comma, names); |
| 3602 | Py_DECREF(comma); |
| 3603 | if (tmp == NULL) { |
| 3604 | Py_DECREF(tail); |
| 3605 | return; |
| 3606 | } |
| 3607 | name_str = PyUnicode_Concat(tmp, tail); |
| 3608 | Py_DECREF(tmp); |
| 3609 | Py_DECREF(tail); |
| 3610 | break; |
| 3611 | } |
| 3612 | if (name_str == NULL) |
| 3613 | return; |
| 3614 | PyErr_Format(PyExc_TypeError, |
| 3615 | "%U() missing %i required %s argument%s: %U", |
| 3616 | co->co_name, |
| 3617 | len, |
| 3618 | kind, |
| 3619 | len == 1 ? "" : "s", |
| 3620 | name_str); |
| 3621 | Py_DECREF(name_str); |
| 3622 | } |
| 3623 | |
| 3624 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3625 | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3626 | PyObject **fastlocals) |
| 3627 | { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3628 | Py_ssize_t i, j = 0; |
| 3629 | Py_ssize_t start, end; |
| 3630 | int positional = (defcount != -1); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3631 | const char *kind = positional ? "positional" : "keyword-only"; |
| 3632 | PyObject *missing_names; |
| 3633 | |
| 3634 | /* Compute the names of the arguments that are missing. */ |
| 3635 | missing_names = PyList_New(missing); |
| 3636 | if (missing_names == NULL) |
| 3637 | return; |
| 3638 | if (positional) { |
| 3639 | start = 0; |
| 3640 | end = co->co_argcount - defcount; |
| 3641 | } |
| 3642 | else { |
| 3643 | start = co->co_argcount; |
| 3644 | end = start + co->co_kwonlyargcount; |
| 3645 | } |
| 3646 | for (i = start; i < end; i++) { |
| 3647 | if (GETLOCAL(i) == NULL) { |
| 3648 | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3649 | PyObject *name = PyObject_Repr(raw); |
| 3650 | if (name == NULL) { |
| 3651 | Py_DECREF(missing_names); |
| 3652 | return; |
| 3653 | } |
| 3654 | PyList_SET_ITEM(missing_names, j++, name); |
| 3655 | } |
| 3656 | } |
| 3657 | assert(j == missing); |
| 3658 | format_missing(kind, co, missing_names); |
| 3659 | Py_DECREF(missing_names); |
| 3660 | } |
| 3661 | |
| 3662 | static void |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3663 | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
| 3664 | PyObject **fastlocals) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3665 | { |
| 3666 | int plural; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3667 | Py_ssize_t kwonly_given = 0; |
| 3668 | Py_ssize_t i; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3669 | PyObject *sig, *kwonly_sig; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3670 | Py_ssize_t co_argcount = co->co_argcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3671 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3672 | assert((co->co_flags & CO_VARARGS) == 0); |
| 3673 | /* Count missing keyword-only args. */ |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3674 | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
| 3675 | if (GETLOCAL(i) != NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3676 | kwonly_given++; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3677 | } |
| 3678 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3679 | if (defcount) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3680 | Py_ssize_t atleast = co_argcount - defcount; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3681 | plural = 1; |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3682 | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3683 | } |
| 3684 | else { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3685 | plural = (co_argcount != 1); |
| 3686 | sig = PyUnicode_FromFormat("%zd", co_argcount); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3687 | } |
| 3688 | if (sig == NULL) |
| 3689 | return; |
| 3690 | if (kwonly_given) { |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3691 | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
| 3692 | kwonly_sig = PyUnicode_FromFormat(format, |
| 3693 | given != 1 ? "s" : "", |
| 3694 | kwonly_given, |
| 3695 | kwonly_given != 1 ? "s" : ""); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3696 | if (kwonly_sig == NULL) { |
| 3697 | Py_DECREF(sig); |
| 3698 | return; |
| 3699 | } |
| 3700 | } |
| 3701 | else { |
| 3702 | /* This will not fail. */ |
| 3703 | kwonly_sig = PyUnicode_FromString(""); |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3704 | assert(kwonly_sig != NULL); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3705 | } |
| 3706 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3707 | "%U() takes %U positional argument%s but %zd%U %s given", |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3708 | co->co_name, |
| 3709 | sig, |
| 3710 | plural ? "s" : "", |
| 3711 | given, |
| 3712 | kwonly_sig, |
| 3713 | given == 1 && !kwonly_given ? "was" : "were"); |
| 3714 | Py_DECREF(sig); |
| 3715 | Py_DECREF(kwonly_sig); |
| 3716 | } |
| 3717 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3718 | /* This is gonna seem *real weird*, but if you put some other code between |
Marcel Plch | 3a9ccee | 2018-04-06 23:22:04 +0200 | [diff] [blame] | 3719 | PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3720 | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 3721 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 3722 | PyObject * |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3723 | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3724 | PyObject *const *args, Py_ssize_t argcount, |
| 3725 | PyObject *const *kwnames, PyObject *const *kwargs, |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3726 | Py_ssize_t kwcount, int kwstep, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 3727 | PyObject *const *defs, Py_ssize_t defcount, |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 3728 | PyObject *kwdefs, PyObject *closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 3729 | PyObject *name, PyObject *qualname) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3730 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 3731 | PyCodeObject* co = (PyCodeObject*)_co; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3732 | PyFrameObject *f; |
| 3733 | PyObject *retval = NULL; |
| 3734 | PyObject **fastlocals, **freevars; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3735 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3736 | PyObject *x, *u; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3737 | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
| 3738 | Py_ssize_t i, n; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3739 | PyObject *kwdict; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | if (globals == NULL) { |
| 3742 | PyErr_SetString(PyExc_SystemError, |
| 3743 | "PyEval_EvalCodeEx: NULL globals"); |
| 3744 | return NULL; |
| 3745 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3746 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3747 | /* Create the frame */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 3748 | tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3750 | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3751 | if (f == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | return NULL; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3753 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | fastlocals = f->f_localsplus; |
| 3755 | freevars = f->f_localsplus + co->co_nlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3756 | |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3757 | /* Create a dictionary for keyword parameters (**kwags) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3758 | if (co->co_flags & CO_VARKEYWORDS) { |
| 3759 | kwdict = PyDict_New(); |
| 3760 | if (kwdict == NULL) |
| 3761 | goto fail; |
| 3762 | i = total_args; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3763 | if (co->co_flags & CO_VARARGS) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3764 | i++; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3765 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3766 | SETLOCAL(i, kwdict); |
| 3767 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3768 | else { |
| 3769 | kwdict = NULL; |
| 3770 | } |
| 3771 | |
| 3772 | /* Copy positional arguments into local variables */ |
| 3773 | if (argcount > co->co_argcount) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3774 | n = co->co_argcount; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3775 | } |
| 3776 | else { |
| 3777 | n = argcount; |
| 3778 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3779 | for (i = 0; i < n; i++) { |
| 3780 | x = args[i]; |
| 3781 | Py_INCREF(x); |
| 3782 | SETLOCAL(i, x); |
| 3783 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3784 | |
| 3785 | /* Pack other positional arguments into the *args argument */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3786 | if (co->co_flags & CO_VARARGS) { |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 3787 | u = _PyTuple_FromArray(args + n, argcount - n); |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3788 | if (u == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3789 | goto fail; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3790 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3791 | SETLOCAL(total_args, u); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3792 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3793 | |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3794 | /* Handle keyword arguments passed as two strided arrays */ |
| 3795 | kwcount *= kwstep; |
| 3796 | for (i = 0; i < kwcount; i += kwstep) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3797 | PyObject **co_varnames; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3798 | PyObject *keyword = kwnames[i]; |
| 3799 | PyObject *value = kwargs[i]; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3800 | Py_ssize_t j; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3801 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3802 | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
| 3803 | PyErr_Format(PyExc_TypeError, |
| 3804 | "%U() keywords must be strings", |
| 3805 | co->co_name); |
| 3806 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3808 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3809 | /* Speed hack: do raw pointer compares. As names are |
| 3810 | normally interned this should almost always hit. */ |
| 3811 | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
| 3812 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3813 | PyObject *name = co_varnames[j]; |
| 3814 | if (name == keyword) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3815 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3816 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3817 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3818 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3819 | /* Slow fallback, just in case */ |
| 3820 | for (j = 0; j < total_args; j++) { |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3821 | PyObject *name = co_varnames[j]; |
| 3822 | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
| 3823 | if (cmp > 0) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3824 | goto kw_found; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3825 | } |
| 3826 | else if (cmp < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | goto fail; |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3828 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3829 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3830 | |
Victor Stinner | 231d1f3 | 2017-01-11 02:12:06 +0100 | [diff] [blame] | 3831 | assert(j >= total_args); |
| 3832 | if (kwdict == NULL) { |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3833 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3834 | "%U() got an unexpected keyword argument '%S'", |
| 3835 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3836 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3838 | |
Christian Heimes | 0bd447f | 2013-07-20 14:48:10 +0200 | [diff] [blame] | 3839 | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
| 3840 | goto fail; |
| 3841 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3842 | continue; |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3843 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3844 | kw_found: |
| 3845 | if (GETLOCAL(j) != NULL) { |
| 3846 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 6fea7f7 | 2016-08-22 23:17:30 +0200 | [diff] [blame] | 3847 | "%U() got multiple values for argument '%S'", |
| 3848 | co->co_name, keyword); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3849 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3850 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3851 | Py_INCREF(value); |
| 3852 | SETLOCAL(j, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3854 | |
| 3855 | /* Check the number of positional arguments */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3856 | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3857 | too_many_positional(co, argcount, defcount, fastlocals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | goto fail; |
| 3859 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3860 | |
| 3861 | /* Add missing positional arguments (copy default values from defs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3862 | if (argcount < co->co_argcount) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3863 | Py_ssize_t m = co->co_argcount - defcount; |
| 3864 | Py_ssize_t missing = 0; |
| 3865 | for (i = argcount; i < m; i++) { |
| 3866 | if (GETLOCAL(i) == NULL) { |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3867 | missing++; |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3868 | } |
| 3869 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3870 | if (missing) { |
| 3871 | missing_arguments(co, missing, defcount, fastlocals); |
| 3872 | goto fail; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3873 | } |
| 3874 | if (n > m) |
| 3875 | i = n - m; |
| 3876 | else |
| 3877 | i = 0; |
| 3878 | for (; i < defcount; i++) { |
| 3879 | if (GETLOCAL(m+i) == NULL) { |
| 3880 | PyObject *def = defs[i]; |
| 3881 | Py_INCREF(def); |
| 3882 | SETLOCAL(m+i, def); |
| 3883 | } |
| 3884 | } |
| 3885 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3886 | |
| 3887 | /* Add missing keyword arguments (copy default values from kwdefs) */ |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3888 | if (co->co_kwonlyargcount > 0) { |
Victor Stinner | 17061a9 | 2016-08-16 23:39:42 +0200 | [diff] [blame] | 3889 | Py_ssize_t missing = 0; |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3890 | for (i = co->co_argcount; i < total_args; i++) { |
| 3891 | PyObject *name; |
| 3892 | if (GETLOCAL(i) != NULL) |
| 3893 | continue; |
| 3894 | name = PyTuple_GET_ITEM(co->co_varnames, i); |
| 3895 | if (kwdefs != NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3896 | PyObject *def = PyDict_GetItemWithError(kwdefs, name); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3897 | if (def) { |
| 3898 | Py_INCREF(def); |
| 3899 | SETLOCAL(i, def); |
| 3900 | continue; |
| 3901 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 3902 | else if (PyErr_Occurred()) { |
| 3903 | goto fail; |
| 3904 | } |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3905 | } |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 3906 | missing++; |
| 3907 | } |
| 3908 | if (missing) { |
| 3909 | missing_arguments(co, missing, -1, fastlocals); |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 3910 | goto fail; |
| 3911 | } |
| 3912 | } |
| 3913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3914 | /* Allocate and initialize storage for cell vars, and copy free |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3915 | vars into frame. */ |
| 3916 | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3917 | PyObject *c; |
Serhiy Storchaka | 5bb8b91 | 2016-12-16 19:19:02 +0200 | [diff] [blame] | 3918 | Py_ssize_t arg; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3919 | /* Possibly account for the cell variable being an argument. */ |
| 3920 | if (co->co_cell2arg != NULL && |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3921 | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3922 | c = PyCell_New(GETLOCAL(arg)); |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3923 | /* Clear the local copy. */ |
| 3924 | SETLOCAL(arg, NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3925 | } |
| 3926 | else { |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3927 | c = PyCell_New(NULL); |
Guido van Rossum | 6832c81 | 2013-05-10 08:47:42 -0700 | [diff] [blame] | 3928 | } |
Benjamin Peterson | 159ae41 | 2013-05-12 18:16:06 -0500 | [diff] [blame] | 3929 | if (c == NULL) |
| 3930 | goto fail; |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3931 | SETLOCAL(co->co_nlocals + i, c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3932 | } |
Victor Stinner | c702001 | 2016-08-16 23:40:29 +0200 | [diff] [blame] | 3933 | |
| 3934 | /* Copy closure variables to free variables */ |
Benjamin Peterson | 9003760 | 2011-06-25 22:54:45 -0500 | [diff] [blame] | 3935 | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
| 3936 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 3937 | Py_INCREF(o); |
| 3938 | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3940 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3941 | /* Handle generator/coroutine/asynchronous generator */ |
| 3942 | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3943 | PyObject *gen; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3944 | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3945 | int is_coro = co->co_flags & CO_COROUTINE; |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3946 | |
| 3947 | if (is_coro && tstate->in_coroutine_wrapper) { |
| 3948 | assert(coro_wrapper != NULL); |
| 3949 | PyErr_Format(PyExc_RuntimeError, |
| 3950 | "coroutine wrapper %.200R attempted " |
| 3951 | "to recursively wrap %.200R", |
| 3952 | coro_wrapper, |
| 3953 | co); |
| 3954 | goto fail; |
| 3955 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | /* Don't need to keep the reference to f_back, it will be set |
| 3958 | * when the generator is resumed. */ |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 3959 | Py_CLEAR(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 3960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | /* Create a new generator that owns the ready to run frame |
| 3962 | * and return that as the value. */ |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3963 | if (is_coro) { |
| 3964 | gen = PyCoro_New(f, name, qualname); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 3965 | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
| 3966 | gen = PyAsyncGen_New(f, name, qualname); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 3967 | } else { |
| 3968 | gen = PyGen_NewWithQualName(f, name, qualname); |
| 3969 | } |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3970 | if (gen == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3971 | return NULL; |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3972 | } |
INADA Naoki | 9c15776 | 2016-12-26 18:52:46 +0900 | [diff] [blame] | 3973 | |
INADA Naoki | 6a3cedf | 2016-12-26 18:01:46 +0900 | [diff] [blame] | 3974 | _PyObject_GC_TRACK(f); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3975 | |
Yury Selivanov | 94c2263 | 2015-06-04 10:16:51 -0400 | [diff] [blame] | 3976 | if (is_coro && coro_wrapper != NULL) { |
| 3977 | PyObject *wrapped; |
| 3978 | tstate->in_coroutine_wrapper = 1; |
| 3979 | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
| 3980 | tstate->in_coroutine_wrapper = 0; |
| 3981 | return wrapped; |
| 3982 | } |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 3983 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3984 | return gen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3985 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3986 | |
Victor Stinner | 59a7327 | 2016-12-09 18:51:13 +0100 | [diff] [blame] | 3987 | retval = PyEval_EvalFrameEx(f,0); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3988 | |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 3989 | fail: /* Jump here from prelude on failure */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | /* decref'ing the frame can cause __del__ methods to get invoked, |
| 3992 | which can call back into Python. While we're done with the |
| 3993 | current Python frame (f), the associated C stack is still in use, |
| 3994 | so recursion_depth must be boosted for the duration. |
| 3995 | */ |
| 3996 | assert(tstate != NULL); |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 3997 | if (Py_REFCNT(f) > 1) { |
| 3998 | Py_DECREF(f); |
| 3999 | _PyObject_GC_TRACK(f); |
| 4000 | } |
| 4001 | else { |
| 4002 | ++tstate->recursion_depth; |
| 4003 | Py_DECREF(f); |
| 4004 | --tstate->recursion_depth; |
| 4005 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4006 | return retval; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4009 | PyObject * |
| 4010 | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 4011 | PyObject *const *args, int argcount, |
| 4012 | PyObject *const *kws, int kwcount, |
| 4013 | PyObject *const *defs, int defcount, |
| 4014 | PyObject *kwdefs, PyObject *closure) |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4015 | { |
| 4016 | return _PyEval_EvalCodeWithName(_co, globals, locals, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4017 | args, argcount, |
Zackery Spytz | c6ea897 | 2017-07-31 08:24:37 -0600 | [diff] [blame] | 4018 | kws, kws != NULL ? kws + 1 : NULL, |
| 4019 | kwcount, 2, |
Victor Stinner | 9be7e7b | 2016-08-19 16:11:43 +0200 | [diff] [blame] | 4020 | defs, defcount, |
| 4021 | kwdefs, closure, |
Victor Stinner | 40ee301 | 2014-06-16 15:59:28 +0200 | [diff] [blame] | 4022 | NULL, NULL); |
| 4023 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4024 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4025 | static PyObject * |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4026 | special_lookup(PyObject *o, _Py_Identifier *id) |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4028 | PyObject *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4029 | res = _PyObject_LookupSpecial(o, id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | if (res == NULL && !PyErr_Occurred()) { |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 4031 | PyErr_SetObject(PyExc_AttributeError, id->object); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | return NULL; |
| 4033 | } |
| 4034 | return res; |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4038 | /* Logic for the raise statement (too complicated for inlining). |
| 4039 | This *consumes* a reference count to each of its arguments. */ |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4040 | static int |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4041 | do_raise(PyObject *exc, PyObject *cause) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | PyObject *type = NULL, *value = NULL; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | if (exc == NULL) { |
| 4046 | /* Reraise */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4047 | PyThreadState *tstate = _PyThreadState_GET(); |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4048 | _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | PyObject *tb; |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 4050 | type = exc_info->exc_type; |
| 4051 | value = exc_info->exc_value; |
| 4052 | tb = exc_info->exc_traceback; |
Victor Stinner | eec9331 | 2016-08-18 18:13:10 +0200 | [diff] [blame] | 4053 | if (type == Py_None || type == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | PyErr_SetString(PyExc_RuntimeError, |
| 4055 | "No active exception to reraise"); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4056 | return 0; |
| 4057 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | Py_XINCREF(type); |
| 4059 | Py_XINCREF(value); |
| 4060 | Py_XINCREF(tb); |
| 4061 | PyErr_Restore(type, value, tb); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4062 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4063 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | /* We support the following forms of raise: |
| 4066 | raise |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4067 | raise <instance> |
| 4068 | raise <type> */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4070 | if (PyExceptionClass_Check(exc)) { |
| 4071 | type = exc; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4072 | value = _PyObject_CallNoArg(exc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4073 | if (value == NULL) |
| 4074 | goto raise_error; |
Benjamin Peterson | 5afa03a | 2011-07-15 14:09:26 -0500 | [diff] [blame] | 4075 | if (!PyExceptionInstance_Check(value)) { |
| 4076 | PyErr_Format(PyExc_TypeError, |
| 4077 | "calling %R should have returned an instance of " |
| 4078 | "BaseException, not %R", |
| 4079 | type, Py_TYPE(value)); |
| 4080 | goto raise_error; |
| 4081 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | } |
| 4083 | else if (PyExceptionInstance_Check(exc)) { |
| 4084 | value = exc; |
| 4085 | type = PyExceptionInstance_Class(exc); |
| 4086 | Py_INCREF(type); |
| 4087 | } |
| 4088 | else { |
| 4089 | /* Not something you can raise. You get an exception |
| 4090 | anyway, just not what you specified :-) */ |
| 4091 | Py_DECREF(exc); |
| 4092 | PyErr_SetString(PyExc_TypeError, |
| 4093 | "exceptions must derive from BaseException"); |
| 4094 | goto raise_error; |
| 4095 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4096 | |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4097 | assert(type != NULL); |
| 4098 | assert(value != NULL); |
| 4099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4100 | if (cause) { |
| 4101 | PyObject *fixed_cause; |
| 4102 | if (PyExceptionClass_Check(cause)) { |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 4103 | fixed_cause = _PyObject_CallNoArg(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | if (fixed_cause == NULL) |
| 4105 | goto raise_error; |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4106 | Py_DECREF(cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4107 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4108 | else if (PyExceptionInstance_Check(cause)) { |
| 4109 | fixed_cause = cause; |
| 4110 | } |
| 4111 | else if (cause == Py_None) { |
| 4112 | Py_DECREF(cause); |
| 4113 | fixed_cause = NULL; |
| 4114 | } |
| 4115 | else { |
| 4116 | PyErr_SetString(PyExc_TypeError, |
| 4117 | "exception causes must derive from " |
| 4118 | "BaseException"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4119 | goto raise_error; |
| 4120 | } |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4121 | PyException_SetCause(value, fixed_cause); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4122 | } |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4124 | PyErr_SetObject(type, value); |
| 4125 | /* PyErr_SetObject incref's its arguments */ |
Serhiy Storchaka | c019158 | 2016-09-27 11:37:10 +0300 | [diff] [blame] | 4126 | Py_DECREF(value); |
| 4127 | Py_DECREF(type); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4128 | return 0; |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 4129 | |
| 4130 | raise_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4131 | Py_XDECREF(value); |
| 4132 | Py_XDECREF(type); |
| 4133 | Py_XDECREF(cause); |
Benjamin Peterson | 31a58ff | 2012-10-12 11:34:51 -0400 | [diff] [blame] | 4134 | return 0; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4137 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4138 | sp). Return 1 for success, 0 if error. |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 4139 | |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4140 | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
| 4141 | with a variable target. |
| 4142 | */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4143 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4144 | static int |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4145 | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | int i = 0, j = 0; |
| 4148 | Py_ssize_t ll = 0; |
| 4149 | PyObject *it; /* iter(v) */ |
| 4150 | PyObject *w; |
| 4151 | PyObject *l = NULL; /* variable list */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4153 | assert(v != NULL); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | it = PyObject_GetIter(v); |
Serhiy Storchaka | 13a6c09 | 2017-12-26 12:30:41 +0200 | [diff] [blame] | 4156 | if (it == NULL) { |
| 4157 | if (PyErr_ExceptionMatches(PyExc_TypeError) && |
| 4158 | v->ob_type->tp_iter == NULL && !PySequence_Check(v)) |
| 4159 | { |
| 4160 | PyErr_Format(PyExc_TypeError, |
| 4161 | "cannot unpack non-iterable %.200s object", |
| 4162 | v->ob_type->tp_name); |
| 4163 | } |
| 4164 | return 0; |
| 4165 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | for (; i < argcnt; i++) { |
| 4168 | w = PyIter_Next(it); |
| 4169 | if (w == NULL) { |
| 4170 | /* Iterator done, via error or exhaustion. */ |
| 4171 | if (!PyErr_Occurred()) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4172 | if (argcntafter == -1) { |
| 4173 | PyErr_Format(PyExc_ValueError, |
| 4174 | "not enough values to unpack (expected %d, got %d)", |
| 4175 | argcnt, i); |
| 4176 | } |
| 4177 | else { |
| 4178 | PyErr_Format(PyExc_ValueError, |
| 4179 | "not enough values to unpack " |
| 4180 | "(expected at least %d, got %d)", |
| 4181 | argcnt + argcntafter, i); |
| 4182 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4183 | } |
| 4184 | goto Error; |
| 4185 | } |
| 4186 | *--sp = w; |
| 4187 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4189 | if (argcntafter == -1) { |
| 4190 | /* We better have exhausted the iterator now. */ |
| 4191 | w = PyIter_Next(it); |
| 4192 | if (w == NULL) { |
| 4193 | if (PyErr_Occurred()) |
| 4194 | goto Error; |
| 4195 | Py_DECREF(it); |
| 4196 | return 1; |
| 4197 | } |
| 4198 | Py_DECREF(w); |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4199 | PyErr_Format(PyExc_ValueError, |
| 4200 | "too many values to unpack (expected %d)", |
| 4201 | argcnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | goto Error; |
| 4203 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | l = PySequence_List(it); |
| 4206 | if (l == NULL) |
| 4207 | goto Error; |
| 4208 | *--sp = l; |
| 4209 | i++; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | ll = PyList_GET_SIZE(l); |
| 4212 | if (ll < argcntafter) { |
R David Murray | 4171bbe | 2015-04-15 17:08:45 -0400 | [diff] [blame] | 4213 | PyErr_Format(PyExc_ValueError, |
| 4214 | "not enough values to unpack (expected at least %d, got %zd)", |
| 4215 | argcnt + argcntafter, argcnt + ll); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | goto Error; |
| 4217 | } |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | /* Pop the "after-variable" args off the list. */ |
| 4220 | for (j = argcntafter; j > 0; j--, i++) { |
| 4221 | *--sp = PyList_GET_ITEM(l, ll - j); |
| 4222 | } |
| 4223 | /* Resize the list. */ |
| 4224 | Py_SIZE(l) = ll - argcntafter; |
| 4225 | Py_DECREF(it); |
| 4226 | return 1; |
Guido van Rossum | 0368b72 | 2007-05-11 16:50:42 +0000 | [diff] [blame] | 4227 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 4228 | Error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | for (; i > 0; i--, sp++) |
| 4230 | Py_DECREF(*sp); |
| 4231 | Py_XDECREF(it); |
| 4232 | return 0; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4236 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4237 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4238 | prtrace(PyObject *v, const char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4240 | printf("%s ", str); |
| 4241 | if (PyObject_Print(v, stdout, 0) != 0) |
| 4242 | PyErr_Clear(); /* Don't know what else to do */ |
| 4243 | printf("\n"); |
| 4244 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4245 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4246 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4247 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4248 | static void |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4249 | call_exc_trace(Py_tracefunc func, PyObject *self, |
| 4250 | PyThreadState *tstate, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4251 | { |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4252 | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4253 | int err; |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4254 | PyErr_Fetch(&type, &value, &orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | if (value == NULL) { |
| 4256 | value = Py_None; |
| 4257 | Py_INCREF(value); |
| 4258 | } |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4259 | PyErr_NormalizeException(&type, &value, &orig_traceback); |
| 4260 | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | arg = PyTuple_Pack(3, type, value, traceback); |
| 4262 | if (arg == NULL) { |
Antoine Pitrou | 8933521 | 2013-11-23 14:05:23 +0100 | [diff] [blame] | 4263 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | return; |
| 4265 | } |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4266 | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4267 | Py_DECREF(arg); |
| 4268 | if (err == 0) |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4269 | PyErr_Restore(type, value, orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4270 | else { |
| 4271 | Py_XDECREF(type); |
| 4272 | Py_XDECREF(value); |
Victor Stinner | aaa8ed8 | 2013-07-10 13:57:55 +0200 | [diff] [blame] | 4273 | Py_XDECREF(orig_traceback); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4274 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Amaury Forgeot d'Arc | f05149a | 2007-11-13 01:05:30 +0000 | [diff] [blame] | 4277 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4278 | call_trace_protected(Py_tracefunc func, PyObject *obj, |
| 4279 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4280 | int what, PyObject *arg) |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4282 | PyObject *type, *value, *traceback; |
| 4283 | int err; |
| 4284 | PyErr_Fetch(&type, &value, &traceback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4285 | err = call_trace(func, obj, tstate, frame, what, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | if (err == 0) |
| 4287 | { |
| 4288 | PyErr_Restore(type, value, traceback); |
| 4289 | return 0; |
| 4290 | } |
| 4291 | else { |
| 4292 | Py_XDECREF(type); |
| 4293 | Py_XDECREF(value); |
| 4294 | Py_XDECREF(traceback); |
| 4295 | return -1; |
| 4296 | } |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 4299 | static int |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4300 | call_trace(Py_tracefunc func, PyObject *obj, |
| 4301 | PyThreadState *tstate, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | int result; |
| 4305 | if (tstate->tracing) |
| 4306 | return 0; |
| 4307 | tstate->tracing++; |
| 4308 | tstate->use_tracing = 0; |
| 4309 | result = func(obj, frame, what, arg); |
| 4310 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4311 | || (tstate->c_profilefunc != NULL)); |
| 4312 | tstate->tracing--; |
| 4313 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4316 | PyObject * |
| 4317 | _PyEval_CallTracing(PyObject *func, PyObject *args) |
| 4318 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4319 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | int save_tracing = tstate->tracing; |
| 4321 | int save_use_tracing = tstate->use_tracing; |
| 4322 | PyObject *result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4324 | tstate->tracing = 0; |
| 4325 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 4326 | || (tstate->c_profilefunc != NULL)); |
| 4327 | result = PyObject_Call(func, args, NULL); |
| 4328 | tstate->tracing = save_tracing; |
| 4329 | tstate->use_tracing = save_use_tracing; |
| 4330 | return result; |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 4333 | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4334 | static int |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4335 | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4336 | PyThreadState *tstate, PyFrameObject *frame, |
| 4337 | int *instr_lb, int *instr_ub, int *instr_prev) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4339 | int result = 0; |
| 4340 | int line = frame->f_lineno; |
Michael W. Hudson | 006c752 | 2002-11-08 13:08:46 +0000 | [diff] [blame] | 4341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | /* If the last instruction executed isn't in the current |
| 4343 | instruction window, reset the window. |
| 4344 | */ |
| 4345 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 4346 | PyAddrPair bounds; |
| 4347 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 4348 | &bounds); |
| 4349 | *instr_lb = bounds.ap_lower; |
| 4350 | *instr_ub = bounds.ap_upper; |
| 4351 | } |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4352 | /* If the last instruction falls at the start of a line or if it |
| 4353 | represents a jump backwards, update the frame's line number and |
| 4354 | then call the trace function if we're tracing source lines. |
| 4355 | */ |
| 4356 | if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | frame->f_lineno = line; |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 4358 | if (frame->f_trace_lines) { |
| 4359 | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
| 4360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | } |
George King | 20faa68 | 2017-10-18 17:44:22 -0700 | [diff] [blame] | 4362 | /* Always emit an opcode event if we're tracing all opcodes. */ |
| 4363 | if (frame->f_trace_opcodes) { |
| 4364 | result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None); |
| 4365 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | *instr_prev = frame->f_lasti; |
| 4367 | return result; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4370 | void |
| 4371 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4372 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4373 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | PyObject *temp = tstate->c_profileobj; |
| 4375 | Py_XINCREF(arg); |
| 4376 | tstate->c_profilefunc = NULL; |
| 4377 | tstate->c_profileobj = NULL; |
| 4378 | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
| 4379 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
| 4380 | Py_XDECREF(temp); |
| 4381 | tstate->c_profilefunc = func; |
| 4382 | tstate->c_profileobj = arg; |
| 4383 | /* Flag that tracing or profiling is turned on */ |
| 4384 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | void |
| 4388 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 4389 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4390 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | PyObject *temp = tstate->c_traceobj; |
| 4392 | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
| 4393 | Py_XINCREF(arg); |
| 4394 | tstate->c_tracefunc = NULL; |
| 4395 | tstate->c_traceobj = NULL; |
| 4396 | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
| 4397 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
| 4398 | Py_XDECREF(temp); |
| 4399 | tstate->c_tracefunc = func; |
| 4400 | tstate->c_traceobj = arg; |
| 4401 | /* Flag that tracing or profiling is turned on */ |
| 4402 | tstate->use_tracing = ((func != NULL) |
| 4403 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4406 | void |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4407 | _PyEval_SetCoroutineOriginTrackingDepth(int new_depth) |
| 4408 | { |
| 4409 | assert(new_depth >= 0); |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4410 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4411 | tstate->coroutine_origin_tracking_depth = new_depth; |
| 4412 | } |
| 4413 | |
| 4414 | int |
| 4415 | _PyEval_GetCoroutineOriginTrackingDepth(void) |
| 4416 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4417 | PyThreadState *tstate = _PyThreadState_GET(); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 4418 | return tstate->coroutine_origin_tracking_depth; |
| 4419 | } |
| 4420 | |
| 4421 | void |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4422 | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4423 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4424 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4425 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4426 | Py_XINCREF(wrapper); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4427 | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | PyObject * |
Yury Selivanov | d8cf382 | 2015-06-01 12:15:23 -0400 | [diff] [blame] | 4431 | _PyEval_GetCoroutineWrapper(void) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4432 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4433 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4434 | return tstate->coroutine_wrapper; |
| 4435 | } |
| 4436 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4437 | void |
| 4438 | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
| 4439 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4440 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4441 | |
| 4442 | Py_XINCREF(firstiter); |
| 4443 | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
| 4444 | } |
| 4445 | |
| 4446 | PyObject * |
| 4447 | _PyEval_GetAsyncGenFirstiter(void) |
| 4448 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4449 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4450 | return tstate->async_gen_firstiter; |
| 4451 | } |
| 4452 | |
| 4453 | void |
| 4454 | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
| 4455 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4456 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4457 | |
| 4458 | Py_XINCREF(finalizer); |
| 4459 | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
| 4460 | } |
| 4461 | |
| 4462 | PyObject * |
| 4463 | _PyEval_GetAsyncGenFinalizer(void) |
| 4464 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4465 | PyThreadState *tstate = _PyThreadState_GET(); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 4466 | return tstate->async_gen_finalizer; |
| 4467 | } |
| 4468 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4469 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4470 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4473 | if (current_frame == NULL) |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4474 | return _PyInterpreterState_GET_UNSAFE()->builtins; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | else |
| 4476 | return current_frame->f_builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 4479 | /* Convenience function to get a builtin from its name */ |
| 4480 | PyObject * |
| 4481 | _PyEval_GetBuiltinId(_Py_Identifier *name) |
| 4482 | { |
| 4483 | PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); |
| 4484 | if (attr) { |
| 4485 | Py_INCREF(attr); |
| 4486 | } |
| 4487 | else if (!PyErr_Occurred()) { |
| 4488 | PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name)); |
| 4489 | } |
| 4490 | return attr; |
| 4491 | } |
| 4492 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4493 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4494 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | PyFrameObject *current_frame = PyEval_GetFrame(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4497 | if (current_frame == NULL) { |
| 4498 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
| 4503 | return NULL; |
| 4504 | |
| 4505 | assert(current_frame->f_locals != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | return current_frame->f_locals; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4509 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4510 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4513 | if (current_frame == NULL) |
| 4514 | return NULL; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 4515 | |
| 4516 | assert(current_frame->f_globals != NULL); |
| 4517 | return current_frame->f_globals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 4520 | PyFrameObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4521 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4522 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4523 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | return _PyThreadState_GetFrame(tstate); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 4527 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4528 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | PyFrameObject *current_frame = PyEval_GetFrame(); |
| 4531 | int result = cf->cf_flags != 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 4532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | if (current_frame != NULL) { |
| 4534 | const int codeflags = current_frame->f_code->co_flags; |
| 4535 | const int compilerflags = codeflags & PyCF_MASK; |
| 4536 | if (compilerflags) { |
| 4537 | result = 1; |
| 4538 | cf->cf_flags |= compilerflags; |
| 4539 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4540 | #if 0 /* future keyword */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | if (codeflags & CO_GENERATOR_ALLOWED) { |
| 4542 | result = 1; |
| 4543 | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
| 4544 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 4545 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | } |
| 4547 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4550 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4551 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4552 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | if (PyMethod_Check(func)) |
| 4555 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
| 4556 | else if (PyFunction_Check(func)) |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 4557 | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | else if (PyCFunction_Check(func)) |
| 4559 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 4560 | else |
| 4561 | return func->ob_type->tp_name; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4564 | const char * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4565 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | if (PyMethod_Check(func)) |
| 4568 | return "()"; |
| 4569 | else if (PyFunction_Check(func)) |
| 4570 | return "()"; |
| 4571 | else if (PyCFunction_Check(func)) |
| 4572 | return "()"; |
| 4573 | else |
| 4574 | return " object"; |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Armin Rigo | 1c2d7e5 | 2005-09-20 18:34:01 +0000 | [diff] [blame] | 4577 | #define C_TRACE(x, call) \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4578 | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4579 | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
| 4580 | tstate, tstate->frame, \ |
| 4581 | PyTrace_C_CALL, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4582 | x = NULL; \ |
| 4583 | } \ |
| 4584 | else { \ |
| 4585 | x = call; \ |
| 4586 | if (tstate->c_profilefunc != NULL) { \ |
| 4587 | if (x == NULL) { \ |
| 4588 | call_trace_protected(tstate->c_profilefunc, \ |
| 4589 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4590 | tstate, tstate->frame, \ |
| 4591 | PyTrace_C_EXCEPTION, func); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | /* XXX should pass (type, value, tb) */ \ |
| 4593 | } else { \ |
| 4594 | if (call_trace(tstate->c_profilefunc, \ |
| 4595 | tstate->c_profileobj, \ |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4596 | tstate, tstate->frame, \ |
| 4597 | PyTrace_C_RETURN, func)) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | Py_DECREF(x); \ |
| 4599 | x = NULL; \ |
| 4600 | } \ |
| 4601 | } \ |
| 4602 | } \ |
| 4603 | } \ |
Nicholas Bastin | d858a77 | 2004-06-25 23:31:06 +0000 | [diff] [blame] | 4604 | } else { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4605 | x = call; \ |
| 4606 | } |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 4607 | |
Victor Stinner | 415c510 | 2017-01-11 00:54:57 +0100 | [diff] [blame] | 4608 | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
| 4609 | to reduce the stack consumption. */ |
| 4610 | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
Benjamin Peterson | 4fd64b9 | 2016-09-09 14:57:58 -0700 | [diff] [blame] | 4611 | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4612 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4613 | PyObject **pfunc = (*pp_stack) - oparg - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | PyObject *func = *pfunc; |
| 4615 | PyObject *x, *w; |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4616 | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
| 4617 | Py_ssize_t nargs = oparg - nkwargs; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4618 | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4620 | /* Always dispatch PyCFunction first, because these are |
| 4621 | presumed to be the most frequent callable object. |
| 4622 | */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4623 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4624 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4625 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4626 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4627 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4628 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | 56868f9 | 2018-07-21 10:30:59 +0200 | [diff] [blame] | 4629 | if (nargs > 0 && tstate->use_tracing) { |
| 4630 | /* We need to create a temporary bound method as argument |
| 4631 | for profiling. |
| 4632 | |
| 4633 | If nargs == 0, then this cannot work because we have no |
| 4634 | "self". In any case, the call itself would raise |
| 4635 | TypeError (foo needs an argument), so we just skip |
| 4636 | profiling. */ |
| 4637 | PyObject *self = stack[0]; |
| 4638 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4639 | if (func != NULL) { |
| 4640 | C_TRACE(x, _PyCFunction_FastCallKeywords(func, |
| 4641 | stack+1, nargs-1, |
| 4642 | kwnames)); |
| 4643 | Py_DECREF(func); |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4644 | } |
jdemeyer | 147d955 | 2018-07-23 18:41:20 +0200 | [diff] [blame] | 4645 | else { |
| 4646 | x = NULL; |
| 4647 | } |
INADA Naoki | 93fac8d | 2017-03-07 14:24:37 +0900 | [diff] [blame] | 4648 | } |
| 4649 | else { |
| 4650 | x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames); |
| 4651 | } |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4652 | } |
Victor Stinner | 4a7cc88 | 2015-03-06 23:35:27 +0100 | [diff] [blame] | 4653 | else { |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4654 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
Victor Stinner | b69ee8c | 2016-11-28 18:32:31 +0100 | [diff] [blame] | 4655 | /* Optimize access to bound methods. Reuse the Python stack |
| 4656 | to pass 'self' as the first argument, replace 'func' |
| 4657 | with 'self'. It avoids the creation of a new temporary tuple |
| 4658 | for arguments (to replace func with self) when the method uses |
| 4659 | FASTCALL. */ |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4660 | PyObject *self = PyMethod_GET_SELF(func); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4661 | Py_INCREF(self); |
| 4662 | func = PyMethod_GET_FUNCTION(func); |
| 4663 | Py_INCREF(func); |
| 4664 | Py_SETREF(*pfunc, self); |
| 4665 | nargs++; |
INADA Naoki | 5566bbb | 2017-02-03 07:43:03 +0900 | [diff] [blame] | 4666 | stack--; |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4667 | } |
| 4668 | else { |
| 4669 | Py_INCREF(func); |
| 4670 | } |
Victor Stinner | d873572 | 2016-09-09 12:36:44 -0700 | [diff] [blame] | 4671 | |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4672 | if (PyFunction_Check(func)) { |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4673 | x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames); |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4674 | } |
| 4675 | else { |
| 4676 | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4677 | } |
Victor Stinner | ae8b69c | 2016-09-09 14:07:44 -0700 | [diff] [blame] | 4678 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | } |
Tim Peters | 8a5c3c7 | 2004-04-05 19:36:21 +0000 | [diff] [blame] | 4680 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4681 | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
| 4682 | |
Victor Stinner | c22bfaa | 2017-02-12 19:27:05 +0100 | [diff] [blame] | 4683 | /* Clear the stack of the function object. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4684 | while ((*pp_stack) > pfunc) { |
| 4685 | w = EXT_POP(*pp_stack); |
| 4686 | Py_DECREF(w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | } |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 4688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4689 | return x; |
Jeremy Hylton | e8c0432 | 2002-08-16 17:47:26 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4692 | static PyObject * |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4693 | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4694 | { |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4695 | PyObject *result; |
| 4696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | if (PyCFunction_Check(func)) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4698 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4700 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4702 | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4703 | PyThreadState *tstate = _PyThreadState_GET(); |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4704 | Py_ssize_t nargs = PyTuple_GET_SIZE(callargs); |
| 4705 | if (nargs > 0 && tstate->use_tracing) { |
| 4706 | /* We need to create a temporary bound method as argument |
| 4707 | for profiling. |
| 4708 | |
| 4709 | If nargs == 0, then this cannot work because we have no |
| 4710 | "self". In any case, the call itself would raise |
| 4711 | TypeError (foo needs an argument), so we just skip |
| 4712 | profiling. */ |
| 4713 | PyObject *self = PyTuple_GET_ITEM(callargs, 0); |
| 4714 | func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self)); |
| 4715 | if (func == NULL) { |
| 4716 | return NULL; |
| 4717 | } |
| 4718 | |
| 4719 | C_TRACE(result, _PyCFunction_FastCallDict(func, |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4720 | &_PyTuple_ITEMS(callargs)[1], |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4721 | nargs - 1, |
| 4722 | kwdict)); |
| 4723 | Py_DECREF(func); |
| 4724 | return result; |
| 4725 | } |
Victor Stinner | 74319ae | 2016-08-25 00:04:09 +0200 | [diff] [blame] | 4726 | } |
jdemeyer | e89de73 | 2018-09-19 12:06:20 +0200 | [diff] [blame] | 4727 | return PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 4730 | /* Extract a slice index from a PyLong or an object with the |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 4731 | nb_index slot defined, and store in *pi. |
| 4732 | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 4733 | and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN. |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 4734 | Return 0 on error, 1 on success. |
Tim Peters | cb479e7 | 2001-12-16 19:11:44 +0000 | [diff] [blame] | 4735 | */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 4736 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4737 | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4738 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4739 | if (v != Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | Py_ssize_t x; |
| 4741 | if (PyIndex_Check(v)) { |
| 4742 | x = PyNumber_AsSsize_t(v, NULL); |
| 4743 | if (x == -1 && PyErr_Occurred()) |
| 4744 | return 0; |
| 4745 | } |
| 4746 | else { |
| 4747 | PyErr_SetString(PyExc_TypeError, |
| 4748 | "slice indices must be integers or " |
| 4749 | "None or have an __index__ method"); |
| 4750 | return 0; |
| 4751 | } |
| 4752 | *pi = x; |
| 4753 | } |
| 4754 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4757 | int |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4758 | _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi) |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4759 | { |
Serhiy Storchaka | d4edfc9 | 2017-03-30 18:29:23 +0300 | [diff] [blame] | 4760 | Py_ssize_t x; |
| 4761 | if (PyIndex_Check(v)) { |
| 4762 | x = PyNumber_AsSsize_t(v, NULL); |
| 4763 | if (x == -1 && PyErr_Occurred()) |
| 4764 | return 0; |
| 4765 | } |
| 4766 | else { |
| 4767 | PyErr_SetString(PyExc_TypeError, |
| 4768 | "slice indices must be integers or " |
| 4769 | "have an __index__ method"); |
| 4770 | return 0; |
| 4771 | } |
| 4772 | *pi = x; |
| 4773 | return 1; |
Serhiy Storchaka | 80ec836 | 2017-03-19 19:37:40 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 4777 | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | "BaseException is not allowed" |
Brett Cannon | f74225d | 2007-02-26 21:10:16 +0000 | [diff] [blame] | 4779 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 4780 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 4781 | cmp_outcome(int op, PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4783 | int res = 0; |
| 4784 | switch (op) { |
| 4785 | case PyCmp_IS: |
| 4786 | res = (v == w); |
| 4787 | break; |
| 4788 | case PyCmp_IS_NOT: |
| 4789 | res = (v != w); |
| 4790 | break; |
| 4791 | case PyCmp_IN: |
| 4792 | res = PySequence_Contains(w, v); |
| 4793 | if (res < 0) |
| 4794 | return NULL; |
| 4795 | break; |
| 4796 | case PyCmp_NOT_IN: |
| 4797 | res = PySequence_Contains(w, v); |
| 4798 | if (res < 0) |
| 4799 | return NULL; |
| 4800 | res = !res; |
| 4801 | break; |
| 4802 | case PyCmp_EXC_MATCH: |
| 4803 | if (PyTuple_Check(w)) { |
| 4804 | Py_ssize_t i, length; |
| 4805 | length = PyTuple_Size(w); |
| 4806 | for (i = 0; i < length; i += 1) { |
| 4807 | PyObject *exc = PyTuple_GET_ITEM(w, i); |
| 4808 | if (!PyExceptionClass_Check(exc)) { |
| 4809 | PyErr_SetString(PyExc_TypeError, |
| 4810 | CANNOT_CATCH_MSG); |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | else { |
| 4816 | if (!PyExceptionClass_Check(w)) { |
| 4817 | PyErr_SetString(PyExc_TypeError, |
| 4818 | CANNOT_CATCH_MSG); |
| 4819 | return NULL; |
| 4820 | } |
| 4821 | } |
| 4822 | res = PyErr_GivenExceptionMatches(v, w); |
| 4823 | break; |
| 4824 | default: |
| 4825 | return PyObject_RichCompare(v, w, op); |
| 4826 | } |
| 4827 | v = res ? Py_True : Py_False; |
| 4828 | Py_INCREF(v); |
| 4829 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4832 | static PyObject * |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4833 | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
| 4834 | { |
| 4835 | _Py_IDENTIFIER(__import__); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4836 | PyObject *import_func, *res; |
| 4837 | PyObject* stack[5]; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4838 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4839 | import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4840 | if (import_func == NULL) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4841 | if (!PyErr_Occurred()) { |
| 4842 | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
| 4843 | } |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4844 | return NULL; |
| 4845 | } |
| 4846 | |
| 4847 | /* Fast path for not overloaded __import__. */ |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 4848 | if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) { |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4849 | int ilevel = _PyLong_AsInt(level); |
| 4850 | if (ilevel == -1 && PyErr_Occurred()) { |
| 4851 | return NULL; |
| 4852 | } |
| 4853 | res = PyImport_ImportModuleLevelObject( |
| 4854 | name, |
| 4855 | f->f_globals, |
| 4856 | f->f_locals == NULL ? Py_None : f->f_locals, |
| 4857 | fromlist, |
| 4858 | ilevel); |
| 4859 | return res; |
| 4860 | } |
| 4861 | |
| 4862 | Py_INCREF(import_func); |
Victor Stinner | df142fd | 2016-08-20 00:44:42 +0200 | [diff] [blame] | 4863 | |
| 4864 | stack[0] = name; |
| 4865 | stack[1] = f->f_globals; |
| 4866 | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
| 4867 | stack[3] = fromlist; |
| 4868 | stack[4] = level; |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4869 | res = _PyObject_FastCall(import_func, stack, 5); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 4870 | Py_DECREF(import_func); |
| 4871 | return res; |
| 4872 | } |
| 4873 | |
| 4874 | static PyObject * |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4875 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 4876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4877 | PyObject *x; |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4878 | _Py_IDENTIFIER(__name__); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4879 | PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4880 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4881 | if (_PyObject_LookupAttr(v, name, &x) != 0) { |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4882 | return x; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4883 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4884 | /* Issue #17636: in case this failed because of a circular relative |
| 4885 | import, try to fallback on reading the module directly from |
| 4886 | sys.modules. */ |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4887 | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4888 | if (pkgname == NULL) { |
| 4889 | goto error; |
| 4890 | } |
Oren Milman | 6db7033 | 2017-09-19 14:23:01 +0300 | [diff] [blame] | 4891 | if (!PyUnicode_Check(pkgname)) { |
| 4892 | Py_CLEAR(pkgname); |
| 4893 | goto error; |
| 4894 | } |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4895 | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4896 | if (fullmodname == NULL) { |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4897 | Py_DECREF(pkgname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4898 | return NULL; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4899 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 4900 | x = PyImport_GetModule(fullmodname); |
Antoine Pitrou | 0373a10 | 2014-10-13 20:19:45 +0200 | [diff] [blame] | 4901 | Py_DECREF(fullmodname); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4902 | if (x == NULL) { |
| 4903 | goto error; |
| 4904 | } |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4905 | Py_DECREF(pkgname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | return x; |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4907 | error: |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4908 | pkgpath = PyModule_GetFilenameObject(v); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4909 | if (pkgname == NULL) { |
| 4910 | pkgname_or_unknown = PyUnicode_FromString("<unknown module name>"); |
| 4911 | if (pkgname_or_unknown == NULL) { |
| 4912 | Py_XDECREF(pkgpath); |
| 4913 | return NULL; |
| 4914 | } |
| 4915 | } else { |
| 4916 | pkgname_or_unknown = pkgname; |
| 4917 | } |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4918 | |
| 4919 | if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) { |
| 4920 | PyErr_Clear(); |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4921 | errmsg = PyUnicode_FromFormat( |
| 4922 | "cannot import name %R from %R (unknown location)", |
| 4923 | name, pkgname_or_unknown |
| 4924 | ); |
| 4925 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4926 | PyErr_SetImportError(errmsg, pkgname, NULL); |
| 4927 | } |
| 4928 | else { |
| 4929 | errmsg = PyUnicode_FromFormat( |
| 4930 | "cannot import name %R from %R (%S)", |
| 4931 | name, pkgname_or_unknown, pkgpath |
| 4932 | ); |
| 4933 | /* NULL check for errmsg done by PyErr_SetImportError. */ |
| 4934 | PyErr_SetImportError(errmsg, pkgname, pkgpath); |
Matthias Bussonnier | bc4bed4 | 2017-02-14 16:05:25 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
Xiang Zhang | 4830f58 | 2017-03-21 11:13:42 +0800 | [diff] [blame] | 4937 | Py_XDECREF(errmsg); |
Matthias Bussonnier | 1bc1564 | 2017-02-22 07:06:50 -0800 | [diff] [blame] | 4938 | Py_XDECREF(pkgname_or_unknown); |
| 4939 | Py_XDECREF(pkgpath); |
Brett Cannon | 3008bc0 | 2015-08-11 18:01:31 -0700 | [diff] [blame] | 4940 | return NULL; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4941 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 4942 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4943 | static int |
| 4944 | import_all_from(PyObject *locals, PyObject *v) |
| 4945 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 4946 | _Py_IDENTIFIER(__all__); |
| 4947 | _Py_IDENTIFIER(__dict__); |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4948 | _Py_IDENTIFIER(__name__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4949 | PyObject *all, *dict, *name, *value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | int skip_leading_underscores = 0; |
| 4951 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 4952 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4953 | if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) { |
| 4954 | return -1; /* Unexpected error */ |
| 4955 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | if (all == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4957 | if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) { |
| 4958 | return -1; |
| 4959 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | if (dict == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | PyErr_SetString(PyExc_ImportError, |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4962 | "from-import-* object has no __dict__ and no __all__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4963 | return -1; |
| 4964 | } |
| 4965 | all = PyMapping_Keys(dict); |
| 4966 | Py_DECREF(dict); |
| 4967 | if (all == NULL) |
| 4968 | return -1; |
| 4969 | skip_leading_underscores = 1; |
| 4970 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 4971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | for (pos = 0, err = 0; ; pos++) { |
| 4973 | name = PySequence_GetItem(all, pos); |
| 4974 | if (name == NULL) { |
| 4975 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 4976 | err = -1; |
| 4977 | else |
| 4978 | PyErr_Clear(); |
| 4979 | break; |
| 4980 | } |
Xiang Zhang | d8b291a | 2018-03-24 18:39:36 +0800 | [diff] [blame] | 4981 | if (!PyUnicode_Check(name)) { |
| 4982 | PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__); |
| 4983 | if (modname == NULL) { |
| 4984 | Py_DECREF(name); |
| 4985 | err = -1; |
| 4986 | break; |
| 4987 | } |
| 4988 | if (!PyUnicode_Check(modname)) { |
| 4989 | PyErr_Format(PyExc_TypeError, |
| 4990 | "module __name__ must be a string, not %.100s", |
| 4991 | Py_TYPE(modname)->tp_name); |
| 4992 | } |
| 4993 | else { |
| 4994 | PyErr_Format(PyExc_TypeError, |
| 4995 | "%s in %U.%s must be str, not %.100s", |
| 4996 | skip_leading_underscores ? "Key" : "Item", |
| 4997 | modname, |
| 4998 | skip_leading_underscores ? "__dict__" : "__all__", |
| 4999 | Py_TYPE(name)->tp_name); |
| 5000 | } |
| 5001 | Py_DECREF(modname); |
| 5002 | Py_DECREF(name); |
| 5003 | err = -1; |
| 5004 | break; |
| 5005 | } |
| 5006 | if (skip_leading_underscores) { |
Serhiy Storchaka | e3b2b4b | 2017-09-08 09:58:51 +0300 | [diff] [blame] | 5007 | if (PyUnicode_READY(name) == -1) { |
| 5008 | Py_DECREF(name); |
| 5009 | err = -1; |
| 5010 | break; |
| 5011 | } |
| 5012 | if (PyUnicode_READ_CHAR(name, 0) == '_') { |
| 5013 | Py_DECREF(name); |
| 5014 | continue; |
| 5015 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | } |
| 5017 | value = PyObject_GetAttr(v, name); |
| 5018 | if (value == NULL) |
| 5019 | err = -1; |
| 5020 | else if (PyDict_CheckExact(locals)) |
| 5021 | err = PyDict_SetItem(locals, name, value); |
| 5022 | else |
| 5023 | err = PyObject_SetItem(locals, name, value); |
| 5024 | Py_DECREF(name); |
| 5025 | Py_XDECREF(value); |
| 5026 | if (err != 0) |
| 5027 | break; |
| 5028 | } |
| 5029 | Py_DECREF(all); |
| 5030 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5033 | static int |
| 5034 | check_args_iterable(PyObject *func, PyObject *args) |
| 5035 | { |
| 5036 | if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) { |
| 5037 | PyErr_Format(PyExc_TypeError, |
| 5038 | "%.200s%.200s argument after * " |
| 5039 | "must be an iterable, not %.200s", |
| 5040 | PyEval_GetFuncName(func), |
| 5041 | PyEval_GetFuncDesc(func), |
| 5042 | args->ob_type->tp_name); |
| 5043 | return -1; |
| 5044 | } |
| 5045 | return 0; |
| 5046 | } |
| 5047 | |
| 5048 | static void |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5049 | format_kwargs_error(PyObject *func, PyObject *kwargs) |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5050 | { |
Serhiy Storchaka | f1ec3ce | 2019-01-12 10:12:24 +0200 | [diff] [blame] | 5051 | /* _PyDict_MergeEx raises attribute |
| 5052 | * error (percolated from an attempt |
| 5053 | * to get 'keys' attribute) instead of |
| 5054 | * a type error if its second argument |
| 5055 | * is not a mapping. |
| 5056 | */ |
| 5057 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5058 | PyErr_Format(PyExc_TypeError, |
| 5059 | "%.200s%.200s argument after ** " |
| 5060 | "must be a mapping, not %.200s", |
| 5061 | PyEval_GetFuncName(func), |
| 5062 | PyEval_GetFuncDesc(func), |
| 5063 | kwargs->ob_type->tp_name); |
| 5064 | } |
| 5065 | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 5066 | PyObject *exc, *val, *tb; |
| 5067 | PyErr_Fetch(&exc, &val, &tb); |
| 5068 | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
| 5069 | PyObject *key = PyTuple_GET_ITEM(val, 0); |
| 5070 | if (!PyUnicode_Check(key)) { |
| 5071 | PyErr_Format(PyExc_TypeError, |
| 5072 | "%.200s%.200s keywords must be strings", |
| 5073 | PyEval_GetFuncName(func), |
| 5074 | PyEval_GetFuncDesc(func)); |
| 5075 | } else { |
| 5076 | PyErr_Format(PyExc_TypeError, |
| 5077 | "%.200s%.200s got multiple " |
| 5078 | "values for keyword argument '%U'", |
| 5079 | PyEval_GetFuncName(func), |
| 5080 | PyEval_GetFuncDesc(func), |
| 5081 | key); |
| 5082 | } |
| 5083 | Py_XDECREF(exc); |
| 5084 | Py_XDECREF(val); |
| 5085 | Py_XDECREF(tb); |
| 5086 | } |
| 5087 | else { |
| 5088 | PyErr_Restore(exc, val, tb); |
| 5089 | } |
| 5090 | } |
Serhiy Storchaka | 25e4f77 | 2017-08-03 11:37:15 +0300 | [diff] [blame] | 5091 | } |
| 5092 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 5093 | static void |
Neal Norwitz | da059e3 | 2007-08-26 05:33:45 +0000 | [diff] [blame] | 5094 | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | const char *obj_str; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | if (!obj) |
| 5099 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5100 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 5101 | obj_str = PyUnicode_AsUTF8(obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | if (!obj_str) |
| 5103 | return; |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | PyErr_Format(exc, format_str, obj_str); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 5106 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5107 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 5108 | static void |
| 5109 | format_exc_unbound(PyCodeObject *co, int oparg) |
| 5110 | { |
| 5111 | PyObject *name; |
| 5112 | /* Don't stomp existing exception */ |
| 5113 | if (PyErr_Occurred()) |
| 5114 | return; |
| 5115 | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
| 5116 | name = PyTuple_GET_ITEM(co->co_cellvars, |
| 5117 | oparg); |
| 5118 | format_exc_check_arg( |
| 5119 | PyExc_UnboundLocalError, |
| 5120 | UNBOUNDLOCAL_ERROR_MSG, |
| 5121 | name); |
| 5122 | } else { |
| 5123 | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
| 5124 | PyTuple_GET_SIZE(co->co_cellvars)); |
| 5125 | format_exc_check_arg(PyExc_NameError, |
| 5126 | UNBOUNDFREE_ERROR_MSG, name); |
| 5127 | } |
| 5128 | } |
| 5129 | |
Serhiy Storchaka | a68f2f0 | 2018-04-03 01:41:38 +0300 | [diff] [blame] | 5130 | static void |
| 5131 | format_awaitable_error(PyTypeObject *type, int prevopcode) |
| 5132 | { |
| 5133 | if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) { |
| 5134 | if (prevopcode == BEFORE_ASYNC_WITH) { |
| 5135 | PyErr_Format(PyExc_TypeError, |
| 5136 | "'async with' received an object from __aenter__ " |
| 5137 | "that does not implement __await__: %.100s", |
| 5138 | type->tp_name); |
| 5139 | } |
| 5140 | else if (prevopcode == WITH_CLEANUP_START) { |
| 5141 | PyErr_Format(PyExc_TypeError, |
| 5142 | "'async with' received an object from __aexit__ " |
| 5143 | "that does not implement __await__: %.100s", |
| 5144 | type->tp_name); |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5149 | static PyObject * |
| 5150 | unicode_concatenate(PyObject *v, PyObject *w, |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5151 | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5152 | { |
| 5153 | PyObject *res; |
| 5154 | if (Py_REFCNT(v) == 2) { |
| 5155 | /* In the common case, there are 2 references to the value |
| 5156 | * stored in 'variable' when the += is performed: one on the |
| 5157 | * value stack (in 'v') and one still stored in the |
| 5158 | * 'variable'. We try to delete the variable now to reduce |
| 5159 | * the refcnt to 1. |
| 5160 | */ |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5161 | int opcode, oparg; |
| 5162 | NEXTOPARG(); |
| 5163 | switch (opcode) { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5164 | case STORE_FAST: |
| 5165 | { |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5166 | PyObject **fastlocals = f->f_localsplus; |
| 5167 | if (GETLOCAL(oparg) == v) |
| 5168 | SETLOCAL(oparg, NULL); |
| 5169 | break; |
| 5170 | } |
| 5171 | case STORE_DEREF: |
| 5172 | { |
| 5173 | PyObject **freevars = (f->f_localsplus + |
| 5174 | f->f_code->co_nlocals); |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5175 | PyObject *c = freevars[oparg]; |
Raymond Hettinger | c32f9db | 2016-11-12 04:10:35 -0500 | [diff] [blame] | 5176 | if (PyCell_GET(c) == v) { |
| 5177 | PyCell_SET(c, NULL); |
| 5178 | Py_DECREF(v); |
| 5179 | } |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5180 | break; |
| 5181 | } |
| 5182 | case STORE_NAME: |
| 5183 | { |
| 5184 | PyObject *names = f->f_code->co_names; |
Serhiy Storchaka | f60bf5f | 2016-05-25 20:02:01 +0300 | [diff] [blame] | 5185 | PyObject *name = GETITEM(names, oparg); |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5186 | PyObject *locals = f->f_locals; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 5187 | if (locals && PyDict_CheckExact(locals)) { |
| 5188 | PyObject *w = PyDict_GetItemWithError(locals, name); |
| 5189 | if ((w == v && PyDict_DelItem(locals, name) != 0) || |
| 5190 | (w == NULL && PyErr_Occurred())) |
| 5191 | { |
| 5192 | Py_DECREF(v); |
| 5193 | return NULL; |
Victor Stinner | d2a915d | 2011-10-02 20:34:20 +0200 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
| 5199 | } |
| 5200 | res = v; |
| 5201 | PyUnicode_Append(&res, w); |
| 5202 | return res; |
| 5203 | } |
| 5204 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5205 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 5206 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 5207 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5208 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5210 | int i; |
| 5211 | PyObject *l = PyList_New(256); |
| 5212 | if (l == NULL) return NULL; |
| 5213 | for (i = 0; i < 256; i++) { |
| 5214 | PyObject *x = PyLong_FromLong(a[i]); |
| 5215 | if (x == NULL) { |
| 5216 | Py_DECREF(l); |
| 5217 | return NULL; |
| 5218 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5219 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | } |
| 5221 | for (i = 0; i < 256; i++) |
| 5222 | a[i] = 0; |
| 5223 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 5227 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5228 | { |
| 5229 | #ifndef DXPAIRS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | return getarray(dxp); |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5231 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5232 | int i; |
| 5233 | PyObject *l = PyList_New(257); |
| 5234 | if (l == NULL) return NULL; |
| 5235 | for (i = 0; i < 257; i++) { |
| 5236 | PyObject *x = getarray(dxpairs[i]); |
| 5237 | if (x == NULL) { |
| 5238 | Py_DECREF(l); |
| 5239 | return NULL; |
| 5240 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 5241 | PyList_SET_ITEM(l, i, x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | } |
| 5243 | return l; |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 5244 | #endif |
| 5245 | } |
| 5246 | |
| 5247 | #endif |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5248 | |
| 5249 | Py_ssize_t |
| 5250 | _PyEval_RequestCodeExtraIndex(freefunc free) |
| 5251 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 5252 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5253 | Py_ssize_t new_index; |
| 5254 | |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5255 | if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5256 | return -1; |
| 5257 | } |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 5258 | new_index = interp->co_extra_user_count++; |
| 5259 | interp->co_extra_freefuncs[new_index] = free; |
Brett Cannon | 5c4de28 | 2016-09-07 11:16:41 -0700 | [diff] [blame] | 5260 | return new_index; |
| 5261 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5262 | |
| 5263 | static void |
| 5264 | dtrace_function_entry(PyFrameObject *f) |
| 5265 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5266 | const char *filename; |
| 5267 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5268 | int lineno; |
| 5269 | |
| 5270 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5271 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5272 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5273 | |
| 5274 | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
| 5275 | } |
| 5276 | |
| 5277 | static void |
| 5278 | dtrace_function_return(PyFrameObject *f) |
| 5279 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5280 | const char *filename; |
| 5281 | const char *funcname; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5282 | int lineno; |
| 5283 | |
| 5284 | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
| 5285 | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
| 5286 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 5287 | |
| 5288 | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
| 5289 | } |
| 5290 | |
| 5291 | /* DTrace equivalent of maybe_call_line_trace. */ |
| 5292 | static void |
| 5293 | maybe_dtrace_line(PyFrameObject *frame, |
| 5294 | int *instr_lb, int *instr_ub, int *instr_prev) |
| 5295 | { |
| 5296 | int line = frame->f_lineno; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 5297 | const char *co_filename, *co_name; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 5298 | |
| 5299 | /* If the last instruction executed isn't in the current |
| 5300 | instruction window, reset the window. |
| 5301 | */ |
| 5302 | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
| 5303 | PyAddrPair bounds; |
| 5304 | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
| 5305 | &bounds); |
| 5306 | *instr_lb = bounds.ap_lower; |
| 5307 | *instr_ub = bounds.ap_upper; |
| 5308 | } |
| 5309 | /* If the last instruction falls at the start of a line or if |
| 5310 | it represents a jump backwards, update the frame's line |
| 5311 | number and call the trace function. */ |
| 5312 | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
| 5313 | frame->f_lineno = line; |
| 5314 | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 5315 | if (!co_filename) |
| 5316 | co_filename = "?"; |
| 5317 | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
| 5318 | if (!co_name) |
| 5319 | co_name = "?"; |
| 5320 | PyDTrace_LINE(co_filename, co_name, line); |
| 5321 | } |
| 5322 | *instr_prev = frame->f_lasti; |
| 5323 | } |