This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author t-vi
Recipients t-vi
Date 2018-06-11.06:16:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1528697796.87.0.592728768989.issue33826@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2018-06-11 06:16:37t-visetrecipients: + t-vi
2018-06-11 06:16:36t-visetmessageid: <1528697796.87.0.592728768989.issue33826@psf.upfronthosting.co.za>
2018-06-11 06:16:36t-vilinkissue33826 messages
2018-06-11 06:16:36t-vicreate