diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index 2f0bd6a..fb6898f 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -2,6 +2,7 @@ """Tests for IPython.core.ultratb """ import io +import logging import sys import os.path from textwrap import dedent @@ -372,3 +373,28 @@ def test_handlers(): handler(*sys.exc_info()) buff.write('') + +class TokenizeFailureTest(unittest.TestCase): + """Tests related to https://github.com/ipython/ipython/issues/6864.""" + + def testLogging(self): + message = "An unexpected error occurred while tokenizing input" + cell = 'raise ValueError("""a\nb""")' + + stream = io.StringIO() + handler = logging.StreamHandler(stream) + logger = logging.getLogger() + loglevel = logger.level + logger.addHandler(handler) + self.addCleanup(lambda: logger.removeHandler(handler)) + self.addCleanup(lambda: logger.setLevel(loglevel)) + + logger.setLevel(logging.INFO) + with tt.AssertNotPrints(message): + ip.run_cell(cell) + self.assertNotIn(message, stream.getvalue()) + + logger.setLevel(logging.DEBUG) + with tt.AssertNotPrints(message): + ip.run_cell(cell) + self.assertIn(message, stream.getvalue()) diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py index f791e2b..bce01e9 100644 --- a/IPython/core/ultratb.py +++ b/IPython/core/ultratb.py @@ -121,7 +121,7 @@ from IPython.utils import path as util_path from IPython.utils import py3compat from IPython.utils.data import uniq_stable from IPython.utils.terminal import get_terminal_size -from logging import info, error +from logging import info, error, debug import IPython.utils.colorable as colorable @@ -952,10 +952,15 @@ class VerboseTB(TBTools): # - see gh-6300 pass except tokenize.TokenError as msg: + # Tokenizing may fail for various reasons, many of which are + # harmless. (A good example is when the line in question is the + # close of a triple-quoted string, cf gh-6864). We don't want to + # show this to users, but want make it available for debugging + # purposes. _m = ("An unexpected error occurred while tokenizing input\n" "The following traceback may be corrupted or invalid\n" "The error message is: %s\n" % msg) - error(_m) + debug(_m) # Join composite names (e.g. "dict.fromkeys") names = ['.'.join(n) for n in names]