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.

leakTest.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import sys
  2. import pythoncom
  3. import win32com.server.policy
  4. from win32com.axscript import axscript
  5. from win32com.axscript.server import axsite
  6. from win32com.axscript.server.error import Exception
  7. from win32com.server import connect, util
  8. class MySite(axsite.AXSite):
  9. def OnScriptError(self, error):
  10. exc = error.GetExceptionInfo()
  11. context, line, char = error.GetSourcePosition()
  12. print(" >Exception:", exc[1])
  13. try:
  14. st = error.GetSourceLineText()
  15. except pythoncom.com_error:
  16. st = None
  17. if st is None:
  18. st = ""
  19. text = st + "\n" + (" " * (char - 1)) + "^" + "\n" + exc[2]
  20. for line in text.splitlines():
  21. print(" >" + line)
  22. class MyCollection(util.Collection):
  23. def _NewEnum(self):
  24. print("Making new Enumerator")
  25. return util.Collection._NewEnum(self)
  26. class Test:
  27. _public_methods_ = ["echo"]
  28. _public_attrs_ = ["collection", "verbose"]
  29. def __init__(self):
  30. self.verbose = 0
  31. self.collection = util.wrap(MyCollection([1, "Two", 3]))
  32. self.last = ""
  33. # self._connect_server_ = TestConnectServer(self)
  34. def echo(self, *args):
  35. self.last = "".join(map(str, args))
  36. if self.verbose:
  37. for arg in args:
  38. print(arg, end=" ")
  39. print()
  40. # self._connect_server_.Broadcast(last)
  41. #### Connections currently wont work, as there is no way for the engine to
  42. #### know what events we support. We need typeinfo support.
  43. IID_ITestEvents = pythoncom.MakeIID("{8EB72F90-0D44-11d1-9C4B-00AA00125A98}")
  44. class TestConnectServer(connect.ConnectableServer):
  45. _connect_interfaces_ = [IID_ITestEvents]
  46. # The single public method that the client can call on us
  47. # (ie, as a normal COM server, this exposes just this single method.
  48. def __init__(self, object):
  49. self.object = object
  50. def Broadcast(self, arg):
  51. # Simply broadcast a notification.
  52. self._BroadcastNotify(self.NotifyDoneIt, (arg,))
  53. def NotifyDoneIt(self, interface, arg):
  54. interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)
  55. VBScript = """\
  56. prop = "Property Value"
  57. sub hello(arg1)
  58. test.echo arg1
  59. end sub
  60. sub testcollection
  61. test.verbose = 1
  62. for each item in test.collection
  63. test.echo "Collection item is", item
  64. next
  65. end sub
  66. """
  67. if sys.version_info < (3,):
  68. PyScript = """print "PyScript is being parsed..."\n"""
  69. else:
  70. PyScript = """print("PyScript is being parsed...")\n"""
  71. PyScript += """\
  72. prop = "Property Value"
  73. def hello(arg1):
  74. test.echo(arg1)
  75. pass
  76. def testcollection():
  77. test.verbose = 1
  78. # test.collection[1] = "New one"
  79. for item in test.collection:
  80. test.echo("Collection item is", item)
  81. pass
  82. """
  83. ErrScript = """\
  84. bad code for everyone!
  85. """
  86. def TestEngine(engineName, code, bShouldWork=1):
  87. echoer = Test()
  88. model = {
  89. "test": util.wrap(echoer),
  90. }
  91. site = MySite(model)
  92. engine = site._AddEngine(engineName)
  93. engine.AddCode(code, axscript.SCRIPTTEXT_ISPERSISTENT)
  94. try:
  95. engine.Start()
  96. finally:
  97. if not bShouldWork:
  98. engine.Close()
  99. return
  100. doTestEngine(engine, echoer)
  101. # re-transition the engine back to the UNINITIALIZED state, a-la ASP.
  102. engine.eScript.SetScriptState(axscript.SCRIPTSTATE_UNINITIALIZED)
  103. engine.eScript.SetScriptSite(util.wrap(site))
  104. print("restarting")
  105. engine.Start()
  106. # all done!
  107. engine.Close()
  108. def doTestEngine(engine, echoer):
  109. # Now call into the scripts IDispatch
  110. from win32com.client.dynamic import Dispatch
  111. ob = Dispatch(engine.GetScriptDispatch())
  112. try:
  113. ob.hello("Goober")
  114. except pythoncom.com_error as exc:
  115. print("***** Calling 'hello' failed", exc)
  116. return
  117. if echoer.last != "Goober":
  118. print("***** Function call didnt set value correctly", repr(echoer.last))
  119. if str(ob.prop) != "Property Value":
  120. print("***** Property Value not correct - ", repr(ob.prop))
  121. ob.testcollection()
  122. # Now make sure my engines can evaluate stuff.
  123. result = engine.eParse.ParseScriptText(
  124. "1+1", None, None, None, 0, 0, axscript.SCRIPTTEXT_ISEXPRESSION
  125. )
  126. if result != 2:
  127. print("Engine could not evaluate '1+1' - said the result was", result)
  128. def dotestall():
  129. for i in range(10):
  130. TestEngine("Python", PyScript)
  131. print(sys.gettotalrefcount())
  132. ## print "Testing Exceptions"
  133. ## try:
  134. ## TestEngine("Python", ErrScript, 0)
  135. ## except pythoncom.com_error:
  136. ## pass
  137. def testall():
  138. dotestall()
  139. pythoncom.CoUninitialize()
  140. print(
  141. "AXScript Host worked correctly - %d/%d COM objects left alive."
  142. % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  143. )
  144. if __name__ == "__main__":
  145. testall()