From 9fe171c5cedd1a2abd96679d89ffc159239b1985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Koutensk=C3=BD?= Date: Wed, 17 May 2017 21:37:38 +0200 Subject: [PATCH] properly safe cast unicode as int --- beets/mediafile.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beets/mediafile.py b/beets/mediafile.py index 9242ab1f1..50c2cb76b 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -154,10 +154,12 @@ def _safe_cast(out_type, val): return int(val) else: # Process any other type as a string. - if not isinstance(val, six.string_types): + if isinstance(val, bytes): + val = val.decode('utf-8', 'ignore') + elif not isinstance(val, six.string_types): val = six.text_type(val) # Get a number from the front of the string. - val = re.match(r'[0-9]*', val.strip()).group(0) + val = re.match(r'[\+-]?[0-9]*', val.strip()).group(0) if not val: return 0 else: