# Buggy code
def _send_string_clipboard(self, string: str, paste_command: model.SendMode):
    """Use the clipboard to send a string."""
    backup = (
        self.clipboard.text
    )  # Keep a backup of current content, to restore the original afterwards.
    self.clipboard.text = string
    self.mediator.send_string(paste_command.value)
    # Because send_string is queued, also enqueue the clipboard restore, to keep the proper action ordering.
    self.__enqueue(self._restore_clipboard_text, backup)


# Traceback (RuntimeError)
2018-12-05 07:02:31,958 DEBUG - service - Raw key: 'b', modifiers: [], Key: b
2018-12-05 07:02:31,958 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:31,958 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:31,958 DEBUG - service - Input stack at end of handle_keypress: deque(['b'], maxlen=150)
2018-12-05 07:02:32,127 DEBUG - service - Raw key: ' ', modifiers: [], Key:
2018-12-05 07:02:32,128 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:32,128 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:32,132 DEBUG - iomediator - Send via clipboard
2018-12-05 07:02:32,133 DEBUG - service - Input stack at end of handle_keypress: deque([], maxlen=150)
2018-12-05 07:02:32,133 DEBUG - interface - Sending string via clipboard: a
2018-12-05 07:02:32,133 ERROR - service - Ignored locking error in handle_keypress
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/service.py", line 207, in __tryReleaseLock
self.configManager.lock.release()
RuntimeError: release unlocked lock
2018-12-05 07:02:32,135 DEBUG - interface - Sending via clipboard enqueued.
2018-12-05 07:02:32,135 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:32,137 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:32,139 DEBUG - iomediator - Send via event interface
2018-12-05 07:02:32,139 DEBUG - interface - Send modified key: modifiers: ['<shift>'] key: <insert>
2018-12-05 07:02:32,242 ERROR - interface - Error in X event loop thread
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 230, in __eventLoop
method(*args)
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 626, in _restore_clipboard_text
self.clipboard.text = backup
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 161, in text
self._clipboard.set_text(new_content, -1)
TypeError: Argument 1 does not allow None as a value
2018-12-05 07:02:33,039 DEBUG - service - Raw key: 'b', modifiers: [], Key: b
2018-12-05 07:02:33,040 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:33,040 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:33,041 DEBUG - service - Input stack at end of handle_keypress: deque(['b'], maxlen=150)
2018-12-05 07:02:33,318 DEBUG - service - Raw key: ' ', modifiers: [], Key:
2018-12-05 07:02:33,319 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:33,319 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:33,322 DEBUG - iomediator - Send via clipboard
2018-12-05 07:02:33,323 DEBUG - service - Input stack at end of handle_keypress: deque([], maxlen=150)
2018-12-05 07:02:33,325 DEBUG - interface - Sending string via clipboard: a
2018-12-05 07:02:33,327 ERROR - service - Ignored locking error in handle_keypress
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/service.py", line 207, in __tryReleaseLock
self.configManager.lock.release()
RuntimeError: release unlocked lock
2018-12-05 07:02:33,328 DEBUG - interface - Sending via clipboard enqueued.
2018-12-05 07:02:33,329 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:33,333 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]

# Fixed code
def _send_string_clipboard(self, string: str, paste_command: model.SendMode):
    """
    Use the clipboard to send a string.
    """
    backup = (
        self.clipboard.text
    )  # Keep a backup of current content, to restore the original afterwards.
    if backup is None:
        logger.warning(
            "Tried to backup the X clipboard content, but got None instead of a string."
        )
    self.clipboard.text = string
    try:
        self.mediator.send_string(paste_command.value)
    finally:
        self.ungrab_keyboard()
    # Because send_string is queued, also enqueue the clipboard restore, to keep the proper action ordering.
    self.__enqueue(self._restore_clipboard_text, backup)