You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.8 KiB

8 years ago
8 years ago
8 years ago
  1. import pytest
  2. def test_multiple_inheritance_cpp():
  3. from pybind11_tests import MIType
  4. mt = MIType(3, 4)
  5. assert mt.foo() == 3
  6. assert mt.bar() == 4
  7. def test_multiple_inheritance_mix1():
  8. from pybind11_tests import Base2
  9. class Base1:
  10. def __init__(self, i):
  11. self.i = i
  12. def foo(self):
  13. return self.i
  14. class MITypePy(Base1, Base2):
  15. def __init__(self, i, j):
  16. Base1.__init__(self, i)
  17. Base2.__init__(self, j)
  18. mt = MITypePy(3, 4)
  19. assert mt.foo() == 3
  20. assert mt.bar() == 4
  21. def test_multiple_inheritance_mix2():
  22. from pybind11_tests import Base1
  23. class Base2:
  24. def __init__(self, i):
  25. self.i = i
  26. def bar(self):
  27. return self.i
  28. class MITypePy(Base1, Base2):
  29. def __init__(self, i, j):
  30. Base1.__init__(self, i)
  31. Base2.__init__(self, j)
  32. mt = MITypePy(3, 4)
  33. assert mt.foo() == 3
  34. assert mt.bar() == 4
  35. def test_multiple_inheritance_error():
  36. """Inheriting from multiple C++ bases in Python is not supported"""
  37. from pybind11_tests import Base1, Base2
  38. with pytest.raises(TypeError) as excinfo:
  39. # noinspection PyUnusedLocal
  40. class MI(Base1, Base2):
  41. pass
  42. assert "Can't inherit from multiple C++ classes in Python" in str(excinfo.value)
  43. def test_multiple_inheritance_virtbase():
  44. from pybind11_tests import Base12a, bar_base2a, bar_base2a_sharedptr
  45. class MITypePy(Base12a):
  46. def __init__(self, i, j):
  47. Base12a.__init__(self, i, j)
  48. mt = MITypePy(3, 4)
  49. assert mt.bar() == 4
  50. assert bar_base2a(mt) == 4
  51. assert bar_base2a_sharedptr(mt) == 4
  52. def test_mi_static_properties():
  53. """Mixing bases with and without static properties should be possible
  54. and the result should be independent of base definition order"""
  55. from pybind11_tests import mi
  56. for d in (mi.VanillaStaticMix1(), mi.VanillaStaticMix2()):
  57. assert d.vanilla() == "Vanilla"
  58. assert d.static_func1() == "WithStatic1"
  59. assert d.static_func2() == "WithStatic2"
  60. assert d.static_func() == d.__class__.__name__
  61. mi.WithStatic1.static_value1 = 1
  62. mi.WithStatic2.static_value2 = 2
  63. assert d.static_value1 == 1
  64. assert d.static_value2 == 2
  65. assert d.static_value == 12
  66. d.static_value1 = 0
  67. assert d.static_value1 == 0
  68. d.static_value2 = 0
  69. assert d.static_value2 == 0
  70. d.static_value = 0
  71. assert d.static_value == 0
  72. @pytest.unsupported_on_pypy
  73. def test_mi_dynamic_attributes():
  74. """Mixing bases with and without dynamic attribute support"""
  75. from pybind11_tests import mi
  76. for d in (mi.VanillaDictMix1(), mi.VanillaDictMix2()):
  77. d.dynamic = 1
  78. assert d.dynamic == 1