diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index ae6151f34..23f61d41c 100755 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -31,10 +31,7 @@ from beets.util import py3_path from beets.dbcore import types from .query import MatchQuery, NullSort, TrueQuery import six -if six.PY2: - from collections import Mapping -else: - from collections.abc import Mapping +from collections.abc import Mapping class DBAccessError(Exception): diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 4f19f4f8d..b9f346a9e 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -25,9 +25,6 @@ import unicodedata from functools import reduce import six -if not six.PY2: - buffer = memoryview # sqlite won't accept memoryview in python 2 - class ParsingError(ValueError): """Abstract class for any unparseable user-requested album/query @@ -260,8 +257,8 @@ class BytesQuery(MatchQuery): if isinstance(self.pattern, (six.text_type, bytes)): if isinstance(self.pattern, six.text_type): self.pattern = self.pattern.encode('utf-8') - self.buf_pattern = buffer(self.pattern) - elif isinstance(self.pattern, buffer): + self.buf_pattern = memoryview(self.pattern) + elif isinstance(self.pattern, memoryview): self.buf_pattern = self.pattern self.pattern = bytes(self.pattern) diff --git a/beets/dbcore/types.py b/beets/dbcore/types.py index c85eb1a50..a5c06bac0 100644 --- a/beets/dbcore/types.py +++ b/beets/dbcore/types.py @@ -21,9 +21,6 @@ from . import query from beets.util import str2bool import six -if not six.PY2: - buffer = memoryview # sqlite won't accept memoryview in python 2 - # Abstract base. @@ -104,7 +101,7 @@ class Type(object): `sql_value` is either a `buffer`/`memoryview` or a `unicode` object` and the method must handle these in addition. """ - if isinstance(sql_value, buffer): + if isinstance(sql_value, memoryview): sql_value = bytes(sql_value).decode('utf-8', 'ignore') if isinstance(sql_value, six.text_type): return self.parse(sql_value) diff --git a/beets/library.py b/beets/library.py index dcd5a6a1f..ae6d384bc 100644 --- a/beets/library.py +++ b/beets/library.py @@ -37,13 +37,9 @@ from beets.dbcore import types import beets # To use the SQLite "blob" type, it doesn't suffice to provide a byte -# string; SQLite treats that as encoded text. Wrapping it in a `buffer` or a -# `memoryview`, depending on the Python version, tells it that we -# actually mean non-text data. -if six.PY2: - BLOB_TYPE = buffer # noqa: F821 -else: - BLOB_TYPE = memoryview +# string; SQLite treats that as encoded text. Wrapping it in a tells it +# that we actually mean non-text data. +BLOB_TYPE = memoryview log = logging.getLogger('beets') @@ -1408,10 +1404,7 @@ def _sqlite_bytelower(bytestring): ``-DSQLITE_LIKE_DOESNT_MATCH_BLOBS``. See ``https://github.com/beetbox/beets/issues/2172`` for details. """ - if not six.PY2: - return bytestring.lower() - - return buffer(bytes(bytestring).lower()) # noqa: F821 + return bytestring.lower() # The Library: interface to the database. diff --git a/beets/plugins.py b/beets/plugins.py index 23f938169..223178ab4 100644 --- a/beets/plugins.py +++ b/beets/plugins.py @@ -129,14 +129,9 @@ class BeetsPlugin(object): value after the function returns). Also determines which params may not be sent for backwards-compatibility. """ - if six.PY2: - argspec = inspect.getargspec(func) - func_args = argspec.args - has_varkw = argspec.keywords is not None - else: - argspec = inspect.getfullargspec(func) - func_args = argspec.args - has_varkw = argspec.varkw is not None + argspec = inspect.getfullargspec(func) + func_args = argspec.args + has_varkw = argspec.varkw is not None @wraps(func) def wrapper(*args, **kwargs): diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 8d980d549..af5b77007 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -113,10 +113,7 @@ def decargs(arglist): """Given a list of command-line argument bytestrings, attempts to decode them to Unicode strings when running under Python 2. """ - if six.PY2: - return [s.decode(util.arg_encoding()) for s in arglist] - else: - return arglist + return arglist def print_(*strings, **kwargs): @@ -138,23 +135,18 @@ def print_(*strings, **kwargs): txt += kwargs.get('end', u'\n') # Encode the string and write it to stdout. - if six.PY2: - # On Python 2, sys.stdout expects bytes. + # On Python 3, sys.stdout expects text strings and uses the + # exception-throwing encoding error policy. To avoid throwing + # errors and use our configurable encoding override, we use the + # underlying bytes buffer instead. + if hasattr(sys.stdout, 'buffer'): out = txt.encode(_out_encoding(), 'replace') - sys.stdout.write(out) + sys.stdout.buffer.write(out) + sys.stdout.buffer.flush() else: - # On Python 3, sys.stdout expects text strings and uses the - # exception-throwing encoding error policy. To avoid throwing - # errors and use our configurable encoding override, we use the - # underlying bytes buffer instead. - if hasattr(sys.stdout, 'buffer'): - out = txt.encode(_out_encoding(), 'replace') - sys.stdout.buffer.write(out) - sys.stdout.buffer.flush() - else: - # In our test harnesses (e.g., DummyOut), sys.stdout.buffer - # does not exist. We instead just record the text string. - sys.stdout.write(txt) + # In our test harnesses (e.g., DummyOut), sys.stdout.buffer + # does not exist. We instead just record the text string. + sys.stdout.write(txt) # Configuration wrappers. @@ -213,10 +205,7 @@ def input_(prompt=None): except EOFError: raise UserError(u'stdin stream ended while input required') - if six.PY2: - return resp.decode(_in_encoding(), 'ignore') - else: - return resp + return resp def input_options(options, require=False, prompt=None, fallback_prompt=None, diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 38687b3a9..456e0afb7 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -963,12 +963,12 @@ def import_func(lib, opts, args): if not paths: raise ui.UserError(u'no path specified') - # On Python 2, we get filenames as raw bytes, which is what we - # need. On Python 3, we need to undo the "helpful" conversion to - # Unicode strings to get the real bytestring filename. - if not six.PY2: - paths = [p.encode(util.arg_encoding(), 'surrogateescape') - for p in paths] + # On Python 2, we used to get filenames as raw bytes, which is + # what we need. On Python 3, we need to undo the "helpful" + # conversion to Unicode strings to get the real bytestring + # filename. + paths = [p.encode(util.arg_encoding(), 'surrogateescape') + for p in paths] import_files(lib, paths, query) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 0ae646a22..541e5f366 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -753,10 +753,7 @@ def as_string(value): """Convert a value to a Unicode object for matching with a query. None becomes the empty string. Bytestrings are silently decoded. """ - if six.PY2: - buffer_types = buffer, memoryview # noqa: F821 - else: - buffer_types = memoryview + buffer_types = memoryview if value is None: return u'' @@ -829,12 +826,8 @@ def convert_command_args(args): assert isinstance(args, list) def convert(arg): - if six.PY2: - if isinstance(arg, six.text_type): - arg = arg.encode(arg_encoding()) - else: - if isinstance(arg, bytes): - arg = arg.decode(arg_encoding(), 'surrogateescape') + if isinstance(arg, bytes): + arg = arg.decode(arg_encoding(), 'surrogateescape') return arg return [convert(a) for a in args] @@ -937,7 +930,7 @@ def shlex_split(s): Raise `ValueError` if the string is not a well-formed shell string. This is a workaround for a bug in some versions of Python. """ - if not six.PY2 or isinstance(s, bytes): # Shlex works fine. + if isinstance(s, bytes): # Shlex works fine. return shlex.split(s) elif isinstance(s, six.text_type): @@ -1120,13 +1113,9 @@ def decode_commandline_path(path): *reversed* to recover the same bytes before invoking the OS. On Windows, we want to preserve the Unicode filename "as is." """ - if six.PY2: - # On Python 2, substitute the bytestring directly into the template. - return path + # On Python 3, the template is a Unicode string, which only supports + # substitution of Unicode variables. + if platform.system() == 'Windows': + return path.decode(_fsencoding()) else: - # On Python 3, the template is a Unicode string, which only supports - # substitution of Unicode variables. - if platform.system() == 'Windows': - return path.decode(_fsencoding()) - else: - return path.decode(arg_encoding(), 'surrogateescape') + return path.decode(arg_encoding(), 'surrogateescape') diff --git a/beets/util/functemplate.py b/beets/util/functemplate.py index 266534a9b..2621ebe30 100644 --- a/beets/util/functemplate.py +++ b/beets/util/functemplate.py @@ -128,24 +128,15 @@ def compile_func(arg_names, statements, name='_the_func', debug=False): the resulting Python function. If `debug`, then print out the bytecode of the compiled function. """ - if six.PY2: - name = name.encode('utf-8') - args = ast.arguments( - args=[ast.Name(n, ast.Param()) for n in arg_names], - vararg=None, - kwarg=None, - defaults=[ex_literal(None) for _ in arg_names], - ) - else: - args_fields = { - 'args': [ast.arg(arg=n, annotation=None) for n in arg_names], - 'kwonlyargs': [], - 'kw_defaults': [], - 'defaults': [ex_literal(None) for _ in arg_names], - } - if 'posonlyargs' in ast.arguments._fields: # Added in Python 3.8. - args_fields['posonlyargs'] = [] - args = ast.arguments(**args_fields) + args_fields = { + 'args': [ast.arg(arg=n, annotation=None) for n in arg_names], + 'kwonlyargs': [], + 'kw_defaults': [], + 'defaults': [ex_literal(None) for _ in arg_names], + } + if 'posonlyargs' in ast.arguments._fields: # Added in Python 3.8. + args_fields['posonlyargs'] = [] + args = ast.arguments(**args_fields) func_def = ast.FunctionDef( name=name, @@ -201,10 +192,7 @@ class Symbol(object): def translate(self): """Compile the variable lookup.""" - if six.PY2: - ident = self.ident.encode('utf-8') - else: - ident = self.ident + ident = self.ident expr = ex_rvalue(VARIABLE_PREFIX + ident) return [expr], set([ident]), set() @@ -239,11 +227,7 @@ class Call(object): def translate(self): """Compile the function call.""" varnames = set() - if six.PY2: - ident = self.ident.encode('utf-8') - else: - ident = self.ident - funcnames = set([ident]) + funcnames = set([self.ident]) arg_exprs = [] for arg in self.args: @@ -265,7 +249,7 @@ class Call(object): )) subexpr_call = ex_call( - FUNCTION_PREFIX + ident, + FUNCTION_PREFIX + self.ident, arg_exprs ) return [subexpr_call], varnames, funcnames diff --git a/beetsplug/bpd/__init__.py b/beetsplug/bpd/__init__.py index 628353942..ef427024a 100644 --- a/beetsplug/bpd/__init__.py +++ b/beetsplug/bpd/__init__.py @@ -984,12 +984,7 @@ class Command(object): raise AttributeError(u'unknown command "{}"'.format(self.name)) func = getattr(target, func_name) - if six.PY2: - # caution: the fields of the namedtuple are slightly different - # between the results of getargspec and getfullargspec. - argspec = inspect.getargspec(func) - else: - argspec = inspect.getfullargspec(func) + argspec = inspect.getfullargspec(func) # Check that `func` is able to handle the number of arguments sent # by the client (so we can raise ERROR_ARG instead of ERROR_SYSTEM). diff --git a/beetsplug/convert.py b/beetsplug/convert.py index dfc792a77..36cfd0ee9 100644 --- a/beetsplug/convert.py +++ b/beetsplug/convert.py @@ -16,14 +16,13 @@ """Converts tracks or albums to external directory """ from __future__ import division, absolute_import, print_function -from beets.util import par_map, decode_commandline_path, arg_encoding +from beets.util import par_map, decode_commandline_path import os import threading import subprocess import tempfile import shlex -import six from string import Template from beets import ui, util, plugins, config @@ -204,9 +203,6 @@ class ConvertPlugin(BeetsPlugin): if not quiet and not pretend: self._log.info(u'Encoding {0}', util.displayable_path(source)) - if not six.PY2: - command = command.decode(arg_encoding(), 'surrogateescape') - source = decode_commandline_path(source) dest = decode_commandline_path(dest) @@ -218,10 +214,7 @@ class ConvertPlugin(BeetsPlugin): 'source': source, 'dest': dest, }) - if six.PY2: - encode_cmd.append(args[i]) - else: - encode_cmd.append(args[i].encode(util.arg_encoding())) + encode_cmd.append(args[i].encode(util.arg_encoding())) if pretend: self._log.info(u'{0}', u' '.join(ui.decargs(args))) diff --git a/beetsplug/edit.py b/beetsplug/edit.py index 9dbfcdd17..892052758 100644 --- a/beetsplug/edit.py +++ b/beetsplug/edit.py @@ -244,15 +244,10 @@ class EditPlugin(plugins.BeetsPlugin): old_data = [flatten(o, fields) for o in objs] # Set up a temporary file with the initial data for editing. - if six.PY2: - new = NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) - else: - new = NamedTemporaryFile(mode='w', suffix='.yaml', delete=False, - encoding='utf-8') + new = NamedTemporaryFile(mode='w', suffix='.yaml', delete=False, + encoding='utf-8') old_str = dump(old_data) new.write(old_str) - if six.PY2: - old_str = old_str.decode('utf-8') new.close() # Loop until we have parseable data and the user confirms. diff --git a/beetsplug/metasync/itunes.py b/beetsplug/metasync/itunes.py index 3cf34e58f..c36bebc8f 100644 --- a/beetsplug/metasync/itunes.py +++ b/beetsplug/metasync/itunes.py @@ -24,7 +24,6 @@ import shutil import tempfile import plistlib -import six from six.moves.urllib.parse import urlparse, unquote from time import mktime @@ -86,11 +85,8 @@ class Itunes(MetaSource): self._log.debug( u'loading iTunes library from {0}'.format(library_path)) with create_temporary_copy(library_path) as library_copy: - if six.PY2: - raw_library = plistlib.readPlist(library_copy) - else: - with open(library_copy, 'rb') as library_copy_f: - raw_library = plistlib.load(library_copy_f) + with open(library_copy, 'rb') as library_copy_f: + raw_library = plistlib.load(library_copy_f) except IOError as e: raise ConfigValueError(u'invalid iTunes library: ' + e.strerror) except Exception: diff --git a/test/_common.py b/test/_common.py index e44fac48b..2f2cffcf7 100644 --- a/test/_common.py +++ b/test/_common.py @@ -21,7 +21,6 @@ import sys import os import tempfile import shutil -import six import unittest from contextlib import contextmanager @@ -263,10 +262,7 @@ class DummyOut(object): self.buf.append(s) def get(self): - if six.PY2: - return b''.join(self.buf) - else: - return ''.join(self.buf) + return ''.join(self.buf) def flush(self): self.clear() @@ -284,10 +280,7 @@ class DummyIn(object): self.out = out def add(self, s): - if six.PY2: - self.buf.append(s + b'\n') - else: - self.buf.append(s + '\n') + self.buf.append(s + '\n') def close(self): pass diff --git a/test/helper.py b/test/helper.py index 0b6eba718..69717830b 100644 --- a/test/helper.py +++ b/test/helper.py @@ -90,8 +90,6 @@ def control_stdin(input=None): """ org = sys.stdin sys.stdin = StringIO(input) - if six.PY2: # StringIO encoding attr isn't writable in python >= 3 - sys.stdin.encoding = 'utf-8' try: yield sys.stdin finally: @@ -110,8 +108,6 @@ def capture_stdout(): """ org = sys.stdout sys.stdout = capture = StringIO() - if six.PY2: # StringIO encoding attr isn't writable in python >= 3 - sys.stdout.encoding = 'utf-8' try: yield sys.stdout finally: @@ -124,12 +120,8 @@ def _convert_args(args): on Python 3. """ for i, elem in enumerate(args): - if six.PY2: - if isinstance(elem, six.text_type): - args[i] = elem.encode(util.arg_encoding()) - else: - if isinstance(elem, bytes): - args[i] = elem.decode(util.arg_encoding()) + if isinstance(elem, bytes): + args[i] = elem.decode(util.arg_encoding()) return args diff --git a/test/test_pipeline.py b/test/test_pipeline.py index 82f155521..caf92d87e 100644 --- a/test/test_pipeline.py +++ b/test/test_pipeline.py @@ -136,10 +136,7 @@ class ExceptionTest(unittest.TestCase): pull = pl.pull() for i in range(3): next(pull) - if six.PY2: - self.assertRaises(ExceptionFixture, pull.next) - else: - self.assertRaises(ExceptionFixture, pull.__next__) + self.assertRaises(ExceptionFixture, pull.__next__) class ParallelExceptionTest(unittest.TestCase): diff --git a/test/test_ui.py b/test/test_ui.py index 5cfed1fda..ab1b4dac1 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -22,7 +22,6 @@ import shutil import re import subprocess import platform -import six import unittest from mock import patch, Mock @@ -66,8 +65,6 @@ class ListTest(unittest.TestCase): stdout = self._run_list([u'na\xefve']) out = stdout.getvalue() - if six.PY2: - out = out.decode(stdout.encoding) self.assertTrue(u'na\xefve' in out) def test_list_item_path(self): diff --git a/test/test_util.py b/test/test_util.py index 0d0bbd7b0..b2452d597 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -104,8 +104,6 @@ class UtilTest(unittest.TestCase): ]) self.assertEqual(p, u'foo/_/bar') - @unittest.skipIf(six.PY2, 'surrogateescape error handler not available' - 'on Python 2') def test_convert_command_args_keeps_undecodeable_bytes(self): arg = b'\x82' # non-ascii bytes cmd_args = util.convert_command_args([arg])