From c29219e193e83208d168f1fa404aa2521f756bc7 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 29 Apr 2019 01:18:02 -0400 Subject: [PATCH] py3: make completion work Stop using python2 hack to duplicate python3 print behavior. --- src/calibre/utils/complete.py | 65 ++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/calibre/utils/complete.py b/src/calibre/utils/complete.py index 2f3ec79695..4f1aa30c34 100644 --- a/src/calibre/utils/complete.py +++ b/src/calibre/utils/complete.py @@ -1,6 +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 with_statement, print_function __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' @@ -14,34 +14,25 @@ completion. import sys, os, shlex, glob, re -from polyglot.builtins import unicode_type +from polyglot.builtins import is_py3, unicode_type -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): - try: - arg = str(arg) - except ValueError: - arg = unicode_type(arg) +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) @@ -49,11 +40,23 @@ def prints(*args, **kwargs): if not safe_encode: raise arg = repr(arg) + if not isinstance(arg, bytes): + try: + arg = str(arg) + except ValueError: + arg = unicode_type(arg) + if isinstance(arg, unicode_type): + 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) def split(src):