@@ -1690,6 +1690,26 @@ PyCode_GetFreevars(PyCodeObject *code)
1690
1690
}
1691
1691
1692
1692
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
+
1693
1713
static int
1694
1714
identify_unbound_names (PyThreadState * tstate , PyCodeObject * co ,
1695
1715
PyObject * globalnames , PyObject * attrnames ,
@@ -1712,7 +1732,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
1712
1732
for (int i = 0 ; i < len ; i ++ ) {
1713
1733
_Py_CODEUNIT inst = _Py_GetBaseCodeUnit (co , i );
1714
1734
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 );
1716
1738
if (counts != NULL ) {
1717
1739
if (PySet_Contains (attrnames , name )) {
1718
1740
if (_PyErr_Occurred (tstate )) {
@@ -1728,7 +1750,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co,
1728
1750
}
1729
1751
}
1730
1752
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 );
1732
1756
if (counts != NULL ) {
1733
1757
if (PySet_Contains (globalnames , name )) {
1734
1758
if (_PyErr_Occurred (tstate )) {
0 commit comments