Skip to content

Clean up u' prefixes and py2 bytes conversions #5400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/en/tmpdir.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 3 additions & 15 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import marshal
import os
import re
import string
import struct
import sys
import types
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down
13 changes: 2 additions & 11 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
_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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand Down
4 changes: 1 addition & 3 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
14 changes: 3 additions & 11 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def test_parametrized_with_null_bytes(self, testdir):
"""\
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
"""
Expand Down Expand Up @@ -998,16 +998,8 @@ def main():

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

Expand Down
2 changes: 1 addition & 1 deletion testing/code/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_show_traceback_import_error_unicode(self, testdir):
"""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(
[
Expand Down
2 changes: 1 addition & 1 deletion testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def test_unicode():
@pytest.mark.parametrize("method", ["fd", "sys"])
def test_capturing_bytes_in_utf8_encoding(testdir, method):
testdir.makepyfile(
"""
"""\
def test_unicode():
print('b\\u00f6y')
"""
"""
)
result = testdir.runpytest("--capture=%s" % method)
result.stdout.fnmatch_lines(["*1 passed*"])
Expand Down Expand Up @@ -511,7 +511,7 @@ def test_stdfd_functional(self, testdir):
"""\
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()
Expand Down Expand Up @@ -564,7 +564,7 @@ def test_keyboardinterrupt_disables_capturing(self, testdir):
"""\
def test_hello(capfd):
import os
os.write(1, str(42).encode('ascii'))
os.write(1, b'42')
raise KeyboardInterrupt()
"""
)
Expand Down Expand Up @@ -1136,12 +1136,12 @@ 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
"""
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(
Expand Down
4 changes: 2 additions & 2 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_encoding(self, testdir, test_string, encoding):
)
)
doctest = """
>>> u"{}"
>>> "{}"
{}
""".format(
test_string, repr(test_string)
Expand Down Expand Up @@ -671,7 +671,7 @@ def test_print_unicode_value(self, testdir):
test_print_unicode_value=r"""
Here is a doctest::

>>> print(u'\xE5\xE9\xEE\xF8\xFC')
>>> print('\xE5\xE9\xEE\xF8\xFC')
åéîøü
"""
)
Expand Down
6 changes: 3 additions & 3 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,12 +873,12 @@ 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
"""
"""
)
result, dom = runandparse(testdir)
assert result.ret == 0
Expand Down
2 changes: 1 addition & 1 deletion testing/test_nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions testing/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,7 @@ def some_internal_function():
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.
Expand All @@ -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: ☺"])
Expand Down Expand Up @@ -790,7 +788,7 @@ def pytest_runtest_makereport():
outcome = yield
rep = outcome.get_result()
if rep.when == "call":
rep.longrepr = u'ä'
rep.longrepr = 'ä'
"""
)
testdir.makepyfile(
Expand Down
1 change: 0 additions & 1 deletion testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,6 @@ def check(msg, width, expected):
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 - 😄😄...")
Expand Down
8 changes: 4 additions & 4 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -207,13 +207,13 @@ def test_show_warning():
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")
result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"])
Expand Down