Skip to content
Myr

Improve _encode_params to handle lists of two-element tuples

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Idan Gazit · committed by Donald Stufft · parent 3e60a9e · Browse files · 1 file+14 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−8 removed

  • other:+14 added−8 removed

1 file+14 added−8 removed

modified:requests/models.py+14 added-8 removed
310 unmodified lines
311 @staticmethod312 def _encode_params(data):313 """Encode parameters in a piece of data.314315 If the data supplied is a dictionary, encodes each parameter in it, and316 returns a list of tuples containing the encoded parameters, and a urlencoded317 version of that.315 Will successfully encode parameters when passed as a dict or a list of316 2-tuples. Order is retained if data is a list of 2-tuples but abritrary317 if parameters are supplied as a dict.318319 If the data supplied contains parameters, encodes each parameter in it,320 and returns a list of tuples containing the encoded parameters, and a321 urlencoded version of that.322323 Otherwise, assumes the data is already encoded appropriately, and324 returns it twice.325 """326327 if isinstance(data, bytes):328 return data, data329 elif hasattr(data, '__iter__'):330 try:331 dict(data)332 except ValueError:333 raise ValueError('Unable to encode lists with elements that are not 2-tuples.')334326 if hasattr(data, '__iter__') and not isinstance(data, str):327 data = dict(data)328329 if hasattr(data, 'items'):335 params = list(data.items() if isinstance(data, dict) else data)336 result = []331 for k, vs in list(data.items()):337 for k, vs in params:338 for v in isinstance(vs, list) and vs or [vs]:339 result.append(340 (k.encode('utf-8') if isinstance(k, types.StringType) else k,341 v.encode('utf-8') if isinstance(v, types.StringType) else v))
511 unmodified lines