Author jonathanunderwood
Recipients jonathanunderwood, r.david.murray, serhiy.storchaka
Date 2017-12-27.19:00:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1514401217.59.0.213398074469.issue32431@psf.upfronthosting.co.za>
In-reply-to
Content
Actually the commentary at the top of bytesobject.c for PyBytes_FromStringAndSize says:

"... If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' bytes (setting the last byte to the null terminating character)... "

So, perhaps that's as close to gospel as it gets - this does imply that bytes objects are expected to be null terminated. Why PyBytesAsString then adds an extra null terminator is a bit of a mystery.

Perhaps what's needed is some documentation clarifications:

1/ State early on that bytes objects are always expected to be null terminated.

2/ As such, the string pointer returned by PyBytes_AsString will point to a null terminated string - I think the current docs could be misinterpreted to suggest that _AsString *adds* an extra byte for the null, which it doesn't.

3/ Document that using Py_SIZE to reduce the length of a bytes object is dangerous, because the null terminator will be lost, and subsequent behaviour undefined.

4/ Document that the preferred way to resize is to use PyBytes_FromStringAndSize with a new size.

5/ Indicate clearly that _PyBytes_Resize is not a public interface and its use is discouraged in favour of PyBytes_FromStringAndSize
History
Date User Action Args
2017-12-27 19:00:17jonathanunderwoodsetrecipients: + jonathanunderwood, r.david.murray, serhiy.storchaka
2017-12-27 19:00:17jonathanunderwoodsetmessageid: <1514401217.59.0.213398074469.issue32431@psf.upfronthosting.co.za>
2017-12-27 19:00:17jonathanunderwoodlinkissue32431 messages
2017-12-27 19:00:17jonathanunderwoodcreate