Skip to content

Commit 7b28aa6

Browse files
committed
Handle missing extension correctly
If the extension module cannot be imported, the fallback path would trigger ‘NameError: name 'maxminddb' is not defined’. While at it: - Use an underscored name for the optional extension import to hide it a bit from view (e.g. autocomplete in interactive Python shells) - Use relative imports not unnecessarily repeat the module name everywhere
1 parent e8a521b commit 7b28aa6

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Diff for: maxminddb/__init__.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
import os
33
from typing import IO, AnyStr, Union, cast
44

5-
try:
6-
import maxminddb.extension
7-
except ImportError:
8-
maxminddb.extension = None # type: ignore
9-
10-
from maxminddb.const import (
5+
from .const import (
116
MODE_AUTO,
127
MODE_FD,
138
MODE_FILE,
149
MODE_MEMORY,
1510
MODE_MMAP,
1611
MODE_MMAP_EXT,
1712
)
18-
from maxminddb.decoder import InvalidDatabaseError
19-
from maxminddb.reader import Reader
13+
from .decoder import InvalidDatabaseError
14+
from .reader import Reader
15+
16+
try:
17+
# pylint: disable=import-self
18+
from . import extension as _extension
19+
except ImportError:
20+
_extension = None # type: ignore[assignment]
21+
2022

2123
__all__ = [
2224
"InvalidDatabaseError",
@@ -60,7 +62,7 @@ def open_database(
6062
):
6163
raise ValueError(f"Unsupported open mode: {mode}")
6264

63-
has_extension = maxminddb.extension and hasattr(maxminddb.extension, "Reader")
65+
has_extension = _extension and hasattr(_extension, "Reader")
6466
use_extension = has_extension if mode == MODE_AUTO else mode == MODE_MMAP_EXT
6567

6668
if not use_extension:
@@ -75,7 +77,7 @@ def open_database(
7577
# checking purposes, pretend it is one. (Ideally this would be a subclass
7678
# of, or share a common parent class with, the Python Reader
7779
# implementation.)
78-
return cast(Reader, maxminddb.extension.Reader(database, mode))
80+
return cast(Reader, _extension.Reader(database, mode))
7981

8082

8183
__title__ = "maxminddb"

Diff for: tests/reader_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ def test_opening_path(self):
250250
self.assertEqual(reader.metadata().database_type, "MaxMind DB Decoder Test")
251251

252252
def test_no_extension_exception(self):
253-
real_extension = maxminddb.extension
254-
maxminddb.extension = None
253+
real_extension = maxminddb._extension
254+
maxminddb._extension = None
255255
with self.assertRaisesRegex(
256256
ValueError,
257257
"MODE_MMAP_EXT requires the maxminddb.extension module to be available",
258258
):
259259
open_database(
260260
"tests/data/test-data/MaxMind-DB-test-decoder.mmdb", MODE_MMAP_EXT
261261
)
262-
maxminddb.extension = real_extension
262+
maxminddb._extension = real_extension
263263

264264
def test_broken_database(self):
265265
reader = open_database(

0 commit comments

Comments
 (0)