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

2017年12月27日 09:53:57 -0800

On 2017年12月27日 17:28:41 +0100
Antoine Pitrou <[email protected]> wrote:
> On 2017年12月27日 14:19:16 +0000
> Jonathan Underwood <[email protected]> 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.
_______________________________________________
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