pybind11_tests.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <pybind11/pybind11.h>
  3. #include <pybind11/eval.h>
  4. #if defined(_MSC_VER) && _MSC_VER < 1910
  5. // We get some really long type names here which causes MSVC 2015 to emit warnings
  6. # pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
  7. #endif
  8. namespace py = pybind11;
  9. using namespace pybind11::literals;
  10. class test_initializer {
  11. using Initializer = void (*)(py::module_ &);
  12. public:
  13. test_initializer(Initializer init);
  14. test_initializer(const char *submodule_name, Initializer init);
  15. };
  16. #define TEST_SUBMODULE(name, variable) \
  17. void test_submodule_##name(py::module_ &); \
  18. test_initializer name(#name, test_submodule_##name); \
  19. void test_submodule_##name(py::module_ &variable)
  20. /// Dummy type which is not exported anywhere -- something to trigger a conversion error
  21. struct UnregisteredType { };
  22. /// A user-defined type which is exported and can be used by any test
  23. class UserType {
  24. public:
  25. UserType() = default;
  26. UserType(int i) : i(i) { }
  27. int value() const { return i; }
  28. void set(int set) { i = set; }
  29. private:
  30. int i = -1;
  31. };
  32. /// Like UserType, but increments `value` on copy for quick reference vs. copy tests
  33. class IncType : public UserType {
  34. public:
  35. using UserType::UserType;
  36. IncType() = default;
  37. IncType(const IncType &other) : IncType(other.value() + 1) { }
  38. IncType(IncType &&) = delete;
  39. IncType &operator=(const IncType &) = delete;
  40. IncType &operator=(IncType &&) = delete;
  41. };
  42. /// A simple union for basic testing
  43. union IntFloat {
  44. int i;
  45. float f;
  46. };
  47. /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
  48. /// Used to test recursive casters (e.g. std::tuple, stl containers).
  49. struct RValueCaster {};
  50. PYBIND11_NAMESPACE_BEGIN(pybind11)
  51. PYBIND11_NAMESPACE_BEGIN(detail)
  52. template<> class type_caster<RValueCaster> {
  53. public:
  54. PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
  55. static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
  56. static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
  57. };
  58. PYBIND11_NAMESPACE_END(detail)
  59. PYBIND11_NAMESPACE_END(pybind11)
  60. template <typename F>
  61. void ignoreOldStyleInitWarnings(F &&body) {
  62. py::exec(R"(
  63. message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"
  64. import warnings
  65. with warnings.catch_warnings():
  66. warnings.filterwarnings("ignore", message=message, category=FutureWarning)
  67. body()
  68. )", py::dict(py::arg("body") = py::cpp_function(body)));
  69. }