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

UnboundLocalError: local variable next_box_height is referenced before assignment #2106

Open
filipweidemann opened this issue Mar 22, 2024 · 2 comments
Labels
crash Problems preventing documents from being rendered

Comments

@filipweidemann
Copy link

Hey there!

This is my first issue here, so let me start by saying that this project is absolutely fantastic.

Back to topic:
we recently observed with one of our customers (using extremely weird data), that WeasyPrint has a slight programming error inside of layout/column.py:220 – here it tries to calculate next_box_height - empty_space but unfortunately a case can occur in which next_box_height is unbound. This is confirmed by myself after opening the project and inspecting the Pyright LSP output, which also complains about this with a red inline hint of "next_box_height is possibly unbound"

The sad thing is that I have absolutely no clue on how to reproduce this. It seems to be a very weird edge case with nested column layouts & weird &nbsp characters that only one of our customers seems to trigger.
I had no luck on narrowing this down to provide you with an example.

However, this is very clearly in itself an issue (which we just happened to run into in prod) and can be fixed without examples I assume?

My approach would be to just initialize next_box_height to 0 in the proper scope, this should probably catch this error. All other cases should remain the same, since next_box_height would be overwritten in those scopes.

I'll start by opening a PR with my intended fix, if you have any other ideas, just let me know!

Again, thanks very much for maintaining, I'll link the PR here shortly…

@grewn0uille grewn0uille added the crash Problems preventing documents from being rendered label Apr 9, 2024
@liZe
Copy link
Member

liZe commented Apr 10, 2024

Hi!

Thanks for your bug report and your pull request.

The sad thing is that I have absolutely no clue on how to reproduce this. It seems to be a very weird edge case with nested column layouts & weird &nbsp characters that only one of our customers seems to trigger.
I had no luck on narrowing this down to provide you with an example.

I’d really like to get a test for this, because we’ve gone through so many bugs like this for columns and we have to avoid regressions. I’ll try to find a way to reproduce. If you have a large example, don’t hesitate to share it. If you don’t want to do this publicly, you can send it to me by mail.

@filipweidemann
Copy link
Author

Hey, thanks for answering!

I can't really dig into this right now, but I will try to come up with a reproduction once I have some spare time at work as well. I also can't rule out that this error is only triggered on malformed HTML because we had proper issues with this when working with customer data.

Currently the bug is not being triggered anymore, this seems to confirm my suspicion that it is somehow related to the way our content looked like. It also only happened when trying to render columns and/or tables nested inside other columns.

Thanks for looking into it and I'll add an update here once I found something that specifically triggers this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Problems preventing documents from being rendered
Projects
None yet
Development

No branches or pull requests

3 participants