diff --git a/setup/port.py b/setup/port.py index 6e426dc0dc..11f6343124 100644 --- a/setup/port.py +++ b/setup/port.py @@ -141,14 +141,20 @@ class UnicodeCheck(Base): str_pat = re.compile(r'\bstr\(') has_unicode_literals = False has_str_calls = False + num_lines = 0 for i, line in enumerate(open(f, 'rb')): line = line.decode('utf-8') + if not line.strip(): + continue + num_lines += 1 if not has_unicode_literals and uni_pat.match(line) is not None: has_unicode_literals = True if not has_str_calls and str_pat.search(line) is not None: has_str_calls = True if has_unicode_literals and has_str_calls: break + if num_lines < 1: + return ans = None if not has_unicode_literals: if has_str_calls: diff --git a/src/calibre/srv/auto_reload.py b/src/calibre/srv/auto_reload.py index 8378e4bdad..66ea949154 100644 --- a/src/calibre/srv/auto_reload.py +++ b/src/calibre/srv/auto_reload.py @@ -17,7 +17,7 @@ from calibre.srv.standalone import create_option_parser from calibre.srv.utils import create_sock_pair from calibre.srv.web_socket import DummyHandler from calibre.utils.monotonic import monotonic -from polyglot.builtins import itervalues, error_message +from polyglot.builtins import itervalues, error_message, native_string_type from polyglot.queue import Queue, Empty MAX_RETRIES = 10 @@ -384,7 +384,7 @@ class ReloadServer(Thread): while not self.loop.ready and self.is_alive(): time.sleep(0.01) self.address = self.loop.bound_address[:2] - os.environ['CALIBRE_AUTORELOAD_PORT'] = str(self.address[1]) + os.environ['CALIBRE_AUTORELOAD_PORT'] = native_string_type(self.address[1]) return self def __exit__(self, *args): diff --git a/src/calibre/srv/legacy.py b/src/calibre/srv/legacy.py index 196b42974e..925115c946 100644 --- a/src/calibre/srv/legacy.py +++ b/src/calibre/srv/legacy.py @@ -63,10 +63,10 @@ def build_search_box(num, search, sort, order, ctx, field_metadata, library_id): num_select = E.select(name='num') for option in (5, 10, 25, 100): - kwargs = {'value':str(option)} + kwargs = {'value':unicode_type(option)} if option == num: kwargs['SELECTED'] = 'SELECTED' - num_select.append(E.option(str(option), **kwargs)) + num_select.append(E.option(unicode_type(option), **kwargs)) num_select.tail = ' books matching ' form.append(num_select) diff --git a/src/calibre/srv/manage_users_cli.py b/src/calibre/srv/manage_users_cli.py index 83ea0e17e3..8e459356f5 100644 --- a/src/calibre/srv/manage_users_cli.py +++ b/src/calibre/srv/manage_users_cli.py @@ -9,7 +9,7 @@ from functools import partial from calibre import prints from calibre.constants import preferred_encoding -from polyglot.builtins import iteritems, raw_input, filter +from polyglot.builtins import iteritems, raw_input, filter, unicode_type # Manage users CLI {{{ @@ -39,7 +39,7 @@ def manage_users_cli(path=None): len(choices), _('default'), default + 1) reply = get_input(prompt) if not reply and default is not None: - reply = str(default + 1) + reply = unicode_type(default + 1) if not reply: prints(_('No choice selected, exiting...')) raise SystemExit(0) diff --git a/src/calibre/srv/metadata.py b/src/calibre/srv/metadata.py index 6d7bf56573..1b9bd20290 100644 --- a/src/calibre/srv/metadata.py +++ b/src/calibre/srv/metadata.py @@ -22,7 +22,7 @@ from calibre.utils.icu import collation_order from calibre.utils.localization import calibre_langcode_to_name from calibre.library.comments import comments_to_html, markdown from calibre.library.field_metadata import category_icon_map -from polyglot.builtins import iteritems, itervalues, range, filter +from polyglot.builtins import iteritems, itervalues, range, filter, unicode_type from polyglot.urllib import quote IGNORED_FIELDS = frozenset('cover ondevice path marked au_map size'.split()) @@ -117,7 +117,7 @@ def category_as_json(items, category, display_name, count, tooltip=None, parent= ans['is_user_category'] = True if is_first_letter: ans['is_first_letter'] = True - item_id = 'c' + str(len(items)) + item_id = 'c' + unicode_type(len(items)) items[item_id] = ans return item_id diff --git a/src/calibre/srv/opds.py b/src/calibre/srv/opds.py index a3e1ad637b..a5caf6ffea 100644 --- a/src/calibre/srv/opds.py +++ b/src/calibre/srv/opds.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import unicode_literals __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' @@ -36,7 +37,7 @@ def atom(ctx, rd, endpoint, output): rd.outheaders.set('Calibre-Instance-Id', force_unicode(prefs['installation_uuid'], 'utf-8'), replace_all=True) if isinstance(output, bytes): ans = output # Assume output is already UTF-8 XML - elif isinstance(output, type('')): + elif isinstance(output, unicode_type): ans = output.encode('utf-8') else: from lxml import etree @@ -116,7 +117,7 @@ PREVIOUS_LINK = partial(NAVLINK, rel='previous') def html_to_lxml(raw): - raw = u'
%s
'%raw + raw = '
%s
'%raw root = html.fragment_fromstring(raw) root.set('xmlns', "http://www.w3.org/1999/xhtml") raw = etree.tostring(root, encoding=None) @@ -143,7 +144,7 @@ def CATALOG_ENTRY(item, item_kind, request_context, updated, catalog_name, id_ = 'calibre:category:'+item.name iid = 'N' + item.name if item.id is not None: - iid = 'I' + str(item.id) + iid = 'I' + unicode_type(item.id) iid += ':'+item_kind href = request_context.url_for('/opds/category', category=as_hex_unicode(catalog_name), which=as_hex_unicode(iid)) link = NAVLINK(href=href) @@ -227,7 +228,7 @@ def ACQUISITION_ENTRY(book_id, updated, request_context): link = E.link(type=mt, href=get(what=fmt), rel="http://opds-spec.org/acquisition") ffm = fm.get(fmt.upper()) if ffm: - link.set('length', str(ffm['size'])) + link.set('length', unicode_type(ffm['size'])) link.set('mtime', ffm['mtime'].isoformat()) ans.append(link) ans.append(E.link(type='image/jpeg', href=get(what='cover'), rel="http://opds-spec.org/cover")) @@ -568,7 +569,7 @@ def opds_category(ctx, rd, category, which): ids = rc.db.get_books_for_category(q, which) sort_by = 'series' if category == 'series' else 'title' - return get_acquisition_feed(rc, ids, offset, page_url, up_url, 'calibre-category:'+category+':'+str(which), sort_by=sort_by) + return get_acquisition_feed(rc, ids, offset, page_url, up_url, 'calibre-category:'+category+':'+unicode_type(which), sort_by=sort_by) @endpoint('/opds/categorygroup/{category}/{which}', postprocess=atom) diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index c269f6d015..cc0cc9755f 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -81,7 +81,7 @@ def transform_declaration(decl): if unit in absolute_units: changed = True l = convert_fontsize(l, unit) - decl.change_property(prop, parent_prop, str(l) + 'rem') + decl.change_property(prop, parent_prop, unicode_type(l) + 'rem') return changed diff --git a/src/calibre/srv/standalone.py b/src/calibre/srv/standalone.py index d0a2668598..0024395c26 100644 --- a/src/calibre/srv/standalone.py +++ b/src/calibre/srv/standalone.py @@ -24,7 +24,7 @@ from calibre.srv.utils import RotatingLog from calibre.utils.config import prefs from calibre.utils.localization import localize_user_manual_link from calibre.utils.lock import singleinstance -from polyglot.builtins import error_message +from polyglot.builtins import error_message, unicode_type def daemonize(): # {{{ @@ -231,7 +231,7 @@ def main(args=sys.argv): daemonize() if opts.pidfile: with lopen(opts.pidfile, 'wb') as f: - f.write(str(os.getpid())) + f.write(unicode_type(os.getpid()).encode('ascii')) signal.signal(signal.SIGTERM, lambda s, f: server.stop()) if not getattr(opts, 'daemonize', False) and not iswindows: signal.signal(signal.SIGHUP, lambda s, f: server.stop()) diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 87836d22d6..4efeb4c708 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -15,7 +15,7 @@ from calibre.constants import ispy3 from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.utils import eintr_retry_call from calibre.utils.monotonic import monotonic -from polyglot.builtins import iteritems, range +from polyglot.builtins import iteritems, range, unicode_type from polyglot import http_client is_ci = os.environ.get('CI', '').lower() == 'true' @@ -172,7 +172,7 @@ class TestHTTP(BaseTest): r = conn.getresponse() self.ae(r.status, http_client.NOT_FOUND) self.assertIsNotNone(r.getheader('Date', None)) - self.ae(r.getheader('Content-Length'), str(len(body))) + self.ae(r.getheader('Content-Length'), unicode_type(len(body))) self.ae(r.getheader('Content-Type'), 'text/plain; charset=UTF-8') self.ae(len(r.getheaders()), 3) self.ae(r.read(), b'') @@ -338,7 +338,7 @@ class TestHTTP(BaseTest): conn = server.connect() conn.request('GET', '/an_etagged_path', headers={'Accept-Encoding':'gzip'}) r = conn.getresponse() - self.ae(str(len(raw)), r.getheader('Calibre-Uncompressed-Length')) + self.ae(unicode_type(len(raw)), r.getheader('Calibre-Uncompressed-Length')) self.ae(r.status, http_client.OK), self.ae(zlib.decompress(r.read(), 16+zlib.MAX_WBITS), raw) # Test dynamic etagged content diff --git a/src/calibre/srv/tests/loop.py b/src/calibre/srv/tests/loop.py index bf59cf7537..540958538a 100644 --- a/src/calibre/srv/tests/loop.py +++ b/src/calibre/srv/tests/loop.py @@ -18,7 +18,7 @@ from calibre.srv.tests.base import BaseTest, TestServer from calibre.ptempfile import TemporaryDirectory from calibre.utils.certgen import create_server_cert from calibre.utils.monotonic import monotonic -from polyglot.builtins import range +from polyglot.builtins import range, unicode_type from polyglot import http_client is_ci = os.environ.get('CI', '').lower() == 'true' @@ -94,7 +94,7 @@ class LoopTest(BaseTest): conn.request('GET', '/') with self.assertRaises(socket.timeout): res = conn.getresponse() - if str(res.status) == str(http_client.REQUEST_TIMEOUT): + if unicode_type(res.status) == unicode_type(http_client.REQUEST_TIMEOUT): raise socket.timeout('Timeout') raise Exception('Got unexpected response: code: %s %s headers: %r data: %r' % ( res.status, res.reason, res.getheaders(), res.read())) @@ -227,7 +227,7 @@ class LoopTest(BaseTest): s.bind(('localhost', 0)) port = s.getsockname()[1] self.ae(s.fileno(), 3) - os.environ['LISTEN_PID'] = str(os.getpid()) + os.environ['LISTEN_PID'] = unicode_type(os.getpid()) os.environ['LISTEN_FDS'] = '1' with TestServer(lambda data:(data.path[0] + data.read()), allow_socket_preallocation=True) as server: conn = server.connect()