forked from fabioz/PyDev.Debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydev_run_in_console.py
81 lines (56 loc) · 2.37 KB
/
pydev_run_in_console.py
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
'''
Entry point module to run a file in the interactive console.
'''
import os
import sys
from pydevconsole import do_exit, InterpreterInterface, process_exec_queue, start_console_server
from _pydev_bundle.pydev_console_utils import BaseStdIn
from _pydev_imps._pydev_saved_modules import threading
from _pydev_bundle import pydev_imports
from _pydevd_bundle.pydevd_utils import save_main_module
from _pydev_bundle.pydev_console_utils import StdIn
def run_file(file, globals=None, locals=None):
if os.path.isdir(file):
new_target = os.path.join(file, '__main__.py')
if os.path.isfile(new_target):
file = new_target
if globals is None:
m = save_main_module(file, 'pydev_run_in_console')
globals = m.__dict__
try:
globals['__builtins__'] = __builtins__
except NameError:
pass # Not there on Jython...
if locals is None:
locals = globals
sys.path.insert(0, os.path.split(file)[0])
print('Running %s'%file)
pydev_imports.execfile(file, globals, locals) # execute the script
return globals
#=======================================================================================================================
# main
#=======================================================================================================================
if __name__ == '__main__':
port, client_port = sys.argv[1:3]
del sys.argv[1]
del sys.argv[1]
file = sys.argv[1]
del sys.argv[0]
from _pydev_bundle import pydev_localhost
if int(port) == 0 and int(client_port) == 0:
(h, p) = pydev_localhost.get_socket_name()
client_port = p
host = pydev_localhost.get_localhost()
#replace exit (see comments on method)
#note that this does not work in jython!!! (sys method can't be replaced).
sys.exit = do_exit
interpreter = InterpreterInterface(host, int(client_port), threading.currentThread())
server_thread = threading.Thread(target=start_console_server,
name='ServerThread',
args=(host, int(port), interpreter))
server_thread.setDaemon(True)
server_thread.start()
sys.stdin = StdIn(interpreter, host, client_port, sys.stdin)
globals = run_file(file, None, None)
interpreter.get_namespace().update(globals)
process_exec_queue(interpreter)