24
24
25
25
staticforward PyTypeObject GeoIP_GeoIPType ;
26
26
27
+ /* Exception object for python */
28
+ static PyObject * PyGeoIPError ;
29
+
27
30
typedef struct {
28
31
PyObject_HEAD ;
29
32
GeoIP * gi ;
@@ -43,6 +46,7 @@ GeoIP_new_Py(PyObject* self, PyObject *args) {
43
46
GeoIP -> gi = GeoIP_new (flags );
44
47
45
48
if (!GeoIP -> gi ) {
49
+ PyErr_SetString (PyGeoIPError , "Can't create GeoIP->gi object" );
46
50
return NULL ;
47
51
}
48
52
@@ -64,6 +68,7 @@ GeoIP_open_Py(PyObject* self, PyObject *args) {
64
68
GeoIP -> gi = GeoIP_open (filename , flags );
65
69
66
70
if (!GeoIP -> gi ) {
71
+ PyErr_SetString (PyGeoIPError , "Can't create GeoIP->gi object" );
67
72
return NULL ;
68
73
}
69
74
@@ -209,7 +214,8 @@ static PyObject * GeoIP_record_by_addr_Py(PyObject *self, PyObject *args) {
209
214
}
210
215
gir = GeoIP_record_by_addr (GeoIP -> gi , addr );
211
216
if (gir == NULL ) {
212
- return NULL ;
217
+ Py_INCREF (Py_None );
218
+ return Py_None ;
213
219
}
214
220
return GeoIP_populate_dict (gir );
215
221
}
@@ -223,7 +229,8 @@ static PyObject * GeoIP_record_by_name_Py(PyObject *self, PyObject *args) {
223
229
}
224
230
gir = GeoIP_record_by_name (GeoIP -> gi , name );
225
231
if (gir == NULL ) {
226
- return NULL ;
232
+ Py_INCREF (Py_None );
233
+ return Py_None ;
227
234
}
228
235
return GeoIP_populate_dict (gir );
229
236
}
@@ -306,6 +313,9 @@ initGeoIP(void)
306
313
m = Py_InitModule ("GeoIP" , GeoIP_Class_methods );
307
314
d = PyModule_GetDict (m );
308
315
316
+ PyGeoIPError = PyErr_NewException ("py_geoip.error" , NULL , NULL );
317
+ PyDict_SetItemString (d , "error" , PyGeoIPError );
318
+
309
319
int total_ccodes = 251 ;
310
320
311
321
PyObject * ccode = PyTuple_New (total_ccodes );
0 commit comments