def testObjSmartPointer(self): # Uncomment to see more debug info about creation of objects and ref counts. # Registry.getInstance().setShouldPrint(True) # Create Obj. o = Obj() self.assertEqual(objCount(), 1) # Create a shared pointer to an Obj together with an Obj. ptrToObj = o.giveSharedPtrToObj() self.assertEqual(objCount(), 2) # Delete the old Obj. o = None self.assertEqual(objCount(), 1) # Get a wrapper to the Obj inside of the shared pointer, object count should not change. obj = ptrToObj.data() self.assertEqual(objCount(), 1) obj.m_integer = 50 self.assertEqual(obj.m_integer, 50) # Set and get a member value via shared pointer (like operator->). ptrToObj.m_integer = 100 self.assertEqual(ptrToObj.m_integer, 100) # Get inner PyObject via shared pointer (like operator->) and set value in it. ptrToObj.m_internalInteger.m_int = 200 self.assertEqual(ptrToObj.m_internalInteger.m_int, 200) # Pass smart pointer as argument to a method, return value is the value of m_integer of # passed Obj inside the smart pointer. result = ptrToObj.takeSharedPtrToObj(ptrToObj) self.assertEqual(result, 100) # Pass an Integer as an argument that returns itself. result = ptrToObj.takeInteger(ptrToObj.m_internalInteger) self.assertEqual(integerCount(), 2) result = None if integerCount() > 1: gc.collect() print('Running garbage collector for reference test', file=sys.stderr) self.assertEqual(integerCount(), 1) # Make a copy of the shared pointer, object count should not change. ptrToObj2 = copy(ptrToObj) self.assertEqual(objCount(), 1) # Delete the first shared pointer, object count should not change because the second # one still has a reference. del ptrToObj self.assertEqual(objCount(), 1) # Delete the second smart pointer, object should be deleted. del ptrToObj2 self.assertEqual(objCount(), 0) self.assertEqual(integerCount(), 0)
def testInvalidParameter(self): # Create Obj. o = Obj() # Create a shared pointer to an Obj together with an Obj. ptrToObj = o.giveSharedPtrToObj() try: ptrToObj.typo self.assertFail() except AttributeError as error: self.assertEqual(error.args[0], "'smart.SharedPtr_Obj' object has no attribute 'typo'")