Skip to content

Commit

Permalink
Merge pull request #27 from pdecat/master
Browse files Browse the repository at this point in the history
Avoid KeyError when no room exists
  • Loading branch information
miguelgrinberg committed Apr 12, 2016
2 parents 6481816 + 626499c commit 0173306
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions socketio/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ def close_room(self, room, namespace):
def get_rooms(self, sid, namespace):
"""Return the rooms a client is in."""
r = []
for room_name, room in six.iteritems(self.rooms[namespace]):
if room_name is not None and sid in room and room[sid]:
r.append(room_name)
try:
for room_name, room in six.iteritems(self.rooms[namespace]):
if room_name is not None and sid in room and room[sid]:
r.append(room_name)
except KeyError:
pass
return r

def emit(self, event, data, namespace, room=None, skip_sid=None,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def test_leave_invalid_room(self):
self.bm.leave_room('123', '/foo', 'baz')
self.bm.leave_room('123', '/bar', 'baz')

def test_no_room(self):
rooms = self.bm.get_rooms('123', '/foo')
self.assertEqual([], rooms)

def test_close_room(self):
self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo')
Expand Down

0 comments on commit 0173306

Please sign in to comment.