home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2754 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  17.6 KB  |  555 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. sys.coinit_flags = 0
  6. import win32api
  7. import types
  8. import pythoncom
  9. import time
  10. import sys
  11. import os
  12. import win32com
  13. import win32com.client.connect as win32com
  14. from win32com.test.util import CheckClean
  15. from win32com.client import constants
  16. import win32com
  17. from util import RegisterPythonServer
  18. 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++'
  19. error = Exception
  20. RegisterPythonServer(os.path.join(os.path.dirname(__file__), '..', 'servers', 'test_pycomtest.py'))
  21. from win32com.client import gencache
  22.  
  23. try:
  24.     gencache.EnsureModule('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1)
  25. except pythoncom.com_error:
  26.     print 'The PyCOMTest module can not be located or generated.'
  27.     print importMsg
  28.     raise RuntimeError, importMsg
  29.  
  30. from win32com import universal
  31. universal.RegisterInterfaces('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1)
  32. verbose = 0
  33.  
  34. def progress(*args):
  35.     if verbose:
  36.         for arg in args:
  37.             print arg,
  38.         
  39.         print 
  40.     
  41.  
  42.  
  43. def TestApplyResult(fn, args, result):
  44.     
  45.     try:
  46.         import string
  47.         fnName = string.split(str(fn))[1]
  48.     except:
  49.         fnName = str(fn)
  50.  
  51.     progress('Testing ', fnName)
  52.     pref = 'function ' + fnName
  53.     rc = apply(fn, args)
  54.     if rc != result:
  55.         raise error, '%s failed - result not %r but %r' % (pref, result, rc)
  56.     rc != result
  57.  
  58.  
  59. def TestConstant(constName, pyConst):
  60.     
  61.     try:
  62.         comConst = getattr(constants, constName)
  63.     except:
  64.         raise error, 'Constant %s missing' % (constName,)
  65.  
  66.     if comConst != pyConst:
  67.         raise error, 'Constant value wrong for %s - got %s, wanted %s' % (constName, comConst, pyConst)
  68.     comConst != pyConst
  69.  
  70.  
  71. class RandomEventHandler:
  72.     
  73.     def _Init(self):
  74.         self.fireds = { }
  75.  
  76.     
  77.     def OnFire(self, no):
  78.         
  79.         try:
  80.             self.fireds[no] = self.fireds[no] + 1
  81.         except KeyError:
  82.             self.fireds[no] = 0
  83.  
  84.  
  85.     
  86.     def OnFireWithNamedParams(self, no, a_bool, out1, out2):
  87.         Missing = pythoncom.Missing
  88.         if no is not Missing:
  89.             pass
  90.         
  91.         return (out1 + 2, out2 + 2)
  92.  
  93.     
  94.     def _DumpFireds(self):
  95.         if not self.fireds:
  96.             print 'ERROR: Nothing was received!'
  97.         
  98.         for firedId, no in self.fireds.items():
  99.             progress('ID %d fired %d times' % (firedId, no))
  100.         
  101.  
  102.  
  103.  
  104. def TestDynamic():
  105.     progress('Testing Dynamic')
  106.     import win32com.client.dynamic as win32com
  107.     o = win32com.client.dynamic.DumbDispatch('PyCOMTest.PyCOMTest')
  108.     progress('Getting counter')
  109.     counter = o.GetSimpleCounter()
  110.     TestCounter(counter, 0)
  111.     progress('Checking default args')
  112.     rc = o.TestOptionals()
  113.     if rc[:-1] != ('def', 0, 1) or abs(rc[-1] - 3.14) > 0.01:
  114.         print rc
  115.         raise error, 'Did not get the optional values correctly'
  116.     abs(rc[-1] - 3.14) > 0.01
  117.     rc = o.TestOptionals('Hi', 2, 3, 1.1)
  118.     if rc[:-1] != ('Hi', 2, 3) or abs(rc[-1] - 1.1) > 0.01:
  119.         print rc
  120.         raise error, 'Did not get the specified optional values correctly'
  121.     abs(rc[-1] - 1.1) > 0.01
  122.     rc = o.TestOptionals2(0)
  123.     if rc != (0, '', 1):
  124.         print rc
  125.         raise error, 'Did not get the optional2 values correctly'
  126.     rc != (0, '', 1)
  127.     rc = o.TestOptionals2(1.1, 'Hi', 2)
  128.     if rc[1:] != ('Hi', 2) or abs(rc[0] - 1.1) > 0.01:
  129.         print rc
  130.         raise error, 'Did not get the specified optional2 values correctly'
  131.     abs(rc[0] - 1.1) > 0.01
  132.     r = o.GetStruct()
  133.     counter = win32com.client.dynamic.DumbDispatch('PyCOMTest.SimpleCounter')
  134.     TestCounter(counter, 0)
  135.     l = []
  136.     TestApplyResult(o.SetVariantSafeArray, (l,), len(l))
  137.     l = [
  138.         1,
  139.         2,
  140.         3,
  141.         4]
  142.     TestApplyResult(o.SetVariantSafeArray, (l,), len(l))
  143.     TestApplyResult(o.CheckVariantSafeArray, ((1, 2, 3, 4),), 1)
  144.     o.LongProp = 3
  145.     if o.LongProp != 3 or o.IntProp != 3:
  146.         raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp)
  147.     o.IntProp != 3
  148.     o.LongProp = o.IntProp = -3
  149.     if o.LongProp != -3 or o.IntProp != -3:
  150.         raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp)
  151.     o.IntProp != -3
  152.     check = 3 * 1000000000
  153.     o.ULongProp = check
  154.     if o.ULongProp != check:
  155.         raise error, 'Property value wrong - got %d (expected %d)' % (o.ULongProp, check)
  156.     o.ULongProp != check
  157.     pythoncom.__future_currency__ = 1
  158.     if o.CurrencyProp != 0:
  159.         raise error, 'Expecting 0, got %r' % (o.CurrencyProp,)
  160.     o.CurrencyProp != 0
  161.     
  162.     try:
  163.         import decimal
  164.     except ImportError:
  165.         import win32com.decimal_23 as decimal
  166.  
  167.     o.CurrencyProp = decimal.Decimal('1234.5678')
  168.     if o.CurrencyProp != decimal.Decimal('1234.5678'):
  169.         raise error, 'got %r' % (o.CurrencyProp,)
  170.     o.CurrencyProp != decimal.Decimal('1234.5678')
  171.     v1 = decimal.Decimal('1234.5678')
  172.     TestApplyResult(o.DoubleCurrency, (v1,), v1 * 2)
  173.     v2 = decimal.Decimal('9012.3456')
  174.     TestApplyResult(o.AddCurrencies, (v1, v2), v1 + v2)
  175.     
  176.     try:
  177.         import datetime
  178.         now = datetime.datetime.now()
  179.         expect = pythoncom.MakeTime(now)
  180.         TestApplyResult(o.EarliestDate, (now, now), expect)
  181.     except ImportError:
  182.         pass
  183.  
  184.  
  185.  
  186. def TestGenerated():
  187.     EnsureDispatch = EnsureDispatch
  188.     import win32com.client.gencache
  189.     o = EnsureDispatch('PyCOMTest.PyCOMTest')
  190.     counter = o.GetSimpleCounter()
  191.     TestCounter(counter, 1)
  192.     counter = EnsureDispatch('PyCOMTest.SimpleCounter')
  193.     TestCounter(counter, 1)
  194.     (i1, i2) = o.GetMultipleInterfaces()
  195.     if type(i1) != types.InstanceType or type(i2) != types.InstanceType:
  196.         raise error, "GetMultipleInterfaces did not return instances - got '%s', '%s'" % (i1, i2)
  197.     type(i2) != types.InstanceType
  198.     del i1
  199.     del i2
  200.     progress('Checking default args')
  201.     rc = o.TestOptionals()
  202.     if rc[:-1] != ('def', 0, 1) or abs(rc[-1] - 3.14) > 0.01:
  203.         print rc
  204.         raise error, 'Did not get the optional values correctly'
  205.     abs(rc[-1] - 3.14) > 0.01
  206.     rc = o.TestOptionals('Hi', 2, 3, 1.1)
  207.     if rc[:-1] != ('Hi', 2, 3) or abs(rc[-1] - 1.1) > 0.01:
  208.         print rc
  209.         raise error, 'Did not get the specified optional values correctly'
  210.     abs(rc[-1] - 1.1) > 0.01
  211.     rc = o.TestOptionals2(0)
  212.     if rc != (0, '', 1):
  213.         print rc
  214.         raise error, 'Did not get the optional2 values correctly'
  215.     rc != (0, '', 1)
  216.     rc = o.TestOptionals2(1.1, 'Hi', 2)
  217.     if rc[1:] != ('Hi', 2) or abs(rc[0] - 1.1) > 0.01:
  218.         print rc
  219.         raise error, 'Did not get the specified optional2 values correctly'
  220.     abs(rc[0] - 1.1) > 0.01
  221.     progress('Checking var args')
  222.     o.SetVarArgs('Hi', 'There', 'From', 'Python', 1)
  223.     if o.GetLastVarArgs() != ('Hi', 'There', 'From', 'Python', 1):
  224.         raise error, 'VarArgs failed -' + str(o.GetLastVarArgs())
  225.     o.GetLastVarArgs() != ('Hi', 'There', 'From', 'Python', 1)
  226.     progress('Checking getting/passing IUnknown')
  227.     if o.GetSetUnknown(o) != o:
  228.         raise error, 'GetSetUnknown failed'
  229.     o.GetSetUnknown(o) != o
  230.     progress('Checking getting/passing IDispatch')
  231.     if type(o.GetSetDispatch(o)) != types.InstanceType:
  232.         raise error, 'GetSetDispatch failed'
  233.     type(o.GetSetDispatch(o)) != types.InstanceType
  234.     progress('Checking getting/passing IDispatch of known type')
  235.     if o.GetSetInterface(o).__class__ != o.__class__:
  236.         raise error, 'GetSetDispatch failed'
  237.     o.GetSetInterface(o).__class__ != o.__class__
  238.     if o.GetSetVariant(4) != 4:
  239.         raise error, 'GetSetVariant (int) failed'
  240.     o.GetSetVariant(4) != 4
  241.     if o.GetSetVariant('foo') != 'foo':
  242.         raise error, 'GetSetVariant (str) failed'
  243.     o.GetSetVariant('foo') != 'foo'
  244.     if o.GetSetVariant(o) != o:
  245.         raise error, 'GetSetVariant (dispatch) failed'
  246.     o.GetSetVariant(o) != o
  247.     for l in (sys.maxint, sys.maxint + 1, 0x20000000000000000L):
  248.         if o.GetSetVariant(l) != l:
  249.             raise error, 'GetSetVariant (long) failed'
  250.         o.GetSetVariant(l) != l
  251.     
  252.     if o.TestByRefVariant(2) != 4:
  253.         raise error, 'TestByRefVariant failed'
  254.     o.TestByRefVariant(2) != 4
  255.     if o.TestByRefString('Foo') != 'FooFoo':
  256.         raise error, 'TestByRefString failed'
  257.     o.TestByRefString('Foo') != 'FooFoo'
  258.     
  259.     try:
  260.         o.SetVariantSafeArray('foo')
  261.         raise error, 'Expected a type error'
  262.     except TypeError:
  263.         pass
  264.  
  265.     
  266.     try:
  267.         o.SetVariantSafeArray(666)
  268.         raise error, 'Expected a type error'
  269.     except TypeError:
  270.         pass
  271.  
  272.     o.GetSimpleSafeArray(None)
  273.     TestApplyResult(o.GetSimpleSafeArray, (None,), tuple(range(10)))
  274.     resultCheck = (tuple(range(5)), tuple(range(10)), tuple(range(20)))
  275.     TestApplyResult(o.GetSafeArrays, (None, None, None), resultCheck)
  276.     l = [
  277.         1,
  278.         2,
  279.         3,
  280.         4]
  281.     TestApplyResult(o.SetVariantSafeArray, (l,), len(l))
  282.     TestApplyResult(o.SetIntSafeArray, (l,), len(l))
  283.     l = []
  284.     TestApplyResult(o.SetVariantSafeArray, (l,), len(l))
  285.     TestApplyResult(o.SetIntSafeArray, (l,), len(l))
  286.     TestApplyResult(o.Test, ('Unused', 99), 1)
  287.     TestApplyResult(o.Test, ('Unused', -1), 1)
  288.     TestApplyResult(o.Test, ('Unused', 1 == 1), 1)
  289.     TestApplyResult(o.Test, ('Unused', 0), 0)
  290.     TestApplyResult(o.Test, ('Unused', 1 == 0), 0)
  291.     TestApplyResult(o.Test2, (constants.Attr2,), constants.Attr2)
  292.     TestApplyResult(o.Test3, (constants.Attr2,), constants.Attr2)
  293.     TestApplyResult(o.Test4, (constants.Attr2,), constants.Attr2)
  294.     TestApplyResult(o.Test5, (constants.Attr2,), constants.Attr2)
  295.     TestApplyResult(o.Test6, (constants.WideAttr1,), constants.WideAttr1)
  296.     TestApplyResult(o.Test6, (constants.WideAttr2,), constants.WideAttr2)
  297.     TestApplyResult(o.Test6, (constants.WideAttr3,), constants.WideAttr3)
  298.     TestApplyResult(o.Test6, (constants.WideAttr4,), constants.WideAttr4)
  299.     TestApplyResult(o.Test6, (constants.WideAttr5,), constants.WideAttr5)
  300.     TestConstant('ULongTest1', 0xFFFFFFFFL)
  301.     TestConstant('ULongTest2', 0x7FFFFFFFL)
  302.     TestConstant('LongTest1', -0x7FFFFFFFL)
  303.     TestConstant('LongTest2', 0x7FFFFFFFL)
  304.     TestConstant('UCharTest', 255)
  305.     TestConstant('CharTest', -1)
  306.     TestConstant('StringTest', u'Hello Lo┬«aine')
  307.     now = pythoncom.MakeTime(time.gmtime(time.time()))
  308.     later = pythoncom.MakeTime(time.gmtime(time.time() + 1))
  309.     TestApplyResult(o.EarliestDate, (now, later), now)
  310.     
  311.     try:
  312.         import datetime
  313.         now = datetime.datetime.now()
  314.         expect = pythoncom.MakeTime(now)
  315.         TestApplyResult(o.EarliestDate, (now, now), expect)
  316.     except ImportError:
  317.         pass
  318.  
  319.     o.LongProp = 3
  320.     if o.LongProp != 3 or o.IntProp != 3:
  321.         raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp)
  322.     o.IntProp != 3
  323.     o.LongProp = o.IntProp = -3
  324.     if o.LongProp != -3 or o.IntProp != -3:
  325.         raise error, 'Property value wrong - got %d/%d' % (o.LongProp, o.IntProp)
  326.     o.IntProp != -3
  327.     check = 3 * 1000000000
  328.     o.ULongProp = check
  329.     if o.ULongProp != check:
  330.         raise error, 'Property value wrong - got %d (expected %d)' % (o.ULongProp, check)
  331.     o.ULongProp != check
  332.     pythoncom.__future_currency__ = 1
  333.     if o.CurrencyProp != 0:
  334.         raise error, 'Expecting 0, got %r' % (o.CurrencyProp,)
  335.     o.CurrencyProp != 0
  336.     
  337.     try:
  338.         import decimal
  339.     except ImportError:
  340.         import win32com.decimal_23 as decimal
  341.  
  342.     for val in ('1234.5678', '1234.56', '1234'):
  343.         o.CurrencyProp = decimal.Decimal(val)
  344.         if o.CurrencyProp != decimal.Decimal(val):
  345.             raise error, '%s got %r' % (val, o.CurrencyProp)
  346.         o.CurrencyProp != decimal.Decimal(val)
  347.     
  348.     v1 = decimal.Decimal('1234.5678')
  349.     TestApplyResult(o.DoubleCurrency, (v1,), v1 * 2)
  350.     TestApplyResult(o.DoubleCurrencyByVal, (v1,), v1 * 2)
  351.     v2 = decimal.Decimal('9012.3456')
  352.     TestApplyResult(o.AddCurrencies, (v1, v2), v1 + v2)
  353.     o.SetParamProp(0, 1)
  354.     if o.ParamProp(0) != 1:
  355.         raise RuntimeError, o.paramProp(0)
  356.     o.ParamProp(0) != 1
  357.     progress('Testing connection points')
  358.     sessions = []
  359.     o = win32com.client.DispatchWithEvents(o, RandomEventHandler)
  360.     o._Init()
  361.     
  362.     try:
  363.         for i in range(3):
  364.             session = o.Start()
  365.             sessions.append(session)
  366.         
  367.         time.sleep(0.5)
  368.     finally:
  369.         for session in sessions:
  370.             o.Stop(session)
  371.         
  372.         o._DumpFireds()
  373.  
  374.     progress('Finished generated .py test.')
  375.  
  376.  
  377. def TestCounter(counter, bIsGenerated):
  378.     progress('Testing counter', `counter`)
  379.     import random
  380.     for i in xrange(50):
  381.         num = int(random.random() * len(counter))
  382.         
  383.         try:
  384.             ret = counter[num]
  385.             if ret != num + 1:
  386.                 raise error, 'Random access into element %d failed - return was %s' % (num, `ret`)
  387.             ret != num + 1
  388.         continue
  389.         except IndexError:
  390.             raise error, '** IndexError accessing collection element %d' % num
  391.             continue
  392.         
  393.  
  394.     
  395.     num = 0
  396.     if bIsGenerated:
  397.         counter.SetTestProperty(1)
  398.         counter.TestProperty = 1
  399.         counter.SetTestProperty(1, 2)
  400.         if counter.TestPropertyWithDef != 0:
  401.             raise error, 'Unexpected property set value!'
  402.         counter.TestPropertyWithDef != 0
  403.         if counter.TestPropertyNoDef(1) != 1:
  404.             raise error, 'Unexpected property set value!'
  405.         counter.TestPropertyNoDef(1) != 1
  406.     
  407.     counter.LBound = 1
  408.     counter.UBound = 10
  409.     if counter.LBound != 1 or counter.UBound != 10:
  410.         print '** Error - counter did not keep its properties'
  411.     
  412.     if bIsGenerated:
  413.         bounds = counter.GetBounds()
  414.         if bounds[0] != 1 or bounds[1] != 10:
  415.             raise error, '** Error - counter did not give the same properties back'
  416.         bounds[1] != 10
  417.         counter.SetBounds(bounds[0], bounds[1])
  418.     
  419.     for item in counter:
  420.         num = num + 1
  421.     
  422.     if num != len(counter):
  423.         raise error, '*** Length of counter and loop iterations dont match ***'
  424.     num != len(counter)
  425.     if num != 10:
  426.         raise error, '*** Unexpected number of loop iterations ***'
  427.     num != 10
  428.     counter = counter._enum_.Clone()
  429.     counter.Reset()
  430.     num = 0
  431.     for item in counter:
  432.         num = num + 1
  433.     
  434.     if num != 10:
  435.         raise error, '*** Unexpected number of loop iterations - got %d ***' % num
  436.     num != 10
  437.     progress('Finished testing counter')
  438.  
  439.  
  440. def TestLocalVTable(ob):
  441.     if ob.DoubleString('foo') != 'foofoo':
  442.         raise error("couldn't foofoo")
  443.     ob.DoubleString('foo') != 'foofoo'
  444.  
  445.  
  446. def TestVTable(clsctx = pythoncom.CLSCTX_ALL):
  447.     ob = win32com.client.Dispatch('Python.Test.PyCOMTest')
  448.     TestLocalVTable(ob)
  449.     tester = win32com.client.Dispatch('PyCOMTest.PyCOMTest')
  450.     testee = pythoncom.CoCreateInstance('Python.Test.PyCOMTest', None, clsctx, pythoncom.IID_IUnknown)
  451.     
  452.     try:
  453.         tester.TestMyInterface(None)
  454.     except pythoncom.com_error:
  455.         details = None
  456.  
  457.     tester.TestMyInterface(testee)
  458.  
  459.  
  460. def TestVTable2():
  461.     ob = win32com.client.Dispatch('Python.Test.PyCOMTest')
  462.     iid = pythoncom.InterfaceNames['IPyCOMTest']
  463.     clsid = 'Python.Test.PyCOMTest'
  464.     clsctx = pythoncom.CLSCTX_SERVER
  465.     
  466.     try:
  467.         testee = pythoncom.CoCreateInstance(clsid, None, clsctx, iid)
  468.     except TypeError:
  469.         pass
  470.  
  471.  
  472.  
  473. def TestVTableMI():
  474.     clsctx = pythoncom.CLSCTX_SERVER
  475.     ob = pythoncom.CoCreateInstance('Python.Test.PyCOMTestMI', None, clsctx, pythoncom.IID_IUnknown)
  476.     ob.QueryInterface(pythoncom.IID_IStream)
  477.     ob.QueryInterface(pythoncom.IID_IStorage)
  478.     ob.QueryInterface(pythoncom.IID_IDispatch)
  479.     iid = pythoncom.InterfaceNames['IPyCOMTest']
  480.     
  481.     try:
  482.         ob.QueryInterface(iid)
  483.     except TypeError:
  484.         pass
  485.  
  486.  
  487.  
  488. def TestQueryInterface(long_lived_server = 0, iterations = 5):
  489.     tester = win32com.client.Dispatch('PyCOMTest.PyCOMTest')
  490.     if long_lived_server:
  491.         t0 = win32com.client.Dispatch('Python.Test.PyCOMTest', clsctx = pythoncom.CLSCTX_LOCAL_SERVER)
  492.     
  493.     prompt = [
  494.         'Testing QueryInterface without long-lived local-server #%d of %d...',
  495.         'Testing QueryInterface with long-lived local-server #%d of %d...']
  496.     for i in range(iterations):
  497.         progress(prompt[long_lived_server != 0] % (i + 1, iterations))
  498.         tester.TestQueryInterface()
  499.     
  500.  
  501.  
  502. class Tester(win32com.test.util.TestCase):
  503.     
  504.     def testVTableInProc(self):
  505.         for i in range(3):
  506.             progress('Testing VTables in-process #%d...' % (i + 1))
  507.             TestVTable(pythoncom.CLSCTX_INPROC_SERVER)
  508.         
  509.  
  510.     
  511.     def testVTableLocalServer(self):
  512.         for i in range(3):
  513.             progress('Testing VTables out-of-process #%d...' % (i + 1))
  514.             TestVTable(pythoncom.CLSCTX_LOCAL_SERVER)
  515.         
  516.  
  517.     
  518.     def testVTable2(self):
  519.         for i in range(3):
  520.             TestVTable2()
  521.         
  522.  
  523.     
  524.     def testVTableMI(self):
  525.         for i in range(3):
  526.             TestVTableMI()
  527.         
  528.  
  529.     
  530.     def testMultiQueryInterface(self):
  531.         TestQueryInterface(0, 6)
  532.         TestQueryInterface(1, 6)
  533.  
  534.     
  535.     def testDynamic(self):
  536.         TestDynamic()
  537.  
  538.     
  539.     def testGenerated(self):
  540.         TestGenerated()
  541.  
  542.  
  543. if __name__ == '__main__':
  544.     
  545.     def NullThreadFunc():
  546.         pass
  547.  
  548.     import thread
  549.     thread.start_new(NullThreadFunc, ())
  550.     if '-v' in sys.argv:
  551.         verbose = 1
  552.     
  553.     win32com.test.util.testmain()
  554.  
  555.