Skip to content

Commit

Permalink
Simplify assert_header_parsing; fixes urllib3#1438
Browse files Browse the repository at this point in the history
  • Loading branch information
timb07 committed Sep 12, 2018
1 parent 0796637 commit 7e78f79
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/urllib3/exceptions.py
Expand Up @@ -236,8 +236,8 @@ def __init__(self, scheme):

class HeaderParsingError(HTTPError):
"Raised by assert_header_parsing, but we convert it to a log.warning statement."
def __init__(self, defects, unparsed_data):
message = '%s, unparsed data: %r' % (defects or 'Unknown', unparsed_data)
def __init__(self, defects):
message = '%s' % (defects or 'Unknown')
super(HeaderParsingError, self).__init__(message)


Expand Down
10 changes: 2 additions & 8 deletions src/urllib3/util/response.py
Expand Up @@ -37,7 +37,6 @@ def is_fp_closed(obj):

def assert_header_parsing(headers):
"""
Asserts whether all headers have been successfully parsed.
Extracts encountered errors from the result of parsing headers.
Only works on Python 3.
Expand All @@ -56,14 +55,9 @@ def assert_header_parsing(headers):
type(headers)))

defects = getattr(headers, 'defects', None)
get_payload = getattr(headers, 'get_payload', None)

unparsed_data = None
if get_payload: # Platform-specific: Python 3.
unparsed_data = get_payload()

if defects or unparsed_data:
raise HeaderParsingError(defects=defects, unparsed_data=unparsed_data)
if defects:
raise HeaderParsingError(defects=defects)


def is_response_to_head(response):
Expand Down
3 changes: 1 addition & 2 deletions test/test_exceptions.py
Expand Up @@ -32,7 +32,6 @@ def test_exceptions(self, exception):

class TestFormat(object):
def test_header_parsing_errors(self):
hpe = HeaderParsingError('defects', 'unparsed_data')
hpe = HeaderParsingError('defects')

assert 'defects' in str(hpe)
assert 'unparsed_data' in str(hpe)

0 comments on commit 7e78f79

Please sign in to comment.