Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > linecache vs egg - reading line of a file in an egg

Reply
Thread Tools

linecache vs egg - reading line of a file in an egg

 
 
R. Bernstein
Guest
Posts: n/a
 
      12-21-2008
Does linecache work with source in Python eggs? If not, is it
contemplated that this is going to be fixed or is there something else
like linecache that currently works?

Right now, I think pdb and pydb (and probably other debuggers) are
broken when they try to trace into code that is part of an egg.

Here's what I tried recently:

Using this egg:
http://pypi.python.org/packages/2.5/....1.0-py2.5.egg

I install that and look for the filename of one of the
functions. Here's a sample session:

>>> import tracer
>>> tracer

<module 'tracer' from '/usr/lib/python2.5/site-packages/tracer-0.1.0-py2.5.egg/tracer.pyc'>
>>> tracer.size

<function size at 0xb7c39a74>
>>> tracer.size.func_code.co_filename

'build/bdist.linux-i686/egg/tracer.py'
>>> tracer.size.func_code.co_firstlineno

216
>>>


To read the source for tracer.py, information from "Accessing Package
Resources"
(http://peak.telecommunity.com/DevCen...kage-resources)
suggests:

>>> from pkg_resources import resource_string
>>> print resource_string('tracer', 'tracer.py')


This gives me one long string which I can split and then index.

Note that I used "tracer.py" above, not
"build/bdist.linux-8686/egg/tracer.py" How do tracebacks and things that
read frame information deal with the discrepency?

Before reinventing the wheel and trying to extend linecache to do
something like the above, has someone already come up with a solution?

Thanks




 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      12-21-2008
R. Bernstein wrote:
> Does linecache work with source in Python eggs? If not, is it
> contemplated that this is going to be fixed or is there something else
> like linecache that currently works?


linecache works with eggs and other zipped Python source, but it had to extend
the API in order to do so. Some of the debuggers don't use the extended API.
This will be fixed in the next 2.6.x bugfix release, but not in 2.5.3.

http://bugs.python.org/issue4201

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      12-21-2008
On Sat, Dec 20, 2008 at 7:18 PM, R. Bernstein <(E-Mail Removed)> wrote:
> Does linecache work with source in Python eggs? If not, is it
> contemplated that this is going to be fixed or is there something else
> like linecache that currently works?


I believe it already does. FYI, eggs are just zip files with a certain
file/directory structure.
And linecache seems to handle reading from zipfile modules. Ergo,
linecache can read source from eggs.

Quoting from the linecache docs
(http://docs.python.org/library/linecache.html), emphasis mine:

linecache.getline(filename, lineno[, module_globals])
<snip>
If a file named filename is not found, the function will ***look
for it in the module search path, sys.path***, after first checking
for a PEP 302 __loader__ in module_globals, in case the module was
***imported from a zipfile*** or other non-filesystem import source.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
R. Bernstein
Guest
Posts: n/a
 
      12-21-2008
Robert Kern <(E-Mail Removed)> writes:

> R. Bernstein wrote:
>> Does linecache work with source in Python eggs? If not, is it
>> contemplated that this is going to be fixed or is there something else
>> like linecache that currently works?

>
> linecache works with eggs and other zipped Python source, but it had
> to extend the API in order to do so. Some of the debuggers don't use
> the extended API. This will be fixed in the next 2.6.x bugfix release,
> but not in 2.5.3.


Ok. I have committed a change in pydb sources to deal with the 2 and 3
argument linecache.getline interface which should cover Python
releases both before and after version 2.5.

>
> http://bugs.python.org/issue4201


Many thanks! I should have dug deeper myself.

For pdb/bdb though isn't there still a problem in reporting the file
location? There is that weird "build" name that seems to be stored in
func_code.co_filename mentioned in the original post.

I just tried patching pdb/bdb along from the current 2.6 svn sources
and here is what I see:

$ pdb /tmp/lc.py
> /tmp/lc.py(1)<module>()

-> import tracer
(Pdb) s
--Call--
> /src/external-vcs/python/build/bdist.linux-i686/egg/tracer.py(6)<module>()


The above filename is wrong. It's very possible I did something wrong,
so I'd be grateful if someone else double checked.

Furthermore, if there is a problem I'm not sure I see how to fix this.

I can think of heuristics to tell if module lives an inside an egg,
but is there a reliable way? Is there a standard convention for
reporting a file location inside of an egg?

Thanks again.

>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
> that is made terrible by our own mad attempt to interpret it as though it had
> an underlying truth."
> -- Umberto Eco

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone use LineCache::remap_file or LineCache::unmap_file? rocky Ruby 0 10-23-2009 09:12 PM
speed up linecache.getline() bbarbero@inescporto.pt Python 1 10-13-2009 02:32 PM
linecache and glob jo3c Python 5 01-08-2008 03:26 AM
round-trip from egg to code and back to egg Catherine Python 2 10-08-2007 10:20 PM
linecache and comparison with input Ross Hetherington Python 2 06-30-2007 02:32 PM



Advertisments