Skip to content

Commit 6c9b997

Browse files
Support sending bytestrings when using pub/sub managers
1 parent db3f1c2 commit 6c9b997

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/socketio/packet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def deconstruct_binary(cls, data):
154154

155155
@classmethod
156156
def _deconstruct_binary_internal(cls, data, attachments):
157-
if isinstance(data, bytes):
157+
if isinstance(data, (bytes, bytearray)):
158158
attachments.append(data)
159159
return {'_placeholder': True, 'num': len(attachments) - 1}
160160
elif isinstance(data, list):
@@ -169,7 +169,7 @@ def _deconstruct_binary_internal(cls, data, attachments):
169169
@classmethod
170170
def data_is_binary(cls, data):
171171
"""Check if the data contains binary components."""
172-
if isinstance(data, bytes):
172+
if isinstance(data, (bytes, bytearray)):
173173
return True
174174
elif isinstance(data, list):
175175
return functools.reduce(

tests/async/test_pubsub_manager.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ async def test_emit_binary(self):
9797
}
9898
)
9999

100+
async def test_emit_bytearray(self):
101+
await self.pm.emit('foo', bytearray(b'bar'))
102+
self.pm._publish.assert_awaited_once_with(
103+
{
104+
'method': 'emit',
105+
'event': 'foo',
106+
'binary': True,
107+
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
108+
'namespace': '/',
109+
'room': None,
110+
'skip_sid': None,
111+
'callback': None,
112+
'host_id': '123456',
113+
}
114+
)
115+
await self.pm.emit('foo', {'foo': bytearray(b'bar')})
116+
self.pm._publish.assert_awaited_with(
117+
{
118+
'method': 'emit',
119+
'event': 'foo',
120+
'binary': True,
121+
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
122+
'namespace': '/',
123+
'room': None,
124+
'skip_sid': None,
125+
'callback': None,
126+
'host_id': '123456',
127+
}
128+
)
129+
100130
async def test_emit_with_to(self):
101131
sid = 'room-mate'
102132
await self.pm.emit('foo', 'bar', to=sid)

tests/common/test_packet.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,15 @@ def test_data_is_binary_list(self):
279279
assert not pkt.data_is_binary(['foo'])
280280
assert not pkt.data_is_binary([])
281281
assert pkt.data_is_binary([b'foo'])
282+
assert pkt.data_is_binary([bytearray(b'foo')])
282283
assert pkt.data_is_binary(['foo', b'bar'])
284+
assert pkt.data_is_binary(['foo', bytearray(b'bar')])
283285

284286
def test_data_is_binary_dict(self):
285287
pkt = packet.Packet()
286288
assert not pkt.data_is_binary({'a': 'foo'})
287289
assert not pkt.data_is_binary({})
288290
assert pkt.data_is_binary({'a': b'foo'})
291+
assert pkt.data_is_binary({'a': bytearray(b'foo')})
289292
assert pkt.data_is_binary({'a': 'foo', 'b': b'bar'})
293+
assert pkt.data_is_binary({'a': 'foo', 'b': bytearray(b'bar')})

tests/common/test_pubsub_manager.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ def test_emit_binary(self):
109109
}
110110
)
111111

112+
def test_emit_bytearray(self):
113+
self.pm.emit('foo', bytearray(b'bar'))
114+
self.pm._publish.assert_called_once_with(
115+
{
116+
'method': 'emit',
117+
'event': 'foo',
118+
'binary': True,
119+
'data': [{'_placeholder': True, 'num': 0}, 'YmFy'],
120+
'namespace': '/',
121+
'room': None,
122+
'skip_sid': None,
123+
'callback': None,
124+
'host_id': '123456',
125+
}
126+
)
127+
self.pm.emit('foo', {'foo': bytearray(b'bar')})
128+
self.pm._publish.assert_called_with(
129+
{
130+
'method': 'emit',
131+
'event': 'foo',
132+
'binary': True,
133+
'data': [{'foo': {'_placeholder': True, 'num': 0}}, 'YmFy'],
134+
'namespace': '/',
135+
'room': None,
136+
'skip_sid': None,
137+
'callback': None,
138+
'host_id': '123456',
139+
}
140+
)
141+
112142
def test_emit_with_to(self):
113143
sid = "ferris"
114144
self.pm.emit('foo', 'bar', to=sid)

0 commit comments

Comments
 (0)