forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.py
More file actions
192 lines (150 loc) · 5.57 KB
/
host.py
File metadata and controls
192 lines (150 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import traceback
import pythoncom
import win32api
import win32com.server.util
import winerror
from win32com.axdebug import adb, axdebug, codecontainer, contexts, documents, gateways
from win32com.axdebug.util import _wrap, _wrap_remove, trace
from win32com.axscript import axscript
from win32com.client.util import Enumerator
from win32com.server.exception import Exception
class ExternalConnection:
_public_methods_ = ["AddConnection", "ReleaseConnection"]
_com_interfaces_ = [pythoncom.IID_IExternalConnection]
numExtRefs = 0
def AddConnection(self, extconn, reserved):
self.numExtRefs = self.numExtRefs + 1
return self.numExtRefs
def ReleaseConnection(self, extconn, reserved, fLastReleaseCloses):
self.numExtRefs = self.numExtRefs - 1
return self.numExtRefs
# externalConnectionManager = ExternalConnection()
# wrappedExternalConnectionManager = _wrap(externalConnectionManager, pythoncom.IID_IExternalConnection)
def DelegatedExternalConnectionQI(iid):
# PyIExternalConnection (do I need this? Keep getting QI'd for it, anyway?)
if iid == pythoncom.IID_IExternalConnection:
return wrappedExternalConnectionManager
return 0
class PySourceModuleDebugDocumentHost(gateways.DebugDocumentHost):
"""A DebugDocumentHost that works with Python source files."""
def __init__(self, module):
self.module = module
gateways.DebugDocumentHost.__init__(self)
self.codeContainer = None
def _query_interface_(self, iid):
from win32com.util import IIDToInterfaceName
trace(
"PySourceModuleDebugDocumentHost QI with %s (%s)"
% (IIDToInterfaceName(iid), str(iid))
)
return 0
def _GetCodeContainer(self):
if self.codeContainer is None:
try:
codeText = open(self.module.__file__).read()
except OSError as details:
codeText = "# Exception opening file\n# %s" % (details)
self.codeContainer = codecontainer.SourceCodeContainer(
codeText, self.module.__file__
)
return self.codeContainer
def GetDeferredText(self, dwTextStartCookie, maxChars, bWantAttr):
try:
trace("GetDeferredText", dwTextStartCookie, maxChars, bWantAttr)
cont = self._GetCodeContainer()
if bWantAttr:
attr = cont.GetSyntaxColorAttributes()
else:
attr = None
return cont.text, attr
except:
traceback.print_exc()
def GetScriptTextAttributes(self, codeText, delimterText, flags):
# Result must be an attribute sequence of same "length" as the code.
trace("GetScriptTextAttributes", delimterText, flags)
raise Exception(scode=winerror.E_NOTIMPL)
def OnCreateDocumentContext(self):
# Result must be a PyIUnknown
trace("OnCreateDocumentContext")
raise Exception(scode=winerror.E_NOTIMPL)
def GetPathName(self):
# Result must be (string, int) where the int is a BOOL
# - TRUE if the path refers to the original file for the document.
# - FALSE if the path refers to a newly created temporary file.
# - raise Exception(scode=E_FAIL) if no source file can be created/determined.
trace("GetPathName")
try:
return win32api.GetFullPathName(self.module.__file__), 1
except (AttributeError, win32api.error):
raise Exception(scode == E_FAIL)
def GetFileName(self):
# Result is a string with just the name of the document, no path information.
trace("GetFileName")
return os.path.split(module.__file__)
def NotifyChanged():
trace("NotifyChanged")
raise Exception(scode=winerror.E_NOTIMPL)
def TestSmartHelper():
pdm = pythoncom.CoCreateInstance(
axdebug.CLSID_ProcessDebugManager,
None,
pythoncom.CLSCTX_ALL,
axdebug.IID_IProcessDebugManager,
)
app = pdm.CreateApplication()
app.SetName("Python Process")
pydebugger = adb.Debugger()
nodes = BuildModuleTree()
all_real_nodes = CreateDebugDocumentHelperNodes(pdm, app, nodes)
root = app.GetRootNode()
AttachParentNodes(root, nodes, all_real_nodes)
pydebugger.AttachApp(app)
cookie = pdm.AddApplication(app)
input("Waiting...")
ttest.test()
pdm.RemoveApplication(cookie)
print("Done")
def testdumb():
pdm = pythoncom.CoCreateInstance(
axdebug.CLSID_ProcessDebugManager,
None,
pythoncom.CLSCTX_ALL,
axdebug.IID_IProcessDebugManager,
)
app = pdm.GetDefaultApplication()
nodes = BuildModuleTree()
all_real_nodes = CreateDebugDocumentHelperNodes(pdm, app, nodes)
AttachParentNodes(None, nodes, all_real_nodes)
parentNode = None
all_real_nodes = {}
input("Waiting...")
print("Done")
def TestSmartProvider():
import ttest
from win32com.axdebug import debugger
d = debugger.AXDebugger()
# d.StartDebugger()
# d.Attach()
d.Break()
input("Waiting...")
ttest.test()
d.Close()
print("Done")
def test():
try:
# app = TestSmartHelper()
app = TestSmartProvider()
# app = testdumb()
except:
traceback.print_exc()
# _wrap_remove(externalConnectionManager)
# wrappedExternalConnectionManager = None
if __name__ == "__main__":
test()
import win32com.axdebug.util
win32com.axdebug.util._dump_wrapped()
print(
" %d/%d com objects still alive"
% (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
)