SWIG python initialise a pointer to NULL

2024/11/16 7:21:08

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 mtrack = 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?

Answer

Just use None from python to represent a null value.

https://en.xdnf.cn/q/71368.html

Related Q&A

Replacing punctuation in a data frame based on punctuation list [duplicate]

This question already has answers here:Fast punctuation removal with pandas(4 answers)Closed 5 years ago.Using Canopy and Pandas, I have data frame a which is defined by:a=pd.read_csv(text.txt)df=pd.Da…

How to import one submodule from different submodule? [duplicate]

This question already has answers here:Relative imports for the billionth time(14 answers)Closed 6 years ago.My project has the following structure:DSTC/st/__init__.pya.pyg.pytb.pydstc.pyHere is a.py i…

How to add dimension to a tensor using Tensorflow

I have method reformat in which using numpy I convert a label(256,) to label(256,2) shape. Now I want to do same operation on a Tensor with shape (256,)My code looks like this (num_labels=2) :--def ref…

Down arrow symbol in matplotlib

I would like to create a plot where some of the points have a downward pointing arrow (see image below). In Astronomy this illustrates that the true value is actually lower than whats measured.Note tha…

Overwrite the previous print value in python?

How can i overwrite the previous "print" value in python?print "hello" print "dude" print "bye"It will output:hello dude byeBut i want to overwrite the value.In…

pyQt4 - How to select table rows and disable editing cells

I create a QTableWidget with:self.table = QtGui.QTableWidget() self.table.setObjectName(table) self.table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) verticalLayout.addWidget(self.table)wi…

Error when checking input: expected dense_input to have shape (21,) but got array with shape (1,)

How to fix the input array to meet the input shape?I tried to transpose the input array, as described here, but an error is the same.ValueError: Error when checking input: expected dense_input to have…

Sort order when loading related objects using selectinload in SQLAlchemy

Is there a way to specify the sort order when loading related objects using the selectinload option in SQLAlchemy?My SQLAlchemy version: 1.2.10 My python version: 3.6.6

How to implement autovivification for nested dictionary ONLY when assigning values?

TL;DR How can I get superkeys to be autovivified in a Python dict when assigning values to subkeys, without also getting them autovivified when checking for subkeys?Background: Normally in Python, se…

How can I iterate across the photos on my connected iPhone from Windows 7 in Python?

When I connect my iPhone to my Windows 7 system, the Windows Explorer opens a Virtual Folder to the DCIM content. I can access the shell library interface via Pywin32 (218) as mentioned here: Can I use…