Skip to content

[C API] Add PyLong_FromInt64() and PyLong_ToInt64() #120389

Closed
@vstinner

Description

@vstinner

Feature or enhancement

I propose to add functions to convert <stdint.h> integers to/from Python int objects:

PyObject* PyLong_FromInt32(int32_t value);
PyObject* PyLong_FromInt64(int64_t value);
PyObject* PyLong_FromUInt32(uint32_t value);
PyObject* PyLong_FromUInt64(uint64_t value);

int PyLong_ToInt32(PyObject *obj, int32_t *value);
int PyLong_ToInt64(PyObject *obj, int64_t *value);
int PyLong_ToUInt32(PyObject *obj, uint32_t *value);
int PyLong_ToUInt64(PyObject *obj, uint64_t *value);

Notes:

  • I prefer to limit the API to 4 types for now: int32/64_t and uint32/64_t. Later, we can discuss add more types, but let's start with the most common ones. (UPDATE: I removed 8-bit and 16-bit types.)
  • I prefer UInt to Uint since there are two words: Unsigned INTeger.
  • To functions don't return the result, but a status: 0 on success, -1 on error (with an exception set). It's to solve the C API Problem #1: "Ambiguous return values". PyLong_AsLong() returns -1 on success and on error (with an exception set).

Related discussion: Avoid C-specific Types.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions