Source

hotpy_2 / Include / high_level_ops.inc

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

        TARGET(PRINT_EXPR)
            v = POP();
            w = PySys_GetObject("displayhook");
            if (w == NULL) {
                PyErr_SetString(PyExc_RuntimeError,
                                "lost sys.displayhook");
                err = -1;
                x = NULL;
            }
            if (err == 0) {
                x = PyTuple_Pack(1, v);
                if (x == NULL)
                    err = -1;
            }
            if (err == 0) {
                w = PyEval_CallObject(w, x);
                Py_XDECREF(w);
                if (w == NULL)
                    err = -1;
            }
            Py_DECREF(v);
            Py_XDECREF(x);
            break;

        TARGET(YIELD_FROM)
            u = POP();
            x = TOP();
            /* send u to x */
            if (PyGen_CheckExact(x)) {
                retval = _PyGen_Send((PyGenObject *)x, u);
            } else {
                _Py_IDENTIFIER(send);
                if (u == Py_None)
                    retval = PyIter_Next(x);
                else
                    retval = _PyObject_CallMethodId(x, &PyId_send, "O", u);
            }
            Py_DECREF(u);
            if (!retval) {
                PyObject *val;
                x = POP(); /* Remove iter from stack */
                Py_DECREF(x);
                err = _PyGen_FetchStopIterationValue(&val);
                if (err < 0) {
                    x = NULL;
                    break;
                }
                x = val;
                PUSH(x);
                continue;
            }
            /* x remains on stack, retval is value to be yielded */
            why = WHY_YIELD;
            /* and repeat... */
            f->f_lasti--;
            goto fast_yield;

        TARGET(LOAD_BUILD_CLASS)
        {
            _Py_IDENTIFIER(__build_class__);

            if (PyDict_CheckExact(f->f_builtins)) {
                x = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
                if (x == NULL) {
                    PyErr_SetString(PyExc_NameError,
                                    "__build_class__ not found");
                    break;
                }
                Py_INCREF(x);
            }
            else {
                PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
                if (build_class_str == NULL)
                    break;
                x = PyObject_GetItem(f->f_builtins, build_class_str);
                if (x == NULL) {
                    if (PyErr_ExceptionMatches(PyExc_KeyError))
                        PyErr_SetString(PyExc_NameError,
                                        "__build_class__ not found");
                    break;
                }
            }
            PUSH(x);
            break;
        }

        TARGET(STORE_NAME)
            w = GETITEM(names, oparg);
            v = POP();
            if ((x = f->f_locals) != NULL) {
                if (PyDict_CheckExact(x))
                    err = PyDict_SetItem(x, w, v);
                else
                    err = PyObject_SetItem(x, w, v);
                Py_DECREF(v);
                if (err == 0) DISPATCH();
                break;
            }
            PyErr_Format(PyExc_SystemError,
                         "no locals found when storing %R", w);
            break;

        TARGET(DELETE_NAME)
            w = GETITEM(names, oparg);
            if ((x = f->f_locals) != NULL) {
                if ((err = PyObject_DelItem(x, w)) != 0)
                    format_exc_check_arg(PyExc_NameError,
                                         NAME_ERROR_MSG,
                                         w);
                break;
            }
            PyErr_Format(PyExc_SystemError,
                         "no locals when deleting %R", w);
            break;

        TARGET(LOAD_NAME)
            w = GETITEM(names, oparg);
            if ((v = f->f_locals) == NULL) {
                PyErr_Format(PyExc_SystemError,
                             "no locals when loading %R", w);
                why = WHY_EXCEPTION;
                break;
            }
            if (PyDict_CheckExact(v)) {
                x = PyDict_GetItem(v, w);
                Py_XINCREF(x);
            }
            else {
                x = PyObject_GetItem(v, w);
                if (x == NULL && PyErr_Occurred()) {
                    if (!PyErr_ExceptionMatches(
                                    PyExc_KeyError))
                        break;
                    PyErr_Clear();
                }
            }
            if (x == NULL) {
                x = PyDict_GetItem(f->f_globals, w);
                Py_XINCREF(x);
                if (x == NULL) {
                    if (PyDict_CheckExact(f->f_builtins)) {
                        x = PyDict_GetItem(f->f_builtins, w);
                        if (x == NULL) {
                            format_exc_check_arg(
                                        PyExc_NameError,
                                        NAME_ERROR_MSG, w);
                            break;
                        }
                        Py_INCREF(x);
                    }
                    else {
                        x = PyObject_GetItem(f->f_builtins, w);
                        if (x == NULL) {
                            if (PyErr_ExceptionMatches(PyExc_KeyError))
                                format_exc_check_arg(
                                            PyExc_NameError,
                                            NAME_ERROR_MSG, w);
                            break;
                        }
                    }
                }
            }
            PUSH(x);
            DISPATCH();

        TARGET(IMPORT_NAME)
        {
            _Py_IDENTIFIER(__import__);
            w = GETITEM(names, oparg);
            x = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
            if (x == NULL) {
                PyErr_SetString(PyExc_ImportError,
                                "__import__ not found");
                break;
            }
            Py_INCREF(x);
            v = POP();
            u = TOP();
            if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
                w = PyTuple_Pack(5,
                            w,
                            f->f_globals,
                            f->f_locals == NULL ?
                                  Py_None : f->f_locals,
                            v,
                            u);
            else
                w = PyTuple_Pack(4,
                            w,
                            f->f_globals,
                            f->f_locals == NULL ?
                                  Py_None : f->f_locals,
                            v);
            Py_DECREF(v);
            Py_DECREF(u);
            if (w == NULL) {
                u = POP();
                Py_DECREF(x);
                x = NULL;
                break;
            }
            READ_TIMESTAMP(intr0);
            v = x;
            x = PyEval_CallObject(v, w);
            Py_DECREF(v);
            READ_TIMESTAMP(intr1);
            Py_DECREF(w);
            SET_TOP(x);
            if (x != NULL) DISPATCH();
            break;
        }

        TARGET(IMPORT_STAR)
            v = POP();
            PyFrame_FastToLocals(f);
            if ((x = f->f_locals) == NULL) {
                PyErr_SetString(PyExc_SystemError,
                    "no locals found during 'import *'");
                break;
            }
            READ_TIMESTAMP(intr0);
            err = import_all_from(x, v);
            READ_TIMESTAMP(intr1);
            PyFrame_LocalsToFast(f, 0);
            Py_DECREF(v);
            if (err == 0) DISPATCH();
            break;

        TARGET(IMPORT_FROM)
            w = GETITEM(names, oparg);
            v = TOP();
            READ_TIMESTAMP(intr0);
            x = import_from(v, w);
            READ_TIMESTAMP(intr1);
            PUSH(x);
            if (x != NULL) DISPATCH();
            break;

        /* This might need to be treated as mid-level since gen-expr
         * use MAKE_FUNCTION
         */
        TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
        TARGET(MAKE_FUNCTION)
        _make_function:
        {
            int posdefaults = oparg & 0xff;
            int kwdefaults = (oparg>>8) & 0xff;
            int num_annotations = (oparg >> 16) & 0x7fff;

            w = POP(); /* qualname */
            v = POP(); /* code object */
            x = PyFunction_NewWithQualName(v, f->f_globals, w);
            Py_DECREF(v);
            Py_DECREF(w);

            if (x != NULL && opcode == MAKE_CLOSURE) {
                v = POP();
                if (PyFunction_SetClosure(x, v) != 0) {
                    /* Can't happen unless bytecode is corrupt. */
                    why = WHY_EXCEPTION;
                }
                Py_DECREF(v);
            }

            if (x != NULL && num_annotations > 0) {
                Py_ssize_t name_ix;
                u = POP(); /* names of args with annotations */
                v = PyDict_New();
                if (v == NULL) {
                    Py_DECREF(x);
                    x = NULL;
                    break;
                }
                name_ix = PyTuple_Size(u);
                assert(num_annotations == name_ix+1);
                while (name_ix > 0) {
                    --name_ix;
                    t = PyTuple_GET_ITEM(u, name_ix);
                    w = POP();
                    /* XXX(nnorwitz): check for errors */
                    PyDict_SetItem(v, t, w);
                    Py_DECREF(w);
                }

                if (PyFunction_SetAnnotations(x, v) != 0) {
                    /* Can't happen unless
                       PyFunction_SetAnnotations changes. */
                    why = WHY_EXCEPTION;
                }
                Py_DECREF(v);
                Py_DECREF(u);
            }

            /* XXX Maybe this should be a separate opcode? */
            if (x != NULL && posdefaults > 0) {
                v = PyTuple_New(posdefaults);
                if (v == NULL) {
                    Py_DECREF(x);
                    x = NULL;
                    break;
                }
                while (--posdefaults >= 0) {
                    w = POP();
                    PyTuple_SET_ITEM(v, posdefaults, w);
                }
                if (PyFunction_SetDefaults(x, v) != 0) {
                    /* Can't happen unless
                       PyFunction_SetDefaults changes. */
                    why = WHY_EXCEPTION;
                }
                Py_DECREF(v);
            }
            if (x != NULL && kwdefaults > 0) {
                v = PyDict_New();
                if (v == NULL) {
                    Py_DECREF(x);
                    x = NULL;
                    break;
                }
                while (--kwdefaults >= 0) {
                    w = POP(); /* default value */
                    u = POP(); /* kw only arg name */
                    /* XXX(nnorwitz): check for errors */
                    PyDict_SetItem(v, u, w);
                    Py_DECREF(w);
                    Py_DECREF(u);
                }
                if (PyFunction_SetKwDefaults(x, v) != 0) {
                    /* Can't happen unless
                       PyFunction_SetKwDefaults changes. */
                    why = WHY_EXCEPTION;
                }
                Py_DECREF(v);
            }
            PUSH(x);
            break;
        }

        PREDICTED(END_FINALLY);
        TARGET(END_FINALLY)
            v = POP();
            if (PyLong_Check(v)) {
                why = (enum why_code) PyLong_AS_LONG(v);
                assert(why != WHY_YIELD);
                if (why == WHY_RETURN ||
                    why == WHY_CONTINUE)
                    retval = POP();
                if (why == WHY_SILENCED) {
                    /* An exception was silenced by 'with', we must
                    manually unwind the EXCEPT_HANDLER block which was
                    created when the exception was caught, otherwise
                    the stack will be in an inconsistent state. */
                    PyTryBlock *b = PyFrame_BlockPop(f);
                    assert(b->b_type == EXCEPT_HANDLER);
                    UNWIND_EXCEPT_HANDLER(b);
                    why = WHY_NOT;
                }
            }
            else if (PyExceptionClass_Check(v)) {
                w = POP();
                u = POP();
                PyErr_Restore(v, w, u);
                why = WHY_RERAISE;
                break;
            }
            else if (v != Py_None) {
                PyErr_SetString(PyExc_SystemError,
                    "'finally' pops bad exception");
                why = WHY_EXCEPTION;
            }
            Py_DECREF(v);
            break;

        TARGET(UNPACK_EX)
        {
            int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
            v = POP();

            if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
                                f->f_stacktop + totalargs)) {
                f->f_stacktop += totalargs;
            } else {
                why = WHY_EXCEPTION;
            }
            Py_DECREF(v);
            break;
        }

        TARGET(SETUP_WITH)
        {
            _Py_IDENTIFIER(__exit__);
            _Py_IDENTIFIER(__enter__);
            w = TOP();
            x = special_lookup(w, &PyId___exit__);
            if (!x)
                break;
            SET_TOP(x);
            u = special_lookup(w, &PyId___enter__);
            Py_DECREF(w);
            if (!u) {
                x = NULL;
                break;
            }
            x = PyObject_CallFunctionObjArgs(u, NULL);
            Py_DECREF(u);
            if (!x)
                break;
            /* Setup the finally block before pushing the result
               of __enter__ on the stack. */
            PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
                               STACK_LEVEL());

            PUSH(x);
            DISPATCH();
        }

        TARGET(WITH_CLEANUP)
        {
            /* At the top of the stack are 1-3 values indicating
               how/why we entered the finally clause:
               - TOP = None
               - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
               - TOP = WHY_*; no retval below it
               - (TOP, SECOND, THIRD) = exc_info()
                 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
               Below them is EXIT, the context.__exit__ bound method.
               In the last case, we must call
                 EXIT(TOP, SECOND, THIRD)
               otherwise we must call
                 EXIT(None, None, None)

               In the first two cases, we remove EXIT from the
               stack, leaving the rest in the same order.  In the
               third case, we shift the bottom 3 values of the
               stack down, and replace the empty spot with NULL.

               In addition, if the stack represents an exception,
               *and* the function call returns a 'true' value, we
               push WHY_SILENCED onto the stack.  END_FINALLY will
               then not re-raise the exception.  (But non-local
               gotos should still be resumed.)
            */

            PyObject *exit_func;
            u = TOP();
            if (u == Py_None) {
                (void)POP();
                exit_func = TOP();
                SET_TOP(u);
                v = w = Py_None;
            }
            else if (PyLong_Check(u)) {
                (void)POP();
                switch(PyLong_AsLong(u)) {
                case WHY_RETURN:
                case WHY_CONTINUE:
                    /* Retval in TOP. */
                    exit_func = SECOND();
                    SET_SECOND(TOP());
                    SET_TOP(u);
                    break;
                default:
                    exit_func = TOP();
                    SET_TOP(u);
                    break;
                }
                u = v = w = Py_None;
            }
            else {
                PyObject *tp, *exc, *tb;
                PyTryBlock *block;
                v = SECOND();
                w = THIRD();
                tp = FOURTH();
                exc = PEEK(5);
                tb = PEEK(6);
                exit_func = PEEK(7);
                SET_VALUE(7, tb);
                SET_VALUE(6, exc);
                SET_VALUE(5, tp);
                /* UNWIND_EXCEPT_HANDLER will pop this off. */
                SET_FOURTH(NULL);
                /* We just shifted the stack down, so we have
                   to tell the except handler block that the
                   values are lower than it expects. */
                block = &f->f_blockstack[f->f_iblock - 1];
                assert(block->b_type == EXCEPT_HANDLER);
                block->b_level--;
            }
            /* XXX Not the fastest way to call it... */
            x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
                                             NULL);
            Py_DECREF(exit_func);
            if (x == NULL)
                break; /* Go to error exit */

            if (u != Py_None)
                err = PyObject_IsTrue(x);
            else
                err = 0;
            Py_DECREF(x);

            if (err < 0)
                break; /* Go to error exit */
            else if (err > 0) {
                err = 0;
                /* There was an exception and a True return */
                PUSH(PyLong_FromLong((long) WHY_SILENCED));
            }
            PREDICT(END_FINALLY);
            break;
        }

        TARGET(RAISE_VARARGS)
            v = w = NULL;
            switch (oparg) {
            case 2:
                v = POP(); /* cause */
            case 1:
                w = POP(); /* exc */
            case 0: /* Fallthrough */
                why = do_raise(w, v);
                break;
            default:
                PyErr_SetString(PyExc_SystemError,
                           "bad RAISE_VARARGS oparg");
                why = WHY_EXCEPTION;
                break;
            }
            break;