# Buggy code
def _restore_clipboard_selection(self, backup: str):
    """Restore the selection clipboard content."""
    # Pasting takes some time, so wait a bit before restoring the content. Otherwise the restore is done before
    # the pasting happens, causing the backup to be pasted instead of the desired clipboard content.

    # Programmatically pressing the middle mouse button seems VERY slow, so wait rather long.
    # It might be a good idea to make this delay configurable. There might be systems that need even longer.
    time.sleep(1)
    self.clipboard.selection = 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 _restore_clipboard_selection(self, backup: str):
    """Restore the selection clipboard content."""
    # Pasting takes some time, so wait a bit before restoring the content. Otherwise the restore is done before
    # the pasting happens, causing the backup to be pasted instead of the desired clipboard content.

    # Programmatically pressing the middle mouse button seems VERY slow, so wait rather long.
    # It might be a good idea to make this delay configurable. There might be systems that need even longer.
    time.sleep(1)
    self.clipboard.selection = backup if backup is not None else ""