将两个参数(int和array)传递给嵌入式Python函数

时间:2017-08-17 13:59:13

标签: python c python-c-api python-embedding

我需要从我的模块调用Python函数并为它设置两个参数:int和array。

现在我在调用此函数时遇到段错误,我不知道我做错了什么。有人可以指出我的错误在哪里吗?

我的Python模块app.py中的函数。如果我从Python代码中调用它,它就可以工作:

def get_model(rate, signal):
    mfcc_train = MFCC().compute(rate, signal) 
    with open('mfcc_test', 'wb') as f:
        pickle.dump(mfcc_train, f)
    return clf()._fit(mfcc_train) 

调用上述函数的我的C代码。最后一次是"在致电"

之前
#include <Python.h>
#include <stdio.h>
#include "wav.h"
#include <numpy/arrayobject.h>

int main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;
    uint8_t *samples = NULL;

    wavread("test.wav", &samples);

    printf("No. of channels: %d\n",     header->num_channels);
    printf("Sample rate:     %d\n",     header->sample_rate);
    printf("Bit rate:        %dkbps\n", header->byte_rate*8 / 1000);
    printf("Bits per sample: %d\n\n",     header->bps);
    printf("Sample 0:        %d\n", samples[0]);
    printf("Sample 1:        %d\n", samples[1]);
    // Initialize the Python Interpreter
    printf("Before init\n");
    Py_Initialize();
    PyObject *sysPath = PySys_GetObject("path");
    const char *scriptDirectoryName = ".";
    PyObject *path = PyUnicode_FromString(scriptDirectoryName);
    int result = PyList_Insert(sysPath, 0, path);
    printf("after init\n");
    // Build the name object
    pName = PyUnicode_DecodeFSDefault(argv[1]);
    printf("after pname %s %d\n", argv[1], pName == NULL ? 1 : 0);

    // Load the module object
    pModule = PyImport_Import(pName);
    printf("after pmodule %d\n", pModule == NULL ? 1 : 0);

    // pFunc is also a borrowed reference 
    pFunc = PyObject_GetAttrString(pModule, "get_model");
    printf("after pfunc\n");

    if (PyCallable_Check(pFunc)) 
    {
        pArgs = PyTuple_New(2);
        printf("after pytuple\n");
        PyTuple_SetItem(pArgs, 0, PyLong_FromLong(header->sample_rate));
        printf("after set item\n");
        uint8_t* array = malloc(header->datachunk_size);
        int dims[1];
        dims[0] = header->datachunk_size;
        printf("alloc\n");
        import_array();
        PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);
        printf("pSamples\n");
        PyArray_ENABLEFLAGS((PyArrayObject*)pSamples, NPY_ARRAY_OWNDATA);
        PyTuple_SetItem(pArgs, 1, pSamples);
        printf("Before calling\n");
        pValue = PyObject_CallObject(pFunc, pArgs);
        printf("After calling\n");
    } else 
    {
        PyErr_Print();
    }

    printf("pValue:        %d\n", pValue);
    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pFunc);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    free(header);
    free(samples);
}

UPD:更新了修复一个问题的代码。但另一个问题仍然存在。它排在PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);行。我无法找出它的错误。

和wav.h以防万一:

#include <inttypes.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
typedef struct {
    char     chunk_id[4];
    uint32_t chunk_size;
    char     format[4];
    char     fmtchunk_id[4];
    uint32_t fmtchunk_size;
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bps;
    char     datachunk_id[4];
    uint32_t datachunk_size;
}WavHeader;
WavHeader *header;
void wavread(char *file_name, int16_t **samples)
{
    int fd;
    if (!file_name)
        errx(1, "Filename not specified");
    if ((fd = open(file_name, O_RDONLY)) < 1)
        errx(1, "Error opening file");
    if (!header)
        header = (WavHeader*)malloc(sizeof(WavHeader));
    if (read(fd, header, sizeof(WavHeader)) < sizeof(WavHeader))
        errx(1, "File broken: header");
    if (strncmp(header->chunk_id, "RIFF", 4) ||
        strncmp(header->format, "WAVE", 4))
        errx(1, "Not a wav file");
    if (header->audio_format != 1)
        errx(1, "Only PCM encoding supported");
    if (*samples) free(*samples);
    *samples = (int16_t*)malloc(header->datachunk_size);
    if (!*samples)
        errx(1, "Error allocating memory");
    if (read(fd, *samples, header->datachunk_size) < header->datachunk_size)
        errx(1, "File broken: samples");
    close(fd);
}

1 个答案:

答案 0 :(得分:2)

如果没有In [227]: df Out[227]: Year Make Model Trim 0 2007 Acura TL Base 1 2007 Acura TL XLR 2 2007 Acura TL NaN 3 2007 Acura TL Base In [228]: (df.groupby(['Year', 'Make', 'Model'])['Trim'] .apply(lambda x: x.fillna(x.mode()[0]))) ...: Out[228]: 0 Base 1 XLR 2 Base 3 Base Name: Trim, dtype: object 的定义,很难说,但我相信这个问题就在行中

header

PyTuple_SetItem(pArgs, 0, header->sample_rate); 需要一个Python对象并且你传递它我认为是一个整数,它被误解为PyTuple_SetItem

我怀疑你想要

PyObject*

(Python3中的PyTuple_SetItem(pArgs, 0, PyInt_FromLong(header->sample_rate));

第二个问题:您PyLong_FromLong free两次。首先,你将它传递给numpy并告诉numpy它拥有数据:

samples

然后在代码的末尾你释放它

PyObject* pSamples = PyArray_SimpleNewFromData(1, dims, NPY_INT8, (void*)samples);
PyArray_ENABLEFLAGS((PyArrayObject*)pSamples, NPY_ARRAY_OWNDATA);

我怀疑您打算将新分配的free(samples); 传递给numpy而不是array。 (如果是这种情况,你仍然需要在它们之间复制数据)

UPD:评论中另一个正确的解决方案是将dims的类型从int更改为npy_intp