aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/pyexiv2.py20
-rw-r--r--unittest/exif.py49
-rw-r--r--unittest/iptc.py3
-rw-r--r--unittest/xmp.py3
4 files changed, 66 insertions, 9 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index b31bf2c..7a57224 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -449,6 +449,12 @@ class ExifTag(MetadataTag):
except ValueError:
raise ExifValueError(value, xtype)
+ elif xtype in ('Long', 'SLong'):
+ try:
+ return long(value)
+ except ValueError:
+ raise ExifValueError(value, xtype)
+
# TODO: other types
raise ExifValueError(value, xtype)
@@ -469,7 +475,19 @@ class ExifTag(MetadataTag):
@raise L{ExifValueError}: if the conversion fails
"""
if xtype == 'Short':
- if type(value) is int:
+ if type(value) is int and value >= 0:
+ return str(value)
+ else:
+ raise ExifValueError(value, xtype)
+
+ elif xtype == 'Long':
+ if type(value) in (int, long) and value >= 0:
+ return str(value)
+ else:
+ raise ExifValueError(value, xtype)
+
+ elif xtype == 'SLong':
+ if type(value) in (int, long):
return str(value)
else:
raise ExifValueError(value, xtype)
diff --git a/unittest/exif.py b/unittest/exif.py
index ae5fca0..d46f138 100644
--- a/unittest/exif.py
+++ b/unittest/exif.py
@@ -35,7 +35,6 @@ class TestExifTag(unittest.TestCase):
# Valid values
self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
- self.assertEqual(ExifTag._convert_to_python('-4', xtype), -4)
# Invalid values
self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
@@ -46,8 +45,50 @@ class TestExifTag(unittest.TestCase):
xtype = 'Short'
# Valid values
self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
- self.assertEqual(ExifTag._convert_to_string(-57, xtype), '-57')
# Invalid values
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, -57, xtype)
self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
- self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, '3.14', xtype)
- self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, '1E3', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)
+
+ def test_convert_to_python_long(self):
+ xtype = 'Long'
+ # Valid values
+ self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
+ self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
+ # Invalid values
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype)
+
+ def test_convert_to_string_long(self):
+ xtype = 'Long'
+ # Valid values
+ self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
+ self.assertEqual(ExifTag._convert_to_string(678024, xtype), '678024')
+ # Invalid values
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, -57, xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)
+
+ def test_convert_to_python_slong(self):
+ xtype = 'SLong'
+ # Valid values
+ self.assertEqual(ExifTag._convert_to_python('23', xtype), 23)
+ self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628)
+ self.assertEqual(ExifTag._convert_to_python('-437', xtype), -437)
+ # Invalid values
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype)
+
+ def test_convert_to_string_slong(self):
+ xtype = 'SLong'
+ # Valid values
+ self.assertEqual(ExifTag._convert_to_string(123, xtype), '123')
+ self.assertEqual(ExifTag._convert_to_string(678024, xtype), '678024')
+ self.assertEqual(ExifTag._convert_to_string(-437, xtype), '-437')
+ # Invalid values
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype)
+ self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype)
diff --git a/unittest/iptc.py b/unittest/iptc.py
index 9987b9d..a52ab01 100644
--- a/unittest/iptc.py
+++ b/unittest/iptc.py
@@ -50,8 +50,7 @@ class TestIptcTag(unittest.TestCase):
self.assertEqual(IptcTag._convert_to_string(-57, xtype), '-57')
# Invalid values
self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype)
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, '3.14', xtype)
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, '1E3', xtype)
+ self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 3.14, xtype)
def test_convert_to_python_string(self):
xtype = 'String'
diff --git a/unittest/xmp.py b/unittest/xmp.py
index 970f8fb..c672d9d 100644
--- a/unittest/xmp.py
+++ b/unittest/xmp.py
@@ -165,8 +165,7 @@ class TestXmpTag(unittest.TestCase):
self.assertEqual(XmpTag._convert_to_string(-57, xtype), '-57')
# Invalid values
self.failUnlessRaises(XmpValueError, XmpTag._convert_to_string, 'invalid', xtype)
- self.failUnlessRaises(XmpValueError, XmpTag._convert_to_string, '3.14', xtype)
- self.failUnlessRaises(XmpValueError, XmpTag._convert_to_string, '1E3', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_string, 3.14, xtype)
def test_convert_to_python_langalt(self):
xtype = 'Lang Alt'