Skip to content

Let C++ function return bytes without extra copy #1236

@ppwwyyxx

Description

@ppwwyyxx

Make sure you've completed the following steps before submitting your issue -- thank you!

Issue description

According to the documentation, to let a C++ function returns bytes instead of unicode string in Python, one needs to use py::bytes: http://pybind11.readthedocs.io/en/stable/advanced/cast/strings.html?highlight=string#return-c-strings-without-conversion.

However I found that py::bytes calls PyBytes_FromString function, which copies the string according to https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromString.

Is there anyway to construct a bytes object directly in C++ without an extra copy?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions