# Buggy code
def on_update_menu(self, event, menu):
    import wx

    assert isinstance(menu, wx.Menu)
    menu_items = list(menu.GetMenuItems())
    breaks = (
        (self.MI_IMAGES, self.__images),
        (self.MI_OBJECTS, self.__objects),
        (self.MI_MASKS, self.__masks),
    )
    for start, item in enumerate(menu_items):
        assert isinstance(item, wx.MenuItem)
        if item.Label == self.MI_INTERPOLATION:
            break
    else:
        return
    window = self.__get_window_from_event(event)
    idx = start + 1
    if menu_items[idx].IsSeparator():
        idx += 1
    label_fmt = "--- %s ---"
    for key, sequence in breaks:
        label = label_fmt % key
        if idx >= len(menu_items) or menu_items[idx].Text != label:
            item = menu.Insert(idx, wx.NewId(), label)
            item.Enable(False)
            menu_items.insert(idx, item)
        idx += 1
        #
        # Pair data items with menu items
        #
        for data in sequence:
            name = data.name
            if (
                idx == len(menu_items)
                or menu_items[idx].Text.startswith("---")
                or menu_items[idx].IsSeparator()
            ):
                sub_menu = wx.Menu()
                my_id = wx.NewId()
                sub_menu_item = menu.InsertMenu(idx, my_id, name, sub_menu)
                if data.mode == MODE_HIDE:
                    sub_menu_item.Enable(False)
                menu_items.insert(idx, sub_menu_item)
                self.__initialize_sub_menu(event, sub_menu, data)

                def on_update_ui(event, sub_menu=sub_menu, data=data):
                    self.__update_sub_menu(event, sub_menu, data)

                window.Bind(wx.EVT_UPDATE_UI, on_update_ui, id=my_id)
                idx += 1
            else:
                self.__update_sub_menu(menu_items[idx], menu_items[idx].GetMenu(), data)
                idx += 1
        #
        # Remove excess menu items
        #
        while len(menu_items) < idx and menu_items[idx].IsEnabled():
            menu.RemoveItem(item)
            del menu_items[idx]


# Traceback (PyAssertionError)
Traceback (most recent call last):
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/pipelinecontroller.py", line 3154, in on_debug_view_workspace
show_workspace_viewer(self.__frame, workspace)
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/viewworkspace.py", line 41, in show_workspace_viewer
__the_workspace_viewer = ViewWorkspace(parent, workspace)
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/viewworkspace.py", line 383, in __init__
self.set_workspace(workspace)
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/viewworkspace.py", line 540, in set_workspace
self.update_menu(self.frame.menu_subplots)
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/viewworkspace.py", line 554, in update_menu
self.image.on_update_menu(event, menu)
File "/Users/dlogan/github/CellProfiler/cellprofiler/gui/cpartists.py", line 782, in on_update_menu
idx, my_id, name, sub_menu)
File "/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core.py", line 11120, in InsertMenu
return _core_.Menu_InsertMenu(*args, **kwargs)
PyAssertionError: C++ assertion "wxAssertFailure" failed at /BUILD/wxPython-src-2.8.12.1/src/common/stockitem.cpp(166) in wxGetStockLabel(): invalid stock item ID

# Fixed code
def on_update_menu(self, event, menu):
    import wx

    assert isinstance(menu, wx.Menu)
    menu_items = list(menu.GetMenuItems())
    breaks = (
        (self.MI_IMAGES, self.__images),
        (self.MI_OBJECTS, self.__objects),
        (self.MI_MASKS, self.__masks),
    )
    for start, item in enumerate(menu_items):
        assert isinstance(item, wx.MenuItem)
        if item.Label == self.MI_INTERPOLATION:
            break
    else:
        return
    window = self.__get_window_from_event(event)
    idx = start + 1
    if menu_items[idx].IsSeparator():
        idx += 1
    label_fmt = "--- %s ---"
    for key, sequence in breaks:
        label = label_fmt % key
        if idx >= len(menu_items) or menu_items[idx].Text != label:
            item = menu.Insert(idx, wx.NewId(), label)
            item.Enable(False)
            menu_items.insert(idx, item)
        idx += 1
        #
        # Pair data items with menu items
        #
        for data in sequence:
            name = data.name
            if (
                idx == len(menu_items)
                or menu_items[idx].Text.startswith("---")
                or menu_items[idx].IsSeparator()
            ):
                sub_menu = wx.Menu()
                my_id = wx.NewId()
                if len(name) == 0:
                    # otherwise bad things happen on Mac
                    # Can't have blank name and non-stock ID
                    name = " "
                sub_menu_item = menu.InsertMenu(idx, my_id, name, sub_menu)
                if data.mode == MODE_HIDE:
                    sub_menu_item.Enable(False)
                menu_items.insert(idx, sub_menu_item)
                self.__initialize_sub_menu(event, sub_menu, data)

                def on_update_ui(event, sub_menu=sub_menu, data=data):
                    self.__update_sub_menu(event, sub_menu, data)

                window.Bind(wx.EVT_UPDATE_UI, on_update_ui, id=my_id)
                idx += 1
            else:
                self.__update_sub_menu(menu_items[idx], menu_items[idx].GetMenu(), data)
                idx += 1
        #
        # Remove excess menu items
        #
        while len(menu_items) < idx and menu_items[idx].IsEnabled():
            menu.RemoveItem(item)
            del menu_items[idx]