Skip to content

Commit c3b94d8

Browse files
committed
Proper handling of ipaddress and unicode
1 parent 7b6a8d1 commit c3b94d8

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

elasticsearch_dsl/field.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
from datetime import date, datetime
77

8-
import six
98
from dateutil import parser, tz
109
from six import itervalues, string_types, iteritems
1110
from six.moves import map
1211

1312
from .utils import DslBase, ObjectBase, AttrDict, AttrList
1413
from .exceptions import ValidationException
1514

15+
unicode = type(u'')
16+
1617
def construct_field(name_or_field, **params):
1718
# {"type": "text", "analyzer": "snowball"}
1819
if isinstance(name_or_field, collections.Mapping):
@@ -310,7 +311,7 @@ class Ip(Field):
310311

311312
def _deserialize(self, data):
312313
# the ipaddress library for pypy, python2.5 and 2.6 only accepts unicode.
313-
return ipaddress.ip_address(six.u(data))
314+
return ipaddress.ip_address(unicode(data))
314315

315316
def _serialize(self, data):
316317
if data is None:

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
install_requires = [
1515
'six',
1616
'python-dateutil',
17-
'ipaddress',
1817
'elasticsearch>=6.0.0,<7.0.0'
1918
]
19+
20+
# ipaddress is included in stdlib sincxe py 3.3
21+
if sys.version_info[:2] < (3, 3):
22+
install_requires.append('ipaddress')
23+
2024
tests_require = [
2125
"mock",
2226
"pytest>=3.0.0",

test_elasticsearch_dsl/test_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_scaled_float():
106106
def test_ipaddress():
107107
f = field.Ip()
108108
assert f.deserialize('127.0.0.1') == ipaddress.ip_address(u'127.0.0.1')
109-
assert f.deserialize('::1') == ipaddress.ip_address(u'::1')
109+
assert f.deserialize(u'::1') == ipaddress.ip_address(u'::1')
110110
assert f.serialize(f.deserialize('::1')) == '::1'
111111
assert f.deserialize(None) is None
112112
with pytest.raises(ValueError):

test_elasticsearch_dsl/test_integration/test_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_serialization(write_client):
8787
assert sd.b == [True, False, True, False, None]
8888
assert sd.d == [0.1, -0.1, None]
8989
assert sd.bin == [b'Hello World', None]
90-
assert sd.ip == [ip_address('::1'), ip_address('127.0.0.1'), None]
90+
assert sd.ip == [ip_address(u'::1'), ip_address(u'127.0.0.1'), None]
9191

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

0 commit comments

Comments
 (0)