MediaFile: detect missing numbers (fix #901)

This commit is contained in:
Adrian Sampson 2014-08-18 10:17:01 -07:00
parent 09b0e1c75d
commit bf553eae34
3 changed files with 12 additions and 5 deletions

View file

@ -154,11 +154,12 @@ def _safe_cast(out_type, val):
else:
if not isinstance(val, basestring):
val = unicode(val)
val = re.match(r'[\+-]?[0-9\.]*', val.strip()).group(0)
if not val:
return 0.0
else:
return float(val)
match = re.match(r'[\+-]?[0-9\.]+', val.strip())
if match:
val = match.group(0)
if val:
return float(val)
return 0.0
else:
return val

View file

@ -77,6 +77,8 @@ Little improvements and fixes:
* :doc:`/plugins/convert`: A new ``--pretend`` option lets you preview the
commands the plugin will execute without actually taking any action. Thanks
to Dietrich Daroch.
* Fix a crash when a float-valued tag field only contained a ``+`` or ``-``
character.

View file

@ -107,6 +107,10 @@ class InvalidValueToleranceTest(unittest.TestCase):
self.assertTrue(isinstance(us, unicode))
self.assertTrue(us.startswith(u'caf'))
def test_safe_cast_float_with_no_numbers(self):
v = _sc(float, '+')
self.assertEqual(v, 0.0)
class SafetyTest(unittest.TestCase, TestHelper):
def setUp(self):