This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients nagisa, orsenthil, terry.reedy
Date 2012-09-14.19:11:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1347649904.4.0.834081817551.issue15887@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-09-14 19:11:44terry.reedysetrecipients: + terry.reedy, orsenthil, nagisa
2012-09-14 19:11:44terry.reedysetmessageid: <1347649904.4.0.834081817551.issue15887@psf.upfronthosting.co.za>
2012-09-14 19:11:23terry.reedylinkissue15887 messages
2012-09-14 19:11:20terry.reedycreate