From 0fc4a806e517ed64717df107a9dca3995326a06f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 22 Aug 2018 19:21:00 -0700 Subject: [PATCH] py.builtins._totext -> string literals or six.text_type --- src/_pytest/outcomes.py | 3 +-- src/_pytest/python.py | 2 +- testing/code/test_code.py | 5 ++--- testing/code/test_excinfo.py | 7 ++++--- testing/python/metafunc.py | 8 ++------ testing/test_assertion.py | 12 ++++++------ testing/test_assertrewrite.py | 6 ++---- testing/test_capture.py | 7 ++++--- testing/test_junitxml.py | 2 +- 9 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index 63b8453b7..0a66fcab4 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -3,7 +3,6 @@ exception classes and constants handling test outcomes as well as functions creating them """ from __future__ import absolute_import, division, print_function -import py import sys @@ -21,7 +20,7 @@ class OutcomeException(BaseException): if self.msg: val = self.msg if isinstance(val, bytes): - val = py._builtin._totext(val, errors="replace") + val = val.decode("UTF-8", errors="replace") return val return "<%s instance>" % (self.__class__.__name__,) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index e269b3bb4..45ef3be61 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -741,7 +741,7 @@ class FunctionMixin(PyobjMixin): def _repr_failure_py(self, excinfo, style="long"): if excinfo.errisinstance(fail.Exception): if not excinfo.value.pytrace: - return py._builtin._totext(excinfo.value) + return six.text_type(excinfo.value) return super(FunctionMixin, self)._repr_failure_py(excinfo, style=style) def repr_failure(self, excinfo, outerr=None): diff --git a/testing/code/test_code.py b/testing/code/test_code.py index e098f136d..27916d64f 100644 --- a/testing/code/test_code.py +++ b/testing/code/test_code.py @@ -3,7 +3,6 @@ from __future__ import absolute_import, division, print_function import sys import _pytest._code -import py import pytest from test_excinfo import TWMock from six import text_type @@ -83,7 +82,7 @@ def test_code_from_func(): def test_unicode_handling(): - value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8") + value = u"ąć".encode("UTF-8") def f(): raise Exception(value) @@ -96,7 +95,7 @@ def test_unicode_handling(): @pytest.mark.skipif(sys.version_info[0] >= 3, reason="python 2 only issue") def test_unicode_handling_syntax_error(): - value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8") + value = u"ąć".encode("UTF-8") def f(): raise SyntaxError("invalid syntax", (None, 1, 3, value)) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index fbdaeacf7..f4fe9bd53 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -8,6 +8,7 @@ import textwrap import _pytest import py import pytest +import six from _pytest._code.code import ( ExceptionInfo, FormattedExcinfo, @@ -884,10 +885,10 @@ raise ValueError() class MyRepr(TerminalRepr): def toterminal(self, tw): - tw.line(py.builtin._totext("я", "utf-8")) + tw.line(u"я") - x = py.builtin._totext(MyRepr()) - assert x == py.builtin._totext("я", "utf-8") + x = six.text_type(MyRepr()) + assert x == u"я" def test_toterminal_long(self, importasmod): mod = importasmod( diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 7ef34678c..db1cd77c5 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -3,7 +3,6 @@ import re import sys import attr import _pytest._code -import py import pytest from _pytest import python, fixtures @@ -295,9 +294,7 @@ class TestMetafunc(object): ) assert result == ["a0-1.0", "a1-b1"] # unicode mixing, issue250 - result = idmaker( - (py.builtin._totext("a"), "b"), [pytest.param({}, b"\xc3\xb4")] - ) + result = idmaker((u"a", "b"), [pytest.param({}, b"\xc3\xb4")]) assert result == ["a0-\\xc3\\xb4"] def test_idmaker_with_bytes_regex(self): @@ -309,7 +306,6 @@ class TestMetafunc(object): def test_idmaker_native_strings(self): from _pytest.python import idmaker - totext = py.builtin._totext result = idmaker( ("a", "b"), [ @@ -324,7 +320,7 @@ class TestMetafunc(object): pytest.param({7}, set("seven")), pytest.param(tuple("eight"), (8, -8, 8)), pytest.param(b"\xc3\xb4", b"name"), - pytest.param(b"\xc3\xb4", totext("other")), + pytest.param(b"\xc3\xb4", u"other"), ], ) assert result == [ diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 23763f078..501477810 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -509,12 +509,12 @@ class TestAssert_reprcompare(object): assert "raised in repr()" not in expl def test_unicode(self): - left = py.builtin._totext("£€", "utf-8") - right = py.builtin._totext("£", "utf-8") + left = u"£€" + right = u"£" expl = callequal(left, right) - assert expl[0] == py.builtin._totext("'£€' == '£'", "utf-8") - assert expl[1] == py.builtin._totext("- £€", "utf-8") - assert expl[2] == py.builtin._totext("+ £", "utf-8") + assert expl[0] == u"'£€' == '£'" + assert expl[1] == u"- £€" + assert expl[2] == u"+ £" def test_nonascii_text(self): """ @@ -542,7 +542,7 @@ class TestAssert_reprcompare(object): expl = callequal(left, right) for line in expl: assert isinstance(line, py.builtin.text) - msg = py.builtin._totext("\n").join(expl) + msg = u"\n".join(expl) assert msg diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 6cec7f003..c5cabf79b 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -654,12 +654,10 @@ class TestRewriteOnImport(object): def test_readonly(self, testdir): sub = testdir.mkdir("testing") sub.join("test_readonly.py").write( - py.builtin._totext( - """ + b""" def test_rewritten(): assert "@py_builtins" in globals() - """ - ).encode("utf-8"), + """, "wb", ) old_mode = sub.stat().mode diff --git a/testing/test_capture.py b/testing/test_capture.py index 93eaaa85c..475f61924 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function # note: py.io capture tests where copied from @@ -1083,9 +1084,9 @@ class TestStdCapture(object): def test_capturing_readouterr_unicode(self): with self.getcapture() as cap: - print("hx\xc4\x85\xc4\x87") + print("hxąć") out, err = cap.readouterr() - assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8") + assert out == u"hxąć\n" @pytest.mark.skipif( "sys.version_info >= (3,)", reason="text output different for bytes on python3" @@ -1095,7 +1096,7 @@ class TestStdCapture(object): # triggered an internal error in pytest print("\xa6") out, err = cap.readouterr() - assert out == py.builtin._totext("\ufffd\n", "unicode-escape") + assert out == u"\ufffd\n" def test_reset_twice_error(self): with self.getcapture() as cap: diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index ae2b4ea76..0678d59e8 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -941,7 +941,7 @@ def test_double_colon_split_method_issue469(testdir): def test_unicode_issue368(testdir): path = testdir.tmpdir.join("test.xml") log = LogXML(str(path), None) - ustr = py.builtin._totext("ВНИ!", "utf-8") + ustr = u"ВНИ!" class Report(BaseReport): longrepr = ustr