home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32comext_axdebug_stackframe.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  5.0 KB  |  148 lines

  1. """Support for stack-frames.
  2.  
  3. Provides Implements a nearly complete wrapper for a stack frame.
  4. """
  5. import sys
  6. from util import _wrap, RaiseNotImpl
  7. import expressions, gateways, axdebug, winerror
  8. import pythoncom
  9. from win32com.server.exception import COMException
  10. import repr, string
  11.  
  12. from util import trace
  13. #def trace(*args):
  14. #    pass
  15.  
  16. class EnumDebugStackFrames(gateways.EnumDebugStackFrames):
  17.     """A class that given a debugger object, can return an enumerator
  18.     of DebugStackFrame objects.
  19.     """
  20.     def __init__(self, debugger):
  21.         infos = []
  22.         frame = debugger.currentframe
  23. #        print "Stack check"        
  24.         while frame:
  25. #            print " Checking frame", frame.f_code.co_filename, frame.f_lineno-1, frame.f_trace,
  26.             # Get a DebugCodeContext for the stack frame.  If we fail, then it
  27.             # is not debuggable, and therefore not worth displaying.
  28.             cc = debugger.codeContainerProvider.FromFileName(frame.f_code.co_filename)
  29.             if cc is not None:
  30.                 try:
  31.                     address = frame.f_locals['__axstack_address__']
  32.                 except KeyError:
  33. #                    print "Couldnt find stack address for",frame.f_code.co_filename, frame.f_lineno-1
  34.                     # Use this one, even tho it is wrong :-(
  35.                     address = axdebug.GetStackAddress()
  36.                 frameInfo = DebugStackFrame(frame, frame.f_lineno-1, cc), address, address+1, 0, None
  37.                 infos.append(frameInfo)
  38. #                print "- Kept!"
  39. #            else:
  40. #                print "- rejected"
  41.             frame = frame.f_back
  42.  
  43.         gateways.EnumDebugStackFrames.__init__(self, infos, 0)
  44. #    def __del__(self):
  45. #        print "EnumDebugStackFrames dieing"
  46.  
  47.     def Next(self, count):
  48.         return gateways.EnumDebugStackFrames.Next(self, count)
  49.     
  50. #    def _query_interface_(self, iid):
  51. #        from win32com.util import IIDToInterfaceName
  52. #        print "EnumDebugStackFrames QI with %s (%s)" % (IIDToInterfaceName(iid), str(iid))
  53. #        return 0
  54.     def _wrap(self, obj):
  55.         # This enum returns a tuple, with 2 com objects in it.
  56.         obFrame, min, lim, fFinal, obFinal = obj
  57.         obFrame = _wrap(obFrame, axdebug.IID_IDebugStackFrame)
  58.         if obFinal:
  59.             obFinal = _wrap(obFinal, pythoncom.IID_IUnknown)
  60.         return obFrame, min, lim, fFinal, obFinal
  61.  
  62. class DebugStackFrame(gateways.DebugStackFrame):
  63.     def __init__(self, frame, lineno, codeContainer):
  64.         self.frame = frame
  65.         self.lineno = lineno
  66.         self.codeContainer = codeContainer
  67.         self.expressionContext = None
  68. #    def __del__(self):
  69. #        print "DSF dieing"
  70.     def _query_interface_(self, iid):
  71.         if iid==axdebug.IID_IDebugExpressionContext:
  72.             if self.expressionContext is None:
  73.                 self.expressionContext = _wrap(expressions.ExpressionContext(self.frame), axdebug.IID_IDebugExpressionContext)
  74.             return self.expressionContext
  75. #        from win32com.util import IIDToInterfaceName
  76. #        print "DebugStackFrame QI with %s (%s)" % (IIDToInterfaceName(iid), str(iid))
  77.         return 0
  78.     #
  79.     # The following need implementation
  80.     def GetThread(self):
  81.         """ Returns the thread associated with this stack frame.
  82.  
  83.         Result must be a IDebugApplicationThread
  84.         """
  85.         RaiseNotImpl("GetThread")
  86.  
  87.     def GetCodeContext(self):
  88.         offset = self.codeContainer.GetPositionOfLine(self.lineno)
  89.         return self.codeContainer.GetCodeContextAtPosition(offset)
  90.     #
  91.     # The following are usefully implemented
  92.     def GetDescriptionString(self, fLong):
  93.         filename = self.frame.f_code.co_filename
  94.         s = ""
  95.         if 0: #fLong:
  96.             s = s + filename
  97.         if self.frame.f_code.co_name:
  98.             s = s + self.frame.f_code.co_name
  99.         else:
  100.             s = s + "<lambda>"
  101.         return s
  102.     def GetLanguageString(self, fLong):
  103.         if fLong:
  104.             return "Python ActiveX Scripting Engine"
  105.         else:
  106.             return "Python"
  107.     def GetDebugProperty(self):
  108.         return _wrap(StackFrameDebugProperty(self.frame), axdebug.IID_IDebugProperty)
  109.  
  110. class DebugStackFrameSniffer:
  111.     _public_methods_ = ["EnumStackFrames"]
  112.     _com_interfaces_ = [axdebug.IID_IDebugStackFrameSniffer]
  113.     def __init__(self, debugger):
  114.         self.debugger = debugger
  115.         trace("DebugStackFrameSniffer instantiated")
  116. #    def __del__(self):
  117. #        print "DSFS dieing"
  118.     def EnumStackFrames(self):
  119.         trace("DebugStackFrameSniffer.EnumStackFrames called")
  120.         return _wrap(EnumDebugStackFrames(self.debugger), axdebug.IID_IEnumDebugStackFrames)
  121.  
  122. # A DebugProperty for a stack frame.
  123. class StackFrameDebugProperty:
  124.     _com_interfaces_ = [axdebug.IID_IDebugProperty]
  125.     _public_methods_ = ['GetPropertyInfo', 'GetExtendedInfo', 'SetValueAsString', 
  126.                         'EnumMembers', 'GetParent'
  127.     ]
  128.     def __init__(self, frame):
  129.         self.frame = frame
  130.         
  131.     def GetPropertyInfo(self, dwFieldSpec, nRadix):
  132.         RaiseNotImpl("StackFrameDebugProperty::GetPropertyInfo")
  133.     def GetExtendedInfo(self): ### Note - not in the framework.
  134.         RaiseNotImpl("StackFrameDebugProperty::GetExtendedInfo")
  135.  
  136.     def SetValueAsString(self, value, radix):
  137.         #
  138.         RaiseNotImpl("DebugProperty::SetValueAsString")
  139.         
  140.     def EnumMembers(self, dwFieldSpec, nRadix, iid):
  141.         print "EnumMembers", dwFieldSpec, nRadix, iid
  142.         import expressions
  143.         return expressions.MakeEnumDebugProperty(self.frame.f_locals, dwFieldSpec, nRadix, iid)
  144.  
  145.     def GetParent(self):
  146.         # return IDebugProperty
  147.         RaiseNotImpl("DebugProperty::GetParent")
  148.