Skip to content

Commit ecb073d

Browse files
committed
bpo-42217: Merge co_code and co_lnotab
1 parent d6238ba commit ecb073d

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

Python/compile.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,11 +5847,9 @@ compute_code_flags(struct compiler *c)
58475847
// Merge *tuple* with constant cache.
58485848
// Unlike merge_consts_recursive(), this function doesn't work recursively.
58495849
static int
5850-
merge_const_tuple(struct compiler *c, PyObject **tuple)
5850+
merge_const_one(struct compiler *c, PyObject **obj)
58515851
{
5852-
assert(PyTuple_CheckExact(*tuple));
5853-
5854-
PyObject *key = _PyCode_ConstantKey(*tuple);
5852+
PyObject *key = _PyCode_ConstantKey(*obj);
58555853
if (key == NULL) {
58565854
return 0;
58575855
}
@@ -5862,14 +5860,18 @@ merge_const_tuple(struct compiler *c, PyObject **tuple)
58625860
if (t == NULL) {
58635861
return 0;
58645862
}
5865-
if (t == key) { // tuple is new constant.
5863+
if (t == key) { // obj is new constant.
58665864
return 1;
58675865
}
58685866

5869-
PyObject *u = PyTuple_GET_ITEM(t, 1);
5870-
Py_INCREF(u);
5871-
Py_DECREF(*tuple);
5872-
*tuple = u;
5867+
if (PyTuple_CheckExact(t)) {
5868+
// t is still borrowed reference
5869+
t = PyTuple_GET_ITEM(t, 1);
5870+
}
5871+
5872+
Py_INCREF(t);
5873+
Py_DECREF(*obj);
5874+
*obj = t;
58735875
return 1;
58745876
}
58755877

@@ -5899,10 +5901,10 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
58995901
if (!freevars)
59005902
goto error;
59015903

5902-
if (!merge_const_tuple(c, &names) ||
5903-
!merge_const_tuple(c, &varnames) ||
5904-
!merge_const_tuple(c, &cellvars) ||
5905-
!merge_const_tuple(c, &freevars))
5904+
if (!merge_const_one(c, &names) ||
5905+
!merge_const_one(c, &varnames) ||
5906+
!merge_const_one(c, &cellvars) ||
5907+
!merge_const_one(c, &freevars))
59065908
{
59075909
goto error;
59085910
}
@@ -5919,7 +5921,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
59195921
if (consts == NULL) {
59205922
goto error;
59215923
}
5922-
if (!merge_const_tuple(c, &consts)) {
5924+
if (!merge_const_one(c, &consts)) {
59235925
Py_DECREF(consts);
59245926
goto error;
59255927
}
@@ -6041,10 +6043,18 @@ assemble(struct compiler *c, int addNone)
60416043
goto error;
60426044
}
60436045

6044-
if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6046+
if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
60456047
goto error;
6046-
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
6048+
}
6049+
if (!merge_const_one(c, &a.a_lnotab)) {
60476050
goto error;
6051+
}
6052+
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
6053+
goto error;
6054+
}
6055+
if (!merge_const_one(c, &a.a_bytecode)) {
6056+
goto error;
6057+
}
60486058

60496059
co = makecode(c, &a, consts);
60506060
error:

0 commit comments

Comments
 (0)