# Buggy code
def _popupMenuDefinition(self):
    menuDefinition = IECore.MenuDefinition()

    if self.getPlug().getInput() is not None:
        menuDefinition.append(
            "/Edit input...", {"command": Gaffer.WeakMethod(self.__editInput)}
        )
        menuDefinition.append("/EditInputDivider", {"divider": True})
        menuDefinition.append(
            "/Remove input",
            {
                "command": Gaffer.WeakMethod(self.__removeInput),
                "active": self.getPlug().acceptsInput(None) and not self.getReadOnly(),
            },
        )
    if (
        hasattr(self.getPlug(), "defaultValue")
        and self.getPlug().direction() == Gaffer.Plug.Direction.In
    ):
        menuDefinition.append(
            "/Default",
            {
                "command": IECore.curry(
                    Gaffer.WeakMethod(self.__setValue), self.getPlug().defaultValue()
                ),
                "active": self._editable(),
            },
        )

    if (
        Gaffer.NodeAlgo.hasUserDefault(self.getPlug())
        and self.getPlug().direction() == Gaffer.Plug.Direction.In
    ):
        menuDefinition.append(
            "/User Default",
            {
                "command": Gaffer.WeakMethod(self.__applyUserDefault),
                "active": self._editable(),
            },
        )

    currentPreset = Gaffer.NodeAlgo.currentPreset(self.getPlug())
    for presetName in Gaffer.NodeAlgo.presets(self.getPlug()):
        menuDefinition.append(
            "/Preset/" + presetName,
            {
                "command": IECore.curry(
                    Gaffer.WeakMethod(self.__applyPreset), presetName
                ),
                "active": self._editable(),
                "checkBox": presetName == currentPreset,
            },
        )

    self.popupMenuSignal()(menuDefinition, self)

    return menuDefinition


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/GafferUI/Widget.py", line 879, in eventFilter
return self.__contextMenu( qObject, qEvent )
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/GafferUI/Widget.py", line 1053, in __contextMenu
return widget._contextMenuSignal( widget )
RuntimeError: Exception : Traceback (most recent call last):
File "/software/apps/cortex/9.0.0-a11/cent6.x86_64/base/python/2.7/gcc/4.1.2/IECore/curry.py", line 48, in curriedFunction
return func( *args, **kwds )
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/Gaffer/WeakMethod.py", line 67, in __call__
return m( *args, **kwArgs )
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/GafferUI/PlugValueWidget.py", line 410, in __contextMenu
menuDefinition = self._popupMenuDefinition()
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/GafferUI/PlugValueWidget.py", line 232, in _popupMenuDefinition
currentPreset = Gaffer.NodeAlgo.currentPreset( self.getPlug() )
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/Gaffer/NodeAlgo.py", line 60, in currentPreset
value = plug.getValue()
RuntimeError: Exception : Traceback (most recent call last):
File "/home/andrewk/apps/gaffer/0.6.0.0dev/cent6.x86_64/cortex/9/gaffer/python/Gaffer/PythonExpressionEngine.py", line 89, in execute
exec( self.__expression, executionDict, executionDict )
File "<string>", line 1, in <module>
RuntimeError: Exception : Context has no entry named "custom"

# Fixed code
def _popupMenuDefinition(self):
    menuDefinition = IECore.MenuDefinition()

    if self.getPlug().getInput() is not None:
        menuDefinition.append(
            "/Edit input...", {"command": Gaffer.WeakMethod(self.__editInput)}
        )
        menuDefinition.append("/EditInputDivider", {"divider": True})
        menuDefinition.append(
            "/Remove input",
            {
                "command": Gaffer.WeakMethod(self.__removeInput),
                "active": self.getPlug().acceptsInput(None) and not self.getReadOnly(),
            },
        )
    if (
        hasattr(self.getPlug(), "defaultValue")
        and self.getPlug().direction() == Gaffer.Plug.Direction.In
    ):
        menuDefinition.append(
            "/Default",
            {
                "command": IECore.curry(
                    Gaffer.WeakMethod(self.__setValue), self.getPlug().defaultValue()
                ),
                "active": self._editable(),
            },
        )

    if (
        Gaffer.NodeAlgo.hasUserDefault(self.getPlug())
        and self.getPlug().direction() == Gaffer.Plug.Direction.In
    ):
        menuDefinition.append(
            "/User Default",
            {
                "command": Gaffer.WeakMethod(self.__applyUserDefault),
                "active": self._editable(),
            },
        )

    with self.getContext():
        currentPreset = Gaffer.NodeAlgo.currentPreset(self.getPlug())

    for presetName in Gaffer.NodeAlgo.presets(self.getPlug()):
        menuDefinition.append(
            "/Preset/" + presetName,
            {
                "command": IECore.curry(
                    Gaffer.WeakMethod(self.__applyPreset), presetName
                ),
                "active": self._editable(),
                "checkBox": presetName == currentPreset,
            },
        )

    self.popupMenuSignal()(menuDefinition, self)

    return menuDefinition