Python 3.0 urllib.parse.parse_qs results in TypeError

John Machin sjmachin at lexicon.net
Wed Jan 21 22:32:30 CET 2009


On Jan 22, 2:17 am, a... at pythoncraft.com (Aahz) wrote:
> In article <313a27f9-c655-4fc4-a8e3-568a4283b... at f40g2000pri.googlegroups.com>,
>
> ag73  <andygrov... at gmail.com> wrote:
>
> >                    form = urllib.parse.parse_qs(qs, keep_blank_values=1)
>
> >However, the last line of code that calls parse_qs causes the
> >following exception to be thrown:
>
> ><class 'TypeError'>
> >Type str doesn't support the buffer API
>
> One of the key features of Python 3.0 is the fact that it now
> distinguishes between bytes and strings.  Unfortunately, there are a lot
> of ambiguous areas where the correct handling is not clear; for example,
> nobody has yet agreed whether URLs are strings or bytes.  As you
> discovered, forced conversion to string seems to work here and I suggest
> you make that your workaround.  

However I'm surprised on further reflection that that workaround
works; it must be only accidental.

"""if I pass in "str(qs)" instead of
"qs" then the call works."""

BUT str(bytes_instance) with no other args passed *doesn't* just do a
decoding: """When only object is given, this returns its nicely
printable representation."""

The nicely printable representation for bytes objects includes:
* wrapping it in b''
* showing non-ASCII characters as \xdd

3.0:

>>> len(str(b'abc'))
6
>>> len(str(b'abc', encoding='ascii'))
3
>>> len(str(b'\xff'))
7
>>> len(str(b'\xff', encoding='ascii'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't d




More information about the Python-list mailing list