summaryrefslogtreecommitdiff
path: root/python/helpers/pydev/pydev_run_in_console.py
blob: 1b8e1d230175177bf5b294cee5eb125f54edeacf (plain)
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

from pydevconsole import *

import pydev_imports


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:
        # patch provided by: Scott Schlesier - when script is run, it does not
        # use globals from pydevd:
        # This will prevent the pydevd script from contaminating the namespace for the script to be debugged

        # pretend pydevd is not the main module, and
        # convince the file to be debugged that it was loaded as main
        sys.modules['pydevd'] = sys.modules['__main__']
        sys.modules['pydevd'].__name__ = 'pydevd'

        from imp import new_module
        m = new_module('__main__')
        sys.modules['__main__'] = m
        if hasattr(sys.modules['pydevd'], '__loader__'):
            setattr(m, '__loader__', getattr(sys.modules['pydevd'], '__loader__'))

        m.__file__ = file
        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

    if locals is None:
        locals = globals


    print('Running %s'%file)
    pydev_imports.execfile(file, globals, locals)  # execute the script

    return globals

#=======================================================================================================================
# main
#=======================================================================================================================
if __name__ == '__main__':
    sys.stdin = BaseStdIn()
    port, client_port = sys.argv[1:3]

    del sys.argv[1]
    del sys.argv[1]

    file = sys.argv[1]

    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 = DoExit

    interpreter = InterpreterInterface(host, int(client_port), threading.currentThread())

    server_thread = threading.Thread(target=start_server,
                                     name='ServerThread',
                                     args=(host, int(port), interpreter))
    server_thread.setDaemon(True)
    server_thread.start()

    globals = run_file(file, None, None)

    interpreter.getNamespace().update(globals)

    process_exec_queue(interpreter)