From 64341d8c1abffa6f8662e81ba3732d4ec107dc7e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 8 Oct 2012 14:21:52 -0700 Subject: [PATCH 01/66] Updated README --- README | 1 + test_domain.py | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100755 test_domain.py diff --git a/README b/README index 535d51a..8fdf1a1 100644 --- a/README +++ b/README @@ -9,6 +9,7 @@ To Install: Usage: See test.py for example usage with GeoIP Country See test_org.py for example usage with GeoIP ISP and Organization + See test_domain.py for an example of GeoIP Domain usage See test_city.py for example usage with GeoIP City See test_region.py for example usage with GeoIP Region See test_netspeed.py for example usage with GeoIP Netspeed diff --git a/test_domain.py b/test_domain.py new file mode 100755 index 0000000..f5ab6a6 --- /dev/null +++ b/test_domain.py @@ -0,0 +1,7 @@ +#!/usr/bin/python + +import GeoIP + +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPDomain.dat",GeoIP.GEOIP_STANDARD) + +print gi.org_by_addr("24.24.24.24") From 5ee128f86fc2bfc03dbf8ee27c639cfa540e40f4 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 19 Mar 2013 14:51:27 +0100 Subject: [PATCH 02/66] Add netspeedcell example --- ChangeLog | 2 ++ setup.py | 2 +- test_netspeedcell.py | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100755 test_netspeedcell.py diff --git a/ChangeLog b/ChangeLog index 8be943d..b62c577 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ +1.2.7 2011-08-23 + * Add example test_netspeedcell.py ( Boris Zentner ) 1.2.6 2011-06-06 * Add new methods record_by_name_v6 and record_by_addr_v6 to support GeoIPCityv6 databases. ( Boris Zentner ) diff --git a/setup.py b/setup.py index f22a1d6..c34324a 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,6 @@ include_dirs = ['/usr/local/include']) setup (name = 'GeoIP-Python', - version = '1.2.6', + version = '1.2.7', description = 'This is a python wrapper to GeoIP', ext_modules = [module1]) diff --git a/test_netspeedcell.py b/test_netspeedcell.py new file mode 100755 index 0000000..102d589 --- /dev/null +++ b/test_netspeedcell.py @@ -0,0 +1,9 @@ +#!/usr/bin/python + +import GeoIP + +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPNetSpeedCell.dat",GeoIP.GEOIP_STANDARD) + +print gi.org_by_name("yahoo.com") +print gi.org_by_name("www.google.com") +print gi.org_by_addr("24.24.24.24") From 17fbefd82ac6d7c1f8b92c24e1bcc207c2941f29 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 19 Mar 2013 14:45:56 +0100 Subject: [PATCH 03/66] Remove unsued code --- ChangeLog | 1 + py_GeoIP.c | 38 -------------------------------------- test_city_acc.py | 28 ---------------------------- 3 files changed, 1 insertion(+), 66 deletions(-) delete mode 100755 test_city_acc.py diff --git a/ChangeLog b/ChangeLog index b62c577..e4d486d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * Remove unused code. ( Boris Zentner ) 1.2.7 2011-08-23 * Add example test_netspeedcell.py ( Boris Zentner ) 1.2.6 2011-06-06 diff --git a/py_GeoIP.c b/py_GeoIP.c index a12cda0..b0ee363 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -253,30 +253,6 @@ void GeoIP_SetItemInt(PyObject *dict, const char * name, int value) { Py_DECREF(valueObj); } -void GeoIP_SetConfItemInt(PyObject *dict, const char * name, int value) { - PyObject * nameObj; - PyObject * valueObj; - nameObj = Py_BuildValue("s",name); - valueObj = value == GEOIP_UNKNOWN_CONF - ? Py_BuildValue("") - : Py_BuildValue("i",value); - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(nameObj); - Py_DECREF(valueObj); -} - -void GeoIP_SetAccuracyItemInt(PyObject *dict, const char * name, int value) { - PyObject * nameObj; - PyObject * valueObj; - nameObj = Py_BuildValue("s",name); - valueObj = value == GEOIP_UNKNOWN_ACCURACY_RADIUS - ? Py_BuildValue("") - : Py_BuildValue("i",value); - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(nameObj); - Py_DECREF(valueObj); -} - static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { PyObject * retval; const char * region_name = NULL; @@ -319,20 +295,6 @@ static PyObject * GeoIP_populate_dict(GeoIP* gi, GeoIPRecord *gir) { GeoIP_SetItemInt(retval,"area_code",gir->area_code); } -/* - * drop support for Confidence and Accuracy Database - * for now - - if ( gi->databaseType != GEOIP_CITY_EDITION_REV1 ){ - GeoIP_SetConfItemInt(retval, "country_conf", gir->country_conf ); - GeoIP_SetConfItemInt(retval, "region_conf", gir->region_conf ); - GeoIP_SetConfItemInt(retval, "city_conf", gir->city_conf ); - GeoIP_SetConfItemInt(retval, "postal_conf", gir->postal_conf ); - - GeoIP_SetAccuracyItemInt(retval, "accuracy_radius", gir->accuracy_radius ); - -*/ - GeoIPRecord_delete(gir); return retval; } diff --git a/test_city_acc.py b/test_city_acc.py deleted file mode 100755 index 388ad3e..0000000 --- a/test_city_acc.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python - -import GeoIP - -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCityConfidenceDist.dat",GeoIP.GEOIP_STANDARD) - -gir = gi.record_by_name("www.google.com") -#gir = gi.record_by_addr("24.24.24.24") - -if gir != None: - print gir['country_code'] - print gir['country_code3'] - print gir['country_name'] - print gir['city'] - print gir['region'] - print gir['region_name'] - print gir['postal_code'] - print gir['latitude'] - print gir['longitude'] - print gir['area_code'] - print gir['time_zone'] - print gir['metro_code'] - print gir['country_conf'] - print gir['region_conf'] - print gir['city_conf'] - print gir['postal_conf'] - print gir['accuracy_radius'] - From 9660210669615e556d23a3127ff2d30088187085 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 19 Mar 2013 18:28:24 +0100 Subject: [PATCH 04/66] Update MANIFEST --- MANIFEST | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MANIFEST b/MANIFEST index 0079edf..d3b8666 100644 --- a/MANIFEST +++ b/MANIFEST @@ -3,6 +3,11 @@ README py_GeoIP.c setup.py test.py -test_org.py test_city.py +test_city_charset.py +test_domain.py +test_netspeed.py +test_netspeedcell.py +test_org.py test_region.py +test_v6.py From 8aadd331e868a96538b849a8b45fc781c77edb87 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 19 Mar 2013 15:39:25 +0100 Subject: [PATCH 05/66] Fix refcounts and low memconditions --- ChangeLog | 2 ++ py_GeoIP.c | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index e4d486d..73fdfaf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,6 @@ * Remove unused code. ( Boris Zentner ) + * Fix low memory error handling and refcount issues on error + reported by Matt Domsch 1.2.7 2011-08-23 * Add example test_netspeedcell.py ( Boris Zentner ) 1.2.6 2011-06-06 diff --git a/py_GeoIP.c b/py_GeoIP.c index b0ee363..7a8e76d 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -43,10 +43,14 @@ GeoIP_new_Py(PyObject* self, PyObject *args) { GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); + if ( !GeoIP ) + return NULL; + GeoIP->gi = GeoIP_new(flags); if (!GeoIP->gi) { PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); + Py_DECREF(GeoIP); return NULL; } @@ -65,10 +69,14 @@ GeoIP_open_Py(PyObject* self, PyObject *args) { GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); + if ( !GeoIP ) + return NULL; + GeoIP->gi = GeoIP_open(filename, flags); if (!GeoIP->gi) { PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); + Py_DECREF(GeoIP); return NULL; } @@ -226,31 +234,37 @@ static PyObject * GeoIP_id_by_name_Py(PyObject *self, PyObject *args) { void GeoIP_SetItemString(PyObject *dict, const char * name, const char * value) { PyObject * nameObj; PyObject * valueObj; - nameObj = Py_BuildValue("s",name); - valueObj = Py_BuildValue("s",value); - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(nameObj); - Py_DECREF(valueObj); + if (( nameObj = Py_BuildValue("s",name))){ + if ((valueObj = Py_BuildValue("s",value))){ + PyDict_SetItem(dict,nameObj,valueObj); + Py_DECREF(valueObj); + } + Py_DECREF(nameObj); + } } void GeoIP_SetItemFloat(PyObject *dict, const char * name, float value) { PyObject * nameObj; PyObject * valueObj; - nameObj = Py_BuildValue("s",name); - valueObj = Py_BuildValue("f",value); - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(nameObj); - Py_DECREF(valueObj); + if (( nameObj = Py_BuildValue("s",name))){ + if (( valueObj = Py_BuildValue("f",value))){ + PyDict_SetItem(dict,nameObj,valueObj); + Py_DECREF(valueObj); + } + Py_DECREF(nameObj); + } } void GeoIP_SetItemInt(PyObject *dict, const char * name, int value) { PyObject * nameObj; PyObject * valueObj; - nameObj = Py_BuildValue("s",name); - valueObj = Py_BuildValue("i",value); - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(nameObj); - Py_DECREF(valueObj); + if ((nameObj = Py_BuildValue("s",name))){ + if ((valueObj = Py_BuildValue("i",value))){ + PyDict_SetItem(dict,nameObj,valueObj); + Py_DECREF(valueObj); + } + Py_DECREF(nameObj); + } } static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { From bc51397988b8ea1f87d2a6dd435b31e452c8ffab Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 19 Mar 2013 18:38:34 +0100 Subject: [PATCH 06/66] Indent py_GeoIP.c --- py_GeoIP.c | 1184 +++++++++++++++++++++++++++------------------------- 1 file changed, 621 insertions(+), 563 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 7a8e76d..6874ae0 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -28,599 +28,657 @@ staticforward PyTypeObject GeoIP_GeoIPType; static PyObject *PyGeoIPError; typedef struct { - PyObject_HEAD; - GeoIP *gi; + PyObject_HEAD; + GeoIP *gi; } GeoIP_GeoIPObject; -static PyObject* -GeoIP_new_Py(PyObject* self, PyObject *args) { - GeoIP_GeoIPObject* GeoIP; - int flags; +static PyObject *GeoIP_new_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP; + int flags; + + if (!PyArg_ParseTuple(args, "i", &flags)) { + return NULL; + } + + GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); + + if (!GeoIP) + return NULL; + + GeoIP->gi = GeoIP_new(flags); + + if (!GeoIP->gi) { + PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); + Py_DECREF(GeoIP); + return NULL; + } + + return (PyObject *) GeoIP; +} + +static PyObject *GeoIP_open_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP; + char *filename; + int flags; + + if (!PyArg_ParseTuple(args, "si", &filename, &flags)) { + return NULL; + } + + GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); + + if (!GeoIP) + return NULL; + + GeoIP->gi = GeoIP_open(filename, flags); + + if (!GeoIP->gi) { + PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); + Py_DECREF(GeoIP); + return NULL; + } + + return (PyObject *) GeoIP; +} + +static void GeoIP_GeoIP_dealloc(PyObject * self) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_delete(GeoIP->gi); + PyObject_Del(self); +} + +static PyObject *GeoIP_country_code_by_name_v6_Py(PyObject * self, + PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_name_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_name_by_name_v6_Py(PyObject * self, + PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_name_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_code_by_addr_v6_Py(PyObject * self, + PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_addr_v6(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_name_by_addr_v6_Py(PyObject * self, + PyObject * args) +{ + char *name; + const char *retval; + PyObject *ret; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_addr_v6(GeoIP->gi, name); + ret = Py_BuildValue("s", retval); + return ret; +} + +static PyObject *GeoIP_country_code_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_name(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_name_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_name(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_code_by_addr_Py(PyObject * self, PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_code_by_addr(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} + +static PyObject *GeoIP_country_name_by_addr_Py(PyObject * self, PyObject * args) +{ + char *name; + const char *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_country_name_by_addr(GeoIP->gi, name); + return Py_BuildValue("s", retval); +} - if (!PyArg_ParseTuple(args, "i", &flags)) { - return NULL; - } +static PyObject *GeoIP_org_by_addr_Py(PyObject * self, PyObject * args) +{ + char *name; + char *org; + PyObject *ret; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + org = GeoIP_org_by_addr(GeoIP->gi, name); + ret = Py_BuildValue("s", org); + free(org); + return ret; +} - GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); +static PyObject *GeoIP_org_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + char *org; + PyObject *ret; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + org = GeoIP_org_by_name(GeoIP->gi, name); + ret = Py_BuildValue("s", org); + free(org); + return ret; +} - if ( !GeoIP ) - return NULL; +static PyObject *GeoIP_id_by_addr_Py(PyObject * self, PyObject * args) +{ + char *name; + int i; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + i = GeoIP_id_by_addr(GeoIP->gi, name); + return Py_BuildValue("i", i); +} + +static PyObject *GeoIP_id_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + int i; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + i = GeoIP_id_by_name(GeoIP->gi, name); + return Py_BuildValue("i", i); +} - GeoIP->gi = GeoIP_new(flags); +void GeoIP_SetItemString(PyObject * dict, const char *name, const char *value) +{ + PyObject *nameObj; + PyObject *valueObj; + if ((nameObj = Py_BuildValue("s", name))) { + if ((valueObj = Py_BuildValue("s", value))) { + PyDict_SetItem(dict, nameObj, valueObj); + Py_DECREF(valueObj); + } + Py_DECREF(nameObj); + } +} - if (!GeoIP->gi) { - PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); - Py_DECREF(GeoIP); - return NULL; - } +void GeoIP_SetItemFloat(PyObject * dict, const char *name, float value) +{ + PyObject *nameObj; + PyObject *valueObj; + if ((nameObj = Py_BuildValue("s", name))) { + if ((valueObj = Py_BuildValue("f", value))) { + PyDict_SetItem(dict, nameObj, valueObj); + Py_DECREF(valueObj); + } + Py_DECREF(nameObj); + } +} - return (PyObject*)GeoIP; -} - -static PyObject* -GeoIP_open_Py(PyObject* self, PyObject *args) { - GeoIP_GeoIPObject* GeoIP; - char * filename; - int flags; - - if (!PyArg_ParseTuple(args, "si", &filename, &flags)) { - return NULL; - } - - GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); - - if ( !GeoIP ) - return NULL; - - GeoIP->gi = GeoIP_open(filename, flags); - - if (!GeoIP->gi) { - PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); - Py_DECREF(GeoIP); - return NULL; - } - - return (PyObject*)GeoIP; -} - -static void -GeoIP_GeoIP_dealloc(PyObject* self) -{ - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - GeoIP_delete(GeoIP->gi); - PyObject_Del(self); -} - -static PyObject * GeoIP_country_code_by_name_v6_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_code_by_name_v6(GeoIP->gi, name); - return Py_BuildValue("s", retval ); -} - -static PyObject * GeoIP_country_name_by_name_v6_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_name_by_name_v6(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_country_code_by_addr_v6_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_code_by_addr_v6(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_country_name_by_addr_v6_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - PyObject * ret; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_name_by_addr_v6(GeoIP->gi, name); - ret = Py_BuildValue("s", retval); - return ret; -} - -static PyObject * GeoIP_country_code_by_name_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_code_by_name(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_country_name_by_name_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_name_by_name(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_country_code_by_addr_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_code_by_addr(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_country_name_by_addr_Py(PyObject *self, PyObject *args) { - char * name; - const char * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_country_name_by_addr(GeoIP->gi, name); - return Py_BuildValue("s", retval); -} - -static PyObject * GeoIP_org_by_addr_Py(PyObject *self, PyObject *args) { - char * name; - char * org; - PyObject * ret; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - org = GeoIP_org_by_addr(GeoIP->gi, name); - ret = Py_BuildValue("s", org); - free(org); - return ret; -} - -static PyObject * GeoIP_org_by_name_Py(PyObject *self, PyObject *args) { - char * name; - char * org; - PyObject * ret; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - org = GeoIP_org_by_name(GeoIP->gi, name); - ret = Py_BuildValue("s", org); - free(org); - return ret; -} - -static PyObject * GeoIP_id_by_addr_Py(PyObject *self, PyObject *args) { - char * name; - int i; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - i = GeoIP_id_by_addr(GeoIP->gi, name); - return Py_BuildValue("i", i); -} - -static PyObject * GeoIP_id_by_name_Py(PyObject *self, PyObject *args) { - char * name; - int i; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - i = GeoIP_id_by_name(GeoIP->gi, name); - return Py_BuildValue("i", i); -} - -void GeoIP_SetItemString(PyObject *dict, const char * name, const char * value) { - PyObject * nameObj; - PyObject * valueObj; - if (( nameObj = Py_BuildValue("s",name))){ - if ((valueObj = Py_BuildValue("s",value))){ - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(valueObj); - } - Py_DECREF(nameObj); - } -} - -void GeoIP_SetItemFloat(PyObject *dict, const char * name, float value) { - PyObject * nameObj; - PyObject * valueObj; - if (( nameObj = Py_BuildValue("s",name))){ - if (( valueObj = Py_BuildValue("f",value))){ - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(valueObj); - } - Py_DECREF(nameObj); - } -} - -void GeoIP_SetItemInt(PyObject *dict, const char * name, int value) { - PyObject * nameObj; - PyObject * valueObj; - if ((nameObj = Py_BuildValue("s",name))){ - if ((valueObj = Py_BuildValue("i",value))){ - PyDict_SetItem(dict,nameObj,valueObj); - Py_DECREF(valueObj); - } - Py_DECREF(nameObj); - } -} - -static PyObject * GeoIP_region_populate_dict(GeoIPRegion * gir) { - PyObject * retval; - const char * region_name = NULL; - retval = PyDict_New(); - GeoIP_SetItemString(retval,"country_code",gir->country_code); - GeoIP_SetItemString(retval,"region",gir->region); - if ( gir->country_code[0] ) { - region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); - } - GeoIP_SetItemString(retval,"region_name",region_name); - GeoIPRegion_delete(gir); - return retval; -} - -static PyObject * GeoIP_populate_dict(GeoIP* gi, GeoIPRecord *gir) { - PyObject * retval; - retval = PyDict_New(); - GeoIP_SetItemString(retval,"country_code",gir->country_code); - GeoIP_SetItemString(retval,"country_code3",gir->country_code3); - GeoIP_SetItemString(retval,"country_name",gir->country_name); - GeoIP_SetItemString(retval,"region",gir->region); - GeoIP_SetItemString(retval,"city",gir->city); - GeoIP_SetItemString(retval,"postal_code",gir->postal_code); - GeoIP_SetItemFloat(retval,"latitude", gir->latitude); - GeoIP_SetItemFloat(retval,"longitude", gir->longitude); - GeoIP_SetItemString(retval, "region_name", - GeoIP_region_name_by_code(gir->country_code, gir->region)); - GeoIP_SetItemString(retval, "time_zone", - GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region)); - if ( gi->databaseType != GEOIP_CITY_EDITION_REV0 ){ - /* - * metro_code is a alias for the depreciated dma_code. - * we use the depreciated gir->dma_code since the CAPI - * wrapper might be outdated and does not supply metro_code - */ - GeoIP_SetItemInt(retval,"dma_code",gir->dma_code); - /* we did __NOT__ use gir->metro_code here, since metro_code is - * somewhat new */ - GeoIP_SetItemInt(retval,"metro_code",gir->dma_code); - GeoIP_SetItemInt(retval,"area_code",gir->area_code); +void GeoIP_SetItemInt(PyObject * dict, const char *name, int value) +{ + PyObject *nameObj; + PyObject *valueObj; + if ((nameObj = Py_BuildValue("s", name))) { + if ((valueObj = Py_BuildValue("i", value))) { + PyDict_SetItem(dict, nameObj, valueObj); + Py_DECREF(valueObj); } + Py_DECREF(nameObj); + } +} + +static PyObject *GeoIP_region_populate_dict(GeoIPRegion * gir) +{ + PyObject *retval; + const char *region_name = NULL; + retval = PyDict_New(); + GeoIP_SetItemString(retval, "country_code", gir->country_code); + GeoIP_SetItemString(retval, "region", gir->region); + if (gir->country_code[0]) { + region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); + } + GeoIP_SetItemString(retval, "region_name", region_name); + GeoIPRegion_delete(gir); + return retval; +} + +static PyObject *GeoIP_populate_dict(GeoIP * gi, GeoIPRecord * gir) +{ + PyObject *retval; + retval = PyDict_New(); + GeoIP_SetItemString(retval, "country_code", gir->country_code); + GeoIP_SetItemString(retval, "country_code3", gir->country_code3); + GeoIP_SetItemString(retval, "country_name", gir->country_name); + GeoIP_SetItemString(retval, "region", gir->region); + GeoIP_SetItemString(retval, "city", gir->city); + GeoIP_SetItemString(retval, "postal_code", gir->postal_code); + GeoIP_SetItemFloat(retval, "latitude", gir->latitude); + GeoIP_SetItemFloat(retval, "longitude", gir->longitude); + GeoIP_SetItemString(retval, "region_name", + GeoIP_region_name_by_code(gir->country_code, + gir->region)); + GeoIP_SetItemString(retval, "time_zone", + GeoIP_time_zone_by_country_and_region(gir->country_code, + gir->region)); + if (gi->databaseType != GEOIP_CITY_EDITION_REV0) { + /* + * metro_code is a alias for the depreciated dma_code. + * we use the depreciated gir->dma_code since the CAPI + * wrapper might be outdated and does not supply metro_code + */ + GeoIP_SetItemInt(retval, "dma_code", gir->dma_code); + /* we did __NOT__ use gir->metro_code here, since metro_code is + * somewhat new */ + GeoIP_SetItemInt(retval, "metro_code", gir->dma_code); + GeoIP_SetItemInt(retval, "area_code", gir->area_code); + } + + GeoIPRecord_delete(gir); + return retval; +} + +static PyObject *GeoIP_record_by_addr_v6_Py(PyObject * self, PyObject * args) +{ + char *addr; + GeoIPRecord *gir; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &addr)) { + return NULL; + } + gir = GeoIP_record_by_addr_v6(GeoIP->gi, addr); + if (gir == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return GeoIP_populate_dict(GeoIP->gi, gir); +} + +static PyObject *GeoIP_record_by_name_v6_Py(PyObject * self, PyObject * args) +{ + char *name; + GeoIPRecord *gir; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + gir = GeoIP_record_by_name_v6(GeoIP->gi, name); + if (gir == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return GeoIP_populate_dict(GeoIP->gi, gir); +} + +static PyObject *GeoIP_record_by_addr_Py(PyObject * self, PyObject * args) +{ + char *addr; + GeoIPRecord *gir; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &addr)) { + return NULL; + } + gir = GeoIP_record_by_addr(GeoIP->gi, addr); + if (gir == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return GeoIP_populate_dict(GeoIP->gi, gir); +} + +static PyObject *GeoIP_record_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + GeoIPRecord *gir; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + gir = GeoIP_record_by_name(GeoIP->gi, name); + if (gir == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return GeoIP_populate_dict(GeoIP->gi, gir); +} + +static PyObject *GeoIP_region_by_name_Py(PyObject * self, PyObject * args) +{ + char *name; + GeoIPRegion *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_region_by_name(GeoIP->gi, name); + return GeoIP_region_populate_dict(retval); +} + +static PyObject *GeoIP_region_by_addr_Py(PyObject * self, PyObject * args) +{ + char *name; + GeoIPRegion *retval; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + retval = GeoIP_region_by_addr(GeoIP->gi, name); + return GeoIP_region_populate_dict(retval); +} + +static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) +{ + char *name; + char **start_stop_ptr; + PyObject *retval; + + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + start_stop_ptr = GeoIP_range_by_ip(GeoIP->gi, name); + if (!start_stop_ptr) { + return NULL; + } + + retval = Py_BuildValue("ss", start_stop_ptr[0], start_stop_ptr[1]); + + /* relplace this code with GeoIP_range_by_ip_delete in the next version + * otherwise the users need 1.4.5 instead of 1.4.4 */ + if (retval) { + if (start_stop_ptr[0]) + free(start_stop_ptr[0]); + if (start_stop_ptr[1]) + free(start_stop_ptr[1]); + free(start_stop_ptr); + } + return retval; +} + +static PyObject *GeoIP_charset_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + return Py_BuildValue("i", GeoIP_charset(GeoIP->gi)); +} + +static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + int charset; + if (!PyArg_ParseTuple(args, "i", &charset)) { + return NULL; + } + return Py_BuildValue("i", GeoIP_set_charset(GeoIP->gi, charset)); + +} - GeoIPRecord_delete(gir); - return retval; -} - -static PyObject * GeoIP_record_by_addr_v6_Py(PyObject *self, PyObject *args) { - char * addr; - GeoIPRecord * gir; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &addr)) { - return NULL; - } - gir = GeoIP_record_by_addr_v6(GeoIP->gi, addr); - if (gir == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return GeoIP_populate_dict(GeoIP->gi, gir); -} - -static PyObject * GeoIP_record_by_name_v6_Py(PyObject *self, PyObject *args) { - char * name; - GeoIPRecord * gir; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - gir = GeoIP_record_by_name_v6(GeoIP->gi, name); - if (gir == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return GeoIP_populate_dict(GeoIP->gi, gir); -} - -static PyObject * GeoIP_record_by_addr_Py(PyObject *self, PyObject *args) { - char * addr; - GeoIPRecord * gir; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &addr)) { - return NULL; - } - gir = GeoIP_record_by_addr(GeoIP->gi, addr); - if (gir == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return GeoIP_populate_dict(GeoIP->gi, gir); -} - -static PyObject * GeoIP_record_by_name_Py(PyObject *self, PyObject *args) { - char * name; - GeoIPRecord * gir; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - gir = GeoIP_record_by_name(GeoIP->gi, name); - if (gir == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return GeoIP_populate_dict(GeoIP->gi, gir); -} - -static PyObject * GeoIP_region_by_name_Py(PyObject *self, PyObject * args) { - char * name; - GeoIPRegion * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_region_by_name(GeoIP->gi, name); - return GeoIP_region_populate_dict(retval); -} - -static PyObject * GeoIP_region_by_addr_Py(PyObject *self, PyObject * args) { - char * name; - GeoIPRegion * retval; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - retval = GeoIP_region_by_addr(GeoIP->gi, name); - return GeoIP_region_populate_dict(retval); -} - -static PyObject * GeoIP_range_by_ip_Py(PyObject *self, PyObject *args) { - char * name; - char ** start_stop_ptr; - PyObject * retval; - - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (!PyArg_ParseTuple(args, "s", &name)) { - return NULL; - } - start_stop_ptr = GeoIP_range_by_ip(GeoIP->gi, name); - if ( !start_stop_ptr ) { - return NULL; - } - - retval = Py_BuildValue("ss", start_stop_ptr[0], start_stop_ptr[1]); - - /* relplace this code with GeoIP_range_by_ip_delete in the next version - * otherwise the users need 1.4.5 instead of 1.4.4 */ - if ( retval ) { - if ( start_stop_ptr[0] ) - free(start_stop_ptr[0]); - if ( start_stop_ptr[1] ) - free(start_stop_ptr[1]); - free(start_stop_ptr); - } - return retval; -} - -static PyObject * GeoIP_charset_Py(PyObject *self, PyObject * args) { - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - return Py_BuildValue("i", GeoIP_charset(GeoIP->gi) ); -} - -static PyObject * GeoIP_set_charset_Py(PyObject *self, PyObject * args) { - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - int charset; - if (!PyArg_ParseTuple(args, "i", &charset)) { - return NULL; - } - return Py_BuildValue("i", GeoIP_set_charset(GeoIP->gi, charset)); - -} - -static PyObject * GeoIP_last_netmask_Py(PyObject *self, PyObject * args) { - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - return Py_BuildValue("i", GeoIP_last_netmask(GeoIP->gi) ); -} - - -static PyObject * GeoIP_teredo_Py(PyObject *self, PyObject * args) { - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - return Py_BuildValue("i", GeoIP_teredo(GeoIP->gi) ); -} - -static PyObject * GeoIP_enable_teredo_Py(PyObject *self, PyObject * args) { - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - int teredo; - if (!PyArg_ParseTuple(args, "i", &teredo)) { - return NULL; - } - return Py_BuildValue("i", GeoIP_enable_teredo(GeoIP->gi, teredo)); -} - -static PyObject* -GeoIP_lib_version_Py(PyObject* self, PyObject *args) { - return Py_BuildValue("s", GeoIP_lib_version()); -} - -static PyObject * GeoIP_time_zone_by_country_and_region_Py(PyObject *self, PyObject * args) { - char * country_code, *region; - if (!PyArg_ParseTuple(args, "ss", &country_code, ®ion)) { - return NULL; - } - return Py_BuildValue("s", GeoIP_time_zone_by_country_and_region(country_code, region)); +static PyObject *GeoIP_last_netmask_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + return Py_BuildValue("i", GeoIP_last_netmask(GeoIP->gi)); +} + +static PyObject *GeoIP_teredo_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + return Py_BuildValue("i", GeoIP_teredo(GeoIP->gi)); +} + +static PyObject *GeoIP_enable_teredo_Py(PyObject * self, PyObject * args) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + int teredo; + if (!PyArg_ParseTuple(args, "i", &teredo)) { + return NULL; + } + return Py_BuildValue("i", GeoIP_enable_teredo(GeoIP->gi, teredo)); +} + +static PyObject *GeoIP_lib_version_Py(PyObject * self, PyObject * args) +{ + return Py_BuildValue("s", GeoIP_lib_version()); +} + +static PyObject *GeoIP_time_zone_by_country_and_region_Py(PyObject * self, + PyObject * args) +{ + char *country_code, *region; + if (!PyArg_ParseTuple(args, "ss", &country_code, ®ion)) { + return NULL; + } + return Py_BuildValue("s", + GeoIP_time_zone_by_country_and_region(country_code, + region)); } static PyMethodDef GeoIP_Object_methods[] = { - {"country_code_by_name", GeoIP_country_code_by_name_Py, 1, "Lookup Country Code By Name"}, - {"country_name_by_name", GeoIP_country_name_by_name_Py, 1, "Lookup Country Name By Name"}, - {"country_code_by_addr", GeoIP_country_code_by_addr_Py, 1, "Lookup Country Code By IP Address"}, - {"country_name_by_addr", GeoIP_country_name_by_addr_Py, 1, "Lookup Country Name By IP Address"}, - {"org_by_addr", GeoIP_org_by_addr_Py, 1, "Lookup Organization or ISP By IP Address"}, - {"org_by_name", GeoIP_org_by_name_Py, 1, "Lookup Organization or ISP By Name"}, - {"region_by_addr", GeoIP_region_by_addr_Py, 1, "Lookup Region By IP Address"}, - {"region_by_name", GeoIP_region_by_name_Py, 1, "Lookup Region By Name"}, - {"record_by_addr", GeoIP_record_by_addr_Py, 1, "Lookup City Region By IP Address"}, - {"record_by_name", GeoIP_record_by_name_Py, 1, "Lookup City Region By Name"}, - {"range_by_ip", GeoIP_range_by_ip_Py, 1, "Lookup start and end IP's for a given IP"}, - {"charset", GeoIP_charset_Py, 1, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )"}, - {"set_charset", GeoIP_set_charset_Py, 1, "Set the charset for city records"}, - {"last_netmask", GeoIP_last_netmask_Py, 1, "Return the netmask depth of the last lookup"}, - {"country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, 1, "Lookup IPv6 Country Code By Name"}, - {"country_name_by_name_v6", GeoIP_country_name_by_name_v6_Py, 1, "Lookup IPv6 Country Name By Name"}, - {"country_code_by_addr_v6", GeoIP_country_code_by_addr_v6_Py, 1, "Lookup IPv6 Country Code By IP Address"}, - {"country_name_by_addr_v6", GeoIP_country_name_by_addr_v6_Py, 1, "Lookup IPv6 Country Name By IP Address"}, - {"enable_teredo", GeoIP_enable_teredo_Py, 1, "Enable / disable teredo"}, - {"teredo", GeoIP_teredo_Py, 1, "Returns true if teredo is enabled"}, - {"id_by_addr", GeoIP_id_by_addr_Py, 1, "Lookup Netspeed By IP Address"}, - {"id_by_name", GeoIP_id_by_name_Py, 1, "Lookup Netspeed By Name"}, - {"record_by_addr_v6", GeoIP_record_by_addr_v6_Py, 1, "Lookup City Region By IP Address"}, - {"record_by_name_v6", GeoIP_record_by_name_v6_Py, 1, "Lookup City Region By Name"}, - {NULL, NULL, 0, NULL} + {"country_code_by_name", GeoIP_country_code_by_name_Py, 1, + "Lookup Country Code By Name"}, + {"country_name_by_name", GeoIP_country_name_by_name_Py, 1, + "Lookup Country Name By Name"}, + {"country_code_by_addr", GeoIP_country_code_by_addr_Py, 1, + "Lookup Country Code By IP Address"}, + {"country_name_by_addr", GeoIP_country_name_by_addr_Py, 1, + "Lookup Country Name By IP Address"}, + {"org_by_addr", GeoIP_org_by_addr_Py, 1, + "Lookup Organization or ISP By IP Address"}, + {"org_by_name", GeoIP_org_by_name_Py, 1, + "Lookup Organization or ISP By Name"}, + {"region_by_addr", GeoIP_region_by_addr_Py, 1, + "Lookup Region By IP Address"}, + {"region_by_name", GeoIP_region_by_name_Py, 1, "Lookup Region By Name"}, + {"record_by_addr", GeoIP_record_by_addr_Py, 1, + "Lookup City Region By IP Address"}, + {"record_by_name", GeoIP_record_by_name_Py, 1, + "Lookup City Region By Name"}, + {"range_by_ip", GeoIP_range_by_ip_Py, 1, + "Lookup start and end IP's for a given IP"}, + {"charset", GeoIP_charset_Py, 1, + "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )"}, + {"set_charset", GeoIP_set_charset_Py, 1, + "Set the charset for city records"}, + {"last_netmask", GeoIP_last_netmask_Py, 1, + "Return the netmask depth of the last lookup"}, + {"country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, 1, + "Lookup IPv6 Country Code By Name"}, + {"country_name_by_name_v6", GeoIP_country_name_by_name_v6_Py, 1, + "Lookup IPv6 Country Name By Name"}, + {"country_code_by_addr_v6", GeoIP_country_code_by_addr_v6_Py, 1, + "Lookup IPv6 Country Code By IP Address"}, + {"country_name_by_addr_v6", GeoIP_country_name_by_addr_v6_Py, 1, + "Lookup IPv6 Country Name By IP Address"}, + {"enable_teredo", GeoIP_enable_teredo_Py, 1, "Enable / disable teredo"}, + {"teredo", GeoIP_teredo_Py, 1, "Returns true if teredo is enabled"}, + {"id_by_addr", GeoIP_id_by_addr_Py, 1, "Lookup Netspeed By IP Address"}, + {"id_by_name", GeoIP_id_by_name_Py, 1, "Lookup Netspeed By Name"}, + {"record_by_addr_v6", GeoIP_record_by_addr_v6_Py, 1, + "Lookup City Region By IP Address"}, + {"record_by_name_v6", GeoIP_record_by_name_v6_Py, 1, + "Lookup City Region By Name"}, + {NULL, NULL, 0, NULL} }; -static PyObject * -GeoIP_GetAttr(PyObject *self, char *attrname) -{ - PyObject * ret; - char * database_info; - GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self; - if (strcmp(attrname, "GEOIP_STANDARD") == 0) { - return Py_BuildValue("i", 0); - } else if (strcmp(attrname, "database_info") == 0) { - database_info = GeoIP_database_info(GeoIP->gi); - ret = Py_BuildValue("z", database_info); - free(database_info); - return ret; - } else if (strcmp(attrname, "database_edition") == 0) { - return Py_BuildValue("z", GeoIPDBDescription[GeoIP_database_edition(GeoIP->gi)]); - } - return Py_FindMethod(GeoIP_Object_methods, self, attrname); +static PyObject *GeoIP_GetAttr(PyObject * self, char *attrname) +{ + PyObject *ret; + char *database_info; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + if (strcmp(attrname, "GEOIP_STANDARD") == 0) { + return Py_BuildValue("i", 0); + } else if (strcmp(attrname, "database_info") == 0) { + database_info = GeoIP_database_info(GeoIP->gi); + ret = Py_BuildValue("z", database_info); + free(database_info); + return ret; + } else if (strcmp(attrname, "database_edition") == 0) { + return Py_BuildValue("z", + GeoIPDBDescription[GeoIP_database_edition + (GeoIP->gi)]); + } + return Py_FindMethod(GeoIP_Object_methods, self, attrname); } static PyTypeObject GeoIP_GeoIPType = { - PyObject_HEAD_INIT(NULL) - 0, - "GeoIP", - sizeof(GeoIP_GeoIPObject), - 0, - GeoIP_GeoIP_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - (getattrfunc)GeoIP_GetAttr, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ + PyObject_HEAD_INIT(NULL) + 0, + "GeoIP", + sizeof(GeoIP_GeoIPObject), + 0, + GeoIP_GeoIP_dealloc, /*tp_dealloc */ + 0, /*tp_print */ + (getattrfunc) GeoIP_GetAttr, /*tp_getattr */ + 0, /*tp_setattr */ + 0, /*tp_compare */ + 0, /*tp_repr */ + 0, /*tp_as_number */ + 0, /*tp_as_sequence */ + 0, /*tp_as_mapping */ + 0, /*tp_hash */ }; static PyMethodDef GeoIP_Class_methods[] = { - {"new", GeoIP_new_Py, 1, "GeoIP Constructor"}, - {"open", GeoIP_open_Py, 1, "GeoIP Constructor with database filename argument"}, - {"lib_version", GeoIP_lib_version_Py, 1, "Returns the CAPI version"}, - {"time_zone_by_country_and_region", GeoIP_time_zone_by_country_and_region_Py, 1, "Returns time_zone for country, region"}, - {NULL, NULL, 0, NULL} + {"new", GeoIP_new_Py, 1, "GeoIP Constructor"}, + {"open", GeoIP_open_Py, 1, + "GeoIP Constructor with database filename argument"}, + {"lib_version", GeoIP_lib_version_Py, 1, "Returns the CAPI version"}, + {"time_zone_by_country_and_region", + GeoIP_time_zone_by_country_and_region_Py, 1, + "Returns time_zone for country, region"}, + {NULL, NULL, 0, NULL} }; -DL_EXPORT(void) -initGeoIP(void) +DL_EXPORT(void) initGeoIP(void) { - PyObject *m, *d, *tmp, *ccode, *cname, *ccont, *name; - int i; - const int total_ccodes = sizeof (GeoIP_country_code) / - sizeof (GeoIP_country_code[0]); - GeoIP_GeoIPType.ob_type = &PyType_Type; + PyObject *m, *d, *tmp, *ccode, *cname, *ccont, *name; + int i; + const int total_ccodes = sizeof(GeoIP_country_code) / + sizeof(GeoIP_country_code[0]); + GeoIP_GeoIPType.ob_type = &PyType_Type; + + m = Py_InitModule("GeoIP", GeoIP_Class_methods); + d = PyModule_GetDict(m); + + PyGeoIPError = PyErr_NewException("py_geoip.error", NULL, NULL); + PyDict_SetItemString(d, "error", PyGeoIPError); + + ccode = PyTuple_New(total_ccodes); + cname = PyDict_New(); + ccont = PyDict_New(); + + for (i = 0; i < total_ccodes; i++) { + name = PyString_FromString(GeoIP_country_code[i]); + PyTuple_SET_ITEM(ccode, i, name); + + tmp = PyString_FromString(GeoIP_country_name[i]); + PyDict_SetItem(cname, name, tmp); + Py_DECREF(tmp); + + tmp = PyString_FromString(GeoIP_country_continent[i]); + PyDict_SetItem(ccont, name, tmp); + Py_DECREF(tmp); + } + + PyDict_SetItemString(d, "country_codes", ccode); + Py_DECREF(ccode); + PyDict_SetItemString(d, "country_names", cname); + Py_DECREF(cname); + PyDict_SetItemString(d, "country_continents", ccont); + Py_DECREF(ccont); + + tmp = PyInt_FromLong(GEOIP_STANDARD); + PyDict_SetItemString(d, "GEOIP_STANDARD", tmp); + Py_DECREF(tmp); - m = Py_InitModule("GeoIP", GeoIP_Class_methods); - d = PyModule_GetDict(m); + tmp = PyInt_FromLong(GEOIP_MEMORY_CACHE); + PyDict_SetItemString(d, "GEOIP_MEMORY_CACHE", tmp); + Py_DECREF(tmp); - PyGeoIPError = PyErr_NewException("py_geoip.error", NULL, NULL); - PyDict_SetItemString(d, "error", PyGeoIPError); + tmp = PyInt_FromLong(GEOIP_CHECK_CACHE); + PyDict_SetItemString(d, "GEOIP_CHECK_CACHE", tmp); + Py_DECREF(tmp); - ccode = PyTuple_New(total_ccodes); - cname = PyDict_New(); - ccont = PyDict_New(); + tmp = PyInt_FromLong(GEOIP_INDEX_CACHE); + PyDict_SetItemString(d, "GEOIP_INDEX_CACHE", tmp); + Py_DECREF(tmp); - for (i = 0; i Date: Thu, 21 Mar 2013 01:21:32 +0100 Subject: [PATCH 07/66] Release 1.2.8 --- ChangeLog | 3 ++- setup.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 73fdfaf..a461abd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ - * Remove unused code. ( Boris Zentner ) +1.2.8 2013-03-20 + * Remove unused code. ( Boris Zentner ) * Fix low memory error handling and refcount issues on error reported by Matt Domsch 1.2.7 2011-08-23 diff --git a/setup.py b/setup.py index c34324a..59c3e55 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,6 @@ include_dirs = ['/usr/local/include']) setup (name = 'GeoIP-Python', - version = '1.2.7', + version = '1.2.8', description = 'This is a python wrapper to GeoIP', ext_modules = [module1]) From 0850e4a98535505e70e9ab56e94791dd69dd1614 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 25 Oct 2013 13:12:12 -0700 Subject: [PATCH 08/66] setup.py and doc cleanup --- ChangeLog | 135 ++++++++++++++++++++++++++++-------------------------- README | 30 ------------ README.md | 39 ++++++++++++++++ setup.py | 35 ++++++++++---- 4 files changed, 137 insertions(+), 102 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/ChangeLog b/ChangeLog index a461abd..72ef4d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,68 +1,75 @@ -1.2.8 2013-03-20 - * Remove unused code. ( Boris Zentner ) - * Fix low memory error handling and refcount issues on error - reported by Matt Domsch -1.2.7 2011-08-23 - * Add example test_netspeedcell.py ( Boris Zentner ) -1.2.6 2011-06-06 - * Add new methods record_by_name_v6 and record_by_addr_v6 to support - GeoIPCityv6 databases. ( Boris Zentner ) -1.2.5 2011-05-10 - * Remove Confidence and Accuracy Database it is unsued anyway. ( Boris - Zentner ) - * Add new methods enable_teredo, teredo, time_zone_by_country_and_region - and lib_version ( Boris Zentner ) - * Add support for Confidence and Accuracy Database. - * Add support for Netspeed Database. via: - id_by_addr - id_by_name - * Add IPv6 glue *** needs libGeoIP 1.4.7 *** - country_code_by_name_v6 - country_name_by_name_v6 - country_code_by_addr_v6 - country_name_by_addr_v6 ( Boris Zentner ) -1.2.4 2008-12-16 - * Add charset and set_charset methods, as well as - the new attributes GeoIP.GEOIP_CHARSET_ISO_8859_1 and - GeoIP.GEOIP_CHARSET_UTF8 ( Boris Zentner ) - * Add test_city_charset.py script showing howto use - charset and set_charset ( Boris Zentner ) - * Add last_netmask ( Boris Zentner ) - * Fix compile warnings ( Boris Zentner ) - * fix small memoryleak in database_info, org_by_name and org_by_addr ( Boris Zentner ) - * fix version number in setup.py ( Boris Zentner ) - * remove dma_code from test_city.py example ( Boris Zentner ) -1.2.3 2008-09-11 - * update test.py - replace www.government.de with www.bundestag.de. - Add range_by_ip example. ( Boris Zentner ) - * Add range_by_ip - returns largest start and stop ip for any ip. - Remember to use a IP not a name ( Boris Zentner ) - * Add metro_code as a alias for the depreciated dma_code ( Boris Zentner ) -1.2.2 2008-08-13 - * Update test_city.py and test_region.py ( Boris Zentner ) - * Add region_name to GeoIPRegion ( Boris Zentner ) - * Add database_info and database_edition attributes to GeoIP object - (James Henstridge) - * Add region_name and time_zone keys to GeoIPRecord wrapper (James - Henstridge). - * Export the country list, country code -> country name mapping, and - country->continent mapping ( Ignacio Vazquez-Abrams ) - * Raise country code counter from 251 to 253 ( Boris Zentner ) - !! record_by_addr and record_by_name return None instead of throwing a -exception. See test_city.py ( Boris Zentner ) - * Add a py_geoip.error Exception object. (Boris Zentner) - * Export the country list, country code -> country name mapping, and country->continent - mapping ( Ignacio Vazquez-Abrams ) - * Changed license to LGPL from GPL +# Change Log -1.2.1 2005-06-28 - * Added support for GeoIP Region +## 1.2.8 (2013-03-20) +* Remove unused code. ( Boris Zentner ) +* Fix low memory error handling and refcount issues on error + reported by Matt Domsch -1.2.0 2003-05-08 - * Added support for GeoIP City +## 1.2.7 (2011-08-23) +* Add example test_netspeedcell.py ( Boris Zentner ) -1.1.0 2003-03-18 - * Added support for GeoIP ISP and Organization +## 1.2.6 (2011-06-06) +* Add new methods record_by_name_v6 and record_by_addr_v6 to support + GeoIPCityv6 databases. ( Boris Zentner ) -0.2.0 2002-06-25 - * Initial Release +## 1.2.5 (2011-05-10) +* Remove Confidence and Accuracy Database it is unsued anyway. ( Boris + Zentner ) +* Add new methods enable_teredo, teredo, time_zone_by_country_and_region and + lib_version ( Boris Zentner ) +* Add support for Confidence and Accuracy Database. +* Add support for Netspeed Database. via: id_by_addr id_by_name +* Add IPv6 glue *** needs libGeoIP 1.4.7 *** + * country_code_by_name_v6 + * country_name_by_name_v6 + * country_code_by_addr_v6 + * country_name_by_addr_v6 ( Boris Zentner ) + +## 1.2.4 (2008-12-16) +* Add charset and set_charset methods, as well as the new attributes + GeoIP.GEOIP_CHARSET_ISO_8859_1 and GeoIP.GEOIP_CHARSET_UTF8 + ( Boris Zentner ) +* Add test_city_charset.py script showing howto use charset and set_charset + ( Boris Zentner ) +* Add last_netmask ( Boris Zentner ) +* Fix compile warnings ( Boris Zentner ) +* fix small memoryleak in database_info, org_by_name and org_by_addr + ( Boris Zentner ) +* fix version number in setup.py ( Boris Zentner ) +* remove dma_code from test_city.py example ( Boris Zentner ) + +## 1.2.3 (2008-09-11) +* update test.py - replace www.government.de with www.bundestag.de. + Add range_by_ip example. ( Boris Zentner ) +* Add range_by_ip - returns largest start and stop ip for any ip. + Remember to use a IP not a name ( Boris Zentner ) +* Add metro_code as a alias for the depreciated dma_code ( Boris Zentner ) + +## 1.2.2 (2008-08-13) +* Update test_city.py and test_region.py ( Boris Zentner ) +* Add region_name to GeoIPRegion ( Boris Zentner ) +* Add database_info and database_edition attributes to GeoIP object + (James Henstridge) +* Add region_name and time_zone keys to GeoIPRecord wrapper (James + Henstridge). +* Export the country list, country code -> country name mapping, and + country->continent mapping ( Ignacio Vazquez-Abrams ) +* Raise country code counter from 251 to 253 ( Boris Zentner ) +* record_by_addr and record_by_name return None instead of throwing a + exception. See test_city.py ( Boris Zentner ) +* Add a py_geoip.error Exception object. (Boris Zentner) +* Export the country list, country code -> country name mapping, and country->continent + mapping ( Ignacio Vazquez-Abrams ) +* Changed license to LGPL from GPL + +## 1.2.1 (2005-06-28) +* Added support for GeoIP Region + +## 1.2.0 (2003-05-08) +* Added support for GeoIP City + +## 1.1.0 (2003-03-18) +* Added support for GeoIP ISP and Organization + +## 0.2.0 (2002-06-25) +* Initial Release diff --git a/README b/README deleted file mode 100644 index 8fdf1a1..0000000 --- a/README +++ /dev/null @@ -1,30 +0,0 @@ -Requirements: - Python 2.0 or greater - GeoIP C Library 1.4.7 or greater - -To Install: - python setup.py build - python setup.py install - -Usage: - See test.py for example usage with GeoIP Country - See test_org.py for example usage with GeoIP ISP and Organization - See test_domain.py for an example of GeoIP Domain usage - See test_city.py for example usage with GeoIP City - See test_region.py for example usage with GeoIP Region - See test_netspeed.py for example usage with GeoIP Netspeed - See test_v6.py for example usage with GeoIP v6 Country Database - See test_city_acc.py for example usage with GeoIP Confidence and Accuracy Database - -Troubleshooting: - -If you get a "libGeoIP.so.1: cannot open shared object No such file or -directory" error, add /usr/local/lib to /etc/ld.so.conf then run -/sbin/ldconfig /etc/ld.so.conf - -License: - -Copyright (c) 2010 MaxMind LLC - -All rights reserved. This package is free software; it is licensed -under the LGPL. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea386e7 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# MaxMind GeoIP Legacy Python Extension API + +## Requirements: + +* Python 2.0 or greater +* GeoIP C Library 1.4.7 or greater + +## Installation + +With `pip`: + $ pip install GeoIP + +From source: + $ python setup.py build + $ python setup.py install + +## Usage + +* See test.py for example usage with GeoIP Country +* See test_org.py for example usage with GeoIP ISP and Organization +* See test_domain.py for an example of GeoIP Domain usage +* See test_city.py for example usage with GeoIP City +* See test_region.py for example usage with GeoIP Region +* See test_netspeed.py for example usage with GeoIP Netspeed +* See test_v6.py for example usage with GeoIP v6 Country Database +* See test_city_acc.py for example usage with GeoIP Confidence and Accuracy Database + +## Troubleshooting + +If you get a "libGeoIP.so.1: cannot open shared object No such file or +directory" error, add /usr/local/lib to /etc/ld.so.conf then run +/sbin/ldconfig /etc/ld.so.conf + +## License + +Copyright (c) 2013 MaxMind LLC + +All rights reserved. This package is free software; it is licensed +under the LGPL 2.1 or greater. diff --git a/setup.py b/setup.py index 59c3e55..3d665eb 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,31 @@ from distutils.core import setup, Extension module1 = Extension('GeoIP', - libraries = ['GeoIP'], - sources = ['py_GeoIP.c'], - library_dirs = ['/usr/local/lib'], - include_dirs = ['/usr/local/include']) + libraries=['GeoIP'], + sources=['py_GeoIP.c'], + library_dirs=['/usr/local/lib'], + include_dirs=['/usr/local/include']) -setup (name = 'GeoIP-Python', - version = '1.2.8', - description = 'This is a python wrapper to GeoIP', - ext_modules = [module1]) +setup( + name='GeoIP', + version='1.2.8', + description='MaxMind GeoIP Legacy Extension', + author="MaxMind, Inc.", + author_email="support@maxmind.com", + url='http://www.maxmind.com/', + bugtrack_url='https://github.com/maxmind/geoip-api-python/issues', + license=open('LICENSE').read(), + ext_modules=[module1], + classifiers=( + 'Development Status :: 6 - Mature', + 'Environment :: Web Environment', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python', + 'Topic :: Internet :: Proxy Servers', + 'Topic :: Internet', + ), +) From 7d6d79ed94512f96da5d6694df7a5eae3a6cb564 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 25 Oct 2013 13:12:40 -0700 Subject: [PATCH 09/66] Renamed to .md --- ChangeLog => ChangeLog.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ChangeLog => ChangeLog.md (100%) diff --git a/ChangeLog b/ChangeLog.md similarity index 100% rename from ChangeLog rename to ChangeLog.md From 5504f98b8f181663b8486e95e5fe47516ee99ff2 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 25 Oct 2013 13:13:34 -0700 Subject: [PATCH 10/66] Added whitespace --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ea386e7..ca66718 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,11 @@ ## Installation With `pip`: + $ pip install GeoIP From source: + $ python setup.py build $ python setup.py install From dd284cbb7b923f225404f3dc7a78ce2e907aaa9e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 25 Oct 2013 13:15:03 -0700 Subject: [PATCH 11/66] Added long description --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 3d665eb..ab91df8 100644 --- a/setup.py +++ b/setup.py @@ -10,6 +10,7 @@ name='GeoIP', version='1.2.8', description='MaxMind GeoIP Legacy Extension', + long_description=open('README.md').read(), author="MaxMind, Inc.", author_email="support@maxmind.com", url='http://www.maxmind.com/', From cde3ead4f2b7737ff7e3c5c49e2813be93eb1252 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 4 Nov 2013 16:19:37 -0800 Subject: [PATCH 12/66] Fixed incorrect MANIFEST file --- ChangeLog.md | 5 +++++ MANIFEST | 3 ++- setup.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 72ef4d8..fe70557 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,10 @@ + # Change Log +## 1.2.9 (2013-11-04) + +* Packaging fix. PyPI-only release. + ## 1.2.8 (2013-03-20) * Remove unused code. ( Boris Zentner ) * Fix low memory error handling and refcount issues on error diff --git a/MANIFEST b/MANIFEST index d3b8666..21733ec 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,5 +1,6 @@ ChangeLog -README +LICENSE +README.md py_GeoIP.c setup.py test.py diff --git a/setup.py b/setup.py index ab91df8..2a846c4 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='GeoIP', - version='1.2.8', + version='1.2.9', description='MaxMind GeoIP Legacy Extension', long_description=open('README.md').read(), author="MaxMind, Inc.", From e2c39307309b26df41a721f2b66206377b284504 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 10:47:01 -0800 Subject: [PATCH 13/66] Check if return value is null before adding to dict --- py_GeoIP.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 6874ae0..4913f0d 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -408,6 +408,10 @@ static PyObject *GeoIP_region_by_name_Py(PyObject * self, PyObject * args) return NULL; } retval = GeoIP_region_by_name(GeoIP->gi, name); + if (retval == NULL) { + Py_INCREF(Py_None); + return Py_None; + } return GeoIP_region_populate_dict(retval); } @@ -420,6 +424,10 @@ static PyObject *GeoIP_region_by_addr_Py(PyObject * self, PyObject * args) return NULL; } retval = GeoIP_region_by_addr(GeoIP->gi, name); + if (retval == NULL) { + Py_INCREF(Py_None); + return Py_None; + } return GeoIP_region_populate_dict(retval); } @@ -440,7 +448,7 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) retval = Py_BuildValue("ss", start_stop_ptr[0], start_stop_ptr[1]); - /* relplace this code with GeoIP_range_by_ip_delete in the next version + /* relplace this code with GeoIP_range_by_ip_delete in the next version * otherwise the users need 1.4.5 instead of 1.4.4 */ if (retval) { if (start_stop_ptr[0]) From 7a2a35a0c10916fff6a407c77aca067d947599e0 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 13:43:21 -0800 Subject: [PATCH 14/66] Added tests, updated API a bit, and made README rst --- .gitignore | 16 +++++ .travis.yml | 26 ++++++++ README.md | 41 ------------- README.rst | 45 ++++++++++++++ test_city.py => examples/city.py | 0 .../city_charset.py | 11 ++-- test_domain.py => examples/domain.py | 0 examples/errors.py | 58 ++++++++++++++++++ test_netspeed.py => examples/netspeed.py | 0 .../netspeedcell.py | 0 test_org.py => examples/org.py | 0 test_region.py => examples/region.py | 0 test.py => examples/test.py | 0 test_v6.py => examples/v6.py | 0 py_GeoIP.c | 6 +- setup.py | 9 ++- tests/data/GeoIP.dat | Bin 0 -> 3315 bytes tests/data/GeoIPASNum.dat | Bin 0 -> 2554 bytes tests/data/GeoIPCity.dat | Bin 0 -> 4065 bytes tests/data/GeoIPDomain.dat | Bin 0 -> 2808 bytes tests/data/GeoIPISP.dat | Bin 0 -> 4752 bytes tests/data/GeoIPNetSpeedCell.dat | Bin 0 -> 3056 bytes tests/data/GeoIPOrg.dat | Bin 0 -> 4938 bytes tests/data/GeoIPRegion.dat | Bin 0 -> 184 bytes tests/data/GeoIPv6.dat | Bin 0 -> 1444 bytes tests/data/GeoLiteCityIPv6.dat | Bin 0 -> 5111 bytes tests/test_city.py | 26 ++++++++ tests/test_country.py | 12 ++++ tests/test_domain.py | 8 +++ tests/test_errors.py | 58 ++++++++++++++++++ tests/test_netspeedcell.py | 8 +++ tests/test_org.py | 8 +++ tests/test_region.py | 15 +++++ tests/test_v6.py | 8 +++ 34 files changed, 304 insertions(+), 51 deletions(-) create mode 100644 .gitignore create mode 100644 .travis.yml delete mode 100644 README.md create mode 100644 README.rst rename test_city.py => examples/city.py (100%) mode change 100755 => 100644 rename test_city_charset.py => examples/city_charset.py (83%) mode change 100755 => 100644 rename test_domain.py => examples/domain.py (100%) mode change 100755 => 100644 create mode 100644 examples/errors.py rename test_netspeed.py => examples/netspeed.py (100%) mode change 100755 => 100644 rename test_netspeedcell.py => examples/netspeedcell.py (100%) mode change 100755 => 100644 rename test_org.py => examples/org.py (100%) mode change 100755 => 100644 rename test_region.py => examples/region.py (100%) mode change 100755 => 100644 rename test.py => examples/test.py (100%) mode change 100755 => 100644 rename test_v6.py => examples/v6.py (100%) mode change 100755 => 100644 create mode 100644 tests/data/GeoIP.dat create mode 100644 tests/data/GeoIPASNum.dat create mode 100644 tests/data/GeoIPCity.dat create mode 100644 tests/data/GeoIPDomain.dat create mode 100644 tests/data/GeoIPISP.dat create mode 100644 tests/data/GeoIPNetSpeedCell.dat create mode 100644 tests/data/GeoIPOrg.dat create mode 100644 tests/data/GeoIPRegion.dat create mode 100644 tests/data/GeoIPv6.dat create mode 100644 tests/data/GeoLiteCityIPv6.dat create mode 100644 tests/test_city.py create mode 100644 tests/test_country.py create mode 100644 tests/test_domain.py create mode 100644 tests/test_errors.py create mode 100644 tests/test_netspeedcell.py create mode 100644 tests/test_org.py create mode 100644 tests/test_region.py create mode 100644 tests/test_v6.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4213a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +*.pyc +*.so +*.sw? +*~ +.coverage +build +core +dist +docs/_build +docs/html +env/ +MANIFEST +maxminddb.egg-info/ +pylint.txt +valgrind-python.supp +violations.pyflakes.txt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d06a695 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +language: python + +python: + - 2.6 + - 2.7 + +before_install: + - git clone git://github.com/maxmind/geoip-api-c + - cd geoip-api-c + - ./bootstrap + - ./configure + - make + - sudo make install + - sudo ldconfig + - cd .. + - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi + +script: + - CFLAGS="-Werror -Wall -Wextra" python setup.py test + +notifications: + email: + recipients: + - dev@maxmind.com + on_success: change + on_failure: always diff --git a/README.md b/README.md deleted file mode 100644 index ca66718..0000000 --- a/README.md +++ /dev/null @@ -1,41 +0,0 @@ -# MaxMind GeoIP Legacy Python Extension API - -## Requirements: - -* Python 2.0 or greater -* GeoIP C Library 1.4.7 or greater - -## Installation - -With `pip`: - - $ pip install GeoIP - -From source: - - $ python setup.py build - $ python setup.py install - -## Usage - -* See test.py for example usage with GeoIP Country -* See test_org.py for example usage with GeoIP ISP and Organization -* See test_domain.py for an example of GeoIP Domain usage -* See test_city.py for example usage with GeoIP City -* See test_region.py for example usage with GeoIP Region -* See test_netspeed.py for example usage with GeoIP Netspeed -* See test_v6.py for example usage with GeoIP v6 Country Database -* See test_city_acc.py for example usage with GeoIP Confidence and Accuracy Database - -## Troubleshooting - -If you get a "libGeoIP.so.1: cannot open shared object No such file or -directory" error, add /usr/local/lib to /etc/ld.so.conf then run -/sbin/ldconfig /etc/ld.so.conf - -## License - -Copyright (c) 2013 MaxMind LLC - -All rights reserved. This package is free software; it is licensed -under the LGPL 2.1 or greater. diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..82d8520 --- /dev/null +++ b/README.rst @@ -0,0 +1,45 @@ +========================================= +MaxMind GeoIP Legacy Python Extension API +========================================= + +Requirements +------------ + +* Python 2.x +* GeoIP C Library 1.4.7 or greater + +Installation +------------ + +With `pip`: + +.. code-block:: bash + + $ pip install GeoIP + +From source: + +.. code-block:: bash + + $ python setup.py build + $ python setup.py install + +Usage +----- + +See the examples in `examples/`. + +Troubleshooting +--------------- + +If you get a "libGeoIP.so.1: cannot open shared object No such file or +directory" error, add /usr/local/lib to /etc/ld.so.conf then run +/sbin/ldconfig /etc/ld.so.conf + +License +------- + +Copyright (c) 2013 MaxMind LLC + +All rights reserved. This package is free software; it is licensed +under the LGPL 2.1 or greater. diff --git a/test_city.py b/examples/city.py old mode 100755 new mode 100644 similarity index 100% rename from test_city.py rename to examples/city.py diff --git a/test_city_charset.py b/examples/city_charset.py old mode 100755 new mode 100644 similarity index 83% rename from test_city_charset.py rename to examples/city_charset.py index 86d6bfe..44d2dea --- a/test_city_charset.py +++ b/examples/city_charset.py @@ -3,13 +3,13 @@ import GeoIP # open the citydatabase. All cities return in iso-8859-1 by default -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat", GeoIP.GEOIP_STANDARD) # lookup a record, where cityname contains chars > 127 ( eg != ascii ) gir = gi.record_by_name("www.osnabrueck.de") # print the cityname with iso-8859-1 charset -print gir['city']; +print gir['city'] # print the cityname transformed to utf8 print unicode(gir['city'], 'iso-8859-1') @@ -19,7 +19,7 @@ # # from now on all records returns in UTF8 until you change the charset again # Notice, that all previous records return in the previous charset -gi.set_charset(GeoIP.GEOIP_CHARSET_UTF8); +gi.set_charset(GeoIP.GEOIP_CHARSET_UTF8) # get a new record, now in utf8 gir2 = gi.record_by_name("www.osnabrueck.de") @@ -27,9 +27,6 @@ # and print it ( should be the same output as on line 2 ) print gir2['city'] -## Some more charset examples +# Some more charset examples # current_charset = gi.charset() # old_charset = gi.set_charset(GeoIP.GEOIP_CHARSET_ISO_8859_1); - - - diff --git a/test_domain.py b/examples/domain.py old mode 100755 new mode 100644 similarity index 100% rename from test_domain.py rename to examples/domain.py diff --git a/examples/errors.py b/examples/errors.py new file mode 100644 index 0000000..791101d --- /dev/null +++ b/examples/errors.py @@ -0,0 +1,58 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_errors(): + # The intent of these tests is to make sure there are no segfaults. + # Most of the lookups are invalid. Unfortunately the API rarely throws + # exceptions. + gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.org_by_addr('1.1.1.1'), None) + + assert_equals(gi.country_code_by_name('1.1.1.1'), None) + + assert_equals(gi.country_name_by_name('1.1.1.1'), None) + + assert_equals(gi.country_code_by_addr('1.1.1.1'), None) + + assert_equals(gi.country_name_by_addr('1.1.1.1'), None) + + assert_equals(gi.asn_by_addr('1.1.1.1'), None) + + assert_equals(gi.isp_by_addr('1.1.1.1'), None) + + assert_equals(gi.org_by_addr('1.1.1.1'), None) + + assert_equals(gi.org_by_name('1.1.1.1'), None) + + assert_equals(gi.region_by_addr('1.1.1.1'), None) + + assert_equals(gi.region_by_name('1.1.1.1'), None) + assert_equals(gi.record_by_addr('1.1.1.1'), None) + + assert_equals(gi.record_by_name('1.1.1.1'), None) + + assert_equals(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) + + assert_equals(gi.charset('1.1.1.1'), 0) + + assert_equals(gi.set_charset(-1), 0) + + assert_equals(gi.last_netmask('1.1.1.1'), 5) + + assert_equals(gi.country_code_by_name_v6('1.1.1.1'), None) + + assert_equals(gi.country_name_by_name_v6('1.1.1.1'), None) + + assert_equals(gi.country_code_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.country_name_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.enable_teredo(-100), 1) + assert_equals(gi.teredo('1.1.1.1'), 0) + assert_equals(gi.id_by_addr('1.1.1.1'), 0) + assert_equals(gi.id_by_name('1.1.1.1'), 0) + assert_equals(gi.record_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.record_by_name_v6('1.1.1.1'), None) diff --git a/test_netspeed.py b/examples/netspeed.py old mode 100755 new mode 100644 similarity index 100% rename from test_netspeed.py rename to examples/netspeed.py diff --git a/test_netspeedcell.py b/examples/netspeedcell.py old mode 100755 new mode 100644 similarity index 100% rename from test_netspeedcell.py rename to examples/netspeedcell.py diff --git a/test_org.py b/examples/org.py old mode 100755 new mode 100644 similarity index 100% rename from test_org.py rename to examples/org.py diff --git a/test_region.py b/examples/region.py old mode 100755 new mode 100644 similarity index 100% rename from test_region.py rename to examples/region.py diff --git a/test.py b/examples/test.py old mode 100755 new mode 100644 similarity index 100% rename from test.py rename to examples/test.py diff --git a/test_v6.py b/examples/v6.py old mode 100755 new mode 100644 similarity index 100% rename from test_v6.py rename to examples/v6.py diff --git a/py_GeoIP.c b/py_GeoIP.c index 4913f0d..a043d97 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -525,8 +525,12 @@ static PyMethodDef GeoIP_Object_methods[] = { "Lookup Country Code By IP Address"}, {"country_name_by_addr", GeoIP_country_name_by_addr_Py, 1, "Lookup Country Name By IP Address"}, + {"asn_by_addr", GeoIP_org_by_addr_Py, 1, + "Lookup ASN By IP Address"}, + {"isp_by_addr", GeoIP_org_by_addr_Py, 1, + "Lookup Organization By IP Address"}, {"org_by_addr", GeoIP_org_by_addr_Py, 1, - "Lookup Organization or ISP By IP Address"}, + "Lookup Organization By IP Address"}, {"org_by_name", GeoIP_org_by_name_Py, 1, "Lookup Organization or ISP By Name"}, {"region_by_addr", GeoIP_region_by_addr_Py, 1, diff --git a/setup.py b/setup.py index 2a846c4..b0192a1 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,7 @@ -from distutils.core import setup, Extension +try: + from setuptools import setup, Extension +except ImportError: + from distutils.core import setup, Extension module1 = Extension('GeoIP', libraries=['GeoIP'], @@ -10,13 +13,15 @@ name='GeoIP', version='1.2.9', description='MaxMind GeoIP Legacy Extension', - long_description=open('README.md').read(), + long_description=open('README.rst').read(), author="MaxMind, Inc.", author_email="support@maxmind.com", url='http://www.maxmind.com/', bugtrack_url='https://github.com/maxmind/geoip-api-python/issues', license=open('LICENSE').read(), ext_modules=[module1], + tests_require=['nose'], + test_suite='nose.collector', classifiers=( 'Development Status :: 6 - Mature', 'Environment :: Web Environment', diff --git a/tests/data/GeoIP.dat b/tests/data/GeoIP.dat new file mode 100644 index 0000000000000000000000000000000000000000..5374dff90b4f6cc59cbfed9cbed624254389ccb2 GIT binary patch literal 3315 zcmXw*1+*1q6h`;CAT8Y?4R)ibD0ZM=Vqv19B8p&Rg5BK$iXGU2iCrKHDh5(Yh#lB* z{oQY!wPx+L|L>eR^Uu8d?%aS1)PmY@Z*X6*0#L2ClXYZS)>Z03eWihHnAJwGVtQj} zlHOElCYwWx%v&n0WNT=Xd0S;+wb~v!Ku73QCI^j!m7p`c32#9c=n5CWh0qPU!y#}e z^zifzWlz~lmi_mJmGxERs+q5*tS;A(Ys$6c+HxJaF07}NY3nO}V1rD`&ilefuyH1X zlue*tCYvgo$^Ni;=36LR%B|$qu#K{|=cZOZEXII!w z-(Bt@2g^O>UUF}_k1P}RRffQRnd}b-q#pAtfu1ddJxkg?quanoq4N94C zqjD46tlT2Y4VKbGS>y@0HT^c2lzzK%hrAQ+Qtr;`J<7fEK6yVpkk!e`gYqGGSa~F? zkHTa6VxpE%NR~E>H@^e{c z*6eT*EH+t^`BLReSeD6G%GdH6_%`$J;CuatOny{;l0VDk@Jm*Ig_?{0Zt{oxQ~s4b ze=GmMznN5v#QzU7WRXfxORp_g$h?kHH>j6MeWgLrFq1~X2>&&j1WkiO&6@?ygI%(x zMX++PO3>1zm255B$hNYbEcD`qcLC;Kj1-|t$XonT#MJ-L2X`ve>48_K?!Zxn2tzKJr)OK%!%=CXfQH&?ch zTLxPxTgz=^*~7NMcKY_2><|plcg$obWuP1s?5qs%qGiIa!EV{Pd$33PU}eu>FJ*7J zkK9)dk^9O0IYjm?Z-LB*1&0SmxI8krP8punqm&Wy=-?P-WLA$= zMg^lYIW9Op{e<8|{iNXJ;FMrYaBgs3aB6T`Fg6%ho>jG4j?+`lP|lQR$QPhgXc1TK6oL0s`6scD?g<#2d`xHRpqtdb>)rB z-&Ed`ZwJ#dpB}uEK0|poSeBD#2D5_qgAXiyAN&w}sLz%k$#O>@E1v|PW->?lOwJAF zWj5(bLER*k+L}RCBf44FO_A%SHai%H^Fzxw`G(YE-%)a@ADt?tTq*X(ti$? zE5DRq>6QP#y?@P~--6%uKQj4K`AhyC{FC{=K{dVn^bhBhQ5B_@tSyK7vZzy`uFHC| zzHA^H%0{wGSg}Im^d=RW>di7~uC%DoQfZZWYo*+E+f3Ri?PZ4w9hFX5T}kO&p-U!R YD|FMl%O08ctk6sEEk{`A9hSV+yDRo literal 0 HcmV?d00001 diff --git a/tests/data/GeoIPASNum.dat b/tests/data/GeoIPASNum.dat new file mode 100644 index 0000000000000000000000000000000000000000..cf4ce9a7963c0a43850285e9e85b746ee4e4cf9a GIT binary patch literal 2554 zcmX|?1$0zL6o$V^DaEb0K3oIY-E5LYyFo&*1U9%evV=f{CL|C_l{$55)V-xjow_S^ zZ>hVxQc3UroN3OS?|t*no%KFO6#7N@{epHGu)b*X>Kbja6q2F#Sqg89C3wq%Z2 zAQp;qVIFLAB*Y@ox>>S71q;PR;$m@$Xa!4Eyv%P|sXQrKhl8Qmdx@k}EQ9U3;8lLL z99Da;fHmINN-D)FsFu|D)pc+PT&7mFQ0IML(f}vJDJtF|ZiGg^chf$xADB-LgTvtn zI1+X^j#qw^-0tOQ*djkhJl5yOsm$taoB${K=}EBFyKO&JZgL@<=KXXy!~2<%ZQ@yQ zw&WaZ;y714PqYr(;e78Gfc*fh<|5T!EM6jBYEMIQxi0lO`4w=brdRpvzZ$NQU+a?{ zs=bYDtNU*Dc1>S|JK#>ZOVh{2UEtFMed7J%1LA}5 z5IhV!9girnyEeD+C_JXb+(H{qz>`Xz@|90Zo`Kyyc^012Q+nP%w-@|#dr37fi?4{U z`dzO{UWYe)@+Q0`f7>T}V3*?^@m=vf@qN)Q?<4pa+WV{h#NMUyPvJB8-1qrG@`d;% zd?oo>{6@4^-%7rN?0298F68mRtXS;9u{y ziuyY-O|;O;Y&5g4fcNJ1^7r0C($djN(pqdIwiVlnd&Lf-b?fNpWN&^=J3G2cy7<*j zX{j@Ib97hIL+mN`5_^lIRn|`)6$8=s_LmG02Ra7%e6VDQW2jGtNrpQ{NJjd6lq6jo z?HJ?8kWWq{M@-Whj+u_J@^RvL(JCiMCORhhVC{Vy-_$$ZHIaiL?8&lfwE zSaBLl#bx4h(avkRaizWdRbMQYh^1ngm~^ailxE4N*>k~(p{qh8YBS2s8|IvO?Iv|T16;)(2xP!91#BoYp1+A$Q$ zjAq#}oDoYg63s}Zk!UO^%p=dCzwz#@985AWelchCPL3&VNL3uK@9Es&*28sIGRqHB?tCQ=4ysDDvmPa!~ z89{DMePeljvhM$l`H4bX3&*0NoFKP0Syob9T^0`Fxv56cSSV!2P$(lDwPSW9CuDuA z^NMTBf_Ou5MP+eGMPd8^)jaf(nY@z9BL&DsJSk3=VCGD5zm7klJRAFv93KgTtUvT3Bhe+MZ^X znpSGIX;!wVrRI`rWtyd-nQ3m7nOpMx=jpuX`@VDT`JLZ=-v52?JNMq1xdK=~1P}>) zDELUw0O)C*+i=`KvVTtAWpp*r8(IGXsLNCN^7zW&{p$ylwKA( z0P#RaAOUDCXd~zZBm&!k9e@o;0v-h(1Cjwd-~q-0oq4#6>_T=W&HO1qH+pxnhvq#g zy~tGZDzZ1}Ak)ZnAcJDMWm23#mL`2D11w|%R|D5*K8$iLaGfUCQ*y|@K(6NfD0yUm zasZG|DIm>x2T}%+gMk|~FQg11Zv=*FUTTGJ<#3=#lVYHReiJ!@97&EMZzgXcZzWBK z+bFjKqcyn$xKrH?jG>p3(=3z$hUUO>CU@(~3c#yAjxwI~0TU>GtpFc z!mH}5fko6^&Sns1?OCBGuK0VjDo%oW?oq#oD> z>;}H(?s4)P@>_Bb`5n2J+(&*-nm+r1ALu`l2Q)tj{6w$Q~Y@(X#C{FO94 zkC|W5i11@$w%>pg%uj0d6y-Es503j_hf;3?yrGcQKCXED9^k_{Q3!2bl$fladQsM;7G-*z0A!w;dD?wk*&{oh+ z;A7ri&_OUt*Tf4Pf;2%#CJAIGGLf{ANu-%MSzxDk)})J|D?Nqmrg?Wk5A~juUV>Cj zt`hX77jeRL&HGa_$V`Egl126*%?vI|w%}^YHRQEgy-sjFJ%{Y8d9I+JdLE^Wryd~4 z=Wc;k2T}%+g9SHG3dtd)nc+skQ2H=Uh6{@5#hR2*ZX!nrMp8mNsp)XD;1*qZtKc^E z+bN?3cTnym-Q*aulq@4Xn>9Nx@Ts2Lula9uYh$m=%G=5tuGmMVTSk z9D#?(ndHMPneZ_)9@jjsi_IZV&}Xr9Bmz$ho)Nq(ct!B6;5or;!5sE6i=WpHFHlT! zDFQDF=CVo4Nc4ydkMWZE?nGjqh9;RWSj620JaD1;LUHA*3{AIxXEWFQHNorr)NhbW z$fe{m@=bC%`4(x8dRy?0`n!Vn=qt#Tn!it3MSftOlgRMz$!hNIAU`He_zC4x@-x92 z&DT=a2|m~43(9)I2Fgawzocv;Hw(6CzE$v*`ZmgT!9-4C&bw2vOR!t;HA`m%zYD&h ze@pHm&C2g6dj!-oO!-;!BZ6Plk5YaW91|R;|0Xy^ zIbohU4t$ck#!su5!Tz8d>0sec`dPtWlyibc{1f?G*PItzpkLJF66G@akKl^t^%8#X zH*L)(7A*A$N+j8UY-nMDUe72C(F_}tO~@FsDH%(eo^ckMsW-RKg5FY-R+QEj+EChR z-j33qTo;LW%{x*O$W9g#DK@PpQIaj#HR)`j3%x6uqIow9-RV8Z{pM1euUOr%TnzJu zLRDj`gXIej)mMd_i}Ot)ItT zYUVszn^=((w`+7%xY-b|uhJWGS0UT!$Z%qPYSPLBQD=t7)D0>`fiFCyXEL%Jj&vvb z98Q|kEcRe}bX`F%`Uk4ZeeSA%KLeJ{vu&Ljdq|>u=?>(3e4(JnynandZZ|Mt$iQqA z2F!IZ@8~m%?enVQ7pA3SXqC^rs;`;bxmSF`;9M69-R1ro^QL|xIifD!WyY?|vG3f| zad5v(6!J1Tvt14cI;7cGpNju)`aeNYz+YP7H78l$+kWvh*W@26t?*Q(hAOHjc&hwf zpXuM*K092?!iaEJzo&YV88qj7@|7z*Vwk6NVztL#8mJD2fbJ>wwP%@^1cD)tsZ{yR z@+EfrrIU19!9u*sbwjFAjV}^rd7JIV|(({IdS19VPK%T$~*_=E{ZMjc+9@aXg+imUZ1cw6n6{m7RUpFyQWlWX{l0sGxI_oOlF3eNdk3)dP}{fF4UEJ3)HAP zb$6;zH!A6O!k)G6k8kaLzH{$=2`h^t&+{57I~RFgV{s?3snUe5*gB=D(oA_-c|~ci zv{24d&R1G0t(1L~eHDAJwKzg4Q`*qA727H8neALRyo1=$={w<_VHdHhvoFEt=?#0Q zyXGErJ;h#lZ*c%_=lbA%VL#X(F3_2Q_#ikK4uM1AFlcv5<-?uXzU4E|f!2+bk8X4`eNpL_ZnY`%~CYg@t?5IWq&!gtNriiruxd#<|Sq73R)7 zPri#Y?~3mRcNh1-_k{M&e0(psx3~b0_^zE>sBWo}WVavOUpxS}a|hxFi3dCVBKaZ8 zp>&6d3Eb`-E*=p%w(ngdzt)+rlUr}+Eo(L3;B+_2Z*u0F<#z5Ccq=UYKDTSWL$UczXnq%d zx46#f?~&iD6#iekPkz5MKOiq*en_;p9)^#=_0V=3@Qvc5PXCzvapotS{z>^J?~HeWUBwc-o7i3Hp=?%qDfX?N z%;wfxdc!`@c9wohe`SC&P$~4|<%5_9!y(RoC_YRqb^77>2v{ci&VD35N*s-si(Bw9 z;#lT!PG8thP&ZL2{G5~U$#9A|6`uy}>~wsFIMeB8$!9aqfpeXGg?u}6X{x3+z1&ZQ zd2d-=n9Da)o4XaELkA=x(gU8dgL1IUn z&eTPfkssx~C{-3D5?MdUW&$sr41-kYS7yCfOy4wS!$d4dCCdGHy4uT!$uJhiqf|c3 m>IdT0>UfaL$7*s>D$L~?y2WYxqQ4}gzkRR1zP?!h82|Wr#9V>8sDsPZ=g3 zt~{+gqlC%`E|R&KsEz73orCUARSDP9Jr!fE1kdlO(C>tIx%Io{_qcwq{66~qZvFuNp!krRKP-QQ{wRFRtv`-G0iT3ViS`}aXRtiW zyhU84Jf}R*>{Ivxd=b6`Uxu&1SK({W&VOD02K`O=mRo;Y{to?JH-8U*AATTyh_8kp z!H=Q6?-Th7vH1BuQ~#XxFWkN_@vp?M-TWK;Tlk&$y<7i5{v-V-H~$&`1^x-UW7b>)r6~Vh=a(iT4tF zyLlh^O1{_r@&QUc>jT9>%FcAV?_l``%7)4i=0o8yI2?v>1hnTz%10G@k(!T|kD;#( z*KzCX%E!7s4qs0k@8%Qa>(f{9xhA^xsrV$gk+?Cw3EULgeK*557q`H-gp;A|+e*GQ zeH%E%ttaHAu9xAfx#KkT>C9%heKYZLxUIMyUIDjOR{ zxD(!`^}oMwyJ)s6b9>Kj^4(qE1K(5J3*Q?iVGT^dG>qUpI3L=3_L0}RzAs(}2kVXv zZapK9l?BZ96VJu7(9X@_c~}q|@g`{d7UIp~{%(GN{6N)Q;t&( zR~9QrE5|6~ba%@V=SSd2!lR%)Ybic+EWXsOA1`07oTi+v*!~lgla!N{6P4og#j}j3 zxcRAgas3SSbC{h8&w_T=*|_a5K4ah8dFtmY<`=*V;R<*Wyck{rFNOB(W%A2ize0W` z{VI61TfYXs7G5V#);;a48<{-{Z-TaVGky!aRb1)TZ^LgF?{M=w@w>#k@q66*z4(3b ze(?df{-FFJ*B{0o5zD!^-S08=$CW3PCl#B0B7UkoMSmJT18vW<_$u)^H-8>~0lp}{ z-o?$k;@!mVcn`PU6YmA>Z>~OWy)WJm_7?}> z1EJk#5I$HO;^sr;!|20d=+;NbN76^ZMf!d$V-wY5s>7-%+gO{5azS<66lHVyY?N#W z%Y({7W4tgdiyImWnc7q`UmIs~L0Oc^7qZP^MQx@os9F#u>!M_?IjD#W>2P*3RTt%h zzt1esk#&HDA-*X=T-k%4)mf1pQhBEiEl=^h5j$BVpeK literal 0 HcmV?d00001 diff --git a/tests/data/GeoIPNetSpeedCell.dat b/tests/data/GeoIPNetSpeedCell.dat new file mode 100644 index 0000000000000000000000000000000000000000..41489a3b1e9f6be59934858feff190199eae74c9 GIT binary patch literal 3056 zcmXw)1+-R06h^-TNJ>daNs5vpA>EG-rArVV4bt66qjWb&E8QTago?C@gwiP8U_TH0 zXV&`HUVFYXckbM|bLTfHmK0lxBTY6W5%OZl6C+7r zQkV=TmtI2r+7w7iDOE^POE2STU|N_CriU4%KTNN{j4%_-470$j(8u?luX0s(_!{*5 zbty;ioKh}4w-jT~9ySlnyzmW}PkNJT{?MlYc|lkR`n83nB2rPQn3U9%%v4+|A)TW` zNvV`HSDGi4mdZ$jr6E#TsHkO)%{9U9eYzE(xnj?^Vxc}i`12>dpD^&-UIf8yir2fbN#`MP@816WT z+*f8OG8y{#A4tQ34@XA8k%Qw=d;G3l_!M7sY;C5*T@>!_vM0Ua5aF4V%R6j?) zfcvEV$N_jz`V#p{I)wNfzLtEdzR3Rb=^Z9N0*{71$E4%IPe>>6Z$t7O^1bwf^kW$9 zC&E+kXLuT(foEZ4HGYwPrFmYuE#1K{z>Dw_ybQ0vtJ1Zw&cBiT4zI%-(oL$jLLYy? zyOf^5d+{f?=Syr$I1%PO`l1!$|covv7j?9YS*`^z|Qx42L7&NH_}m97dbQ z1Ru*FAHt8|IMeu0onV?6d=gck$yAbsa2oXWoQ}+ZGflHXJ{y^1nj4aN$b8cR#3$^R z`)`pS{-(vIB{a91wwadV%iwb8Jy#$rO`nA1Q)CrfZCVrZwa7Ztdea8e#>i<9OIye{ znKqL|{+oQ~_$JvNuDxj5fq!P&iR?0chwKi0_L%nKpNHfNWFOpbIuP=MrZ0nk<%hrN zkpB&Z`5dEk*mQ)%pYdoomyT090eyxi>F{khk-X>krXQ&O2!Dd7;Lq?h^qyx-XM>+J z{eu4*lJm%gF!H64Uq-ILtEOwnZ=w3T>ALBL=_cF5ch>_-x8QAf2i`T^qmR$ze)v)z zQhEd*Lm%NuIJ+d$sYyUNJE-nSHST0l;m& A`Tzg` literal 0 HcmV?d00001 diff --git a/tests/data/GeoIPOrg.dat b/tests/data/GeoIPOrg.dat new file mode 100644 index 0000000000000000000000000000000000000000..75fda313b7374802d36a446d2c0cc1b28bddcf8b GIT binary patch literal 4938 zcmYk9XLuB46on`B-h2HhA|OOm>>_T8B(f>8O8{G(?9P%Qo0)ZXHUX@tVDE|z?4sBk zDt7E*#RB#Q3W#0oVm$ZB*~k0iJnwt%otgb+;0F{~memGni?l-?D6p&tk@oQ2uoz5s zAZ6B(-U;kX??QGZ<+^V0?yC2I_f)+Xy*Jqh?5p5q7;6Sw>L?4IW8Ui1R3`2$^ z1Mm(5krD8b$eYMph(Sgn=OY&&qmeO)gQSqLh@AZZayN2ZzGJsLd_4OEa-!PrL7zlU zCJV_u$tmPs7Ieh_l7+E>#L0jH_?q4eqGVPJ$VzgI*r zCQHat@NoJJQm!wfmy<_;Gu1vyuOKVIDz&f1Gvql(Vm3?7WAHfpY_f)&L(V1Vkw=k7 zlk(hS=*NP`sd+8@c-3uq9eaXYg8P$jp*(9o=Zi_1)x%xY7tkBXG}uV@NV(64XH-9d z9)MXj58*l0o9NBtLUIwg49`B1F28dU{AATnfuE}SY4j!J>EIdkGf8>QQu;FTEbwgl zIpn$EdB}2HzY1A{tV87f3o*Y5xfzju5qU9q30>xw(k}xqSMw|2%T>RUeieB&c@23j zxPmUvT}i(Vyqg(XovOlNh&(mK3UsUs# z;4ibkLatZ)SLv^juaj?pJ8(962gwG`tpdw>8~zURE@wN)_sEUpCh~pq1M)-iBT}CK zG5izuPsz{J{&V;j>|d(+X8IQLEAVUjR&pD;os{4E2EGf-zu&j$-*NxFy6y-1kKj*g z{xkg-awqt!+W!Xso&66r|C9a~`8W9wxQi}l`WN29-luKrIScUZW!HvmOSU6r*Ph-1 z?5O6Q;GNmKsCid-H}>vi54G<}?*;Z&^FH*xU_UkQ50CJ?gW!XaM(&4zLy>vxa^7L^ zy^y_;;hc{kN0NpdMUE!r{xR^e`JN}{!2JRE%nXJzWT za{3YEOfX8XAS+2ZM-{x9{YW*R1&^`E)qFO+hMWV=r7z{#DH z@OZjS){zM^NjhYToKM!1@;fekf$9zPG`SjQ^wi#mXOI&(3&88?SyG-G(sN`J*i2tY z%5{tAi@_7s{3Q6vs-FTsmHjku3357e267>C5ppK76gd|;4_S+|OO`1=i+(nF4k`Ca z^7ouizd-FThF^kQgmklw zA=i+ydzk(R`6#$n?H{8*4nCpgPtu#z0$=mW_?;9&X?QqD7!J`5bL<|E)E z*$p{L?MK7Mu#Y9D;0%(9k@&c{nd1lPr00Yt<^)Y{!pT}Q?I7)XrrL4p(oWVyqt#Za zm-Lgq7rF@($!7h8YlqlWrA*9jvOUkPpOh&~m>Pefn=#Q$U4s>gSCm^dZbQO1>QfVUTw4bm;*Y~Wblk?`Ac{p&?ZZsuzwpHvm*-2CEH#X)x+$UFM zt>St&jiDVlt#42$r!NYeCOeR8b6&W}jL*29)9m}n304Kp>G`GvN3zWfJDYLPZ7UY9 zsw^$5njI@Oekz5pW$@*i{h+}rO{JVf=r);{pU$=ZWE^ju&GHM4xooFHpT9D_$E z>TNISX7R0KxgeEE+w%QqG}e__MSgD4{2ab*+(|nLztJjknnEXGMawIz{(m&SY3KxU zQ_?Z;&uO&thMHrevgACxc~qF literal 0 HcmV?d00001 diff --git a/tests/data/GeoIPv6.dat b/tests/data/GeoIPv6.dat new file mode 100644 index 0000000000000000000000000000000000000000..6fdb12ddabb6d4578eb2f53279dbf02f7d9b5317 GIT binary patch literal 1444 zcmXxh_ghF&9LMnwAtPjFWQA<9w`63bLRMDxCVQ{Qrcg#i*|JAQS=qDfJ+rd?3D*1(!r3u|K?^ufAV59?zCY>17p zF*d=b*bJLv3v7w4&=*^yAGX1^*bdvHKX$;5*a0vzFb=_?I7}KYji6`aNHpIaMU0ll5Myziy*pl-U^`rz zXnPVd8K+27?R*+B9cM^0?R*xI={R$Uxi}B!;{sfW5x5A=4i`(2^b%ZZ=gWxYxB^$= zDqM|gFiMIh%xY_ib<%n}`MTXSEXz8b-ZEcH;G$#8}HyAj;Y7dBq(jz-D=l_I$iqG&lzQC9G y3SZ+Je2eeUto&a3Kxg7d{DhzJ3z{dtN?G(b{Ek2HC;r0U_y@x|J<@UeEByl*B~W(& literal 0 HcmV?d00001 diff --git a/tests/data/GeoLiteCityIPv6.dat b/tests/data/GeoLiteCityIPv6.dat new file mode 100644 index 0000000000000000000000000000000000000000..647b6ed0c5540c04df3d68f5cdf4bb7dc2c1e611 GIT binary patch literal 5111 zcmYkA3AC1D7lyCBOMlU%l!jAhs%Uhil%k?iq9j5jG?Gfjl5kLlC`AaBlqf|>a!?e> zoP$IY88SB^iqij_d;kAh-&(C}-RIu>+0TB4@BO|vga4Gm#Ux5WY3W0tOcK(g#Ij;J z@lf$FvAkG8tSD9zD~qY-;ZQ}os(6H0O{^{+DW=RCP*b`V)P^jm1Kr?ss0-Q90$M^2 z@|w2AaFPykJ(n~7PdXX5>;3aY{ z%@`Y94TM2342HvC7@~xsaGAvAVrHEyGAfLKE8!Zr7DmdyTD(eZld)sh$+=$4td_QP zG~6J6jCi9sR=i2PS-eF|`Gs)n|MhJWx5GFX4|l+waJ6xbl^oVx@GwkM&fVe!@gBGr zCQ46&DR4jBr#~l)oz(dQ(hrJL#UAn>l0IMh5qK0T8kLO4U^=Xa&tV44gje8IcpPTI zv+x`|0kh$0mrE=a7SBdkK|EBa3={3^th;NI_<-aSv zM*0Kz5I%-aV6FUO@uSeni5>n_&N?wFd?vk7IbXmA_yhL9M%V;D!cVXnw!qi$4ScEm zt>RbWcN!Zz{B1a}4PrOGm-B-dYYhKq_yu;tF8Ec^9pd)Tsu{iQmh+n!tN2}duX6r` zztXeXIKV$3!;G@>%ZZU0 zcdWde3S#VP%vISq+&IPPXjCz(8jX!6#t}v}BiqO^svAcdwT&#JhAPw&Yl`L7L)^?d za_WlV#0|_f@{9(?QAWO^^~5-l`qI@^=V<9hVnZ=Ue(0t~Go!U}f^m#dU>s+(FpgDb zb1_yOd(~1-EAe=-QAW3IjJ8HQqrGvWq9=(_Ay(Hx&dFk|DA>v9Y+PvcHBL3U7(I=% zjIPFM#u-L;qnk#Zo-sC7(L>Ie8Gd{UdKqUM=Nac4=O}uv*jsF&&SPDDUTa*J_SR|dTyKmrwi;g>qm3Jkg~qeS7~@9ce&YdStZ|cZmvOgovvG@YyD`ov zROMU6xUsiMcU6burSBB)5PQjwJ3qm=$CzZ?XWXmkL^1k|=j3ELQ!@OhKh=29m~G54 z9x@&_W*Uzh(~L)q8OncDd`yhnHC?)|dWc&zOU@HwIPuhd(wJ+^Gv*slDf+Y+&(-+U zJR@g;7^7kbo--C1Zy9Th#m4i-E5@tF65|D9nemdbRHI%L{#taZI9y;_Vuqt4sLJH}_mdgEQ=J)_w8*m&Rg!1ze{ABtYy`^*6@1=_}i*?0jczGxiyO z8{Zp07(0xe#*fBN#;+Rtv-pb`yBD2omlJxIvD?^V{Av6q{}1tZah$rUDgBqYSBy_z zoXbDP{`A%0lyMFi{~G4Fb5NP-=YD#^$9;}_TS`u8G1e9Qk5kq;%qj1bQ}j?V`i!v^ zg2)%Q;SrTOB)m zywl2QOIZ2GJ!tdam>^$W>?VRFtbgpr(bviknor|1)&Z$lp=WOR3 zr>k?CbB5F1>85I@i}AZCe#`Z6&UAXo?cMmpGR@L!C>V{>~t0urolJ1I4HiPwXLbF3YGBJ8*?F%(>DT=?qtNgcv6n z6|RzVwHUp{oxCnR{GG|p6lat(+PTfS-MPUTnojVl0)49uup6`}3L5w=#Pn2_?I7ytXx#G6m?>yj4cV;+Kod=z1&Lhr4 z%6wRi%;2MP9uuQ}aHjLPGuxTt%u@6Tafm+Au`^G~nJdOE4S$|9-&yZ`?mXixa8^32 z(z={yon_8T&U4NpXNmKIvse|L7i0G5ZK<3W#W(EO%BouPFMe7xd_I9iELY?)u(%*@3qhiJ% zoFAQEoL`-v6#ZF@&ZCFza(0MOGrsqCJHI*qIQyO7oj;tvoW0H-W&SC~xx`%iF{r;OK=}ncK?e(@E$Qqnf zQRke6t8==Qcw_S}d8>BgqzdNbtz49q-Vsak3Rcyrnbg)1*_)f@r$*D8Z@t2o8>P2i z3J2#Gj;q@`sgdpTi`QojOKLcI6NlC5oK%Ol_0ynwNll)Z&x-6;NlmYqU$m~y(2{7M zoMfk0IePBSOMi4vYSz;HoQAarB$YZyl|5AIrDIYnr{uD!R*j@~mB}mal3kQk>N%W|t<`yl> zE1lHJob0rKawTd?ZmQCzM0v?R?@Ma>iu~oPYxPQM{{Ea9b897W@ZiC^>G1yx51*Sz literal 0 HcmV?d00001 diff --git a/tests/test_city.py b/tests/test_city.py new file mode 100644 index 0000000..2be9e92 --- /dev/null +++ b/tests/test_city.py @@ -0,0 +1,26 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_city(): + gi = GeoIP.open("tests/data/GeoIPCity.dat", GeoIP.GEOIP_STANDARD) + + gir = gi.record_by_addr("64.17.254.216") + + record = { + 'area_code': 310, + 'city': 'El Segundo', + 'country_code': 'US', + 'country_code3': 'USA', + 'country_name': 'United States', + 'dma_code': 803, + 'latitude': 33.91640090942383, + 'longitude': -118.40399932861328, + 'metro_code': 803, + 'postal_code': '90245', + 'region': 'CA', + 'region_name': 'California', + 'time_zone': 'America/Los_Angeles' + } + + assert_equals(gir, record) diff --git a/tests/test_country.py b/tests/test_country.py new file mode 100644 index 0000000..70cd59b --- /dev/null +++ b/tests/test_country.py @@ -0,0 +1,12 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_country(): + gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.country_code_by_addr('64.17.254.216'), 'US') + assert_equals(gi.last_netmask(), 29) + assert_equals(gi.country_name_by_addr('64.17.254.216'), 'United States') + assert_equals(gi.range_by_ip('64.17.254.216'), + ('64.17.254.216', '64.17.254.223')) diff --git a/tests/test_domain.py b/tests/test_domain.py new file mode 100644 index 0000000..8a6a6f1 --- /dev/null +++ b/tests/test_domain.py @@ -0,0 +1,8 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_domain(): + gi = GeoIP.open("tests/data/GeoIPDomain.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.org_by_addr("67.43.156.0"), 'shoesfin.NET') diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 0000000..791101d --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,58 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_errors(): + # The intent of these tests is to make sure there are no segfaults. + # Most of the lookups are invalid. Unfortunately the API rarely throws + # exceptions. + gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.org_by_addr('1.1.1.1'), None) + + assert_equals(gi.country_code_by_name('1.1.1.1'), None) + + assert_equals(gi.country_name_by_name('1.1.1.1'), None) + + assert_equals(gi.country_code_by_addr('1.1.1.1'), None) + + assert_equals(gi.country_name_by_addr('1.1.1.1'), None) + + assert_equals(gi.asn_by_addr('1.1.1.1'), None) + + assert_equals(gi.isp_by_addr('1.1.1.1'), None) + + assert_equals(gi.org_by_addr('1.1.1.1'), None) + + assert_equals(gi.org_by_name('1.1.1.1'), None) + + assert_equals(gi.region_by_addr('1.1.1.1'), None) + + assert_equals(gi.region_by_name('1.1.1.1'), None) + assert_equals(gi.record_by_addr('1.1.1.1'), None) + + assert_equals(gi.record_by_name('1.1.1.1'), None) + + assert_equals(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) + + assert_equals(gi.charset('1.1.1.1'), 0) + + assert_equals(gi.set_charset(-1), 0) + + assert_equals(gi.last_netmask('1.1.1.1'), 5) + + assert_equals(gi.country_code_by_name_v6('1.1.1.1'), None) + + assert_equals(gi.country_name_by_name_v6('1.1.1.1'), None) + + assert_equals(gi.country_code_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.country_name_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.enable_teredo(-100), 1) + assert_equals(gi.teredo('1.1.1.1'), 0) + assert_equals(gi.id_by_addr('1.1.1.1'), 0) + assert_equals(gi.id_by_name('1.1.1.1'), 0) + assert_equals(gi.record_by_addr_v6('1.1.1.1'), None) + + assert_equals(gi.record_by_name_v6('1.1.1.1'), None) diff --git a/tests/test_netspeedcell.py b/tests/test_netspeedcell.py new file mode 100644 index 0000000..9b315ad --- /dev/null +++ b/tests/test_netspeedcell.py @@ -0,0 +1,8 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_netspeed_cell(): + gi = GeoIP.open("tests/data/GeoIPNetSpeedCell.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.org_by_addr("2.125.160.1"), 'Dialup') diff --git a/tests/test_org.py b/tests/test_org.py new file mode 100644 index 0000000..73f5778 --- /dev/null +++ b/tests/test_org.py @@ -0,0 +1,8 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_org(): + gi = GeoIP.open("tests/data/GeoIPOrg.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.org_by_addr("12.87.118.0"), 'AT&T Worldnet Services') diff --git a/tests/test_region.py b/tests/test_region.py new file mode 100644 index 0000000..9f953dd --- /dev/null +++ b/tests/test_region.py @@ -0,0 +1,15 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_region(): + gi = GeoIP.open("tests/data/GeoIPRegion.dat", GeoIP.GEOIP_STANDARD) + + gir = gi.region_by_addr('64.17.254.223') + + region = { + 'region': 'CA', + 'region_name': 'California', + 'country_code': 'US' + } + assert_equals(gir, region) diff --git a/tests/test_v6.py b/tests/test_v6.py new file mode 100644 index 0000000..d2000df --- /dev/null +++ b/tests/test_v6.py @@ -0,0 +1,8 @@ +import GeoIP +from nose.tools import assert_equals + + +def test_ipv6(): + gi = GeoIP.open("tests/data/GeoIPv6.dat", GeoIP.GEOIP_STANDARD) + + assert_equals(gi.country_code_by_addr_v6('2001:200::'), 'JP') From 066b470fa2d6604d92b8e2fbcfe4e2ba86f8abfc Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 14:04:29 -0800 Subject: [PATCH 15/66] Make code a bit more sane and uncrustify --- py_GeoIP.c | 242 +++++++++++++++++++++++-------------------- tests/test_errors.py | 6 +- 2 files changed, 131 insertions(+), 117 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index a043d97..a244420 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -22,6 +22,12 @@ #include "GeoIP.h" #include "GeoIPCity.h" +#ifdef __GNUC__ + # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) +#else + # define UNUSED(x) UNUSED_ ## x +#endif + staticforward PyTypeObject GeoIP_GeoIPType; /* Exception object for python */ @@ -32,7 +38,7 @@ typedef struct { GeoIP *gi; } GeoIP_GeoIPObject; -static PyObject *GeoIP_new_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_new_Py(PyObject *UNUSED(self), PyObject * args) { GeoIP_GeoIPObject *GeoIP; int flags; @@ -43,8 +49,9 @@ static PyObject *GeoIP_new_Py(PyObject * self, PyObject * args) GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); - if (!GeoIP) + if (!GeoIP) { return NULL; + } GeoIP->gi = GeoIP_new(flags); @@ -54,10 +61,10 @@ static PyObject *GeoIP_new_Py(PyObject * self, PyObject * args) return NULL; } - return (PyObject *) GeoIP; + return (PyObject *)GeoIP; } -static PyObject *GeoIP_open_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_open_Py(PyObject *UNUSED(self), PyObject * args) { GeoIP_GeoIPObject *GeoIP; char *filename; @@ -69,8 +76,9 @@ static PyObject *GeoIP_open_Py(PyObject * self, PyObject * args) GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); - if (!GeoIP) + if (!GeoIP) { return NULL; + } GeoIP->gi = GeoIP_open(filename, flags); @@ -80,12 +88,12 @@ static PyObject *GeoIP_open_Py(PyObject * self, PyObject * args) return NULL; } - return (PyObject *) GeoIP; + return (PyObject *)GeoIP; } -static void GeoIP_GeoIP_dealloc(PyObject * self) +static void GeoIP_GeoIP_dealloc(PyObject *self) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; GeoIP_delete(GeoIP->gi); PyObject_Del(self); } @@ -95,7 +103,7 @@ static PyObject *GeoIP_country_code_by_name_v6_Py(PyObject * self, { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -108,7 +116,7 @@ static PyObject *GeoIP_country_name_by_name_v6_Py(PyObject * self, { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -121,7 +129,7 @@ static PyObject *GeoIP_country_code_by_addr_v6_Py(PyObject * self, { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -135,7 +143,7 @@ static PyObject *GeoIP_country_name_by_addr_v6_Py(PyObject * self, char *name; const char *retval; PyObject *ret; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -148,7 +156,7 @@ static PyObject *GeoIP_country_code_by_name_Py(PyObject * self, PyObject * args) { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -160,7 +168,7 @@ static PyObject *GeoIP_country_name_by_name_Py(PyObject * self, PyObject * args) { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -172,7 +180,7 @@ static PyObject *GeoIP_country_code_by_addr_Py(PyObject * self, PyObject * args) { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -184,7 +192,7 @@ static PyObject *GeoIP_country_name_by_addr_Py(PyObject * self, PyObject * args) { char *name; const char *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -197,7 +205,7 @@ static PyObject *GeoIP_org_by_addr_Py(PyObject * self, PyObject * args) char *name; char *org; PyObject *ret; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -212,7 +220,7 @@ static PyObject *GeoIP_org_by_name_Py(PyObject * self, PyObject * args) char *name; char *org; PyObject *ret; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -226,7 +234,7 @@ static PyObject *GeoIP_id_by_addr_Py(PyObject * self, PyObject * args) { char *name; int i; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -238,7 +246,7 @@ static PyObject *GeoIP_id_by_name_Py(PyObject * self, PyObject * args) { char *name; int i; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -339,7 +347,7 @@ static PyObject *GeoIP_record_by_addr_v6_Py(PyObject * self, PyObject * args) { char *addr; GeoIPRecord *gir; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &addr)) { return NULL; } @@ -355,7 +363,7 @@ static PyObject *GeoIP_record_by_name_v6_Py(PyObject * self, PyObject * args) { char *name; GeoIPRecord *gir; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -371,7 +379,7 @@ static PyObject *GeoIP_record_by_addr_Py(PyObject * self, PyObject * args) { char *addr; GeoIPRecord *gir; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &addr)) { return NULL; } @@ -387,7 +395,7 @@ static PyObject *GeoIP_record_by_name_Py(PyObject * self, PyObject * args) { char *name; GeoIPRecord *gir; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -403,7 +411,7 @@ static PyObject *GeoIP_region_by_name_Py(PyObject * self, PyObject * args) { char *name; GeoIPRegion *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -419,7 +427,7 @@ static PyObject *GeoIP_region_by_addr_Py(PyObject * self, PyObject * args) { char *name; GeoIPRegion *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -437,7 +445,7 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) char **start_stop_ptr; PyObject *retval; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } @@ -451,24 +459,26 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) /* relplace this code with GeoIP_range_by_ip_delete in the next version * otherwise the users need 1.4.5 instead of 1.4.4 */ if (retval) { - if (start_stop_ptr[0]) + if (start_stop_ptr[0]) { free(start_stop_ptr[0]); - if (start_stop_ptr[1]) + } + if (start_stop_ptr[1]) { free(start_stop_ptr[1]); + } free(start_stop_ptr); } return retval; } -static PyObject *GeoIP_charset_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_charset_Py(PyObject *self, PyObject *UNUSED(args)) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("i", GeoIP_charset(GeoIP->gi)); } static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; int charset; if (!PyArg_ParseTuple(args, "i", &charset)) { return NULL; @@ -477,21 +487,21 @@ static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) } -static PyObject *GeoIP_last_netmask_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_last_netmask_Py(PyObject * self, PyObject *UNUSED(args)) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("i", GeoIP_last_netmask(GeoIP->gi)); } -static PyObject *GeoIP_teredo_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_teredo_Py(PyObject * self, PyObject *UNUSED(args)) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("i", GeoIP_teredo(GeoIP->gi)); } static PyObject *GeoIP_enable_teredo_Py(PyObject * self, PyObject * args) { - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; int teredo; if (!PyArg_ParseTuple(args, "i", &teredo)) { return NULL; @@ -499,12 +509,13 @@ static PyObject *GeoIP_enable_teredo_Py(PyObject * self, PyObject * args) return Py_BuildValue("i", GeoIP_enable_teredo(GeoIP->gi, teredo)); } -static PyObject *GeoIP_lib_version_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_lib_version_Py(PyObject *UNUSED(self), + PyObject *UNUSED(args)) { return Py_BuildValue("s", GeoIP_lib_version()); } -static PyObject *GeoIP_time_zone_by_country_and_region_Py(PyObject * self, +static PyObject *GeoIP_time_zone_by_country_and_region_Py(PyObject *UNUSED(self), PyObject * args) { char *country_code, *region; @@ -517,61 +528,67 @@ static PyObject *GeoIP_time_zone_by_country_and_region_Py(PyObject * self, } static PyMethodDef GeoIP_Object_methods[] = { - {"country_code_by_name", GeoIP_country_code_by_name_Py, 1, - "Lookup Country Code By Name"}, - {"country_name_by_name", GeoIP_country_name_by_name_Py, 1, - "Lookup Country Name By Name"}, - {"country_code_by_addr", GeoIP_country_code_by_addr_Py, 1, - "Lookup Country Code By IP Address"}, - {"country_name_by_addr", GeoIP_country_name_by_addr_Py, 1, - "Lookup Country Name By IP Address"}, - {"asn_by_addr", GeoIP_org_by_addr_Py, 1, - "Lookup ASN By IP Address"}, - {"isp_by_addr", GeoIP_org_by_addr_Py, 1, - "Lookup Organization By IP Address"}, - {"org_by_addr", GeoIP_org_by_addr_Py, 1, - "Lookup Organization By IP Address"}, - {"org_by_name", GeoIP_org_by_name_Py, 1, - "Lookup Organization or ISP By Name"}, - {"region_by_addr", GeoIP_region_by_addr_Py, 1, - "Lookup Region By IP Address"}, - {"region_by_name", GeoIP_region_by_name_Py, 1, "Lookup Region By Name"}, - {"record_by_addr", GeoIP_record_by_addr_Py, 1, - "Lookup City Region By IP Address"}, - {"record_by_name", GeoIP_record_by_name_Py, 1, - "Lookup City Region By Name"}, - {"range_by_ip", GeoIP_range_by_ip_Py, 1, - "Lookup start and end IP's for a given IP"}, - {"charset", GeoIP_charset_Py, 1, - "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )"}, - {"set_charset", GeoIP_set_charset_Py, 1, - "Set the charset for city records"}, - {"last_netmask", GeoIP_last_netmask_Py, 1, - "Return the netmask depth of the last lookup"}, - {"country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, 1, - "Lookup IPv6 Country Code By Name"}, - {"country_name_by_name_v6", GeoIP_country_name_by_name_v6_Py, 1, - "Lookup IPv6 Country Name By Name"}, - {"country_code_by_addr_v6", GeoIP_country_code_by_addr_v6_Py, 1, - "Lookup IPv6 Country Code By IP Address"}, - {"country_name_by_addr_v6", GeoIP_country_name_by_addr_v6_Py, 1, - "Lookup IPv6 Country Name By IP Address"}, - {"enable_teredo", GeoIP_enable_teredo_Py, 1, "Enable / disable teredo"}, - {"teredo", GeoIP_teredo_Py, 1, "Returns true if teredo is enabled"}, - {"id_by_addr", GeoIP_id_by_addr_Py, 1, "Lookup Netspeed By IP Address"}, - {"id_by_name", GeoIP_id_by_name_Py, 1, "Lookup Netspeed By Name"}, - {"record_by_addr_v6", GeoIP_record_by_addr_v6_Py, 1, - "Lookup City Region By IP Address"}, - {"record_by_name_v6", GeoIP_record_by_name_v6_Py, 1, - "Lookup City Region By Name"}, - {NULL, NULL, 0, NULL} + { "country_code_by_name", GeoIP_country_code_by_name_Py, METH_VARARGS, + "Lookup Country Code By Name" }, + { "country_name_by_name", GeoIP_country_name_by_name_Py, METH_VARARGS, + "Lookup Country Name By Name" }, + { "country_code_by_addr", GeoIP_country_code_by_addr_Py, METH_VARARGS, + "Lookup Country Code By IP Address" }, + { "country_name_by_addr", GeoIP_country_name_by_addr_Py, METH_VARARGS, + "Lookup Country Name By IP Address" }, + { "asn_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, + "Lookup ASN By IP Address" }, + { "isp_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, + "Lookup Organization By IP Address" }, + { "org_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, + "Lookup Organization By IP Address" }, + { "org_by_name", GeoIP_org_by_name_Py, METH_VARARGS, + "Lookup Organization or ISP By Name" }, + { "region_by_addr", GeoIP_region_by_addr_Py, METH_VARARGS, + "Lookup Region By IP Address" }, + { "region_by_name", GeoIP_region_by_name_Py, METH_VARARGS, + "Lookup Region By Name" }, + { "record_by_addr", GeoIP_record_by_addr_Py, METH_VARARGS, + "Lookup City Region By IP Address" }, + { "record_by_name", GeoIP_record_by_name_Py, METH_VARARGS, + "Lookup City Region By Name" }, + { "range_by_ip", GeoIP_range_by_ip_Py, METH_VARARGS, + "Lookup start and end IP's for a given IP" }, + { "charset", GeoIP_charset_Py, METH_NOARGS, + "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )" }, + { "set_charset", GeoIP_set_charset_Py, METH_VARARGS, + "Set the charset for city records" }, + { "last_netmask", GeoIP_last_netmask_Py, METH_NOARGS, + "Return the netmask depth of the last lookup" }, + { "country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, METH_VARARGS, + "Lookup IPv6 Country Code By Name" }, + { "country_name_by_name_v6", GeoIP_country_name_by_name_v6_Py, METH_VARARGS, + "Lookup IPv6 Country Name By Name" }, + { "country_code_by_addr_v6", GeoIP_country_code_by_addr_v6_Py, METH_VARARGS, + "Lookup IPv6 Country Code By IP Address" }, + { "country_name_by_addr_v6", GeoIP_country_name_by_addr_v6_Py, METH_VARARGS, + "Lookup IPv6 Country Name By IP Address" }, + { "enable_teredo", GeoIP_enable_teredo_Py, METH_VARARGS, + "Enable / disable teredo" }, + { "teredo", GeoIP_teredo_Py, METH_NOARGS, + "Returns true if teredo is enabled" }, + { "id_by_addr", GeoIP_id_by_addr_Py, METH_VARARGS, + "Lookup Netspeed By IP Address" }, + { "id_by_name", GeoIP_id_by_name_Py, METH_VARARGS, + "Lookup Netspeed By Name" }, + { "record_by_addr_v6", GeoIP_record_by_addr_v6_Py, METH_VARARGS, + "Lookup City Region By IP Address" }, + { "record_by_name_v6", GeoIP_record_by_name_v6_Py, METH_VARARGS, + "Lookup City Region By Name" }, + { NULL, NULL, 0, + NULL } }; static PyObject *GeoIP_GetAttr(PyObject * self, char *attrname) { PyObject *ret; char *database_info; - GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *) self; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; if (strcmp(attrname, "GEOIP_STANDARD") == 0) { return Py_BuildValue("i", 0); } else if (strcmp(attrname, "database_info") == 0) { @@ -582,46 +599,43 @@ static PyObject *GeoIP_GetAttr(PyObject * self, char *attrname) } else if (strcmp(attrname, "database_edition") == 0) { return Py_BuildValue("z", GeoIPDBDescription[GeoIP_database_edition - (GeoIP->gi)]); + (GeoIP->gi)]); } return Py_FindMethod(GeoIP_Object_methods, self, attrname); } static PyTypeObject GeoIP_GeoIPType = { PyObject_HEAD_INIT(NULL) - 0, - "GeoIP", - sizeof(GeoIP_GeoIPObject), - 0, - GeoIP_GeoIP_dealloc, /*tp_dealloc */ - 0, /*tp_print */ - (getattrfunc) GeoIP_GetAttr, /*tp_getattr */ - 0, /*tp_setattr */ - 0, /*tp_compare */ - 0, /*tp_repr */ - 0, /*tp_as_number */ - 0, /*tp_as_sequence */ - 0, /*tp_as_mapping */ - 0, /*tp_hash */ + .tp_name = "GeoIP", + .tp_doc = "GeoIP database object", + .tp_basicsize = sizeof(GeoIP_GeoIPObject), + .tp_dealloc = GeoIP_GeoIP_dealloc, + .tp_getattr = (getattrfunc)GeoIP_GetAttr, }; static PyMethodDef GeoIP_Class_methods[] = { - {"new", GeoIP_new_Py, 1, "GeoIP Constructor"}, - {"open", GeoIP_open_Py, 1, - "GeoIP Constructor with database filename argument"}, - {"lib_version", GeoIP_lib_version_Py, 1, "Returns the CAPI version"}, - {"time_zone_by_country_and_region", - GeoIP_time_zone_by_country_and_region_Py, 1, - "Returns time_zone for country, region"}, - {NULL, NULL, 0, NULL} + { "new", GeoIP_new_Py, + METH_VARARGS, + "GeoIP Constructor" }, + { "open", GeoIP_open_Py, + METH_VARARGS, + "GeoIP Constructor with database filename argument" }, + { "lib_version", GeoIP_lib_version_Py, + METH_NOARGS, + "Returns the CAPI version" }, + { "time_zone_by_country_and_region", + GeoIP_time_zone_by_country_and_region_Py, METH_VARARGS, + "Returns time_zone for country, region" }, + { NULL, NULL, + 0, + NULL } }; -DL_EXPORT(void) initGeoIP(void) -{ +DL_EXPORT(void) initGeoIP(void){ PyObject *m, *d, *tmp, *ccode, *cname, *ccont, *name; int i; const int total_ccodes = sizeof(GeoIP_country_code) / - sizeof(GeoIP_country_code[0]); + sizeof(GeoIP_country_code[0]); GeoIP_GeoIPType.ob_type = &PyType_Type; m = Py_InitModule("GeoIP", GeoIP_Class_methods); diff --git a/tests/test_errors.py b/tests/test_errors.py index 791101d..2585c87 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -35,11 +35,11 @@ def test_errors(): assert_equals(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) - assert_equals(gi.charset('1.1.1.1'), 0) + assert_equals(gi.charset(), 0) assert_equals(gi.set_charset(-1), 0) - assert_equals(gi.last_netmask('1.1.1.1'), 5) + assert_equals(gi.last_netmask(), 5) assert_equals(gi.country_code_by_name_v6('1.1.1.1'), None) @@ -50,7 +50,7 @@ def test_errors(): assert_equals(gi.country_name_by_addr_v6('1.1.1.1'), None) assert_equals(gi.enable_teredo(-100), 1) - assert_equals(gi.teredo('1.1.1.1'), 0) + assert_equals(gi.teredo(), 0) assert_equals(gi.id_by_addr('1.1.1.1'), 0) assert_equals(gi.id_by_name('1.1.1.1'), 0) assert_equals(gi.record_by_addr_v6('1.1.1.1'), None) From 47f8f07efa0d600243d504357410f55807f9aadb Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 19:08:13 -0800 Subject: [PATCH 16/66] Rename country example --- examples/{test.py => country.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{test.py => country.py} (100%) diff --git a/examples/test.py b/examples/country.py similarity index 100% rename from examples/test.py rename to examples/country.py From a1e742f4a082cb208fa234c71812ed4d7479795e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 19:13:43 -0800 Subject: [PATCH 17/66] Updated manifest --- MANIFEST | 14 -------------- MANIFEST.in | 3 +++ 2 files changed, 3 insertions(+), 14 deletions(-) delete mode 100644 MANIFEST create mode 100644 MANIFEST.in diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index 21733ec..0000000 --- a/MANIFEST +++ /dev/null @@ -1,14 +0,0 @@ -ChangeLog -LICENSE -README.md -py_GeoIP.c -setup.py -test.py -test_city.py -test_city_charset.py -test_domain.py -test_netspeed.py -test_netspeedcell.py -test_org.py -test_region.py -test_v6.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..0ea1e05 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +include ChangeLog LICENSE README.rst py_GeoIP.c +recursive-include examples *.py +recursive-include tests *.py From aee38ba95d3f1487b756744ecf391d9e6a5ee52d Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 19:14:16 -0800 Subject: [PATCH 18/66] Removed unused configure.in --- configure.in | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 configure.in diff --git a/configure.in b/configure.in deleted file mode 100644 index 28d5a82..0000000 --- a/configure.in +++ /dev/null @@ -1,5 +0,0 @@ -dnl Process this file with autoconf to produce a configure script. -AC_INIT(py_GeoIP.c) -AC_LANG_C - -AC_OUTPUT(Makefile) From 0bb04f0c10c9894135574b056442234bcf75a269 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 25 Nov 2013 19:16:12 -0800 Subject: [PATCH 19/66] Fix test warning --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index b0192a1..2d4807a 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +import multiprocessing + try: from setuptools import setup, Extension except ImportError: From b56f111b5c7c1c8b341f288f0572f3af006951af Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 26 Nov 2013 15:49:48 +0100 Subject: [PATCH 20/66] Remove new api functions in flavor of name_by_* to match the other API's --- examples/errors.py | 4 ++-- py_GeoIP.c | 24 ++++++++++++------------ tests/test_errors.py | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/errors.py b/examples/errors.py index 791101d..c8eb388 100644 --- a/examples/errors.py +++ b/examples/errors.py @@ -18,9 +18,9 @@ def test_errors(): assert_equals(gi.country_name_by_addr('1.1.1.1'), None) - assert_equals(gi.asn_by_addr('1.1.1.1'), None) + assert_equals(gi.name_by_addr('1.1.1.1'), None) - assert_equals(gi.isp_by_addr('1.1.1.1'), None) + assert_equals(gi.name_by_name('1.1.1.1'), None) assert_equals(gi.org_by_addr('1.1.1.1'), None) diff --git a/py_GeoIP.c b/py_GeoIP.c index a244420..462352e 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -200,7 +200,7 @@ static PyObject *GeoIP_country_name_by_addr_Py(PyObject * self, PyObject * args) return Py_BuildValue("s", retval); } -static PyObject *GeoIP_org_by_addr_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_name_by_addr_Py(PyObject * self, PyObject * args) { char *name; char *org; @@ -209,13 +209,13 @@ static PyObject *GeoIP_org_by_addr_Py(PyObject * self, PyObject * args) if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } - org = GeoIP_org_by_addr(GeoIP->gi, name); + org = GeoIP_name_by_addr(GeoIP->gi, name); ret = Py_BuildValue("s", org); free(org); return ret; } -static PyObject *GeoIP_org_by_name_Py(PyObject * self, PyObject * args) +static PyObject *GeoIP_name_by_name_Py(PyObject * self, PyObject * args) { char *name; char *org; @@ -224,7 +224,7 @@ static PyObject *GeoIP_org_by_name_Py(PyObject * self, PyObject * args) if (!PyArg_ParseTuple(args, "s", &name)) { return NULL; } - org = GeoIP_org_by_name(GeoIP->gi, name); + org = GeoIP_name_by_name(GeoIP->gi, name); ret = Py_BuildValue("s", org); free(org); return ret; @@ -536,14 +536,14 @@ static PyMethodDef GeoIP_Object_methods[] = { "Lookup Country Code By IP Address" }, { "country_name_by_addr", GeoIP_country_name_by_addr_Py, METH_VARARGS, "Lookup Country Name By IP Address" }, - { "asn_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, - "Lookup ASN By IP Address" }, - { "isp_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, - "Lookup Organization By IP Address" }, - { "org_by_addr", GeoIP_org_by_addr_Py, METH_VARARGS, - "Lookup Organization By IP Address" }, - { "org_by_name", GeoIP_org_by_name_Py, METH_VARARGS, - "Lookup Organization or ISP By Name" }, + { "name_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, + "Lookup ASN, Domain, ISP and Organisation By IP Address" }, + { "name_by_name", GeoIP_name_by_name_Py, METH_VARARGS, + "Lookup ASN, Domain, ISP and Organisation By Name" }, + { "org_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, + "Lookup ASN, Domain, ISP and Organisation By IP Address ( deprecated use name_by_addr instead )" }, + { "org_by_name", GeoIP_name_by_name_Py, METH_VARARGS, + "Lookup ASN, Domain, ISP and Organisation By Name ( deprecated use name_by_addr instead )" }, { "region_by_addr", GeoIP_region_by_addr_Py, METH_VARARGS, "Lookup Region By IP Address" }, { "region_by_name", GeoIP_region_by_name_Py, METH_VARARGS, diff --git a/tests/test_errors.py b/tests/test_errors.py index 2585c87..795f521 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -18,9 +18,9 @@ def test_errors(): assert_equals(gi.country_name_by_addr('1.1.1.1'), None) - assert_equals(gi.asn_by_addr('1.1.1.1'), None) + assert_equals(gi.name_by_addr('1.1.1.1'), None) - assert_equals(gi.isp_by_addr('1.1.1.1'), None) + assert_equals(gi.name_by_name('1.1.1.1'), None) assert_equals(gi.org_by_addr('1.1.1.1'), None) From 33bc7125ea9f50e4d91c57d236ebe334664a4426 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 26 Nov 2013 07:18:10 -0800 Subject: [PATCH 21/66] Removed extra file --- examples/errors.py | 58 ---------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 examples/errors.py diff --git a/examples/errors.py b/examples/errors.py deleted file mode 100644 index c8eb388..0000000 --- a/examples/errors.py +++ /dev/null @@ -1,58 +0,0 @@ -import GeoIP -from nose.tools import assert_equals - - -def test_errors(): - # The intent of these tests is to make sure there are no segfaults. - # Most of the lookups are invalid. Unfortunately the API rarely throws - # exceptions. - gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) - - assert_equals(gi.org_by_addr('1.1.1.1'), None) - - assert_equals(gi.country_code_by_name('1.1.1.1'), None) - - assert_equals(gi.country_name_by_name('1.1.1.1'), None) - - assert_equals(gi.country_code_by_addr('1.1.1.1'), None) - - assert_equals(gi.country_name_by_addr('1.1.1.1'), None) - - assert_equals(gi.name_by_addr('1.1.1.1'), None) - - assert_equals(gi.name_by_name('1.1.1.1'), None) - - assert_equals(gi.org_by_addr('1.1.1.1'), None) - - assert_equals(gi.org_by_name('1.1.1.1'), None) - - assert_equals(gi.region_by_addr('1.1.1.1'), None) - - assert_equals(gi.region_by_name('1.1.1.1'), None) - assert_equals(gi.record_by_addr('1.1.1.1'), None) - - assert_equals(gi.record_by_name('1.1.1.1'), None) - - assert_equals(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) - - assert_equals(gi.charset('1.1.1.1'), 0) - - assert_equals(gi.set_charset(-1), 0) - - assert_equals(gi.last_netmask('1.1.1.1'), 5) - - assert_equals(gi.country_code_by_name_v6('1.1.1.1'), None) - - assert_equals(gi.country_name_by_name_v6('1.1.1.1'), None) - - assert_equals(gi.country_code_by_addr_v6('1.1.1.1'), None) - - assert_equals(gi.country_name_by_addr_v6('1.1.1.1'), None) - - assert_equals(gi.enable_teredo(-100), 1) - assert_equals(gi.teredo('1.1.1.1'), 0) - assert_equals(gi.id_by_addr('1.1.1.1'), 0) - assert_equals(gi.id_by_name('1.1.1.1'), 0) - assert_equals(gi.record_by_addr_v6('1.1.1.1'), None) - - assert_equals(gi.record_by_name_v6('1.1.1.1'), None) From 3faf5a99bec67966d05e98c8a3db5f3322f2c192 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 26 Nov 2013 19:12:05 +0100 Subject: [PATCH 22/66] Make py_GeoIP.c more portable --- py_GeoIP.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 462352e..766eded 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -34,7 +34,7 @@ staticforward PyTypeObject GeoIP_GeoIPType; static PyObject *PyGeoIPError; typedef struct { - PyObject_HEAD; + PyObject_HEAD GeoIP *gi; } GeoIP_GeoIPObject; @@ -606,11 +606,28 @@ static PyObject *GeoIP_GetAttr(PyObject * self, char *attrname) static PyTypeObject GeoIP_GeoIPType = { PyObject_HEAD_INIT(NULL) - .tp_name = "GeoIP", - .tp_doc = "GeoIP database object", - .tp_basicsize = sizeof(GeoIP_GeoIPObject), - .tp_dealloc = GeoIP_GeoIP_dealloc, - .tp_getattr = (getattrfunc)GeoIP_GetAttr, + 0, + "GeoIP", + sizeof(GeoIP_GeoIPObject), + 0, + GeoIP_GeoIP_dealloc, /*tp_dealloc */ + 0, /*tp_print */ + (getattrfunc) GeoIP_GetAttr, /*tp_getattr */ + 0, /*tp_setattr */ + 0, /*tp_compare */ + 0, /*tp_repr */ + 0, /*tp_as_number */ + 0, /*tp_as_sequence */ + 0, /*tp_as_mapping */ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "GeoIP database object", /* tp_doc */ + }; static PyMethodDef GeoIP_Class_methods[] = { From cfba3b54f1318a6827509805f67dfba48f419c44 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Tue, 26 Nov 2013 19:51:14 +0100 Subject: [PATCH 23/66] Initialize everything to please -Wextra --- py_GeoIP.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 766eded..0c6c29c 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -627,7 +627,36 @@ static PyTypeObject GeoIP_GeoIPType = { 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/ "GeoIP database object", /* tp_doc */ - + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ + 0, /* tp_mro */ + 0, /* tp_cache */ + 0, /* tp_subclasses */ + 0 /* tp_weaklist */ +#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 3 + , 0 /* tp_del */ +#endif +#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 6 + , 0 /* tp_version_tag */ +#endif }; static PyMethodDef GeoIP_Class_methods[] = { From bb45f8f78d7de9f7cf00f0317933a9d38efc0112 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 27 Nov 2013 12:02:15 -0800 Subject: [PATCH 24/66] Don't specify library_dirs and include_dirs --- setup.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 2d4807a..bfd47d4 100644 --- a/setup.py +++ b/setup.py @@ -7,9 +7,7 @@ module1 = Extension('GeoIP', libraries=['GeoIP'], - sources=['py_GeoIP.c'], - library_dirs=['/usr/local/lib'], - include_dirs=['/usr/local/include']) + sources=['py_GeoIP.c']) setup( name='GeoIP', From fd8bd7f40730c00d932e5b229e7e99b881be958d Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 27 Nov 2013 13:49:23 -0800 Subject: [PATCH 25/66] Compile against ws2_32 on win32 --- setup.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index bfd47d4..03a4467 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,17 @@ -import multiprocessing +import multiprocessing, sys try: from setuptools import setup, Extension except ImportError: from distutils.core import setup, Extension +libs = ['GeoIP'] + +if sys.platform == 'win32': + libs.append('ws2_32') + module1 = Extension('GeoIP', - libraries=['GeoIP'], + libraries=libs, sources=['py_GeoIP.c']) setup( From 87615c02c67360994696677838aaac181ec0719b Mon Sep 17 00:00:00 2001 From: George Macon Date: Tue, 26 Feb 2013 09:09:50 -0500 Subject: [PATCH 26/66] Expose GEOIP_MMAP_CACHE to python scripts --- py_GeoIP.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py_GeoIP.c b/py_GeoIP.c index 0c6c29c..1eb0fe3 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -722,6 +722,10 @@ DL_EXPORT(void) initGeoIP(void){ PyDict_SetItemString(d, "GEOIP_MEMORY_CACHE", tmp); Py_DECREF(tmp); + tmp = PyInt_FromLong(GEOIP_MMAP_CACHE); + PyDict_SetItemString(d, "GEOIP_MMAP_CACHE", tmp); + Py_DECREF(tmp); + tmp = PyInt_FromLong(GEOIP_CHECK_CACHE); PyDict_SetItemString(d, "GEOIP_CHECK_CACHE", tmp); Py_DECREF(tmp); From 712aef300f8c95770bfea8d777b2c15650fd47fa Mon Sep 17 00:00:00 2001 From: George Macon Date: Tue, 26 Feb 2013 09:19:03 -0500 Subject: [PATCH 27/66] Add test_mmap.py script This is the same as test.py, but it uses GEOIP_MMAP_CACHE --- test_mmap.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100755 test_mmap.py diff --git a/test_mmap.py b/test_mmap.py new file mode 100755 index 0000000..3dbbb51 --- /dev/null +++ b/test_mmap.py @@ -0,0 +1,13 @@ +#!/usr/bin/python + +import GeoIP + +gi = GeoIP.new(GeoIP.GEOIP_MMAP_CACHE) + +print gi.country_code_by_name("yahoo.com") +print gi.last_netmask() +print gi.country_name_by_name("www.bundestag.de") +print gi.country_code_by_addr("24.24.24.24") +print gi.country_name_by_addr("24.24.24.24") +print gi.range_by_ip("68.180.206.184") + From d96bd9c4d8231cb39e7ba6fc243891cc8f681951 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Thu, 12 Dec 2013 13:11:35 +0100 Subject: [PATCH 28/66] Move example into the example folder --- test_mmap.py => examples/test_mmap.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test_mmap.py => examples/test_mmap.py (100%) diff --git a/test_mmap.py b/examples/test_mmap.py similarity index 100% rename from test_mmap.py rename to examples/test_mmap.py From 23919a30276d6402209e38d950d24de7def3e1a0 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 12 Dec 2013 07:23:39 -0800 Subject: [PATCH 29/66] Do the country test for different file modes --- examples/{test_mmap.py => mmap.py} | 0 tests/test_country.py | 15 +++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) rename examples/{test_mmap.py => mmap.py} (100%) diff --git a/examples/test_mmap.py b/examples/mmap.py similarity index 100% rename from examples/test_mmap.py rename to examples/mmap.py diff --git a/tests/test_country.py b/tests/test_country.py index 70cd59b..c5ed47a 100644 --- a/tests/test_country.py +++ b/tests/test_country.py @@ -3,10 +3,13 @@ def test_country(): - gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) + for mode in [GeoIP.GEOIP_STANDARD, GeoIP.GEOIP_MEMORY_CACHE, + GeoIP.GEOIP_MMAP_CACHE, GeoIP.GEOIP_INDEX_CACHE]: + gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.country_code_by_addr('64.17.254.216'), 'US') - assert_equals(gi.last_netmask(), 29) - assert_equals(gi.country_name_by_addr('64.17.254.216'), 'United States') - assert_equals(gi.range_by_ip('64.17.254.216'), - ('64.17.254.216', '64.17.254.223')) + assert_equals(gi.country_code_by_addr('64.17.254.216'), 'US') + assert_equals(gi.last_netmask(), 29) + assert_equals( + gi.country_name_by_addr('64.17.254.216'), 'United States') + assert_equals(gi.range_by_ip('64.17.254.216'), + ('64.17.254.216', '64.17.254.223')) From a9e4a0392b8e84d5816549e0e5d839ec3c9aac18 Mon Sep 17 00:00:00 2001 From: Boris Zentner Date: Thu, 12 Dec 2013 18:35:55 +0100 Subject: [PATCH 30/66] Remove the mmap.py example. It is almost the same as the country example. --- examples/country.py | 1 + examples/mmap.py | 13 ------------- 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100755 examples/mmap.py diff --git a/examples/country.py b/examples/country.py index 774e97a..78e5f80 100644 --- a/examples/country.py +++ b/examples/country.py @@ -3,6 +3,7 @@ import GeoIP #gi = GeoIP.new(GeoIP.GEOIP_STANDARD) +#gi = GeoIP.new(GeoIP.GEOIP_MMAP_CACHE) gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) #gi = GeoIP.open("/usr/local/share/GeoIP/GeoIP.dat",GeoIP.GEOIP_STANDARD) diff --git a/examples/mmap.py b/examples/mmap.py deleted file mode 100755 index 3dbbb51..0000000 --- a/examples/mmap.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/python - -import GeoIP - -gi = GeoIP.new(GeoIP.GEOIP_MMAP_CACHE) - -print gi.country_code_by_name("yahoo.com") -print gi.last_netmask() -print gi.country_name_by_name("www.bundestag.de") -print gi.country_code_by_addr("24.24.24.24") -print gi.country_name_by_addr("24.24.24.24") -print gi.range_by_ip("68.180.206.184") - From 6f8e32f56d64fdedeab7231bb5fa8b1286b96ae4 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Mon, 16 Dec 2013 22:18:13 +0000 Subject: [PATCH 31/66] Add name_by_name_v6/name_by_addr_v6 methods to support the ASNum V6 database --- py_GeoIP.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/py_GeoIP.c b/py_GeoIP.c index 1eb0fe3..61d0168 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -215,6 +215,21 @@ static PyObject *GeoIP_name_by_addr_Py(PyObject * self, PyObject * args) return ret; } +static PyObject *GeoIP_name_by_addr_v6_Py(PyObject * self, PyObject * args) +{ + char *name; + char *org; + PyObject *ret; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + org = GeoIP_name_by_addr_v6(GeoIP->gi, name); + ret = Py_BuildValue("s", org); + free(org); + return ret; +} + static PyObject *GeoIP_name_by_name_Py(PyObject * self, PyObject * args) { char *name; @@ -230,6 +245,21 @@ static PyObject *GeoIP_name_by_name_Py(PyObject * self, PyObject * args) return ret; } +static PyObject *GeoIP_name_by_name_v6_Py(PyObject * self, PyObject * args) +{ + char *name; + char *org; + PyObject *ret; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; + if (!PyArg_ParseTuple(args, "s", &name)) { + return NULL; + } + org = GeoIP_name_by_name_v6(GeoIP->gi, name); + ret = Py_BuildValue("s", org); + free(org); + return ret; +} + static PyObject *GeoIP_id_by_addr_Py(PyObject * self, PyObject * args) { char *name; @@ -580,6 +610,10 @@ static PyMethodDef GeoIP_Object_methods[] = { "Lookup City Region By IP Address" }, { "record_by_name_v6", GeoIP_record_by_name_v6_Py, METH_VARARGS, "Lookup City Region By Name" }, + { "name_by_addr_v6", GeoIP_name_by_addr_v6_Py, METH_VARARGS, + "Lookup IPv6 ASN, Domain, ISP and Organisation By IP Address" }, + { "name_by_name_v6", GeoIP_name_by_name_v6_Py, METH_VARARGS, + "Lookup IPv6 ASN, Domain, ISP and Organisation By Name" }, { NULL, NULL, 0, NULL } }; From 26ef4885ac1d727ed7fd0653b2d8a38ab9be7fec Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 20 Mar 2014 18:45:11 -0400 Subject: [PATCH 32/66] Also ignore GeoIP.egg-info --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f4213a8..f33a981 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ docs/html env/ MANIFEST maxminddb.egg-info/ +GeoIP.egg-info/ pylint.txt valgrind-python.supp violations.pyflakes.txt From f323b7bb3ade92f3241c5a83e491fd364be2dbcd Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 20 Mar 2014 21:03:41 -0400 Subject: [PATCH 33/66] Update Python-CAPI use to 2.6/7 standard. --- py_GeoIP.c | 364 ++++++++++++++++++++++++++--------------------------- 1 file changed, 176 insertions(+), 188 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 61d0168..8251ee7 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -1,4 +1,4 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* py_GeoIP.c * * Copyright (C) 2007 MaxMind LLC @@ -19,6 +19,7 @@ */ #include + #include "GeoIP.h" #include "GeoIPCity.h" @@ -28,67 +29,47 @@ # define UNUSED(x) UNUSED_ ## x #endif -staticforward PyTypeObject GeoIP_GeoIPType; +static PyTypeObject GeoIP_GeoIPType; /* Exception object for python */ -static PyObject *PyGeoIPError; +static PyObject *GeoIP_GeoIPError; typedef struct { PyObject_HEAD GeoIP *gi; } GeoIP_GeoIPObject; -static PyObject *GeoIP_new_Py(PyObject *UNUSED(self), PyObject * args) -{ - GeoIP_GeoIPObject *GeoIP; - int flags; - - if (!PyArg_ParseTuple(args, "i", &flags)) { - return NULL; - } - - GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); - - if (!GeoIP) { - return NULL; - } - - GeoIP->gi = GeoIP_new(flags); - - if (!GeoIP->gi) { - PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); - Py_DECREF(GeoIP); - return NULL; - } - - return (PyObject *)GeoIP; -} - -static PyObject *GeoIP_open_Py(PyObject *UNUSED(self), PyObject * args) +static int +GeoIP_GeoIP_init(PyObject *self, PyObject *args, PyObject *kwargs) { - GeoIP_GeoIPObject *GeoIP; - char *filename; + static char *kwlist[] = { "filename", "flags", NULL }; + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; + char *filename = NULL; int flags; - if (!PyArg_ParseTuple(args, "si", &filename, &flags)) { - return NULL; - } - - GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType); + /* For consistency with the C API, positional arguments are in the + order filename, flags; but it is filename that is optional. */ + if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist+1, &flags)) + GeoIP->gi = GeoIP_new(flags); - if (!GeoIP) { - return NULL; + else { + PyErr_Clear(); + if (PyArg_ParseTupleAndKeywords(args, kwargs, "si", kwlist, + &filename, &flags)) + GeoIP->gi = GeoIP_open(filename, flags); + else + return -1; } - GeoIP->gi = GeoIP_open(filename, flags); - if (!GeoIP->gi) { - PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object"); - Py_DECREF(GeoIP); - return NULL; + /* Failure is probably due to a system-call-level failure. */ + if (!filename) + filename = GeoIPDBFileName[GEOIP_COUNTRY_EDITION]; + PyErr_SetFromErrnoWithFilename(GeoIP_GeoIPError, filename); + return -1; } - return (PyObject *)GeoIP; + return 0; } static void GeoIP_GeoIP_dealloc(PyObject *self) @@ -539,6 +520,26 @@ static PyObject *GeoIP_enable_teredo_Py(PyObject * self, PyObject * args) return Py_BuildValue("i", GeoIP_enable_teredo(GeoIP->gi, teredo)); } + +static PyObject *GeoIP_new_Py(PyObject *UNUSED(self), PyObject * args) +{ + int dummy; + /* enforce calling convention */ + if (!PyArg_ParseTuple(args, "i", &dummy)) + return NULL; + return PyObject_Call((PyObject *)&GeoIP_GeoIPType, args, NULL); +} + +static PyObject *GeoIP_open_Py(PyObject *UNUSED(self), PyObject * args) +{ + char *dummy1; + int dummy2; + /* enforce calling convention */ + if (!PyArg_ParseTuple(args, "si", &dummy1, &dummy2)) + return NULL; + return PyObject_Call((PyObject *)&GeoIP_GeoIPType, args, NULL); +} + static PyObject *GeoIP_lib_version_Py(PyObject *UNUSED(self), PyObject *UNUSED(args)) { @@ -557,7 +558,7 @@ static PyObject *GeoIP_time_zone_by_country_and_region_Py(PyObject *UNUSED(self) region)); } -static PyMethodDef GeoIP_Object_methods[] = { +static PyMethodDef GeoIP_GeoIP_methods[] = { { "country_code_by_name", GeoIP_country_code_by_name_Py, METH_VARARGS, "Lookup Country Code By Name" }, { "country_name_by_name", GeoIP_country_name_by_name_Py, METH_VARARGS, @@ -618,82 +619,81 @@ static PyMethodDef GeoIP_Object_methods[] = { NULL } }; -static PyObject *GeoIP_GetAttr(PyObject * self, char *attrname) +static PyObject *GeoIP_get_GEOIP_STANDARD(PyObject *UNUSED(self), + void *UNUSED(closure)) +{ + return Py_BuildValue("i", GEOIP_STANDARD); +} + +static PyObject *GeoIP_get_database_info(PyObject *self, + void *UNUSED(closure)) { - PyObject *ret; - char *database_info; GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; - if (strcmp(attrname, "GEOIP_STANDARD") == 0) { - return Py_BuildValue("i", 0); - } else if (strcmp(attrname, "database_info") == 0) { - database_info = GeoIP_database_info(GeoIP->gi); - ret = Py_BuildValue("z", database_info); - free(database_info); - return ret; - } else if (strcmp(attrname, "database_edition") == 0) { - return Py_BuildValue("z", - GeoIPDBDescription[GeoIP_database_edition - (GeoIP->gi)]); - } - return Py_FindMethod(GeoIP_Object_methods, self, attrname); + char *database_info = GeoIP_database_info(GeoIP->gi); + PyObject *ret = Py_BuildValue("z", database_info); + free(database_info); + return ret; } +static PyObject *GeoIP_get_database_edition(PyObject *self, + void *UNUSED(closure)) +{ + GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; + return Py_BuildValue("z", + GeoIPDBDescription[GeoIP_database_edition + (GeoIP->gi)]); +} + +static PyGetSetDef GeoIP_GeoIP_getsets[] = { + { "database_edition", GeoIP_get_database_edition, NULL, + "Edition of the database ( country, city, ISP, etc )", NULL }, + { "database_info", GeoIP_get_database_info, NULL, + "Information about the database", NULL }, + { "GEOIP_STANDARD", GeoIP_get_GEOIP_STANDARD, NULL, + "Same as module-level GEOIP_STANDARD constant ( deprecated )", NULL }, + { NULL, NULL, NULL, NULL, NULL } +}; + static PyTypeObject GeoIP_GeoIPType = { - PyObject_HEAD_INIT(NULL) - 0, - "GeoIP", - sizeof(GeoIP_GeoIPObject), - 0, - GeoIP_GeoIP_dealloc, /*tp_dealloc */ - 0, /*tp_print */ - (getattrfunc) GeoIP_GetAttr, /*tp_getattr */ - 0, /*tp_setattr */ - 0, /*tp_compare */ - 0, /*tp_repr */ - 0, /*tp_as_number */ - 0, /*tp_as_sequence */ - 0, /*tp_as_mapping */ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - "GeoIP database object", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - 0, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - 0, /* tp_new */ - 0, /* tp_free */ - 0, /* tp_is_gc */ - 0, /* tp_bases */ - 0, /* tp_mro */ - 0, /* tp_cache */ - 0, /* tp_subclasses */ - 0 /* tp_weaklist */ -#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 3 - , 0 /* tp_del */ -#endif -#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 6 - , 0 /* tp_version_tag */ -#endif + PyVarObject_HEAD_INIT(NULL, 0) + "GeoIP.GeoIP", /*tp_name*/ + sizeof(GeoIP_GeoIPObject), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + GeoIP_GeoIP_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping */ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "GeoIP database object", /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + GeoIP_GeoIP_methods, /*tp_methods*/ + 0, /*tp_members*/ + GeoIP_GeoIP_getsets, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + GeoIP_GeoIP_init, /*tp_init*/ }; -static PyMethodDef GeoIP_Class_methods[] = { +static PyMethodDef GeoIP_module_methods[] = { { "new", GeoIP_new_Py, METH_VARARGS, "GeoIP Constructor" }, @@ -706,89 +706,77 @@ static PyMethodDef GeoIP_Class_methods[] = { { "time_zone_by_country_and_region", GeoIP_time_zone_by_country_and_region_Py, METH_VARARGS, "Returns time_zone for country, region" }, - { NULL, NULL, - 0, - NULL } + { NULL, NULL, 0, NULL } }; -DL_EXPORT(void) initGeoIP(void){ - PyObject *m, *d, *tmp, *ccode, *cname, *ccont, *name; - int i; - const int total_ccodes = sizeof(GeoIP_country_code) / - sizeof(GeoIP_country_code[0]); - GeoIP_GeoIPType.ob_type = &PyType_Type; +PyMODINIT_FUNC initGeoIP(void) +{ + PyObject *m = NULL, *tmp = NULL, *ccode = NULL, + *cname = NULL, *ccont = NULL; + size_t i; + const size_t total_ccodes = sizeof(GeoIP_country_code) / + sizeof(GeoIP_country_code[0]); + + GeoIP_GeoIPType.tp_new = PyType_GenericNew; + if (PyType_Ready(&GeoIP_GeoIPType)) + return; + +#define CHECK(expr) do { if (expr) goto fail; } while (0) +#define CHECK_NULL(expr) do { if (!(expr)) goto fail; } while (0) + + CHECK_NULL(m = Py_InitModule3("GeoIP", GeoIP_module_methods, + "MaxMind GeoIP databases - Python API.")); - m = Py_InitModule("GeoIP", GeoIP_Class_methods); - d = PyModule_GetDict(m); + Py_INCREF(&GeoIP_GeoIPType); + CHECK(PyModule_AddObject(m, "GeoIP", (PyObject *)&GeoIP_GeoIPType)); - PyGeoIPError = PyErr_NewException("py_geoip.error", NULL, NULL); - PyDict_SetItemString(d, "error", PyGeoIPError); + GeoIP_GeoIPError = PyErr_NewException("GeoIP.error", PyExc_OSError, NULL); + Py_INCREF(GeoIP_GeoIPError); + CHECK(PyModule_AddObject(m, "error", GeoIP_GeoIPError)); - ccode = PyTuple_New(total_ccodes); - cname = PyDict_New(); - ccont = PyDict_New(); + CHECK_NULL(ccode = PyTuple_New(total_ccodes)); + CHECK_NULL(cname = PyDict_New()); + CHECK_NULL(ccont = PyDict_New()); for (i = 0; i < total_ccodes; i++) { - name = PyString_FromString(GeoIP_country_code[i]); - PyTuple_SET_ITEM(ccode, i, name); + CHECK_NULL(tmp = PyString_FromString(GeoIP_country_code[i])); + PyTuple_SET_ITEM(ccode, i, tmp); - tmp = PyString_FromString(GeoIP_country_name[i]); - PyDict_SetItem(cname, name, tmp); + CHECK_NULL(tmp = PyString_FromString(GeoIP_country_name[i])); + CHECK(PyDict_SetItemString(cname, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); - tmp = PyString_FromString(GeoIP_country_continent[i]); - PyDict_SetItem(ccont, name, tmp); + CHECK_NULL(tmp = PyString_FromString(GeoIP_country_continent[i])); + CHECK(PyDict_SetItemString(ccont, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); } - - PyDict_SetItemString(d, "country_codes", ccode); - Py_DECREF(ccode); - PyDict_SetItemString(d, "country_names", cname); - Py_DECREF(cname); - PyDict_SetItemString(d, "country_continents", ccont); - Py_DECREF(ccont); - - tmp = PyInt_FromLong(GEOIP_STANDARD); - PyDict_SetItemString(d, "GEOIP_STANDARD", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_MEMORY_CACHE); - PyDict_SetItemString(d, "GEOIP_MEMORY_CACHE", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_MMAP_CACHE); - PyDict_SetItemString(d, "GEOIP_MMAP_CACHE", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_CHECK_CACHE); - PyDict_SetItemString(d, "GEOIP_CHECK_CACHE", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_INDEX_CACHE); - PyDict_SetItemString(d, "GEOIP_INDEX_CACHE", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_CHARSET_ISO_8859_1); - PyDict_SetItemString(d, "GEOIP_CHARSET_ISO_8859_1", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_CHARSET_UTF8); - PyDict_SetItemString(d, "GEOIP_CHARSET_UTF8", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_UNKNOWN_SPEED); - PyDict_SetItemString(d, "GEOIP_UNKNOWN_SPEED", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_DIALUP_SPEED); - PyDict_SetItemString(d, "GEOIP_DIALUP_SPEED", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_CABLEDSL_SPEED); - PyDict_SetItemString(d, "GEOIP_CABLEDSL_SPEED", tmp); - Py_DECREF(tmp); - - tmp = PyInt_FromLong(GEOIP_CORPORATE_SPEED); - PyDict_SetItemString(d, "GEOIP_CORPORATE_SPEED", tmp); - Py_DECREF(tmp); + tmp = NULL; + + CHECK(PyModule_AddObject(m, "country_codes", ccode)); + 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)); + CHECK(PyModule_AddIntMacro(m, GEOIP_CHECK_CACHE)); + CHECK(PyModule_AddIntMacro(m, GEOIP_INDEX_CACHE)); + CHECK(PyModule_AddIntMacro(m, GEOIP_CHARSET_ISO_8859_1)); + CHECK(PyModule_AddIntMacro(m, GEOIP_CHARSET_UTF8)); + CHECK(PyModule_AddIntMacro(m, GEOIP_UNKNOWN_SPEED)); + CHECK(PyModule_AddIntMacro(m, GEOIP_DIALUP_SPEED)); + CHECK(PyModule_AddIntMacro(m, GEOIP_CABLEDSL_SPEED)); + CHECK(PyModule_AddIntMacro(m, GEOIP_CORPORATE_SPEED)); + return; + +#undef CHECK +#undef CHECK_NULL + + fail: + Py_XDECREF(ccode); + Py_XDECREF(cname); + Py_XDECREF(ccont); + Py_XDECREF(tmp); + Py_XDECREF(m); + Py_XDECREF(GeoIP_GeoIPError); GeoIP_GeoIPError = NULL; } From 336d4156a558c682cc645a066c194595de07e7e6 Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Thu, 20 Mar 2014 22:29:41 -0400 Subject: [PATCH 34/66] Python 3 support. --- py_GeoIP.c | 60 +++++++++++++++++++++++++++++++------- setup.py | 6 ++-- tests/test_city.py | 4 +-- tests/test_country.py | 10 +++---- tests/test_domain.py | 4 +-- tests/test_errors.py | 56 +++++++++++++++++------------------ tests/test_netspeedcell.py | 4 +-- tests/test_org.py | 4 +-- tests/test_region.py | 4 +-- tests/test_v6.py | 4 +-- 10 files changed, 98 insertions(+), 58 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 8251ee7..d938310 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -29,6 +29,10 @@ # define UNUSED(x) UNUSED_ ## x #endif +#if PY_MAJOR_VERSION >= 3 + # define PyString_FromString(s) PyUnicode_FromString(s) +#endif + static PyTypeObject GeoIP_GeoIPType; /* Exception object for python */ @@ -709,24 +713,23 @@ static PyMethodDef GeoIP_module_methods[] = { { NULL, NULL, 0, NULL } }; -PyMODINIT_FUNC initGeoIP(void) +/* Code shared between 2.x and 3.x module initialization. */ +static int +GeoIP_populate_module(PyObject *m) { - PyObject *m = NULL, *tmp = NULL, *ccode = NULL, - *cname = NULL, *ccont = NULL; + PyObject *tmp = NULL; + PyObject *ccode = NULL; + PyObject *cname = NULL; + PyObject *ccont = NULL; size_t i; const size_t total_ccodes = sizeof(GeoIP_country_code) / sizeof(GeoIP_country_code[0]); - GeoIP_GeoIPType.tp_new = PyType_GenericNew; - if (PyType_Ready(&GeoIP_GeoIPType)) - return; - #define CHECK(expr) do { if (expr) goto fail; } while (0) #define CHECK_NULL(expr) do { if (!(expr)) goto fail; } while (0) - CHECK_NULL(m = Py_InitModule3("GeoIP", GeoIP_module_methods, - "MaxMind GeoIP databases - Python API.")); - + GeoIP_GeoIPType.tp_new = PyType_GenericNew; + CHECK(PyType_Ready(&GeoIP_GeoIPType)); Py_INCREF(&GeoIP_GeoIPType); CHECK(PyModule_AddObject(m, "GeoIP", (PyObject *)&GeoIP_GeoIPType)); @@ -767,7 +770,8 @@ PyMODINIT_FUNC initGeoIP(void) CHECK(PyModule_AddIntMacro(m, GEOIP_DIALUP_SPEED)); CHECK(PyModule_AddIntMacro(m, GEOIP_CABLEDSL_SPEED)); CHECK(PyModule_AddIntMacro(m, GEOIP_CORPORATE_SPEED)); - return; + + return 0; #undef CHECK #undef CHECK_NULL @@ -779,4 +783,38 @@ PyMODINIT_FUNC initGeoIP(void) Py_XDECREF(tmp); Py_XDECREF(m); Py_XDECREF(GeoIP_GeoIPError); GeoIP_GeoIPError = NULL; + return -1; +} + +#if PY_MAJOR_VERSION < 3 + +PyMODINIT_FUNC initGeoIP(void) +{ + PyObject *m = Py_InitModule3("GeoIP", + GeoIP_module_methods, + "MaxMind GeoIP databases - Python API."); + if (m) + GeoIP_populate_module(m); } + +#else + +static PyModuleDef GeoIP_module = { + PyModuleDef_HEAD_INIT, + "GeoIP", + "MaxMind GeoIP databases - Python API", + -1, + GeoIP_module_methods +}; + +PyMODINIT_FUNC PyInit_GeoIP(void) +{ + PyObject *m = PyModule_Create(&GeoIP_module); + if (!m) + return NULL; + if (GeoIP_populate_module(m)) + return NULL; + return m; +} + +#endif diff --git a/setup.py b/setup.py index 03a4467..6472880 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -import multiprocessing, sys +import sys try: from setuptools import setup, Extension @@ -17,7 +17,7 @@ setup( name='GeoIP', version='1.2.9', - description='MaxMind GeoIP Legacy Extension', + description='MaxMind GeoIP Database - Python API', long_description=open('README.rst').read(), author="MaxMind, Inc.", author_email="support@maxmind.com", @@ -35,6 +35,8 @@ 'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', 'Programming Language :: Python', 'Topic :: Internet :: Proxy Servers', 'Topic :: Internet', diff --git a/tests/test_city.py b/tests/test_city.py index 2be9e92..4d2866b 100644 --- a/tests/test_city.py +++ b/tests/test_city.py @@ -1,5 +1,5 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_city(): @@ -23,4 +23,4 @@ def test_city(): 'time_zone': 'America/Los_Angeles' } - assert_equals(gir, record) + assert_equal(gir, record) diff --git a/tests/test_country.py b/tests/test_country.py index c5ed47a..2c6761a 100644 --- a/tests/test_country.py +++ b/tests/test_country.py @@ -1,5 +1,5 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_country(): @@ -7,9 +7,9 @@ def test_country(): GeoIP.GEOIP_MMAP_CACHE, GeoIP.GEOIP_INDEX_CACHE]: gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.country_code_by_addr('64.17.254.216'), 'US') - assert_equals(gi.last_netmask(), 29) - assert_equals( + assert_equal(gi.country_code_by_addr('64.17.254.216'), 'US') + assert_equal(gi.last_netmask(), 29) + assert_equal( gi.country_name_by_addr('64.17.254.216'), 'United States') - assert_equals(gi.range_by_ip('64.17.254.216'), + assert_equal(gi.range_by_ip('64.17.254.216'), ('64.17.254.216', '64.17.254.223')) diff --git a/tests/test_domain.py b/tests/test_domain.py index 8a6a6f1..17eaa86 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -1,8 +1,8 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_domain(): gi = GeoIP.open("tests/data/GeoIPDomain.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.org_by_addr("67.43.156.0"), 'shoesfin.NET') + assert_equal(gi.org_by_addr("67.43.156.0"), 'shoesfin.NET') diff --git a/tests/test_errors.py b/tests/test_errors.py index 795f521..c4caa3e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,5 +1,5 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_errors(): @@ -8,51 +8,51 @@ def test_errors(): # exceptions. gi = GeoIP.open("tests/data/GeoIP.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.org_by_addr('1.1.1.1'), None) + assert_equal(gi.org_by_addr('1.1.1.1'), None) - assert_equals(gi.country_code_by_name('1.1.1.1'), None) + assert_equal(gi.country_code_by_name('1.1.1.1'), None) - assert_equals(gi.country_name_by_name('1.1.1.1'), None) + assert_equal(gi.country_name_by_name('1.1.1.1'), None) - assert_equals(gi.country_code_by_addr('1.1.1.1'), None) + assert_equal(gi.country_code_by_addr('1.1.1.1'), None) - assert_equals(gi.country_name_by_addr('1.1.1.1'), None) + assert_equal(gi.country_name_by_addr('1.1.1.1'), None) - assert_equals(gi.name_by_addr('1.1.1.1'), None) + assert_equal(gi.name_by_addr('1.1.1.1'), None) - assert_equals(gi.name_by_name('1.1.1.1'), None) + assert_equal(gi.name_by_name('1.1.1.1'), None) - assert_equals(gi.org_by_addr('1.1.1.1'), None) + assert_equal(gi.org_by_addr('1.1.1.1'), None) - assert_equals(gi.org_by_name('1.1.1.1'), None) + assert_equal(gi.org_by_name('1.1.1.1'), None) - assert_equals(gi.region_by_addr('1.1.1.1'), None) + assert_equal(gi.region_by_addr('1.1.1.1'), None) - assert_equals(gi.region_by_name('1.1.1.1'), None) - assert_equals(gi.record_by_addr('1.1.1.1'), None) + assert_equal(gi.region_by_name('1.1.1.1'), None) + assert_equal(gi.record_by_addr('1.1.1.1'), None) - assert_equals(gi.record_by_name('1.1.1.1'), None) + assert_equal(gi.record_by_name('1.1.1.1'), None) - assert_equals(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) + assert_equal(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) - assert_equals(gi.charset(), 0) + assert_equal(gi.charset(), 0) - assert_equals(gi.set_charset(-1), 0) + assert_equal(gi.set_charset(-1), 0) - assert_equals(gi.last_netmask(), 5) + assert_equal(gi.last_netmask(), 5) - assert_equals(gi.country_code_by_name_v6('1.1.1.1'), None) + assert_equal(gi.country_code_by_name_v6('1.1.1.1'), None) - assert_equals(gi.country_name_by_name_v6('1.1.1.1'), None) + assert_equal(gi.country_name_by_name_v6('1.1.1.1'), None) - assert_equals(gi.country_code_by_addr_v6('1.1.1.1'), None) + assert_equal(gi.country_code_by_addr_v6('1.1.1.1'), None) - assert_equals(gi.country_name_by_addr_v6('1.1.1.1'), None) + assert_equal(gi.country_name_by_addr_v6('1.1.1.1'), None) - assert_equals(gi.enable_teredo(-100), 1) - assert_equals(gi.teredo(), 0) - assert_equals(gi.id_by_addr('1.1.1.1'), 0) - assert_equals(gi.id_by_name('1.1.1.1'), 0) - assert_equals(gi.record_by_addr_v6('1.1.1.1'), None) + assert_equal(gi.enable_teredo(-100), 1) + assert_equal(gi.teredo(), 0) + assert_equal(gi.id_by_addr('1.1.1.1'), 0) + assert_equal(gi.id_by_name('1.1.1.1'), 0) + assert_equal(gi.record_by_addr_v6('1.1.1.1'), None) - assert_equals(gi.record_by_name_v6('1.1.1.1'), None) + assert_equal(gi.record_by_name_v6('1.1.1.1'), None) diff --git a/tests/test_netspeedcell.py b/tests/test_netspeedcell.py index 9b315ad..7a1b797 100644 --- a/tests/test_netspeedcell.py +++ b/tests/test_netspeedcell.py @@ -1,8 +1,8 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_netspeed_cell(): gi = GeoIP.open("tests/data/GeoIPNetSpeedCell.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.org_by_addr("2.125.160.1"), 'Dialup') + assert_equal(gi.org_by_addr("2.125.160.1"), 'Dialup') diff --git a/tests/test_org.py b/tests/test_org.py index 73f5778..865b698 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -1,8 +1,8 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_org(): gi = GeoIP.open("tests/data/GeoIPOrg.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.org_by_addr("12.87.118.0"), 'AT&T Worldnet Services') + assert_equal(gi.org_by_addr("12.87.118.0"), 'AT&T Worldnet Services') diff --git a/tests/test_region.py b/tests/test_region.py index 9f953dd..459ea4c 100644 --- a/tests/test_region.py +++ b/tests/test_region.py @@ -1,5 +1,5 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_region(): @@ -12,4 +12,4 @@ def test_region(): 'region_name': 'California', 'country_code': 'US' } - assert_equals(gir, region) + assert_equal(gir, region) diff --git a/tests/test_v6.py b/tests/test_v6.py index d2000df..0e8f7ed 100644 --- a/tests/test_v6.py +++ b/tests/test_v6.py @@ -1,8 +1,8 @@ import GeoIP -from nose.tools import assert_equals +from nose.tools import assert_equal def test_ipv6(): gi = GeoIP.open("tests/data/GeoIPv6.dat", GeoIP.GEOIP_STANDARD) - assert_equals(gi.country_code_by_addr_v6('2001:200::'), 'JP') + assert_equal(gi.country_code_by_addr_v6('2001:200::'), 'JP') From 2b62b53b080f0510e993d323e4a468cfe91be08e Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Fri, 21 Mar 2014 15:33:49 -0400 Subject: [PATCH 35/66] Add more initializers to make -Wmissing-field-initializers happy. --- py_GeoIP.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index d938310..de53a22 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -695,6 +695,23 @@ static PyTypeObject GeoIP_GeoIPType = { 0, /*tp_descr_set*/ 0, /*tp_dictoffset*/ GeoIP_GeoIP_init, /*tp_init*/ + 0, /*tp_alloc*/ + 0, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ + 0, /*tp_version_tag*/ +#if PY_MAJOR_VERSION >= 4 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4) + 0, /*tp_finalize*/ +#endif + + /* More fields are present #ifdef COUNT_ALLOCS, but object.h + specifically says not to explicitly initialize them. */ }; static PyMethodDef GeoIP_module_methods[] = { @@ -800,11 +817,15 @@ PyMODINIT_FUNC initGeoIP(void) #else static PyModuleDef GeoIP_module = { - PyModuleDef_HEAD_INIT, - "GeoIP", - "MaxMind GeoIP databases - Python API", - -1, - GeoIP_module_methods + PyModuleDef_HEAD_INIT, /*m_base*/ + "GeoIP", /*m_name*/ + "MaxMind GeoIP databases - Python API", /*m_doc*/ + -1, /*m_size*/ + GeoIP_module_methods, /*m_methods*/ + 0, /*m_reload*/ + 0, /*m_traverse*/ + 0, /*m_clear*/ + 0, /*m_free*/ }; PyMODINIT_FUNC PyInit_GeoIP(void) From 617a8d84da021f24c82b50c3af400b4eb2baa405 Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Fri, 21 Mar 2014 15:43:18 -0400 Subject: [PATCH 36/66] Further adjustments to restore Python 2.5 support. Also flip one piece of compatibility logic around, so the ifdefs at the top of the file consistently define new stuff in terms of old stuff when necessary, allowing the bulk of the code to be written new-style. --- py_GeoIP.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index de53a22..7923704 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -29,8 +29,19 @@ # define UNUSED(x) UNUSED_ ## x #endif -#if PY_MAJOR_VERSION >= 3 - # define PyString_FromString(s) PyUnicode_FromString(s) +/* Python 2.x compatibility: in all the places PyUnicode_FromString is used, + we actually want whatever the default str() for that Python is */ +#if PY_MAJOR_VERSION <= 2 + # undef PyUnicode_FromString + # define PyUnicode_FromString(s) PyString_FromString(s) +#endif + +/* Python 2.5 compatibility */ +#ifndef PyVarObject_HEAD_INIT + # define PyVarObject_HEAD_INIT(x,y) PyObject_HEAD_INIT(x) y, +#endif +#ifndef PyModule_AddIntMacro + # define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c) #endif static PyTypeObject GeoIP_GeoIPType; @@ -705,9 +716,11 @@ static PyTypeObject GeoIP_GeoIPType = { 0, /*tp_subclasses*/ 0, /*tp_weaklist*/ 0, /*tp_del*/ +#if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6) 0, /*tp_version_tag*/ #if PY_MAJOR_VERSION >= 4 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4) 0, /*tp_finalize*/ +#endif #endif /* More fields are present #ifdef COUNT_ALLOCS, but object.h @@ -759,14 +772,14 @@ GeoIP_populate_module(PyObject *m) CHECK_NULL(ccont = PyDict_New()); for (i = 0; i < total_ccodes; i++) { - CHECK_NULL(tmp = PyString_FromString(GeoIP_country_code[i])); + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_code[i])); PyTuple_SET_ITEM(ccode, i, tmp); - CHECK_NULL(tmp = PyString_FromString(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 = PyString_FromString(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); } From 3b4ed13dec8aa4603fa5425f93534c522a2589cc Mon Sep 17 00:00:00 2001 From: Zack Weinberg Date: Fri, 21 Mar 2014 15:47:24 -0400 Subject: [PATCH 37/66] Ignore local nose eggs. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f33a981..add68a0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ env/ MANIFEST maxminddb.egg-info/ GeoIP.egg-info/ +nose*.egg/ pylint.txt valgrind-python.supp violations.pyflakes.txt From 60dfd5aa5bd1dc36c046590ad1800ea066a475f0 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:06:03 -0700 Subject: [PATCH 38/66] Uncrustify --- py_GeoIP.c | 144 ++++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 69 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 7923704..0186ce9 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -38,10 +38,10 @@ /* Python 2.5 compatibility */ #ifndef PyVarObject_HEAD_INIT - # define PyVarObject_HEAD_INIT(x,y) PyObject_HEAD_INIT(x) y, + # define PyVarObject_HEAD_INIT(x, y) PyObject_HEAD_INIT(x) y, #endif #ifndef PyModule_AddIntMacro - # define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c) + # define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, # c, c) #endif static PyTypeObject GeoIP_GeoIPType; @@ -64,22 +64,23 @@ GeoIP_GeoIP_init(PyObject *self, PyObject *args, PyObject *kwargs) /* For consistency with the C API, positional arguments are in the order filename, flags; but it is filename that is optional. */ - if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist+1, &flags)) + if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist + 1, &flags)) { GeoIP->gi = GeoIP_new(flags); - - else { + } else{ PyErr_Clear(); if (PyArg_ParseTupleAndKeywords(args, kwargs, "si", kwlist, - &filename, &flags)) + &filename, &flags)) { GeoIP->gi = GeoIP_open(filename, flags); - else + } else{ return -1; + } } if (!GeoIP->gi) { /* Failure is probably due to a system-call-level failure. */ - if (!filename) + if (!filename) { filename = GeoIPDBFileName[GEOIP_COUNTRY_EDITION]; + } PyErr_SetFromErrnoWithFilename(GeoIP_GeoIPError, filename); return -1; } @@ -540,8 +541,9 @@ static PyObject *GeoIP_new_Py(PyObject *UNUSED(self), PyObject * args) { int dummy; /* enforce calling convention */ - if (!PyArg_ParseTuple(args, "i", &dummy)) + if (!PyArg_ParseTuple(args, "i", &dummy)) { return NULL; + } return PyObject_Call((PyObject *)&GeoIP_GeoIPType, args, NULL); } @@ -550,8 +552,9 @@ static PyObject *GeoIP_open_Py(PyObject *UNUSED(self), PyObject * args) char *dummy1; int dummy2; /* enforce calling convention */ - if (!PyArg_ParseTuple(args, "si", &dummy1, &dummy2)) + if (!PyArg_ParseTuple(args, "si", &dummy1, &dummy2)) { return NULL; + } return PyObject_Call((PyObject *)&GeoIP_GeoIPType, args, NULL); } @@ -582,9 +585,9 @@ static PyMethodDef GeoIP_GeoIP_methods[] = { "Lookup Country Code By IP Address" }, { "country_name_by_addr", GeoIP_country_name_by_addr_Py, METH_VARARGS, "Lookup Country Name By IP Address" }, - { "name_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, + { "name_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, "Lookup ASN, Domain, ISP and Organisation By IP Address" }, - { "name_by_name", GeoIP_name_by_name_Py, METH_VARARGS, + { "name_by_name", GeoIP_name_by_name_Py, METH_VARARGS, "Lookup ASN, Domain, ISP and Organisation By Name" }, { "org_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, "Lookup ASN, Domain, ISP and Organisation By IP Address ( deprecated use name_by_addr instead )" }, @@ -656,70 +659,70 @@ static PyObject *GeoIP_get_database_edition(PyObject *self, GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("z", GeoIPDBDescription[GeoIP_database_edition - (GeoIP->gi)]); + (GeoIP->gi)]); } static PyGetSetDef GeoIP_GeoIP_getsets[] = { { "database_edition", GeoIP_get_database_edition, NULL, "Edition of the database ( country, city, ISP, etc )", NULL }, - { "database_info", GeoIP_get_database_info, NULL, + { "database_info", GeoIP_get_database_info, NULL, "Information about the database", NULL }, - { "GEOIP_STANDARD", GeoIP_get_GEOIP_STANDARD, NULL, + { "GEOIP_STANDARD", GeoIP_get_GEOIP_STANDARD, NULL, "Same as module-level GEOIP_STANDARD constant ( deprecated )", NULL }, - { NULL, NULL, NULL, NULL, NULL } + { NULL, NULL, NULL,NULL,NULL } }; static PyTypeObject GeoIP_GeoIPType = { PyVarObject_HEAD_INIT(NULL, 0) - "GeoIP.GeoIP", /*tp_name*/ - sizeof(GeoIP_GeoIPObject), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - GeoIP_GeoIP_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping */ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - "GeoIP database object", /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - GeoIP_GeoIP_methods, /*tp_methods*/ - 0, /*tp_members*/ - GeoIP_GeoIP_getsets, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - GeoIP_GeoIP_init, /*tp_init*/ - 0, /*tp_alloc*/ - 0, /*tp_new*/ - 0, /*tp_free*/ - 0, /*tp_is_gc*/ - 0, /*tp_bases*/ - 0, /*tp_mro*/ - 0, /*tp_cache*/ - 0, /*tp_subclasses*/ - 0, /*tp_weaklist*/ - 0, /*tp_del*/ + "GeoIP.GeoIP", /*tp_name*/ + sizeof(GeoIP_GeoIPObject), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + GeoIP_GeoIP_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping */ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "GeoIP database object", /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + GeoIP_GeoIP_methods, /*tp_methods*/ + 0, /*tp_members*/ + GeoIP_GeoIP_getsets, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + GeoIP_GeoIP_init, /*tp_init*/ + 0, /*tp_alloc*/ + 0, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6) - 0, /*tp_version_tag*/ + 0, /*tp_version_tag*/ #if PY_MAJOR_VERSION >= 4 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4) - 0, /*tp_finalize*/ + 0, /*tp_finalize*/ #endif #endif @@ -740,7 +743,7 @@ static PyMethodDef GeoIP_module_methods[] = { { "time_zone_by_country_and_region", GeoIP_time_zone_by_country_and_region_Py, METH_VARARGS, "Returns time_zone for country, region" }, - { NULL, NULL, 0, NULL } + { NULL, NULL, 0,NULL } }; /* Code shared between 2.x and 3.x module initialization. */ @@ -755,8 +758,8 @@ GeoIP_populate_module(PyObject *m) const size_t total_ccodes = sizeof(GeoIP_country_code) / sizeof(GeoIP_country_code[0]); -#define CHECK(expr) do { if (expr) goto fail; } while (0) -#define CHECK_NULL(expr) do { if (!(expr)) goto fail; } while (0) +#define CHECK(expr) do { if (expr) { goto fail; } } while (0) +#define CHECK_NULL(expr) do { if (!(expr)) { goto fail; } } while (0) GeoIP_GeoIPType.tp_new = PyType_GenericNew; CHECK(PyType_Ready(&GeoIP_GeoIPType)); @@ -823,8 +826,9 @@ PyMODINIT_FUNC initGeoIP(void) PyObject *m = Py_InitModule3("GeoIP", GeoIP_module_methods, "MaxMind GeoIP databases - Python API."); - if (m) + if (m) { GeoIP_populate_module(m); + } } #else @@ -844,10 +848,12 @@ static PyModuleDef GeoIP_module = { PyMODINIT_FUNC PyInit_GeoIP(void) { PyObject *m = PyModule_Create(&GeoIP_module); - if (!m) + if (!m) { return NULL; - if (GeoIP_populate_module(m)) + } + if (GeoIP_populate_module(m)) { return NULL; + } return m; } From b41bfca14f78f91ab0965c30dccb7febd38fbc2b Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:07:11 -0700 Subject: [PATCH 39/66] Build for 3.3 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index d06a695..4d96190 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: python python: - 2.6 - 2.7 + - 3.3 before_install: - git clone git://github.com/maxmind/geoip-api-c From 0c49d17ad27d57051070e0d86e65a89e07fb8ce8 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:07:34 -0700 Subject: [PATCH 40/66] Add Legacy to description --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 6472880..c4d61dc 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ setup( name='GeoIP', version='1.2.9', - description='MaxMind GeoIP Database - Python API', + description='MaxMind GeoIP Legacy Database - Python API', long_description=open('README.rst').read(), author="MaxMind, Inc.", author_email="support@maxmind.com", From 0a26089d6f2dc0f7611386a3ba0252370978288e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:20:49 -0700 Subject: [PATCH 41/66] Prepare for 1.3.0 --- ChangeLog.md | 10 ++++++++++ setup.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index fe70557..9b9239f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,16 @@ # Change Log +## 1.3.0 (2014-03-21) + +* Python 3 support and significant code cleanup. ( Zack Weinberg ) +* The methods `name_by_addr`, `name_by_addr_v6`, `name_by_name`, and + `name_by_name_v6` were added for ISP, Org, and ASNum lookups. + ( Carlos Rodrigues ) +* Portability fixes. +* Expose `GEOIP_MMAP_CACHE` from the C library. ( George Macon ) +* Unit tests were added. + ## 1.2.9 (2013-11-04) * Packaging fix. PyPI-only release. diff --git a/setup.py b/setup.py index c4d61dc..60e665a 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setup( name='GeoIP', - version='1.2.9', + version='1.3.0', description='MaxMind GeoIP Legacy Database - Python API', long_description=open('README.rst').read(), author="MaxMind, Inc.", From 510257e3b9372653bfc1b90f674f98e98c45c1d6 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:21:31 -0700 Subject: [PATCH 42/66] Added Uncrustify config --- .uncrustify.cfg | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 .uncrustify.cfg diff --git a/.uncrustify.cfg b/.uncrustify.cfg new file mode 100644 index 0000000..46b4428 --- /dev/null +++ b/.uncrustify.cfg @@ -0,0 +1,78 @@ +# +# based on uncrustify config file for the linux kernel +# + +code_width = 80 +indent_case_brace = 4 +indent_columns = 4 +indent_label = 2 # pos: absolute col, neg: relative column +indent_with_tabs = 0 + +# +# inter-symbol newlines +# +nl_brace_else = remove # "} else" vs "} \n else" - cuddle else +nl_brace_while = remove # "} while" vs "} \n while" - cuddle while +nl_do_brace = remove # "do {" vs "do \n {" +nl_else_brace = remove # "else {" vs "else \n {" +nl_enum_brace = remove # "enum {" vs "enum \n {" +nl_fcall_brace = remove # "list_for_each() {" vs "list_for_each()\n{" +nl_fdef_brace = force # "int foo() {" vs "int foo()\n{" +nl_for_brace = remove # "for () {" vs "for () \n {" +nl_func_var_def_blk = 0 # don't add newlines after a block of var declarations +nl_if_brace = remove # "if () {" vs "if () \n {" +nl_multi_line_define = true +nl_struct_brace = remove # "struct {" vs "struct \n {" +nl_switch_brace = remove # "switch () {" vs "switch () \n {" +nl_union_brace = remove # "union {" vs "union \n {" +nl_while_brace = remove # "while () {" vs "while () \n {" + + +# +# Source code modifications +# +mod_full_brace_do = force # "do a--; while ();" vs "do { a--; } while ();" +mod_full_brace_for = force # "for () a--;" vs "for () { a--; }" +mod_full_brace_if = force # "if (a) a--;" vs "if (a) { a--; }" +mod_full_brace_nl = 3 # don't remove if more than 3 newlines +mod_full_brace_while = force # "while (a) a--;" vs "while (a) { a--; }" +mod_paren_on_return = remove # "return 1;" vs "return (1);" + + +# +# inter-character spacing options +# +sp_after_cast = remove # "(int) a" vs "(int)a" +sp_after_comma = force +sp_after_sparen = force # "if () {" vs "if (){" +sp_arith = force +sp_assign = force +sp_assign = force +sp_before_comma = remove +sp_before_ptr_star = force # "char *foo" vs "char* foo +sp_before_sparen = force # "if (" vs "if(" +sp_between_ptr_star = remove # "char * *foo" vs "char **foo" +sp_bool = force +sp_compare = force +sp_func_call_paren = remove # "foo (" vs "foo(" +sp_func_def_paren = remove # "int foo (){" vs "int foo(){" +sp_func_proto_paren = remove # "int foo ();" vs "int foo();" +sp_inside_braces = force # "{ 1 }" vs "{1}" +sp_inside_braces_enum = force # "{ 1 }" vs "{1}" +sp_inside_braces_struct = force # "{ 1 }" vs "{1}" +sp_inside_sparen = remove +sp_paren_brace = force +sp_sizeof_paren = remove # "sizeof (int)" vs "sizeof(int)" + +# +# Aligning stuff +# +align_enum_equ_span = 4 # '=' in enum definition +align_nl_cont = true +align_on_tabstop = FALSE # align on tabstops +align_right_cmt_span = 3 +align_struct_init_span = 1 +align_struct_init_span = 3 # align stuff in a structure init '= { }' +align_var_def_star_style = 2 # void *foo; +align_var_struct_span = 0 +align_with_tabs = FALSE # use tabs to align From 5e2329b3dcc86331784f3299895c661b45c6d8a2 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:23:50 -0700 Subject: [PATCH 43/66] Updated docs --- README.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 82d8520..2455b39 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,7 @@ MaxMind GeoIP Legacy Python Extension API Requirements ------------ -* Python 2.x +* Python 2.5+ or 3.3+ * GeoIP C Library 1.4.7 or greater Installation @@ -27,7 +27,7 @@ From source: Usage ----- -See the examples in `examples/`. +See the examples in the ``examples`` folder. Troubleshooting --------------- @@ -39,7 +39,7 @@ directory" error, add /usr/local/lib to /etc/ld.so.conf then run License ------- -Copyright (c) 2013 MaxMind LLC +Copyright (c) 2014 MaxMind LLC All rights reserved. This package is free software; it is licensed under the LGPL 2.1 or greater. From dfcaf8dffe98ce1e469fa5f960603a75c72f9123 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 13:40:39 -0700 Subject: [PATCH 44/66] Made examples Python 3 compatible --- examples/city.py | 32 +++++++++++++++++--------------- examples/country.py | 15 ++++++++------- examples/domain.py | 6 ++++-- examples/netspeed.py | 11 +++++++---- examples/netspeedcell.py | 11 +++++++---- examples/org.py | 10 ++++++---- examples/region.py | 21 +++++++++++---------- examples/v6.py | 13 +++++++------ 8 files changed, 67 insertions(+), 52 deletions(-) diff --git a/examples/city.py b/examples/city.py index b0b3f76..00d9946 100644 --- a/examples/city.py +++ b/examples/city.py @@ -1,23 +1,25 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat", GeoIP.GEOIP_STANDARD) gir = gi.record_by_name("www.google.com") #gir = gi.record_by_addr("24.24.24.24") -if gir != None: - print gir['country_code'] - print gir['country_code3'] - print gir['country_name'] - print gir['city'] - print gir['region'] - print gir['region_name'] - print gir['postal_code'] - print gir['latitude'] - print gir['longitude'] - print gir['area_code'] - print gir['time_zone'] - print gir['metro_code'] - print str(gir) +if gir is not None: + print(gir['country_code']) + print(gir['country_code3']) + print(gir['country_name']) + print(gir['city']) + print(gir['region']) + print(gir['region_name']) + print(gir['postal_code']) + print(gir['latitude']) + print(gir['longitude']) + print(gir['area_code']) + print(gir['time_zone']) + print(gir['metro_code']) + print(str(gir)) diff --git a/examples/country.py b/examples/country.py index 78e5f80..5f2b42b 100644 --- a/examples/country.py +++ b/examples/country.py @@ -1,5 +1,7 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP #gi = GeoIP.new(GeoIP.GEOIP_STANDARD) @@ -7,10 +9,9 @@ gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) #gi = GeoIP.open("/usr/local/share/GeoIP/GeoIP.dat",GeoIP.GEOIP_STANDARD) -print gi.country_code_by_name("yahoo.com") -print gi.last_netmask() -print gi.country_name_by_name("www.bundestag.de") -print gi.country_code_by_addr("24.24.24.24") -print gi.country_name_by_addr("24.24.24.24") -print gi.range_by_ip("68.180.206.184") - +print(gi.country_code_by_name("yahoo.com")) +print(gi.last_netmask()) +print(gi.country_name_by_name("www.bundestag.de")) +print(gi.country_code_by_addr("24.24.24.24")) +print(gi.country_name_by_addr("24.24.24.24")) +print(gi.range_by_ip("68.180.206.184")) diff --git a/examples/domain.py b/examples/domain.py index f5ab6a6..05d82e7 100644 --- a/examples/domain.py +++ b/examples/domain.py @@ -1,7 +1,9 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPDomain.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPDomain.dat", GeoIP.GEOIP_STANDARD) -print gi.org_by_addr("24.24.24.24") +print(gi.org_by_addr("24.24.24.24")) diff --git a/examples/netspeed.py b/examples/netspeed.py index 3665523..4490f21 100644 --- a/examples/netspeed.py +++ b/examples/netspeed.py @@ -1,12 +1,15 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPNetspeed.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open( + "/usr/local/share/GeoIP/GeoIPNetspeed.dat", GeoIP.GEOIP_STANDARD) # GEOIP_UNKNOWN_SPEED, GEOIP_DIALUP_SPEED, GEOIP_CABLEDSL_SPEED or # GEOIP_CORPORATE_SPEED -print gi.id_by_name("yahoo.com") -print gi.id_by_name("www.maxmind.com") == GeoIP.GEOIP_UNKNOWN_SPEED -print gi.id_by_addr("203.195.93.0") +print(gi.id_by_name("yahoo.com")) +print(gi.id_by_name("www.maxmind.com") == GeoIP.GEOIP_UNKNOWN_SPEED) +print(gi.id_by_addr("203.195.93.0")) diff --git a/examples/netspeedcell.py b/examples/netspeedcell.py index 102d589..0a258dd 100644 --- a/examples/netspeedcell.py +++ b/examples/netspeedcell.py @@ -1,9 +1,12 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPNetSpeedCell.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open( + "/usr/local/share/GeoIP/GeoIPNetSpeedCell.dat", GeoIP.GEOIP_STANDARD) -print gi.org_by_name("yahoo.com") -print gi.org_by_name("www.google.com") -print gi.org_by_addr("24.24.24.24") +print(gi.org_by_name("yahoo.com")) +print(gi.org_by_name("www.google.com")) +print(gi.org_by_addr("24.24.24.24")) diff --git a/examples/org.py b/examples/org.py index 99b40ab..cf22284 100644 --- a/examples/org.py +++ b/examples/org.py @@ -1,9 +1,11 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPOrg.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPOrg.dat", GeoIP.GEOIP_STANDARD) -print gi.org_by_name("yahoo.com") -print gi.org_by_name("www.google.com") -print gi.org_by_addr("24.24.24.24") +print(gi.org_by_name("yahoo.com")) +print(gi.org_by_name("www.google.com")) +print(gi.org_by_addr("24.24.24.24")) diff --git a/examples/region.py b/examples/region.py index 2b23107..fef9fc1 100644 --- a/examples/region.py +++ b/examples/region.py @@ -1,18 +1,19 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPRegion.dat",GeoIP.GEOIP_STANDARD) +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPRegion.dat", GeoIP.GEOIP_STANDARD) gir = gi.region_by_name("www.google.com") -if gir != None: - print gir['country_code'] +if gir is not None: + print(gir['country_code']) - print gir['region'] - print gir['region_name'] + print(gir['region']) + print(gir['region_name']) gir = gi.region_by_addr("24.24.24.24") -if gir != None: - print gir['country_code'] - print gir['region'] - print gir['region_name'] - +if gir is not None: + print(gir['country_code']) + print(gir['region']) + print(gir['region_name']) diff --git a/examples/v6.py b/examples/v6.py index be2d484..5827c07 100644 --- a/examples/v6.py +++ b/examples/v6.py @@ -1,13 +1,14 @@ #!/usr/bin/python +from __future__ import print_function + import GeoIP #gi = GeoIP.new(GeoIP.GEOIP_STANDARD) #gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) -gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPv6.dat",GeoIP.GEOIP_STANDARD) - -print gi.country_code_by_name_v6("ipv6.google.com") -print gi.country_name_by_name_v6("ipv6.google.com") -print gi.country_code_by_addr_v6("2001:4860:0:1001::68") -print gi.country_name_by_addr_v6("2001:4860:0:1001::68") +gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPv6.dat", GeoIP.GEOIP_STANDARD) +print(gi.country_code_by_name_v6("ipv6.google.com")) +print(gi.country_name_by_name_v6("ipv6.google.com")) +print(gi.country_code_by_addr_v6("2001:4860:0:1001::68")) +print(gi.country_name_by_addr_v6("2001:4860:0:1001::68")) From 3204b288b9d9ac10b17fba9e193b111b2c153ad7 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 14:17:00 -0700 Subject: [PATCH 45/66] 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. --- py_GeoIP.c | 8 ++++++++ tests/test_city.py | 23 +++++++++++++++++++++++ tests/test_errors.py | 7 +++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 0186ce9..94cf5b7 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -85,6 +85,10 @@ GeoIP_GeoIP_init(PyObject *self, PyObject *args, PyObject *kwargs) return -1; } +#if PY_MAJOR_VERSION >= 3 + GeoIP_set_charset(GeoIP->gi, GEOIP_CHARSET_UTF8); +#endif + return 0; } @@ -497,11 +501,13 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) return retval; } +#if PY_MAJOR_VERSION <= 2 static PyObject *GeoIP_charset_Py(PyObject *self, PyObject *UNUSED(args)) { GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("i", GeoIP_charset(GeoIP->gi)); } +#endif static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) { @@ -603,8 +609,10 @@ static PyMethodDef GeoIP_GeoIP_methods[] = { "Lookup City Region By Name" }, { "range_by_ip", GeoIP_range_by_ip_Py, METH_VARARGS, "Lookup start and end IP's for a given IP" }, +#if PY_MAJOR_VERSION <= 2 { "charset", GeoIP_charset_Py, METH_NOARGS, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )" }, +#endif { "set_charset", GeoIP_set_charset_Py, METH_VARARGS, "Set the charset for city records" }, { "last_netmask", GeoIP_last_netmask_Py, METH_NOARGS, diff --git a/tests/test_city.py b/tests/test_city.py index 4d2866b..df415d6 100644 --- a/tests/test_city.py +++ b/tests/test_city.py @@ -24,3 +24,26 @@ def test_city(): } assert_equal(gir, record) + + +def test_non_ascii_city(): + gi = GeoIP.open("tests/data/GeoIPCity.dat", GeoIP.GEOIP_STANDARD) + + gir = gi.record_by_addr("89.92.212.80") + + record = { + 'city': 'F\xe2ches-thumesnil', + 'region_name': 'Nord-Pas-de-Calais', + 'region': 'B4', 'area_code': 0, + 'time_zone': 'Europe/Paris', + 'longitude': 3.0808000564575195, + 'metro_code': 0, + 'country_code3': 'FRA', + 'latitude': 50.5906982421875, + 'postal_code': None, + 'dma_code': 0, + 'country_code': 'FR', + 'country_name': 'France' + } + + assert_equal(gir, record) diff --git a/tests/test_errors.py b/tests/test_errors.py index c4caa3e..dd22337 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,3 +1,5 @@ +import sys + import GeoIP from nose.tools import assert_equal @@ -35,9 +37,10 @@ def test_errors(): assert_equal(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) - assert_equal(gi.charset(), 0) + if sys.version_info[0] == 2: + assert_equal(gi.charset(), 0) - assert_equal(gi.set_charset(-1), 0) + assert_equal(gi.set_charset(-1), 0) assert_equal(gi.last_netmask(), 5) From a71eada6eac5192e97df08c9a00bbf5ea891ecf4 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 14:19:37 -0700 Subject: [PATCH 46/66] Also disable set_charset --- py_GeoIP.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 94cf5b7..5f9746d 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -507,7 +507,6 @@ static PyObject *GeoIP_charset_Py(PyObject *self, PyObject *UNUSED(args)) GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; return Py_BuildValue("i", GeoIP_charset(GeoIP->gi)); } -#endif static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) { @@ -519,6 +518,7 @@ static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) return Py_BuildValue("i", GeoIP_set_charset(GeoIP->gi, charset)); } +#endif static PyObject *GeoIP_last_netmask_Py(PyObject * self, PyObject *UNUSED(args)) { @@ -612,9 +612,9 @@ static PyMethodDef GeoIP_GeoIP_methods[] = { #if PY_MAJOR_VERSION <= 2 { "charset", GeoIP_charset_Py, METH_NOARGS, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )" }, -#endif { "set_charset", GeoIP_set_charset_Py, METH_VARARGS, "Set the charset for city records" }, +#endif { "last_netmask", GeoIP_last_netmask_Py, METH_NOARGS, "Return the netmask depth of the last lookup" }, { "country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, METH_VARARGS, From d2d5298f44063f9dd3ab5675fde32e02d84d47f8 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 14:25:28 -0700 Subject: [PATCH 47/66] Added note to example --- examples/city_charset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/city_charset.py b/examples/city_charset.py index 44d2dea..402a6bb 100644 --- a/examples/city_charset.py +++ b/examples/city_charset.py @@ -1,5 +1,7 @@ #!/usr/bin/python +# This example is only for Python 2. In Python 3, all returned strings are +# unicode. import GeoIP # open the citydatabase. All cities return in iso-8859-1 by default From 71656ec582a53f242babc6632db4d172ab37f4c3 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 15:46:42 -0700 Subject: [PATCH 48/66] Reenabled set_charset/charset on Python 3 --- py_GeoIP.c | 13 ++++++++----- tests/test_errors.py | 7 ------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index 5f9746d..e64ce73 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -501,10 +501,10 @@ static PyObject *GeoIP_range_by_ip_Py(PyObject * self, PyObject * args) return retval; } -#if PY_MAJOR_VERSION <= 2 static PyObject *GeoIP_charset_Py(PyObject *self, PyObject *UNUSED(args)) { GeoIP_GeoIPObject *GeoIP = (GeoIP_GeoIPObject *)self; + return Py_BuildValue("i", GeoIP_charset(GeoIP->gi)); } @@ -515,10 +515,15 @@ static PyObject *GeoIP_set_charset_Py(PyObject * self, PyObject * args) if (!PyArg_ParseTuple(args, "i", &charset)) { return NULL; } +#if PY_MAJOR_VERSION >= 3 + if (charset != GEOIP_CHARSET_UTF8) { + PyErr_SetString(PyExc_ValueError, + "Only UTF-8 is supported for Python 3+."); + return NULL; + } +#endif return Py_BuildValue("i", GeoIP_set_charset(GeoIP->gi, charset)); - } -#endif static PyObject *GeoIP_last_netmask_Py(PyObject * self, PyObject *UNUSED(args)) { @@ -609,12 +614,10 @@ static PyMethodDef GeoIP_GeoIP_methods[] = { "Lookup City Region By Name" }, { "range_by_ip", GeoIP_range_by_ip_Py, METH_VARARGS, "Lookup start and end IP's for a given IP" }, -#if PY_MAJOR_VERSION <= 2 { "charset", GeoIP_charset_Py, METH_NOARGS, "Return the current charset ( either GEOIP_CHARSET_ISO_8859_1 or GEOIP_CHARSET_UTF8 )" }, { "set_charset", GeoIP_set_charset_Py, METH_VARARGS, "Set the charset for city records" }, -#endif { "last_netmask", GeoIP_last_netmask_Py, METH_NOARGS, "Return the netmask depth of the last lookup" }, { "country_code_by_name_v6", GeoIP_country_code_by_name_v6_Py, METH_VARARGS, diff --git a/tests/test_errors.py b/tests/test_errors.py index dd22337..aee1203 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -1,5 +1,3 @@ -import sys - import GeoIP from nose.tools import assert_equal @@ -37,11 +35,6 @@ def test_errors(): assert_equal(gi.range_by_ip('1.1.1.1'), ('0.0.0.0', '12.87.117.255')) - if sys.version_info[0] == 2: - assert_equal(gi.charset(), 0) - - assert_equal(gi.set_charset(-1), 0) - assert_equal(gi.last_netmask(), 5) assert_equal(gi.country_code_by_name_v6('1.1.1.1'), None) From 7b9738d651810793a7a543162c9598bed151cecf Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 15:53:47 -0700 Subject: [PATCH 49/66] Added basic charset/set_charset test --- tests/test_charset.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/test_charset.py diff --git a/tests/test_charset.py b/tests/test_charset.py new file mode 100644 index 0000000..92697b1 --- /dev/null +++ b/tests/test_charset.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +import GeoIP +from nose.tools import assert_equal + + +def test_charset(): + gi = GeoIP.open("tests/data/GeoIPOrg.dat", GeoIP.GEOIP_STANDARD) + + gi.set_charset(GeoIP.GEOIP_CHARSET_UTF8) + assert_equal(gi.charset(), GeoIP.GEOIP_CHARSET_UTF8) + + assert_equal(gi.name_by_addr("142.217.214.0"), + 'LINO Solutions Internet de Télébec') From b6dac12745501cc9705a093a24003b36dc8abc15 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 21 Mar 2014 16:07:30 -0700 Subject: [PATCH 50/66] Updated change log for 1.3.1 --- ChangeLog.md | 8 +++++++- setup.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9b9239f..4d3e3eb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,12 @@ - # Change Log +## 1.3.1 (2014-03-21) + +* Fix for broken strings in Python 3. Strings with non-ASCII characters + would throw a UTF-8 decoding error. In Python 3, all strings from the + database are in UTF-8 and using `set_charset` to set the character set + to something other than UTF-8 will throw an `ValueError` exception. + ## 1.3.0 (2014-03-21) * Python 3 support and significant code cleanup. ( Zack Weinberg ) diff --git a/setup.py b/setup.py index 60e665a..ccd6fad 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setup( name='GeoIP', - version='1.3.0', + version='1.3.1', description='MaxMind GeoIP Legacy Database - Python API', long_description=open('README.rst').read(), author="MaxMind, Inc.", From c402d29c7534fdc03e0f06fb1d1e534ffd6ca369 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 16 May 2014 08:24:27 -0700 Subject: [PATCH 51/66] Fixed org_by_name description --- ChangeLog.md | 5 +++++ py_GeoIP.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 4d3e3eb..214bcfc 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,10 @@ # Change Log +## 1.3.x (2014-XX-XX) + +* The description for `org_by_name` incorrectly directed people to use + `name_by_addr` rather than `name_by_name`. + ## 1.3.1 (2014-03-21) * Fix for broken strings in Python 3. Strings with non-ASCII characters diff --git a/py_GeoIP.c b/py_GeoIP.c index e64ce73..a7fd12b 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -603,7 +603,7 @@ static PyMethodDef GeoIP_GeoIP_methods[] = { { "org_by_addr", GeoIP_name_by_addr_Py, METH_VARARGS, "Lookup ASN, Domain, ISP and Organisation By IP Address ( deprecated use name_by_addr instead )" }, { "org_by_name", GeoIP_name_by_name_Py, METH_VARARGS, - "Lookup ASN, Domain, ISP and Organisation By Name ( deprecated use name_by_addr instead )" }, + "Lookup ASN, Domain, ISP and Organisation By Name ( deprecated use name_by_name instead )" }, { "region_by_addr", GeoIP_region_by_addr_Py, METH_VARARGS, "Lookup Region By IP Address" }, { "region_by_name", GeoIP_region_by_name_Py, METH_VARARGS, From b6820acff7b056f30236459fe465f2b96e19e254 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 16 May 2014 08:43:08 -0700 Subject: [PATCH 52/66] Disable strict-aliasing --- setup.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ccd6fad..9026e35 100644 --- a/setup.py +++ b/setup.py @@ -10,9 +10,15 @@ if sys.platform == 'win32': libs.append('ws2_32') +compile_args = [] +# http://bugs.python.org/issue969718 +if sys.version_info[0] == 2: + compile_args.append('-fno-strict-aliasing') + module1 = Extension('GeoIP', libraries=libs, - sources=['py_GeoIP.c']) + sources=['py_GeoIP.c'], + extra_compile_args=compile_args) setup( name='GeoIP', From 77ee667f8b9feae0ca42a908c27d926f304205b0 Mon Sep 17 00:00:00 2001 From: Sergey Farbotka Date: Fri, 22 Aug 2014 13:54:10 +0300 Subject: [PATCH 53/66] 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); From 4466b548f61d4cb055c914b912b924cc8cc806a1 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 22 Aug 2014 07:36:10 -0700 Subject: [PATCH 54/66] Use GeoIP_country_name under Python 2.x --- py_GeoIP.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py_GeoIP.c b/py_GeoIP.c index e6d142b..fcf0b6b 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -789,7 +789,11 @@ GeoIP_populate_module(PyObject *m) CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_code[i])); PyTuple_SET_ITEM(ccode, i, tmp); +#if PY_MAJOR_VERSION >= 3 CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_utf8_country_name[i])); +#else + CHECK_NULL(tmp = PyUnicode_FromString(GeoIP_country_name[i])); +#endif CHECK(PyDict_SetItemString(cname, GeoIP_country_code[i], tmp)); Py_DECREF(tmp); From e26d53497dbc7b6f7f3372e1415514b7316bc305 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 22 Aug 2014 07:58:15 -0700 Subject: [PATCH 55/66] Prepare for 1.3.2 --- ChangeLog.md | 7 ++++++- setup.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 214bcfc..62091e1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,9 +1,14 @@ # Change Log -## 1.3.x (2014-XX-XX) +## 1.3.2 (2014-08-22) * The description for `org_by_name` incorrectly directed people to use `name_by_addr` rather than `name_by_name`. +* Previously `GeoIP.country_names` was populated from `GeoIP_country_name` in + the libGeoIP C API. Some versions of the libGeoIP include non-ASCII + ISO-8859-1 characters in these names, causing encoding errors under Python + 3. When installed under Python 3, this API now uses UTF-8 country names to + populate this dict. Reported and fixed by Sergey Farbotka. GitHub PR #15. ## 1.3.1 (2014-03-21) diff --git a/setup.py b/setup.py index 9026e35..d14ea69 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ setup( name='GeoIP', - version='1.3.1', + version='1.3.2', description='MaxMind GeoIP Legacy Database - Python API', long_description=open('README.rst').read(), author="MaxMind, Inc.", From 4972bf7464970920677d6b5921fd63a6a6164885 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 22 Aug 2014 08:03:54 -0700 Subject: [PATCH 56/66] Test under 3.4 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4d96190..e1ce555 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ python: - 2.6 - 2.7 - 3.3 + - 3.4 before_install: - git clone git://github.com/maxmind/geoip-api-c From 2b33282363218057d6d7d8c0de1ac24e3530649f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 29 Aug 2014 11:07:45 -0700 Subject: [PATCH 57/66] Make clear that this is for the legacy format --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 2455b39..15dab44 100644 --- a/README.rst +++ b/README.rst @@ -6,7 +6,7 @@ Requirements ------------ * Python 2.5+ or 3.3+ -* GeoIP C Library 1.4.7 or greater +* GeoIP Legacy C Library 1.4.7 or greater Installation ------------ From 670b23cff547cc0891262077e6a23216f78454b0 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 2 Sep 2014 14:42:54 -0700 Subject: [PATCH 58/66] Use angle brackets for libGeoIP imports --- py_GeoIP.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py_GeoIP.c b/py_GeoIP.c index fcf0b6b..11c9063 100644 --- a/py_GeoIP.c +++ b/py_GeoIP.c @@ -20,8 +20,8 @@ #include -#include "GeoIP.h" -#include "GeoIPCity.h" +#include +#include #ifdef __GNUC__ # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) From 69e46eec3a9f0cfaf6db83769a251a9619316003 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 1 Jan 2016 15:26:03 -0800 Subject: [PATCH 59/66] Update ChangeLog.md in MANIFEST.in --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 0ea1e05..881cb08 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,3 @@ -include ChangeLog LICENSE README.rst py_GeoIP.c +include ChangeLog.md LICENSE README.rst py_GeoIP.c recursive-include examples *.py recursive-include tests *.py From 69af569ef0c33ef56e5f81eab09014c9d5ed09fb Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Mon, 4 Jan 2016 15:49:31 -0600 Subject: [PATCH 60/66] Send all emails to dev-ci@mm --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e1ce555..f191839 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,6 @@ script: notifications: email: recipients: - - dev@maxmind.com + - dev-ci@maxmind.com on_success: change on_failure: always From 59a6ab034440ab3f3cbb304de5aa8db9bc711185 Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Thu, 21 Jan 2016 09:47:50 -0600 Subject: [PATCH 61/66] Send AppVeyor Slack notifications on every build --- .travis.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index f191839..4089619 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,10 @@ +--- language: python - python: - - 2.6 - - 2.7 - - 3.3 - - 3.4 - + - '2.6' + - '2.7' + - '3.3' + - '3.4' before_install: - git clone git://github.com/maxmind/geoip-api-c - cd geoip-api-c @@ -15,14 +14,15 @@ before_install: - sudo make install - sudo ldconfig - cd .. - - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi - + - "if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi" script: - CFLAGS="-Werror -Wall -Wextra" python setup.py test - notifications: email: + on_failure: always + on_success: change recipients: - dev-ci@maxmind.com - on_success: change - on_failure: always + slack: + rooms: + secure: "mNw9bQtM+fHITJiYGwex/YB42uy6IfIYSLQZWaD6F3mb2Y8aSXAtck+raV/C85CTM4LyBq6YyFLr1912+Xd0gmM7mSEQc5wXVf/vlzpMfNyDedonBSZeed7SGfm63xCTN9KWkOmhzIAhRy7WozmBP+bkNjyH9D1gYt863W6OYiA=\n" From e4cf4640420aed7bbfc21b76311cef9424263411 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 15 Apr 2016 09:27:38 -0700 Subject: [PATCH 62/66] Add location usage warnings --- README.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.rst b/README.rst index 15dab44..0261229 100644 --- a/README.rst +++ b/README.rst @@ -24,6 +24,14 @@ From source: $ python setup.py build $ python setup.py install + +IP Geolocation Usage +-------------------- + +IP geolocation is inherently imprecise. Locations are often near the center of +the population. Any location provided by a GeoIP database should not be used to +identify a particular address or household. + Usage ----- From 9c4ec28c8887e8a71dba357266270a1fef625150 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Sun, 11 Aug 2019 10:05:14 -0700 Subject: [PATCH 63/66] Update Travis config to use Trusty and add new Pythons --- .travis.yml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4089619..e1b6762 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,16 @@ --- language: python -python: - - '2.6' - - '2.7' - - '3.3' - - '3.4' +matrix: + include: + - python: '2.6' + dist: trusty + - python: '2.7' + - python: '3.3' + dist: trusty + - python: '3.4' + - python: '3.5' + - python: '3.6' + - python: '3.7' before_install: - git clone git://github.com/maxmind/geoip-api-c - cd geoip-api-c From f0cf04f10e0756c2bd4f839ed2d85ca7db4063f3 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 25 Jun 2020 08:06:11 -0700 Subject: [PATCH 64/66] Create codeql-analysis.yml --- .github/workflows/codeql-analysis.yml | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..8256fbf --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,46 @@ +name: "Code scanning - action" + +on: + push: + pull_request: + schedule: + - cron: '0 20 * * 4' + +jobs: + CodeQL-Build: + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + # We must fetch at least the immediate parents so that if this is + # a pull request then we can checkout the head. + fetch-depth: 2 + + # If this run was triggered by a pull request event, then checkout + # the head of the pull request instead of the merge commit. + - run: git checkout HEAD^2 + if: ${{ github.event_name == 'pull_request' }} + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + # Override language selection by uncommenting this and choosing your languages + with: + languages: python, cpp + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + - run: | + sudo apt install libgeoip-dev + python setup.py build + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 From 2604c2e124116951c7b09cc52100b0c97a6e65e2 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 5 Feb 2021 12:41:06 -0800 Subject: [PATCH 65/66] Add EOL notice --- README.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.rst b/README.rst index 0261229..ac0974b 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,20 @@ MaxMind GeoIP Legacy Python Extension API ========================================= +End of Life +----------- + +MaxMind will be retiring the GeoIP Legacy databases at the end of May +2022. Until then, this library will only receive critical security and bug +fixes. Support for this library will end completely with the last release of +the legacy GeoIP databases. + +We recommend that you upgrade to our GeoIP2 databases. You can read these +from Python using `our GeoIP2 Python API `_. + +See `our blog post `_ +for more information. + Requirements ------------ From 3f95af6c2d0d39ad84d81cde1241812064260e2c Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 11 Feb 2021 19:55:45 +0000 Subject: [PATCH 66/66] Create Dependabot config file --- .github/dependabot.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..491deae --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +version: 2 +updates: +- package-ecosystem: pip + directory: "/" + schedule: + interval: daily + open-pull-requests-limit: 10