1- # Author: Stefan Wunsch CERN 08/2018
1+ # Author: Stefan Wunsch, Enric Tejedor CERN 08/2018
22
33################################################################################
44# Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. #
1111from . import pythonization
1212from ._rvec import add_array_interface_property
1313
14+ def _data_vec_char (self ):
15+ # vector<char>::data() returns char*.
16+ # Cppyy attemps to convert char* into Python string, but if the
17+ # character sequence is not null-terminated the conversion fails.
18+ # This is likely to happen with the result of vector<char>::data().
19+ # For the conversion char* -> str to succeed when calling data(),
20+ # temporarily append a null character to the vector<char>.
21+ self .push_back ('\0 ' )
22+ d = self ._original_data ()
23+ self .pop_back ()
24+ return d
1425
1526@pythonization ("vector<" , ns = "std" , is_prefix = True )
1627def pythonize_stl_vector (klass , name ):
@@ -21,3 +32,8 @@ def pythonize_stl_vector(klass, name):
2132 # Add numpy array interface
2233 # NOTE: The pythonization is reused from ROOT::VecOps::RVec
2334 add_array_interface_property (klass , name )
35+
36+ # Inject custom vector<char>::data()
37+ if klass .value_type == 'char' :
38+ klass ._original_data = klass .data
39+ klass .data = _data_vec_char
0 commit comments