You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. enum MyEnum { EFirstEntry = 1, ESecondEntry };
  9. std::string test_function1() {
  10. return "test_function()";
  11. }
  12. std::string test_function2(MyEnum k) {
  13. return "test_function(enum=" + std::to_string(k) + ")";
  14. }
  15. std::string test_function3(int i) {
  16. return "test_function(" + std::to_string(i) + ")";
  17. }
  18. py::str test_function4(int, float) { return "test_function(int, float)"; }
  19. py::str test_function4(float, int) { return "test_function(float, int)"; }
  20. py::bytes return_bytes() {
  21. const char *data = "\x01\x00\x02\x00";
  22. return std::string(data, 4);
  23. }
  24. std::string print_bytes(py::bytes bytes) {
  25. std::string ret = "bytes[";
  26. const auto value = static_cast<std::string>(bytes);
  27. for (size_t i = 0; i < value.length(); ++i) {
  28. ret += std::to_string(static_cast<int>(value[i])) + " ";
  29. }
  30. ret.back() = ']';
  31. return ret;
  32. }
  33. // Test that we properly handle C++17 exception specifiers (which are part of the function signature
  34. // in C++17). These should all still work before C++17, but don't affect the function signature.
  35. namespace test_exc_sp {
  36. int f1(int x) noexcept { return x+1; }
  37. int f2(int x) noexcept(true) { return x+2; }
  38. int f3(int x) noexcept(false) { return x+3; }
  39. int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true)
  40. struct C {
  41. int m1(int x) noexcept { return x-1; }
  42. int m2(int x) const noexcept { return x-2; }
  43. int m3(int x) noexcept(true) { return x-3; }
  44. int m4(int x) const noexcept(true) { return x-4; }
  45. int m5(int x) noexcept(false) { return x-5; }
  46. int m6(int x) const noexcept(false) { return x-6; }
  47. int m7(int x) throw() { return x-7; }
  48. int m8(int x) const throw() { return x-8; }
  49. };
  50. }
  51. test_initializer constants_and_functions([](py::module &m) {
  52. m.attr("some_constant") = py::int_(14);
  53. m.def("test_function", &test_function1);
  54. m.def("test_function", &test_function2);
  55. m.def("test_function", &test_function3);
  56. #if defined(PYBIND11_OVERLOAD_CAST)
  57. m.def("test_function", py::overload_cast<int, float>(&test_function4));
  58. m.def("test_function", py::overload_cast<float, int>(&test_function4));
  59. #else
  60. m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
  61. m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
  62. #endif
  63. py::enum_<MyEnum>(m, "MyEnum")
  64. .value("EFirstEntry", EFirstEntry)
  65. .value("ESecondEntry", ESecondEntry)
  66. .export_values();
  67. m.def("return_bytes", &return_bytes);
  68. m.def("print_bytes", &print_bytes);
  69. using namespace test_exc_sp;
  70. py::module m2 = m.def_submodule("exc_sp");
  71. py::class_<C>(m2, "C")
  72. .def(py::init<>())
  73. .def("m1", &C::m1)
  74. .def("m2", &C::m2)
  75. .def("m3", &C::m3)
  76. .def("m4", &C::m4)
  77. .def("m5", &C::m5)
  78. .def("m6", &C::m6)
  79. .def("m7", &C::m7)
  80. .def("m8", &C::m8)
  81. ;
  82. m2.def("f1", f1);
  83. m2.def("f2", f2);
  84. m2.def("f3", f3);
  85. m2.def("f4", f4);
  86. });