8000 gh-135801: Fix inaccurate module info for SyntaxWarnings during AST parsing by heliang666s · Pull Request #135829 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-135801: Fix inaccurate module info for SyntaxWarnings duri 8000 ng 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
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

heliang666s
Copy link
@heliang666s heliang666s commented Jun 23, 20 8000 25

This commit addresses the inaccurate module information provided for SyntaxWarnings during AST parsing, specifically reported in issue #135801.

Previously, _PyErr_EmitSyntaxWarning (used by the compiler) relied on _PyErr_WarnExplicitObjectWithContext to infer the module name from the call stack. During compilation, this often resulted in misleading module names like sys or importlib._bootstrap, making it difficult to filter warnings effectively (e.g., using -W error::SyntaxWarning:test).

To resolve this, a new internal function _PyErr_EmitSyntaxWarningFromCompiler was introduced in Python/errors.c. This function explicitly calls PyErr_WarnExplicitObject with a NULL module argument, forcing the warning system to derive the module name from the filename provided by the compiler.

The normalize_module function in Python/_warnings.c was also refined. Instead of simple string manipulation or problematic pure C path parsing, it now leverages Python's os.path module (via Python C API calls like basename, splitext, and dirname). This ensures robust and accurate extraction of module names from arbitrary file paths, including correct handling of __init__.py files, thereby providing correct module information for compiler-generated SyntaxWarning messages.

This approach ensures cross-platform compatibility and reliability by utilizing Python's battle-tested path handling capabilities, directly addressing the core issue reported.

@bedevere-app
Copy link
bedevere-app bot commented Jun 23, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link
bedevere-app bot commented Jun 23, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link
python-cla-bot bot commented Jun 23, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

*/
int
_PyErr_EmitSyntaxWarningFromCompiler(PyObject *msg, PyObject *filename, int lineno, int col_offset,
int end_lineno, int end_col_offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PyErr_EmitSyntaxWarning is called from compile.c and ast_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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0