Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06032cb

Browse files
committed
Coded WHY flags as bitfields (taking inspiration from tp_flags).
This allows multiple flags to be tested in a single compare which eliminates unnecessary compares and saves a few bytes.
1 parent 4057483 commit 06032cb

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

Python/ceval.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,15 @@ _Py_CheckRecursiveCall(char *where)
537537
}
538538

539539
/* Status code for main loop (reason for stack unwind) */
540-
enum why_code {
541-
WHY_NOT, /* No error */
542-
WHY_EXCEPTION, /* Exception occurred */
543-
WHY_RERAISE, /* Exception re-raised by 'finally' */
544-
WHY_RETURN, /* 'return' statement */
545-
WHY_BREAK, /* 'break' statement */
546-
WHY_CONTINUE, /* 'continue' statement */
547-
WHY_YIELD /* 'yield' operator */
548-
};
549-
550-
static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
540+
#define WHY_NOT 0x0001
541+
#define WHY_EXCEPTION 0x0002
542+
#define WHY_RERAISE 0x0004
543+
#define WHY_RETURN 0x0008
544+
#define WHY_BREAK 0x0010
545+
#define WHY_CONTINUE 0x0020
546+
#define WHY_YIELD 0x0040
547+
548+
static int do_raise(PyObject *, PyObject *, PyObject *);
551549
static int unpack_iterable(PyObject *, int, PyObject **);
552550

553551
/* for manipulating the thread switch and periodic "stuff" - used to be
@@ -580,7 +578,7 @@ eval_frame(PyFrameObject *f)
580578
register unsigned char *next_instr;
581579
register int opcode=0; /* Current opcode */
582580
register int oparg=0; /* Current opcode argument, if any */
583-
register enum why_code why; /* Reason for block stack unwind */
581+
register int why; /* Reason for block stack unwind */
584582
register int err; /* Error status -- nonzero if error */
585583
register PyObject *x; /* Result object -- NULL if error */
586584
register PyObject *v; /* Temporary objects popped off stack */
@@ -1652,10 +1650,9 @@ eval_frame(PyFrameObject *f)
16521650
case END_FINALLY:
16531651
v = POP();
16541652
if (PyInt_Check(v)) {
1655-
why = (enum why_code) PyInt_AS_LONG(v);
1653+
why = (int) PyInt_AS_LONG(v);
16561654
assert(why != WHY_YIELD);
1657-
if (why == WHY_RETURN ||
1658-
why == WHY_CONTINUE)
1655+
if (why & (WHY_RETURN | WHY_CONTINUE))
16591656
retval = POP();
16601657
}
16611658
else if (PyString_Check(v) || PyClass_Check(v)) {
@@ -2288,7 +2285,7 @@ eval_frame(PyFrameObject *f)
22882285

22892286
/* Double-check exception status */
22902287

2291-
if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2288+
if (why & (WHY_EXCEPTION | WHY_RERAISE)) {
22922289
if (!PyErr_Occurred()) {
22932290
PyErr_SetString(PyExc_SystemError,
22942291
"error return without exception set");
@@ -2379,8 +2376,7 @@ eval_frame(PyFrameObject *f)
23792376
PUSH(exc);
23802377
}
23812378
else {
2382-
if (why == WHY_RETURN ||
2383-
why == WHY_CONTINUE)
2379+
if (why & (WHY_RETURN | WHY_CONTINUE))
23842380
PUSH(retval);
23852381
v = PyInt_FromLong((long)why);
23862382
PUSH(v);
@@ -2411,7 +2407,7 @@ eval_frame(PyFrameObject *f)
24112407
fast_yield:
24122408
if (tstate->use_tracing) {
24132409
if (tstate->c_tracefunc
2414-
&& (why == WHY_RETURN || why == WHY_YIELD)) {
2410+
&& (why & (WHY_RETURN | WHY_YIELD))) {
24152411
if (call_trace(tstate->c_tracefunc,
24162412
tstate->c_traceobj, f,
24172413
PyTrace_RETURN, retval)) {
@@ -2838,7 +2834,7 @@ reset_exc_info(PyThreadState *tstate)
28382834

28392835
/* Logic for the raise statement (too complicated for inlining).
28402836
This *consumes* a reference count to each of its arguments. */
2841-
static enum why_code
2837+
static int
28422838
do_raise(PyObject *type, PyObject *value, PyObject *tb)
28432839
{
28442840
if (type == NULL) {

0 commit comments

Comments
 (0)