Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:52

0001 /*
0002     tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw
0003     byte strings
0004 
0005     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
0006 
0007     All rights reserved. Use of this source code is governed by a
0008     BSD-style license that can be found in the LICENSE file.
0009 */
0010 
0011 #include "pybind11_tests.h"
0012 
0013 enum MyEnum { EFirstEntry = 1, ESecondEntry };
0014 
0015 std::string test_function1() { return "test_function()"; }
0016 
0017 std::string test_function2(MyEnum k) { return "test_function(enum=" + std::to_string(k) + ")"; }
0018 
0019 std::string test_function3(int i) { return "test_function(" + std::to_string(i) + ")"; }
0020 
0021 py::str test_function4() { return "test_function()"; }
0022 py::str test_function4(char *) { return "test_function(char *)"; }
0023 py::str test_function4(int, float) { return "test_function(int, float)"; }
0024 py::str test_function4(float, int) { return "test_function(float, int)"; }
0025 
0026 py::bytes return_bytes() {
0027     const char *data = "\x01\x00\x02\x00";
0028     return std::string(data, 4);
0029 }
0030 
0031 std::string print_bytes(const py::bytes &bytes) {
0032     std::string ret = "bytes[";
0033     const auto value = static_cast<std::string>(bytes);
0034     for (char c : value) {
0035         ret += std::to_string(static_cast<int>(c)) + ' ';
0036     }
0037     ret.back() = ']';
0038     return ret;
0039 }
0040 
0041 // Test that we properly handle C++17 exception specifiers (which are part of the function
0042 // signature in C++17).  These should all still work before C++17, but don't affect the function
0043 // signature.
0044 namespace test_exc_sp {
0045 // [workaround(intel)] Unable to use noexcept instead of noexcept(true)
0046 // Make the f1 test basically the same as the f2 test in C++17 mode for the Intel compiler as
0047 // it fails to compile with a plain noexcept (tested with icc (ICC) 2021.1 Beta 20200827).
0048 #if defined(__INTEL_COMPILER) && defined(PYBIND11_CPP17)
0049 int f1(int x) noexcept(true) { return x + 1; }
0050 #else
0051 int f1(int x) noexcept { return x + 1; }
0052 #endif
0053 int f2(int x) noexcept(true) { return x + 2; }
0054 int f3(int x) noexcept(false) { return x + 3; }
0055 PYBIND11_WARNING_PUSH
0056 PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated")
0057 PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated")
0058 // NOLINTNEXTLINE(modernize-use-noexcept)
0059 int f4(int x) throw() { return x + 4; } // Deprecated equivalent to noexcept(true)
0060 PYBIND11_WARNING_POP
0061 struct C {
0062     int m1(int x) noexcept { return x - 1; }
0063     int m2(int x) const noexcept { return x - 2; }
0064     int m3(int x) noexcept(true) { return x - 3; }
0065     int m4(int x) const noexcept(true) { return x - 4; }
0066     int m5(int x) noexcept(false) { return x - 5; }
0067     int m6(int x) const noexcept(false) { return x - 6; }
0068     PYBIND11_WARNING_PUSH
0069     PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated")
0070     PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated")
0071     // NOLINTNEXTLINE(modernize-use-noexcept)
0072     int m7(int x) throw() { return x - 7; }
0073     // NOLINTNEXTLINE(modernize-use-noexcept)
0074     int m8(int x) const throw() { return x - 8; }
0075     PYBIND11_WARNING_POP
0076 };
0077 } // namespace test_exc_sp
0078 
0079 TEST_SUBMODULE(constants_and_functions, m) {
0080     // test_constants
0081     m.attr("some_constant") = py::int_(14);
0082 
0083     // test_function_overloading
0084     m.def("test_function", &test_function1);
0085     m.def("test_function", &test_function2);
0086     m.def("test_function", &test_function3);
0087 
0088 #if defined(PYBIND11_OVERLOAD_CAST)
0089     m.def("test_function", py::overload_cast<>(&test_function4));
0090     m.def("test_function", py::overload_cast<char *>(&test_function4));
0091     m.def("test_function", py::overload_cast<int, float>(&test_function4));
0092     m.def("test_function", py::overload_cast<float, int>(&test_function4));
0093 #else
0094     m.def("test_function", static_cast<py::str (*)()>(&test_function4));
0095     m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
0096     m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
0097     m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
0098 #endif
0099 
0100     py::enum_<MyEnum>(m, "MyEnum")
0101         .value("EFirstEntry", EFirstEntry)
0102         .value("ESecondEntry", ESecondEntry)
0103         .export_values();
0104 
0105     // test_bytes
0106     m.def("return_bytes", &return_bytes);
0107     m.def("print_bytes", &print_bytes);
0108 
0109     // test_exception_specifiers
0110     using namespace test_exc_sp;
0111     py::class_<C>(m, "C")
0112         .def(py::init<>())
0113         .def("m1", &C::m1)
0114         .def("m2", &C::m2)
0115         .def("m3", &C::m3)
0116         .def("m4", &C::m4)
0117         .def("m5", &C::m5)
0118         .def("m6", &C::m6)
0119         .def("m7", &C::m7)
0120         .def("m8", &C::m8);
0121     m.def("f1", f1);
0122     m.def("f2", f2);
0123 
0124     PYBIND11_WARNING_PUSH
0125     PYBIND11_WARNING_DISABLE_INTEL(878) // incompatible exception specifications
0126     m.def("f3", f3);
0127     PYBIND11_WARNING_POP
0128 
0129     m.def("f4", f4);
0130 
0131     // test_function_record_leaks
0132     m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
0133         // This should always be enough to trigger the alternative branch
0134         // where `sizeof(capture) > sizeof(rec->data)`
0135         uint64_t capture[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
0136 #if defined(__GNUC__) && __GNUC__ == 4 // CentOS7
0137         py::detail::silence_unused_warnings(capture);
0138 #endif
0139         m.def(
0140             "should_raise", [capture](int) { return capture[9] + 33; }, py::kw_only(), py::arg());
0141     });
0142     m.def("register_with_raising_repr", [](py::module_ m, const py::object &default_value) {
0143         m.def(
0144             "should_raise",
0145             [](int, int, const py::object &) { return 42; },
0146             "some docstring",
0147             py::arg_v("x", 42),
0148             py::arg_v("y", 42, "<the answer>"),
0149             py::arg_v("z", default_value));
0150     });
0151 }