[Python-Dev] PEP: __source__ proposal

holger krekel hpk at trillke.net
Fri Dec 3 13:36:16 CET 2004

Hi Stelios, 

[Stelios Xanthakis Fri, Dec 03, 2004 at 11:54:25AM +0200]
> Abstract
>     This PEP suggests the implementation of __source__ attribute for
>     functions and classes.  The attribute is a read-only string which
>     is generated by the parser and is a copy of the original source
>     code of the function/class (including comments, indentation and
>     whitespace).

I've had similar ideas in the past as we are doing dynamic
code generation in PyPy, as well as in other projects.  After
some discussion with Armin i think there is another
possibility for storing "source" or any other such meta information
with code/module objects: make __file__ and co_filename
instances of a subclass of 'str' providing an extra attribute.
For a simple example, they could have a 'source' attribute, which 
could be tried first by appropriate inspect functions and traceback 
related functionality.  

We are about to test out this approach with the py lib 
(http://codespeak.net/py) and want to have it work for 
for Python 2.2, 2.3. and 2.4.  I suspect there may be some 
issues lurking (also in your proposed PEP) especially with 
respect to encodings.  Also we have some use cases where we 
want to retrieve source code from non-local locations and
want to integrate this seemlessly with the introspection 
facilities of Python which obviously is an important part 
of the equation.  I can report back if there is interest. 



More information about the Python-Dev mailing list