Skip to content

BYTES directive leads to exception when the bytes aren't UTF-8 decodeable #227

Description

@BT8298

An exception is thrown anytime a string appears in a BYTES directive (#220) that is not utf-8 decodeable. Using the dialogue pair

- q: "BYTES(\xaa\x55\x00\x02\x08\x01\xf4)"
  r: "BYTES(\xaa\x55\x00\x02\x08\x01\xf4)"

my project's test suite fails with the traceback

Traceback (most recent call last):
  File "/home/user/python-project/tests/test_tpi.py", line 20, in setUp
    self.tpi = package.aux_control.TPI_RF_Generator("ASRL1::INSTR")
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/user/python-project/package/aux_control.py", line 876, in __init__
    if self._query(TPI_CMD["set_user_control"]) != TPI_CMD["set_user_control"]:
       ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-project/package/aux_control.py", line 1093, in _query
    self._write(command, value)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/user/python-project/package/aux_control.py", line 919, in _write
    bytes_written = self._instrument.write_raw(packet)
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa/resources/messagebase
d.py", line 173, in write_raw
    return self.visalib.write(self.session, message)[0]
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa_sim/highlevel.py", li
ne 290, in write
    return sess.write(data)  # type: ignore
           ~~~~~~~~~~^^^^^^
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa_sim/sessions/serial.p
y", line 59, in write
    self.device.write(val)
    ~~~~~~~~~~~~~~~~~^^^^^
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa_sim/devices.py", line
 317, in write
    response = self._match(message)
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa_sim/devices.py", line
 423, in _match
    response = self._match_setters(query)
  File "/home/user/python-project/.venv/lib/python3.14/site-packages/pyvisa_sim/component.py", li
ne 418, in _match_setters
    q = query.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte

I was able to solve this by replacing a few instances of utf-8 codec in the source with latin-1. However, this limits the dialogue definitions and properties to the latin-1 codec only, preventing simulation of devices that use characters outside of latin-1. This is why I open the issue: to ask whether this change could negatively impact some users. If anyone knows of devices relevant to PyVISA which use utf-8 strings in the I/O protocol, please mention it in a reply. Also, feedback on alternate implementation of the fix are welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions