3
3
import queue
4
4
except ImportError :
5
5
import Queue as queue
6
- from threading import Timer
6
+ from threading import Timer , Lock
7
7
from time import sleep
8
8
9
9
class ReplReader (threading .Thread ):
@@ -25,44 +25,46 @@ def run(self):
25
25
26
26
class ReplProxy (object ):
27
27
def __init__ (self , repl ):
28
+ self .runCmdLock = Lock ()
29
+
28
30
self ._repl = repl
29
31
self ._repl_reader = ReplReader (repl )
30
- # this is a hack to detect when we stop processing this input
31
- self .send_input ('function prompt() {"^"}' )
32
32
33
33
self .stop_flag = False
34
34
self .output = ''
35
35
self .timer = Timer (0.1 , self .update_view_loop )
36
36
self .timer .start ()
37
- # get preambula and eveluation of the prompt
38
- self .get_output ()
39
37
40
38
self .output_prefix_stripped = True
41
39
self .expected_output_prefix = ''
42
40
self .expected_output_len = 0
43
41
42
+ # this is a hack to detect when we stop processing this input
43
+ self .run_command ('function prompt() {"^"}' )
44
44
45
- def get_output (self ):
46
- while not self .stop_flag :
47
- sleep (0.05 )
48
- out = self .output
49
- self .output = ''
50
- self .stop_flag = False
51
- return out
52
-
53
- def send_input (self , input ):
54
- # TODO: we should block here until we return output for previous command, should we?
55
-
56
- # for multiline statements we should send 1 extra new line
57
- # https://stackoverflow.com/questions/13229066/how-to-end-a-multi-line-command-in-powershell
58
- if '\n ' in input :
59
- input += '\n '
60
-
61
- self .expected_output_prefix = input .replace ('\n ' , '\n >> ' ) + '\n '
62
- self .expected_output_len = len (self .expected_output_prefix )
63
- self .output_prefix_stripped = False
64
-
65
- self ._repl .write (input + '\n ' )
45
+ def run_command (self , input ):
46
+ self .runCmdLock .acquire ()
47
+ try :
48
+ self .output = ''
49
+ self .stop_flag = False
50
+
51
+ # Append newline to the original input to handle single line comments on the last line
52
+ #
53
+ # Also, for multiline statements we should send 1 extra new line at the end
54
+ # https://stackoverflow.com/questions/13229066/how-to-end-a-multi-line-command-in-powershell
55
+ input = '. {\n ' + input + '\n }\n '
56
+
57
+ self .expected_output_prefix = input .replace ('\n ' , '\n >> ' ) + '\n '
58
+ self .expected_output_len = len (self .expected_output_prefix )
59
+ self .output_prefix_stripped = False
60
+
61
+ self ._repl .write (input + '\n ' )
62
+
63
+ while not self .stop_flag :
64
+ sleep (0.05 )
65
+ return self .output
66
+ finally :
67
+ self .runCmdLock .release ()
66
68
67
69
def handle_repl_output (self ):
68
70
"""Returns new data from Repl and bool indicating if Repl is still
0 commit comments