Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/cabledyn/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ def eval_channel(model: CableDyn, token: str) -> float:
"""Evaluate one OUTPUTS token (the caller holds the model lock)."""
if not isinstance(token, str):
raise TypeError(f"channel token must be a string; got {type(token).__name__}")
if "\0" in token:
# the library ends a token at its first NUL, so "FairTen1\0x" would read as FairTen1
raise ValueError(f"channel token must not contain a NUL character; got {token!r}")
try:
raw = token.strip().encode("ascii")
except UnicodeEncodeError:
Expand Down
10 changes: 10 additions & 0 deletions python/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ def test_channel_tokens_must_be_ascii(tmp_path):
model.channel("FairTen\u00b91")


@pytest.mark.parametrize("token", ["FairTen1\x00", "FairTen1\x00x", "\x00FairTen1"])
def test_channel_tokens_must_not_hold_a_nul(tmp_path, token):
# the library reads a token up to its first NUL: "FairTen1\0x" would read as FairTen1
with CableDyn(_held_chain(tmp_path)) as model:
with pytest.raises(ValueError, match="NUL"):
model.channel(token)
with pytest.raises(ValueError, match="NUL"):
model.channels(["FairTen1", token])


def test_zero_length_rod_axis_is_a_value_error(tmp_path, monkeypatch):
deck = _write(tmp_path, "rod.dat", _ROD_DECK)
with CableDyn(deck) as model:
Expand Down
Loading