Skip to content

Commit

Permalink
regression tests for char16_t array data members
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Feb 19, 2022
1 parent 109b865 commit da6cb70
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ master: 2.3.0
* Only apply system_dirs patch (for asan) on Linux
* Add unloaded classes to namespaces in dir()
* Fix lookup of templates of function with template args
* Fix regression for accessing `char16_t` data member arrays
* Add custom `__reshape__` method to CPPInstance to allow array cast


2021-11-14: 2.2.0
Expand Down
10 changes: 8 additions & 2 deletions python/cppyy/ll.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

import cppyy
import warnings

try:
import __pypy__
Expand Down Expand Up @@ -75,8 +76,13 @@ def __getitem__(self, t):
return self
def __call__(self, size, managed=False):
res = self.func[self.array_type](size)
res.reshape((size,)+res.shape[1:])
if managed: res.__python_owns__ = True
try:
res.reshape((size,)+res.shape[1:])
if managed: res.__python_owns__ = True
except AttributeError:
res.__reshape__((size,))
if managed:
warnings.warn("managed low-level arrays of instances not supported")
return res

class CArraySizer(ArraySizer):
Expand Down
56 changes: 56 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,59 @@ class V;
'iii', 'ivi', 'vii', 'vvi',
'iiv', 'ivv', 'viv', 'vvv']:
assert getattr(ns, t)

def test39_char16_arrays(self):
"""Access to fixed-size char16 arrays as data members"""

import cppyy
import cppyy.ll
import warnings

cppyy.cppdef(r"""\
namespace Char16Fixed {
struct AxisInformation {
char16_t name[6];
};
void fillem(AxisInformation* a, int N) {
char16_t fill[] = {u'h', u'e', u'l', u'l', u'o', u'\0'};
for (int i = 0; i < N; ++i)
memcpy(a[i].name, fill, sizeof(fill));
}}""")

N = 10

ns = cppyy.gbl.Char16Fixed

ai = ns.AxisInformation()
for s in [u'hello', u'hellow']:
ai.name = s
len(ai.name) == 6
assert ai.name[:len(s)] == s

with warnings.catch_warnings(record=True) as w:
ai.name = u'hellowd'
assert 'too long' in str(w[-1].message)

# vector of objects
va = cppyy.gbl.std.vector[ns.AxisInformation](N)
ns.fillem(va.data(), N)
for ai in va:
assert len(ai.name) == 6
assert ai.name[:5] == u'hello'

# array of objects
aa = cppyy.gbl.std.array[ns.AxisInformation, N]()
ns.fillem(aa.data(), N)
for ai in aa:
assert len(ai.name) == 6
assert ai.name[:5] == u'hello'

# low-level array of objects
aa = cppyy.ll.array_new[ns.AxisInformation](N)
ns.fillem(aa, N)
for ai in aa:
assert len(ai.name) == 6
assert ai.name[:5] == u'hello'
cppyy.ll.array_delete(aa)

0 comments on commit da6cb70

Please sign in to comment.