def test_read_line_bad_data(self): linesep, new_line_pos = find_eol(b"", 0) self.assertEqual(linesep, -1) self.assertEqual(new_line_pos, 0) linesep, new_line_pos = find_eol(b'\n', 0) self.assertEqual(linesep, 0) self.assertEqual(new_line_pos, 1) linesep, new_line_pos = find_eol(b'\r\n', 0) self.assertEqual(linesep, 0) self.assertEqual(new_line_pos, 2) linesep, new_line_pos = find_eol(b'\r', 0) self.assertEqual(linesep, -1) self.assertEqual(new_line_pos, 0)
def _read_output_buffer(self, _buffer, timeout=None): timer = GTimeout(seconds=timeout, exception=Timeout) remainder = b"" remainder_len = 0 timer.start() try: for data in _buffer: pos = 0 size = len(data) while pos < size: linesep, new_line_pos = find_eol(data, pos) if linesep == -1: remainder += data[pos:] remainder_len = len(remainder) break end_of_line = pos+linesep if remainder_len > 0: line = remainder + data[pos:end_of_line] remainder = b"" remainder_len = 0 else: line = data[pos:end_of_line] yield line pos += linesep + new_line_pos if remainder_len > 0: # Finished reading without finding ending linesep yield remainder finally: timer.close()
def _read_output(self, read_func, timeout=None): remainder = b"" remainder_len = 0 size, data = read_func() t = GTimeout(seconds=timeout, exception=Timeout) t.start() try: while size == LIBSSH2_ERROR_EAGAIN or size > 0: while size == LIBSSH2_ERROR_EAGAIN: self.poll(timeout=timeout) size, data = read_func() while size > 0: pos = 0 while pos < size: linesep, new_line_pos = find_eol(data, pos) if linesep == -1: remainder += data[pos:] remainder_len = len(remainder) break end_of_line = pos + linesep if remainder_len > 0: line = remainder + data[pos:end_of_line] remainder = b"" remainder_len = 0 else: line = data[pos:end_of_line] yield line pos += linesep + new_line_pos size, data = read_func() if remainder_len > 0: # Finished reading without finding ending linesep yield remainder finally: t.close()
def test_read_line_crnl(self): lines = [b'a line', b'another line', b'third'] buf = b"\r\n".join(lines) pos = 0 line_num = 0 linesep, new_line_pos = find_eol(buf, 0) self.assertTrue(linesep > 0) self.assertTrue(linesep < len(buf)) while pos < len(buf): if linesep < 0: break end_of_line = pos + linesep line = buf[pos:end_of_line] self.assertEqual(lines[line_num], line) pos += linesep + new_line_pos line_num += 1 linesep, new_line_pos = find_eol(buf, pos) line = buf[pos:] self.assertEqual(lines[line_num], line)
def test_find_eol_no_lines(self): buf = b"a buffer" linepos, new_line_pos = find_eol(buf, 0) self.assertEqual(linepos, -1) self.assertEqual(new_line_pos, 0)
def test_read_line_cr_only(self): lines = [b'a line', b'another line', b'third'] buf = b"\r".join(lines) linesep, new_line_pos = find_eol(buf, 0) self.assertEqual(linesep, -1)