Cleanup logging and exception handling.

The syntax and attribute errors raised during a disconnect/reconnect
attempt are now caught and produce nicer log messages.
This commit is contained in:
Lance Stout 2011-05-31 10:23:05 -07:00
parent 1735c194cd
commit 8080b4cae2
2 changed files with 36 additions and 30 deletions

View file

@ -22,6 +22,8 @@ class FileSocket(_fileobject):
def read(self, size=4096): def read(self, size=4096):
"""Read data from the socket as if it were a file.""" """Read data from the socket as if it were a file."""
if self._sock is None:
return None
data = self._sock.recv(size) data = self._sock.recv(size)
if data is not None: if data is not None:
return data return data

View file

@ -727,7 +727,7 @@ class XMLStream(object):
Defaults to self.auto_reconnect. Defaults to self.auto_reconnect.
""" """
if now: if now:
log.debug("SEND: %s" % data) log.debug("SEND (IMMED): %s" % data)
try: try:
self.socket.send(data.encode('utf-8')) self.socket.send(data.encode('utf-8'))
except Socket.error as serr: except Socket.error as serr:
@ -829,35 +829,39 @@ class XMLStream(object):
""" """
depth = 0 depth = 0
root = None root = None
for (event, xml) in ET.iterparse(self.filesocket, (b'end', b'start')): try:
if event == b'start': for (event, xml) in ET.iterparse(self.filesocket,
if depth == 0: (b'end', b'start')):
# We have received the start of the root element. if event == b'start':
root = xml if depth == 0:
# Perform any stream initialization actions, such # We have received the start of the root element.
# as handshakes. root = xml
self.stream_end_event.clear() # Perform any stream initialization actions, such
self.start_stream_handler(root) # as handshakes.
depth += 1 self.stream_end_event.clear()
if event == b'end': self.start_stream_handler(root)
depth -= 1 depth += 1
if depth == 0: if event == b'end':
# The stream's root element has closed, depth -= 1
# terminating the stream. if depth == 0:
log.debug("End of stream recieved") # The stream's root element has closed,
self.stream_end_event.set() # terminating the stream.
return False log.debug("End of stream recieved")
elif depth == 1: self.stream_end_event.set()
# We only raise events for stanzas that are direct return False
# children of the root element. elif depth == 1:
try: # We only raise events for stanzas that are direct
self.__spawn_event(xml) # children of the root element.
except RestartStream: try:
return True self.__spawn_event(xml)
if root: except RestartStream:
# Keep the root element empty of children to return True
# save on memory use. if root:
root.clear() # Keep the root element empty of children to
# save on memory use.
root.clear()
except SyntaxError:
log.error("Error reading from XML stream.")
log.debug("Ending read XML loop") log.debug("Ending read XML loop")
def _build_stanza(self, xml, default_ns=None): def _build_stanza(self, xml, default_ns=None):