GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — global declaration in except has incorrect prior use · Issue #111123 · python/cpython

global declaration in except has incorrect prior use #111123

New issue
Copy link
New issue
Copy link
Closed
#111142
Closed
global declaration in except has incorrect prior use#111123
#111142
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@15r10nk
15r10nk
opened on Oct 20, 2023
Issue body actions

Bug report

Bug description:

I think the global a has no prior use in this code (and pyright tells me the same). But I don't understand why cpython thinks it has a prior use.

a=5

def f():
    try:
        pass
    except:
        global a
    else:
        print(a)

output (Python 3.12.0):

  File "/home/frank/projects/pysource-playground/pysource-codegen/bug.py", line 8
    global a
    ^^^^^^^^
SyntaxError: name 'a' is used prior to global declaration

the following code has no syntax error:

a=5

def f():
    try:
        pass
    except:
        global a
    print(a)

I can also reproduce this issue in 3.7.

I also don't know what the exact semantic of global/nonlocal inside statements like if/while/try/... is. I would like to know more about it because I'm currently writing pysource-codegen where I generate such cases.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

  • gh-111123: symtable should visit exception handlers before the else block #111142

Activity

  1. 15r10nk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 20, 2023
  2. AlexWaygood
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Oct 20, 2023
  3. sobolevn commented on Oct 20, 2023

    @sobolevn
    sobolevn
    on Oct 20, 2023
    Member
    More actions

    I feel like this issue might be interesting to @iritkatriel and @pablogsal.

  4. iritkatriel commented on Oct 20, 2023

    @iritkatriel
    iritkatriel
    on Oct 20, 2023
    Member
    More actions

    I think this is because symtable_visit_stmt visits the else clause before the handlers:

        case Try_kind:      
            VISIT_SEQ(st, stmt, s->v.Try.body);
            VISIT_SEQ(st, stmt, s->v.Try.orelse);
            VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
            VISIT_SEQ(st, stmt, s->v.Try.finalbody);
            break;
    
    Reacted by Jelle Zijlstra
  5. iritkatriel
    added a commit that references this issue on Oct 20, 2023

    pythongh-111123: symtable should visit exception handlers before the …

    b76251b
  6. JelleZijlstra commented on Oct 20, 2023

    @JelleZijlstra
    JelleZijlstra
    on Oct 20, 2023
    Member
    More actions

    The converse of this bug is that this code currently works (and prints 1):

    a = 0
    
    def f():
        try:
            1 / 0
        except:
            a = 1
        else:
            global a
    
    f()
    print(a)

    With Irit's fix, this would instead be a SyntaxError.

    I don't know if any code in existence would rely on this weird behavior, but we should probably fix this only on main (not the bugfix branches) and call out the change in behavior in What's New.

    Reacted by Irit Katriel
  7. 15r10nk commented on Oct 21, 2023

    @15r10nk
    15r10nk
    on Oct 21, 2023
    ContributorAuthor
    More actions

    I agree @JelleZijlstra

    I think I understood how the logic behind global works.

    a=0
    
    def f(cnd,value):
        if cnd:
            global a
        a=value
    
    f(True,1)
    print(a)
    f(False,2)
    print(a)

    output (Python 3.12.0):

    1
    2

    global is not affected by the actual order of execution. It is more "the line" in which it was declared.

    The syntax error for prior use exists mainly to safe the user from thinking that he could put the global behind the use of some variable and to expect a different behavior.
    Leaving out a lot of other wired uses like the if above.

    A SyntaxWarning might be useful here, but I also don't know if this generated code is representative for real world problem. Hard to tell if such a SyntaxWarning could help someone.

    @iritkatriel I saw your fix and I think TryStar is also affected by this bug.

  8. iritkatriel commented on Oct 21, 2023

    @iritkatriel
    iritkatriel
    on Oct 21, 2023
    Member
    More actions

    @iritkatriel I saw your fix and I think TryStar is also affected by this bug.

    Right, thanks.

  9. iritkatriel
    closed this as completedin #111142on Oct 21, 2023
  10. iritkatriel
    added a commit that references this issue on Oct 21, 2023

    gh-111123: symtable should visit exception handlers before the else b…

    b578e51
  11. 15r10nk
    added 4 commits that reference this issue on Nov 29, 2023

    fix: workaround for python/cpython#111123

    a81b9c7

    fix: workaround for python/cpython#111123

    0def07d

    fix: workaround for python/cpython#111123

    cb14f29

    fix: workaround for python/cpython#111123

    aaf4bfa
  12. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-111123: symtable should visit exception handlers before the …

    56242d5
  13. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-111123: symtable should visit exception handlers before the …

    dca6007
  14. ntBre
    added a commit that references this issue on Apr 9, 2025

    [syntax-errors] Document behavior of `global` declarations in `try` n…

    2fbc4d5
  15. Glyphack
    added a commit that references this issue on Apr 9, 2025

    [syntax-errors] Document behavior of `global` declarations in `try` n…

    c190599
  16. KotlinIsland
    added a commit that references this issue on May 1, 2026

    [syntax-errors] Document behavior of `global` declarations in `try` n…

    bdab13c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.