Revert "Remove use of prints in the setup package since nowadays most systems are UTF-8 based anyway"

This reverts commit f4b358554942b1b13d4f07481a4767c527d94c70.
This commit is contained in:
Kovid Goyal 2018-09-28 09:44:41 +05:30
parent 70f53a0a80
commit 4d4ffcff7c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 49 additions and 8 deletions

View File

@ -25,7 +25,7 @@ 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

View File

@ -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 <kovid@kovidgoyal.net>'
@ -121,6 +122,46 @@ 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 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 @@ class Command(object):
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()