Skip to content

Remove assert from Python script processor#5410

Merged
jtanx merged 1 commit intofontforge:masterfrom
iorsh:py_no_assert
Jul 16, 2024
Merged

Remove assert from Python script processor#5410
jtanx merged 1 commit intofontforge:masterfrom
iorsh:py_no_assert

Conversation

@iorsh
Copy link
Copy Markdown
Contributor

@iorsh iorsh commented Apr 24, 2024

Python methods normally exit gracefully in case of error. Killing FontForge due to incorrect scripting is generally undesirable.

Type of change

  • Bug fix

Copy link
Copy Markdown
Contributor

@skef skef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that the description on this PR reflects a confused idea of the place of assert() calls. Assertions are only complied when debugging flags are turned on. No one is expected to be running python scripts with debug-compiled code under normal circumstances, and if they do the goal should be to capture a problem at the point in the code where it is detected, which an assert() does just fine.

So the question here is how you can reach this part of the code while failing PyTuple_Check(), and whether that circumstance is "valid". If it is valid, then the change is appropriate because we need to handle it in RELEASE contexts. If it is not valid it should be fixed upstream.

@iorsh
Copy link
Copy Markdown
Contributor Author

iorsh commented Apr 25, 2024

Oh, my bad! In Release mode there is no crash, naturally. After the asset the workflow continues to address a non-tuple object as tuple and eventually leads to a misleading error message:

Traceback (most recent call last):
  File "/home/iorsh/.config/fontforge/python/main.py", line 15, in <module>
    fontforge.registerMenuItem(CreatePrecomposedGlyphs.CreatePrecomposedGlyphs,None,None,"Font",None,[1, 2],"Create Precomposed Glyphs");
ValueError: A `name` or `submenu` tuple must have either 2 or 3 elements

So maybe the patch is still worth something...

@skef
Copy link
Copy Markdown
Contributor

skef commented Apr 25, 2024

Note: I haven't the code leading up to this code. Unlike with other contributors I'm shifting to asking you the questions I would normally ask and research myself, both to lower my "workload" and to reflect the role you're (voluntarily) taking on.

Anyway ...

It sounds like you're saying that there are valid cases in which "args" won't be a tuple, where "valid" doesn't mean "correct", but "undesirable to fix up-stream". That is, this is the appropriate place to handle a case in which "args" is not a tuple (or string, given the earlier check).

If that's right, then yes, this PR is appropriate, not because asserts are bad in this context in theory, but because this assertion is wrong: "args" can be something other than a string or tuple at this point of execution.

If you confirm this analysis I'll approve and merge.

@iorsh
Copy link
Copy Markdown
Contributor Author

iorsh commented Apr 26, 2024

Well, the answer is probably no. I'm talking about a Python command with intentionally incorrect argument (a list where only tuple or string is permitted by spec) aimed at demonstrating the issue. The correct course of action for the user would be fixing the input.

Code in question:
fontforge.registerMenuItem(CreatePrecomposedGlyphs.CreatePrecomposedGlyphs,None,None,"Font",None,[1, 2],"Create Precomposed Glyphs");
The 6th argument [1,2] should not be list according to spec. Hope this clarifies the situation

@jtanx jtanx merged commit a6be404 into fontforge:master Jul 16, 2024
@iorsh iorsh deleted the py_no_assert branch March 28, 2025 07:34
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

Successfully merging this pull request may close these issues.

3 participants