Skip to content

Commit 145fb68

Browse files
committed
feat: added fatal error handler and interruption; RNG has to be disabled temporarily
1 parent 8f030b7 commit 145fb68

File tree

7 files changed

+147
-41
lines changed

7 files changed

+147
-41
lines changed

poetry.lock

Lines changed: 55 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ packages = [
1010
[tool.poetry.dependencies]
1111
python = "^3.10"
1212
numpy = "^1.23.3"
13+
pycapi = "^0.82.1"
1314

1415
[tool.poetry.dev-dependencies]
1516
pytest = "^7.2.1"

src/codegen/internal_lib.py.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# fmt: off
88

9-
from ctypes import cdll, c_char_p, c_double, c_int, c_void_p, POINTER
9+
from ctypes import cdll, c_bool, c_char_p, c_double, c_int, c_void_p, POINTER
1010
from ctypes.util import find_library
1111

1212
from .errors import handle_igraph_error_t
@@ -36,9 +36,11 @@ from .types import (
3636
igraph_arpack_options_t,
3737
igraph_bliss_info_t,
3838
igraph_error_handler_t,
39+
igraph_fatal_handler_t,
3940
igraph_hrg_t,
4041
igraph_layout_drl_options_t,
4142
igraph_maxflow_stats_t,
43+
igraph_interruption_handler_t,
4244
igraph_isocompat_t,
4345
igraph_plfit_result_t,
4446
igraph_rng_t,
@@ -382,11 +384,19 @@ igraph_destroy = _lib.igraph_destroy
382384
igraph_destroy.restype = None
383385
igraph_destroy.argtypes = [POINTER(igraph_t)]
384386

385-
# Error handling
387+
# Error handling and interruptions
386388

387389
igraph_set_error_handler = _lib.igraph_set_error_handler
388390
igraph_set_error_handler.restype = igraph_error_handler_t
389391
igraph_set_error_handler.argtypes = [igraph_error_handler_t]
390392

393+
igraph_set_fatal_handler = _lib.igraph_set_fatal_handler
394+
igraph_set_fatal_handler.restype = igraph_fatal_handler_t
395+
igraph_set_fatal_handler.argtypes = [igraph_fatal_handler_t]
396+
397+
igraph_set_interruption_handler = _lib.igraph_set_interruption_handler
398+
igraph_set_interruption_handler.restype = igraph_interruption_handler_t
399+
igraph_set_interruption_handler.argtypes = [igraph_interruption_handler_t]
400+
391401
# The rest of this file is generated by Stimulus
392402

src/igraph_ctypes/_internal/errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ def handle_igraph_error_t(code: igraph_error_t) -> None:
1111
error_state = _get_last_error_state()
1212
if error_state:
1313
error_state.raise_error()
14+
elif code == 13: # IGRAPH_INTERRUPTED; does not call error handler
15+
raise KeyboardInterrupt
1416
else:
1517
raise RuntimeError(f"igraph returned error code {code}")

src/igraph_ctypes/_internal/lib.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
igraph_arpack_options_t,
3737
igraph_bliss_info_t,
3838
igraph_error_handler_t,
39+
igraph_fatal_handler_t,
3940
igraph_hrg_t,
4041
igraph_layout_drl_options_t,
4142
igraph_maxflow_stats_t,
43+
igraph_interruption_handler_t,
4244
igraph_isocompat_t,
4345
igraph_plfit_result_t,
4446
igraph_rng_t,
@@ -382,12 +384,20 @@ def _load_igraph_c_library():
382384
igraph_destroy.restype = None
383385
igraph_destroy.argtypes = [POINTER(igraph_t)]
384386

385-
# Error handling
387+
# Error handling and interruptions
386388

387389
igraph_set_error_handler = _lib.igraph_set_error_handler
388390
igraph_set_error_handler.restype = igraph_error_handler_t
389391
igraph_set_error_handler.argtypes = [igraph_error_handler_t]
390392

393+
igraph_set_fatal_handler = _lib.igraph_set_fatal_handler
394+
igraph_set_fatal_handler.restype = igraph_fatal_handler_t
395+
igraph_set_fatal_handler.argtypes = [igraph_fatal_handler_t]
396+
397+
igraph_set_interruption_handler = _lib.igraph_set_interruption_handler
398+
igraph_set_interruption_handler.restype = igraph_interruption_handler_t
399+
igraph_set_interruption_handler.argtypes = [igraph_interruption_handler_t]
400+
391401
# The rest of this file is generated by Stimulus
392402

393403
# Set up aliases for all enum types

0 commit comments

Comments
 (0)