cppyabm  1.0.17
An agent-based library to integrate C++ and Python
test_constants_and_functions.cpp
Go to the documentation of this file.
1 /*
2  tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
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.
8 */
9 
10 #include "pybind11_tests.h"
11 
13 
14 std::string test_function1() {
15  return "test_function()";
16 }
17 
18 std::string test_function2(MyEnum k) {
19  return "test_function(enum=" + std::to_string(k) + ")";
20 }
21 
22 std::string test_function3(int i) {
23  return "test_function(" + std::to_string(i) + ")";
24 }
25 
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)"; }
30 
31 py::bytes return_bytes() {
32  const char *data = "\x01\x00\x02\x00";
33  return std::string(data, 4);
34 }
35 
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])) + " ";
41  }
42  ret.back() = ']';
43  return ret;
44 }
45 
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 // [workaround(intel)] Unable to use noexcept instead of noexcept(true)
50 // Make the f1 test basically the same as the f2 test in C++17 mode for the Intel compiler as
51 // it fails to compile with a plain noexcept (tested with icc (ICC) 2021.1 Beta 20200827).
52 #if defined(__INTEL_COMPILER) && defined(PYBIND11_CPP17)
53 int f1(int x) noexcept(true) { return x+1; }
54 #else
55 int f1(int x) noexcept { return x+1; }
56 #endif
57 int f2(int x) noexcept(true) { return x+2; }
58 int f3(int x) noexcept(false) { return x+3; }
59 #if defined(__GNUG__)
60 # pragma GCC diagnostic push
61 # pragma GCC diagnostic ignored "-Wdeprecated"
62 #endif
63 int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
64 #if defined(__GNUG__)
65 # pragma GCC diagnostic pop
66 #endif
67 struct C {
68  int m1(int x) noexcept { return x-1; }
69  int m2(int x) const noexcept { return x-2; }
70  int m3(int x) noexcept(true) { return x-3; }
71  int m4(int x) const noexcept(true) { return x-4; }
72  int m5(int x) noexcept(false) { return x-5; }
73  int m6(int x) const noexcept(false) { return x-6; }
74 #if defined(__GNUG__)
75 # pragma GCC diagnostic push
76 # pragma GCC diagnostic ignored "-Wdeprecated"
77 #endif
78  int m7(int x) throw() { return x-7; }
79  int m8(int x) const throw() { return x-8; }
80 #if defined(__GNUG__)
81 # pragma GCC diagnostic pop
82 #endif
83 };
84 } // namespace test_exc_sp
85 
86 
87 TEST_SUBMODULE(constants_and_functions, m) {
88  // test_constants
89  m.attr("some_constant") = py::int_(14);
90 
91  // test_function_overloading
92  m.def("test_function", &test_function1);
93  m.def("test_function", &test_function2);
94  m.def("test_function", &test_function3);
95 
96 #if defined(PYBIND11_OVERLOAD_CAST)
97  m.def("test_function", py::overload_cast<>(&test_function4));
98  m.def("test_function", py::overload_cast<char *>(&test_function4));
99  m.def("test_function", py::overload_cast<int, float>(&test_function4));
100  m.def("test_function", py::overload_cast<float, int>(&test_function4));
101 #else
102  m.def("test_function", static_cast<py::str (*)()>(&test_function4));
103  m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
104  m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
105  m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
106 #endif
107 
108  py::enum_<MyEnum>(m, "MyEnum")
109  .value("EFirstEntry", EFirstEntry)
110  .value("ESecondEntry", ESecondEntry)
111  .export_values();
112 
113  // test_bytes
114  m.def("return_bytes", &return_bytes);
115  m.def("print_bytes", &print_bytes);
116 
117  // test_exception_specifiers
118  using namespace test_exc_sp;
119  py::class_<C>(m, "C")
120  .def(py::init<>())
121  .def("m1", &C::m1)
122  .def("m2", &C::m2)
123  .def("m3", &C::m3)
124  .def("m4", &C::m4)
125  .def("m5", &C::m5)
126  .def("m6", &C::m6)
127  .def("m7", &C::m7)
128  .def("m8", &C::m8)
129  ;
130  m.def("f1", f1);
131  m.def("f2", f2);
132  m.def("f3", f3);
133  m.def("f4", f4);
134 
135  // test_function_record_leaks
136  struct LargeCapture {
137  // This should always be enough to trigger the alternative branch
138  // where `sizeof(capture) > sizeof(rec->data)`
139  uint64_t zeros[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
140  };
141  m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
142  LargeCapture capture; // VS 2015's MSVC is acting up if we create the array here
143  m.def("should_raise", [capture](int) { return capture.zeros[9] + 33; }, py::kw_only(), py::arg());
144  });
145  m.def("register_with_raising_repr", [](py::module_ m, py::object default_value) {
146  m.def("should_raise", [](int, int, py::object) { return 42; }, "some docstring",
147  py::arg_v("x", 42), py::arg_v("y", 42, "<the answer>"), py::arg_v("z", default_value));
148  });
149 }
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
TEST_SUBMODULE
TEST_SUBMODULE(constants_and_functions, m)
Definition: test_constants_and_functions.cpp:87
test_exc_sp::C::m6
int m6(int x) const noexcept(false)
Definition: test_constants_and_functions.cpp:73
bytes
Definition: pytypes.h:1013
test_builtin_casters.x
x
Definition: test_builtin_casters.py:467
data
arr data(const arr &a, Ix... index)
Definition: test_numpy_array.cpp:82
test_exc_sp::f3
int f3(int x) noexcept(false)
Definition: test_constants_and_functions.cpp:58
test_exc_sp
Definition: test_constants_and_functions.cpp:48
test_exc_sp::C::m3
int m3(int x) noexcept(true)
Definition: test_constants_and_functions.cpp:70
test_exc_sp::f4
int f4(int x)
Definition: test_constants_and_functions.cpp:63
test_exc_sp::f1
int f1(int x) noexcept
Definition: test_constants_and_functions.cpp:55
return_bytes
py::bytes return_bytes()
Definition: test_constants_and_functions.cpp:31
ESecondEntry
@ ESecondEntry
Definition: test_constants_and_functions.cpp:12
conftest.capture
def capture(capsys)
Definition: conftest.py:124
test_exc_sp::C::m7
int m7(int x)
Definition: test_constants_and_functions.cpp:78
test_exc_sp::f2
int f2(int x) noexcept(true)
Definition: test_constants_and_functions.cpp:57
test_exc_sp::C::m1
int m1(int x) noexcept
Definition: test_constants_and_functions.cpp:68
test_function3
std::string test_function3(int i)
Definition: test_constants_and_functions.cpp:22
test_function1
std::string test_function1()
Definition: test_constants_and_functions.cpp:14
test_exc_sp::C
Definition: test_constants_and_functions.cpp:67
test_exc_sp::C::m5
int m5(int x) noexcept(false)
Definition: test_constants_and_functions.cpp:72
MyEnum
MyEnum
Definition: test_constants_and_functions.cpp:12
test_exc_sp::C::m2
int m2(int x) const noexcept
Definition: test_constants_and_functions.cpp:69
pybind11_tests.h
test_exc_sp::C::m8
int m8(int x) const
Definition: test_constants_and_functions.cpp:79
test_function2
std::string test_function2(MyEnum k)
Definition: test_constants_and_functions.cpp:18
test_function4
py::str test_function4()
Definition: test_constants_and_functions.cpp:26
test_exc_sp::C::m4
int m4(int x) const noexcept(true)
Definition: test_constants_and_functions.cpp:71
EFirstEntry
@ EFirstEntry
Definition: test_constants_and_functions.cpp:12
test_async.m
m
Definition: test_async.py:5
print_bytes
std::string print_bytes(py::bytes bytes)
Definition: test_constants_and_functions.cpp:36
test_callbacks.value
value
Definition: test_callbacks.py:126