python-hyperscan icon indicating copy to clipboard operation
python-hyperscan copied to clipboard

Handling scan termination from match callback could be cleaner

Open kaiw opened this issue 3 years ago • 0 comments

When calling scan methods, the match_event_handler callback can return a truthy value to indicate that the scan should be stopped. In the current error handling in HANDLE_HYPERSCAN_ERR, this is treated as a regular error and raises a hyperscan.error. While an early termination does feel like something that should show up in a return value, arguably it shouldn't be a regular exception.

As a closely related issue, HANDLE_HYPERSCAN_ERR nicely formats an error string, but discards the error int, which makes it awkward to conditionally handle errors. For example, storing the error int on the exception would allow a workaround to the above issue like:

try:
    hs_db.scan(sample, match_event_handler=on_match)
except hyperscan.error as err:
    if err.code != HS_SCAN_TERMINATED:
        raise

kaiw avatar Jul 12 '22 00:07 kaiw