diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2011-08-17 18:56:52 +0200 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2011-08-17 18:56:52 +0200 |
commit | 8f076d1fe72e3ad167bbbd29b076b88d7143ef3a (patch) | |
tree | 6b00931a4d081bf8f23fd775c6082cc23dd43e5a | |
parent | f58166679a8334bd9a75647a6bc4a0c227f7f254 (diff) | |
download | pyexiv2-8f076d1fe72e3ad167bbbd29b076b88d7143ef3a.tar.gz |
Check the type before setting a tag’s value and raise a TypeError if it doesn’t match.
-rw-r--r-- | src/pyexiv2/xmp.py | 4 | ||||
-rw-r--r-- | test/xmp.py | 12 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/pyexiv2/xmp.py b/src/pyexiv2/xmp.py index bed1b66..2b3149e 100644 --- a/src/pyexiv2/xmp.py +++ b/src/pyexiv2/xmp.py @@ -218,11 +218,15 @@ class XmpTag(object): stype = stype[17:] self.raw_value = self._convert_to_string(value, stype) elif type in ('XmpAlt', 'XmpBag', 'XmpSeq'): + if not isinstance(value, (list, tuple)): + raise TypeError('Expecting a list of values') stype = self.type[4:] if stype.lower().startswith('closed choice of'): stype = stype[17:] self.raw_value = map(lambda x: self._convert_to_string(x, stype), value) elif type == 'LangAlt': + if not isinstance(value, dict): + raise TypeError('Expecting a dictionary mapping language codes to values') raw_value = {} for k, v in value.iteritems(): try: diff --git a/test/xmp.py b/test/xmp.py index 7e7522e..c0ea11f 100644 --- a/test/xmp.py +++ b/test/xmp.py @@ -2,7 +2,7 @@ # ****************************************************************************** # -# Copyright (C) 2009-2010 Olivier Tilloy <olivier@tilloy.net> +# Copyright (C) 2009-2011 Olivier Tilloy <olivier@tilloy.net> # # This file is part of the pyexiv2 distribution. # @@ -364,6 +364,16 @@ class TestXmpTag(unittest.TestCase): self.failUnlessEqual(tag.type, 'Lang Alt') self.failUnlessRaises(ValueError, tag._set_value, {}) + def test_set_value_incorrect_type(self): + # Expecting a list of values + tag = XmpTag('Xmp.dc.publisher') + self.failUnlessRaises(TypeError, tag._set_value, None) + self.failUnlessRaises(TypeError, tag._set_value, 'bleh') + # Expecting a dictionary mapping language codes to values + tag = XmpTag('Xmp.dc.description') + self.failUnlessRaises(TypeError, tag._set_value, None) + self.failUnlessRaises(TypeError, tag._set_value, 'bleh') + class TestXmpNamespaces(unittest.TestCase): |