Skip to content

Commit cc486df

Browse files
committed
Update Pure python reader to support ipaddress objects
1 parent 6142e41 commit cc486df

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

maxminddb/compat.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def int_from_bytes(b):
2424

2525
string_type = basestring
2626

27-
string_type_name = 'string'
2827
else:
2928

3029
def compat_ip_address(address):
@@ -39,5 +38,3 @@ def compat_ip_address(address):
3938
byte_from_int = lambda x: bytes([x])
4039

4140
string_type = str
42-
43-
string_type_name = string_type.__name__

maxminddb/reader.py

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

1616
import struct
1717

18-
from maxminddb.compat import (byte_from_int, compat_ip_address, string_type,
19-
string_type_name)
18+
from maxminddb.compat import byte_from_int, compat_ip_address, string_type
2019
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD
2120
from maxminddb.decoder import Decoder
2221
from maxminddb.errors import InvalidDatabaseError
@@ -105,12 +104,15 @@ def get(self, ip_address):
105104
Arguments:
106105
ip_address -- an IP address in the standard string notation
107106
"""
108-
if not isinstance(ip_address, string_type):
107+
if isinstance(ip_address,
108+
(ipaddress.IPv4Address, ipaddress.IPv6Address)):
109+
address = ip_address
110+
elif isinstance(ip_address, string_type):
111+
address = compat_ip_address(ip_address)
112+
else:
109113
raise TypeError('argument 1 must be %s, not %s' %
110114
(string_type_name, type(ip_address).__name__))
111115

112-
address = compat_ip_address(ip_address)
113-
114116
if address.version == 6 and self._metadata.ip_version == 4:
115117
raise ValueError(
116118
'Error looking up {0}. You attempted to look up '

0 commit comments

Comments
 (0)