Remove py3 conditionals

This commit is contained in:
Kovid Goyal 2019-11-18 14:03:58 +05:30
parent 36d81d74d5
commit 764e8bff7e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
71 changed files with 192 additions and 3169 deletions

View File

@ -18,7 +18,6 @@ from datetime import date
base = os.path.dirname(os.path.abspath(__file__)) base = os.path.dirname(os.path.abspath(__file__))
sys.path.append(base) sys.path.append(base)
sys.path.insert(0, os.path.dirname(base)) sys.path.insert(0, os.path.dirname(base))
ispy3 = sys.version_info.major > 2
from setup import __appname__, __version__ from setup import __appname__, __version__
import calibre.utils.localization as l # Ensure calibre translations are installed import calibre.utils.localization as l # Ensure calibre translations are installed
import custom import custom
@ -101,7 +100,7 @@ if language not in {'en', 'eng'}:
except IOError: except IOError:
pass pass
else: else:
title = getattr(t, 'gettext' if ispy3 else 'ugettext')(title) title = t.gettext(title)
# If true, '()' will be appended to :func: etc. cross-reference text. # If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True # add_function_parentheses = True

View File

@ -11,7 +11,6 @@ from contextlib import contextmanager
is64bit = platform.architecture()[0] == '64bit' is64bit = platform.architecture()[0] == '64bit'
iswindows = re.search('win(32|64)', sys.platform) iswindows = re.search('win(32|64)', sys.platform)
ispy3 = sys.version_info.major > 2
isosx = 'darwin' in sys.platform isosx = 'darwin' in sys.platform
isfreebsd = 'freebsd' in sys.platform isfreebsd = 'freebsd' in sys.platform
isnetbsd = 'netbsd' in sys.platform isnetbsd = 'netbsd' in sys.platform
@ -128,52 +127,8 @@ def initialize_constants():
initialize_constants() initialize_constants()
preferred_encoding = 'utf-8' preferred_encoding = 'utf-8'
prints = print
if ispy3:
prints = print
else:
def prints(*args, **kwargs):
'''
Print unicode arguments safely by encoding them to preferred_encoding
Has the same signature as the print function from Python 3, except for the
additional keyword argument safe_encode, which if set to True will cause the
function to use repr when encoding fails.
'''
file = kwargs.get('file', sys.stdout)
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n')
enc = preferred_encoding
safe_encode = kwargs.get('safe_encode', False)
for i, arg in enumerate(args):
if isinstance(arg, type(u'')):
try:
arg = arg.encode(enc)
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
if not isinstance(arg, str):
try:
arg = str(arg)
except ValueError:
arg = type(u'')(arg)
if isinstance(arg, type(u'')):
try:
arg = arg.encode(enc)
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
file.write(arg)
if i != len(args)-1:
file.write(sep)
file.write(end)
warnings = [] warnings = []

View File

@ -1,8 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -10,14 +8,14 @@ __docformat__ = 'restructuredtext en'
import textwrap, os, shlex, subprocess, glob, shutil, re, sys, json import textwrap, os, shlex, subprocess, glob, shutil, re, sys, json
from collections import namedtuple from collections import namedtuple
from setup import Command, islinux, isbsd, isfreebsd, isosx, ishaiku, SRC, iswindows, __version__, ispy3 from setup import Command, islinux, isbsd, isfreebsd, isosx, ishaiku, SRC, iswindows, __version__
isunix = islinux or isosx or isbsd or ishaiku isunix = islinux or isosx or isbsd or ishaiku
py_lib = os.path.join(sys.prefix, 'libs', 'python%d%d.lib' % sys.version_info[:2]) py_lib = os.path.join(sys.prefix, 'libs', 'python%d%d.lib' % sys.version_info[:2])
def init_symbol_name(name): def init_symbol_name(name):
prefix = 'PyInit_' if ispy3 else 'init' prefix = 'PyInit_'
return prefix + name return prefix + name
@ -44,7 +42,7 @@ class Extension(object):
if iswindows: if iswindows:
self.cflags.append('/DCALIBRE_MODINIT_FUNC=PyMODINIT_FUNC') self.cflags.append('/DCALIBRE_MODINIT_FUNC=PyMODINIT_FUNC')
else: else:
return_type = 'PyObject*' if ispy3 else 'void' return_type = 'PyObject*'
extern_decl = 'extern "C"' if self.needs_cxx else '' extern_decl = 'extern "C"' if self.needs_cxx else ''
self.cflags.append( self.cflags.append(
@ -220,12 +218,8 @@ def init_env():
class Build(Command): class Build(Command):
short_description = 'Build calibre C/C++ extension modules' short_description = 'Build calibre C/C++ extension modules'
if ispy3: DEFAULT_OUTPUTDIR = os.path.abspath(os.path.join(SRC, 'calibre', 'plugins', sys.version_info.major))
DEFAULT_OUTPUTDIR = os.path.abspath(os.path.join(SRC, 'calibre', 'plugins', '3')) DEFAULT_BUILDDIR = os.path.abspath(os.path.join(os.path.dirname(SRC), 'build', sys.version_info.major))
DEFAULT_BUILDDIR = os.path.abspath(os.path.join(os.path.dirname(SRC), 'build', '3'))
else:
DEFAULT_OUTPUTDIR = os.path.abspath(os.path.join(SRC, 'calibre', 'plugins'))
DEFAULT_BUILDDIR = os.path.abspath(os.path.join(os.path.dirname(SRC), 'build'))
description = textwrap.dedent('''\ description = textwrap.dedent('''\
calibre depends on several python extensions written in C/C++. calibre depends on several python extensions written in C/C++.
@ -275,8 +269,6 @@ class Build(Command):
for ext in extensions: for ext in extensions:
if opts.only != 'all' and opts.only != ext.name: if opts.only != 'all' and opts.only != ext.name:
continue continue
if ext.needs_py2 and ispy3:
continue
if ext.error: if ext.error:
if ext.optional: if ext.optional:
self.warn(ext.error) self.warn(ext.error)

View File

@ -8,12 +8,6 @@
"windows_libraries": "libhunspell", "windows_libraries": "libhunspell",
"needs_c++11": true "needs_c++11": true
}, },
{
"name": "monotonic",
"sources": "calibre/utils/monotonic.c",
"linux_libraries": "rt",
"needs_py2": true
},
{ {
"name": "hyphen", "name": "hyphen",
"sources": "calibre/utils/hyphenation/hyphen.c", "sources": "calibre/utils/hyphenation/hyphen.c",
@ -34,15 +28,6 @@
"libraries": "m", "libraries": "m",
"windows_libraries": "" "windows_libraries": ""
}, },
{
"name": "zlib2",
"sources": "calibre/utils/zlib2.c",
"inc_dirs": "!zlib_inc_dirs",
"lib_dirs": "!zlib_lib_dirs",
"libraries": "z",
"windows_libraries": "zlib",
"needs_py2": true
},
{ {
"name": "certgen", "name": "certgen",
"sources": "calibre/utils/certgen.c", "sources": "calibre/utils/certgen.c",

View File

@ -11,7 +11,7 @@ from collections import defaultdict
from locale import normalize as normalize_locale from locale import normalize as normalize_locale
from functools import partial from functools import partial
from setup import Command, __appname__, __version__, require_git_master, build_cache_dir, edit_file, dump_json, ispy3 from setup import Command, __appname__, __version__, require_git_master, build_cache_dir, edit_file, dump_json
from setup.parallel_build import parallel_check_output from setup.parallel_build import parallel_check_output
from polyglot.builtins import codepoint_to_chr, iteritems, range from polyglot.builtins import codepoint_to_chr, iteritems, range
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -602,7 +602,7 @@ class Translations(POT): # {{{
lang_names = {} lang_names = {}
for l in dl: for l in dl:
translator = translator_for_lang(l)['translator'] translator = translator_for_lang(l)['translator']
t = getattr(translator, 'gettext' if ispy3 else 'ugettext') t = translator.gettext
t = partial(get_language, gettext_func=t) t = partial(get_language, gettext_func=t)
lang_names[l] = {x: t(x) for x in dl} lang_names[l] = {x: t(x) for x in dl}
zi = ZipInfo('lang-names.json') zi = ZipInfo('lang-names.json')

View File

@ -1,199 +0,0 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
import functools
from collections import namedtuple
from threading import RLock
_CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"])
@functools.wraps(functools.update_wrapper)
def update_wrapper(
wrapper,
wrapped,
assigned=functools.WRAPPER_ASSIGNMENTS,
updated=functools.WRAPPER_UPDATES
):
"""
Patch two bugs in functools.update_wrapper.
"""
# workaround for http://bugs.python.org/issue3445
assigned = tuple(attr for attr in assigned if hasattr(wrapped, attr))
wrapper = functools.update_wrapper(wrapper, wrapped, assigned, updated)
# workaround for https://bugs.python.org/issue17482
wrapper.__wrapped__ = wrapped
return wrapper
class _HashedSeq(list):
__slots__ = 'hashvalue'
def __init__(self, tup, hash=hash):
self[:] = tup
self.hashvalue = hash(tup)
def __hash__(self):
return self.hashvalue
def _make_key(
args,
kwds,
typed,
kwd_mark=(object(), ),
fasttypes=set([int, str, frozenset, type(None)]),
sorted=sorted,
tuple=tuple,
type=type,
len=len
):
'Make a cache key from optionally typed positional and keyword arguments'
key = args
if kwds:
sorted_items = sorted(kwds.items())
key += kwd_mark
for item in sorted_items:
key += item
if typed:
key += tuple(type(v) for v in args)
if kwds:
key += tuple(type(v) for k, v in sorted_items)
elif len(key) == 1 and type(key[0]) in fasttypes:
return key[0]
return _HashedSeq(key)
def lru_cache(maxsize=100, typed=False):
"""Least-recently-used cache decorator.
If *maxsize* is set to None, the LRU features are disabled and the cache
can grow without bound.
If *typed* is True, arguments of different types will be cached separately.
For example, f(3.0) and f(3) will be treated as distinct calls with
distinct results.
Arguments to the cached function must be hashable.
View the cache statistics named tuple (hits, misses, maxsize, currsize) with
f.cache_info(). Clear the cache and statistics with f.cache_clear().
Access the underlying function with f.__wrapped__.
See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
"""
# Users should only access the lru_cache through its public API:
# cache_info, cache_clear, and f.__wrapped__
# The internals of the lru_cache are encapsulated for thread safety and
# to allow the implementation to change (including a possible C version).
def decorating_function(user_function):
cache = dict()
stats = [0, 0] # make statistics updateable non-locally
HITS, MISSES = 0, 1 # names for the stats fields
make_key = _make_key
cache_get = cache.get # bound method to lookup key or return None
_len = len # localize the global len() function
lock = RLock() # because linkedlist updates aren't threadsafe
root = [] # root of the circular doubly linked list
root[:] = [root, root, None, None] # initialize by pointing to self
nonlocal_root = [root] # make updateable non-locally
PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
if maxsize == 0:
def wrapper(*args, **kwds):
# no caching, just do a statistics update after a successful call
result = user_function(*args, **kwds)
stats[MISSES] += 1
return result
elif maxsize is None:
def wrapper(*args, **kwds):
# simple caching without ordering or size limit
key = make_key(args, kwds, typed)
result = cache_get(
key, root
) # root used here as a unique not-found sentinel
if result is not root:
stats[HITS] += 1
return result
result = user_function(*args, **kwds)
cache[key] = result
stats[MISSES] += 1
return result
else:
def wrapper(*args, **kwds):
# size limited caching that tracks accesses by recency
key = make_key(args, kwds, typed) if kwds or typed else args
with lock:
link = cache_get(key)
if link is not None:
# record recent use of the key by moving it to the front of the list
root, = nonlocal_root
link_prev, link_next, key, result = link
link_prev[NEXT] = link_next
link_next[PREV] = link_prev
last = root[PREV]
last[NEXT] = root[PREV] = link
link[PREV] = last
link[NEXT] = root
stats[HITS] += 1
return result
result = user_function(*args, **kwds)
with lock:
root, = nonlocal_root
if key in cache:
# getting here means that this same key was added to the
# cache while the lock was released. since the link
# update is already done, we need only return the
# computed result and update the count of misses.
pass
elif _len(cache) >= maxsize:
# use the old root to store the new key and result
oldroot = root
oldroot[KEY] = key
oldroot[RESULT] = result
# empty the oldest link and make it the new root
root = nonlocal_root[0] = oldroot[NEXT]
oldkey = root[KEY]
root[KEY] = root[RESULT] = None
# now update the cache dictionary for the new links
del cache[oldkey]
cache[key] = oldroot
else:
# put result in a new link at the front of the list
last = root[PREV]
link = [last, root, key, result]
last[NEXT] = root[PREV] = cache[key] = link
stats[MISSES] += 1
return result
def cache_info():
"""Report cache statistics"""
with lock:
return _CacheInfo(stats[HITS], stats[MISSES], maxsize, len(cache))
def cache_clear():
"""Clear the cache and cache statistics"""
with lock:
cache.clear()
root = nonlocal_root[0]
root[:] = [root, root, None, None]
stats[:] = [0, 0]
wrapper.__wrapped__ = user_function
wrapper.cache_info = cache_info
wrapper.cache_clear = cache_clear
return update_wrapper(wrapper, user_function)
return decorating_function

View File

@ -18,7 +18,7 @@ except EnvironmentError:
from calibre.constants import (iswindows, isosx, islinux, isfrozen, from calibre.constants import (iswindows, isosx, islinux, isfrozen,
isbsd, preferred_encoding, __appname__, __version__, __author__, isbsd, preferred_encoding, __appname__, __version__, __author__,
win32event, win32api, winerror, fcntl, ispy3, win32event, win32api, winerror, fcntl,
filesystem_encoding, plugins, config_dir) filesystem_encoding, plugins, config_dir)
from calibre.startup import winutil, winutilerror from calibre.startup import winutil, winutilerror
from calibre.utils.icu import safe_chr from calibre.utils.icu import safe_chr
@ -446,30 +446,11 @@ class CurrentDir(object):
_ncpus = None _ncpus = None
if ispy3: def detect_ncpus():
def detect_ncpus(): global _ncpus
global _ncpus if _ncpus is None:
if _ncpus is None: _ncpus = max(1, os.cpu_count() or 1)
_ncpus = max(1, os.cpu_count() or 1) return _ncpus
return _ncpus
else:
def detect_ncpus():
"""Detects the number of effective CPUs in the system"""
global _ncpus
if _ncpus is None:
if iswindows:
import win32api
ans = win32api.GetSystemInfo()[5]
else:
import multiprocessing
ans = -1
try:
ans = multiprocessing.cpu_count()
except Exception:
from PyQt5.Qt import QThread
ans = QThread.idealThreadCount()
_ncpus = max(1, ans)
return _ncpus
relpath = os.path.relpath relpath = os.path.relpath

View File

@ -150,9 +150,7 @@ def cache_dir():
return ans return ans
plugins_loc = sys.extensions_location plugins_loc = os.path.join(sys.extensions_location, sys.version_info.major)
if ispy3:
plugins_loc = os.path.join(plugins_loc, '3')
# plugins {{{ # plugins {{{
@ -186,11 +184,6 @@ class Plugins(collections.Mapping):
'certgen', 'certgen',
'lzma_binding', 'lzma_binding',
] ]
if not ispy3:
plugins.extend([
'monotonic',
'zlib2',
])
if iswindows: if iswindows:
plugins.extend(['winutil', 'wpd', 'winfonts']) plugins.extend(['winutil', 'wpd', 'winfonts'])
if isosx: if isosx:

View File

@ -11,7 +11,6 @@ from collections import OrderedDict
from functools import partial from functools import partial
from calibre import as_unicode from calibre import as_unicode
from calibre.constants import ispy3
from calibre.customize import (Plugin, numeric_version, platform, from calibre.customize import (Plugin, numeric_version, platform,
InvalidPlugin, PluginNotFound) InvalidPlugin, PluginNotFound)
from polyglot.builtins import (itervalues, map, string_or_bytes, from polyglot.builtins import (itervalues, map, string_or_bytes,
@ -111,8 +110,8 @@ def load_translations(namespace, zfp):
from io import BytesIO from io import BytesIO
trans = _translations_cache[zfp] = GNUTranslations(BytesIO(mo)) trans = _translations_cache[zfp] = GNUTranslations(BytesIO(mo))
namespace['_'] = getattr(trans, 'gettext' if ispy3 else 'ugettext') namespace['_'] = trans.gettext
namespace['ngettext'] = getattr(trans, 'ngettext' if ispy3 else 'ungettext') namespace['ngettext'] = trans.ngettext
class PluginLoader(object): class PluginLoader(object):

View File

@ -16,7 +16,7 @@ from polyglot.builtins import (iteritems, itervalues,
from calibre import isbytestring, force_unicode, prints, as_unicode from calibre import isbytestring, force_unicode, prints, as_unicode
from calibre.constants import (iswindows, filesystem_encoding, from calibre.constants import (iswindows, filesystem_encoding,
preferred_encoding, ispy3) preferred_encoding)
from calibre.ptempfile import PersistentTemporaryFile, TemporaryFile from calibre.ptempfile import PersistentTemporaryFile, TemporaryFile
from calibre.db import SPOOL_SIZE from calibre.db import SPOOL_SIZE
from calibre.db.schema_upgrades import SchemaUpgrade from calibre.db.schema_upgrades import SchemaUpgrade
@ -1752,8 +1752,6 @@ class DB(object):
x = native_string_type(x) x = native_string_type(x)
x = x.encode('utf-8') if isinstance(x, unicode_type) else x x = x.encode('utf-8') if isinstance(x, unicode_type) else x
x = pickle_binary_string(x) x = pickle_binary_string(x)
if not ispy3:
x = buffer(x) # noqa
return x return x
options = [(book_id, fmt.upper(), map_data(data)) for book_id, data in iteritems(options)] options = [(book_id, fmt.upper(), map_data(data)) for book_id, data in iteritems(options)]
self.executemany('INSERT OR REPLACE INTO conversion_options(book,format,data) VALUES (?,?,?)', options) self.executemany('INSERT OR REPLACE INTO conversion_options(book,format,data) VALUES (?,?,?)', options)

View File

@ -10,7 +10,6 @@ import copy
from functools import partial from functools import partial
from polyglot.builtins import iteritems, unicode_type, map, native_string_type from polyglot.builtins import iteritems, unicode_type, map, native_string_type
from calibre.constants import ispy3
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from calibre.utils.icu import sort_key, collation_order from calibre.utils.icu import sort_key, collation_order
@ -47,15 +46,8 @@ class Tag(object):
def string_representation(self): def string_representation(self):
return u'%s:%s:%s:%s:%s'%(self.name, self.count, self.id, self.state, self.category) return u'%s:%s:%s:%s:%s'%(self.name, self.count, self.id, self.state, self.category)
if ispy3: def __str__(self):
def __str__(self): return self.string_representation
return self.string_representation
else:
def __str__(self):
return self.string_representation.encode('utf-8')
def __unicode__(self):
return self.string_representation
def __repr__(self): def __repr__(self):
return native_string_type(self) return native_string_type(self)

View File

@ -2,8 +2,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import json import json
import os import os
import sys import sys
@ -125,7 +123,7 @@ def read_credentials(opts):
pw = opts.password pw = opts.password
if pw: if pw:
if pw == '<stdin>': if pw == '<stdin>':
from calibre.utils.unicode_getpass import getpass from getpass import getpass
pw = getpass(_('Enter the password: ')) pw = getpass(_('Enter the password: '))
elif pw.startswith('<f:') and pw.endswith('>'): elif pw.startswith('<f:') and pw.endswith('>'):
with lopen(pw[3:-1], 'rb') as f: with lopen(pw[3:-1], 'rb') as f:

View File

@ -7,7 +7,6 @@ __docformat__ = 'restructuredtext en'
import os import os
from calibre.constants import ispy3
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type
from calibre.devices.usbms.driver import debug_print from calibre.devices.usbms.driver import debug_print
@ -190,7 +189,6 @@ class Bookmark(): # {{{
return ans return ans
if ispy3: __str__ = __unicode__
__str__ = __unicode__
# }}} # }}}

View File

@ -6,7 +6,7 @@ __docformat__ = 'restructuredtext en'
import os, time, sys import os, time, sys
from functools import cmp_to_key from functools import cmp_to_key
from calibre.constants import preferred_encoding, DEBUG, ispy3 from calibre.constants import preferred_encoding, DEBUG
from calibre import isbytestring from calibre import isbytestring
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
@ -118,8 +118,7 @@ class Book(Book_):
return super(Book,self).__unicode__() + u"\n" + ans return super(Book,self).__unicode__() + u"\n" + ans
if ispy3: __str__ = __unicode__
__str__ = __unicode__
class ImageWrapper(object): class ImageWrapper(object):

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -12,11 +11,11 @@ from collections import namedtuple
from functools import partial from functools import partial
from calibre import prints, as_unicode, force_unicode from calibre import prints, as_unicode, force_unicode
from calibre.constants import plugins, islinux, isosx, ispy3 from calibre.constants import plugins, islinux, isosx
from calibre.ptempfile import SpooledTemporaryFile from calibre.ptempfile import SpooledTemporaryFile
from calibre.devices.errors import OpenFailed, DeviceError, BlacklistedDevice, OpenActionNeeded from calibre.devices.errors import OpenFailed, DeviceError, BlacklistedDevice, OpenActionNeeded
from calibre.devices.mtp.base import MTPDeviceBase, synchronous, debug from calibre.devices.mtp.base import MTPDeviceBase, synchronous, debug
from polyglot.builtins import unicode_type, as_bytes from polyglot.builtins import unicode_type
MTPDevice = namedtuple('MTPDevice', 'busnum devnum vendor_id product_id ' MTPDevice = namedtuple('MTPDevice', 'busnum devnum vendor_id product_id '
'bcd serial manufacturer product') 'bcd serial manufacturer product')
@ -167,9 +166,8 @@ class MTP_DEVICE(MTPDeviceBase):
def create_device(self, connected_device): def create_device(self, connected_device):
d = connected_device d = connected_device
man, prod = d.manufacturer, d.product man, prod = d.manufacturer, d.product
if ispy3: man = force_unicode(man, 'utf-8') if isinstance(man, bytes) else man
man = force_unicode(man, 'utf-8') if isinstance(man, bytes) else man prod = force_unicode(prod, 'utf-8') if isinstance(prod, bytes) else prod
prod = force_unicode(prod, 'utf-8') if isinstance(prod, bytes) else prod
return self.libmtp.Device(d.busnum, d.devnum, d.vendor_id, return self.libmtp.Device(d.busnum, d.devnum, d.vendor_id,
d.product_id, man, prod, d.serial) d.product_id, man, prod, d.serial)
@ -399,9 +397,8 @@ class MTP_DEVICE(MTPDeviceBase):
sid, pid = parent.storage_id, parent.object_id sid, pid = parent.storage_id, parent.object_id
if pid == sid: if pid == sid:
pid = 0xFFFFFFFF pid = 0xFFFFFFFF
ename = name if ispy3 else as_bytes(name)
ans, errs = self.dev.put_file(sid, pid, ename, stream, size, callback) ans, errs = self.dev.put_file(sid, pid, name, stream, size, callback)
if ans is None: if ans is None:
raise DeviceError('Failed to upload file named: %s to %s: %s' raise DeviceError('Failed to upload file named: %s to %s: %s'
%(name, parent.full_path, self.format_errorstack(errs))) %(name, parent.full_path, self.format_errorstack(errs)))

View File

@ -6,7 +6,7 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import re import re
from polyglot.builtins import int_to_byte, is_py3, range from polyglot.builtins import int_to_byte, range
class TCRCompressor(object): class TCRCompressor(object):
@ -42,8 +42,6 @@ class TCRCompressor(object):
possible_codes.append(single_code.pop()) possible_codes.append(single_code.pop())
for code in possible_codes: for code in possible_codes:
if not is_py3:
code = bytearray(code)
self.coded_txt = self.coded_txt.replace(code, code[0:1]) self.coded_txt = self.coded_txt.replace(code, code[0:1])
self.codes[code[0]] = b'%s%s' % (self.codes[code[0]], self.codes[code[1]]) self.codes[code[0]] = b'%s%s' % (self.codes[code[0]], self.codes[code[1]])

View File

@ -17,7 +17,7 @@ from calibre.ebooks.oeb.base import urlunquote
from calibre.ebooks.chardet import detect_xml_encoding from calibre.ebooks.chardet import detect_xml_encoding
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre import unicode_path, as_unicode, replace_entities from calibre import unicode_path, as_unicode, replace_entities
from polyglot.builtins import is_py3, unicode_type from polyglot.builtins import unicode_type
from polyglot.urllib import urlparse, urlunparse from polyglot.urllib import urlparse, urlunparse
@ -67,9 +67,6 @@ class Link(object):
def __str__(self): def __str__(self):
return 'Link: %s --> %s'%(self.url, self.path) return 'Link: %s --> %s'%(self.url, self.path)
if not is_py3:
__unicode__ = __str__
class IgnoreFile(Exception): class IgnoreFile(Exception):

View File

@ -17,7 +17,6 @@ import calibre.ebooks.lit.mssha1 as mssha1
from calibre.ebooks.oeb.base import urlnormalize, xpath from calibre.ebooks.oeb.base import urlnormalize, xpath
from calibre.ebooks.oeb.reader import OEBReader from calibre.ebooks.oeb.reader import OEBReader
from calibre.ebooks import DRMError from calibre.ebooks import DRMError
from calibre.constants import ispy3
from calibre import plugins from calibre import plugins
from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, range, itervalues from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, range, itervalues
from polyglot.urllib import unquote as urlunquote, urldefrag from polyglot.urllib import unquote as urlunquote, urldefrag
@ -185,7 +184,7 @@ class UnBinary(object):
return self.unicode_representation return self.unicode_representation
def __str__(self): def __str__(self):
return self.unicode_representation if ispy3 else self.binary_representation return self.unicode_representation
def binary_to_text(self, bin, buf): def binary_to_text(self, bin, buf):
stack = [(0, None, None, 0, 0, False, False, 'text', 0)] stack = [(0, None, None, 0, 0, False, False, 'text', 0)]

View File

@ -7,7 +7,7 @@ import struct, array, zlib, io, collections, re
from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE
from calibre import entity_to_unicode, prepare_string_for_xml from calibre import entity_to_unicode, prepare_string_for_xml
from calibre.ebooks.lrf.tags import Tag from calibre.ebooks.lrf.tags import Tag
from polyglot.builtins import is_py3, unicode_type from polyglot.builtins import unicode_type
ruby_tags = { ruby_tags = {
0xF575: ['rubyAlignAndAdjust', 'W'], 0xF575: ['rubyAlignAndAdjust', 'W'],
@ -208,9 +208,6 @@ class StyleObject(object):
s += '/>\n' s += '/>\n'
return s return s
if not is_py3:
__unicode__ = __str__
def as_dict(self): def as_dict(self):
d = {} d = {}
for h in self.tag_map.values(): for h in self.tag_map.values():
@ -256,9 +253,6 @@ class Color(object):
def __str__(self): def __str__(self):
return '0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b) return '0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b)
if not is_py3:
__unicode__ = __str__
def __len__(self): def __len__(self):
return 4 return 4
@ -289,9 +283,6 @@ class PageDiv(EmptyPageElement):
return '\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\ return '\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\
(self.pain, self.spacesize, self.linewidth, self.color) (self.pain, self.spacesize, self.linewidth, self.color)
if not is_py3:
__unicode__ = __str__
class RuledLine(EmptyPageElement): class RuledLine(EmptyPageElement):
@ -307,9 +298,6 @@ class RuledLine(EmptyPageElement):
return '\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\ return '\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\
(self.linelength, self.linetype, self.linewidth, self.linecolor) (self.linelength, self.linetype, self.linewidth, self.linecolor)
if not is_py3:
__unicode__ = __str__
class Wait(EmptyPageElement): class Wait(EmptyPageElement):
@ -319,9 +307,6 @@ class Wait(EmptyPageElement):
def __str__(self): def __str__(self):
return '\n<Wait time="%d" />\n'%(self.time) return '\n<Wait time="%d" />\n'%(self.time)
if not is_py3:
__unicode__ = __str__
class Locate(EmptyPageElement): class Locate(EmptyPageElement):
@ -333,9 +318,6 @@ class Locate(EmptyPageElement):
def __str__(self): def __str__(self):
return '\n<Locate pos="%s" />\n'%(self.pos) return '\n<Locate pos="%s" />\n'%(self.pos)
if not is_py3:
__unicode__ = __str__
class BlockSpace(EmptyPageElement): class BlockSpace(EmptyPageElement):
@ -346,9 +328,6 @@ class BlockSpace(EmptyPageElement):
return '\n<BlockSpace xspace="%d" yspace="%d" />\n'%\ return '\n<BlockSpace xspace="%d" yspace="%d" />\n'%\
(self.xspace, self.yspace) (self.xspace, self.yspace)
if not is_py3:
__unicode__ = __str__
class Page(LRFStream): class Page(LRFStream):
tag_map = { tag_map = {
@ -450,9 +429,6 @@ class Page(LRFStream):
s += '\n</Page>\n' s += '\n</Page>\n'
return s return s
if not is_py3:
__unicode__ = __str__
def to_html(self): def to_html(self):
s = '' s = ''
for i in self: for i in self:
@ -641,9 +617,6 @@ class Block(LRFStream, TextCSS):
return s return s
return s.rstrip() + ' />\n' return s.rstrip() + ' />\n'
if not is_py3:
__unicode__ = __str__
def to_html(self): def to_html(self):
if self.name == 'TextBlock': if self.name == 'TextBlock':
return '<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html()) return '<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html())
@ -722,9 +695,6 @@ class Text(LRFStream):
s += '%s="%s" '%(name, val) s += '%s="%s" '%(name, val)
return s.rstrip() + (' />' if self.self_closing else '>') return s.rstrip() + (' />' if self.self_closing else '>')
if not is_py3:
__unicode__ = __str__
def to_html(self): def to_html(self):
s = '' s = ''
return s return s
@ -922,9 +892,6 @@ class Text(LRFStream):
raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],)) raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],))
return s return s
if not is_py3:
__unicode__ = __str__
def to_html(self): def to_html(self):
s = '' s = ''
open_containers = collections.deque() open_containers = collections.deque()
@ -971,9 +938,6 @@ class Image(LRFObject):
return '<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\ return '<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\
(self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream) (self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream)
if not is_py3:
__unicode__ = __str__
class PutObj(EmptyPageElement): class PutObj(EmptyPageElement):
@ -984,9 +948,6 @@ class PutObj(EmptyPageElement):
def __str__(self): def __str__(self):
return '<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj) return '<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj)
if not is_py3:
__unicode__ = __str__
class Canvas(LRFStream): class Canvas(LRFStream):
tag_map = { tag_map = {
@ -1035,9 +996,6 @@ class Canvas(LRFStream):
s += '</%s>\n'%(self.__class__.__name__,) s += '</%s>\n'%(self.__class__.__name__,)
return s return s
if not is_py3:
__unicode__ = __str__
def __iter__(self): def __iter__(self):
for i in self._contents: for i in self._contents:
yield i yield i
@ -1075,9 +1033,6 @@ class ImageStream(LRFStream):
return '<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\ return '<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\
(self.id, self.encoding, self.file) (self.id, self.encoding, self.file)
if not is_py3:
__unicode__ = __str__
class Import(LRFStream): class Import(LRFStream):
pass pass
@ -1167,9 +1122,6 @@ class Button(LRFObject):
s += '</Button>\n' s += '</Button>\n'
return s return s
if not is_py3:
__unicode__ = __str__
refpage = property(fget=lambda self : self.jump_action(2)[0]) refpage = property(fget=lambda self : self.jump_action(2)[0])
refobj = property(fget=lambda self : self.jump_action(2)[1]) refobj = property(fget=lambda self : self.jump_action(2)[1])
@ -1241,9 +1193,6 @@ class BookAttr(StyleObject, LRFObject):
s += '</BookStyle>\n' s += '</BookStyle>\n'
return s return s
if not is_py3:
__unicode__ = __str__
class SimpleText(Text): class SimpleText(Text):
pass pass
@ -1257,9 +1206,6 @@ class TocLabel(object):
def __str__(self): def __str__(self):
return '<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label) return '<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label)
if not is_py3:
__unicode__ = __str__
class TOCObject(LRFStream): class TOCObject(LRFStream):
@ -1287,9 +1233,6 @@ class TOCObject(LRFStream):
s += unicode_type(i) s += unicode_type(i)
return s + '</TOC>\n' return s + '</TOC>\n'
if not is_py3:
__unicode__ = __str__
object_map = [ object_map = [
None, # 00 None, # 00

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import copy, traceback import copy, traceback
from calibre import prints from calibre import prints
from calibre.constants import DEBUG, ispy3 from calibre.constants import DEBUG
from calibre.ebooks.metadata.book import (SC_COPYABLE_FIELDS, from calibre.ebooks.metadata.book import (SC_COPYABLE_FIELDS,
SC_FIELDS_COPY_NOT_NULL, STANDARD_METADATA_FIELDS, SC_FIELDS_COPY_NOT_NULL, STANDARD_METADATA_FIELDS,
TOP_LEVEL_IDENTIFIERS, ALL_METADATA_FIELDS) TOP_LEVEL_IDENTIFIERS, ALL_METADATA_FIELDS)
@ -810,13 +810,7 @@ class Metadata(object):
ans[i] = '<tr><td><b>%s</b></td><td>%s</td></tr>'%x ans[i] = '<tr><td><b>%s</b></td><td>%s</td></tr>'%x
return '<table>%s</table>'%'\n'.join(ans) return '<table>%s</table>'%'\n'.join(ans)
if ispy3: __str__ = __unicode__representation__
__str__ = __unicode__representation__
else:
__unicode__ = __unicode__representation__
def __str__(self):
return self.__unicode__().encode('utf-8')
def __nonzero__(self): def __nonzero__(self):
return bool(self.title or self.author or self.comments or self.tags) return bool(self.title or self.author or self.comments or self.tags)

View File

@ -13,7 +13,7 @@ import re, sys, unittest, functools, os, uuid, glob, io, json, copy
from lxml import etree from lxml import etree
from calibre.ebooks import escape_xpath_attr from calibre.ebooks import escape_xpath_attr
from calibre.constants import __appname__, __version__, filesystem_encoding, ispy3 from calibre.constants import __appname__, __version__, filesystem_encoding
from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata.toc import TOC
from calibre.ebooks.metadata.utils import parse_opf, pretty_print_opf as _pretty_print from calibre.ebooks.metadata.utils import parse_opf, pretty_print_opf as _pretty_print
from calibre.ebooks.metadata import string_to_authors, MetaInformation, check_isbn from calibre.ebooks.metadata import string_to_authors, MetaInformation, check_isbn
@ -205,13 +205,7 @@ class ManifestItem(Resource): # {{{
def __unicode__representation__(self): def __unicode__representation__(self):
return u'<item id="%s" href="%s" media-type="%s" />'%(self.id, self.href(), self.media_type) return u'<item id="%s" href="%s" media-type="%s" />'%(self.id, self.href(), self.media_type)
if ispy3: __str__ = __unicode__representation__
__str__ = __unicode__representation__
else:
__unicode__ = __unicode__representation__
def __str__(self):
return unicode_type(self).encode('utf-8')
def __repr__(self): def __repr__(self):
return unicode_type(self) return unicode_type(self)

View File

@ -1,4 +1,3 @@
''' '''
Basic support for manipulating OEB 1.x/2.0 content and metadata. Basic support for manipulating OEB 1.x/2.0 content and metadata.
''' '''
@ -14,7 +13,7 @@ from operator import attrgetter
from lxml import etree, html from lxml import etree, html
from calibre import force_unicode from calibre import force_unicode
from calibre.constants import filesystem_encoding, __version__, ispy3 from calibre.constants import filesystem_encoding, __version__
from calibre.translations.dynamic import translate from calibre.translations.dynamic import translate
from calibre.utils.xml_parse import safe_xml_fromstring from calibre.utils.xml_parse import safe_xml_fromstring
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
@ -756,15 +755,8 @@ class Metadata(object):
return 'Item(term=%r, value=%r, attrib=%r)' \ return 'Item(term=%r, value=%r, attrib=%r)' \
% (barename(self.term), self.value, self.attrib) % (barename(self.term), self.value, self.attrib)
if ispy3: def __str__(self):
def __str__(self): return as_unicode(self.value)
return as_unicode(self.value)
else:
def __str__(self):
return unicode_type(self.value).encode('ascii', 'xmlcharrefreplace')
def __unicode__(self):
return as_unicode(self.value)
def to_opf1(self, dcmeta=None, xmeta=None, nsrmap={}): def to_opf1(self, dcmeta=None, xmeta=None, nsrmap={}):
attrib = {} attrib = {}
@ -1099,15 +1091,8 @@ class Manifest(object):
def bytes_representation(self): def bytes_representation(self):
return serialize(self.data, self.media_type, pretty_print=self.oeb.pretty_print) return serialize(self.data, self.media_type, pretty_print=self.oeb.pretty_print)
if ispy3: def __str__(self):
def __str__(self): return self.unicode_representation
return self.unicode_representation
else:
def __unicode__(self):
return self.unicode_representation
def __str__(self):
return self.bytes_representation
def __eq__(self, other): def __eq__(self, other):
return self is other return self is other
@ -1617,15 +1602,8 @@ class TOC(object):
ans.extend(child.get_lines(lvl+1)) ans.extend(child.get_lines(lvl+1))
return ans return ans
if ispy3: def __str__(self):
def __str__(self): return '\n'.join(self.get_lines())
return '\n'.join(self.get_lines())
else:
def __unicode__(self):
return '\n'.join(self.get_lines())
def __str__(self):
return b'\n'.join([x.encode('utf-8') for x in self.get_lines()])
def to_opf1(self, tour): def to_opf1(self, tour):
for node in self.nodes: for node in self.nodes:

View File

@ -2,8 +2,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2008, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2008, Kovid Goyal <kovid at kovidgoyal.net>
import errno import errno
import os import os
import re import re
@ -11,23 +9,18 @@ import shutil
import subprocess import subprocess
import sys import sys
from calibre import CurrentDir, xml_replace_entities, prints from calibre import CurrentDir, prints, xml_replace_entities
from calibre.constants import ( from calibre.constants import isbsd, islinux, isosx, iswindows
filesystem_encoding, isbsd, islinux, isosx, ispy3, iswindows
)
from calibre.ebooks import ConversionError, DRMError from calibre.ebooks import ConversionError, DRMError
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.cleantext import clean_xml_chars from calibre.utils.cleantext import clean_xml_chars
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
PDFTOHTML = 'pdftohtml' PDFTOHTML = 'pdftohtml'
def popen(cmd, **kw): def popen(cmd, **kw):
if not ispy3:
cmd = [x.encode(filesystem_encoding) if not isinstance(x, bytes) else x for x in cmd]
if iswindows: if iswindows:
kw['creationflags'] = 0x08 kw['creationflags'] = 0x08
return subprocess.Popen(cmd, **kw) return subprocess.Popen(cmd, **kw)

View File

@ -10,7 +10,7 @@ import codecs, zlib, numbers
from io import BytesIO from io import BytesIO
from datetime import datetime from datetime import datetime
from calibre.constants import plugins, ispy3 from calibre.constants import plugins
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from polyglot.builtins import iteritems, unicode_type, codepoint_to_chr from polyglot.builtins import iteritems, unicode_type, codepoint_to_chr
from polyglot.binary import as_hex_bytes from polyglot.binary import as_hex_bytes
@ -69,7 +69,7 @@ def serialize(o, stream):
# Must check bool before int as bools are subclasses of int # Must check bool before int as bools are subclasses of int
stream.write_raw(b'true' if o else b'false') stream.write_raw(b'true' if o else b'false')
elif isinstance(o, numbers.Integral): elif isinstance(o, numbers.Integral):
stream.write_raw(unicode_type(o).encode('ascii') if ispy3 else bytes(o)) stream.write_raw(unicode_type(o).encode('ascii'))
elif hasattr(o, 'pdf_serialize'): elif hasattr(o, 'pdf_serialize'):
o.pdf_serialize(stream) o.pdf_serialize(stream)
elif o is None: elif o is None:

View File

@ -2,8 +2,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import functools import functools
import os import os
import subprocess import subprocess
@ -14,7 +12,7 @@ from threading import Thread
from PyQt5.Qt import QEventLoop from PyQt5.Qt import QEventLoop
from calibre import force_unicode from calibre import force_unicode
from calibre.constants import DEBUG, filesystem_encoding, ispy3, preferred_encoding from calibre.constants import DEBUG, filesystem_encoding, preferred_encoding
from calibre.utils.config import dynamic from calibre.utils.config import dynamic
from polyglot.builtins import getenv, reraise, string_or_bytes, unicode_type from polyglot.builtins import getenv, reraise, string_or_bytes, unicode_type
@ -107,14 +105,13 @@ def decode_output(raw):
def run(cmd): def run(cmd):
from calibre.gui2 import sanitize_env_vars from calibre.gui2 import sanitize_env_vars
ecmd = cmd if ispy3 else list(map(encode_arg, cmd))
if DEBUG: if DEBUG:
try: try:
print(ecmd) print(cmd)
except Exception: except Exception:
pass pass
with sanitize_env_vars(): with sanitize_env_vars():
p = subprocess.Popen(ecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
ret = p.wait() ret = p.wait()
return ret, decode_output(stdout), decode_output(stderr) return ret, decode_output(stdout), decode_output(stderr)

View File

@ -25,7 +25,7 @@ from calibre.library.custom_columns import CustomColumns
from calibre.library.sqlite import connect, IntegrityError from calibre.library.sqlite import connect, IntegrityError
from calibre.library.prefs import DBPrefs from calibre.library.prefs import DBPrefs
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.constants import preferred_encoding, iswindows, filesystem_encoding, ispy3 from calibre.constants import preferred_encoding, iswindows, filesystem_encoding
from calibre.ptempfile import (PersistentTemporaryFile, from calibre.ptempfile import (PersistentTemporaryFile,
base_dir, SpooledTemporaryFile) base_dir, SpooledTemporaryFile)
from calibre.customize.ui import (run_plugins_on_import, from calibre.customize.ui import (run_plugins_on_import,
@ -1752,10 +1752,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
return 'n=%s s=%s c=%d rt=%d rc=%d id=%s' % ( return 'n=%s s=%s c=%d rt=%d rc=%d id=%s' % (
self.n, self.s, self.c, self.rt, self.rc, self.id) self.n, self.s, self.c, self.rt, self.rc, self.id)
if ispy3: __str__ = __unicode_representation__
__str__ = __unicode_representation__
else:
__str__ = __unicode__ = __unicode_representation__
def clean_user_categories(self): def clean_user_categories(self):
user_cats = self.prefs.get('user_categories', {}) user_cats = self.prefs.get('user_categories', {})

View File

@ -2,8 +2,6 @@
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
# License: GPLv3 Copyright: 2008, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2008, Kovid Goyal <kovid at kovidgoyal.net>
''' Post installation script for linux ''' ''' Post installation script for linux '''
import sys, os, textwrap, stat, errno import sys, os, textwrap, stat, errno
@ -11,7 +9,7 @@ from subprocess import check_call, check_output
from functools import partial from functools import partial
from calibre import __appname__, prints, guess_type from calibre import __appname__, prints, guess_type
from calibre.constants import islinux, isbsd, ispy3 from calibre.constants import islinux, isbsd
from calibre.customize.ui import all_input_formats from calibre.customize.ui import all_input_formats
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre import CurrentDir from calibre import CurrentDir
@ -1191,7 +1189,7 @@ def write_appdata(key, entry, base, translators):
for para in entry['description']: for para in entry['description']:
description.append(E.p(para)) description.append(E.p(para))
for lang, t in iteritems(translators): for lang, t in iteritems(translators):
tp = getattr(t, 'gettext' if ispy3 else 'ugettext')(para) tp = t.gettext(para)
if tp != para: if tp != para:
description.append(E.p(tp)) description.append(E.p(tp))
description[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang) description[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang)
@ -1208,7 +1206,7 @@ def write_appdata(key, entry, base, translators):
type='desktop' type='desktop'
) )
for lang, t in iteritems(translators): for lang, t in iteritems(translators):
tp = getattr(t, 'gettext' if ispy3 else 'ugettext')(entry['summary']) tp = t.gettext(entry['summary'])
if tp != entry['summary']: if tp != entry['summary']:
root.append(E.summary(tp)) root.append(E.summary(tp))
root[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang) root[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang)

View File

@ -9,7 +9,7 @@ import tempfile, os, atexit
from polyglot.builtins import map, getenv from polyglot.builtins import map, getenv
from calibre.constants import (__version__, __appname__, filesystem_encoding, from calibre.constants import (__version__, __appname__, filesystem_encoding,
iswindows, get_windows_temp_path, isosx, ispy3) iswindows, get_windows_temp_path, isosx)
def cleanup(path): def cleanup(path):
@ -265,23 +265,17 @@ class SpooledTemporaryFile(tempfile.SpooledTemporaryFile):
suffix = '' suffix = ''
if dir is None: if dir is None:
dir = base_dir() dir = base_dir()
if ispy3: self._name = None
self._name = None tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size,
tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size, suffix=suffix, prefix=prefix, dir=dir, mode=mode)
suffix=suffix, prefix=prefix, dir=dir, mode=mode)
else:
tempfile.SpooledTemporaryFile.__init__(self, max_size=max_size,
suffix=suffix, prefix=prefix, dir=dir, mode=mode,
bufsize=bufsize)
if ispy3: @property
@property def name(self):
def name(self): return self._name
return self._name
@name.setter @name.setter
def name(self, val): def name(self, val):
self._name = val self._name = val
def truncate(self, *args): def truncate(self, *args):
# The stdlib SpooledTemporaryFile implementation of truncate() doesn't # The stdlib SpooledTemporaryFile implementation of truncate() doesn't

View File

@ -14,7 +14,7 @@ from itertools import chain
from calibre import prints from calibre import prints
from calibre.constants import ( from calibre.constants import (
config_dir, filesystem_encoding, ispy3, iswindows, plugins config_dir, filesystem_encoding, iswindows, plugins
) )
from calibre.spell import parse_lang_code from calibre.spell import parse_lang_code
from calibre.utils.config import JSONConfig from calibre.utils.config import JSONConfig
@ -174,8 +174,6 @@ def load_dictionary(dictionary):
path = os.path.abspath(path) path = os.path.abspath(path)
if iswindows: if iswindows:
path = r'\\?\{}'.format(path) path = r'\\?\{}'.format(path)
if not ispy3:
path = path.encode('utf-8')
return path return path
obj = hunspell.Dictionary(fix_path(dictionary.dicpath), fix_path(dictionary.affpath)) obj = hunspell.Dictionary(fix_path(dictionary.dicpath), fix_path(dictionary.affpath))

View File

@ -12,10 +12,10 @@ from itertools import chain, repeat
from operator import itemgetter from operator import itemgetter
from functools import wraps from functools import wraps
from polyglot.builtins import iteritems, itervalues, reraise, map, is_py3, unicode_type, string_or_bytes from polyglot.builtins import iteritems, itervalues, reraise, map, unicode_type, string_or_bytes
from calibre import guess_type, force_unicode from calibre import guess_type, force_unicode
from calibre.constants import __version__, plugins, ispy3 from calibre.constants import __version__
from calibre.srv.loop import WRITE from calibre.srv.loop import WRITE
from calibre.srv.errors import HTTPSimpleResponse from calibre.srv.errors import HTTPSimpleResponse
from calibre.srv.http_request import HTTPRequest, read_headers from calibre.srv.http_request import HTTPRequest, read_headers
@ -33,15 +33,8 @@ MULTIPART_SEPARATOR = uuid.uuid4().hex
if isinstance(MULTIPART_SEPARATOR, bytes): if isinstance(MULTIPART_SEPARATOR, bytes):
MULTIPART_SEPARATOR = MULTIPART_SEPARATOR.decode('ascii') MULTIPART_SEPARATOR = MULTIPART_SEPARATOR.decode('ascii')
COMPRESSIBLE_TYPES = {'application/json', 'application/javascript', 'application/xml', 'application/oebps-package+xml'} COMPRESSIBLE_TYPES = {'application/json', 'application/javascript', 'application/xml', 'application/oebps-package+xml'}
if is_py3: import zlib
import zlib from itertools import zip_longest
from itertools import zip_longest
else:
zlib, zlib2_err = plugins['zlib2']
if zlib2_err:
raise RuntimeError('Failed to load the zlib2 module with error: ' + zlib2_err)
del zlib2_err
from itertools import izip_longest as zip_longest
def header_list_to_file(buf): # {{{ def header_list_to_file(buf): # {{{
@ -290,8 +283,8 @@ class RequestData(object): # {{{
if lang_code != self.lang_code: if lang_code != self.lang_code:
found, lang, t = self.get_translator(lang_code) found, lang, t = self.get_translator(lang_code)
self.lang_code = lang self.lang_code = lang
self.gettext_func = getattr(t, 'gettext' if ispy3 else 'ugettext') self.gettext_func = t.gettext
self.ngettext_func = getattr(t, 'ngettext' if ispy3 else 'ungettext') self.ngettext_func = t.ngettext
# }}} # }}}

View File

@ -2,8 +2,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import sys import sys
from functools import partial from functools import partial
@ -77,7 +75,7 @@ def manage_users_cli(path=None):
return get_valid(_('Enter the username'), validate) return get_valid(_('Enter the username'), validate)
def get_pass(username): def get_pass(username):
from calibre.utils.unicode_getpass import getpass from getpass import getpass
while True: while True:
one = getpass( one = getpass(

View File

@ -12,11 +12,8 @@ from functools import partial
from calibre.constants import config_dir from calibre.constants import config_dir
from calibre.utils.lock import ExclusiveFile from calibre.utils.lock import ExclusiveFile
from polyglot.builtins import itervalues, is_py3 from polyglot.builtins import itervalues
if is_py3: from itertools import zip_longest
from itertools import zip_longest
else:
from itertools import izip_longest as zip_longest
Option = namedtuple('Option', 'name default longdoc shortdoc choices') Option = namedtuple('Option', 'name default longdoc shortdoc choices')

View File

@ -8,7 +8,6 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import sys, inspect, re, time, numbers, json as jsonlib, textwrap import sys, inspect, re, time, numbers, json as jsonlib, textwrap
from operator import attrgetter from operator import attrgetter
from calibre.constants import ispy3
from calibre.srv.errors import HTTPSimpleResponse, HTTPNotFound, RouteError from calibre.srv.errors import HTTPSimpleResponse, HTTPNotFound, RouteError
from calibre.srv.utils import http_date from calibre.srv.utils import http_date
from calibre.utils.serialize import msgpack_dumps, json_dumps, MSGPACK_MIME from calibre.utils.serialize import msgpack_dumps, json_dumps, MSGPACK_MIME
@ -88,7 +87,7 @@ def endpoint(route,
f.ok_code = ok_code f.ok_code = ok_code
f.is_endpoint = True f.is_endpoint = True
f.needs_db_write = needs_db_write f.needs_db_write = needs_db_write
argspec = inspect.getfullargspec(f) if ispy3 else inspect.getargspec(f) argspec = inspect.getfullargspec(f)
if len(argspec.args) < 2: if len(argspec.args) < 2:
raise TypeError('The endpoint %r must take at least two arguments' % f.route) raise TypeError('The endpoint %r must take at least two arguments' % f.route)
f.__annotations__ = { f.__annotations__ = {
@ -158,10 +157,7 @@ class Route(object):
self.names = [n for n, m in matchers if n is not None] self.names = [n for n, m in matchers if n is not None]
self.all_names = frozenset(self.names) self.all_names = frozenset(self.names)
self.required_names = self.all_names - frozenset(self.defaults) self.required_names = self.all_names - frozenset(self.defaults)
if ispy3: argspec = inspect.getfullargspec(self.endpoint)
argspec = inspect.getfullargspec(self.endpoint)
else:
argspec = inspect.getargspec(self.endpoint)
if len(self.names) + 2 != len(argspec.args) - len(argspec.defaults or ()): if len(self.names) + 2 != len(argspec.args) - len(argspec.defaults or ()):
raise route_error('Function must take %d non-default arguments' % (len(self.names) + 2)) raise route_error('Function must take %d non-default arguments' % (len(self.names) + 2))
if argspec.args[2:len(self.names)+2] != self.names: if argspec.args[2:len(self.names)+2] != self.names:

View File

@ -12,7 +12,6 @@ from functools import partial
from threading import Thread from threading import Thread
from calibre.srv.utils import ServerLog from calibre.srv.utils import ServerLog
from calibre.constants import ispy3
from polyglot import http_client from polyglot import http_client
rmtree = partial(shutil.rmtree, ignore_errors=True) rmtree = partial(shutil.rmtree, ignore_errors=True)
@ -121,10 +120,7 @@ class TestServer(Thread):
timeout = self.loop.opts.timeout timeout = self.loop.opts.timeout
if interface is None: if interface is None:
interface = self.address[0] interface = self.address[0]
if ispy3: return http_client.HTTPConnection(interface, self.address[1], timeout=timeout)
return http_client.HTTPConnection(interface, self.address[1], timeout=timeout)
else:
return http_client.HTTPConnection(interface, self.address[1], strict=True, timeout=timeout)
def change_handler(self, handler): def change_handler(self, handler):
from calibre.srv.http_response import create_http_handler from calibre.srv.http_response import create_http_handler

View File

@ -10,7 +10,6 @@ from io import BytesIO
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from calibre import guess_type from calibre import guess_type
from calibre.constants import ispy3
from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.tests.base import BaseTest, TestServer
from calibre.srv.utils import eintr_retry_call from calibre.srv.utils import eintr_retry_call
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
@ -96,7 +95,7 @@ class TestHTTP(BaseTest):
conn.request('GET', '/', headers={'Accept-Language': al}) conn.request('GET', '/', headers={'Accept-Language': al})
r = conn.getresponse() r = conn.getresponse()
self.ae(r.status, http_client.OK) self.ae(r.status, http_client.OK)
q += getattr(get_translator(q)[-1], 'gettext' if ispy3 else 'ugettext')('Unknown') q += get_translator(q)[-1].gettext('Unknown')
self.ae(r.read(), q.encode('utf-8')) self.ae(r.read(), q.encode('utf-8'))
test('en', 'en') test('en', 'en')

View File

@ -11,7 +11,6 @@ from unittest import skipIf
from glob import glob from glob import glob
from threading import Event from threading import Event
from calibre.constants import ispy3
from calibre.srv.pre_activated import has_preactivated_support from calibre.srv.pre_activated import has_preactivated_support
from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.tests.base import BaseTest, TestServer
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
@ -114,10 +113,7 @@ class LoopTest(BaseTest):
def test_bonjour(self): def test_bonjour(self):
'Test advertising via BonJour' 'Test advertising via BonJour'
from calibre.srv.bonjour import BonJour from calibre.srv.bonjour import BonJour
if ispy3: from zeroconf import Zeroconf
from zeroconf import Zeroconf
else:
from calibre.utils.Zeroconf import Zeroconf
b = BonJour(wait_for_stop=False) b = BonJour(wait_for_stop=False)
with TestServer(lambda data:(data.path[0] + data.read()), plugins=(b,), shutdown_timeout=5) as server: with TestServer(lambda data:(data.path[0] + data.read()), plugins=(b,), shutdown_timeout=5) as server:
self.assertTrue(b.started.wait(5), 'BonJour not started') self.assertTrue(b.started.wait(5), 'BonJour not started')

View File

@ -11,7 +11,7 @@ from email.utils import formatdate
from operator import itemgetter from operator import itemgetter
from calibre import prints from calibre import prints
from calibre.constants import iswindows, ispy3 from calibre.constants import iswindows
from calibre.srv.errors import HTTPNotFound from calibre.srv.errors import HTTPNotFound
from calibre.utils.localization import get_translator from calibre.utils.localization import get_translator
from calibre.utils.socket_inheritance import set_socket_inherit from calibre.utils.socket_inheritance import set_socket_inherit
@ -47,8 +47,7 @@ class MultiDict(dict): # {{{
@staticmethod @staticmethod
def create_from_query_string(qs): def create_from_query_string(qs):
ans = MultiDict() ans = MultiDict()
if ispy3: qs = as_unicode(qs)
qs = as_unicode(qs)
for k, v in iteritems(parse_qs(qs, keep_blank_values=True)): for k, v in iteritems(parse_qs(qs, keep_blank_values=True)):
dict.__setitem__(ans, as_unicode(k), [as_unicode(x) for x in v]) dict.__setitem__(ans, as_unicode(k), [as_unicode(x) for x in v])
return ans return ans
@ -59,7 +58,7 @@ class MultiDict(dict): # {{{
self[key] = val self[key] = val
def items(self, duplicates=True): def items(self, duplicates=True):
f = dict.items if ispy3 else dict.iteritems f = dict.items
for k, v in f(self): for k, v in f(self):
if duplicates: if duplicates:
for x in v: for x in v:
@ -69,7 +68,7 @@ class MultiDict(dict): # {{{
iteritems = items iteritems = items
def values(self, duplicates=True): def values(self, duplicates=True):
f = dict.values if ispy3 else dict.itervalues f = dict.values
for v in f(self): for v in f(self):
if duplicates: if duplicates:
for x in v: for x in v:
@ -291,8 +290,6 @@ def encode_path(*components):
class Cookie(SimpleCookie): class Cookie(SimpleCookie):
def _BaseCookie__set(self, key, real_value, coded_value): def _BaseCookie__set(self, key, real_value, coded_value):
if not ispy3 and not isinstance(key, bytes):
key = key.encode('ascii') # Python 2.x cannot handle unicode keys
return SimpleCookie._BaseCookie__set(self, key, real_value, coded_value) return SimpleCookie._BaseCookie__set(self, key, real_value, coded_value)
@ -315,14 +312,11 @@ class RotatingStream(object):
self.set_output() self.set_output()
def set_output(self): def set_output(self):
if ispy3: if iswindows:
if iswindows: self.stream = share_open(self.filename, 'ab')
self.stream = share_open(self.filename, 'ab')
else:
# see https://bugs.python.org/issue27805
self.stream = open(os.open(self.filename, os.O_WRONLY|os.O_APPEND|os.O_CREAT|os.O_CLOEXEC), 'wb')
else: else:
self.stream = share_open(self.filename, 'ab', -1 if iswindows else 1) # line buffered # see https://bugs.python.org/issue27805
self.stream = open(os.open(self.filename, os.O_WRONLY|os.O_APPEND|os.O_CREAT|os.O_CLOEXEC), 'wb')
try: try:
self.current_pos = self.stream.tell() self.current_pos = self.stream.tell()
except EnvironmentError: except EnvironmentError:
@ -337,14 +331,12 @@ class RotatingStream(object):
kwargs['safe_encode'] = True kwargs['safe_encode'] = True
kwargs['file'] = self.stream kwargs['file'] = self.stream
self.current_pos += prints(*args, **kwargs) self.current_pos += prints(*args, **kwargs)
if iswindows or ispy3: # line bufferring only works with text mode streams
# For some reason line buffering does not work on windows end = kwargs.get('end', b'\n')
# and in python 3 it only works with text mode streams if isinstance(end, unicode_type):
end = kwargs.get('end', b'\n') end = end.encode('utf-8')
if isinstance(end, unicode_type): if b'\n' in end:
end = end.encode('utf-8') self.flush()
if b'\n' in end:
self.flush()
self.rollover() self.rollover()
def rename(self, src, dest): def rename(self, src, dest):

View File

@ -10,7 +10,7 @@ Perform various initialization tasks.
import locale, sys import locale, sys
# Default translation is NOOP # Default translation is NOOP
from polyglot.builtins import builtins, is_py3, unicode_type from polyglot.builtins import builtins, unicode_type
builtins.__dict__['_'] = lambda s: s builtins.__dict__['_'] = lambda s: s
# For strings which belong in the translation tables, but which shouldn't be # For strings which belong in the translation tables, but which shouldn't be
@ -116,44 +116,7 @@ if not _run_once:
pass pass
# local_open() opens a file that wont be inherited by child processes # local_open() opens a file that wont be inherited by child processes
if is_py3: local_open = open # PEP 446
local_open = open # PEP 446
elif iswindows:
def local_open(name, mode='r', bufsize=-1):
mode += 'N'
return open(name, mode, bufsize)
elif isosx:
import fcntl
FIOCLEX = 0x20006601
def local_open(name, mode='r', bufsize=-1):
ans = open(name, mode, bufsize)
try:
fcntl.ioctl(ans.fileno(), FIOCLEX)
except EnvironmentError:
fcntl.fcntl(ans, fcntl.F_SETFD, fcntl.fcntl(ans, fcntl.F_GETFD) | fcntl.FD_CLOEXEC)
return ans
else:
import fcntl
try:
cloexec_flag = fcntl.FD_CLOEXEC
except AttributeError:
cloexec_flag = 1
supports_mode_e = False
def local_open(name, mode='r', bufsize=-1):
global supports_mode_e
mode += 'e'
ans = open(name, mode, bufsize)
if supports_mode_e:
return ans
old = fcntl.fcntl(ans, fcntl.F_GETFD)
if not (old & cloexec_flag):
fcntl.fcntl(ans, fcntl.F_SETFD, old | cloexec_flag)
else:
supports_mode_e = True
return ans
builtins.__dict__['lopen'] = local_open builtins.__dict__['lopen'] = local_open
from calibre.utils.icu import title_case, lower as icu_lower, upper as icu_upper from calibre.utils.icu import title_case, lower as icu_lower, upper as icu_upper

View File

@ -12,7 +12,7 @@ Test a binary calibre build to ensure that all needed binary images/libraries ha
import os, ctypes, sys, unittest, time import os, ctypes, sys, unittest, time
from calibre.constants import plugins, iswindows, islinux, isosx, ispy3, plugins_loc from calibre.constants import plugins, iswindows, islinux, isosx, plugins_loc
from polyglot.builtins import iteritems, map, unicode_type, getenv, native_string_type from polyglot.builtins import iteritems, map, unicode_type, getenv, native_string_type
is_ci = os.environ.get('CI', '').lower() == 'true' is_ci = os.environ.get('CI', '').lower() == 'true'
@ -81,11 +81,7 @@ class BuildTest(unittest.TestCase):
del soupsieve, bs4 del soupsieve, bs4
def test_zeroconf(self): def test_zeroconf(self):
if ispy3: import zeroconf as z, ifaddr
import zeroconf as z, ifaddr
else:
import calibre.utils.Zeroconf as z
ifaddr = None
del z del z
del ifaddr del ifaddr

File diff suppressed because it is too large Load Diff

View File

@ -14,45 +14,8 @@ completion.
import sys, os, shlex, glob, re import sys, os, shlex, glob, re
from polyglot.builtins import is_py3, unicode_type
prints = print
if is_py3:
prints = print
else:
def prints(*args, **kwargs):
'''
Print unicode arguments safely by encoding them to preferred_encoding
Has the same signature as the print function from Python 3, except for the
additional keyword argument safe_encode, which if set to True will cause the
function to use repr when encoding fails.
'''
file = kwargs.get('file', sys.stdout)
sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n')
enc = 'utf-8'
safe_encode = kwargs.get('safe_encode', False)
for i, arg in enumerate(args):
if isinstance(arg, unicode_type):
try:
arg = arg.encode(enc)
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
if not isinstance(arg, bytes):
arg = unicode_type(arg)
try:
arg = arg.encode(enc)
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
file.write(arg)
if i != len(args)-1:
file.write(sep)
file.write(end)
def split(src): def split(src):

View File

@ -11,17 +11,14 @@ from collections import defaultdict
from copy import deepcopy from copy import deepcopy
from calibre.utils.lock import ExclusiveFile from calibre.utils.lock import ExclusiveFile
from calibre.constants import config_dir, CONFIG_DIR_MODE, ispy3, preferred_encoding, filesystem_encoding, iswindows from calibre.constants import config_dir, CONFIG_DIR_MODE, preferred_encoding, filesystem_encoding, iswindows
from polyglot.builtins import unicode_type, iteritems, map from polyglot.builtins import unicode_type, iteritems, map
plugin_dir = os.path.join(config_dir, 'plugins') plugin_dir = os.path.join(config_dir, 'plugins')
def parse_old_style(src): def parse_old_style(src):
if ispy3: import pickle as cPickle
import pickle as cPickle
else:
import cPickle
options = {'cPickle':cPickle} options = {'cPickle':cPickle}
try: try:
if not isinstance(src, unicode_type): if not isinstance(src, unicode_type):

View File

@ -45,7 +45,7 @@ from dbus.exceptions import (
from dbus.lowlevel import ErrorMessage, MethodReturnMessage, MethodCallMessage from dbus.lowlevel import ErrorMessage, MethodReturnMessage, MethodCallMessage
from dbus.proxies import LOCAL_PATH from dbus.proxies import LOCAL_PATH
from polyglot.builtins import itervalues, zip, is_py3, native_string_type from polyglot.builtins import itervalues, zip, native_string_type
class dbus_property(object): class dbus_property(object):
@ -161,9 +161,6 @@ class _VariantSignature(object):
"""Return 'v' whenever called.""" """Return 'v' whenever called."""
return 'v' return 'v'
if not is_py3:
next = __next__
class BusName(object): class BusName(object):
"""A base class for exporting your own Named Services across the Bus. """A base class for exporting your own Named Services across the Bus.

View File

@ -12,7 +12,7 @@ from math import ceil
from calibre import force_unicode, isbytestring, prints, sanitize_file_name from calibre import force_unicode, isbytestring, prints, sanitize_file_name
from calibre.constants import ( from calibre.constants import (
filesystem_encoding, iswindows, plugins, preferred_encoding, isosx, ispy3 filesystem_encoding, iswindows, plugins, preferred_encoding, isosx
) )
from calibre.utils.localization import get_udc from calibre.utils.localization import get_udc
from polyglot.builtins import iteritems, itervalues, unicode_type, range from polyglot.builtins import iteritems, itervalues, unicode_type, range
@ -629,14 +629,4 @@ def copytree_using_links(path, dest, dest_is_parent=True, filecopyfunc=copyfile)
filecopyfunc(src, df) filecopyfunc(src, df)
if not ispy3 and not iswindows: rmtree = shutil.rmtree
# On POSIX in python2 if you pass a unicode path to rmtree
# it tries to decode all filenames it encounters while walking
# the tree which leads to unicode errors on Linux where there
# can be non-decodeable filenames.
def rmtree(x, **kw):
if not isinstance(x, bytes):
x = x.encode('utf-8')
return shutil.rmtree(x, **kw)
else:
rmtree = shutil.rmtree

View File

@ -6,7 +6,6 @@
from struct import unpack, error from struct import unpack, error
import os import os
from calibre.utils.speedups import ReadOnlyFileBuffer from calibre.utils.speedups import ReadOnlyFileBuffer
from calibre.constants import ispy3
from polyglot.builtins import string_or_bytes, unicode_type from polyglot.builtins import string_or_bytes, unicode_type
""" Recognize image file formats and sizes based on their first few bytes.""" """ Recognize image file formats and sizes based on their first few bytes."""
@ -125,12 +124,8 @@ def jpeg_dimensions(stream):
raise ValueError('Truncated JPEG data') raise ValueError('Truncated JPEG data')
return ans return ans
if ispy3: def read_byte():
def read_byte(): return read(1)[0]
return read(1)[0]
else:
def read_byte():
return ord(read(1)[0])
x = None x = None
while True: while True:

View File

@ -10,7 +10,7 @@ import os, errno, sys
from threading import Thread from threading import Thread
from calibre import force_unicode from calibre import force_unicode
from calibre.constants import iswindows, get_windows_username, islinux, filesystem_encoding, ispy3 from calibre.constants import iswindows, get_windows_username, islinux, filesystem_encoding
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from polyglot.functools import lru_cache from polyglot.functools import lru_cache
@ -48,8 +48,6 @@ def socket_address(which):
from tempfile import gettempdir from tempfile import gettempdir
tmp = force_unicode(gettempdir(), filesystem_encoding) tmp = force_unicode(gettempdir(), filesystem_encoding)
ans = os.path.join(tmp, sock_name) ans = os.path.join(tmp, sock_name)
if not ispy3 and not isinstance(ans, bytes):
ans = ans.encode(filesystem_encoding)
return ans return ans

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -9,11 +8,11 @@ __docformat__ = 'restructuredtext en'
import subprocess, os, sys, time import subprocess, os, sys, time
from functools import partial from functools import partial
from calibre.constants import iswindows, isosx, isfrozen, filesystem_encoding, ispy3 from calibre.constants import iswindows, isosx, isfrozen
from calibre.utils.config import prefs from calibre.utils.config import prefs
from calibre.ptempfile import PersistentTemporaryFile, base_dir from calibre.ptempfile import PersistentTemporaryFile, base_dir
from calibre.utils.serialize import msgpack_dumps from calibre.utils.serialize import msgpack_dumps
from polyglot.builtins import iteritems, unicode_type, string_or_bytes, environ_item, native_string_type, getcwd from polyglot.builtins import string_or_bytes, environ_item, native_string_type, getcwd
from polyglot.binary import as_hex_unicode from polyglot.binary import as_hex_unicode
if iswindows: if iswindows:
@ -86,26 +85,7 @@ class Worker(object):
@property @property
def env(self): def env(self):
if ispy3: env = os.environ.copy()
env = os.environ.copy()
else:
# We use this inefficient method of copying the environment variables
# because of non ascii env vars on windows. See https://bugs.launchpad.net/bugs/811191
env = {}
for key in os.environ:
try:
val = os.environ[key]
if isinstance(val, unicode_type):
# On windows subprocess cannot handle unicode env vars
try:
val = val.encode(filesystem_encoding)
except ValueError:
val = val.encode('utf-8')
if isinstance(key, unicode_type):
key = key.encode('ascii')
env[key] = val
except:
pass
env[native_string_type('CALIBRE_WORKER')] = environ_item('1') env[native_string_type('CALIBRE_WORKER')] = environ_item('1')
td = as_hex_unicode(msgpack_dumps(base_dir())) td = as_hex_unicode(msgpack_dumps(base_dir()))
env[native_string_type('CALIBRE_WORKER_TEMP_DIR')] = environ_item(td) env[native_string_type('CALIBRE_WORKER_TEMP_DIR')] = environ_item(td)
@ -156,22 +136,7 @@ class Worker(object):
self._env = {} self._env = {}
self.gui = gui self.gui = gui
self.job_name = job_name self.job_name = job_name
if ispy3: self._env = env.copy()
self._env = env.copy()
else:
# Windows cannot handle unicode env vars
for k, v in iteritems(env):
try:
if isinstance(k, unicode_type):
k = k.encode('ascii')
if isinstance(v, unicode_type):
try:
v = v.encode(filesystem_encoding)
except:
v = v.encode('utf-8')
self._env[k] = v
except:
pass
def __call__(self, redirect_output=True, cwd=None, priority=None): def __call__(self, redirect_output=True, cwd=None, priority=None):
''' '''

View File

@ -19,7 +19,7 @@ from multiprocessing.connection import Listener, arbitrary_address
from threading import RLock, Thread from threading import RLock, Thread
from calibre import detect_ncpus as cpu_count, force_unicode from calibre import detect_ncpus as cpu_count, force_unicode
from calibre.constants import DEBUG, islinux, iswindows, ispy3 from calibre.constants import DEBUG, islinux, iswindows
from calibre.ptempfile import base_dir from calibre.ptempfile import base_dir
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.utils.ipc.launch import Worker from calibre.utils.ipc.launch import Worker
@ -138,8 +138,6 @@ if islinux:
prefix = '\0calibre-ipc-listener-%d-%%d' % os.getpid() prefix = '\0calibre-ipc-listener-%d-%%d' % os.getpid()
while True: while True:
address = (prefix % next(_name_counter)) address = (prefix % next(_name_counter))
if not ispy3 and not isinstance(address, bytes):
address = address.encode('ascii')
try: try:
l = LinuxListener(address=address, authkey=authkey, backlog=backlog) l = LinuxListener(address=address, authkey=authkey, backlog=backlog)
return address, l return address, l
@ -162,8 +160,6 @@ else:
while max_tries > 0: while max_tries > 0:
max_tries -= 1 max_tries -= 1
address = prefix % next(_name_counter) address = prefix % next(_name_counter)
if not ispy3 and not isinstance(address, bytes):
address = address.encode('utf-8') # multiprocessing needs bytes in python 2
try: try:
return address, Listener(address=address, authkey=authkey, backlog=backlog) return address, Listener(address=address, authkey=authkey, backlog=backlog)
except EnvironmentError as err: except EnvironmentError as err:

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import os, locale, re, io, sys import os, locale, re, io, sys
from gettext import GNUTranslations, NullTranslations from gettext import GNUTranslations, NullTranslations
from polyglot.builtins import is_py3, iteritems, unicode_type from polyglot.builtins import iteritems, unicode_type
_available_translations = None _available_translations = None
@ -268,10 +268,7 @@ def set_translators():
set_translators.lang = t.info().get('language') set_translators.lang = t.info().get('language')
except Exception: except Exception:
pass pass
if is_py3: t.install(names=('ngettext',))
t.install(names=('ngettext',))
else:
t.install(unicode=True, names=('ngettext',))
# Now that we have installed a translator, we have to retranslate the help # Now that we have installed a translator, we have to retranslate the help
# for the global prefs object as it was instantiated in get_lang(), before # for the global prefs object as it was instantiated in get_lang(), before
# the translator was installed. # the translator was installed.

View File

@ -11,7 +11,7 @@ import time
from functools import partial from functools import partial
from calibre.constants import ( from calibre.constants import (
__appname__, fcntl, filesystem_encoding, islinux, isosx, iswindows, plugins, ispy3 __appname__, fcntl, filesystem_encoding, islinux, isosx, iswindows, plugins
) )
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
@ -151,8 +151,6 @@ elif islinux:
) )
name = name name = name
address = '\0' + name.replace(' ', '_') address = '\0' + name.replace(' ', '_')
if not ispy3:
address = address.encode('utf-8')
sock = socket.socket(family=socket.AF_UNIX) sock = socket.socket(family=socket.AF_UNIX)
try: try:
eintr_retry_call(sock.bind, address) eintr_retry_call(sock.bind, address)

View File

@ -1,4 +1,3 @@
__license__ = 'GPL 3' __license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
@ -9,8 +8,6 @@ from threading import Thread
from calibre.utils.filenames import ascii_text from calibre.utils.filenames import ascii_text
from calibre import force_unicode from calibre import force_unicode
from calibre.constants import ispy3
from polyglot.builtins import iteritems, unicode_type
_server = None _server = None
@ -110,10 +107,7 @@ def get_external_ip():
def start_server(): def start_server():
global _server global _server
if _server is None: if _server is None:
if ispy3: from zeroconf import Zeroconf
from zeroconf import Zeroconf
else:
from calibre.utils.Zeroconf import Zeroconf
try: try:
_server = Zeroconf() _server = Zeroconf()
except Exception: except Exception:
@ -150,21 +144,7 @@ def create_service(desc, service_type, port, properties, add_hostname, use_ip_ad
service_type = service_type+'.local.' service_type = service_type+'.local.'
service_name = desc + '.' + service_type service_name = desc + '.' + service_type
server_name = hostname+'.local.' server_name = hostname+'.local.'
if ispy3: from zeroconf import ServiceInfo
from zeroconf import ServiceInfo
else:
from calibre.utils.Zeroconf import ServiceInfo
def enc(x):
if isinstance(x, unicode_type):
x = x.encode('ascii')
return x
service_type = enc(service_type)
service_name = enc(service_name)
server_name = enc(server_name)
if properties:
properties = {enc(k): enc(v) for k, v in iteritems(properties)}
return ServiceInfo( return ServiceInfo(
service_type, service_name, service_type, service_name,

View File

@ -1,88 +0,0 @@
/*
* monotonic.c
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#define UNICODE
#include <Python.h>
/* To millisecond (10^-3) */
#define SEC_TO_MS 1000
/* To microseconds (10^-6) */
#define MS_TO_US 1000
#define SEC_TO_US (SEC_TO_MS * MS_TO_US)
/* To nanoseconds (10^-9) */
#define US_TO_NS 1000
#define MS_TO_NS (MS_TO_US * US_TO_NS)
#define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
/* Conversion from nanoseconds */
#define NS_TO_MS (1000 * 1000)
#define NS_TO_US (1000)
#ifdef _MSC_VER
#include <Windows.h>
static PyObject* monotonic(PyObject *self, PyObject *args) {
return PyFloat_FromDouble(((double)GetTickCount64())/SEC_TO_MS);
}
/* QueryPerformanceCounter() is wildly inaccurate, so we use the more stable
* the lower resolution GetTickCount64() (this is what python 3.x uses)
* static LARGE_INTEGER frequency = {0}, ts = {0};
* static PyObject* monotonic(PyObject *self, PyObject *args) {
* if (!QueryPerformanceCounter(&ts)) { PyErr_SetFromWindowsErr(0); return NULL; }
* return PyFloat_FromDouble(((double)ts.QuadPart)/frequency.QuadPart);
* }
*/
#elif defined(__APPLE__)
#include <mach/mach_time.h>
static mach_timebase_info_data_t timebase = {0};
static PyObject* monotonic(PyObject *self, PyObject *args) {
return PyFloat_FromDouble(((double)(mach_absolute_time() * timebase.numer) / timebase.denom)/SEC_TO_NS);
}
#else
#include <time.h>
static struct timespec ts = {0};
#ifdef CLOCK_HIGHRES
const static clockid_t clk_id = CLOCK_HIGHRES;
#elif defined(CLOCK_MONOTONIC_RAW)
const static clockid_t clk_id = CLOCK_MONOTONIC_RAW;
#else
const static clockid_t clk_id = CLOCK_MONOTONIC;
#endif
static PyObject* monotonic(PyObject *self, PyObject *args) {
if (clock_gettime(clk_id, &ts) != 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
return PyFloat_FromDouble((((double)ts.tv_nsec) / SEC_TO_NS) + (double)ts.tv_sec);
}
#endif
static PyMethodDef monotonic_methods[] = {
{"monotonic", monotonic, METH_NOARGS,
"monotonic()\n\nReturn a monotonically increasing time value."
},
{NULL, NULL, 0, NULL}
};
CALIBRE_MODINIT_FUNC
initmonotonic(void) {
PyObject *m;
#ifdef _MSC_VER
/* if(!QueryPerformanceFrequency(&frequency)) { PyErr_SetFromWindowsErr(0); return; } */
#endif
#ifdef __APPLE__
mach_timebase_info(&timebase);
#endif
m = Py_InitModule3("monotonic", monotonic_methods,
"Implementation of time.monotonic() in C for speed"
);
if (m == NULL) return;
}

View File

@ -1,13 +1,4 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
from time import monotonic
try: monotonic
from time import monotonic
except ImportError:
from calibre.constants import plugins
monotonicp, err = plugins['monotonic']
if err:
raise RuntimeError('Failed to load the monotonic module with error: ' + err)
monotonic = monotonicp.monotonic
del monotonicp, err

View File

@ -2,10 +2,7 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type
from calibre.constants import ispy3
MSGPACK_MIME = 'application/x-msgpack' MSGPACK_MIME = 'application/x-msgpack'
@ -118,22 +115,11 @@ def json_loads(data):
return json.loads(data, object_hook=json_decoder) return json.loads(data, object_hook=json_decoder)
if ispy3: def pickle_dumps(data):
import pickle
return pickle.dumps(data, -1)
def pickle_dumps(data):
import pickle
return pickle.dumps(data, -1)
def pickle_loads(dump): def pickle_loads(dump):
import pickle import pickle
return pickle.loads(dump, encoding='utf-8') return pickle.loads(dump, encoding='utf-8')
else:
def pickle_dumps(data):
import cPickle as pickle
return pickle.dumps(data, -1)
def pickle_loads(dump):
import cPickle as pickle
return pickle.loads(dump)

View File

@ -9,7 +9,7 @@ import os, sys
from polyglot.builtins import reraise from polyglot.builtins import reraise
from calibre.constants import iswindows, plugins, ispy3 from calibre.constants import iswindows, plugins
''' '''
This module defines a share_open() function which is a replacement for This module defines a share_open() function which is a replacement for
@ -177,13 +177,7 @@ if iswindows:
return speedup.fdopen(os_open(path, flags), path, mode, buffering) return speedup.fdopen(os_open(path, flags), path, mode, buffering)
else: else:
if ispy3: share_open = open
# See PEP 446
share_open = open
else:
def share_open(path, mode='r', buffering=-1):
flags = flags_from_mode(mode) | speedup.O_CLOEXEC
return speedup.fdopen(os.open(path, flags), path, mode, buffering)
def raise_winerror(x): def raise_winerror(x):
reraise(NotImplementedError, None, sys.exc_info()[2]) reraise(NotImplementedError, None, sys.exc_info()[2])

View File

@ -12,7 +12,7 @@ This module implements a simple commandline SMTP client that supports:
import sys, traceback, os, socket, encodings.idna as idna import sys, traceback, os, socket, encodings.idna as idna
from calibre import isbytestring from calibre import isbytestring
from calibre.constants import ispy3, iswindows from calibre.constants import iswindows
from polyglot.builtins import unicode_type, as_unicode, native_string_type from polyglot.builtins import unicode_type, as_unicode, native_string_type
@ -150,7 +150,7 @@ def get_smtp_class(use_ssl=False, debuglevel=0):
# but there is no way to set debuglevel before connect() is called # but there is no way to set debuglevel before connect() is called
import polyglot.smtplib as smtplib import polyglot.smtplib as smtplib
cls = smtplib.SMTP_SSL if use_ssl else smtplib.SMTP cls = smtplib.SMTP_SSL if use_ssl else smtplib.SMTP
bases = (cls,) if ispy3 else (cls, object) bases = (cls,)
return type(native_string_type('SMTP'), bases, {native_string_type('debuglevel'): debuglevel}) return type(native_string_type('SMTP'), bases, {native_string_type('debuglevel'): debuglevel})
@ -177,8 +177,6 @@ def sendmail(msg, from_, to, localhost=None, verbose=0, timeout=None,
s.starttls(context=context) s.starttls(context=context)
s.ehlo() s.ehlo()
if username is not None and password is not None: if username is not None and password is not None:
if encryption == 'SSL' and not ispy3:
s.sock = s.file.sslobj
s.login(username, password) s.login(username, password)
ret = None ret = None
try: try:

View File

@ -1,14 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, sys, re import os, sys, re
from calibre.constants import iswindows, ispy3 from calibre.constants import iswindows
from polyglot.builtins import iteritems, range, zip, native_string_type from polyglot.builtins import iteritems, range, zip, native_string_type
if iswindows: if iswindows:
@ -150,12 +149,7 @@ class Detect(object):
while text: while text:
t, text = text[:chunk], text[chunk:] t, text = text[:chunk], text[chunk:]
wt = c_wchar_p(t) wt = c_wchar_p(t)
if ispy3: text_len = len(t.encode('utf-16'))
text_len = len(t.encode('utf-16'))
else:
# Use the fact that len(t) == wcslen(wt) in python 2.7 on
# windows where the python unicode type uses UTF-16
text_len = len(t)
if not self.write_console(self.file_handle, wt, text_len, byref(written), None): if not self.write_console(self.file_handle, wt, text_len, byref(written), None):
# Older versions of windows can fail to write large strings # Older versions of windows can fail to write large strings
# to console with WriteConsoleW (seen it happen on Win XP) # to console with WriteConsoleW (seen it happen on Win XP)

View File

@ -1,40 +0,0 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import sys
from calibre.constants import iswindows, preferred_encoding, ispy3
if ispy3:
from getpass import getpass
getpass
else:
def getpass(prompt):
if iswindows:
# getpass is broken on windows with python 2.x and unicode, the
# below implementation is from the python 3 source code
import msvcrt
for c in prompt:
msvcrt.putwch(c)
pw = ""
while 1:
c = msvcrt.getwch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putwch('\r')
msvcrt.putwch('\n')
return pw
else:
enc = getattr(sys.stdin, 'encoding', preferred_encoding) or preferred_encoding
from getpass import getpass
return getpass(prompt).decode(enc)

View File

@ -1,426 +0,0 @@
/*
* crc32.c
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#define UNICODE
#include <Python.h>
#include <zlib.h>
#define DEF_BUF_SIZE (16*1024)
/* The following parameters are copied from zutil.h, version 0.95 */
#define DEFLATED 8
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
static PyTypeObject Comptype;
static PyObject *ZlibError = NULL;
typedef struct
{
PyObject_HEAD
z_stream zst;
PyObject *unused_data;
PyObject *unconsumed_tail;
char eof;
int is_initialised;
PyObject *zdict;
} compobject;
static void
zlib_error(z_stream zst, int err, char *msg)
{
const char *zmsg = Z_NULL;
/* In case of a version mismatch, zst.msg won't be initialized.
Check for this case first, before looking at zst.msg. */
if (err == Z_VERSION_ERROR)
zmsg = "library version mismatch";
if (zmsg == Z_NULL)
zmsg = zst.msg;
if (zmsg == Z_NULL) {
switch (err) {
case Z_BUF_ERROR:
zmsg = "incomplete or truncated stream";
break;
case Z_STREAM_ERROR:
zmsg = "inconsistent stream state";
break;
case Z_DATA_ERROR:
zmsg = "invalid input data";
break;
}
}
if (zmsg == Z_NULL)
PyErr_Format(ZlibError, "Error %d %s", err, msg);
else
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
}
static compobject *
newcompobject(PyTypeObject *type)
{
compobject *self;
self = PyObject_New(compobject, type);
if (self == NULL)
return NULL;
self->eof = 0;
self->is_initialised = 0;
self->zdict = NULL;
self->unused_data = PyBytes_FromStringAndSize("", 0);
if (self->unused_data == NULL) {
Py_DECREF(self);
return NULL;
}
self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
if (self->unconsumed_tail == NULL) {
Py_DECREF(self);
return NULL;
}
return self;
}
static PyObject *
PyZlib_compressobj(PyObject *selfptr, PyObject *args)
{
compobject *self = NULL;
int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY, err;
if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
&memLevel, &strategy))
return NULL;
self = newcompobject(&Comptype);
if (self==NULL) return NULL;
self->zst.zalloc = (alloc_func)Z_NULL;
self->zst.zfree = (free_func)Z_NULL;
self->zst.next_in = Z_NULL;
self->zst.avail_in = 0;
err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
switch(err) {
case (Z_OK):
self->is_initialised = 1;
return (PyObject*)self;
case (Z_MEM_ERROR):
Py_DECREF(self);
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory for compression object");
return NULL;
case(Z_STREAM_ERROR):
Py_DECREF(self);
PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
return NULL;
default:
zlib_error(self->zst, err, "while creating compression object");
Py_DECREF(self);
return NULL;
}
return (PyObject*) self;
}
static void
Dealloc(compobject *self)
{
Py_XDECREF(self->unused_data);
Py_XDECREF(self->unconsumed_tail);
Py_XDECREF(self->zdict);
PyObject_Del(self);
}
static void
Comp_dealloc(compobject *self)
{
if (self->is_initialised)
deflateEnd(&self->zst);
Dealloc(self);
}
static PyObject *
Compress_compress(compobject *self, PyObject *data_obj)
/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
{
int err = 0, len = 0;
unsigned int inplen = 0;
unsigned int length = DEF_BUF_SIZE, new_length = 0;
PyObject *RetVal = NULL;
Py_buffer indata = {0};
Byte *input = NULL;
unsigned long start_total_out = 0;
if (PyObject_GetBuffer(data_obj, &indata, PyBUF_SIMPLE) != 0) return NULL;
input = indata.buf; len = indata.len;
if ((size_t)len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError, "Size does not fit in an unsigned int");
goto done;
}
inplen = (unsigned int)len;
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) goto done;
start_total_out = self->zst.total_out;
self->zst.avail_in = inplen;
self->zst.next_in = input;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
Py_END_ALLOW_THREADS
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
if (length <= (UINT_MAX >> 1))
new_length = length << 1;
else
new_length = UINT_MAX;
if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Py_CLEAR(RetVal);
goto done;
}
self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = new_length;
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
Py_END_ALLOW_THREADS
}
/* We will only get Z_BUF_ERROR if the output buffer was full but
there wasn't more output when we tried again, so it is not an error
condition.
*/
if (err != Z_OK && err != Z_BUF_ERROR) {
zlib_error(self->zst, err, "while compressing data");
Py_CLEAR(RetVal);
goto done;
}
if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_CLEAR(RetVal);
}
done:
if (indata.obj) PyBuffer_Release(&indata);
return RetVal;
}
static PyObject *
Compress_flush(compobject *self, PyObject *args)
{
int err = 0, mode=Z_FINISH;
unsigned int length = DEF_BUF_SIZE, new_length = 0;
PyObject *RetVal = NULL;
unsigned long start_total_out = 0;
if (!PyArg_ParseTuple(args, "|i:flush", &mode)) return NULL;
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
doing any work at all; just return an empty string. */
if (mode == Z_NO_FLUSH) {
return PyBytes_FromStringAndSize(NULL, 0);
}
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
return NULL;
start_total_out = self->zst.total_out;
self->zst.avail_in = 0;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), mode);
Py_END_ALLOW_THREADS
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
if (length <= (UINT_MAX >> 1))
new_length = length << 1;
else
new_length = UINT_MAX;
if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Py_CLEAR(RetVal);
goto error;
}
self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
self->zst.avail_out = length;
length = new_length;
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), mode);
Py_END_ALLOW_THREADS
}
/* If mode is Z_FINISH, we also have to call deflateEnd() to free
various data structures. Note we should only get Z_STREAM_END when
mode is Z_FINISH, but checking both for safety*/
if (err == Z_STREAM_END && mode == Z_FINISH) {
err = deflateEnd(&(self->zst));
if (err != Z_OK) {
zlib_error(self->zst, err, "while finishing compression");
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
}
else
self->is_initialised = 0;
/* We will only get Z_BUF_ERROR if the output buffer was full
but there wasn't more output when we tried again, so it is
not an error condition.
*/
} else if (err!=Z_OK && err!=Z_BUF_ERROR) {
zlib_error(self->zst, err, "while flushing");
Py_DECREF(RetVal);
RetVal = NULL;
goto error;
}
if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_CLEAR(RetVal);
}
error:
return RetVal;
}
static PyMethodDef comp_methods[] =
{
{"compress", (PyCFunction)Compress_compress, METH_O, "compress(data) -- returns compressed data, dont forget to call flush when done."},
{"flush", (PyCFunction)Compress_flush, METH_VARARGS, "flush([mode]) -- returns any remaining data"},
{NULL}
};
static PyTypeObject Comptype = {
PyVarObject_HEAD_INIT(0, 0)
"zlib2.Compress",
sizeof(compobject),
0,
(destructor)Comp_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
comp_methods, /*tp_methods*/
};
static PyObject *
zlib_crc32(PyObject *self, PyObject *args)
{
int signed_val = 0, len = 0;
unsigned int value = 0;
unsigned char *buf = NULL;
Py_buffer indata = {0};
PyObject* obj = NULL;
if(!PyArg_ParseTuple(args, "O|I:crc32", &obj, &value)) return NULL;
if (PyObject_GetBuffer(obj, &indata, PyBUF_SIMPLE) != 0) return NULL;
buf = indata.buf; len = indata.len;
/* Releasing the GIL for very small buffers is inefficient
and may lower performance */
if (len > 1024*5) {
Py_BEGIN_ALLOW_THREADS
/* Avoid truncation of length for very large buffers. crc32() takes
length as an unsigned int, which may be narrower than Py_ssize_t. */
while ((size_t)len > UINT_MAX) {
value = crc32(value, buf, UINT_MAX);
buf += (size_t) UINT_MAX;
len -= (size_t) UINT_MAX;
}
signed_val = crc32(value, buf, (unsigned int)len);
Py_END_ALLOW_THREADS
} else {
signed_val = crc32(value, buf, len);
}
if (indata.obj) PyBuffer_Release(&indata);
return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
}
static PyMethodDef methods[] = {
{"crc32", zlib_crc32, METH_VARARGS,
"crc32(data, [, state=0)\n\nCalculate crc32 for the given data starting from the given state."
},
{"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS, "Create compression object"},
{NULL, NULL, 0, NULL}
};
CALIBRE_MODINIT_FUNC
initzlib2(void) {
PyObject *m, *ver;
Comptype.tp_new = PyType_GenericNew;
if (PyType_Ready(&Comptype) < 0)
return;
m = Py_InitModule3("zlib2", methods,
"Implementation of zlib compression with support for the buffer protocol, which is missing in Python2. Code taken from the Python3 zlib module"
);
if (m == NULL) return;
PyModule_AddIntMacro(m, MAX_WBITS);
PyModule_AddIntMacro(m, DEFLATED);
PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
PyModule_AddIntMacro(m, DEF_BUF_SIZE);
PyModule_AddIntMacro(m, Z_BEST_SPEED);
PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
PyModule_AddIntMacro(m, Z_FILTERED);
PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
PyModule_AddIntMacro(m, Z_FINISH);
PyModule_AddIntMacro(m, Z_NO_FLUSH);
PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
PyModule_AddIntMacro(m, Z_FULL_FLUSH);
ver = PyUnicode_FromString(ZLIB_VERSION);
if (ver != NULL)
PyModule_AddObject(m, "ZLIB_VERSION", ver);
ver = PyUnicode_FromString(zlibVersion());
if (ver != NULL)
PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
ZlibError = PyErr_NewException("zlib2.error", NULL, NULL);
if (ZlibError != NULL) {
Py_INCREF(ZlibError);
PyModule_AddObject(m, "error", ZlibError);
}
}

View File

@ -2,12 +2,6 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
from functools import lru_cache
from polyglot.builtins import is_py3
if is_py3:
from functools import lru_cache
else:
from backports.functools_lru_cache import lru_cache
lru_cache lru_cache

View File

@ -2,9 +2,5 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from html.entities import name2codepoint
name2codepoint
if is_py3:
from html.entities import name2codepoint
else:
from htmlentitydefs import name2codepoint

View File

@ -2,21 +2,15 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from http.client import (responses, HTTPConnection, HTTPSConnection,
BAD_REQUEST, FOUND, FORBIDDEN, HTTP_VERSION_NOT_SUPPORTED,
INTERNAL_SERVER_ERROR, METHOD_NOT_ALLOWED, MOVED_PERMANENTLY,
NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT,
PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE, REQUEST_URI_TOO_LONG,
REQUESTED_RANGE_NOT_SATISFIABLE, REQUEST_TIMEOUT, SEE_OTHER,
SERVICE_UNAVAILABLE, UNAUTHORIZED, _CS_IDLE, _CS_REQ_SENT)
if is_py3: if False:
from http.client import (responses, HTTPConnection, HTTPSConnection, responses, HTTPConnection, HTTPSConnection, BAD_REQUEST, FOUND, FORBIDDEN, HTTP_VERSION_NOT_SUPPORTED, INTERNAL_SERVER_ERROR, METHOD_NOT_ALLOWED
BAD_REQUEST, FOUND, FORBIDDEN, HTTP_VERSION_NOT_SUPPORTED, MOVED_PERMANENTLY, NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT, PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE, REQUEST_URI_TOO_LONG
INTERNAL_SERVER_ERROR, METHOD_NOT_ALLOWED, MOVED_PERMANENTLY, REQUESTED_RANGE_NOT_SATISFIABLE, REQUEST_TIMEOUT, SEE_OTHER, SERVICE_UNAVAILABLE, UNAUTHORIZED, _CS_IDLE, _CS_REQ_SENT
NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT,
PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE, REQUEST_URI_TOO_LONG,
REQUESTED_RANGE_NOT_SATISFIABLE, REQUEST_TIMEOUT, SEE_OTHER,
SERVICE_UNAVAILABLE, UNAUTHORIZED, _CS_IDLE, _CS_REQ_SENT)
else:
from httplib import (responses, HTTPConnection, HTTPSConnection,
BAD_REQUEST, FOUND, FORBIDDEN, HTTP_VERSION_NOT_SUPPORTED,
INTERNAL_SERVER_ERROR, METHOD_NOT_ALLOWED, MOVED_PERMANENTLY,
NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT,
PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE, REQUEST_URI_TOO_LONG,
REQUESTED_RANGE_NOT_SATISFIABLE, REQUEST_TIMEOUT, SEE_OTHER,
SERVICE_UNAVAILABLE, UNAUTHORIZED, _CS_IDLE, _CS_REQ_SENT)

View File

@ -2,11 +2,5 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from http.cookies import SimpleCookie # noqa
from http.cookiejar import CookieJar, Cookie # noqa
if is_py3:
from http.cookies import SimpleCookie # noqa
from http.cookiejar import CookieJar, Cookie # noqa
else:
from Cookie import SimpleCookie # noqa
from cookielib import CookieJar, Cookie # noqa

View File

@ -2,12 +2,4 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
from http.server import HTTPServer, SimpleHTTPRequestHandler # noqa
from polyglot.builtins import is_py3
if is_py3:
from http.server import HTTPServer, SimpleHTTPRequestHandler
else:
from BaseHTTPServer import HTTPServer # noqa
from SimpleHTTPServer import SimpleHTTPRequestHandler # noqa

View File

@ -2,25 +2,10 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
def unwrap_bytes(x):
return x
def wrap_bytes(x):
return x
from polyglot.builtins import is_py3 from plistlib import loads, dumps, Data # noqa
if is_py3:
from plistlib import loads, dumps # noqa
def unwrap_bytes(x):
return x
def wrap_bytes(x):
return x
else:
from plistlib import readPlistFromString as loads, writePlistToString as dumps, Data # noqa
def unwrap_bytes(x):
if isinstance(x, Data):
x = x.data
return x
def wrap_bytes(x):
return Data(x)

View File

@ -2,9 +2,4 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from queue import Queue, Empty, Full, PriorityQueue, LifoQueue # noqa
if is_py3:
from queue import Queue, Empty, Full, PriorityQueue, LifoQueue # noqa
else:
from Queue import Queue, Empty, Full, PriorityQueue, LifoQueue # noqa

View File

@ -2,9 +2,5 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from reprlib import repr
repr
if is_py3:
from reprlib import repr
else:
from repr import repr

View File

@ -2,13 +2,9 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
import sys import sys
from functools import partial from functools import partial
from polyglot.builtins import is_py3
class FilteredLog(object): class FilteredLog(object):
@ -30,44 +26,30 @@ class FilteredLog(object):
self.debug_to(*a) self.debug_to(*a)
if is_py3: import smtplib
import smtplib
class SMTP(smtplib.SMTP):
def __init__(self, *a, **kw): class SMTP(smtplib.SMTP):
self.debug_to = FilteredLog(kw.pop('debug_to', None))
super().__init__(*a, **kw)
def _print_debug(self, *a): def __init__(self, *a, **kw):
if self.debug_to is not None: self.debug_to = FilteredLog(kw.pop('debug_to', None))
self.debug_to(*a) super().__init__(*a, **kw)
else:
super()._print_debug(*a)
class SMTP_SSL(smtplib.SMTP_SSL): def _print_debug(self, *a):
if self.debug_to is not None:
self.debug_to(*a)
else:
super()._print_debug(*a)
def __init__(self, *a, **kw):
self.debug_to = FilteredLog(kw.pop('debug_to', None))
super().__init__(*a, **kw)
def _print_debug(self, *a): class SMTP_SSL(smtplib.SMTP_SSL):
if self.debug_to is not None:
self.debug_to(*a)
else:
super()._print_debug(*a)
else: def __init__(self, *a, **kw):
import calibre.utils.smtplib as smtplib self.debug_to = FilteredLog(kw.pop('debug_to', None))
super().__init__(*a, **kw)
class SMTP(smtplib.SMTP): def _print_debug(self, *a):
if self.debug_to is not None:
def __init__(self, *a, **kw): self.debug_to(*a)
kw['debug_to'] = FilteredLog(kw.get('debug_to')) else:
smtplib.SMTP.__init__(self, *a, **kw) super()._print_debug(*a)
class SMTP_SSL(smtplib.SMTP_SSL):
def __init__(self, *a, **kw):
kw['debug_to'] = FilteredLog(kw.get('debug_to'))
smtplib.SMTP_SSL.__init__(self, *a, **kw)

View File

@ -2,9 +2,4 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org> # License: GPL v3 Copyright: 2019, Eli Schwartz <eschwartz@archlinux.org>
from polyglot.builtins import is_py3 from socketserver import TCPServer, ThreadingMixIn # noqa
if is_py3:
from socketserver import TCPServer, ThreadingMixIn # noqa
else:
from SocketServer import TCPServer, ThreadingMixIn # noqa

View File

@ -4,46 +4,22 @@
from polyglot.builtins import is_py3 from urllib.request import (build_opener, getproxies, install_opener, # noqa
HTTPBasicAuthHandler, HTTPCookieProcessor, HTTPDigestAuthHandler, # noqa
url2pathname, urlopen, Request) # noqa
from urllib.parse import (parse_qs, quote, unquote as uq, quote_plus, urldefrag, # noqa
urlencode, urljoin, urlparse, urlunparse, urlsplit, urlunsplit) # noqa
from urllib.error import HTTPError, URLError # noqa
if is_py3:
from urllib.request import (build_opener, getproxies, install_opener, # noqa
HTTPBasicAuthHandler, HTTPCookieProcessor, HTTPDigestAuthHandler, # noqa
url2pathname, urlopen, Request) # noqa
from urllib.parse import (parse_qs, quote, unquote as uq, quote_plus, urldefrag, # noqa
urlencode, urljoin, urlparse, urlunparse, urlsplit, urlunsplit) # noqa
from urllib.error import HTTPError, URLError # noqa
def unquote(x, encoding='utf-8', errors='replace'): def unquote(x, encoding='utf-8', errors='replace'):
binary = isinstance(x, bytes) binary = isinstance(x, bytes)
if binary: if binary:
x = x.decode(encoding, errors) x = x.decode(encoding, errors)
ans = uq(x, encoding, errors) ans = uq(x, encoding, errors)
if binary: if binary:
ans = ans.encode(encoding, errors) ans = ans.encode(encoding, errors)
return ans return ans
else:
from urllib import (getproxies, quote, unquote as uq, quote_plus, url2pathname, # noqa
urlencode) # noqa
from urllib2 import (build_opener, install_opener, HTTPBasicAuthHandler, # noqa
HTTPCookieProcessor, HTTPDigestAuthHandler, HTTPError, URLError, # noqa
urlopen, Request) # noqa
from urlparse import (parse_qs, urldefrag, urljoin, urlparse, urlunparse, # noqa
urlsplit, urlunsplit) # noqa
def unquote(x, encoding='utf-8', errors='replace'):
# unquote must run on a bytestring and will return a bytestring
# If it runs on a unicode object, it returns a double encoded unicode
# string: unquote(u'%C3%A4') != unquote(b'%C3%A4').decode('utf-8')
# and the latter is correct
binary = isinstance(x, bytes)
if not binary:
x = x.encode(encoding, errors)
ans = uq(x)
if not binary:
ans = ans.decode(encoding, errors)
return ans
def unquote_plus(x, encoding='utf-8', errors='replace'): def unquote_plus(x, encoding='utf-8', errors='replace'):