py3: make completion work

Stop using python2 hack to duplicate python3 print behavior.
This commit is contained in:
Eli Schwartz 2019-04-29 01:18:02 -04:00
parent 61a8db8988
commit c29219e193
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6

View File

@ -1,6 +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
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -14,34 +14,25 @@ completion.
import sys, os, shlex, glob, re import sys, os, shlex, glob, re
from polyglot.builtins import unicode_type from polyglot.builtins import is_py3, unicode_type
def prints(*args, **kwargs): if is_py3:
''' 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 = 'utf-8' 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_type): enc = 'utf-8'
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, bytes):
try:
arg = str(arg)
except ValueError:
arg = unicode_type(arg)
if isinstance(arg, unicode_type): if isinstance(arg, unicode_type):
try: try:
arg = arg.encode(enc) arg = arg.encode(enc)
@ -49,11 +40,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, 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) 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)
def split(src): def split(src):