How to pass a NULL pointer as a function argument?

Hi,

let’s say we have a function like this one (fun.C):

#include <iostream>
void fun(const char* name){
	if(name)
    	std::cout << "name=" << name << std::endl;  
	else
		std::cout << "name is NULL" << std::endl;
}

and I would like to call this function and pass a NULL pointer as an argument (run.py):

[code]import ROOT

ROOT.gROOT.ProcessLine(".L fun.C+")

#ROOT.fun(“something”) # this works without problems
ROOT.fun(None) # doesn’t work
ROOT.fun(ROOT.NULL) # doesn’t work
ROOT.fun(0) # doesn’t work[/code]

unfortunately none of the last tree lines works:

[code]
----> 6 ROOT.fun(None) # doesn’t work
7 ROOT.fun(ROOT.NULL) # doesn’t work
8 ROOT.fun(0) # doesn’t work

TypeError: void ::fun(const char* name) =>
could not convert argument 1 (expected string or Unicode object, NoneType found)[/code]

Is it possible to pass a C/C++ null pointer as an argument from python?

Cheers,
Jiri
run.py (161 Bytes)
fun.C (160 Bytes)

Jiri,

null-pointer in general, yes, but not through a const char* … one of those philosophical choices whether the code should behave more “python-like” or more “C-like”.

Cheers,
Wim

Hi Wim,

ok, and what about a const double* (doesn’t work for me either), is it the same problem?

Thanks,
Jiri

Jiri,

not the same, but similar: the const char* checks whether it receives an actual python string object, the double* code checks whether it receives a buffer that fits doubles, and is of at least size 1. The first is philosophical, the second is a matter of clean (internal, e.g. for overloading) error handling. Note that of the built-in types, void* is fare game, though … it accepts None, 0, arbitrary CObjects, and of course null pointers.

Is there a specific example that is limiting you?

Cheers,
Wim

Hi Wim,

I had a problem how to pass params = 0 to:

but I solved it passing params=0 on C-side and not python-side.

I’ve also tried to load this function:

const double * nullConstDoublePointer(){ return 0; }

and then call a function taking const double* , similarly as in my first post: ROOT.fun(ROOT.nullConstDoublePointer()) but it seems that it is equivalent to calling just ROOT.fun(0) (it also doesn’t work).

It is not limiting me too much right now but it is good to know that there is such a limitation with null pointers

Thanks,
Jiri

Jiri,

yes, the nullConstDoublePointer() result will be a buffer of size 0, so that fails to pass. As for the other, sounds like it’s time to work on keyword arguments again, or at least to allow any value that is the default.

Cheers,
Wim

Hi,

This would be amazing! It is already on my private wishlist :wink:

Cheers,
Jiri

Jiri,

up to an extent … the problem I’ve always had with keyword arguments is that argument names don’t have any real meaning on the C++ side, and they don’t even need to be provided in the header files (that the dictionary generator sees). OTOH, once a header file reaches a certain age, nobody is going to touch it anyway, so perhaps the argument names are stable enough for most of ROOT.

But yes, from a technical POV, it’d be cute. (And it’d solve this particular calling issue.)

Cheers,
Wim