Skip to content

Commit 39e2864

Browse files
author
borisz
committed
record_by_addr and record_by_name return None instead of throwing a exception
1 parent b476ec5 commit 39e2864

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
!! record_by_addr and record_by_name return None instead of throwing a
2+
exception. See test_city.py ( Boris Zentner )
3+
* Add a py_geoip.error Exception object. (Boris Zentner)
14
* Export the country list, country code -> country name mapping, and country->continent
25
mapping ( Ignacio Vazquez-Abrams )
36
* Changed license to LGPL from GPL

py_GeoIP.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
staticforward PyTypeObject GeoIP_GeoIPType;
2626

27+
/* Exception object for python */
28+
static PyObject *PyGeoIPError;
29+
2730
typedef struct {
2831
PyObject_HEAD;
2932
GeoIP *gi;
@@ -43,6 +46,7 @@ GeoIP_new_Py(PyObject* self, PyObject *args) {
4346
GeoIP->gi = GeoIP_new(flags);
4447

4548
if (!GeoIP->gi) {
49+
PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object");
4650
return NULL;
4751
}
4852

@@ -64,6 +68,7 @@ GeoIP_open_Py(PyObject* self, PyObject *args) {
6468
GeoIP->gi = GeoIP_open(filename, flags);
6569

6670
if (!GeoIP->gi) {
71+
PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object");
6772
return NULL;
6873
}
6974

@@ -209,7 +214,8 @@ static PyObject * GeoIP_record_by_addr_Py(PyObject *self, PyObject *args) {
209214
}
210215
gir = GeoIP_record_by_addr(GeoIP->gi, addr);
211216
if (gir == NULL) {
212-
return NULL;
217+
Py_INCREF(Py_None);
218+
return Py_None;
213219
}
214220
return GeoIP_populate_dict(gir);
215221
}
@@ -223,7 +229,8 @@ static PyObject * GeoIP_record_by_name_Py(PyObject *self, PyObject *args) {
223229
}
224230
gir = GeoIP_record_by_name(GeoIP->gi, name);
225231
if (gir == NULL) {
226-
return NULL;
232+
Py_INCREF(Py_None);
233+
return Py_None;
227234
}
228235
return GeoIP_populate_dict(gir);
229236
}
@@ -306,6 +313,9 @@ initGeoIP(void)
306313
m = Py_InitModule("GeoIP", GeoIP_Class_methods);
307314
d = PyModule_GetDict(m);
308315

316+
PyGeoIPError = PyErr_NewException("py_geoip.error", NULL, NULL);
317+
PyDict_SetItemString(d, "error", PyGeoIPError);
318+
309319
int total_ccodes = 251;
310320

311321
PyObject *ccode = PyTuple_New(total_ccodes);

0 commit comments

Comments
 (0)