From 4d4ffcff7c247072c28d20822abed12e2539cdd2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Sep 2018 09:44:41 +0530 Subject: [PATCH] Revert "Remove use of prints in the setup package since nowadays most systems are UTF-8 based anyway" This reverts commit f4b358554942b1b13d4f07481a4767c527d94c70. --- setup.py | 10 +++++----- setup/__init__.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 07bf575bbc66..ad1252576621 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def check_version_info(): sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import setup.commands as commands -from setup import get_warnings +from setup import prints, get_warnings def option_parser(): @@ -93,13 +93,13 @@ def main(args=sys.argv): clean_backups() if opts.clean: - print('Cleaning', args[1]) + prints('Cleaning', args[1]) command.clean() return 0 if opts.clean_all: for cmd in commands.__all__: - print('Cleaning', cmd) + prints('Cleaning', cmd) getattr(commands, cmd).clean() return 0 @@ -108,10 +108,10 @@ def main(args=sys.argv): warnings = get_warnings() if warnings: print() - print('There were', len(warnings), 'warning(s):') + prints('There were', len(warnings), 'warning(s):') print() for args, kwargs in warnings: - print('*', *args, **kwargs) + prints('*', *args, **kwargs) print() return 0 diff --git a/setup/__init__.py b/setup/__init__.py index 21b2b28468db..f3c90d46d785 100644 --- a/setup/__init__.py +++ b/setup/__init__.py @@ -1,6 +1,7 @@ #!/usr/bin/env python2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai -from __future__ import with_statement, print_function +from __future__ import with_statement +from __future__ import print_function __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' @@ -121,6 +122,46 @@ def e2f(ep): 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 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) + + warnings = [] @@ -227,12 +268,12 @@ def newer(cls, targets, sources): return newer(targets, sources) def info(self, *args, **kwargs): - print(*args, **kwargs) + prints(*args, **kwargs) sys.stdout.flush() def warn(self, *args, **kwargs): print('\n'+'_'*20, 'WARNING','_'*20) - print(*args, **kwargs) + prints(*args, **kwargs) print('_'*50) warnings.append((args, kwargs)) sys.stdout.flush()