8000 Add Buffer Protocol support to int.from_bytes · Issue #132108 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Add Buffer Protocol support to int.from_bytes #132108
Open
@cmaloney

Description

@cmaloney

Feature or enhancement

Proposal:

Currently int.from_bytes takes a PyObject* and always converts it to a bytes using PyObject_Bytes. When a "bytes-like" object is passed in, such as bytearray in _pylong, the PyObject_Bytes function has to make a new PyBytes* and copy the data from the bytes-like into it. Utilize the buffer protocol, with a fallback to PyObject_Bytes, to remove that object allocation and copy making the conversion faster.

I think the fallback is needed because PyObject_Bytes supports additional methods, such as .__bytes__() to construct the bytes, which should remain supported. That means need to keep doing bytes as bytes_obj: object in Argument Clinic, rather than bytes as bytes_obj: Py_buffer.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0