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

nested variables in 'class:' statements #39270

Closed
arigo mannequin opened this issue Sep 22, 2003 · 8 comments
Closed

nested variables in 'class:' statements #39270

arigo mannequin opened this issue Sep 22, 2003 · 8 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@arigo
Copy link
Mannequin

arigo mannequin commented Sep 22, 2003

BPO 810714
Nosy @arigo, @rhettinger
Files
  • classbug.py: unexpected names and name clashes
  • classvars.diff: Draft patch
  • 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 2007-02-27.07:23:57.000>
    created_at = <Date 2003-09-22.17:05:02.000>
    labels = ['interpreter-core']
    title = "nested variables in 'class:' statements"
    updated_at = <Date 2007-02-27.07:23:57.000>
    user = 'https://github.com/arigo'

    bugs.python.org fields:

    activity = <Date 2007-02-27.07:23:57.000>
    actor = 'arigo'
    assignee = 'jhylton'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2003-09-22.17:05:02.000>
    creator = 'arigo'
    dependencies = []
    files = ['1034', '1035']
    hgrepos = []
    issue_num = 810714
    keywords = []
    message_count = 8.0
    messages = ['18249', '18250', '18251', '18252', '18253', '18254', '18255', '18256']
    nosy_count = 3.0
    nosy_names = ['jhylton', 'arigo', 'rhettinger']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue810714'
    versions = ['Python 2.3']

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Sep 22, 2003

    from the user's point of view, variables originating
    from nested scopes could erratically become bound in
    the 'class' statement.

    The 'class:' statements works by capturing all locals()
    after executing the body; however, this is not exactly
    the same as what an explicit call to locals() would
    return because of the missing PyFrame_FastToLocals()
    call in the implementation of LOAD_LOCALS in ceval.c.
    It was thought that PyFrame_FastToLocals() was
    unnecessary at that point because the class body
    bytecode only uses LOAD_NAME/STORE_NAME and not fast
    locals -- but this is no longer true with nested
    scopes. See attached examples.

    @arigo arigo mannequin closed this as completed Sep 22, 2003
    @arigo arigo mannequin assigned jhylton Sep 22, 2003
    @arigo arigo mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Sep 22, 2003
    @arigo arigo mannequin closed this as completed Sep 22, 2003
    @arigo arigo mannequin assigned jhylton Sep 22, 2003
    @arigo arigo mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Sep 22, 2003
    @rhettinger
    Copy link
    Contributor

    Logged In: YES
    user_id=80475

    Do you have a proposed patch?

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Sep 24, 2003

    Logged In: YES
    user_id=4771

    I'm not sure how to solve this. I could make a patch to
    LOAD_LOCALS to prevent free variables from showing up in the
    class.__dict__. For the 2nd problem I'm not familiar enough
    with compile.c to know how to make the binding 'b="foo"'
    sufficient to prevent 'b' from also being considered free in
    the class definition (which is what occurs).

    Note also that bare 'exec' statements should then be
    forbidden in class definitions in the presence of free
    variables, for the same reason as it is forbidden in
    functions: you cannot tell whether a variable is free or
    local.

    As an example of this, if in the attached example we replace
    b="foo" with exec 'b="foo"' then the last print correctly
    outputs 'foo' instead of 6 but what actually occurs is that
    the value of the argument b in f(a,b) was modified by the
    class definition -- it is a way to change the binding of a
    variable in an enclosing frame, which should not be
    possible.

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Jan 8, 2004

    Logged In: YES
    user_id=4771

    Attached is a draft. I am not sure that I understand all the implications of nested variables in the presence of class bodies, so please don't check in this patch.

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 25, 2007

    I'm not sure I understand all the implications of nested variables, either. The current behavior of locals() is to return the names of all free variables that are being passed through the class body so that they can be used by methods. Is the behavior correct? I see that IronPython implements locals() that way, but does not bind them as class variables (good). Should we change the "spec" of locals() and cause IronPython to be incompatible, or should we fix CPython and PyPy to behave the same way? The fix for CPython will be somewhat involved.

    1 similar comment
    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 25, 2007

    I'm not sure I understand all the implications of nested variables, either. The current behavior of locals() is to return the names of all free variables that are being passed through the class body so that they can be used by methods. Is the behavior correct? I see that IronPython implements locals() that way, but does not bind them as class variables (good). Should we change the "spec" of locals() and cause IronPython to be incompatible, or should we fix CPython and PyPy to behave the same way? The fix for CPython will be somewhat involved.

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 26, 2007

    Committed revision 53954.

    @arigo
    Copy link
    Mannequin Author

    arigo mannequin commented Feb 27, 2007

    Jeremy, you inserted many spaces instead of tabs
    in frameobject.c.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant