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.

55 lines
1.5 KiB

  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::bytes return_bytes() {
  19. const char *data = "\x01\x00\x02\x00";
  20. return std::string(data, 4);
  21. }
  22. std::string print_bytes(py::bytes bytes) {
  23. std::string ret = "bytes[";
  24. const auto value = static_cast<std::string>(bytes);
  25. for (size_t i = 0; i < value.length(); ++i) {
  26. ret += std::to_string(static_cast<int>(value[i])) + " ";
  27. }
  28. ret.back() = ']';
  29. return ret;
  30. }
  31. test_initializer constants_and_functions([](py::module &m) {
  32. m.attr("some_constant") = py::int_(14);
  33. m.def("test_function", &test_function1);
  34. m.def("test_function", &test_function2);
  35. m.def("test_function", &test_function3);
  36. py::enum_<MyEnum>(m, "MyEnum")
  37. .value("EFirstEntry", EFirstEntry)
  38. .value("ESecondEntry", ESecondEntry)
  39. .export_values();
  40. m.def("return_bytes", &return_bytes);
  41. m.def("print_bytes", &print_bytes);
  42. });