From 2f5f9ea17465be028a741c5c5fbc020a642e3dbd Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 19 Aug 2021 17:14:16 -0400 Subject: [PATCH] Remove shlex_split utility This works around a bug that does not exist in Python 3.x, and the workaround (by calling the underlying shlex.split function with bytes) was causing crashes on some versions of Python 3. Seemed to work fine on 3.10-dev, though, oddly. --- beets/library.py | 3 ++- beets/util/__init__.py | 21 +-------------------- beetsplug/edit.py | 3 ++- beetsplug/hook.py | 5 +++-- beetsplug/play.py | 3 ++- 5 files changed, 10 insertions(+), 25 deletions(-) diff --git a/beets/library.py b/beets/library.py index ae6d384bc..54911488a 100644 --- a/beets/library.py +++ b/beets/library.py @@ -24,6 +24,7 @@ import time import re import six import string +import shlex from beets import logging from mediafile import MediaFile, UnreadableFileError @@ -1391,7 +1392,7 @@ def parse_query_string(s, model_cls): message = u"Query is not unicode: {0!r}".format(s) assert isinstance(s, six.text_type), message try: - parts = util.shlex_split(s) + parts = shlex.split(s) except ValueError as exc: raise dbcore.InvalidQueryError(s, exc) return parse_query_parts(parts, model_cls) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 541e5f366..f43f11db5 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -924,25 +924,6 @@ def editor_command(): return open_anything() -def shlex_split(s): - """Split a Unicode or bytes string according to shell lexing rules. - - 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 isinstance(s, bytes): # Shlex works fine. - return shlex.split(s) - - elif isinstance(s, six.text_type): - # Work around a Python bug. - # http://bugs.python.org/issue6988 - bs = s.encode('utf-8') - return [c.decode('utf-8') for c in shlex.split(bs)] - - else: - raise TypeError(u'shlex_split called with non-string') - - def interactive_open(targets, command): """Open the files in `targets` by `exec`ing a new `command`, given as a Unicode string. (The new program takes over, and Python @@ -954,7 +935,7 @@ def interactive_open(targets, command): # Split the command string into its arguments. try: - args = shlex_split(command) + args = shlex.split(command) except ValueError: # Malformed shell tokens. args = [command] diff --git a/beetsplug/edit.py b/beetsplug/edit.py index 892052758..1d923cc36 100644 --- a/beetsplug/edit.py +++ b/beetsplug/edit.py @@ -29,6 +29,7 @@ import yaml from tempfile import NamedTemporaryFile import os import six +import shlex # These "safe" types can avoid the format/parse cycle that most fields go @@ -45,7 +46,7 @@ class ParseError(Exception): def edit(filename, log): """Open `filename` in a text editor. """ - cmd = util.shlex_split(util.editor_command()) + cmd = shlex.split(util.editor_command()) cmd.append(filename) log.debug(u'invoking editor command: {!r}', cmd) try: diff --git a/beetsplug/hook.py b/beetsplug/hook.py index ff3968a6a..595531f38 100644 --- a/beetsplug/hook.py +++ b/beetsplug/hook.py @@ -18,9 +18,10 @@ from __future__ import division, absolute_import, print_function import string import subprocess +import shlex from beets.plugins import BeetsPlugin -from beets.util import shlex_split, arg_encoding +from beets.util import arg_encoding class CodingFormatter(string.Formatter): @@ -95,7 +96,7 @@ class HookPlugin(BeetsPlugin): # Use a string formatter that works on Unicode strings. formatter = CodingFormatter(arg_encoding()) - command_pieces = shlex_split(command) + command_pieces = shlex.split(command) for i, piece in enumerate(command_pieces): command_pieces[i] = formatter.format(piece, event=event, diff --git a/beetsplug/play.py b/beetsplug/play.py index 408db846e..d41346042 100644 --- a/beetsplug/play.py +++ b/beetsplug/play.py @@ -26,6 +26,7 @@ from beets import util from os.path import relpath from tempfile import NamedTemporaryFile import subprocess +import shlex # Indicate where arguments should be inserted into the command string. # If this is missing, they're placed at the end. @@ -44,7 +45,7 @@ def play(command_str, selection, paths, open_args, log, item_type='track', try: if keep_open: - command = util.shlex_split(command_str) + command = shlex.split(command_str) command = command + open_args subprocess.call(command) else: