diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-11-17 09:19:33 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-11-17 09:19:33 +0100 |
commit | 063b2729c2ad04b0b5385b369873294b51655bde (patch) | |
tree | 6042562704918c7e60991a68d09934ca19566dab | |
parent | 712d3725f35a9bfdb55b4bcf41dd194a5786ef32 (diff) | |
download | pyexiv2-063b2729c2ad04b0b5385b369873294b51655bde.tar.gz |
Throw an exception instead of silently failing when unable to parse the raw value for EXIF and IPTC tags.
Note that parsing never fails for XMP tags.
Added corresponding unit tests.
-rw-r--r-- | src/exiv2wrapper.cpp | 22 | ||||
-rw-r--r-- | test/exif.py | 5 | ||||
-rw-r--r-- | test/iptc.py | 5 |
3 files changed, 29 insertions, 3 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index 121062e..d217144 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -33,6 +33,7 @@ #define METADATA_NOT_READ 101 #define NON_REPEATABLE 102 #define KEY_NOT_FOUND 103 +#define INVALID_VALUE 104 // Custom macros #define CHECK_METADATA_READ \ @@ -477,7 +478,11 @@ ExifTag::~ExifTag() void ExifTag::setRawValue(const std::string& value) { - _datum->setValue(value); + int result = _datum->setValue(value); + if (result != 0) + { + throw Exiv2::Error(INVALID_VALUE); + } } void ExifTag::setParentImage(Image& image) @@ -616,7 +621,11 @@ void IptcTag::setRawValues(const boost::python::list& values) if (iterator != _data->end()) { // Override an existing value - iterator->setValue(value); + int result = iterator->setValue(value); + if (result != 0) + { + throw Exiv2::Error(INVALID_VALUE); + } // Jump to the next datum matching the key ++iterator; while ((iterator != _data->end()) && (iterator->key() != _key.key())) @@ -628,7 +637,11 @@ void IptcTag::setRawValues(const boost::python::list& values) { // Append a new value Exiv2::Iptcdatum datum(_key); - datum.setValue(value); + int result = datum.setValue(value); + if (result != 0) + { + throw Exiv2::Error(INVALID_VALUE); + } int state = _data->add(datum); if (state == 6) { @@ -1198,6 +1211,9 @@ void translateExiv2Error(Exiv2::Error const& error) case KEY_NOT_FOUND: PyErr_SetString(PyExc_KeyError, "Tag not set"); break; + case INVALID_VALUE: + PyErr_SetString(PyExc_ValueError, "Invalid value"); + break; // Default handler default: diff --git a/test/exif.py b/test/exif.py index bb218c2..4ddb360 100644 --- a/test/exif.py +++ b/test/exif.py @@ -313,3 +313,8 @@ class TestExifTag(unittest.TestCase): tag.value = 2 self.failIfEqual(tag.value, old_value) + def test_set_raw_value_invalid(self): + tag = ExifTag('Exif.GPSInfo.GPSVersionID') + value = '2 0 0 foo' + self.failUnlessRaises(ValueError, tag._set_raw_value, value) + diff --git a/test/iptc.py b/test/iptc.py index 3b151a5..1940efd 100644 --- a/test/iptc.py +++ b/test/iptc.py @@ -190,3 +190,8 @@ class TestIptcTag(unittest.TestCase): tag.values = ['Barcelona'] self.failIfEqual(tag.values, old_values) + def test_set_raw_values_invalid(self): + tag = IptcTag('Iptc.Envelope.DateSent') + values = ['foo'] + self.failUnlessRaises(ValueError, tag._set_raw_values, values) + |