diff -r af793c7580f1 Lib/dis.py --- a/Lib/dis.py Wed Jun 17 10:09:24 2015 -0500 +++ b/Lib/dis.py Fri Jun 19 13:21:43 2015 -0400 @@ -73,20 +73,17 @@ while tb.tb_next: tb = tb.tb_next disassemble(tb.tb_frame.f_code, tb.tb_lasti, file=file) + # 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 = {} +_code = (lambda:None).__code__ +for _name in {"OPTIMIZED", "NEWLOCALS", "VARARGS", "VARKEYWORDS", + "NESTED", "GENERATOR", "NOFREE", "COROUTINE", + "ITERABLE_COROUTINE"}: + COMPILER_FLAG_NAMES[getattr(_code, 'CO_' + _name)] = _name + def pretty_flags(flags): """Return pretty representation of code flags.""" diff -r af793c7580f1 Objects/codeobject.c --- a/Objects/codeobject.c Wed Jun 17 10:09:24 2015 -0500 +++ b/Objects/codeobject.c Fri Jun 19 13:21:43 2015 -0400 @@ -224,6 +224,40 @@ {NULL} /* Sentinel */ }; + +#define CODE_CO_GETTER(NAME) \ + static PyObject* \ + code_get_##NAME(PyCodeObject* code) { \ + return PyLong_FromLong(NAME); \ + } + +#define CODE_CO_DESCR(NAME) \ + {#NAME, (getter)code_get_##NAME, NULL, NULL} + +CODE_CO_GETTER(CO_OPTIMIZED); +CODE_CO_GETTER(CO_NEWLOCALS); +CODE_CO_GETTER(CO_VARARGS); +CODE_CO_GETTER(CO_VARKEYWORDS); +CODE_CO_GETTER(CO_NESTED); +CODE_CO_GETTER(CO_GENERATOR); +CODE_CO_GETTER(CO_NOFREE); +CODE_CO_GETTER(CO_COROUTINE); +CODE_CO_GETTER(CO_ITERABLE_COROUTINE); + +static PyGetSetDef code_getsetlist[] = { + CODE_CO_DESCR(CO_OPTIMIZED), + CODE_CO_DESCR(CO_NEWLOCALS), + CODE_CO_DESCR(CO_VARARGS), + CODE_CO_DESCR(CO_VARKEYWORDS), + CODE_CO_DESCR(CO_NESTED), + CODE_CO_DESCR(CO_GENERATOR), + CODE_CO_DESCR(CO_NOFREE), + CODE_CO_DESCR(CO_COROUTINE), + CODE_CO_DESCR(CO_ITERABLE_COROUTINE), + {NULL} /* Sentinel */ +}; + + /* Helper for code_new: return a shallow copy of a tuple that is guaranteed to contain exact strings, by converting string subclasses to exact strings and complaining if a non-string is found. */ @@ -531,7 +565,7 @@ 0, /* tp_iternext */ code_methods, /* tp_methods */ code_memberlist, /* tp_members */ - 0, /* tp_getset */ + code_getsetlist, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */