Skip to content

Make IntoPyObject fallible for all integer conversions #6039

@chirizxc

Description

@chirizxc

See: #6016 (comment), #6016 (comment)

Currently IntoPyObject for all primitive integer types (u8..u128, i8..i128, usize, isize,
their NonZero<…> wrappers, and & references) uses type Error = Infallible. This means the
underlying FFI calls — PyLong_FromLong, PyLong_FromLongLong, PyLong_FromNativeBytes,
PyLongWriter_Create / PyLongWriter_Finishpanic on failure (NULL return) via
assume_owned, rather than propagating a PyErr.

These failures are very rare in practice (allocation failure, interpreter shutdown, MemoryError),
but they can happen, and panicking across an FFI boundary is the worst possible reaction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions