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
3  byte strings
4 
5  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6 
7  All rights reserved. Use of this source code is governed by a
8  BSD-style license that can be found in the LICENSE file.
9 */
10 
11 #include "pybind11_tests.h"
12 
14 
15 std::string test_function1() { return "test_function()"; }
16 
17 std::string test_function2(MyEnum k) { return "test_function(enum=" + std::to_string(k) + ")"; }
18 
19 std::string test_function3(int i) { return "test_function(" + std::to_string(i) + ")"; }
20 
21 py::str test_function4() { return "test_function()"; }
22 py::str test_function4(char *) { return "test_function(char *)"; }
23 py::str test_function4(int, float) { return "test_function(int, float)"; }
24 py::str test_function4(float, int) { return "test_function(float, int)"; }
25 
26 py::bytes return_bytes() {
27  const char *data = "\x01\x00\x02\x00";
28  return std::string(data, 4);
29 }
30 
31 std::string print_bytes(const py::bytes &bytes) {
32  std::string ret = "bytes[";
33  const auto value = static_cast<std::string>(bytes);
34  for (char c : value) {
35  ret += std::to_string(static_cast<int>(c)) + ' ';
36  }
37  ret.back() = ']';
38  return ret;
39 }
40 
41 // Test that we properly handle C++17 exception specifiers (which are part of the function
42 // signature in C++17). These should all still work before C++17, but don't affect the function
43 // signature.
44 namespace test_exc_sp {
45 // [workaround(intel)] Unable to use noexcept instead of noexcept(true)
46 // Make the f1 test basically the same as the f2 test in C++17 mode for the Intel compiler as
47 // it fails to compile with a plain noexcept (tested with icc (ICC) 2021.1 Beta 20200827).
48 #if defined(__INTEL_COMPILER) && defined(PYBIND11_CPP17)
49 int f1(int x) noexcept(true) { return x + 1; }
50 #else
51 int f1(int x) noexcept { return x + 1; }
52 #endif
53 int f2(int x) noexcept(true) { return x + 2; }
54 int f3(int x) noexcept(false) { return x + 3; }
55 PYBIND11_WARNING_PUSH
56 PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated")
57 #if defined(__clang_major__) && __clang_major__ >= 5
58 PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated-dynamic-exception-spec")
59 #else
60 PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated")
61 #endif
62 // NOLINTNEXTLINE(modernize-use-noexcept)
63 int f4(int x) throw() { return x + 4; } // Deprecated equivalent to noexcept(true)
65 struct C {
66  int m1(int x) noexcept { return x - 1; }
67  int m2(int x) const noexcept { return x - 2; }
68  int m3(int x) noexcept(true) { return x - 3; }
69  int m4(int x) const noexcept(true) { return x - 4; }
70  int m5(int x) noexcept(false) { return x - 5; }
71  int m6(int x) const noexcept(false) { return x - 6; }
72  PYBIND11_WARNING_PUSH
73  PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated")
74  PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated")
75  // NOLINTNEXTLINE(modernize-use-noexcept)
76  int m7(int x) throw() { return x - 7; }
77  // NOLINTNEXTLINE(modernize-use-noexcept)
78  int m8(int x) const throw() { return x - 8; }
80 };
81 } // namespace test_exc_sp
82 
83 TEST_SUBMODULE(constants_and_functions, m) {
84  // test_constants
85  m.attr("some_constant") = py::int_(14);
86 
87  // test_function_overloading
88  m.def("test_function", &test_function1);
89  m.def("test_function", &test_function2);
90  m.def("test_function", &test_function3);
91 
92 #if defined(PYBIND11_OVERLOAD_CAST)
93  m.def("test_function", py::overload_cast<>(&test_function4));
94  m.def("test_function", py::overload_cast<char *>(&test_function4));
95  m.def("test_function", py::overload_cast<int, float>(&test_function4));
96  m.def("test_function", py::overload_cast<float, int>(&test_function4));
97 #else
98  m.def("test_function", static_cast<py::str (*)()>(&test_function4));
99  m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
100  m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
101  m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
102 #endif
103 
104  py::enum_<MyEnum>(m, "MyEnum")
105  .value("EFirstEntry", EFirstEntry)
106  .value("ESecondEntry", ESecondEntry)
107  .export_values();
108 
109  // test_bytes
110  m.def("return_bytes", &return_bytes);
111  m.def("print_bytes", &print_bytes);
112 
113  // test_exception_specifiers
114  using namespace test_exc_sp;
115  py::class_<C>(m, "C")
116  .def(py::init<>())
117  .def("m1", &C::m1)
118  .def("m2", &C::m2)
119  .def("m3", &C::m3)
120  .def("m4", &C::m4)
121  .def("m5", &C::m5)
122  .def("m6", &C::m6)
123  .def("m7", &C::m7)
124  .def("m8", &C::m8);
125  m.def("f1", f1);
126  m.def("f2", f2);
127 
128  PYBIND11_WARNING_PUSH
129  PYBIND11_WARNING_DISABLE_INTEL(878) // incompatible exception specifications
130  m.def("f3", f3);
132 
133  m.def("f4", f4);
134 
135  // test_function_record_leaks
136  m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
137  // This should always be enough to trigger the alternative branch
138  // where `sizeof(capture) > sizeof(rec->data)`
139  uint64_t capture[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
140 #if defined(__GNUC__) && __GNUC__ == 4 // CentOS7
142 #endif
143  m.def(
144  "should_raise", [capture](int) { return capture[9] + 33; }, py::kw_only(), py::arg());
145  });
146  m.def("register_with_raising_repr", [](py::module_ m, const py::object &default_value) {
147  m.def(
148  "should_raise",
149  [](int, int, const py::object &) { return 42; },
150  "some docstring",
151  py::arg_v("x", 42),
152  py::arg_v("y", 42, "<the answer>"),
153  py::arg_v("z", default_value));
154  });
155 
156  // test noexcept(true) lambda (#4565)
157  m.def("l1", []() noexcept(true) { return 0; });
158 }
TEST_SUBMODULE
TEST_SUBMODULE(constants_and_functions, m)
Definition: test_constants_and_functions.cpp:83
print_bytes
std::string print_bytes(const py::bytes &bytes)
Definition: test_constants_and_functions.cpp:31
test_exc_sp::C::m1
int m1(int x) noexcept
Definition: test_constants_and_functions.cpp:66
bytes
Definition: pytypes.h:1662
test_exc_sp::C::m3
int m3(int x) noexcept(true)
Definition: test_constants_and_functions.cpp:68
PYBIND11_WARNING_DISABLE_GCC
#define PYBIND11_WARNING_DISABLE_GCC(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:67
test_exc_sp::f4
PYBIND11_WARNING_PUSH int f4(int x)
Definition: test_constants_and_functions.cpp:63
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
ret
DenseIndex ret
Definition: level1_cplx_impl.h:44
PYBIND11_WARNING_POP
PYBIND11_WARNING_PUSH PYBIND11_WARNING_POP
Definition: tensor.h:30
test_exc_sp::f3
int f3(int x) noexcept(false)
Definition: test_constants_and_functions.cpp:54
test_exc_sp
Definition: test_constants_and_functions.cpp:44
test_exc_sp::C
Definition: test_constants_and_functions.cpp:65
test_exc_sp::C::m2
int m2(int x) const noexcept
Definition: test_constants_and_functions.cpp:67
test_exc_sp::C::m8
int m8(int x) const
Definition: test_constants_and_functions.cpp:78
test_exc_sp::f1
int f1(int x) noexcept
Definition: test_constants_and_functions.cpp:51
data
int data[]
Definition: Map_placement_new.cpp:1
return_bytes
py::bytes return_bytes()
Definition: test_constants_and_functions.cpp:26
test_exc_sp::C::m7
PYBIND11_WARNING_PUSH int m7(int x)
Definition: test_constants_and_functions.cpp:76
test_exc_sp::C::m5
int m5(int x) noexcept(false)
Definition: test_constants_and_functions.cpp:70
ESecondEntry
@ ESecondEntry
Definition: test_constants_and_functions.cpp:13
conftest.capture
def capture(capsys)
Definition: conftest.py:140
test_exc_sp::C::m6
int m6(int x) const noexcept(false)
Definition: test_constants_and_functions.cpp:71
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
test_exc_sp::f2
int f2(int x) noexcept(true)
Definition: test_constants_and_functions.cpp:53
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
test_function3
std::string test_function3(int i)
Definition: test_constants_and_functions.cpp:19
silence_unused_warnings
void silence_unused_warnings(Args &&...)
Definition: wrap/pybind11/include/pybind11/detail/common.h:1222
test_function1
std::string test_function1()
Definition: test_constants_and_functions.cpp:15
MyEnum
MyEnum
Definition: test_constants_and_functions.cpp:13
pybind11_tests.h
PYBIND11_WARNING_DISABLE_INTEL
#define PYBIND11_WARNING_DISABLE_INTEL(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:73
test_exc_sp::C::m4
int m4(int x) const noexcept(true)
Definition: test_constants_and_functions.cpp:69
test_function2
std::string test_function2(MyEnum k)
Definition: test_constants_and_functions.cpp:17
test_function4
py::str test_function4()
Definition: test_constants_and_functions.cpp:21
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
EFirstEntry
@ EFirstEntry
Definition: test_constants_and_functions.cpp:13
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
PYBIND11_WARNING_DISABLE_CLANG
#define PYBIND11_WARNING_DISABLE_CLANG(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:61


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:00