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.

expressions.py 6.5KB

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