我是Python C-API和模块创建的新手.我试图创建一个c-hash python模块.我使用python 3.4.3和TDM-gcc(64bit)4.9.2进行Windows上的编译.
这是我的代码:
// hash_mod.c
#include <Python.h>
unsigned long _hash(unsigned char const* str)
{
unsigned long hash = 5381;
int c;
int i;
i = 0;
while (str[i] != '\0')
{
c = str[i];
hash = ((hash << 5) + hash) + c;
++i;
}
return hash;
}
static PyObject*
hash_hash(PyObject* self, PyObject* args)
{
unsigned char const* str;
if (!PyArg_ParseTuple(args, "s", &str))
return NULL;
return PyLong_FromUnsignedLong(_hash(str));
}
static PyMethodDef HashMethods[] = {
{"hash", hash_hash, METH_VARARGS, "String Hash"}, …Run Code Online (Sandbox Code Playgroud)