Exif headers with zero-denominator rational (fraction) types raise DivideByZero exception unnecessarily

Bug #786253 reported by Hobson Lane
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pyexiv2
Fix Released
Medium
Olivier Tilloy

Bug Description

Applies to pyexiv2 version 0.3
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
distributed with Ubuntu Natty Narwhal

#!/bin/python
# show_bug2.py
import pyexiv2
im = pyexiv2.ImageMetadata('bora_bora--stone_tiki.JPG')
im.read()
k = 'Exif.Nikon3.FocusDistance'
s = im[k].value;
print "{0}: {1}".format(k,s)

hobs@hobs-laptop:~/Photos/malformed_exif_examples$ ipython show_bug2.py
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (33, 0))

---------------------------------------------------------------------------
ExifValueError Traceback (most recent call last)

/media/Win7/Users/Hobs/Documents/Photos/malformed_exif_examples/show_bug2.py in <module>()
      4 im.read()
      5 k = 'Exif.Nikon3.FocusDistance'
----> 6 s = im[k].value;
      7 print "{0}: {1}".format(k,s)
      8

/usr/lib/python2.7/dist-packages/pyexiv2/exif.pyc in _get_value(self)
    187 def _get_value(self):
    188 if self._value_cookie:
--> 189 self._compute_value()
    190 return self._value
    191

/usr/lib/python2.7/dist-packages/pyexiv2/exif.pyc in _compute_value(self)
    182 return
    183
--> 184 self._value = self._convert_to_python(self._raw_value)
    185 self._value_cookie = False
    186

/usr/lib/python2.7/dist-packages/pyexiv2/exif.pyc in _convert_to_python(self, value)
    320 r = make_fraction(value)
    321 except (ValueError, ZeroDivisionError):
--> 322 raise ExifValueError(value, self.type)
    323 else:
    324 if self.type == 'Rational' and r.numerator < 0:

ExifValueError: Invalid value for EXIF type [Rational]: [0/0]
WARNING: Failure executing file: <show_bug2.py>

Revision history for this message
Hobson Lane (hobs) wrote :
Changed in pyexiv2:
assignee: nobody → Hobson Lane (hobs)
Revision history for this message
Hobson Lane (hobs) wrote :

Here's a work-around patch for this bug and a related bug, #781464.

Revision history for this message
Olivier Tilloy (osomon) wrote :

pyexiv2 is voluntarily strict with value parsing, so as a rule of thumb accessing the value should always be enclosed in a try…catch statement if the metadata is not known to be always well-formed. You can still access the raw value (unparsed) without errors ("0/0" in this case).

However in this specific case it seems that it’s not uncommon to get invalid fractions of "0/0", so pyexiv2 could indeed be made more fault-tolerant.

Your patch is correct but too invasive though, it contains unrelated changes. I’ll keep the relevant changes and update the unit tests accordingly, thanks a lot!

Changed in pyexiv2:
importance: Undecided → Medium
status: New → Confirmed
Olivier Tilloy (osomon)
Changed in pyexiv2:
assignee: Hobson Lane (hobs) → Olivier Tilloy (osomon)
status: Confirmed → Fix Committed
Olivier Tilloy (osomon)
Changed in pyexiv2:
milestone: none → 0.3.1
Olivier Tilloy (osomon)
Changed in pyexiv2:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.