def TestGenerated(): # Create an instance of the server. from win32com.client.gencache import EnsureDispatch o = EnsureDispatch("PyCOMTest.PyCOMTest") counter = o.GetSimpleCounter() TestCounter(counter, 1) counter = EnsureDispatch("PyCOMTest.SimpleCounter") TestCounter(counter, 1) i1, i2 = o.GetMultipleInterfaces() if not isinstance(i1, DispatchBaseClass) or not isinstance(i2, DispatchBaseClass): # Yay - is now an instance returned! raise error("GetMultipleInterfaces did not return instances - got '%s', '%s'" % (i1, i2)) del i1 del i2 progress("Checking default args") rc = o.TestOptionals() if rc[:-1] != ("def", 0, 1) or abs(rc[-1]-3.14)>.01: print rc raise error("Did not get the optional values correctly") rc = o.TestOptionals("Hi", 2, 3, 1.1) if rc[:-1] != ("Hi", 2, 3) or abs(rc[-1]-1.1)>.01: print rc raise error("Did not get the specified optional values correctly") rc = o.TestOptionals2(0) if rc != (0, "", 1): print rc raise error("Did not get the optional2 values correctly") rc = o.TestOptionals2(1.1, "Hi", 2) if rc[1:] != ("Hi", 2) or abs(rc[0]-1.1)>.01: print rc raise error("Did not get the specified optional2 values correctly") progress("Checking var args") o.SetVarArgs("Hi", "There", "From", "Python", 1) if o.GetLastVarArgs() != ("Hi", "There", "From", "Python", 1): raise error("VarArgs failed -" + str(o.GetLastVarArgs())) progress("Checking getting/passing IUnknown") if o.GetSetUnknown(o) != o: raise error("GetSetUnknown failed") progress("Checking getting/passing IDispatch") if not isinstance(o.GetSetDispatch(o), DispatchBaseClass): raise error("GetSetDispatch failed") progress("Checking getting/passing IDispatch of known type") if o.GetSetInterface(o).__class__ != o.__class__: raise error("GetSetDispatch failed") if o.GetSetVariant(4) != 4: raise error("GetSetVariant (int) failed") if o.GetSetVariant("foo") != "foo": raise error("GetSetVariant (str) failed") if o.GetSetVariant(o) != o: raise error("GetSetVariant (dispatch) failed") # We want to explicitly test > 32 bits. py3k has no 'maxint' and # 'maxsize+1' is no good on 64bit platforms as its 65 bits! big = 2147483647 # sys.maxint on py2k for l in big, big+1, 1 << 65: if o.GetSetVariant(l) != l: raise error("GetSetVariant (long) failed") if o.TestByRefVariant(2) != 4: raise error("TestByRefVariant failed") if o.TestByRefString("Foo") != "FooFoo": raise error("TestByRefString failed") # Pass some non-sequence objects to our array decoder, and watch it fail. try: o.SetVariantSafeArray("foo") raise error("Expected a type error") except TypeError: pass try: o.SetVariantSafeArray(666) raise error("Expected a type error") except TypeError: pass o.GetSimpleSafeArray(None) TestApplyResult(o.GetSimpleSafeArray, (None,), tuple(range(10))) resultCheck = tuple(range(5)), tuple(range(10)), tuple(range(20)) TestApplyResult(o.GetSafeArrays, (None, None, None), resultCheck) l=[1,2,3,4] TestApplyResult(o.SetVariantSafeArray, (l,), len(l)) TestApplyResult(o.SetIntSafeArray, (l,), len(l)) ll=[1,2,3,0x100000000] TestApplyResult(o.SetLongLongSafeArray, (ll,), len(ll)) TestApplyResult(o.SetULongLongSafeArray, (ll,), len(ll)) # check we can pass ints as a VT_UI1 TestApplyResult(o.SetBinSafeArray, (l,), len(l)) # and binary TestApplyResult(o.SetBinSafeArray, (str2memory('foo\0bar'),), 7) l=[] TestApplyResult(o.SetVariantSafeArray, (l,), len(l)) TestApplyResult(o.SetIntSafeArray, (l,), len(l)) # Tell the server to do what it does! TestApplyResult(o.Test, ("Unused", 99), 1) # A bool function TestApplyResult(o.Test, ("Unused", -1), 1) # A bool function TestApplyResult(o.Test, ("Unused", 1==1), 1) # A bool function TestApplyResult(o.Test, ("Unused", 0), 0) TestApplyResult(o.Test, ("Unused", 1==0), 0) TestApplyResult(o.Test2, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test3, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test4, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test5, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test6, (constants.WideAttr1,), constants.WideAttr1) TestApplyResult(o.Test6, (constants.WideAttr2,), constants.WideAttr2) TestApplyResult(o.Test6, (constants.WideAttr3,), constants.WideAttr3) TestApplyResult(o.Test6, (constants.WideAttr4,), constants.WideAttr4) TestApplyResult(o.Test6, (constants.WideAttr5,), constants.WideAttr5) TestConstant("ULongTest1", ensure_long(0xFFFFFFFF)) TestConstant("ULongTest2", ensure_long(0x7FFFFFFF)) TestConstant("LongTest1", ensure_long(-0x7FFFFFFF)) TestConstant("LongTest2", ensure_long(0x7FFFFFFF)) TestConstant("UCharTest", 255) TestConstant("CharTest", -1) # 'Hello Loraine', but the 'r' is the "Registered" sign (\xae) TestConstant("StringTest", u"Hello Lo\xaeaine") if issubclass(pywintypes.TimeType, datetime.datetime): # For now *all* times passed must be tz-aware. now = win32timezone.now() # but conversion to and from a VARIANT loses sub-second... now = now.replace(microsecond=0) later = now + datetime.timedelta(seconds=1) TestApplyResult(o.EarliestDate, (now, later), now) else: # old PyTime object now = pythoncom.MakeTime(time.gmtime(time.time())) later = pythoncom.MakeTime(time.gmtime(time.time()+1)) TestApplyResult(o.EarliestDate, (now, later), now) # But it can still *accept* tz-naive datetime objects... now = datetime.datetime.now() expect = pythoncom.MakeTime(now) TestApplyResult(o.EarliestDate, (now, now), expect) assert o.DoubleString("foo") == "foofoo" assert o.DoubleInOutString("foo") == "foofoo" o.LongProp = 3 if o.LongProp != 3 or o.IntProp != 3: raise error("Property value wrong - got %d/%d" % (o.LongProp,o.IntProp)) o.LongProp = o.IntProp = -3 if o.LongProp != -3 or o.IntProp != -3: raise error("Property value wrong - got %d/%d" % (o.LongProp,o.IntProp)) check = 3 *10 **9 o.ULongProp = check if o.ULongProp != check: raise error("Property value wrong - got %d (expected %d)" % (o.ULongProp, check)) # currency. pythoncom.__future_currency__ = 1 if o.CurrencyProp != 0: raise error("Expecting 0, got %r" % (o.CurrencyProp,)) try: import decimal except ImportError: import win32com.decimal_23 as decimal for val in ("1234.5678", "1234.56", "1234"): o.CurrencyProp = decimal.Decimal(val) if o.CurrencyProp != decimal.Decimal(val): raise error("%s got %r" % (val, o.CurrencyProp)) v1 = decimal.Decimal("1234.5678") TestApplyResult(o.DoubleCurrency, (v1,), v1*2) TestApplyResult(o.DoubleCurrencyByVal, (v1,), v1*2) v2 = decimal.Decimal("9012.3456") TestApplyResult(o.AddCurrencies, (v1, v2), v1+v2) o.SetParamProp(0, 1) if o.ParamProp(0) != 1: raise RuntimeError(o.paramProp(0)) # Do the connection point thing... # Create a connection object. progress("Testing connection points") sessions = [] o = win32com.client.DispatchWithEvents( o, RandomEventHandler) o._Init() try: for i in range(3): session = o.Start() sessions.append(session) time.sleep(.5) finally: # Stop the servers for session in sessions: o.Stop(session) o._DumpFireds() progress("Finished generated .py test.")
def TestGenerated(): # Create an instance of the server. from win32com.client.gencache import EnsureDispatch o = EnsureDispatch("PyCOMTest.PyCOMTest") TestCommon(o, True) counter = EnsureDispatch("PyCOMTest.SimpleCounter") TestCounter(counter, True) # This dance lets us get a CoClass even though it's not explicitly registered. # This is `CoPyComTest` from win32com.client.CLSIDToClass import GetClass coclass_o = GetClass("{8EE0C520-5605-11D0-AE5F-CADD4C000000}")() TestCommon(coclass_o, True) # Test the regression reported in #1753 assert bool(coclass_o) # This is `CoSimpleCounter` and the counter tests should work. coclass = GetClass("{B88DD310-BAE8-11D0-AE86-76F2C1000000}")() TestCounter(coclass, True) # XXX - this is failing in dynamic tests, but should work fine. i1, i2 = o.GetMultipleInterfaces() if not isinstance(i1, DispatchBaseClass) or not isinstance(i2, DispatchBaseClass): # Yay - is now an instance returned! raise error( "GetMultipleInterfaces did not return instances - got '%s', '%s'" % (i1, i2) ) del i1 del i2 # Generated knows to only pass a 32bit int, so should fail. check_get_set_raises(OverflowError, o.GetSetInt, 0x80000000) check_get_set_raises(OverflowError, o.GetSetLong, 0x80000000) # Generated knows this should be an int, so raises ValueError check_get_set_raises(ValueError, o.GetSetInt, "foo") check_get_set_raises(ValueError, o.GetSetLong, "foo") # Pass some non-sequence objects to our array decoder, and watch it fail. try: o.SetVariantSafeArray("foo") raise error("Expected a type error") except TypeError: pass try: o.SetVariantSafeArray(666) raise error("Expected a type error") except TypeError: pass o.GetSimpleSafeArray(None) TestApplyResult(o.GetSimpleSafeArray, (None,), tuple(range(10))) resultCheck = tuple(range(5)), tuple(range(10)), tuple(range(20)) TestApplyResult(o.GetSafeArrays, (None, None, None), resultCheck) l = [] TestApplyResult(o.SetIntSafeArray, (l,), len(l)) l = [1, 2, 3, 4] TestApplyResult(o.SetIntSafeArray, (l,), len(l)) ll = [1, 2, 3, 0x100000000] TestApplyResult(o.SetLongLongSafeArray, (ll,), len(ll)) TestApplyResult(o.SetULongLongSafeArray, (ll,), len(ll)) # Tell the server to do what it does! TestApplyResult(o.Test2, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test3, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test4, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test5, (constants.Attr2,), constants.Attr2) TestApplyResult(o.Test6, (constants.WideAttr1,), constants.WideAttr1) TestApplyResult(o.Test6, (constants.WideAttr2,), constants.WideAttr2) TestApplyResult(o.Test6, (constants.WideAttr3,), constants.WideAttr3) TestApplyResult(o.Test6, (constants.WideAttr4,), constants.WideAttr4) TestApplyResult(o.Test6, (constants.WideAttr5,), constants.WideAttr5) TestApplyResult(o.TestInOut, (2.0, True, 4), (4.0, False, 8)) o.SetParamProp(0, 1) if o.ParamProp(0) != 1: raise RuntimeError(o.paramProp(0)) # Make sure CastTo works - even though it is only casting it to itself! o2 = CastTo(o, "IPyCOMTest") if o != o2: raise error("CastTo should have returned the same object") # Do the connection point thing... # Create a connection object. progress("Testing connection points") o2 = win32com.client.DispatchWithEvents(o, RandomEventHandler) TestEvents(o2, o2) o2 = win32com.client.DispatchWithEvents(o, NewStyleRandomEventHandler) TestEvents(o2, o2) # and a plain "WithEvents". handler = win32com.client.WithEvents(o, RandomEventHandler) TestEvents(o, handler) handler = win32com.client.WithEvents(o, NewStyleRandomEventHandler) TestEvents(o, handler) progress("Finished generated .py test.")
def TestGenerated(): # Create an instance of the server. from win32com.client.gencache import EnsureDispatch o = EnsureDispatch("PyCOMTest.PyCOMTest") counter = o.GetSimpleCounter() TestCounter(counter, 1) counter = EnsureDispatch("PyCOMTest.SimpleCounter") TestCounter(counter, 1) i1, i2 = o.GetMultipleInterfaces() if type(i1) != types.InstanceType or type(i2) != types.InstanceType: # Yay - is now an instance returned! raise error, "GetMultipleInterfaces did not return instances - got '%s', '%s'" % ( i1, i2) del i1 del i2 progress("Checking default args") rc = o.TestOptionals() if rc[:-1] != ("def", 0, 1) or abs(rc[-1] - 3.14) > .01: print rc raise error, "Did not get the optional values correctly" rc = o.TestOptionals("Hi", 2, 3, 1.1) if rc[:-1] != ("Hi", 2, 3) or abs(rc[-1] - 1.1) > .01: print rc raise error, "Did not get the specified optional values correctly" rc = o.TestOptionals2(0) if rc != (0, "", 1): print rc raise error, "Did not get the optional2 values correctly" rc = o.TestOptionals2(1.1, "Hi", 2) if rc[1:] != ("Hi", 2) or abs(rc[0] - 1.1) > .01: print rc raise error, "Did not get the specified optional2 values correctly" progress("Checking var args") o.SetVarArgs("Hi", "There", "From", "Python", 1) if o.GetLastVarArgs() != ("Hi", "There", "From", "Python", 1): raise error, "VarArgs failed -" + str(o.GetLastVarArgs()) progress("Checking getting/passing IUnknown") if o.GetSetUnknown(o) != o: raise error, "GetSetUnknown failed" progress("Checking getting/passing IDispatch") if type(o.GetSetDispatch(o)) != types.InstanceType: raise error, "GetSetDispatch failed" progress("Checking getting/passing IDispatch of known type") if o.GetSetInterface(o).__class__ != o.__class__: raise error, "GetSetDispatch failed" if o.GetSetVariant(4) != 4: raise error, "GetSetVariant (int) failed" if o.GetSetVariant("foo") != "foo": raise error, "GetSetVariant (str) failed" if o.GetSetVariant(o) != o: raise error, "GetSetVariant (dispatch) failed" for l in sys.maxint, sys.maxint + 1, 1 << 65L: if o.GetSetVariant(l) != l: raise error, "GetSetVariant (long) failed" if o.TestByRefVariant(2) != 4: raise error, "TestByRefVariant failed" if o.TestByRefString("Foo") != "FooFoo": raise error, "TestByRefString failed" # Pass some non-sequence objects to our array decoder, and watch it fail. try: o.SetVariantSafeArray("foo") raise error, "Expected a type error" except TypeError: pass try: o.SetVariantSafeArray(666) raise error, "Expected a type error" except TypeError: pass o.GetSimpleSafeArray(None) TestApplyResult(o.GetSimpleSafeArray, (None, ), tuple(range(10))) resultCheck = tuple(range(5)), tuple(range(10)), tuple(range(20)) TestApplyResult(o.GetSafeArrays, (None, None, None), resultCheck) l = [1, 2, 3, 4] TestApplyResult(o.SetVariantSafeArray, (l, ), len(l)) TestApplyResult(o.SetIntSafeArray, (l, ), len(l)) l = [] TestApplyResult(o.SetVariantSafeArray, (l, ), len(l)) TestApplyResult(o.SetIntSafeArray, (l, ), len(l)) # Tell the server to do what it does! TestApplyResult(o.Test, ("Unused", 99), 1) # A bool function TestApplyResult(o.Test, ("Unused", -1), 1) # A bool function TestApplyResult(o.Test, ("Unused", 1 == 1), 1) # A bool function TestApplyResult(o.Test, ("Unused", 0), 0) TestApplyResult(o.Test, ("Unused", 1 == 0), 0) TestApplyResult(o.Test2, (constants.Attr2, ), constants.Attr2) TestApplyResult(o.Test3, (constants.Attr2, ), constants.Attr2) TestApplyResult(o.Test4, (constants.Attr2, ), constants.Attr2) TestApplyResult(o.Test5, (constants.Attr2, ), constants.Attr2) TestApplyResult(o.Test6, (constants.WideAttr1, ), constants.WideAttr1) TestApplyResult(o.Test6, (constants.WideAttr2, ), constants.WideAttr2) TestApplyResult(o.Test6, (constants.WideAttr3, ), constants.WideAttr3) TestApplyResult(o.Test6, (constants.WideAttr4, ), constants.WideAttr4) TestApplyResult(o.Test6, (constants.WideAttr5, ), constants.WideAttr5) TestConstant("ULongTest1", 0xFFFFFFFFL) TestConstant("ULongTest2", 0x7FFFFFFFL) TestConstant("LongTest1", -0x7FFFFFFFL) TestConstant("LongTest2", 0x7FFFFFFFL) TestConstant("UCharTest", 255) TestConstant("CharTest", -1) # 'Hello Loraine', but the 'r' is the "Registered" sign (\xae) TestConstant("StringTest", u"Hello Lo\xaeaine") now = pythoncom.MakeTime(time.gmtime(time.time())) later = pythoncom.MakeTime(time.gmtime(time.time() + 1)) TestApplyResult(o.EarliestDate, (now, later), now) try: import datetime now = datetime.datetime.now() expect = pythoncom.MakeTime(now) TestApplyResult(o.EarliestDate, (now, now), expect) except ImportError: pass # py 2.2 - no datetime assert o.DoubleString("foo") == "foofoo" assert o.DoubleInOutString("foo") == "foofoo" o.LongProp = 3 if o.LongProp != 3 or o.IntProp != 3: raise error, "Property value wrong - got %d/%d" % (o.LongProp, o.IntProp) o.LongProp = o.IntProp = -3 if o.LongProp != -3 or o.IntProp != -3: raise error, "Property value wrong - got %d/%d" % (o.LongProp, o.IntProp) check = 3 * 10**9 o.ULongProp = check if o.ULongProp != check: raise error, "Property value wrong - got %d (expected %d)" % ( o.ULongProp, check) # currency. pythoncom.__future_currency__ = 1 if o.CurrencyProp != 0: raise error, "Expecting 0, got %r" % (o.CurrencyProp, ) try: import decimal except ImportError: import win32com.decimal_23 as decimal for val in ("1234.5678", "1234.56", "1234"): o.CurrencyProp = decimal.Decimal(val) if o.CurrencyProp != decimal.Decimal(val): raise error, "%s got %r" % (val, o.CurrencyProp) v1 = decimal.Decimal("1234.5678") TestApplyResult(o.DoubleCurrency, (v1, ), v1 * 2) TestApplyResult(o.DoubleCurrencyByVal, (v1, ), v1 * 2) v2 = decimal.Decimal("9012.3456") TestApplyResult(o.AddCurrencies, (v1, v2), v1 + v2) o.SetParamProp(0, 1) if o.ParamProp(0) != 1: raise RuntimeError, o.paramProp(0) # Do the connection point thing... # Create a connection object. progress("Testing connection points") sessions = [] o = win32com.client.DispatchWithEvents(o, RandomEventHandler) o._Init() try: for i in range(3): session = o.Start() sessions.append(session) time.sleep(.5) finally: # Stop the servers for session in sessions: o.Stop(session) o._DumpFireds() progress("Finished generated .py test.")