Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 038018a

Browse files
committed
Issue #4608: urllib.request.urlopen does not return an iterable object
1 parent 4850d52 commit 038018a

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ def test_iter(self):
110110
# Test iterator
111111
# Don't need to count number of iterations since test would fail the
112112
# instant it returned anything beyond the first line from the
113-
# comparison
114-
for line in self.returned_obj.__iter__():
113+
# comparison.
114+
# Use the iterator in the usual implicit way to test for ticket #4608.
115+
for line in self.returned_obj:
115116
self.assertEqual(line, self.text)
116117

117118
class ProxyTests(unittest.TestCase):

Lib/urllib/response.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ def __init__(self, fp):
2323
self.fileno = self.fp.fileno
2424
else:
2525
self.fileno = lambda: None
26-
if hasattr(self.fp, "__iter__"):
27-
self.__iter__ = self.fp.__iter__
28-
if hasattr(self.fp, "__next__"):
29-
self.__next__ = self.fp.__next__
26+
27+
def __iter__(self):
28+
# Assigning `__iter__` to the instance doesn't work as intended
29+
# because the iter builtin does something like `cls.__iter__(obj)`
30+
# and thus fails to find the _bound_ method `obj.__iter__`.
31+
# Returning just `self.fp` works for built-in file objects but
32+
# might not work for general file-like objects.
33+
return iter(self.fp)
3034

3135
def __repr__(self):
3236
return '<%s at %r whose fp = %r>' % (self.__class__.__name__,

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Lele Gaifax
309309
Santiago Gala
310310
Yitzchak Gale
311311
Quentin Gallet-Gilles
312+
Riccardo Attilio Galli
312313
Raymund Galvin
313314
Nitin Ganatra
314315
Fred Gansevles

0 commit comments

Comments
 (0)