PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testDynamic.py



0001 # Test dynamic policy, and running object table.
0002 
0003 import pythoncom
0004 import winerror
0005 
0006 from win32com.server.exception import Exception
0007 
0008 error = "testDynamic error"
0009 
0010 iid = pythoncom.MakeIID("{b48969a0-784b-11d0-ae71-d23f56000000}")
0011 
0012 class VeryPermissive:
0013     def _dynamic_(self, name, lcid, wFlags, args):
0014         if wFlags & pythoncom.DISPATCH_METHOD:
0015             return apply(getattr(self,name),args)
0016 
0017         if wFlags & pythoncom.DISPATCH_PROPERTYGET:
0018             try:
0019                 # to avoid problems with byref param handling, tuple results are converted to lists.
0020                 ret = self.__dict__[name]
0021                 if type(ret)==type(()):
0022                     ret = list(ret)
0023                 return ret
0024             except KeyError: # Probably a method request.
0025                 raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND)
0026 
0027         if wFlags & (pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF):
0028             setattr(self, name, args[0])
0029             return
0030 
0031         raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
0032 
0033     def write(self, *args):
0034         if len(args)==0:
0035             raise Exception(scode=winerror.DISP_E_BADPARAMCOUNT) # Probably call as PROPGET.
0036 
0037         for arg in args[:-1]:
0038             print str(arg),
0039         print str(args[-1])
0040 
0041 def Test():
0042     import win32com.server.util, win32com.server.policy
0043 #       import win32dbg;win32dbg.brk()
0044     ob = win32com.server.util.wrap(VeryPermissive(),usePolicy=win32com.server.policy.DynamicPolicy)
0045     try:
0046         handle = pythoncom.RegisterActiveObject(ob, iid, 0)
0047     except pythoncom.com_error, details:
0048         print "Warning - could not register the object in the ROT:", details
0049         handle = None
0050     try:
0051         import win32com.client.dynamic
0052         client = win32com.client.dynamic.Dispatch(iid)
0053         client.ANewAttr = "Hello"
0054         if client.ANewAttr != "Hello":
0055             raise error, "Could not set dynamic property"
0056 
0057         v = ["Hello","From","Python",1.4]
0058         client.TestSequence = v
0059         if v != list(client.TestSequence):
0060             raise error, "Dynamic sequences not working! %r/%r" % (repr(v), repr(client.testSequence))
0061 
0062         client.write("This","output","has","come","via","COM")
0063         # Check our new "_FlagAsMethod" works (kinda!)
0064         client._FlagAsMethod("NotReallyAMethod")
0065         if not callable(client.NotReallyAMethod):
0066             raise error, "Method I flagged as callable isn't!"
0067 
0068 
0069         client = None
0070     finally:
0071         if handle is not None:
0072             pythoncom.RevokeActiveObject(handle)
0073 
0074 if __name__=='__main__':
0075     Test()
0076 

Generated by PyXR 0.9.4
SourceForge.net Logo