Opened 7 years ago

Last modified 7 years ago

#28981 closed Cleanup/optimization

django.contrib.gis.geoip2.GeoIP2 issue when GEOIP_PATH exists but there is no MaxMind database — at Version 1

Reported by: Hugo Rodger-Brown Owned by: nobody
Component: GIS Version: 1.11
Severity: Normal Keywords: geoip2 maxmind GEOIP_PATH
Cc: Alex Stovbur Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Hugo Rodger-Brown)

When initialising a GeoIP2 object, if the GEOIP_PATH setting points to a directory that exists, but there is no MaxMind database in that location, the object is created in a state such that any call to obj.__repr__ or obj.info() will raise an AttributeError.

Expected behaviour:

If it is possible to create a new GeoIP2 object in the absence of a source database, then the object methods should either handle that situation gracefully, or raise an appropriate error (e.g. GeoIP2Exception).

Actual behaviour:

If you create object and then call either __repr__ (which can be done implicitly by calling print(obj)) or info methods, an AttributeError is raised.

>>> from django.contrib.gis.geoip2 import GeoIP2
>>> g = GeoIP2()  # the GEOIP_PATH exists, but there is no database
>>> print(g)
# traceback truncated
---> meta = self._reader.metadata()
AttributeError: 'NoneType' object has no attribute 'metadata'
>>> g.info()
# traceback truncated
---> meta = self._reader.metadata()
AttributeError: 'NoneType' object has no attribute 'metadata'

The problem is caused by both methods assuming that the _reader attribute will always be a valid geoip2.database.Reader object, which is not true in this case:

>>> from django.contrib.gis.geoip2 import GeoIP2
>>> g = GeoIP2()  # the GEOIP_PATH exists, but there is no database
>>> assert g._city is None
>>> assert g._country is None
>>> assert g._reader is None

Change History (1)

comment:1 by Hugo Rodger-Brown, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top