Skip to content
Snippets Groups Projects
Commit d48f1329 authored by Stefan Wunsch's avatar Stefan Wunsch
Browse files

[PyROOT exp] Port fix for adoption of empty std::vector

parent ed8bc7da
No related branches found
No related tags found
No related merge requests found
......@@ -33,7 +33,10 @@ def get_array_interface(self):
dtype_size = GetSizeOfType(dtype)
endianess = GetEndianess()
size = self.size()
pointer = GetVectorDataPointer(self, cppname)
if self.empty(): # Numpy sees a null pointer as error even though the data is never accessed.
pointer = 1
else:
pointer = GetVectorDataPointer(self, cppname)
return {
"shape": (size, ),
"typestr": "{}{}{}".format(endianess, dtype_numpy, dtype_size),
......
......@@ -72,14 +72,14 @@ PyObject *PyROOT::GetVectorDataPointer(PyObject * /*self*/, PyObject *args)
std::string cppname = CPyCppyy_PyUnicode_AsString(pycppname);
// Call interpreter to get pointer to data (using `data` method)
long pointer;
unsigned long long pointer;
std::stringstream code;
code << "*((long*)" << &pointer << ") = reinterpret_cast<long>(reinterpret_cast<" << cppname << "*>(" << cppobj
<< ")->data())";
gInterpreter->Calc(code.str().c_str());
// Return pointer as integer
PyObject *pypointer = PyInt_FromLong(pointer);
PyObject *pypointer = PyLong_FromUnsignedLongLong(pointer);
return pypointer;
}
......
......@@ -4,6 +4,11 @@ import numpy as np
class ArrayInterface(unittest.TestCase):
"""
Test memory adoption of std::vector and ROOT::RVec with the numpy
array interface.
"""
# Helpers
dtypes = [
"int", "unsigned int", "long", "unsigned long", "float", "double"
......@@ -32,6 +37,9 @@ class ArrayInterface(unittest.TestCase):
# Tests
def test_RVec(self):
"""
Test correct adoption of different datatypes for std::vector
"""
for dtype in self.dtypes:
root_obj = ROOT.ROOT.VecOps.RVec(dtype)(2)
np_obj = np.asarray(root_obj)
......@@ -39,12 +47,33 @@ class ArrayInterface(unittest.TestCase):
self.check_shape((2, ), np_obj)
def test_STLVector(self):
"""
Test correct adoption of different datatypes for ROOT::RVec
"""
for dtype in self.dtypes:
root_obj = ROOT.std.vector(dtype)(2)
np_obj = np.asarray(root_obj)
self.check_memory_adoption(root_obj, np_obj)
self.check_shape((2, ), np_obj)
def test_STLVector_empty(self):
"""
Test adoption of empty std::vector
"""
root_obj = ROOT.std.vector("float")()
np_obj = np.asarray(root_obj)
self.assertEqual(np_obj.shape, (0,))
self.assertEqual(np_obj.__array_interface__["data"][0], 1)
def test_RVec_empty(self):
"""
Test adoption of empty ROOT::RVec
"""
root_obj = ROOT.ROOT.VecOps.RVec("float")()
np_obj = np.asarray(root_obj)
self.assertEqual(np_obj.shape, (0,))
self.assertEqual(np_obj.__array_interface__["data"][0], 1)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment