2 tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
10 #include "pybind11_tests.h"
12 enum MyEnum
{ EFirstEntry
= 1, ESecondEntry
};
14 std::string
test_function1() {
15 return "test_function()";
18 std::string
test_function2(MyEnum k
) {
19 return "test_function(enum=" + std::to_string(k
) + ")";
22 std::string
test_function3(int i
) {
23 return "test_function(" + std::to_string(i
) + ")";
26 py::str
test_function4() { return "test_function()"; }
27 py::str
test_function4(char *) { return "test_function(char *)"; }
28 py::str
test_function4(int, float) { return "test_function(int, float)"; }
29 py::str
test_function4(float, int) { return "test_function(float, int)"; }
31 py::bytes
return_bytes() {
32 const char *data
= "\x01\x00\x02\x00";
33 return std::string(data
, 4);
36 std::string
print_bytes(py::bytes bytes
) {
37 std::string ret
= "bytes[";
38 const auto value
= static_cast<std::string
>(bytes
);
39 for (size_t i
= 0; i
< value
.length(); ++i
) {
40 ret
+= std::to_string(static_cast<int>(value
[i
])) + " ";
46 // Test that we properly handle C++17 exception specifiers (which are part of the function signature
47 // in C++17). These should all still work before C++17, but don't affect the function signature.
48 namespace test_exc_sp
{
49 int f1(int x
) noexcept
{ return x
+1; }
50 int f2(int x
) noexcept(true) { return x
+2; }
51 int f3(int x
) noexcept(false) { return x
+3; }
53 # pragma GCC diagnostic push
54 # pragma GCC diagnostic ignored "-Wdeprecated"
56 int f4(int x
) throw() { return x
+4; } // Deprecated equivalent to noexcept(true)
58 # pragma GCC diagnostic pop
61 int m1(int x
) noexcept
{ return x
-1; }
62 int m2(int x
) const noexcept
{ return x
-2; }
63 int m3(int x
) noexcept(true) { return x
-3; }
64 int m4(int x
) const noexcept(true) { return x
-4; }
65 int m5(int x
) noexcept(false) { return x
-5; }
66 int m6(int x
) const noexcept(false) { return x
-6; }
68 # pragma GCC diagnostic push
69 # pragma GCC diagnostic ignored "-Wdeprecated"
71 int m7(int x
) throw() { return x
-7; }
72 int m8(int x
) const throw() { return x
-8; }
74 # pragma GCC diagnostic pop
80 TEST_SUBMODULE(constants_and_functions
, m
) {
82 m
.attr("some_constant") = py::int_(14);
84 // test_function_overloading
85 m
.def("test_function", &test_function1
);
86 m
.def("test_function", &test_function2
);
87 m
.def("test_function", &test_function3
);
89 #if defined(PYBIND11_OVERLOAD_CAST)
90 m
.def("test_function", py::overload_cast
<>(&test_function4
));
91 m
.def("test_function", py::overload_cast
<char *>(&test_function4
));
92 m
.def("test_function", py::overload_cast
<int, float>(&test_function4
));
93 m
.def("test_function", py::overload_cast
<float, int>(&test_function4
));
95 m
.def("test_function", static_cast<py::str (*)()>(&test_function4
));
96 m
.def("test_function", static_cast<py::str (*)(char *)>(&test_function4
));
97 m
.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4
));
98 m
.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4
));
101 py::enum_
<MyEnum
>(m
, "MyEnum")
102 .value("EFirstEntry", EFirstEntry
)
103 .value("ESecondEntry", ESecondEntry
)
107 m
.def("return_bytes", &return_bytes
);
108 m
.def("print_bytes", &print_bytes
);
110 // test_exception_specifiers
111 using namespace test_exc_sp
;
112 py::class_
<C
>(m
, "C")