Xinne / osc4py3

31 stars 6 forks source link

Crash when using list of client channels #8

Open mxklb opened 2 years ago

mxklb commented 2 years ago
def flat_names(names, _out=None):
    """Flatten nested names containers into a set.

    :param names: nested structures containing names.
    :type names:
    :return: all names found in the structures, in one set.
    :rtype: set
    """
    # Called recursively with an (modified) _out argument.
    # Top call dont provide the _out argument and use the returned value.
    if _out is None:
        _out = set()
    if isinstance(names, str):
        _out.add(names)
    else:   # Assume _names is iterable (list, tuple, dict, set...)
        for n in names:
            if isinstance(n, str):     # Avoid too much recursions.
                _out.add(names)
            else:
                flat_names(n, _out)
    return _out

I think there is a bug if names is iterable (list, tuple, dict, set...)!

Shouldn't this be:

def flat_names(names, _out=None):
    """Flatten nested names containers into a set.

    :param names: nested structures containing names.
    :type names:
    :return: all names found in the structures, in one set.
    :rtype: set
    """
    # Called recursively with an (modified) _out argument.
    # Top call dont provide the _out argument and use the returned value.
    if _out is None:
        _out = set()
    if isinstance(names, str):
        _out.add(names)
    else:   # Assume _names is iterable (list, tuple, dict, set...)
        for n in names:
            if isinstance(n, str):     # Avoid too much recursions.
                _out.add(n)
            else:
                flat_names(n, _out)
    return _out

Actually if I use a list with names oscReceivers=["oscClient1", "oscClient2", ..] like osc_send(msg, oscReceivers) to send OSC to multiple clients (where osc_udp_client(ip, port, "oscClientX") is called for each in the list before) an error is thrown:

    osc_send(msg, oscReceivers)
  File "/usr/local/lib/python3.9/dist-packages/osc4py3/as_eventloop.py", line 124, in osc_send
    oscdistributing.send_packet(packet, names)
   File "/usr/local/lib/python3.9/dist-packages/osc4py3/oscdistributing.py", line 118, in send_packet
     targets = flat_names(names)
  File "/usr/local/lib/python3.9/dist-packages/osc4py3/oscdistributing.py", line 93, in flat_names
     _out.add(names)
TypeError: unhashable type: 'list'

If I look at the code snippets above - the solution is hopefully already provide by _out.add(n) instead of _out.add(names), this should be it. I could make a merge request but just wanted to inform about that issue to not forget ..

mxklb commented 2 years ago

That should fix this: https://github.com/Xinne/osc4py3/pull/9