Closed p1-ra closed 2 years ago
Hi again,
Rather than loosing some perf by always forcing the conversion of a potential mutable bytearray to an immutable bytes (for everyone), I'm using a workaround on my side, I'm injecting a new method on the pysctp socket's instance in charge of that, bellow the code if anyone have the same need:
import types
(...)
def stcp_send_mutable(self: sctpsocket, msg: Union[bytes, bytearray], flags: int=0, to: Tuple[str, int]=("",0), ppid: Optional[int]=None, stream: Optional[int]=None, timetolive: Optional[int]=None, context: int=0,
record_file_prefix: str ="RECORD_sctp_traffic", datalogging: bool = False) -> bytes:
return self.sctp_send(bytes(msg), to=to, ppid=ppid, flags=flags, stream=stream, timetolive=timetolive, context=context,
record_file_prefix=record_file_prefix, datalogging=datalogging)
(...)
data = bytearray(b'0000')
my_sctp_socket.stcp_send_mutable = types.MethodType(stcp_send_mutable, my_sctp_socket) # type: ignore
my_sctp_socket.stcp_send_mutable(data)
In my actual use case, with asyncio, I'm actually overriding the send
method btw:
my_sctp_socket.send = types.MethodType(stcp_send_mutable, my_sctp_socket) # type: ignore
I'm closing the PR.
Hi,
Actually, the wrapper arround
_sctp.sctp_send_msg
only accept readonlybytes
.On my side I'm using python asyncio from an existing sctp socket. The library will time to time call the socket send method with a mutable
bytearray
instead of an immutablebytes
as parameter, which will trigger an error:It's seems common for python to accept either a bytearray or bytes and looks to not ~fully be a bug on asyncio side, python socket.py write's method description says:
Proposed fix here, always enforce the conversion to read only
bytes