Message122905
I don't fully understand Lib/urllib/request.py either, I just ported it and ran the unittests... it seems like what it does is that if you send an iterator through as 'data' you can't know the length in advance, and rather than let the len(data) raise an exception catlee thought it's better to raise an exception to tell the user exactly why the code failed (ie, because the user sent an iterator and there's no way to meaningfully find the Content-Length of that).
As for the catching exceptions vs using isinstance: I thought about it for a while, I think something like this feels right to me:
try:
self.sock.sendall(data)
except TypeError:
if isinstance(data, collections.Iterable):
for d in t:
self.sock.sendall(d)
else:
raise TypeError("data should be a bytes-like object or an iterable, got %r" % type(it))
anyway, calling iter(data) is equivalent to calling data.__iter__(), so catching the exception is equivalent to hasattr(data, '__iter__'), which is roughly the same as isinstance(data, collections.Iterable). so we try the most straightforward method (sending everything) then if that fails, data is either an iterator or a wrong type. |
|
Date |
User |
Action |
Args |
2010-11-30 14:53:51 | xuanji | set | recipients:
+ xuanji, jhylton, rhettinger, orsenthil, pitrou, catlee, eric.araujo, rcoyner |
2010-11-30 14:53:51 | xuanji | set | messageid: <1291128831.51.0.350043740266.issue3243@psf.upfronthosting.co.za> |
2010-11-30 14:53:44 | xuanji | link | issue3243 messages |
2010-11-30 14:53:44 | xuanji | create | |
|