Skip to content

Yet another possible fix for #528#530

Merged
jonasmalacofilho merged 3 commits intopyusb:masterfrom
jonasmalacofilho:yet-another-fix-for-528
Jan 8, 2025
Merged

Yet another possible fix for #528#530
jonasmalacofilho merged 3 commits intopyusb:masterfrom
jonasmalacofilho:yet-another-fix-for-528

Conversation

@jonasmalacofilho
Copy link
Copy Markdown
Member

@jonasmalacofilho jonasmalacofilho commented Jan 7, 2025

(See individual commit messages).

Fixes: #528
Closes: #527
Closes: #529

`Device.ctrl_transfer` depends on `create_buffer` raising a `TypeError`
if its parameter is not a valid length. In particular, if an array is
passed in, `create_buffer` must *not* return a copy of it.

Commit 3ea79b0 accidentally broke this and, consequently, any calls
to `ctrl_transfer` passing in a buffer. Thanks @kauwua for reporting the
issue and discussing the potential solutions.

Note that `length * b'\x00'` is actually faster than `bytes(length)`
(tested with Python 3.13 on x86_64).

Fixes: pyusb#528
Fixes: 3ea79b0 ("util: remove left over python 2 compatibility workaround")
While discussing pyusb#527, @kauwua made the valid point that we could
optimize for the case where the user has passed in a byte `array`.

In that spirit, prioritize the no-op conversion case, but also accept
the parameter to be a length for a new array in preparation for
replacing the conditional in `Device.ctrl_transfer`.

Related: pyusb#528
Optimize for the case where `data_or_wLength` is already a byte `array`,
as suggested by @kauwua in pyusb#527.

Related: pyusb#528
@jonasmalacofilho jonasmalacofilho force-pushed the yet-another-fix-for-528 branch from 913546d to 7000919 Compare January 7, 2025 21:14
@kauwua
Copy link
Copy Markdown

kauwua commented Jan 8, 2025

Works for me

@jonasmalacofilho
Copy link
Copy Markdown
Member Author

Thanks!

@jonasmalacofilho jonasmalacofilho merged commit e920187 into pyusb:master Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in v1.3.0: ctrl_transfer function in utils/core.py does not re-use the array passed in the data_or_wLength argument

3 participants