"vscode:/vscode.git/clone" did not exist on "0ac4c8afd6484bc95b84061fb7651ca59e4701e6"
test_constants_and_functions.cpp 5.75 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
3
    tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw
    byte strings
Wenzel Jakob's avatar
Wenzel Jakob committed
4

5
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
6
7
8
9
10

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

Dean Moldovan's avatar
Dean Moldovan committed
11
#include "pybind11_tests.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
12

13
enum MyEnum { EFirstEntry = 1, ESecondEntry };
Wenzel Jakob's avatar
Wenzel Jakob committed
14

15
16
std::string test_function1() {
    return "test_function()";
Wenzel Jakob's avatar
Wenzel Jakob committed
17
18
}

19
std::string test_function2(MyEnum k) {
20
    return "test_function(enum=" + std::to_string(k) + ")";
Wenzel Jakob's avatar
Wenzel Jakob committed
21
22
}

23
24
std::string test_function3(int i) {
    return "test_function(" + std::to_string(i) + ")";
Wenzel Jakob's avatar
Wenzel Jakob committed
25
26
}

27
28
py::str test_function4()           { return "test_function()"; }
py::str test_function4(char *)     { return "test_function(char *)"; }
29
30
31
py::str test_function4(int, float) { return "test_function(int, float)"; }
py::str test_function4(float, int) { return "test_function(float, int)"; }

32
33
py::bytes return_bytes() {
    const char *data = "\x01\x00\x02\x00";
34
    return std::string(data, 4);
35
36
}

37
std::string print_bytes(const py::bytes &bytes) {
38
39
40
41
42
43
44
    std::string ret = "bytes[";
    const auto value = static_cast<std::string>(bytes);
    for (size_t i = 0; i < value.length(); ++i) {
        ret += std::to_string(static_cast<int>(value[i])) + " ";
    }
    ret.back() = ']';
    return ret;
45
46
}

47
48
49
// Test that we properly handle C++17 exception specifiers (which are part of the function signature
// in C++17).  These should all still work before C++17, but don't affect the function signature.
namespace test_exc_sp {
50
51
52
53
54
55
// [workaround(intel)] Unable to use noexcept instead of noexcept(true)
// Make the f1 test basically the same as the f2 test in C++17 mode for the Intel compiler as
// it fails to compile with a plain noexcept (tested with icc (ICC) 2021.1 Beta 20200827).
#if defined(__INTEL_COMPILER) && defined(PYBIND11_CPP17)
int f1(int x) noexcept(true) { return x+1; }
#else
56
int f1(int x) noexcept { return x+1; }
57
#endif
58
59
int f2(int x) noexcept(true) { return x+2; }
int f3(int x) noexcept(false) { return x+3; }
60
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
61
62
63
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wdeprecated"
#endif
64
// NOLINTNEXTLINE(modernize-use-noexcept)
65
int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
66
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
67
68
#  pragma GCC diagnostic pop
#endif
69
70
71
72
73
74
75
struct C {
    int m1(int x) noexcept { return x-1; }
    int m2(int x) const noexcept { return x-2; }
    int m3(int x) noexcept(true) { return x-3; }
    int m4(int x) const noexcept(true) { return x-4; }
    int m5(int x) noexcept(false) { return x-5; }
    int m6(int x) const noexcept(false) { return x-6; }
76
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
77
78
79
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wdeprecated"
#endif
80
81
82
83
    // NOLINTNEXTLINE(modernize-use-noexcept)
    int m7(int x) throw() { return x - 7; }
    // NOLINTNEXTLINE(modernize-use-noexcept)
    int m8(int x) const throw() { return x - 8; }
84
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
85
86
#  pragma GCC diagnostic pop
#endif
87
};
88
} // namespace test_exc_sp
89
90


91
92
TEST_SUBMODULE(constants_and_functions, m) {
    // test_constants
93
94
    m.attr("some_constant") = py::int_(14);

95
    // test_function_overloading
Wenzel Jakob's avatar
Wenzel Jakob committed
96
97
98
99
    m.def("test_function", &test_function1);
    m.def("test_function", &test_function2);
    m.def("test_function", &test_function3);

100
#if defined(PYBIND11_OVERLOAD_CAST)
101
102
    m.def("test_function", py::overload_cast<>(&test_function4));
    m.def("test_function", py::overload_cast<char *>(&test_function4));
103
104
105
    m.def("test_function", py::overload_cast<int, float>(&test_function4));
    m.def("test_function", py::overload_cast<float, int>(&test_function4));
#else
106
107
    m.def("test_function", static_cast<py::str (*)()>(&test_function4));
    m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
108
109
110
111
    m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
    m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
#endif

112
    py::enum_<MyEnum>(m, "MyEnum")
Wenzel Jakob's avatar
Wenzel Jakob committed
113
114
115
116
        .value("EFirstEntry", EFirstEntry)
        .value("ESecondEntry", ESecondEntry)
        .export_values();

117
    // test_bytes
118
119
    m.def("return_bytes", &return_bytes);
    m.def("print_bytes", &print_bytes);
120

121
    // test_exception_specifiers
122
    using namespace test_exc_sp;
123
    py::class_<C>(m, "C")
124
125
126
127
128
129
130
131
132
133
        .def(py::init<>())
        .def("m1", &C::m1)
        .def("m2", &C::m2)
        .def("m3", &C::m3)
        .def("m4", &C::m4)
        .def("m5", &C::m5)
        .def("m6", &C::m6)
        .def("m7", &C::m7)
        .def("m8", &C::m8)
        ;
134
135
    m.def("f1", f1);
    m.def("f2", f2);
136
137
138
139
#if defined(__INTEL_COMPILER)
#    pragma warning push
#    pragma warning disable 878 // incompatible exception specifications
#endif
140
    m.def("f3", f3);
141
142
143
#if defined(__INTEL_COMPILER)
#    pragma warning pop
#endif
144
    m.def("f4", f4);
145
146
147
148
149
150
151
152
153
154
155

    // test_function_record_leaks
    struct LargeCapture {
        // This should always be enough to trigger the alternative branch
        // where `sizeof(capture) > sizeof(rec->data)`
        uint64_t zeros[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    };
    m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
        LargeCapture capture;  // VS 2015's MSVC is acting up if we create the array here
        m.def("should_raise", [capture](int) { return capture.zeros[9] + 33; }, py::kw_only(), py::arg());
    });
156
157
158
159
160
161
162
163
    m.def("register_with_raising_repr", [](py::module_ m, const py::object &default_value) {
        m.def(
            "should_raise",
            [](int, int, const py::object &) { return 42; },
            "some docstring",
            py::arg_v("x", 42),
            py::arg_v("y", 42, "<the answer>"),
            py::arg_v("z", default_value));
164
    });
165
}