diff -r af793c7580f1 Lib/dis.py --- a/Lib/dis.py Wed Jun 17 10:09:24 2015 -0500 +++ b/Lib/dis.py Sat Jun 20 16:33:24 2015 -0400 @@ -5,6 +5,7 @@ import collections import io +import _opcode from opcode import * from opcode import __all__ as _opcodes_all @@ -76,17 +77,10 @@ # The inspect module interrogates this dictionary to build its # list of CO_* constants. It is also used by pretty_flags to # turn the co_flags field into a human readable list. -COMPILER_FLAG_NAMES = { - 1: "OPTIMIZED", - 2: "NEWLOCALS", - 4: "VARARGS", - 8: "VARKEYWORDS", - 16: "NESTED", - 32: "GENERATOR", - 64: "NOFREE", - 128: "COROUTINE", - 256: "ITERABLE_COROUTINE", -} +COMPILER_FLAG_NAMES = {} +for _name in dir(_opcode): + if _name.startswith('CO_'): + COMPILER_FLAG_NAMES[getattr(_opcode, _name)] = _name[3:] def pretty_flags(flags): """Return pretty representation of code flags.""" diff -r af793c7580f1 Lib/inspect.py --- a/Lib/inspect.py Wed Jun 17 10:09:24 2015 -0500 +++ b/Lib/inspect.py Sat Jun 20 16:33:24 2015 -0400 @@ -50,11 +50,13 @@ from operator import attrgetter from collections import namedtuple, OrderedDict +import _opcode + # Create constants for the compiler flags in Include/code.h -# We try to get them from dis to avoid duplication mod_dict = globals() -for k, v in dis.COMPILER_FLAG_NAMES.items(): - mod_dict["CO_" + v] = k +for _name in dir(_opcode): + if _name.startswith('CO_'): + mod_dict[_name] = getattr(_opcode, _name) # See Include/object.h TPFLAGS_IS_ABSTRACT = 1 << 20 diff -r af793c7580f1 Modules/_opcode.c --- a/Modules/_opcode.c Wed Jun 17 10:09:24 2015 -0500 +++ b/Modules/_opcode.c Sat Jun 20 16:33:24 2015 -0400 @@ -74,5 +74,22 @@ PyMODINIT_FUNC PyInit__opcode(void) { - return PyModule_Create(&opcodemodule); + PyObject *m; + + m = PyModule_Create(&opcodemodule); + if (m == NULL) + return NULL; + + if (PyModule_AddIntMacro(m, CO_OPTIMIZED) == -1 || + PyModule_AddIntMacro(m, CO_NEWLOCALS) == -1 || + PyModule_AddIntMacro(m, CO_VARARGS) == -1 || + PyModule_AddIntMacro(m, CO_VARKEYWORDS) == -1 || + PyModule_AddIntMacro(m, CO_NESTED) == -1 || + PyModule_AddIntMacro(m, CO_GENERATOR) == -1 || + PyModule_AddIntMacro(m, CO_NOFREE) == -1 || + PyModule_AddIntMacro(m, CO_COROUTINE) == -1 || + PyModule_AddIntMacro(m, CO_ITERABLE_COROUTINE) == -1) + return NULL; + + return m; }