Skip to content

Commit 2dd33ae

Browse files
committed
Address Serhiy's review
1 parent 53e6f41 commit 2dd33ae

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

Python/assemble.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define ERROR -1
1919

2020
#define RETURN_IF_ERROR(X) \
21-
if ((X) == -1) { \
21+
if ((X) < 0) { \
2222
return ERROR; \
2323
}
2424

@@ -448,13 +448,17 @@ static PyObject *
448448
dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
449449
{
450450
PyObject *tuple, *k, *v;
451-
Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
451+
Py_ssize_t pos = 0, size = PyDict_GET_SIZE(dict);
452452

453453
tuple = PyTuple_New(size);
454454
if (tuple == NULL)
455455
return NULL;
456456
while (PyDict_Next(dict, &pos, &k, &v)) {
457-
i = PyLong_AS_LONG(v);
457+
Py_ssize_t i = PyLong_AsSsize_t(v);
458+
if (i == -1 && PyErr_Occurred()) {
459+
Py_DECREF(tuple);
460+
return NULL;
461+
}
458462
assert((i - offset) < size);
459463
assert((i - offset) >= 0);
460464
PyTuple_SET_ITEM(tuple, i - offset, Py_NewRef(k));
@@ -473,21 +477,27 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
473477
PyObject *k, *v;
474478
Py_ssize_t pos = 0;
475479
while (PyDict_Next(umd->u_varnames, &pos, &k, &v)) {
476-
int offset = (int)PyLong_AS_LONG(v);
480+
int offset = _PyLong_AsInt(v);
481+
if (offset == -1 && PyErr_Occurred()) {
482+
return ERROR;
483+
}
477484
assert(offset >= 0);
478485
assert(offset < nlocalsplus);
486+
479487
// For now we do not distinguish arg kinds.
480488
_PyLocals_Kind kind = CO_FAST_LOCAL;
481-
if (PyDict_Contains(umd->u_fasthidden, k)) {
489+
int has_key = PyDict_Contains(umd->u_fasthidden, k);
490+
RETURN_IF_ERROR(has_key);
491+
if (has_key) {
482492
kind |= CO_FAST_HIDDEN;
483493
}
484-
int has_cell = PyDict_Contains(umd->u_cellvars, k);
485-
if (has_cell < 0) {
486-
return -1;
487-
}
488-
if (has_cell) {
494+
495+
has_key = PyDict_Contains(umd->u_cellvars, k);
496+
RETURN_IF_ERROR(has_key);
497+
if (has_key) {
489498
kind |= CO_FAST_CELL;
490499
}
500+
491501
_Py_set_localsplus_info(offset, k, kind, names, kinds);
492502
}
493503
int nlocals = (int)PyDict_GET_SIZE(umd->u_varnames);
@@ -497,15 +507,17 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
497507
pos = 0;
498508
while (PyDict_Next(umd->u_cellvars, &pos, &k, &v)) {
499509
int has_name = PyDict_Contains(umd->u_varnames, k);
500-
if (has_name < 0) {
501-
return -1;
502-
}
510+
RETURN_IF_ERROR(has_name);
503511
if (has_name) {
504512
// Skip cells that are already covered by locals.
505513
numdropped += 1;
506514
continue;
507515
}
508-
int offset = (int)PyLong_AS_LONG(v);
516+
517+
int offset = _PyLong_AsInt(v);
518+
if (offset == -1 && PyErr_Occurred()) {
519+
return ERROR;
520+
}
509521
assert(offset >= 0);
510522
offset += nlocals - numdropped;
511523
assert(offset < nlocalsplus);
@@ -514,13 +526,16 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
514526

515527
pos = 0;
516528
while (PyDict_Next(umd->u_freevars, &pos, &k, &v)) {
517-
int offset = (int)PyLong_AS_LONG(v);
529+
int offset = _PyLong_AsInt(v);
530+
if (offset == -1 && PyErr_Occurred()) {
531+
return ERROR;
532+
}
518533
assert(offset >= 0);
519534
offset += nlocals - numdropped;
520535
assert(offset < nlocalsplus);
521536
_Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds);
522537
}
523-
return 0;
538+
return SUCCESS;
524539
}
525540

526541
static PyCodeObject *
@@ -566,7 +581,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
566581
goto error;
567582
}
568583
if (compute_localsplus_info(umd, nlocalsplus,
569-
localsplusnames, localspluskinds) < 0) {
584+
localsplusnames, localspluskinds) == ERROR) {
570585
goto error;
571586
}
572587

Python/compile.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,7 +4201,7 @@ compiler_nameop(struct compiler *c, location loc,
42014201
PyObject *item;
42024202
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
42034203
&item) < 0) {
4204-
return ERROR;
4204+
goto error;
42054205
}
42064206
if (item == Py_True) {
42074207
optype = OP_FAST;
@@ -4232,15 +4232,15 @@ compiler_nameop(struct compiler *c, location loc,
42324232
op = LOAD_FROM_DICT_OR_DEREF;
42334233
// First load the locals
42344234
if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) {
4235-
return ERROR;
4235+
goto error;
42364236
}
42374237
}
42384238
else if (c->u->u_ste->ste_can_see_class_scope) {
42394239
op = LOAD_FROM_DICT_OR_DEREF;
42404240
// First load the classdict
42414241
if (compiler_addop_o(c->u, loc, LOAD_DEREF,
42424242
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4243-
return ERROR;
4243+
goto error;
42444244
}
42454245
}
42464246
else {
@@ -4267,7 +4267,7 @@ compiler_nameop(struct compiler *c, location loc,
42674267
// First load the classdict
42684268
if (compiler_addop_o(c->u, loc, LOAD_DEREF,
42694269
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4270-
return ERROR;
4270+
goto error;
42714271
}
42724272
} else {
42734273
op = LOAD_GLOBAL;
@@ -4301,6 +4301,10 @@ compiler_nameop(struct compiler *c, location loc,
43014301
arg <<= 1;
43024302
}
43034303
return codegen_addop_i(INSTR_SEQUENCE(c), op, arg, loc);
4304+
4305+
error:
4306+
Py_DECREF(mangled);
4307+
return ERROR;
43044308
}
43054309

43064310
static int

Python/flowgraph.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,19 +2406,29 @@ build_cellfixedoffsets(_PyCompile_CodeUnitMetadata *umd)
24062406
while (PyDict_Next(umd->u_cellvars, &pos, &varname, &cellindex)) {
24072407
PyObject *varindex;
24082408
if (PyDict_GetItemRef(umd->u_varnames, varname, &varindex) < 0) {
2409-
return NULL;
2409+
goto error;
24102410
}
2411-
if (varindex != NULL) {
2412-
assert(PyLong_AS_LONG(cellindex) < INT_MAX);
2413-
assert(PyLong_AS_LONG(varindex) < INT_MAX);
2414-
int oldindex = (int)PyLong_AS_LONG(cellindex);
2415-
int argoffset = (int)PyLong_AS_LONG(varindex);
2416-
Py_DECREF(varindex);
2417-
fixed[oldindex] = argoffset;
2411+
if (varindex == NULL) {
2412+
continue;
24182413
}
2419-
}
24202414

2415+
int argoffset = _PyLong_AsInt(varindex);
2416+
Py_DECREF(varindex);
2417+
if (argoffset == -1 && PyErr_Occurred()) {
2418+
goto error;
2419+
}
2420+
2421+
int oldindex = _PyLong_AsInt(cellindex);
2422+
if (oldindex == -1 && PyErr_Occurred()) {
2423+
goto error;
2424+
}
2425+
fixed[oldindex] = argoffset;
2426+
}
24212427
return fixed;
2428+
2429+
error:
2430+
PyMem_Free(fixed);
2431+
return NULL;
24222432
}
24232433

24242434
#define IS_GENERATOR(CF) \

0 commit comments

Comments
 (0)