use unicode_type instead of introspecting type('')

This commit is contained in:
Eli Schwartz 2019-09-02 21:58:27 -04:00 committed by Kovid Goyal
parent 519cf86786
commit c05fdfa1c6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
42 changed files with 109 additions and 108 deletions

View File

@ -120,16 +120,15 @@ def as_machine_data(book_ids, data, metadata):
def prepare_output_table(fields, book_ids, data, metadata):
ans = []
u = type('')
for book_id in book_ids:
row = []
ans.append(row)
for field in fields:
if field == 'id':
row.append(u(book_id))
row.append(unicode_type(book_id))
continue
val = data.get(field.replace('*', '#'), {}).get(book_id)
row.append(u(val).replace('\n', ' '))
row.append(unicode_type(val).replace('\n', ' '))
return ans

View File

@ -12,7 +12,7 @@ from operator import itemgetter
from calibre.library.field_metadata import fm_as_dict
from calibre.db.tests.base import BaseTest
from polyglot.builtins import iteritems, range, zip
from polyglot.builtins import iteritems, range, unicode_type, zip
from polyglot import reprlib
# Utils {{{
@ -116,7 +116,7 @@ class LegacyTest(BaseTest):
for label, loc in iteritems(db.FIELD_MAP):
if isinstance(label, numbers.Integral):
label = '#'+db.custom_column_num_map[label]['label']
label = type('')(label)
label = unicode_type(label)
ans[label] = tuple(db.get_property(i, index_is_id=True, loc=loc)
for i in db.all_ids())
if label in ('id', 'title', '#tags'):
@ -282,7 +282,7 @@ class LegacyTest(BaseTest):
old = db.get_data_as_dict(prefix='test-prefix')
new = ndb.get_data_as_dict(prefix='test-prefix')
for o, n in zip(old, new):
o = {type('')(k) if isinstance(k, bytes) else k:set(v) if isinstance(v, list) else v for k, v in iteritems(o)}
o = {unicode_type(k) if isinstance(k, bytes) else k:set(v) if isinstance(v, list) else v for k, v in iteritems(o)}
n = {k:set(v) if isinstance(v, list) else v for k, v in iteritems(n)}
self.assertEqual(o, n)

View File

@ -6,7 +6,7 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import OrderedDict
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, unicode_type
class Note(object):
@ -52,8 +52,8 @@ class Footnotes(object):
if note is not None and note.type == 'normal':
self.counter += 1
anchor = 'note_%d' % self.counter
self.notes[anchor] = (type('')(self.counter), note)
return anchor, type('')(self.counter)
self.notes[anchor] = (unicode_type(self.counter), note)
return anchor, unicode_type(self.counter)
return None, None
def __iter__(self):

View File

@ -9,7 +9,7 @@ from lxml.html.builder import TABLE, TR, TD
from calibre.ebooks.docx.block_styles import inherit, read_shd as rs, read_border, binary_property, border_props, ParagraphStyle, border_to_css
from calibre.ebooks.docx.char_styles import RunStyle
from polyglot.builtins import iteritems, itervalues, range, filter
from polyglot.builtins import filter, iteritems, itervalues, range, unicode_type
# Read from XML {{{
read_shd = rs
@ -646,9 +646,9 @@ class Table(object):
td = TD()
style_map[td] = s = self.style_map[tc]
if s.col_span is not inherit:
td.set('colspan', type('')(s.col_span))
td.set('colspan', unicode_type(s.col_span))
if s.row_span is not inherit:
td.set('rowspan', type('')(s.row_span))
td.set('rowspan', unicode_type(s.row_span))
td.tail = '\n\t\t\t'
tr.append(td)
for x in self.namespace.XPath('./w:p|./w:tbl')(tc):

View File

@ -128,7 +128,7 @@ class TextRun(object):
def style_weight(self):
ans = 0
for text, preserve_whitespace, bookmark in self.texts:
if isinstance(text, type('')):
if isinstance(text, unicode_type):
ans += len(text)
return ans

View File

@ -1,14 +1,13 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import re
from collections import deque
from calibre.utils.icu import capitalize, lower, upper
from polyglot.builtins import filter
from polyglot.builtins import filter, unicode_type
def cap_author_token(token):
@ -162,9 +161,9 @@ def find_tests():
def run(rules, authors, expected):
if isinstance(rules, dict):
rules = [rules]
if isinstance(authors, type('')):
if isinstance(authors, unicode_type):
authors = [x.strip() for x in authors.split('&')]
if isinstance(expected, type('')):
if isinstance(expected, unicode_type):
expected = [x.strip() for x in expected.split('&')]
ans = map_authors(authors, compile_rules(rules))
self.assertEqual(ans, expected)

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import deque
from polyglot.builtins import filter
from polyglot.builtins import filter, unicode_type
def compile_pat(pat):
@ -144,9 +145,9 @@ def find_tests():
def run(rules, tags, expected):
if isinstance(rules, dict):
rules = [rules]
if isinstance(tags, type('')):
if isinstance(tags, unicode_type):
tags = [x.strip() for x in tags.split(',')]
if isinstance(expected, type('')):
if isinstance(expected, unicode_type):
expected = [x.strip() for x in expected.split(',')]
ans = map_tags(tags, rules)
self.assertEqual(ans, expected)

View File

@ -18,7 +18,7 @@ from calibre.ebooks.oeb.base import OEB_STYLES, XHTML, css_text
from calibre.ebooks.oeb.normalize_css import normalizers, DEFAULTS
from calibre.ebooks.oeb.stylizer import media_ok, INHERITED
from tinycss.fonts3 import serialize_font_family, parse_font_family
from polyglot.builtins import iteritems, itervalues
from polyglot.builtins import iteritems, itervalues, unicode_type
_html_css_stylesheet = None
@ -233,8 +233,7 @@ _defvals = None
def defvals():
global _defvals
if _defvals is None:
u = type('')
_defvals = {k:Values(Property(k, u(val)).propertyValue) for k, val in iteritems(DEFAULTS)}
_defvals = {k:Values(Property(k, unicode_type(val)).propertyValue) for k, val in iteritems(DEFAULTS)}
return _defvals

View File

@ -203,7 +203,7 @@ class NonUTF8(BaseError):
def __call__(self, container):
raw = container.raw_data(self.name)
if isinstance(raw, type('')):
if isinstance(raw, unicode_type):
raw, changed = replace_encoding_declarations(raw)
if changed:
container.open(self.name, 'wb').write(raw.encode('utf-8'))

View File

@ -14,7 +14,6 @@ import time
import unicodedata
import uuid
from collections import defaultdict
from polyglot.builtins import iteritems, unicode_type, zip, map
from io import BytesIO
from itertools import count
@ -54,6 +53,7 @@ from calibre.utils.filenames import hardlink_file, nlinks_file
from calibre.utils.ipc.simple_worker import WorkerError, fork_job
from calibre.utils.logging import default_log
from calibre.utils.zipfile import ZipFile
from polyglot.builtins import iteritems, map, unicode_type, zip
from polyglot.urllib import urlparse
exists, join, relpath = os.path.exists, os.path.join, os.path.relpath
@ -672,7 +672,7 @@ class Container(ContainerBase): # {{{
''' The names of all manifest items whose media-type matches predicate.
`predicate` can be a set, a list, a string or a function taking a single
argument, which will be called with the media-type. '''
if isinstance(predicate, type('')):
if isinstance(predicate, unicode_type):
predicate = predicate.__eq__
elif hasattr(predicate, '__contains__'):
predicate = predicate.__contains__

View File

@ -184,7 +184,7 @@ def split(container, name, loc_or_xpath, before=True, totals=None):
'''
root = container.parsed(name)
if isinstance(loc_or_xpath, type('')):
if isinstance(loc_or_xpath, unicode_type):
split_point = root.xpath(loc_or_xpath)[0]
else:
try:

View File

@ -15,9 +15,8 @@ import regex
from calibre.ebooks.oeb.base import XHTML, css_text
from calibre.ebooks.oeb.polish.cascade import iterrules, resolve_styles, iterdeclaration
from calibre.utils.icu import ord_string, safe_chr
from polyglot.builtins import unicode_type
from polyglot.builtins import iteritems, itervalues, range, unicode_type
from tinycss.fonts3 import parse_font_family
from polyglot.builtins import iteritems, itervalues, range
def normalize_font_properties(font):
@ -162,7 +161,7 @@ def get_font_dict(elem, resolve_property, pseudo=None):
for p in 'weight', 'style', 'stretch':
p = 'font-' + p
rp = resolve_property(elem, p) if pseudo is None else resolve_property(elem, pseudo, p)
ans[p] = type('')(rp[0].value)
ans[p] = unicode_type(rp[0].value)
normalize_font_properties(ans)
return ans

View File

@ -12,7 +12,7 @@ from calibre.ptempfile import TemporaryDirectory
from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.utils.logging import DevNull
import calibre.ebooks.oeb.polish.container as pc
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, unicode_type
def get_cache():
@ -24,7 +24,7 @@ def get_cache():
def needs_recompile(obj, srcs):
if isinstance(srcs, type('')):
if isinstance(srcs, unicode_type):
srcs = [srcs]
try:
obj_mtime = os.stat(obj).st_mtime

View File

@ -18,7 +18,7 @@ from calibre.ebooks.oeb.polish.container import ContainerBase, href_to_name
from calibre.ebooks.oeb.polish.stats import StatsCollector, font_keys, normalize_font_properties, prepare_font_rule
from calibre.ebooks.oeb.polish.tests.base import BaseTest
from calibre.utils.logging import Log, Stream
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, unicode_type
class VirtualContainer(ContainerBase):
@ -83,7 +83,7 @@ class CascadeTest(BaseTest):
elem = next(select(selector))
ans = resolve_property(elem, name)
if val is None:
val = type('')(DEFAULTS[name])
val = unicode_type(DEFAULTS[name])
self.assertEqual(val, ans.cssText)
def test_pseudo_property(select, resolve_pseudo_property, selector, prop, name, val=None, abort_on_missing=False):
@ -94,7 +94,7 @@ class CascadeTest(BaseTest):
self.assertTrue(ans is None)
return
if val is None:
val = type('')(DEFAULTS[name])
val = unicode_type(DEFAULTS[name])
self.assertEqual(val, ans.cssText)
def get_maps(html, styles=None, pseudo=False):

View File

@ -64,7 +64,7 @@ def create_epub(manifest, spine=(), guide=(), meta_cover=None, ver=3):
</container>''')
zf.writestr('content.opf', opf.encode('utf-8'))
for name, data, properties in manifest:
if isinstance(data, type('')):
if isinstance(data, unicode_type):
data = data.encode('utf-8')
zf.writestr(name, data)
buf.seek(0)

View File

@ -9,7 +9,8 @@ import os
from lxml.html import tostring
from lxml.html.builder import (HTML, HEAD, BODY, TABLE, TR, TD, H2, STYLE)
from polyglot.builtins import range
from polyglot.builtins import range, unicode_type
def calculate_page_number(num, map_expression, evaljs):
@ -36,7 +37,7 @@ def convert_node(toc, table, level, pdf, pdf_page_number_map, evaljs):
a = anchors[path]
dest = a.get(frag, a[None])
num = calculate_page_number(pdf.page_tree.obj.get_num(dest[0]), pdf_page_number_map, evaljs)
tr[1].text = type('')(num)
tr[1].text = unicode_type(num)
table.append(tr)

View File

@ -613,7 +613,7 @@ class ChooseTheme(Dialog):
w.l = l = QGridLayout(w)
def add_row(x, y=None):
if isinstance(x, type('')):
if isinstance(x, unicode_type):
x = QLabel(x)
row = l.rowCount()
if y is None:

View File

@ -21,7 +21,7 @@ from calibre.gui2.widgets2 import Dialog
from calibre.gui2.progress_indicator import ProgressIndicator
from calibre.utils.config import JSONConfig
from calibre.utils.icu import numeric_sort_key as sort_key
from polyglot.builtins import iteritems, string_or_bytes, range
from polyglot.builtins import iteritems, string_or_bytes, range, unicode_type
ENTRY_ROLE = Qt.UserRole
@ -324,7 +324,7 @@ def choose_program(file_type='jpeg', parent=None, prefs=oprefs):
entry = choose_manually(file_type, parent) if d.select_manually else d.selected_entry
if entry is not None:
entry = finalize_entry(entry)
entry['uuid'] = type('')(uuid.uuid4())
entry['uuid'] = unicode_type(uuid.uuid4())
entries = oprefs['entries']
if oft not in entries:
entries[oft] = []

View File

@ -69,7 +69,7 @@ from calibre.utils.config import JSONConfig
from calibre.utils.icu import numeric_sort_key
from calibre.utils.imghdr import identify
from calibre.utils.tdir_in_cache import tdir_in_cache
from polyglot.builtins import iteritems, itervalues, string_or_bytes, map
from polyglot.builtins import iteritems, itervalues, string_or_bytes, map, unicode_type
from polyglot.urllib import urlparse
_diff_dialogs = []
@ -376,7 +376,7 @@ class Boss(QObject):
import traceback
traceback.print_exc()
if ef:
if isinstance(ef, type('')):
if isinstance(ef, unicode_type):
ef = [ef]
tuple(map(self.gui.file_list.request_edit, ef))
else:

View File

@ -112,7 +112,7 @@ def create_anchor_map(root):
def complete_anchor(name, data_conn):
if name not in file_cache:
data = raw = get_data(data_conn, 'file_data', name)
if isinstance(raw, type('')):
if isinstance(raw, unicode_type):
try:
root = parse(raw, decoder=lambda x:x.decode('utf-8'))
except Exception:

View File

@ -528,7 +528,7 @@ class DiffSplit(QSplitter): # {{{
def add_diff(self, left_name, right_name, left_text, right_text, context=None, syntax=None, beautify=False):
left_text, right_text = left_text or '', right_text or ''
is_identical = len(left_text) == len(right_text) and left_text == right_text and left_name == right_name
is_text = isinstance(left_text, type('')) and isinstance(right_text, type(''))
is_text = isinstance(left_text, unicode_type) and isinstance(right_text, unicode_type)
left_name = left_name or '[%s]'%_('This file was added')
right_name = right_name or '[%s]'%_('This file was removed')
self.left.headers.append((self.left.blockCount() - 1, left_name))

View File

@ -173,7 +173,7 @@ def snippets(refresh=False):
if _snippets is None or refresh:
_snippets = copy.deepcopy(builtin_snippets)
for snip in user_snippets.get('snippets', []):
if snip['trigger'] and isinstance(snip['trigger'], type('')):
if snip['trigger'] and isinstance(snip['trigger'], unicode_type):
key = snip_key(snip['trigger'], *snip['syntaxes'])
_snippets[key] = {'template':snip['template'], 'description':snip['description']}
_snippets = sorted(iteritems(_snippets), key=(lambda key_snip:string_length(key_snip[0].trigger)), reverse=True)

View File

@ -66,11 +66,11 @@ class EmbeddingData(Dialog):
text.append('<li style="margin-bottom:2em">' + '<b>' + face['path'] + '</b>')
name = face.get('full_name') or face.get('family_name') or face.get('subfamily_name')
if name:
text.append('<br>' + _('Name:') + '\xa0<b>' + type('')(name) + '</b>')
text.append('<br>' + _('Name:') + '\xa0<b>' + unicode_type(name) + '</b>')
if 'font-weight' in face:
text.append('<br>' + 'font-weight:\xa0' + type('')(face['font-weight']))
text.append('<br>' + 'font-weight:\xa0' + unicode_type(face['font-weight']))
if 'font-style' in face:
text.append('<br>' + 'font-style:\xa0' + type('')(face['font-style']))
text.append('<br>' + 'font-style:\xa0' + unicode_type(face['font-style']))
self.text.setHtml('\n'.join(text))

View File

@ -166,7 +166,7 @@ class NetworkReply(QNetworkReply):
QTimer.singleShot(0, self.check_for_parse)
else:
data = get_data(name)
if isinstance(data, type('')):
if isinstance(data, unicode_type):
data = data.encode('utf-8')
mime_type += '; charset=utf-8'
self.__data = data

View File

@ -265,7 +265,7 @@ class FilesModel(FileCollection):
return entry.basename
if col == 2:
sz = entry.size / 1024.
return ('%.2f' % sz if int(sz) != sz else type('')(sz))
return ('%.2f' % sz if int(sz) != sz else unicode_type(sz))
if col == 3:
return self.CATEGORY_NAMES.get(entry.category)
@ -447,9 +447,9 @@ class ImagesModel(FileCollection):
return entry.basename
if col == 1:
sz = entry.size / 1024.
return ('%.2f' % sz if int(sz) != sz else type('')(sz))
return ('%.2f' % sz if int(sz) != sz else unicode_type(sz))
if col == 2:
return type('')(len(entry.usage))
return unicode_type(len(entry.usage))
if col == 3:
return '%d x %d' % (entry.width, entry.height)
elif role == Qt.UserRole:
@ -713,7 +713,7 @@ class WordsModel(FileCollection):
ans += ' (%s)' % entry.locale.countrycode
return ans
if col == 2:
return type('')(len(entry.usage))
return unicode_type(len(entry.usage))
elif role == Qt.UserRole:
try:
return self.files[index.row()]
@ -802,7 +802,7 @@ class CharsModel(FileCollection):
if col == 2:
return ('U+%04X' if entry.codepoint < 0x10000 else 'U+%06X') % entry.codepoint
if col == 3:
return type('')(entry.count)
return unicode_type(entry.count)
elif role == Qt.UserRole:
try:
return self.files[index.row()]

View File

@ -320,7 +320,7 @@ class Main(MainWindow):
toolbar_actions[sid] = ac
if target is not None:
ac.triggered.connect(target)
if isinstance(keys, type('')):
if isinstance(keys, unicode_type):
keys = (keys,)
self.keyboard.register_shortcut(
sid, unicode_type(ac.text()).replace('&', ''), default_keys=keys, description=description, action=ac, group=group)

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import sys, subprocess, struct, os
from threading import Thread
from uuid import uuid4
@ -59,7 +59,7 @@ def serialize_binary(key, val):
def serialize_string(key, val):
key = key.encode('ascii') if not isinstance(key, bytes) else key
val = type('')(val).encode('utf-8')
val = unicode_type(val).encode('utf-8')
if len(val) > 2**16 - 1:
raise ValueError('%s is too long' % key)
return struct.pack(b'=B%dsH%ds' % (len(key), len(val)), len(key), key, len(val), val)
@ -198,7 +198,7 @@ def run_file_dialog(
from calibre import prints
from calibre.constants import DEBUG
if DEBUG:
prints('stdout+stderr from file dialog helper:', type('')([h.stdoutdata, h.stderrdata]))
prints('stdout+stderr from file dialog helper:', unicode_type([h.stdoutdata, h.stderrdata]))
if h.rc != 0:
raise Exception('File dialog failed (return code %s): %s' % (h.rc, get_errors()))
@ -211,7 +211,7 @@ def run_file_dialog(
return ()
parts = list(filter(None, server.data.split(b'\0')))
if DEBUG:
prints('piped data from file dialog helper:', type('')(parts))
prints('piped data from file dialog helper:', unicode_type(parts))
if len(parts) < 2:
return ()
if parts[0] != secret:
@ -310,7 +310,7 @@ class PipeServer(Thread):
def as_unicode(err):
try:
self.err_msg = type('')(err)
self.err_msg = unicode_type(err)
except Exception:
self.err_msg = repr(err)
try:

View File

@ -15,7 +15,7 @@ from calibre.srv.routes import Router
from calibre.srv.users import UserManager
from calibre.utils.date import utcnow
from calibre.utils.search_query_parser import ParseException
from polyglot.builtins import itervalues, filter
from polyglot.builtins import itervalues, filter, unicode_type
class Context(object):
@ -148,7 +148,7 @@ class Context(object):
if old is None or old[0] <= db.last_modified():
categories = db.get_categories(book_ids=restrict_to_ids, sort=opts.sort_by, first_letter_sort=opts.collapse_model == 'first letter')
data = json.dumps(render(db, categories), ensure_ascii=False)
if isinstance(data, type('')):
if isinstance(data, unicode_type):
data = data.encode('utf-8')
cache[key] = old = (utcnow(), data)
if len(cache) > self.CATEGORY_CACHE_SIZE:

View File

@ -23,7 +23,7 @@ from calibre.utils.socket_inheritance import set_socket_inherit
from calibre.utils.logging import ThreadSafeLog
from calibre.utils.monotonic import monotonic
from calibre.utils.mdns import get_external_ip
from polyglot.builtins import iteritems
from polyglot.builtins import iteritems, unicode_type
from polyglot.queue import Empty, Full
READ, WRITE, RDWR, WAIT = 'READ', 'WRITE', 'RDWR', 'WAIT'
@ -451,7 +451,7 @@ class ServerLoop(object):
self.socket.listen(min(socket.SOMAXCONN, 128))
self.bound_address = ba = self.socket.getsockname()
if isinstance(ba, tuple):
ba = ':'.join(map(type(''), ba))
ba = ':'.join(map(unicode_type, ba))
self.pool.start()
with TemporaryDirectory(prefix='srv-') as tdir:
self.tdir = tdir

View File

@ -143,7 +143,7 @@ class Route(object):
default = self.defaults[name] = eval(default)
if isinstance(default, numbers.Number):
self.type_checkers[name] = type(default)
if is_sponge and not isinstance(default, type('')):
if is_sponge and not isinstance(default, unicode_type):
raise route_error('Soak up path component must have a default value of string type')
else:
if found_optional_part is not False:

View File

@ -353,25 +353,25 @@ class TestHTTP(BaseTest):
conn = server.connect()
conn.request('GET', '/test')
r = conn.getresponse()
etag = type('')(r.getheader('ETag'))
etag = unicode_type(r.getheader('ETag'))
self.assertTrue(etag)
self.ae(r.getheader('Content-Type'), guess_type(f.name)[0])
self.ae(type('')(r.getheader('Accept-Ranges')), 'bytes')
self.ae(unicode_type(r.getheader('Accept-Ranges')), 'bytes')
self.ae(int(r.getheader('Content-Length')), len(fdata))
self.ae(r.status, http_client.OK), self.ae(r.read(), fdata)
conn.request('GET', '/test', headers={'Range':'bytes=2-25'})
r = conn.getresponse()
self.ae(r.status, http_client.PARTIAL_CONTENT)
self.ae(type('')(r.getheader('Accept-Ranges')), 'bytes')
self.ae(type('')(r.getheader('Content-Range')), 'bytes 2-25/%d' % len(fdata))
self.ae(unicode_type(r.getheader('Accept-Ranges')), 'bytes')
self.ae(unicode_type(r.getheader('Content-Range')), 'bytes 2-25/%d' % len(fdata))
self.ae(int(r.getheader('Content-Length')), 24)
self.ae(r.read(), fdata[2:26])
conn.request('GET', '/test', headers={'Range':'bytes=100000-'})
r = conn.getresponse()
self.ae(r.status, http_client.REQUESTED_RANGE_NOT_SATISFIABLE)
self.ae(type('')(r.getheader('Content-Range')), 'bytes */%d' % len(fdata))
self.ae(unicode_type(r.getheader('Content-Range')), 'bytes */%d' % len(fdata))
conn.request('GET', '/test', headers={'Range':'bytes=25-50', 'If-Range':etag})
r = conn.getresponse()

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import socket, os, struct, errno, numbers
from collections import deque, namedtuple
from functools import partial
@ -132,7 +132,7 @@ class WSClient(object):
def write_message(self, msg, chunk_size=None):
if isinstance(msg, tuple):
opcode, msg = msg
if isinstance(msg, type('')):
if isinstance(msg, unicode_type):
msg = msg.encode('utf-8')
return self.write_frame(1, opcode, msg)
w = MessageWriter(msg, self.mask, chunk_size)
@ -147,7 +147,7 @@ class WSClient(object):
self.socket.sendall(frame)
def write_close(self, code, reason=b''):
if isinstance(reason, type('')):
if isinstance(reason, unicode_type):
reason = reason.encode('utf-8')
self.write_frame(1, CLOSE, struct.pack(b'!H', code) + reason)

View File

@ -32,7 +32,7 @@ encode_name, decode_name
def http_date(timeval=None):
return type('')(formatdate(timeval=timeval, usegmt=True))
return unicode_type(formatdate(timeval=timeval, usegmt=True))
class MultiDict(dict): # {{{

View File

@ -21,6 +21,7 @@ from calibre.srv.loop import (
from calibre.srv.utils import DESIRED_SEND_BUFFER_SIZE
from calibre.utils.speedups import ReadOnlyFileBuffer
from polyglot import http_client
from polylot.builtins import unicode_type
from polyglot.binary import as_base64_unicode
from polyglot.queue import Empty, Queue
@ -184,7 +185,7 @@ class ReadFrame(object): # {{{
def create_frame(fin, opcode, payload, mask=None, rsv=0):
if isinstance(payload, type('')):
if isinstance(payload, unicode_type):
payload = payload.encode('utf-8')
l = len(payload)
header_len = 2 + (0 if l < 126 else 2 if 126 <= l <= 65535 else 8) + (0 if mask is None else 4)
@ -213,7 +214,7 @@ class MessageWriter(object):
def __init__(self, buf, mask=None, chunk_size=None):
self.buf, self.data_type, self.mask = buf, BINARY, mask
if isinstance(buf, type('')):
if isinstance(buf, unicode_type):
self.buf, self.data_type = ReadOnlyFileBuffer(buf.encode('utf-8')), TEXT
elif isinstance(buf, bytes):
self.buf = ReadOnlyFileBuffer(buf)
@ -428,7 +429,7 @@ class WebSocketConnection(HTTPConnection):
self.set_ws_state()
def websocket_close(self, code=NORMAL_CLOSE, reason=b''):
if isinstance(reason, type('')):
if isinstance(reason, unicode_type):
reason = reason.encode('utf-8')
self.stop_reading = True
reason = reason[:123]
@ -493,7 +494,7 @@ class WebSocketConnection(HTTPConnection):
''' Useful for streaming handlers that want to break up messages into
frames themselves. Note that these frames will be interleaved with
control frames, so they should not be too large. '''
opcode = (TEXT if isinstance(data, type('')) else BINARY) if is_first else CONTINUATION
opcode = (TEXT if isinstance(data, unicode_type) else BINARY) if is_first else CONTINUATION
fin = 1 if is_last else 0
frame = create_frame(fin, opcode, data)
with self.cf_lock:
@ -502,7 +503,7 @@ class WebSocketConnection(HTTPConnection):
def send_websocket_ping(self, data=b''):
''' Send a PING to the remote client, it should reply with a PONG which
will be sent to the handle_websocket_pong callback in your handler. '''
if isinstance(data, type('')):
if isinstance(data, unicode_type):
data = data.encode('utf-8')
frame = create_frame(True, PING, data)
with self.cf_lock:

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import os, json, struct, hashlib, sys, errno, tempfile, time, shutil, uuid
from collections import Counter
@ -25,7 +25,7 @@ def send_file(from_obj, to_obj, chunksize=1<<20):
break
m.update(raw)
to_obj.write(raw)
return type('')(m.hexdigest())
return unicode_type(m.hexdigest())
class FileDest(object):
@ -55,7 +55,7 @@ class FileDest(object):
def close(self):
if not self._discard:
size = self.exporter.f.tell() - self.start_pos
digest = type('')(self.hasher.hexdigest())
digest = unicode_type(self.hasher.hexdigest())
self.exporter.file_metadata[self.key] = (len(self.exporter.parts), self.start_pos, size, digest, self.mtime)
del self.exporter, self.hasher

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015-2019, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
import errno
@ -54,7 +53,7 @@ def get_exe_path(name):
def load_jxr_data(data):
with TemporaryDirectory() as tdir:
if iswindows and isinstance(tdir, type('')):
if iswindows and isinstance(tdir, unicode_type):
tdir = tdir.encode('mbcs')
with lopen(os.path.join(tdir, 'input.jxr'), 'wb') as f:
f.write(data)
@ -97,7 +96,7 @@ def image_from_path(path):
def image_from_x(x):
' Create an image from a bytestring or a path or a file like object. '
if isinstance(x, type('')):
if isinstance(x, unicode_type):
return image_from_path(x)
if hasattr(x, 'read'):
return image_from_data(x.read())
@ -512,7 +511,7 @@ def run_optimizer(file_path, cmd, as_filter=False, input_data=None):
# encodeable in mbcs, so we fail here, where it is more explicit,
# instead.
cmd = [x.encode('mbcs') if isinstance(x, unicode_type) else x for x in cmd]
if isinstance(cwd, type('')):
if isinstance(cwd, unicode_type):
cwd = cwd.encode('mbcs')
stdin = subprocess.PIPE if as_filter else None
stderr = subprocess.PIPE if as_filter else subprocess.STDOUT

View File

@ -1,13 +1,13 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
from struct import unpack, error
import os
from calibre.utils.speedups import ReadOnlyFileBuffer
from calibre.constants import ispy3
from polyglot.builtins import string_or_bytes
from polyglot.builtins import string_or_bytes, unicode_type
""" Recognize image file formats and sizes based on their first few bytes."""
@ -43,7 +43,7 @@ def identify(src):
recognized. '''
width = height = -1
if isinstance(src, type('')):
if isinstance(src, unicode_type):
stream = lopen(src, 'rb')
elif isinstance(src, bytes):
stream = ReadOnlyFileBuffer(src)

View File

@ -13,7 +13,7 @@ from calibre.constants import filesystem_encoding, cache_dir
from calibre.utils.icu import numeric_sort_key as sort_key
from calibre.utils.localization import canonicalize_lang, get_lang
from calibre.utils.serialize import msgpack_dumps, msgpack_loads
from polyglot.builtins import iteritems, itervalues, string_or_bytes
from polyglot.builtins import iteritems, itervalues, string_or_bytes, unicode_type
def parse_localized_key(key):
@ -66,7 +66,7 @@ def parse_desktop_file(path):
name, lang = parse_localized_key(k)
if name not in ans:
ans[name] = {}
if isinstance(ans[name], type('')):
if isinstance(ans[name], unicode_type):
ans[name] = {None:ans[name]}
ans[name][lang] = v
else:

View File

@ -39,7 +39,7 @@ def update_rapydscript():
base = d(d(d(d(d(abspath(__file__))))))
base = os.path.join(base, 'rapydscript')
raw = subprocess.check_output(['node', '--harmony', os.path.join(base, 'bin', 'export')])
if isinstance(raw, type('')):
if isinstance(raw, unicode_type):
raw = raw.encode('utf-8')
path = P(COMPILER_PATH, allow_user_override=False)
with open(path, 'wb') as f:
@ -273,7 +273,7 @@ def leading_whitespace(line):
def format_error(data):
return ':'.join(map(type(''), (data['file'], data['line'], data['col'], data['message'])))
return ':'.join(map(unicode_type, (data['file'], data['line'], data['col'], data['message'])))
class Repl(Thread):

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
'''
@ -10,6 +9,8 @@ Generate UUID encoded using a user specified alphabet.
import string, math, uuid as _uuid
from polyglot.builtins import unicode_type
def num_to_string(number, alphabet, alphabet_len, pad_to_length=None):
ans = []
@ -35,7 +36,7 @@ class ShortUUID(object):
# We do not include zero and one in the default alphabet as they can be
# confused with the letters O and I in some fonts. And removing them
# does not change the uuid_pad_len.
self.alphabet = tuple(sorted(type('')(alphabet or (string.digits + string.ascii_letters)[2:])))
self.alphabet = tuple(sorted(unicode_type(alphabet or (string.digits + string.ascii_letters)[2:])))
self.alphabet_len = len(self.alphabet)
self.alphabet_map = {c:i for i, c in enumerate(self.alphabet)}
self.uuid_pad_len = int(math.ceil(math.log(1 << 128, self.alphabet_len)))

View File

@ -8,6 +8,8 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import ctypes, ctypes.wintypes as types, struct, datetime, numbers
import winerror, win32con
from polyglot.builtins import unicode_type
try:
import winreg
except ImportError:
@ -111,11 +113,11 @@ def expand_environment_strings(src):
def convert_to_registry_data(value, has_expansions=False):
if value is None:
return None, winreg.REG_NONE, 0
if isinstance(value, (type(''), bytes)):
if isinstance(value, (unicode_type, bytes)):
buf = ctypes.create_unicode_buffer(value)
return buf, (winreg.REG_EXPAND_SZ if has_expansions else winreg.REG_SZ), len(buf) * 2
if isinstance(value, (list, tuple)):
buf = ctypes.create_unicode_buffer('\0'.join(map(type(''), value)) + '\0\0')
buf = ctypes.create_unicode_buffer('\0'.join(map(unicode_type, value)) + '\0\0')
return buf, winreg.REG_MULTI_SZ, len(buf) * 2
if isinstance(value, numbers.Integral):
try:

View File

@ -137,7 +137,7 @@ def writefile(path, data, enc='utf-8'):
if enc == undefined:
enc = 'utf-8'
try:
if isinstance(data, type('')):
if isinstance(data, unicode_type):
data = data.encode(enc or 'utf-8')
atomic_write(path, data)
except UnicodeEncodeError as e:
@ -170,8 +170,8 @@ class Function(object):
def to_python(x):
try:
if isinstance(x, (numbers.Number, type(''), bytes, bool)):
if isinstance(x, type('')):
if isinstance(x, (numbers.Number, unicode_type, bytes, bool)):
if isinstance(x, unicode_type):
x = x.encode('utf-8')
if isinstance(x, numbers.Integral):
x = int(x)
@ -195,11 +195,11 @@ class JSError(Exception):
if isinstance(e, dict):
if 'message' in e:
fn, ln = e.get('fileName'), e.get('lineNumber')
msg = type('')(e['message'])
msg = unicode_type(e['message'])
if ln:
msg = type('')(ln) + ':' + msg
msg = unicode_type(ln) + ':' + msg
if fn:
msg = type('')(fn) + ':' + msg
msg = unicode_type(fn) + ':' + msg
Exception.__init__(self, msg)
for k, v in e.items():
if k != 'message':
@ -207,11 +207,11 @@ class JSError(Exception):
else:
setattr(self, 'js_message', v)
else:
Exception.__init__(self, type('')(to_python(e)))
Exception.__init__(self, unicode_type(to_python(e)))
else:
# Happens if js code throws a string or integer rather than a
# subclass of Error
Exception.__init__(self, type('')(e))
Exception.__init__(self, unicode_type(e))
self.name = self.js_message = self.fileName = self.lineNumber = self.stack = None
def as_dict(self):
@ -246,7 +246,7 @@ def run_in_context(code, ctx, options=None):
except Exception as e:
import traceback
traceback.print_exc()
return [False, {'message':type('')(e)}]
return [False, {'message':unicode_type(e)}]
return [True, to_python(ans)]