Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9d5e39c

Browse files
committed
Add a more general fix for #127 (CPy #20007) based on #136.
1 parent 082c042 commit 9d5e39c

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

html5lib/inputstream.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, division, unicode_literals
2-
from six import text_type
2+
3+
from six import text_type, get_method_self
34
from six.moves import http_client
45

56
import codecs
@@ -130,12 +131,13 @@ def _readFromBuffer(self, bytes):
130131

131132

132133
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True):
133-
if isinstance(source, http_client.HTTPResponse):
134-
# Work around Python bug #20007: read(0) closes the connection.
135-
# http://bugs.python.org/issue20007
136-
isUnicode = False
137-
elif hasattr(source, "read"):
138-
isUnicode = isinstance(source.read(0), text_type)
134+
if hasattr(source, "read"):
135+
if isinstance(get_method_self(source.read), http_client.HTTPResponse):
136+
# Work around Python bug #20007: read(0) closes the connection.
137+
# http://bugs.python.org/issue20007
138+
isUnicode = False
139+
else:
140+
isUnicode = isinstance(source.read(0), text_type)
139141
else:
140142
isUnicode = isinstance(source, text_type)
141143

html5lib/tests/test_stream.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import codecs
66
from io import BytesIO
77

8-
from six.moves import http_client
8+
from six.moves import http_client, urllib
99

1010
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
1111
HTMLUnicodeInputStream, HTMLBinaryInputStream)
@@ -170,6 +170,21 @@ def makefile(self, _mode, _bufsize=None):
170170
stream = HTMLInputStream(source)
171171
self.assertEqual(stream.charsUntil(" "), "Text")
172172

173+
def test_python_issue_20007_b(self):
174+
"""
175+
Make sure we have a work-around for Python bug #20007
176+
http://bugs.python.org/issue20007
177+
"""
178+
class FakeSocket(object):
179+
def makefile(self, _mode, _bufsize=None):
180+
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
181+
182+
source = http_client.HTTPResponse(FakeSocket())
183+
source.begin()
184+
wrapped = urllib.response.addinfourl(source, source.msg, "http://example.com")
185+
stream = HTMLInputStream(source)
186+
self.assertEqual(stream.charsUntil(" "), "Text")
187+
173188

174189
def buildTestSuite():
175190
return unittest.defaultTestLoader.loadTestsFromName(__name__)

0 commit comments

Comments
 (0)