[ZODB-Dev] embedded null bytes in python strings?

Jeremy Hylton jeremy@zope.com
10 Apr 2003 13:44:12 -0400


On Thu, 2003-04-10 at 13:33, rys mccusker wrote:
> can python strings contain bytes equal to 0x00?
> 
> I.e. can strings contain arbitrary binary data?

Yes.

>From Include/stringobject.h:

Type PyStringObject represents a character string.  An extra zero byte
is
reserved at the end to ensure it is zero-terminated, but a size is
present so strings with null bytes in them can be represented.  This
is an immutable object type.

There are functions to create new string objects, to test
an object for string-ness, and to get the
string value.  The latter function returns a null pointer
if the object is not of the proper type.
There is a variant that takes an explicit size as well as a
variant that assumes a zero-terminated string.  Note that none of the
functions should be applied to nil objects.

Jeremy