Skip to content

Commit 5ee1114

Browse files
authored
Merge branch 'root-project:master' into master
2 parents 1de31a0 + c89362d commit 5ee1114

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_stl_vector.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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. #
@@ -11,6 +11,17 @@
1111
from . import pythonization
1212
from ._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)
1627
def 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

bindings/pyroot/pythonizations/test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_decorator pythonization_decorator.py)
2626
ROOT_ADD_PYUNITTEST(pyroot_pyz_pretty_printing pretty_printing.py)
2727
ROOT_ADD_PYUNITTEST(pyroot_pyz_array_interface array_interface.py PYTHON_DEPS numpy)
2828

29+
# STL vector pythonizations
30+
ROOT_ADD_PYUNITTEST(pyroot_pyz_stl_vector stl_vector.py)
31+
2932
# TObject and subclasses pythonisations
3033
ROOT_ADD_PYUNITTEST(pyroot_pyz_tobject_contains tobject_contains.py)
3134
ROOT_ADD_PYUNITTEST(pyroot_pyz_tobject_comparisonops tobject_comparisonops.py)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
import ROOT
3+
4+
5+
class STL_vector(unittest.TestCase):
6+
"""
7+
Tests for the pythonizations of std::vector.
8+
"""
9+
10+
def test_vec_char_data(self):
11+
'''
12+
Test that calling std::vector<char>::data() returns a Python string
13+
that contains the characters of the vector and no exception is raised.
14+
Check also that the iteration over the vector runs normally (#9632).
15+
'''
16+
17+
elems = ['a','b','c']
18+
v = ROOT.std.vector['char'](elems)
19+
self.assertEqual(v.data(), ''.join(elems))
20+
21+
for elem in v:
22+
self.assertEqual(elem, elems.pop(0))
23+
24+
if __name__ == '__main__':
25+
unittest.main()

0 commit comments

Comments
 (0)