[New-bugs-announce] [issue33826] enable discovery of class source code in IPython interactively defined classes

Thomas Viehmann report at bugs.python.org
Mon Jun 11 02:16:36 EDT 2018


New submission from Thomas Viehmann <tv.python.org at beamnet.de>:

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

----------
components: Interpreter Core, Library (Lib)
files: class-filename.diff
keywords: patch
messages: 319274
nosy: t-vi
priority: normal
severity: normal
status: open
title: enable discovery of class source code in IPython interactively defined classes
type: enhancement
versions: Python 3.8
Added file: https://bugs.python.org/file47638/class-filename.diff

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33826>
_______________________________________


More information about the New-bugs-announce mailing list