Skip to content

Commit a991aa4

Browse files
Avoid buffering in interact mode.
1 parent c3829d8 commit a991aa4

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/pexpect_serialspawn/serial_spawn.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import os
2-
import sys
1+
#!/usr/bin/env python
2+
33
import threading
44
from pexpect.fdpexpect import SpawnBase
55
from pexpect.exceptions import EOF, ExceptionPexpect
6-
7-
PY3 = (sys.version_info[0] >= 3)
6+
from pexpect_serialspawn.stdin_helpers import read_char
87

98

109
class SerialSpawn(SpawnBase):
@@ -54,7 +53,7 @@ def _interact_output_loop(self, escape_character=chr(29), input_filter=None, out
5453
if not self.isalive():
5554
self.interacting.set()
5655
try:
57-
data = self.read_nonblocking(size=1000)
56+
data = self.read_nonblocking()
5857
if output_filter:
5958
data = output_filter(data)
6059
self.write_to_stdout(data)
@@ -64,8 +63,7 @@ def _interact_output_loop(self, escape_character=chr(29), input_filter=None, out
6463

6564
def _interact_input_loop(self, escape_character=chr(29), input_filter=None):
6665
while self.interacting.is_set():
67-
data = os.read(self.stdin.fileno(), 1000)
68-
print(data)
66+
data = read_char()
6967
i = -1
7068
if escape_character is not None:
7169
i = data.rfind(escape_character)
@@ -78,13 +76,12 @@ def _interact_input_loop(self, escape_character=chr(29), input_filter=None):
7876
if data:
7977
self.send(data)
8078

81-
def interact(self, escape_character=chr(29), input_filter=None, output_filter=None):
79+
def interact(self, escape_character=chr(0x1b), input_filter=None, output_filter=None):
8280
# Flush buffer to stdout...
8381
self.write_to_stdout(self.buffer)
8482
self.stdout.flush()
8583
# Fix escape character encoding
86-
if escape_character is not None and PY3:
87-
escape_character = escape_character.encode('latin-1')
84+
escape_character = escape_character.encode('latin-1')
8885
# Set the interacting event and start the input thread
8986
self.interacting.set()
9087
input_thread = threading.Thread(

0 commit comments

Comments
 (0)