home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testDynamic.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.7 KB  |  62 lines

  1. # Test dynamic policy, and running object table.
  2.  
  3. import pythoncom
  4. import winerror
  5.  
  6. from win32com.server.exception import Exception
  7.  
  8. error = "testDynamic error"
  9.  
  10. iid = pythoncom.MakeIID("{b48969a0-784b-11d0-ae71-d23f56000000}")
  11.  
  12. class VeryPermissive:
  13.     def _dynamic_(self, name, lcid, wFlags, args):
  14.         if wFlags & pythoncom.DISPATCH_METHOD:
  15.             return apply(getattr(self,name),args)
  16.  
  17.         if wFlags & pythoncom.DISPATCH_PROPERTYGET:
  18.             try:
  19.                 return self.__dict__[name]
  20.             except KeyError: # Probably a method request.
  21.                 raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND)
  22.  
  23.         if wFlags & (pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF):
  24.             setattr(self, name, args[0])
  25.             return
  26.  
  27.         raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
  28.  
  29.     def write(self, *args):
  30.         if len(args)==0:
  31.             raise Exception(scode=winerror.DISP_E_BADPARAMCOUNT) # Probably call as PROPGET.
  32.  
  33.         for arg in args[:-1]:
  34.             print str(arg),
  35.         print str(args[-1])
  36.  
  37. def Test():
  38.     import win32com.server.util, win32com.server.policy
  39. #    import win32dbg;win32dbg.brk()
  40.     ob = win32com.server.util.wrap(VeryPermissive(),usePolicy=win32com.server.policy.DynamicPolicy)
  41.     handle = pythoncom.RegisterActiveObject(ob, iid, 0)
  42.     try:
  43.         import win32com.client.dynamic
  44.         client = win32com.client.dynamic.Dispatch(iid)
  45.         client.ANewAttr = "Hello"
  46.         if client.ANewAttr != "Hello":
  47.             raise error, "Could not set dynamic property"
  48.  
  49.         v = ["Hello","From","Python",1.4]
  50.         client.TestSequence = v
  51.         if v != list(client.TestSequence):
  52.             raise error, "Dynamic sequences not working!"
  53.             
  54.         client.write("This","output","has","come","via","COM")
  55.  
  56.         client = None
  57.     finally:
  58.         pythoncom.RevokeActiveObject(handle)
  59.  
  60. if __name__=='__main__':
  61.     Test()
  62.