Made module_local types take precedence over global types
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.
diff --git a/tests/test_local_bindings.py b/tests/test_local_bindings.py
index 4c5a874..4e35665 100644
--- a/tests/test_local_bindings.py
+++ b/tests/test_local_bindings.py
@@ -103,6 +103,37 @@
assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!'
+def test_mixed_local_global():
+ """Local types take precedence over globally registered types: a module with a `module_local`
+ type can be registered even if the type is already registered globally. With the module,
+ casting will go to the local type; outside the module casting goes to the global type."""
+ import pybind11_cross_module_tests as cm
+ m.register_mixed_global()
+ m.register_mixed_local()
+
+ a = []
+ a.append(m.MixedGlobalLocal(1))
+ a.append(m.MixedLocalGlobal(2))
+ a.append(m.get_mixed_gl(3))
+ a.append(m.get_mixed_lg(4))
+
+ assert [x.get() for x in a] == [101, 1002, 103, 1004]
+
+ cm.register_mixed_global_local()
+ cm.register_mixed_local_global()
+ a.append(m.MixedGlobalLocal(5))
+ a.append(m.MixedLocalGlobal(6))
+ a.append(cm.MixedGlobalLocal(7))
+ a.append(cm.MixedLocalGlobal(8))
+ a.append(m.get_mixed_gl(9))
+ a.append(m.get_mixed_lg(10))
+ a.append(cm.get_mixed_gl(11))
+ a.append(cm.get_mixed_lg(12))
+
+ assert [x.get() for x in a] == \
+ [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
+
+
def test_internal_locals_differ():
"""Makes sure the internal local type map differs across the two modules"""
import pybind11_cross_module_tests as cm