py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

warning when `no_smooth()` is not immediatly after size() #177

Closed villares closed 1 year ago

villares commented 1 year ago

When calling no_smooth() in setup(), I get a warning it was not called in the right moment. I think the settings() preparation code might be missing it.

def setup():
    size(200, 200)
    a = 10 # if you remove this line, it works
    no_smooth()

def draw():
    pass
noSmooth() can only be used inside setup()
When run from the PDE, noSmooth() is automatically moved from setup() to settings()
hx2A commented 1 year ago

That error message is confusing, and is coming from Processing, not py5. no_smooth() is supposed to be moved from setup() to settings(). If you move that line to before a = 10 or remove a = 10, it should work fine.

I wonder what can be done to improve that error message to make it more helpful to users.

villares commented 1 year ago

Maybe if you could intercept it and say: "no_smooth() can only be called once immediately after calling size()." That could be enough for most purposes.

On Wed, 2 Nov 2022, 22:19 hx2A, @.***> wrote:

That error message is confusing, and is coming from Processing, not py5. no_smooth() is supposed to be moved from setup() to settings(). If you move that line to before a = 10 or remove a = 10, it should work fine.

I wonder what can be done to improve that error message to make it more helpful to users.

— Reply to this email directly, view it on GitHub https://github.com/py5coding/py5generator/issues/177#issuecomment-1301541981, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4GADBCO6X72DKLCQKPYZLWGMHLRANCNFSM6AAAAAARVUVGSA . You are receiving this because you authored the thread.Message ID: @.***>

hx2A commented 1 year ago

Intercepting Processing from writing to stderr or stdout would be difficult if not impossible to do reliably.

no_smooth() is one of the special methods that py5 (and Processing) lets you put in setup() but gets moved settings() for you. The other py5 methods are smooth(), pixel_density(), full_screen() and size(). These methods need to be in your setup() before other Python statements (except for comments and global statements, which can be anywhere) for them to be correctly moved to settings(). If the setup() method contains those special methods after other Python statements, that code will remain in setup(), leaving you to get a Processing error which is confusing to a py5 user.

A better way to handle this is to maybe put a wrapper around those 5 special methods that can detect if the Sketch is inside settings() or not. If it is, great, and if not, it can intercept the call and throw an exception instead. That would prevent the Processing message and would enable us to provide the user with better feedback on the mistake.

Let me take a look and see how feasible this idea is.

hx2A commented 1 year ago

This idea will work, and is easy to implement. I can detect if the Sketch is in settings(), setup(), etc, when one of the special methods is called and throw an exception with a proper message when it is not settings().

villares commented 1 year ago

Sounds great!

I know you are doing clever introspection and etc, I was thinking of a far nastier hack... Dummy empty functions with the names no_smooth, size etc. So they don't have to be removed... A pre-processor (the sketch_runner?) scans for them anywhere in the user setup and builds an appropriate settings function.

hx2A commented 1 year ago

That's kind of what I will be doing, except I will wrap the real methods size(), no_smooth(), etc with decorators that only allow the call to continue if the Sketch is in settings(), and throws an exception otherwise. This is simple to implement and is sure to cover all the py5 modes.

hx2A commented 1 year ago

This idea is working well. Here's where I am now:

In [2]: def setup():
   ...:     py5.size(500, 500)
   ...:     a = 10
   ...:     py5.no_smooth()
   ...: 

In [3]: py5.run_sketch()

py5 encountered an error in your code:File "<ipython-input-2-ca48f90cb60e>", line 4, in _py5_faux_setup
    1    def setup():
    2        py5.size(500, 500)
    3        a = 10
--> 4        py5.no_smooth()

RuntimeError: Cannot use the no_smooth() method here. Either move it to a settings() function or move it to closer to the start of setup().

Next steps are to better word-smith that error message and test. Also, I might need to make some adjustments for py5bot.

hx2A commented 1 year ago

Fixed! All caught up on closing issues!

If you can isolate the PDF - SVG crashing problem you told me about, I'll see what I can do about fixing that also.

I intend to do a release of the accumulated bug fixes by the end of the month.