Skip to content

Commit 117bfd2

Browse files
authored
Remove PyTryblock struct (GH-26059)
1 parent 78b2abc commit 117bfd2

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

Include/cpython/frameobject.h

-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ enum _framestate {
1919

2020
typedef signed char PyFrameState;
2121

22-
typedef struct {
23-
int b_type; /* what kind of block this is */
24-
int b_handler; /* where to jump to find handler */
25-
int b_level; /* value stack level to pop to */
26-
} PyTryBlock;
27-
2822
struct _frame {
2923
PyObject_VAR_HEAD
3024
struct _frame *f_back; /* previous frame, or NULL */

Python/ceval.c

+17-26
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
9595
static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
9696
static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
9797
static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
98-
static PyTryBlock get_exception_handler(PyCodeObject *, int);
98+
static int get_exception_handler(PyCodeObject *, int, int*, int*, int*);
9999

100100
#define NAME_ERROR_MSG \
101101
"name '%.200s' is not defined"
@@ -4461,21 +4461,20 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
44614461
exception_unwind:
44624462
f->f_state = FRAME_UNWINDING;
44634463
/* We can't use f->f_lasti here, as RERAISE may have set it */
4464-
int lasti = INSTR_OFFSET()-1;
4465-
PyTryBlock from_table = get_exception_handler(co, lasti);
4466-
if (from_table.b_handler < 0) {
4464+
int offset = INSTR_OFFSET()-1;
4465+
int level, handler, lasti;
4466+
if (get_exception_handler(co, offset, &level, &handler, &lasti) == 0) {
44674467
// No handlers, so exit.
44684468
break;
44694469
}
44704470

4471-
assert(STACK_LEVEL() >= from_table.b_level);
4472-
while (STACK_LEVEL() > from_table.b_level) {
4471+
assert(STACK_LEVEL() >= level);
4472+
while (STACK_LEVEL() > level) {
44734473
PyObject *v = POP();
44744474
Py_XDECREF(v);
44754475
}
44764476
PyObject *exc, *val, *tb;
4477-
int handler = from_table.b_handler;
4478-
if (from_table.b_type) {
4477+
if (lasti) {
44794478
PyObject *lasti = PyLong_FromLong(f->f_lasti);
44804479
if (lasti == NULL) {
44814480
goto exception_unwind;
@@ -4811,21 +4810,11 @@ parse_range(unsigned char *p, int *start, int*end)
48114810
return p;
48124811
}
48134812

4814-
static inline void
4815-
parse_block(unsigned char *p, PyTryBlock *block) {
4816-
int depth_and_lasti;
4817-
p = parse_varint(p, &block->b_handler);
4818-
p = parse_varint(p, &depth_and_lasti);
4819-
block->b_level = depth_and_lasti >> 1;
4820-
block->b_type = depth_and_lasti & 1;
4821-
}
4822-
48234813
#define MAX_LINEAR_SEARCH 40
48244814

4825-
static PyTryBlock
4826-
get_exception_handler(PyCodeObject *code, int index)
4815+
static int
4816+
get_exception_handler(PyCodeObject *code, int index, int *level, int *handler, int *lasti)
48274817
{
4828-
PyTryBlock res;
48294818
unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code->co_exceptiontable);
48304819
unsigned char *end = start + PyBytes_GET_SIZE(code->co_exceptiontable);
48314820
/* Invariants:
@@ -4837,8 +4826,7 @@ get_exception_handler(PyCodeObject *code, int index)
48374826
int offset;
48384827
parse_varint(start, &offset);
48394828
if (offset > index) {
4840-
res.b_handler = -1;
4841-
return res;
4829+
return 0;
48424830
}
48434831
do {
48444832
unsigned char * mid = start + ((end-start)>>1);
@@ -4862,13 +4850,16 @@ get_exception_handler(PyCodeObject *code, int index)
48624850
}
48634851
scan = parse_varint(scan, &size);
48644852
if (start_offset + size > index) {
4865-
parse_block(scan, &res);
4866-
return res;
4853+
scan = parse_varint(scan, handler);
4854+
int depth_and_lasti;
4855+
parse_varint(scan, &depth_and_lasti);
4856+
*level = depth_and_lasti >> 1;
4857+
*lasti = depth_and_lasti & 1;
4858+
return 1;
48674859
}
48684860
scan = skip_to_next_entry(scan, end);
48694861
}
4870-
res.b_handler = -1;
4871-
return res;
4862+
return 0;
48724863
}
48734864

48744865
PyFrameObject *

0 commit comments

Comments
 (0)