Skip to content

code objects: missing attributes #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
6 of 8 tasks
ep12 opened this issue Apr 3, 2020 · 2 comments
Open
6 of 8 tasks

code objects: missing attributes #1838

ep12 opened this issue Apr 3, 2020 · 2 comments
Assignees
Labels
A-stdlib C-compat A discrepancy between RustPython and CPython

Comments

@ep12
Copy link

ep12 commented Apr 3, 2020

Feature

code objects do not have the following attributes

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).

Python Documentation

cpython source code

@ep12 ep12 added the C-compat A discrepancy between RustPython and CPython label Apr 3, 2020
@ep12
Copy link
Author

ep12 commented Apr 3, 2020

Maybe also related: #1327

@coolreader18 coolreader18 self-assigned this Dec 21, 2020
@coolreader18
Copy link
Member

This should be doable now that #2355 is merged, and the co_stacksize property can be added to #2373.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stdlib C-compat A discrepancy between RustPython and CPython
Projects
None yet
Development

No branches or pull requests

3 participants