Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21238

Error with co_filename when loading modules from zip file

Date 2012-03-05 15:36 -0500
From Bob <bob@brasko.net>
Subject Error with co_filename when loading modules from zip file
Newsgroups comp.lang.python
Message-ID <mailman.410.1330979936.3037.python-list@python.org> (permalink)

Show all headers | View raw


Hi,

I'm using a program that distributes python in a zip
file and ran into an issue with the logging package.
It seems to return the wrong filename/line number when
loading python from a zip file. Please help!

I'm using python31, and have copied the lib directory to
  /home/user/python3.1
and have created a zip of that directory and placed it in 
  /home/user/python3.1/python31.zip

The logging package gets the filename and line number
of the calling function by looking at two variables, the filename
of the frame in the stack trace and the variable logging._srcfile.
The comparison is done in logging/__init__.py:findCaller.

In the situation above, when I run,
  PYTHONPATH=/home/user/python3.1 ./myexe run.py
I see 
  filename=/home/user/python3.1/logging/__init__.py
  _srcfile=/home/user/python3.1/logging/__init__.py
Here, filename and _srcfile are the same, so the logger correctly
outputs the filename of run.py.

When I run,
  PYTHONPATH=/home/user/python3.1/python31.zip ./myexe run.py
I see 
  filename=/home/user/python3.1/logging/__init__.py
  _srcfile=/home/user/python3.1/python31.zip/logging/__init__.py
Here, filename and _srcfile are different, so the logger incorrectly
outputs the filename of /home/user/python3.1/logging/__init__.py

I've noticed this:
  - the filename seems to be set when you compile the module
  - it seems to be set when you load the module (even after moving it)
  - it does not seem to get set when you load the module from
    the pyc in the zip file!

I've tried putting only the pyc files, only the py files
and both in the zip file.

Any help?

Thanks,
Bob

run.py:
import logging

class Handler(logging.Handler):
    def __init__(self):
        logging.Handler.__init__(self)

    def emit(self, record):
        print('message: ' + record.msg)
        print('filename: ' + record.pathname)
        print('line: ' + str(record.lineno))

logging.getLogger().addHandler(Handler())
logging.error('hi')

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Error with co_filename when loading modules from zip file Bob <bob@brasko.net> - 2012-03-05 15:36 -0500
  Re: Error with co_filename when loading modules from zip file Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2012-03-05 14:22 -0800
    Re: Error with co_filename when loading modules from zip file Bob Rossi <bob@brasko.net> - 2012-03-05 21:14 -0500
    Re: Error with co_filename when loading modules from zip file Bob Rossi <bob@brasko.net> - 2012-03-05 21:40 -0500
      Re: Error with co_filename when loading modules from zip file Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2012-03-06 02:38 -0800
        Re: Error with co_filename when loading modules from zip file Bob Rossi <bob@brasko.net> - 2012-03-06 08:41 -0500
        Re: Error with co_filename when loading modules from zip file Peter Otten <__peter__@web.de> - 2012-03-06 14:59 +0100

csiph-web