From: Stefano Rivera <stefanor@debian.org>
Date: Wed, 23 Sep 2020 21:31:30 -0700
Subject: Stdlib: Handle InvalidTerminal in rlcompleter

Pypy's readline module can throw InvalidTerminal if the terminal doesn't
support "clear". This is the case for TERM=dumb, which we use for tests.

Forwarded: https://foss.heptapod.net/pypy/pypy/-/issues/3308
---
 lib-python/3/rlcompleter.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/lib-python/3/rlcompleter.py b/lib-python/3/rlcompleter.py
index 923f5c0..5a440c1 100644
--- a/lib-python/3/rlcompleter.py
+++ b/lib-python/3/rlcompleter.py
@@ -32,6 +32,7 @@ Notes:
 import atexit
 import builtins
 import __main__
+from pyrepl.unix_console import InvalidTerminal
 
 __all__ = ["Completer"]
 
@@ -76,11 +77,13 @@ class Completer:
         if not text.strip():
             if state == 0:
                 if _readline_available:
-                    readline.insert_text('\t')
-                    readline.redisplay()
-                    return ''
-                else:
-                    return '\t'
+                    try:
+                        readline.insert_text('\t')
+                        readline.redisplay()
+                        return ''
+                    except InvalidTerminal:
+                        pass
+                return '\t'
             else:
                 return None
 
