Coverage for tests / test_smart_holder.py: 51%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-26 08:43 +0000

1# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (https://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with this program. If not, see <https://www.gnu.org/licenses/>. 

20# 

21 

22import gc 

23import unittest 

24 

25import _inheritance 

26 

27 

28class SmartHolderTestSuite(unittest.TestCase): 

29 """Test the ability of pybind11 to safely pass hybrid objects 

30 between C++ and Python.""" 

31 

32 class PyDerived(_inheritance.CppBase): 

33 def __init__(self): 

34 super().__init__() 

35 

36 # Don't override overridable() 

37 

38 def abstract(self): 

39 return "py-abstract" 

40 

41 class PyCppDerived(_inheritance.CppDerived): 

42 def __init__(self): 

43 super().__init__() 

44 

45 def nonOverridable(self): 

46 return "error -- should never be called!" 

47 

48 def overridable(self): 

49 return "py-override" 

50 

51 def abstract(self): 

52 return "py-abstract" 

53 

54 def checkGarbageCollection(self, concreteClass, returns): 

55 """Generic test for whether a C++/Python class survives garbage 

56 collection. 

57 

58 Parameters 

59 ---------- 

60 concreteClass : `_inheritance.CppBase`-type 

61 The class to test. Must be default-constructible. 

62 returns : `tuple` 

63 A tuple of the return values from ``concreteClass``'s 

64 ``nonOverridable``, ``overridable``, and ``abstract`` methods, in 

65 that order. 

66 """ 

67 storage = _inheritance.CppStorage(concreteClass()) 

68 

69 gc.collect() 

70 

71 retrieved = _inheritance.getFromStorage(storage) 

72 self.assertIsInstance(retrieved, _inheritance.CppBase) 

73 self.assertIsInstance(retrieved, concreteClass) 

74 self.assertEqual(_inheritance.printFromCpp(retrieved), " ".join(returns)) 

75 

76 def testPyDerivedGarbageCollection(self): 

77 self.checkGarbageCollection(self.PyDerived, ("42", "", "py-abstract")) 

78 

79 def testCppDerivedGarbageCollection(self): 

80 self.checkGarbageCollection(_inheritance.CppDerived, ("42", "overridden", "implemented")) 

81 

82 def testPyCppDerivedGarbageCollection(self): 

83 self.checkGarbageCollection(self.PyCppDerived, ("42", "py-override", "py-abstract")) 

84 

85 

86if __name__ == "__main__": 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true

87 unittest.main()