Skip to content

Commit 883cb5e

Browse files
Removing escape sequence from output in cmd_output function!
In console outputs we are getting escape sequences because of which most of the testcases are failing to use the output of few commands. This PR removes the escape sequence first then use the output.This PR fixes multiple testcases. Signed-off-by: Anushree Mathur <[email protected]>
1 parent 72d2e67 commit 883cb5e

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

aexpect/client.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
11881188
print_func)[1]
11891189

11901190
def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1191-
print_func=None, safe=False):
1191+
print_func=None, safe=False, strip_console_codes=False):
11921192
"""
11931193
Send a command and return its output.
11941194
@@ -1203,15 +1203,19 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12031203
error messages that make read_up_to_prompt to timeout. Let's
12041204
try to be a little more robust and send a carriage return, to
12051205
see if we can get to the prompt when safe=True.
1206-
1206+
:param strip_console_codes: Whether to remove the escape sequence from the output.
1207+
In serial sessions, there are escape sequences present. If it is not
1208+
expected while reading the output remove it by passing
1209+
serial_console_codes = True.
12071210
:return: The output of cmd
12081211
:raise ShellTimeoutError: Raised if timeout expires
12091212
:raise ShellProcessTerminatedError: Raised if the shell process
12101213
terminates while waiting for output
12111214
:raise ShellError: Raised if an unknown error occurs
1215+
12121216
"""
12131217
if safe:
1214-
return self.cmd_output_safe(cmd, timeout)
1218+
return self.cmd_output_safe(cmd, timeout, strip_console_codes)
12151219
session_tag = f"[{self.output_prefix}] " if self.output_prefix else ""
12161220
LOG.debug("%sSending command: %s", session_tag, cmd)
12171221
self.read_nonblocking(0, timeout)
@@ -1229,10 +1233,13 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
12291233
raise ShellError(cmd, output) from error
12301234

12311235
# Remove the echoed command and the final shell prompt
1236+
if strip_console_codes:
1237+
# Removing the escape sequence
1238+
out = astring.strip_console_codes(out)
12321239
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12331240
cmd))
12341241

1235-
def cmd_output_safe(self, cmd, timeout=60):
1242+
def cmd_output_safe(self, cmd, timeout=60, strip_console_codes=False):
12361243
"""
12371244
Send a command and return its output (serial sessions).
12381245
@@ -1244,7 +1251,10 @@ def cmd_output_safe(self, cmd, timeout=60):
12441251
:param cmd: Command to send (must not contain newline characters)
12451252
:param timeout: The duration (in seconds) to wait for the prompt to
12461253
return
1247-
1254+
:param strip_console_codes: Whether to remove the escape sequence from the output.
1255+
In serial sessions, there are escape sequences present. If it is not
1256+
expected while reading the output remove it by passing
1257+
serial_console_codes = True.
12481258
:return: The output of cmd
12491259
:raise ShellTimeoutError: Raised if timeout expires
12501260
:raise ShellProcessTerminatedError: Raised if the shell process
@@ -1278,6 +1288,9 @@ def cmd_output_safe(self, cmd, timeout=60):
12781288
raise ShellTimeoutError(cmd, out)
12791289

12801290
# Remove the echoed command and the final shell prompt
1291+
if strip_console_codes:
1292+
# Removing the escape sequence
1293+
out = astring.strip_console_codes(out)
12811294
return self.remove_last_nonempty_line(self.remove_command_echo(out,
12821295
cmd))
12831296

0 commit comments

Comments
 (0)