From 804204db7cf1e8a28defd359d17fa72949e13eff Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 31 Aug 2026 23:05:16 +0200 Subject: Implement with ptrCast to make it more transparent. --- src/error.zig | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/error.zig (limited to 'src/error.zig') diff --git a/src/error.zig b/src/error.zig new file mode 100644 index 0000000..21002f2 --- /dev/null +++ b/src/error.zig @@ -0,0 +1,126 @@ +const std = @import("std"); +const c = @import("c"); + +pub const Exception = enum { + + const Self = @This(); + + base_exception, + base_exception_group, + exception, + arithmetic_error, + assertion_error, + attribute_error, + blocking_io_error, + broken_pipe_error, + buffer_error, + child_process_error, + connection_aborted_error, + connection_error, + connection_refused_error, + connection_reset_error, + eof_error, + file_exists_error, + file_not_found_error, + floating_point_error, + generator_exit, + import_error, + indentation_error, + index_error, + interrupted_error, + is_a_directory_error, + key_error, + keyboard_interrupt, + lookup_error, + memory_error, + module_not_found_error, + name_error, + not_a_directory_error, + not_implemented_error, + os_error, + overflow_error, + permission_error, + process_lookup_error, + python_finalization_error, + recursion_error, + reference_error, + runtime_error, + stop_async_iteration, + stop_iteration, + syntax_error, + system_error, + system_exit, + tab_error, + timeout_error, + type_error, + unbound_local_error, + unicode_decode_error, + unicode_encode_error, + unicode_error, + unicode_translate_error, + value_error, + zero_division_error, + unknown_error, + + pub fn concreteException() ?Self { + const err = c.PyErr_Occurred() orelse return null; + + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_BaseException) == 1) return .base_exception; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_BaseExceptionGroup) == 1) return .base_exception_group; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_Exception) == 1) return .exception; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ArithmeticError) == 1) return .arithmetic_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_AssertionError) == 1) return .assertion_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_AttributeError) == 1) return .attribute_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_BlockingIOError) == 1) return .blocking_io_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_BrokenPipeError) == 1) return .broken_pipe_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_BufferError) == 1) return .buffer_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ChildProcessError) == 1) return .child_process_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ConnectionAbortedError) == 1) return .connection_aborted_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ConnectionError) == 1) return .connection_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ConnectionRefusedError) == 1) return .connection_refused_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ConnectionResetError) == 1) return .connection_reset_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_EOFError) == 1) return .e_o_f_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_FileExistsError) == 1) return .file_exists_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_FileNotFoundError) == 1) return .file_not_found_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_FloatingPointError) == 1) return .floating_point_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_GeneratorExit) == 1) return .generator_exit; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ImportError) == 1) return .import_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_IndentationError) == 1) return .indentation_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_IndexError) == 1) return .index_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_InterruptedError) == 1) return .interrupted_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_IsADirectoryError) == 1) return .is_a_directory_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_KeyError) == 1) return .key_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_KeyboardInterrupt) == 1) return .keyboard_interrupt; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_LookupError) == 1) return .lookup_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_MemoryError) == 1) return .memory_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ModuleNotFoundError) == 1) return .module_not_found_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_NameError) == 1) return .name_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_NotADirectoryError) == 1) return .not_a_directory_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_NotImplementedError) == 1) return .not_implemented_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_OSError) == 1) return .o_s_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_OverflowError) == 1) return .overflow_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_PermissionError) == 1) return .permission_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ProcessLookupError) == 1) return .process_lookup_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_PythonFinalizationError) == 1) return .python_finalization_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_RecursionError) == 1) return .recursion_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ReferenceError) == 1) return .reference_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_RuntimeError) == 1) return .runtime_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_StopAsyncIteration) == 1) return .stop_async_iteration; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_StopIteration) == 1) return .stop_iteration; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_SyntaxError) == 1) return .syntax_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_SystemError) == 1) return .system_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_SystemExit) == 1) return .system_exit; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_TabError) == 1) return .tab_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_TimeoutError) == 1) return .timeout_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_TypeError) == 1) return .type_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_UnboundLocalError) == 1) return .unbound_local_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_UnicodeDecodeError) == 1) return .unicode_decode_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_UnicodeEncodeError) == 1) return .unicode_encode_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_UnicodeError) == 1) return .unicode_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_UnicodeTranslateError) == 1) return .unicode_translate_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ValueError) == 1) return .value_error; + if (c.PyErr_GivenExceptionMatches(err, c.PyErr_ZeroDivisionError) == 1) return .zero_division_error; + + return .unknown_error; + } +}; -- cgit v1.2.3-70-g09d2