Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer allow ipaddress objects in pure Python implementation #32

Merged
merged 7 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
History
-------

1.4.0
++++++++++++++++++

* IMPORTANT: Previously, the pure Python reader would allow
`ipaddress.IPv4Address` and `ipaddress.IPv6Address` objects when calling
`.get()`. This would fail with the C extension. The fact that these objects
worked at all was an implementation details and has varied with different
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/details/detail/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed in e9e1cc2.

releases. This release makes the pure Python implementation consistent
with the extension. A `TypeError` will now be thrown if you attempt to
use these types with either the pure Python implementation or the
extension. The IP address passed to `.get()` should be a string type.
* Fix issue where incorrect size was used when unpacking some types with the
pure Python reader. Reported by Lee Symes. GitHub #30.

Expand Down
8 changes: 8 additions & 0 deletions maxminddb/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def int_from_bytes(b):
return 0

byte_from_int = chr

string_type = basestring

string_type_name = 'string'
else:

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

byte_from_int = lambda x: bytes([x])

string_type = str

string_type_name = string_type.__name__
6 changes: 5 additions & 1 deletion maxminddb/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import struct

from maxminddb.compat import byte_from_int, compat_ip_address
from maxminddb.compat import (byte_from_int, compat_ip_address, string_type,
string_type_name)
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY
from maxminddb.decoder import Decoder
from maxminddb.errors import InvalidDatabaseError
Expand Down Expand Up @@ -93,6 +94,9 @@ def get(self, ip_address):
Arguments:
ip_address -- an IP address in the standard string notation
"""
if not isinstance(ip_address, string_type):
raise TypeError('argument 1 must be %s, not %s' %
(string_type_name, type(ip_address).__name__))

address = compat_ip_address(ip_address)

Expand Down
10 changes: 10 additions & 0 deletions tests/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import maxminddb

from maxminddb.compat import compat_ip_address

try:
import maxminddb.extension
except ImportError:
Expand Down Expand Up @@ -105,6 +107,14 @@ def test_no_extension_exception(self):
MODE_MMAP_EXT)
maxminddb.extension = real_extension

def test_ip_object_lookup(self):
reader = open_database('tests/data/test-data/GeoIP2-City-Test.mmdb',
self.mode)
with self.assertRaisesRegex(
TypeError, "must be str(?:ing)?, not IPv6Address"):
reader.get(compat_ip_address('2001:220::'))
reader.close()

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