Skip to content

Commit c4d1655

Browse files
committed
fixup! Add a more general fix for #127 (CPy #20007) based on #136.
1 parent 498629d commit c4d1655

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
@@ -120,13 +120,15 @@ def _readFromBuffer(self, bytes):
120120

121121

122122
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True):
123-
if hasattr(source, "read"):
124-
if isinstance(get_method_self(source.read), http_client.HTTPResponse):
125-
# Work around Python bug #20007: read(0) closes the connection.
126-
# http://bugs.python.org/issue20007
127-
isUnicode = False
128-
else:
129-
isUnicode = isinstance(source.read(0), text_type)
123+
# Work around Python bug #20007: read(0) closes the connection.
124+
# http://bugs.python.org/issue20007
125+
if (isinstance(source, http_client.HTTPResponse) or
126+
# Also check for addinfourl wrapping HTTPResponse
127+
(isinstance(source, urllib.response.addbase) and
128+
isinstance(source.fp, http_client.HTTPResponse))):
129+
isUnicode = False
130+
elif hasattr(source, "read"):
131+
isUnicode = isinstance(source.read(0), text_type)
130132
else:
131133
isUnicode = isinstance(source, text_type)
132134

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy