Skip to content

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

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

Activity

  1. added a commit that references this issue on Jun 12, 2024
  2. vstinner commented on Jun 12, 2024

    @vstinner
    MemberAuthor
  3. vstinner commented on Jun 12, 2024

    @vstinner
    MemberAuthor

    See also #117031 and #117032: "Support more integer types in PyMemberDef".

    Add support for standard C and Posix integer types like Py_T_UINT32, Py_T_PTRDIFF, Py_T_OFF and Py_T_PID.
    Add Py_T_SSIZE as alias of Py_T_PYSSIZET.

  4. serhiy-storchaka commented on Jun 12, 2024

    @serhiy-storchaka
    Member

    This conflicts with the interface of other PyLong_As* functions. If you want to use different convention, it is better to use different names.

    There are some design questions:

    • Should all these functions be separate functions or aliases of other functions with the same size and signness?
    • Which functions should call __index__() (and therefore release the GIL) and which should be PyLong only?
    • How to handle negative values for unsigned types?
    • How to handle overflow?
    • It is worth to make these function compatible with the O& format unit in PyArg_Parse -- returning 1 for success and 0 for failure.

    See also #117031.

  5. vstinner commented on Jun 12, 2024

    @vstinner
    MemberAuthor

    This conflicts with the interface of other PyLong_As* functions. If you want to use different convention, it is better to use different names.

    We can use PyLong_To*() convention. What do you think?

    How to handle negative values for unsigned types?

    They must fail with ValueError (or OverflowError).

    How to handle overflow?

    Raise a OverflowError.

    It is worth to make these function compatible with the O& format unit in PyArg_Parse -- returning 1 for success and 0 for failure.

    New API should follow new guidelines: 0 on success, -1 on error: https://devguide.python.org/developer-workflow/c-api/index.html#guidelines-for-expanding-changing-the-public-api

  6. GalaxySnail commented on Jun 12, 2024

    @GalaxySnail
    Contributor
    • I prefer UInt to Uint since there are two words: Unsigned INTeger.

    bikeshedding: We have PyLong_AsSsize_t instead of PyLong_AsSSize_t for ssize_t/Py_ssize_t, so I personally prefer Uint to UInt because of consistency.

  7. serhiy-storchaka commented on Jun 12, 2024

    @serhiy-storchaka
    Member

    We can use PyLong_To*() convention. What do you think?

    I was going to suggest the same. Or include Convert in the name.

    New API should follow new guidelines: 0 on success, -1 on error: https://devguide.python.org/developer-workflow/c-api/index.html#guidelines-for-expanding-changing-the-public-api

    Then we will end with two sets of functions with the same signature, but opposite meaning of the returned value. I am going to propose to make PyArg_Parse-compatible converters public.

  8. changed the title [-][C API] Add PyLong_FromInt64() and PyLong_AsInt64()[/-] [+][C API] Add PyLong_FromInt64() and PyLong_ToInt64()[/+] on Jun 17, 2024
  9. vstinner commented on Jun 17, 2024

    @vstinner
    MemberAuthor

    I renamed the functions to PyLong_ToInt64() and PyLong_ToUInt64().

  10. added 5 commits that reference this issue on Jun 19, 2024
  11. vstinner commented on Jun 20, 2024

    @vstinner
    MemberAuthor

    @serhiy-storchaka:

    There are some design questions: (...)

    You can now check #120390 implementation, especially the test suite, to get answers to your questions.

  12. vstinner commented on Jun 24, 2024

    @vstinner
    MemberAuthor

    I created capi-workgroup/decisions#32 "Add PyLong_FromInt64() and PyLong_ToInt64()" in the C API WG Decisions project.

  13. added a commit that references this issue on Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions