Skip to content

Commit c6578d4

Browse files
authored
Merge pull request #27 from maxmind/rafl/mmap-ext-without-extension
Fail with a more useful error message when MODE_MMAP_EXT is chosen but the extension isn't available
2 parents 2099787 + 2efade7 commit c6578d4

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

HISTORY.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
History
44
-------
55

6+
* Provide a more useful error message when ``MODE_MMAP_EXT`` is requested but
7+
the C extension is not available.
8+
69
1.2.3 (2017-01-11)
710
++++++++++++++++++
811

maxminddb/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ def open_database(database, mode=MODE_AUTO):
2727
* MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
2828
order. Default mode.
2929
"""
30-
if (mode == MODE_AUTO and maxminddb.extension and
31-
hasattr(maxminddb.extension, 'Reader')) or mode == MODE_MMAP_EXT:
30+
has_extension = maxminddb.extension and hasattr(maxminddb.extension, 'Reader')
31+
if (mode == MODE_AUTO and has_extension) or mode == MODE_MMAP_EXT:
32+
if not has_extension:
33+
raise ValueError(
34+
"MODE_MMAP_EXT requires the maxminddb.extension module to be available")
3235
return maxminddb.extension.Reader(database)
3336
elif mode in (MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY):
3437
return maxminddb.reader.Reader(database, mode)

tests/reader_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def test_ipv6_address_in_ipv4_database(self):
9494
reader.get('2001::')
9595
reader.close()
9696

97+
def test_no_extension_exception(self):
98+
real_extension = maxminddb.extension
99+
maxminddb.extension = None
100+
with self.assertRaisesRegex(ValueError, 'MODE_MMAP_EXT requires the maxminddb.extension module to be available'):
101+
open_database(
102+
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb', MODE_MMAP_EXT)
103+
maxminddb.extension = real_extension
104+
97105
def test_broken_database(self):
98106
reader = open_database('tests/data/test-data/'
99107
'GeoIP2-City-Test-Broken-Double-Format.mmdb',

0 commit comments

Comments
 (0)