home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / debug.py < prev    next >
Text File  |  2002-06-01  |  7KB  |  209 lines

  1. import traceback, sys, string
  2.  
  3. import win32com.server.util
  4. from win32com.util import IIDToInterfaceName
  5. from win32com.client.util import Enumerator
  6. from win32com.server.exception import COMException
  7. import pythoncom
  8. from framework import trace
  9. from win32com.axdebug import axdebug, gateways, contexts, stackframe, documents, adb
  10. from win32com.axdebug.codecontainer import SourceCodeContainer
  11. from win32com.axdebug.util import _wrap, _wrap_remove
  12. import win32com.client.connect
  13. import win32api, winerror
  14. import os
  15.  
  16. try:
  17.     os.environ["DEBUG_AXDEBUG"]
  18.     debuggingTrace = 1        # Should we print "trace" output?
  19. except KeyError:
  20.     debuggingTrace = 0
  21.  
  22. def trace(*args):
  23.     """A function used instead of "print" for debugging output.
  24.     """
  25.     if not debuggingTrace:
  26.         return 
  27.     print win32api.GetCurrentThreadId(),
  28.     for arg in args:
  29.         print arg,
  30.     print
  31.  
  32. # Note that the DebugManager is not a COM gateway class for the 
  33. # debugger - but it does create and manage them.
  34. class DebugManager:
  35.     _debugger_interfaces_ = [axdebug.IID_IActiveScriptDebug]
  36.     def __init__(self, scriptEngine):
  37.         self.scriptEngine = scriptEngine
  38.         self.adb = adb.Debugger()
  39.         self.rootNode = None
  40.         self.debugApplication = None
  41.         self.ccProvider = documents.CodeContainerProvider()
  42.         try:
  43.             self.scriptSiteDebug = scriptEngine.GetScriptSite(axdebug.IID_IActiveScriptSiteDebug)
  44.         except pythoncom.com_error:
  45.             # No debugger interface (ie, dumb host).  Do the extra work.
  46.             trace("Scripting site has no debugger interface")
  47.             self.scriptSiteDebug = None
  48.         # Get the debug application object.
  49.         self.debugApplication = None
  50.         if self.scriptSiteDebug is not None:
  51.             # Spec says that we should test for this, and if it fails revert to
  52.             # PDM application.
  53.             try:
  54.                 self.debugApplication = self.scriptSiteDebug.GetApplication()
  55.                 self.rootNode = self.scriptSiteDebug.GetRootApplicationNode()
  56.             except pythoncom.com_error:
  57.                 self.debugApplication = None
  58.                 
  59.         if self.debugApplication is None:
  60.             # Try to get/create the default one
  61.             # NOTE - Dont catch exceptions here - let the parent do it,
  62.             # so it knows debug support is available.
  63.             pdm=pythoncom.CoCreateInstance(axdebug.CLSID_ProcessDebugManager,None,pythoncom.CLSCTX_ALL, axdebug.IID_IProcessDebugManager)
  64.             self.debugApplication = pdm.GetDefaultApplication()
  65.             self.rootNode = self.debugApplication.GetRootNode()
  66.             
  67.         assert self.debugApplication is not None, "Need to have a DebugApplication object by now!"
  68.         self.activeScriptDebug = None
  69.  
  70.         if self.debugApplication is not None:
  71.             self.adb.AttachApp(self.debugApplication, self.ccProvider)
  72.         self.codeContainers = {}
  73.         self.activeScriptDebug = _wrap(ActiveScriptDebug(self, self.codeContainers), axdebug.IID_IActiveScriptDebug)
  74.  
  75.     def Close(self):
  76.         # Called by the language engine when it receives a close request
  77.         if self.activeScriptDebug is not None:
  78.             _wrap_remove(self.activeScriptDebug)
  79.             self.activeScriptDebug = None
  80.         self.scriptEngine = None
  81.         self.rootNode = None
  82.         self.debugApplication = None
  83.         self.scriptSiteDebug = None
  84.         if self.ccProvider is not None:
  85.             self.ccProvider.Close()
  86.             self.ccProvider = None
  87.         self.codeContainers = {}
  88.         if self.adb:
  89.             self.adb.CloseApp()
  90.             self.adb = None
  91. #        print "Close complete"
  92.  
  93.     def IsAnyHost(self):
  94.         "Do we have _any_ debugging interfaces installed?"
  95.         return self.debugApplication is not None
  96.  
  97.     def IsSimpleHost(self):
  98.         return self.scriptSiteDebug is None
  99.  
  100.     def HandleRuntimeError( self ):
  101.         """Called by the engine when a runtime error occurs.  If we have a debugger,
  102.         we let it know.
  103.         
  104.         The result is a boolean which indicates if the error handler should call
  105.         IActiveScriptSite::OnScriptError()
  106.         """
  107. #        if self.IsAnyHost:
  108. #            site = _wrap(self, axdebug.IID_IActiveScriptSite)
  109. #            breakResume, errorResume, fCallOnError = self.debugApplication(activeScriptErrorDebug, site)
  110.             # Do something with these!
  111. #        else:
  112.         trace("HandleRuntimeError")
  113.         fCallOnError = 1
  114.         return fCallOnError
  115.  
  116.     def _query_interface_for_debugger_(self, iid):
  117.         if iid in self._debugger_interfaces_:
  118.             return self.activeScriptDebug
  119.         trace("DebugManager QI - unknown IID", iid)
  120.         return 0
  121.         
  122.         
  123.     def OnEnterScript(self):
  124.         trace("OnEnterScript")
  125.         try:
  126.             1/0
  127.         except:
  128.             # Bit of a hack - reach into engine.
  129.             baseFrame = sys.exc_info()[2].tb_frame.f_back
  130.         self.adb.SetupAXDebugging(baseFrame)
  131.  
  132.     def OnLeaveScript(self):
  133.         trace("OnLeaveScript")
  134.         self.adb.ResetAXDebugging()
  135.  
  136.     def AddScriptBlock(self, codeBlock):
  137.         # If we dont have debugging support, dont bother.
  138.         cc = DebugCodeBlockContainer(codeBlock, self.scriptSiteDebug)
  139.         if self.IsSimpleHost():
  140.             document = documents.DebugDocumentText(cc)
  141.             document = _wrap(document, axdebug.IID_IDebugDocument)
  142.             provider = documents.DebugDocumentProvider(document)
  143.             provider = _wrap(provider, axdebug.IID_IDebugDocumentProvider)
  144.             cc.debugDocument = document
  145.             newNode = self.debugApplication.CreateApplicationNode()
  146.             newNode.SetDocumentProvider(provider)
  147.             newNode.Attach(self.rootNode)
  148.         else:
  149.             newNode = None # Managed by smart host.
  150.             self.codeContainers[cc.sourceContext] = cc
  151.         self.ccProvider.AddCodeContainer(cc, newNode)
  152.  
  153. class DebugCodeBlockContainer(SourceCodeContainer):
  154.     def __init__(self, codeBlock, site):
  155.         self.codeBlock = codeBlock
  156.         SourceCodeContainer.__init__(self, codeBlock.codeText, codeBlock.GetFileName(), codeBlock.sourceContextCookie, codeBlock.startLineNumber, site)
  157.  
  158.     def GetName(self, dnt):
  159.         if dnt==axdebug.DOCUMENTNAMETYPE_APPNODE:
  160.             return self.codeBlock.GetDisplayName()
  161.         elif dnt==axdebug.DOCUMENTNAMETYPE_TITLE:
  162.             return self.codeBlock.GetDisplayName()
  163. #        elif dnt==axdebug.DOCUMENTNAMETYPE_FILE_TAIL:
  164. #        elif dnt==axdebug.DOCUMENTNAMETYPE_URL:
  165.         else:
  166.             raise COMException(scode=winerror.S_FALSE)
  167.  
  168.  
  169. class EnumDebugCodeContexts(gateways.EnumDebugCodeContexts):
  170.     def _wrap(self, ob):
  171.         return ob
  172.  
  173. class ActiveScriptDebug:
  174.     """The class which implements the IActiveScriptDebug interface for the Active Script engine.
  175.  
  176.        Only ever used by smart hosts.
  177.     """
  178.     _public_methods_ = ["GetScriptTextAttributes", "GetScriptletTextAttributes", "EnumCodeContextsOfPosition"]
  179.     _com_interfaces_ = [axdebug.IID_IActiveScriptDebug]
  180.     def __init__(self, debugMgr, codeContainers):
  181.         self.debugMgr = debugMgr
  182.         self.scriptSiteDebug = debugMgr.scriptSiteDebug
  183.         self.codeContainers = codeContainers
  184.  
  185.     def _Close(self):
  186.         self.debugMgr = None
  187.         self.scriptSiteDebug = None
  188.         self.codeContainers = {}
  189.  
  190.     def _query_interface_(self, iid):
  191.         trace("DebuggerQI with", iid)
  192.         return 0
  193.  
  194.     def GetScriptTextAttributes(self, code, delim, flags):
  195.         container = SourceCodeContainer(code, "<Temp Code Block>")
  196.         return container.GetSyntaxColorAttributes()
  197.     def GetScriptletTextAttributes(self, code, delim, flags):
  198.         trace ("GetScriptletTextAttributes", code, delim, flags)
  199.         container = SourceCodeContainer(code, "<Temp Code Block>")
  200.         return container.GetSyntaxColorAttributes()
  201.  
  202.     def EnumCodeContextsOfPosition(self, context, charOffset, numChars):
  203.         trace("EnumCodeContextsOfPosition", context, charOffset, numChars)
  204.         try:
  205.             context = self.codeContainers[context].GetCodeContextAtPosition(charOffset)
  206.         except KeyError:
  207.             raise COMException(scode=winerror.E_UNEXPECTED)
  208.         enum = EnumDebugCodeContexts([context])
  209.         return _wrap(enum, axdebug.IID_IEnumDebugCodeContexts)