From e5895306bfd56504e4a7be2c7163ac54d73e1acd Mon Sep 17 00:00:00 2001 From: Sergey Farbotka Date: Fri, 22 Aug 2014 00:05:43 +0300 Subject: [PATCH 1/3] Encoding fixes for python 3.4 --- py_GeoIP.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index a7fd12b..05817aa 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -757,6 +757,21 @@ static PyMethodDef GeoIP_module_methods[] = { { NULL, NULL, 0,NULL } }; + +#if PY_MAJOR_VERSION >= 3 + +PyObject* GeoIP_Latin1ToPyString(const char *u) +{ + return PyUnicode_DecodeLatin1(u, strlen(u), NULL); +} + +#else + +// Do not change behavior for compatibility with previous python-geoip versions +#define GeoIP_Latin1ToPyString PyString_FromString + +#endif + /* Code shared between 2.x and 3.x module initialization. */ static int GeoIP_populate_module(PyObject *m) @@ -786,14 +801,15 @@ GeoIP_populate_module(PyObject *m) CHECK_NULL(ccont = PyDict_New()); for (i = 0; i < total_ccodes; i++) { - CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_code[i])); + // All GeoIP_country_{code,name,continent} are in LATIN1 encoding + CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_code[i])); PyTuple_SET_ITEM(ccode, i, tmp); - CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_name[i])); + CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_name[i])); CHECK(PyDict_SetItemString(cname, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); - CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_continent[i])); + CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_continent[i])); CHECK(PyDict_SetItemString(ccont, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); } @@ -803,6 +819,7 @@ GeoIP_populate_module(PyObject *m) CHECK(PyModule_AddObject(m, "country_names", cname)); CHECK(PyModule_AddObject(m, "country_continents", ccont)); + CHECK(PyModule_AddIntMacro(m, GEOIP_STANDARD)); CHECK(PyModule_AddIntMacro(m, GEOIP_MEMORY_CACHE)); CHECK(PyModule_AddIntMacro(m, GEOIP_MMAP_CACHE)); From 1651fd07f8a0b33d1a0e9f01390b9d6e8766bece Mon Sep 17 00:00:00 2001 From: Sergey Farbotka Date: Fri, 22 Aug 2014 13:06:40 +0300 Subject: [PATCH 2/3] Revert "Encoding fixes for python 3.4" This reverts commit e5895306bfd56504e4a7be2c7163ac54d73e1acd. --- py_GeoIP.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 05817aa..a7fd12b 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -757,21 +757,6 @@ static PyMethodDef GeoIP_module_methods[] = { { NULL, NULL, 0,NULL } }; - -#if PY_MAJOR_VERSION >= 3 - -PyObject* GeoIP_Latin1ToPyString(const char *u) -{ - return PyUnicode_DecodeLatin1(u, strlen(u), NULL); -} - -#else - -// Do not change behavior for compatibility with previous python-geoip versions -#define GeoIP_Latin1ToPyString PyString_FromString - -#endif - /* Code shared between 2.x and 3.x module initialization. */ static int GeoIP_populate_module(PyObject *m) @@ -801,15 +786,14 @@ GeoIP_populate_module(PyObject *m) CHECK_NULL(ccont = PyDict_New()); for (i = 0; i < total_ccodes; i++) { - // All GeoIP_country_{code,name,continent} are in LATIN1 encoding - CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_code[i])); + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_code[i])); PyTuple_SET_ITEM(ccode, i, tmp); - CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_name[i])); + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_name[i])); CHECK(PyDict_SetItemString(cname, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); - CHECK_NULL(tmp = GeoIP_Latin1ToPyString(GeoIP_country_continent[i])); + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_continent[i])); CHECK(PyDict_SetItemString(ccont, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); } @@ -819,7 +803,6 @@ GeoIP_populate_module(PyObject *m) CHECK(PyModule_AddObject(m, "country_names", cname)); CHECK(PyModule_AddObject(m, "country_continents", ccont)); - CHECK(PyModule_AddIntMacro(m, GEOIP_STANDARD)); CHECK(PyModule_AddIntMacro(m, GEOIP_MEMORY_CACHE)); CHECK(PyModule_AddIntMacro(m, GEOIP_MMAP_CACHE)); From 10b2a4a096f272514dd43b69c183dd217f842feb Mon Sep 17 00:00:00 2001 From: Sergey Farbotka Date: Fri, 22 Aug 2014 13:54:10 +0300 Subject: [PATCH 3/3] Use UTF-8 encoded country names --- py_GeoIP.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index a7fd12b..e6d142b 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -789,7 +789,7 @@ GeoIP_populate_module(PyObject *m) CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_code[i])); PyTuple_SET_ITEM(ccode, i, tmp); - CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_name[i])); + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_utf8_country_name[i])); CHECK(PyDict_SetItemString(cname, GeoIP_country_code[i], tmp)); Py_DECREF(tmp);