Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in function Ratio.decimal (probably very easy to fix ZeroDivisionError) #192

Open
AnthonyZi opened this issue Jan 17, 2024 · 0 comments

Comments

@AnthonyZi
Copy link

First of all thank you very much for this great Project helping me to extract meta data from photos!

Using exifread for reading the GPS data from images and I ran into an unexpected exception calling the "decimal" function of a Rational object and adding rationals with the addition ("+") operator.

The following three lines in an interactive python shell can be used to reproduce the error:

>>> import exifread
>>> exifread.utils.Ratio(0, 0) + 1  # -> ZeroDivisionError
>>> exifread.utils.Ratio(0, 0).decimal()  # -> ZeroDivisionError

How severe is this issue? The photos taken with my ordinary smartphone have some exif data for GPS coordinates stored that I read with exifread yielding exactly the "0/0" Rational issue:

>>> gps_longitude, gps_latitude, gps_altitude = get_exif_gps_coordinates()
>>> print(f"{gps_longitude}, {type(gps_longitude)}")
>>> 0/0 <class 'exifread.utils.Ratio'>
>>> print(f"{gps_latitude}, {type(gps_latitude)}")
>>> 0/0 <class 'exifread.utils.Ratio'>
>>> print(f"{gps_altitude}, {type(gps_altitude)}")
>>> 0/0 <class 'exifread.utils.Ratio'>

Bug fix brainstorming ideas ( Idid not really think through any of this ideas in depth):

  • Initialize denominator with '1' if the numerator is '0'
    • The Ratio object is intended to be immutable either way, hence this modification should not harm e.g., when the user would change the numerator, but still this must be considered with more care maybe!
  • Throw an exception when the denominator is initialized to '0' (-> remove except block in the new function)
    • I don't know the reasoning for the except block, but maybe it is obsolete or exception can be catched elsewhere?
  • Introduce a variable which can accessed to query the "state" of the Ratio object, and a specific exception when using an "invalid" Ratio object, e.g., (dummy code):
class Ratio:
    def __new__(self):
        ...
        self.state = "invalid" if self.den == 0 else "valid"

    def decimal(self) -> float:
        if self.den == 0:
            raise RatioInvalidException(f"The Ratio {self} is invalid and cannot be converted to a decimal")
            # mathematically something like if self.den == 0: return float("inf") maybe also makes sense (sign must be handled too)
        ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant