Skip to content

Commit 8781f75

Browse files
[PATCH] Apply changes as per review (- WIP PR #371 -)
* This is part of the work for GHI #317 Changes in file multicast/recv.py: * Minor ocumentation improvements * added some type hints (- WIP #134 -) * related work
1 parent 4324d77 commit 8781f75

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

multicast/recv.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,56 +212,65 @@
212212
)
213213

214214

215-
_mcast_recv_bind_not_join_prefix = "Unusual call to multicast.joinstep with no groups."
215+
_w_prefix: str = "Unusual call to multicast.joinstep with no groups."
216216

217217

218-
_mcast_recv_just_bind_code = """
218+
_w_example_code: str = """
219219
sock = multicast.genSocket() if isock is None else isock
220220
sock.bind((multicast._MCAST_DEFAULT_GROUP, port))
221221
222222
"""
223223

224224

225-
_mcast_recv_bind_not_join_msg = "...".join([
225+
_w_advice: str = "...".join([
226226
"Consider using",
227-
_mcast_recv_just_bind_code,
227+
_w_example_code,
228228
"instead, for improved performance. Otherwise specify the multicast bind group.",
229229
])
230230

231231

232-
_mcast_recv_empty_join_warn_message = "\n".join([
233-
_mcast_recv_bind_not_join_prefix,
234-
_mcast_recv_bind_not_join_msg,
232+
_w_empty_join_warning: str = "\n".join([
233+
_w_prefix,
234+
_w_advice,
235235
])
236236

237237

238-
_mcast_recv_lazy_bind_warning_message = "\n".join([
238+
_w_unspec_bind: str = "\n".join([
239239
"Lazy call to multicast.joinstep with unspecified bind group.",
240240
f"Will bind to {multicast._MCAST_DEFAULT_BIND_IP}.", # skipcq: PYL-W0212 - module ok
241241
"Tip: Pass a value for bind_group to suppress this message",
242242
"(such as 'bind_group=multicast._MCAST_DEFAULT_BIND_IP')",
243243
])
244244

245-
_mcast_recv_join_only_multicast_warn_msg = "\n".join([
246-
_mcast_recv_bind_not_join_prefix,
247-
"Just use socket.Socket.bind(...) for non-multicast networking.",
248-
])
245+
246+
_w_non_multicast = f"{_w_prefix}\nJust use socket.Socket.bind(...) for non-multicast networking."
249247

250248

251-
def _validate_join_args(groups=None, port=None, iface=None, bind_group=None, isock=None):
249+
def _validate_join_args(groups=None, port=None, iface=None, bind_group=None, isock=None) -> tuple:
252250
"""Validates joinstep arguments.
253251
254252
This is a helper function and should NOT be called directly.
255253
254+
Args:
255+
groups (list): List of multicast group addresses to join.
256+
port (int): Port number to bind the socket to.
257+
iface (str, optional): Network interface to use.
258+
bind_group (str, optional): Specific group address to bind to.
259+
isock (socket.socket, optional): Existing socket to configure.
260+
261+
Note:
262+
All warning messages are only emitted when __debug__ is True
263+
(i.e., when Python is not running with -O or -OO).
264+
256265
Returns:
257-
List of inputs after normalizing.
266+
A tuple of (groups, port, iface, bind_group, isock) after normalization.
258267
"""
259268
if not groups:
260269
groups = []
261270
if __debug__: # pragma: no branch
262271
if not bind_group:
263272
warnings.warn(
264-
_mcast_recv_empty_join_warn_message,
273+
_w_empty_join_warning,
265274
category=SyntaxWarning,
266275
stacklevel=3,
267276
)
@@ -270,7 +279,7 @@ def _validate_join_args(groups=None, port=None, iface=None, bind_group=None, iso
270279
groups = [bind_group]
271280
else:
272281
warnings.warn(
273-
_mcast_recv_join_only_multicast_warn_msg,
282+
_w_non_multicast,
274283
category=SyntaxWarning,
275284
stacklevel=3,
276285
)
@@ -284,14 +293,14 @@ def _validate_join_args(groups=None, port=None, iface=None, bind_group=None, iso
284293
groups[0] != multicast._MCAST_DEFAULT_BIND_IP # skipcq: PYL-W0212 - module ok
285294
):
286295
warnings.warn(
287-
_mcast_recv_lazy_bind_warning_message,
296+
_w_unspec_bind,
288297
category=ResourceWarning,
289298
stacklevel=3,
290299
)
291300
return (groups, port, iface, bind_group, isock)
292301

293302

294-
def joinstep(groups, port, iface=None, bind_group=None, isock=None):
303+
def joinstep(groups, port, iface=None, bind_group=None, isock=None) -> _socket.socket:
295304
"""
296305
Join multicast groups to prepare for receiving messages.
297306
@@ -373,10 +382,7 @@ def joinstep(groups, port, iface=None, bind_group=None, isock=None):
373382
374383
"""
375384
groups, _, _, bind_group, _ = _validate_join_args(groups=groups, bind_group=bind_group)
376-
if isock is None:
377-
sock = multicast.genSocket()
378-
else:
379-
sock = isock.dup()
385+
sock = multicast.genSocket() if isock is None else isock.dup()
380386
try:
381387
# skipcq: PYL-W0212
382388
sock.bind((multicast._MCAST_DEFAULT_BIND_IP if bind_group is None else bind_group, port))
@@ -392,7 +398,7 @@ def joinstep(groups, port, iface=None, bind_group=None, isock=None):
392398
return sock
393399

394400

395-
def tryrecv(msgbuffer, chunk, sock):
401+
def tryrecv(msgbuffer: list, chunk: bytes, sock: _socket.socket) -> str:
396402
"""
397403
Attempt to receive data on the given socket and decode it into the message buffer.
398404
@@ -481,7 +487,7 @@ def tryrecv(msgbuffer, chunk, sock):
481487
return msgbuffer
482488

483489

484-
def recvstep(msgbuffer, chunk, sock):
490+
def recvstep(msgbuffer: list, chunk: bytes, sock: _socket.socket) -> str:
485491
"""
486492
Receive messages continuously until interrupted.
487493

0 commit comments

Comments
 (0)