Skip to content

Commit 3b3bc21

Browse files
committed
fixup! Add a more general fix for html5lib#127 (CPy #20007) based on html5lib#136.
1 parent 9d5e39c commit 3b3bc21

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

html5lib/inputstream.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import, division, unicode_literals
22

3-
from six import text_type, get_method_self
4-
from six.moves import http_client
3+
from six import text_type
4+
from six.moves import http_client, urllib
55

66
import codecs
77
import re
@@ -131,13 +131,15 @@ def _readFromBuffer(self, bytes):
131131

132132

133133
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True):
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)
134+
# Work around Python bug #20007: read(0) closes the connection.
135+
# http://bugs.python.org/issue20007
136+
if (isinstance(source, http_client.HTTPResponse) or
137+
# Also check for addinfourl wrapping HTTPResponse
138+
(isinstance(source, urllib.response.addbase) and
139+
isinstance(source.fp, http_client.HTTPResponse))):
140+
isUnicode = False
141+
elif hasattr(source, "read"):
142+
isUnicode = isinstance(source.read(0), text_type)
141143
else:
142144
isUnicode = isinstance(source, text_type)
143145

html5lib/tests/test_stream.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import unittest
55
import codecs
66
from io import BytesIO
7+
import socket
78

9+
import six
810
from six.moves import http_client, urllib
911

1012
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
@@ -175,14 +177,17 @@ def test_python_issue_20007_b(self):
175177
Make sure we have a work-around for Python bug #20007
176178
http://bugs.python.org/issue20007
177179
"""
180+
if six.PY2:
181+
return
182+
178183
class FakeSocket(object):
179184
def makefile(self, _mode, _bufsize=None):
180185
return BytesIO(b"HTTP/1.1 200 Ok\r\n\r\nText")
181186

182187
source = http_client.HTTPResponse(FakeSocket())
183188
source.begin()
184189
wrapped = urllib.response.addinfourl(source, source.msg, "http://example.com")
185-
stream = HTMLInputStream(source)
190+
stream = HTMLInputStream(wrapped)
186191
self.assertEqual(stream.charsUntil(" "), "Text")
187192

188193

0 commit comments

Comments
 (0)