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 xuanji
Recipients catlee, eric.araujo, jhylton, orsenthil, pitrou, rcoyner, rhettinger, xuanji
Date 2010-11-30.14:53:44
SpamBayes Score 4.367595e-08
Marked as misclassified No
Message-id <1291128831.51.0.350043740266.issue3243@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2010-11-30 14:53:51xuanjisetrecipients: + xuanji, jhylton, rhettinger, orsenthil, pitrou, catlee, eric.araujo, rcoyner
2010-11-30 14:53:51xuanjisetmessageid: <1291128831.51.0.350043740266.issue3243@psf.upfronthosting.co.za>
2010-11-30 14:53:44xuanjilinkissue3243 messages
2010-11-30 14:53:44xuanjicreate