diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-12-08 21:24:07 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-12-08 21:24:07 +0100 |
commit | 6734ebcb98ce248ce9c975b3a06a6b65935f6c54 (patch) | |
tree | 221f4e77baec6aa1678dc184115e1612b3b9950a | |
parent | 7cb0035390da451bde8ebe534ad28dceeaa1c967 (diff) | |
parent | 10e8d9e401d1fb4f262c6c17c2a9f2d9942bee96 (diff) | |
download | pyexiv2-6734ebcb98ce248ce9c975b3a06a6b65935f6c54.tar.gz |
Raise an IOError with a relevant error message when trying to access the metadata of an image that has not been read() yet.
Previously, a misleading AttributeError would be raised (this was a regression from the 0.1.x series).
-rw-r--r-- | src/pyexiv2/metadata.py | 16 | ||||
-rw-r--r-- | test/metadata.py | 35 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py index a121286..8adeab6 100644 --- a/src/pyexiv2/metadata.py +++ b/src/pyexiv2/metadata.py @@ -61,7 +61,7 @@ class ImageMetadata(MutableMapping): self.filename = filename if filename is not None: self.filename = filename.encode(sys.getfilesystemencoding()) - self._image = None + self.__image = None self._keys = {'exif': None, 'iptc': None, 'xmp': None} self._tags = {'exif': {}, 'iptc': {}, 'xmp': {}} self._exif_thumbnail = None @@ -86,9 +86,15 @@ class ImageMetadata(MutableMapping): :type buffer: string """ obj = cls(None) - obj._image = libexiv2python._Image(buffer, len(buffer)) + obj.__image = libexiv2python._Image(buffer, len(buffer)) return obj + @property + def _image(self): + if self.__image is None: + raise IOError('Image metadata has not been read yet') + return self.__image + def read(self): """ Read the metadata embedded in the associated image. @@ -96,9 +102,9 @@ class ImageMetadata(MutableMapping): the metadata (an exception will be raised if trying to access metadata before calling this method). """ - if self._image is None: - self._image = self._instantiate_image(self.filename) - self._image._readMetadata() + if self.__image is None: + self.__image = self._instantiate_image(self.filename) + self.__image._readMetadata() def write(self, preserve_timestamps=False): """ diff --git a/test/metadata.py b/test/metadata.py index a4994d4..96a7510 100644 --- a/test/metadata.py +++ b/test/metadata.py @@ -83,8 +83,41 @@ class TestImageMetadata(unittest.TestCase): # Test general methods ###################### + def test_not_read_raises(self): + # http://bugs.launchpad.net/pyexiv2/+bug/687373 + self.assertRaises(IOError, self.metadata.write) + self.assertRaises(IOError, self.metadata.__getattribute__, 'dimensions') + self.assertRaises(IOError, self.metadata.__getattribute__, 'mime_type') + self.assertRaises(IOError, self.metadata.__getattribute__, 'exif_keys') + self.assertRaises(IOError, self.metadata.__getattribute__, 'iptc_keys') + self.assertRaises(IOError, self.metadata.__getattribute__, 'xmp_keys') + self.assertRaises(IOError, self.metadata._get_exif_tag, 'Exif.Image.Make') + self.assertRaises(IOError, self.metadata._get_iptc_tag, 'Iptc.Application2.Caption') + self.assertRaises(IOError, self.metadata._get_xmp_tag, 'Xmp.dc.format') + self.assertRaises(IOError, self.metadata._set_exif_tag, 'Exif.Image.Make', 'foobar') + self.assertRaises(IOError, self.metadata._set_iptc_tag, 'Iptc.Application2.Caption', ['foobar']) + self.assertRaises(IOError, self.metadata._set_xmp_tag, 'Xmp.dc.format', ('foo', 'bar')) + self.assertRaises(IOError, self.metadata._delete_exif_tag, 'Exif.Image.Make') + self.assertRaises(IOError, self.metadata._delete_iptc_tag, 'Iptc.Application2.Caption') + self.assertRaises(IOError, self.metadata._delete_xmp_tag, 'Xmp.dc.format') + self.assertRaises(IOError, self.metadata._get_comment) + self.assertRaises(IOError, self.metadata._set_comment, 'foobar') + self.assertRaises(IOError, self.metadata._del_comment) + self.assertRaises(IOError, self.metadata.__getattribute__, 'previews') + other = ImageMetadata(self.pathname) + self.assertRaises(IOError, self.metadata.copy, other) + self.assertRaises(IOError, self.metadata.__getattribute__, 'buffer') + thumb = self.metadata.exif_thumbnail + self.assertRaises(IOError, thumb.__getattribute__, 'mime_type') + self.assertRaises(IOError, thumb.__getattribute__, 'extension') + self.assertRaises(IOError, thumb.write_to_file, '/tmp/foobar.jpg') + self.assertRaises(IOError, thumb.erase) + self.assertRaises(IOError, thumb.set_from_file, '/tmp/foobar.jpg') + self.assertRaises(IOError, thumb.__getattribute__, 'data') + self.assertRaises(IOError, thumb.__setattr__, 'data', EMPTY_JPG_DATA) + def test_read(self): - self.assertEqual(self.metadata._image, None) + self.assertRaises(IOError, self.metadata.__getattribute__, '_image') self.metadata.read() self.failIfEqual(self.metadata._image, None) |