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')
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to