code objects: missing attributes
Feature
code objects do not have the following attributes
-
co_varnames: typing.Tuple[str, ...](see this issue, Add json output for CPython tests #1834) -
co_names: typing.Tuple[str, ...]Addco_namestoPyCode#4177 -
co_cellvars: typing.Tuple[str, ...]Add co_cellvars to code object #4507 -
co_freevars: typing.Tuple[str, ...]Add co_freevars to code object #4509 -
co_nlocals: int(=len(self.co_varnames)?) Add co_nlocals to code object #4553 -
co_stacksize: intAdd co_stacksize to code objects #4554 -
co_code: bytes(only useful if the bytecode is cpython compatible) -
co_lnotab: bytes(line number <-> byte code index)
Maybe it does not make sense to implement the last two or three items (as far as I know the bytecode is not cpython compatible?). But the other ones can be implemented, I think.
I wish I'd be able to do that, but the only language I really speak is python ๐.
Example
# cpython 3.8 import inspect from pprint import pprint def code_info(code): return {(k1:=f'co_{k}'): getattr(code, k1) for k in ('names', 'varnames', 'cellvars', 'freevars')} def foo(arg1, arg2=None): x_foo = 42 def bar(arg_bar): x_bar = 73 return arg_bar * x_bar, x_foo, arg2 return bar, inspect.currentframe() bar_func, f = foo(1) pprint(code_info(f.f_code)) pprint(code_info(bar_func.__code__))
should print
{'co_names': ('inspect', 'currentframe'),
'co_varnames': ('arg1', 'arg2', 'bar'),
'co_cellvars': ('arg2', 'x_foo'),
'co_freevars': ()}
{'co_names': (),
'co_varnames': ('arg_bar', 'x_bar'),
'co_cellvars': (),
'co_freevars': ('arg2', 'x_foo')}
Note that with #1834 f.f_code.co_varnames is ('arg1', 'arg2') (bar missing) and bar_func.__code__.co_varnames is ('arg_bar',) (x_bar missing).