mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
mediafile._safe_cast: be safer when converting to int
The regex «[\+-]?[0-9]*» possibly matches a single minus/plus, which would then be passed on to int(), raising a ValueError from within _safe_cast. The test suite covered this for float, but not for int. We now make sure we actually have a number after the sign by using a Kleene plus.
This commit is contained in:
parent
c10e62f212
commit
c40ea4f22a
3 changed files with 7 additions and 5 deletions
|
|
@ -159,11 +159,8 @@ def _safe_cast(out_type, val):
|
||||||
elif not isinstance(val, six.string_types):
|
elif not isinstance(val, six.string_types):
|
||||||
val = six.text_type(val)
|
val = six.text_type(val)
|
||||||
# Get a number from the front of the string.
|
# Get a number from the front of the string.
|
||||||
val = re.match(r'[\+-]?[0-9]*', val.strip()).group(0)
|
match = re.match(r'[\+-]?[0-9]+', val.strip())
|
||||||
if not val:
|
return int(match.group(0)) if match else 0
|
||||||
return 0
|
|
||||||
else:
|
|
||||||
return int(val)
|
|
||||||
|
|
||||||
elif out_type == bool:
|
elif out_type == bool:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,8 @@ Fixes:
|
||||||
versions of Mutagen. :bug:`2716`
|
versions of Mutagen. :bug:`2716`
|
||||||
* :doc:`/plugins/fetchart`: Fix: don't skip running the fetchart plugin during import, when the
|
* :doc:`/plugins/fetchart`: Fix: don't skip running the fetchart plugin during import, when the
|
||||||
"Edit Candidates" option is used. :bug:`2734`
|
"Edit Candidates" option is used. :bug:`2734`
|
||||||
|
* Fix a crash when numeric metadata fields contain just a minus or plus sign
|
||||||
|
with no following numbers.
|
||||||
|
|
||||||
For developers:
|
For developers:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,9 @@ class InvalidValueToleranceTest(unittest.TestCase):
|
||||||
def test_safe_cast_string_to_int(self):
|
def test_safe_cast_string_to_int(self):
|
||||||
self.assertEqual(_sc(int, u'something'), 0)
|
self.assertEqual(_sc(int, u'something'), 0)
|
||||||
|
|
||||||
|
def test_safe_cast_string_to_int_with_no_numbers(self):
|
||||||
|
self.assertEqual(_sc(int, u'-'), 0)
|
||||||
|
|
||||||
def test_safe_cast_int_string_to_int(self):
|
def test_safe_cast_int_string_to_int(self):
|
||||||
self.assertEqual(_sc(int, u'20'), 20)
|
self.assertEqual(_sc(int, u'20'), 20)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue