def testMonkeyPatchOnVirtualMethod(self): '''Injects new 'virtualMethod0' on a VirtualMethods instance and makes C++ call it.''' vm = VirtualMethods() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = vm.virtualMethod0(pt, val, cpx, b) result2 = vm.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) result1 = vm.callVirtualMethod0(pt, val, cpx, b) self.assertTrue(self.duck_method_called) result2 = vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual( result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b) * self.multiplier) # This is done to decrease the refcount of the vm object # allowing the object wrapper to be deleted before the # BindingManager. This is useful when compiling Shiboken # for debug, since the BindingManager destructor has an # assert that checks if the wrapper mapper is empty. vm.virtualMethod0 = None
def testMonkeyPatchOnVirtualMethodWithInheritance(self): '''Injects new 'virtualMethod0' on an object that inherits from VirtualMethods and makes C++ call it.''' duck = Duck() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = duck.virtualMethod0(pt, val, cpx, b) result2 = duck.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier duck.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, duck) result1 = duck.callVirtualMethod0(pt, val, cpx, b) self.assertTrue(self.duck_method_called) result2 = duck.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual( result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b) * self.multiplier) duck.virtualMethod0 = None
def testForInfiniteRecursion(self): def myVirtualMethod0(obj, pt, val, cpx, b): self.call_counter += 1 return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) vm = VirtualMethods() vm.virtualMethod0 = types.MethodType(myVirtualMethod0, vm, VirtualMethods) pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(self.call_counter, 1)
def testForInfiniteRecursion(self): def myVirtualMethod0(obj, pt, val, cpx, b): self.call_counter += 1 return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) vm = VirtualMethods() vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(self.call_counter, 1)
def testMonkeyPatchOnVirtualMethod(self): '''Injects new 'virtualMethod0' on a VirtualMethods instance and makes C++ call it.''' vm = VirtualMethods() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = vm.virtualMethod0(pt, val, cpx, b) result2 = vm.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier vm.virtualMethod0 = types.MethodType(myVirtualMethod0, vm, VirtualMethods) result1 = vm.callVirtualMethod0(pt, val, cpx, b) self.assert_(self.duck_method_called) result2 = vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b) * self.multiplier) # This is done to decrease the refcount of the vm object # allowing the object wrapper to be deleted before the # BindingManager. This is useful when compiling Shiboken # for debug, since the BindingManager destructor has an # assert that checks if the wrapper mapper is empty. vm.virtualMethod0 = None
def __init__(self): VirtualMethods.__init__(self) self.name_called = False self.sum0_called = False self.sum1_called = False self.sum2_called = False self.sum3_called = False self.sum4_called = False self.sumThree_called = False self.callMe_called = 0 self.multiplier = 12345
def testMonkeyPatchOnVirtualMethodWithInheritance(self): '''Injects new 'virtualMethod0' on an object that inherits from VirtualMethods and makes C++ call it.''' duck = Duck() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = duck.virtualMethod0(pt, val, cpx, b) result2 = duck.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier duck.virtualMethod0 = types.MethodType(myVirtualMethod0, duck, Duck) result1 = duck.callVirtualMethod0(pt, val, cpx, b) self.assert_(self.duck_method_called) result2 = duck.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b) * self.multiplier) duck.virtualMethod0 = None
def testCallNonReimplementedMethodWithString(self): """Calls createStr method from C++ with a Python string argument.""" obj = VirtualMethods() ok, string = obj.callCreateStr("foo") self.assertTrue(ok) self.assertEqual(string, Str("foo"))
def __init__(self): ObjectType.__init__(self) VirtualMethods.__init__(self)
def testCallNonReimplementedMethodWithNone(self): '''Calls createStr method from C++ with a None argument.''' obj = VirtualMethods() ok, string = obj.callCreateStr(None) self.assertFalse(ok) self.assertEqual(string, None)
def testSimpleCallWithNone(self): '''Simple call to createStr method with a None argument.''' obj = VirtualMethods() ok, string = obj.createStr(None) self.assertFalse(ok) self.assertEqual(string, None)
def createStr(self, text): ext_text = text if text is not None: ext_text = self.prefix + text print ext_text return VirtualMethods.createStr(self, ext_text)
def __init__(self): VirtualMethods.__init__(self)
def getMargins(self): return tuple([m*2 for m in VirtualMethods.getMargins(self)])
def sum1(self, a0, a1, a2): self.sum1_called = True return VirtualMethods.sum1(self, a0, a1, a2) * self.multiplier
def testSimpleCallWithString(self): '''Simple call to createStr method with a Python string argument.''' obj = VirtualMethods() ok, string = obj.createStr('foo') self.assert_(ok) self.assertEqual(string, Str('foo'))
def myVirtualMethod0(obj, pt, val, cpx, b): self.call_counter += 1 return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b)
def setUp(self): self.vm = VirtualMethods() self.evm = ExtendedVirtualMethods()
def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier
def testCallNonReimplementedMethodWithString(self): '''Calls createStr method from C++ with a Python string argument.''' obj = VirtualMethods() ok, string = obj.callCreateStr('foo') self.assert_(ok) self.assertEqual(string, Str('foo'))
def sum4(self, a0, a1): self.sum4_called = True return VirtualMethods.sum4(self, a0, a1) * self.multiplier
class VirtualMethodsTest(unittest.TestCase): '''Test case for modified virtual methods.''' def setUp(self): self.vm = VirtualMethods() self.evm = ExtendedVirtualMethods() def tearDown(self): del self.vm del self.evm def testModifiedVirtualMethod0(self): '''Renamed virtual method.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.callSum0(a0, a1, a2) result1 = self.vm.sumThree(a0, a1, a2) self.assertEqual(result0, a0 + a1 + a2) self.assertEqual(result0, result1) self.assertRaises(AttributeError, getattr, self.vm, 'sum0') def testReimplementedModifiedVirtualMethod0(self): '''Override of a renamed virtual method.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.callSum0(a0, a1, a2) result1 = self.vm.sumThree(a0, a1, a2) result2 = self.evm.callSum0(a0, a1, a2) self.assertEqual(result0, result1) self.assertEqual(result0 * self.evm.multiplier, result2) self.assert_(self.evm.sumThree_called) self.assertFalse(self.evm.sum0_called) def testModifiedVirtualMethod1(self): '''Virtual method with three arguments and the last one changed to have the default value set to 1000.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.sum1(a0, a1) self.assertEqual(result0, a0 + a1 + 1000) result1 = self.vm.sum1(a0, a1, a2) result2 = self.vm.callSum1(a0, a1, a2) self.assertEqual(result1, result2) def testReimplementedModifiedVirtualMethod1(self): '''Override of the virtual method with three arguments and the last one changed to have the default value set to 1000.''' a0, a1 = 2, 3 result0 = self.vm.sum1(a0, a1) result1 = self.evm.callSum1(a0, a1, 1000) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum1_called) def testModifiedVirtualMethod2(self): '''Virtual method originally with three arguments, the last one was removed and the default value set to 2000.''' a0, a1 = 1, 2 result0 = self.vm.sum2(a0, a1) self.assertEqual(result0, a0 + a1 + 2000) result1 = self.vm.sum2(a0, a1) result2 = self.vm.callSum2(a0, a1, 2000) self.assertEqual(result1, result2) self.assertRaises(TypeError, self.vm.sum2, 1, 2, 3) def testReimplementedModifiedVirtualMethod2(self): '''Override of the virtual method originally with three arguments, the last one was removed and the default value set to 2000.''' a0, a1 = 1, 2 ignored = 54321 result0 = self.vm.sum2(a0, a1) result1 = self.evm.callSum2(a0, a1, ignored) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum2_called) def testModifiedVirtualMethod3(self): '''Virtual method originally with three arguments have the second one removed and replaced by custom code that replaces it by the sum of the first and the last arguments.''' a0, a1 = 1, 2 result0 = self.vm.sum3(a0, a1) self.assertEqual(result0, a0 + (a0 + a1) + a1) result1 = self.vm.callSum3(a0, 10, a1) self.assertNotEqual(result0, result1) result2 = self.vm.callSum3(a0, a0 + a1, a1) self.assertEqual(result0, result2) self.assertRaises(TypeError, self.vm.sum3, 1, 2, 3) def testReimplementedModifiedVirtualMethod3(self): '''Override of the virtual method originally with three arguments have the second one removed and replaced by custom code that replaces it by the sum of the first and the last arguments.''' a0, a1 = 1, 2 ignored = 54321 result0 = self.vm.sum3(a0, a1) result1 = self.evm.callSum3(a0, ignored, a1) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum3_called) def testModifiedVirtualMethod4(self): '''Virtual method originally with three arguments, the last one was removed and the default value set to 3000.''' a0, a1 = 1, 2 default_value = 3000 result0 = self.vm.sum4(a0, a1) self.assertEqual(result0, a0 + default_value + a1) removed_arg_value = 100 result1 = self.vm.callSum4(a0, removed_arg_value, a1) self.assertEqual(result1, a0 + removed_arg_value + a1) self.assertRaises(TypeError, self.vm.sum4, 1, 2, 3) def testReimplementedModifiedVirtualMethod4(self): '''Override of the virtual method originally with three arguments, the last one was removed and the default value set to 3000. The method was modified with code injection on the binding override (the one that receives calls from C++ with the original signature and forwards it to Python overrides) that subtracts the value of the second argument (removed in Python) from the value of the first before sending them to Python.''' a0, a1 = 1, 2 removed_arg_value = 2011 default_value = 3000 result = self.evm.callSum4(a0, removed_arg_value, a1) self.assertEqual(result, (a0 - removed_arg_value + a1 + default_value) * self.evm.multiplier) self.assert_(self.evm.sum4_called) def testOverridenMethodResultModification(self): '''Injected code modifies the result of a call to a virtual method overridden in Python.''' orig_name = self.vm.callName() self.assertEqual(orig_name, 'VirtualMethods') name = self.evm.callName() self.assertEqual(name, 'PimpedExtendedVirtualMethods') self.assertEqual(name, Str('PimpedExtendedVirtualMethods')) self.assert_(self.evm.name_called) def testInjectCodeCallsPythonVirtualMethodOverride(self): '''When injected code calls the Python override by itself no code for the method call should be generated.''' self.evm.callCallMe() self.assertEqual(self.evm.callMe_called, 1) def testAllArgumentsRemoved(self): values = (10, 20, 30, 40) self.vm.setMargins(*values) self.assertEquals(self.vm.getMargins(), values) def testAllArgumentsRemovedCallVirtual(self): values = (10, 20, 30, 40) self.vm.setMargins(*values) self.assertEquals(self.vm.callGetMargins(), values) def testExtendedAllArgumentsRemoved(self): values = (10, 20, 30, 40) self.evm.setMargins(*values) double = tuple([m*2 for m in values]) self.assertEquals(self.evm.getMargins(), double) def testExtendedAllArgumentsRemovedCallVirtual(self): values = (10, 20, 30, 40) self.evm.setMargins(*values) double = tuple([m*2 for m in values]) self.assertEquals(self.evm.callGetMargins(), double)
def __init__(self): VirtualMethods.__init__(self) self.prefix = 'Ext'
def getMargins(self): return tuple([m * 2 for m in VirtualMethods.getMargins(self)])
class VirtualMethodsTest(unittest.TestCase): '''Test case for modified virtual methods.''' def setUp(self): self.vm = VirtualMethods() self.evm = ExtendedVirtualMethods() def tearDown(self): del self.vm del self.evm def testModifiedVirtualMethod0(self): '''Renamed virtual method.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.callSum0(a0, a1, a2) result1 = self.vm.sumThree(a0, a1, a2) self.assertEqual(result0, a0 + a1 + a2) self.assertEqual(result0, result1) self.assertRaises(AttributeError, getattr, self.vm, 'sum0') def testReimplementedModifiedVirtualMethod0(self): '''Override of a renamed virtual method.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.callSum0(a0, a1, a2) result1 = self.vm.sumThree(a0, a1, a2) result2 = self.evm.callSum0(a0, a1, a2) self.assertEqual(result0, result1) self.assertEqual(result0 * self.evm.multiplier, result2) self.assert_(self.evm.sumThree_called) self.assertFalse(self.evm.sum0_called) def testModifiedVirtualMethod1(self): '''Virtual method with three arguments and the last one changed to have the default value set to 1000.''' a0, a1, a2 = 2, 3, 5 result0 = self.vm.sum1(a0, a1) self.assertEqual(result0, a0 + a1 + 1000) result1 = self.vm.sum1(a0, a1, a2) result2 = self.vm.callSum1(a0, a1, a2) self.assertEqual(result1, result2) def testReimplementedModifiedVirtualMethod1(self): '''Override of the virtual method with three arguments and the last one changed to have the default value set to 1000.''' a0, a1 = 2, 3 result0 = self.vm.sum1(a0, a1) result1 = self.evm.callSum1(a0, a1, 1000) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum1_called) def testModifiedVirtualMethod2(self): '''Virtual method originally with three arguments, the last one was removed and the default value set to 2000.''' a0, a1 = 1, 2 result0 = self.vm.sum2(a0, a1) self.assertEqual(result0, a0 + a1 + 2000) result1 = self.vm.sum2(a0, a1) result2 = self.vm.callSum2(a0, a1, 2000) self.assertEqual(result1, result2) self.assertRaises(TypeError, self.vm.sum2, 1, 2, 3) def testReimplementedModifiedVirtualMethod2(self): '''Override of the virtual method originally with three arguments, the last one was removed and the default value set to 2000.''' a0, a1 = 1, 2 ignored = 54321 result0 = self.vm.sum2(a0, a1) result1 = self.evm.callSum2(a0, a1, ignored) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum2_called) def testModifiedVirtualMethod3(self): '''Virtual method originally with three arguments have the second one removed and replaced by custom code that replaces it by the sum of the first and the last arguments.''' a0, a1 = 1, 2 result0 = self.vm.sum3(a0, a1) self.assertEqual(result0, a0 + (a0 + a1) + a1) result1 = self.vm.callSum3(a0, 10, a1) self.assertNotEqual(result0, result1) result2 = self.vm.callSum3(a0, a0 + a1, a1) self.assertEqual(result0, result2) self.assertRaises(TypeError, self.vm.sum3, 1, 2, 3) def testReimplementedModifiedVirtualMethod3(self): '''Override of the virtual method originally with three arguments have the second one removed and replaced by custom code that replaces it by the sum of the first and the last arguments.''' a0, a1 = 1, 2 ignored = 54321 result0 = self.vm.sum3(a0, a1) result1 = self.evm.callSum3(a0, ignored, a1) self.assertEqual(result0 * self.evm.multiplier, result1) self.assert_(self.evm.sum3_called) def testModifiedVirtualMethod4(self): '''Virtual method originally with three arguments, the last one was removed and the default value set to 3000.''' a0, a1 = 1, 2 default_value = 3000 result0 = self.vm.sum4(a0, a1) self.assertEqual(result0, a0 + default_value + a1) removed_arg_value = 100 result1 = self.vm.callSum4(a0, removed_arg_value, a1) self.assertEqual(result1, a0 + removed_arg_value + a1) self.assertRaises(TypeError, self.vm.sum4, 1, 2, 3) def testReimplementedModifiedVirtualMethod4(self): '''Override of the virtual method originally with three arguments, the last one was removed and the default value set to 3000. The method was modified with code injection on the binding override (the one that receives calls from C++ with the original signature and forwards it to Python overrides) that subtracts the value of the second argument (removed in Python) from the value of the first before sending them to Python.''' a0, a1 = 1, 2 removed_arg_value = 2011 default_value = 3000 result = self.evm.callSum4(a0, removed_arg_value, a1) self.assertEqual(result, (a0 - removed_arg_value + a1 + default_value) * self.evm.multiplier) self.assert_(self.evm.sum4_called) def testOverridenMethodResultModification(self): '''Injected code modifies the result of a call to a virtual method overridden in Python.''' orig_name = self.vm.callName() self.assertEqual(orig_name, 'VirtualMethods') name = self.evm.callName() self.assertEqual(name, 'PimpedExtendedVirtualMethods') self.assertEqual(name, Str('PimpedExtendedVirtualMethods')) self.assert_(self.evm.name_called) def testInjectCodeCallsPythonVirtualMethodOverride(self): '''When injected code calls the Python override by itself no code for the method call should be generated.''' self.evm.callCallMe() self.assertEqual(self.evm.callMe_called, 1) def testAllArgumentsRemoved(self): values = (10, 20, 30, 40) self.vm.setMargins(*values) self.assertEquals(self.vm.getMargins(), values) def testAllArgumentsRemovedCallVirtual(self): values = (10, 20, 30, 40) self.vm.setMargins(*values) self.assertEquals(self.vm.callGetMargins(), values) def testExtendedAllArgumentsRemoved(self): values = (10, 20, 30, 40) self.evm.setMargins(*values) double = tuple([m * 2 for m in values]) self.assertEquals(self.evm.getMargins(), double) def testExtendedAllArgumentsRemovedCallVirtual(self): values = (10, 20, 30, 40) self.evm.setMargins(*values) double = tuple([m * 2 for m in values]) self.assertEquals(self.evm.callGetMargins(), double)
def createStr(self, text): ext_text = text if text is not None: ext_text = self.prefix + text return VirtualMethods.createStr(self, ext_text)
def testSimpleCallWithString(self): '''Simple call to createStr method with a Python string argument.''' obj = VirtualMethods() ok, string = obj.createStr('foo') self.assertTrue(ok) self.assertEqual(string, Str('foo'))
def testCallNonReimplementedMethodWithString(self): '''Calls createStr method from C++ with a Python string argument.''' obj = VirtualMethods() ok, string = obj.callCreateStr('foo') self.assertTrue(ok) self.assertEqual(string, Str('foo'))
def __init__(self): ObjectType.__init__(self) VirtualMethods.__init__(self) self.event_processed = False
def testSimpleCallWithString(self): """Simple call to createStr method with a Python string argument.""" obj = VirtualMethods() ok, string = obj.createStr("foo") self.assert_(ok) self.assertEqual(string, Str("foo"))