Browse Source

Merge pull request #27 from pdecat/master

Avoid KeyError when no room exists
pull/30/merge
Miguel Grinberg 9 years ago
parent
commit
0173306ee9
  1. 9
      socketio/base_manager.py
  2. 4
      tests/test_base_manager.py

9
socketio/base_manager.py

@ -83,9 +83,12 @@ class BaseManager(object):
def get_rooms(self, sid, namespace): def get_rooms(self, sid, namespace):
"""Return the rooms a client is in.""" """Return the rooms a client is in."""
r = [] r = []
for room_name, room in six.iteritems(self.rooms[namespace]): try:
if room_name is not None and sid in room and room[sid]: for room_name, room in six.iteritems(self.rooms[namespace]):
r.append(room_name) if room_name is not None and sid in room and room[sid]:
r.append(room_name)
except KeyError:
pass
return r return r
def emit(self, event, data, namespace, room=None, skip_sid=None, def emit(self, event, data, namespace, room=None, skip_sid=None,

4
tests/test_base_manager.py

@ -131,6 +131,10 @@ class TestBaseManager(unittest.TestCase):
self.bm.leave_room('123', '/foo', 'baz') self.bm.leave_room('123', '/foo', 'baz')
self.bm.leave_room('123', '/bar', '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): def test_close_room(self):
self.bm.connect('123', '/foo') self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo') self.bm.connect('456', '/foo')

Loading…
Cancel
Save