From 01a9fb66871abb485ac9b3a69ea7b7086d2ce51a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Sep 2018 09:47:35 +0530 Subject: [PATCH] Fix #1794915 [setup.py build does not work due to UnicodeEncodeError](https://bugs.launchpad.net/calibre/+bug/1794915) --- setup/__init__.py | 65 +++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/setup/__init__.py b/setup/__init__.py index f3c90d46d7..0f9b840120 100644 --- a/setup/__init__.py +++ b/setup/__init__.py @@ -1,7 +1,6 @@ #!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai -from __future__ import with_statement -from __future__ import print_function +from __future__ import with_statement, print_function __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' @@ -11,6 +10,7 @@ import sys, re, os, platform, subprocess, time, errno is64bit = platform.architecture()[0] == '64bit' iswindows = re.search('win(32|64)', sys.platform) +ispy3 = sys.version_info.major > 2 isosx = 'darwin' in sys.platform isfreebsd = 'freebsd' in sys.platform isnetbsd = 'netbsd' in sys.platform @@ -123,31 +123,22 @@ initialize_constants() preferred_encoding = 'utf-8' -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, unicode): - 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 = unicode(arg) +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, unicode): try: arg = arg.encode(enc) @@ -155,11 +146,23 @@ def prints(*args, **kwargs): 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): + 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) + file.write(arg) + if i != len(args)-1: + file.write(sep) + file.write(end) warnings = []