From 240deb27d4be6d6a7f4acc44d09df4e1f6670612 Mon Sep 17 00:00:00 2001 From: totokaka Date: Sat, 3 Jan 2015 10:51:19 +0100 Subject: [PATCH] Raise IOError when TCPConnection.read(length) recieves no data. This commit fixes #24 by simply checking whether the socket received any data at all. If it did not, an IOError is raised. It might be that we should try to recieve multiple times before the IOError is raised, but this is not implemented in this commit as to avoid bloat. --- mcstatus/protocol/connection.py | 5 ++++- mcstatus/tests/protocol/test_connection.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mcstatus/protocol/connection.py b/mcstatus/protocol/connection.py index e028a8d..ff2c0c8 100644 --- a/mcstatus/protocol/connection.py +++ b/mcstatus/protocol/connection.py @@ -140,7 +140,10 @@ class TCPSocketConnection(Connection): def read(self, length): result = bytearray() while len(result) < length: - result.extend(self.socket.recv(length - len(result))) + new = self.socket.recv(length - len(result)) + if len(new) == 0: + raise IOError("Server did not respond with any information!") + result.extend(new) return result def write(self, data): diff --git a/mcstatus/tests/protocol/test_connection.py b/mcstatus/tests/protocol/test_connection.py index 43d46b4..810834e 100644 --- a/mcstatus/tests/protocol/test_connection.py +++ b/mcstatus/tests/protocol/test_connection.py @@ -225,6 +225,12 @@ class TCPSocketConnectionTest(TestCase): self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA")) + def test_read_empty(self): + self.connection.socket.recv.return_value = bytearray.fromhex("") + + with self.assertRaises(IOError): + self.connection.read(2) + def test_write(self): self.connection.write(bytearray.fromhex("7FAA"))