Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG python initialise a pointer to NULL

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?

like image 558
bph Avatar asked Nov 01 '13 12:11

bph


1 Answers

Just use None from python to represent a null value.

like image 180
Mark Tolonen Avatar answered Oct 02 '22 23:10

Mark Tolonen