|
@@ -1,54 +1,50 @@
|
1
|
1
|
#########################################################################
|
2
|
|
-""" wingdbstub.py -- Debug stub for debuggifying Python programs
|
|
2
|
+""" wingdbstub.py -- Start debugging Python programs from outside of Wing
|
3
|
3
|
|
4
|
|
-Copyright (c) 1999-2001, Archaeopteryx Software, Inc. All rights reserved.
|
|
4
|
+Copyright (c) 1999-2018, Archaeopteryx Software, Inc. All rights reserved.
|
5
|
5
|
|
6
|
6
|
Written by Stephan R.A. Deibel and John P. Ehresman
|
7
|
7
|
|
8
|
8
|
Usage:
|
9
|
9
|
-----
|
10
|
10
|
|
11
|
|
-This is the file that Wing DB users copy into their python project
|
12
|
|
-directory if they want to be able to debug programs that are launched
|
13
|
|
-outside of the IDE (e.g., CGI scripts, in response to a browser page
|
14
|
|
-load).
|
15
|
|
-
|
16
|
|
-To use this, edit the configuration values below to match your
|
17
|
|
-Wing installation and requirements of your project.
|
18
|
|
-
|
19
|
|
-Then, add the following line to your code:
|
|
11
|
+This file is used to initiate debug in Python code without launching
|
|
12
|
+that code from Wing. To use it, edit the configuration values below,
|
|
13
|
+start Wing and configure it to listen for outside debug connections,
|
|
14
|
+and then add the following line to your code:
|
20
|
15
|
|
21
|
16
|
import wingdbstub
|
22
|
17
|
|
23
|
|
-Debugging will start immediately after this import statements.
|
24
|
|
-
|
25
|
|
-Next make sure that your IDE is running and that it's configured to accept
|
26
|
|
-connections from the host the debug program will be running on.
|
27
|
|
-
|
28
|
|
-Now, invoking your python file should run the code within the debugger.
|
29
|
|
-Note, however, that Wing will not stop in the code unless a breakpoint
|
30
|
|
-is set.
|
31
|
|
-
|
32
|
|
-If the debug process is started before the IDE, or is not listening
|
33
|
|
-at the time this module is imported then the program will run with
|
34
|
|
-debugging until an attach request is seen. Attaching only works
|
35
|
|
-if the .wingdebugpw file is present; see the manual for details.
|
|
18
|
+Debugging will start immediately after this import statement is reached.
|
36
|
19
|
|
37
|
|
-On win32, you either need to edit WINGHOME in this script or
|
38
|
|
-pass in an environment variable called WINGHOME that points to
|
39
|
|
-the Wing installation directory.
|
|
20
|
+For details, see Debugging Externally Launched Code in the manual.
|
40
|
21
|
|
41
|
22
|
"""
|
42
|
23
|
#########################################################################
|
43
|
24
|
|
44
|
25
|
import sys
|
|
26
|
+orig_sys_modules = list(sys.modules.keys())
|
|
27
|
+orig_sys_path = list(sys.path)
|
|
28
|
+orig_meta_path = list(sys.meta_path)
|
|
29
|
+
|
45
|
30
|
import os
|
46
|
|
-import imp
|
|
31
|
+if sys.version_info >= (3, 7):
|
|
32
|
+ import importlib
|
|
33
|
+else:
|
|
34
|
+ import imp
|
|
35
|
+
|
|
36
|
+#------------------------------------------------------------------------
|
|
37
|
+# Required configuration values (some installers set this automatically)
|
47
|
38
|
|
|
39
|
+# This should be the full path to your Wing installation. On OS X, use
|
|
40
|
+# the full path of the Wing application bundle, for example
|
|
41
|
+# /Applications/WingPro.app. When set to None, the environment variable
|
|
42
|
+# WINGHOME is used instead.
|
|
43
|
+WINGHOME = r"C:\Program Files (x86)\Wing Pro 7.0"
|
48
|
44
|
|
49
|
45
|
#------------------------------------------------------------------------
|
50
|
|
-# Default configuration values: Note that the named environment
|
51
|
|
-# variables, if set, will override these settings.
|
|
46
|
+# Optional configuration values: The named environment variables, if set,
|
|
47
|
+# will override these settings.
|
52
|
48
|
|
53
|
49
|
# Set this to 1 to disable all debugging; 0 to enable debugging
|
54
|
50
|
# (WINGDB_DISABLED environment variable)
|
|
@@ -60,30 +56,33 @@ kWingDebugDisabled = 0
|
60
|
56
|
kWingHostPort = 'localhost:50005'
|
61
|
57
|
|
62
|
58
|
# Port on which to listen for connection requests, so that the
|
63
|
|
-# IDE can (re)attach to the debug process after it has started.
|
|
59
|
+# IDE can attach or reattach to the debug process after it has started.
|
64
|
60
|
# Set this to '-1' to disable listening for connection requests.
|
65
|
61
|
# This is only used when the debug process is not attached to
|
66
|
62
|
# an IDE or the IDE has dropped its connection. The configured
|
67
|
63
|
# port can optionally be added to the IDE's Common Attach Hosts
|
68
|
|
-# preference. Note that a random port is used instead if this
|
69
|
|
-# port is already in use!
|
|
64
|
+# preference. Note that a random port is used instead if the given
|
|
65
|
+# port is already in use.
|
70
|
66
|
# (WINGDB_ATTACHPORT environment variable)
|
71
|
67
|
kAttachPort = '50015'
|
72
|
68
|
|
73
|
69
|
# Set this to a filename to log verbose information about the debugger
|
74
|
70
|
# internals to a file. If the file does not exist, it will be created
|
75
|
71
|
# as long as its enclosing directory exists and is writeable. Use
|
76
|
|
-# "<stderr>" or "<stdout>". Note that "<stderr>" may cause problems
|
77
|
|
-# on win32 if the debug process is not running in a console.
|
|
72
|
+# "<stderr>" or "<stdout>" to write to stderr or stdout. Note that
|
|
73
|
+# "<stderr>" may cause problems on Windows if the debug process is not
|
|
74
|
+# running in a console.
|
78
|
75
|
# (WINGDB_LOGFILE environment variable)
|
79
|
76
|
kLogFile = None
|
80
|
77
|
|
81
|
|
-# Set to get a tremendous amount of logging from the debugger internals
|
82
|
|
-# (WINGDB_LOGVERYVERBOSE)
|
|
78
|
+# Produce a tremendous amount of logging from the debugger internals.
|
|
79
|
+# Do not set this unless instructed to do so by Wingware support. It
|
|
80
|
+# will slow the debugger to a crawl.
|
|
81
|
+# (WINGDB_LOGVERYVERBOSE environment variable)
|
83
|
82
|
kLogVeryVerbose = 0
|
84
|
83
|
|
85
|
84
|
# Set this to 1 when debugging embedded scripts in an environment that
|
86
|
|
-# creates and reuses a Python instance across multiple script invocations:
|
|
85
|
+# creates and reuses a Python instance across multiple script invocations.
|
87
|
86
|
# It turns off automatic detection of program quit so that the debug session
|
88
|
87
|
# can span multiple script invocations. When this is turned on, you may
|
89
|
88
|
# need to call ProgramQuit() on the debugger object to shut down the
|
|
@@ -92,58 +91,44 @@ kLogVeryVerbose = 0
|
92
|
91
|
# only the first one will be able to debug unless it terminates debug
|
93
|
92
|
# and the environment variable WINGDB_ACTIVE is unset before importing
|
94
|
93
|
# this module in the second or later Python instance. See the Wing
|
95
|
|
-# IDE manual for details.
|
|
94
|
+# manual for details.
|
|
95
|
+# (WINGDB_EMBEDDED environment variable)
|
96
|
96
|
kEmbedded = 0
|
97
|
97
|
|
98
|
98
|
# Path to search for the debug password file and the name of the file
|
99
|
|
-# to use. The password file contains the encryption type and connect
|
100
|
|
-# password for all connections to the IDE and must match the wingdebugpw
|
101
|
|
-# file in the profile dir used by the IDE. Any entry of '$<winguserprofile>'
|
102
|
|
-# is replaced by the wing user profile directory for the user that the
|
103
|
|
-# current process is running as
|
|
99
|
+# to use. The password file contains a security token for all
|
|
100
|
+# connections to the IDE and must match the wingdebugpw file in the
|
|
101
|
+# User Settngs directory used by the IDE. '$<winguserprofile>'
|
|
102
|
+# is replaced by the User Settings directory for the user that
|
|
103
|
+# is running the process.
|
104
|
104
|
# (WINGDB_PWFILEPATH environment variable)
|
105
|
105
|
kPWFilePath = [os.path.dirname(__file__), '$<winguserprofile>']
|
106
|
106
|
kPWFileName = 'wingdebugpw'
|
107
|
107
|
|
108
|
|
-# Whether to exit if the debugger fails to run or to connect with an IDE
|
109
|
|
-# for whatever reason
|
|
108
|
+# Whether to exit when the debugger fails to run or to connect with the IDE
|
|
109
|
+# By default, execution continues without debug or without connecting to
|
|
110
|
+# the IDE.
|
|
111
|
+# (WINGDB_EXITONFAILURE environment variable)
|
110
|
112
|
kExitOnFailure = 0
|
111
|
113
|
|
112
|
114
|
#------------------------------------------------------------------------
|
113
|
115
|
# Find Wing debugger installation location
|
114
|
116
|
|
115
|
|
-# Edit this to point to your Wing installation or set to None to use env WINGHOME
|
116
|
|
-# On OS X this must be set to name of the Wing application bundle
|
117
|
|
-# (for example, /Applications/WingIDE.app)
|
118
|
|
-WINGHOME = None
|
119
|
|
-
|
120
|
|
-if sys.hexversion >= 0x03000000:
|
121
|
|
- def has_key(o, key):
|
122
|
|
- return key in o
|
123
|
|
-else:
|
124
|
|
- def has_key(o, key):
|
125
|
|
- return o.has_key(key)
|
126
|
|
-
|
127
|
117
|
# Check environment: Must have WINGHOME defined if still == None
|
128
|
118
|
if WINGHOME == None:
|
129
|
|
- if has_key(os.environ, 'WINGHOME'):
|
|
119
|
+ if 'WINGHOME' in os.environ:
|
130
|
120
|
WINGHOME=os.environ['WINGHOME']
|
131
|
121
|
else:
|
132
|
|
- sys.stdout.write("*******************************************************************\n")
|
133
|
|
- sys.stdout.write("Error: Could not find Wing installation! You must set WINGHOME or edit\n")
|
134
|
|
- sys.stdout.write("wingdbstub.py where indicated to point it to the location where\n")
|
135
|
|
- sys.stdout.write("Wing is installed.\n")
|
136
|
|
- sys.exit(1)
|
137
|
|
-
|
138
|
|
-kPWFilePath.append(WINGHOME)
|
139
|
|
-
|
140
|
|
-# The user settings dir where per-user settings & patches are located. Will be
|
141
|
|
-# set from environment if left as None
|
142
|
|
-kUserSettingsDir = None
|
143
|
|
-if kUserSettingsDir is None:
|
144
|
|
- kUserSettingsDir = os.environ.get('WINGDB_USERSETTINGS')
|
|
122
|
+ msg = '\n'.join(["*******************************************************************",
|
|
123
|
+ "Error: Could not find Wing installation! You must set WINGHOME or edit",
|
|
124
|
+ "wingdbstub.py where indicated to point it to the location where",
|
|
125
|
+ "Wing is installed.\n"])
|
|
126
|
+ sys.stderr.write(msg)
|
|
127
|
+ raise ImportError(msg)
|
145
|
128
|
|
146
|
|
-def _FindActualWingHome(winghome):
|
|
129
|
+WINGHOME = os.path.expanduser(WINGHOME)
|
|
130
|
+
|
|
131
|
+def NP_FindActualWingHome(winghome):
|
147
|
132
|
""" Find the actual directory to use for winghome. Needed on OS X
|
148
|
133
|
where the .app directory is the preferred dir to use for WINGHOME and
|
149
|
134
|
.app/Contents/MacOS is accepted for backward compatibility. """
|
|
@@ -168,28 +153,38 @@ def _FindActualWingHome(winghome):
|
168
|
153
|
|
169
|
154
|
return winghome
|
170
|
155
|
|
171
|
|
-def _ImportWingdb(winghome, user_settings=None):
|
172
|
|
- """ Find & import wingdb module. """
|
|
156
|
+WINGHOME = NP_FindActualWingHome(WINGHOME)
|
|
157
|
+os.environ['WINGHOME'] = WINGHOME
|
|
158
|
+
|
|
159
|
+# Path used to find the wingdebugpw file
|
|
160
|
+kPWFilePath.append(WINGHOME)
|
|
161
|
+
|
|
162
|
+#-----------------------------------------------------------------------
|
|
163
|
+def NP_LoadModuleFromBootstrap(winghome, modname):
|
|
164
|
+ """Load a module from the installation bootstrap directory. Assumes that
|
|
165
|
+ imports don't change sys.path. The modules are unloaded from sys.modules
|
|
166
|
+ so they can be loaded again later from an update."""
|
173
|
167
|
|
174
|
|
- try:
|
175
|
|
- exec_dict = {}
|
176
|
|
- execfile(os.path.join(winghome, 'bin', '_patchsupport.py'), exec_dict)
|
177
|
|
- find_matching = exec_dict['FindMatching']
|
178
|
|
- dir_list = find_matching('bin', winghome, user_settings)
|
179
|
|
- except Exception:
|
180
|
|
- dir_list = []
|
181
|
|
- dir_list.extend([os.path.join(winghome, 'bin'), os.path.join(winghome, 'src')])
|
182
|
|
- for path in dir_list:
|
183
|
|
- try:
|
184
|
|
- f, p, d = imp.find_module('wingdb', [path])
|
185
|
|
- try:
|
186
|
|
- return imp.load_module('wingdb', f, p, d)
|
187
|
|
- finally:
|
188
|
|
- if f is not None:
|
189
|
|
- f.close()
|
190
|
|
- break
|
191
|
|
- except ImportError:
|
192
|
|
- pass
|
|
168
|
+ # Limited to simple module loads
|
|
169
|
+ assert '.' not in modname
|
|
170
|
+
|
|
171
|
+ orig_sys_path = sys.path[:]
|
|
172
|
+ orig_modules = set(sys.modules)
|
|
173
|
+
|
|
174
|
+ dirname = winghome + '/bootstrap'
|
|
175
|
+ sys.path.insert(0, dirname)
|
|
176
|
+
|
|
177
|
+ code = 'import %s' % modname
|
|
178
|
+ exec(code)
|
|
179
|
+
|
|
180
|
+ new_modules = set(sys.modules)
|
|
181
|
+ new_modules.difference_update(orig_modules)
|
|
182
|
+ for mod in new_modules:
|
|
183
|
+ del sys.modules[mod]
|
|
184
|
+
|
|
185
|
+ sys.path = orig_sys_path
|
|
186
|
+
|
|
187
|
+ return locals()[modname]
|
193
|
188
|
|
194
|
189
|
#------------------------------------------------------------------------
|
195
|
190
|
# Set debugger if it hasn't been set -- this is to handle module reloading
|
|
@@ -199,16 +194,18 @@ try:
|
199
|
194
|
except NameError:
|
200
|
195
|
debugger = None
|
201
|
196
|
|
|
197
|
+#-----------------------------------------------------------------------
|
202
|
198
|
# Unset WINGDB_ACTIVE env if it was inherited from another process
|
203
|
199
|
# XXX Would be better to be able to call getpid() on dbgtracer but can't access it yet
|
204
|
200
|
if 'WINGDB_ACTIVE' in os.environ and os.environ['WINGDB_ACTIVE'] != str(os.getpid()):
|
205
|
201
|
del os.environ['WINGDB_ACTIVE']
|
206
|
202
|
|
|
203
|
+#-----------------------------------------------------------------------
|
207
|
204
|
# Start debugging if not disabled and this module has never been imported
|
208
|
205
|
# before
|
209
|
|
-if (not kWingDebugDisabled and debugger is None
|
210
|
|
- and not has_key(os.environ, 'WINGDB_DISABLED') and
|
211
|
|
- not has_key(os.environ, 'WINGDB_ACTIVE')):
|
|
206
|
+if (not kWingDebugDisabled and debugger is None and
|
|
207
|
+ 'WINGDB_DISABLED' not in os.environ and
|
|
208
|
+ 'WINGDB_ACTIVE' not in os.environ):
|
212
|
209
|
|
213
|
210
|
exit_on_fail = 0
|
214
|
211
|
|
|
@@ -238,35 +235,54 @@ if (not kWingDebugDisabled and debugger is None
|
238
|
235
|
embedded = int(os.environ.get('WINGDB_EMBEDDED', kEmbedded))
|
239
|
236
|
|
240
|
237
|
# Obtain debug password file search path
|
241
|
|
- if has_key(os.environ, 'WINGDB_PWFILEPATH'):
|
|
238
|
+ if 'WINGDB_PWFILEPATH' in os.environ:
|
242
|
239
|
pwfile_path = os.environ['WINGDB_PWFILEPATH'].split(os.pathsep)
|
243
|
240
|
else:
|
244
|
241
|
pwfile_path = kPWFilePath
|
245
|
242
|
|
246
|
243
|
# Obtain debug password file name
|
247
|
|
- if has_key(os.environ, 'WINGDB_PWFILENAME'):
|
|
244
|
+ if 'WINGDB_PWFILENAME' in os.environ:
|
248
|
245
|
pwfile_name = os.environ['WINGDB_PWFILENAME']
|
249
|
246
|
else:
|
250
|
247
|
pwfile_name = kPWFileName
|
251
|
248
|
|
252
|
|
- # Load wingdb.py
|
253
|
|
- actual_winghome = _FindActualWingHome(WINGHOME)
|
254
|
|
- wingdb = _ImportWingdb(actual_winghome, kUserSettingsDir)
|
255
|
|
- if wingdb == None:
|
256
|
|
- sys.stdout.write("*******************************************************************\n")
|
257
|
|
- sys.stdout.write("Error: Cannot find wingdb.py in $(WINGHOME)/bin or $(WINGHOME)/src\n")
|
258
|
|
- sys.stdout.write("Error: Please check the WINGHOME definition in wingdbstub.py\n")
|
259
|
|
- sys.exit(2)
|
|
249
|
+ # Set up temporary log for errors from merge importer Setup
|
|
250
|
+ class CTmpLog:
|
|
251
|
+ def __init__(self):
|
|
252
|
+ self.fErrors = []
|
|
253
|
+ def write(self, s):
|
|
254
|
+ self.fErrors.append(s)
|
|
255
|
+ tmp_log = CTmpLog()
|
|
256
|
+
|
|
257
|
+ # Set up the meta importer; everything after this point can be update
|
|
258
|
+ bootstrap_utils = NP_LoadModuleFromBootstrap(WINGHOME, 'bootstrap_utils')
|
|
259
|
+ winghome, user_settings = bootstrap_utils.NP_SetupWingHomeModule(WINGHOME)
|
|
260
|
+ meta = bootstrap_utils.NP_CreateMetaImporter(WINGHOME, user_settings, 'dbg',
|
|
261
|
+ tmp_log)
|
|
262
|
+ import _winghome
|
|
263
|
+ _winghome.kWingHome = winghome
|
|
264
|
+ _winghome.kUserSettings = user_settings
|
260
|
265
|
|
261
|
266
|
# Find the netserver module and create an error stream
|
262
|
|
- netserver = wingdb.FindNetServerModule(actual_winghome, kUserSettingsDir)
|
263
|
|
- err = wingdb.CreateErrStream(netserver, logfile, very_verbose_log)
|
|
267
|
+ from debug.tserver import startdebug
|
|
268
|
+ netserver = startdebug.FindNetServerModule(WINGHOME, user_settings)
|
|
269
|
+ err = startdebug.CreateErrStream(netserver, logfile, very_verbose_log)
|
264
|
270
|
|
265
|
|
- # Start debugging
|
|
271
|
+ # Write any temporary log entries
|
|
272
|
+ for s in tmp_log.fErrors:
|
|
273
|
+ err.write(s)
|
|
274
|
+
|
|
275
|
+ # Create debug server
|
266
|
276
|
debugger = netserver.CNetworkServer(host, port, attachport, err,
|
267
|
277
|
pwfile_path=pwfile_path,
|
268
|
278
|
pwfile_name=pwfile_name,
|
269
|
279
|
autoquit=not embedded)
|
|
280
|
+
|
|
281
|
+ # Restore module and path environment
|
|
282
|
+ from debug.tserver import startdebug
|
|
283
|
+ startdebug.RestoreEnvironment(orig_sys_modules, orig_sys_path, orig_meta_path)
|
|
284
|
+
|
|
285
|
+ # Start debugging
|
270
|
286
|
debugger.StartDebug(stophere=0)
|
271
|
287
|
os.environ['WINGDB_ACTIVE'] = str(os.getpid())
|
272
|
288
|
if debugger.ChannelClosed():
|
|
@@ -278,6 +294,7 @@ if (not kWingDebugDisabled and debugger is None
|
278
|
294
|
else:
|
279
|
295
|
pass
|
280
|
296
|
|
|
297
|
+#-----------------------------------------------------------------------
|
281
|
298
|
def Ensure(require_connection=1, require_debugger=1):
|
282
|
299
|
""" Ensure the debugger is started and attempt to connect to the IDE if
|
283
|
300
|
not already connected. Will raise a ValueError if:
|