Skip to content

Commit

Permalink
Proper handling of ipaddress and unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Dec 31, 2017
1 parent 7b6a8d1 commit c3b94d8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions elasticsearch_dsl/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

from datetime import date, datetime

import six
from dateutil import parser, tz
from six import itervalues, string_types, iteritems
from six.moves import map

from .utils import DslBase, ObjectBase, AttrDict, AttrList
from .exceptions import ValidationException

unicode = type(u'')

def construct_field(name_or_field, **params):
# {"type": "text", "analyzer": "snowball"}
if isinstance(name_or_field, collections.Mapping):
Expand Down Expand Up @@ -310,7 +311,7 @@ class Ip(Field):

def _deserialize(self, data):
# the ipaddress library for pypy, python2.5 and 2.6 only accepts unicode.
return ipaddress.ip_address(six.u(data))
return ipaddress.ip_address(unicode(data))

def _serialize(self, data):
if data is None:
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
install_requires = [
'six',
'python-dateutil',
'ipaddress',
'elasticsearch>=6.0.0,<7.0.0'
]

# ipaddress is included in stdlib sincxe py 3.3
if sys.version_info[:2] < (3, 3):
install_requires.append('ipaddress')

tests_require = [
"mock",
"pytest>=3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch_dsl/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_scaled_float():
def test_ipaddress():
f = field.Ip()
assert f.deserialize('127.0.0.1') == ipaddress.ip_address(u'127.0.0.1')
assert f.deserialize('::1') == ipaddress.ip_address(u'::1')
assert f.deserialize(u'::1') == ipaddress.ip_address(u'::1')
assert f.serialize(f.deserialize('::1')) == '::1'
assert f.deserialize(None) is None
with pytest.raises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch_dsl/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_serialization(write_client):
assert sd.b == [True, False, True, False, None]
assert sd.d == [0.1, -0.1, None]
assert sd.bin == [b'Hello World', None]
assert sd.ip == [ip_address('::1'), ip_address('127.0.0.1'), None]
assert sd.ip == [ip_address(u'::1'), ip_address(u'127.0.0.1'), None]

assert sd.to_dict() == {
'b': [True, False, True, False, None],
Expand Down

0 comments on commit c3b94d8

Please sign in to comment.