pybind11_tests.h 2.85 KB
Newer Older
1
#pragma once
2
3
4

// This must be kept first for MSVC 2015.
// Do not remove the empty line between the #includes.
5
#include <pybind11/pybind11.h>
6

7
#include <pybind11/eval.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
8

9
10
#if defined(_MSC_VER) && _MSC_VER < 1910
// We get some really long type names here which causes MSVC 2015 to emit warnings
11
12
#    pragma warning(                                                                              \
        disable : 4503) // warning C4503: decorated name length exceeded, name was truncated
13
14
#endif

15
namespace py = pybind11;
16
using namespace pybind11::literals;
17
18

class test_initializer {
19
    using Initializer = void (*)(py::module_ &);
20
21
22
23
24
25
26

public:
    test_initializer(Initializer init);
    test_initializer(const char *submodule_name, Initializer init);
};

#define TEST_SUBMODULE(name, variable)                   \
27
    void test_submodule_##name(py::module_ &);            \
28
    test_initializer name(#name, test_submodule_##name); \
29
    void test_submodule_##name(py::module_ &variable)
30
31
32
33
34
35
36
37
38
39
40
41


/// Dummy type which is not exported anywhere -- something to trigger a conversion error
struct UnregisteredType { };

/// A user-defined type which is exported and can be used by any test
class UserType {
public:
    UserType() = default;
    UserType(int i) : i(i) { }

    int value() const { return i; }
42
    void set(int set) { i = set; }
43
44
45
46
47
48
49

private:
    int i = -1;
};

/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
class IncType : public UserType {
50
public:
51
52
53
54
55
56
    using UserType::UserType;
    IncType() = default;
    IncType(const IncType &other) : IncType(other.value() + 1) { }
    IncType(IncType &&) = delete;
    IncType &operator=(const IncType &) = delete;
    IncType &operator=(IncType &&) = delete;
57
};
58

59
60
61
62
63
64
/// A simple union for basic testing
union IntFloat {
    int i;
    float f;
};

65
66
67
/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
/// Used to test recursive casters (e.g. std::tuple, stl containers).
struct RValueCaster {};
68
69
PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)
70
71
72
73
74
75
template<> class type_caster<RValueCaster> {
public:
    PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
    static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
    static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
};
76
77
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(pybind11)
78
79
80
81
82
83
84
85
86
87
88
89

template <typename F>
void ignoreOldStyleInitWarnings(F &&body) {
    py::exec(R"(
    message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated"

    import warnings
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", message=message, category=FutureWarning)
        body()
    )", py::dict(py::arg("body") = py::cpp_function(body)));
}