Skip to content
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

Convert SyntaxWarning exception raised at code generation time to a SyntaxError #79210

Closed
serhiy-storchaka opened this issue Oct 20, 2018 · 2 comments
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 35029
Nosy @serhiy-storchaka
PRs
  • bpo-35029: Replace the SyntaxWarning exception with a SyntaxError. #9999
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-10-21.07:28:51.979>
    created_at = <Date 2018-10-20.07:24:03.894>
    labels = ['interpreter-core', 'type-feature', '3.8']
    title = 'Convert SyntaxWarning exception raised at code generation time to a SyntaxError'
    updated_at = <Date 2018-10-21.07:28:51.978>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2018-10-21.07:28:51.978>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-21.07:28:51.979>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-10-20.07:24:03.894>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35029
    keywords = ['patch']
    message_count = 2.0
    messages = ['328130', '328192']
    nosy_count = 1.0
    nosy_names = ['serhiy.storchaka']
    pr_nums = ['9999']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue35029'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    SyntaxError contains more useful information than SyntaxWarning, and SyntaxError is special cased in tracebacks for better reporting. When SyntaxWarning is raised as an exception, the error report doesn't contain information about the source. It is hard to find the source of a SyntaxWarning. Currently:

    $ ./python syntaxwarning.py 
    syntaxwarning.py:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
      x = 1; assert (x, "msg")
    $ ./python -We syntaxwarning.py 
    SyntaxWarning: assertion is always true, perhaps remove parentheses?
    $ ./python -m syntaxwarning
    /home/serhiy/py/cpython3.7/syntaxwarning.py:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
      x = 1; assert (x, "msg")
    $ ./python -We -m syntaxwarning
    Traceback (most recent call last):
      File "/home/serhiy/py/cpython3.7/Lib/runpy.py", line 183, in _run_module_as_main
        mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
      File "/home/serhiy/py/cpython3.7/Lib/runpy.py", line 153, in _get_module_details
        code = loader.get_code(mod_name)
      File "<frozen importlib._bootstrap_external>", line 860, in get_code
      File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    SyntaxWarning: assertion is always true, perhaps remove parentheses?

    The proposed PR replaces SyntaxWarning with a SyntaxError if the former was raised as an exception:

    $ ./python -We syntaxwarning.py 
      File "syntaxwarning.py", line 1
        x = 1; assert (x, "msg")
               ^
    SyntaxError: assertion is always true, perhaps remove parentheses?
    $ ./python -We -m syntaxwarning
    Traceback (most recent call last):
      File "/home/serhiy/py/cpython/Lib/runpy.py", line 183, in _run_module_as_main
        mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
      File "/home/serhiy/py/cpython/Lib/runpy.py", line 153, in _get_module_details
        code = loader.get_code(mod_name)
      File "<frozen importlib._bootstrap_external>", line 909, in get_code
      File "<frozen importlib._bootstrap_external>", line 839, in source_to_code
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/home/serhiy/py/cpython/syntaxwarning.py", line 1
        x = 1; assert (x, "msg")
               ^
    SyntaxError: assertion is always true, perhaps remove parentheses?

    Similar replacement is already performed for warnings raised for unrecognized escape sequences at parsing time (see bpo-32912).

    @serhiy-storchaka serhiy-storchaka added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Oct 20, 2018
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset d31e773 by Serhiy Storchaka in branch 'master':
    bpo-35029: Replace the SyntaxWarning exception with a SyntaxError. (GH-9999)
    d31e773

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant