objwatch.event_handls module

class objwatch.event_handls.EventHandls(output_xml: str | None = None)[source]

Bases: object

Handles various events for ObjWatch, including function execution and variable updates. Optionally saves the events in an XML format.

__init__(output_xml: str | None = None) None[source]

Initialize the EventHandls with optional XML output configuration.

Parameters:

output_xml (Optional[str]) – Path to the XML file for writing structured logs.

handle_run(lineno: int, func_info: dict, abc_wrapper: Any | None, call_depth: int, index_info: str) None[source]

Handle the ‘run’ event indicating the start of a function or method execution.

handle_end(lineno: int, func_info: dict, abc_wrapper: Any | None, call_depth: int, index_info: str, result: Any) None[source]

Handle the ‘end’ event indicating the end of a function or method execution.

handle_upd(lineno: int, class_name: str, key: str, old_value: Any, current_value: Any, call_depth: int, index_info: str, abc_wrapper: Any | None = None) None[source]

Handle the ‘upd’ event representing the creation or updating of a variable.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the variable.

  • key (str) – Variable name.

  • old_value (Any) – Previous value of the variable.

  • current_value (Any) – New value of the variable.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

  • abc_wrapper (Optional[Any]) – Custom wrapper for additional processing.

handle_apd(lineno: int, class_name: str, key: str, value_type: type, old_value_len: int | None, current_value_len: int | None, call_depth: int, index_info: str) None[source]

Handle the ‘apd’ event denoting the addition of elements to data structures.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the data structure.

  • key (str) – Name of the data structure.

  • value_type (type) – Type of the elements being added.

  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

handle_pop(lineno: int, class_name: str, key: str, value_type: type, old_value_len: int | None, current_value_len: int | None, call_depth: int, index_info: str) None[source]

Handle the ‘pop’ event marking the removal of elements from data structures.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the data structure.

  • key (str) – Name of the data structure.

  • value_type (type) – Type of the elements being removed.

  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

determine_change_type(old_value_len: int, current_value_len: int) EventType | None[source]

Determine the type of change based on the difference in lengths.

Parameters:
  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

Returns:

The determined event type (APD or POP).

Return type:

EventType

static format_sequence(seq: Any, max_elements: int = 3, func: LambdaType | None = None) str[source]

Format a sequence to display a limited number of elements.

Parameters:
  • seq (Any) – The sequence to format.

  • max_elements (int) – Maximum number of elements to display.

  • func (Optional[FunctionType]) – Optional function to process elements.

Returns:

The formatted sequence string.

Return type:

str

static _format_value(value: Any) str[source]

Format individual values for the ‘upd’ event when no wrapper is provided.

Parameters:

value (Any) – The value to format.

Returns:

The formatted value string.

Return type:

str

save_xml() None[source]

Save the accumulated events to an XML file upon program exit.

signal_handler(signum, frame)[source]

Signal handler for abnormal program termination. Calls save_xml when a termination signal is received.

Parameters:
  • signum (int) – The signal number.

  • frame (frame) – The current stack frame.