-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-129025: fix too wide source location for bytecodes emitted for except* #129026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix too wide source locations of instructions emitted for ``except`` and | ||
``except*``. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2365,7 +2365,8 @@ codegen_try_except(compiler *c, stmt_ty s) | |||||
for (i = 0; i < n; i++) { | ||||||
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( | ||||||
s->v.Try.handlers, i); | ||||||
location loc = LOC(handler); | ||||||
/* Set location to the entire line of the except keyword */ | ||||||
location loc = LOCATION(handler->lineno, handler->lineno, 0, 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the entire line? Would the location of the type would be better? Is this legal syntax? except *\
ValueError: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The type has the location of the expression that defines it. This is for exceptions raised from the
We don't have accurate information in the AST about the location of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for the If the AST had all the location information for everything, what location would you give the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't generally attach locations to syntax like Maybe we should attach the locations to the keywords? Though, as you say, it will need changes to the AST. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this issue to add locations to the AST #129157. But we need to do something here that we can backport. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we go with the type, then we have the analogous situation with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also valid (PEP 8 notwithstanding :-):
I agree with Irit that the location we want to show in the traceback is the (Sorry for the delay Irit. Today I am attempting to catch up with a lot of email I've been procrastinating on.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following the discussion with @pablogsal on #129162 I think there are two different things here: (1) the location we put for the instruction in the code object and (2) what we display in the traceback. For the traceback, I now think we should ideally hilight the whole I think Pablo's PR should just add the missing location info to the AST, and then we can take it from there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did find an example for plain
I'd like to see that attached to the expression that gave a non-exception rather than on For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the extra blank line I noticed in the test output is because we use zeros for the column positions? So maybe this needs to be more careful and figure out the position of the last character on the line, or just put it only on the I guess if it's hard to do that right, I'm okay with the blank line. |
||||||
if (!handler->v.ExceptHandler.type && i < n-1) { | ||||||
return _PyCompile_Error(c, loc, "default 'except:' must be last"); | ||||||
} | ||||||
|
@@ -2546,7 +2547,8 @@ codegen_try_star_except(compiler *c, stmt_ty s) | |||||
for (Py_ssize_t i = 0; i < n; i++) { | ||||||
excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( | ||||||
s->v.TryStar.handlers, i); | ||||||
location loc = LOC(handler); | ||||||
/* Set location to the entire line of the except keyword */ | ||||||
location loc = LOCATION(handler->lineno, handler->lineno, 0, 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion here:
Suggested change
This highlights the For plain |
||||||
NEW_JUMP_TARGET_LABEL(c, next_except); | ||||||
except = next_except; | ||||||
NEW_JUMP_TARGET_LABEL(c, except_with_error); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This blank line is weird. I'd expect either an arrow and/or squiggles pointing at
except
or at the first character of the line or at the whole line, OR no arrows/squiggles and no blank line.The old way showed the entire except block, but no extra blank line.