From 249e2d3af1ef8c1af0cb47e335045f2391bb81d4 2013-07-25 23:52:39 From: David Wolever Date: 2013-07-25 23:52:39 Subject: [PATCH] Raise a named exception when pandoc is missing In the normal case this will case the interpriter to exit just like sys.exit(...), but it will make it possible for callers to explicitly catch the exception too. Also, removed parens from the pandoc URL so it's easier to triple-click-copy-and-paste. --- diff --git a/IPython/nbconvert/utils/pandoc.py b/IPython/nbconvert/utils/pandoc.py index 299b648..c853d3c 100644 --- a/IPython/nbconvert/utils/pandoc.py +++ b/IPython/nbconvert/utils/pandoc.py @@ -14,7 +14,6 @@ from __future__ import print_function # Stdlib imports -import sys import subprocess # IPython imports @@ -24,6 +23,15 @@ from IPython.utils.py3compat import cast_bytes # Classes and functions #----------------------------------------------------------------------------- +class PandocMissing(SystemExit): + """Exception raised when Pandoc is missing. + + A subclass of SystemExit so it will cause an exit if not caught, but + explicitly named so that it's possible to catch. + """ + pass + + def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): """Convert an input string in format `from` to format `to` via pandoc. @@ -49,11 +57,14 @@ def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): command.extend(extra_args) try: p = subprocess.Popen(command, - stdin=subprocess.PIPE, stdout=subprocess.PIPE + stdin=subprocess.PIPE, stdout=subprocess.PIPE + ) + except OSError as e: + raise PandocMissing( + "Error trying to run '%s': %s.\n" %(" ".join(command), e) + + "Please check that pandoc is installed:\n" + + "http://johnmacfarlane.net/pandoc/installing.html" ) - except OSError: - sys.exit("ERROR: Unable to launch pandoc. Please install pandoc:\n" - "(http://johnmacfarlane.net/pandoc/installing.html)") out, _ = p.communicate(cast_bytes(source, encoding)) out = out.decode(encoding, 'replace') return out[:-1]