Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions geoip2/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def __init__(self, filename, locales=None, mode=MODE_AUTO):
self._db_reader = maxminddb.open_database(filename, mode)
self._locales = locales

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.close()

def country(self, ip_address):
"""Get the Country object for the IP address

Expand Down
6 changes: 6 additions & 0 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def test_isp(self):

reader.close()

def test_context_manager(self):
with geoip2.database.Reader(
'tests/data/test-data/GeoIP2-Country-Test.mmdb') as reader:
record = reader.country('81.2.69.160')
self.assertEqual(record.traits.ip_address, '81.2.69.160')


@unittest.skipUnless(maxminddb.extension, 'No C extension module found. Skipping tests')
class TestExtensionReader(BaseTestReader, unittest.TestCase):
Expand Down