Clean up u' prefixes and py2 bytes conversions
This commit is contained in:
		
							parent
							
								
									0f4992c223
								
							
						
					
					
						commit
						be2be040f9
					
				|  | @ -22,7 +22,7 @@ created in the `base temporary directory`_. | ||||||
|     # content of test_tmp_path.py |     # content of test_tmp_path.py | ||||||
|     import os |     import os | ||||||
| 
 | 
 | ||||||
|     CONTENT = u"content" |     CONTENT = "content" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     def test_create_file(tmp_path): |     def test_create_file(tmp_path): | ||||||
|  |  | ||||||
|  | @ -6,7 +6,6 @@ import itertools | ||||||
| import marshal | import marshal | ||||||
| import os | import os | ||||||
| import re | import re | ||||||
| import string |  | ||||||
| import struct | import struct | ||||||
| import sys | import sys | ||||||
| import types | import types | ||||||
|  | @ -336,8 +335,8 @@ def _write_pyc(state, co, source_stat, pyc): | ||||||
|     return True |     return True | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| RN = "\r\n".encode() | RN = b"\r\n" | ||||||
| N = "\n".encode() | N = b"\n" | ||||||
| 
 | 
 | ||||||
| cookie_re = re.compile(r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+") | cookie_re = re.compile(r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+") | ||||||
| BOM_UTF8 = "\xef\xbb\xbf" | BOM_UTF8 = "\xef\xbb\xbf" | ||||||
|  | @ -420,15 +419,7 @@ def _saferepr(obj): | ||||||
|     JSON reprs. |     JSON reprs. | ||||||
| 
 | 
 | ||||||
|     """ |     """ | ||||||
|     r = saferepr(obj) |     return saferepr(obj).replace("\n", "\\n") | ||||||
|     # 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") |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def _format_assertmsg(obj): | def _format_assertmsg(obj): | ||||||
|  | @ -448,9 +439,6 @@ def _format_assertmsg(obj): | ||||||
|         obj = saferepr(obj) |         obj = saferepr(obj) | ||||||
|         replaces.append(("\\n", "\n~")) |         replaces.append(("\\n", "\n~")) | ||||||
| 
 | 
 | ||||||
|     if isinstance(obj, bytes): |  | ||||||
|         replaces = [(r1.encode(), r2.encode()) for r1, r2 in replaces] |  | ||||||
| 
 |  | ||||||
|     for r1, r2 in replaces: |     for r1, r2 in replaces: | ||||||
|         obj = obj.replace(r1, r2) |         obj = obj.replace(r1, r2) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -13,15 +13,6 @@ from _pytest._io.saferepr import saferepr | ||||||
| _reprcompare = None | _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): | def format_explanation(explanation): | ||||||
|     """This formats an 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 |     for when one explanation needs to span multiple lines, e.g. when | ||||||
|     displaying diffs. |     displaying diffs. | ||||||
|     """ |     """ | ||||||
|     explanation = ecu(explanation) |     explanation = explanation | ||||||
|     lines = _split_explanation(explanation) |     lines = _split_explanation(explanation) | ||||||
|     result = _format_lines(lines) |     result = _format_lines(lines) | ||||||
|     return "\n".join(result) |     return "\n".join(result) | ||||||
|  | @ -135,7 +126,7 @@ def assertrepr_compare(config, op, left, right): | ||||||
|     left_repr = saferepr(left, maxsize=int(width // 2)) |     left_repr = saferepr(left, maxsize=int(width // 2)) | ||||||
|     right_repr = saferepr(right, maxsize=width - len(left_repr)) |     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") |     verbose = config.getoption("verbose") | ||||||
|     explanation = None |     explanation = None | ||||||
|  |  | ||||||
|  | @ -170,7 +170,7 @@ def ascii_escaped(val): | ||||||
|     """If val is pure ascii, returns it as a str().  Otherwise, escapes |     """If val is pure ascii, returns it as a str().  Otherwise, escapes | ||||||
|     bytes objects into a sequence of escaped bytes: |     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 |     and escapes unicode objects into a sequence of escaped unicode | ||||||
|     ids, e.g.: |     ids, e.g.: | ||||||
|  |  | ||||||
|  | @ -134,9 +134,7 @@ def create_cleanup_lock(p): | ||||||
|             raise |             raise | ||||||
|     else: |     else: | ||||||
|         pid = os.getpid() |         pid = os.getpid() | ||||||
|         spid = str(pid) |         spid = str(pid).encode() | ||||||
|         if not isinstance(spid, bytes): |  | ||||||
|             spid = spid.encode("ascii") |  | ||||||
|         os.write(fd, spid) |         os.write(fd, spid) | ||||||
|         os.close(fd) |         os.close(fd) | ||||||
|         if not lock_path.is_file(): |         if not lock_path.is_file(): | ||||||
|  |  | ||||||
|  | @ -510,7 +510,7 @@ class TestGeneralUsage: | ||||||
|             """\ |             """\ | ||||||
|             import pytest |             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): |             def test_foo(data): | ||||||
|                 assert data |                 assert data | ||||||
|             """ |             """ | ||||||
|  | @ -998,16 +998,8 @@ def test_zipimport_hook(testdir, tmpdir): | ||||||
| 
 | 
 | ||||||
| def test_import_plugin_unicode_name(testdir): | def test_import_plugin_unicode_name(testdir): | ||||||
|     testdir.makepyfile(myplugin="") |     testdir.makepyfile(myplugin="") | ||||||
|     testdir.makepyfile( |     testdir.makepyfile("def test(): pass") | ||||||
|         """ |     testdir.makeconftest("pytest_plugins = ['myplugin']") | ||||||
|         def test(): pass |  | ||||||
|     """ |  | ||||||
|     ) |  | ||||||
|     testdir.makeconftest( |  | ||||||
|         """ |  | ||||||
|         pytest_plugins = [u'myplugin'] |  | ||||||
|     """ |  | ||||||
|     ) |  | ||||||
|     r = testdir.runpytest() |     r = testdir.runpytest() | ||||||
|     assert r.ret == 0 |     assert r.ret == 0 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -28,7 +28,7 @@ def test_source_str_function(): | ||||||
| def test_unicode(): | def test_unicode(): | ||||||
|     x = Source("4") |     x = Source("4") | ||||||
|     assert str(x) == "4" |     assert str(x) == "4" | ||||||
|     co = _pytest._code.compile('u"å"', mode="eval") |     co = _pytest._code.compile('"å"', mode="eval") | ||||||
|     val = eval(co) |     val = eval(co) | ||||||
|     assert isinstance(val, str) |     assert isinstance(val, str) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -116,7 +116,7 @@ class TestModule: | ||||||
|         """Check test modules collected which raise ImportError with unicode messages |         """Check test modules collected which raise ImportError with unicode messages | ||||||
|         are handled properly (#2336). |         are handled properly (#2336). | ||||||
|         """ |         """ | ||||||
|         testdir.makepyfile("raise ImportError(u'Something bad happened ☺')") |         testdir.makepyfile("raise ImportError('Something bad happened ☺')") | ||||||
|         result = testdir.runpytest() |         result = testdir.runpytest() | ||||||
|         result.stdout.fnmatch_lines( |         result.stdout.fnmatch_lines( | ||||||
|             [ |             [ | ||||||
|  |  | ||||||
|  | @ -1219,7 +1219,7 @@ def test_assert_with_unicode(monkeypatch, testdir): | ||||||
|     testdir.makepyfile( |     testdir.makepyfile( | ||||||
|         """\ |         """\ | ||||||
|         def test_unicode(): |         def test_unicode(): | ||||||
|             assert u'유니코드' == u'Unicode' |             assert '유니코드' == 'Unicode' | ||||||
|         """ |         """ | ||||||
|     ) |     ) | ||||||
|     result = testdir.runpytest() |     result = testdir.runpytest() | ||||||
|  |  | ||||||
|  | @ -111,7 +111,7 @@ def test_capturing_unicode(testdir, method): | ||||||
| @pytest.mark.parametrize("method", ["fd", "sys"]) | @pytest.mark.parametrize("method", ["fd", "sys"]) | ||||||
| def test_capturing_bytes_in_utf8_encoding(testdir, method): | def test_capturing_bytes_in_utf8_encoding(testdir, method): | ||||||
|     testdir.makepyfile( |     testdir.makepyfile( | ||||||
|         """ |         """\ | ||||||
|         def test_unicode(): |         def test_unicode(): | ||||||
|             print('b\\u00f6y') |             print('b\\u00f6y') | ||||||
|         """ |         """ | ||||||
|  | @ -511,7 +511,7 @@ class TestCaptureFixture: | ||||||
|             """\ |             """\ | ||||||
|             def test_hello(capfd): |             def test_hello(capfd): | ||||||
|                 import os |                 import os | ||||||
|                 os.write(1, "42".encode('ascii')) |                 os.write(1, b"42") | ||||||
|                 out, err = capfd.readouterr() |                 out, err = capfd.readouterr() | ||||||
|                 assert out.startswith("42") |                 assert out.startswith("42") | ||||||
|                 capfd.close() |                 capfd.close() | ||||||
|  | @ -564,7 +564,7 @@ class TestCaptureFixture: | ||||||
|             """\ |             """\ | ||||||
|             def test_hello(capfd): |             def test_hello(capfd): | ||||||
|                 import os |                 import os | ||||||
|                 os.write(1, str(42).encode('ascii')) |                 os.write(1, b'42') | ||||||
|                 raise KeyboardInterrupt() |                 raise KeyboardInterrupt() | ||||||
|             """ |             """ | ||||||
|         ) |         ) | ||||||
|  | @ -1136,10 +1136,10 @@ class TestStdCaptureFD(TestStdCapture): | ||||||
| 
 | 
 | ||||||
|     def test_simple_only_fd(self, testdir): |     def test_simple_only_fd(self, testdir): | ||||||
|         testdir.makepyfile( |         testdir.makepyfile( | ||||||
|             """ |             """\ | ||||||
|             import os |             import os | ||||||
|             def test_x(): |             def test_x(): | ||||||
|                 os.write(1, "hello\\n".encode("ascii")) |                 os.write(1, b"hello\\n") | ||||||
|                 assert 0 |                 assert 0 | ||||||
|             """ |             """ | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  | @ -153,7 +153,7 @@ class TestDoctests: | ||||||
|             ) |             ) | ||||||
|         ) |         ) | ||||||
|         doctest = """ |         doctest = """ | ||||||
|             >>> u"{}" |             >>> "{}" | ||||||
|             {} |             {} | ||||||
|         """.format( |         """.format( | ||||||
|             test_string, repr(test_string) |             test_string, repr(test_string) | ||||||
|  | @ -671,7 +671,7 @@ class TestDoctests: | ||||||
|             test_print_unicode_value=r""" |             test_print_unicode_value=r""" | ||||||
|             Here is a doctest:: |             Here is a doctest:: | ||||||
| 
 | 
 | ||||||
|                 >>> print(u'\xE5\xE9\xEE\xF8\xFC') |                 >>> print('\xE5\xE9\xEE\xF8\xFC') | ||||||
|                 åéîøü |                 åéîøü | ||||||
|         """ |         """ | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  | @ -873,9 +873,9 @@ def test_logxml_check_isdir(testdir): | ||||||
| 
 | 
 | ||||||
| def test_escaped_parametrized_names_xml(testdir): | def test_escaped_parametrized_names_xml(testdir): | ||||||
|     testdir.makepyfile( |     testdir.makepyfile( | ||||||
|         """ |         """\ | ||||||
|         import pytest |         import pytest | ||||||
|         @pytest.mark.parametrize('char', [u"\\x00"]) |         @pytest.mark.parametrize('char', ["\\x00"]) | ||||||
|         def test_func(char): |         def test_func(char): | ||||||
|             assert char |             assert char | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|  | @ -370,7 +370,7 @@ def test_skip_test_with_unicode(testdir): | ||||||
|         import unittest |         import unittest | ||||||
|         class TestClass(): |         class TestClass(): | ||||||
|             def test_io(self): |             def test_io(self): | ||||||
|                 raise unittest.SkipTest(u'😊') |                 raise unittest.SkipTest('😊') | ||||||
|         """ |         """ | ||||||
|     ) |     ) | ||||||
|     result = testdir.runpytest() |     result = testdir.runpytest() | ||||||
|  |  | ||||||
|  | @ -201,12 +201,10 @@ def test_makepyfile_utf8(testdir): | ||||||
|     """Ensure makepyfile accepts utf-8 bytes as input (#2738)""" |     """Ensure makepyfile accepts utf-8 bytes as input (#2738)""" | ||||||
|     utf8_contents = """ |     utf8_contents = """ | ||||||
|         def setup_function(function): |         def setup_function(function): | ||||||
|             mixed_encoding = u'São Paulo' |             mixed_encoding = 'São Paulo' | ||||||
|     """.encode( |     """.encode() | ||||||
|         "utf-8" |  | ||||||
|     ) |  | ||||||
|     p = testdir.makepyfile(utf8_contents) |     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: | class TestInlineRunModulesCleanup: | ||||||
|  |  | ||||||
|  | @ -632,8 +632,7 @@ def test_pytest_fail_notrace_collection(testdir): | ||||||
|     assert "def some_internal_function()" not in result.stdout.str() |     assert "def some_internal_function()" not in result.stdout.str() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @pytest.mark.parametrize("str_prefix", ["u", ""]) | def test_pytest_fail_notrace_non_ascii(testdir): | ||||||
| def test_pytest_fail_notrace_non_ascii(testdir, str_prefix): |  | ||||||
|     """Fix pytest.fail with pytrace=False with non-ascii characters (#1178). |     """Fix pytest.fail with pytrace=False with non-ascii characters (#1178). | ||||||
| 
 | 
 | ||||||
|     This tests with native and unicode strings containing non-ascii chars. |     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 |         import pytest | ||||||
| 
 | 
 | ||||||
|         def test_hello(): |         def test_hello(): | ||||||
|             pytest.fail(%s'oh oh: ☺', pytrace=False) |             pytest.fail('oh oh: ☺', pytrace=False) | ||||||
|         """ |         """ | ||||||
|         % str_prefix |  | ||||||
|     ) |     ) | ||||||
|     result = testdir.runpytest() |     result = testdir.runpytest() | ||||||
|     result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"]) |     result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"]) | ||||||
|  | @ -790,7 +788,7 @@ def test_unicode_in_longrepr(testdir): | ||||||
|             outcome = yield |             outcome = yield | ||||||
|             rep = outcome.get_result() |             rep = outcome.get_result() | ||||||
|             if rep.when == "call": |             if rep.when == "call": | ||||||
|                 rep.longrepr = u'ä' |                 rep.longrepr = 'ä' | ||||||
|         """ |         """ | ||||||
|     ) |     ) | ||||||
|     testdir.makepyfile( |     testdir.makepyfile( | ||||||
|  |  | ||||||
|  | @ -1672,7 +1672,6 @@ def test_line_with_reprcrash(monkeypatch): | ||||||
|     check("😄😄😄😄😄\n2nd line", 29, "FAILED some::nodeid - 😄😄...") |     check("😄😄😄😄😄\n2nd line", 29, "FAILED some::nodeid - 😄😄...") | ||||||
| 
 | 
 | ||||||
|     # NOTE: constructed, not sure if this is supported. |     # 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" |     mocked_pos = "nodeid::😄::withunicode" | ||||||
|     check("😄😄😄😄😄\n2nd line", 29, "FAILED nodeid::😄::withunicode") |     check("😄😄😄😄😄\n2nd line", 29, "FAILED nodeid::😄::withunicode") | ||||||
|     check("😄😄😄😄😄\n2nd line", 40, "FAILED nodeid::😄::withunicode - 😄😄...") |     check("😄😄😄😄😄\n2nd line", 40, "FAILED nodeid::😄::withunicode - 😄😄...") | ||||||
|  |  | ||||||
|  | @ -130,7 +130,7 @@ def test_unicode(testdir, pyfile_with_warnings): | ||||||
| 
 | 
 | ||||||
|         @pytest.fixture |         @pytest.fixture | ||||||
|         def fix(): |         def fix(): | ||||||
|             warnings.warn(u"测试") |             warnings.warn("测试") | ||||||
|             yield |             yield | ||||||
| 
 | 
 | ||||||
|         def test_func(fix): |         def test_func(fix): | ||||||
|  | @ -207,12 +207,12 @@ def test_filterwarnings_mark(testdir, default_config): | ||||||
| def test_non_string_warning_argument(testdir): | def test_non_string_warning_argument(testdir): | ||||||
|     """Non-str argument passed to warning breaks pytest (#2956)""" |     """Non-str argument passed to warning breaks pytest (#2956)""" | ||||||
|     testdir.makepyfile( |     testdir.makepyfile( | ||||||
|         """ |         """\ | ||||||
|         import warnings |         import warnings | ||||||
|         import pytest |         import pytest | ||||||
| 
 | 
 | ||||||
|         def test(): |         def test(): | ||||||
|             warnings.warn(UserWarning(1, u'foo')) |             warnings.warn(UserWarning(1, 'foo')) | ||||||
|         """ |         """ | ||||||
|     ) |     ) | ||||||
|     result = testdir.runpytest("-W", "always") |     result = testdir.runpytest("-W", "always") | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue