Clean up u' prefixes and py2 bytes conversions

This commit is contained in:
Anthony Sottile
2019-06-04 17:48:06 -07:00
parent 0f4992c223
commit be2be040f9
17 changed files with 37 additions and 73 deletions

View File

@@ -6,7 +6,6 @@ import itertools
import marshal
import os
import re
import string
import struct
import sys
import types
@@ -336,8 +335,8 @@ def _write_pyc(state, co, source_stat, pyc):
return True
RN = "\r\n".encode()
N = "\n".encode()
RN = b"\r\n"
N = b"\n"
cookie_re = re.compile(r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+")
BOM_UTF8 = "\xef\xbb\xbf"
@@ -420,15 +419,7 @@ def _saferepr(obj):
JSON reprs.
"""
r = saferepr(obj)
# only occurs in python2.x, repr must return text in python3+
if isinstance(r, bytes):
# Represent unprintable bytes as `\x##`
r = "".join(
"\\x{:x}".format(ord(c)) if c not in string.printable else c.decode()
for c in r
)
return r.replace("\n", "\\n")
return saferepr(obj).replace("\n", "\\n")
def _format_assertmsg(obj):
@@ -448,9 +439,6 @@ def _format_assertmsg(obj):
obj = saferepr(obj)
replaces.append(("\\n", "\n~"))
if isinstance(obj, bytes):
replaces = [(r1.encode(), r2.encode()) for r1, r2 in replaces]
for r1, r2 in replaces:
obj = obj.replace(r1, r2)

View File

@@ -13,15 +13,6 @@ from _pytest._io.saferepr import saferepr
_reprcompare = None
# the re-encoding is needed for python2 repr
# with non-ascii characters (see issue 877 and 1379)
def ecu(s):
if isinstance(s, bytes):
return s.decode("UTF-8", "replace")
else:
return s
def format_explanation(explanation):
"""This formats an explanation
@@ -32,7 +23,7 @@ def format_explanation(explanation):
for when one explanation needs to span multiple lines, e.g. when
displaying diffs.
"""
explanation = ecu(explanation)
explanation = explanation
lines = _split_explanation(explanation)
result = _format_lines(lines)
return "\n".join(result)
@@ -135,7 +126,7 @@ def assertrepr_compare(config, op, left, right):
left_repr = saferepr(left, maxsize=int(width // 2))
right_repr = saferepr(right, maxsize=width - len(left_repr))
summary = "{} {} {}".format(ecu(left_repr), op, ecu(right_repr))
summary = "{} {} {}".format(left_repr, op, right_repr)
verbose = config.getoption("verbose")
explanation = None

View File

@@ -170,7 +170,7 @@ def ascii_escaped(val):
"""If val is pure ascii, returns it as a str(). Otherwise, escapes
bytes objects into a sequence of escaped bytes:
b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6'
b'\xc3\xb4\xc5\xd6' -> '\\xc3\\xb4\\xc5\\xd6'
and escapes unicode objects into a sequence of escaped unicode
ids, e.g.:

View File

@@ -134,9 +134,7 @@ def create_cleanup_lock(p):
raise
else:
pid = os.getpid()
spid = str(pid)
if not isinstance(spid, bytes):
spid = spid.encode("ascii")
spid = str(pid).encode()
os.write(fd, spid)
os.close(fd)
if not lock_path.is_file():