Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 lsst.utils.tests 

26import _inheritance 

27 

28 

29class PySharedPtrTestSuite(lsst.utils.tests.TestCase): 

30 """Test the ability of PySharedPtr to safely pass hybrid objects 

31 between C++ and Python.""" 

32 

33 class PyDerived(_inheritance.CppBase): 

34 def __init__(self): 

35 super().__init__() 

36 

37 # Don't override overridable() 

38 

39 def abstract(self): 

40 return "py-abstract" 

41 

42 class PyCppDerived(_inheritance.CppDerived): 

43 def __init__(self): 

44 super().__init__() 

45 

46 def nonOverridable(self): 

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

48 

49 def overridable(self): 

50 return "py-override" 

51 

52 def abstract(self): 

53 return "py-abstract" 

54 

55 def checkGarbageCollection(self, concreteClass, returns): 

56 """Generic test for whether a C++/Python class survives garbage 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 

86class TestMemory(lsst.utils.tests.MemoryTestCase): 

87 pass 

88 

89 

90def setup_module(module): 

91 lsst.utils.tests.init() 

92 

93 

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

95 lsst.utils.tests.init() 

96 unittest.main()