[Python-Dev] When val=b'', but val == b'' returns False - bytes initialization
Jonathan Underwood
jonathan.underwood at gmail.com
Wed Dec 27 09:19:16 EST 2017
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
More information about the Python-Dev
mailing list