When val=b'', but val == b'' returns False - bytes initialization
Hello, I am not sure if this is expected behaviour, or a bug. In a C extension module, if I create and return an empty bytes object like this: val = PyBytes_FromStringAndSize (NULL, 20); Py_SIZE(val) = 0; Then from the Python interpreter's perspective: isinstance(val, bytes) returns True print(val) returns b'' print(repr(val)) returns b'' BUT val == b'' returns False. On the other hand, initializing the underlying memory: val = PyBytes_FromStringAndSize (NULL, 20); PyBytes_AS_STRING (val); c[0] = '\0'; Py_SIZE(val) = 0; Then, from the Python interpreter, val == b'' returns True, as expected. So, my question is: is this the expected behaviour, or a bug? I was slightly surprised to have to initialize the storage. On the other hand, I can perhaps also see it might be expected, since the docs do say that PyBytes_FromStringAndSize will not initialize the underlying storage. Please cc me on any replies - am not subscribed to the list. Many thanks, Jonathan
On Wed, 27 Dec 2017 14:19:16 +0000 Jonathan Underwood <jonathan.underwood@gmail.com> wrote:
Hello,
I am not sure if this is expected behaviour, or a bug.
In a C extension module, if I create and return an empty bytes object like this:
val = PyBytes_FromStringAndSize (NULL, 20); Py_SIZE(val) = 0;
I wouldn't call it "expected", but bytes objects are supposed to be NULL-terminated internally, so your code technically creates an invalid bytes object. The NULL-terminated constraint may be relied on by some code, for example when the string gets passed to a third-party C function. Perhaps that should be mentioned in the C API docs. Regards Antoine.
On Wed, 27 Dec 2017 17:28:41 +0100 Antoine Pitrou <solipsis@pitrou.net> wrote:
On Wed, 27 Dec 2017 14:19:16 +0000 Jonathan Underwood <jonathan.underwood@gmail.com> wrote:
Hello,
I am not sure if this is expected behaviour, or a bug.
In a C extension module, if I create and return an empty bytes object like this:
val = PyBytes_FromStringAndSize (NULL, 20); Py_SIZE(val) = 0;
I wouldn't call it "expected", but bytes objects are supposed to be NULL-terminated internally, so your code technically creates an invalid bytes object. The NULL-terminated constraint may be relied on by some code, for example when the string gets passed to a third-party C function.
Note this is really happening because you're allocating a 20-long bytes object and then shortening it to 0 bytes. PyBytes_FromStringAndSize() already stored a NULL byte in the 21st place. Regards Antoine.
participants (2)
-
Antoine Pitrou -
Jonathan Underwood