Skip to content

Commit

Permalink
pydablooms: add error handling and reporting
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
martinthomas authored and ploxiln committed Dec 15, 2024
1 parent e09c109 commit 3868a60
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions pydablooms/pydablooms.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

int Py_ModuleVersion = 1;

static PyObject *DabloomsError;

typedef struct {
PyObject_HEAD
scaling_bloom_t *filter; /* Type-specific fields go here. */
} Dablooms;

static void Dablooms_dealloc(Dablooms *self)
{
free_scaling_bloom(self->filter);
if(self->filter)
free_scaling_bloom(self->filter);
self->ob_type->tp_free((PyObject *)self);
}

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

self->filter = NULL;

return (PyObject *) self;
}

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

if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ids", kwlist,
&capacity, &error_rate, &filepath)) {
return -1;
}

if (capacity < 1){
PyErr_SetString(DabloomsError, "Bloom creation failed: capacity must be greater than zero");
return -1;
}
if (error_rate > 1 || error_rate < 0){
PyErr_SetString(DabloomsError, "Bloom creation failed: error_rate must be between 0 and 1");
return -1;
}
if(!(filepath && strlen(filepath))){
PyErr_SetString(DabloomsError, "Bloom creation failed: filepath required");
return -1;
}

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

Expand Down Expand Up @@ -181,18 +196,36 @@ static PyTypeObject DabloomsType = {
static PyObject *load_dabloom(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Dablooms *self = (Dablooms *)PyObject_New(Dablooms, &DabloomsType);
double error_rate;
const char *filepath;
unsigned int capacity;
double error_rate = 0.1;
const char *filepath = "/tmp/bloom.bin";
int capacity = 1; // dropped the unsigned modifier to avoid implicit conversion from negative
int result = 0;
static char *kwlist[] = {"capacity", "error_rate", "filepath", NULL};

if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ids", kwlist,
&capacity, &error_rate, &filepath)) {
return NULL;
}

self->filter = new_scaling_bloom_from_file(capacity, error_rate, filepath);
return (PyObject *) self;

if (capacity < 1){
PyErr_SetString(DabloomsError, "Bloom creation failed: capacity must be greater than zero");
result = -1;
}
else if (error_rate > 1 || error_rate < 0){
PyErr_SetString(DabloomsError, "Bloom creation failed: error_rate must be between 0 and 1");
result = -1;
}
else if(!(filepath && strlen(filepath))){
PyErr_SetString(DabloomsError, "Bloom creation failed: filepath required");
result = -1;
}

if (!result){
self->filter = new_scaling_bloom_from_file(capacity, error_rate, filepath);
return (PyObject *) self;
}
Dablooms_dealloc(self);
return NULL;
}

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

Py_INCREF(&DabloomsType);
PyModule_AddObject(m, "Dablooms", (PyObject *)&DabloomsType);

DabloomsError = PyErr_NewException("Dablooms.Error", NULL, NULL);
Py_INCREF(DabloomsError);
PyModule_AddObject(m, "error", DabloomsError);
}

0 comments on commit 3868a60

Please sign in to comment.