This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: line number when tracing an implicit return
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, Mark.Shannon, iritkatriel, nedbat, sdeibel, terry.reedy
Priority: normal Keywords:

Created on 2011-12-07 16:28 by sdeibel, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
badlineevent.py sdeibel, 2011-12-07 16:28 Illustrates the bug -- 'line' event on line 13 should not occur
Messages (9)
msg148974 - (view) Author: Stephan R.A. Deibel (sdeibel) Date: 2011-12-07 16:28
The tracer set with sys.settrace() is called incorrectly with a 'line' event on a 'pass' that is at the end of an 'else' clause on the final line of a function even if the else block is not executed by the interpreter.  Whew, talk about an end case!  The attached file illustrates this.
msg148975 - (view) Author: Stephan R.A. Deibel (sdeibel) Date: 2011-12-07 16:32
Sorry, the print statement in the file needs a tweak to work with Python 3.2, but the bug does occur there also.
msg149365 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-12-12 23:20
(Snippet examples can be made 2/3 agnostic with 
from __future__ import print_function)

3.2.2 on win7, IDLE, gives me
F:\Python\mypy\tem.py 7 call
F:\Python\mypy\tem.py 8 line
F:\Python\mypy\tem.py 10 line
F:\Python\mypy\tem.py 11 line
F:\Python\mypy\tem.py 13 line
F:\Python\mypy\tem.py 13 return
...
Commenting out else:pass or changing pass to r=2 changes the output to
F:\Python\mypy\tem.py 7 call
F:\Python\mypy\tem.py 8 line
F:\Python\mypy\tem.py 10 line
F:\Python\mypy\tem.py 11 line
F:\Python\mypy\tem.py 11 return

Looking as the dis outputs of the three versions, I find the difference of behavior somewhat puzzling.
msg266889 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2016-06-02 17:06
The reason for this behavior is that the trace function is called whenever execution in the bytecode jumps to a new source line. See ceval.c line 4440 or so:

    /* If the last instruction falls at the start of a line or if
       it represents a jump backwards, update the frame's line
       number and call the trace function. */

Running dis.dis on x in the example file shows that the bytecode for returning None is assigned to the "pass" line.

I think the bug here is really with the line number assignment in the bytecode, not with the tracing, but I don't see an obvious way to fix it.
msg407823 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-06 15:12
Reproduced on 3.11. 


An implicit return doesn't have a line, so it's not clear what should happen. Assigning the next line to it wouldn't be correct either (it could be the first line of the next function). 

We could say that in this case there is no line event, just a return event (unless that messes up the tracer somehow).

If you add a return statement it makes sense:

/Users/iritkatriel/src/cpython/mm.py 7 call
/Users/iritkatriel/src/cpython/mm.py 8 line
/Users/iritkatriel/src/cpython/mm.py 10 line
/Users/iritkatriel/src/cpython/mm.py 11 line
/Users/iritkatriel/src/cpython/mm.py 14 line
/Users/iritkatriel/src/cpython/mm.py 14 return

(line 13 does not appear because that's the else, the return is assigned line 14 which is where it really is).
msg407869 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-12-06 22:12
This seems to be fixed.  On 3.11.0a2 installed on Windows and fresh build of .0a2+, I see return on line 11, which is correct.

f:\dev\3x>python ../tem/tem.py
Running Debug|x64 interpreter...
f:\dev\tem\tem.py 7 call
f:\dev\tem\tem.py 8 line
f:\dev\tem\tem.py 10 line
f:\dev\tem\tem.py 11 line
f:\dev\tem\tem.py 11 return

Irit, please confirm (or not) on your *nix system.
msg407871 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-06 22:13
Terry, I'm pretty sure I saw the problem on the mac earlier. Can you paste the contents of tem.py?
msg407873 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-12-06 22:38
The attached badlineevent.py with print updated.
msg407877 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-06 23:16
Yes, you're right. I don't know what I thought I saw before. It works on the mac as well.
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57757
2021-12-06 23:16:40iritkatrielsetstatus: open -> closed
resolution: out of date
messages: + msg407877

stage: resolved
2021-12-06 22:38:24terry.reedysetmessages: + msg407873
2021-12-06 22:13:52iritkatrielsetmessages: + msg407871
2021-12-06 22:12:04terry.reedysetnosy: + Mark.Shannon
messages: + msg407869
2021-12-06 15:12:48iritkatrielsetnosy: + iritkatriel
title: Invalid 'line' tracer event on pass within else clause -> line number when tracing an implicit return
messages: + msg407823

versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.2, Python 3.3
2016-06-02 17:06:41JelleZijlstrasetnosy: + JelleZijlstra
messages: + msg266889
2011-12-12 23:20:05terry.reedysetnosy: + terry.reedy

messages: + msg149365
versions: + Python 3.3, - Python 2.6, Python 3.1
2011-12-07 16:32:02sdeibelsetmessages: + msg148975
2011-12-07 16:30:17nedbatsetnosy: + nedbat
2011-12-07 16:28:09sdeibelcreate