Tuesday, July 26, 2011

Weird bug: wxPython hates ampersands

Yesterday I was discussing a new Tribler 5.4 feature (What kind of feature? Well, that's a secret. :p)** with Niels, when I mentioned some cosmetic bug that still had to be fixed in Tribler: ampersands are not displayed correctly, or at least, they are not displayed correctly in most of Tribler.

The bug was solved for the Manage My Channel panel in December 2010. Before that date, if you entered an URL of an RSS-feed that contained an ampersand, it wouldn't show up. Instead, it would underline the next character. The use-case for this particular behaviour is for indicating accelerator keys.

Now, the fix is quite simple. Just escape the ampersands before displaying them:
rsstext = wx.StaticText(rssPanel, -1, url.replace('&', '&&'))
After applying this simple fix, everything works:

Yesterday we decided to apply the same fix in other places of Tribler, for example in the search results list. Everything should just work, right? Well, guess again. No matter what we tried, wxPython refused to display the ampersand character properly. We are clueless why this strange behaviour occurred. If the same piece of code works in one part of the program, but not in another, what could possibly be wrong?

We turned on the debugger and tried to chase our value that was given to the StaticText constructor. Unfortunately, the constructor is nothing more than a shell around some compiled code:

We put a breakpoint right after the call to the C++ constructor to check the internal state of the StaticText. We saw that the value of the label argument was correct, but the Label and LabelText attributes of the StaticText were not. Aaaargh...

To end the blog post on a more positive note, the workaround that seems to work is to write this instead:
text = wx.StaticText(parent, -1)
But we might as well subclass StaticText or monkeypatch it...

** Although it's not really a secret if you know svn... :p

No comments:

Post a Comment