Skip to content

fix(PythonClient): make byte-array helpers work on NumPy 2 and add offline tests - #50

Merged
donghaiwang merged 1 commit into
OpenHUTB:mainfrom
xy18574557330:fix/numpy2-pythonclient-tests
Oct 31, 2026
Merged

donghaiwang merged 1 commit into
OpenHUTB:mainfrom
xy18574557330:fix/numpy2-pythonclient-tests

Conversation

@xy18574557330

Copy link
Copy Markdown
Contributor

Related: #29 — this adds offline unit tests for the Python client; the C++ AirLibUnitTests CI item in #29 remains open.

About

PythonClient/airsim/utils.py parsed raw payloads with np.fromstring(..., dtype). NumPy 2.0 removed the binary mode of fromstring, so on numpy>=2 both public helpers raise immediately:

>>> airsim.string_to_uint8_array(b'\x00\x7f\xff')
ValueError: The binary mode of fromstring is removed, use frombuffer instead
>>> airsim.string_to_float_array(struct.pack('<4f', 1.5, -2.25, 0.0, 42.0))
ValueError: The binary mode of fromstring is removed, use frombuffer instead

Any script that decodes the byte payloads returned by simGetImages / depth data (image_data_uint8, image_data_float) through these helpers breaks on numpy>=2. The fix parses with np.frombuffer(...).copy(), which keeps the returned array writable exactly like the old fromstring result, and keeps the same dtype and error behaviour for mis-sized buffers.

While in the file, write_pfm() no longer relies on garbage collection to close the file (on Windows the handle made the file unreadable until collected), and PythonClient/README.md gains a short "Tests" section so the new suite is discoverable.

How Has This Been Tested?

Windows 11, Python 3.12.14, numpy 2.3.5, msgpack-rpc-python 0.4.1. No Unreal Engine, no simulator and no OpenCV: the tests import airsim.utils only.

cd PythonClient
python -m unittest discover -s tests -v
Ran 10 tests in 0.132s
OK

Coverage:

  • byte payload parsing with uint8 / float32 dtypes, empty payloads and writable results;
  • list_to_2d_float_array() and get_pfm_array() shapes and dtypes;
  • quaternion round trip (to_quaternion -> to_eularian_angles);
  • PFM grayscale and colour round trip, plus the dtype/shape guards in write_pfm().

Before the change the two helpers raise ValueError exactly as shown above; after the change they return the expected arrays and all ten tests pass.

Screenshots (if appropriate)

N/A — no visual output.

np.fromstring lost its binary mode in NumPy 2.0, so
string_to_uint8_array() and string_to_float_array() raise ValueError on
numpy>=2 and any caller decoding simGetImages / depth payloads fails.
Parse with np.frombuffer and keep the writable-array behaviour.

Also close the PFM file explicitly in write_pfm() and add offline unit
tests for the client helpers (no simulator or OpenCV needed).
@donghaiwang
donghaiwang merged commit a846094 into OpenHUTB:main Oct 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants