Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hook byte string interpolation #3167

Merged
merged 2 commits into from Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions beetsplug/hook.py
Expand Up @@ -18,7 +18,6 @@

import string
import subprocess
import six

from beets.plugins import BeetsPlugin
from beets.util import shlex_split, arg_encoding
Expand Down Expand Up @@ -46,10 +45,8 @@ def format(self, format_string, *args, **kwargs):

See str.format and string.Formatter.format.
"""
try:
if isinstance(format_string, bytes):
format_string = format_string.decode(self._coding)
except UnicodeEncodeError:
pass

return super(CodingFormatter, self).format(format_string, *args,
**kwargs)
Expand Down Expand Up @@ -96,10 +93,7 @@ def hook_function(**kwargs):
return

# Use a string formatter that works on Unicode strings.
if six.PY2:
formatter = CodingFormatter(arg_encoding())
else:
formatter = string.Formatter()
formatter = CodingFormatter(arg_encoding())

command_pieces = shlex_split(command)

Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -150,6 +150,8 @@ Fixes:
* :doc:`/plugins/badfiles`: Avoid a crash when the underlying tool emits
undecodable output.
:bug:`3165`
* :doc:`/plugins/hook`: Fix byte string interpolation in hook commands.
:bug:`2967` :bug:`3167`

.. _python-itunes: https://github.com/ocelma/python-itunes

Expand Down
19 changes: 19 additions & 0 deletions test/test_hook.py
Expand Up @@ -110,6 +110,25 @@ def test_hook_argument_substitution(self):
self.assertTrue(os.path.isfile(path))
os.remove(path)

def test_hook_bytes_interpolation(self):
temporary_paths = [
get_temporary_path().encode('utf-8')
for i in range(self.TEST_HOOK_COUNT)
]

for index, path in enumerate(temporary_paths):
self._add_hook('test_bytes_event_{0}'.format(index),
'touch "{path}"')

self.load_plugins('hook')

for index, path in enumerate(temporary_paths):
plugins.send('test_bytes_event_{0}'.format(index), path=path)

for path in temporary_paths:
self.assertTrue(os.path.isfile(path))
os.remove(path)


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)
Expand Down