From: Matt Kimball Date: Sun, 4 Dec 2016 18:03:44 +0000 (-0800) Subject: test: Fix mtr-packet tests for Python 3 X-Git-Tag: v0.88~15^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac58c7a4b744752958975b93d8f774572186a421;p=thirdparty%2Fmtr.git test: Fix mtr-packet tests for Python 3 The mtr-packet test suite was written to run with Python 2, and lacked the Unicode to bytes and bytes to Unicode conversions required to run with Python 3. This change adds those conversions, and the result works with either Python 2 or Python 3. Also, close the subprocess's stdin and stdout explicitly. Python 3's unittest complains about unclosed file descriptors otherwise. --- diff --git a/packet/testpacket.py b/packet/testpacket.py old mode 100755 new mode 100644 index 67bb82c..b145bb4 --- a/packet/testpacket.py +++ b/packet/testpacket.py @@ -78,10 +78,16 @@ class TestProbe(unittest.TestCase): except OSError: return + self.packet_process.stdin.close() + self.packet_process.stdout.close() + def write_command(self, cmd): # type: (str) -> None 'Send a command string to the mtr-packet instance' - self.packet_process.stdin.write(cmd + '\n') + command_str = cmd + '\n' + command_bytes = command_str.encode('utf-8') + + self.packet_process.stdin.write(command_bytes) self.packet_process.stdin.flush() def read_reply(self, timeout=10.0): # type: (float) -> str @@ -106,11 +112,16 @@ class TestProbe(unittest.TestCase): select.select([self.stdout_fd], [], [], select_time) + reply_bytes = None + try: - self.reply_buffer += os.read(self.stdout_fd, 1024) + reply_bytes = os.read(self.stdout_fd, 1024) except OSError: pass + if reply_bytes: + self.reply_buffer += reply_bytes.decode('utf-8') + # If we have read a newline character, we can stop waiting # for more input. newline_ix = self.reply_buffer.find('\n')