Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit 3204b28

Browse files
committed
Test and fix for UTF-8 issue on Python 3
Disabled charset and set_charset on Python 3 as those no longer make much sense since we return the native unicode string.
1 parent dfcaf8d commit 3204b28

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

py_GeoIP.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ GeoIP_GeoIP_init(PyObject *self, PyObject *args, PyObject *kwargs)
8585
return -1;
8686
}
8787

88+
#if PY_MAJOR_VERSION >= 3
89+
GeoIP_set_charset(GeoIP->gi, GEOIP_CHARSET_UTF8);
90+
#endif
91+
8892
return 0;
8993
}
9094

@@ -497,11 +501,13 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args)
497501
return retval;
498502
}
499503

504+
#if PY_MAJOR_VERSION <= 2
500505
static PyObject *GeoIP_charset_Py(PyObject *self, PyObject *UNUSED(args))
501506
{
502507
GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self;
503508
return Py_BuildValue("i", GeoIP_charset(GeoIP->gi));
504509
}
510+
#endif
505511

506512
static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args)
507513
{
@@ -603,8 +609,10 @@ static PyMethodDef GeoIP_GeoIP_methods[] = {
603609
"Lookup City Region By Name" },
604610
{ "range_by_ip", GeoIP_range_by_ip_Py, METH_VARARGS,
605611
"Lookup start and end IP's for a given IP" },
612+
#if PY_MAJOR_VERSION <= 2
606613
{ "charset", GeoIP_charset_Py, METH_NOARGS,
607614
"Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )" },
615+
#endif
608616
{ "set_charset", GeoIP_set_charset_Py, METH_VARARGS,
609617
"Set the charset for city records" },
610618
{ "last_netmask", GeoIP_last_netmask_Py, METH_NOARGS,

tests/test_city.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,26 @@ def test_city():
2424
}
2525

2626
assert_equal(gir, record)
27+
28+
29+
def test_non_ascii_city():
30+
gi = GeoIP.open("tests/data/GeoIPCity.dat", GeoIP.GEOIP_STANDARD)
31+
32+
gir = gi.record_by_addr("89.92.212.80")
33+
34+
record = {
35+
'city': 'F\xe2ches-thumesnil',
36+
'region_name': 'Nord-Pas-de-Calais',
37+
'region': 'B4', 'area_code': 0,
38+
'time_zone': 'Europe/Paris',
39+
'longitude': 3.0808000564575195,
40+
'metro_code': 0,
41+
'country_code3': 'FRA',
42+
'latitude': 50.5906982421875,
43+
'postal_code': None,
44+
'dma_code': 0,
45+
'country_code': 'FR',
46+
'country_name': 'France'
47+
}
48+
49+
assert_equal(gir, record)

tests/test_errors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import GeoIP
24
from nose.tools import assert_equal
35

@@ -35,9 +37,10 @@ def test_errors():
3537

3638
assert_equal(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255'))
3739

38-
assert_equal(gi.charset(), 0)
40+
if sys.version_info[0] == 2:
41+
assert_equal(gi.charset(), 0)
3942

40-
assert_equal(gi.set_charset(-1), 0)
43+
assert_equal(gi.set_charset(-1), 0)
4144

4245
assert_equal(gi.last_netmask(), 5)
4346

0 commit comments

Comments
 (0)