Skip to content

Skip __index__ handling in PyLong_AsNativeBytes #121115

Closed
@zooba

Description

@zooba

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.

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.14bugs and security fixes

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions