From 2f1cd59e1be4de669abf2d94e3c42ed8a91a4224 Mon Sep 17 00:00:00 2001 From: Johnny Robeson Date: Wed, 29 Jun 2016 03:16:42 -0400 Subject: [PATCH 1/2] make sure ui.print_() only accepts unicode strings --- beets/ui/__init__.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 032143928..4fd79974f 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -135,25 +135,25 @@ def print_(*strings, **kwargs): is not in the terminal's encoding's character set, just silently replaces it. - If the arguments are strings then they're expected to share the same - type: either bytes or unicode. + The arguments must be unicode strings The `end` keyword argument behaves similarly to the built-in `print` (it defaults to a newline). The value should have the same string type as the arguments. """ + if not strings: + strings = [u''] + end = kwargs.get('end') + assert isinstance(strings[0], six.text_type) - if not strings or isinstance(strings[0], six.text_type): - txt = u' '.join(strings) - txt += u'\n' if end is None else end - else: - txt = b' '.join(strings) - txt += b'\n' if end is None else end + txt = u' '.join(strings) + txt += u'\n' if end is None else end - # Always send bytes to the stdout stream. - if isinstance(txt, six.text_type): - txt = txt.encode(_out_encoding(), 'replace') + # Always send bytes to the stdout stream on python 2. + if six.PY2: + if isinstance(txt, six.text_type): + txt = txt.encode(_out_encoding(), 'replace') sys.stdout.write(txt) @@ -207,7 +207,7 @@ def input_(prompt=None): # use print_() explicitly to display prompts. # http://bugs.python.org/issue1927 if prompt: - print_(prompt, end=' ') + print_(prompt, end=u' ') try: resp = input() From 6b3cc6ff379419a939dc8fdb4a97591571cbe73b Mon Sep 17 00:00:00 2001 From: Johnny Robeson Date: Wed, 29 Jun 2016 03:17:02 -0400 Subject: [PATCH 2/2] adapt print_() callers to send unicode strings --- beets/ui/commands.py | 17 +++++++++-------- beetsplug/info.py | 3 ++- beetsplug/play.py | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 0db17b88e..325b549d3 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -86,13 +86,13 @@ def _print_keys(query): returned row, with identation of 2 spaces. """ for row in query: - print_(' ' * 2 + row['key']) + print_(u' ' * 2 + six.text_type(row['key'])) def fields_func(lib, opts, args): def _print_rows(names): names.sort() - print_(" " + "\n ".join(names)) + print_(six.text_type(' ' + '\n '.join(names))) print_(u"Item fields:") _print_rows(library.Item.all_keys()) @@ -1060,10 +1060,10 @@ def list_items(lib, query, album, fmt=''): """ if album: for album in lib.albums(query): - ui.print_(format(album, fmt)) + ui.print_(six.text_type(format(album, fmt))) else: for item in lib.items(query): - ui.print_(format(item, fmt)) + ui.print_(six.text_type(format(item, fmt))) def list_func(lib, opts, args): @@ -1211,7 +1211,7 @@ def remove_items(lib, query, album, delete, force): prompt = u'Really DELETE %i file%s (y/n)?' % \ (len(items), 's' if len(items) > 1 else '') else: - fmt = '' + fmt = u'' prompt = u'Really remove %i item%s from the library (y/n)?' % \ (len(items), 's' if len(items) > 1 else '') @@ -1608,7 +1608,7 @@ def config_func(lib, opts, args): filenames.insert(0, user_path) for filename in filenames: - print_(filename) + print_(displayable_path(filename)) # Open in editor. elif opts.edit: @@ -1616,7 +1616,8 @@ def config_func(lib, opts, args): # Dump configuration. else: - print_(config.dump(full=opts.defaults, redact=opts.redact)) + config_out = config.dump(full=opts.defaults, redact=opts.redact) + print_(six.text_type(config_out)) def config_edit(): @@ -1662,7 +1663,7 @@ default_commands.append(config_cmd) def print_completion(*args): for line in completion_script(default_commands + plugins.commands()): - print_(line, end='') + print_(six.text_type(line), end=u'') if not any(map(os.path.isfile, BASH_COMPLETION_PATHS)): log.warn(u'Warning: Unable to find the bash-completion package. ' u'Command line completion might not work.') diff --git a/beetsplug/info.py b/beetsplug/info.py index 8e6f25953..3865865d4 100644 --- a/beetsplug/info.py +++ b/beetsplug/info.py @@ -20,6 +20,7 @@ from __future__ import division, absolute_import, print_function import os import re +import six from beets.plugins import BeetsPlugin from beets import ui @@ -91,7 +92,7 @@ def print_data(data, item=None, fmt=None): """ if fmt: # use fmt specified by the user - ui.print_(format(item, fmt)) + ui.print_(six.text_type(format(item, fmt))) return path = displayable_path(item.path) if item else None diff --git a/beetsplug/play.py b/beetsplug/play.py index 465d8e6d5..409ac46f9 100644 --- a/beetsplug/play.py +++ b/beetsplug/play.py @@ -128,7 +128,7 @@ class PlayPlugin(BeetsPlugin): u'You are about to queue {0} {1}.'.format( len(selection), item_type))) - if ui.input_options(('Continue', 'Abort')) == 'a': + if ui.input_options((u'Continue', u'Abort')) == 'a': return ui.print_(u'Playing {0} {1}.'.format(len(selection), item_type))