[Python-Dev] bytes with trailing \0?

Guido van Rossum guido at python.org
Tue Apr 29 06:34:47 CEST 2014


It is to improve the experience of passing bytes to a C function that
expects a trailing \0. For example syscalls taking filenames. The wrapper
must still check for embedded \0 but the bytes don't need to be copied.

On Monday, April 28, 2014, Nikolaus Rath <Nikolaus at rath.org> wrote:

> Hello,
>
> I was surprised to find the following in bytesobject.c:
>
> ,----
> | [...]
> |    As always, an extra byte is allocated for a trailing \0 byte (newsize
> |    does *not* include that), and a trailing \0 byte is stored.
> | */
> |
> | int
> | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
> | {
> | [...]
> |
> `----
>
> Does this mean that bytes objects are internally stored with a trailing
> \0? Why is that? Isn't that just wasting a byte, because \0 might also
> be in the middle of the byte sequence, and the bytes objects stores its
> length explicitly anyway?
>
>
> Best,
> -Nikolaus
> --
> GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
> Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F
>
>              »Time flies like an arrow, fruit flies like a Banana.«
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org <javascript:;>
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>


-- 
--Guido van Rossum (on iPad)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140428/cb255b49/attachment-0001.html>


More information about the Python-Dev mailing list