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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <bob@brasko.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'help?': 0.03; 'skip:p 40': 0.04; 'subject:Error': 0.05; 'run,': 0.07; 'skip:/ 40': 0.07; 'subject:when': 0.07; 'python': 0.08; 'filename': 0.09; 'subject:modules': 0.09; 'variables,': 0.09; 'package.': 0.12; 'def': 0.13; 'subject:file': 0.13; 'different,': 0.16; 'outputs': 0.16; 'this:': 0.16; 'loading': 0.18; 'trace': 0.18; 'thanks,': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.21; 'stack': 0.24; 'cc:2**0': 0.26; 'module': 0.26; 'noticed': 0.26; 'load': 0.26; 'function': 0.27; 'import': 0.27; 'tried': 0.27; 'variable': 0.28; 'putting': 0.28; "i'm": 0.28; '(even': 0.29; 'compile': 0.29; 'class': 0.29; 'seem': 0.29; 'skip:p 30': 0.29; 'ran': 0.30; 'skip:_ 40': 0.30; 'file.': 0.31; 'skip:l 30': 0.32; "i've": 0.32; 'does': 0.32; 'header:User-Agent:1': 0.33; 'file': 0.34; 'done': 0.34; 'hi,': 0.34; 'calling': 0.34; 'copied': 0.34; 'frame': 0.34; 'here,': 0.35; 'to:addr:python-list': 0.35; 'moving': 0.35; 'two': 0.36; 'issue': 0.37; 'created': 0.37; 'help!': 0.37; 'charset:us- ascii': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'subject:with': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'files': 0.39; 'correctly': 0.39; 'subject:from': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'header:Received:6': 0.61; 'zip': 0.62; 'it)': 0.67; 'same,': 0.67; 'placed': 0.69; 'distributes': 0.84
Received-SPF pass (google.com: domain of bob@brasko.net designates 10.68.129.38 as permitted sender) client-ip=10.68.129.38;
Authentication-Results mr.google.com; spf=pass (google.com: domain of bob@brasko.net designates 10.68.129.38 as permitted sender) smtp.mail=bob@brasko.net
Date Mon, 5 Mar 2012 15:36:39 -0500
From Bob <bob@brasko.net>
To python-list@python.org
Subject Error with co_filename when loading modules from zip file
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
User-Agent Mutt/1.5.21 (2010-09-15)
X-Gm-Message-State ALoCoQlPmBO8BwRMrWGTcGyIY/grD2rfghub27RVtrPu96H+SJXYEY8YbkcZ5r4P6wN41SQl6ZmR
Cc vinay_sajip@red-dove.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.410.1330979936.3037.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330979936 news.xs4all.nl 6858 [2001:888:2000:d::a6]:33023
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21238

Show key headers only | 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