Skip to content

PyBytesWriter_GetData should mention that PyBytesWriter_Resize can also invalidate the returned pointer #146353

Closed

Description

@cdhowie

Documentation

See relevant discussion on PyO3/pyo3#5906 and the discussion linked from that.

PyBytesWriter_Resize may need to reallocate the buffer to grow it. If this happens, any pointer previously returned by PyBytesWriter_GetData will be invalidated, but this is not documented -- only PyBytesWriter_Finish and PyBytesWriter_Discard are documented to invalidate previous data pointers.

I am happy to submit a PR for this issue if that would be helpful. (I assume I only need to modify Doc/c-api/bytes.rst?)

Linked PRs

Activity

  1. sergey-miryanov commented on Mar 24, 2026

    @sergey-miryanov
    Contributor
  2. vstinner commented on Mar 24, 2026

    @vstinner
    Member

    I am happy to submit a PR for this issue if that would be helpful. (I assume I only need to modify Doc/c-api/bytes.rst?)

    Go ahead. In fact, many PyBytesWriter functions can invalidate the data pointer. Any function calling PyBytesWriter_Resize():

    • PyBytesWriter_Grow()
    • PyBytesWriter_GrowAndUpdatePointer()
    • PyBytesWriter_WriteBytes()
    • PyBytesWriter_Format()

    You may just say:

       The pointer is valid until a :c:type:`PyBytesWriter` function is called on
       *writer*.
    
  3. vstinner commented on Mar 24, 2026

    @vstinner
    Member

    (I assume I only need to modify Doc/c-api/bytes.rst?)

    Correct.

  4. TeamSpen210 commented on Mar 24, 2026

    @TeamSpen210

    Should probably be more specific, state that PyBytesWriter_GetSize() and PyBytesWriter_GetData() are exceptions and don't invalidate.

  5. picnixz commented on Apr 3, 2026

    @picnixz
    Member

    I think we can say "Unless stated otherwise, the pointer is valid until a PyBytesWriter function is called" and then we modify case by case the functions that do not invalidate the pointer. If there are more functions like that, we just document those that invalidate the pointer.

  6. added a commit that references this issue on Jun 18, 2026
  7. added a commit that references this issue on Jun 18, 2026
  8. added a commit that references this issue on Jul 5, 2026
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions