From 21cff66d08a06a236371685d5677a55bef7d4cc0 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 5 Jul 2012 23:12:36 +0200 Subject: [PATCH] Handle unicode in replacement config The 'decode' call fails in what is already a unicode string. I'm not sure under what circumstances the string is or isn't unicode (apparently it varies), so I added a check. The test passes with the patch, at least. --- beets/ui/__init__.py | 3 ++- test/test_ui.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 1c3f5e019..44372b9b8 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -460,7 +460,8 @@ def _get_replacements(config): repl_string = config_val(config, 'beets', 'replace', None) if not repl_string: return - repl_string = repl_string.decode('utf8') + if not isinstance(repl_string, unicode): + repl_string = repl_string.decode('utf8') parts = repl_string.strip().split() if not parts: diff --git a/test/test_ui.py b/test/test_ui.py index 3c3ddf81d..0d673a30b 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -547,6 +547,14 @@ class ConfigTest(unittest.TestCase): [beets] replace=[xy] z"""), func) + def test_replacements_parsed_unicode(self): + def func(lib, config, opts, args): + replacements = lib.replacements + self.assertEqual(replacements, [(re.compile(ur'\u2019'), u'z')]) + self._run_main([], textwrap.dedent(u""" + [beets] + replace=\u2019 z"""), func) + def test_empty_replacements_produce_none(self): def func(lib, config, opts, args): replacements = lib.replacements