[Python-Dev] When val=b'', but val == b'' returns False - bytes initialization

2017年12月27日 08:14:04 -0800

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
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to