Show exceptions by default and add `handle_exception' parameter to Connection.

This commit is contained in:
joo committed 2016-11-20 06:04:22 +00:00
1 parent 115693f8c3
commit d72f05c8b0
2 files changed
+30 -4

No files matched your search

+28 -3
View File
@@ -53,6 +53,7 @@ class Connection(object):
username=None, username=None,
initial_version=None, initial_version=None,
allowed_versions=None, allowed_versions=None,
handle_exception=None,
): ):
"""Sets up an instance of this object to be able to connect to a """Sets up an instance of this object to be able to connect to a
minecraft server. minecraft server.
@@ -73,6 +74,17 @@ class Connection(object):
version string or protocol version number, version string or protocol version number,
restricting the versions that the client may restricting the versions that the client may
use in connecting to the server. use in connecting to the server.
:param handle_exception: A function to be called when an exception
occurs in the client's networking thread,
taking 2 arguments: the exception object `e'
as in `except Exception as e', and a 3-tuple
given by sys.exc_info(); or None for the
default behaviour of raising the exception
from its original context; or False for no
action. In any case, the networking thread
will terminate, the exception will be
available via the `exception' and `exc_info'
attributes of the `Connection' instance.
""" """
self._write_lock = Lock() self._write_lock = Lock()
@@ -109,6 +121,9 @@ class Connection(object):
self.auth_token = auth_token self.auth_token = auth_token
self.username = username self.username = username
self.handle_exception = handle_exception
self.exception, self.exc_info = None, None
# The reactor handles all the default responses to packets, # The reactor handles all the default responses to packets,
# it should be changed per networking state # it should be changed per networking state
self.reactor = PacketReactor(self) self.reactor = PacketReactor(self)
@@ -271,6 +286,17 @@ class Connection(object):
self.write_packet(handshake) self.write_packet(handshake)
def _handle_exception(self, exc, exc_info):
try:
exc.exc_info = exc_info # For backward compatibility.
except (TypeError, AttributeError):
pass
self.exception, self.exc_info = exc, exc_info
if self.handle_exception is None:
raise_(*exc_info)
elif self.handle_exception is not False:
self.handle_exception(exc, exc_info)
class NetworkingThread(threading.Thread): class NetworkingThread(threading.Thread):
def __init__(self, connection): def __init__(self, connection):
@@ -283,9 +309,8 @@ class NetworkingThread(threading.Thread):
def run(self): def run(self):
try: try:
self._run() self._run()
except Exception as e: except BaseException as e:
e.exc_info = sys.exc_info() self.connection._handle_exception(e, sys.exc_info())
self.connection.exception = e
finally: finally:
self.connection.networking_thread = None self.connection.networking_thread = None
+2 -1
View File
@@ -24,7 +24,8 @@ class _ConnectTest(unittest.TestCase):
addr, port = server.listen_socket.getsockname() addr, port = server.listen_socket.getsockname()
client = connection.Connection( client = connection.Connection(
addr, port, username='User', initial_version=client_version) addr, port, username='User', initial_version=client_version,
handle_exception=False)
client.register_packet_listener( client.register_packet_listener(
lambda packet: logging.debug('[ ->C] %s' % packet), packets.Packet) lambda packet: logging.debug('[ ->C] %s' % packet), packets.Packet)