Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default value for named parameters, checks for parameter values, reporting for errors and fixed segmentation fault when object creation fails #60

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}