Skip to content

Commit 881c798

Browse files
Add some helper macros.
1 parent 1e6aa82 commit 881c798

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Objects/codeobject.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,26 @@ PyCode_GetFreevars(PyCodeObject *code)
16901690
}
16911691

16921692

1693+
#define GET_OPARG(co, i, initial) (initial)
1694+
// We may want to move these macros to pycore_opcode_utils.h
1695+
// and use them in Python/bytecodes.c.
1696+
#define LOAD_GLOBAL_NAME_INDEX(oparg) ((oparg)>>1)
1697+
#define LOAD_ATTR_NAME_INDEX(oparg) ((oparg)>>1)
1698+
1699+
#ifndef Py_DEBUG
1700+
#define GETITEM(v, i) PyTuple_GET_ITEM((v), (i))
1701+
#else
1702+
static inline PyObject *
1703+
GETITEM(PyObject *v, Py_ssize_t i)
1704+
{
1705+
assert(PyTuple_Check(v));
1706+
assert(i >= 0);
1707+
assert(i < PyTuple_GET_SIZE(v));
1708+
assert(PyTuple_GET_ITEM(v, i) != NULL);
1709+
return PyTuple_GET_ITEM(v, i);
1710+
}
1711+
#endif
1712+
16931713
static int
16941714
identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
16951715
PyObject *globalnames, PyObject *attrnames,
@@ -1712,7 +1732,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
17121732
for (int i = 0; i < len; i++) {
17131733
_Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i);
17141734
if (inst.op.code == LOAD_ATTR) {
1715-
PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1);
1735+
int oparg = GET_OPARG(co, i, inst.op.arg);
1736+
int index = LOAD_ATTR_NAME_INDEX(oparg);
1737+
PyObject *name = GETITEM(co->co_names, index);
17161738
if (counts != NULL) {
17171739
if (PySet_Contains(attrnames, name)) {
17181740
if (_PyErr_Occurred(tstate)) {
@@ -1728,7 +1750,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
17281750
}
17291751
}
17301752
else if (inst.op.code == LOAD_GLOBAL) {
1731-
PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1);
1753+
int oparg = GET_OPARG(co, i, inst.op.arg);
1754+
int index = LOAD_ATTR_NAME_INDEX(oparg);
1755+
PyObject *name = GETITEM(co->co_names, index);
17321756
if (counts != NULL) {
17331757
if (PySet_Contains(globalnames, name)) {
17341758
if (_PyErr_Occurred(tstate)) {

0 commit comments

Comments
 (0)