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
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
History
-------

* Provide a more useful error message when ``MODE_MMAP_EXT`` is requested but
the C extension is not available.

1.2.3 (2017-01-11)
++++++++++++++++++

Expand Down
7 changes: 5 additions & 2 deletions maxminddb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ def open_database(database, mode=MODE_AUTO):
* MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
order. Default mode.
"""
if (mode == MODE_AUTO and maxminddb.extension and
hasattr(maxminddb.extension, 'Reader')) or mode == MODE_MMAP_EXT:
has_extension = maxminddb.extension and hasattr(maxminddb.extension, 'Reader')
if (mode == MODE_AUTO and has_extension) or mode == MODE_MMAP_EXT:
if not has_extension:
raise ValueError(
"MODE_MMAP_EXT requires the maxminddb.extension module to be available")
return maxminddb.extension.Reader(database)
elif mode in (MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY):
return maxminddb.reader.Reader(database, mode)
Expand Down
8 changes: 8 additions & 0 deletions tests/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def test_ipv6_address_in_ipv4_database(self):
reader.get('2001::')
reader.close()

def test_no_extension_exception(self):
real_extension = maxminddb.extension
maxminddb.extension = None
with self.assertRaisesRegex(ValueError, 'MODE_MMAP_EXT requires the maxminddb.extension module to be available'):
open_database(
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb', MODE_MMAP_EXT)
maxminddb.extension = real_extension

def test_broken_database(self):
reader = open_database('tests/data/test-data/'
'GeoIP2-City-Test-Broken-Double-Format.mmdb',
Expand Down