Commits

Georg Brandl  committed a748d7b

Fix #767: safely encode SphinxErrors when printing to sys.stderr.

  • Participants
  • Parent commits 6a5de7d

Comments (0)

Files changed (2)

File sphinx/cmdline.py

 from sphinx.application import Sphinx
 from sphinx.util import Tee, format_exception_cut_frames, save_traceback
 from sphinx.util.console import red, nocolor, color_terminal
+from sphinx.util.pycompat import terminal_safe
 
 
 def usage(argv, msg=None):
     except KeyboardInterrupt:
         if use_pdb:
             import pdb
-            print >>error, red('Interrupted while building, '
-                                   'starting debugger:')
+            print >>error, red('Interrupted while building, starting debugger:')
             traceback.print_exc()
             pdb.post_mortem(sys.exc_info()[2])
         return 1
         if use_pdb:
             import pdb
             print >>error, red('Exception occurred while building, '
-                                   'starting debugger:')
+                               'starting debugger:')
             traceback.print_exc()
             pdb.post_mortem(sys.exc_info()[2])
         else:
             print >>error
             if isinstance(err, SystemMessage):
                 print >>error, red('reST markup error:')
-                print >>error, err.args[0].encode('ascii', 'backslashreplace')
+                print >>error, terminal_safe(err.args[0])
             elif isinstance(err, SphinxError):
                 print >>error, red('%s:' % err.category)
-                print >>error, err
+                print >>error, terminal_safe(unicode(err))
             else:
                 print >>error, red('Exception occurred:')
                 print >>error, format_exception_cut_frames().rstrip()

File sphinx/util/pycompat.py

     u = ''
     # StringIO/BytesIO classes
     from io import StringIO, BytesIO, TextIOWrapper
+    # safely encode a string for printing to the terminal
+    def terminal_safe(s):
+        return s.encode('ascii', 'backslashreplace').decode('ascii')
     # support for running 2to3 over config files
     def convert_with_2to3(filepath):
         from lib2to3.refactor import RefactoringTool, get_fixers_from_package
     convert_with_2to3 = None
     def TextIOWrapper(stream, encoding):
         return codecs.lookup(encoding or 'ascii')[2](stream)
+    # safely encode a string for printing to the terminal
+    def terminal_safe(s):
+        return s.encode('ascii', 'backslashreplace')
 
 
 # ------------------------------------------------------------------------------