def test_format_encoding(): s = u'éééààà' with raises(TypeError): read_message(format_message('/test', [s])[0]) assert read_message(format_message('/test', [s], encoding='utf8')[0])[2][0] == s.encode( 'utf8') # noqa assert read_message(format_message('/test', [s], encoding='utf8')[0], encoding='utf8')[2][0] == s # noqa with raises(UnicodeEncodeError): format_message('/test', [s], encoding='ascii') # noqa with raises(UnicodeDecodeError): read_message(format_message('/test', [s], encoding='utf8')[0], encoding='ascii') # noqa assert read_message(format_message('/test', [s], encoding='utf8')[0], encoding='ascii', encoding_errors='ignore')[2][0] == '' assert read_message(format_message('/test', [s], encoding='utf8')[0], encoding='ascii', encoding_errors='replace')[2][0] == u'������������'
def send_message(osc_address, values, ip_address, port, sock=SOCK, safer=False, encoding='', encoding_errors='strict'): """Send an osc message to a socket address. - `osc_address` is the osc endpoint to send the data to (e.g b'/test') it should be a bytestring - `values` is the list of values to send, they can be any supported osc type (bytestring, float, int, blob...) - `ip_address` can either be an ip address if the used socket is of the AF_INET family, or a filename if the socket is of type AF_UNIX - `port` value will be ignored if socket is of type AF_UNIX - `sock` should be a socket object, the client's default socket can be used as default - the `safer` parameter allows to wait a little after sending, to make sure the message is actually sent before doing anything else, should only be useful in tight loop or cpu-busy code. - `encoding` if defined, will be used to encode/decode all strings sent/received to/from unicode/string objects, if left empty, the interface will only accept bytes and return bytes to callback functions. - `encoding_errors` if `encoding` is set, this value will be used as `errors` parameter in encode/decode calls. examples: send_message(b'/test', [b'hello', 1000, 1.234], 'localhost', 8000) send_message(b'/test', [], '192.168.0.1', 8000, safer=True) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) send_message(b'/test', [], '192.168.0.1', 8000, sock=sock, safer=True) # unix sockets work on linux and osx, and over unix platforms, # but not windows sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) send_message(b'/some/address', [1, 2, 3], b'/tmp/sock') """ if platform != 'win32' and sock.family == socket.AF_UNIX: address = ip_address else: address = (ip_address, port) message, stats = format_message(osc_address, values, encoding=encoding, encoding_errors=encoding_errors) sock.sendto(message, address) if safer: sleep(10e-9) return stats
[1.2345, 1.2345, 10000000000.], list(range(500)), ] print("#" * 80) print("format/parse test") for i, pattern in enumerate(patterns): print("*" * 100) print(f"pattern: {i}") n = 0 timeout = time() + DURATION while time() < timeout: n += 1 p, s = format_message(b'/address', pattern) size = len(p) / 1000 print(f"formated message {n} times ({n / DURATION}/s) " f"({n * size / DURATION:.2f}MB/s)") n = 0 timeout = time() + DURATION while time() < timeout: n += 1 read_message(p) print(f"parsed message {n} times ({n / DURATION}/s) " f"({n * size / DURATION:.2f}MB/s)")
def test_format_unknown_type(): with raises(TypeError): format_message(b'/test', values=[object])
def test_format_wrong_types(): with raises(TypeError): format_message(b'/test', values=[u'test'])
def tests_format_message_null_terminated_address(): for message in message_1, message_2: source, msg, result = message source = source[0] + b'\0', source[1] msg = struct.pack('>%iB' % len(msg), *msg) assert format_message(*source)[0] == msg
def tests_format_message(): for message in message_1, message_2: source, msg, result = message msg = struct.pack('>%iB' % len(msg), *msg) assert format_message(*source)[0] == msg
def test_read_message_wrong_address(): msg, stat = format_message(b'test', []) with raises(ValueError) as e: address, tags, values, size = read_message(msg) assert "doesn't start with a '/'" in str(e)