[New-bugs-announce] [issue9323] trace.py bug with the main file being traced

Eli Bendersky report at bugs.python.org
Wed Jul 21 16:30:32 CEST 2010


New submission from Eli Bendersky <eliben at gmail.com>:

[This bug was discovered by Alexander Belopolsky, during the work on Issue 9317]

Bug report
**********

The attached traceme.py file demonstrates the following problem:

With python 2.7:
$ python2 -m trace  -c -s traceme.py
lines   cov%   module   (path)
    1   100%   threading   (Lib/threading.py)
    6   100%   traceme   (traceme.py)

The first entry is clearly spurious, but traceme looks right.  With py3k, however, I get

$ python3 -m trace  -c -s traceme.py
lines   cov%   module   (path)
    1   100%   threading   (Lib/threading.py)

No traceme line at all.

Analysis
********

When trace.py actually runs the script, it uses this code in 3.x:

           t.run('exec(%r)' % (script,))

instead of this code in 2.x:

            t.run('execfile(%r)' % (progname,))

`exec` doesn't have the program name to attach to the code object created, and calls it '<string>' by default. However, this file name is ignored by the trace module.

`execfile` doesn't exist in 3.x, so an alternative approach is needed.

Proposed solution
*****************

Instead of t.run as displayed above, use this code:

            with open(progname) as fp:
                code = compile(fp.read(), progname, 'exec')
                t.run(code)

The code object created by `compile` attaches the program name and is executable by t.run (because t.run actually calls `exec` which can run the result of `compile`).

This solution fixes the problem:

$ py3d -m trace -c -s traceme.py 
lines   cov%   module   (path)
    1   100%   threading   (/home/eliben/python_src/eliben-py3k/Lib/threading.py)
    6   100%   traceme   (traceme.py)

I'll attach a patch file that was creates vs. the latest trunk, with this fix.

----------
files: traceme.py
messages: 111066
nosy: eli.bendersky
priority: normal
severity: normal
status: open
title: trace.py bug with the main file being traced
type: behavior
versions: Python 3.1, Python 3.2
Added file: http://bugs.python.org/file18105/traceme.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9323>
_______________________________________


More information about the New-bugs-announce mailing list