remove old Python: remove util.text_string

This was a helper for situations when Python 2 and 3 APIs returned bytes
and unicode, respectively. In these situation, we should nowadays know
which of the two we receive, so there's no need to wrap & hide the
`bytes.decode()` anymore (when it is still required).

Detailed justification:

beets/ui/__init__.py:
- command line options are always parsed to str

beets/ui/commands.py:
- confuse's config.dump always returns str
- open(...) defaults to text mode, read()ing str

beetsplug/keyfinder.py:
- ...

beetsplug/web/__init__.py:
- internally, paths are always bytestrings
- additionally, I took the liberty to slighlty re-arrange the code: it
  makes sense to split off the basename first, since we're only
  interested in the unicode conversion of that part.

test/helper.py:
- capture_stdout() gives a StringIO, which yields str

test/test_ui.py:
- self.io, from _common.TestCase, ultimately contains a
  _common.DummyOut, which appears to be dealing with str (cf.
  DummyOut.get)
This commit is contained in:
wisp3rwind 2022-06-15 12:15:24 +02:00
parent 3510e6311d
commit d24cf69269
7 changed files with 7 additions and 24 deletions

View file

@ -790,9 +790,6 @@ def _store_dict(option, opt_str, value, parser):
setattr(parser.values, dest, {}) setattr(parser.values, dest, {})
option_values = getattr(parser.values, dest) option_values = getattr(parser.values, dest)
# Decode the argument using the platform's argument encoding.
value = util.text_string(value, util.arg_encoding())
try: try:
key, value = value.split('=', 1) key, value = value.split('=', 1)
if not (key and value): if not (key and value):

View file

@ -1778,7 +1778,7 @@ def config_func(lib, opts, args):
else: else:
config_out = config.dump(full=opts.defaults, redact=opts.redact) config_out = config.dump(full=opts.defaults, redact=opts.redact)
if config_out.strip() != '{}': if config_out.strip() != '{}':
print_(util.text_string(config_out)) print_(config_out)
else: else:
print("Empty configuration") print("Empty configuration")
@ -1852,7 +1852,7 @@ def completion_script(commands):
""" """
base_script = os.path.join(os.path.dirname(__file__), 'completion_base.sh') base_script = os.path.join(os.path.dirname(__file__), 'completion_base.sh')
with open(base_script) as base_script: with open(base_script) as base_script:
yield util.text_string(base_script.read()) yield base_script.read()
options = {} options = {}
aliases = {} aliases = {}

View file

@ -770,19 +770,6 @@ def as_string(value):
return str(value) return str(value)
def text_string(value, encoding='utf-8'):
"""Convert a string, which can either be bytes or unicode, to
unicode.
Text (unicode) is left untouched; bytes are decoded. This is useful
to convert from a "native string" (bytes on Python 2, str on Python
3) to a consistently unicode value.
"""
if isinstance(value, bytes):
return value.decode(encoding)
return value
def plurality(objs): def plurality(objs):
"""Given a sequence of hashble objects, returns the object that """Given a sequence of hashble objects, returns the object that
is most common in the set and the its number of appearance. The is most common in the set and the its number of appearance. The

View file

@ -77,7 +77,7 @@ class KeyFinderPlugin(BeetsPlugin):
continue continue
try: try:
key = util.text_string(key_raw) key = key_raw.decode("utf-8")
except UnicodeDecodeError: except UnicodeDecodeError:
self._log.error('output is invalid UTF-8') self._log.error('output is invalid UTF-8')
continue continue

View file

@ -320,9 +320,9 @@ def item_file(item_id):
try: try:
# Imitate http.server behaviour # Imitate http.server behaviour
unicode_base_filename.encode("latin-1", "strict") base_filename.encode("latin-1", "strict")
except UnicodeError: except UnicodeError:
safe_filename = unidecode(unicode_base_filename) safe_filename = unidecode(base_filename)
else: else:
safe_filename = unicode_base_filename safe_filename = unicode_base_filename

View file

@ -453,7 +453,7 @@ class TestHelper:
def run_with_output(self, *args): def run_with_output(self, *args):
with capture_stdout() as out: with capture_stdout() as out:
self.run_command(*args) self.run_command(*args)
return util.text_string(out.getvalue()) return out.getvalue()
# Safe file operations # Safe file operations

View file

@ -1163,8 +1163,7 @@ class ShowChangeTest(_common.TestCase):
cur_album, cur_album,
autotag.AlbumMatch(album_dist, info, mapping, set(), set()), autotag.AlbumMatch(album_dist, info, mapping, set(), set()),
) )
# FIXME decoding shouldn't be done here return self.io.getoutput().lower()
return util.text_string(self.io.getoutput().lower())
def test_null_change(self): def test_null_change(self):
msg = self._show_change() msg = self._show_change()