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_expressions.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  4.6 KB  |  142 lines

  1. import axdebug, gateways
  2. from util import _wrap, _wrap_remove, RaiseNotImpl
  3. import cStringIO, traceback
  4. from pprint import pprint
  5. from win32com.server.exception import COMException
  6. import winerror
  7. import string
  8. import sys
  9.  
  10. # Given an object, return a nice string
  11. def MakeNiceString(ob):
  12.     stream = cStringIO.StringIO()
  13.     pprint(ob, stream)
  14.     return string.strip(stream.getvalue())
  15.  
  16. class ProvideExpressionContexts(gateways.ProvideExpressionContexts):
  17.     pass
  18.  
  19. class ExpressionContext(gateways.DebugExpressionContext):
  20.     def __init__(self, frame):
  21.         self.frame = frame
  22.     def ParseLanguageText(self, code, radix, delim, flags):
  23.         return _wrap(Expression(self.frame, code, radix, delim, flags), axdebug.IID_IDebugExpression)
  24.     def GetLanguageInfo(self):
  25. #        print "GetLanguageInfo"
  26.         return "Python", "{DF630910-1C1D-11d0-AE36-8C0F5E000000}"
  27.     
  28. class Expression(gateways.DebugExpression):
  29.     def __init__(self, frame, code, radix, delim, flags):
  30.         self.callback = None
  31.         self.frame = frame
  32.         self.code = str(code)
  33.         self.radix = radix
  34.         self.delim = delim
  35.         self.flags = flags
  36.         self.isComplete = 0
  37.         self.result=None
  38.         self.hresult = winerror.E_UNEXPECTED
  39.     def Start(self, callback):
  40.         try:
  41.             try:
  42.                 try:
  43.                     self.result = eval(self.code, self.frame.f_globals, self.frame.f_locals)
  44.                 except SyntaxError:
  45.                     exec self.code in self.frame.f_globals, self.frame.f_locals
  46.                     self.result = ""
  47.                 self.hresult = 0
  48.             except:
  49.                 l = traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])
  50.                 # l is a list of strings with trailing "\n"
  51.                 self.result = string.join(map(lambda s:s[:-1], l), "\n")
  52.                 self.hresult = winerror.E_FAIL
  53.         finally:
  54.             self.isComplete = 1
  55.             callback.onComplete()
  56.     def Abort(self):
  57.         print "** ABORT **"
  58.         
  59.     def QueryIsComplete(self):
  60.         return self.isComplete
  61.         
  62.     def GetResultAsString(self):
  63. #        print "GetStrAsResult returning", self.result
  64.         return self.hresult, MakeNiceString(self.result)
  65.     
  66.     def GetResultAsDebugProperty(self):
  67.         result = _wrap(DebugProperty(self.code, self.result, None, self.hresult), axdebug.IID_IDebugProperty)
  68.         return self.hresult, result
  69.  
  70. def MakeEnumDebugProperty(object, dwFieldSpec, nRadix, iid):
  71.     name_vals = []
  72.     if hasattr(object, "has_key"): # If it is a dict.
  73.         name_vals = object.items()
  74.     infos = []
  75.     for name, val in name_vals:
  76.         infos.append(GetPropertyInfo(name, val, dwFieldSpec, nRadix, 0))
  77.     return _wrap(EnumDebugPropertyInfo(infos), axdebug.IID_IEnumDebugPropertyInfo)
  78.  
  79. def GetPropertyInfo(obname, obvalue, dwFieldSpec, nRadix, hresult=0):
  80.     # returns a tuple
  81.     name = typ = value = fullname = None
  82.     if dwFieldSpec & axdebug.DBGPROP_INFO_VALUE:
  83.         value = MakeNiceString(obvalue)
  84.     if dwFieldSpec & axdebug.DBGPROP_INFO_NAME:
  85.         name = obname
  86.     if dwFieldSpec & axdebug.DBGPROP_INFO_TYPE:
  87.         if hresult:
  88.             typ = "Error"
  89.         else:
  90.             try:
  91.                 typ = type(obvalue).__name__
  92.             except AttributeError:
  93.                 typ = str(type(obvalue))
  94.     if dwFieldSpec & axdebug.DBGPROP_INFO_FULLNAME:
  95.         fullname = obname
  96.     return name, typ, value, fullname
  97.  
  98. from win32com.server.util import ListEnumeratorGateway
  99. class EnumDebugPropertyInfo(ListEnumeratorGateway):
  100.     """A class to expose a Python sequence as an EnumDebugCodeContexts
  101.  
  102.     Create an instance of this class passing a sequence (list, tuple, or
  103.     any sequence protocol supporting object) and it will automatically
  104.     support the EnumDebugCodeContexts interface for the object.
  105.  
  106.     """
  107.     _public_methods_ = ListEnumeratorGateway._public_methods_ + ["GetCount"]
  108.     _com_interfaces_ = [ axdebug.IID_IEnumDebugPropertyInfo]
  109.     def GetCount(self):
  110.         return len(self._list_)
  111.     def _wrap(self, ob):
  112.         return ob
  113.  
  114. class DebugProperty:
  115.     _com_interfaces_ = [axdebug.IID_IDebugProperty]
  116.     _public_methods_ = ['GetPropertyInfo', 'GetExtendedInfo', 'SetValueAsString', 
  117.                         'EnumMembers', 'GetParent'
  118.     ]
  119.     def __init__(self, name, value, parent = None, hresult = 0):
  120.         self.name = name
  121.         self.value = value
  122.         self.parent = parent
  123.         self.hresult = hresult
  124.  
  125.     def GetPropertyInfo(self, dwFieldSpec, nRadix):
  126.         return GetPropertyInfo(self.name, self.value, dwFieldSpec, nRadix, hresult=self.hresult)
  127.  
  128.     def GetExtendedInfo(self): ### Note - not in the framework.
  129.         RaiseNotImpl("DebugProperty::GetExtendedInfo")
  130.  
  131.     def SetValueAsString(self, value, radix):
  132.         #
  133.         RaiseNotImpl("DebugProperty::SetValueAsString")
  134.  
  135.     def EnumMembers(self, dwFieldSpec, nRadix, iid):
  136.         # Returns IEnumDebugPropertyInfo
  137.         return MakeEnumDebugProperty(self.value, dwFieldSpec, nRadix, iid)
  138.  
  139.     def GetParent(self):
  140.         # return IDebugProperty
  141.         RaiseNotImpl("DebugProperty::GetParent")
  142.