Merge pull request #579 from johtso/fix-safe-case-asfunicodeattribute

Fix _safe_cast of ASFUnicodeAttribute
This commit is contained in:
geigerzaehler 2014-03-06 11:20:16 +01:00
commit 69cb44003e
2 changed files with 11 additions and 5 deletions

View file

@ -94,6 +94,9 @@ def _safe_cast(out_type, val):
returned. out_type should be bool, int, or unicode; otherwise, the
value is just passed through.
"""
if isinstance(val, mutagen.asf.ASFBaseAttribute):
val = val.value
if out_type == int:
if val is None:
return 0
@ -116,11 +119,8 @@ def _safe_cast(out_type, val):
return False
else:
try:
if isinstance(val, mutagen.asf.ASFBoolAttribute):
return val.value
else:
# Should work for strings, bools, ints:
return bool(int(val))
# Should work for strings, bools, ints:
return bool(int(val))
except ValueError:
return False

View file

@ -17,6 +17,8 @@
import os
import shutil
import mutagen
import _common
from _common import unittest
import beets.mediafile
@ -81,6 +83,10 @@ class InvalidValueToleranceTest(unittest.TestCase):
def test_safe_cast_string_to_bool(self):
self.assertEqual(_sc(bool, 'whatever'), False)
def test_safe_cast_asfunicodeattribute_to_bool(self):
self.assertEqual(_sc(bool, mutagen.asf.ASFUnicodeAttribute('foo')),
False)
def test_safe_cast_intstring_to_bool(self):
self.assertEqual(_sc(bool, '5'), True)