2016-09-03 18:54:22 +00:00
|
|
|
#pragma once
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/pybind11.h>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1910
|
|
|
|
// We get some really long type names here which causes MSVC 2015 to emit warnings
|
|
|
|
# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
|
|
|
|
#endif
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
namespace py = pybind11;
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
using namespace pybind11::literals;
|
2016-09-03 18:54:22 +00:00
|
|
|
|
|
|
|
class test_initializer {
|
2017-06-08 22:44:49 +00:00
|
|
|
using Initializer = void (*)(py::module &);
|
|
|
|
|
|
|
|
public:
|
|
|
|
test_initializer(Initializer init);
|
|
|
|
test_initializer(const char *submodule_name, Initializer init);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TEST_SUBMODULE(name, variable) \
|
|
|
|
void test_submodule_##name(py::module &); \
|
|
|
|
test_initializer name(#name, test_submodule_##name); \
|
|
|
|
void test_submodule_##name(py::module &variable)
|
|
|
|
|
|
|
|
|
|
|
|
/// 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; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int i = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
|
|
|
|
class IncType : public UserType {
|
2016-09-03 18:54:22 +00:00
|
|
|
public:
|
2017-06-08 22:44:49 +00:00
|
|
|
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;
|
2016-09-03 18:54:22 +00:00
|
|
|
};
|
2017-07-03 23:12:09 +00:00
|
|
|
|
|
|
|
/// 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 {};
|
|
|
|
NAMESPACE_BEGIN(pybind11)
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
NAMESPACE_END(detail)
|
|
|
|
NAMESPACE_END(pybind11)
|