Fix #1794915 [setup.py build does not work due to UnicodeEncodeError](https://bugs.launchpad.net/calibre/+bug/1794915)

This commit is contained in:
Kovid Goyal 2018-09-28 09:47:35 +05:30
parent 4d4ffcff7c
commit 01a9fb6687
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# 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
from __future__ import with_statement from __future__ import with_statement, print_function
from __future__ import print_function
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -11,6 +10,7 @@ import sys, re, os, platform, subprocess, time, errno
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
@ -123,31 +123,22 @@ initialize_constants()
preferred_encoding = 'utf-8' preferred_encoding = 'utf-8'
def prints(*args, **kwargs): if ispy3:
''' prints = print
Print unicode arguments safely by encoding them to preferred_encoding else:
Has the same signature as the print function from Python 3, except for the def prints(*args, **kwargs):
additional keyword argument safe_encode, which if set to True will cause the '''
function to use repr when encoding fails. Print unicode arguments safely by encoding them to preferred_encoding
''' Has the same signature as the print function from Python 3, except for the
file = kwargs.get('file', sys.stdout) additional keyword argument safe_encode, which if set to True will cause the
sep = kwargs.get('sep', ' ') function to use repr when encoding fails.
end = kwargs.get('end', '\n') '''
enc = preferred_encoding file = kwargs.get('file', sys.stdout)
safe_encode = kwargs.get('safe_encode', False) sep = kwargs.get('sep', ' ')
for i, arg in enumerate(args): end = kwargs.get('end', '\n')
if isinstance(arg, unicode): enc = preferred_encoding
try: safe_encode = kwargs.get('safe_encode', False)
arg = arg.encode(enc) for i, arg in enumerate(args):
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
if not isinstance(arg, str):
try:
arg = str(arg)
except ValueError:
arg = unicode(arg)
if isinstance(arg, unicode): if isinstance(arg, unicode):
try: try:
arg = arg.encode(enc) arg = arg.encode(enc)
@ -155,11 +146,23 @@ def prints(*args, **kwargs):
if not safe_encode: if not safe_encode:
raise raise
arg = repr(arg) arg = repr(arg)
if not isinstance(arg, str):
try:
arg = str(arg)
except ValueError:
arg = unicode(arg)
if isinstance(arg, unicode):
try:
arg = arg.encode(enc)
except UnicodeEncodeError:
if not safe_encode:
raise
arg = repr(arg)
file.write(arg) file.write(arg)
if i != len(args)-1: if i != len(args)-1:
file.write(sep) file.write(sep)
file.write(end) file.write(end)
warnings = [] warnings = []