[Distutils] Bug? zimpimporter putting in a misleading co_filename?

Paul Moore p.f.moore at gmail.com
Tue Dec 23 17:09:21 CET 2008


2008/12/23 Phillip J. Eby <pje at telecommunity.com>:
> At 10:11 AM 12/23/2008 -0500, Rocky Bernstein wrote:
>>
>> This is useful information and thanks for suggesting the workaround.
>>
>> However this does not address the issue which is not about how I can
>> create an unzipped egg  for my use or the use of others. (Or what I did or
>> didn't do to create such an zipped egg).
>>
>> I am interested in writing tools that work with both zipped an unzipped
>> eggs. To the extent that setuptools/distutils is responsible for setting the
>> co_filename field in the code object what should go inside that field? And
>> how can a programmer reliably and, if possible, uniformly untangle this to
>> get/report a location.
>
> Distutils and setuptools aren't responsible.  Python sets the co_filename to
> the original location of the source from which a file was compiled.  So one
> way to get the source file of a function is to look in its func_globals for
> a __file__, and then convert .pyc/.pyo on the extension to .py.
>
> In Python 2.5 and above, the inspect and linecache modules (among others)
> have been modified to support zipped files; they automatically handle
> retrieving the source lines via the module __loader__.get_source() method as
> well.  You may want to look at that code to see how it's handled, if you're
> needing to support 2.4 or 2.3 as well as 2.5+.

I just spotted this thread. I assume this is related to your question
on python-dev where I asked you to clarify what you were doing :-)

Phillip is correct. The inspect and linecache modules are good
examples of what you need to do. But ultimately, co_filename *could*
be completely arbitrary, and your code needs to deal gracefully with
that worst-case situation (while handling known cases as well as it
can - Nick Coghlan's mail on python-dev gives you the basic steps for
that).

Paul.


More information about the Distutils-SIG mailing list