Issue1751

classification
Title: sys.settrace does not consider "try", "except" or "break" statements
Type: Severity: normal
Components: Versions: Jython 2.5
Milestone: Jython 2.7.2
process
Status: open Resolution: remind
Dependencies: Superseder:
Assigned To: Nosy List: fwierzbicki, geoffbache, nedbat, zyasoft
Priority: normal Keywords:

Created on 2011-05-30.18:52:20 by geoffbache, last changed 2017-01-19.00:26:56 by nedbat.

Messages
msg6532 (view) Author: Geoff Bache (geoffbache) Date: 2011-05-30.18:52:19
I've been trying to measure coverage of my Jython program and finding
that (at least) all lines containing "try:", "except:" or "break"
never get shown as covered.

This appears to be because the sys.settrace function, on which
coverage.py relies for its information, is not triggered by such
lines. 

For example, compare the output of the following program under Jython
and CPython. No output is created for lines 10, 12 or 13 when running
under Jython, whereas it is for CPython.

import sys

def tracefunc(frame, event, arg):
   print "Trace", event, frame.f_lineno
   return tracefunc

def do():
   x = [ 1 ]
   for i in range(2):
       try:
           x[i]
       except IndexError:
           break

sys.settrace(tracefunc)
do()
msg11020 (view) Author: Ned Batchelder (nedbat) Date: 2017-01-14.19:27:54
BTW, #1248 is a dup of this issue.
msg11025 (view) Author: Ned Batchelder (nedbat) Date: 2017-01-19.00:26:56
"with" statements are also overlooked:

import linecache, sys

def tracefunc(frame, event, arg):
    lineno = frame.f_lineno
    print "Trace", event, lineno, linecache.getline(__file__, lineno).rstrip()
    return tracefunc

def do():
    x = [ 1 ]
    for i in range(2):
        try:
            x[i]
        except IndexError:
            break

    with open(__file__) as f:
        print len(f.read())

sys.settrace(tracefunc)
do()


Produces:

Trace call 8 def do():
Trace line 9     x = [ 1 ]
Trace line 10     for i in range(2):
Trace line 10     for i in range(2):
Trace line 12             x[i]
Trace line 10     for i in range(2):
Trace line 12             x[i]
Trace exception 12             x[i]
Trace line 17         print len(f.read())
397
Trace return 17         print len(f.read())
History
Date User Action Args
2017-01-19 00:26:56nedbatsetmessages: + msg11025
2017-01-14 19:27:54nedbatsetmessages: + msg11020
2017-01-14 19:26:45nedbatsetnosy: + nedbat
2015-11-02 03:17:56zyasoftsetmilestone: Jython 2.7.2
2014-07-01 04:09:32zyasoftsetnosy: + zyasoft
2013-02-20 00:10:19fwierzbickisetnosy: + fwierzbicki
2013-02-20 00:10:12fwierzbickisetpriority: normal
resolution: remind
versions: + Jython 2.5, - 2.5.2rc
2011-05-30 18:52:20geoffbachecreate