[Python-Dev] Should there be a way or API for retrieving from a code object a loader method and package file where the code comes from?

Phillip J. Eby pje at telecommunity.com
Tue Dec 23 17:19:52 CET 2008


At 06:55 AM 12/23/2008 -0500, Rocky Bernstein wrote:
>Now that there is a package mechanism (are package mechanisms?) like
>zipimporter that bundle source code into a single file, should the
>notion of a "file" location should be adjusted to include the package
>and/or importer?
>
>Is there a standard API or routine which can extract this information
>given a code object?

The inspect module (in 2.5 and up) supports retrieving the source 
lines for any object that has module globals.  So you could do it for 
a class, a function, a method, module-level code, or even a frame, 
but not for a standalone code object.

I believe there are also certain inspect module APIs that will return 
a pseudo-filename, i.e. the zipfile name followed by the path within 
the zipfile.


>Also I'm not sure there *is* a standard print string way to show
>member inside a package. zipimporter may insert co_filename strings
>like:
>
>   /usr/lib/python2.5/site-packages/tracer-0.1.0-py2.5.egg/tracer.py

AFAIK, it'll only do this if the zipfile doesn't contain a usable 
.pyc or .pyo.  Ordinarily, co_filename will be the name of the 
original source file before the zipfile was created.



More information about the Python-Dev mailing list