From 60e51fd2764291df2332f36ff478777627d92b57 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Thu, 9 Aug 2012 10:10:42 +0200 Subject: Add python bindings for murmurhash3 --- src/python/pysss_murmur.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/python/pysss_murmur.c (limited to 'src/python/pysss_murmur.c') diff --git a/src/python/pysss_murmur.c b/src/python/pysss_murmur.c new file mode 100644 index 000000000..1e89681c8 --- /dev/null +++ b/src/python/pysss_murmur.c @@ -0,0 +1,65 @@ +/* + Authors: + Sumit Bose + + Copyright (C) 2012 Red Hat + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include "util/murmurhash3.h" + +PyDoc_STRVAR(murmurhash3_doc, +"murmurhash3(key, key_len, seed) -> 32bit integer hash\n\ +\n\ +Calculate the murmur hash version 3 of the first key_len bytes from key\n\ +using the given seed." +); + +static PyObject * py_murmurhash3(PyObject *module, PyObject *args) +{ + const char *key; + long key_len; + long long seed; + uint32_t hash; + + if (!PyArg_ParseTuple(args, "slL", &key, &key_len, &seed)) { + PyErr_Format(PyExc_ValueError, "Invalid argument\n"); + return NULL; + } + + if (seed > UINT32_MAX || key_len > INT_MAX || key_len < 0 || + key_len > strlen(key)) { + PyErr_Format(PyExc_ValueError, "Invalid value\n"); + return NULL; + } + + hash = murmurhash3(key, key_len, seed); + + return PyLong_FromUnsignedLong((unsigned long) hash); +} + +static PyMethodDef methods[] = { + {"murmurhash3", (PyCFunction) py_murmurhash3, METH_VARARGS, murmurhash3_doc}, + {NULL,NULL, 0, NULL} +}; + + +PyMODINIT_FUNC +initpysss_murmur(void) +{ + Py_InitModule3("pysss_murmur", methods, "murmur hash functions"); +} -- cgit