Jonathan Underwood <jonathan.underw...@gmail.com> added the comment:
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 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue32431> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com