testHost4Dbg.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os, sys, traceback
  2. from win32com.axscript import axscript
  3. from win32com.axscript.server import axsite
  4. from win32com.axscript.server.error import Exception
  5. import pythoncom
  6. from win32com.server import util
  7. import win32ui
  8. version = "0.0.1"
  9. class MySite(axsite.AXSite):
  10. def OnScriptError(self, error):
  11. print("An error occurred in the Script Code")
  12. exc = error.GetExceptionInfo()
  13. try:
  14. text = error.GetSourceLineText()
  15. except:
  16. text = "<unknown>"
  17. context, line, char = error.GetSourcePosition()
  18. print("Exception: %s (line %d)\n%s\n%s^\n%s" % (exc[1], line, text, " " * (char-1), exc[2]))
  19. class ObjectModel:
  20. _public_methods_ = [ 'echo', 'msgbox' ]
  21. def echo(self, *args):
  22. print(''.join(map(str, args)))
  23. def msgbox(self, *args):
  24. msg = ''.join(map(str, args))
  25. win32ui.MessageBox(msg)
  26. def TestEngine():
  27. model = {'Test' : util.wrap(ObjectModel()) }
  28. scriptDir = "."
  29. site = MySite(model)
  30. pyEngine = site._AddEngine("Python")
  31. # pyEngine2 = site._AddEngine("Python")
  32. vbEngine = site._AddEngine("VBScript")
  33. # forthEngine = site._AddEngine("ForthScript")
  34. try:
  35. # code = open(os.path.join(scriptDir, "debugTest.4ths"),"rb").read()
  36. # forthEngine.AddCode(code)
  37. code = open(os.path.join(scriptDir, "debugTest.pys"),"rb").read()
  38. pyEngine.AddCode(code)
  39. code = open(os.path.join(scriptDir, "debugTest.vbs"),"rb").read()
  40. vbEngine.AddCode(code)
  41. # code = open(os.path.join(scriptDir, "debugTestFail.pys"),"rb").read()
  42. # pyEngine2.AddCode(code)
  43. # from win32com.axdebug import axdebug
  44. # sessionProvider=pythoncom.CoCreateInstance(axdebug.CLSID_DefaultDebugSessionProvider,None,pythoncom.CLSCTX_ALL, axdebug.IID_IDebugSessionProvider)
  45. # sessionProvider.StartDebugSession(None)
  46. input("Press enter to continue")
  47. # forthEngine.Start()
  48. pyEngine.Start() # Actually run the Python code
  49. vbEngine.Start() # Actually run the VB code
  50. except pythoncom.com_error as details:
  51. print("Script failed: %s (0x%x)" % (details[1], details[0]))
  52. # Now run the code expected to fail!
  53. # try:
  54. # pyEngine2.Start() # Actually run the Python code that fails!
  55. # print "Script code worked when it should have failed."
  56. # except pythoncom.com_error:
  57. # pass
  58. site._Close()
  59. if __name__ == '__main__':
  60. import win32com.axdebug.util
  61. try:
  62. TestEngine()
  63. except:
  64. traceback.print_exc()
  65. win32com.axdebug.util._dump_wrapped()
  66. sys.exc_type = sys.exc_value = sys.exc_traceback = None
  67. print(pythoncom._GetInterfaceCount(),"com objects still alive")