-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135801: Fix inaccurate module info for SyntaxWarnings during AST parsing #135829
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
Open
heliang666s
wants to merge
8
commits into
python:main
Choose a base branch
from
heliang666s:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−33
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d971dd5
Fix: Better module info for SyntaxWarnings during AST parsing
0deed9e
Fix SyntaxWarning deduplication with pseudo-filenames
4b05629
Fix SyntaxWarning deduplication with pseudo-filenames
423fae3
Add Unit Test
20c4390
Fix test_warnings failures in CI
3ddfa71
Fix: Correct ImportError and NameError in test_warnings
93b3bca
Merge branch 'main' into main
heliang666s 15ab120
Merge branch 'python:main' into main
heliang666s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ext/Core_and_Builtins/2024-06-23-10-56-32.gh-issue-135801.fix-warning-dedup.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix warning deduplication regression for SyntaxWarnings with pseudo-filenames (e.g., <stdin>, <string>). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
/* Error handling */ | ||
|
||
#include "Python.h" | ||
|
@@ -1952,6 +1951,39 @@ _PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_ | |
return 0; | ||
} | ||
|
||
/* Emits a SyntaxWarning and returns 0 on success. | ||
If a SyntaxWarning is raised as error, replaces it with a SyntaxError | ||
and returns -1. | ||
This version is for the compiler, and derives the module from the filename. | ||
*/ | ||
int | ||
_PyErr_EmitSyntaxWarningFromCompiler(PyObject *msg, PyObject *filename, int lineno, int col_offset, | ||
int end_lineno, int end_col_offset) | ||
{ | ||
Py_ssize_t len = PyUnicode_GET_LENGTH(filename); | ||
if (len > 1 && | ||
PyUnicode_READ_CHAR(filename, 0) == '<' && | ||
PyUnicode_READ_CHAR(filename, len - 1) == '>') | ||
{ | ||
return _PyErr_EmitSyntaxWarning(msg, filename, lineno, col_offset, | ||
end_lineno, end_col_offset); | ||
} | ||
|
||
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, | ||
filename, lineno, NULL, NULL) < 0) | ||
{ | ||
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { | ||
/* Replace the SyntaxWarning exception with a SyntaxError | ||
to get a more accurate error report */ | ||
PyErr_Clear(); | ||
_PyErr_RaiseSyntaxError(msg, filename, lineno, col_offset, | ||
end_lineno, end_col_offset); | ||
} | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
/* Attempt to load the line of text that the exception refers to. If it | ||
fails, it will return NULL but will not set an exception. | ||
45DD
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
_PyErr_EmitSyntaxWarning
is called fromcompile.c
andast_preprocess.c
. Arguably, these are both parts of "the compiler". Why do we need to introduce a new function for this rather than fix_PyErr_EmitSyntaxWarning
?