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

setup() / settings() disentanglement feature edge case? #40

Closed
villares opened this issue Sep 5, 2021 · 5 comments
Closed

setup() / settings() disentanglement feature edge case? #40

villares opened this issue Sep 5, 2021 · 5 comments

Comments

@villares
Copy link
Collaborator

villares commented Sep 5, 2021

I understand you might choose not to tackle this case... but it is a common idiom to to put global statements at the start of functions, and this seems to break the rule of having size() as the first thing...

image

(As I know what is going on I had no trouble reordering...)

@hx2A
Copy link
Collaborator

hx2A commented Sep 5, 2021

Hmmm, I should fix this, because it breaks the illusion of setup() being a real function and not something that is split into two.

As a workaround, you can switch the order of the global statement and the call to size().

I don't think this will be a hard fix. Right now you can put # comments before the call to size() and it will work fine. There's no reason why global can't also work. However, py5 will need to reshuffle things and put the global statements in the _py5_faux_setup() function (that's what it is called internally). They should not end up in _py5_faux_settings() with size(). The reshuffling will require impact a few things...fixing this is achievable, and I'll work on it.

hx2A added a commit that referenced this issue Sep 28, 2021
@hx2A
Copy link
Collaborator

hx2A commented Sep 28, 2021

This fix seems to work. Global statements that precede the call to size() get moved to the fake setup() function.

@hx2A
Copy link
Collaborator

hx2A commented Sep 29, 2021

The hard part here isn't dealing with the global statements, it is making sure that there are no misleading error messages. If I have this in a notebook cell:

x = 10

def setup():
    # random comment
    global x
    # another comment
    size(200, 200)
    x = 42
    print(0 / 0)

This is the error message I get:

File "<ipython-input-10-b0e58154cf74>", line 9, in _py5_faux_setup
    3    def setup():
 (...)
    5        global x
    6        # another comment
    7        size(200, 200)
    8        x = 42
--> 9        print(0 / 0)

ZeroDivisionError: division by zero

Which points to the correct line number.

I think I have everything working now. I also add sensible global support for py5bot, even though using the global keyword outside of a function is a bit dubious. Not supporting this would potentially lead to other confusing problems for users.

@hx2A
Copy link
Collaborator

hx2A commented Sep 30, 2021

Done! On to the next bug!

@hx2A hx2A closed this as completed Sep 30, 2021
@tabreturn
Copy link

Nice work, @hx2A!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants