Message319274
Hello,
thank you for Python!
In IPython (or Jupyter), `inspect.getsource` is able to retrieve the source code for functions but not classes.
The fundamental reason is that for functions, a "filename" (input reference rather) is available via fn.__code__.co_filename, while for classes, there is no equivalent (and __module__ is __main__, so no "filename" there).
This could be helped by providing a "__filename__" (or however named) attribute in classes.
Some digging in the Python code suggests that Python/bltinmodule.c might be a good place to resolve this, namely in function builtin___build_class__. In there, there is the func object and so ((PyCodeObject*) PyFunction_GET_CODE(func))->co_filename has the filename.
Then one would copy that to an appropriate item in the namespace ns.
I do have a patch (against python 3.6), but as this is my first attempt at digging in the internals, it is probably more than just a bit raw (I see test failures in test_dbm test_lib2to3 test_pydoc test_site, at least test_pydoc is due to the patch).
Best regards
Thomas |
|
Date |
User |
Action |
Args |
2018-06-11 06:16:37 | t-vi | set | recipients:
+ t-vi |
2018-06-11 06:16:36 | t-vi | set | messageid: <1528697796.87.0.592728768989.issue33826@psf.upfronthosting.co.za> |
2018-06-11 06:16:36 | t-vi | link | issue33826 messages |
2018-06-11 06:16:36 | t-vi | create | |
|