From cf59fcb049c222f20563301a6cf24ebe0ff1eadf Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Wed, 10 Feb 2010 19:56:53 +0100 Subject: Raise an IOError when trying to read an image of an unknown type. --- src/exiv2wrapper.cpp | 47 +++++++++++++++++++++++++++++++---------------- src/exiv2wrapper.hpp | 2 ++ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index ab52892..8c63701 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -41,36 +41,51 @@ namespace exiv2wrapper { -// Base constructor -Image::Image(const std::string& filename) +void Image::_instantiate_image() { + bool success = true; + int error_code; + // Release the GIL to allow other python threads to run // while opening the file. Py_BEGIN_ALLOW_THREADS - _filename = filename; - _image = Exiv2::ImageFactory::open(filename); - assert(_image.get() != 0); - _dataRead = false; + try + { + _image = Exiv2::ImageFactory::open(_filename); + } + catch (Exiv2::Error& error) + { + success = false; + error_code = error.code(); + } // Re-acquire the GIL Py_END_ALLOW_THREADS + + if (success) + { + assert(_image.get() != 0); + _dataRead = false; + } + else + { + throw Exiv2::Error(error_code, _filename); + } +} + +// Base constructor +Image::Image(const std::string& filename) +{ + _filename = filename; + _instantiate_image(); } // Copy constructor Image::Image(const Image& image) { - // Release the GIL to allow other python threads to run - // while opening the file. - Py_BEGIN_ALLOW_THREADS - _filename = image._filename; - _image = Exiv2::ImageFactory::open(_filename); - assert(_image.get() != 0); - _dataRead = false; - - // Re-acquire the GIL - Py_END_ALLOW_THREADS + _instantiate_image(); } void Image::readMetadata() diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp index f87d032..48b7b3a 100644 --- a/src/exiv2wrapper.hpp +++ b/src/exiv2wrapper.hpp @@ -234,6 +234,8 @@ private: // true if the image's internal metadata has already been read, // false otherwise bool _dataRead; + + void _instantiate_image(); }; -- cgit