[Python-3000] PEP3137: "".encode() return type

Guido van Rossum guido at python.org
Thu Nov 1 15:34:07 CET 2007


I know; right before 3.0a1 was released I fixed all encoders to return
PyBytes. IMO they should return PyString though. The best place to fix
this quickly is in the wrapper function(s) -- if the codec returned
PyBytes, just change it to PyString again. Then we can fix the codecs
later (or leave it as it is for now and wait until we rename the C
types :-).

To avoid us doing double work, can you just submit any straightforward
fixes you might already have made, and email me about projects you
have underway? I expect to be getting back to this in about two hours.
:-)

--Guido

On 11/1/07, Christian Heimes <lists at cheimes.de> wrote:
> I'm trying to fix some of the errors in the py3k-pep3137 branch and I
> stumbled over an issue with encode().
>
> TypeError: encoder did not return a bytes object (type=bytes)
>
> The sentence "... encoding always takes a Unicode string and returns a
> bytes sequence, and decoding always takes a bytes sequence and returns a
> Unicode string." isn't helpful because it's ambiguous. Do you mean the
> old bytes type PyBytes or the new bytes type PyString?
>
> One of the encode methods in unicodeobject.c PyUnicode_AsEncodedString()
> checks for PyString and converts it into a buffer (PyBytes).
>
>     if (!PyBytes_Check(v)) {
>         if (PyString_Check(v)) {
>             /* Old codec, turn it into bytes */
>             PyObject *b = PyBytes_FromObject(v);
>             Py_DECREF(v);
>             return b;
>         }
>
> unicode_encode() doesn't like PyString at all and immediately raises a
> type error.
>
> * What's the correct return type, PyBytes or PyString?
>
> * If PyString is wrong shouldn't we fix the PyUnicode_AsEncodedString
> and remove PyBytes_FromObject?
>
> Christian
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list