diff --git a/CHANGES b/CHANGES index e8d72d06d..3077ee31c 100644 --- a/CHANGES +++ b/CHANGES @@ -33,6 +33,9 @@ Bugs fixed * #933: Do not crash if an ``:option:`` value is malformed (contains spaces but no option name). +* #908: On Python 3, handle error messages from LaTeX correctly in the pngmath + extension. + Documentation ------------- diff --git a/sphinx/ext/pngmath.py b/sphinx/ext/pngmath.py index 3938dab17..a9a2203ac 100644 --- a/sphinx/ext/pngmath.py +++ b/sphinx/ext/pngmath.py @@ -26,7 +26,7 @@ from docutils import nodes from sphinx.errors import SphinxError from sphinx.util.png import read_png_depth, write_png_depth from sphinx.util.osutil import ensuredir, ENOENT -from sphinx.util.pycompat import b +from sphinx.util.pycompat import b, sys_encoding from sphinx.ext.mathbase import setup_math as mathbase_setup, wrap_displaymath class MathExtError(SphinxError): @@ -34,9 +34,9 @@ class MathExtError(SphinxError): def __init__(self, msg, stderr=None, stdout=None): if stderr: - msg += '\n[stderr]\n' + stderr + msg += '\n[stderr]\n' + stderr.decode(sys_encoding, 'replace') if stdout: - msg += '\n[stdout]\n' + stdout + msg += '\n[stdout]\n' + stdout.decode(sys_encoding, 'replace') SphinxError.__init__(self, msg) @@ -192,11 +192,11 @@ def html_visit_math(self, node): try: fname, depth = render_math(self, '$'+node['latex']+'$') except MathExtError, exc: - msg = unicode(str(exc), 'utf-8', 'replace') + msg = unicode(exc) sm = nodes.system_message(msg, type='WARNING', level=2, backrefs=[], source=node['latex']) sm.walkabout(self) - self.builder.warn('display latex %r: ' % node['latex'] + str(exc)) + self.builder.warn('display latex %r: ' % node['latex'] + msg) raise nodes.SkipNode if fname is None: # something failed -- use text-only as a bad substitute diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py index 3d252c917..1e5ea3145 100644 --- a/sphinx/util/pycompat.py +++ b/sphinx/util/pycompat.py @@ -30,6 +30,9 @@ if sys.version_info >= (3, 0): # safely encode a string for printing to the terminal def terminal_safe(s): return s.encode('ascii', 'backslashreplace').decode('ascii') + # some kind of default system encoding; should be used with a lenient + # error handler + sys_encoding = sys.getdefaultencoding() # support for running 2to3 over config files def convert_with_2to3(filepath): from lib2to3.refactor import RefactoringTool, get_fixers_from_package @@ -62,6 +65,10 @@ else: # safely encode a string for printing to the terminal def terminal_safe(s): return s.encode('ascii', 'backslashreplace') + # some kind of default system encoding; should be used with a lenient + # error handler + import locale + sys_encoding = locale.getpreferredencoding() def execfile_(filepath, _globals):