Skip to content

Commit 0c18eb5

Browse files
committed
can't figure out this set_file_handler CFUNCTYPE error out. Going to comment out and fix at a later date
1 parent 5be64ad commit 0c18eb5

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

PyPI/Package/src/webui/webui.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,13 @@ def __init__(self, window_id: Optional[int] = None):
601601

602602
# Dispatch function conversion to C bind for binding functions
603603
callback_function_type = CFUNCTYPE(None, POINTER(_raw.WebuiEventT))
604-
self._dispatcher_cfunc = callback_function_type(self._make_dispatcher())
604+
self._dispatcher_cb = callback_function_type(self._make_dispatcher())
605605

606606
# Dict to keep track of bound functions: {bind_id: python_function}
607607
self._cb_func_list: dict = {}
608608

609609
# gets used for both filehandler and filehandler_window, should wipe out the other just how it does in C
610-
self._file_handler_cb = None
610+
self._file_handler_cb: _raw.FILE_HANDLER_CB = None
611611
self._buffers = []
612612

613613
# -- dispatcher for function bindings -----------
@@ -651,7 +651,7 @@ def my_function(event: Event):
651651
print(f"Function bound with ID: {bind_id}")
652652
"""
653653
element_c = element.encode('utf-8') if element else None
654-
bind_id = _raw.webui_bind(c_size_t(self._window), element_c, self._dispatcher_cfunc)
654+
bind_id = _raw.webui_bind(c_size_t(self._window), element_c, self._dispatcher_cb)
655655
self._cb_func_list[bind_id] = func
656656
return bind_id
657657

@@ -893,31 +893,30 @@ def my_handler(filename: str) -> str:
893893
894894
my_window.set_file_handler(my_handler)
895895
"""
896+
def _internal_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
897+
"""
898+
Internal C callback that matches the signature required by webui_set_file_handler_window.
899+
"""
900+
# Decode the incoming filename from C
901+
filename = filename_ptr.decode('utf-8') if filename_ptr else ""
896902

897-
# Define the internal C callback matching the C function signature.
898-
def c_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p:
899-
# Convert the C string (if any) to a Python string.
900-
fname = filename_ptr.decode('utf-8') if filename_ptr else ""
901-
# Call the user-supplied Python handler.
902-
response = handler(fname)
903-
if response is None:
904-
response_bytes = b""
905-
else:
906-
response_bytes = response.encode("utf-8")
907-
# Create a ctypes buffer to hold the response bytes.
903+
# Call the Python-level handler to get the HTTP response
904+
response_bytes = handler(filename).encode("utf-8")
905+
906+
# Create a ctypes buffer from the Python bytes; this buffer must remain alive
907+
# at least until WebUI is done with it.
908908
buf = create_string_buffer(response_bytes)
909-
# Save the buffer reference to keep it alive.
910-
self._buffers.append(buf)
911-
# Set the output length value.
909+
910+
# Set the length (the int* that C expects)
912911
length_ptr[0] = len(response_bytes)
913-
# Return the pointer to the buffer.
912+
913+
# Return a pointer (void*) to the buffer
914914
return cast(buf, c_void_p)
915915

916-
# Wrap our Python callback with our CFUNCTYPE.
917-
FILE_HANDLER_CB = CFUNCTYPE(c_void_p, c_char_p, POINTER(c_int))
918-
self._file_handler_cb = FILE_HANDLER_CB(c_file_handler)
919-
# Now call the C function with our window handle and callback.
920-
_raw.webui_set_file_handler(c_size_t(self._window), FILE_HANDLER_CB(c_file_handler))
916+
# Keep a reference so it doesn't get garbage collected
917+
self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
918+
_raw.webui_set_file_handler_window(c_size_t(self._window), _raw.FILE_HANDLER_CB(self._file_handler_cb))
919+
921920

922921
# -- set_file_handler_window --------------------
923922
def set_file_handler_window(self, handler: Callable[[int, str], Optional[str]]) -> None:
@@ -969,9 +968,9 @@ def _internal_file_handler(window_id: c_size_t, filename_ptr: c_char_p, length_p
969968
return cast(buf, c_void_p)
970969

971970
# Keep a reference so it doesn't get garbage collected
972-
self._file_handler_cfunc = filehandler_window_callback(_internal_file_handler)
971+
self._file_handler_cb = filehandler_window_callback(_internal_file_handler)
973972

974-
_raw.webui_set_file_handler_window(c_size_t(self._window), self._file_handler_cfunc)
973+
_raw.webui_set_file_handler_window(c_size_t(self._window), self._file_handler_cb)
975974

976975
# -- is_shown -----------------------------------
977976
def is_shown(self) -> bool:

PyPI/Package/src/webui/webui_bindings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ class WebuiEventT(Structure):
661661
C Signature:
662662
WEBUI_EXPORT void webui_set_file_handler(size_t window, const void* (*handler)(const char* filename, int* length));
663663
"""
664+
FILE_HANDLER_CB = CFUNCTYPE(c_void_p, c_char_p, POINTER(c_int))
664665
webui_set_file_handler.argtypes = [
665666
c_size_t, # size_t window
666-
CFUNCTYPE(c_void_p, c_char_p, POINTER(c_int)) # const void* (*handler)(const char* filename, int* length)
667+
FILE_HANDLER_CB
667668
]
668669
webui_set_file_handler.restype = None
669670

examples/serve_folder/main.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
from __future__ import annotations
2+
13
# --[ Debugging and Development Test ]-----------
24
# > pip uninstall webui2
35
import sys
4-
5-
from typing_extensions import Optional
6-
76
sys.path.append('../../PyPI/Package/src/webui')
87
import webui
98

109
# Install WebUI
1110
# pip install --upgrade webui2
1211
# from webui import webui
13-
12+
from typing import Optional
1413

1514
my_window = webui.Window(1)
1615
my_second_window = webui.Window(2)
17-
count: int = 0
1816

1917

2018
def exit_app(e : webui.Event):
@@ -75,14 +73,14 @@ def my_file_handler(filename: str) -> Optional[str]:
7573
)
7674
elif filename == "/dynamic.html":
7775
# Dynamic file example
76+
my_file_handler.count += 1
7877
body = (
7978
"<html>"
8079
" This is a dynamic file content example. <br>"
81-
f" Count: {count} <a href=\"dynamic.html\">[Refresh]</a><br>"
80+
f" Count: {my_file_handler.count} <a href=\"dynamic.html\">[Refresh]</a><br>"
8281
" <script src=\"webui.js\"></script>"
8382
"</html>"
8483
)
85-
count += 1
8684
header_and_body = (
8785
"HTTP/1.1 200 OK\r\n"
8886
"Content-Type: text/html\r\n"
@@ -96,6 +94,7 @@ def my_file_handler(filename: str) -> Optional[str]:
9694
# looks for the file locally.
9795
return None
9896

97+
my_file_handler.count: int = 0
9998

10099
def main():
101100
# windows were made globally already at the top.

0 commit comments

Comments
 (0)