From my reading and assumptions, I guess PyBytesWriter_WriteBytes and PyBytesWriter_Format will extend the buffer? It should say so explicitly in their description, including how it relates to a prior overallocation (e.g. if I PyBytesWriter_Create(10) and then PyBytesWriter_WriteBytes(<10 bytes>), does it use the 10 I specified or does it allocate 10 more?).
If I call PyBytesWriter_WriteBytes multiple times, does it append or overwrite?
Does PyBytesWriter_GetData get the start of the data or the position where WriteBytes would next write to? (Obviously irrelevant if it overwrites from the start each time.)
Why disallow shrinking with PyBytesWriter_Grow? It can handle negative growth just fine (potentially with data loss, but that’s likely intentional). I have plenty of cases where I would PyBytesWriter_Grow(-1) to trim a trailing null or character.
What does “update the buf pointer” mean for PyBytesWriter_GrowAndUpdatePointer? Maybe it needs a realistic example, because it seems like Grow is going to allocate garbage/zeros and UpdatePointer is going to move my pointer past it so that I don’t write values into the new part of the allocation? Doesn’t seem useful.
Got up to the next example, and I see it would make sense to pass in the result of a strlen previously used to strcpy into the result of GetData? Is that the intent? Still not entirely clear how or when I’d use this API.
The “Overallocation” section should mention that (whether?) finishing the writer will trim overallocations.
The implementation allocates internally a bytes object …
Maybe call out that this isn’t a required part of the design? Other implementations may do it differently, and CPython may do it differently in the future.
There is no impact on the backward compatibility
I don’t think you can deprecate (even soft deprecate) functions and say there’s no impact
A short example of how to change code using PyBytes_FromStringAndSize(NULL, size) into code that will compile with both the old API and the new API would be useful here - checking PY_VERSION_HEX is probably the best option here?