commit 21afbdc1d6247ed77697c31e50653f230ae4bfd5
Author: Kovid Goyal <kovid@kovidgoyal.net>
Date:   Wed Oct 30 08:28:14 2019 +0530

    py3: Fix #1850571 [[Python3] kobo driver fails to load](https://bugs.launchpad.net/calibre/+bug/1850571)

diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py
index f7ba01e0f9..10135eaeab 100644
--- a/src/calibre/devices/kobo/driver.py
+++ b/src/calibre/devices/kobo/driver.py
@@ -408,7 +408,7 @@ class KOBO(USBMS):
         # Remove books that are no longer in the filesystem. Cache contains
         # indices into the booklist if book not in filesystem, None otherwise
         # Do the operation in reverse order so indices remain valid
-        for idx in sorted(itervalues(bl_cache), reverse=True):
+        for idx in sorted(itervalues(bl_cache), reverse=True, key=lambda x: x or -1):
             if idx is not None:
                 need_sync = True
                 del bl[idx]
@@ -1971,7 +1971,7 @@ class KOBOTOUCH(KOBO):
         # Remove books that are no longer in the filesystem. Cache contains
         # indices into the booklist if book not in filesystem, None otherwise
         # Do the operation in reverse order so indices remain valid
-        for idx in sorted(itervalues(bl_cache), reverse=True):
+        for idx in sorted(itervalues(bl_cache), reverse=True, key=lambda x: x or -1):
             if idx is not None:
                 if not os.path.exists(self.normalize_path(os.path.join(prefix, bl[idx].lpath))) or not bl[idx].contentID:
                     need_sync = True
