home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import sys sys.coinit_flags = 0 import win32api import types import pythoncom import time import sys import os import win32com import win32com.client.connect as win32com from win32com.test.util import CheckClean from win32com.client import constants import win32com from util import RegisterPythonServer importMsg = '**** PyCOMTest is not installed ***\n PyCOMTest is a Python test specific COM client and server.\n It is likely this server is not installed on this machine\n To install the server, you must get the win32com sources\n and build it using MS Visual C++' error = Exception RegisterPythonServer(os.path.join(os.path.dirname(__file__), '..', 'servers', 'test_pycomtest.py')) from win32com.client import gencache try: gencache.EnsureModule('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1) except pythoncom.com_error: print 'The PyCOMTest module can not be located or generated.' print importMsg raise RuntimeError, importMsg from win32com import universal universal.RegisterInterfaces('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1) verbose = 0 def progress(*args): if verbose: for arg in args: print arg, print def TestApplyResult(fn, args, result): try: import string fnName = string.split(str(fn))[1] except: fnName = str(fn) progress('Testing ', fnName) pref = 'function ' + fnName rc = apply(fn, args) if rc != result: raise error, '%s failed - result not %r but %r' % (pref, result, rc) rc != result def TestConstant(constName, pyConst): try: comConst = getattr(constants, constName) except: raise error, 'Constant %s missing' % (constName,) if comConst != pyConst: raise error, 'Constant value wrong for %s - got %s, wanted %s' % (constName, comConst, pyConst) comConst != pyConst class RandomEventHandler: def _Init(self): self.fireds = { } def OnFire(self, no): try: self.fireds[no] = self.fireds[no] + 1 except KeyError: self.fireds[no] = 0 def OnFireWithNamedParams(self, no, a_bool, out1, out2): Missing = pythoncom.Missing if no is not Missing: pass return (out1 + 2, out2 + 2) def _DumpFireds(self): if not self.fireds: print 'ERROR: Nothing was received!' for firedId, no in self.fireds.items(): progress('ID %d fired %d times' % (firedId, no)) def TestDynamic(): progress('Testing Dynamic') import win32com.client.dynamic as win32com o = win32com.client.dynamic.DumbDispatch('PyCOMTest.PyCOMTest') progress('Getting counter') counter = o.GetSimpleCounter() TestCounter(counter, 0) progress('Checking default args') rc = o.TestOptionals() if rc[:-1] != ('def', 0, 1) or abs(rc[-1] - 3.14) > 0.01: print rc raise error, 'Did not get the optional values correctly' abs(rc[-1] - 3.14) > 0.01 rc = o.TestOptionals('Hi', 2, 3, 1.1) if rc[:-1] != ('Hi', 2, 3) or abs(rc[-1] - 1.1) > 0.01: print rc raise error, 'Did not get the specified optional values correctly' abs(rc[-1] - 1.1) > 0.01 rc = o.TestOptionals2(0) if rc != (0, '', 1): print rc raise error, 'Did not get the optional2 values correctly' rc != (0, '', 1) rc = o.TestOptionals2(1.1, 'Hi', 2) if rc[1:] != ('Hi', 2) or abs(rc[0] - 1.1) > 0.01: print rc raise error, 'Did not get the specified optional2 values correctly' abs(rc[0] - 1.1) > 0.01 r = o.GetStruct() counter = win32com.client.dynamic.DumbDispatch('PyCOMTest.SimpleCounter') TestCounter(counter, 0) l = [] TestApplyResult(o.SetVariantSafeArray, (l,), len(l)) l = [ 1, 2, 3, 4] TestApplyResult(o.SetVariantSafeArray, (l,), len(l)) TestApplyResult(o.CheckVariantSafeArray, ((1, 2, 3, 4),), 1) o.LongProp = 3 if o.LongProp != 3 or o.IntProp != 3: raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp) o.IntProp != 3 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) o.IntProp != -3 check = 3 * 1000000000 o.ULongProp = check if o.ULongProp != check: raise error, 'Property value wrong - got %d (expected %d)' % (o.ULongProp, check) o.ULongProp != check pythoncom.__future_currency__ = 1 if o.CurrencyProp != 0: raise error, 'Expecting 0, got %r' % (o.CurrencyProp,) o.CurrencyProp != 0 try: import decimal except ImportError: import win32com.decimal_23 as decimal o.CurrencyProp = decimal.Decimal('1234.5678') if o.CurrencyProp != decimal.Decimal('1234.5678'): raise error, 'got %r' % (o.CurrencyProp,) o.CurrencyProp != decimal.Decimal('1234.5678') v1 = decimal.Decimal('1234.5678') TestApplyResult(o.DoubleCurrency, (v1,), v1 * 2) v2 = decimal.Decimal('9012.3456') TestApplyResult(o.AddCurrencies, (v1, v2), v1 + v2) try: import datetime now = datetime.datetime.now() expect = pythoncom.MakeTime(now) TestApplyResult(o.EarliestDate, (now, now), expect) except ImportError: pass def TestGenerated(): EnsureDispatch = EnsureDispatch import win32com.client.gencache 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: raise error, "GetMultipleInterfaces did not return instances - got '%s', '%s'" % (i1, i2) type(i2) != types.InstanceType del i1 del i2 progress('Checking default args') rc = o.TestOptionals() if rc[:-1] != ('def', 0, 1) or abs(rc[-1] - 3.14) > 0.01: print rc raise error, 'Did not get the optional values correctly' abs(rc[-1] - 3.14) > 0.01 rc = o.TestOptionals('Hi', 2, 3, 1.1) if rc[:-1] != ('Hi', 2, 3) or abs(rc[-1] - 1.1) > 0.01: print rc raise error, 'Did not get the specified optional values correctly' abs(rc[-1] - 1.1) > 0.01 rc = o.TestOptionals2(0) if rc != (0, '', 1): print rc raise error, 'Did not get the optional2 values correctly' rc != (0, '', 1) rc = o.TestOptionals2(1.1, 'Hi', 2) if rc[1:] != ('Hi', 2) or abs(rc[0] - 1.1) > 0.01: print rc raise error, 'Did not get the specified optional2 values correctly' abs(rc[0] - 1.1) > 0.01 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()) o.GetLastVarArgs() != ('Hi', 'There', 'From', 'Python', 1) progress('Checking getting/passing IUnknown') if o.GetSetUnknown(o) != o: raise error, 'GetSetUnknown failed' o.GetSetUnknown(o) != o progress('Checking getting/passing IDispatch') if type(o.GetSetDispatch(o)) != types.InstanceType: raise error, 'GetSetDispatch failed' type(o.GetSetDispatch(o)) != types.InstanceType progress('Checking getting/passing IDispatch of known type') if o.GetSetInterface(o).__class__ != o.__class__: raise error, 'GetSetDispatch failed' o.GetSetInterface(o).__class__ != o.__class__ if o.GetSetVariant(4) != 4: raise error, 'GetSetVariant (int) failed' o.GetSetVariant(4) != 4 if o.GetSetVariant('foo') != 'foo': raise error, 'GetSetVariant (str) failed' o.GetSetVariant('foo') != 'foo' if o.GetSetVariant(o) != o: raise error, 'GetSetVariant (dispatch) failed' o.GetSetVariant(o) != o for l in (sys.maxint, sys.maxint + 1, 0x20000000000000000L): if o.GetSetVariant(l) != l: raise error, 'GetSetVariant (long) failed' o.GetSetVariant(l) != l if o.TestByRefVariant(2) != 4: raise error, 'TestByRefVariant failed' o.TestByRefVariant(2) != 4 if o.TestByRefString('Foo') != 'FooFoo': raise error, 'TestByRefString failed' o.TestByRefString('Foo') != 'FooFoo' 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)) TestApplyResult(o.Test, ('Unused', 99), 1) TestApplyResult(o.Test, ('Unused', -1), 1) TestApplyResult(o.Test, ('Unused', 1 == 1), 1) 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) TestConstant('StringTest', u'Hello Lo®aine') 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 o.LongProp = 3 if o.LongProp != 3 or o.IntProp != 3: raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp) o.IntProp != 3 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) o.IntProp != -3 check = 3 * 1000000000 o.ULongProp = check if o.ULongProp != check: raise error, 'Property value wrong - got %d (expected %d)' % (o.ULongProp, check) o.ULongProp != check pythoncom.__future_currency__ = 1 if o.CurrencyProp != 0: raise error, 'Expecting 0, got %r' % (o.CurrencyProp,) o.CurrencyProp != 0 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) o.CurrencyProp != decimal.Decimal(val) 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) o.ParamProp(0) != 1 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(0.5) finally: for session in sessions: o.Stop(session) o._DumpFireds() progress('Finished generated .py test.') def TestCounter(counter, bIsGenerated): progress('Testing counter', `counter`) import random for i in xrange(50): num = int(random.random() * len(counter)) try: ret = counter[num] if ret != num + 1: raise error, 'Random access into element %d failed - return was %s' % (num, `ret`) ret != num + 1 continue except IndexError: raise error, '** IndexError accessing collection element %d' % num continue num = 0 if bIsGenerated: counter.SetTestProperty(1) counter.TestProperty = 1 counter.SetTestProperty(1, 2) if counter.TestPropertyWithDef != 0: raise error, 'Unexpected property set value!' counter.TestPropertyWithDef != 0 if counter.TestPropertyNoDef(1) != 1: raise error, 'Unexpected property set value!' counter.TestPropertyNoDef(1) != 1 counter.LBound = 1 counter.UBound = 10 if counter.LBound != 1 or counter.UBound != 10: print '** Error - counter did not keep its properties' if bIsGenerated: bounds = counter.GetBounds() if bounds[0] != 1 or bounds[1] != 10: raise error, '** Error - counter did not give the same properties back' bounds[1] != 10 counter.SetBounds(bounds[0], bounds[1]) for item in counter: num = num + 1 if num != len(counter): raise error, '*** Length of counter and loop iterations dont match ***' num != len(counter) if num != 10: raise error, '*** Unexpected number of loop iterations ***' num != 10 counter = counter._enum_.Clone() counter.Reset() num = 0 for item in counter: num = num + 1 if num != 10: raise error, '*** Unexpected number of loop iterations - got %d ***' % num num != 10 progress('Finished testing counter') def TestLocalVTable(ob): if ob.DoubleString('foo') != 'foofoo': raise error("couldn't foofoo") ob.DoubleString('foo') != 'foofoo' def TestVTable(clsctx = pythoncom.CLSCTX_ALL): ob = win32com.client.Dispatch('Python.Test.PyCOMTest') TestLocalVTable(ob) tester = win32com.client.Dispatch('PyCOMTest.PyCOMTest') testee = pythoncom.CoCreateInstance('Python.Test.PyCOMTest', None, clsctx, pythoncom.IID_IUnknown) try: tester.TestMyInterface(None) except pythoncom.com_error: details = None tester.TestMyInterface(testee) def TestVTable2(): ob = win32com.client.Dispatch('Python.Test.PyCOMTest') iid = pythoncom.InterfaceNames['IPyCOMTest'] clsid = 'Python.Test.PyCOMTest' clsctx = pythoncom.CLSCTX_SERVER try: testee = pythoncom.CoCreateInstance(clsid, None, clsctx, iid) except TypeError: pass def TestVTableMI(): clsctx = pythoncom.CLSCTX_SERVER ob = pythoncom.CoCreateInstance('Python.Test.PyCOMTestMI', None, clsctx, pythoncom.IID_IUnknown) ob.QueryInterface(pythoncom.IID_IStream) ob.QueryInterface(pythoncom.IID_IStorage) ob.QueryInterface(pythoncom.IID_IDispatch) iid = pythoncom.InterfaceNames['IPyCOMTest'] try: ob.QueryInterface(iid) except TypeError: pass def TestQueryInterface(long_lived_server = 0, iterations = 5): tester = win32com.client.Dispatch('PyCOMTest.PyCOMTest') if long_lived_server: t0 = win32com.client.Dispatch('Python.Test.PyCOMTest', clsctx = pythoncom.CLSCTX_LOCAL_SERVER) prompt = [ 'Testing QueryInterface without long-lived local-server #%d of %d...', 'Testing QueryInterface with long-lived local-server #%d of %d...'] for i in range(iterations): progress(prompt[long_lived_server != 0] % (i + 1, iterations)) tester.TestQueryInterface() class Tester(win32com.test.util.TestCase): def testVTableInProc(self): for i in range(3): progress('Testing VTables in-process #%d...' % (i + 1)) TestVTable(pythoncom.CLSCTX_INPROC_SERVER) def testVTableLocalServer(self): for i in range(3): progress('Testing VTables out-of-process #%d...' % (i + 1)) TestVTable(pythoncom.CLSCTX_LOCAL_SERVER) def testVTable2(self): for i in range(3): TestVTable2() def testVTableMI(self): for i in range(3): TestVTableMI() def testMultiQueryInterface(self): TestQueryInterface(0, 6) TestQueryInterface(1, 6) def testDynamic(self): TestDynamic() def testGenerated(self): TestGenerated() if __name__ == '__main__': def NullThreadFunc(): pass import thread thread.start_new(NullThreadFunc, ()) if '-v' in sys.argv: verbose = 1 win32com.test.util.testmain()