|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from .functions import igraph_strerror |
| 4 | +from .lib import igraph_set_error_handler |
1 | 5 | from .rng import NumPyRNG |
| 6 | +from .types import igraph_error_t, igraph_error_handler_t |
2 | 7 |
|
3 | 8 | __all__ = ("setup_igraph_library",) |
4 | 9 |
|
5 | 10 |
|
6 | | -def setup_igraph_library() -> None: |
7 | | - """Initializes the random number generator of the igraph library. |
| 11 | +@dataclass |
| 12 | +class IgraphErrorState: |
| 13 | + """Dataclass storing the details of the last error message received from |
| 14 | + igraph. |
| 15 | + """ |
8 | 16 |
|
9 | | - This function is called when the ``igraph_ctypes`` module is imported by the user. |
| 17 | + message: bytes = b"" |
| 18 | + filename: bytes = b"" |
| 19 | + line: int = 0 |
| 20 | + error: igraph_error_t = igraph_error_t(0) |
| 21 | + |
| 22 | + def _error_handler( |
| 23 | + self, message: bytes, filename: bytes, line: int, error: igraph_error_t |
| 24 | + ): |
| 25 | + self.message = message |
| 26 | + self.filename = filename |
| 27 | + self.line = line |
| 28 | + self.error = error |
| 29 | + print(repr(self)) |
| 30 | + |
| 31 | + @property |
| 32 | + def has_error(self) -> bool: |
| 33 | + """Returns whether the error state object currently stores an error.""" |
| 34 | + return int(self.error) != 0 |
| 35 | + |
| 36 | + def raise_error(self) -> None: |
| 37 | + """Raises an appropriate Python exception if there is an error stored |
| 38 | + in the state object. No-op otherwise. |
| 39 | + """ |
| 40 | + code = int(self.error) |
| 41 | + |
| 42 | + if code == 0: |
| 43 | + return |
| 44 | + elif code == 12: # IGRAPH_UNIMPLEMENTED |
| 45 | + exc = NotImplementedError |
| 46 | + elif code == 2: # IGRAPH_ENOMEM |
| 47 | + exc = MemoryError |
| 48 | + else: |
| 49 | + exc = RuntimeError |
| 50 | + |
| 51 | + msg = self.message |
| 52 | + if msg and msg[-1] not in b".!?": |
| 53 | + msg = msg + b"." |
| 54 | + |
| 55 | + filename_str = self.filename.decode("utf-8", errors="replace") |
| 56 | + message_str = msg.decode("utf-8", errors="replace") |
| 57 | + error_code_str = igraph_strerror(self.error).decode("utf-8", errors="replace") |
| 58 | + |
| 59 | + raise exc( |
| 60 | + f"Error at {filename_str}:{self.line}: {message_str} -- {error_code_str}" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +_last_error = IgraphErrorState() |
| 65 | + |
| 66 | + |
| 67 | +@igraph_error_handler_t |
| 68 | +def _error_handler(message: bytes, filename: bytes, line: int, error: igraph_error_t): |
| 69 | + global _last_error |
| 70 | + _last_error._error_handler(message, filename, line, error) |
| 71 | + |
| 72 | + |
| 73 | +def _get_last_error_state() -> IgraphErrorState: |
| 74 | + global _last_error |
| 75 | + return _last_error |
| 76 | + |
| 77 | + |
| 78 | +def _setup_error_handler() -> None: |
| 79 | + """Sets up the error handler needed to integrate igraph's error handling |
| 80 | + nicely with Python. |
10 | 81 | """ |
| 82 | + igraph_set_error_handler(_error_handler) |
| 83 | + |
| 84 | + |
| 85 | +def _setup_rng() -> None: |
| 86 | + """Initializes the random number generator of the igraph library.""" |
11 | 87 | from numpy.random import default_rng |
12 | 88 |
|
13 | 89 | NumPyRNG(default_rng()).attach() |
| 90 | + |
| 91 | + |
| 92 | +def setup_igraph_library() -> None: |
| 93 | + """Integrates the facilities of the igraph library with Python. |
| 94 | +
|
| 95 | + This function is called when the ``igraph_ctypes`` module is imported by the user. |
| 96 | + You should not need to call this function directly. |
| 97 | + """ |
| 98 | + _setup_error_handler() |
| 99 | + _setup_rng() |
0 commit comments