Merge pull request #5400 from asottile/prefixes

Clean up u' prefixes and py2 bytes conversions
This commit is contained in:
Anthony Sottile 2019-06-04 19:03:12 -07:00 committed by GitHub
commit 5fdc2d7744
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 37 additions and 73 deletions

View File

@ -22,7 +22,7 @@ created in the `base temporary directory`_.
# content of test_tmp_path.py
import os
CONTENT = u"content"
CONTENT = "content"
def test_create_file(tmp_path):

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():

View File

@ -510,7 +510,7 @@ class TestGeneralUsage:
"""\
import pytest
@pytest.mark.parametrize("data", [b"\\x00", "\\x00", u'ação'])
@pytest.mark.parametrize("data", [b"\\x00", "\\x00", 'ação'])
def test_foo(data):
assert data
"""
@ -998,16 +998,8 @@ def test_zipimport_hook(testdir, tmpdir):
def test_import_plugin_unicode_name(testdir):
testdir.makepyfile(myplugin="")
testdir.makepyfile(
"""
def test(): pass
"""
)
testdir.makeconftest(
"""
pytest_plugins = [u'myplugin']
"""
)
testdir.makepyfile("def test(): pass")
testdir.makeconftest("pytest_plugins = ['myplugin']")
r = testdir.runpytest()
assert r.ret == 0

View File

@ -28,7 +28,7 @@ def test_source_str_function():
def test_unicode():
x = Source("4")
assert str(x) == "4"
co = _pytest._code.compile('u"å"', mode="eval")
co = _pytest._code.compile('"å"', mode="eval")
val = eval(co)
assert isinstance(val, str)

View File

@ -116,7 +116,7 @@ class TestModule:
"""Check test modules collected which raise ImportError with unicode messages
are handled properly (#2336).
"""
testdir.makepyfile("raise ImportError(u'Something bad happened ☺')")
testdir.makepyfile("raise ImportError('Something bad happened ☺')")
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[

View File

@ -1219,7 +1219,7 @@ def test_assert_with_unicode(monkeypatch, testdir):
testdir.makepyfile(
"""\
def test_unicode():
assert u'유니코드' == u'Unicode'
assert '유니코드' == 'Unicode'
"""
)
result = testdir.runpytest()

View File

@ -111,7 +111,7 @@ def test_capturing_unicode(testdir, method):
@pytest.mark.parametrize("method", ["fd", "sys"])
def test_capturing_bytes_in_utf8_encoding(testdir, method):
testdir.makepyfile(
"""
"""\
def test_unicode():
print('b\\u00f6y')
"""
@ -511,7 +511,7 @@ class TestCaptureFixture:
"""\
def test_hello(capfd):
import os
os.write(1, "42".encode('ascii'))
os.write(1, b"42")
out, err = capfd.readouterr()
assert out.startswith("42")
capfd.close()
@ -564,7 +564,7 @@ class TestCaptureFixture:
"""\
def test_hello(capfd):
import os
os.write(1, str(42).encode('ascii'))
os.write(1, b'42')
raise KeyboardInterrupt()
"""
)
@ -1136,10 +1136,10 @@ class TestStdCaptureFD(TestStdCapture):
def test_simple_only_fd(self, testdir):
testdir.makepyfile(
"""
"""\
import os
def test_x():
os.write(1, "hello\\n".encode("ascii"))
os.write(1, b"hello\\n")
assert 0
"""
)

View File

@ -153,7 +153,7 @@ class TestDoctests:
)
)
doctest = """
>>> u"{}"
>>> "{}"
{}
""".format(
test_string, repr(test_string)
@ -671,7 +671,7 @@ class TestDoctests:
test_print_unicode_value=r"""
Here is a doctest::
>>> print(u'\xE5\xE9\xEE\xF8\xFC')
>>> print('\xE5\xE9\xEE\xF8\xFC')
åéîøü
"""
)

View File

@ -873,9 +873,9 @@ def test_logxml_check_isdir(testdir):
def test_escaped_parametrized_names_xml(testdir):
testdir.makepyfile(
"""
"""\
import pytest
@pytest.mark.parametrize('char', [u"\\x00"])
@pytest.mark.parametrize('char', ["\\x00"])
def test_func(char):
assert char
"""

View File

@ -370,7 +370,7 @@ def test_skip_test_with_unicode(testdir):
import unittest
class TestClass():
def test_io(self):
raise unittest.SkipTest(u'😊')
raise unittest.SkipTest('😊')
"""
)
result = testdir.runpytest()

View File

@ -201,12 +201,10 @@ def test_makepyfile_utf8(testdir):
"""Ensure makepyfile accepts utf-8 bytes as input (#2738)"""
utf8_contents = """
def setup_function(function):
mixed_encoding = u'São Paulo'
""".encode(
"utf-8"
)
mixed_encoding = 'São Paulo'
""".encode()
p = testdir.makepyfile(utf8_contents)
assert "mixed_encoding = u'São Paulo'".encode() in p.read("rb")
assert "mixed_encoding = 'São Paulo'".encode() in p.read("rb")
class TestInlineRunModulesCleanup:

View File

@ -632,8 +632,7 @@ def test_pytest_fail_notrace_collection(testdir):
assert "def some_internal_function()" not in result.stdout.str()
@pytest.mark.parametrize("str_prefix", ["u", ""])
def test_pytest_fail_notrace_non_ascii(testdir, str_prefix):
def test_pytest_fail_notrace_non_ascii(testdir):
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
This tests with native and unicode strings containing non-ascii chars.
@ -643,9 +642,8 @@ def test_pytest_fail_notrace_non_ascii(testdir, str_prefix):
import pytest
def test_hello():
pytest.fail(%s'oh oh: ☺', pytrace=False)
pytest.fail('oh oh: ☺', pytrace=False)
"""
% str_prefix
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"])
@ -790,7 +788,7 @@ def test_unicode_in_longrepr(testdir):
outcome = yield
rep = outcome.get_result()
if rep.when == "call":
rep.longrepr = u'ä'
rep.longrepr = 'ä'
"""
)
testdir.makepyfile(

View File

@ -1672,7 +1672,6 @@ def test_line_with_reprcrash(monkeypatch):
check("😄😄😄😄😄\n2nd line", 29, "FAILED some::nodeid - 😄😄...")
# NOTE: constructed, not sure if this is supported.
# It would fail if not using u"" in Python 2 for mocked_pos.
mocked_pos = "nodeid::😄::withunicode"
check("😄😄😄😄😄\n2nd line", 29, "FAILED nodeid::😄::withunicode")
check("😄😄😄😄😄\n2nd line", 40, "FAILED nodeid::😄::withunicode - 😄😄...")

View File

@ -130,7 +130,7 @@ def test_unicode(testdir, pyfile_with_warnings):
@pytest.fixture
def fix():
warnings.warn(u"测试")
warnings.warn("测试")
yield
def test_func(fix):
@ -207,12 +207,12 @@ def test_filterwarnings_mark(testdir, default_config):
def test_non_string_warning_argument(testdir):
"""Non-str argument passed to warning breaks pytest (#2956)"""
testdir.makepyfile(
"""
"""\
import warnings
import pytest
def test():
warnings.warn(UserWarning(1, u'foo'))
warnings.warn(UserWarning(1, 'foo'))
"""
)
result = testdir.runpytest("-W", "always")