[Python-3000] C API cleanup str

Walter Dörwald walter at livinglogic.de
Mon Aug 6 09:51:08 CEST 2007


Martin v. Löwis wrote:

>> You mean if it were fixed it could fail, right? Code calling it should
>> be checking for errors anyway because it allocates memory.
>>
>> Have you tried making this particular change and seeing what fails?
> 
> I now tried, and it turned out that bytes.__reduce__ would break
> (again); I fixed it and changed it in r56755.
> 
> It turned out that PyUnicode_FromString was even documented to
> accept latin-1.

Yes, that seemed to me to be the most obvious interpretion.

> While I was looking at it, I wondered why PyUnicode_FromStringAndSize
> allows a NULL first argument, creating a null-initialized Unicode
> object.

Because that's what PyString_FromStringAndSize() does.

> This functionality is already available as
> PyUnicode_FromUnicode, and callers who previously wrote
> 
>    obuf = PyString_FromStringAndSize(NULL, bufsize);
>    if (!obuf)return NULL;
>    buf = PyString_AsString(buf);
> 
> could be tricked into believing that they now can change the
> string object they just created - which they cannot, as
> buf will just be the UTF-8 encoded version of the real string.

True, this will no longer work.

So should NULL support be dropped from PyUnicode_FromStringAndSize()?

Servus,
    Walter


More information about the Python-3000 mailing list