Skip to content

Commit 31a7603

Browse files
committed
No longer allow ipaddress object in pure Python
This makes the pure Python implementation consistent with the C extension. The fact that these worked at all was an implementation detail that changed over time.
1 parent c4989eb commit 31a7603

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

HISTORY.rst

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

6+
1.4.0
7+
++++++++++++++++++
8+
9+
* IMPORTANT: Previously, the pure Python reader would allow
10+
`ipaddress.IPv4Address` and `ipaddress.IPv6Address` objects when calling
11+
`.get()`. This would fail with the C extension. The fact that these objects
12+
worked at all was an implementation details and has varied with different
13+
releases. This release makes the pure Python implementation consistent
14+
with the extension. A `TypeError` will now be thrown if you attempt to
15+
use these types with either the pure Python implementation or the
16+
extension. The IP address passed to `.get()` should be a string type.
617
* Fix issue where incorrect size was used when unpacking some types with the
718
pure Python reader. Reported by Lee Symes. GitHub #30.
819

maxminddb/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def int_from_bytes(b):
2121
return 0
2222

2323
byte_from_int = chr
24+
25+
string_type = basestring
26+
27+
string_type_name = 'string'
2428
else:
2529

2630
def compat_ip_address(address):
@@ -33,3 +37,7 @@ def compat_ip_address(address):
3337
int_from_bytes = lambda x: int.from_bytes(x, 'big')
3438

3539
byte_from_int = lambda x: bytes([x])
40+
41+
string_type = str
42+
43+
string_type_name = string_type.__name__

maxminddb/reader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import struct
1717

18-
from maxminddb.compat import byte_from_int, compat_ip_address
18+
from maxminddb.compat import (byte_from_int, compat_ip_address, string_type,
19+
string_type_name)
1920
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY
2021
from maxminddb.decoder import Decoder
2122
from maxminddb.errors import InvalidDatabaseError
@@ -93,6 +94,9 @@ def get(self, ip_address):
9394
Arguments:
9495
ip_address -- an IP address in the standard string notation
9596
"""
97+
if not isinstance(ip_address, string_type):
98+
raise TypeError('argument 1 must be %s, not %s' %
99+
(string_type_name, type(ip_address).__name__))
96100

97101
address = compat_ip_address(ip_address)
98102

tests/reader_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import maxminddb
1414

15+
from maxminddb.compat import compat_ip_address
16+
1517
try:
1618
import maxminddb.extension
1719
except ImportError:
@@ -105,6 +107,14 @@ def test_no_extension_exception(self):
105107
MODE_MMAP_EXT)
106108
maxminddb.extension = real_extension
107109

110+
def test_ip_object_lookup(self):
111+
reader = open_database('tests/data/test-data/GeoIP2-City-Test.mmdb',
112+
self.mode)
113+
with self.assertRaisesRegex(
114+
TypeError, "must be str(?:ing)?, not IPv6Address"):
115+
reader.get(compat_ip_address('2001:220::'))
116+
reader.close()
117+
108118
def test_broken_database(self):
109119
reader = open_database('tests/data/test-data/'
110120
'GeoIP2-City-Test-Broken-Double-Format.mmdb',

0 commit comments

Comments
 (0)