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

Commit bf7da77

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

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

html5lib/inputstream.py

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

56
import codecs
67
import re
@@ -130,9 +131,12 @@ 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
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))):
136140
isUnicode = False
137141
elif hasattr(source, "read"):
138142
isUnicode = isinstance(source.read(0), text_type)

html5lib/tests/test_stream.py

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

8-
from six.moves import http_client
9+
import six
10+
from six.moves import http_client, urllib
911

1012
from html5lib.inputstream import (BufferedStream, HTMLInputStream,
1113
HTMLUnicodeInputStream, HTMLBinaryInputStream)
@@ -170,6 +172,24 @@ def makefile(self, _mode, _bufsize=None):
170172
stream = HTMLInputStream(source)
171173
self.assertEqual(stream.charsUntil(" "), "Text")
172174

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

174194
def buildTestSuite():
175195
return unittest.defaultTestLoader.loadTestsFromName(__name__)

0 commit comments

Comments
 (0)