Skip to content

Commit 18bc278

Browse files
committed
Fix #127, add a work-around for CPython bug 20007.
The bug has been fixed upstream, but this works around it on older Python versions. This does not change any behavior on Python versions with the fix.
1 parent e269a2f commit 18bc278

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

html5lib/inputstream.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, division, unicode_literals
22
from six import text_type
3+
from six.moves import http_client
34

45
import codecs
56
import re
@@ -118,7 +119,11 @@ def _readFromBuffer(self, bytes):
118119

119120

120121
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True):
121-
if hasattr(source, "read"):
122+
if isinstance(source, http_client.HTTPResponse):
123+
# Work around Python bug #20007: read(0) closes the connection.
124+
# http://bugs.python.org/issue20007
125+
isUnicode = False
126+
elif hasattr(source, "read"):
122127
isUnicode = isinstance(source.read(0), text_type)
123128
else:
124129
isUnicode = isinstance(source, text_type)

html5lib/tests/test_stream.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import codecs
66
from io import BytesIO
77

8+
from six.moves import http_client
9+
810
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
911
HTMLUnicodeInputStream, HTMLBinaryInputStream)
1012

@@ -154,6 +156,20 @@ def test_position2(self):
154156
self.assertEqual(stream.char(), "d")
155157
self.assertEqual(stream.position(), (2, 1))
156158

159+
def test_python_issue_20007(self):
160+
"""
161+
Make sure we have a work-around for Python bug #20007
162+
http://bugs.python.org/issue20007
163+
"""
164+
class FakeSocket(object):
165+
def makefile(self, _mode, _bufsize=None):
166+
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
167+
168+
source = http_client.HTTPResponse(FakeSocket())
169+
source.begin()
170+
stream = HTMLInputStream(source)
171+
self.assertEqual(stream.charsUntil(" "), "Text")
172+
157173

158174
def buildTestSuite():
159175
return unittest.defaultTestLoader.loadTestsFromName(__name__)

0 commit comments

Comments
 (0)