SWIG python initialise a pointer to NULL

牧云@^-^@ 提交于 2019-12-07 03:43:34

问题


Is it possible to initialise a ptr to NULL from the python side when dealing with SWIG module?

For example, say I have wrapped a struct track_t in a swig module m (_m.so), I can create a pointer to the struct from python as follows:

import m

track = m.track_t()

this will just malloc a track_t for me in the appropriate wrapper function.

I would however like to be able to achieve the following:

track_t *track = NULL;

But in python rather than C, e.g. initialise a pointer to NULL from the python side

I need to do this as it is a requirement of the hash table C implementation I am using that new hash tables start with a NULL pointer

I could write a helper function, say, track_t* create_null_track() that returns a NULL pointer but thought there may be a simpler way?

EDIT:

I can confirm that the helper function works but I would expect there to be a built in way of doing this as requiring a NULL pointer would seem a common requirement

helper function is as simple as:

track_t* create_null_track(void)
{
    return NULL;
}

not sure that the return type information specified in the function is necessary so a more generic approach could be:

void* get_null(void)
{
    return NULL;
}

perhaps?


回答1:


Just use None from python to represent a null value.



来源:https://stackoverflow.com/questions/19726689/swig-python-initialise-a-pointer-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!