[issue32431] Two bytes objects of zero length don't compare equal

Jonathan Underwood report at bugs.python.org
Wed Dec 27 14:00:17 EST 2017


Jonathan Underwood <jonathan.underwood at 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 <report at bugs.python.org>
<https://bugs.python.org/issue32431>
_______________________________________


More information about the Python-bugs-list mailing list