Skip to content

Commit 0013dda

Browse files
coriverachlafreniere
authored andcommitted
Refactor send_input to handle single line comments and lingering output (#29)
* Use locks to prevent commands from executing simultaneously. * Clear output before executing command. * Rename send_input * Update a comment and shorten input formatting.
1 parent 7b2f06d commit 0013dda

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

powershell_kernel/kernel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def do_execute(self, code, silent, store_history=True,
6161
if not self.proxy:
6262
self.__createProxy()
6363

64-
self.proxy.send_input('. { ' + code + ' }')
65-
output = self.proxy.get_output()
64+
output = self.proxy.run_command(code)
6665

6766
message = {'name': 'stdout', 'text': output}
6867
self.send_response(self.iopub_socket, 'stream', message)

powershell_kernel/powershell_proxy.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import queue
44
except ImportError:
55
import Queue as queue
6-
from threading import Timer
6+
from threading import Timer, Lock
77
from time import sleep
88

99
class ReplReader(threading.Thread):
@@ -25,44 +25,46 @@ def run(self):
2525

2626
class ReplProxy(object):
2727
def __init__(self, repl):
28+
self.runCmdLock = Lock()
29+
2830
self._repl = repl
2931
self._repl_reader = ReplReader(repl)
30-
# this is a hack to detect when we stop processing this input
31-
self.send_input('function prompt() {"^"}')
3232

3333
self.stop_flag = False
3434
self.output = ''
3535
self.timer = Timer(0.1, self.update_view_loop)
3636
self.timer.start()
37-
# get preambula and eveluation of the prompt
38-
self.get_output()
3937

4038
self.output_prefix_stripped = True
4139
self.expected_output_prefix = ''
4240
self.expected_output_len = 0
4341

42+
# this is a hack to detect when we stop processing this input
43+
self.run_command('function prompt() {"^"}')
4444

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()
6668

6769
def handle_repl_output(self):
6870
"""Returns new data from Repl and bool indicating if Repl is still

0 commit comments

Comments
 (0)