Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a414fa6
Add SETUP_CLEANUP bytecode to track lasti for re-raising exceptions i…
markshannon Apr 19, 2021
902690d
Move some handling of exceptions to bytecode from unwind code.
markshannon Apr 21, 2021
21646b1
Move setting up except block out of unwind code into bytecode.
markshannon Apr 21, 2021
9c9575a
Move cleanup code for try-finally and try-except in bytecode.
markshannon Apr 21, 2021
3be89db
Move cleanup code for with and sync with into bytecode.
markshannon Apr 21, 2021
cf664f7
Simplify exit code for async for loops.
markshannon Apr 21, 2021
722b722
Remove SETUP_EXCEPT opcode.
markshannon Apr 21, 2021
caa355f
Remove EXCEPT_HANDLER as a type of exception handling block.
markshannon Apr 21, 2021
4c21ddd
Factor out all block pops and pushes into simple opcodes.
markshannon Apr 21, 2021
6a8d90c
Generate exception handling table
markshannon Apr 22, 2021
9115cea
Parse and verify exception table when unwinding exceptions.
markshannon Apr 23, 2021
350334c
Update test to account for new code object constructor.
markshannon Apr 26, 2021
d6da9cb
Push lasti to stack when cleaning up exception handlers, and use it t…
markshannon Apr 26, 2021
a6b9292
Remove block stack from frames add convert SETUP/POP_BLOCK instructio…
markshannon Apr 26, 2021
9d0a6c1
Make dis understand the exception table.
markshannon Apr 29, 2021
ca482d0
Merge branch 'master' into zero-cost-except
markshannon Apr 29, 2021
4629a97
Set line numbers at start of exception handlers in try-except and try…
markshannon Apr 30, 2021
58b4a5a
Handle exceptions in trace functions correctly.
markshannon Apr 30, 2021
e7a9221
Fix syntax error test.
markshannon Apr 30, 2021
062ffa4
Get frame.setlineno working again.
markshannon Apr 30, 2021
71d95c1
Merge branch 'master' into zero-cost-except
markshannon Apr 30, 2021
e5c2c3e
Remove debugging prints
markshannon Apr 30, 2021
7571e90
Fix test_dis after merge
markshannon Apr 30, 2021
812e78f
Layout frameobject in most compact form.
markshannon Apr 30, 2021
5801089
Add NEWS.
markshannon Apr 30, 2021
8cb6366
Remove SETUP_FINALLY, SETUP_ASYNC_WITH, SETUP_CLEANUP, POP_BLOCK inst…
markshannon Apr 30, 2021
4ec3417
Update dis.rst
markshannon Apr 30, 2021
07cd46d
fix NEWS formatting
markshannon Apr 30, 2021
3b52a3f
Fix typos and clarify text.
markshannon Apr 30, 2021
594a636
Restore dis after discarded experiment.
markshannon Apr 30, 2021
585f306
Bump magic number for 3.11
markshannon Apr 30, 2021
0f11d34
Fix typo
markshannon Apr 30, 2021
63a9b0e
Merge branch 'master' into zero-cost-except
markshannon May 3, 2021
133f74f
Re-order optimization passes to remove more NOPs.
markshannon May 3, 2021
299e051
Update importlib yet again.
markshannon May 3, 2021
43c199d
Assorted clarifications.
markshannon May 6, 2021
630e755
Rename frame.setlineno assistant code to clarify that we now model th…
markshannon May 6, 2021
6333f6c
Revert mistaken change.
markshannon May 6, 2021
e1d6e1e
Update Include/opcode.h
markshannon May 6, 2021
73b600a
Extend notes on exception handling.
markshannon May 6, 2021
f263ee2
Fix refleaks
markshannon May 6, 2021
2f31c84
Merge branch 'main' into zero-cost-except
markshannon May 7, 2021
0c3fd95
Document removal frame push/pop C-API functions.
markshannon May 7, 2021
b92ada2
Update importlib
markshannon May 7, 2021
1878117
Clarify exception handling notes
markshannon May 7, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove block stack from frames add convert SETUP/POP_BLOCK instructio…
…ns into NOPs.
  • Loading branch information
markshannon committed Apr 26, 2021
commit a6b9292b05b1f6bc9752024c0b3b8d2c5ed93b28
7 changes: 0 additions & 7 deletions Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ struct _frame {

int f_lasti; /* Last instruction if called */
int f_lineno; /* Current line number. Only valid if non-zero */
int f_iblock; /* index in f_blockstack */
PyFrameState f_state; /* What state the frame is in */
PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
};

Expand Down Expand Up @@ -77,11 +75,6 @@ _PyFrame_New_NoTrack(PyThreadState *, PyFrameConstructor *, PyObject *);

/* The rest of the interface is specific for frame objects */

/* Block management functions */

PyAPI_FUNC(void) PyFrame_BlockSetup(PyFrameObject *, int, int, int);
PyAPI_FUNC(PyTryBlock *) PyFrame_BlockPop(PyFrameObject *);
Comment on lines -82 to -83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these were part of the public C-API, it would probably be worth adding a note to the What's New doc about their removal. I suppose the same may be true of f_blockstack, though it's less likely that matters.


/* Conversions between "fast locals" and locals in dictionary */

PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ class C(object): pass
nfrees = len(x.f_code.co_freevars)
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
ncells + nfrees - 1
check(x, vsize('4Pi2c4P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
check(x, vsize('4Pi2c4P3ic' + 'P' + extras*'P'))
# function
def func(): pass
check(func, size('14P'))
Expand Down
38 changes: 1 addition & 37 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,7 @@ frame_stack_pop(PyFrameObject *f)
static void
frame_block_unwind(PyFrameObject *f)
{
assert(f->f_stackdepth >= 0);
assert(f->f_iblock > 0);
f->f_iblock--;
PyTryBlock *b = &f->f_blockstack[f->f_iblock];
intptr_t delta = f->f_stackdepth - b->b_level;
while (delta > 0) {
frame_stack_pop(f);
delta--;
}
(void)f;
}


Expand Down Expand Up @@ -845,7 +837,6 @@ _PyFrame_New_NoTrack(PyThreadState *tstate, PyFrameConstructor *con, PyObject *l
f->f_gen = NULL;
f->f_lasti = -1;
f->f_lineno = 0;
f->f_iblock = 0;
f->f_state = FRAME_CREATED;
// f_blockstack and f_localsplus initialized by frame_alloc()
return f;
Expand Down Expand Up @@ -877,33 +868,6 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
return f;
}


/* Block management */

void
PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
{
PyTryBlock *b;
if (f->f_iblock >= CO_MAXBLOCKS) {
Py_FatalError("block stack overflow");
}
b = &f->f_blockstack[f->f_iblock++];
b->b_type = type;
b->b_level = level;
b->b_handler = handler;
}

PyTryBlock *
PyFrame_BlockPop(PyFrameObject *f)
{
PyTryBlock *b;
if (f->f_iblock <= 0) {
Py_FatalError("block stack underflow");
}
b = &f->f_blockstack[--f->f_iblock];
return b;
}

/* Convert between "fast" version of locals and dictionary version.

map and values are input arguments. map is a tuple of strings.
Expand Down
38 changes: 11 additions & 27 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
assert(!_PyErr_Occurred(tstate));
#endif

main_loop:
for (;;) {
assert(stack_pointer >= f->f_valuestack); /* else underflow */
assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Expand Down Expand Up @@ -2398,7 +2397,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)

case TARGET(RETURN_VALUE): {
retval = POP();
assert(f->f_iblock == 0);
assert(EMPTY());
f->f_state = FRAME_RETURNED;
f->f_stackdepth = 0;
Expand Down Expand Up @@ -2660,10 +2658,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
goto error;
}
PyTryBlock *b = &f->f_blockstack[f->f_iblock];
PyObject *type, *value, *traceback;
_PyErr_StackItem *exc_info;
assert(STACK_LEVEL() == (b)->b_level + 4);
type = POP();
value = POP();
traceback = POP();
Expand All @@ -2683,7 +2679,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(POP_BLOCK): {
PyFrame_BlockPop(f);
/* PyFrame_BlockPop(f); */
DISPATCH();
}

Expand Down Expand Up @@ -4082,23 +4078,15 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

case TARGET(SETUP_FINALLY): {
PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
STACK_LEVEL());
DISPATCH();
}

case TARGET(SETUP_CLEANUP): {
PyFrame_BlockSetup(f, SETUP_CLEANUP, INSTR_OFFSET() + oparg,
STACK_LEVEL());
DISPATCH();
}

case TARGET(SETUP_ASYNC_WITH): {
PREDICTED(SETUP_ASYNC_WITH);
/* Setup the finally block before pushing the result
of __aenter__ on the stack. */
PyFrame_BlockSetup(f, SETUP_CLEANUP, INSTR_OFFSET() + oparg,
STACK_LEVEL()-1);
DISPATCH();
}

Expand Down Expand Up @@ -4541,27 +4529,23 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}
exception_unwind:
f->f_state = FRAME_UNWINDING;
if (f->f_iblock == 0) {
// No handlers, so exit.
break;
}
/* Pop the current block. */
PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
/* We can't use f->f_lasti here, as RERAISE may have set it */
int lasti = INSTR_OFFSET()-1;
PyTryBlock from_table = get_exception_handler(co, lasti);
assert(from_table.b_handler == b->b_handler);
assert(from_table.b_type == b->b_type);
assert(from_table.b_level == b->b_level);
assert(b->b_type == SETUP_FINALLY || b->b_type == SETUP_CLEANUP);
assert(STACK_LEVEL() >= b->b_level);
while (STACK_LEVEL() > (b)->b_level) {
if (from_table.b_handler < 0) {
// No handlers, so exit.
break;
}

assert(from_table.b_type == SETUP_FINALLY || from_table.b_type == SETUP_CLEANUP);
assert(STACK_LEVEL() >= from_table.b_level);
while (STACK_LEVEL() > from_table.b_level) {
PyObject *v = POP();
Py_XDECREF(v);
}
PyObject *exc, *val, *tb;
int handler = b->b_handler;
if (b->b_type == SETUP_CLEANUP) {
int handler = from_table.b_handler;
if (from_table.b_type == SETUP_CLEANUP) {
PyObject *lasti = PyLong_FromLong(f->f_lasti);
if (lasti == NULL) {
goto exception_unwind;
Expand Down
29 changes: 22 additions & 7 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6589,6 +6589,19 @@ label_exception_targets(basicblock *entry) {
return 0;
}


static void
convert_exception_handlers_to_nops(basicblock *entry) {
for (basicblock *b = entry; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
struct instr *instr = &b->b_instr[i];
if (is_block_push(instr) || instr->i_opcode == POP_BLOCK) {
instr->i_opcode = NOP;
}
}
}
}

static inline void
write_except_byte(struct assembler *a, int byte) {
unsigned char *p = (unsigned char *) PyBytes_AS_STRING(a->a_except_table);
Expand Down Expand Up @@ -7141,13 +7154,6 @@ assemble(struct compiler *c, int addNone)
/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(&a, c);

/* Emit code. */
for(b = entryblock; b != NULL; b = b->b_next) {
for (j = 0; j < b->b_iused; j++)
if (!assemble_emit(&a, &b->b_instr[j]))
goto error;
}
/* We need to compute stack depths before the exception table */
int maxdepth = stackdepth(c);
if (maxdepth < 0) {
goto error;
Expand All @@ -7158,6 +7164,15 @@ assemble(struct compiler *c, int addNone)
maxdepth);
goto error;
}
convert_exception_handlers_to_nops(entryblock);

/* Emit code. */
for(b = entryblock; b != NULL; b = b->b_next) {
for (j = 0; j < b->b_iused; j++)
if (!assemble_emit(&a, &b->b_instr[j]))
goto error;
}

if (!assemble_exception_table(&a)) {
return 0;
}
Expand Down