<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<br>
Bill Janssen wrote:
<blockquote cite="mid:35441.1235863997@parc.com" type="cite">
<pre wrap="">Bill Janssen <a class="moz-txt-link-rfc2396E" href="mailto:janssen@parc.com"><janssen@parc.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Dan Mahn <a class="moz-txt-link-rfc2396E" href="mailto:dan.mahn@digidescorp.com"><dan.mahn@digidescorp.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">3) Regarding the following code fragment in urlencode():
k = quote_plus(str(k))
if isinstance(v, str):
v = quote_plus(v)
l.append(k + '=' + v)
elif isinstance(v, str):
# is there a reasonable way to convert to ASCII?
# encode generates a string, but "replace" or "ignore"
# lose information and "strict" can raise UnicodeError
v = quote_plus(v.encode("ASCII","replace"))
l.append(k + '=' + v)
I don't understand how the "elif" section is invoked, as it uses the
same condition as the "if" section.
</pre>
</blockquote>
<pre wrap="">This looks like a 2->3 bug; clearly only the second branch should be
used in Py3K. And that "replace" is also a bug; it should signal an
error on encoding failures. It should probably catch UnicodeError and
explain the problem, which is that only Latin-1 values can be passed in
the query string. So the encode() to "ASCII" is also a mistake; it
should be "ISO-8859-1", and the "replace" should be a "strict", I think.
</pre>
</blockquote>
<pre wrap=""><!---->
Sorry! In 3.0.1, this whole thing boils down to
l.append(quote_plus(k) + '=' + quote_plus(v))
Bill
</pre>
</blockquote>
<br>
Thanks. Generally, I would tend to agree. I actually tried something
like that, but I found that I had inadvertently been sending numeric
values, in which case the str() was saving me. Considering that, I
would rather just see something like ...<br>
<br>
k = quote_plus(k) if isinstance(k,(str,bytes)) else quote_plus(str(k))<br>
if isinstance(v,(str,bytes)):<br>
l.append(k + "=" + quote_plus(v))<br>
else:<br>
# just keep what's in the else<br>
<br>
I think it would be more compatible with existing code calling
urlencode(). Additionally, I think it would be nice to allow selection
of the quote_plus() string encoding parameters, but that was one of the
other points I listed.<br>
<br>
A similar thing could be done when "not doseq", but the handling of "v"
would be exactly like "k".<br>
<br>
- Dan<br>
<br>
</body>
</html>