From 305a1bab298e5d3e0d3ced3c9798043dd3949202 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Apr 2019 15:14:44 +0530 Subject: [PATCH] py3: port use of isinstance(str) --- src/calibre/__init__.py | 2 +- src/calibre/db/search.py | 2 +- src/calibre/ebooks/lrf/pylrs/pylrf.py | 8 ++++---- src/calibre/ebooks/lrf/pylrs/pylrs.py | 2 +- src/calibre/ebooks/metadata/__init__.py | 2 +- src/calibre/ebooks/oeb/base.py | 2 +- src/calibre/ebooks/rtf2xml/ParseRtf.py | 2 +- src/calibre/library/database2.py | 6 +++--- src/calibre/utils/Zeroconf.py | 2 +- src/calibre/utils/complete.py | 2 +- src/calibre/utils/config.py | 3 ++- src/calibre/web/feeds/news.py | 2 +- src/calibre/web/feeds/recipes/collection.py | 4 ++-- src/css_selectors/ordered_set.py | 7 +++---- 14 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index cd724b17fe..e9b10df6c8 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -220,7 +220,7 @@ def prints(*args, **kwargs): if not safe_encode: raise arg = repr(arg) - if not isinstance(arg, str): + if not isinstance(arg, bytes): try: arg = str(arg) except ValueError: diff --git a/src/calibre/db/search.py b/src/calibre/db/search.py index 19b9b00109..594ffde15c 100644 --- a/src/calibre/db/search.py +++ b/src/calibre/db/search.py @@ -203,7 +203,7 @@ class DateSearch(object): # {{{ field_count = query.count('/') + 1 for v, book_ids in field_iter(): - if isinstance(v, (str, unicode_type)): + if isinstance(v, string_or_bytes): v = parse_date(v) if v is not None and relop(dt_as_local(v), qd, field_count): matches |= book_ids diff --git a/src/calibre/ebooks/lrf/pylrs/pylrf.py b/src/calibre/ebooks/lrf/pylrs/pylrf.py index d39418399f..3b21c25402 100644 --- a/src/calibre/ebooks/lrf/pylrs/pylrf.py +++ b/src/calibre/ebooks/lrf/pylrs/pylrf.py @@ -10,7 +10,7 @@ import codecs import os from pylrfopt import tagListOptimizer -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, string_or_bytes PYLRF_VERSION = "1.0" @@ -130,7 +130,7 @@ def writeLineWidth(f, width): def writeUnicode(f, string, encoding): - if isinstance(string, str): + if isinstance(string, bytes): string = string.decode(encoding) string = string.encode("utf-16-le") length = len(string) @@ -141,7 +141,7 @@ def writeUnicode(f, string, encoding): def writeRaw(f, string, encoding): - if isinstance(string, str): + if isinstance(string, bytes): string = string.decode(encoding) string = string.encode("utf-16-le") @@ -398,7 +398,7 @@ class LrfTag(object): for f in self.format: if isinstance(f, dict): p = f[p] - elif isinstance(f, str): + elif isinstance(f, string_or_bytes): if isinstance(p, tuple): writeString(lrf, struct.pack(f, *p)) else: diff --git a/src/calibre/ebooks/lrf/pylrs/pylrs.py b/src/calibre/ebooks/lrf/pylrs/pylrs.py index 7b8e5c75f4..3d1a056749 100644 --- a/src/calibre/ebooks/lrf/pylrs/pylrs.py +++ b/src/calibre/ebooks/lrf/pylrs/pylrs.py @@ -1721,7 +1721,7 @@ class Text(LrsContainer): def toLrfContainer(self, lrfWriter, parent): if self.text: - if isinstance(self.text, str): + if isinstance(self.text, bytes): parent.appendLrfTag(LrfTag("rawtext", self.text)) else: parent.appendLrfTag(LrfTag("textstring", self.text)) diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index cd32c04ce6..f1dd980d23 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -217,7 +217,7 @@ class Resource(object): path = href_or_path if not os.path.isabs(path): path = os.path.abspath(os.path.join(basedir, path)) - if isinstance(path, str): + if isinstance(path, bytes): path = path.decode(sys.getfilesystemencoding()) self.path = path else: diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 767bba1bca..35204efa6b 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -1079,7 +1079,7 @@ class Manifest(object): return property(fget, fset, fdel, doc=doc) def unload_data_from_memory(self, memory=None): - if isinstance(self._data, (str, bytes)): + if isinstance(self._data, bytes): if memory is None: from calibre.ptempfile import PersistentTemporaryFile pt = PersistentTemporaryFile(suffix='_oeb_base_mem_unloader.img') diff --git a/src/calibre/ebooks/rtf2xml/ParseRtf.py b/src/calibre/ebooks/rtf2xml/ParseRtf.py index be979c733b..8321f5cccd 100755 --- a/src/calibre/ebooks/rtf2xml/ParseRtf.py +++ b/src/calibre/ebooks/rtf2xml/ParseRtf.py @@ -248,7 +248,7 @@ class ParseRtf: enc = 'cp' + enc msg = '%s\nException in token processing' % str(msg) if check_encoding_obj.check_encoding(self.__file, enc): - file_name = self.__file if isinstance(self.__file, str) \ + file_name = self.__file if isinstance(self.__file, bytes) \ else self.__file.encode('utf-8') msg +='\nFile %s does not appear to be correctly encoded.\n' % file_name try: diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 02c76a9f97..b7712501aa 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -3508,9 +3508,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if mi.series_index is None else mi.series_index aus = mi.author_sort if mi.author_sort else self.author_sort_from_authors(mi.authors) title = mi.title - if isinstance(aus, str): + if isinstance(aus, bytes): aus = aus.decode(preferred_encoding, 'replace') - if isinstance(title, str): + if isinstance(title, bytes): title = title.decode(preferred_encoding) obj = self.conn.execute('INSERT INTO books(title, series_index, author_sort) VALUES (?, ?, ?)', (title, series_index, aus)) @@ -3552,7 +3552,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if not mi.authors: mi.authors = [_('Unknown')] aus = mi.author_sort if mi.author_sort else self.author_sort_from_authors(mi.authors) - if isinstance(aus, str): + if isinstance(aus, bytes): aus = aus.decode(preferred_encoding, 'replace') title = mi.title if isinstance(mi.title, unicode_type) else \ mi.title.decode(preferred_encoding, 'replace') diff --git a/src/calibre/utils/Zeroconf.py b/src/calibre/utils/Zeroconf.py index c93231e731..9db8181e9d 100755 --- a/src/calibre/utils/Zeroconf.py +++ b/src/calibre/utils/Zeroconf.py @@ -1137,7 +1137,7 @@ class ServiceInfo(object): value = properties[key] if value is None: suffix = '' - elif isinstance(value, str): + elif isinstance(value, bytes): suffix = value elif isinstance(value, numbers.Integral): suffix = value and 'true' or 'false' diff --git a/src/calibre/utils/complete.py b/src/calibre/utils/complete.py index 7a66bf7a51..d688033fce 100644 --- a/src/calibre/utils/complete.py +++ b/src/calibre/utils/complete.py @@ -37,7 +37,7 @@ def prints(*args, **kwargs): if not safe_encode: raise arg = repr(arg) - if not isinstance(arg, str): + if not isinstance(arg, bytes): try: arg = str(arg) except ValueError: diff --git a/src/calibre/utils/config.py b/src/calibre/utils/config.py index 3d71e4b438..ffb0474374 100644 --- a/src/calibre/utils/config.py +++ b/src/calibre/utils/config.py @@ -22,6 +22,7 @@ from calibre.utils.config_base import ( tweaks, from_json, to_json ) from calibre.utils.lock import ExclusiveFile +from polyglot.builtins import string_or_bytes # optparse uses gettext.gettext instead of _ from builtins, so we @@ -379,7 +380,7 @@ class XMLConfig(dict): return self.defaults.get(key, default) def __setitem__(self, key, val): - if isinstance(val, (bytes, str)): + if isinstance(val, string_or_bytes): val = plistlib.Data(val) dict.__setitem__(self, key, val) self.commit() diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 101e68b510..90c33d4bcd 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -1104,7 +1104,7 @@ class BasicNewsRecipe(Recipe): self.image_map[feed.image_url] = img except: pass - if isinstance(feed.image_url, str): + if isinstance(feed.image_url, bytes): feed.image_url = feed.image_url.decode(sys.getfilesystemencoding(), 'strict') templ = (templates.TouchscreenFeedTemplate if self.touchscreen else diff --git a/src/calibre/web/feeds/recipes/collection.py b/src/calibre/web/feeds/recipes/collection.py index 94b900c4ed..07eff6f545 100644 --- a/src/calibre/web/feeds/recipes/collection.py +++ b/src/calibre/web/feeds/recipes/collection.py @@ -42,7 +42,7 @@ def serialize_recipe(urn, recipe_class): def attr(n, d): ans = getattr(recipe_class, n, d) - if isinstance(ans, str): + if isinstance(ans, bytes): ans = ans.decode('utf-8', 'replace') return ans @@ -66,7 +66,7 @@ def serialize_collection(mapping_of_recipe_classes): collection = E.recipe_collection() '''for u, x in mapping_of_recipe_classes.items(): print 11111, u, repr(x.title) - if isinstance(x.title, str): + if isinstance(x.title, bytes): x.title.decode('ascii') ''' for urn in sorted(mapping_of_recipe_classes.keys(), diff --git a/src/css_selectors/ordered_set.py b/src/css_selectors/ordered_set.py index 9d0950a547..f423b43b59 100644 --- a/src/css_selectors/ordered_set.py +++ b/src/css_selectors/ordered_set.py @@ -7,9 +7,11 @@ __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' import collections +from polyglot.builtins import string_or_bytes SLICE_ALL = slice(None) + def is_iterable(obj): """ Are we being asked to look up a list of things, instead of a single thing? @@ -18,11 +20,8 @@ def is_iterable(obj): Strings, however, should be considered as atomic values to look up, not iterables. - - We don't need to check for the Python 2 `unicode` type, because it doesn't - have an `__iter__` attribute anyway. """ - return hasattr(obj, '__iter__') and not isinstance(obj, str) + return hasattr(obj, '__iter__') and not isinstance(obj, string_or_bytes) class OrderedSet(collections.MutableSet):