Skip to content

Commit 34b7b54

Browse files
abergmeier-dsfishlabsdean0x7d
authored andcommitted
Add tests for passing STL containers by pointer
`nullptr` is not expected to work in this case.
1 parent c67033a commit 34b7b54

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

tests/test_stl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,6 @@ TEST_SUBMODULE(stl, m) {
160160
};
161161
});
162162

163+
// test_stl_pass_by_pointer
164+
m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
163165
}

tests/test_stl.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,26 @@ def test_vec_of_reference_wrapper():
131131
"""#171: Can't return reference wrappers (or STL structures containing them)"""
132132
assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
133133
"[UserType(1), UserType(2), UserType(3), UserType(4)]"
134+
135+
136+
def test_stl_pass_by_pointer(msg):
137+
"""Passing nullptr or None to an STL container pointer is not expected to work"""
138+
with pytest.raises(TypeError) as excinfo:
139+
m.stl_pass_by_pointer() # default value is `nullptr`
140+
assert msg(excinfo.value) == """
141+
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
142+
1. (v: List[int]=None) -> List[int]
143+
144+
Invoked with:
145+
""" # noqa: E501 line too long
146+
147+
with pytest.raises(TypeError) as excinfo:
148+
m.stl_pass_by_pointer(None)
149+
assert msg(excinfo.value) == """
150+
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
151+
1. (v: List[int]=None) -> List[int]
152+
153+
Invoked with: None
154+
""" # noqa: E501 line too long
155+
156+
assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]

0 commit comments

Comments
 (0)