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

Discussion in 'Python' started by R. Bernstein, Dec 21, 2008.

  1. R. Bernstein

    R. Bernstein Guest

    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/t/tracer/tracer-0.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/DevCenter/PythonEggs#accessing-package-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
    R. Bernstein, Dec 21, 2008
    #1
    1. Advertising

  2. R. Bernstein

    Robert Kern Guest

    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
    Robert Kern, Dec 21, 2008
    #2
    1. Advertising

  3. R. Bernstein

    Chris Rebert Guest

    On Sat, Dec 20, 2008 at 7:18 PM, 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?


    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
    Chris Rebert, Dec 21, 2008
    #3
  4. R. Bernstein

    R. Bernstein Guest

    Robert Kern <> 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
    R. Bernstein, Dec 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ross Hetherington

    linecache and comparison with input

    Ross Hetherington, Jun 30, 2007, in forum: Python
    Replies:
    2
    Views:
    354
    Ross Hetherington
    Jun 30, 2007
  2. Catherine
    Replies:
    2
    Views:
    318
    John Nagle
    Oct 8, 2007
  3. jo3c

    linecache and glob

    jo3c, Jan 4, 2008, in forum: Python
    Replies:
    5
    Views:
    487
  4. Replies:
    1
    Views:
    565
    Peter Otten
    Oct 13, 2009
  5. rocky
    Replies:
    0
    Views:
    106
    rocky
    Oct 23, 2009
Loading...

Share This Page