Made GeoIP2.__del__() close all databases.

This commit is contained in:
Nick Pope 2023-12-01 12:55:45 +00:00 committed by GitHub
parent 79099a7ba4
commit 0f83133a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -132,8 +132,10 @@ class GeoIP2:
def __del__(self):
# Cleanup any GeoIP file handles lying around.
if self._reader:
self._reader.close()
if self._city:
self._city.close()
if self._country:
self._country.close()
def __repr__(self):
meta = self._reader.metadata()

View File

@ -152,6 +152,16 @@ class GeoIPTest(SimpleTestCase):
self.assertEqual("Lawrence", d["city"])
self.assertEqual("KS", d["region"])
def test_del(self):
g = GeoIP2()
city = g._city
country = g._country
self.assertIs(city._db_reader.closed, False)
self.assertIs(country._db_reader.closed, False)
del g
self.assertIs(city._db_reader.closed, True)
self.assertIs(country._db_reader.closed, True)
def test_repr(self):
path = settings.GEOIP_PATH
g = GeoIP2(path=path)