Skip to content

Commit 3868a60

Browse files
martinthomasploxiln
authored andcommitted
pydablooms: add error handling and reporting
* add default values for named parameters * add simple parameter checking * add new exception type to be used when reporting errors * add check for existing filter in dealloc from bitly#60
1 parent e09c109 commit 3868a60

1 file changed

Lines changed: 49 additions & 12 deletions

File tree

pydablooms/pydablooms.c

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
int Py_ModuleVersion = 1;
66

7+
static PyObject *DabloomsError;
8+
79
typedef struct {
810
PyObject_HEAD
911
scaling_bloom_t *filter; /* Type-specific fields go here. */
1012
} Dablooms;
1113

1214
static void Dablooms_dealloc(Dablooms *self)
1315
{
14-
free_scaling_bloom(self->filter);
16+
if(self->filter)
17+
free_scaling_bloom(self->filter);
1518
self->ob_type->tp_free((PyObject *)self);
1619
}
1720

@@ -24,21 +27,33 @@ static PyObject *Dablooms_new(PyTypeObject *type, PyObject *args, PyObject *kwds
2427
}
2528

2629
self->filter = NULL;
27-
2830
return (PyObject *) self;
2931
}
3032

3133
static int Dablooms_init(Dablooms *self, PyObject *args, PyObject *kwds)
3234
{
33-
double error_rate;
34-
const char *filepath;
35-
unsigned int capacity;
35+
double error_rate = 0.1;
36+
const char *filepath = "/tmp/bloom.bin";
37+
int capacity = 1; // dropped the unsigned modifier to avoid implicit conversion from negative
3638
static char *kwlist[] = {"capacity", "error_rate", "filepath", NULL};
3739

3840
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ids", kwlist,
3941
&capacity, &error_rate, &filepath)) {
4042
return -1;
4143
}
44+
45+
if (capacity < 1){
46+
PyErr_SetString(DabloomsError, "Bloom creation failed: capacity must be greater than zero");
47+
return -1;
48+
}
49+
if (error_rate > 1 || error_rate < 0){
50+
PyErr_SetString(DabloomsError, "Bloom creation failed: error_rate must be between 0 and 1");
51+
return -1;
52+
}
53+
if(!(filepath && strlen(filepath))){
54+
PyErr_SetString(DabloomsError, "Bloom creation failed: filepath required");
55+
return -1;
56+
}
4257

4358
self->filter = new_scaling_bloom(capacity, error_rate, filepath);
4459

@@ -181,18 +196,36 @@ static PyTypeObject DabloomsType = {
181196
static PyObject *load_dabloom(PyTypeObject *type, PyObject *args, PyObject *kwds)
182197
{
183198
Dablooms *self = (Dablooms *)PyObject_New(Dablooms, &DabloomsType);
184-
double error_rate;
185-
const char *filepath;
186-
unsigned int capacity;
199+
double error_rate = 0.1;
200+
const char *filepath = "/tmp/bloom.bin";
201+
int capacity = 1; // dropped the unsigned modifier to avoid implicit conversion from negative
202+
int result = 0;
187203
static char *kwlist[] = {"capacity", "error_rate", "filepath", NULL};
188-
204+
189205
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ids", kwlist,
190206
&capacity, &error_rate, &filepath)) {
191207
return NULL;
192208
}
193-
194-
self->filter = new_scaling_bloom_from_file(capacity, error_rate, filepath);
195-
return (PyObject *) self;
209+
210+
if (capacity < 1){
211+
PyErr_SetString(DabloomsError, "Bloom creation failed: capacity must be greater than zero");
212+
result = -1;
213+
}
214+
else if (error_rate > 1 || error_rate < 0){
215+
PyErr_SetString(DabloomsError, "Bloom creation failed: error_rate must be between 0 and 1");
216+
result = -1;
217+
}
218+
else if(!(filepath && strlen(filepath))){
219+
PyErr_SetString(DabloomsError, "Bloom creation failed: filepath required");
220+
result = -1;
221+
}
222+
223+
if (!result){
224+
self->filter = new_scaling_bloom_from_file(capacity, error_rate, filepath);
225+
return (PyObject *) self;
226+
}
227+
Dablooms_dealloc(self);
228+
return NULL;
196229
}
197230

198231
static PyMethodDef pydablooms_methods[] = {
@@ -222,4 +255,8 @@ PyMODINIT_FUNC initpydablooms(void)
222255

223256
Py_INCREF(&DabloomsType);
224257
PyModule_AddObject(m, "Dablooms", (PyObject *)&DabloomsType);
258+
259+
DabloomsError = PyErr_NewException("Dablooms.Error", NULL, NULL);
260+
Py_INCREF(DabloomsError);
261+
PyModule_AddObject(m, "error", DabloomsError);
225262
}

0 commit comments

Comments
 (0)