diff --git a/beets/mediafile.py b/beets/mediafile.py index da3b32906..3d86b0750 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -114,7 +114,12 @@ def _safe_cast(out_type, val): if val is None: return u'' else: - return unicode(val) + if isinstance(val, str): + return val.decode('utf-8', errors='ignore') + elif isinstance(val, unicode): + return val + else: + return unicode(val) elif out_type == float: if val is None: diff --git a/docs/changelog.rst b/docs/changelog.rst index 2cbb81307..111c927e2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,8 @@ Changelog included with beets, making it easy to fetch **song lyrics**. * Fix a bug in the ``rewrite`` plugin that broke the use of multiple rules for a single field. +* Fix a crash with non-ASCII characters in bytestring metadata fields (e.g., + MusicBrainz IDs). 1.0b12 (January 16, 2012) diff --git a/test/test_mediafile.py b/test/test_mediafile.py index a75d6b2d2..e0b8ef84d 100644 --- a/test/test_mediafile.py +++ b/test/test_mediafile.py @@ -106,6 +106,11 @@ class InvalidValueToleranceTest(unittest.TestCase): def test_safe_cast_negative_string_to_float(self): self.assertAlmostEqual(_sc(float, '-1.234'), -1.234) + def test_safe_cast_special_chars_to_unicode(self): + us = _sc(unicode, 'caf\xc3\xa9') + self.assertTrue(isinstance(us, unicode)) + self.assertTrue(us.startswith(u'caf')) + class SafetyTest(unittest.TestCase): def _exccheck(self, fn, exc, data=''): fn = os.path.join(_common.RSRC, fn)