Terry J. Reedy added the comment:

The first sentence of the urlencode entry is a bit garbled as something is 
clearly missing.

"Convert a mapping object or a sequence of two-element tuples, which may either 
be a str or a bytes, to a “percent-encoded” string."

Mappings, duple sequences, and duples cannot be str or bytes. I presume the 
query argument can be a string (as stated in the docstring, but not here, 
though only vacuously true since only empty strings are accepted -- see below), 
mapping, or duple sequence, and that the phrase "which may either be a str or a 
bytes" refers to the unstated string option. This sentence should be fixed 
after deciding that the new truth should be.

The stated requirement of 'two-element tuples' is overly strict. The two loop 
headers
  for k, v in query:
only requires pairs, or rather iterables that produce two items, just as in 
two-target assignment statements.

The enhancement proposal should be the expansion of 'sequence' to 'iterable'. 
This is in line with the general trend in 3.x of replacing list or sequence 
with iterable. There is no good reason to force the creation of an temporary 
list. The two loops
  for k, v in query:
work perfectly well with any key-value iterable. In fact, dict (mapping) 
queries are now (in Python3) handled by replacing them with a key-value 
iterable that is not a sequence.
    if hasattr(query, "items"):
        query = query.items()
So directly passing dict.items(), for instance, should be allowed.

The only change needed is to the query argument rejection logic.

Currently, non-empty strings are rejected, even though the doc string and docs 
state or imply that query may be a string. They should state that only empty 
strings are accepted (though I do not see the point of accepting and returning 
an empty string, so maybe empty strings should also be disallowed). I believe 
the following reproduces current behavior as regards strings except for giving 
a more accurate error message.

if isinstance(query, (str, bytes)):
  if len(query): raise TypeError("non empty strings not allowed")
  else: return query

.with_traceback is meant for when one replaces one exception type with another 
or modified the tb object. The current usage in the code does neither and is a 
no-op and should be omitted in a patch.

The main part of the code can be wrapped with try-except to customize an error 
message if query is not iterable or if the items produced by iterating query 
are not iterables of pairs.

----------
nosy: +terry.reedy
title: urlencode should accept generators or two elements tuples -> urlencode 
should accept iterables of pairs

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15887>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to