Skip to content

Commit ae1efb3

Browse files
Track counts using a local variable.
1 parent 881c798 commit ae1efb3

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

Objects/codeobject.c

+29-29
Original file line numberDiff line numberDiff line change
@@ -1728,23 +1728,22 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
17281728
assert(globalsns == NULL || PyDict_Check(globalsns));
17291729
assert(builtinsns == NULL || PyDict_Check(builtinsns));
17301730
assert(counts == NULL || counts->total == 0);
1731+
struct co_unbound_counts unbound = {0};
17311732
Py_ssize_t len = Py_SIZE(co);
17321733
for (int i = 0; i < len; i++) {
17331734
_Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i);
17341735
if (inst.op.code == LOAD_ATTR) {
17351736
int oparg = GET_OPARG(co, i, inst.op.arg);
17361737
int index = LOAD_ATTR_NAME_INDEX(oparg);
17371738
PyObject *name = GETITEM(co->co_names, index);
1738-
if (counts != NULL) {
1739-
if (PySet_Contains(attrnames, name)) {
1740-
if (_PyErr_Occurred(tstate)) {
1741-
return -1;
1742-
}
1743-
continue;
1739+
if (PySet_Contains(attrnames, name)) {
1740+
if (_PyErr_Occurred(tstate)) {
1741+
return -1;
17441742
}
1745-
counts->total += 1;
1746-
counts->numattrs += 1;
1743+
continue;
17471744
}
1745+
unbound.total += 1;
1746+
unbound.numattrs += 1;
17481747
if (PySet_Add(attrnames, name) < 0) {
17491748
return -1;
17501749
}
@@ -1753,36 +1752,37 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
17531752
int oparg = GET_OPARG(co, i, inst.op.arg);
17541753
int index = LOAD_ATTR_NAME_INDEX(oparg);
17551754
PyObject *name = GETITEM(co->co_names, index);
1756-
if (counts != NULL) {
1757-
if (PySet_Contains(globalnames, name)) {
1758-
if (_PyErr_Occurred(tstate)) {
1759-
return -1;
1760-
}
1761-
continue;
1755+
if (PySet_Contains(globalnames, name)) {
1756+
if (_PyErr_Occurred(tstate)) {
1757+
return -1;
17621758
}
1763-
counts->total += 1;
1764-
counts->globals.total += 1;
1765-
counts->globals.numunknown += 1;
1766-
if (globalsns != NULL && PyDict_Contains(globalsns, name)) {
1767-
if (_PyErr_Occurred(tstate)) {
1768-
return -1;
1769-
}
1770-
counts->globals.numglobal += 1;
1771-
counts->globals.numunknown -= 1;
1759+
continue;
1760+
}
1761+
unbound.total += 1;
1762+
unbound.globals.total += 1;
1763+
unbound.globals.numunknown += 1;
1764+
if (globalsns != NULL && PyDict_Contains(globalsns, name)) {
1765+
if (_PyErr_Occurred(tstate)) {
1766+
return -1;
17721767
}
1773-
if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) {
1774-
if (_PyErr_Occurred(tstate)) {
1775-
return -1;
1776-
}
1777-
counts->globals.numbuiltin += 1;
1778-
counts->globals.numunknown -= 1;
1768+
unbound.globals.numglobal += 1;
1769+
unbound.globals.numunknown -= 1;
1770+
}
1771+
if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) {
1772+
if (_PyErr_Occurred(tstate)) {
1773+
return -1;
17791774
}
1775+
unbound.globals.numbuiltin += 1;
1776+
unbound.globals.numunknown -= 1;
17801777
}
17811778
if (PySet_Add(globalnames, name) < 0) {
17821779
return -1;
17831780
}
17841781
}
17851782
}
1783+
if (counts != NULL) {
1784+
*counts = unbound;
1785+
}
17861786
return 0;
17871787
}
17881788

0 commit comments

Comments
 (0)