Python 3.0 urllib.parse.parse_qs results in TypeError

John Machin sjmachin at lexicon.net
Wed Jan 14 01:07:00 CET 2009


On Jan 14, 9:56 am, Andy Grove <andygrov... at gmail.com> wrote:
> On Jan 13, 3:08 pm, John Machin <sjmac... at lexicon.net> wrote:
>
> > Please show the full traceback.
>
> John,
>
> Thanks. Here it is:
>
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/socketserver.py", line 281, in _handle_request_noblock
>     self.process_request(request, client_address)
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/socketserver.py", line 307, in process_request
>     self.finish_request(request, client_address)
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/socketserver.py", line 320, in finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/socketserver.py", line 614, in __init__
>     self.handle()
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/http/server.py", line 363, in handle
>     self.handle_one_request()
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/http/server.py", line 357, in handle_one_request
>     method()
>   File "/Users/andy/Development/EclipseWorkspace/dbsManage/kernel.py",
> line 178, in do_POST
>     form = urllib.parse.parse_qs(qs, keep_blank_values=1)
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/urllib/parse.py", line 351, in parse_qs
> ----------------------------------------
>     for name, value in parse_qsl(qs, keep_blank_values,
> strict_parsing):
>   File "/Library/Frameworks/Python.framework/Versions/3.0/lib/
> python3.0/urllib/parse.py", line 377, in parse_qsl
>     pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
> TypeError: Type str doesn't support the buffer API


| Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> qs_bytes = b'a;b&c;d'
| >>> qs_str = 'a;b&c;d'
| >>> pairs = [s2 for s1 in qs_bytes.split('&') for s2 in s1.split
(';')]
| Traceback (most recent call last):
|   File "<stdin>", line 1, in <module>
| TypeError: Type str doesn't support the buffer API
| >>> pairs = [s2 for s1 in qs_str.split('&') for s2 in s1.split(';')]
| >>> pairs
| ['a', 'b', 'c', 'd']
| >>> b'x&y'.split('&')
| Traceback (most recent call last):
|   File "<stdin>", line 1, in <module>
| TypeError: Type str doesn't support the buffer API
| >>> b'x&y'.split(b'&')
| [b'x', b'y']
| >>> 'x&y'.split('&')
| ['x', 'y']
| >>>

The immediate cause is that as expected mixing str and bytes raises an
exception -- this one however qualifies as "not very informative" and
possibly wrong [not having inspected the code for whatever.split() I'm
left wondering what is the relevance of the buffer API].

The docs for urllib.parse.parse_qs() and .parse_qsl() are a bit vague:
"""query string given as a string argument (data of type application/x-
www-form-urlencoded)""" ... does "string" mean "str only" or "str or
bytes"?

Until someone can give an authoritative answer [*], you might like to
try decoding your data (presuming you know what it is or how to dig it
out like you found the type and length) and feeding the result to
the .parse_qs().

[*] I know next to zilch about cgi and urllib -- I'm just trying to
give you some clues to see if you can get yourself back on the road.




More information about the Python-list mailing list