You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
line number for a return in a with block after 3.10 is that of the with, not the return statement #93975
The jump back to line 2 may seem surprising, but it is necessary.
Because the call to __exit__ when exiting the with statement is is observable and may raise an exception, it needs to have a line number.
Thanks Mark! From a bytecode perspective I can understand how this might not be a bug, especially as our bytecode becomes more optimal.
If this is a non-issue feel free to close it.
I believe the question from a pytype perspective, which does its program analysis via bytecode, is if it is possible to determine where the return statement was in the code for error reporting reasons. '' not being an int, the return statement attempting the wrong type is where an error message would ideally point. In absense of concrete information from the bytecode the analysis could possibly detect that a with or try block is being exited and perhaps tell the user the more vague block the errant return was exiting from. I'll leave that up to pytype folks to figure out.
If the expression in the return statement is anything but a constant, it should have the exact location information, not just the line number. If it is a constant, then simple AST analysis will do.
My advice to the pytype developers would be to build their own CFG. The transformations required for precise type analysis and for code generation are different enough that a custom CFG is probably the way to go.
Activity
dignissimus commentedon Jun 18, 2022
The issue was introduced in 937cebc
See bpo-44298: #88464
The same behaviour occurs with try-finally blocks on both 3.9 and 3.10+
With the above code, disassembling on Python 3.10.5 produces the following
And on 3.9.0
gh-93975: Nicer error reporting in test_venv (GH-93959)
[3.11] gh-93975: Nicer error reporting in test_venv (GH-93959)
[3.10] gh-93975: Nicer error reporting in test_venv (GH-93959)
[3.9] gh-93975: Nicer error reporting in test_venv (GH-93959)
markshannon commentedon Jun 20, 2022
I believe the current behavior is correct.
The sequence of events is:
openand enter the with blockThe jump back to line 2 may seem surprising, but it is necessary.
Because the call to
__exit__when exiting thewithstatement is is observable and may raise an exception, it needs to have a line number.markshannon commentedon Jun 20, 2022
If you change
"a"tolen("a"), note thatlen("a")is on line 3.markshannon commentedon Jun 20, 2022
On more thing.
In Python 3.9, if
__exit__raises, it reports the wrong line number.Will report the exception on line 4, which is clearly wrong.
It is 3.9 that is broken, not 3.10 or 3.11, IMO.
jaraco commentedon Jun 20, 2022
Please disregard the commits above referencing this issue. They should have referenced #93957.
gpshead commentedon Jun 20, 2022
Thanks Mark! From a bytecode perspective I can understand how this might not be a bug, especially as our bytecode becomes more optimal.
If this is a non-issue feel free to close it.
I believe the question from a pytype perspective, which does its program analysis via bytecode, is if it is possible to determine where the return statement was in the code for error reporting reasons.
''not being anint, the return statement attempting the wrong type is where an error message would ideally point. In absense of concrete information from the bytecode the analysis could possibly detect that awithortryblock is being exited and perhaps tell the user the more vague block the errantreturnwas exiting from. I'll leave that up to pytype folks to figure out.markshannon commentedon Jun 20, 2022
If the expression in the
returnstatement is anything but a constant, it should have the exact location information, not just the line number. If it is a constant, then simple AST analysis will do.My advice to the pytype developers would be to build their own CFG. The transformations required for precise type analysis and for code generation are different enough that a custom CFG is probably the way to go.
Here's one I made earier 🙂
https://codeql.github.com/docs/codeql-language-guides/analyzing-control-flow-in-python/
markshannon commentedon Jun 20, 2022
OK, I'll close this.
Revert 30935 defer bpo45162 to 312 (#94285)
[3.11] gh-93975: Provide nicer error reporting from subprocesses in t…
[3.10] gh-93975: Provide nicer error reporting from subprocesses in t…