From 2d6cb431e328c8cd57b03e7272dc750c9ace08ed Mon Sep 17 00:00:00 2001 From: erhankur Date: Sat, 10 Aug 2024 12:43:34 +0200 Subject: [PATCH] fix(jtag): flush previous gdb responses before the first gdb command --- .../pytest_embedded_jtag/gdb.py | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/pytest-embedded-jtag/pytest_embedded_jtag/gdb.py b/pytest-embedded-jtag/pytest_embedded_jtag/gdb.py index 756875c8..0f5e6b64 100644 --- a/pytest-embedded-jtag/pytest_embedded_jtag/gdb.py +++ b/pytest-embedded-jtag/pytest_embedded_jtag/gdb.py @@ -20,36 +20,39 @@ def __init__(self, gdb_prog_path: Optional[str] = None, gdb_cli_args: Optional[s gdb_prog_path = gdb_prog_path or self.GDB_PROG_PATH gdb_cli_args = shlex.split(gdb_cli_args or self.GDB_DEFAULT_ARGS) - self._gdb_first_prompt_matched = False + self._gdb_first_write = True super().__init__(cmd=[gdb_prog_path, *gdb_cli_args], **kwargs) def write(self, s: AnyStr, non_blocking: bool = False, timeout: float = 30) -> Optional[str]: - super().write(s) - _buffer = '' - _t_start = time.time() - with open(self._logfile) as fr: + if self._gdb_first_write: + # Discard all queued responses before the first write + _ = fr.readlines() + self._logfile_offset = fr.tell() + self._gdb_first_write = False + + super().write(s) + + if non_blocking: + logging.debug('non-blocking write...') + return None + + _buffer = '' + _t_start = time.time() fr.seek(self._logfile_offset) while True: line = fr.readline() - _t_now = time.time() - if non_blocking: - logging.debug('non-blocking write...') - return None - - if (_t_now - _t_start) >= timeout: - logging.debug(f'current buffer: {_buffer}') - raise ValueError(f'gdb no response after {timeout} seconds') - if line: _buffer += line if self._GDB_RESPONSE_FINISHED_RE.match(line): - if not self._gdb_first_prompt_matched: - self._gdb_first_prompt_matched = True - continue break + _t_now = time.time() + if (_t_now - _t_start) >= timeout: + logging.debug(f'current buffer: {_buffer}') + raise ValueError(f'gdb no response after {timeout} seconds') + self._logfile_offset = fr.tell() logging.debug(f'{self.SOURCE} <-: {_buffer}')