[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?

Rocky Bernstein rocky at gnu.org
Tue Dec 23 12:55:40 CET 2008

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?

A use case here I am thinking of here is in a stack trace or a
debugger, or a tool which wants to show in great detail information
from a code object possibly via a frame. For example does this come
from a zipped egg? And if so, which one?

For concreteness, here is what I did and here's what I saw.  Select
one of the zipimporter eggs at http://code.google.com/p/pytracer and
install one of these.

I did this on GNU/Linux and Python 2.5 and I look at the co_filename
of one of the methods:

>>> import tracer
>>> tracer.__dict__['size'].func_code.co_filename

But there is no file called "build/bdist.linux-686/egg/tracer.py" in
the filesystem. Instead there is a member "tracer.py" inside

It's possible I caused this egg to get built incorrectly or that
setuptools has a bug which entered that misleading information.
However, shouldn't there be a standard way to untangle package
location, loader and member inside the package?

As best as I can tell, PEP 302 which discussed importer hooks and
suggests a standard way to get file data. But it doesn't address a
standard way to get container package and/or loader information.

Also I'm not sure there *is* a standard print string way to show
member inside a package. zipimporter may insert co_filename strings


but the trouble with this is that it means file routines have to scan
the path and notice say that
/usr/lib/python2.5/site-packages/tracer-0.1.0-py2.5.egg is a *file*,
not a directory. And a file stat/reading routine needs to understand
what kind of packager that is in order to get tracer.py information.

(Are there any file routines in place for doing this?)


More information about the Python-Dev mailing list