Tolerate stream metadata that is not valid UTF-8 - #117
Open
adityasingh2400 wants to merge 1 commit into
Open
Conversation
Every string returned by liblsl was decoded with a strict UTF-8 decode. The hostname is read from the operating system, and Windows returns it in the active ANSI code page, so a machine whose name contains accented characters made StreamInfo.hostname() and StreamInfo.as_xml() raise UnicodeDecodeError. Add a _to_str helper that tries UTF-8 and falls back to latin-1, which cannot fail and preserves the original bytes, and use it for the metadata accessors in info.py. Fixes labstreaminglayer#69
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StreamInfo.hostname()raisesUnicodeDecodeErroron Windows machines whose hostname contains accented characters, as reported in #69.as_xml()fails in the same way because the hostname is embedded in the info document, so a resolver loop can die simply from discovering a stream that lives on such a machine.The root cause is that every string coming back from liblsl was run through a strict
bytes.decode("utf-8"). That is safe for values pylsl itself wrote, since pylsl encodes them as UTF-8 on the way in, but the hostname is read from the operating system and Windows hands it back in the active ANSI code page. The same applies to any metadata written by a non-Python LSL application running on such a machine.This adds a small
_to_strhelper inpylsl.utilthat tries UTF-8 first and falls back to latin-1. The fallback cannot fail, keeps every original byte so a caller can re-encode and decode with the correct code page if it knows better, and renders the common accented-Latin case correctly, which is the fix the reporter validated in the issue thread. The helper is applied to the metadata accessors ininfo.py:name,type,source_id,uid,session_id,hostname,as_xml, and theXMLElementname,value, andchild_valuegetters. The sample data path ininlet.pyis deliberately left alone, since raw bytes incf_stringchannels are a separate discussion in #54.Tested with a new
test/test_encoding.pythat exercises the helper directly and simulates a non-UTF-8 platform by monkeypatching the relevant liblsl calls to return cp1252 bytes. With the tests in place butinfo.pyreverted, three of them fail with exactly the error from the issue,UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 7. With the change applied the whole suite passes, 18 tests against liblsl 1.17.7 on macOS with Python 3.14.ruff checkandruff format --checkreport nothing new relative tomain.Fixes #69