mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 22:52:01 +00:00
4b159230d9
Attempting to mix py::module_local and non-module_local classes results in some unexpected/undesirable behaviour: - if a class is registered non-local by some other module, a later attempt to register it locally fails. It doesn't need to: it is perfectly acceptable for the local registration to simply override the external global registration. - going the other way (i.e. module `A` registers a type `T` locally, then `B` registers the same type `T` globally) causes a more serious issue: `A.T`'s constructors no longer work because the `self` argument gets converted to a `B.T`, which then fails to resolve. Changing the cast precedence to prefer local over global fixes this and makes it work more consistently, regardless of module load order.
31 lines
1013 B
C++
31 lines
1013 B
C++
#pragma once
|
|
#include "pybind11_tests.h"
|
|
|
|
/// Simple class used to test py::local:
|
|
template <int> class LocalBase {
|
|
public:
|
|
LocalBase(int i) : i(i) { }
|
|
int i = -1;
|
|
};
|
|
|
|
/// Registered with py::local in both main and secondary modules:
|
|
using LocalType = LocalBase<0>;
|
|
/// Registered without py::local in both modules:
|
|
using NonLocalType = LocalBase<1>;
|
|
/// A second non-local type (for stl_bind tests):
|
|
using NonLocal2 = LocalBase<2>;
|
|
/// Tests within-module, different-compilation-unit local definition conflict:
|
|
using LocalExternal = LocalBase<3>;
|
|
/// Mixed: registered local first, then global
|
|
using MixedLocalGlobal = LocalBase<4>;
|
|
/// Mixed: global first, then local (which fails)
|
|
using MixedGlobalLocal = LocalBase<5>;
|
|
|
|
// Simple bindings (used with the above):
|
|
template <typename T, int Adjust, typename... Args>
|
|
py::class_<T> bind_local(Args && ...args) {
|
|
return py::class_<T>(std::forward<Args>(args)...)
|
|
.def(py::init<int>())
|
|
.def("get", [](T &i) { return i.i + Adjust; });
|
|
};
|