intellicrack.bridges.parse_helpers
Shared parsing helpers for Intellicrack bridge modules.
This module centralises two recurring patterns from the bridge layer:
safe_int_from_strparses an integer from a string and emits a structured debug log when parsing fails instead of silently swallowing theValueError. This is the canonical replacement for theexcept ValueError: return None / 0 / continue / passidiom that bridge code historically used when reading plugin response payloads, x64dbg address strings, label/comment lists, and similar serialised-integer fields.safe_callinvokes a zero-argument callable and returns a configured default when one of the expected exception types is raised. The exception is logged at debug level with the supplied call-site context. It is the analogue ofsafe_int_from_strfor non-integer parse sites (struct.unpack_frompayload reads, Win32 mitigation policy probes,ctypes.ArgumentErrordecode failures, etc.).
Both helpers log via the intellicrack.core.logging structlog
binding, so every captured failure is observable through the project’s
JSON log aggregation pipeline.
- safe_int_from_str(value, *, base=0, context, default=None)[source]
Parse an integer from a value, logging at debug level on failure.
The helper accepts
intdirectly (returned unchanged unless it is abool, which is rejected to match the historical_coerce_address/_coerce_hex_intsemantics) and any string or bytes-like object thatint(value, base)can consume. Boolean values are treated as invalid because Python’sboolsubclassesintand would otherwise be silently coerced to0or1for fields that expect numeric strings.- Parameters:
value (object) – Raw payload to parse. Accepts
int(returned as-is) orstr/bytes/bytearray(parsed with the requested base). Any other type is treated as a parse failure anddefaultis returned.base (int) – Numeric base passed to
int().0(default) requests auto-detection from a0x/0o/0bprefix, matching the behaviour expected by x64dbg plugin payloads.context (str) – Snake_case identifier of the call site (e.g.
"x64dbg_coerce_address"). Recorded as a structured log field so failures can be correlated to the originating bridge method.default (int | None) – Value to return when parsing fails. Defaults to
None; callers that semantically require a sentinel integer (for example_coerce_addressreturning0) pass it explicitly.
- Returns:
- Parsed integer, or
defaultwhen the value cannot be parsed.
- Parsed integer, or
- Return type:
int | None
- safe_call(func, *, exceptions, context, default)[source]
Call
funcand returndefaulton any of the listed exceptions.The captured exception is logged at debug level with the call-site
contextso silent-swallow failures become observable without raising. Useful for replacingexcept (struct.error, OSError): return <sentinel>blocks that previously discarded failures from struct unpacks, ctypes calls, Win32 policy probes, and similar small-scope operations.- Parameters:
func (Callable[[], T]) – Zero-argument callable that performs the guarded operation. Use a
lambdaorfunctools.partialwhen the underlying API needs arguments.exceptions (type[BaseException] | tuple[type[BaseException], ...]) – Exception class or tuple of exception classes that should be caught and converted to
default. Any other exception propagates to the caller unchanged.context (str) – Snake_case identifier of the call site, recorded as a structured log field on failure.
default (D) – Value to return when one of
exceptionsis raised.
- Returns:
Result of
func()on success, otherwisedefault.- Return type:
T | D