8000 Also fix #127 with addinfourl · SimonSapin/html5lib-python@1544aca · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 1544aca

Browse files
committed
Also fix html5lib#127 with addinfourl
1 parent c36197d commit 1544aca

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

html5lib/inputstream.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import, division, unicode_literals
22
from six import text_type
33
from six.moves import http_client
4+
from six.moves.urllib.response import addinfourl
45

56
import codecs
67
import re
@@ -119,12 +120,17 @@ def _readFromBuffer(self, bytes):
119120

120121

121122
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True):
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"):
127-
isUnicode = isinstance(source.read(0), text_type)
123+
if hasattr(source, "read"):
124+
if isinstance(source, addinfourl):
125+
checked_source = source.fp
126+
else:
127+
checked_source = source
128+
if isinstance(checked_source, http_client.HTTPResponse):
129+
# Work around Python bug #20007: read(0) closes the connection.
130+
# http://bugs.python.org/issue20007
131+
isUnicode = False
132+
else:
133+
isUnicode = isinstance(source.read(0), text_type)
128134
else:
129135
isUnicode = isinstance(source, text_type)
130136

html5lib/tests/test_stream.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from io import BytesIO
77

88
from six.moves import http_client
9+
from six.moves.urllib.response import addinfourl
910

1011
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
1112
HTMLUnicodeInputStream, HTMLBinaryInputStream)
@@ -170,6 +171,17 @@ def makefile(self, _mode, _bufsize=None):
170171
stream = HTMLInputStream(source)
171172
self.assertEqual(stream.charsUntil(" "), "Text")
172173

174+
source = http_client.HTTPResponse(FakeSocket())
175+
source.begin()
176+
try:
177+
source = addinfourl(source, None, None)
178+
except AttributeError:
179+
# Fails on Python 2.x.
180+
# Apparently, addinfourl it only used with HTTPResponse on 3.x
181+
pass
182+
else:
183+
stream = HTMLInputStream(source)
184+
self.assertEqual(stream.charsUntil(" "), "Text")
173185

174186
def buildTestSuite():
175187
return unittest.defaultTestLoader.loadTestsFromName(__name__)

0 commit comments

Comments
 (0)
0