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

Assignment operator allows to assign to __debug__ #80233

Closed
serhiy-storchaka opened this issue Feb 20, 2019 · 11 comments
Closed

Assignment operator allows to assign to __debug__ #80233

serhiy-storchaka opened this issue Feb 20, 2019 · 11 comments
Assignees
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 36052
Nosy @gvanrossum, @serhiy-storchaka, @matrixise, @emilyemorehouse, @pablogsal
PRs
  • bpo-36052: Disallow assigning to __debug__ in named expressions #11957
  • bpo-36052: Raise a SyntaxError when we assign a value to __debug__ #11958
  • 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 = 'https://github.com/matrixise'
    closed_at = <Date 2019-02-21.10:12:23.071>
    created_at = <Date 2019-02-20.15:23:36.274>
    labels = ['interpreter-core', 'type-bug', '3.8']
    title = 'Assignment operator allows to assign to __debug__'
    updated_at = <Date 2019-02-21.10:12:23.071>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-02-21.10:12:23.071>
    actor = 'pablogsal'
    assignee = 'matrixise'
    closed = True
    closed_date = <Date 2019-02-21.10:12:23.071>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2019-02-20.15:23:36.274>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36052
    keywords = ['patch']
    message_count = 11.0
    messages = ['336100', '336103', '336104', '336108', '336110', '336112', '336113', '336115', '336119', '336124', '336197']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'matrixise', 'emilyemorehouse', 'pablogsal']
    pr_nums = ['11957', '11958']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36052'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    All ways of assigning to __debug__ are forbidden:

    >>> __debug__ = 1
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> for __debug__ in []: pass
    ... 
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> with cm() as __debug__: pass
    ... 
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> class __debug__: pass
    ... 
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> def __debug__(): pass
    ... 
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> def foo(__debug__): pass
    ... 
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__
    >>> import __debug__
      File "<stdin>", line 1
    SyntaxError: cannot assign to __debug__

    The only exception is the assignment operator.

    >>> (__debug__ := 'spam')
    'spam'
    >>> globals()['__debug__']
    'spam'

    This looks like a bug.

    @serhiy-storchaka serhiy-storchaka added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 20, 2019
    @matrixise
    Copy link
    Member

    An idea for the fix? a direction where I could find the solution? ;-)

    @serhiy-storchaka
    Copy link
    Member Author

    You need to add for the target of the assignment operator the same check as for other targets.

    @matrixise
    Copy link
    Member

    ok, I will check for the assignment operator in the code.

    maybe in the AST (Python-ast.c) but I think not

    @matrixise
    Copy link
    Member

    ok, I found the commit of @Emily 8f59ee0

    and will try to find the assignment operator :=

    @emilyemorehouse
    Copy link
    Member

    You should look in Python/ast.c. The naming convention follows some form of "named expression" (e.g. NamedExpr, ast_for_namedexpr).

    I'll have more time to look later this week, but let me know if you have any questions.

    @matrixise
    Copy link
    Member

    Hi @Emily

    Thank you for your help.

    In fact, I have started to read Python-ast.c and ast.c, they are the main files for this kind of operations.

    Thank you again,

    @matrixise matrixise self-assigned this Feb 20, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    Look at compile.c. The code for restricting other assignments is located here.

    @pablogsal
    Copy link
    Member

    When I started working on this I did not notice the extra comments here.

    I will close my PR so Stéphane can do the PR.

    @matrixise
    Copy link
    Member

    Thank you Pablo, if I see you at PyCon, maybe we could drink a good beer ;-)

    @pablogsal
    Copy link
    Member

    New changeset 3ad9167 by Pablo Galindo (Stéphane Wirtel) in branch 'master':
    bpo-36052: Raise a SyntaxError when assigning a value to __debug__ with := (GH-11958)
    3ad9167

    @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-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants