Possible bug and work around for Py2Exe / linecache module

If someone already knows about this I’m going to be very unhappy.

But I think it’s cool that I didn’t know about it, found out about it,

and solved it for myself.

There isn’t a whole lot of activity on the Py2Exe mailing list, and

that is where this “bug” or whatever belong, but here goes anyhow.

On the program I’ve been working on I use the module linecache.

I use the “import linecache” in my program and as a Py file the

program works great. As an EXE it doesn’t work. The line(s)

being read don’t get stored in the cache that linecache creates.

Ultimately I get an empty string the “” thing. I guess it is a value

of null.

I won’t go into the every step I took to work around this but the

shortest story I can tell is that I copied linecache to my programs

directory, changed the name of the module to “lc”, the import to lc

and every instance of linecache.readline / linecache.clearcache

to lc.readline / lc.clearcache, and ran Py2Exe, and POW! It works

like a champ.

So the bug / problem is that linecache didn’t get imported as it

should have or something to that effect.

Hope that this helps someone, and if you all already knew about it

I’m sorry for being late to the party…

Thanks gang, you are all great!

Steve

This reminds me of the problems I had with peak (similar “module copy inside trunk” solution)

The better solution turned out to be adding some code for manual module inclusion at the beginning of setup.py . It goes something like this:

#============= begin code
import sys
sys.argv.append(‘py2exe’)
sys.argv.append(‘-b1’)

try:
# if this doesn’t work, try import modulefinder
import py2exe.mf as modulefinder

import peak
for p in peak.__path__[1:]:
    modulefinder.AddPackagePath("peak", p)

for extra in ["peak.events", 'peak.util']: #,"win32com.mapi"
    __import__(extra)

    m = sys.modules[extra]
    for p in m.__path__[1:]:
        modulefinder.AddPackagePath(extra, p)

except ImportError:
# no build path setup, no worries.
pass

from distutils.core import setup

import py2exe
#================end code

In the above code I added the “peak” module and the “events” and “util” submodules.
Maybe this works for "
linecache" too.

Peter

···

On Sat, Aug 16, 2008 at 9:10 AM, Steve Freedenburg stevefreedenburg@charter.net wrote:

If someone already knows about this I’m going to be very unhappy.

But I think it’s cool that I didn’t know about it, found out about it,

and solved it for myself.

There isn’t a whole lot of activity on the Py2Exe mailing list, and

that is where this “bug” or whatever belong, but here goes anyhow.

On the program I’ve been working on I use the module linecache.

I use the “import linecache” in my program and as a Py file the

program works great. As an EXE it doesn’t work. The line(s)

being read don’t get stored in the cache that linecache creates.

Ultimately I get an empty string the “” thing. I guess it is a value

of null.

I won’t go into the every step I took to work around this but the

shortest story I can tell is that I copied linecache to my programs

directory, changed the name of the module to “lc”, the import to lc

and every instance of linecache.readline / linecache.clearcache

to lc.readline / lc.clearcache, and ran Py2Exe, and POW! It works

like a champ.

So the bug / problem is that linecache didn’t get imported as it

should have or something to that effect.

Hope that this helps someone, and if you all already knew about it

I’m sorry for being late to the party…

Thanks gang, you are all great!

Steve


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


There is NO FATE, we are the creators.

Steve Freedenburg wrote:

If someone already knows about this I'm going to be very unhappy.
But I think it's cool that I didn't know about it, found out about it,
and solved it for myself.
There isn't a whole lot of activity on the Py2Exe mailing list, and
that is where this "bug" or whatever belong, but here goes anyhow.
On the program I've been working on I use the module linecache.
I use the "import linecache" in my program and as a Py file the
program works great. As an EXE it doesn't work. The line(s)
being read don't get stored in the cache that linecache creates.
Ultimately I get an empty string the "" thing. I guess it is a value
of null.
I won't go into the every step I took to work around this but the
shortest story I can tell is that I copied linecache to my programs
directory, changed the name of the module to "lc", the import to lc
and every instance of linecache.readline / linecache.clearcache
to lc.readline / lc.clearcache, and ran Py2Exe, and POW! It works
like a champ.
So the bug / problem is that linecache didn't get imported as it
should have or something to that effect.

This might be related to something I discovered a few months ago. Py2exe replaces (monkey-patches) part of the linecache module in order to override (disable actually) part of the functionality, but it's done in a way that can potentially have problems. Basically the getline function takes two or three parameters, but the one that py2exe uses to replace it only accepts two. So if anything tries to call it with the 3rd parameter will raise an error.

I have this code in one of my projects, and it's enabled by using the custom_boot_script py2exe option so it runs early enough in the load process to make a difference:

# Py2exe's boot script replaces linecache.getline, but does it with a
# function that only accepts two args. This works for lots of things,
# but there is an optional 3rd arg that is missing and causes an
# exception when the code uses the traceback module because it tries
# to pass the 3rd arg.

import linecache
def fake_getline(*args):
     return ''
linecache.orig_getline = linecache.getline
linecache.getline = fake_getline

# We'll also replace getlines
def fake_getlines(*args):
     return
linecache.orig_getlines = linecache.getlines
linecache.getlines = fake_getlines

···

Hope that this helps someone, and if you all already knew about it
I'm sorry for being late to the party...
Thanks gang, you are all great!
Steve

------------------------------------------------------------------------

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn schrieb:

Steve Freedenburg wrote:

If someone already knows about this I'm going to be very unhappy.
But I think it's cool that I didn't know about it, found out about it,
and solved it for myself.

There isn't a whole lot of activity on the Py2Exe mailing list, and
that is where this "bug" or whatever belong, but here goes anyhow.

On the program I've been working on I use the module linecache.
I use the "import linecache" in my program and as a Py file the
program works great. As an EXE it doesn't work. The line(s)
being read don't get stored in the cache that linecache creates.
Ultimately I get an empty string the "" thing. I guess it is a value
of null.

I won't go into the every step I took to work around this but the
shortest story I can tell is that I copied linecache to my programs
directory, changed the name of the module to "lc", the import to lc
and every instance of linecache.readline / linecache.clearcache
to lc.readline / lc.clearcache, and ran Py2Exe, and POW! It works
like a champ.

So the bug / problem is that linecache didn't get imported as it
should have or something to that effect.

This might be related to something I discovered a few months ago.
Py2exe replaces (monkey-patches) part of the linecache module in order
to override (disable actually) part of the functionality, but it's done
in a way that can potentially have problems. Basically the getline
function takes two or three parameters, but the one that py2exe uses to
replace it only accepts two. So if anything tries to call it with the
3rd parameter will raise an error.

I have this code in one of my projects, and it's enabled by using the
custom_boot_script py2exe option so it runs early enough in the load
process to make a difference:

# Py2exe's boot script replaces linecache.getline, but does it with a
# function that only accepts two args. This works for lots of things,
# but there is an optional 3rd arg that is missing and causes an
# exception when the code uses the traceback module because it tries
# to pass the 3rd arg.

import linecache
def fake_getline(*args):
     return ''
linecache.orig_getline = linecache.getline
linecache.getline = fake_getline

# We'll also replace getlines
def fake_getlines(*args):
     return
linecache.orig_getlines = linecache.getlines
linecache.getlines = fake_getlines

The current code in the latest release of py2exe is this:

"""
# Disable linecache.getline() which is called by
# traceback.extract_stack() when an exception occurs to try and read
# the filenames embedded in the packaged python code. This is really
# annoying on windows when the d: or e: on our build box refers to
# someone elses removable or network drive so the getline() call
# causes it to ask them to insert a disk in that drive.
import linecache
def fake_getline(filename, lineno, module_globals=None):
    return ''
linecache.orig_getline = linecache.getline
linecache.getline = fake_getline

del linecache, fake_getline
"""

So the bug that getline accepting only two args is already fixed. I did not
know that getlines did also change (from Python 2.4 to Python 2.5); but
if these bugs are never reported to the py2exe list or tracker they of
course never get fixed.

I think it would be great if py2exe project would get more active maintainers.

···

--
Thanks,
Thomas