小编Imp*_*Imp的帖子

C Python模块具有不寻常的内存使用

我是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)

c python-module python-3.x

6
推荐指数
1
解决办法
287
查看次数

标签 统计

c ×1

python-3.x ×1

python-module ×1