Check for buffer vs bounded collection

Hi, I’m hoping to write a single python function using PyROOT that can take either:

  • two ordered collections like lists, numpy arrays, etc, or
  • two “buffer” objects as often returned by PyROOT (e.g. TGraph::GetX())

Is there a good pythonic way to check which kind of object was passed? Or a good try-except block? Unfortunately I can’t just assume the first, because ROOT will seg fault when trying to get len() if it’s just a buffer.

I know I could just check the type() of the object, hence asking for a pythonic way. I could also just convert everything into a numpy.array, but then I’d have to worry about the dtypes changing and such.

Jean-Francois

Jean-Francois,

what does “seg fault when trying to get len()” mean? The call to len() should work, but it’ll be UINT_MAX, and so can’t be used for looping (which would segfault eventually).

Is the number of cases such as TGraph::GetX() that you have to deal with known and limited? If yes and yes, these can be decorated, using the buffer’s SetSize() method. Once the proper size is set, iteration works fine.

What is the current behavior (irrespective of checking)? I.e. how is the size known in the method called?

Cheers,
Wim

Right, the seg fault isn’t on len() but on trying to put the contents in a list or python array. I think as you say, my number of cases is known and limited, so I will just copy the contents into a numpy.array with count = g.GetN() as before.

Jean-François

Jean-Francois,

no, I meant decorated in Pythonize.cxx. But then I do need to know the list of methods you’re interested in.

Cheers,
Wim