Removed redundant decode/encode.

This commit is contained in:
Jakub Roztocil 2012-07-29 03:52:24 +02:00
parent f5bc081fda
commit 16635870e3

View File

@ -30,7 +30,7 @@ def format(msg, prettifier=None, with_headers=True, with_body=True,
env=Environment()): env=Environment()):
"""Return a UTF8-encoded representation of a `models.HTTPMessage`. """Return a UTF8-encoded representation of a `models.HTTPMessage`.
Sometimes it contains binary data so we always return `bytes`. Sometimes the body contains binary data so we always return `bytes`.
If `prettifier` is set or the output is a terminal then a binary If `prettifier` is set or the output is a terminal then a binary
body is not included in the output and is replaced with notice. body is not included in the output and is replaced with notice.
@ -44,14 +44,12 @@ def format(msg, prettifier=None, with_headers=True, with_body=True,
chunks = [] chunks = []
if with_headers: if with_headers:
headers = '\n'.join([msg.line, msg.headers]).encode('utf8') headers = '\n'.join([msg.line, msg.headers])
if prettifier: if prettifier:
# Prettifies work on unicode headers = prettifier.process_headers(headers)
headers = prettifier.process_headers(
headers.decode('utf8')).encode('utf8')
chunks.append(headers.strip()) chunks.append(headers.strip().encode('utf8'))
if with_body and msg.body or env.stdout_isatty: if with_body and msg.body or env.stdout_isatty:
chunks.append(b'\n\n') chunks.append(b'\n\n')
@ -62,37 +60,29 @@ def format(msg, prettifier=None, with_headers=True, with_body=True,
bin_suppressed = False bin_suppressed = False
if prettifier or env.stdout_isatty: if prettifier or env.stdout_isatty:
# Convert body to UTF8.
try: try:
body = msg.body.decode(msg.encoding or 'utf8') body = msg.body.decode(msg.encoding or 'utf8')
except UnicodeDecodeError: except UnicodeDecodeError:
# Assume binary. It could also be that `self.encoding` # Assume binary
# doesn't correspond to the actual encoding.
bin_suppressed = True bin_suppressed = True
body = BINARY_SUPPRESSED_NOTICE.encode('utf8') body = BINARY_SUPPRESSED_NOTICE.encode('utf8')
if not with_headers: if not with_headers:
body = b'\n' + body body = b'\n' + body
else: else:
# Convert (possibly back) to UTF8.
body = body.encode('utf8') body = body.encode('utf8')
if not bin_suppressed and prettifier and msg.content_type: if not bin_suppressed and prettifier and msg.content_type:
# Prettifies work on unicode.
body = (prettifier body = (prettifier
.process_body(body.decode('utf8'), .process_body(body.decode('utf8'), msg.content_type)
msg.content_type) .strip()
.encode('utf8').strip()) .encode('utf8'))
chunks.append(body) chunks.append(body)
if env.stdout_isatty: if env.stdout_isatty:
chunks.append(b'\n\n') chunks.append(b'\n\n')
formatted = b''.join(chunks) return b''.join(chunks)
return formatted
class HTTPLexer(lexer.RegexLexer): class HTTPLexer(lexer.RegexLexer):