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

Update line number handling and provide better traceback. #14

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

tillahoffmann
Copy link
Owner

@tillahoffmann tillahoffmann commented Jul 15, 2024

from localscope import localscope


@localscope
def foo():
    # This
    # is
    # a
    # long
    # source
    # file.
    if True:
        print(x)

    # We
    # have
    # printed
    # something
    # here.
---------------------------------------------------------------------------
LocalscopeException                       Traceback (most recent call last)
Cell In[4], line 5
      1 from localscope import localscope
      4 @localscope
----> 5 def foo():
      6     # This
      7     # is
      8     # a
      9     # long
     10     # source
     11     # file.
     12     if True:
     13         print(x)

File ~/git/localscope/localscope/__init__.py:103, in localscope(func, predicate, allowed, allow_closure)
     95 if not func:
     96     return ft.partial(
     97         localscope,
     98         allow_closure=allow_closure,
     99         allowed=allowed,
    100         predicate=predicate,
    101     )
--> 103 return _localscope(
    104     func,
    105     allow_closure=allow_closure,
    106     allowed=allowed,
    107     predicate=predicate,
    108     _globals={},
    109 )

File ~/git/localscope/localscope/__init__.py:200, in _localscope(func, predicate, allowed, allow_closure, _globals)
    198 # Complain if the variable is not available.
    199 if name not in _globals:
--> 200     raise LocalscopeException(
    201         f"`{name}` is not in globals", code, instruction, lineno
    202     )
    203 # Check if variable is allowed by value.
    204 value = _globals[name]

LocalscopeException: `x` is not in globals (file "/var/folders/l5/cw82lksx6sd80pskw1npx_kh0000gn/T/ipykernel_50104/3725129688.py", line 13, in foo)
     11:     # file.
     12:     if True:
-->  13:         print(x)
     14: 
     15:     # We

@tillahoffmann tillahoffmann added the enhancement New feature or request label Jul 15, 2024
@tillahoffmann tillahoffmann merged commit 96f0d5d into main Jul 15, 2024
5 checks passed
@tillahoffmann tillahoffmann deleted the source branch July 15, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant