Search code examples
python-2.7wxpythonpycharm

Setting a breakpoint in pycharm changes the behavior of the code


That's really a Pycharm - IDE question - the python/wx bug is fixed.

A seemingly innocuous change broke the raceText.SetLabel() call (which simply sets the text in the static text control) below. So I set a breakpoint in item = self.items[itemDex]:

def __init__(self):
    # ...
    self.raceText  = staticText(self,u'') # see below for staticText definition

def EvtListBox(self,event):
    """Responds to listbox selection."""
    itemDex = event.GetSelection()
    item = self.items[itemDex] # breakpoint here
    face = self.data[item]
    self.nameText.SetLabel(face.pcName)
    self.raceText.SetLabel(face.getRaceName()) # this

Lo and behold self.raceText.SetLabel(face.getRaceName()) now succeeded.

So how is this possible ? What does setting a breakpoint trigger ?

EDIT: some more data:

What originally broke the SetLabel() call was this commit:

-def staticText(parent,label=u'',pos=defPos,size=defSize,style=0,name=u"staticText",id=defId,):
+def staticText(parent, label=u'', pos=defPos, size=defSize, style=0,
+               noAutoResize=True, name=u"staticText"):
     """Static text element."""
-    return wx.StaticText(parent,id,label,pos,size,style,name)
+    if noAutoResize: style |= wx.ST_NO_AUTORESIZE
+    return wx.StaticText(parent, defId, label, pos, size, style, name)

Flipping noAutoResize default value to False squashed the bug - the text was set but wx.ST_NO_AUTORESIZE would prevent the control to adjust its size from u'' - so no text was displayed. So this was a plain old bug.

The question remains why on earth when setting a breakpoint in the debugger self.raceText.SetLabel() shows the text ?

EDIT: do read the answer - sanity check


Solution

  • IIUC, I think it's possible that running in the debugger is simply hiding or confusing the real issue.

    When wx.ST_NO_AUTORESIZE is not set then changes to the label will cause the widget's size to be adjusted, which will typically cause the widget to be repainted soon via an automatic paint event from the system. When wx.ST_NO_AUTORESIZE is set, then the resize doesn't happen and so that particular paint event doesn't happen and the widget may not get repainted until the next time the parent is refreshed or something else happens that triggers a paint event.

    So a likely fix would be to add a call to self.fooText.Refresh() after each SetLabel. The display will still not be updated while stopped in the debugger because the paint event normally won't happen until return to the active event loop, but in a normal run it will happen soon enough that the user will not notice unless there is some long-running thing blocking the return to the event loop.