簡體   English   中英

C函數中的Python模塊不返回任何內容

[英]Python module in C for function that doesn't return anything

我一直在努力將一些C代碼轉換成Python模塊。 我正在關注本教程,

http://dan.iel.fm/posts/python-c-extensions/

使Python模塊具有簡單的功能(例如我在本文中所做的規范功能)非常簡單,

在C語言編寫的Python模塊中傳遞參數

但是,對於在傳遞指針而不返回任何內容的函數上該做什么我感到困惑。

例如,我在C語言中有此功能,

void insert_source(node_t *node, source_t *source) {
    node_t *quadrant;
    // Check if the MAX has been reached
    if (node->contents.length == MAX)
        subdivide(node);

    // A node in the tree will be filled with either content or sub
    // quadrants. Check to see whether subquads exist.
    if (node->q1 != NULL) {

        if (source->alpha >= node->xmid) {
            if (source->delta >= node->ymid)
                quadrant = node->q1;
            else
                quadrant = node->q4;
        } else {
            if (source->delta >= node->ymid)
                quadrant = node->q2;
            else
                quadrant = node->q3;
        }
        insert_source(quadrant, source);

    } else {
        // If no subquads exist add source to the list in contents element 
        // Use push() to prepend the source on the list.
        push(&node->contents, source);
    }
}

然后我(不完整的)嘗試包裝器,

static void *Quadtree_insert_source(PyObject *self, PyObject *args) {
    PyObject *node_obj, *source_obj;
    if (!PyArg_ParseTuple(args, "OO", &node_obj, &source_obj))
        return NULL;

    PyObject *node_array = PyArray_FROM_OTF(node_obj, NPY_DOUBLE, NPY_IN_ARRAY);
    PyObject *source_array = PyArray_FROM_OTF(source_obj, NPY_DOUBLE, NPY_IN_ARRAY);

    if (node_array == NULL || source_array == NULL) {
        Py_XDECREF(node_array);
        Py_XDECREF(source_array);
    }

    node_t *node = (node_t*)PyArray_DATA(node_array)
    source_t *source = (source_t*)PyArray_DATA(source_array)

    /* Don't know what to put on this line */
    void insert_source(node, source);

    Py_DECREF(node_array);
    Py_DECREF(source_array);

    /* Don't know what to return/if I should return anything */
}

我鏈接到的教程說,我要調用的任何函數都必須返回一個PyObject,我只是不知道在這種情況下應該是什么。

供參考,這是我在insert_source中使用的結構:

node_t

typedef struct node_t {
    box_t box;
    double xmid, ymid;
    struct node_t *q1, *q2, *q3, *q4;
    list_t contents;
} node_t;

source_t

typedef struct source_t {
    list_links_t links;
    struct source_t *next, *prev;
    int number;
    double flux_iso, fluxerr_iso, flux_aper, fluxerr_aper;
    double x_image, y_image, alpha, delta;
    double mag_auto, magerr_auto, mag_best, magerr_best;
    double mag_aper, magerr_aper, a_world, erra_world;
    double b_world, errb_world, theta;
    double errtheta, isoarea_img, mu_max, flux_radius;
    int flags;
    double fwhm, elongation, vignet;
    struct source_t *match2, *match3;
} source_t;

而四叉樹只是構成四叉樹功能的功能的集合,包括insert_source。 C代碼現在全部可用,而這只是制作Python接口的問題。

我更仔細地閱讀了文檔 ,並看到了

如果您有一個C函數不返回任何有用的參數(一個函數返回void),則相應的Python函數必須返回None。 您需要使用以下慣用法(由Py_RETURN_NONE宏實現):

Py_INCREF(Py_None);

返回Py_None;

這樣包裝程序的最后一部分會看起來像這樣,

 insert_source(node, source);

 Py_DECREF(node_array);
 Py_DECREF(source_array);

 Py_INCREF(Py_None);
 return Py_None;

誰能確認?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM