]> granicus.if.org Git - python/commitdiff
Issue #4608: urllib.request.urlopen does not return an iterable object
authorRaymond Hettinger <python@rcn.com>
Sun, 26 Jun 2011 12:29:35 +0000 (14:29 +0200)
committerRaymond Hettinger <python@rcn.com>
Sun, 26 Jun 2011 12:29:35 +0000 (14:29 +0200)
Lib/test/test_urllib.py
Lib/urllib/response.py
Misc/ACKS

index 7bb8a098c7e3baf7fe8bde1a19bc8a7b9ad5bad7..3a806bff167994ff6539a95163df580bf4e9edab 100644 (file)
@@ -110,8 +110,9 @@ class urlopen_FileTests(unittest.TestCase):
         # 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):
index 985964231093c4c0e7c2fb0781ac410dee419f6d..8c6dcca5aa1a781275b6f024e7315d74f9811b08 100644 (file)
@@ -23,10 +23,14 @@ class addbase(object):
             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__,
index 06853b9535c24454db3ed35dade23cb6d2ee52b1..8c39e82a23bd162143e93a2eb8b73757d6982c24 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -309,6 +309,7 @@ Lele Gaifax
 Santiago Gala
 Yitzchak Gale
 Quentin Gallet-Gilles
+Riccardo Attilio Galli
 Raymund Galvin
 Nitin Ganatra
 Fred Gansevles