unicode in str MediaFile fields (#311, closes #15)

This commit is contained in:
Adrian Sampson 2012-01-27 12:02:26 -08:00
parent dbab290ba4
commit f685bdd89a
3 changed files with 13 additions and 1 deletions

View file

@ -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:

View file

@ -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)

View file

@ -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)