-
Notifications
You must be signed in to change notification settings - Fork 19
send_raw function fix #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…end_raw_client functions, and updated version text to 2.5.2
fixed version number
|
Thank you for the update. I think @DocQuantic mentioned that my def send_raw(self, function: str, raw: any) -> None:
"""
Safely send raw data to the UI for all clients.
This function sends a raw data buffer to a JavaScript function in the UI.
The JavaScript function should be capable of handling raw binary data.
Args:
function (str): The JavaScript function that will receive the raw data.
data (raw): The raw data buffer.
Raises:
ValueError: If `data` is `None`.
Returns:
None
Example:
send_raw("myJavaScriptFunc", bytearray([0x0A, 0x0B, 0x0C]))
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
# myJavaScriptFunc(myUint8Array)
"""
if raw is None:
raise ValueError("Invalid pointer: Cannot send a null pointer.")
_raw.webui_send_raw(
c_size_t(self._window),
c_char_p(function.encode("utf-8")),
cast(raw, c_void_p),
len(raw)
) |
|
Also, example |
|
@AlbertShown May have missed. I had similar issues with your implementation. The issue was that the memoryview buffer was not writable causing an error. I fixed it with an extra check. I confirmed it only seem to error out when normal bytes are passed in. The function looks like this now: # -- send_raw -----------------------------------
def send_raw(self, function: str, data: Union[bytes, bytearray, memoryview, array.array]) -> None:
"""
Safely send raw data to the UI for all clients.
This function sends a raw data buffer to a JavaScript function in the UI.
The JavaScript function should be capable of handling raw binary data.
Args:
function (str): The JavaScript function that will receive the raw data.
data (Union[bytes, bytearray, memoryview, array.array]): The raw data buffer.
Raises:
ValueError: If `data` is `None` or empty.
Returns:
None
Example:
my_window.send_raw("myJavaScriptFunc", bytearray([0x01, 0x0A, 0xFF]))
# Sends 3 bytes of raw data to the JavaScript function `myJavaScriptFunc`.
"""
if data is None or len(data) == 0:
raise ValueError("Data must not be None or empty.")
# Ensure data is a memoryview for uniformity
if not isinstance(data, memoryview):
data = memoryview(data)
# Ensure that data is a writable copy to obtain void pointer,
# from_buffer will throw error if the buffer passed in is not writable.
if data.readonly:
data = memoryview(bytearray(data))
# Obtain a c_void_p pointer to the data buffer
ptr = c_void_p(addressof(c_char.from_buffer(data)))
# Determine the size of the data
size = len(data)
# Call the underlying function to send the data
_raw.webui_send_raw(
c_size_t(self._window),
c_char_p(function.encode("utf-8")),
ptr,
c_size_t(size)
)this section here I added should fix the issue: # Ensure that data is a writable copy to obtain void pointer,
# from_buffer will throw error if the buffer passed in is not writable.
if data.readonly:
data = memoryview(bytearray(data))And just to be sure, I tested @DocQuantic example that they posted and confirmed it work with this implementation of send_raw. I added the same fix to send_raw_client as well. |
I'll change the directory to that. I agree, it makes more sense that way. |
|
Sounds good then, thank you for the fix and improvement. |
fixed send_raw and send_raw_client issue.