Skip to content

Commit 42b131f

Browse files
committed
Demonstrate issue from #2786
1 parent 8449a80 commit 42b131f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

tests/test_pytypes.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ TEST_SUBMODULE(pytypes, m) {
270270
throw std::runtime_error("Invalid type");
271271
});
272272

273+
// test_implicit_casting
273274
m.def("get_implicit_casting", []() {
274275
py::dict d;
275276
d["char*_i1"] = "abc";
@@ -307,6 +308,10 @@ TEST_SUBMODULE(pytypes, m) {
307308
);
308309
});
309310

311+
// (see also test_builtin_casters)
312+
m.def("implicitly_cast_to_int32", [](py::int_ value) { return int32_t{value}; });
313+
m.def("implicitly_cast_to_uint32", [](py::int_ value) { return uint32_t{value}; });
314+
310315
// test_print
311316
m.def("print_function", []() {
312317
py::print("Hello, World!");

tests/test_pytypes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ def test_implicit_casting():
331331
}
332332
assert z["l"] == [3, 6, 9, 12, 15]
333333

334+
assert m.implicitly_cast_to_int32(42) == 42
335+
assert m.implicitly_cast_to_int32(2 ** 31 - 1) == 2 ** 31 - 1
336+
assert m.implicitly_cast_to_int32(2 ** 31)
337+
338+
assert m.implicitly_cast_to_uint32(42) == 42
339+
assert m.implicitly_cast_to_uint32(2 ** 32 - 1) == 2 ** 32 - 1
340+
assert m.implicitly_cast_to_uint32(2 ** 32)
341+
334342

335343
def test_print(capture):
336344
with capture:

0 commit comments

Comments
 (0)