Additionally, you could use pyobject.Code
instead, as constructing types.CodeType
is too complex and not compatible across multiple Python versions.
The pyobject library, which can be installed via pip install pyobject
, provides a high-level wrapper for code objects.
For example:
>>> def f(a,b,c,*args):
... print(a,b,c,args)
...
>>> c=Code.fromfunc(f)
>>> c.get_flags() # automatically parse them, not by manually calculating
['OPTIMIZED', 'NEWLOCALS', 'VARARGS']
>>> c.co_flags
7
The pyobject library has greatly changed, and pyobject versions since 1.2.4 has fixed the issue. pyobject ยท Github
Note: I'm the developer of pyobject
.