From 72c7ad9fcd71a27e1549e5bf3594a13281e86e69 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Sun, 8 Mar 2015 10:50:17 +0100 Subject: [PATCH] * wrap/python/tests/ipnbdoctest.py: Skip if IPython is missing. --- wrap/python/tests/ipnbdoctest.py | 52 ++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/wrap/python/tests/ipnbdoctest.py b/wrap/python/tests/ipnbdoctest.py index bfe3e897a..9a7d20cff 100755 --- a/wrap/python/tests/ipnbdoctest.py +++ b/wrap/python/tests/ipnbdoctest.py @@ -4,7 +4,8 @@ simple example script for running and testing notebooks. Usage: `ipnbdoctest.py foo.ipynb [bar.ipynb [...]]` -Each cell is submitted to the kernel, and the outputs are compared with those stored in the notebook. +Each cell is submitted to the kernel, and the outputs are compared +with those stored in the notebook. """ from __future__ import print_function @@ -21,6 +22,13 @@ except ImportError: print('Python 3.x is needed to run this script.') sys.exit(77) +import imp +try: + imp.find_module('IPython') +except: + print('IPython is needed to run this script.') + sys.exit(77) + try: from IPython.kernel import KernelManager except ImportError: @@ -41,23 +49,24 @@ def compare_png(a64, b64): def sanitize(s): """sanitize a string for comparison. - - fix universal newlines, strip trailing newlines, and normalize likely random values (memory addresses and UUIDs) + + fix universal newlines, strip trailing newlines, and normalize likely + random values (memory addresses and UUIDs) """ if not isinstance(s, str): return s # normalize newline: s = s.replace('\r\n', '\n') - + # ignore trailing newlines (but not space) s = s.rstrip('\n') - + # normalize hex addresses: s = re.sub(r'0x[a-f0-9]+', '0xFFFFFFFF', s) - + # normalize UUIDs: s = re.sub(r'[a-f0-9]{8}(\-[a-f0-9]{4}){3}\-[a-f0-9]{12}', 'U-U-I-D', s) - + return s @@ -66,25 +75,27 @@ def consolidate_outputs(outputs): data = defaultdict(list) data['stdout'] = '' data['stderr'] = '' - + for out in outputs: if out.type == 'stream': data[out.stream] += out.text elif out.type == 'pyerr': data['pyerr'] = dict(ename=out.ename, evalue=out.evalue) else: - for key in ('png', 'svg', 'latex', 'html', 'javascript', 'text', 'jpeg',): + for key in ('png', 'svg', 'latex', 'html', + 'javascript', 'text', 'jpeg',): if key in out: data[key].append(out[key]) return data -def compare_outputs(test, ref, skip_compare=('png', 'traceback', 'latex', 'prompt_number')): +def compare_outputs(test, ref, skip_cmp=('png', 'traceback', + 'latex', 'prompt_number')): for key in ref: if key not in test: print("missing key: %s != %s" % (test.keys(), ref.keys())) return False - elif key not in skip_compare and sanitize(test[key]) != sanitize(ref[key]): + elif key not in skip_cmp and sanitize(test[key]) != sanitize(ref[key]): print("mismatch %s:" % key) exp = ref[key] eff = test[key] @@ -104,7 +115,7 @@ def run_cell(shell, iopub, cell): # wait for finish, maximum 20s shell.get_msg(timeout=20) outs = [] - + while True: try: msg = iopub.get_msg(timeout=0.2) @@ -116,11 +127,11 @@ def run_cell(shell, iopub, cell): elif msg_type == 'clear_output': outs = [] continue - + content = msg['content'] # print msg_type, content out = NotebookNode(output_type=msg_type) - + if msg_type == 'stream': out.stream = content['name'] out.text = content['data'] @@ -139,14 +150,15 @@ def run_cell(shell, iopub, cell): out.traceback = content['traceback'] else: print("unhandled iopub msg:", msg_type) - + outs.append(out) return outs - + def test_notebook(nb): km = KernelManager() - km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) + km.start_kernel(extra_arguments=['--pylab=inline'], + stderr=open(os.devnull, 'w')) try: kc = km.client() kc.start_channels() @@ -157,7 +169,7 @@ def test_notebook(nb): kc.start_channels() iopub = kc.sub_channel shell = kc.shell_channel - + # run %pylab inline, because some notebooks assume this # even though they shouldn't shell.execute("pass") @@ -167,7 +179,7 @@ def test_notebook(nb): iopub.get_msg(timeout=1) except Empty: break - + successes = 0 failures = 0 errors = 0 @@ -182,7 +194,7 @@ def test_notebook(nb): print(cell.input) errors += 1 continue - + failed = False if len(outs) != len(cell.outputs): print("output length mismatch (expected {}, got {})".format(