1 | n/a | |
---|
2 | n/a | /* Execute compiled code */ |
---|
3 | n/a | |
---|
4 | n/a | /* XXX TO DO: |
---|
5 | n/a | XXX speed up searching for keywords by using a dictionary |
---|
6 | n/a | XXX document it! |
---|
7 | n/a | */ |
---|
8 | n/a | |
---|
9 | n/a | /* enable more aggressive intra-module optimizations, where available */ |
---|
10 | n/a | #define PY_LOCAL_AGGRESSIVE |
---|
11 | n/a | |
---|
12 | n/a | #include "Python.h" |
---|
13 | n/a | |
---|
14 | n/a | #include "code.h" |
---|
15 | n/a | #include "dictobject.h" |
---|
16 | n/a | #include "frameobject.h" |
---|
17 | n/a | #include "opcode.h" |
---|
18 | n/a | #include "pydtrace.h" |
---|
19 | n/a | #include "setobject.h" |
---|
20 | n/a | #include "structmember.h" |
---|
21 | n/a | |
---|
22 | n/a | #include <ctype.h> |
---|
23 | n/a | |
---|
24 | n/a | /* Turn this on if your compiler chokes on the big switch: */ |
---|
25 | n/a | /* #define CASE_TOO_BIG 1 */ |
---|
26 | n/a | |
---|
27 | n/a | #ifdef Py_DEBUG |
---|
28 | n/a | /* For debugging the interpreter: */ |
---|
29 | n/a | #define LLTRACE 1 /* Low-level trace feature */ |
---|
30 | n/a | #define CHECKEXC 1 /* Double-check exception checking */ |
---|
31 | n/a | #endif |
---|
32 | n/a | |
---|
33 | n/a | /* Private API for the LOAD_METHOD opcode. */ |
---|
34 | n/a | extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **); |
---|
35 | n/a | |
---|
36 | n/a | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
---|
37 | n/a | |
---|
38 | n/a | /* Forward declarations */ |
---|
39 | n/a | Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t, PyObject *); |
---|
40 | n/a | static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *); |
---|
41 | n/a | static PyObject * do_call_core(PyObject *, PyObject *, PyObject *); |
---|
42 | n/a | |
---|
43 | n/a | #ifdef LLTRACE |
---|
44 | n/a | static int lltrace; |
---|
45 | n/a | static int prtrace(PyObject *, const char *); |
---|
46 | n/a | #endif |
---|
47 | n/a | static int call_trace(Py_tracefunc, PyObject *, |
---|
48 | n/a | PyThreadState *, PyFrameObject *, |
---|
49 | n/a | int, PyObject *); |
---|
50 | n/a | static int call_trace_protected(Py_tracefunc, PyObject *, |
---|
51 | n/a | PyThreadState *, PyFrameObject *, |
---|
52 | n/a | int, PyObject *); |
---|
53 | n/a | static void call_exc_trace(Py_tracefunc, PyObject *, |
---|
54 | n/a | PyThreadState *, PyFrameObject *); |
---|
55 | n/a | static int maybe_call_line_trace(Py_tracefunc, PyObject *, |
---|
56 | n/a | PyThreadState *, PyFrameObject *, int *, int *, int *); |
---|
57 | n/a | static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *); |
---|
58 | n/a | static void dtrace_function_entry(PyFrameObject *); |
---|
59 | n/a | static void dtrace_function_return(PyFrameObject *); |
---|
60 | n/a | |
---|
61 | n/a | static PyObject * cmp_outcome(int, PyObject *, PyObject *); |
---|
62 | n/a | static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *); |
---|
63 | n/a | static PyObject * import_from(PyObject *, PyObject *); |
---|
64 | n/a | static int import_all_from(PyObject *, PyObject *); |
---|
65 | n/a | static void format_exc_check_arg(PyObject *, const char *, PyObject *); |
---|
66 | n/a | static void format_exc_unbound(PyCodeObject *co, int oparg); |
---|
67 | n/a | static PyObject * unicode_concatenate(PyObject *, PyObject *, |
---|
68 | n/a | PyFrameObject *, const _Py_CODEUNIT *); |
---|
69 | n/a | static PyObject * special_lookup(PyObject *, _Py_Identifier *); |
---|
70 | n/a | |
---|
71 | n/a | #define NAME_ERROR_MSG \ |
---|
72 | n/a | "name '%.200s' is not defined" |
---|
73 | n/a | #define UNBOUNDLOCAL_ERROR_MSG \ |
---|
74 | n/a | "local variable '%.200s' referenced before assignment" |
---|
75 | n/a | #define UNBOUNDFREE_ERROR_MSG \ |
---|
76 | n/a | "free variable '%.200s' referenced before assignment" \ |
---|
77 | n/a | " in enclosing scope" |
---|
78 | n/a | |
---|
79 | n/a | /* Dynamic execution profile */ |
---|
80 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
---|
81 | n/a | #ifdef DXPAIRS |
---|
82 | n/a | static long dxpairs[257][256]; |
---|
83 | n/a | #define dxp dxpairs[256] |
---|
84 | n/a | #else |
---|
85 | n/a | static long dxp[256]; |
---|
86 | n/a | #endif |
---|
87 | n/a | #endif |
---|
88 | n/a | |
---|
89 | n/a | #ifdef WITH_THREAD |
---|
90 | n/a | #define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request) |
---|
91 | n/a | #else |
---|
92 | n/a | #define GIL_REQUEST 0 |
---|
93 | n/a | #endif |
---|
94 | n/a | |
---|
95 | n/a | /* This can set eval_breaker to 0 even though gil_drop_request became |
---|
96 | n/a | 1. We believe this is all right because the eval loop will release |
---|
97 | n/a | the GIL eventually anyway. */ |
---|
98 | n/a | #define COMPUTE_EVAL_BREAKER() \ |
---|
99 | n/a | _Py_atomic_store_relaxed( \ |
---|
100 | n/a | &eval_breaker, \ |
---|
101 | n/a | GIL_REQUEST | \ |
---|
102 | n/a | _Py_atomic_load_relaxed(&pendingcalls_to_do) | \ |
---|
103 | n/a | pending_async_exc) |
---|
104 | n/a | |
---|
105 | n/a | #ifdef WITH_THREAD |
---|
106 | n/a | |
---|
107 | n/a | #define SET_GIL_DROP_REQUEST() \ |
---|
108 | n/a | do { \ |
---|
109 | n/a | _Py_atomic_store_relaxed(&gil_drop_request, 1); \ |
---|
110 | n/a | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
---|
111 | n/a | } while (0) |
---|
112 | n/a | |
---|
113 | n/a | #define RESET_GIL_DROP_REQUEST() \ |
---|
114 | n/a | do { \ |
---|
115 | n/a | _Py_atomic_store_relaxed(&gil_drop_request, 0); \ |
---|
116 | n/a | COMPUTE_EVAL_BREAKER(); \ |
---|
117 | n/a | } while (0) |
---|
118 | n/a | |
---|
119 | n/a | #endif |
---|
120 | n/a | |
---|
121 | n/a | /* Pending calls are only modified under pending_lock */ |
---|
122 | n/a | #define SIGNAL_PENDING_CALLS() \ |
---|
123 | n/a | do { \ |
---|
124 | n/a | _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \ |
---|
125 | n/a | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
---|
126 | n/a | } while (0) |
---|
127 | n/a | |
---|
128 | n/a | #define UNSIGNAL_PENDING_CALLS() \ |
---|
129 | n/a | do { \ |
---|
130 | n/a | _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \ |
---|
131 | n/a | COMPUTE_EVAL_BREAKER(); \ |
---|
132 | n/a | } while (0) |
---|
133 | n/a | |
---|
134 | n/a | #define SIGNAL_ASYNC_EXC() \ |
---|
135 | n/a | do { \ |
---|
136 | n/a | pending_async_exc = 1; \ |
---|
137 | n/a | _Py_atomic_store_relaxed(&eval_breaker, 1); \ |
---|
138 | n/a | } while (0) |
---|
139 | n/a | |
---|
140 | n/a | #define UNSIGNAL_ASYNC_EXC() \ |
---|
141 | n/a | do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0) |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | #ifdef WITH_THREAD |
---|
145 | n/a | |
---|
146 | n/a | #ifdef HAVE_ERRNO_H |
---|
147 | n/a | #include <errno.h> |
---|
148 | n/a | #endif |
---|
149 | n/a | #include "pythread.h" |
---|
150 | n/a | |
---|
151 | n/a | static PyThread_type_lock pending_lock = 0; /* for pending calls */ |
---|
152 | n/a | static long main_thread = 0; |
---|
153 | n/a | /* This single variable consolidates all requests to break out of the fast path |
---|
154 | n/a | in the eval loop. */ |
---|
155 | n/a | static _Py_atomic_int eval_breaker = {0}; |
---|
156 | n/a | /* Request for dropping the GIL */ |
---|
157 | n/a | static _Py_atomic_int gil_drop_request = {0}; |
---|
158 | n/a | /* Request for running pending calls. */ |
---|
159 | n/a | static _Py_atomic_int pendingcalls_to_do = {0}; |
---|
160 | n/a | /* Request for looking at the `async_exc` field of the current thread state. |
---|
161 | n/a | Guarded by the GIL. */ |
---|
162 | n/a | static int pending_async_exc = 0; |
---|
163 | n/a | |
---|
164 | n/a | #include "ceval_gil.h" |
---|
165 | n/a | |
---|
166 | n/a | int |
---|
167 | n/a | PyEval_ThreadsInitialized(void) |
---|
168 | n/a | { |
---|
169 | n/a | return gil_created(); |
---|
170 | n/a | } |
---|
171 | n/a | |
---|
172 | n/a | void |
---|
173 | n/a | PyEval_InitThreads(void) |
---|
174 | n/a | { |
---|
175 | n/a | if (gil_created()) |
---|
176 | n/a | return; |
---|
177 | n/a | create_gil(); |
---|
178 | n/a | take_gil(PyThreadState_GET()); |
---|
179 | n/a | main_thread = PyThread_get_thread_ident(); |
---|
180 | n/a | if (!pending_lock) |
---|
181 | n/a | pending_lock = PyThread_allocate_lock(); |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | void |
---|
185 | n/a | _PyEval_FiniThreads(void) |
---|
186 | n/a | { |
---|
187 | n/a | if (!gil_created()) |
---|
188 | n/a | return; |
---|
189 | n/a | destroy_gil(); |
---|
190 | n/a | assert(!gil_created()); |
---|
191 | n/a | } |
---|
192 | n/a | |
---|
193 | n/a | void |
---|
194 | n/a | PyEval_AcquireLock(void) |
---|
195 | n/a | { |
---|
196 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
197 | n/a | if (tstate == NULL) |
---|
198 | n/a | Py_FatalError("PyEval_AcquireLock: current thread state is NULL"); |
---|
199 | n/a | take_gil(tstate); |
---|
200 | n/a | } |
---|
201 | n/a | |
---|
202 | n/a | void |
---|
203 | n/a | PyEval_ReleaseLock(void) |
---|
204 | n/a | { |
---|
205 | n/a | /* This function must succeed when the current thread state is NULL. |
---|
206 | n/a | We therefore avoid PyThreadState_GET() which dumps a fatal error |
---|
207 | n/a | in debug mode. |
---|
208 | n/a | */ |
---|
209 | n/a | drop_gil((PyThreadState*)_Py_atomic_load_relaxed( |
---|
210 | n/a | &_PyThreadState_Current)); |
---|
211 | n/a | } |
---|
212 | n/a | |
---|
213 | n/a | void |
---|
214 | n/a | PyEval_AcquireThread(PyThreadState *tstate) |
---|
215 | n/a | { |
---|
216 | n/a | if (tstate == NULL) |
---|
217 | n/a | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
---|
218 | n/a | /* Check someone has called PyEval_InitThreads() to create the lock */ |
---|
219 | n/a | assert(gil_created()); |
---|
220 | n/a | take_gil(tstate); |
---|
221 | n/a | if (PyThreadState_Swap(tstate) != NULL) |
---|
222 | n/a | Py_FatalError( |
---|
223 | n/a | "PyEval_AcquireThread: non-NULL old thread state"); |
---|
224 | n/a | } |
---|
225 | n/a | |
---|
226 | n/a | void |
---|
227 | n/a | PyEval_ReleaseThread(PyThreadState *tstate) |
---|
228 | n/a | { |
---|
229 | n/a | if (tstate == NULL) |
---|
230 | n/a | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
---|
231 | n/a | if (PyThreadState_Swap(NULL) != tstate) |
---|
232 | n/a | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
---|
233 | n/a | drop_gil(tstate); |
---|
234 | n/a | } |
---|
235 | n/a | |
---|
236 | n/a | /* This function is called from PyOS_AfterFork to destroy all threads which are |
---|
237 | n/a | * not running in the child process, and clear internal locks which might be |
---|
238 | n/a | * held by those threads. (This could also be done using pthread_atfork |
---|
239 | n/a | * mechanism, at least for the pthreads implementation.) */ |
---|
240 | n/a | |
---|
241 | n/a | void |
---|
242 | n/a | PyEval_ReInitThreads(void) |
---|
243 | n/a | { |
---|
244 | n/a | _Py_IDENTIFIER(_after_fork); |
---|
245 | n/a | PyObject *threading, *result; |
---|
246 | n/a | PyThreadState *current_tstate = PyThreadState_GET(); |
---|
247 | n/a | |
---|
248 | n/a | if (!gil_created()) |
---|
249 | n/a | return; |
---|
250 | n/a | recreate_gil(); |
---|
251 | n/a | pending_lock = PyThread_allocate_lock(); |
---|
252 | n/a | take_gil(current_tstate); |
---|
253 | n/a | main_thread = PyThread_get_thread_ident(); |
---|
254 | n/a | |
---|
255 | n/a | /* Update the threading module with the new state. |
---|
256 | n/a | */ |
---|
257 | n/a | threading = PyMapping_GetItemString(current_tstate->interp->modules, |
---|
258 | n/a | "threading"); |
---|
259 | n/a | if (threading == NULL) { |
---|
260 | n/a | /* threading not imported */ |
---|
261 | n/a | PyErr_Clear(); |
---|
262 | n/a | return; |
---|
263 | n/a | } |
---|
264 | n/a | result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); |
---|
265 | n/a | if (result == NULL) |
---|
266 | n/a | PyErr_WriteUnraisable(threading); |
---|
267 | n/a | else |
---|
268 | n/a | Py_DECREF(result); |
---|
269 | n/a | Py_DECREF(threading); |
---|
270 | n/a | |
---|
271 | n/a | /* Destroy all threads except the current one */ |
---|
272 | n/a | _PyThreadState_DeleteExcept(current_tstate); |
---|
273 | n/a | } |
---|
274 | n/a | |
---|
275 | n/a | #else |
---|
276 | n/a | static _Py_atomic_int eval_breaker = {0}; |
---|
277 | n/a | static int pending_async_exc = 0; |
---|
278 | n/a | #endif /* WITH_THREAD */ |
---|
279 | n/a | |
---|
280 | n/a | /* This function is used to signal that async exceptions are waiting to be |
---|
281 | n/a | raised, therefore it is also useful in non-threaded builds. */ |
---|
282 | n/a | |
---|
283 | n/a | void |
---|
284 | n/a | _PyEval_SignalAsyncExc(void) |
---|
285 | n/a | { |
---|
286 | n/a | SIGNAL_ASYNC_EXC(); |
---|
287 | n/a | } |
---|
288 | n/a | |
---|
289 | n/a | /* Functions save_thread and restore_thread are always defined so |
---|
290 | n/a | dynamically loaded modules needn't be compiled separately for use |
---|
291 | n/a | with and without threads: */ |
---|
292 | n/a | |
---|
293 | n/a | PyThreadState * |
---|
294 | n/a | PyEval_SaveThread(void) |
---|
295 | n/a | { |
---|
296 | n/a | PyThreadState *tstate = PyThreadState_Swap(NULL); |
---|
297 | n/a | if (tstate == NULL) |
---|
298 | n/a | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
---|
299 | n/a | #ifdef WITH_THREAD |
---|
300 | n/a | if (gil_created()) |
---|
301 | n/a | drop_gil(tstate); |
---|
302 | n/a | #endif |
---|
303 | n/a | return tstate; |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | void |
---|
307 | n/a | PyEval_RestoreThread(PyThreadState *tstate) |
---|
308 | n/a | { |
---|
309 | n/a | if (tstate == NULL) |
---|
310 | n/a | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
---|
311 | n/a | #ifdef WITH_THREAD |
---|
312 | n/a | if (gil_created()) { |
---|
313 | n/a | int err = errno; |
---|
314 | n/a | take_gil(tstate); |
---|
315 | n/a | /* _Py_Finalizing is protected by the GIL */ |
---|
316 | n/a | if (_Py_Finalizing && tstate != _Py_Finalizing) { |
---|
317 | n/a | drop_gil(tstate); |
---|
318 | n/a | PyThread_exit_thread(); |
---|
319 | n/a | assert(0); /* unreachable */ |
---|
320 | n/a | } |
---|
321 | n/a | errno = err; |
---|
322 | n/a | } |
---|
323 | n/a | #endif |
---|
324 | n/a | PyThreadState_Swap(tstate); |
---|
325 | n/a | } |
---|
326 | n/a | |
---|
327 | n/a | |
---|
328 | n/a | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
---|
329 | n/a | signal handlers or Mac I/O completion routines) can schedule calls |
---|
330 | n/a | to a function to be called synchronously. |
---|
331 | n/a | The synchronous function is called with one void* argument. |
---|
332 | n/a | It should return 0 for success or -1 for failure -- failure should |
---|
333 | n/a | be accompanied by an exception. |
---|
334 | n/a | |
---|
335 | n/a | If registry succeeds, the registry function returns 0; if it fails |
---|
336 | n/a | (e.g. due to too many pending calls) it returns -1 (without setting |
---|
337 | n/a | an exception condition). |
---|
338 | n/a | |
---|
339 | n/a | Note that because registry may occur from within signal handlers, |
---|
340 | n/a | or other asynchronous events, calling malloc() is unsafe! |
---|
341 | n/a | |
---|
342 | n/a | #ifdef WITH_THREAD |
---|
343 | n/a | Any thread can schedule pending calls, but only the main thread |
---|
344 | n/a | will execute them. |
---|
345 | n/a | There is no facility to schedule calls to a particular thread, but |
---|
346 | n/a | that should be easy to change, should that ever be required. In |
---|
347 | n/a | that case, the static variables here should go into the python |
---|
348 | n/a | threadstate. |
---|
349 | n/a | #endif |
---|
350 | n/a | */ |
---|
351 | n/a | |
---|
352 | n/a | #ifdef WITH_THREAD |
---|
353 | n/a | |
---|
354 | n/a | /* The WITH_THREAD implementation is thread-safe. It allows |
---|
355 | n/a | scheduling to be made from any thread, and even from an executing |
---|
356 | n/a | callback. |
---|
357 | n/a | */ |
---|
358 | n/a | |
---|
359 | n/a | #define NPENDINGCALLS 32 |
---|
360 | n/a | static struct { |
---|
361 | n/a | int (*func)(void *); |
---|
362 | n/a | void *arg; |
---|
363 | n/a | } pendingcalls[NPENDINGCALLS]; |
---|
364 | n/a | static int pendingfirst = 0; |
---|
365 | n/a | static int pendinglast = 0; |
---|
366 | n/a | |
---|
367 | n/a | int |
---|
368 | n/a | Py_AddPendingCall(int (*func)(void *), void *arg) |
---|
369 | n/a | { |
---|
370 | n/a | int i, j, result=0; |
---|
371 | n/a | PyThread_type_lock lock = pending_lock; |
---|
372 | n/a | |
---|
373 | n/a | /* try a few times for the lock. Since this mechanism is used |
---|
374 | n/a | * for signal handling (on the main thread), there is a (slim) |
---|
375 | n/a | * chance that a signal is delivered on the same thread while we |
---|
376 | n/a | * hold the lock during the Py_MakePendingCalls() function. |
---|
377 | n/a | * This avoids a deadlock in that case. |
---|
378 | n/a | * Note that signals can be delivered on any thread. In particular, |
---|
379 | n/a | * on Windows, a SIGINT is delivered on a system-created worker |
---|
380 | n/a | * thread. |
---|
381 | n/a | * We also check for lock being NULL, in the unlikely case that |
---|
382 | n/a | * this function is called before any bytecode evaluation takes place. |
---|
383 | n/a | */ |
---|
384 | n/a | if (lock != NULL) { |
---|
385 | n/a | for (i = 0; i<100; i++) { |
---|
386 | n/a | if (PyThread_acquire_lock(lock, NOWAIT_LOCK)) |
---|
387 | n/a | break; |
---|
388 | n/a | } |
---|
389 | n/a | if (i == 100) |
---|
390 | n/a | return -1; |
---|
391 | n/a | } |
---|
392 | n/a | |
---|
393 | n/a | i = pendinglast; |
---|
394 | n/a | j = (i + 1) % NPENDINGCALLS; |
---|
395 | n/a | if (j == pendingfirst) { |
---|
396 | n/a | result = -1; /* Queue full */ |
---|
397 | n/a | } else { |
---|
398 | n/a | pendingcalls[i].func = func; |
---|
399 | n/a | pendingcalls[i].arg = arg; |
---|
400 | n/a | pendinglast = j; |
---|
401 | n/a | } |
---|
402 | n/a | /* signal main loop */ |
---|
403 | n/a | SIGNAL_PENDING_CALLS(); |
---|
404 | n/a | if (lock != NULL) |
---|
405 | n/a | PyThread_release_lock(lock); |
---|
406 | n/a | return result; |
---|
407 | n/a | } |
---|
408 | n/a | |
---|
409 | n/a | int |
---|
410 | n/a | Py_MakePendingCalls(void) |
---|
411 | n/a | { |
---|
412 | n/a | static int busy = 0; |
---|
413 | n/a | int i; |
---|
414 | n/a | int r = 0; |
---|
415 | n/a | |
---|
416 | n/a | if (!pending_lock) { |
---|
417 | n/a | /* initial allocation of the lock */ |
---|
418 | n/a | pending_lock = PyThread_allocate_lock(); |
---|
419 | n/a | if (pending_lock == NULL) |
---|
420 | n/a | return -1; |
---|
421 | n/a | } |
---|
422 | n/a | |
---|
423 | n/a | /* only service pending calls on main thread */ |
---|
424 | n/a | if (main_thread && PyThread_get_thread_ident() != main_thread) |
---|
425 | n/a | return 0; |
---|
426 | n/a | /* don't perform recursive pending calls */ |
---|
427 | n/a | if (busy) |
---|
428 | n/a | return 0; |
---|
429 | n/a | busy = 1; |
---|
430 | n/a | /* perform a bounded number of calls, in case of recursion */ |
---|
431 | n/a | for (i=0; i<NPENDINGCALLS; i++) { |
---|
432 | n/a | int j; |
---|
433 | n/a | int (*func)(void *); |
---|
434 | n/a | void *arg = NULL; |
---|
435 | n/a | |
---|
436 | n/a | /* pop one item off the queue while holding the lock */ |
---|
437 | n/a | PyThread_acquire_lock(pending_lock, WAIT_LOCK); |
---|
438 | n/a | j = pendingfirst; |
---|
439 | n/a | if (j == pendinglast) { |
---|
440 | n/a | func = NULL; /* Queue empty */ |
---|
441 | n/a | } else { |
---|
442 | n/a | func = pendingcalls[j].func; |
---|
443 | n/a | arg = pendingcalls[j].arg; |
---|
444 | n/a | pendingfirst = (j + 1) % NPENDINGCALLS; |
---|
445 | n/a | } |
---|
446 | n/a | if (pendingfirst != pendinglast) |
---|
447 | n/a | SIGNAL_PENDING_CALLS(); |
---|
448 | n/a | else |
---|
449 | n/a | UNSIGNAL_PENDING_CALLS(); |
---|
450 | n/a | PyThread_release_lock(pending_lock); |
---|
451 | n/a | /* having released the lock, perform the callback */ |
---|
452 | n/a | if (func == NULL) |
---|
453 | n/a | break; |
---|
454 | n/a | r = func(arg); |
---|
455 | n/a | if (r) |
---|
456 | n/a | break; |
---|
457 | n/a | } |
---|
458 | n/a | busy = 0; |
---|
459 | n/a | return r; |
---|
460 | n/a | } |
---|
461 | n/a | |
---|
462 | n/a | #else /* if ! defined WITH_THREAD */ |
---|
463 | n/a | |
---|
464 | n/a | /* |
---|
465 | n/a | WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
---|
466 | n/a | This code is used for signal handling in python that isn't built |
---|
467 | n/a | with WITH_THREAD. |
---|
468 | n/a | Don't use this implementation when Py_AddPendingCalls() can happen |
---|
469 | n/a | on a different thread! |
---|
470 | n/a | |
---|
471 | n/a | There are two possible race conditions: |
---|
472 | n/a | (1) nested asynchronous calls to Py_AddPendingCall() |
---|
473 | n/a | (2) AddPendingCall() calls made while pending calls are being processed. |
---|
474 | n/a | |
---|
475 | n/a | (1) is very unlikely because typically signal delivery |
---|
476 | n/a | is blocked during signal handling. So it should be impossible. |
---|
477 | n/a | (2) is a real possibility. |
---|
478 | n/a | The current code is safe against (2), but not against (1). |
---|
479 | n/a | The safety against (2) is derived from the fact that only one |
---|
480 | n/a | thread is present, interrupted by signals, and that the critical |
---|
481 | n/a | section is protected with the "busy" variable. On Windows, which |
---|
482 | n/a | delivers SIGINT on a system thread, this does not hold and therefore |
---|
483 | n/a | Windows really shouldn't use this version. |
---|
484 | n/a | The two threads could theoretically wiggle around the "busy" variable. |
---|
485 | n/a | */ |
---|
486 | n/a | |
---|
487 | n/a | #define NPENDINGCALLS 32 |
---|
488 | n/a | static struct { |
---|
489 | n/a | int (*func)(void *); |
---|
490 | n/a | void *arg; |
---|
491 | n/a | } pendingcalls[NPENDINGCALLS]; |
---|
492 | n/a | static volatile int pendingfirst = 0; |
---|
493 | n/a | static volatile int pendinglast = 0; |
---|
494 | n/a | static _Py_atomic_int pendingcalls_to_do = {0}; |
---|
495 | n/a | |
---|
496 | n/a | int |
---|
497 | n/a | Py_AddPendingCall(int (*func)(void *), void *arg) |
---|
498 | n/a | { |
---|
499 | n/a | static volatile int busy = 0; |
---|
500 | n/a | int i, j; |
---|
501 | n/a | /* XXX Begin critical section */ |
---|
502 | n/a | if (busy) |
---|
503 | n/a | return -1; |
---|
504 | n/a | busy = 1; |
---|
505 | n/a | i = pendinglast; |
---|
506 | n/a | j = (i + 1) % NPENDINGCALLS; |
---|
507 | n/a | if (j == pendingfirst) { |
---|
508 | n/a | busy = 0; |
---|
509 | n/a | return -1; /* Queue full */ |
---|
510 | n/a | } |
---|
511 | n/a | pendingcalls[i].func = func; |
---|
512 | n/a | pendingcalls[i].arg = arg; |
---|
513 | n/a | pendinglast = j; |
---|
514 | n/a | |
---|
515 | n/a | SIGNAL_PENDING_CALLS(); |
---|
516 | n/a | busy = 0; |
---|
517 | n/a | /* XXX End critical section */ |
---|
518 | n/a | return 0; |
---|
519 | n/a | } |
---|
520 | n/a | |
---|
521 | n/a | int |
---|
522 | n/a | Py_MakePendingCalls(void) |
---|
523 | n/a | { |
---|
524 | n/a | static int busy = 0; |
---|
525 | n/a | if (busy) |
---|
526 | n/a | return 0; |
---|
527 | n/a | busy = 1; |
---|
528 | n/a | UNSIGNAL_PENDING_CALLS(); |
---|
529 | n/a | for (;;) { |
---|
530 | n/a | int i; |
---|
531 | n/a | int (*func)(void *); |
---|
532 | n/a | void *arg; |
---|
533 | n/a | i = pendingfirst; |
---|
534 | n/a | if (i == pendinglast) |
---|
535 | n/a | break; /* Queue empty */ |
---|
536 | n/a | func = pendingcalls[i].func; |
---|
537 | n/a | arg = pendingcalls[i].arg; |
---|
538 | n/a | pendingfirst = (i + 1) % NPENDINGCALLS; |
---|
539 | n/a | if (func(arg) < 0) { |
---|
540 | n/a | busy = 0; |
---|
541 | n/a | SIGNAL_PENDING_CALLS(); /* We're not done yet */ |
---|
542 | n/a | return -1; |
---|
543 | n/a | } |
---|
544 | n/a | } |
---|
545 | n/a | busy = 0; |
---|
546 | n/a | return 0; |
---|
547 | n/a | } |
---|
548 | n/a | |
---|
549 | n/a | #endif /* WITH_THREAD */ |
---|
550 | n/a | |
---|
551 | n/a | |
---|
552 | n/a | /* The interpreter's recursion limit */ |
---|
553 | n/a | |
---|
554 | n/a | #ifndef Py_DEFAULT_RECURSION_LIMIT |
---|
555 | n/a | #define Py_DEFAULT_RECURSION_LIMIT 1000 |
---|
556 | n/a | #endif |
---|
557 | n/a | static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; |
---|
558 | n/a | int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; |
---|
559 | n/a | |
---|
560 | n/a | int |
---|
561 | n/a | Py_GetRecursionLimit(void) |
---|
562 | n/a | { |
---|
563 | n/a | return recursion_limit; |
---|
564 | n/a | } |
---|
565 | n/a | |
---|
566 | n/a | void |
---|
567 | n/a | Py_SetRecursionLimit(int new_limit) |
---|
568 | n/a | { |
---|
569 | n/a | recursion_limit = new_limit; |
---|
570 | n/a | _Py_CheckRecursionLimit = recursion_limit; |
---|
571 | n/a | } |
---|
572 | n/a | |
---|
573 | n/a | /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() |
---|
574 | n/a | if the recursion_depth reaches _Py_CheckRecursionLimit. |
---|
575 | n/a | If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit |
---|
576 | n/a | to guarantee that _Py_CheckRecursiveCall() is regularly called. |
---|
577 | n/a | Without USE_STACKCHECK, there is no need for this. */ |
---|
578 | n/a | int |
---|
579 | n/a | _Py_CheckRecursiveCall(const char *where) |
---|
580 | n/a | { |
---|
581 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
582 | n/a | |
---|
583 | n/a | #ifdef USE_STACKCHECK |
---|
584 | n/a | if (PyOS_CheckStack()) { |
---|
585 | n/a | --tstate->recursion_depth; |
---|
586 | n/a | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
---|
587 | n/a | return -1; |
---|
588 | n/a | } |
---|
589 | n/a | #endif |
---|
590 | n/a | _Py_CheckRecursionLimit = recursion_limit; |
---|
591 | n/a | if (tstate->recursion_critical) |
---|
592 | n/a | /* Somebody asked that we don't check for recursion. */ |
---|
593 | n/a | return 0; |
---|
594 | n/a | if (tstate->overflowed) { |
---|
595 | n/a | if (tstate->recursion_depth > recursion_limit + 50) { |
---|
596 | n/a | /* Overflowing while handling an overflow. Give up. */ |
---|
597 | n/a | Py_FatalError("Cannot recover from stack overflow."); |
---|
598 | n/a | } |
---|
599 | n/a | return 0; |
---|
600 | n/a | } |
---|
601 | n/a | if (tstate->recursion_depth > recursion_limit) { |
---|
602 | n/a | --tstate->recursion_depth; |
---|
603 | n/a | tstate->overflowed = 1; |
---|
604 | n/a | PyErr_Format(PyExc_RecursionError, |
---|
605 | n/a | "maximum recursion depth exceeded%s", |
---|
606 | n/a | where); |
---|
607 | n/a | return -1; |
---|
608 | n/a | } |
---|
609 | n/a | return 0; |
---|
610 | n/a | } |
---|
611 | n/a | |
---|
612 | n/a | /* Status code for main loop (reason for stack unwind) */ |
---|
613 | n/a | enum why_code { |
---|
614 | n/a | WHY_NOT = 0x0001, /* No error */ |
---|
615 | n/a | WHY_EXCEPTION = 0x0002, /* Exception occurred */ |
---|
616 | n/a | WHY_RETURN = 0x0008, /* 'return' statement */ |
---|
617 | n/a | WHY_BREAK = 0x0010, /* 'break' statement */ |
---|
618 | n/a | WHY_CONTINUE = 0x0020, /* 'continue' statement */ |
---|
619 | n/a | WHY_YIELD = 0x0040, /* 'yield' operator */ |
---|
620 | n/a | WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */ |
---|
621 | n/a | }; |
---|
622 | n/a | |
---|
623 | n/a | static void save_exc_state(PyThreadState *, PyFrameObject *); |
---|
624 | n/a | static void swap_exc_state(PyThreadState *, PyFrameObject *); |
---|
625 | n/a | static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *); |
---|
626 | n/a | static int do_raise(PyObject *, PyObject *); |
---|
627 | n/a | static int unpack_iterable(PyObject *, int, int, PyObject **); |
---|
628 | n/a | |
---|
629 | n/a | /* Records whether tracing is on for any thread. Counts the number of |
---|
630 | n/a | threads for which tstate->c_tracefunc is non-NULL, so if the value |
---|
631 | n/a | is 0, we know we don't have to check this thread's c_tracefunc. |
---|
632 | n/a | This speeds up the if statement in PyEval_EvalFrameEx() after |
---|
633 | n/a | fast_next_opcode*/ |
---|
634 | n/a | static int _Py_TracingPossible = 0; |
---|
635 | n/a | |
---|
636 | n/a | |
---|
637 | n/a | |
---|
638 | n/a | PyObject * |
---|
639 | n/a | PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals) |
---|
640 | n/a | { |
---|
641 | n/a | return PyEval_EvalCodeEx(co, |
---|
642 | n/a | globals, locals, |
---|
643 | n/a | (PyObject **)NULL, 0, |
---|
644 | n/a | (PyObject **)NULL, 0, |
---|
645 | n/a | (PyObject **)NULL, 0, |
---|
646 | n/a | NULL, NULL); |
---|
647 | n/a | } |
---|
648 | n/a | |
---|
649 | n/a | |
---|
650 | n/a | /* Interpreter main loop */ |
---|
651 | n/a | |
---|
652 | n/a | PyObject * |
---|
653 | n/a | PyEval_EvalFrame(PyFrameObject *f) { |
---|
654 | n/a | /* This is for backward compatibility with extension modules that |
---|
655 | n/a | used this API; core interpreter code should call |
---|
656 | n/a | PyEval_EvalFrameEx() */ |
---|
657 | n/a | return PyEval_EvalFrameEx(f, 0); |
---|
658 | n/a | } |
---|
659 | n/a | |
---|
660 | n/a | PyObject * |
---|
661 | n/a | PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) |
---|
662 | n/a | { |
---|
663 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
664 | n/a | return tstate->interp->eval_frame(f, throwflag); |
---|
665 | n/a | } |
---|
666 | n/a | |
---|
667 | n/a | PyObject* _Py_HOT_FUNCTION |
---|
668 | n/a | _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) |
---|
669 | n/a | { |
---|
670 | n/a | #ifdef DXPAIRS |
---|
671 | n/a | int lastopcode = 0; |
---|
672 | n/a | #endif |
---|
673 | n/a | PyObject **stack_pointer; /* Next free slot in value stack */ |
---|
674 | n/a | const _Py_CODEUNIT *next_instr; |
---|
675 | n/a | int opcode; /* Current opcode */ |
---|
676 | n/a | int oparg; /* Current opcode argument, if any */ |
---|
677 | n/a | enum why_code why; /* Reason for block stack unwind */ |
---|
678 | n/a | PyObject **fastlocals, **freevars; |
---|
679 | n/a | PyObject *retval = NULL; /* Return value */ |
---|
680 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
681 | n/a | PyCodeObject *co; |
---|
682 | n/a | |
---|
683 | n/a | /* when tracing we set things up so that |
---|
684 | n/a | |
---|
685 | n/a | not (instr_lb <= current_bytecode_offset < instr_ub) |
---|
686 | n/a | |
---|
687 | n/a | is true when the line being executed has changed. The |
---|
688 | n/a | initial values are such as to make this false the first |
---|
689 | n/a | time it is tested. */ |
---|
690 | n/a | int instr_ub = -1, instr_lb = 0, instr_prev = -1; |
---|
691 | n/a | |
---|
692 | n/a | const _Py_CODEUNIT *first_instr; |
---|
693 | n/a | PyObject *names; |
---|
694 | n/a | PyObject *consts; |
---|
695 | n/a | |
---|
696 | n/a | #ifdef LLTRACE |
---|
697 | n/a | _Py_IDENTIFIER(__ltrace__); |
---|
698 | n/a | #endif |
---|
699 | n/a | |
---|
700 | n/a | /* Computed GOTOs, or |
---|
701 | n/a | the-optimization-commonly-but-improperly-known-as-"threaded code" |
---|
702 | n/a | using gcc's labels-as-values extension |
---|
703 | n/a | (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html). |
---|
704 | n/a | |
---|
705 | n/a | The traditional bytecode evaluation loop uses a "switch" statement, which |
---|
706 | n/a | decent compilers will optimize as a single indirect branch instruction |
---|
707 | n/a | combined with a lookup table of jump addresses. However, since the |
---|
708 | n/a | indirect jump instruction is shared by all opcodes, the CPU will have a |
---|
709 | n/a | hard time making the right prediction for where to jump next (actually, |
---|
710 | n/a | it will be always wrong except in the uncommon case of a sequence of |
---|
711 | n/a | several identical opcodes). |
---|
712 | n/a | |
---|
713 | n/a | "Threaded code" in contrast, uses an explicit jump table and an explicit |
---|
714 | n/a | indirect jump instruction at the end of each opcode. Since the jump |
---|
715 | n/a | instruction is at a different address for each opcode, the CPU will make a |
---|
716 | n/a | separate prediction for each of these instructions, which is equivalent to |
---|
717 | n/a | predicting the second opcode of each opcode pair. These predictions have |
---|
718 | n/a | a much better chance to turn out valid, especially in small bytecode loops. |
---|
719 | n/a | |
---|
720 | n/a | A mispredicted branch on a modern CPU flushes the whole pipeline and |
---|
721 | n/a | can cost several CPU cycles (depending on the pipeline depth), |
---|
722 | n/a | and potentially many more instructions (depending on the pipeline width). |
---|
723 | n/a | A correctly predicted branch, however, is nearly free. |
---|
724 | n/a | |
---|
725 | n/a | At the time of this writing, the "threaded code" version is up to 15-20% |
---|
726 | n/a | faster than the normal "switch" version, depending on the compiler and the |
---|
727 | n/a | CPU architecture. |
---|
728 | n/a | |
---|
729 | n/a | We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined, |
---|
730 | n/a | because it would render the measurements invalid. |
---|
731 | n/a | |
---|
732 | n/a | |
---|
733 | n/a | NOTE: care must be taken that the compiler doesn't try to "optimize" the |
---|
734 | n/a | indirect jumps by sharing them between all opcodes. Such optimizations |
---|
735 | n/a | can be disabled on gcc by using the -fno-gcse flag (or possibly |
---|
736 | n/a | -fno-crossjumping). |
---|
737 | n/a | */ |
---|
738 | n/a | |
---|
739 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
---|
740 | n/a | #undef USE_COMPUTED_GOTOS |
---|
741 | n/a | #define USE_COMPUTED_GOTOS 0 |
---|
742 | n/a | #endif |
---|
743 | n/a | |
---|
744 | n/a | #ifdef HAVE_COMPUTED_GOTOS |
---|
745 | n/a | #ifndef USE_COMPUTED_GOTOS |
---|
746 | n/a | #define USE_COMPUTED_GOTOS 1 |
---|
747 | n/a | #endif |
---|
748 | n/a | #else |
---|
749 | n/a | #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
---|
750 | n/a | #error "Computed gotos are not supported on this compiler." |
---|
751 | n/a | #endif |
---|
752 | n/a | #undef USE_COMPUTED_GOTOS |
---|
753 | n/a | #define USE_COMPUTED_GOTOS 0 |
---|
754 | n/a | #endif |
---|
755 | n/a | |
---|
756 | n/a | #if USE_COMPUTED_GOTOS |
---|
757 | n/a | /* Import the static jump table */ |
---|
758 | n/a | #include "opcode_targets.h" |
---|
759 | n/a | |
---|
760 | n/a | #define TARGET(op) \ |
---|
761 | n/a | TARGET_##op: \ |
---|
762 | n/a | case op: |
---|
763 | n/a | |
---|
764 | n/a | #define DISPATCH() \ |
---|
765 | n/a | { \ |
---|
766 | n/a | if (!_Py_atomic_load_relaxed(&eval_breaker)) { \ |
---|
767 | n/a | FAST_DISPATCH(); \ |
---|
768 | n/a | } \ |
---|
769 | n/a | continue; \ |
---|
770 | n/a | } |
---|
771 | n/a | |
---|
772 | n/a | #ifdef LLTRACE |
---|
773 | n/a | #define FAST_DISPATCH() \ |
---|
774 | n/a | { \ |
---|
775 | n/a | if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
---|
776 | n/a | f->f_lasti = INSTR_OFFSET(); \ |
---|
777 | n/a | NEXTOPARG(); \ |
---|
778 | n/a | goto *opcode_targets[opcode]; \ |
---|
779 | n/a | } \ |
---|
780 | n/a | goto fast_next_opcode; \ |
---|
781 | n/a | } |
---|
782 | n/a | #else |
---|
783 | n/a | #define FAST_DISPATCH() \ |
---|
784 | n/a | { \ |
---|
785 | n/a | if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \ |
---|
786 | n/a | f->f_lasti = INSTR_OFFSET(); \ |
---|
787 | n/a | NEXTOPARG(); \ |
---|
788 | n/a | goto *opcode_targets[opcode]; \ |
---|
789 | n/a | } \ |
---|
790 | n/a | goto fast_next_opcode; \ |
---|
791 | n/a | } |
---|
792 | n/a | #endif |
---|
793 | n/a | |
---|
794 | n/a | #else |
---|
795 | n/a | #define TARGET(op) \ |
---|
796 | n/a | case op: |
---|
797 | n/a | |
---|
798 | n/a | #define DISPATCH() continue |
---|
799 | n/a | #define FAST_DISPATCH() goto fast_next_opcode |
---|
800 | n/a | #endif |
---|
801 | n/a | |
---|
802 | n/a | |
---|
803 | n/a | /* Tuple access macros */ |
---|
804 | n/a | |
---|
805 | n/a | #ifndef Py_DEBUG |
---|
806 | n/a | #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i)) |
---|
807 | n/a | #else |
---|
808 | n/a | #define GETITEM(v, i) PyTuple_GetItem((v), (i)) |
---|
809 | n/a | #endif |
---|
810 | n/a | |
---|
811 | n/a | /* Code access macros */ |
---|
812 | n/a | |
---|
813 | n/a | /* The integer overflow is checked by an assertion below. */ |
---|
814 | n/a | #define INSTR_OFFSET() (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr)) |
---|
815 | n/a | #define NEXTOPARG() do { \ |
---|
816 | n/a | _Py_CODEUNIT word = *next_instr; \ |
---|
817 | n/a | opcode = _Py_OPCODE(word); \ |
---|
818 | n/a | oparg = _Py_OPARG(word); \ |
---|
819 | n/a | next_instr++; \ |
---|
820 | n/a | } while (0) |
---|
821 | n/a | #define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT)) |
---|
822 | n/a | #define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT)) |
---|
823 | n/a | |
---|
824 | n/a | /* OpCode prediction macros |
---|
825 | n/a | Some opcodes tend to come in pairs thus making it possible to |
---|
826 | n/a | predict the second code when the first is run. For example, |
---|
827 | n/a | COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE. |
---|
828 | n/a | |
---|
829 | n/a | Verifying the prediction costs a single high-speed test of a register |
---|
830 | n/a | variable against a constant. If the pairing was good, then the |
---|
831 | n/a | processor's own internal branch predication has a high likelihood of |
---|
832 | n/a | success, resulting in a nearly zero-overhead transition to the |
---|
833 | n/a | next opcode. A successful prediction saves a trip through the eval-loop |
---|
834 | n/a | including its unpredictable switch-case branch. Combined with the |
---|
835 | n/a | processor's internal branch prediction, a successful PREDICT has the |
---|
836 | n/a | effect of making the two opcodes run as if they were a single new opcode |
---|
837 | n/a | with the bodies combined. |
---|
838 | n/a | |
---|
839 | n/a | If collecting opcode statistics, your choices are to either keep the |
---|
840 | n/a | predictions turned-on and interpret the results as if some opcodes |
---|
841 | n/a | had been combined or turn-off predictions so that the opcode frequency |
---|
842 | n/a | counter updates for both opcodes. |
---|
843 | n/a | |
---|
844 | n/a | Opcode prediction is disabled with threaded code, since the latter allows |
---|
845 | n/a | the CPU to record separate branch prediction information for each |
---|
846 | n/a | opcode. |
---|
847 | n/a | |
---|
848 | n/a | */ |
---|
849 | n/a | |
---|
850 | n/a | #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS |
---|
851 | n/a | #define PREDICT(op) if (0) goto PRED_##op |
---|
852 | n/a | #else |
---|
853 | n/a | #define PREDICT(op) \ |
---|
854 | n/a | do{ \ |
---|
855 | n/a | _Py_CODEUNIT word = *next_instr; \ |
---|
856 | n/a | opcode = _Py_OPCODE(word); \ |
---|
857 | n/a | if (opcode == op){ \ |
---|
858 | n/a | oparg = _Py_OPARG(word); \ |
---|
859 | n/a | next_instr++; \ |
---|
860 | n/a | goto PRED_##op; \ |
---|
861 | n/a | } \ |
---|
862 | n/a | } while(0) |
---|
863 | n/a | #endif |
---|
864 | n/a | #define PREDICTED(op) PRED_##op: |
---|
865 | n/a | |
---|
866 | n/a | |
---|
867 | n/a | /* Stack manipulation macros */ |
---|
868 | n/a | |
---|
869 | n/a | /* The stack can grow at most MAXINT deep, as co_nlocals and |
---|
870 | n/a | co_stacksize are ints. */ |
---|
871 | n/a | #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack)) |
---|
872 | n/a | #define EMPTY() (STACK_LEVEL() == 0) |
---|
873 | n/a | #define TOP() (stack_pointer[-1]) |
---|
874 | n/a | #define SECOND() (stack_pointer[-2]) |
---|
875 | n/a | #define THIRD() (stack_pointer[-3]) |
---|
876 | n/a | #define FOURTH() (stack_pointer[-4]) |
---|
877 | n/a | #define PEEK(n) (stack_pointer[-(n)]) |
---|
878 | n/a | #define SET_TOP(v) (stack_pointer[-1] = (v)) |
---|
879 | n/a | #define SET_SECOND(v) (stack_pointer[-2] = (v)) |
---|
880 | n/a | #define SET_THIRD(v) (stack_pointer[-3] = (v)) |
---|
881 | n/a | #define SET_FOURTH(v) (stack_pointer[-4] = (v)) |
---|
882 | n/a | #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v)) |
---|
883 | n/a | #define BASIC_STACKADJ(n) (stack_pointer += n) |
---|
884 | n/a | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
---|
885 | n/a | #define BASIC_POP() (*--stack_pointer) |
---|
886 | n/a | |
---|
887 | n/a | #ifdef LLTRACE |
---|
888 | n/a | #define PUSH(v) { (void)(BASIC_PUSH(v), \ |
---|
889 | n/a | lltrace && prtrace(TOP(), "push")); \ |
---|
890 | n/a | assert(STACK_LEVEL() <= co->co_stacksize); } |
---|
891 | n/a | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \ |
---|
892 | n/a | BASIC_POP()) |
---|
893 | n/a | #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \ |
---|
894 | n/a | lltrace && prtrace(TOP(), "stackadj")); \ |
---|
895 | n/a | assert(STACK_LEVEL() <= co->co_stacksize); } |
---|
896 | n/a | #define EXT_POP(STACK_POINTER) ((void)(lltrace && \ |
---|
897 | n/a | prtrace((STACK_POINTER)[-1], "ext_pop")), \ |
---|
898 | n/a | *--(STACK_POINTER)) |
---|
899 | n/a | #else |
---|
900 | n/a | #define PUSH(v) BASIC_PUSH(v) |
---|
901 | n/a | #define POP() BASIC_POP() |
---|
902 | n/a | #define STACKADJ(n) BASIC_STACKADJ(n) |
---|
903 | n/a | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
---|
904 | n/a | #endif |
---|
905 | n/a | |
---|
906 | n/a | /* Local variable macros */ |
---|
907 | n/a | |
---|
908 | n/a | #define GETLOCAL(i) (fastlocals[i]) |
---|
909 | n/a | |
---|
910 | n/a | /* The SETLOCAL() macro must not DECREF the local variable in-place and |
---|
911 | n/a | then store the new value; it must copy the old value to a temporary |
---|
912 | n/a | value, then store the new value, and then DECREF the temporary value. |
---|
913 | n/a | This is because it is possible that during the DECREF the frame is |
---|
914 | n/a | accessed by other code (e.g. a __del__ method or gc.collect()) and the |
---|
915 | n/a | variable would be pointing to already-freed memory. */ |
---|
916 | n/a | #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \ |
---|
917 | n/a | GETLOCAL(i) = value; \ |
---|
918 | n/a | Py_XDECREF(tmp); } while (0) |
---|
919 | n/a | |
---|
920 | n/a | |
---|
921 | n/a | #define UNWIND_BLOCK(b) \ |
---|
922 | n/a | while (STACK_LEVEL() > (b)->b_level) { \ |
---|
923 | n/a | PyObject *v = POP(); \ |
---|
924 | n/a | Py_XDECREF(v); \ |
---|
925 | n/a | } |
---|
926 | n/a | |
---|
927 | n/a | #define UNWIND_EXCEPT_HANDLER(b) \ |
---|
928 | n/a | do { \ |
---|
929 | n/a | PyObject *type, *value, *traceback; \ |
---|
930 | n/a | assert(STACK_LEVEL() >= (b)->b_level + 3); \ |
---|
931 | n/a | while (STACK_LEVEL() > (b)->b_level + 3) { \ |
---|
932 | n/a | value = POP(); \ |
---|
933 | n/a | Py_XDECREF(value); \ |
---|
934 | n/a | } \ |
---|
935 | n/a | type = tstate->exc_type; \ |
---|
936 | n/a | value = tstate->exc_value; \ |
---|
937 | n/a | traceback = tstate->exc_traceback; \ |
---|
938 | n/a | tstate->exc_type = POP(); \ |
---|
939 | n/a | tstate->exc_value = POP(); \ |
---|
940 | n/a | tstate->exc_traceback = POP(); \ |
---|
941 | n/a | Py_XDECREF(type); \ |
---|
942 | n/a | Py_XDECREF(value); \ |
---|
943 | n/a | Py_XDECREF(traceback); \ |
---|
944 | n/a | } while(0) |
---|
945 | n/a | |
---|
946 | n/a | /* Start of code */ |
---|
947 | n/a | |
---|
948 | n/a | /* push frame */ |
---|
949 | n/a | if (Py_EnterRecursiveCall("")) |
---|
950 | n/a | return NULL; |
---|
951 | n/a | |
---|
952 | n/a | tstate->frame = f; |
---|
953 | n/a | |
---|
954 | n/a | if (tstate->use_tracing) { |
---|
955 | n/a | if (tstate->c_tracefunc != NULL) { |
---|
956 | n/a | /* tstate->c_tracefunc, if defined, is a |
---|
957 | n/a | function that will be called on *every* entry |
---|
958 | n/a | to a code block. Its return value, if not |
---|
959 | n/a | None, is a function that will be called at |
---|
960 | n/a | the start of each executed line of code. |
---|
961 | n/a | (Actually, the function must return itself |
---|
962 | n/a | in order to continue tracing.) The trace |
---|
963 | n/a | functions are called with three arguments: |
---|
964 | n/a | a pointer to the current frame, a string |
---|
965 | n/a | indicating why the function is called, and |
---|
966 | n/a | an argument which depends on the situation. |
---|
967 | n/a | The global trace function is also called |
---|
968 | n/a | whenever an exception is detected. */ |
---|
969 | n/a | if (call_trace_protected(tstate->c_tracefunc, |
---|
970 | n/a | tstate->c_traceobj, |
---|
971 | n/a | tstate, f, PyTrace_CALL, Py_None)) { |
---|
972 | n/a | /* Trace function raised an error */ |
---|
973 | n/a | goto exit_eval_frame; |
---|
974 | n/a | } |
---|
975 | n/a | } |
---|
976 | n/a | if (tstate->c_profilefunc != NULL) { |
---|
977 | n/a | /* Similar for c_profilefunc, except it needn't |
---|
978 | n/a | return itself and isn't called for "line" events */ |
---|
979 | n/a | if (call_trace_protected(tstate->c_profilefunc, |
---|
980 | n/a | tstate->c_profileobj, |
---|
981 | n/a | tstate, f, PyTrace_CALL, Py_None)) { |
---|
982 | n/a | /* Profile function raised an error */ |
---|
983 | n/a | goto exit_eval_frame; |
---|
984 | n/a | } |
---|
985 | n/a | } |
---|
986 | n/a | } |
---|
987 | n/a | |
---|
988 | n/a | if (PyDTrace_FUNCTION_ENTRY_ENABLED()) |
---|
989 | n/a | dtrace_function_entry(f); |
---|
990 | n/a | |
---|
991 | n/a | co = f->f_code; |
---|
992 | n/a | names = co->co_names; |
---|
993 | n/a | consts = co->co_consts; |
---|
994 | n/a | fastlocals = f->f_localsplus; |
---|
995 | n/a | freevars = f->f_localsplus + co->co_nlocals; |
---|
996 | n/a | assert(PyBytes_Check(co->co_code)); |
---|
997 | n/a | assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); |
---|
998 | n/a | assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); |
---|
999 | n/a | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); |
---|
1000 | n/a | first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); |
---|
1001 | n/a | /* |
---|
1002 | n/a | f->f_lasti refers to the index of the last instruction, |
---|
1003 | n/a | unless it's -1 in which case next_instr should be first_instr. |
---|
1004 | n/a | |
---|
1005 | n/a | YIELD_FROM sets f_lasti to itself, in order to repeatedly yield |
---|
1006 | n/a | multiple values. |
---|
1007 | n/a | |
---|
1008 | n/a | When the PREDICT() macros are enabled, some opcode pairs follow in |
---|
1009 | n/a | direct succession without updating f->f_lasti. A successful |
---|
1010 | n/a | prediction effectively links the two codes together as if they |
---|
1011 | n/a | were a single new opcode; accordingly,f->f_lasti will point to |
---|
1012 | n/a | the first code in the pair (for instance, GET_ITER followed by |
---|
1013 | n/a | FOR_ITER is effectively a single opcode and f->f_lasti will point |
---|
1014 | n/a | to the beginning of the combined pair.) |
---|
1015 | n/a | */ |
---|
1016 | n/a | assert(f->f_lasti >= -1); |
---|
1017 | n/a | next_instr = first_instr; |
---|
1018 | n/a | if (f->f_lasti >= 0) { |
---|
1019 | n/a | assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0); |
---|
1020 | n/a | next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1; |
---|
1021 | n/a | } |
---|
1022 | n/a | stack_pointer = f->f_stacktop; |
---|
1023 | n/a | assert(stack_pointer != NULL); |
---|
1024 | n/a | f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */ |
---|
1025 | n/a | f->f_executing = 1; |
---|
1026 | n/a | |
---|
1027 | n/a | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
---|
1028 | n/a | if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) { |
---|
1029 | n/a | /* We were in an except handler when we left, |
---|
1030 | n/a | restore the exception state which was put aside |
---|
1031 | n/a | (see YIELD_VALUE). */ |
---|
1032 | n/a | swap_exc_state(tstate, f); |
---|
1033 | n/a | } |
---|
1034 | n/a | else |
---|
1035 | n/a | save_exc_state(tstate, f); |
---|
1036 | n/a | } |
---|
1037 | n/a | |
---|
1038 | n/a | #ifdef LLTRACE |
---|
1039 | n/a | lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL; |
---|
1040 | n/a | #endif |
---|
1041 | n/a | |
---|
1042 | n/a | why = WHY_NOT; |
---|
1043 | n/a | |
---|
1044 | n/a | if (throwflag) /* support for generator.throw() */ |
---|
1045 | n/a | goto error; |
---|
1046 | n/a | |
---|
1047 | n/a | #ifdef Py_DEBUG |
---|
1048 | n/a | /* PyEval_EvalFrameEx() must not be called with an exception set, |
---|
1049 | n/a | because it can clear it (directly or indirectly) and so the |
---|
1050 | n/a | caller loses its exception */ |
---|
1051 | n/a | assert(!PyErr_Occurred()); |
---|
1052 | n/a | #endif |
---|
1053 | n/a | |
---|
1054 | n/a | for (;;) { |
---|
1055 | n/a | assert(stack_pointer >= f->f_valuestack); /* else underflow */ |
---|
1056 | n/a | assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ |
---|
1057 | n/a | assert(!PyErr_Occurred()); |
---|
1058 | n/a | |
---|
1059 | n/a | /* Do periodic things. Doing this every time through |
---|
1060 | n/a | the loop would add too much overhead, so we do it |
---|
1061 | n/a | only every Nth instruction. We also do it if |
---|
1062 | n/a | ``pendingcalls_to_do'' is set, i.e. when an asynchronous |
---|
1063 | n/a | event needs attention (e.g. a signal handler or |
---|
1064 | n/a | async I/O handler); see Py_AddPendingCall() and |
---|
1065 | n/a | Py_MakePendingCalls() above. */ |
---|
1066 | n/a | |
---|
1067 | n/a | if (_Py_atomic_load_relaxed(&eval_breaker)) { |
---|
1068 | n/a | if (_Py_OPCODE(*next_instr) == SETUP_FINALLY) { |
---|
1069 | n/a | /* Make the last opcode before |
---|
1070 | n/a | a try: finally: block uninterruptible. */ |
---|
1071 | n/a | goto fast_next_opcode; |
---|
1072 | n/a | } |
---|
1073 | n/a | if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) { |
---|
1074 | n/a | if (Py_MakePendingCalls() < 0) |
---|
1075 | n/a | goto error; |
---|
1076 | n/a | } |
---|
1077 | n/a | #ifdef WITH_THREAD |
---|
1078 | n/a | if (_Py_atomic_load_relaxed(&gil_drop_request)) { |
---|
1079 | n/a | /* Give another thread a chance */ |
---|
1080 | n/a | if (PyThreadState_Swap(NULL) != tstate) |
---|
1081 | n/a | Py_FatalError("ceval: tstate mix-up"); |
---|
1082 | n/a | drop_gil(tstate); |
---|
1083 | n/a | |
---|
1084 | n/a | /* Other threads may run now */ |
---|
1085 | n/a | |
---|
1086 | n/a | take_gil(tstate); |
---|
1087 | n/a | |
---|
1088 | n/a | /* Check if we should make a quick exit. */ |
---|
1089 | n/a | if (_Py_Finalizing && _Py_Finalizing != tstate) { |
---|
1090 | n/a | drop_gil(tstate); |
---|
1091 | n/a | PyThread_exit_thread(); |
---|
1092 | n/a | } |
---|
1093 | n/a | |
---|
1094 | n/a | if (PyThreadState_Swap(tstate) != NULL) |
---|
1095 | n/a | Py_FatalError("ceval: orphan tstate"); |
---|
1096 | n/a | } |
---|
1097 | n/a | #endif |
---|
1098 | n/a | /* Check for asynchronous exceptions. */ |
---|
1099 | n/a | if (tstate->async_exc != NULL) { |
---|
1100 | n/a | PyObject *exc = tstate->async_exc; |
---|
1101 | n/a | tstate->async_exc = NULL; |
---|
1102 | n/a | UNSIGNAL_ASYNC_EXC(); |
---|
1103 | n/a | PyErr_SetNone(exc); |
---|
1104 | n/a | Py_DECREF(exc); |
---|
1105 | n/a | goto error; |
---|
1106 | n/a | } |
---|
1107 | n/a | } |
---|
1108 | n/a | |
---|
1109 | n/a | fast_next_opcode: |
---|
1110 | n/a | f->f_lasti = INSTR_OFFSET(); |
---|
1111 | n/a | |
---|
1112 | n/a | if (PyDTrace_LINE_ENABLED()) |
---|
1113 | n/a | maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev); |
---|
1114 | n/a | |
---|
1115 | n/a | /* line-by-line tracing support */ |
---|
1116 | n/a | |
---|
1117 | n/a | if (_Py_TracingPossible && |
---|
1118 | n/a | tstate->c_tracefunc != NULL && !tstate->tracing) { |
---|
1119 | n/a | int err; |
---|
1120 | n/a | /* see maybe_call_line_trace |
---|
1121 | n/a | for expository comments */ |
---|
1122 | n/a | f->f_stacktop = stack_pointer; |
---|
1123 | n/a | |
---|
1124 | n/a | err = maybe_call_line_trace(tstate->c_tracefunc, |
---|
1125 | n/a | tstate->c_traceobj, |
---|
1126 | n/a | tstate, f, |
---|
1127 | n/a | &instr_lb, &instr_ub, &instr_prev); |
---|
1128 | n/a | /* Reload possibly changed frame fields */ |
---|
1129 | n/a | JUMPTO(f->f_lasti); |
---|
1130 | n/a | if (f->f_stacktop != NULL) { |
---|
1131 | n/a | stack_pointer = f->f_stacktop; |
---|
1132 | n/a | f->f_stacktop = NULL; |
---|
1133 | n/a | } |
---|
1134 | n/a | if (err) |
---|
1135 | n/a | /* trace function raised an exception */ |
---|
1136 | n/a | goto error; |
---|
1137 | n/a | } |
---|
1138 | n/a | |
---|
1139 | n/a | /* Extract opcode and argument */ |
---|
1140 | n/a | |
---|
1141 | n/a | NEXTOPARG(); |
---|
1142 | n/a | dispatch_opcode: |
---|
1143 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
---|
1144 | n/a | #ifdef DXPAIRS |
---|
1145 | n/a | dxpairs[lastopcode][opcode]++; |
---|
1146 | n/a | lastopcode = opcode; |
---|
1147 | n/a | #endif |
---|
1148 | n/a | dxp[opcode]++; |
---|
1149 | n/a | #endif |
---|
1150 | n/a | |
---|
1151 | n/a | #ifdef LLTRACE |
---|
1152 | n/a | /* Instruction tracing */ |
---|
1153 | n/a | |
---|
1154 | n/a | if (lltrace) { |
---|
1155 | n/a | if (HAS_ARG(opcode)) { |
---|
1156 | n/a | printf("%d: %d, %d\n", |
---|
1157 | n/a | f->f_lasti, opcode, oparg); |
---|
1158 | n/a | } |
---|
1159 | n/a | else { |
---|
1160 | n/a | printf("%d: %d\n", |
---|
1161 | n/a | f->f_lasti, opcode); |
---|
1162 | n/a | } |
---|
1163 | n/a | } |
---|
1164 | n/a | #endif |
---|
1165 | n/a | |
---|
1166 | n/a | switch (opcode) { |
---|
1167 | n/a | |
---|
1168 | n/a | /* BEWARE! |
---|
1169 | n/a | It is essential that any operation that fails sets either |
---|
1170 | n/a | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
---|
1171 | n/a | and that no operation that succeeds does this! */ |
---|
1172 | n/a | |
---|
1173 | n/a | TARGET(NOP) |
---|
1174 | n/a | FAST_DISPATCH(); |
---|
1175 | n/a | |
---|
1176 | n/a | TARGET(LOAD_FAST) { |
---|
1177 | n/a | PyObject *value = GETLOCAL(oparg); |
---|
1178 | n/a | if (value == NULL) { |
---|
1179 | n/a | format_exc_check_arg(PyExc_UnboundLocalError, |
---|
1180 | n/a | UNBOUNDLOCAL_ERROR_MSG, |
---|
1181 | n/a | PyTuple_GetItem(co->co_varnames, oparg)); |
---|
1182 | n/a | goto error; |
---|
1183 | n/a | } |
---|
1184 | n/a | Py_INCREF(value); |
---|
1185 | n/a | PUSH(value); |
---|
1186 | n/a | FAST_DISPATCH(); |
---|
1187 | n/a | } |
---|
1188 | n/a | |
---|
1189 | n/a | PREDICTED(LOAD_CONST); |
---|
1190 | n/a | TARGET(LOAD_CONST) { |
---|
1191 | n/a | PyObject *value = GETITEM(consts, oparg); |
---|
1192 | n/a | Py_INCREF(value); |
---|
1193 | n/a | PUSH(value); |
---|
1194 | n/a | FAST_DISPATCH(); |
---|
1195 | n/a | } |
---|
1196 | n/a | |
---|
1197 | n/a | PREDICTED(STORE_FAST); |
---|
1198 | n/a | TARGET(STORE_FAST) { |
---|
1199 | n/a | PyObject *value = POP(); |
---|
1200 | n/a | SETLOCAL(oparg, value); |
---|
1201 | n/a | FAST_DISPATCH(); |
---|
1202 | n/a | } |
---|
1203 | n/a | |
---|
1204 | n/a | TARGET(POP_TOP) { |
---|
1205 | n/a | PyObject *value = POP(); |
---|
1206 | n/a | Py_DECREF(value); |
---|
1207 | n/a | FAST_DISPATCH(); |
---|
1208 | n/a | } |
---|
1209 | n/a | |
---|
1210 | n/a | TARGET(ROT_TWO) { |
---|
1211 | n/a | PyObject *top = TOP(); |
---|
1212 | n/a | PyObject *second = SECOND(); |
---|
1213 | n/a | SET_TOP(second); |
---|
1214 | n/a | SET_SECOND(top); |
---|
1215 | n/a | FAST_DISPATCH(); |
---|
1216 | n/a | } |
---|
1217 | n/a | |
---|
1218 | n/a | TARGET(ROT_THREE) { |
---|
1219 | n/a | PyObject *top = TOP(); |
---|
1220 | n/a | PyObject *second = SECOND(); |
---|
1221 | n/a | PyObject *third = THIRD(); |
---|
1222 | n/a | SET_TOP(second); |
---|
1223 | n/a | SET_SECOND(third); |
---|
1224 | n/a | SET_THIRD(top); |
---|
1225 | n/a | FAST_DISPATCH(); |
---|
1226 | n/a | } |
---|
1227 | n/a | |
---|
1228 | n/a | TARGET(DUP_TOP) { |
---|
1229 | n/a | PyObject *top = TOP(); |
---|
1230 | n/a | Py_INCREF(top); |
---|
1231 | n/a | PUSH(top); |
---|
1232 | n/a | FAST_DISPATCH(); |
---|
1233 | n/a | } |
---|
1234 | n/a | |
---|
1235 | n/a | TARGET(DUP_TOP_TWO) { |
---|
1236 | n/a | PyObject *top = TOP(); |
---|
1237 | n/a | PyObject *second = SECOND(); |
---|
1238 | n/a | Py_INCREF(top); |
---|
1239 | n/a | Py_INCREF(second); |
---|
1240 | n/a | STACKADJ(2); |
---|
1241 | n/a | SET_TOP(top); |
---|
1242 | n/a | SET_SECOND(second); |
---|
1243 | n/a | FAST_DISPATCH(); |
---|
1244 | n/a | } |
---|
1245 | n/a | |
---|
1246 | n/a | TARGET(UNARY_POSITIVE) { |
---|
1247 | n/a | PyObject *value = TOP(); |
---|
1248 | n/a | PyObject *res = PyNumber_Positive(value); |
---|
1249 | n/a | Py_DECREF(value); |
---|
1250 | n/a | SET_TOP(res); |
---|
1251 | n/a | if (res == NULL) |
---|
1252 | n/a | goto error; |
---|
1253 | n/a | DISPATCH(); |
---|
1254 | n/a | } |
---|
1255 | n/a | |
---|
1256 | n/a | TARGET(UNARY_NEGATIVE) { |
---|
1257 | n/a | PyObject *value = TOP(); |
---|
1258 | n/a | PyObject *res = PyNumber_Negative(value); |
---|
1259 | n/a | Py_DECREF(value); |
---|
1260 | n/a | SET_TOP(res); |
---|
1261 | n/a | if (res == NULL) |
---|
1262 | n/a | goto error; |
---|
1263 | n/a | DISPATCH(); |
---|
1264 | n/a | } |
---|
1265 | n/a | |
---|
1266 | n/a | TARGET(UNARY_NOT) { |
---|
1267 | n/a | PyObject *value = TOP(); |
---|
1268 | n/a | int err = PyObject_IsTrue(value); |
---|
1269 | n/a | Py_DECREF(value); |
---|
1270 | n/a | if (err == 0) { |
---|
1271 | n/a | Py_INCREF(Py_True); |
---|
1272 | n/a | SET_TOP(Py_True); |
---|
1273 | n/a | DISPATCH(); |
---|
1274 | n/a | } |
---|
1275 | n/a | else if (err > 0) { |
---|
1276 | n/a | Py_INCREF(Py_False); |
---|
1277 | n/a | SET_TOP(Py_False); |
---|
1278 | n/a | err = 0; |
---|
1279 | n/a | DISPATCH(); |
---|
1280 | n/a | } |
---|
1281 | n/a | STACKADJ(-1); |
---|
1282 | n/a | goto error; |
---|
1283 | n/a | } |
---|
1284 | n/a | |
---|
1285 | n/a | TARGET(UNARY_INVERT) { |
---|
1286 | n/a | PyObject *value = TOP(); |
---|
1287 | n/a | PyObject *res = PyNumber_Invert(value); |
---|
1288 | n/a | Py_DECREF(value); |
---|
1289 | n/a | SET_TOP(res); |
---|
1290 | n/a | if (res == NULL) |
---|
1291 | n/a | goto error; |
---|
1292 | n/a | DISPATCH(); |
---|
1293 | n/a | } |
---|
1294 | n/a | |
---|
1295 | n/a | TARGET(BINARY_POWER) { |
---|
1296 | n/a | PyObject *exp = POP(); |
---|
1297 | n/a | PyObject *base = TOP(); |
---|
1298 | n/a | PyObject *res = PyNumber_Power(base, exp, Py_None); |
---|
1299 | n/a | Py_DECREF(base); |
---|
1300 | n/a | Py_DECREF(exp); |
---|
1301 | n/a | SET_TOP(res); |
---|
1302 | n/a | if (res == NULL) |
---|
1303 | n/a | goto error; |
---|
1304 | n/a | DISPATCH(); |
---|
1305 | n/a | } |
---|
1306 | n/a | |
---|
1307 | n/a | TARGET(BINARY_MULTIPLY) { |
---|
1308 | n/a | PyObject *right = POP(); |
---|
1309 | n/a | PyObject *left = TOP(); |
---|
1310 | n/a | PyObject *res = PyNumber_Multiply(left, right); |
---|
1311 | n/a | Py_DECREF(left); |
---|
1312 | n/a | Py_DECREF(right); |
---|
1313 | n/a | SET_TOP(res); |
---|
1314 | n/a | if (res == NULL) |
---|
1315 | n/a | goto error; |
---|
1316 | n/a | DISPATCH(); |
---|
1317 | n/a | } |
---|
1318 | n/a | |
---|
1319 | n/a | TARGET(BINARY_MATRIX_MULTIPLY) { |
---|
1320 | n/a | PyObject *right = POP(); |
---|
1321 | n/a | PyObject *left = TOP(); |
---|
1322 | n/a | PyObject *res = PyNumber_MatrixMultiply(left, right); |
---|
1323 | n/a | Py_DECREF(left); |
---|
1324 | n/a | Py_DECREF(right); |
---|
1325 | n/a | SET_TOP(res); |
---|
1326 | n/a | if (res == NULL) |
---|
1327 | n/a | goto error; |
---|
1328 | n/a | DISPATCH(); |
---|
1329 | n/a | } |
---|
1330 | n/a | |
---|
1331 | n/a | TARGET(BINARY_TRUE_DIVIDE) { |
---|
1332 | n/a | PyObject *divisor = POP(); |
---|
1333 | n/a | PyObject *dividend = TOP(); |
---|
1334 | n/a | PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); |
---|
1335 | n/a | Py_DECREF(dividend); |
---|
1336 | n/a | Py_DECREF(divisor); |
---|
1337 | n/a | SET_TOP(quotient); |
---|
1338 | n/a | if (quotient == NULL) |
---|
1339 | n/a | goto error; |
---|
1340 | n/a | DISPATCH(); |
---|
1341 | n/a | } |
---|
1342 | n/a | |
---|
1343 | n/a | TARGET(BINARY_FLOOR_DIVIDE) { |
---|
1344 | n/a | PyObject *divisor = POP(); |
---|
1345 | n/a | PyObject *dividend = TOP(); |
---|
1346 | n/a | PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); |
---|
1347 | n/a | Py_DECREF(dividend); |
---|
1348 | n/a | Py_DECREF(divisor); |
---|
1349 | n/a | SET_TOP(quotient); |
---|
1350 | n/a | if (quotient == NULL) |
---|
1351 | n/a | goto error; |
---|
1352 | n/a | DISPATCH(); |
---|
1353 | n/a | } |
---|
1354 | n/a | |
---|
1355 | n/a | TARGET(BINARY_MODULO) { |
---|
1356 | n/a | PyObject *divisor = POP(); |
---|
1357 | n/a | PyObject *dividend = TOP(); |
---|
1358 | n/a | PyObject *res = PyUnicode_CheckExact(dividend) ? |
---|
1359 | n/a | PyUnicode_Format(dividend, divisor) : |
---|
1360 | n/a | PyNumber_Remainder(dividend, divisor); |
---|
1361 | n/a | Py_DECREF(divisor); |
---|
1362 | n/a | Py_DECREF(dividend); |
---|
1363 | n/a | SET_TOP(res); |
---|
1364 | n/a | if (res == NULL) |
---|
1365 | n/a | goto error; |
---|
1366 | n/a | DISPATCH(); |
---|
1367 | n/a | } |
---|
1368 | n/a | |
---|
1369 | n/a | TARGET(BINARY_ADD) { |
---|
1370 | n/a | PyObject *right = POP(); |
---|
1371 | n/a | PyObject *left = TOP(); |
---|
1372 | n/a | PyObject *sum; |
---|
1373 | n/a | /* NOTE(haypo): Please don't try to micro-optimize int+int on |
---|
1374 | n/a | CPython using bytecode, it is simply worthless. |
---|
1375 | n/a | See http://bugs.python.org/issue21955 and |
---|
1376 | n/a | http://bugs.python.org/issue10044 for the discussion. In short, |
---|
1377 | n/a | no patch shown any impact on a realistic benchmark, only a minor |
---|
1378 | n/a | speedup on microbenchmarks. */ |
---|
1379 | n/a | if (PyUnicode_CheckExact(left) && |
---|
1380 | n/a | PyUnicode_CheckExact(right)) { |
---|
1381 | n/a | sum = unicode_concatenate(left, right, f, next_instr); |
---|
1382 | n/a | /* unicode_concatenate consumed the ref to left */ |
---|
1383 | n/a | } |
---|
1384 | n/a | else { |
---|
1385 | n/a | sum = PyNumber_Add(left, right); |
---|
1386 | n/a | Py_DECREF(left); |
---|
1387 | n/a | } |
---|
1388 | n/a | Py_DECREF(right); |
---|
1389 | n/a | SET_TOP(sum); |
---|
1390 | n/a | if (sum == NULL) |
---|
1391 | n/a | goto error; |
---|
1392 | n/a | DISPATCH(); |
---|
1393 | n/a | } |
---|
1394 | n/a | |
---|
1395 | n/a | TARGET(BINARY_SUBTRACT) { |
---|
1396 | n/a | PyObject *right = POP(); |
---|
1397 | n/a | PyObject *left = TOP(); |
---|
1398 | n/a | PyObject *diff = PyNumber_Subtract(left, right); |
---|
1399 | n/a | Py_DECREF(right); |
---|
1400 | n/a | Py_DECREF(left); |
---|
1401 | n/a | SET_TOP(diff); |
---|
1402 | n/a | if (diff == NULL) |
---|
1403 | n/a | goto error; |
---|
1404 | n/a | DISPATCH(); |
---|
1405 | n/a | } |
---|
1406 | n/a | |
---|
1407 | n/a | TARGET(BINARY_SUBSCR) { |
---|
1408 | n/a | PyObject *sub = POP(); |
---|
1409 | n/a | PyObject *container = TOP(); |
---|
1410 | n/a | PyObject *res = PyObject_GetItem(container, sub); |
---|
1411 | n/a | Py_DECREF(container); |
---|
1412 | n/a | Py_DECREF(sub); |
---|
1413 | n/a | SET_TOP(res); |
---|
1414 | n/a | if (res == NULL) |
---|
1415 | n/a | goto error; |
---|
1416 | n/a | DISPATCH(); |
---|
1417 | n/a | } |
---|
1418 | n/a | |
---|
1419 | n/a | TARGET(BINARY_LSHIFT) { |
---|
1420 | n/a | PyObject *right = POP(); |
---|
1421 | n/a | PyObject *left = TOP(); |
---|
1422 | n/a | PyObject *res = PyNumber_Lshift(left, right); |
---|
1423 | n/a | Py_DECREF(left); |
---|
1424 | n/a | Py_DECREF(right); |
---|
1425 | n/a | SET_TOP(res); |
---|
1426 | n/a | if (res == NULL) |
---|
1427 | n/a | goto error; |
---|
1428 | n/a | DISPATCH(); |
---|
1429 | n/a | } |
---|
1430 | n/a | |
---|
1431 | n/a | TARGET(BINARY_RSHIFT) { |
---|
1432 | n/a | PyObject *right = POP(); |
---|
1433 | n/a | PyObject *left = TOP(); |
---|
1434 | n/a | PyObject *res = PyNumber_Rshift(left, right); |
---|
1435 | n/a | Py_DECREF(left); |
---|
1436 | n/a | Py_DECREF(right); |
---|
1437 | n/a | SET_TOP(res); |
---|
1438 | n/a | if (res == NULL) |
---|
1439 | n/a | goto error; |
---|
1440 | n/a | DISPATCH(); |
---|
1441 | n/a | } |
---|
1442 | n/a | |
---|
1443 | n/a | TARGET(BINARY_AND) { |
---|
1444 | n/a | PyObject *right = POP(); |
---|
1445 | n/a | PyObject *left = TOP(); |
---|
1446 | n/a | PyObject *res = PyNumber_And(left, right); |
---|
1447 | n/a | Py_DECREF(left); |
---|
1448 | n/a | Py_DECREF(right); |
---|
1449 | n/a | SET_TOP(res); |
---|
1450 | n/a | if (res == NULL) |
---|
1451 | n/a | goto error; |
---|
1452 | n/a | DISPATCH(); |
---|
1453 | n/a | } |
---|
1454 | n/a | |
---|
1455 | n/a | TARGET(BINARY_XOR) { |
---|
1456 | n/a | PyObject *right = POP(); |
---|
1457 | n/a | PyObject *left = TOP(); |
---|
1458 | n/a | PyObject *res = PyNumber_Xor(left, right); |
---|
1459 | n/a | Py_DECREF(left); |
---|
1460 | n/a | Py_DECREF(right); |
---|
1461 | n/a | SET_TOP(res); |
---|
1462 | n/a | if (res == NULL) |
---|
1463 | n/a | goto error; |
---|
1464 | n/a | DISPATCH(); |
---|
1465 | n/a | } |
---|
1466 | n/a | |
---|
1467 | n/a | TARGET(BINARY_OR) { |
---|
1468 | n/a | PyObject *right = POP(); |
---|
1469 | n/a | PyObject *left = TOP(); |
---|
1470 | n/a | PyObject *res = PyNumber_Or(left, right); |
---|
1471 | n/a | Py_DECREF(left); |
---|
1472 | n/a | Py_DECREF(right); |
---|
1473 | n/a | SET_TOP(res); |
---|
1474 | n/a | if (res == NULL) |
---|
1475 | n/a | goto error; |
---|
1476 | n/a | DISPATCH(); |
---|
1477 | n/a | } |
---|
1478 | n/a | |
---|
1479 | n/a | TARGET(LIST_APPEND) { |
---|
1480 | n/a | PyObject *v = POP(); |
---|
1481 | n/a | PyObject *list = PEEK(oparg); |
---|
1482 | n/a | int err; |
---|
1483 | n/a | err = PyList_Append(list, v); |
---|
1484 | n/a | Py_DECREF(v); |
---|
1485 | n/a | if (err != 0) |
---|
1486 | n/a | goto error; |
---|
1487 | n/a | PREDICT(JUMP_ABSOLUTE); |
---|
1488 | n/a | DISPATCH(); |
---|
1489 | n/a | } |
---|
1490 | n/a | |
---|
1491 | n/a | TARGET(SET_ADD) { |
---|
1492 | n/a | PyObject *v = POP(); |
---|
1493 | n/a | PyObject *set = PEEK(oparg); |
---|
1494 | n/a | int err; |
---|
1495 | n/a | err = PySet_Add(set, v); |
---|
1496 | n/a | Py_DECREF(v); |
---|
1497 | n/a | if (err != 0) |
---|
1498 | n/a | goto error; |
---|
1499 | n/a | PREDICT(JUMP_ABSOLUTE); |
---|
1500 | n/a | DISPATCH(); |
---|
1501 | n/a | } |
---|
1502 | n/a | |
---|
1503 | n/a | TARGET(INPLACE_POWER) { |
---|
1504 | n/a | PyObject *exp = POP(); |
---|
1505 | n/a | PyObject *base = TOP(); |
---|
1506 | n/a | PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); |
---|
1507 | n/a | Py_DECREF(base); |
---|
1508 | n/a | Py_DECREF(exp); |
---|
1509 | n/a | SET_TOP(res); |
---|
1510 | n/a | if (res == NULL) |
---|
1511 | n/a | goto error; |
---|
1512 | n/a | DISPATCH(); |
---|
1513 | n/a | } |
---|
1514 | n/a | |
---|
1515 | n/a | TARGET(INPLACE_MULTIPLY) { |
---|
1516 | n/a | PyObject *right = POP(); |
---|
1517 | n/a | PyObject *left = TOP(); |
---|
1518 | n/a | PyObject *res = PyNumber_InPlaceMultiply(left, right); |
---|
1519 | n/a | Py_DECREF(left); |
---|
1520 | n/a | Py_DECREF(right); |
---|
1521 | n/a | SET_TOP(res); |
---|
1522 | n/a | if (res == NULL) |
---|
1523 | n/a | goto error; |
---|
1524 | n/a | DISPATCH(); |
---|
1525 | n/a | } |
---|
1526 | n/a | |
---|
1527 | n/a | TARGET(INPLACE_MATRIX_MULTIPLY) { |
---|
1528 | n/a | PyObject *right = POP(); |
---|
1529 | n/a | PyObject *left = TOP(); |
---|
1530 | n/a | PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); |
---|
1531 | n/a | Py_DECREF(left); |
---|
1532 | n/a | Py_DECREF(right); |
---|
1533 | n/a | SET_TOP(res); |
---|
1534 | n/a | if (res == NULL) |
---|
1535 | n/a | goto error; |
---|
1536 | n/a | DISPATCH(); |
---|
1537 | n/a | } |
---|
1538 | n/a | |
---|
1539 | n/a | TARGET(INPLACE_TRUE_DIVIDE) { |
---|
1540 | n/a | PyObject *divisor = POP(); |
---|
1541 | n/a | PyObject *dividend = TOP(); |
---|
1542 | n/a | PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); |
---|
1543 | n/a | Py_DECREF(dividend); |
---|
1544 | n/a | Py_DECREF(divisor); |
---|
1545 | n/a | SET_TOP(quotient); |
---|
1546 | n/a | if (quotient == NULL) |
---|
1547 | n/a | goto error; |
---|
1548 | n/a | DISPATCH(); |
---|
1549 | n/a | } |
---|
1550 | n/a | |
---|
1551 | n/a | TARGET(INPLACE_FLOOR_DIVIDE) { |
---|
1552 | n/a | PyObject *divisor = POP(); |
---|
1553 | n/a | PyObject *dividend = TOP(); |
---|
1554 | n/a | PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); |
---|
1555 | n/a | Py_DECREF(dividend); |
---|
1556 | n/a | Py_DECREF(divisor); |
---|
1557 | n/a | SET_TOP(quotient); |
---|
1558 | n/a | if (quotient == NULL) |
---|
1559 | n/a | goto error; |
---|
1560 | n/a | DISPATCH(); |
---|
1561 | n/a | } |
---|
1562 | n/a | |
---|
1563 | n/a | TARGET(INPLACE_MODULO) { |
---|
1564 | n/a | PyObject *right = POP(); |
---|
1565 | n/a | PyObject *left = TOP(); |
---|
1566 | n/a | PyObject *mod = PyNumber_InPlaceRemainder(left, right); |
---|
1567 | n/a | Py_DECREF(left); |
---|
1568 | n/a | Py_DECREF(right); |
---|
1569 | n/a | SET_TOP(mod); |
---|
1570 | n/a | if (mod == NULL) |
---|
1571 | n/a | goto error; |
---|
1572 | n/a | DISPATCH(); |
---|
1573 | n/a | } |
---|
1574 | n/a | |
---|
1575 | n/a | TARGET(INPLACE_ADD) { |
---|
1576 | n/a | PyObject *right = POP(); |
---|
1577 | n/a | PyObject *left = TOP(); |
---|
1578 | n/a | PyObject *sum; |
---|
1579 | n/a | if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { |
---|
1580 | n/a | sum = unicode_concatenate(left, right, f, next_instr); |
---|
1581 | n/a | /* unicode_concatenate consumed the ref to left */ |
---|
1582 | n/a | } |
---|
1583 | n/a | else { |
---|
1584 | n/a | sum = PyNumber_InPlaceAdd(left, right); |
---|
1585 | n/a | Py_DECREF(left); |
---|
1586 | n/a | } |
---|
1587 | n/a | Py_DECREF(right); |
---|
1588 | n/a | SET_TOP(sum); |
---|
1589 | n/a | if (sum == NULL) |
---|
1590 | n/a | goto error; |
---|
1591 | n/a | DISPATCH(); |
---|
1592 | n/a | } |
---|
1593 | n/a | |
---|
1594 | n/a | TARGET(INPLACE_SUBTRACT) { |
---|
1595 | n/a | PyObject *right = POP(); |
---|
1596 | n/a | PyObject *left = TOP(); |
---|
1597 | n/a | PyObject *diff = PyNumber_InPlaceSubtract(left, right); |
---|
1598 | n/a | Py_DECREF(left); |
---|
1599 | n/a | Py_DECREF(right); |
---|
1600 | n/a | SET_TOP(diff); |
---|
1601 | n/a | if (diff == NULL) |
---|
1602 | n/a | goto error; |
---|
1603 | n/a | DISPATCH(); |
---|
1604 | n/a | } |
---|
1605 | n/a | |
---|
1606 | n/a | TARGET(INPLACE_LSHIFT) { |
---|
1607 | n/a | PyObject *right = POP(); |
---|
1608 | n/a | PyObject *left = TOP(); |
---|
1609 | n/a | PyObject *res = PyNumber_InPlaceLshift(left, right); |
---|
1610 | n/a | Py_DECREF(left); |
---|
1611 | n/a | Py_DECREF(right); |
---|
1612 | n/a | SET_TOP(res); |
---|
1613 | n/a | if (res == NULL) |
---|
1614 | n/a | goto error; |
---|
1615 | n/a | DISPATCH(); |
---|
1616 | n/a | } |
---|
1617 | n/a | |
---|
1618 | n/a | TARGET(INPLACE_RSHIFT) { |
---|
1619 | n/a | PyObject *right = POP(); |
---|
1620 | n/a | PyObject *left = TOP(); |
---|
1621 | n/a | PyObject *res = PyNumber_InPlaceRshift(left, right); |
---|
1622 | n/a | Py_DECREF(left); |
---|
1623 | n/a | Py_DECREF(right); |
---|
1624 | n/a | SET_TOP(res); |
---|
1625 | n/a | if (res == NULL) |
---|
1626 | n/a | goto error; |
---|
1627 | n/a | DISPATCH(); |
---|
1628 | n/a | } |
---|
1629 | n/a | |
---|
1630 | n/a | TARGET(INPLACE_AND) { |
---|
1631 | n/a | PyObject *right = POP(); |
---|
1632 | n/a | PyObject *left = TOP(); |
---|
1633 | n/a | PyObject *res = PyNumber_InPlaceAnd(left, right); |
---|
1634 | n/a | Py_DECREF(left); |
---|
1635 | n/a | Py_DECREF(right); |
---|
1636 | n/a | SET_TOP(res); |
---|
1637 | n/a | if (res == NULL) |
---|
1638 | n/a | goto error; |
---|
1639 | n/a | DISPATCH(); |
---|
1640 | n/a | } |
---|
1641 | n/a | |
---|
1642 | n/a | TARGET(INPLACE_XOR) { |
---|
1643 | n/a | PyObject *right = POP(); |
---|
1644 | n/a | PyObject *left = TOP(); |
---|
1645 | n/a | PyObject *res = PyNumber_InPlaceXor(left, right); |
---|
1646 | n/a | Py_DECREF(left); |
---|
1647 | n/a | Py_DECREF(right); |
---|
1648 | n/a | SET_TOP(res); |
---|
1649 | n/a | if (res == NULL) |
---|
1650 | n/a | goto error; |
---|
1651 | n/a | DISPATCH(); |
---|
1652 | n/a | } |
---|
1653 | n/a | |
---|
1654 | n/a | TARGET(INPLACE_OR) { |
---|
1655 | n/a | PyObject *right = POP(); |
---|
1656 | n/a | PyObject *left = TOP(); |
---|
1657 | n/a | PyObject *res = PyNumber_InPlaceOr(left, right); |
---|
1658 | n/a | Py_DECREF(left); |
---|
1659 | n/a | Py_DECREF(right); |
---|
1660 | n/a | SET_TOP(res); |
---|
1661 | n/a | if (res == NULL) |
---|
1662 | n/a | goto error; |
---|
1663 | n/a | DISPATCH(); |
---|
1664 | n/a | } |
---|
1665 | n/a | |
---|
1666 | n/a | TARGET(STORE_SUBSCR) { |
---|
1667 | n/a | PyObject *sub = TOP(); |
---|
1668 | n/a | PyObject *container = SECOND(); |
---|
1669 | n/a | PyObject *v = THIRD(); |
---|
1670 | n/a | int err; |
---|
1671 | n/a | STACKADJ(-3); |
---|
1672 | n/a | /* container[sub] = v */ |
---|
1673 | n/a | err = PyObject_SetItem(container, sub, v); |
---|
1674 | n/a | Py_DECREF(v); |
---|
1675 | n/a | Py_DECREF(container); |
---|
1676 | n/a | Py_DECREF(sub); |
---|
1677 | n/a | if (err != 0) |
---|
1678 | n/a | goto error; |
---|
1679 | n/a | DISPATCH(); |
---|
1680 | n/a | } |
---|
1681 | n/a | |
---|
1682 | n/a | TARGET(STORE_ANNOTATION) { |
---|
1683 | n/a | _Py_IDENTIFIER(__annotations__); |
---|
1684 | n/a | PyObject *ann_dict; |
---|
1685 | n/a | PyObject *ann = POP(); |
---|
1686 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
1687 | n/a | int err; |
---|
1688 | n/a | if (f->f_locals == NULL) { |
---|
1689 | n/a | PyErr_Format(PyExc_SystemError, |
---|
1690 | n/a | "no locals found when storing annotation"); |
---|
1691 | n/a | Py_DECREF(ann); |
---|
1692 | n/a | goto error; |
---|
1693 | n/a | } |
---|
1694 | n/a | /* first try to get __annotations__ from locals... */ |
---|
1695 | n/a | if (PyDict_CheckExact(f->f_locals)) { |
---|
1696 | n/a | ann_dict = _PyDict_GetItemId(f->f_locals, |
---|
1697 | n/a | &PyId___annotations__); |
---|
1698 | n/a | if (ann_dict == NULL) { |
---|
1699 | n/a | PyErr_SetString(PyExc_NameError, |
---|
1700 | n/a | "__annotations__ not found"); |
---|
1701 | n/a | Py_DECREF(ann); |
---|
1702 | n/a | goto error; |
---|
1703 | n/a | } |
---|
1704 | n/a | Py_INCREF(ann_dict); |
---|
1705 | n/a | } |
---|
1706 | n/a | else { |
---|
1707 | n/a | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
---|
1708 | n/a | if (ann_str == NULL) { |
---|
1709 | n/a | Py_DECREF(ann); |
---|
1710 | n/a | goto error; |
---|
1711 | n/a | } |
---|
1712 | n/a | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
---|
1713 | n/a | if (ann_dict == NULL) { |
---|
1714 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
---|
1715 | n/a | PyErr_SetString(PyExc_NameError, |
---|
1716 | n/a | "__annotations__ not found"); |
---|
1717 | n/a | } |
---|
1718 | n/a | Py_DECREF(ann); |
---|
1719 | n/a | goto error; |
---|
1720 | n/a | } |
---|
1721 | n/a | } |
---|
1722 | n/a | /* ...if succeeded, __annotations__[name] = ann */ |
---|
1723 | n/a | if (PyDict_CheckExact(ann_dict)) { |
---|
1724 | n/a | err = PyDict_SetItem(ann_dict, name, ann); |
---|
1725 | n/a | } |
---|
1726 | n/a | else { |
---|
1727 | n/a | err = PyObject_SetItem(ann_dict, name, ann); |
---|
1728 | n/a | } |
---|
1729 | n/a | Py_DECREF(ann_dict); |
---|
1730 | n/a | Py_DECREF(ann); |
---|
1731 | n/a | if (err != 0) { |
---|
1732 | n/a | goto error; |
---|
1733 | n/a | } |
---|
1734 | n/a | DISPATCH(); |
---|
1735 | n/a | } |
---|
1736 | n/a | |
---|
1737 | n/a | TARGET(DELETE_SUBSCR) { |
---|
1738 | n/a | PyObject *sub = TOP(); |
---|
1739 | n/a | PyObject *container = SECOND(); |
---|
1740 | n/a | int err; |
---|
1741 | n/a | STACKADJ(-2); |
---|
1742 | n/a | /* del container[sub] */ |
---|
1743 | n/a | err = PyObject_DelItem(container, sub); |
---|
1744 | n/a | Py_DECREF(container); |
---|
1745 | n/a | Py_DECREF(sub); |
---|
1746 | n/a | if (err != 0) |
---|
1747 | n/a | goto error; |
---|
1748 | n/a | DISPATCH(); |
---|
1749 | n/a | } |
---|
1750 | n/a | |
---|
1751 | n/a | TARGET(PRINT_EXPR) { |
---|
1752 | n/a | _Py_IDENTIFIER(displayhook); |
---|
1753 | n/a | PyObject *value = POP(); |
---|
1754 | n/a | PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); |
---|
1755 | n/a | PyObject *res; |
---|
1756 | n/a | if (hook == NULL) { |
---|
1757 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
1758 | n/a | "lost sys.displayhook"); |
---|
1759 | n/a | Py_DECREF(value); |
---|
1760 | n/a | goto error; |
---|
1761 | n/a | } |
---|
1762 | n/a | res = PyObject_CallFunctionObjArgs(hook, value, NULL); |
---|
1763 | n/a | Py_DECREF(value); |
---|
1764 | n/a | if (res == NULL) |
---|
1765 | n/a | goto error; |
---|
1766 | n/a | Py_DECREF(res); |
---|
1767 | n/a | DISPATCH(); |
---|
1768 | n/a | } |
---|
1769 | n/a | |
---|
1770 | n/a | #ifdef CASE_TOO_BIG |
---|
1771 | n/a | default: switch (opcode) { |
---|
1772 | n/a | #endif |
---|
1773 | n/a | TARGET(RAISE_VARARGS) { |
---|
1774 | n/a | PyObject *cause = NULL, *exc = NULL; |
---|
1775 | n/a | switch (oparg) { |
---|
1776 | n/a | case 2: |
---|
1777 | n/a | cause = POP(); /* cause */ |
---|
1778 | n/a | case 1: |
---|
1779 | n/a | exc = POP(); /* exc */ |
---|
1780 | n/a | case 0: /* Fallthrough */ |
---|
1781 | n/a | if (do_raise(exc, cause)) { |
---|
1782 | n/a | why = WHY_EXCEPTION; |
---|
1783 | n/a | goto fast_block_end; |
---|
1784 | n/a | } |
---|
1785 | n/a | break; |
---|
1786 | n/a | default: |
---|
1787 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1788 | n/a | "bad RAISE_VARARGS oparg"); |
---|
1789 | n/a | break; |
---|
1790 | n/a | } |
---|
1791 | n/a | goto error; |
---|
1792 | n/a | } |
---|
1793 | n/a | |
---|
1794 | n/a | TARGET(RETURN_VALUE) { |
---|
1795 | n/a | retval = POP(); |
---|
1796 | n/a | why = WHY_RETURN; |
---|
1797 | n/a | goto fast_block_end; |
---|
1798 | n/a | } |
---|
1799 | n/a | |
---|
1800 | n/a | TARGET(GET_AITER) { |
---|
1801 | n/a | unaryfunc getter = NULL; |
---|
1802 | n/a | PyObject *iter = NULL; |
---|
1803 | n/a | PyObject *awaitable = NULL; |
---|
1804 | n/a | PyObject *obj = TOP(); |
---|
1805 | n/a | PyTypeObject *type = Py_TYPE(obj); |
---|
1806 | n/a | |
---|
1807 | n/a | if (type->tp_as_async != NULL) { |
---|
1808 | n/a | getter = type->tp_as_async->am_aiter; |
---|
1809 | n/a | } |
---|
1810 | n/a | |
---|
1811 | n/a | if (getter != NULL) { |
---|
1812 | n/a | iter = (*getter)(obj); |
---|
1813 | n/a | Py_DECREF(obj); |
---|
1814 | n/a | if (iter == NULL) { |
---|
1815 | n/a | SET_TOP(NULL); |
---|
1816 | n/a | goto error; |
---|
1817 | n/a | } |
---|
1818 | n/a | } |
---|
1819 | n/a | else { |
---|
1820 | n/a | SET_TOP(NULL); |
---|
1821 | n/a | PyErr_Format( |
---|
1822 | n/a | PyExc_TypeError, |
---|
1823 | n/a | "'async for' requires an object with " |
---|
1824 | n/a | "__aiter__ method, got %.100s", |
---|
1825 | n/a | type->tp_name); |
---|
1826 | n/a | Py_DECREF(obj); |
---|
1827 | n/a | goto error; |
---|
1828 | n/a | } |
---|
1829 | n/a | |
---|
1830 | n/a | if (Py_TYPE(iter)->tp_as_async != NULL && |
---|
1831 | n/a | Py_TYPE(iter)->tp_as_async->am_anext != NULL) { |
---|
1832 | n/a | |
---|
1833 | n/a | /* Starting with CPython 3.5.2 __aiter__ should return |
---|
1834 | n/a | asynchronous iterators directly (not awaitables that |
---|
1835 | n/a | resolve to asynchronous iterators.) |
---|
1836 | n/a | |
---|
1837 | n/a | Therefore, we check if the object that was returned |
---|
1838 | n/a | from __aiter__ has an __anext__ method. If it does, |
---|
1839 | n/a | we wrap it in an awaitable that resolves to `iter`. |
---|
1840 | n/a | |
---|
1841 | n/a | See http://bugs.python.org/issue27243 for more |
---|
1842 | n/a | details. |
---|
1843 | n/a | */ |
---|
1844 | n/a | |
---|
1845 | n/a | PyObject *wrapper = _PyAIterWrapper_New(iter); |
---|
1846 | n/a | Py_DECREF(iter); |
---|
1847 | n/a | SET_TOP(wrapper); |
---|
1848 | n/a | DISPATCH(); |
---|
1849 | n/a | } |
---|
1850 | n/a | |
---|
1851 | n/a | awaitable = _PyCoro_GetAwaitableIter(iter); |
---|
1852 | n/a | if (awaitable == NULL) { |
---|
1853 | n/a | SET_TOP(NULL); |
---|
1854 | n/a | PyErr_Format( |
---|
1855 | n/a | PyExc_TypeError, |
---|
1856 | n/a | "'async for' received an invalid object " |
---|
1857 | n/a | "from __aiter__: %.100s", |
---|
1858 | n/a | Py_TYPE(iter)->tp_name); |
---|
1859 | n/a | |
---|
1860 | n/a | Py_DECREF(iter); |
---|
1861 | n/a | goto error; |
---|
1862 | n/a | } else { |
---|
1863 | n/a | Py_DECREF(iter); |
---|
1864 | n/a | |
---|
1865 | n/a | if (PyErr_WarnFormat( |
---|
1866 | n/a | PyExc_DeprecationWarning, 1, |
---|
1867 | n/a | "'%.100s' implements legacy __aiter__ protocol; " |
---|
1868 | n/a | "__aiter__ should return an asynchronous " |
---|
1869 | n/a | "iterator, not awaitable", |
---|
1870 | n/a | type->tp_name)) |
---|
1871 | n/a | { |
---|
1872 | n/a | /* Warning was converted to an error. */ |
---|
1873 | n/a | Py_DECREF(awaitable); |
---|
1874 | n/a | SET_TOP(NULL); |
---|
1875 | n/a | goto error; |
---|
1876 | n/a | } |
---|
1877 | n/a | } |
---|
1878 | n/a | |
---|
1879 | n/a | SET_TOP(awaitable); |
---|
1880 | n/a | PREDICT(LOAD_CONST); |
---|
1881 | n/a | DISPATCH(); |
---|
1882 | n/a | } |
---|
1883 | n/a | |
---|
1884 | n/a | TARGET(GET_ANEXT) { |
---|
1885 | n/a | unaryfunc getter = NULL; |
---|
1886 | n/a | PyObject *next_iter = NULL; |
---|
1887 | n/a | PyObject *awaitable = NULL; |
---|
1888 | n/a | PyObject *aiter = TOP(); |
---|
1889 | n/a | PyTypeObject *type = Py_TYPE(aiter); |
---|
1890 | n/a | |
---|
1891 | n/a | if (PyAsyncGen_CheckExact(aiter)) { |
---|
1892 | n/a | awaitable = type->tp_as_async->am_anext(aiter); |
---|
1893 | n/a | if (awaitable == NULL) { |
---|
1894 | n/a | goto error; |
---|
1895 | n/a | } |
---|
1896 | n/a | } else { |
---|
1897 | n/a | if (type->tp_as_async != NULL){ |
---|
1898 | n/a | getter = type->tp_as_async->am_anext; |
---|
1899 | n/a | } |
---|
1900 | n/a | |
---|
1901 | n/a | if (getter != NULL) { |
---|
1902 | n/a | next_iter = (*getter)(aiter); |
---|
1903 | n/a | if (next_iter == NULL) { |
---|
1904 | n/a | goto error; |
---|
1905 | n/a | } |
---|
1906 | n/a | } |
---|
1907 | n/a | else { |
---|
1908 | n/a | PyErr_Format( |
---|
1909 | n/a | PyExc_TypeError, |
---|
1910 | n/a | "'async for' requires an iterator with " |
---|
1911 | n/a | "__anext__ method, got %.100s", |
---|
1912 | n/a | type->tp_name); |
---|
1913 | n/a | goto error; |
---|
1914 | n/a | } |
---|
1915 | n/a | |
---|
1916 | n/a | awaitable = _PyCoro_GetAwaitableIter(next_iter); |
---|
1917 | n/a | if (awaitable == NULL) { |
---|
1918 | n/a | PyErr_Format( |
---|
1919 | n/a | PyExc_TypeError, |
---|
1920 | n/a | "'async for' received an invalid object " |
---|
1921 | n/a | "from __anext__: %.100s", |
---|
1922 | n/a | Py_TYPE(next_iter)->tp_name); |
---|
1923 | n/a | |
---|
1924 | n/a | Py_DECREF(next_iter); |
---|
1925 | n/a | goto error; |
---|
1926 | n/a | } else { |
---|
1927 | n/a | Py_DECREF(next_iter); |
---|
1928 | n/a | } |
---|
1929 | n/a | } |
---|
1930 | n/a | |
---|
1931 | n/a | PUSH(awaitable); |
---|
1932 | n/a | PREDICT(LOAD_CONST); |
---|
1933 | n/a | DISPATCH(); |
---|
1934 | n/a | } |
---|
1935 | n/a | |
---|
1936 | n/a | PREDICTED(GET_AWAITABLE); |
---|
1937 | n/a | TARGET(GET_AWAITABLE) { |
---|
1938 | n/a | PyObject *iterable = TOP(); |
---|
1939 | n/a | PyObject *iter = _PyCoro_GetAwaitableIter(iterable); |
---|
1940 | n/a | |
---|
1941 | n/a | Py_DECREF(iterable); |
---|
1942 | n/a | |
---|
1943 | n/a | if (iter != NULL && PyCoro_CheckExact(iter)) { |
---|
1944 | n/a | PyObject *yf = _PyGen_yf((PyGenObject*)iter); |
---|
1945 | n/a | if (yf != NULL) { |
---|
1946 | n/a | /* `iter` is a coroutine object that is being |
---|
1947 | n/a | awaited, `yf` is a pointer to the current awaitable |
---|
1948 | n/a | being awaited on. */ |
---|
1949 | n/a | Py_DECREF(yf); |
---|
1950 | n/a | Py_CLEAR(iter); |
---|
1951 | n/a | PyErr_SetString( |
---|
1952 | n/a | PyExc_RuntimeError, |
---|
1953 | n/a | "coroutine is being awaited already"); |
---|
1954 | n/a | /* The code below jumps to `error` if `iter` is NULL. */ |
---|
1955 | n/a | } |
---|
1956 | n/a | } |
---|
1957 | n/a | |
---|
1958 | n/a | SET_TOP(iter); /* Even if it's NULL */ |
---|
1959 | n/a | |
---|
1960 | n/a | if (iter == NULL) { |
---|
1961 | n/a | goto error; |
---|
1962 | n/a | } |
---|
1963 | n/a | |
---|
1964 | n/a | PREDICT(LOAD_CONST); |
---|
1965 | n/a | DISPATCH(); |
---|
1966 | n/a | } |
---|
1967 | n/a | |
---|
1968 | n/a | TARGET(YIELD_FROM) { |
---|
1969 | n/a | PyObject *v = POP(); |
---|
1970 | n/a | PyObject *receiver = TOP(); |
---|
1971 | n/a | int err; |
---|
1972 | n/a | if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) { |
---|
1973 | n/a | retval = _PyGen_Send((PyGenObject *)receiver, v); |
---|
1974 | n/a | } else { |
---|
1975 | n/a | _Py_IDENTIFIER(send); |
---|
1976 | n/a | if (v == Py_None) |
---|
1977 | n/a | retval = Py_TYPE(receiver)->tp_iternext(receiver); |
---|
1978 | n/a | else |
---|
1979 | n/a | retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL); |
---|
1980 | n/a | } |
---|
1981 | n/a | Py_DECREF(v); |
---|
1982 | n/a | if (retval == NULL) { |
---|
1983 | n/a | PyObject *val; |
---|
1984 | n/a | if (tstate->c_tracefunc != NULL |
---|
1985 | n/a | && PyErr_ExceptionMatches(PyExc_StopIteration)) |
---|
1986 | n/a | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
---|
1987 | n/a | err = _PyGen_FetchStopIterationValue(&val); |
---|
1988 | n/a | if (err < 0) |
---|
1989 | n/a | goto error; |
---|
1990 | n/a | Py_DECREF(receiver); |
---|
1991 | n/a | SET_TOP(val); |
---|
1992 | n/a | DISPATCH(); |
---|
1993 | n/a | } |
---|
1994 | n/a | /* receiver remains on stack, retval is value to be yielded */ |
---|
1995 | n/a | f->f_stacktop = stack_pointer; |
---|
1996 | n/a | why = WHY_YIELD; |
---|
1997 | n/a | /* and repeat... */ |
---|
1998 | n/a | assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT)); |
---|
1999 | n/a | f->f_lasti -= sizeof(_Py_CODEUNIT); |
---|
2000 | n/a | goto fast_yield; |
---|
2001 | n/a | } |
---|
2002 | n/a | |
---|
2003 | n/a | TARGET(YIELD_VALUE) { |
---|
2004 | n/a | retval = POP(); |
---|
2005 | n/a | |
---|
2006 | n/a | if (co->co_flags & CO_ASYNC_GENERATOR) { |
---|
2007 | n/a | PyObject *w = _PyAsyncGenValueWrapperNew(retval); |
---|
2008 | n/a | Py_DECREF(retval); |
---|
2009 | n/a | if (w == NULL) { |
---|
2010 | n/a | retval = NULL; |
---|
2011 | n/a | goto error; |
---|
2012 | n/a | } |
---|
2013 | n/a | retval = w; |
---|
2014 | n/a | } |
---|
2015 | n/a | |
---|
2016 | n/a | f->f_stacktop = stack_pointer; |
---|
2017 | n/a | why = WHY_YIELD; |
---|
2018 | n/a | goto fast_yield; |
---|
2019 | n/a | } |
---|
2020 | n/a | |
---|
2021 | n/a | TARGET(POP_EXCEPT) { |
---|
2022 | n/a | PyTryBlock *b = PyFrame_BlockPop(f); |
---|
2023 | n/a | if (b->b_type != EXCEPT_HANDLER) { |
---|
2024 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
2025 | n/a | "popped block is not an except handler"); |
---|
2026 | n/a | goto error; |
---|
2027 | n/a | } |
---|
2028 | n/a | UNWIND_EXCEPT_HANDLER(b); |
---|
2029 | n/a | DISPATCH(); |
---|
2030 | n/a | } |
---|
2031 | n/a | |
---|
2032 | n/a | PREDICTED(POP_BLOCK); |
---|
2033 | n/a | TARGET(POP_BLOCK) { |
---|
2034 | n/a | PyTryBlock *b = PyFrame_BlockPop(f); |
---|
2035 | n/a | UNWIND_BLOCK(b); |
---|
2036 | n/a | DISPATCH(); |
---|
2037 | n/a | } |
---|
2038 | n/a | |
---|
2039 | n/a | PREDICTED(END_FINALLY); |
---|
2040 | n/a | TARGET(END_FINALLY) { |
---|
2041 | n/a | PyObject *status = POP(); |
---|
2042 | n/a | if (PyLong_Check(status)) { |
---|
2043 | n/a | why = (enum why_code) PyLong_AS_LONG(status); |
---|
2044 | n/a | assert(why != WHY_YIELD && why != WHY_EXCEPTION); |
---|
2045 | n/a | if (why == WHY_RETURN || |
---|
2046 | n/a | why == WHY_CONTINUE) |
---|
2047 | n/a | retval = POP(); |
---|
2048 | n/a | if (why == WHY_SILENCED) { |
---|
2049 | n/a | /* An exception was silenced by 'with', we must |
---|
2050 | n/a | manually unwind the EXCEPT_HANDLER block which was |
---|
2051 | n/a | created when the exception was caught, otherwise |
---|
2052 | n/a | the stack will be in an inconsistent state. */ |
---|
2053 | n/a | PyTryBlock *b = PyFrame_BlockPop(f); |
---|
2054 | n/a | assert(b->b_type == EXCEPT_HANDLER); |
---|
2055 | n/a | UNWIND_EXCEPT_HANDLER(b); |
---|
2056 | n/a | why = WHY_NOT; |
---|
2057 | n/a | Py_DECREF(status); |
---|
2058 | n/a | DISPATCH(); |
---|
2059 | n/a | } |
---|
2060 | n/a | Py_DECREF(status); |
---|
2061 | n/a | goto fast_block_end; |
---|
2062 | n/a | } |
---|
2063 | n/a | else if (PyExceptionClass_Check(status)) { |
---|
2064 | n/a | PyObject *exc = POP(); |
---|
2065 | n/a | PyObject *tb = POP(); |
---|
2066 | n/a | PyErr_Restore(status, exc, tb); |
---|
2067 | n/a | why = WHY_EXCEPTION; |
---|
2068 | n/a | goto fast_block_end; |
---|
2069 | n/a | } |
---|
2070 | n/a | else if (status != Py_None) { |
---|
2071 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
2072 | n/a | "'finally' pops bad exception"); |
---|
2073 | n/a | Py_DECREF(status); |
---|
2074 | n/a | goto error; |
---|
2075 | n/a | } |
---|
2076 | n/a | Py_DECREF(status); |
---|
2077 | n/a | DISPATCH(); |
---|
2078 | n/a | } |
---|
2079 | n/a | |
---|
2080 | n/a | TARGET(LOAD_BUILD_CLASS) { |
---|
2081 | n/a | _Py_IDENTIFIER(__build_class__); |
---|
2082 | n/a | |
---|
2083 | n/a | PyObject *bc; |
---|
2084 | n/a | if (PyDict_CheckExact(f->f_builtins)) { |
---|
2085 | n/a | bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__); |
---|
2086 | n/a | if (bc == NULL) { |
---|
2087 | n/a | PyErr_SetString(PyExc_NameError, |
---|
2088 | n/a | "__build_class__ not found"); |
---|
2089 | n/a | goto error; |
---|
2090 | n/a | } |
---|
2091 | n/a | Py_INCREF(bc); |
---|
2092 | n/a | } |
---|
2093 | n/a | else { |
---|
2094 | n/a | PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); |
---|
2095 | n/a | if (build_class_str == NULL) |
---|
2096 | n/a | goto error; |
---|
2097 | n/a | bc = PyObject_GetItem(f->f_builtins, build_class_str); |
---|
2098 | n/a | if (bc == NULL) { |
---|
2099 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2100 | n/a | PyErr_SetString(PyExc_NameError, |
---|
2101 | n/a | "__build_class__ not found"); |
---|
2102 | n/a | goto error; |
---|
2103 | n/a | } |
---|
2104 | n/a | } |
---|
2105 | n/a | PUSH(bc); |
---|
2106 | n/a | DISPATCH(); |
---|
2107 | n/a | } |
---|
2108 | n/a | |
---|
2109 | n/a | TARGET(STORE_NAME) { |
---|
2110 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2111 | n/a | PyObject *v = POP(); |
---|
2112 | n/a | PyObject *ns = f->f_locals; |
---|
2113 | n/a | int err; |
---|
2114 | n/a | if (ns == NULL) { |
---|
2115 | n/a | PyErr_Format(PyExc_SystemError, |
---|
2116 | n/a | "no locals found when storing %R", name); |
---|
2117 | n/a | Py_DECREF(v); |
---|
2118 | n/a | goto error; |
---|
2119 | n/a | } |
---|
2120 | n/a | if (PyDict_CheckExact(ns)) |
---|
2121 | n/a | err = PyDict_SetItem(ns, name, v); |
---|
2122 | n/a | else |
---|
2123 | n/a | err = PyObject_SetItem(ns, name, v); |
---|
2124 | n/a | Py_DECREF(v); |
---|
2125 | n/a | if (err != 0) |
---|
2126 | n/a | goto error; |
---|
2127 | n/a | DISPATCH(); |
---|
2128 | n/a | } |
---|
2129 | n/a | |
---|
2130 | n/a | TARGET(DELETE_NAME) { |
---|
2131 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2132 | n/a | PyObject *ns = f->f_locals; |
---|
2133 | n/a | int err; |
---|
2134 | n/a | if (ns == NULL) { |
---|
2135 | n/a | PyErr_Format(PyExc_SystemError, |
---|
2136 | n/a | "no locals when deleting %R", name); |
---|
2137 | n/a | goto error; |
---|
2138 | n/a | } |
---|
2139 | n/a | err = PyObject_DelItem(ns, name); |
---|
2140 | n/a | if (err != 0) { |
---|
2141 | n/a | format_exc_check_arg(PyExc_NameError, |
---|
2142 | n/a | NAME_ERROR_MSG, |
---|
2143 | n/a | name); |
---|
2144 | n/a | goto error; |
---|
2145 | n/a | } |
---|
2146 | n/a | DISPATCH(); |
---|
2147 | n/a | } |
---|
2148 | n/a | |
---|
2149 | n/a | PREDICTED(UNPACK_SEQUENCE); |
---|
2150 | n/a | TARGET(UNPACK_SEQUENCE) { |
---|
2151 | n/a | PyObject *seq = POP(), *item, **items; |
---|
2152 | n/a | if (PyTuple_CheckExact(seq) && |
---|
2153 | n/a | PyTuple_GET_SIZE(seq) == oparg) { |
---|
2154 | n/a | items = ((PyTupleObject *)seq)->ob_item; |
---|
2155 | n/a | while (oparg--) { |
---|
2156 | n/a | item = items[oparg]; |
---|
2157 | n/a | Py_INCREF(item); |
---|
2158 | n/a | PUSH(item); |
---|
2159 | n/a | } |
---|
2160 | n/a | } else if (PyList_CheckExact(seq) && |
---|
2161 | n/a | PyList_GET_SIZE(seq) == oparg) { |
---|
2162 | n/a | items = ((PyListObject *)seq)->ob_item; |
---|
2163 | n/a | while (oparg--) { |
---|
2164 | n/a | item = items[oparg]; |
---|
2165 | n/a | Py_INCREF(item); |
---|
2166 | n/a | PUSH(item); |
---|
2167 | n/a | } |
---|
2168 | n/a | } else if (unpack_iterable(seq, oparg, -1, |
---|
2169 | n/a | stack_pointer + oparg)) { |
---|
2170 | n/a | STACKADJ(oparg); |
---|
2171 | n/a | } else { |
---|
2172 | n/a | /* unpack_iterable() raised an exception */ |
---|
2173 | n/a | Py_DECREF(seq); |
---|
2174 | n/a | goto error; |
---|
2175 | n/a | } |
---|
2176 | n/a | Py_DECREF(seq); |
---|
2177 | n/a | DISPATCH(); |
---|
2178 | n/a | } |
---|
2179 | n/a | |
---|
2180 | n/a | TARGET(UNPACK_EX) { |
---|
2181 | n/a | int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); |
---|
2182 | n/a | PyObject *seq = POP(); |
---|
2183 | n/a | |
---|
2184 | n/a | if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8, |
---|
2185 | n/a | stack_pointer + totalargs)) { |
---|
2186 | n/a | stack_pointer += totalargs; |
---|
2187 | n/a | } else { |
---|
2188 | n/a | Py_DECREF(seq); |
---|
2189 | n/a | goto error; |
---|
2190 | n/a | } |
---|
2191 | n/a | Py_DECREF(seq); |
---|
2192 | n/a | DISPATCH(); |
---|
2193 | n/a | } |
---|
2194 | n/a | |
---|
2195 | n/a | TARGET(STORE_ATTR) { |
---|
2196 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2197 | n/a | PyObject *owner = TOP(); |
---|
2198 | n/a | PyObject *v = SECOND(); |
---|
2199 | n/a | int err; |
---|
2200 | n/a | STACKADJ(-2); |
---|
2201 | n/a | err = PyObject_SetAttr(owner, name, v); |
---|
2202 | n/a | Py_DECREF(v); |
---|
2203 | n/a | Py_DECREF(owner); |
---|
2204 | n/a | if (err != 0) |
---|
2205 | n/a | goto error; |
---|
2206 | n/a | DISPATCH(); |
---|
2207 | n/a | } |
---|
2208 | n/a | |
---|
2209 | n/a | TARGET(DELETE_ATTR) { |
---|
2210 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2211 | n/a | PyObject *owner = POP(); |
---|
2212 | n/a | int err; |
---|
2213 | n/a | err = PyObject_SetAttr(owner, name, (PyObject *)NULL); |
---|
2214 | n/a | Py_DECREF(owner); |
---|
2215 | n/a | if (err != 0) |
---|
2216 | n/a | goto error; |
---|
2217 | n/a | DISPATCH(); |
---|
2218 | n/a | } |
---|
2219 | n/a | |
---|
2220 | n/a | TARGET(STORE_GLOBAL) { |
---|
2221 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2222 | n/a | PyObject *v = POP(); |
---|
2223 | n/a | int err; |
---|
2224 | n/a | err = PyDict_SetItem(f->f_globals, name, v); |
---|
2225 | n/a | Py_DECREF(v); |
---|
2226 | n/a | if (err != 0) |
---|
2227 | n/a | goto error; |
---|
2228 | n/a | DISPATCH(); |
---|
2229 | n/a | } |
---|
2230 | n/a | |
---|
2231 | n/a | TARGET(DELETE_GLOBAL) { |
---|
2232 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2233 | n/a | int err; |
---|
2234 | n/a | err = PyDict_DelItem(f->f_globals, name); |
---|
2235 | n/a | if (err != 0) { |
---|
2236 | n/a | format_exc_check_arg( |
---|
2237 | n/a | PyExc_NameError, NAME_ERROR_MSG, name); |
---|
2238 | n/a | goto error; |
---|
2239 | n/a | } |
---|
2240 | n/a | DISPATCH(); |
---|
2241 | n/a | } |
---|
2242 | n/a | |
---|
2243 | n/a | TARGET(LOAD_NAME) { |
---|
2244 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2245 | n/a | PyObject *locals = f->f_locals; |
---|
2246 | n/a | PyObject *v; |
---|
2247 | n/a | if (locals == NULL) { |
---|
2248 | n/a | PyErr_Format(PyExc_SystemError, |
---|
2249 | n/a | "no locals when loading %R", name); |
---|
2250 | n/a | goto error; |
---|
2251 | n/a | } |
---|
2252 | n/a | if (PyDict_CheckExact(locals)) { |
---|
2253 | n/a | v = PyDict_GetItem(locals, name); |
---|
2254 | n/a | Py_XINCREF(v); |
---|
2255 | n/a | } |
---|
2256 | n/a | else { |
---|
2257 | n/a | v = PyObject_GetItem(locals, name); |
---|
2258 | n/a | if (v == NULL) { |
---|
2259 | n/a | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2260 | n/a | goto error; |
---|
2261 | n/a | PyErr_Clear(); |
---|
2262 | n/a | } |
---|
2263 | n/a | } |
---|
2264 | n/a | if (v == NULL) { |
---|
2265 | n/a | v = PyDict_GetItem(f->f_globals, name); |
---|
2266 | n/a | Py_XINCREF(v); |
---|
2267 | n/a | if (v == NULL) { |
---|
2268 | n/a | if (PyDict_CheckExact(f->f_builtins)) { |
---|
2269 | n/a | v = PyDict_GetItem(f->f_builtins, name); |
---|
2270 | n/a | if (v == NULL) { |
---|
2271 | n/a | format_exc_check_arg( |
---|
2272 | n/a | PyExc_NameError, |
---|
2273 | n/a | NAME_ERROR_MSG, name); |
---|
2274 | n/a | goto error; |
---|
2275 | n/a | } |
---|
2276 | n/a | Py_INCREF(v); |
---|
2277 | n/a | } |
---|
2278 | n/a | else { |
---|
2279 | n/a | v = PyObject_GetItem(f->f_builtins, name); |
---|
2280 | n/a | if (v == NULL) { |
---|
2281 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2282 | n/a | format_exc_check_arg( |
---|
2283 | n/a | PyExc_NameError, |
---|
2284 | n/a | NAME_ERROR_MSG, name); |
---|
2285 | n/a | goto error; |
---|
2286 | n/a | } |
---|
2287 | n/a | } |
---|
2288 | n/a | } |
---|
2289 | n/a | } |
---|
2290 | n/a | PUSH(v); |
---|
2291 | n/a | DISPATCH(); |
---|
2292 | n/a | } |
---|
2293 | n/a | |
---|
2294 | n/a | TARGET(LOAD_GLOBAL) { |
---|
2295 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2296 | n/a | PyObject *v; |
---|
2297 | n/a | if (PyDict_CheckExact(f->f_globals) |
---|
2298 | n/a | && PyDict_CheckExact(f->f_builtins)) |
---|
2299 | n/a | { |
---|
2300 | n/a | v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, |
---|
2301 | n/a | (PyDictObject *)f->f_builtins, |
---|
2302 | n/a | name); |
---|
2303 | n/a | if (v == NULL) { |
---|
2304 | n/a | if (!_PyErr_OCCURRED()) { |
---|
2305 | n/a | /* _PyDict_LoadGlobal() returns NULL without raising |
---|
2306 | n/a | * an exception if the key doesn't exist */ |
---|
2307 | n/a | format_exc_check_arg(PyExc_NameError, |
---|
2308 | n/a | NAME_ERROR_MSG, name); |
---|
2309 | n/a | } |
---|
2310 | n/a | goto error; |
---|
2311 | n/a | } |
---|
2312 | n/a | Py_INCREF(v); |
---|
2313 | n/a | } |
---|
2314 | n/a | else { |
---|
2315 | n/a | /* Slow-path if globals or builtins is not a dict */ |
---|
2316 | n/a | |
---|
2317 | n/a | /* namespace 1: globals */ |
---|
2318 | n/a | v = PyObject_GetItem(f->f_globals, name); |
---|
2319 | n/a | if (v == NULL) { |
---|
2320 | n/a | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2321 | n/a | goto error; |
---|
2322 | n/a | PyErr_Clear(); |
---|
2323 | n/a | |
---|
2324 | n/a | /* namespace 2: builtins */ |
---|
2325 | n/a | v = PyObject_GetItem(f->f_builtins, name); |
---|
2326 | n/a | if (v == NULL) { |
---|
2327 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2328 | n/a | format_exc_check_arg( |
---|
2329 | n/a | PyExc_NameError, |
---|
2330 | n/a | NAME_ERROR_MSG, name); |
---|
2331 | n/a | goto error; |
---|
2332 | n/a | } |
---|
2333 | n/a | } |
---|
2334 | n/a | } |
---|
2335 | n/a | PUSH(v); |
---|
2336 | n/a | DISPATCH(); |
---|
2337 | n/a | } |
---|
2338 | n/a | |
---|
2339 | n/a | TARGET(DELETE_FAST) { |
---|
2340 | n/a | PyObject *v = GETLOCAL(oparg); |
---|
2341 | n/a | if (v != NULL) { |
---|
2342 | n/a | SETLOCAL(oparg, NULL); |
---|
2343 | n/a | DISPATCH(); |
---|
2344 | n/a | } |
---|
2345 | n/a | format_exc_check_arg( |
---|
2346 | n/a | PyExc_UnboundLocalError, |
---|
2347 | n/a | UNBOUNDLOCAL_ERROR_MSG, |
---|
2348 | n/a | PyTuple_GetItem(co->co_varnames, oparg) |
---|
2349 | n/a | ); |
---|
2350 | n/a | goto error; |
---|
2351 | n/a | } |
---|
2352 | n/a | |
---|
2353 | n/a | TARGET(DELETE_DEREF) { |
---|
2354 | n/a | PyObject *cell = freevars[oparg]; |
---|
2355 | n/a | PyObject *oldobj = PyCell_GET(cell); |
---|
2356 | n/a | if (oldobj != NULL) { |
---|
2357 | n/a | PyCell_SET(cell, NULL); |
---|
2358 | n/a | Py_DECREF(oldobj); |
---|
2359 | n/a | DISPATCH(); |
---|
2360 | n/a | } |
---|
2361 | n/a | format_exc_unbound(co, oparg); |
---|
2362 | n/a | goto error; |
---|
2363 | n/a | } |
---|
2364 | n/a | |
---|
2365 | n/a | TARGET(LOAD_CLOSURE) { |
---|
2366 | n/a | PyObject *cell = freevars[oparg]; |
---|
2367 | n/a | Py_INCREF(cell); |
---|
2368 | n/a | PUSH(cell); |
---|
2369 | n/a | DISPATCH(); |
---|
2370 | n/a | } |
---|
2371 | n/a | |
---|
2372 | n/a | TARGET(LOAD_CLASSDEREF) { |
---|
2373 | n/a | PyObject *name, *value, *locals = f->f_locals; |
---|
2374 | n/a | Py_ssize_t idx; |
---|
2375 | n/a | assert(locals); |
---|
2376 | n/a | assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); |
---|
2377 | n/a | idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); |
---|
2378 | n/a | assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); |
---|
2379 | n/a | name = PyTuple_GET_ITEM(co->co_freevars, idx); |
---|
2380 | n/a | if (PyDict_CheckExact(locals)) { |
---|
2381 | n/a | value = PyDict_GetItem(locals, name); |
---|
2382 | n/a | Py_XINCREF(value); |
---|
2383 | n/a | } |
---|
2384 | n/a | else { |
---|
2385 | n/a | value = PyObject_GetItem(locals, name); |
---|
2386 | n/a | if (value == NULL) { |
---|
2387 | n/a | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
2388 | n/a | goto error; |
---|
2389 | n/a | PyErr_Clear(); |
---|
2390 | n/a | } |
---|
2391 | n/a | } |
---|
2392 | n/a | if (!value) { |
---|
2393 | n/a | PyObject *cell = freevars[oparg]; |
---|
2394 | n/a | value = PyCell_GET(cell); |
---|
2395 | n/a | if (value == NULL) { |
---|
2396 | n/a | format_exc_unbound(co, oparg); |
---|
2397 | n/a | goto error; |
---|
2398 | n/a | } |
---|
2399 | n/a | Py_INCREF(value); |
---|
2400 | n/a | } |
---|
2401 | n/a | PUSH(value); |
---|
2402 | n/a | DISPATCH(); |
---|
2403 | n/a | } |
---|
2404 | n/a | |
---|
2405 | n/a | TARGET(LOAD_DEREF) { |
---|
2406 | n/a | PyObject *cell = freevars[oparg]; |
---|
2407 | n/a | PyObject *value = PyCell_GET(cell); |
---|
2408 | n/a | if (value == NULL) { |
---|
2409 | n/a | format_exc_unbound(co, oparg); |
---|
2410 | n/a | goto error; |
---|
2411 | n/a | } |
---|
2412 | n/a | Py_INCREF(value); |
---|
2413 | n/a | PUSH(value); |
---|
2414 | n/a | DISPATCH(); |
---|
2415 | n/a | } |
---|
2416 | n/a | |
---|
2417 | n/a | TARGET(STORE_DEREF) { |
---|
2418 | n/a | PyObject *v = POP(); |
---|
2419 | n/a | PyObject *cell = freevars[oparg]; |
---|
2420 | n/a | PyObject *oldobj = PyCell_GET(cell); |
---|
2421 | n/a | PyCell_SET(cell, v); |
---|
2422 | n/a | Py_XDECREF(oldobj); |
---|
2423 | n/a | DISPATCH(); |
---|
2424 | n/a | } |
---|
2425 | n/a | |
---|
2426 | n/a | TARGET(BUILD_STRING) { |
---|
2427 | n/a | PyObject *str; |
---|
2428 | n/a | PyObject *empty = PyUnicode_New(0, 0); |
---|
2429 | n/a | if (empty == NULL) { |
---|
2430 | n/a | goto error; |
---|
2431 | n/a | } |
---|
2432 | n/a | str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); |
---|
2433 | n/a | Py_DECREF(empty); |
---|
2434 | n/a | if (str == NULL) |
---|
2435 | n/a | goto error; |
---|
2436 | n/a | while (--oparg >= 0) { |
---|
2437 | n/a | PyObject *item = POP(); |
---|
2438 | n/a | Py_DECREF(item); |
---|
2439 | n/a | } |
---|
2440 | n/a | PUSH(str); |
---|
2441 | n/a | DISPATCH(); |
---|
2442 | n/a | } |
---|
2443 | n/a | |
---|
2444 | n/a | TARGET(BUILD_TUPLE) { |
---|
2445 | n/a | PyObject *tup = PyTuple_New(oparg); |
---|
2446 | n/a | if (tup == NULL) |
---|
2447 | n/a | goto error; |
---|
2448 | n/a | while (--oparg >= 0) { |
---|
2449 | n/a | PyObject *item = POP(); |
---|
2450 | n/a | PyTuple_SET_ITEM(tup, oparg, item); |
---|
2451 | n/a | } |
---|
2452 | n/a | PUSH(tup); |
---|
2453 | n/a | DISPATCH(); |
---|
2454 | n/a | } |
---|
2455 | n/a | |
---|
2456 | n/a | TARGET(BUILD_LIST) { |
---|
2457 | n/a | PyObject *list = PyList_New(oparg); |
---|
2458 | n/a | if (list == NULL) |
---|
2459 | n/a | goto error; |
---|
2460 | n/a | while (--oparg >= 0) { |
---|
2461 | n/a | PyObject *item = POP(); |
---|
2462 | n/a | PyList_SET_ITEM(list, oparg, item); |
---|
2463 | n/a | } |
---|
2464 | n/a | PUSH(list); |
---|
2465 | n/a | DISPATCH(); |
---|
2466 | n/a | } |
---|
2467 | n/a | |
---|
2468 | n/a | TARGET(BUILD_TUPLE_UNPACK_WITH_CALL) |
---|
2469 | n/a | TARGET(BUILD_TUPLE_UNPACK) |
---|
2470 | n/a | TARGET(BUILD_LIST_UNPACK) { |
---|
2471 | n/a | int convert_to_tuple = opcode != BUILD_LIST_UNPACK; |
---|
2472 | n/a | Py_ssize_t i; |
---|
2473 | n/a | PyObject *sum = PyList_New(0); |
---|
2474 | n/a | PyObject *return_value; |
---|
2475 | n/a | |
---|
2476 | n/a | if (sum == NULL) |
---|
2477 | n/a | goto error; |
---|
2478 | n/a | |
---|
2479 | n/a | for (i = oparg; i > 0; i--) { |
---|
2480 | n/a | PyObject *none_val; |
---|
2481 | n/a | |
---|
2482 | n/a | none_val = _PyList_Extend((PyListObject *)sum, PEEK(i)); |
---|
2483 | n/a | if (none_val == NULL) { |
---|
2484 | n/a | if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL && |
---|
2485 | n/a | PyErr_ExceptionMatches(PyExc_TypeError)) { |
---|
2486 | n/a | PyObject *func = PEEK(1 + oparg); |
---|
2487 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2488 | n/a | "%.200s%.200s argument after * " |
---|
2489 | n/a | "must be an iterable, not %.200s", |
---|
2490 | n/a | PyEval_GetFuncName(func), |
---|
2491 | n/a | PyEval_GetFuncDesc(func), |
---|
2492 | n/a | PEEK(i)->ob_type->tp_name); |
---|
2493 | n/a | } |
---|
2494 | n/a | Py_DECREF(sum); |
---|
2495 | n/a | goto error; |
---|
2496 | n/a | } |
---|
2497 | n/a | Py_DECREF(none_val); |
---|
2498 | n/a | } |
---|
2499 | n/a | |
---|
2500 | n/a | if (convert_to_tuple) { |
---|
2501 | n/a | return_value = PyList_AsTuple(sum); |
---|
2502 | n/a | Py_DECREF(sum); |
---|
2503 | n/a | if (return_value == NULL) |
---|
2504 | n/a | goto error; |
---|
2505 | n/a | } |
---|
2506 | n/a | else { |
---|
2507 | n/a | return_value = sum; |
---|
2508 | n/a | } |
---|
2509 | n/a | |
---|
2510 | n/a | while (oparg--) |
---|
2511 | n/a | Py_DECREF(POP()); |
---|
2512 | n/a | PUSH(return_value); |
---|
2513 | n/a | DISPATCH(); |
---|
2514 | n/a | } |
---|
2515 | n/a | |
---|
2516 | n/a | TARGET(BUILD_SET) { |
---|
2517 | n/a | PyObject *set = PySet_New(NULL); |
---|
2518 | n/a | int err = 0; |
---|
2519 | n/a | int i; |
---|
2520 | n/a | if (set == NULL) |
---|
2521 | n/a | goto error; |
---|
2522 | n/a | for (i = oparg; i > 0; i--) { |
---|
2523 | n/a | PyObject *item = PEEK(i); |
---|
2524 | n/a | if (err == 0) |
---|
2525 | n/a | err = PySet_Add(set, item); |
---|
2526 | n/a | Py_DECREF(item); |
---|
2527 | n/a | } |
---|
2528 | n/a | STACKADJ(-oparg); |
---|
2529 | n/a | if (err != 0) { |
---|
2530 | n/a | Py_DECREF(set); |
---|
2531 | n/a | goto error; |
---|
2532 | n/a | } |
---|
2533 | n/a | PUSH(set); |
---|
2534 | n/a | DISPATCH(); |
---|
2535 | n/a | } |
---|
2536 | n/a | |
---|
2537 | n/a | TARGET(BUILD_SET_UNPACK) { |
---|
2538 | n/a | Py_ssize_t i; |
---|
2539 | n/a | PyObject *sum = PySet_New(NULL); |
---|
2540 | n/a | if (sum == NULL) |
---|
2541 | n/a | goto error; |
---|
2542 | n/a | |
---|
2543 | n/a | for (i = oparg; i > 0; i--) { |
---|
2544 | n/a | if (_PySet_Update(sum, PEEK(i)) < 0) { |
---|
2545 | n/a | Py_DECREF(sum); |
---|
2546 | n/a | goto error; |
---|
2547 | n/a | } |
---|
2548 | n/a | } |
---|
2549 | n/a | |
---|
2550 | n/a | while (oparg--) |
---|
2551 | n/a | Py_DECREF(POP()); |
---|
2552 | n/a | PUSH(sum); |
---|
2553 | n/a | DISPATCH(); |
---|
2554 | n/a | } |
---|
2555 | n/a | |
---|
2556 | n/a | TARGET(BUILD_MAP) { |
---|
2557 | n/a | Py_ssize_t i; |
---|
2558 | n/a | PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); |
---|
2559 | n/a | if (map == NULL) |
---|
2560 | n/a | goto error; |
---|
2561 | n/a | for (i = oparg; i > 0; i--) { |
---|
2562 | n/a | int err; |
---|
2563 | n/a | PyObject *key = PEEK(2*i); |
---|
2564 | n/a | PyObject *value = PEEK(2*i - 1); |
---|
2565 | n/a | err = PyDict_SetItem(map, key, value); |
---|
2566 | n/a | if (err != 0) { |
---|
2567 | n/a | Py_DECREF(map); |
---|
2568 | n/a | goto error; |
---|
2569 | n/a | } |
---|
2570 | n/a | } |
---|
2571 | n/a | |
---|
2572 | n/a | while (oparg--) { |
---|
2573 | n/a | Py_DECREF(POP()); |
---|
2574 | n/a | Py_DECREF(POP()); |
---|
2575 | n/a | } |
---|
2576 | n/a | PUSH(map); |
---|
2577 | n/a | DISPATCH(); |
---|
2578 | n/a | } |
---|
2579 | n/a | |
---|
2580 | n/a | TARGET(SETUP_ANNOTATIONS) { |
---|
2581 | n/a | _Py_IDENTIFIER(__annotations__); |
---|
2582 | n/a | int err; |
---|
2583 | n/a | PyObject *ann_dict; |
---|
2584 | n/a | if (f->f_locals == NULL) { |
---|
2585 | n/a | PyErr_Format(PyExc_SystemError, |
---|
2586 | n/a | "no locals found when setting up annotations"); |
---|
2587 | n/a | goto error; |
---|
2588 | n/a | } |
---|
2589 | n/a | /* check if __annotations__ in locals()... */ |
---|
2590 | n/a | if (PyDict_CheckExact(f->f_locals)) { |
---|
2591 | n/a | ann_dict = _PyDict_GetItemId(f->f_locals, |
---|
2592 | n/a | &PyId___annotations__); |
---|
2593 | n/a | if (ann_dict == NULL) { |
---|
2594 | n/a | /* ...if not, create a new one */ |
---|
2595 | n/a | ann_dict = PyDict_New(); |
---|
2596 | n/a | if (ann_dict == NULL) { |
---|
2597 | n/a | goto error; |
---|
2598 | n/a | } |
---|
2599 | n/a | err = _PyDict_SetItemId(f->f_locals, |
---|
2600 | n/a | &PyId___annotations__, ann_dict); |
---|
2601 | n/a | Py_DECREF(ann_dict); |
---|
2602 | n/a | if (err != 0) { |
---|
2603 | n/a | goto error; |
---|
2604 | n/a | } |
---|
2605 | n/a | } |
---|
2606 | n/a | } |
---|
2607 | n/a | else { |
---|
2608 | n/a | /* do the same if locals() is not a dict */ |
---|
2609 | n/a | PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); |
---|
2610 | n/a | if (ann_str == NULL) { |
---|
2611 | n/a | goto error; |
---|
2612 | n/a | } |
---|
2613 | n/a | ann_dict = PyObject_GetItem(f->f_locals, ann_str); |
---|
2614 | n/a | if (ann_dict == NULL) { |
---|
2615 | n/a | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
---|
2616 | n/a | goto error; |
---|
2617 | n/a | } |
---|
2618 | n/a | PyErr_Clear(); |
---|
2619 | n/a | ann_dict = PyDict_New(); |
---|
2620 | n/a | if (ann_dict == NULL) { |
---|
2621 | n/a | goto error; |
---|
2622 | n/a | } |
---|
2623 | n/a | err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); |
---|
2624 | n/a | Py_DECREF(ann_dict); |
---|
2625 | n/a | if (err != 0) { |
---|
2626 | n/a | goto error; |
---|
2627 | n/a | } |
---|
2628 | n/a | } |
---|
2629 | n/a | else { |
---|
2630 | n/a | Py_DECREF(ann_dict); |
---|
2631 | n/a | } |
---|
2632 | n/a | } |
---|
2633 | n/a | DISPATCH(); |
---|
2634 | n/a | } |
---|
2635 | n/a | |
---|
2636 | n/a | TARGET(BUILD_CONST_KEY_MAP) { |
---|
2637 | n/a | Py_ssize_t i; |
---|
2638 | n/a | PyObject *map; |
---|
2639 | n/a | PyObject *keys = TOP(); |
---|
2640 | n/a | if (!PyTuple_CheckExact(keys) || |
---|
2641 | n/a | PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { |
---|
2642 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
2643 | n/a | "bad BUILD_CONST_KEY_MAP keys argument"); |
---|
2644 | n/a | goto error; |
---|
2645 | n/a | } |
---|
2646 | n/a | map = _PyDict_NewPresized((Py_ssize_t)oparg); |
---|
2647 | n/a | if (map == NULL) { |
---|
2648 | n/a | goto error; |
---|
2649 | n/a | } |
---|
2650 | n/a | for (i = oparg; i > 0; i--) { |
---|
2651 | n/a | int err; |
---|
2652 | n/a | PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); |
---|
2653 | n/a | PyObject *value = PEEK(i + 1); |
---|
2654 | n/a | err = PyDict_SetItem(map, key, value); |
---|
2655 | n/a | if (err != 0) { |
---|
2656 | n/a | Py_DECREF(map); |
---|
2657 | n/a | goto error; |
---|
2658 | n/a | } |
---|
2659 | n/a | } |
---|
2660 | n/a | |
---|
2661 | n/a | Py_DECREF(POP()); |
---|
2662 | n/a | while (oparg--) { |
---|
2663 | n/a | Py_DECREF(POP()); |
---|
2664 | n/a | } |
---|
2665 | n/a | PUSH(map); |
---|
2666 | n/a | DISPATCH(); |
---|
2667 | n/a | } |
---|
2668 | n/a | |
---|
2669 | n/a | TARGET(BUILD_MAP_UNPACK) { |
---|
2670 | n/a | Py_ssize_t i; |
---|
2671 | n/a | PyObject *sum = PyDict_New(); |
---|
2672 | n/a | if (sum == NULL) |
---|
2673 | n/a | goto error; |
---|
2674 | n/a | |
---|
2675 | n/a | for (i = oparg; i > 0; i--) { |
---|
2676 | n/a | PyObject *arg = PEEK(i); |
---|
2677 | n/a | if (PyDict_Update(sum, arg) < 0) { |
---|
2678 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
---|
2679 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2680 | n/a | "'%.200s' object is not a mapping", |
---|
2681 | n/a | arg->ob_type->tp_name); |
---|
2682 | n/a | } |
---|
2683 | n/a | Py_DECREF(sum); |
---|
2684 | n/a | goto error; |
---|
2685 | n/a | } |
---|
2686 | n/a | } |
---|
2687 | n/a | |
---|
2688 | n/a | while (oparg--) |
---|
2689 | n/a | Py_DECREF(POP()); |
---|
2690 | n/a | PUSH(sum); |
---|
2691 | n/a | DISPATCH(); |
---|
2692 | n/a | } |
---|
2693 | n/a | |
---|
2694 | n/a | TARGET(BUILD_MAP_UNPACK_WITH_CALL) { |
---|
2695 | n/a | Py_ssize_t i; |
---|
2696 | n/a | PyObject *sum = PyDict_New(); |
---|
2697 | n/a | if (sum == NULL) |
---|
2698 | n/a | goto error; |
---|
2699 | n/a | |
---|
2700 | n/a | for (i = oparg; i > 0; i--) { |
---|
2701 | n/a | PyObject *arg = PEEK(i); |
---|
2702 | n/a | if (_PyDict_MergeEx(sum, arg, 2) < 0) { |
---|
2703 | n/a | PyObject *func = PEEK(2 + oparg); |
---|
2704 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
---|
2705 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2706 | n/a | "%.200s%.200s argument after ** " |
---|
2707 | n/a | "must be a mapping, not %.200s", |
---|
2708 | n/a | PyEval_GetFuncName(func), |
---|
2709 | n/a | PyEval_GetFuncDesc(func), |
---|
2710 | n/a | arg->ob_type->tp_name); |
---|
2711 | n/a | } |
---|
2712 | n/a | else if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
---|
2713 | n/a | PyObject *exc, *val, *tb; |
---|
2714 | n/a | PyErr_Fetch(&exc, &val, &tb); |
---|
2715 | n/a | if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) { |
---|
2716 | n/a | PyObject *key = PyTuple_GET_ITEM(val, 0); |
---|
2717 | n/a | if (!PyUnicode_Check(key)) { |
---|
2718 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2719 | n/a | "%.200s%.200s keywords must be strings", |
---|
2720 | n/a | PyEval_GetFuncName(func), |
---|
2721 | n/a | PyEval_GetFuncDesc(func)); |
---|
2722 | n/a | } else { |
---|
2723 | n/a | PyErr_Format(PyExc_TypeError, |
---|
2724 | n/a | "%.200s%.200s got multiple " |
---|
2725 | n/a | "values for keyword argument '%U'", |
---|
2726 | n/a | PyEval_GetFuncName(func), |
---|
2727 | n/a | PyEval_GetFuncDesc(func), |
---|
2728 | n/a | key); |
---|
2729 | n/a | } |
---|
2730 | n/a | Py_XDECREF(exc); |
---|
2731 | n/a | Py_XDECREF(val); |
---|
2732 | n/a | Py_XDECREF(tb); |
---|
2733 | n/a | } |
---|
2734 | n/a | else { |
---|
2735 | n/a | PyErr_Restore(exc, val, tb); |
---|
2736 | n/a | } |
---|
2737 | n/a | } |
---|
2738 | n/a | Py_DECREF(sum); |
---|
2739 | n/a | goto error; |
---|
2740 | n/a | } |
---|
2741 | n/a | } |
---|
2742 | n/a | |
---|
2743 | n/a | while (oparg--) |
---|
2744 | n/a | Py_DECREF(POP()); |
---|
2745 | n/a | PUSH(sum); |
---|
2746 | n/a | DISPATCH(); |
---|
2747 | n/a | } |
---|
2748 | n/a | |
---|
2749 | n/a | TARGET(MAP_ADD) { |
---|
2750 | n/a | PyObject *key = TOP(); |
---|
2751 | n/a | PyObject *value = SECOND(); |
---|
2752 | n/a | PyObject *map; |
---|
2753 | n/a | int err; |
---|
2754 | n/a | STACKADJ(-2); |
---|
2755 | n/a | map = PEEK(oparg); /* dict */ |
---|
2756 | n/a | assert(PyDict_CheckExact(map)); |
---|
2757 | n/a | err = PyDict_SetItem(map, key, value); /* map[key] = value */ |
---|
2758 | n/a | Py_DECREF(value); |
---|
2759 | n/a | Py_DECREF(key); |
---|
2760 | n/a | if (err != 0) |
---|
2761 | n/a | goto error; |
---|
2762 | n/a | PREDICT(JUMP_ABSOLUTE); |
---|
2763 | n/a | DISPATCH(); |
---|
2764 | n/a | } |
---|
2765 | n/a | |
---|
2766 | n/a | TARGET(LOAD_ATTR) { |
---|
2767 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2768 | n/a | PyObject *owner = TOP(); |
---|
2769 | n/a | PyObject *res = PyObject_GetAttr(owner, name); |
---|
2770 | n/a | Py_DECREF(owner); |
---|
2771 | n/a | SET_TOP(res); |
---|
2772 | n/a | if (res == NULL) |
---|
2773 | n/a | goto error; |
---|
2774 | n/a | DISPATCH(); |
---|
2775 | n/a | } |
---|
2776 | n/a | |
---|
2777 | n/a | TARGET(COMPARE_OP) { |
---|
2778 | n/a | PyObject *right = POP(); |
---|
2779 | n/a | PyObject *left = TOP(); |
---|
2780 | n/a | PyObject *res = cmp_outcome(oparg, left, right); |
---|
2781 | n/a | Py_DECREF(left); |
---|
2782 | n/a | Py_DECREF(right); |
---|
2783 | n/a | SET_TOP(res); |
---|
2784 | n/a | if (res == NULL) |
---|
2785 | n/a | goto error; |
---|
2786 | n/a | PREDICT(POP_JUMP_IF_FALSE); |
---|
2787 | n/a | PREDICT(POP_JUMP_IF_TRUE); |
---|
2788 | n/a | DISPATCH(); |
---|
2789 | n/a | } |
---|
2790 | n/a | |
---|
2791 | n/a | TARGET(IMPORT_NAME) { |
---|
2792 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2793 | n/a | PyObject *fromlist = POP(); |
---|
2794 | n/a | PyObject *level = TOP(); |
---|
2795 | n/a | PyObject *res; |
---|
2796 | n/a | res = import_name(f, name, fromlist, level); |
---|
2797 | n/a | Py_DECREF(level); |
---|
2798 | n/a | Py_DECREF(fromlist); |
---|
2799 | n/a | SET_TOP(res); |
---|
2800 | n/a | if (res == NULL) |
---|
2801 | n/a | goto error; |
---|
2802 | n/a | DISPATCH(); |
---|
2803 | n/a | } |
---|
2804 | n/a | |
---|
2805 | n/a | TARGET(IMPORT_STAR) { |
---|
2806 | n/a | PyObject *from = POP(), *locals; |
---|
2807 | n/a | int err; |
---|
2808 | n/a | if (PyFrame_FastToLocalsWithError(f) < 0) |
---|
2809 | n/a | goto error; |
---|
2810 | n/a | |
---|
2811 | n/a | locals = f->f_locals; |
---|
2812 | n/a | if (locals == NULL) { |
---|
2813 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
2814 | n/a | "no locals found during 'import *'"); |
---|
2815 | n/a | goto error; |
---|
2816 | n/a | } |
---|
2817 | n/a | err = import_all_from(locals, from); |
---|
2818 | n/a | PyFrame_LocalsToFast(f, 0); |
---|
2819 | n/a | Py_DECREF(from); |
---|
2820 | n/a | if (err != 0) |
---|
2821 | n/a | goto error; |
---|
2822 | n/a | DISPATCH(); |
---|
2823 | n/a | } |
---|
2824 | n/a | |
---|
2825 | n/a | TARGET(IMPORT_FROM) { |
---|
2826 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
2827 | n/a | PyObject *from = TOP(); |
---|
2828 | n/a | PyObject *res; |
---|
2829 | n/a | res = import_from(from, name); |
---|
2830 | n/a | PUSH(res); |
---|
2831 | n/a | if (res == NULL) |
---|
2832 | n/a | goto error; |
---|
2833 | n/a | DISPATCH(); |
---|
2834 | n/a | } |
---|
2835 | n/a | |
---|
2836 | n/a | TARGET(JUMP_FORWARD) { |
---|
2837 | n/a | JUMPBY(oparg); |
---|
2838 | n/a | FAST_DISPATCH(); |
---|
2839 | n/a | } |
---|
2840 | n/a | |
---|
2841 | n/a | PREDICTED(POP_JUMP_IF_FALSE); |
---|
2842 | n/a | TARGET(POP_JUMP_IF_FALSE) { |
---|
2843 | n/a | PyObject *cond = POP(); |
---|
2844 | n/a | int err; |
---|
2845 | n/a | if (cond == Py_True) { |
---|
2846 | n/a | Py_DECREF(cond); |
---|
2847 | n/a | FAST_DISPATCH(); |
---|
2848 | n/a | } |
---|
2849 | n/a | if (cond == Py_False) { |
---|
2850 | n/a | Py_DECREF(cond); |
---|
2851 | n/a | JUMPTO(oparg); |
---|
2852 | n/a | FAST_DISPATCH(); |
---|
2853 | n/a | } |
---|
2854 | n/a | err = PyObject_IsTrue(cond); |
---|
2855 | n/a | Py_DECREF(cond); |
---|
2856 | n/a | if (err > 0) |
---|
2857 | n/a | err = 0; |
---|
2858 | n/a | else if (err == 0) |
---|
2859 | n/a | JUMPTO(oparg); |
---|
2860 | n/a | else |
---|
2861 | n/a | goto error; |
---|
2862 | n/a | DISPATCH(); |
---|
2863 | n/a | } |
---|
2864 | n/a | |
---|
2865 | n/a | PREDICTED(POP_JUMP_IF_TRUE); |
---|
2866 | n/a | TARGET(POP_JUMP_IF_TRUE) { |
---|
2867 | n/a | PyObject *cond = POP(); |
---|
2868 | n/a | int err; |
---|
2869 | n/a | if (cond == Py_False) { |
---|
2870 | n/a | Py_DECREF(cond); |
---|
2871 | n/a | FAST_DISPATCH(); |
---|
2872 | n/a | } |
---|
2873 | n/a | if (cond == Py_True) { |
---|
2874 | n/a | Py_DECREF(cond); |
---|
2875 | n/a | JUMPTO(oparg); |
---|
2876 | n/a | FAST_DISPATCH(); |
---|
2877 | n/a | } |
---|
2878 | n/a | err = PyObject_IsTrue(cond); |
---|
2879 | n/a | Py_DECREF(cond); |
---|
2880 | n/a | if (err > 0) { |
---|
2881 | n/a | err = 0; |
---|
2882 | n/a | JUMPTO(oparg); |
---|
2883 | n/a | } |
---|
2884 | n/a | else if (err == 0) |
---|
2885 | n/a | ; |
---|
2886 | n/a | else |
---|
2887 | n/a | goto error; |
---|
2888 | n/a | DISPATCH(); |
---|
2889 | n/a | } |
---|
2890 | n/a | |
---|
2891 | n/a | TARGET(JUMP_IF_FALSE_OR_POP) { |
---|
2892 | n/a | PyObject *cond = TOP(); |
---|
2893 | n/a | int err; |
---|
2894 | n/a | if (cond == Py_True) { |
---|
2895 | n/a | STACKADJ(-1); |
---|
2896 | n/a | Py_DECREF(cond); |
---|
2897 | n/a | FAST_DISPATCH(); |
---|
2898 | n/a | } |
---|
2899 | n/a | if (cond == Py_False) { |
---|
2900 | n/a | JUMPTO(oparg); |
---|
2901 | n/a | FAST_DISPATCH(); |
---|
2902 | n/a | } |
---|
2903 | n/a | err = PyObject_IsTrue(cond); |
---|
2904 | n/a | if (err > 0) { |
---|
2905 | n/a | STACKADJ(-1); |
---|
2906 | n/a | Py_DECREF(cond); |
---|
2907 | n/a | err = 0; |
---|
2908 | n/a | } |
---|
2909 | n/a | else if (err == 0) |
---|
2910 | n/a | JUMPTO(oparg); |
---|
2911 | n/a | else |
---|
2912 | n/a | goto error; |
---|
2913 | n/a | DISPATCH(); |
---|
2914 | n/a | } |
---|
2915 | n/a | |
---|
2916 | n/a | TARGET(JUMP_IF_TRUE_OR_POP) { |
---|
2917 | n/a | PyObject *cond = TOP(); |
---|
2918 | n/a | int err; |
---|
2919 | n/a | if (cond == Py_False) { |
---|
2920 | n/a | STACKADJ(-1); |
---|
2921 | n/a | Py_DECREF(cond); |
---|
2922 | n/a | FAST_DISPATCH(); |
---|
2923 | n/a | } |
---|
2924 | n/a | if (cond == Py_True) { |
---|
2925 | n/a | JUMPTO(oparg); |
---|
2926 | n/a | FAST_DISPATCH(); |
---|
2927 | n/a | } |
---|
2928 | n/a | err = PyObject_IsTrue(cond); |
---|
2929 | n/a | if (err > 0) { |
---|
2930 | n/a | err = 0; |
---|
2931 | n/a | JUMPTO(oparg); |
---|
2932 | n/a | } |
---|
2933 | n/a | else if (err == 0) { |
---|
2934 | n/a | STACKADJ(-1); |
---|
2935 | n/a | Py_DECREF(cond); |
---|
2936 | n/a | } |
---|
2937 | n/a | else |
---|
2938 | n/a | goto error; |
---|
2939 | n/a | DISPATCH(); |
---|
2940 | n/a | } |
---|
2941 | n/a | |
---|
2942 | n/a | PREDICTED(JUMP_ABSOLUTE); |
---|
2943 | n/a | TARGET(JUMP_ABSOLUTE) { |
---|
2944 | n/a | JUMPTO(oparg); |
---|
2945 | n/a | #if FAST_LOOPS |
---|
2946 | n/a | /* Enabling this path speeds-up all while and for-loops by bypassing |
---|
2947 | n/a | the per-loop checks for signals. By default, this should be turned-off |
---|
2948 | n/a | because it prevents detection of a control-break in tight loops like |
---|
2949 | n/a | "while 1: pass". Compile with this option turned-on when you need |
---|
2950 | n/a | the speed-up and do not need break checking inside tight loops (ones |
---|
2951 | n/a | that contain only instructions ending with FAST_DISPATCH). |
---|
2952 | n/a | */ |
---|
2953 | n/a | FAST_DISPATCH(); |
---|
2954 | n/a | #else |
---|
2955 | n/a | DISPATCH(); |
---|
2956 | n/a | #endif |
---|
2957 | n/a | } |
---|
2958 | n/a | |
---|
2959 | n/a | TARGET(GET_ITER) { |
---|
2960 | n/a | /* before: [obj]; after [getiter(obj)] */ |
---|
2961 | n/a | PyObject *iterable = TOP(); |
---|
2962 | n/a | PyObject *iter = PyObject_GetIter(iterable); |
---|
2963 | n/a | Py_DECREF(iterable); |
---|
2964 | n/a | SET_TOP(iter); |
---|
2965 | n/a | if (iter == NULL) |
---|
2966 | n/a | goto error; |
---|
2967 | n/a | PREDICT(FOR_ITER); |
---|
2968 | n/a | PREDICT(CALL_FUNCTION); |
---|
2969 | n/a | DISPATCH(); |
---|
2970 | n/a | } |
---|
2971 | n/a | |
---|
2972 | n/a | TARGET(GET_YIELD_FROM_ITER) { |
---|
2973 | n/a | /* before: [obj]; after [getiter(obj)] */ |
---|
2974 | n/a | PyObject *iterable = TOP(); |
---|
2975 | n/a | PyObject *iter; |
---|
2976 | n/a | if (PyCoro_CheckExact(iterable)) { |
---|
2977 | n/a | /* `iterable` is a coroutine */ |
---|
2978 | n/a | if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { |
---|
2979 | n/a | /* and it is used in a 'yield from' expression of a |
---|
2980 | n/a | regular generator. */ |
---|
2981 | n/a | Py_DECREF(iterable); |
---|
2982 | n/a | SET_TOP(NULL); |
---|
2983 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
2984 | n/a | "cannot 'yield from' a coroutine object " |
---|
2985 | n/a | "in a non-coroutine generator"); |
---|
2986 | n/a | goto error; |
---|
2987 | n/a | } |
---|
2988 | n/a | } |
---|
2989 | n/a | else if (!PyGen_CheckExact(iterable)) { |
---|
2990 | n/a | /* `iterable` is not a generator. */ |
---|
2991 | n/a | iter = PyObject_GetIter(iterable); |
---|
2992 | n/a | Py_DECREF(iterable); |
---|
2993 | n/a | SET_TOP(iter); |
---|
2994 | n/a | if (iter == NULL) |
---|
2995 | n/a | goto error; |
---|
2996 | n/a | } |
---|
2997 | n/a | PREDICT(LOAD_CONST); |
---|
2998 | n/a | DISPATCH(); |
---|
2999 | n/a | } |
---|
3000 | n/a | |
---|
3001 | n/a | PREDICTED(FOR_ITER); |
---|
3002 | n/a | TARGET(FOR_ITER) { |
---|
3003 | n/a | /* before: [iter]; after: [iter, iter()] *or* [] */ |
---|
3004 | n/a | PyObject *iter = TOP(); |
---|
3005 | n/a | PyObject *next = (*iter->ob_type->tp_iternext)(iter); |
---|
3006 | n/a | if (next != NULL) { |
---|
3007 | n/a | PUSH(next); |
---|
3008 | n/a | PREDICT(STORE_FAST); |
---|
3009 | n/a | PREDICT(UNPACK_SEQUENCE); |
---|
3010 | n/a | DISPATCH(); |
---|
3011 | n/a | } |
---|
3012 | n/a | if (PyErr_Occurred()) { |
---|
3013 | n/a | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
---|
3014 | n/a | goto error; |
---|
3015 | n/a | else if (tstate->c_tracefunc != NULL) |
---|
3016 | n/a | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f); |
---|
3017 | n/a | PyErr_Clear(); |
---|
3018 | n/a | } |
---|
3019 | n/a | /* iterator ended normally */ |
---|
3020 | n/a | STACKADJ(-1); |
---|
3021 | n/a | Py_DECREF(iter); |
---|
3022 | n/a | JUMPBY(oparg); |
---|
3023 | n/a | PREDICT(POP_BLOCK); |
---|
3024 | n/a | DISPATCH(); |
---|
3025 | n/a | } |
---|
3026 | n/a | |
---|
3027 | n/a | TARGET(BREAK_LOOP) { |
---|
3028 | n/a | why = WHY_BREAK; |
---|
3029 | n/a | goto fast_block_end; |
---|
3030 | n/a | } |
---|
3031 | n/a | |
---|
3032 | n/a | TARGET(CONTINUE_LOOP) { |
---|
3033 | n/a | retval = PyLong_FromLong(oparg); |
---|
3034 | n/a | if (retval == NULL) |
---|
3035 | n/a | goto error; |
---|
3036 | n/a | why = WHY_CONTINUE; |
---|
3037 | n/a | goto fast_block_end; |
---|
3038 | n/a | } |
---|
3039 | n/a | |
---|
3040 | n/a | TARGET(SETUP_LOOP) |
---|
3041 | n/a | TARGET(SETUP_EXCEPT) |
---|
3042 | n/a | TARGET(SETUP_FINALLY) { |
---|
3043 | n/a | /* NOTE: If you add any new block-setup opcodes that |
---|
3044 | n/a | are not try/except/finally handlers, you may need |
---|
3045 | n/a | to update the PyGen_NeedsFinalizing() function. |
---|
3046 | n/a | */ |
---|
3047 | n/a | |
---|
3048 | n/a | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
---|
3049 | n/a | STACK_LEVEL()); |
---|
3050 | n/a | DISPATCH(); |
---|
3051 | n/a | } |
---|
3052 | n/a | |
---|
3053 | n/a | TARGET(BEFORE_ASYNC_WITH) { |
---|
3054 | n/a | _Py_IDENTIFIER(__aexit__); |
---|
3055 | n/a | _Py_IDENTIFIER(__aenter__); |
---|
3056 | n/a | |
---|
3057 | n/a | PyObject *mgr = TOP(); |
---|
3058 | n/a | PyObject *exit = special_lookup(mgr, &PyId___aexit__), |
---|
3059 | n/a | *enter; |
---|
3060 | n/a | PyObject *res; |
---|
3061 | n/a | if (exit == NULL) |
---|
3062 | n/a | goto error; |
---|
3063 | n/a | SET_TOP(exit); |
---|
3064 | n/a | enter = special_lookup(mgr, &PyId___aenter__); |
---|
3065 | n/a | Py_DECREF(mgr); |
---|
3066 | n/a | if (enter == NULL) |
---|
3067 | n/a | goto error; |
---|
3068 | n/a | res = _PyObject_CallNoArg(enter); |
---|
3069 | n/a | Py_DECREF(enter); |
---|
3070 | n/a | if (res == NULL) |
---|
3071 | n/a | goto error; |
---|
3072 | n/a | PUSH(res); |
---|
3073 | n/a | PREDICT(GET_AWAITABLE); |
---|
3074 | n/a | DISPATCH(); |
---|
3075 | n/a | } |
---|
3076 | n/a | |
---|
3077 | n/a | TARGET(SETUP_ASYNC_WITH) { |
---|
3078 | n/a | PyObject *res = POP(); |
---|
3079 | n/a | /* Setup the finally block before pushing the result |
---|
3080 | n/a | of __aenter__ on the stack. */ |
---|
3081 | n/a | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
---|
3082 | n/a | STACK_LEVEL()); |
---|
3083 | n/a | PUSH(res); |
---|
3084 | n/a | DISPATCH(); |
---|
3085 | n/a | } |
---|
3086 | n/a | |
---|
3087 | n/a | TARGET(SETUP_WITH) { |
---|
3088 | n/a | _Py_IDENTIFIER(__exit__); |
---|
3089 | n/a | _Py_IDENTIFIER(__enter__); |
---|
3090 | n/a | PyObject *mgr = TOP(); |
---|
3091 | n/a | PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit; |
---|
3092 | n/a | PyObject *res; |
---|
3093 | n/a | if (enter == NULL) |
---|
3094 | n/a | goto error; |
---|
3095 | n/a | exit = special_lookup(mgr, &PyId___exit__); |
---|
3096 | n/a | if (exit == NULL) { |
---|
3097 | n/a | Py_DECREF(enter); |
---|
3098 | n/a | goto error; |
---|
3099 | n/a | } |
---|
3100 | n/a | SET_TOP(exit); |
---|
3101 | n/a | Py_DECREF(mgr); |
---|
3102 | n/a | res = _PyObject_CallNoArg(enter); |
---|
3103 | n/a | Py_DECREF(enter); |
---|
3104 | n/a | if (res == NULL) |
---|
3105 | n/a | goto error; |
---|
3106 | n/a | /* Setup the finally block before pushing the result |
---|
3107 | n/a | of __enter__ on the stack. */ |
---|
3108 | n/a | PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, |
---|
3109 | n/a | STACK_LEVEL()); |
---|
3110 | n/a | |
---|
3111 | n/a | PUSH(res); |
---|
3112 | n/a | DISPATCH(); |
---|
3113 | n/a | } |
---|
3114 | n/a | |
---|
3115 | n/a | TARGET(WITH_CLEANUP_START) { |
---|
3116 | n/a | /* At the top of the stack are 1-6 values indicating |
---|
3117 | n/a | how/why we entered the finally clause: |
---|
3118 | n/a | - TOP = None |
---|
3119 | n/a | - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval |
---|
3120 | n/a | - TOP = WHY_*; no retval below it |
---|
3121 | n/a | - (TOP, SECOND, THIRD) = exc_info() |
---|
3122 | n/a | (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER |
---|
3123 | n/a | Below them is EXIT, the context.__exit__ bound method. |
---|
3124 | n/a | In the last case, we must call |
---|
3125 | n/a | EXIT(TOP, SECOND, THIRD) |
---|
3126 | n/a | otherwise we must call |
---|
3127 | n/a | EXIT(None, None, None) |
---|
3128 | n/a | |
---|
3129 | n/a | In the first three cases, we remove EXIT from the |
---|
3130 | n/a | stack, leaving the rest in the same order. In the |
---|
3131 | n/a | fourth case, we shift the bottom 3 values of the |
---|
3132 | n/a | stack down, and replace the empty spot with NULL. |
---|
3133 | n/a | |
---|
3134 | n/a | In addition, if the stack represents an exception, |
---|
3135 | n/a | *and* the function call returns a 'true' value, we |
---|
3136 | n/a | push WHY_SILENCED onto the stack. END_FINALLY will |
---|
3137 | n/a | then not re-raise the exception. (But non-local |
---|
3138 | n/a | gotos should still be resumed.) |
---|
3139 | n/a | */ |
---|
3140 | n/a | |
---|
3141 | n/a | PyObject* stack[3]; |
---|
3142 | n/a | PyObject *exit_func; |
---|
3143 | n/a | PyObject *exc, *val, *tb, *res; |
---|
3144 | n/a | |
---|
3145 | n/a | val = tb = Py_None; |
---|
3146 | n/a | exc = TOP(); |
---|
3147 | n/a | if (exc == Py_None) { |
---|
3148 | n/a | (void)POP(); |
---|
3149 | n/a | exit_func = TOP(); |
---|
3150 | n/a | SET_TOP(exc); |
---|
3151 | n/a | } |
---|
3152 | n/a | else if (PyLong_Check(exc)) { |
---|
3153 | n/a | STACKADJ(-1); |
---|
3154 | n/a | switch (PyLong_AsLong(exc)) { |
---|
3155 | n/a | case WHY_RETURN: |
---|
3156 | n/a | case WHY_CONTINUE: |
---|
3157 | n/a | /* Retval in TOP. */ |
---|
3158 | n/a | exit_func = SECOND(); |
---|
3159 | n/a | SET_SECOND(TOP()); |
---|
3160 | n/a | SET_TOP(exc); |
---|
3161 | n/a | break; |
---|
3162 | n/a | default: |
---|
3163 | n/a | exit_func = TOP(); |
---|
3164 | n/a | SET_TOP(exc); |
---|
3165 | n/a | break; |
---|
3166 | n/a | } |
---|
3167 | n/a | exc = Py_None; |
---|
3168 | n/a | } |
---|
3169 | n/a | else { |
---|
3170 | n/a | PyObject *tp2, *exc2, *tb2; |
---|
3171 | n/a | PyTryBlock *block; |
---|
3172 | n/a | val = SECOND(); |
---|
3173 | n/a | tb = THIRD(); |
---|
3174 | n/a | tp2 = FOURTH(); |
---|
3175 | n/a | exc2 = PEEK(5); |
---|
3176 | n/a | tb2 = PEEK(6); |
---|
3177 | n/a | exit_func = PEEK(7); |
---|
3178 | n/a | SET_VALUE(7, tb2); |
---|
3179 | n/a | SET_VALUE(6, exc2); |
---|
3180 | n/a | SET_VALUE(5, tp2); |
---|
3181 | n/a | /* UNWIND_EXCEPT_HANDLER will pop this off. */ |
---|
3182 | n/a | SET_FOURTH(NULL); |
---|
3183 | n/a | /* We just shifted the stack down, so we have |
---|
3184 | n/a | to tell the except handler block that the |
---|
3185 | n/a | values are lower than it expects. */ |
---|
3186 | n/a | block = &f->f_blockstack[f->f_iblock - 1]; |
---|
3187 | n/a | assert(block->b_type == EXCEPT_HANDLER); |
---|
3188 | n/a | block->b_level--; |
---|
3189 | n/a | } |
---|
3190 | n/a | |
---|
3191 | n/a | stack[0] = exc; |
---|
3192 | n/a | stack[1] = val; |
---|
3193 | n/a | stack[2] = tb; |
---|
3194 | n/a | res = _PyObject_FastCall(exit_func, stack, 3); |
---|
3195 | n/a | Py_DECREF(exit_func); |
---|
3196 | n/a | if (res == NULL) |
---|
3197 | n/a | goto error; |
---|
3198 | n/a | |
---|
3199 | n/a | Py_INCREF(exc); /* Duplicating the exception on the stack */ |
---|
3200 | n/a | PUSH(exc); |
---|
3201 | n/a | PUSH(res); |
---|
3202 | n/a | PREDICT(WITH_CLEANUP_FINISH); |
---|
3203 | n/a | DISPATCH(); |
---|
3204 | n/a | } |
---|
3205 | n/a | |
---|
3206 | n/a | PREDICTED(WITH_CLEANUP_FINISH); |
---|
3207 | n/a | TARGET(WITH_CLEANUP_FINISH) { |
---|
3208 | n/a | PyObject *res = POP(); |
---|
3209 | n/a | PyObject *exc = POP(); |
---|
3210 | n/a | int err; |
---|
3211 | n/a | |
---|
3212 | n/a | if (exc != Py_None) |
---|
3213 | n/a | err = PyObject_IsTrue(res); |
---|
3214 | n/a | else |
---|
3215 | n/a | err = 0; |
---|
3216 | n/a | |
---|
3217 | n/a | Py_DECREF(res); |
---|
3218 | n/a | Py_DECREF(exc); |
---|
3219 | n/a | |
---|
3220 | n/a | if (err < 0) |
---|
3221 | n/a | goto error; |
---|
3222 | n/a | else if (err > 0) { |
---|
3223 | n/a | err = 0; |
---|
3224 | n/a | /* There was an exception and a True return */ |
---|
3225 | n/a | PUSH(PyLong_FromLong((long) WHY_SILENCED)); |
---|
3226 | n/a | } |
---|
3227 | n/a | PREDICT(END_FINALLY); |
---|
3228 | n/a | DISPATCH(); |
---|
3229 | n/a | } |
---|
3230 | n/a | |
---|
3231 | n/a | TARGET(LOAD_METHOD) { |
---|
3232 | n/a | /* Designed to work in tamdem with CALL_METHOD. */ |
---|
3233 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
3234 | n/a | PyObject *obj = TOP(); |
---|
3235 | n/a | PyObject *meth = NULL; |
---|
3236 | n/a | |
---|
3237 | n/a | int meth_found = _PyObject_GetMethod(obj, name, &meth); |
---|
3238 | n/a | |
---|
3239 | n/a | if (meth == NULL) { |
---|
3240 | n/a | /* Most likely attribute wasn't found. */ |
---|
3241 | n/a | goto error; |
---|
3242 | n/a | } |
---|
3243 | n/a | |
---|
3244 | n/a | if (meth_found) { |
---|
3245 | n/a | /* We can bypass temporary bound method object. |
---|
3246 | n/a | meth is unbound method and obj is self. |
---|
3247 | n/a | |
---|
3248 | n/a | meth | self | arg1 | ... | argN |
---|
3249 | n/a | */ |
---|
3250 | n/a | SET_TOP(meth); |
---|
3251 | n/a | PUSH(obj); // self |
---|
3252 | n/a | } |
---|
3253 | n/a | else { |
---|
3254 | n/a | /* meth is not an unbound method (but a regular attr, or |
---|
3255 | n/a | something was returned by a descriptor protocol). Set |
---|
3256 | n/a | the second element of the stack to NULL, to signal |
---|
3257 | n/a | CALL_METHOD that it's not a method call. |
---|
3258 | n/a | |
---|
3259 | n/a | NULL | meth | arg1 | ... | argN |
---|
3260 | n/a | */ |
---|
3261 | n/a | SET_TOP(NULL); |
---|
3262 | n/a | Py_DECREF(obj); |
---|
3263 | n/a | PUSH(meth); |
---|
3264 | n/a | } |
---|
3265 | n/a | DISPATCH(); |
---|
3266 | n/a | } |
---|
3267 | n/a | |
---|
3268 | n/a | TARGET(CALL_METHOD) { |
---|
3269 | n/a | /* Designed to work in tamdem with LOAD_METHOD. */ |
---|
3270 | n/a | PyObject **sp, *res, *meth; |
---|
3271 | n/a | |
---|
3272 | n/a | sp = stack_pointer; |
---|
3273 | n/a | |
---|
3274 | n/a | meth = PEEK(oparg + 2); |
---|
3275 | n/a | if (meth == NULL) { |
---|
3276 | n/a | /* `meth` is NULL when LOAD_METHOD thinks that it's not |
---|
3277 | n/a | a method call. |
---|
3278 | n/a | |
---|
3279 | n/a | Stack layout: |
---|
3280 | n/a | |
---|
3281 | n/a | ... | NULL | callable | arg1 | ... | argN |
---|
3282 | n/a | ^- TOP() |
---|
3283 | n/a | ^- (-oparg) |
---|
3284 | n/a | ^- (-oparg-1) |
---|
3285 | n/a | ^- (-oparg-2) |
---|
3286 | n/a | |
---|
3287 | n/a | `callable` will be POPed by call_funtion. |
---|
3288 | n/a | NULL will will be POPed manually later. |
---|
3289 | n/a | */ |
---|
3290 | n/a | res = call_function(&sp, oparg, NULL); |
---|
3291 | n/a | stack_pointer = sp; |
---|
3292 | n/a | (void)POP(); /* POP the NULL. */ |
---|
3293 | n/a | } |
---|
3294 | n/a | else { |
---|
3295 | n/a | /* This is a method call. Stack layout: |
---|
3296 | n/a | |
---|
3297 | n/a | ... | method | self | arg1 | ... | argN |
---|
3298 | n/a | ^- TOP() |
---|
3299 | n/a | ^- (-oparg) |
---|
3300 | n/a | ^- (-oparg-1) |
---|
3301 | n/a | ^- (-oparg-2) |
---|
3302 | n/a | |
---|
3303 | n/a | `self` and `method` will be POPed by call_function. |
---|
3304 | n/a | We'll be passing `oparg + 1` to call_function, to |
---|
3305 | n/a | make it accept the `self` as a first argument. |
---|
3306 | n/a | */ |
---|
3307 | n/a | res = call_function(&sp, oparg + 1, NULL); |
---|
3308 | n/a | stack_pointer = sp; |
---|
3309 | n/a | } |
---|
3310 | n/a | |
---|
3311 | n/a | PUSH(res); |
---|
3312 | n/a | if (res == NULL) |
---|
3313 | n/a | goto error; |
---|
3314 | n/a | DISPATCH(); |
---|
3315 | n/a | } |
---|
3316 | n/a | |
---|
3317 | n/a | PREDICTED(CALL_FUNCTION); |
---|
3318 | n/a | TARGET(CALL_FUNCTION) { |
---|
3319 | n/a | PyObject **sp, *res; |
---|
3320 | n/a | sp = stack_pointer; |
---|
3321 | n/a | res = call_function(&sp, oparg, NULL); |
---|
3322 | n/a | stack_pointer = sp; |
---|
3323 | n/a | PUSH(res); |
---|
3324 | n/a | if (res == NULL) { |
---|
3325 | n/a | goto error; |
---|
3326 | n/a | } |
---|
3327 | n/a | DISPATCH(); |
---|
3328 | n/a | } |
---|
3329 | n/a | |
---|
3330 | n/a | TARGET(CALL_FUNCTION_KW) { |
---|
3331 | n/a | PyObject **sp, *res, *names; |
---|
3332 | n/a | |
---|
3333 | n/a | names = POP(); |
---|
3334 | n/a | assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg); |
---|
3335 | n/a | sp = stack_pointer; |
---|
3336 | n/a | res = call_function(&sp, oparg, names); |
---|
3337 | n/a | stack_pointer = sp; |
---|
3338 | n/a | PUSH(res); |
---|
3339 | n/a | Py_DECREF(names); |
---|
3340 | n/a | |
---|
3341 | n/a | if (res == NULL) { |
---|
3342 | n/a | goto error; |
---|
3343 | n/a | } |
---|
3344 | n/a | DISPATCH(); |
---|
3345 | n/a | } |
---|
3346 | n/a | |
---|
3347 | n/a | TARGET(CALL_FUNCTION_EX) { |
---|
3348 | n/a | PyObject *func, *callargs, *kwargs = NULL, *result; |
---|
3349 | n/a | if (oparg & 0x01) { |
---|
3350 | n/a | kwargs = POP(); |
---|
3351 | n/a | if (!PyDict_CheckExact(kwargs)) { |
---|
3352 | n/a | PyObject *d = PyDict_New(); |
---|
3353 | n/a | if (d == NULL) |
---|
3354 | n/a | goto error; |
---|
3355 | n/a | if (PyDict_Update(d, kwargs) != 0) { |
---|
3356 | n/a | Py_DECREF(d); |
---|
3357 | n/a | /* PyDict_Update raises attribute |
---|
3358 | n/a | * error (percolated from an attempt |
---|
3359 | n/a | * to get 'keys' attribute) instead of |
---|
3360 | n/a | * a type error if its second argument |
---|
3361 | n/a | * is not a mapping. |
---|
3362 | n/a | */ |
---|
3363 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
---|
3364 | n/a | func = SECOND(); |
---|
3365 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3366 | n/a | "%.200s%.200s argument after ** " |
---|
3367 | n/a | "must be a mapping, not %.200s", |
---|
3368 | n/a | PyEval_GetFuncName(func), |
---|
3369 | n/a | PyEval_GetFuncDesc(func), |
---|
3370 | n/a | kwargs->ob_type->tp_name); |
---|
3371 | n/a | } |
---|
3372 | n/a | Py_DECREF(kwargs); |
---|
3373 | n/a | goto error; |
---|
3374 | n/a | } |
---|
3375 | n/a | Py_DECREF(kwargs); |
---|
3376 | n/a | kwargs = d; |
---|
3377 | n/a | } |
---|
3378 | n/a | assert(PyDict_CheckExact(kwargs)); |
---|
3379 | n/a | } |
---|
3380 | n/a | callargs = POP(); |
---|
3381 | n/a | func = TOP(); |
---|
3382 | n/a | if (!PyTuple_CheckExact(callargs)) { |
---|
3383 | n/a | if (Py_TYPE(callargs)->tp_iter == NULL && |
---|
3384 | n/a | !PySequence_Check(callargs)) { |
---|
3385 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3386 | n/a | "%.200s%.200s argument after * " |
---|
3387 | n/a | "must be an iterable, not %.200s", |
---|
3388 | n/a | PyEval_GetFuncName(func), |
---|
3389 | n/a | PyEval_GetFuncDesc(func), |
---|
3390 | n/a | callargs->ob_type->tp_name); |
---|
3391 | n/a | Py_DECREF(callargs); |
---|
3392 | n/a | goto error; |
---|
3393 | n/a | } |
---|
3394 | n/a | Py_SETREF(callargs, PySequence_Tuple(callargs)); |
---|
3395 | n/a | if (callargs == NULL) { |
---|
3396 | n/a | goto error; |
---|
3397 | n/a | } |
---|
3398 | n/a | } |
---|
3399 | n/a | assert(PyTuple_CheckExact(callargs)); |
---|
3400 | n/a | |
---|
3401 | n/a | result = do_call_core(func, callargs, kwargs); |
---|
3402 | n/a | Py_DECREF(func); |
---|
3403 | n/a | Py_DECREF(callargs); |
---|
3404 | n/a | Py_XDECREF(kwargs); |
---|
3405 | n/a | |
---|
3406 | n/a | SET_TOP(result); |
---|
3407 | n/a | if (result == NULL) { |
---|
3408 | n/a | goto error; |
---|
3409 | n/a | } |
---|
3410 | n/a | DISPATCH(); |
---|
3411 | n/a | } |
---|
3412 | n/a | |
---|
3413 | n/a | TARGET(MAKE_FUNCTION) { |
---|
3414 | n/a | PyObject *qualname = POP(); |
---|
3415 | n/a | PyObject *codeobj = POP(); |
---|
3416 | n/a | PyFunctionObject *func = (PyFunctionObject *) |
---|
3417 | n/a | PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); |
---|
3418 | n/a | |
---|
3419 | n/a | Py_DECREF(codeobj); |
---|
3420 | n/a | Py_DECREF(qualname); |
---|
3421 | n/a | if (func == NULL) { |
---|
3422 | n/a | goto error; |
---|
3423 | n/a | } |
---|
3424 | n/a | |
---|
3425 | n/a | if (oparg & 0x08) { |
---|
3426 | n/a | assert(PyTuple_CheckExact(TOP())); |
---|
3427 | n/a | func ->func_closure = POP(); |
---|
3428 | n/a | } |
---|
3429 | n/a | if (oparg & 0x04) { |
---|
3430 | n/a | assert(PyDict_CheckExact(TOP())); |
---|
3431 | n/a | func->func_annotations = POP(); |
---|
3432 | n/a | } |
---|
3433 | n/a | if (oparg & 0x02) { |
---|
3434 | n/a | assert(PyDict_CheckExact(TOP())); |
---|
3435 | n/a | func->func_kwdefaults = POP(); |
---|
3436 | n/a | } |
---|
3437 | n/a | if (oparg & 0x01) { |
---|
3438 | n/a | assert(PyTuple_CheckExact(TOP())); |
---|
3439 | n/a | func->func_defaults = POP(); |
---|
3440 | n/a | } |
---|
3441 | n/a | |
---|
3442 | n/a | PUSH((PyObject *)func); |
---|
3443 | n/a | DISPATCH(); |
---|
3444 | n/a | } |
---|
3445 | n/a | |
---|
3446 | n/a | TARGET(BUILD_SLICE) { |
---|
3447 | n/a | PyObject *start, *stop, *step, *slice; |
---|
3448 | n/a | if (oparg == 3) |
---|
3449 | n/a | step = POP(); |
---|
3450 | n/a | else |
---|
3451 | n/a | step = NULL; |
---|
3452 | n/a | stop = POP(); |
---|
3453 | n/a | start = TOP(); |
---|
3454 | n/a | slice = PySlice_New(start, stop, step); |
---|
3455 | n/a | Py_DECREF(start); |
---|
3456 | n/a | Py_DECREF(stop); |
---|
3457 | n/a | Py_XDECREF(step); |
---|
3458 | n/a | SET_TOP(slice); |
---|
3459 | n/a | if (slice == NULL) |
---|
3460 | n/a | goto error; |
---|
3461 | n/a | DISPATCH(); |
---|
3462 | n/a | } |
---|
3463 | n/a | |
---|
3464 | n/a | TARGET(FORMAT_VALUE) { |
---|
3465 | n/a | /* Handles f-string value formatting. */ |
---|
3466 | n/a | PyObject *result; |
---|
3467 | n/a | PyObject *fmt_spec; |
---|
3468 | n/a | PyObject *value; |
---|
3469 | n/a | PyObject *(*conv_fn)(PyObject *); |
---|
3470 | n/a | int which_conversion = oparg & FVC_MASK; |
---|
3471 | n/a | int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; |
---|
3472 | n/a | |
---|
3473 | n/a | fmt_spec = have_fmt_spec ? POP() : NULL; |
---|
3474 | n/a | value = POP(); |
---|
3475 | n/a | |
---|
3476 | n/a | /* See if any conversion is specified. */ |
---|
3477 | n/a | switch (which_conversion) { |
---|
3478 | n/a | case FVC_STR: conv_fn = PyObject_Str; break; |
---|
3479 | n/a | case FVC_REPR: conv_fn = PyObject_Repr; break; |
---|
3480 | n/a | case FVC_ASCII: conv_fn = PyObject_ASCII; break; |
---|
3481 | n/a | |
---|
3482 | n/a | /* Must be 0 (meaning no conversion), since only four |
---|
3483 | n/a | values are allowed by (oparg & FVC_MASK). */ |
---|
3484 | n/a | default: conv_fn = NULL; break; |
---|
3485 | n/a | } |
---|
3486 | n/a | |
---|
3487 | n/a | /* If there's a conversion function, call it and replace |
---|
3488 | n/a | value with that result. Otherwise, just use value, |
---|
3489 | n/a | without conversion. */ |
---|
3490 | n/a | if (conv_fn != NULL) { |
---|
3491 | n/a | result = conv_fn(value); |
---|
3492 | n/a | Py_DECREF(value); |
---|
3493 | n/a | if (result == NULL) { |
---|
3494 | n/a | Py_XDECREF(fmt_spec); |
---|
3495 | n/a | goto error; |
---|
3496 | n/a | } |
---|
3497 | n/a | value = result; |
---|
3498 | n/a | } |
---|
3499 | n/a | |
---|
3500 | n/a | /* If value is a unicode object, and there's no fmt_spec, |
---|
3501 | n/a | then we know the result of format(value) is value |
---|
3502 | n/a | itself. In that case, skip calling format(). I plan to |
---|
3503 | n/a | move this optimization in to PyObject_Format() |
---|
3504 | n/a | itself. */ |
---|
3505 | n/a | if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { |
---|
3506 | n/a | /* Do nothing, just transfer ownership to result. */ |
---|
3507 | n/a | result = value; |
---|
3508 | n/a | } else { |
---|
3509 | n/a | /* Actually call format(). */ |
---|
3510 | n/a | result = PyObject_Format(value, fmt_spec); |
---|
3511 | n/a | Py_DECREF(value); |
---|
3512 | n/a | Py_XDECREF(fmt_spec); |
---|
3513 | n/a | if (result == NULL) { |
---|
3514 | n/a | goto error; |
---|
3515 | n/a | } |
---|
3516 | n/a | } |
---|
3517 | n/a | |
---|
3518 | n/a | PUSH(result); |
---|
3519 | n/a | DISPATCH(); |
---|
3520 | n/a | } |
---|
3521 | n/a | |
---|
3522 | n/a | TARGET(EXTENDED_ARG) { |
---|
3523 | n/a | int oldoparg = oparg; |
---|
3524 | n/a | NEXTOPARG(); |
---|
3525 | n/a | oparg |= oldoparg << 8; |
---|
3526 | n/a | goto dispatch_opcode; |
---|
3527 | n/a | } |
---|
3528 | n/a | |
---|
3529 | n/a | |
---|
3530 | n/a | #if USE_COMPUTED_GOTOS |
---|
3531 | n/a | _unknown_opcode: |
---|
3532 | n/a | #endif |
---|
3533 | n/a | default: |
---|
3534 | n/a | fprintf(stderr, |
---|
3535 | n/a | "XXX lineno: %d, opcode: %d\n", |
---|
3536 | n/a | PyFrame_GetLineNumber(f), |
---|
3537 | n/a | opcode); |
---|
3538 | n/a | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
---|
3539 | n/a | goto error; |
---|
3540 | n/a | |
---|
3541 | n/a | #ifdef CASE_TOO_BIG |
---|
3542 | n/a | } |
---|
3543 | n/a | #endif |
---|
3544 | n/a | |
---|
3545 | n/a | } /* switch */ |
---|
3546 | n/a | |
---|
3547 | n/a | /* This should never be reached. Every opcode should end with DISPATCH() |
---|
3548 | n/a | or goto error. */ |
---|
3549 | n/a | assert(0); |
---|
3550 | n/a | |
---|
3551 | n/a | error: |
---|
3552 | n/a | |
---|
3553 | n/a | assert(why == WHY_NOT); |
---|
3554 | n/a | why = WHY_EXCEPTION; |
---|
3555 | n/a | |
---|
3556 | n/a | /* Double-check exception status. */ |
---|
3557 | n/a | #ifdef NDEBUG |
---|
3558 | n/a | if (!PyErr_Occurred()) |
---|
3559 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
3560 | n/a | "error return without exception set"); |
---|
3561 | n/a | #else |
---|
3562 | n/a | assert(PyErr_Occurred()); |
---|
3563 | n/a | #endif |
---|
3564 | n/a | |
---|
3565 | n/a | /* Log traceback info. */ |
---|
3566 | n/a | PyTraceBack_Here(f); |
---|
3567 | n/a | |
---|
3568 | n/a | if (tstate->c_tracefunc != NULL) |
---|
3569 | n/a | call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, |
---|
3570 | n/a | tstate, f); |
---|
3571 | n/a | |
---|
3572 | n/a | fast_block_end: |
---|
3573 | n/a | assert(why != WHY_NOT); |
---|
3574 | n/a | |
---|
3575 | n/a | /* Unwind stacks if a (pseudo) exception occurred */ |
---|
3576 | n/a | while (why != WHY_NOT && f->f_iblock > 0) { |
---|
3577 | n/a | /* Peek at the current block. */ |
---|
3578 | n/a | PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1]; |
---|
3579 | n/a | |
---|
3580 | n/a | assert(why != WHY_YIELD); |
---|
3581 | n/a | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
---|
3582 | n/a | why = WHY_NOT; |
---|
3583 | n/a | JUMPTO(PyLong_AS_LONG(retval)); |
---|
3584 | n/a | Py_DECREF(retval); |
---|
3585 | n/a | break; |
---|
3586 | n/a | } |
---|
3587 | n/a | /* Now we have to pop the block. */ |
---|
3588 | n/a | f->f_iblock--; |
---|
3589 | n/a | |
---|
3590 | n/a | if (b->b_type == EXCEPT_HANDLER) { |
---|
3591 | n/a | UNWIND_EXCEPT_HANDLER(b); |
---|
3592 | n/a | continue; |
---|
3593 | n/a | } |
---|
3594 | n/a | UNWIND_BLOCK(b); |
---|
3595 | n/a | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
---|
3596 | n/a | why = WHY_NOT; |
---|
3597 | n/a | JUMPTO(b->b_handler); |
---|
3598 | n/a | break; |
---|
3599 | n/a | } |
---|
3600 | n/a | if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT |
---|
3601 | n/a | || b->b_type == SETUP_FINALLY)) { |
---|
3602 | n/a | PyObject *exc, *val, *tb; |
---|
3603 | n/a | int handler = b->b_handler; |
---|
3604 | n/a | /* Beware, this invalidates all b->b_* fields */ |
---|
3605 | n/a | PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL()); |
---|
3606 | n/a | PUSH(tstate->exc_traceback); |
---|
3607 | n/a | PUSH(tstate->exc_value); |
---|
3608 | n/a | if (tstate->exc_type != NULL) { |
---|
3609 | n/a | PUSH(tstate->exc_type); |
---|
3610 | n/a | } |
---|
3611 | n/a | else { |
---|
3612 | n/a | Py_INCREF(Py_None); |
---|
3613 | n/a | PUSH(Py_None); |
---|
3614 | n/a | } |
---|
3615 | n/a | PyErr_Fetch(&exc, &val, &tb); |
---|
3616 | n/a | /* Make the raw exception data |
---|
3617 | n/a | available to the handler, |
---|
3618 | n/a | so a program can emulate the |
---|
3619 | n/a | Python main loop. */ |
---|
3620 | n/a | PyErr_NormalizeException( |
---|
3621 | n/a | &exc, &val, &tb); |
---|
3622 | n/a | if (tb != NULL) |
---|
3623 | n/a | PyException_SetTraceback(val, tb); |
---|
3624 | n/a | else |
---|
3625 | n/a | PyException_SetTraceback(val, Py_None); |
---|
3626 | n/a | Py_INCREF(exc); |
---|
3627 | n/a | tstate->exc_type = exc; |
---|
3628 | n/a | Py_INCREF(val); |
---|
3629 | n/a | tstate->exc_value = val; |
---|
3630 | n/a | tstate->exc_traceback = tb; |
---|
3631 | n/a | if (tb == NULL) |
---|
3632 | n/a | tb = Py_None; |
---|
3633 | n/a | Py_INCREF(tb); |
---|
3634 | n/a | PUSH(tb); |
---|
3635 | n/a | PUSH(val); |
---|
3636 | n/a | PUSH(exc); |
---|
3637 | n/a | why = WHY_NOT; |
---|
3638 | n/a | JUMPTO(handler); |
---|
3639 | n/a | break; |
---|
3640 | n/a | } |
---|
3641 | n/a | if (b->b_type == SETUP_FINALLY) { |
---|
3642 | n/a | if (why & (WHY_RETURN | WHY_CONTINUE)) |
---|
3643 | n/a | PUSH(retval); |
---|
3644 | n/a | PUSH(PyLong_FromLong((long)why)); |
---|
3645 | n/a | why = WHY_NOT; |
---|
3646 | n/a | JUMPTO(b->b_handler); |
---|
3647 | n/a | break; |
---|
3648 | n/a | } |
---|
3649 | n/a | } /* unwind stack */ |
---|
3650 | n/a | |
---|
3651 | n/a | /* End the loop if we still have an error (or return) */ |
---|
3652 | n/a | |
---|
3653 | n/a | if (why != WHY_NOT) |
---|
3654 | n/a | break; |
---|
3655 | n/a | |
---|
3656 | n/a | assert(!PyErr_Occurred()); |
---|
3657 | n/a | |
---|
3658 | n/a | } /* main loop */ |
---|
3659 | n/a | |
---|
3660 | n/a | assert(why != WHY_YIELD); |
---|
3661 | n/a | /* Pop remaining stack entries. */ |
---|
3662 | n/a | while (!EMPTY()) { |
---|
3663 | n/a | PyObject *o = POP(); |
---|
3664 | n/a | Py_XDECREF(o); |
---|
3665 | n/a | } |
---|
3666 | n/a | |
---|
3667 | n/a | if (why != WHY_RETURN) |
---|
3668 | n/a | retval = NULL; |
---|
3669 | n/a | |
---|
3670 | n/a | assert((retval != NULL) ^ (PyErr_Occurred() != NULL)); |
---|
3671 | n/a | |
---|
3672 | n/a | fast_yield: |
---|
3673 | n/a | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
---|
3674 | n/a | |
---|
3675 | n/a | /* The purpose of this block is to put aside the generator's exception |
---|
3676 | n/a | state and restore that of the calling frame. If the current |
---|
3677 | n/a | exception state is from the caller, we clear the exception values |
---|
3678 | n/a | on the generator frame, so they are not swapped back in latter. The |
---|
3679 | n/a | origin of the current exception state is determined by checking for |
---|
3680 | n/a | except handler blocks, which we must be in iff a new exception |
---|
3681 | n/a | state came into existence in this frame. (An uncaught exception |
---|
3682 | n/a | would have why == WHY_EXCEPTION, and we wouldn't be here). */ |
---|
3683 | n/a | int i; |
---|
3684 | n/a | for (i = 0; i < f->f_iblock; i++) { |
---|
3685 | n/a | if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) { |
---|
3686 | n/a | break; |
---|
3687 | n/a | } |
---|
3688 | n/a | } |
---|
3689 | n/a | if (i == f->f_iblock) |
---|
3690 | n/a | /* We did not create this exception. */ |
---|
3691 | n/a | restore_and_clear_exc_state(tstate, f); |
---|
3692 | n/a | else |
---|
3693 | n/a | swap_exc_state(tstate, f); |
---|
3694 | n/a | } |
---|
3695 | n/a | |
---|
3696 | n/a | if (tstate->use_tracing) { |
---|
3697 | n/a | if (tstate->c_tracefunc) { |
---|
3698 | n/a | if (why == WHY_RETURN || why == WHY_YIELD) { |
---|
3699 | n/a | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
---|
3700 | n/a | tstate, f, |
---|
3701 | n/a | PyTrace_RETURN, retval)) { |
---|
3702 | n/a | Py_CLEAR(retval); |
---|
3703 | n/a | why = WHY_EXCEPTION; |
---|
3704 | n/a | } |
---|
3705 | n/a | } |
---|
3706 | n/a | else if (why == WHY_EXCEPTION) { |
---|
3707 | n/a | call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, |
---|
3708 | n/a | tstate, f, |
---|
3709 | n/a | PyTrace_RETURN, NULL); |
---|
3710 | n/a | } |
---|
3711 | n/a | } |
---|
3712 | n/a | if (tstate->c_profilefunc) { |
---|
3713 | n/a | if (why == WHY_EXCEPTION) |
---|
3714 | n/a | call_trace_protected(tstate->c_profilefunc, |
---|
3715 | n/a | tstate->c_profileobj, |
---|
3716 | n/a | tstate, f, |
---|
3717 | n/a | PyTrace_RETURN, NULL); |
---|
3718 | n/a | else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, |
---|
3719 | n/a | tstate, f, |
---|
3720 | n/a | PyTrace_RETURN, retval)) { |
---|
3721 | n/a | Py_CLEAR(retval); |
---|
3722 | n/a | /* why = WHY_EXCEPTION; */ |
---|
3723 | n/a | } |
---|
3724 | n/a | } |
---|
3725 | n/a | } |
---|
3726 | n/a | |
---|
3727 | n/a | /* pop frame */ |
---|
3728 | n/a | exit_eval_frame: |
---|
3729 | n/a | if (PyDTrace_FUNCTION_RETURN_ENABLED()) |
---|
3730 | n/a | dtrace_function_return(f); |
---|
3731 | n/a | Py_LeaveRecursiveCall(); |
---|
3732 | n/a | f->f_executing = 0; |
---|
3733 | n/a | tstate->frame = f->f_back; |
---|
3734 | n/a | |
---|
3735 | n/a | return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx"); |
---|
3736 | n/a | } |
---|
3737 | n/a | |
---|
3738 | n/a | static void |
---|
3739 | n/a | format_missing(const char *kind, PyCodeObject *co, PyObject *names) |
---|
3740 | n/a | { |
---|
3741 | n/a | int err; |
---|
3742 | n/a | Py_ssize_t len = PyList_GET_SIZE(names); |
---|
3743 | n/a | PyObject *name_str, *comma, *tail, *tmp; |
---|
3744 | n/a | |
---|
3745 | n/a | assert(PyList_CheckExact(names)); |
---|
3746 | n/a | assert(len >= 1); |
---|
3747 | n/a | /* Deal with the joys of natural language. */ |
---|
3748 | n/a | switch (len) { |
---|
3749 | n/a | case 1: |
---|
3750 | n/a | name_str = PyList_GET_ITEM(names, 0); |
---|
3751 | n/a | Py_INCREF(name_str); |
---|
3752 | n/a | break; |
---|
3753 | n/a | case 2: |
---|
3754 | n/a | name_str = PyUnicode_FromFormat("%U and %U", |
---|
3755 | n/a | PyList_GET_ITEM(names, len - 2), |
---|
3756 | n/a | PyList_GET_ITEM(names, len - 1)); |
---|
3757 | n/a | break; |
---|
3758 | n/a | default: |
---|
3759 | n/a | tail = PyUnicode_FromFormat(", %U, and %U", |
---|
3760 | n/a | PyList_GET_ITEM(names, len - 2), |
---|
3761 | n/a | PyList_GET_ITEM(names, len - 1)); |
---|
3762 | n/a | if (tail == NULL) |
---|
3763 | n/a | return; |
---|
3764 | n/a | /* Chop off the last two objects in the list. This shouldn't actually |
---|
3765 | n/a | fail, but we can't be too careful. */ |
---|
3766 | n/a | err = PyList_SetSlice(names, len - 2, len, NULL); |
---|
3767 | n/a | if (err == -1) { |
---|
3768 | n/a | Py_DECREF(tail); |
---|
3769 | n/a | return; |
---|
3770 | n/a | } |
---|
3771 | n/a | /* Stitch everything up into a nice comma-separated list. */ |
---|
3772 | n/a | comma = PyUnicode_FromString(", "); |
---|
3773 | n/a | if (comma == NULL) { |
---|
3774 | n/a | Py_DECREF(tail); |
---|
3775 | n/a | return; |
---|
3776 | n/a | } |
---|
3777 | n/a | tmp = PyUnicode_Join(comma, names); |
---|
3778 | n/a | Py_DECREF(comma); |
---|
3779 | n/a | if (tmp == NULL) { |
---|
3780 | n/a | Py_DECREF(tail); |
---|
3781 | n/a | return; |
---|
3782 | n/a | } |
---|
3783 | n/a | name_str = PyUnicode_Concat(tmp, tail); |
---|
3784 | n/a | Py_DECREF(tmp); |
---|
3785 | n/a | Py_DECREF(tail); |
---|
3786 | n/a | break; |
---|
3787 | n/a | } |
---|
3788 | n/a | if (name_str == NULL) |
---|
3789 | n/a | return; |
---|
3790 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3791 | n/a | "%U() missing %i required %s argument%s: %U", |
---|
3792 | n/a | co->co_name, |
---|
3793 | n/a | len, |
---|
3794 | n/a | kind, |
---|
3795 | n/a | len == 1 ? "" : "s", |
---|
3796 | n/a | name_str); |
---|
3797 | n/a | Py_DECREF(name_str); |
---|
3798 | n/a | } |
---|
3799 | n/a | |
---|
3800 | n/a | static void |
---|
3801 | n/a | missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount, |
---|
3802 | n/a | PyObject **fastlocals) |
---|
3803 | n/a | { |
---|
3804 | n/a | Py_ssize_t i, j = 0; |
---|
3805 | n/a | Py_ssize_t start, end; |
---|
3806 | n/a | int positional = (defcount != -1); |
---|
3807 | n/a | const char *kind = positional ? "positional" : "keyword-only"; |
---|
3808 | n/a | PyObject *missing_names; |
---|
3809 | n/a | |
---|
3810 | n/a | /* Compute the names of the arguments that are missing. */ |
---|
3811 | n/a | missing_names = PyList_New(missing); |
---|
3812 | n/a | if (missing_names == NULL) |
---|
3813 | n/a | return; |
---|
3814 | n/a | if (positional) { |
---|
3815 | n/a | start = 0; |
---|
3816 | n/a | end = co->co_argcount - defcount; |
---|
3817 | n/a | } |
---|
3818 | n/a | else { |
---|
3819 | n/a | start = co->co_argcount; |
---|
3820 | n/a | end = start + co->co_kwonlyargcount; |
---|
3821 | n/a | } |
---|
3822 | n/a | for (i = start; i < end; i++) { |
---|
3823 | n/a | if (GETLOCAL(i) == NULL) { |
---|
3824 | n/a | PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); |
---|
3825 | n/a | PyObject *name = PyObject_Repr(raw); |
---|
3826 | n/a | if (name == NULL) { |
---|
3827 | n/a | Py_DECREF(missing_names); |
---|
3828 | n/a | return; |
---|
3829 | n/a | } |
---|
3830 | n/a | PyList_SET_ITEM(missing_names, j++, name); |
---|
3831 | n/a | } |
---|
3832 | n/a | } |
---|
3833 | n/a | assert(j == missing); |
---|
3834 | n/a | format_missing(kind, co, missing_names); |
---|
3835 | n/a | Py_DECREF(missing_names); |
---|
3836 | n/a | } |
---|
3837 | n/a | |
---|
3838 | n/a | static void |
---|
3839 | n/a | too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount, |
---|
3840 | n/a | PyObject **fastlocals) |
---|
3841 | n/a | { |
---|
3842 | n/a | int plural; |
---|
3843 | n/a | Py_ssize_t kwonly_given = 0; |
---|
3844 | n/a | Py_ssize_t i; |
---|
3845 | n/a | PyObject *sig, *kwonly_sig; |
---|
3846 | n/a | Py_ssize_t co_argcount = co->co_argcount; |
---|
3847 | n/a | |
---|
3848 | n/a | assert((co->co_flags & CO_VARARGS) == 0); |
---|
3849 | n/a | /* Count missing keyword-only args. */ |
---|
3850 | n/a | for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) { |
---|
3851 | n/a | if (GETLOCAL(i) != NULL) { |
---|
3852 | n/a | kwonly_given++; |
---|
3853 | n/a | } |
---|
3854 | n/a | } |
---|
3855 | n/a | if (defcount) { |
---|
3856 | n/a | Py_ssize_t atleast = co_argcount - defcount; |
---|
3857 | n/a | plural = 1; |
---|
3858 | n/a | sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount); |
---|
3859 | n/a | } |
---|
3860 | n/a | else { |
---|
3861 | n/a | plural = (co_argcount != 1); |
---|
3862 | n/a | sig = PyUnicode_FromFormat("%zd", co_argcount); |
---|
3863 | n/a | } |
---|
3864 | n/a | if (sig == NULL) |
---|
3865 | n/a | return; |
---|
3866 | n/a | if (kwonly_given) { |
---|
3867 | n/a | const char *format = " positional argument%s (and %zd keyword-only argument%s)"; |
---|
3868 | n/a | kwonly_sig = PyUnicode_FromFormat(format, |
---|
3869 | n/a | given != 1 ? "s" : "", |
---|
3870 | n/a | kwonly_given, |
---|
3871 | n/a | kwonly_given != 1 ? "s" : ""); |
---|
3872 | n/a | if (kwonly_sig == NULL) { |
---|
3873 | n/a | Py_DECREF(sig); |
---|
3874 | n/a | return; |
---|
3875 | n/a | } |
---|
3876 | n/a | } |
---|
3877 | n/a | else { |
---|
3878 | n/a | /* This will not fail. */ |
---|
3879 | n/a | kwonly_sig = PyUnicode_FromString(""); |
---|
3880 | n/a | assert(kwonly_sig != NULL); |
---|
3881 | n/a | } |
---|
3882 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3883 | n/a | "%U() takes %U positional argument%s but %zd%U %s given", |
---|
3884 | n/a | co->co_name, |
---|
3885 | n/a | sig, |
---|
3886 | n/a | plural ? "s" : "", |
---|
3887 | n/a | given, |
---|
3888 | n/a | kwonly_sig, |
---|
3889 | n/a | given == 1 && !kwonly_given ? "was" : "were"); |
---|
3890 | n/a | Py_DECREF(sig); |
---|
3891 | n/a | Py_DECREF(kwonly_sig); |
---|
3892 | n/a | } |
---|
3893 | n/a | |
---|
3894 | n/a | /* This is gonna seem *real weird*, but if you put some other code between |
---|
3895 | n/a | PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust |
---|
3896 | n/a | the test in the if statements in Misc/gdbinit (pystack and pystackv). */ |
---|
3897 | n/a | |
---|
3898 | n/a | static PyObject * |
---|
3899 | n/a | _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals, |
---|
3900 | n/a | PyObject **args, Py_ssize_t argcount, |
---|
3901 | n/a | PyObject **kwnames, PyObject **kwargs, |
---|
3902 | n/a | Py_ssize_t kwcount, int kwstep, |
---|
3903 | n/a | PyObject **defs, Py_ssize_t defcount, |
---|
3904 | n/a | PyObject *kwdefs, PyObject *closure, |
---|
3905 | n/a | PyObject *name, PyObject *qualname) |
---|
3906 | n/a | { |
---|
3907 | n/a | PyCodeObject* co = (PyCodeObject*)_co; |
---|
3908 | n/a | PyFrameObject *f; |
---|
3909 | n/a | PyObject *retval = NULL; |
---|
3910 | n/a | PyObject **fastlocals, **freevars; |
---|
3911 | n/a | PyThreadState *tstate; |
---|
3912 | n/a | PyObject *x, *u; |
---|
3913 | n/a | const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount; |
---|
3914 | n/a | Py_ssize_t i, n; |
---|
3915 | n/a | PyObject *kwdict; |
---|
3916 | n/a | |
---|
3917 | n/a | if (globals == NULL) { |
---|
3918 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
3919 | n/a | "PyEval_EvalCodeEx: NULL globals"); |
---|
3920 | n/a | return NULL; |
---|
3921 | n/a | } |
---|
3922 | n/a | |
---|
3923 | n/a | /* Create the frame */ |
---|
3924 | n/a | tstate = PyThreadState_GET(); |
---|
3925 | n/a | assert(tstate != NULL); |
---|
3926 | n/a | f = _PyFrame_New_NoTrack(tstate, co, globals, locals); |
---|
3927 | n/a | if (f == NULL) { |
---|
3928 | n/a | return NULL; |
---|
3929 | n/a | } |
---|
3930 | n/a | fastlocals = f->f_localsplus; |
---|
3931 | n/a | freevars = f->f_localsplus + co->co_nlocals; |
---|
3932 | n/a | |
---|
3933 | n/a | /* Create a dictionary for keyword parameters (**kwags) */ |
---|
3934 | n/a | if (co->co_flags & CO_VARKEYWORDS) { |
---|
3935 | n/a | kwdict = PyDict_New(); |
---|
3936 | n/a | if (kwdict == NULL) |
---|
3937 | n/a | goto fail; |
---|
3938 | n/a | i = total_args; |
---|
3939 | n/a | if (co->co_flags & CO_VARARGS) { |
---|
3940 | n/a | i++; |
---|
3941 | n/a | } |
---|
3942 | n/a | SETLOCAL(i, kwdict); |
---|
3943 | n/a | } |
---|
3944 | n/a | else { |
---|
3945 | n/a | kwdict = NULL; |
---|
3946 | n/a | } |
---|
3947 | n/a | |
---|
3948 | n/a | /* Copy positional arguments into local variables */ |
---|
3949 | n/a | if (argcount > co->co_argcount) { |
---|
3950 | n/a | n = co->co_argcount; |
---|
3951 | n/a | } |
---|
3952 | n/a | else { |
---|
3953 | n/a | n = argcount; |
---|
3954 | n/a | } |
---|
3955 | n/a | for (i = 0; i < n; i++) { |
---|
3956 | n/a | x = args[i]; |
---|
3957 | n/a | Py_INCREF(x); |
---|
3958 | n/a | SETLOCAL(i, x); |
---|
3959 | n/a | } |
---|
3960 | n/a | |
---|
3961 | n/a | /* Pack other positional arguments into the *args argument */ |
---|
3962 | n/a | if (co->co_flags & CO_VARARGS) { |
---|
3963 | n/a | u = PyTuple_New(argcount - n); |
---|
3964 | n/a | if (u == NULL) { |
---|
3965 | n/a | goto fail; |
---|
3966 | n/a | } |
---|
3967 | n/a | SETLOCAL(total_args, u); |
---|
3968 | n/a | for (i = n; i < argcount; i++) { |
---|
3969 | n/a | x = args[i]; |
---|
3970 | n/a | Py_INCREF(x); |
---|
3971 | n/a | PyTuple_SET_ITEM(u, i-n, x); |
---|
3972 | n/a | } |
---|
3973 | n/a | } |
---|
3974 | n/a | |
---|
3975 | n/a | /* Handle keyword arguments passed as two strided arrays */ |
---|
3976 | n/a | kwcount *= kwstep; |
---|
3977 | n/a | for (i = 0; i < kwcount; i += kwstep) { |
---|
3978 | n/a | PyObject **co_varnames; |
---|
3979 | n/a | PyObject *keyword = kwnames[i]; |
---|
3980 | n/a | PyObject *value = kwargs[i]; |
---|
3981 | n/a | Py_ssize_t j; |
---|
3982 | n/a | |
---|
3983 | n/a | if (keyword == NULL || !PyUnicode_Check(keyword)) { |
---|
3984 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3985 | n/a | "%U() keywords must be strings", |
---|
3986 | n/a | co->co_name); |
---|
3987 | n/a | goto fail; |
---|
3988 | n/a | } |
---|
3989 | n/a | |
---|
3990 | n/a | /* Speed hack: do raw pointer compares. As names are |
---|
3991 | n/a | normally interned this should almost always hit. */ |
---|
3992 | n/a | co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item; |
---|
3993 | n/a | for (j = 0; j < total_args; j++) { |
---|
3994 | n/a | PyObject *name = co_varnames[j]; |
---|
3995 | n/a | if (name == keyword) { |
---|
3996 | n/a | goto kw_found; |
---|
3997 | n/a | } |
---|
3998 | n/a | } |
---|
3999 | n/a | |
---|
4000 | n/a | /* Slow fallback, just in case */ |
---|
4001 | n/a | for (j = 0; j < total_args; j++) { |
---|
4002 | n/a | PyObject *name = co_varnames[j]; |
---|
4003 | n/a | int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ); |
---|
4004 | n/a | if (cmp > 0) { |
---|
4005 | n/a | goto kw_found; |
---|
4006 | n/a | } |
---|
4007 | n/a | else if (cmp < 0) { |
---|
4008 | n/a | goto fail; |
---|
4009 | n/a | } |
---|
4010 | n/a | } |
---|
4011 | n/a | |
---|
4012 | n/a | assert(j >= total_args); |
---|
4013 | n/a | if (kwdict == NULL) { |
---|
4014 | n/a | PyErr_Format(PyExc_TypeError, |
---|
4015 | n/a | "%U() got an unexpected keyword argument '%S'", |
---|
4016 | n/a | co->co_name, keyword); |
---|
4017 | n/a | goto fail; |
---|
4018 | n/a | } |
---|
4019 | n/a | |
---|
4020 | n/a | if (PyDict_SetItem(kwdict, keyword, value) == -1) { |
---|
4021 | n/a | goto fail; |
---|
4022 | n/a | } |
---|
4023 | n/a | continue; |
---|
4024 | n/a | |
---|
4025 | n/a | kw_found: |
---|
4026 | n/a | if (GETLOCAL(j) != NULL) { |
---|
4027 | n/a | PyErr_Format(PyExc_TypeError, |
---|
4028 | n/a | "%U() got multiple values for argument '%S'", |
---|
4029 | n/a | co->co_name, keyword); |
---|
4030 | n/a | goto fail; |
---|
4031 | n/a | } |
---|
4032 | n/a | Py_INCREF(value); |
---|
4033 | n/a | SETLOCAL(j, value); |
---|
4034 | n/a | } |
---|
4035 | n/a | |
---|
4036 | n/a | /* Check the number of positional arguments */ |
---|
4037 | n/a | if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { |
---|
4038 | n/a | too_many_positional(co, argcount, defcount, fastlocals); |
---|
4039 | n/a | goto fail; |
---|
4040 | n/a | } |
---|
4041 | n/a | |
---|
4042 | n/a | /* Add missing positional arguments (copy default values from defs) */ |
---|
4043 | n/a | if (argcount < co->co_argcount) { |
---|
4044 | n/a | Py_ssize_t m = co->co_argcount - defcount; |
---|
4045 | n/a | Py_ssize_t missing = 0; |
---|
4046 | n/a | for (i = argcount; i < m; i++) { |
---|
4047 | n/a | if (GETLOCAL(i) == NULL) { |
---|
4048 | n/a | missing++; |
---|
4049 | n/a | } |
---|
4050 | n/a | } |
---|
4051 | n/a | if (missing) { |
---|
4052 | n/a | missing_arguments(co, missing, defcount, fastlocals); |
---|
4053 | n/a | goto fail; |
---|
4054 | n/a | } |
---|
4055 | n/a | if (n > m) |
---|
4056 | n/a | i = n - m; |
---|
4057 | n/a | else |
---|
4058 | n/a | i = 0; |
---|
4059 | n/a | for (; i < defcount; i++) { |
---|
4060 | n/a | if (GETLOCAL(m+i) == NULL) { |
---|
4061 | n/a | PyObject *def = defs[i]; |
---|
4062 | n/a | Py_INCREF(def); |
---|
4063 | n/a | SETLOCAL(m+i, def); |
---|
4064 | n/a | } |
---|
4065 | n/a | } |
---|
4066 | n/a | } |
---|
4067 | n/a | |
---|
4068 | n/a | /* Add missing keyword arguments (copy default values from kwdefs) */ |
---|
4069 | n/a | if (co->co_kwonlyargcount > 0) { |
---|
4070 | n/a | Py_ssize_t missing = 0; |
---|
4071 | n/a | for (i = co->co_argcount; i < total_args; i++) { |
---|
4072 | n/a | PyObject *name; |
---|
4073 | n/a | if (GETLOCAL(i) != NULL) |
---|
4074 | n/a | continue; |
---|
4075 | n/a | name = PyTuple_GET_ITEM(co->co_varnames, i); |
---|
4076 | n/a | if (kwdefs != NULL) { |
---|
4077 | n/a | PyObject *def = PyDict_GetItem(kwdefs, name); |
---|
4078 | n/a | if (def) { |
---|
4079 | n/a | Py_INCREF(def); |
---|
4080 | n/a | SETLOCAL(i, def); |
---|
4081 | n/a | continue; |
---|
4082 | n/a | } |
---|
4083 | n/a | } |
---|
4084 | n/a | missing++; |
---|
4085 | n/a | } |
---|
4086 | n/a | if (missing) { |
---|
4087 | n/a | missing_arguments(co, missing, -1, fastlocals); |
---|
4088 | n/a | goto fail; |
---|
4089 | n/a | } |
---|
4090 | n/a | } |
---|
4091 | n/a | |
---|
4092 | n/a | /* Allocate and initialize storage for cell vars, and copy free |
---|
4093 | n/a | vars into frame. */ |
---|
4094 | n/a | for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) { |
---|
4095 | n/a | PyObject *c; |
---|
4096 | n/a | Py_ssize_t arg; |
---|
4097 | n/a | /* Possibly account for the cell variable being an argument. */ |
---|
4098 | n/a | if (co->co_cell2arg != NULL && |
---|
4099 | n/a | (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) { |
---|
4100 | n/a | c = PyCell_New(GETLOCAL(arg)); |
---|
4101 | n/a | /* Clear the local copy. */ |
---|
4102 | n/a | SETLOCAL(arg, NULL); |
---|
4103 | n/a | } |
---|
4104 | n/a | else { |
---|
4105 | n/a | c = PyCell_New(NULL); |
---|
4106 | n/a | } |
---|
4107 | n/a | if (c == NULL) |
---|
4108 | n/a | goto fail; |
---|
4109 | n/a | SETLOCAL(co->co_nlocals + i, c); |
---|
4110 | n/a | } |
---|
4111 | n/a | |
---|
4112 | n/a | /* Copy closure variables to free variables */ |
---|
4113 | n/a | for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) { |
---|
4114 | n/a | PyObject *o = PyTuple_GET_ITEM(closure, i); |
---|
4115 | n/a | Py_INCREF(o); |
---|
4116 | n/a | freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o; |
---|
4117 | n/a | } |
---|
4118 | n/a | |
---|
4119 | n/a | /* Handle generator/coroutine/asynchronous generator */ |
---|
4120 | n/a | if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
---|
4121 | n/a | PyObject *gen; |
---|
4122 | n/a | PyObject *coro_wrapper = tstate->coroutine_wrapper; |
---|
4123 | n/a | int is_coro = co->co_flags & CO_COROUTINE; |
---|
4124 | n/a | |
---|
4125 | n/a | if (is_coro && tstate->in_coroutine_wrapper) { |
---|
4126 | n/a | assert(coro_wrapper != NULL); |
---|
4127 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
4128 | n/a | "coroutine wrapper %.200R attempted " |
---|
4129 | n/a | "to recursively wrap %.200R", |
---|
4130 | n/a | coro_wrapper, |
---|
4131 | n/a | co); |
---|
4132 | n/a | goto fail; |
---|
4133 | n/a | } |
---|
4134 | n/a | |
---|
4135 | n/a | /* Don't need to keep the reference to f_back, it will be set |
---|
4136 | n/a | * when the generator is resumed. */ |
---|
4137 | n/a | Py_CLEAR(f->f_back); |
---|
4138 | n/a | |
---|
4139 | n/a | /* Create a new generator that owns the ready to run frame |
---|
4140 | n/a | * and return that as the value. */ |
---|
4141 | n/a | if (is_coro) { |
---|
4142 | n/a | gen = PyCoro_New(f, name, qualname); |
---|
4143 | n/a | } else if (co->co_flags & CO_ASYNC_GENERATOR) { |
---|
4144 | n/a | gen = PyAsyncGen_New(f, name, qualname); |
---|
4145 | n/a | } else { |
---|
4146 | n/a | gen = PyGen_NewWithQualName(f, name, qualname); |
---|
4147 | n/a | } |
---|
4148 | n/a | if (gen == NULL) { |
---|
4149 | n/a | return NULL; |
---|
4150 | n/a | } |
---|
4151 | n/a | |
---|
4152 | n/a | _PyObject_GC_TRACK(f); |
---|
4153 | n/a | |
---|
4154 | n/a | if (is_coro && coro_wrapper != NULL) { |
---|
4155 | n/a | PyObject *wrapped; |
---|
4156 | n/a | tstate->in_coroutine_wrapper = 1; |
---|
4157 | n/a | wrapped = PyObject_CallFunction(coro_wrapper, "N", gen); |
---|
4158 | n/a | tstate->in_coroutine_wrapper = 0; |
---|
4159 | n/a | return wrapped; |
---|
4160 | n/a | } |
---|
4161 | n/a | |
---|
4162 | n/a | return gen; |
---|
4163 | n/a | } |
---|
4164 | n/a | |
---|
4165 | n/a | retval = PyEval_EvalFrameEx(f,0); |
---|
4166 | n/a | |
---|
4167 | n/a | fail: /* Jump here from prelude on failure */ |
---|
4168 | n/a | |
---|
4169 | n/a | /* decref'ing the frame can cause __del__ methods to get invoked, |
---|
4170 | n/a | which can call back into Python. While we're done with the |
---|
4171 | n/a | current Python frame (f), the associated C stack is still in use, |
---|
4172 | n/a | so recursion_depth must be boosted for the duration. |
---|
4173 | n/a | */ |
---|
4174 | n/a | assert(tstate != NULL); |
---|
4175 | n/a | if (Py_REFCNT(f) > 1) { |
---|
4176 | n/a | Py_DECREF(f); |
---|
4177 | n/a | _PyObject_GC_TRACK(f); |
---|
4178 | n/a | } |
---|
4179 | n/a | else { |
---|
4180 | n/a | ++tstate->recursion_depth; |
---|
4181 | n/a | Py_DECREF(f); |
---|
4182 | n/a | --tstate->recursion_depth; |
---|
4183 | n/a | } |
---|
4184 | n/a | return retval; |
---|
4185 | n/a | } |
---|
4186 | n/a | |
---|
4187 | n/a | PyObject * |
---|
4188 | n/a | PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals, |
---|
4189 | n/a | PyObject **args, int argcount, PyObject **kws, int kwcount, |
---|
4190 | n/a | PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure) |
---|
4191 | n/a | { |
---|
4192 | n/a | return _PyEval_EvalCodeWithName(_co, globals, locals, |
---|
4193 | n/a | args, argcount, |
---|
4194 | n/a | kws, kws + 1, kwcount, 2, |
---|
4195 | n/a | defs, defcount, |
---|
4196 | n/a | kwdefs, closure, |
---|
4197 | n/a | NULL, NULL); |
---|
4198 | n/a | } |
---|
4199 | n/a | |
---|
4200 | n/a | static PyObject * |
---|
4201 | n/a | special_lookup(PyObject *o, _Py_Identifier *id) |
---|
4202 | n/a | { |
---|
4203 | n/a | PyObject *res; |
---|
4204 | n/a | res = _PyObject_LookupSpecial(o, id); |
---|
4205 | n/a | if (res == NULL && !PyErr_Occurred()) { |
---|
4206 | n/a | PyErr_SetObject(PyExc_AttributeError, id->object); |
---|
4207 | n/a | return NULL; |
---|
4208 | n/a | } |
---|
4209 | n/a | return res; |
---|
4210 | n/a | } |
---|
4211 | n/a | |
---|
4212 | n/a | |
---|
4213 | n/a | /* These 3 functions deal with the exception state of generators. */ |
---|
4214 | n/a | |
---|
4215 | n/a | static void |
---|
4216 | n/a | save_exc_state(PyThreadState *tstate, PyFrameObject *f) |
---|
4217 | n/a | { |
---|
4218 | n/a | PyObject *type, *value, *traceback; |
---|
4219 | n/a | Py_XINCREF(tstate->exc_type); |
---|
4220 | n/a | Py_XINCREF(tstate->exc_value); |
---|
4221 | n/a | Py_XINCREF(tstate->exc_traceback); |
---|
4222 | n/a | type = f->f_exc_type; |
---|
4223 | n/a | value = f->f_exc_value; |
---|
4224 | n/a | traceback = f->f_exc_traceback; |
---|
4225 | n/a | f->f_exc_type = tstate->exc_type; |
---|
4226 | n/a | f->f_exc_value = tstate->exc_value; |
---|
4227 | n/a | f->f_exc_traceback = tstate->exc_traceback; |
---|
4228 | n/a | Py_XDECREF(type); |
---|
4229 | n/a | Py_XDECREF(value); |
---|
4230 | n/a | Py_XDECREF(traceback); |
---|
4231 | n/a | } |
---|
4232 | n/a | |
---|
4233 | n/a | static void |
---|
4234 | n/a | swap_exc_state(PyThreadState *tstate, PyFrameObject *f) |
---|
4235 | n/a | { |
---|
4236 | n/a | PyObject *tmp; |
---|
4237 | n/a | tmp = tstate->exc_type; |
---|
4238 | n/a | tstate->exc_type = f->f_exc_type; |
---|
4239 | n/a | f->f_exc_type = tmp; |
---|
4240 | n/a | tmp = tstate->exc_value; |
---|
4241 | n/a | tstate->exc_value = f->f_exc_value; |
---|
4242 | n/a | f->f_exc_value = tmp; |
---|
4243 | n/a | tmp = tstate->exc_traceback; |
---|
4244 | n/a | tstate->exc_traceback = f->f_exc_traceback; |
---|
4245 | n/a | f->f_exc_traceback = tmp; |
---|
4246 | n/a | } |
---|
4247 | n/a | |
---|
4248 | n/a | static void |
---|
4249 | n/a | restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f) |
---|
4250 | n/a | { |
---|
4251 | n/a | PyObject *type, *value, *tb; |
---|
4252 | n/a | type = tstate->exc_type; |
---|
4253 | n/a | value = tstate->exc_value; |
---|
4254 | n/a | tb = tstate->exc_traceback; |
---|
4255 | n/a | tstate->exc_type = f->f_exc_type; |
---|
4256 | n/a | tstate->exc_value = f->f_exc_value; |
---|
4257 | n/a | tstate->exc_traceback = f->f_exc_traceback; |
---|
4258 | n/a | f->f_exc_type = NULL; |
---|
4259 | n/a | f->f_exc_value = NULL; |
---|
4260 | n/a | f->f_exc_traceback = NULL; |
---|
4261 | n/a | Py_XDECREF(type); |
---|
4262 | n/a | Py_XDECREF(value); |
---|
4263 | n/a | Py_XDECREF(tb); |
---|
4264 | n/a | } |
---|
4265 | n/a | |
---|
4266 | n/a | |
---|
4267 | n/a | /* Logic for the raise statement (too complicated for inlining). |
---|
4268 | n/a | This *consumes* a reference count to each of its arguments. */ |
---|
4269 | n/a | static int |
---|
4270 | n/a | do_raise(PyObject *exc, PyObject *cause) |
---|
4271 | n/a | { |
---|
4272 | n/a | PyObject *type = NULL, *value = NULL; |
---|
4273 | n/a | |
---|
4274 | n/a | if (exc == NULL) { |
---|
4275 | n/a | /* Reraise */ |
---|
4276 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4277 | n/a | PyObject *tb; |
---|
4278 | n/a | type = tstate->exc_type; |
---|
4279 | n/a | value = tstate->exc_value; |
---|
4280 | n/a | tb = tstate->exc_traceback; |
---|
4281 | n/a | if (type == Py_None || type == NULL) { |
---|
4282 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4283 | n/a | "No active exception to reraise"); |
---|
4284 | n/a | return 0; |
---|
4285 | n/a | } |
---|
4286 | n/a | Py_XINCREF(type); |
---|
4287 | n/a | Py_XINCREF(value); |
---|
4288 | n/a | Py_XINCREF(tb); |
---|
4289 | n/a | PyErr_Restore(type, value, tb); |
---|
4290 | n/a | return 1; |
---|
4291 | n/a | } |
---|
4292 | n/a | |
---|
4293 | n/a | /* We support the following forms of raise: |
---|
4294 | n/a | raise |
---|
4295 | n/a | raise <instance> |
---|
4296 | n/a | raise <type> */ |
---|
4297 | n/a | |
---|
4298 | n/a | if (PyExceptionClass_Check(exc)) { |
---|
4299 | n/a | type = exc; |
---|
4300 | n/a | value = _PyObject_CallNoArg(exc); |
---|
4301 | n/a | if (value == NULL) |
---|
4302 | n/a | goto raise_error; |
---|
4303 | n/a | if (!PyExceptionInstance_Check(value)) { |
---|
4304 | n/a | PyErr_Format(PyExc_TypeError, |
---|
4305 | n/a | "calling %R should have returned an instance of " |
---|
4306 | n/a | "BaseException, not %R", |
---|
4307 | n/a | type, Py_TYPE(value)); |
---|
4308 | n/a | goto raise_error; |
---|
4309 | n/a | } |
---|
4310 | n/a | } |
---|
4311 | n/a | else if (PyExceptionInstance_Check(exc)) { |
---|
4312 | n/a | value = exc; |
---|
4313 | n/a | type = PyExceptionInstance_Class(exc); |
---|
4314 | n/a | Py_INCREF(type); |
---|
4315 | n/a | } |
---|
4316 | n/a | else { |
---|
4317 | n/a | /* Not something you can raise. You get an exception |
---|
4318 | n/a | anyway, just not what you specified :-) */ |
---|
4319 | n/a | Py_DECREF(exc); |
---|
4320 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4321 | n/a | "exceptions must derive from BaseException"); |
---|
4322 | n/a | goto raise_error; |
---|
4323 | n/a | } |
---|
4324 | n/a | |
---|
4325 | n/a | assert(type != NULL); |
---|
4326 | n/a | assert(value != NULL); |
---|
4327 | n/a | |
---|
4328 | n/a | if (cause) { |
---|
4329 | n/a | PyObject *fixed_cause; |
---|
4330 | n/a | if (PyExceptionClass_Check(cause)) { |
---|
4331 | n/a | fixed_cause = _PyObject_CallNoArg(cause); |
---|
4332 | n/a | if (fixed_cause == NULL) |
---|
4333 | n/a | goto raise_error; |
---|
4334 | n/a | Py_DECREF(cause); |
---|
4335 | n/a | } |
---|
4336 | n/a | else if (PyExceptionInstance_Check(cause)) { |
---|
4337 | n/a | fixed_cause = cause; |
---|
4338 | n/a | } |
---|
4339 | n/a | else if (cause == Py_None) { |
---|
4340 | n/a | Py_DECREF(cause); |
---|
4341 | n/a | fixed_cause = NULL; |
---|
4342 | n/a | } |
---|
4343 | n/a | else { |
---|
4344 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4345 | n/a | "exception causes must derive from " |
---|
4346 | n/a | "BaseException"); |
---|
4347 | n/a | goto raise_error; |
---|
4348 | n/a | } |
---|
4349 | n/a | PyException_SetCause(value, fixed_cause); |
---|
4350 | n/a | } |
---|
4351 | n/a | |
---|
4352 | n/a | PyErr_SetObject(type, value); |
---|
4353 | n/a | /* PyErr_SetObject incref's its arguments */ |
---|
4354 | n/a | Py_DECREF(value); |
---|
4355 | n/a | Py_DECREF(type); |
---|
4356 | n/a | return 0; |
---|
4357 | n/a | |
---|
4358 | n/a | raise_error: |
---|
4359 | n/a | Py_XDECREF(value); |
---|
4360 | n/a | Py_XDECREF(type); |
---|
4361 | n/a | Py_XDECREF(cause); |
---|
4362 | n/a | return 0; |
---|
4363 | n/a | } |
---|
4364 | n/a | |
---|
4365 | n/a | /* Iterate v argcnt times and store the results on the stack (via decreasing |
---|
4366 | n/a | sp). Return 1 for success, 0 if error. |
---|
4367 | n/a | |
---|
4368 | n/a | If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack |
---|
4369 | n/a | with a variable target. |
---|
4370 | n/a | */ |
---|
4371 | n/a | |
---|
4372 | n/a | static int |
---|
4373 | n/a | unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) |
---|
4374 | n/a | { |
---|
4375 | n/a | int i = 0, j = 0; |
---|
4376 | n/a | Py_ssize_t ll = 0; |
---|
4377 | n/a | PyObject *it; /* iter(v) */ |
---|
4378 | n/a | PyObject *w; |
---|
4379 | n/a | PyObject *l = NULL; /* variable list */ |
---|
4380 | n/a | |
---|
4381 | n/a | assert(v != NULL); |
---|
4382 | n/a | |
---|
4383 | n/a | it = PyObject_GetIter(v); |
---|
4384 | n/a | if (it == NULL) |
---|
4385 | n/a | goto Error; |
---|
4386 | n/a | |
---|
4387 | n/a | for (; i < argcnt; i++) { |
---|
4388 | n/a | w = PyIter_Next(it); |
---|
4389 | n/a | if (w == NULL) { |
---|
4390 | n/a | /* Iterator done, via error or exhaustion. */ |
---|
4391 | n/a | if (!PyErr_Occurred()) { |
---|
4392 | n/a | if (argcntafter == -1) { |
---|
4393 | n/a | PyErr_Format(PyExc_ValueError, |
---|
4394 | n/a | "not enough values to unpack (expected %d, got %d)", |
---|
4395 | n/a | argcnt, i); |
---|
4396 | n/a | } |
---|
4397 | n/a | else { |
---|
4398 | n/a | PyErr_Format(PyExc_ValueError, |
---|
4399 | n/a | "not enough values to unpack " |
---|
4400 | n/a | "(expected at least %d, got %d)", |
---|
4401 | n/a | argcnt + argcntafter, i); |
---|
4402 | n/a | } |
---|
4403 | n/a | } |
---|
4404 | n/a | goto Error; |
---|
4405 | n/a | } |
---|
4406 | n/a | *--sp = w; |
---|
4407 | n/a | } |
---|
4408 | n/a | |
---|
4409 | n/a | if (argcntafter == -1) { |
---|
4410 | n/a | /* We better have exhausted the iterator now. */ |
---|
4411 | n/a | w = PyIter_Next(it); |
---|
4412 | n/a | if (w == NULL) { |
---|
4413 | n/a | if (PyErr_Occurred()) |
---|
4414 | n/a | goto Error; |
---|
4415 | n/a | Py_DECREF(it); |
---|
4416 | n/a | return 1; |
---|
4417 | n/a | } |
---|
4418 | n/a | Py_DECREF(w); |
---|
4419 | n/a | PyErr_Format(PyExc_ValueError, |
---|
4420 | n/a | "too many values to unpack (expected %d)", |
---|
4421 | n/a | argcnt); |
---|
4422 | n/a | goto Error; |
---|
4423 | n/a | } |
---|
4424 | n/a | |
---|
4425 | n/a | l = PySequence_List(it); |
---|
4426 | n/a | if (l == NULL) |
---|
4427 | n/a | goto Error; |
---|
4428 | n/a | *--sp = l; |
---|
4429 | n/a | i++; |
---|
4430 | n/a | |
---|
4431 | n/a | ll = PyList_GET_SIZE(l); |
---|
4432 | n/a | if (ll < argcntafter) { |
---|
4433 | n/a | PyErr_Format(PyExc_ValueError, |
---|
4434 | n/a | "not enough values to unpack (expected at least %d, got %zd)", |
---|
4435 | n/a | argcnt + argcntafter, argcnt + ll); |
---|
4436 | n/a | goto Error; |
---|
4437 | n/a | } |
---|
4438 | n/a | |
---|
4439 | n/a | /* Pop the "after-variable" args off the list. */ |
---|
4440 | n/a | for (j = argcntafter; j > 0; j--, i++) { |
---|
4441 | n/a | *--sp = PyList_GET_ITEM(l, ll - j); |
---|
4442 | n/a | } |
---|
4443 | n/a | /* Resize the list. */ |
---|
4444 | n/a | Py_SIZE(l) = ll - argcntafter; |
---|
4445 | n/a | Py_DECREF(it); |
---|
4446 | n/a | return 1; |
---|
4447 | n/a | |
---|
4448 | n/a | Error: |
---|
4449 | n/a | for (; i > 0; i--, sp++) |
---|
4450 | n/a | Py_DECREF(*sp); |
---|
4451 | n/a | Py_XDECREF(it); |
---|
4452 | n/a | return 0; |
---|
4453 | n/a | } |
---|
4454 | n/a | |
---|
4455 | n/a | |
---|
4456 | n/a | #ifdef LLTRACE |
---|
4457 | n/a | static int |
---|
4458 | n/a | prtrace(PyObject *v, const char *str) |
---|
4459 | n/a | { |
---|
4460 | n/a | printf("%s ", str); |
---|
4461 | n/a | if (PyObject_Print(v, stdout, 0) != 0) |
---|
4462 | n/a | PyErr_Clear(); /* Don't know what else to do */ |
---|
4463 | n/a | printf("\n"); |
---|
4464 | n/a | return 1; |
---|
4465 | n/a | } |
---|
4466 | n/a | #endif |
---|
4467 | n/a | |
---|
4468 | n/a | static void |
---|
4469 | n/a | call_exc_trace(Py_tracefunc func, PyObject *self, |
---|
4470 | n/a | PyThreadState *tstate, PyFrameObject *f) |
---|
4471 | n/a | { |
---|
4472 | n/a | PyObject *type, *value, *traceback, *orig_traceback, *arg; |
---|
4473 | n/a | int err; |
---|
4474 | n/a | PyErr_Fetch(&type, &value, &orig_traceback); |
---|
4475 | n/a | if (value == NULL) { |
---|
4476 | n/a | value = Py_None; |
---|
4477 | n/a | Py_INCREF(value); |
---|
4478 | n/a | } |
---|
4479 | n/a | PyErr_NormalizeException(&type, &value, &orig_traceback); |
---|
4480 | n/a | traceback = (orig_traceback != NULL) ? orig_traceback : Py_None; |
---|
4481 | n/a | arg = PyTuple_Pack(3, type, value, traceback); |
---|
4482 | n/a | if (arg == NULL) { |
---|
4483 | n/a | PyErr_Restore(type, value, orig_traceback); |
---|
4484 | n/a | return; |
---|
4485 | n/a | } |
---|
4486 | n/a | err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg); |
---|
4487 | n/a | Py_DECREF(arg); |
---|
4488 | n/a | if (err == 0) |
---|
4489 | n/a | PyErr_Restore(type, value, orig_traceback); |
---|
4490 | n/a | else { |
---|
4491 | n/a | Py_XDECREF(type); |
---|
4492 | n/a | Py_XDECREF(value); |
---|
4493 | n/a | Py_XDECREF(orig_traceback); |
---|
4494 | n/a | } |
---|
4495 | n/a | } |
---|
4496 | n/a | |
---|
4497 | n/a | static int |
---|
4498 | n/a | call_trace_protected(Py_tracefunc func, PyObject *obj, |
---|
4499 | n/a | PyThreadState *tstate, PyFrameObject *frame, |
---|
4500 | n/a | int what, PyObject *arg) |
---|
4501 | n/a | { |
---|
4502 | n/a | PyObject *type, *value, *traceback; |
---|
4503 | n/a | int err; |
---|
4504 | n/a | PyErr_Fetch(&type, &value, &traceback); |
---|
4505 | n/a | err = call_trace(func, obj, tstate, frame, what, arg); |
---|
4506 | n/a | if (err == 0) |
---|
4507 | n/a | { |
---|
4508 | n/a | PyErr_Restore(type, value, traceback); |
---|
4509 | n/a | return 0; |
---|
4510 | n/a | } |
---|
4511 | n/a | else { |
---|
4512 | n/a | Py_XDECREF(type); |
---|
4513 | n/a | Py_XDECREF(value); |
---|
4514 | n/a | Py_XDECREF(traceback); |
---|
4515 | n/a | return -1; |
---|
4516 | n/a | } |
---|
4517 | n/a | } |
---|
4518 | n/a | |
---|
4519 | n/a | static int |
---|
4520 | n/a | call_trace(Py_tracefunc func, PyObject *obj, |
---|
4521 | n/a | PyThreadState *tstate, PyFrameObject *frame, |
---|
4522 | n/a | int what, PyObject *arg) |
---|
4523 | n/a | { |
---|
4524 | n/a | int result; |
---|
4525 | n/a | if (tstate->tracing) |
---|
4526 | n/a | return 0; |
---|
4527 | n/a | tstate->tracing++; |
---|
4528 | n/a | tstate->use_tracing = 0; |
---|
4529 | n/a | result = func(obj, frame, what, arg); |
---|
4530 | n/a | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
---|
4531 | n/a | || (tstate->c_profilefunc != NULL)); |
---|
4532 | n/a | tstate->tracing--; |
---|
4533 | n/a | return result; |
---|
4534 | n/a | } |
---|
4535 | n/a | |
---|
4536 | n/a | PyObject * |
---|
4537 | n/a | _PyEval_CallTracing(PyObject *func, PyObject *args) |
---|
4538 | n/a | { |
---|
4539 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4540 | n/a | int save_tracing = tstate->tracing; |
---|
4541 | n/a | int save_use_tracing = tstate->use_tracing; |
---|
4542 | n/a | PyObject *result; |
---|
4543 | n/a | |
---|
4544 | n/a | tstate->tracing = 0; |
---|
4545 | n/a | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
---|
4546 | n/a | || (tstate->c_profilefunc != NULL)); |
---|
4547 | n/a | result = PyObject_Call(func, args, NULL); |
---|
4548 | n/a | tstate->tracing = save_tracing; |
---|
4549 | n/a | tstate->use_tracing = save_use_tracing; |
---|
4550 | n/a | return result; |
---|
4551 | n/a | } |
---|
4552 | n/a | |
---|
4553 | n/a | /* See Objects/lnotab_notes.txt for a description of how tracing works. */ |
---|
4554 | n/a | static int |
---|
4555 | n/a | maybe_call_line_trace(Py_tracefunc func, PyObject *obj, |
---|
4556 | n/a | PyThreadState *tstate, PyFrameObject *frame, |
---|
4557 | n/a | int *instr_lb, int *instr_ub, int *instr_prev) |
---|
4558 | n/a | { |
---|
4559 | n/a | int result = 0; |
---|
4560 | n/a | int line = frame->f_lineno; |
---|
4561 | n/a | |
---|
4562 | n/a | /* If the last instruction executed isn't in the current |
---|
4563 | n/a | instruction window, reset the window. |
---|
4564 | n/a | */ |
---|
4565 | n/a | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
---|
4566 | n/a | PyAddrPair bounds; |
---|
4567 | n/a | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
---|
4568 | n/a | &bounds); |
---|
4569 | n/a | *instr_lb = bounds.ap_lower; |
---|
4570 | n/a | *instr_ub = bounds.ap_upper; |
---|
4571 | n/a | } |
---|
4572 | n/a | /* If the last instruction falls at the start of a line or if |
---|
4573 | n/a | it represents a jump backwards, update the frame's line |
---|
4574 | n/a | number and call the trace function. */ |
---|
4575 | n/a | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
---|
4576 | n/a | frame->f_lineno = line; |
---|
4577 | n/a | result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None); |
---|
4578 | n/a | } |
---|
4579 | n/a | *instr_prev = frame->f_lasti; |
---|
4580 | n/a | return result; |
---|
4581 | n/a | } |
---|
4582 | n/a | |
---|
4583 | n/a | void |
---|
4584 | n/a | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
---|
4585 | n/a | { |
---|
4586 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4587 | n/a | PyObject *temp = tstate->c_profileobj; |
---|
4588 | n/a | Py_XINCREF(arg); |
---|
4589 | n/a | tstate->c_profilefunc = NULL; |
---|
4590 | n/a | tstate->c_profileobj = NULL; |
---|
4591 | n/a | /* Must make sure that tracing is not ignored if 'temp' is freed */ |
---|
4592 | n/a | tstate->use_tracing = tstate->c_tracefunc != NULL; |
---|
4593 | n/a | Py_XDECREF(temp); |
---|
4594 | n/a | tstate->c_profilefunc = func; |
---|
4595 | n/a | tstate->c_profileobj = arg; |
---|
4596 | n/a | /* Flag that tracing or profiling is turned on */ |
---|
4597 | n/a | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
---|
4598 | n/a | } |
---|
4599 | n/a | |
---|
4600 | n/a | void |
---|
4601 | n/a | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
---|
4602 | n/a | { |
---|
4603 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4604 | n/a | PyObject *temp = tstate->c_traceobj; |
---|
4605 | n/a | _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL); |
---|
4606 | n/a | Py_XINCREF(arg); |
---|
4607 | n/a | tstate->c_tracefunc = NULL; |
---|
4608 | n/a | tstate->c_traceobj = NULL; |
---|
4609 | n/a | /* Must make sure that profiling is not ignored if 'temp' is freed */ |
---|
4610 | n/a | tstate->use_tracing = tstate->c_profilefunc != NULL; |
---|
4611 | n/a | Py_XDECREF(temp); |
---|
4612 | n/a | tstate->c_tracefunc = func; |
---|
4613 | n/a | tstate->c_traceobj = arg; |
---|
4614 | n/a | /* Flag that tracing or profiling is turned on */ |
---|
4615 | n/a | tstate->use_tracing = ((func != NULL) |
---|
4616 | n/a | || (tstate->c_profilefunc != NULL)); |
---|
4617 | n/a | } |
---|
4618 | n/a | |
---|
4619 | n/a | void |
---|
4620 | n/a | _PyEval_SetCoroutineWrapper(PyObject *wrapper) |
---|
4621 | n/a | { |
---|
4622 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4623 | n/a | |
---|
4624 | n/a | Py_XINCREF(wrapper); |
---|
4625 | n/a | Py_XSETREF(tstate->coroutine_wrapper, wrapper); |
---|
4626 | n/a | } |
---|
4627 | n/a | |
---|
4628 | n/a | PyObject * |
---|
4629 | n/a | _PyEval_GetCoroutineWrapper(void) |
---|
4630 | n/a | { |
---|
4631 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4632 | n/a | return tstate->coroutine_wrapper; |
---|
4633 | n/a | } |
---|
4634 | n/a | |
---|
4635 | n/a | void |
---|
4636 | n/a | _PyEval_SetAsyncGenFirstiter(PyObject *firstiter) |
---|
4637 | n/a | { |
---|
4638 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4639 | n/a | |
---|
4640 | n/a | Py_XINCREF(firstiter); |
---|
4641 | n/a | Py_XSETREF(tstate->async_gen_firstiter, firstiter); |
---|
4642 | n/a | } |
---|
4643 | n/a | |
---|
4644 | n/a | PyObject * |
---|
4645 | n/a | _PyEval_GetAsyncGenFirstiter(void) |
---|
4646 | n/a | { |
---|
4647 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4648 | n/a | return tstate->async_gen_firstiter; |
---|
4649 | n/a | } |
---|
4650 | n/a | |
---|
4651 | n/a | void |
---|
4652 | n/a | _PyEval_SetAsyncGenFinalizer(PyObject *finalizer) |
---|
4653 | n/a | { |
---|
4654 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4655 | n/a | |
---|
4656 | n/a | Py_XINCREF(finalizer); |
---|
4657 | n/a | Py_XSETREF(tstate->async_gen_finalizer, finalizer); |
---|
4658 | n/a | } |
---|
4659 | n/a | |
---|
4660 | n/a | PyObject * |
---|
4661 | n/a | _PyEval_GetAsyncGenFinalizer(void) |
---|
4662 | n/a | { |
---|
4663 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4664 | n/a | return tstate->async_gen_finalizer; |
---|
4665 | n/a | } |
---|
4666 | n/a | |
---|
4667 | n/a | PyObject * |
---|
4668 | n/a | PyEval_GetBuiltins(void) |
---|
4669 | n/a | { |
---|
4670 | n/a | PyFrameObject *current_frame = PyEval_GetFrame(); |
---|
4671 | n/a | if (current_frame == NULL) |
---|
4672 | n/a | return PyThreadState_GET()->interp->builtins; |
---|
4673 | n/a | else |
---|
4674 | n/a | return current_frame->f_builtins; |
---|
4675 | n/a | } |
---|
4676 | n/a | |
---|
4677 | n/a | PyObject * |
---|
4678 | n/a | PyEval_GetLocals(void) |
---|
4679 | n/a | { |
---|
4680 | n/a | PyFrameObject *current_frame = PyEval_GetFrame(); |
---|
4681 | n/a | if (current_frame == NULL) { |
---|
4682 | n/a | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
---|
4683 | n/a | return NULL; |
---|
4684 | n/a | } |
---|
4685 | n/a | |
---|
4686 | n/a | if (PyFrame_FastToLocalsWithError(current_frame) < 0) |
---|
4687 | n/a | return NULL; |
---|
4688 | n/a | |
---|
4689 | n/a | assert(current_frame->f_locals != NULL); |
---|
4690 | n/a | return current_frame->f_locals; |
---|
4691 | n/a | } |
---|
4692 | n/a | |
---|
4693 | n/a | PyObject * |
---|
4694 | n/a | PyEval_GetGlobals(void) |
---|
4695 | n/a | { |
---|
4696 | n/a | PyFrameObject *current_frame = PyEval_GetFrame(); |
---|
4697 | n/a | if (current_frame == NULL) |
---|
4698 | n/a | return NULL; |
---|
4699 | n/a | |
---|
4700 | n/a | assert(current_frame->f_globals != NULL); |
---|
4701 | n/a | return current_frame->f_globals; |
---|
4702 | n/a | } |
---|
4703 | n/a | |
---|
4704 | n/a | PyFrameObject * |
---|
4705 | n/a | PyEval_GetFrame(void) |
---|
4706 | n/a | { |
---|
4707 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4708 | n/a | return _PyThreadState_GetFrame(tstate); |
---|
4709 | n/a | } |
---|
4710 | n/a | |
---|
4711 | n/a | int |
---|
4712 | n/a | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
---|
4713 | n/a | { |
---|
4714 | n/a | PyFrameObject *current_frame = PyEval_GetFrame(); |
---|
4715 | n/a | int result = cf->cf_flags != 0; |
---|
4716 | n/a | |
---|
4717 | n/a | if (current_frame != NULL) { |
---|
4718 | n/a | const int codeflags = current_frame->f_code->co_flags; |
---|
4719 | n/a | const int compilerflags = codeflags & PyCF_MASK; |
---|
4720 | n/a | if (compilerflags) { |
---|
4721 | n/a | result = 1; |
---|
4722 | n/a | cf->cf_flags |= compilerflags; |
---|
4723 | n/a | } |
---|
4724 | n/a | #if 0 /* future keyword */ |
---|
4725 | n/a | if (codeflags & CO_GENERATOR_ALLOWED) { |
---|
4726 | n/a | result = 1; |
---|
4727 | n/a | cf->cf_flags |= CO_GENERATOR_ALLOWED; |
---|
4728 | n/a | } |
---|
4729 | n/a | #endif |
---|
4730 | n/a | } |
---|
4731 | n/a | return result; |
---|
4732 | n/a | } |
---|
4733 | n/a | |
---|
4734 | n/a | |
---|
4735 | n/a | /* External interface to call any callable object. |
---|
4736 | n/a | The arg must be a tuple or NULL. The kw must be a dict or NULL. */ |
---|
4737 | n/a | |
---|
4738 | n/a | PyObject * |
---|
4739 | n/a | PyEval_CallObjectWithKeywords(PyObject *callable, |
---|
4740 | n/a | PyObject *args, PyObject *kwargs) |
---|
4741 | n/a | { |
---|
4742 | n/a | #ifdef Py_DEBUG |
---|
4743 | n/a | /* PyEval_CallObjectWithKeywords() must not be called with an exception |
---|
4744 | n/a | set. It raises a new exception if parameters are invalid or if |
---|
4745 | n/a | PyTuple_New() fails, and so the original exception is lost. */ |
---|
4746 | n/a | assert(!PyErr_Occurred()); |
---|
4747 | n/a | #endif |
---|
4748 | n/a | |
---|
4749 | n/a | if (args == NULL) { |
---|
4750 | n/a | return _PyObject_FastCallDict(callable, NULL, 0, kwargs); |
---|
4751 | n/a | } |
---|
4752 | n/a | |
---|
4753 | n/a | if (!PyTuple_Check(args)) { |
---|
4754 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4755 | n/a | "argument list must be a tuple"); |
---|
4756 | n/a | return NULL; |
---|
4757 | n/a | } |
---|
4758 | n/a | |
---|
4759 | n/a | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
---|
4760 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4761 | n/a | "keyword list must be a dictionary"); |
---|
4762 | n/a | return NULL; |
---|
4763 | n/a | } |
---|
4764 | n/a | |
---|
4765 | n/a | return PyObject_Call(callable, args, kwargs); |
---|
4766 | n/a | } |
---|
4767 | n/a | |
---|
4768 | n/a | const char * |
---|
4769 | n/a | PyEval_GetFuncName(PyObject *func) |
---|
4770 | n/a | { |
---|
4771 | n/a | if (PyMethod_Check(func)) |
---|
4772 | n/a | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
---|
4773 | n/a | else if (PyFunction_Check(func)) |
---|
4774 | n/a | return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name); |
---|
4775 | n/a | else if (PyCFunction_Check(func)) |
---|
4776 | n/a | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
---|
4777 | n/a | else |
---|
4778 | n/a | return func->ob_type->tp_name; |
---|
4779 | n/a | } |
---|
4780 | n/a | |
---|
4781 | n/a | const char * |
---|
4782 | n/a | PyEval_GetFuncDesc(PyObject *func) |
---|
4783 | n/a | { |
---|
4784 | n/a | if (PyMethod_Check(func)) |
---|
4785 | n/a | return "()"; |
---|
4786 | n/a | else if (PyFunction_Check(func)) |
---|
4787 | n/a | return "()"; |
---|
4788 | n/a | else if (PyCFunction_Check(func)) |
---|
4789 | n/a | return "()"; |
---|
4790 | n/a | else |
---|
4791 | n/a | return " object"; |
---|
4792 | n/a | } |
---|
4793 | n/a | |
---|
4794 | n/a | #define C_TRACE(x, call) \ |
---|
4795 | n/a | if (tstate->use_tracing && tstate->c_profilefunc) { \ |
---|
4796 | n/a | if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ |
---|
4797 | n/a | tstate, tstate->frame, \ |
---|
4798 | n/a | PyTrace_C_CALL, func)) { \ |
---|
4799 | n/a | x = NULL; \ |
---|
4800 | n/a | } \ |
---|
4801 | n/a | else { \ |
---|
4802 | n/a | x = call; \ |
---|
4803 | n/a | if (tstate->c_profilefunc != NULL) { \ |
---|
4804 | n/a | if (x == NULL) { \ |
---|
4805 | n/a | call_trace_protected(tstate->c_profilefunc, \ |
---|
4806 | n/a | tstate->c_profileobj, \ |
---|
4807 | n/a | tstate, tstate->frame, \ |
---|
4808 | n/a | PyTrace_C_EXCEPTION, func); \ |
---|
4809 | n/a | /* XXX should pass (type, value, tb) */ \ |
---|
4810 | n/a | } else { \ |
---|
4811 | n/a | if (call_trace(tstate->c_profilefunc, \ |
---|
4812 | n/a | tstate->c_profileobj, \ |
---|
4813 | n/a | tstate, tstate->frame, \ |
---|
4814 | n/a | PyTrace_C_RETURN, func)) { \ |
---|
4815 | n/a | Py_DECREF(x); \ |
---|
4816 | n/a | x = NULL; \ |
---|
4817 | n/a | } \ |
---|
4818 | n/a | } \ |
---|
4819 | n/a | } \ |
---|
4820 | n/a | } \ |
---|
4821 | n/a | } else { \ |
---|
4822 | n/a | x = call; \ |
---|
4823 | n/a | } |
---|
4824 | n/a | |
---|
4825 | n/a | /* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault() |
---|
4826 | n/a | to reduce the stack consumption. */ |
---|
4827 | n/a | Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION |
---|
4828 | n/a | call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames) |
---|
4829 | n/a | { |
---|
4830 | n/a | PyObject **pfunc = (*pp_stack) - oparg - 1; |
---|
4831 | n/a | PyObject *func = *pfunc; |
---|
4832 | n/a | PyObject *x, *w; |
---|
4833 | n/a | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
---|
4834 | n/a | Py_ssize_t nargs = oparg - nkwargs; |
---|
4835 | n/a | PyObject **stack = (*pp_stack) - nargs - nkwargs; |
---|
4836 | n/a | |
---|
4837 | n/a | /* Always dispatch PyCFunction first, because these are |
---|
4838 | n/a | presumed to be the most frequent callable object. |
---|
4839 | n/a | */ |
---|
4840 | n/a | if (PyCFunction_Check(func)) { |
---|
4841 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4842 | n/a | C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames)); |
---|
4843 | n/a | } |
---|
4844 | n/a | else if (Py_TYPE(func) == &PyMethodDescr_Type) { |
---|
4845 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4846 | n/a | C_TRACE(x, _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames)); |
---|
4847 | n/a | } |
---|
4848 | n/a | else { |
---|
4849 | n/a | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) { |
---|
4850 | n/a | /* Optimize access to bound methods. Reuse the Python stack |
---|
4851 | n/a | to pass 'self' as the first argument, replace 'func' |
---|
4852 | n/a | with 'self'. It avoids the creation of a new temporary tuple |
---|
4853 | n/a | for arguments (to replace func with self) when the method uses |
---|
4854 | n/a | FASTCALL. */ |
---|
4855 | n/a | PyObject *self = PyMethod_GET_SELF(func); |
---|
4856 | n/a | Py_INCREF(self); |
---|
4857 | n/a | func = PyMethod_GET_FUNCTION(func); |
---|
4858 | n/a | Py_INCREF(func); |
---|
4859 | n/a | Py_SETREF(*pfunc, self); |
---|
4860 | n/a | nargs++; |
---|
4861 | n/a | stack--; |
---|
4862 | n/a | } |
---|
4863 | n/a | else { |
---|
4864 | n/a | Py_INCREF(func); |
---|
4865 | n/a | } |
---|
4866 | n/a | |
---|
4867 | n/a | if (PyFunction_Check(func)) { |
---|
4868 | n/a | x = fast_function(func, stack, nargs, kwnames); |
---|
4869 | n/a | } |
---|
4870 | n/a | else { |
---|
4871 | n/a | x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
---|
4872 | n/a | } |
---|
4873 | n/a | Py_DECREF(func); |
---|
4874 | n/a | } |
---|
4875 | n/a | |
---|
4876 | n/a | assert((x != NULL) ^ (PyErr_Occurred() != NULL)); |
---|
4877 | n/a | |
---|
4878 | n/a | /* Clear the stack of the function object. Also removes |
---|
4879 | n/a | the arguments in case they weren't consumed already |
---|
4880 | n/a | (fast_function() and err_args() leave them on the stack). |
---|
4881 | n/a | */ |
---|
4882 | n/a | while ((*pp_stack) > pfunc) { |
---|
4883 | n/a | w = EXT_POP(*pp_stack); |
---|
4884 | n/a | Py_DECREF(w); |
---|
4885 | n/a | } |
---|
4886 | n/a | |
---|
4887 | n/a | return x; |
---|
4888 | n/a | } |
---|
4889 | n/a | |
---|
4890 | n/a | /* The fast_function() function optimize calls for which no argument |
---|
4891 | n/a | tuple is necessary; the objects are passed directly from the stack. |
---|
4892 | n/a | For the simplest case -- a function that takes only positional |
---|
4893 | n/a | arguments and is called with only positional arguments -- it |
---|
4894 | n/a | inlines the most primitive frame setup code from |
---|
4895 | n/a | PyEval_EvalCodeEx(), which vastly reduces the checks that must be |
---|
4896 | n/a | done before evaluating the frame. |
---|
4897 | n/a | */ |
---|
4898 | n/a | |
---|
4899 | n/a | static PyObject* _Py_HOT_FUNCTION |
---|
4900 | n/a | _PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs, |
---|
4901 | n/a | PyObject *globals) |
---|
4902 | n/a | { |
---|
4903 | n/a | PyFrameObject *f; |
---|
4904 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
4905 | n/a | PyObject **fastlocals; |
---|
4906 | n/a | Py_ssize_t i; |
---|
4907 | n/a | PyObject *result; |
---|
4908 | n/a | |
---|
4909 | n/a | assert(globals != NULL); |
---|
4910 | n/a | /* XXX Perhaps we should create a specialized |
---|
4911 | n/a | _PyFrame_New_NoTrack() that doesn't take locals, but does |
---|
4912 | n/a | take builtins without sanity checking them. |
---|
4913 | n/a | */ |
---|
4914 | n/a | assert(tstate != NULL); |
---|
4915 | n/a | f = _PyFrame_New_NoTrack(tstate, co, globals, NULL); |
---|
4916 | n/a | if (f == NULL) { |
---|
4917 | n/a | return NULL; |
---|
4918 | n/a | } |
---|
4919 | n/a | |
---|
4920 | n/a | fastlocals = f->f_localsplus; |
---|
4921 | n/a | |
---|
4922 | n/a | for (i = 0; i < nargs; i++) { |
---|
4923 | n/a | Py_INCREF(*args); |
---|
4924 | n/a | fastlocals[i] = *args++; |
---|
4925 | n/a | } |
---|
4926 | n/a | result = PyEval_EvalFrameEx(f,0); |
---|
4927 | n/a | |
---|
4928 | n/a | if (Py_REFCNT(f) > 1) { |
---|
4929 | n/a | Py_DECREF(f); |
---|
4930 | n/a | _PyObject_GC_TRACK(f); |
---|
4931 | n/a | } |
---|
4932 | n/a | else { |
---|
4933 | n/a | ++tstate->recursion_depth; |
---|
4934 | n/a | Py_DECREF(f); |
---|
4935 | n/a | --tstate->recursion_depth; |
---|
4936 | n/a | } |
---|
4937 | n/a | return result; |
---|
4938 | n/a | } |
---|
4939 | n/a | |
---|
4940 | n/a | static PyObject * |
---|
4941 | n/a | fast_function(PyObject *func, PyObject **stack, |
---|
4942 | n/a | Py_ssize_t nargs, PyObject *kwnames) |
---|
4943 | n/a | { |
---|
4944 | n/a | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
---|
4945 | n/a | PyObject *globals = PyFunction_GET_GLOBALS(func); |
---|
4946 | n/a | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
---|
4947 | n/a | PyObject *kwdefs, *closure, *name, *qualname; |
---|
4948 | n/a | PyObject **d; |
---|
4949 | n/a | Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); |
---|
4950 | n/a | Py_ssize_t nd; |
---|
4951 | n/a | |
---|
4952 | n/a | assert(PyFunction_Check(func)); |
---|
4953 | n/a | assert(nargs >= 0); |
---|
4954 | n/a | assert(kwnames == NULL || PyTuple_CheckExact(kwnames)); |
---|
4955 | n/a | assert((nargs == 0 && nkwargs == 0) || stack != NULL); |
---|
4956 | n/a | /* kwnames must only contains str strings, no subclass, and all keys must |
---|
4957 | n/a | be unique */ |
---|
4958 | n/a | |
---|
4959 | n/a | if (co->co_kwonlyargcount == 0 && nkwargs == 0 && |
---|
4960 | n/a | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
---|
4961 | n/a | { |
---|
4962 | n/a | if (argdefs == NULL && co->co_argcount == nargs) { |
---|
4963 | n/a | return _PyFunction_FastCall(co, stack, nargs, globals); |
---|
4964 | n/a | } |
---|
4965 | n/a | else if (nargs == 0 && argdefs != NULL |
---|
4966 | n/a | && co->co_argcount == Py_SIZE(argdefs)) { |
---|
4967 | n/a | /* function called with no arguments, but all parameters have |
---|
4968 | n/a | a default value: use default values as arguments .*/ |
---|
4969 | n/a | stack = &PyTuple_GET_ITEM(argdefs, 0); |
---|
4970 | n/a | return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals); |
---|
4971 | n/a | } |
---|
4972 | n/a | } |
---|
4973 | n/a | |
---|
4974 | n/a | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
---|
4975 | n/a | closure = PyFunction_GET_CLOSURE(func); |
---|
4976 | n/a | name = ((PyFunctionObject *)func) -> func_name; |
---|
4977 | n/a | qualname = ((PyFunctionObject *)func) -> func_qualname; |
---|
4978 | n/a | |
---|
4979 | n/a | if (argdefs != NULL) { |
---|
4980 | n/a | d = &PyTuple_GET_ITEM(argdefs, 0); |
---|
4981 | n/a | nd = Py_SIZE(argdefs); |
---|
4982 | n/a | } |
---|
4983 | n/a | else { |
---|
4984 | n/a | d = NULL; |
---|
4985 | n/a | nd = 0; |
---|
4986 | n/a | } |
---|
4987 | n/a | return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
---|
4988 | n/a | stack, nargs, |
---|
4989 | n/a | nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL, |
---|
4990 | n/a | stack + nargs, |
---|
4991 | n/a | nkwargs, 1, |
---|
4992 | n/a | d, (int)nd, kwdefs, |
---|
4993 | n/a | closure, name, qualname); |
---|
4994 | n/a | } |
---|
4995 | n/a | |
---|
4996 | n/a | PyObject * |
---|
4997 | n/a | _PyFunction_FastCallKeywords(PyObject *func, PyObject **stack, |
---|
4998 | n/a | Py_ssize_t nargs, PyObject *kwnames) |
---|
4999 | n/a | { |
---|
5000 | n/a | return fast_function(func, stack, nargs, kwnames); |
---|
5001 | n/a | } |
---|
5002 | n/a | |
---|
5003 | n/a | PyObject * |
---|
5004 | n/a | _PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, |
---|
5005 | n/a | PyObject *kwargs) |
---|
5006 | n/a | { |
---|
5007 | n/a | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
---|
5008 | n/a | PyObject *globals = PyFunction_GET_GLOBALS(func); |
---|
5009 | n/a | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
---|
5010 | n/a | PyObject *kwdefs, *closure, *name, *qualname; |
---|
5011 | n/a | PyObject *kwtuple, **k; |
---|
5012 | n/a | PyObject **d; |
---|
5013 | n/a | Py_ssize_t nd, nk; |
---|
5014 | n/a | PyObject *result; |
---|
5015 | n/a | |
---|
5016 | n/a | assert(func != NULL); |
---|
5017 | n/a | assert(nargs >= 0); |
---|
5018 | n/a | assert(nargs == 0 || args != NULL); |
---|
5019 | n/a | assert(kwargs == NULL || PyDict_Check(kwargs)); |
---|
5020 | n/a | |
---|
5021 | n/a | if (co->co_kwonlyargcount == 0 && |
---|
5022 | n/a | (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) && |
---|
5023 | n/a | co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) |
---|
5024 | n/a | { |
---|
5025 | n/a | /* Fast paths */ |
---|
5026 | n/a | if (argdefs == NULL && co->co_argcount == nargs) { |
---|
5027 | n/a | return _PyFunction_FastCall(co, args, nargs, globals); |
---|
5028 | n/a | } |
---|
5029 | n/a | else if (nargs == 0 && argdefs != NULL |
---|
5030 | n/a | && co->co_argcount == Py_SIZE(argdefs)) { |
---|
5031 | n/a | /* function called with no arguments, but all parameters have |
---|
5032 | n/a | a default value: use default values as arguments .*/ |
---|
5033 | n/a | args = &PyTuple_GET_ITEM(argdefs, 0); |
---|
5034 | n/a | return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals); |
---|
5035 | n/a | } |
---|
5036 | n/a | } |
---|
5037 | n/a | |
---|
5038 | n/a | nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0; |
---|
5039 | n/a | if (nk != 0) { |
---|
5040 | n/a | Py_ssize_t pos, i; |
---|
5041 | n/a | |
---|
5042 | n/a | /* Issue #29318: Caller and callee functions must not share the |
---|
5043 | n/a | dictionary: kwargs must be copied. */ |
---|
5044 | n/a | kwtuple = PyTuple_New(2 * nk); |
---|
5045 | n/a | if (kwtuple == NULL) { |
---|
5046 | n/a | return NULL; |
---|
5047 | n/a | } |
---|
5048 | n/a | |
---|
5049 | n/a | k = &PyTuple_GET_ITEM(kwtuple, 0); |
---|
5050 | n/a | pos = i = 0; |
---|
5051 | n/a | while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { |
---|
5052 | n/a | /* We must hold strong references because keyword arguments can be |
---|
5053 | n/a | indirectly modified while the function is called: |
---|
5054 | n/a | see issue #2016 and test_extcall */ |
---|
5055 | n/a | Py_INCREF(k[i]); |
---|
5056 | n/a | Py_INCREF(k[i+1]); |
---|
5057 | n/a | i += 2; |
---|
5058 | n/a | } |
---|
5059 | n/a | nk = i / 2; |
---|
5060 | n/a | } |
---|
5061 | n/a | else { |
---|
5062 | n/a | kwtuple = NULL; |
---|
5063 | n/a | k = NULL; |
---|
5064 | n/a | } |
---|
5065 | n/a | |
---|
5066 | n/a | kwdefs = PyFunction_GET_KW_DEFAULTS(func); |
---|
5067 | n/a | closure = PyFunction_GET_CLOSURE(func); |
---|
5068 | n/a | name = ((PyFunctionObject *)func) -> func_name; |
---|
5069 | n/a | qualname = ((PyFunctionObject *)func) -> func_qualname; |
---|
5070 | n/a | |
---|
5071 | n/a | if (argdefs != NULL) { |
---|
5072 | n/a | d = &PyTuple_GET_ITEM(argdefs, 0); |
---|
5073 | n/a | nd = Py_SIZE(argdefs); |
---|
5074 | n/a | } |
---|
5075 | n/a | else { |
---|
5076 | n/a | d = NULL; |
---|
5077 | n/a | nd = 0; |
---|
5078 | n/a | } |
---|
5079 | n/a | |
---|
5080 | n/a | result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL, |
---|
5081 | n/a | args, nargs, |
---|
5082 | n/a | k, k + 1, nk, 2, |
---|
5083 | n/a | d, nd, kwdefs, |
---|
5084 | n/a | closure, name, qualname); |
---|
5085 | n/a | Py_XDECREF(kwtuple); |
---|
5086 | n/a | return result; |
---|
5087 | n/a | } |
---|
5088 | n/a | |
---|
5089 | n/a | static PyObject * |
---|
5090 | n/a | do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict) |
---|
5091 | n/a | { |
---|
5092 | n/a | if (PyCFunction_Check(func)) { |
---|
5093 | n/a | PyObject *result; |
---|
5094 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
5095 | n/a | C_TRACE(result, PyCFunction_Call(func, callargs, kwdict)); |
---|
5096 | n/a | return result; |
---|
5097 | n/a | } |
---|
5098 | n/a | else { |
---|
5099 | n/a | return PyObject_Call(func, callargs, kwdict); |
---|
5100 | n/a | } |
---|
5101 | n/a | } |
---|
5102 | n/a | |
---|
5103 | n/a | /* Extract a slice index from a PyLong or an object with the |
---|
5104 | n/a | nb_index slot defined, and store in *pi. |
---|
5105 | n/a | Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX, |
---|
5106 | n/a | and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1. |
---|
5107 | n/a | Return 0 on error, 1 on success. |
---|
5108 | n/a | */ |
---|
5109 | n/a | /* Note: If v is NULL, return success without storing into *pi. This |
---|
5110 | n/a | is because_PyEval_SliceIndex() is called by apply_slice(), which can be |
---|
5111 | n/a | called by the SLICE opcode with v and/or w equal to NULL. |
---|
5112 | n/a | */ |
---|
5113 | n/a | int |
---|
5114 | n/a | _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi) |
---|
5115 | n/a | { |
---|
5116 | n/a | if (v != NULL) { |
---|
5117 | n/a | Py_ssize_t x; |
---|
5118 | n/a | if (PyIndex_Check(v)) { |
---|
5119 | n/a | x = PyNumber_AsSsize_t(v, NULL); |
---|
5120 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
5121 | n/a | return 0; |
---|
5122 | n/a | } |
---|
5123 | n/a | else { |
---|
5124 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
5125 | n/a | "slice indices must be integers or " |
---|
5126 | n/a | "None or have an __index__ method"); |
---|
5127 | n/a | return 0; |
---|
5128 | n/a | } |
---|
5129 | n/a | *pi = x; |
---|
5130 | n/a | } |
---|
5131 | n/a | return 1; |
---|
5132 | n/a | } |
---|
5133 | n/a | |
---|
5134 | n/a | #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ |
---|
5135 | n/a | "BaseException is not allowed" |
---|
5136 | n/a | |
---|
5137 | n/a | static PyObject * |
---|
5138 | n/a | cmp_outcome(int op, PyObject *v, PyObject *w) |
---|
5139 | n/a | { |
---|
5140 | n/a | int res = 0; |
---|
5141 | n/a | switch (op) { |
---|
5142 | n/a | case PyCmp_IS: |
---|
5143 | n/a | res = (v == w); |
---|
5144 | n/a | break; |
---|
5145 | n/a | case PyCmp_IS_NOT: |
---|
5146 | n/a | res = (v != w); |
---|
5147 | n/a | break; |
---|
5148 | n/a | case PyCmp_IN: |
---|
5149 | n/a | res = PySequence_Contains(w, v); |
---|
5150 | n/a | if (res < 0) |
---|
5151 | n/a | return NULL; |
---|
5152 | n/a | break; |
---|
5153 | n/a | case PyCmp_NOT_IN: |
---|
5154 | n/a | res = PySequence_Contains(w, v); |
---|
5155 | n/a | if (res < 0) |
---|
5156 | n/a | return NULL; |
---|
5157 | n/a | res = !res; |
---|
5158 | n/a | break; |
---|
5159 | n/a | case PyCmp_EXC_MATCH: |
---|
5160 | n/a | if (PyTuple_Check(w)) { |
---|
5161 | n/a | Py_ssize_t i, length; |
---|
5162 | n/a | length = PyTuple_Size(w); |
---|
5163 | n/a | for (i = 0; i < length; i += 1) { |
---|
5164 | n/a | PyObject *exc = PyTuple_GET_ITEM(w, i); |
---|
5165 | n/a | if (!PyExceptionClass_Check(exc)) { |
---|
5166 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
5167 | n/a | CANNOT_CATCH_MSG); |
---|
5168 | n/a | return NULL; |
---|
5169 | n/a | } |
---|
5170 | n/a | } |
---|
5171 | n/a | } |
---|
5172 | n/a | else { |
---|
5173 | n/a | if (!PyExceptionClass_Check(w)) { |
---|
5174 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
5175 | n/a | CANNOT_CATCH_MSG); |
---|
5176 | n/a | return NULL; |
---|
5177 | n/a | } |
---|
5178 | n/a | } |
---|
5179 | n/a | res = PyErr_GivenExceptionMatches(v, w); |
---|
5180 | n/a | break; |
---|
5181 | n/a | default: |
---|
5182 | n/a | return PyObject_RichCompare(v, w, op); |
---|
5183 | n/a | } |
---|
5184 | n/a | v = res ? Py_True : Py_False; |
---|
5185 | n/a | Py_INCREF(v); |
---|
5186 | n/a | return v; |
---|
5187 | n/a | } |
---|
5188 | n/a | |
---|
5189 | n/a | static PyObject * |
---|
5190 | n/a | import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level) |
---|
5191 | n/a | { |
---|
5192 | n/a | _Py_IDENTIFIER(__import__); |
---|
5193 | n/a | PyObject *import_func, *res; |
---|
5194 | n/a | PyObject* stack[5]; |
---|
5195 | n/a | |
---|
5196 | n/a | import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__); |
---|
5197 | n/a | if (import_func == NULL) { |
---|
5198 | n/a | PyErr_SetString(PyExc_ImportError, "__import__ not found"); |
---|
5199 | n/a | return NULL; |
---|
5200 | n/a | } |
---|
5201 | n/a | |
---|
5202 | n/a | /* Fast path for not overloaded __import__. */ |
---|
5203 | n/a | if (import_func == PyThreadState_GET()->interp->import_func) { |
---|
5204 | n/a | int ilevel = _PyLong_AsInt(level); |
---|
5205 | n/a | if (ilevel == -1 && PyErr_Occurred()) { |
---|
5206 | n/a | return NULL; |
---|
5207 | n/a | } |
---|
5208 | n/a | res = PyImport_ImportModuleLevelObject( |
---|
5209 | n/a | name, |
---|
5210 | n/a | f->f_globals, |
---|
5211 | n/a | f->f_locals == NULL ? Py_None : f->f_locals, |
---|
5212 | n/a | fromlist, |
---|
5213 | n/a | ilevel); |
---|
5214 | n/a | return res; |
---|
5215 | n/a | } |
---|
5216 | n/a | |
---|
5217 | n/a | Py_INCREF(import_func); |
---|
5218 | n/a | |
---|
5219 | n/a | stack[0] = name; |
---|
5220 | n/a | stack[1] = f->f_globals; |
---|
5221 | n/a | stack[2] = f->f_locals == NULL ? Py_None : f->f_locals; |
---|
5222 | n/a | stack[3] = fromlist; |
---|
5223 | n/a | stack[4] = level; |
---|
5224 | n/a | res = _PyObject_FastCall(import_func, stack, 5); |
---|
5225 | n/a | Py_DECREF(import_func); |
---|
5226 | n/a | return res; |
---|
5227 | n/a | } |
---|
5228 | n/a | |
---|
5229 | n/a | static PyObject * |
---|
5230 | n/a | import_from(PyObject *v, PyObject *name) |
---|
5231 | n/a | { |
---|
5232 | n/a | PyObject *x; |
---|
5233 | n/a | _Py_IDENTIFIER(__name__); |
---|
5234 | n/a | PyObject *fullmodname, *pkgname; |
---|
5235 | n/a | |
---|
5236 | n/a | x = PyObject_GetAttr(v, name); |
---|
5237 | n/a | if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
---|
5238 | n/a | return x; |
---|
5239 | n/a | /* Issue #17636: in case this failed because of a circular relative |
---|
5240 | n/a | import, try to fallback on reading the module directly from |
---|
5241 | n/a | sys.modules. */ |
---|
5242 | n/a | PyErr_Clear(); |
---|
5243 | n/a | pkgname = _PyObject_GetAttrId(v, &PyId___name__); |
---|
5244 | n/a | if (pkgname == NULL) { |
---|
5245 | n/a | goto error; |
---|
5246 | n/a | } |
---|
5247 | n/a | fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); |
---|
5248 | n/a | Py_DECREF(pkgname); |
---|
5249 | n/a | if (fullmodname == NULL) { |
---|
5250 | n/a | return NULL; |
---|
5251 | n/a | } |
---|
5252 | n/a | x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); |
---|
5253 | n/a | Py_DECREF(fullmodname); |
---|
5254 | n/a | if (x == NULL) { |
---|
5255 | n/a | goto error; |
---|
5256 | n/a | } |
---|
5257 | n/a | Py_INCREF(x); |
---|
5258 | n/a | return x; |
---|
5259 | n/a | error: |
---|
5260 | n/a | PyErr_Format(PyExc_ImportError, "cannot import name %R", name); |
---|
5261 | n/a | return NULL; |
---|
5262 | n/a | } |
---|
5263 | n/a | |
---|
5264 | n/a | static int |
---|
5265 | n/a | import_all_from(PyObject *locals, PyObject *v) |
---|
5266 | n/a | { |
---|
5267 | n/a | _Py_IDENTIFIER(__all__); |
---|
5268 | n/a | _Py_IDENTIFIER(__dict__); |
---|
5269 | n/a | PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); |
---|
5270 | n/a | PyObject *dict, *name, *value; |
---|
5271 | n/a | int skip_leading_underscores = 0; |
---|
5272 | n/a | int pos, err; |
---|
5273 | n/a | |
---|
5274 | n/a | if (all == NULL) { |
---|
5275 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
---|
5276 | n/a | return -1; /* Unexpected error */ |
---|
5277 | n/a | PyErr_Clear(); |
---|
5278 | n/a | dict = _PyObject_GetAttrId(v, &PyId___dict__); |
---|
5279 | n/a | if (dict == NULL) { |
---|
5280 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
---|
5281 | n/a | return -1; |
---|
5282 | n/a | PyErr_SetString(PyExc_ImportError, |
---|
5283 | n/a | "from-import-* object has no __dict__ and no __all__"); |
---|
5284 | n/a | return -1; |
---|
5285 | n/a | } |
---|
5286 | n/a | all = PyMapping_Keys(dict); |
---|
5287 | n/a | Py_DECREF(dict); |
---|
5288 | n/a | if (all == NULL) |
---|
5289 | n/a | return -1; |
---|
5290 | n/a | skip_leading_underscores = 1; |
---|
5291 | n/a | } |
---|
5292 | n/a | |
---|
5293 | n/a | for (pos = 0, err = 0; ; pos++) { |
---|
5294 | n/a | name = PySequence_GetItem(all, pos); |
---|
5295 | n/a | if (name == NULL) { |
---|
5296 | n/a | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
---|
5297 | n/a | err = -1; |
---|
5298 | n/a | else |
---|
5299 | n/a | PyErr_Clear(); |
---|
5300 | n/a | break; |
---|
5301 | n/a | } |
---|
5302 | n/a | if (skip_leading_underscores && |
---|
5303 | n/a | PyUnicode_Check(name) && |
---|
5304 | n/a | PyUnicode_READY(name) != -1 && |
---|
5305 | n/a | PyUnicode_READ_CHAR(name, 0) == '_') |
---|
5306 | n/a | { |
---|
5307 | n/a | Py_DECREF(name); |
---|
5308 | n/a | continue; |
---|
5309 | n/a | } |
---|
5310 | n/a | value = PyObject_GetAttr(v, name); |
---|
5311 | n/a | if (value == NULL) |
---|
5312 | n/a | err = -1; |
---|
5313 | n/a | else if (PyDict_CheckExact(locals)) |
---|
5314 | n/a | err = PyDict_SetItem(locals, name, value); |
---|
5315 | n/a | else |
---|
5316 | n/a | err = PyObject_SetItem(locals, name, value); |
---|
5317 | n/a | Py_DECREF(name); |
---|
5318 | n/a | Py_XDECREF(value); |
---|
5319 | n/a | if (err != 0) |
---|
5320 | n/a | break; |
---|
5321 | n/a | } |
---|
5322 | n/a | Py_DECREF(all); |
---|
5323 | n/a | return err; |
---|
5324 | n/a | } |
---|
5325 | n/a | |
---|
5326 | n/a | static void |
---|
5327 | n/a | format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj) |
---|
5328 | n/a | { |
---|
5329 | n/a | const char *obj_str; |
---|
5330 | n/a | |
---|
5331 | n/a | if (!obj) |
---|
5332 | n/a | return; |
---|
5333 | n/a | |
---|
5334 | n/a | obj_str = PyUnicode_AsUTF8(obj); |
---|
5335 | n/a | if (!obj_str) |
---|
5336 | n/a | return; |
---|
5337 | n/a | |
---|
5338 | n/a | PyErr_Format(exc, format_str, obj_str); |
---|
5339 | n/a | } |
---|
5340 | n/a | |
---|
5341 | n/a | static void |
---|
5342 | n/a | format_exc_unbound(PyCodeObject *co, int oparg) |
---|
5343 | n/a | { |
---|
5344 | n/a | PyObject *name; |
---|
5345 | n/a | /* Don't stomp existing exception */ |
---|
5346 | n/a | if (PyErr_Occurred()) |
---|
5347 | n/a | return; |
---|
5348 | n/a | if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) { |
---|
5349 | n/a | name = PyTuple_GET_ITEM(co->co_cellvars, |
---|
5350 | n/a | oparg); |
---|
5351 | n/a | format_exc_check_arg( |
---|
5352 | n/a | PyExc_UnboundLocalError, |
---|
5353 | n/a | UNBOUNDLOCAL_ERROR_MSG, |
---|
5354 | n/a | name); |
---|
5355 | n/a | } else { |
---|
5356 | n/a | name = PyTuple_GET_ITEM(co->co_freevars, oparg - |
---|
5357 | n/a | PyTuple_GET_SIZE(co->co_cellvars)); |
---|
5358 | n/a | format_exc_check_arg(PyExc_NameError, |
---|
5359 | n/a | UNBOUNDFREE_ERROR_MSG, name); |
---|
5360 | n/a | } |
---|
5361 | n/a | } |
---|
5362 | n/a | |
---|
5363 | n/a | static PyObject * |
---|
5364 | n/a | unicode_concatenate(PyObject *v, PyObject *w, |
---|
5365 | n/a | PyFrameObject *f, const _Py_CODEUNIT *next_instr) |
---|
5366 | n/a | { |
---|
5367 | n/a | PyObject *res; |
---|
5368 | n/a | if (Py_REFCNT(v) == 2) { |
---|
5369 | n/a | /* In the common case, there are 2 references to the value |
---|
5370 | n/a | * stored in 'variable' when the += is performed: one on the |
---|
5371 | n/a | * value stack (in 'v') and one still stored in the |
---|
5372 | n/a | * 'variable'. We try to delete the variable now to reduce |
---|
5373 | n/a | * the refcnt to 1. |
---|
5374 | n/a | */ |
---|
5375 | n/a | int opcode, oparg; |
---|
5376 | n/a | NEXTOPARG(); |
---|
5377 | n/a | switch (opcode) { |
---|
5378 | n/a | case STORE_FAST: |
---|
5379 | n/a | { |
---|
5380 | n/a | PyObject **fastlocals = f->f_localsplus; |
---|
5381 | n/a | if (GETLOCAL(oparg) == v) |
---|
5382 | n/a | SETLOCAL(oparg, NULL); |
---|
5383 | n/a | break; |
---|
5384 | n/a | } |
---|
5385 | n/a | case STORE_DEREF: |
---|
5386 | n/a | { |
---|
5387 | n/a | PyObject **freevars = (f->f_localsplus + |
---|
5388 | n/a | f->f_code->co_nlocals); |
---|
5389 | n/a | PyObject *c = freevars[oparg]; |
---|
5390 | n/a | if (PyCell_GET(c) == v) { |
---|
5391 | n/a | PyCell_SET(c, NULL); |
---|
5392 | n/a | Py_DECREF(v); |
---|
5393 | n/a | } |
---|
5394 | n/a | break; |
---|
5395 | n/a | } |
---|
5396 | n/a | case STORE_NAME: |
---|
5397 | n/a | { |
---|
5398 | n/a | PyObject *names = f->f_code->co_names; |
---|
5399 | n/a | PyObject *name = GETITEM(names, oparg); |
---|
5400 | n/a | PyObject *locals = f->f_locals; |
---|
5401 | n/a | if (PyDict_CheckExact(locals) && |
---|
5402 | n/a | PyDict_GetItem(locals, name) == v) { |
---|
5403 | n/a | if (PyDict_DelItem(locals, name) != 0) { |
---|
5404 | n/a | PyErr_Clear(); |
---|
5405 | n/a | } |
---|
5406 | n/a | } |
---|
5407 | n/a | break; |
---|
5408 | n/a | } |
---|
5409 | n/a | } |
---|
5410 | n/a | } |
---|
5411 | n/a | res = v; |
---|
5412 | n/a | PyUnicode_Append(&res, w); |
---|
5413 | n/a | return res; |
---|
5414 | n/a | } |
---|
5415 | n/a | |
---|
5416 | n/a | #ifdef DYNAMIC_EXECUTION_PROFILE |
---|
5417 | n/a | |
---|
5418 | n/a | static PyObject * |
---|
5419 | n/a | getarray(long a[256]) |
---|
5420 | n/a | { |
---|
5421 | n/a | int i; |
---|
5422 | n/a | PyObject *l = PyList_New(256); |
---|
5423 | n/a | if (l == NULL) return NULL; |
---|
5424 | n/a | for (i = 0; i < 256; i++) { |
---|
5425 | n/a | PyObject *x = PyLong_FromLong(a[i]); |
---|
5426 | n/a | if (x == NULL) { |
---|
5427 | n/a | Py_DECREF(l); |
---|
5428 | n/a | return NULL; |
---|
5429 | n/a | } |
---|
5430 | n/a | PyList_SetItem(l, i, x); |
---|
5431 | n/a | } |
---|
5432 | n/a | for (i = 0; i < 256; i++) |
---|
5433 | n/a | a[i] = 0; |
---|
5434 | n/a | return l; |
---|
5435 | n/a | } |
---|
5436 | n/a | |
---|
5437 | n/a | PyObject * |
---|
5438 | n/a | _Py_GetDXProfile(PyObject *self, PyObject *args) |
---|
5439 | n/a | { |
---|
5440 | n/a | #ifndef DXPAIRS |
---|
5441 | n/a | return getarray(dxp); |
---|
5442 | n/a | #else |
---|
5443 | n/a | int i; |
---|
5444 | n/a | PyObject *l = PyList_New(257); |
---|
5445 | n/a | if (l == NULL) return NULL; |
---|
5446 | n/a | for (i = 0; i < 257; i++) { |
---|
5447 | n/a | PyObject *x = getarray(dxpairs[i]); |
---|
5448 | n/a | if (x == NULL) { |
---|
5449 | n/a | Py_DECREF(l); |
---|
5450 | n/a | return NULL; |
---|
5451 | n/a | } |
---|
5452 | n/a | PyList_SetItem(l, i, x); |
---|
5453 | n/a | } |
---|
5454 | n/a | return l; |
---|
5455 | n/a | #endif |
---|
5456 | n/a | } |
---|
5457 | n/a | |
---|
5458 | n/a | #endif |
---|
5459 | n/a | |
---|
5460 | n/a | Py_ssize_t |
---|
5461 | n/a | _PyEval_RequestCodeExtraIndex(freefunc free) |
---|
5462 | n/a | { |
---|
5463 | n/a | PyThreadState *tstate = PyThreadState_Get(); |
---|
5464 | n/a | Py_ssize_t new_index; |
---|
5465 | n/a | |
---|
5466 | n/a | if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) { |
---|
5467 | n/a | return -1; |
---|
5468 | n/a | } |
---|
5469 | n/a | new_index = tstate->co_extra_user_count++; |
---|
5470 | n/a | tstate->co_extra_freefuncs[new_index] = free; |
---|
5471 | n/a | return new_index; |
---|
5472 | n/a | } |
---|
5473 | n/a | |
---|
5474 | n/a | static void |
---|
5475 | n/a | dtrace_function_entry(PyFrameObject *f) |
---|
5476 | n/a | { |
---|
5477 | n/a | const char *filename; |
---|
5478 | n/a | const char *funcname; |
---|
5479 | n/a | int lineno; |
---|
5480 | n/a | |
---|
5481 | n/a | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
---|
5482 | n/a | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
---|
5483 | n/a | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
---|
5484 | n/a | |
---|
5485 | n/a | PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); |
---|
5486 | n/a | } |
---|
5487 | n/a | |
---|
5488 | n/a | static void |
---|
5489 | n/a | dtrace_function_return(PyFrameObject *f) |
---|
5490 | n/a | { |
---|
5491 | n/a | const char *filename; |
---|
5492 | n/a | const char *funcname; |
---|
5493 | n/a | int lineno; |
---|
5494 | n/a | |
---|
5495 | n/a | filename = PyUnicode_AsUTF8(f->f_code->co_filename); |
---|
5496 | n/a | funcname = PyUnicode_AsUTF8(f->f_code->co_name); |
---|
5497 | n/a | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
---|
5498 | n/a | |
---|
5499 | n/a | PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); |
---|
5500 | n/a | } |
---|
5501 | n/a | |
---|
5502 | n/a | /* DTrace equivalent of maybe_call_line_trace. */ |
---|
5503 | n/a | static void |
---|
5504 | n/a | maybe_dtrace_line(PyFrameObject *frame, |
---|
5505 | n/a | int *instr_lb, int *instr_ub, int *instr_prev) |
---|
5506 | n/a | { |
---|
5507 | n/a | int line = frame->f_lineno; |
---|
5508 | n/a | const char *co_filename, *co_name; |
---|
5509 | n/a | |
---|
5510 | n/a | /* If the last instruction executed isn't in the current |
---|
5511 | n/a | instruction window, reset the window. |
---|
5512 | n/a | */ |
---|
5513 | n/a | if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) { |
---|
5514 | n/a | PyAddrPair bounds; |
---|
5515 | n/a | line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti, |
---|
5516 | n/a | &bounds); |
---|
5517 | n/a | *instr_lb = bounds.ap_lower; |
---|
5518 | n/a | *instr_ub = bounds.ap_upper; |
---|
5519 | n/a | } |
---|
5520 | n/a | /* If the last instruction falls at the start of a line or if |
---|
5521 | n/a | it represents a jump backwards, update the frame's line |
---|
5522 | n/a | number and call the trace function. */ |
---|
5523 | n/a | if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) { |
---|
5524 | n/a | frame->f_lineno = line; |
---|
5525 | n/a | co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); |
---|
5526 | n/a | if (!co_filename) |
---|
5527 | n/a | co_filename = "?"; |
---|
5528 | n/a | co_name = PyUnicode_AsUTF8(frame->f_code->co_name); |
---|
5529 | n/a | if (!co_name) |
---|
5530 | n/a | co_name = "?"; |
---|
5531 | n/a | PyDTrace_LINE(co_filename, co_name, line); |
---|
5532 | n/a | } |
---|
5533 | n/a | *instr_prev = frame->f_lasti; |
---|
5534 | n/a | } |
---|