Description
As per the discussion in capi-workgroup/decisions#32 (and some other bugs here that I haven't bothered to look up), having int conversion functions call back into Python to evaluate __index__
can be problematic or unexpected (Python code may cause the GIL to be released, which could break threading invariants in the calling code).
We should disable __index__
handling by default in PyLong_AsNativeBytes
and provide a flag to reenable it. In general, calling __index__
is only really useful on user-provided arguments, which are easily distinguishable from "private" variables in the caller's code.
This allows the caller to omit PyLong_Check
before calling the conversion function (if they need to guarantee they retain the thread), or to provide a single flag if they will allow it. They'll discover they need the flag due to a TypeError
, which is safe, rather than a crash or native threading related issue, which is unsafe.