Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testDynamic.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Test dynamic policy, and running object table.
  2. import pythoncom
  3. import winerror
  4. from win32com.server.exception import Exception
  5. error = "testDynamic error"
  6. iid = pythoncom.MakeIID("{b48969a0-784b-11d0-ae71-d23f56000000}")
  7. class VeryPermissive:
  8. def _dynamic_(self, name, lcid, wFlags, args):
  9. if wFlags & pythoncom.DISPATCH_METHOD:
  10. return getattr(self, name)(*args)
  11. if wFlags & pythoncom.DISPATCH_PROPERTYGET:
  12. try:
  13. # to avoid problems with byref param handling, tuple results are converted to lists.
  14. ret = self.__dict__[name]
  15. if type(ret) == type(()):
  16. ret = list(ret)
  17. return ret
  18. except KeyError: # Probably a method request.
  19. raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND)
  20. if wFlags & (
  21. pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF
  22. ):
  23. setattr(self, name, args[0])
  24. return
  25. raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
  26. def write(self, *args):
  27. if len(args) == 0:
  28. raise Exception(
  29. scode=winerror.DISP_E_BADPARAMCOUNT
  30. ) # Probably call as PROPGET.
  31. for arg in args[:-1]:
  32. print(str(arg), end=" ")
  33. print(str(args[-1]))
  34. def Test():
  35. import win32com.server.policy
  36. import win32com.server.util
  37. # import win32dbg;win32dbg.brk()
  38. ob = win32com.server.util.wrap(
  39. VeryPermissive(), usePolicy=win32com.server.policy.DynamicPolicy
  40. )
  41. try:
  42. handle = pythoncom.RegisterActiveObject(ob, iid, 0)
  43. except pythoncom.com_error as details:
  44. print("Warning - could not register the object in the ROT:", details)
  45. handle = None
  46. try:
  47. import win32com.client.dynamic
  48. client = win32com.client.dynamic.Dispatch(iid)
  49. client.ANewAttr = "Hello"
  50. if client.ANewAttr != "Hello":
  51. raise error("Could not set dynamic property")
  52. v = ["Hello", "From", "Python", 1.4]
  53. client.TestSequence = v
  54. if v != list(client.TestSequence):
  55. raise error(
  56. "Dynamic sequences not working! %r/%r"
  57. % (repr(v), repr(client.testSequence))
  58. )
  59. client.write("This", "output", "has", "come", "via", "testDynamic.py")
  60. # Check our new "_FlagAsMethod" works (kinda!)
  61. client._FlagAsMethod("NotReallyAMethod")
  62. if not callable(client.NotReallyAMethod):
  63. raise error("Method I flagged as callable isn't!")
  64. client = None
  65. finally:
  66. if handle is not None:
  67. pythoncom.RevokeActiveObject(handle)
  68. print("Test worked!")
  69. if __name__ == "__main__":
  70. Test()