From 5716cd10240a2bda57abfa8d3bc700b0d2a89643 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 6 Mar 2014 00:21:33 +0000 Subject: [PATCH] Fix _safe_cast of ASFUnicodeAttribute Fixes #578 --- beets/mediafile.py | 10 +++++----- test/test_mediafile_edge.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/beets/mediafile.py b/beets/mediafile.py index de63a1f58..c4854ad3f 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -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 diff --git a/test/test_mediafile_edge.py b/test/test_mediafile_edge.py index f87eaf132..e5061275c 100644 --- a/test/test_mediafile_edge.py +++ b/test/test_mediafile_edge.py @@ -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)