Commit 291aee6
committed
[PyROOT][9632] Fix iteration on std::vector<char>
As part of the implementation of std.vector.__iter__, the vector's
data() method is called. In the case of a vector<char>, data()
returns a char*, which is converted by cppyy to a Python string.
The issue is that the conversion char*->str expects that the
sequence of characters to convert is null-terminated, e.g. in
Python3 the following function is used for the conversion:
https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FromString
In the case of the data pointed by the char* returned by data(),
there is no guarantee that the sequence of characters will be
null-terminated, and this results in unpredictable and erroneous
behaviour.
The fix of this commit adds a pythonization for vector<char>::data()
that temporarily adds a null character to the vector before calling
the actual data(), so that the conversion is done correctly and
the returned Python string contains the characters of the vector.
This also fixes the vector<char> iteration, which relies on data().1 parent e3d6a83 commit 291aee6
1 file changed
Lines changed: 17 additions & 1 deletion
Lines changed: 17 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
14 | 25 | | |
15 | 26 | | |
16 | 27 | | |
| |||
21 | 32 | | |
22 | 33 | | |
23 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
0 commit comments