For reference this is how I would write that block:

    if (getattr(document, 'editorconfig_whitespace_handler', None) and
        not trim_trailing_whitespace):
        # The trimmer exists, so remove it if necessary:
        document.disconnect(document.editorconfig_whitespace_handler)
        document.editorconfig_whitespace_handler = None
    elif trim_trailing_whitespace:
        # The trimmer does not exist, so install it if necessary:
        handler_id = document.connect('save', trim_whitespace_on_save)
        document.editorconfig_whitespace_handler = handler_id