-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpydevconsole.py
More file actions
275 lines (220 loc) · 10.3 KB
/
pydevconsole.py
File metadata and controls
275 lines (220 loc) · 10.3 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
try:
from code import InteractiveConsole
except ImportError:
from pydevconsole_code_for_ironpython import InteractiveConsole
import os
import sys
try:
False
True
except NameError: # version < 2.3 -- didn't have the True/False builtins
import __builtin__
setattr(__builtin__, 'True', 1) #Python 3.0 does not accept __builtin__.True = 1 in its syntax
setattr(__builtin__, 'False', 0)
from pydev_console_utils import BaseStdIn, StdIn, BaseInterpreterInterface
try:
class ExecState:
FIRST_CALL = True
PYDEV_CONSOLE_RUN_IN_UI = False #Defines if we should run commands in the UI thread.
from org.python.pydev.core.uiutils import RunInUiThread #@UnresolvedImport
from java.lang import Runnable #@UnresolvedImport
class Command(Runnable):
def __init__(self, interpreter, line):
self.interpreter = interpreter
self.line = line
def run(self):
if ExecState.FIRST_CALL:
ExecState.FIRST_CALL = False
sys.stdout.write('\nYou are now in a console within Eclipse.\nUse it with care as it can halt the VM.\n')
sys.stdout.write('Typing a line with "PYDEV_CONSOLE_TOGGLE_RUN_IN_UI"\nwill start executing all the commands in the UI thread.\n\n')
if self.line == 'PYDEV_CONSOLE_TOGGLE_RUN_IN_UI':
ExecState.PYDEV_CONSOLE_RUN_IN_UI = not ExecState.PYDEV_CONSOLE_RUN_IN_UI
if ExecState.PYDEV_CONSOLE_RUN_IN_UI:
sys.stdout.write('Running commands in UI mode. WARNING: using sys.stdin (i.e.: calling raw_input()) WILL HALT ECLIPSE.\n')
else:
sys.stdout.write('No longer running commands in UI mode.\n')
self.more = False
else:
self.more = self.interpreter.push(self.line)
def Sync(runnable):
if ExecState.PYDEV_CONSOLE_RUN_IN_UI:
return RunInUiThread.sync(runnable)
else:
return runnable.run()
except:
#If things are not there, define a way in which there's no 'real' sync, only the default execution.
class Command:
def __init__(self, interpreter, line):
self.interpreter = interpreter
self.line = line
def run(self):
self.more = self.interpreter.push(self.line)
def Sync(runnable):
runnable.run()
try:
try:
execfile #Not in Py3k
except NameError:
from pydev_imports import execfile
import builtins #@UnresolvedImport -- only Py3K
builtins.execfile = execfile
except:
pass
#=======================================================================================================================
# InterpreterInterface
#=======================================================================================================================
class InterpreterInterface(BaseInterpreterInterface):
'''
The methods in this class should be registered in the xml-rpc server.
'''
def __init__(self, host, client_port):
self.client_port = client_port
self.host = host
try:
import pydevd #@UnresolvedImport
if pydevd.GetGlobalDebugger() is None:
raise RuntimeError() #Work as if the debugger does not exist as it's not connected.
except:
self.namespace = globals()
else:
#Adapted from the code in pydevd
#patch provided by: Scott Schlesier - when script is run, it does not
#pretend pydevconsole is not the main module, and
#convince the file to be debugged that it was loaded as main
sys.modules['pydevconsole'] = sys.modules['__main__']
sys.modules['pydevconsole'].__name__ = 'pydevconsole'
from imp import new_module
m = new_module('__main__')
sys.modules['__main__'] = m
ns = m.__dict__
try:
ns['__builtins__'] = __builtins__
except NameError:
pass #Not there on Jython...
self.namespace = ns
self.interpreter = InteractiveConsole(self.namespace)
self._input_error_printed = False
def doAddExec(self, line):
command = Command(self.interpreter, line)
Sync(command)
return command.more
def getNamespace(self):
return self.namespace
def getCompletions(self, text, act_tok):
try:
from _pydev_completer import Completer
completer = Completer(self.namespace, None)
return completer.complete(act_tok)
except:
import traceback;traceback.print_exc()
return []
def close(self):
sys.exit(0)
try:
from pydev_ipython_console import InterpreterInterface
except:
sys.stderr.write('PyDev console: using default backend (IPython not available).\n')
pass #IPython not available, proceed as usual.
#=======================================================================================================================
# _DoExit
#=======================================================================================================================
def _DoExit(*args):
'''
We have to override the exit because calling sys.exit will only actually exit the main thread,
and as we're in a Xml-rpc server, that won't work.
'''
try:
import java.lang.System
java.lang.System.exit(1)
except ImportError:
if len(args) == 1:
os._exit(args[0])
else:
os._exit(0)
#=======================================================================================================================
# StartServer
#=======================================================================================================================
def StartServer(host, port, client_port):
#replace exit (see comments on method)
#note that this does not work in jython!!! (sys method can't be replaced).
sys.exit = _DoExit
from pydev_imports import SimpleXMLRPCServer
try:
interpreter = InterpreterInterface(host, client_port)
server = SimpleXMLRPCServer((host, port), logRequests=False)
except:
sys.stderr.write('Error starting server with host: %s, port: %s, client_port: %s\n' % (host, port, client_port))
raise
if True:
#Functions for basic protocol
server.register_function(interpreter.addExec)
server.register_function(interpreter.getCompletions)
server.register_function(interpreter.getDescription)
server.register_function(interpreter.close)
#Functions so that the console can work as a debugger (i.e.: variables view, expressions...)
server.register_function(interpreter.connectToDebugger)
server.register_function(interpreter.postCommand)
server.register_function(interpreter.hello)
server.serve_forever()
else:
#This is still not finished -- that's why the if True is there :)
from pydev_imports import Queue
queue_requests_received = Queue.Queue() #@UndefinedVariable
queue_return_computed = Queue.Queue() #@UndefinedVariable
def addExec(line):
queue_requests_received.put(('addExec', line))
return queue_return_computed.get(block=True)
def getCompletions(text):
queue_requests_received.put(('getCompletions', text))
return queue_return_computed.get(block=True)
def getDescription(text):
queue_requests_received.put(('getDescription', text))
return queue_return_computed.get(block=True)
def close():
queue_requests_received.put(('close', None))
return queue_return_computed.get(block=True)
server.register_function(addExec)
server.register_function(getCompletions)
server.register_function(getDescription)
server.register_function(close)
try:
import PyQt4.QtGui #We can only start the PyQt4 loop if we actually have access to it.
except ImportError:
print('Unable to process gui events (PyQt4.QtGui not imported)')
server.serve_forever()
else:
import threading
class PydevHandleRequestsThread(threading.Thread):
def run(self):
while 1:
#This is done on a thread (so, it may be blocking or not blocking, it doesn't matter)
#anyways, the request will be put on a queue and the return will be gotten from another
#one -- and those queues are shared with the main thread.
server.handle_request()
app = PyQt4.QtGui.QApplication([])
def serve_forever():
"""Handle one request at a time until doomsday."""
while 1:
try:
try:
func, param = queue_requests_received.get(block=True,timeout=1.0/20.0) #20 loops/second
attr = getattr(interpreter, func)
if param is not None:
queue_return_computed.put(attr(param))
else:
queue_return_computed.put(attr())
except Queue.Empty: #@UndefinedVariable
pass
PyQt4.QtGui.qApp.processEvents()
except:
import traceback;traceback.print_exc()
PydevHandleRequestsThread().start()
serve_forever()
#=======================================================================================================================
# main
#=======================================================================================================================
if __name__ == '__main__':
sys.stdin = BaseStdIn()
port, client_port = sys.argv[1:3]
import pydev_localhost
StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))