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

incorrect: failed local variable referenced before assignment #51082

Closed
paultjuhatwork mannequin opened this issue Sep 3, 2009 · 2 comments
Closed

incorrect: failed local variable referenced before assignment #51082

paultjuhatwork mannequin opened this issue Sep 3, 2009 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@paultjuhatwork
Copy link
Mannequin

paultjuhatwork mannequin commented Sep 3, 2009

BPO 6833
Nosy @mdickinson
Files
  • bug.py: 3 test cases stripped down to the bare minimum
  • 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 2009-09-03.11:44:35.275>
    created_at = <Date 2009-09-03.11:25:31.158>
    labels = ['type-bug', 'invalid']
    title = 'incorrect: failed local variable referenced before assignment'
    updated_at = <Date 2009-09-03.11:44:35.262>
    user = 'https://bugs.python.org/paultjuhatwork'

    bugs.python.org fields:

    activity = <Date 2009-09-03.11:44:35.262>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-09-03.11:44:35.275>
    closer = 'mark.dickinson'
    components = ['None']
    creation = <Date 2009-09-03.11:25:31.158>
    creator = 'paultjuhatwork'
    dependencies = []
    files = ['14825']
    hgrepos = []
    issue_num = 6833
    keywords = []
    message_count = 2.0
    messages = ['92199', '92201']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'paultjuhatwork']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue6833'
    versions = ['Python 2.6', 'Python 2.5', 'Python 2.4', 'Python 3.0']

    @paultjuhatwork
    Copy link
    Mannequin Author

    paultjuhatwork mannequin commented Sep 3, 2009

    The attached python file will give the following output, which is
    incorrect behavior as far as I know:
    "
    this will fail
    failed local variable 'in_std' referenced before assignment

    this won't fail
    Not failed
    this won't fail either
    Not failed
    "

    This is tested on windows with python2.6(standard msi) and on centos
    5.3 with python2.6 (custom rpm), python2.4 (system rpm), freebsd with
    python2.5 (system package), python2.6 ("hand" compiled) and python3.0
    ("hand" compiled).

    The attached code is stripped down to the bare minimum and therefore
    won't do anything usefull.

    @paultjuhatwork paultjuhatwork mannequin added the type-bug An unexpected behavior, bug, or error label Sep 3, 2009
    @mdickinson
    Copy link
    Member

    This is not a bug. The behaviour you're seeing is described here:

    http://docs.python.org/reference/executionmodel.html#naming-and-binding

    "If a name binding operation occurs anywhere within a code block, all
    uses of the name within the block are treated as references to the
    current block. This can lead to errors when a name is used within a
    block before it is bound. This rule is subtle. Python lacks declarations
    and allows name binding operations to occur anywhere within a code
    block. The local variables of a code block can be determined by scanning
    the entire text of the block for name binding operations."

    In the failing example, the registerdecorator function contains an
    assignment to in_std, so by the rules above in_std is local to the
    function. The 'if in_std' line therefore tries to lookup 'in_std' in
    the local namespace; it doesn't exist (yet), so an UnboundLocalError
    exception occurs.

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

    No branches or pull requests

    1 participant