From 78a027e128d8846a2babdffee5c38c374e5c93d9 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Wed, 30 Aug 2017 16:39:44 +0200 Subject: [PATCH 1/3] simplyfy ascii escaping by using backslashreplace error handling --- _pytest/compat.py | 9 +-------- changelog/2734.trivial | 1 + 2 files changed, 2 insertions(+), 8 deletions(-) create mode 100644 changelog/2734.trivial diff --git a/_pytest/compat.py b/_pytest/compat.py index 45f9f86d4..edb68c075 100644 --- a/_pytest/compat.py +++ b/_pytest/compat.py @@ -121,7 +121,6 @@ if sys.version_info[:2] == (2, 6): if _PY3: - import codecs imap = map izip = zip STRING_TYPES = bytes, str @@ -146,13 +145,7 @@ if _PY3: """ if isinstance(val, bytes): - if val: - # source: http://goo.gl/bGsnwC - encoded_bytes, _ = codecs.escape_encode(val) - return encoded_bytes.decode('ascii') - else: - # empty bytes crashes codecs.escape_encode (#1087) - return '' + return val.decode('ascii', 'backslashreplace') else: return val.encode('unicode_escape').decode('ascii') else: diff --git a/changelog/2734.trivial b/changelog/2734.trivial new file mode 100644 index 000000000..bbf701d16 --- /dev/null +++ b/changelog/2734.trivial @@ -0,0 +1 @@ +- simplify ascii string escaping by using the backslashreplace error handler \ No newline at end of file From 13eac944ae860a3cc4dcde49a5a7dca814941670 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Mon, 4 Sep 2017 21:25:46 +0200 Subject: [PATCH 2/3] restore ascii escaping for python 3.3/3.4 --- _pytest/compat.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/_pytest/compat.py b/_pytest/compat.py index edb68c075..e93cf6c2a 100644 --- a/_pytest/compat.py +++ b/_pytest/compat.py @@ -7,6 +7,7 @@ import inspect import types import re import functools +import codecs import py @@ -126,6 +127,19 @@ if _PY3: STRING_TYPES = bytes, str UNICODE_TYPES = str, + if PY35: + def _bytes_to_ascii(val): + return val.decode('ascii', 'backslashreplace') + else: + def _bytes_to_ascii(val): + if val: + # source: http://goo.gl/bGsnwC + encoded_bytes, _ = codecs.escape_encode(val) + return encoded_bytes.decode('ascii') + else: + # empty bytes crashes codecs.escape_encode (#1087) + return '' + def _ascii_escaped(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: @@ -145,7 +159,7 @@ if _PY3: """ if isinstance(val, bytes): - return val.decode('ascii', 'backslashreplace') + return _bytes_to_ascii(val) else: return val.encode('unicode_escape').decode('ascii') else: From 3d707270213091e231ebadfb526e6da4135685d6 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 5 Sep 2017 19:36:53 -0300 Subject: [PATCH 3/3] Improve wording in changelog entry --- changelog/2734.trivial | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/2734.trivial b/changelog/2734.trivial index bbf701d16..b3f8471af 100644 --- a/changelog/2734.trivial +++ b/changelog/2734.trivial @@ -1 +1 @@ -- simplify ascii string escaping by using the backslashreplace error handler \ No newline at end of file +Internal refactor: simplify ascii string escaping by using the backslashreplace error handler in newer Python 3 versions.