8000 gh-99955: standardize return values of functions in assembler and opt… · python/cpython@ab02262 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab02262

Browse files
authored
gh-99955: standardize return values of functions in assembler and optimizer. (#99956)
1 parent b4f3505 commit ab02262

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

Python/compile.c

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7268,12 +7268,12 @@ assemble_init(struct assembler *a, int firstlineno)
72687268
if (a->a_except_table == NULL) {
72697269
goto error;
72707270
}
7271-
return 1;
7271+
return 0;
72727272
error:
72737273
Py_XDECREF(a->a_bytecode);
72747274
Py_XDECREF(a->a_linetable);
72757275
Py_XDECREF(a->a_except_table);
7276-
return 0;
7276+
return -1;
72777277
}
72787278

72797279
static void
@@ -7667,8 +7667,9 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end, bas
76677667
{
76687668
Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
76697669
if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
7670-
if (_PyBytes_Resize(&a->a_except_table, len * 2) < 0)
7671-
return 0;
7670+
if (_PyBytes_Resize(&a->a_except_table, len * 2) < 0) {
7671+
return -1;
7672+
}
76727673
}
76737674
int size = end-start;
76747675
assert(end > start);
@@ -7683,7 +7684,7 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end, bas
76837684
assemble_emit_exception_table_item(a, size, 0);
76847685
assemble_emit_exception_table_item(a, target, 0);
76857686
assemble_emit_exception_table_item(a, depth_lasti, 0);
7686-
return 1;
7687+
return 0;
76877688
}
76887689

76897690
static int
@@ -7699,7 +7700,9 @@ assemble_exception_table(struct assembler *a, basicblock *entryblock)
76997700
struct instr *instr = &b->b_instr[i];
77007701
if (instr->i_except != handler) {
77017702
if (handler != NULL) {
7702-
RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
7703+
if (assemble_emit_exception_table_entry(a, start, ioffset, handler) < 0) {
7704+
return -1;
7705+
}
77037706
}
77047707
start = ioffset;
77057708
handler = instr->i_except;
@@ -7708,9 +7711,11 @@ assemble_exception_table(struct assembler *a, basicblock *entryblock)
77087711
}
77097712
}
77107713
if (handler != NULL) {
7711-
RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
7714+
if (assemble_emit_exception_table_entry(a, start, ioffset, handler) < 0) {
7715+
return -1;
7716+
}
77127717
}
7713-
return 1;
7718+
return 0;
77147719
}
77157720

77167721
/* Code location emitting code. See locations.md for a description of the format. */
@@ -7813,12 +7818,12 @@ write_location_info_entry(struct assembler* a, struct instr* i, int isize)
78137818
if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) {
78147819
assert(len > THEORETICAL_MAX_ENTRY_SIZE);
78157820
if (_PyBytes_Resize(&a->a_linetable, len*2) < 0) {
7816-
return 0;
7821+
return -1;
78177822
}
78187823
}
78197824
if (i->i_loc.lineno < 0) {
78207825
write_location_info_none(a, isize);
7821-
return 1;
7826+
return 0;
78227827
}
78237828
int line_delta = i->i_loc.lineno - a->a_lineno;
78247829
int column = i->i_loc.col_offset;
@@ -7829,32 +7834,32 @@ write_location_info_entry(struct assembler* a, struct instr* i, int isize)
78297834
if (i->i_loc.end_lineno == i->i_loc.lineno || i->i_loc.end_lineno == -1) {
78307835
write_location_info_no_column(a, isize, line_delta);
78317836
a->a_lineno = i->i_loc.lineno;
7832-
return 1;
7837+
return 0;
78337838
}
78347839
}
78357840
else if (i->i_loc.end_lineno == i->i_loc.lineno) {
78367841
if (line_delta == 0 && column < 80 && end_column - column < 16 && end_column >= column) {
78377842
write_location_info_short_form(a, isize, column, end_column);
7838-
return 1;
7843+
return 0;
78397844
}
78407845
if (line_delta >= 0 && line_delta < 3 && column < 128 && end_column < 128) {
78417846
write_location_info_oneline_form(a, isize, line_delta, column, end_column);
78427847
a->a_lineno = i->i_loc.lineno;
7843-
return 1;
7848+
return 0;
78447849
}
78457850
}
78467851
write_location_info_long_form(a, i, isize);
78477852
a->a_lineno = i->i_loc.lineno;
7848-
return 1;
7853+
return 0;
78497854
}
78507855

78517856
static int
78527857
assemble_emit_location(struct assembler* a, struct instr* i)
78537858
{
78547859
int isize = instr_size(i);
78557860
while (isize > 8) {
7856-
if (!write_location_info_entry(a, i, 8)) {
7857-
return 0;
7861+
if (write_location_info_entry(a, i, 8) < 0) {
7862+
return -1;
78587863
}
78597864
isize -= 8;
78607865
}
@@ -7874,15 +7879,17 @@ assemble_emit(struct assembler *a, struct instr *i)
78747879

78757880
int size = instr_size(i);
78767881
if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
7877-
if (len > PY_SSIZE_T_MAX / 2)
7878-
return 0;
7879-
if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
7880-
return 0;
7882+
if (len > PY_SSIZE_T_MAX / 2) {
7883+
return -1;
7884+
}
7885+
if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) {
7886+
return -1;
7887+
}
78817888
}
78827889
code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
78837890
a->a_offset += size;
78847891
write_instr(code, i, size);
7885-
return 1;
7892+
return 0;
78867893
}
78877894

78887895
static int
@@ -8282,17 +8289,17 @@ merge_const_one(PyObject *const_cache, PyObject **obj)
82828289
PyDict_CheckExact(const_cache);
82838290
PyObject *key = _PyCode_ConstantKey(*obj);
82848291
if (key == NULL) {
8285-
return 0;
8292+
return -1;
82868293
}
82878294

82888295
// t is borrowed reference
82898296
PyObject *t = PyDict_SetDefault(const_cache, key, key);
82908297
Py_DECREF(key);
82918298
if (t == NULL) {
8292-
return 0;
8299+
return -1;
82938300
}
82948301
if (t == key) { // obj is new constant.
8295-
return 1;
8302+
return 0;
82968303
}
82978304

82988305
if (PyTuple_CheckExact(t)) {
@@ -8301,7 +8308,7 @@ merge_const_one(PyObject *const_cache, PyObject **obj)
83018308
}
83028309

83038310
Py_SETREF(*obj, Py_NewRef(t));
8304-
return 1;
8311+
return 0;
83058312
}
83068313

83078314
// This is in codeobject.c.
@@ -8367,15 +8374,15 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
83678374
if (!names) {
83688375
goto error;
83698376
}
8370-
if (!merge_const_one(c->c_const_cache, &names)) {
8377+
if (merge_const_one(c->c_const_cache, &names) < 0) {
83718378
goto error;
83728379
}
83738380

83748381
consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */
83758382
if (consts == NULL) {
83768383
goto error;
83778384
}
8378-
if (!merge_const_one(c->c_const_cache, &consts)) {
8385+
if (merge_const_one(c->c_const_cache, &consts) < 0) {
83798386
goto error;
83808387
}
83818388

@@ -8426,7 +8433,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
84268433
goto error;
84278434
}
84288435

8429-
if (!merge_const_one(c->c_const_cache, &localsplusnames)) {
8436+
if (merge_const_one(c->c_const_cache, &localsplusnames) < 0) {
84308437
goto error;
84318438
}
84328439
con.localsplusnames = localsplusnames;
@@ -8892,45 +8899,50 @@ assemble(struct compiler *c, int addNone)
88928899
assemble_jump_offsets(g->g_entryblock);
88938900

88948901
/* Create assembler */
8895-
if (!assemble_init(&a, c->u->u_firstlineno))
8902+
if (assemble_init(&a, c->u->u_firstlineno) < 0) {
88968903
goto error;
8904+
}
88978905

88988906
/* Emit code. */
88998907
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
8900-
for (int j = 0; j < b->b_iused; j++)
8901-
if (!assemble_emit(&a, &b->b_instr[j]))
8908+
for (int j = 0; j < b->b_iused; j++) {
8909+
if (assemble_emit(&a, &b->b_instr[j]) < 0) {
89028910
goto error;
8911+
}
8912+
}
89038913
}
89048914

89058915
/* Emit location info */
89068916
a.a_lineno = c->u->u_firstlineno;
89078917
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
8908-
for (int j = 0; j < b->b_iused; j++)
8909-
if (!assemble_emit_location(&a, &b->b_instr[j]))
8918+
for (int j = 0; j < b->b_iused; j++) {
8919+
if (assemble_emit_location(&a, &b->b_instr[j]) < 0) {
89108920
goto error;
8921+
}
8922+
}
89118923
}
89128924

8913-
if (!assemble_exception_table(&a, g->g_entryblock)) {
8925+
if (assemble_exception_table(&a, g->g_entryblock) < 0) {
89148926
goto error;
89158927
}
89168928
if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
89178929
goto error;
89188930
}
8919-
if (!merge_const_one(c->c_const_cache, &a.a_except_table)) {
8931+
if (merge_const_one(c->c_const_cache, &a.a_except_table) < 0) {
89208932
goto error;
89218933
}
89228934

89238935
if (_PyBytes_Resize(&a.a_linetable, a.a_location_off) < 0) {
89248936
goto error;
89258937
}
8926-
if (!merge_const_one(c->c_const_cache, &a.a_linetable)) {
8938+
if (merge_const_one(c->c_const_cache, &a.a_linetable) < 0) {
89278939
goto error;
89288940
}
89298941

89308942
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
89318943
goto error;
89328944
}
8933-
if (!merge_const_one(c->c_const_cache, &a.a_bytecode)) {
8945+
if (merge_const_one(c->c_const_cache, &a.a_bytecode) < 0) {
89348946
goto error;
89358947
}
89368948

@@ -8995,7 +9007,7 @@ fold_tuple_on_constants(PyObject *const_cache,
89959007
}
89969008
PyTuple_SET_ITEM(newconst, i, constant);
89979009
}
8998-
if (merge_const_one(const_cache, &newconst) == 0) {
9010+
if (merge_const_one(const_cache, &newconst) < 0) {
89999011
Py_DECREF(newconst);
90009012
return -1;
90019013
}
@@ -9849,17 +9861,17 @@ remove_unused_consts(basicblock *entryblock, PyObject *consts)
98499861
return err;
98509862
}
98519863

9852-
static inline int
9864+
static inline bool
98539865
is_exit_without_lineno(basicblock *b) {
98549866
if (!basicblock_exits_scope(b)) {
9855-
return 0;
9867+
return false;
98569868
}
98579869
for (int i = 0; i < b->b_iused; i++) {
98589870
if (b->b_instr[i].i_loc.lineno >= 0) {
9859-
return 0;
9871+
return false;
98609872
}
98619873
}
9862-
return 1;
9874+
return true;
98639875
}
98649876

98659877
/* PEP 626 mandates that the f_lineno of a frame is correct

0 commit comments

Comments
 (0)
0