@@ -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 :
0 commit comments