(original) (raw)
changeset: 70996:c0a68b948f5d branch: 3.2 parent: 70994:f852e2d556af user: Raymond Hettinger python@rcn.com date: Sun Jun 26 14:29:35 2011 +0200 files: Lib/test/test_urllib.py Lib/urllib/response.py Misc/ACKS description: Issue #4608: urllib.request.urlopen does not return an iterable object diff -r f852e2d556af -r c0a68b948f5d Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Sun Jun 26 13:34:56 2011 +0300 +++ b/Lib/test/test_urllib.py Sun Jun 26 14:29:35 2011 +0200 @@ -110,8 +110,9 @@ # Test iterator # Don't need to count number of iterations since test would fail the # instant it returned anything beyond the first line from the - # comparison - for line in self.returned_obj.__iter__(): + # comparison. + # Use the iterator in the usual implicit way to test for ticket #4608. + for line in self.returned_obj: self.assertEqual(line, self.text) class ProxyTests(unittest.TestCase): diff -r f852e2d556af -r c0a68b948f5d Lib/urllib/response.py --- a/Lib/urllib/response.py Sun Jun 26 13:34:56 2011 +0300 +++ b/Lib/urllib/response.py Sun Jun 26 14:29:35 2011 +0200 @@ -23,10 +23,14 @@ self.fileno = self.fp.fileno else: self.fileno = lambda: None - if hasattr(self.fp, "__iter__"): - self.__iter__ = self.fp.__iter__ - if hasattr(self.fp, "__next__"): - self.__next__ = self.fp.__next__ + + def __iter__(self): + # Assigning `__iter__` to the instance doesn't work as intended + # because the iter builtin does something like `cls.__iter__(obj)` + # and thus fails to find the _bound_ method `obj.__iter__`. + # Returning just `self.fp` works for built-in file objects but + # might not work for general file-like objects. + return iter(self.fp) def __repr__(self): return '<%s at %r whose fp = %r>' % (self.__class__.__name__, diff -r f852e2d556af -r c0a68b948f5d Misc/ACKS --- a/Misc/ACKS Sun Jun 26 13:34:56 2011 +0300 +++ b/Misc/ACKS Sun Jun 26 14:29:35 2011 +0200 @@ -309,6 +309,7 @@ Santiago Gala Yitzchak Gale Quentin Gallet-Gilles +Riccardo Attilio Galli Raymund Galvin Nitin Ganatra Fred Gansevles /python@rcn.com