diff --git a/doc/en/tmpdir.rst b/doc/en/tmpdir.rst index 330011bb3..01397a5ba 100644 --- a/doc/en/tmpdir.rst +++ b/doc/en/tmpdir.rst @@ -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): diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index ac9b85e67..ce698f368 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -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) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 2be759bac..9d6af5d69 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -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 diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index a4f3bc003..6f1275e61 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -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.: diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 09b1bb3d5..3269c25ed 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -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(): diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 9d903f802..caa0d5191 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -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 diff --git a/testing/code/test_source.py b/testing/code/test_source.py index a12a102a0..15e0bf24a 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -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) diff --git a/testing/python/collect.py b/testing/python/collect.py index 981e30fc3..d648452fd 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -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( [ diff --git a/testing/test_assertion.py b/testing/test_assertion.py index e651c09ce..eced5461a 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -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() diff --git a/testing/test_capture.py b/testing/test_capture.py index 0825745ad..a85025c27 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -111,10 +111,10 @@ 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') - """ + """ ) result = testdir.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) @@ -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,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( diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 65c8cf366..bf0405546 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -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') åéîøü """ ) diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 69b9c09c3..bcf83b352 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -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 diff --git a/testing/test_nose.py b/testing/test_nose.py index 8a3ce6454..f60c3af53 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -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() diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 82ff37c13..96bf85040 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -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: diff --git a/testing/test_runner.py b/testing/test_runner.py index 77fdcecc3..13f722036 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -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( diff --git a/testing/test_terminal.py b/testing/test_terminal.py index f53cb6837..cc2c474ab 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -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 - 😄😄...") diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 165402478..08a368521 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -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,13 +207,13 @@ 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") result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"])