Skip to content

Commit

Permalink
regression tests for converting std::vector<T* to list
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Jun 26, 2022
1 parent d3f85f8 commit 687d32d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
7 changes: 4 additions & 3 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ master: 2.4.0
* Support for free (templated) functions in Numba
* Basic support for unboxing C++ public data members in Numba
* Basic support for calling methods of C++ structs in Numba
* Added conventional __cpp_reflex__ method for inspection in Numba
* Added conventional `__cpp_reflex__` method for inspection in Numba
* Support for globally overloaded ordering operators
* Special cases for __repr__/__str__ returning C++ stringy types
* Special cases for `__repr__`/`__str__` returning C++ stringy types
* Fix lookup of templates of function with template args
* Correct typing of int8_t/uint8_t enums
* Basic support for hidden enums
* Support function pointer returns and optimize function point variables
* Fix reuse of CPPOverload proxies in vector calls from different threads
* Use -march=native instead of checking the cpu for avx
* Use `-march=native` instead of checking the cpu for avx
* Workaround for handling exceptions from JITed code on ARM
* Drop ``from cppyy.interactive import *`` from CPython 3.11
* Fix regression in converting `std::vector<T*` to `list`


2022-04-03: 2.3.1
Expand Down
67 changes: 67 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,70 @@ def test38_char16_arrays(self):
assert ai.name[:5] == u'hello'
cppyy.ll.array_delete(aa)

def test39_vector_of_pointers_conversion(self):
"""vector<T*>'s const T*& used to be T**, now T*"""

import cppyy

cppyy.cppdef(r"""\
namespace VectorOfPointers {
struct Base1 { std::string name; explicit Base1(const std::string& name) : name(name) { }};
struct Derived1 : Base1 { explicit Derived1(const std::string& name) : Base1(name) { } };
struct Owner {
Derived1 d1 { "d1" };
Derived1 d2 { "d2" };
std::vector<const Base1*> GetVector() { return { &d1, &d2 }; }
}; }""")

cppyy.gbl.VectorOfPointers
from cppyy.gbl.VectorOfPointers import Base1, Derived1, Owner

o = Owner()

assert len(o.GetVector()) == 2
assert type(o.GetVector()[0]) == Base1
assert type(o.GetVector()[1]) == Base1
assert o.GetVector()[0].name == "d1"
assert o.GetVector()[1].name == "d2"

v = o.GetVector()
assert len(list(v)) == 2
assert list(v)[0].name == "d1"
assert list(v)[1].name == "d2"

assert len(list(o.GetVector())) == 2
assert list(o.GetVector())[0].name == "d1"
assert list(o.GetVector())[1].name == "d2"

l = list(v)
v.__destruct__()
assert l[0].name == "d1"
assert l[1].name == "d2"

cppyy.cppdef(r"""\
namespace VectorOfPointers {
struct Base2 { };
struct Derived2 : Base2 { };
struct Base3 { virtual ~Base3() noexcept = default; };
struct Derived3 : Base3 { };
Derived2 d2;
Derived3 d3;
std::vector<const Base2*> vec2 { &d2 };
std::vector<const Base3*> vec3 { &d3 };
}""")

from cppyy.gbl import std
from cppyy.gbl.VectorOfPointers import Base2, Derived2, Base3, Derived3, vec2, vec3

assert len(vec2) == 1
assert type(vec2[0]) == Base2
assert len(list(vec2)) == 1
assert type(list(vec2)[0]) == Base2
assert len([d for d in vec2 if isinstance(d, Derived2)]) == 0

assert len(vec3) == 1
assert type(vec3[0]) == Derived3
assert len(list(vec3)) == 1
assert type(list(vec2)[0]) == Base2
assert len([d for d in vec3 if isinstance(d, Derived3)]) == 1

0 comments on commit 687d32d

Please sign in to comment.