Convert any ctypes pointer-like value to a raw integer. macOS ctypes has a long-standing quirk: passing a `POINTER(struct)` instance directly to a `c_void_p`-typed parameter sometimes yields a corrupted value at the C boundary (the bug is reproducible with `restype = POINTER(...)` c
(p)
| 38 | |
| 39 | |
| 40 | def _ptr_as_int(p) -> int: |
| 41 | """Convert any ctypes pointer-like value to a raw integer. |
| 42 | |
| 43 | macOS ctypes has a long-standing quirk: passing a `POINTER(struct)` |
| 44 | instance directly to a `c_void_p`-typed parameter sometimes yields |
| 45 | a corrupted value at the C boundary (the bug is reproducible with |
| 46 | `restype = POINTER(...)` chained into another call). Using the raw |
| 47 | integer pointer value side-steps the issue entirely. |
| 48 | """ |
| 49 | if p is None: |
| 50 | return 0 |
| 51 | if isinstance(p, int): |
| 52 | return p |
| 53 | return ctypes.cast(p, c_void_p).value or 0 |
| 54 | |
| 55 | |
| 56 | # ============================================================================ |
no outgoing calls
no test coverage detected