From e253029ad0bd9a3a7cb4f477c5c8d2d23672188c Mon Sep 17 00:00:00 2001
From: Bruno Oliveira <nicoddemus@gmail.com>
Date: Wed, 15 May 2019 20:08:28 -0300
Subject: [PATCH] Handle lone surrogate unicode character not being
 representable in Jython

No tests for this because we don't test with Jython currently.

Fix #5256
---
 changelog/5256.bugfix.rst |  1 +
 src/_pytest/terminal.py   | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 changelog/5256.bugfix.rst

diff --git a/changelog/5256.bugfix.rst b/changelog/5256.bugfix.rst
new file mode 100644
index 000000000..4df83454e
--- /dev/null
+++ b/changelog/5256.bugfix.rst
@@ -0,0 +1 @@
+Handle internal error due to a lone surrogate unicode character not being representable in Jython.
diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py
index 771e6a835..1780792b4 100644
--- a/src/_pytest/terminal.py
+++ b/src/_pytest/terminal.py
@@ -998,7 +998,15 @@ def _get_line_with_reprcrash_message(config, rep, termwidth):
                     # u'😄' will result in a High Surrogate (U+D83D) character, which is
                     # rendered as u'�'; in this case we just strip that character out as it
                     # serves no purpose being rendered
-                    msg = msg.rstrip(u"\uD83D")
+                    try:
+                        surrogate = six.unichr(0xD83D)
+                        msg = msg.rstrip(surrogate)
+                    except ValueError:  # pragma: no cover
+                        # Jython cannot represent this lone surrogate at all (#5256):
+                        # ValueError: unichr() arg is a lone surrogate in range
+                        #     (0xD800, 0xDFFF) (Jython UTF-16 encoding)
+                        # ignore this case as it shouldn't appear in the string anyway
+                        pass
                 msg += ellipsis
             line += sep + msg
     return line