Skip to content

[RFT BUG] nonlocal / global statements are not renamed #2379

Description

@AlecHaring

A global/nonlocal statement keeps the original name while the variable it refers to is renamed, producing a SyntaxError.

Input

main.py

from pkg.core import bump, make_adder

print(bump(), make_adder()(2))

pkg/__init__.py - empty package marker

pkg/core.py

counter = 0


def bump():
    global counter
    counter += 1
    return counter


def make_adder():
    total = 0

    def add(n):
        nonlocal total
        total += n
        return total

    return add

Steps

pyarmor init -s . -p pkg -e main.py
pyarmor build --rft

Refactored output (the inconsistency)

dist/pkg/core.py

pyarmor__3 = 0

def pyarmor__1():
    global counter
    pyarmor__3 += 1
    return pyarmor__3

def pyarmor__2():
    pyarmor__4 = 0

    def pyarmor__6(pyarmor__5):
        nonlocal total
        pyarmor__4 += pyarmor__5
        return pyarmor__4
    return pyarmor__6

Running the refactored code:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from pkg.core import pyarmor__1, pyarmor__2
  File "pkg/core.py", line 12
    nonlocal total
    ^^^^^^^^^^^^^^
SyntaxError: no binding for nonlocal 'total' found

Environment

  • PyArmor: 9.2.7 (pro), build 005037
  • Python: 3.11.9
  • Platform: windows.x86_64 (Windows 11)
  • Mode: RFT (pyarmor build --rft)

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions