Show More
@@ -1,170 +1,186 | |||
|
1 | 1 | """test the IPython Kernel""" |
|
2 | 2 | |
|
3 | 3 | #------------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2013 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #------------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import sys |
|
15 | 15 | |
|
16 | 16 | import nose.tools as nt |
|
17 | 17 | |
|
18 | 18 | from IPython.testing import decorators as dec, tools as tt |
|
19 | 19 | from IPython.utils import py3compat |
|
20 | from IPython.utils.path import locate_profile | |
|
20 | 21 | |
|
21 | 22 | from .utils import new_kernel, kernel, TIMEOUT, assemble_output, execute, flush_channels |
|
22 | 23 | |
|
23 | 24 | #------------------------------------------------------------------------------- |
|
24 | 25 | # Tests |
|
25 | 26 | #------------------------------------------------------------------------------- |
|
26 | 27 | |
|
27 | 28 | |
|
28 | 29 | def _check_mp_mode(kc, expected=False, stream="stdout"): |
|
29 | 30 | execute(kc=kc, code="import sys") |
|
30 | 31 | flush_channels(kc) |
|
31 | 32 | msg_id, content = execute(kc=kc, code="print (sys.%s._check_mp_mode())" % stream) |
|
32 | 33 | stdout, stderr = assemble_output(kc.iopub_channel) |
|
33 | 34 | nt.assert_equal(eval(stdout.strip()), expected) |
|
34 | 35 | |
|
35 | 36 | |
|
36 | 37 | # printing tests |
|
37 | 38 | |
|
38 | 39 | def test_simple_print(): |
|
39 | 40 | """simple print statement in kernel""" |
|
40 | 41 | with kernel() as kc: |
|
41 | 42 | iopub = kc.iopub_channel |
|
42 | 43 | msg_id, content = execute(kc=kc, code="print ('hi')") |
|
43 | 44 | stdout, stderr = assemble_output(iopub) |
|
44 | 45 | nt.assert_equal(stdout, 'hi\n') |
|
45 | 46 | nt.assert_equal(stderr, '') |
|
46 | 47 | _check_mp_mode(kc, expected=False) |
|
47 | 48 | |
|
48 | 49 | |
|
50 | def test_sys_path(): | |
|
51 | """test that sys.path doesn't get messed up by default""" | |
|
52 | with kernel() as kc: | |
|
53 | msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))") | |
|
54 | stdout, stderr = assemble_output(kc.iopub_channel) | |
|
55 | nt.assert_equal(stdout, "''\n") | |
|
56 | ||
|
57 | def test_sys_path_profile_dir(): | |
|
58 | """test that sys.path doesn't get messed up when `--profile-dir` is specified""" | |
|
59 | ||
|
60 | with new_kernel(['--profile-dir', locate_profile('default')]) as kc: | |
|
61 | msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))") | |
|
62 | stdout, stderr = assemble_output(kc.iopub_channel) | |
|
63 | nt.assert_equal(stdout, "''\n") | |
|
64 | ||
|
49 | 65 | @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows") |
|
50 | 66 | def test_subprocess_print(): |
|
51 | 67 | """printing from forked mp.Process""" |
|
52 | 68 | with new_kernel() as kc: |
|
53 | 69 | iopub = kc.iopub_channel |
|
54 | 70 | |
|
55 | 71 | _check_mp_mode(kc, expected=False) |
|
56 | 72 | flush_channels(kc) |
|
57 | 73 | np = 5 |
|
58 | 74 | code = '\n'.join([ |
|
59 | 75 | "from __future__ import print_function", |
|
60 | 76 | "import multiprocessing as mp", |
|
61 | 77 | "pool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]" % np, |
|
62 | 78 | "for p in pool: p.start()", |
|
63 | 79 | "for p in pool: p.join()" |
|
64 | 80 | ]) |
|
65 | 81 | |
|
66 | 82 | expected = '\n'.join([ |
|
67 | 83 | "hello %s" % i for i in range(np) |
|
68 | 84 | ]) + '\n' |
|
69 | 85 | |
|
70 | 86 | msg_id, content = execute(kc=kc, code=code) |
|
71 | 87 | stdout, stderr = assemble_output(iopub) |
|
72 | 88 | nt.assert_equal(stdout.count("hello"), np, stdout) |
|
73 | 89 | for n in range(np): |
|
74 | 90 | nt.assert_equal(stdout.count(str(n)), 1, stdout) |
|
75 | 91 | nt.assert_equal(stderr, '') |
|
76 | 92 | _check_mp_mode(kc, expected=False) |
|
77 | 93 | _check_mp_mode(kc, expected=False, stream="stderr") |
|
78 | 94 | |
|
79 | 95 | |
|
80 | 96 | def test_subprocess_noprint(): |
|
81 | 97 | """mp.Process without print doesn't trigger iostream mp_mode""" |
|
82 | 98 | with kernel() as kc: |
|
83 | 99 | iopub = kc.iopub_channel |
|
84 | 100 | |
|
85 | 101 | np = 5 |
|
86 | 102 | code = '\n'.join([ |
|
87 | 103 | "import multiprocessing as mp", |
|
88 | 104 | "pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]" % np, |
|
89 | 105 | "for p in pool: p.start()", |
|
90 | 106 | "for p in pool: p.join()" |
|
91 | 107 | ]) |
|
92 | 108 | |
|
93 | 109 | msg_id, content = execute(kc=kc, code=code) |
|
94 | 110 | stdout, stderr = assemble_output(iopub) |
|
95 | 111 | nt.assert_equal(stdout, '') |
|
96 | 112 | nt.assert_equal(stderr, '') |
|
97 | 113 | |
|
98 | 114 | _check_mp_mode(kc, expected=False) |
|
99 | 115 | _check_mp_mode(kc, expected=False, stream="stderr") |
|
100 | 116 | |
|
101 | 117 | |
|
102 | 118 | @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows") |
|
103 | 119 | def test_subprocess_error(): |
|
104 | 120 | """error in mp.Process doesn't crash""" |
|
105 | 121 | with new_kernel() as kc: |
|
106 | 122 | iopub = kc.iopub_channel |
|
107 | 123 | |
|
108 | 124 | code = '\n'.join([ |
|
109 | 125 | "import multiprocessing as mp", |
|
110 | 126 | "p = mp.Process(target=int, args=('hi',))", |
|
111 | 127 | "p.start()", |
|
112 | 128 | "p.join()", |
|
113 | 129 | ]) |
|
114 | 130 | |
|
115 | 131 | msg_id, content = execute(kc=kc, code=code) |
|
116 | 132 | stdout, stderr = assemble_output(iopub) |
|
117 | 133 | nt.assert_equal(stdout, '') |
|
118 | 134 | nt.assert_true("ValueError" in stderr, stderr) |
|
119 | 135 | |
|
120 | 136 | _check_mp_mode(kc, expected=False) |
|
121 | 137 | _check_mp_mode(kc, expected=False, stream="stderr") |
|
122 | 138 | |
|
123 | 139 | # raw_input tests |
|
124 | 140 | |
|
125 | 141 | def test_raw_input(): |
|
126 | 142 | """test [raw_]input""" |
|
127 | 143 | with kernel() as kc: |
|
128 | 144 | iopub = kc.iopub_channel |
|
129 | 145 | |
|
130 | 146 | input_f = "input" if py3compat.PY3 else "raw_input" |
|
131 | 147 | theprompt = "prompt> " |
|
132 | 148 | code = 'print({input_f}("{theprompt}"))'.format(**locals()) |
|
133 | 149 | msg_id = kc.execute(code, allow_stdin=True) |
|
134 | 150 | msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT) |
|
135 | 151 | nt.assert_equal(msg['header']['msg_type'], u'input_request') |
|
136 | 152 | content = msg['content'] |
|
137 | 153 | nt.assert_equal(content['prompt'], theprompt) |
|
138 | 154 | text = "some text" |
|
139 | 155 | kc.input(text) |
|
140 | 156 | reply = kc.get_shell_msg(block=True, timeout=TIMEOUT) |
|
141 | 157 | nt.assert_equal(reply['content']['status'], 'ok') |
|
142 | 158 | stdout, stderr = assemble_output(iopub) |
|
143 | 159 | nt.assert_equal(stdout, text + "\n") |
|
144 | 160 | |
|
145 | 161 | |
|
146 | 162 | @dec.skipif(py3compat.PY3) |
|
147 | 163 | def test_eval_input(): |
|
148 | 164 | """test input() on Python 2""" |
|
149 | 165 | with kernel() as kc: |
|
150 | 166 | iopub = kc.iopub_channel |
|
151 | 167 | |
|
152 | 168 | input_f = "input" if py3compat.PY3 else "raw_input" |
|
153 | 169 | theprompt = "prompt> " |
|
154 | 170 | code = 'print(input("{theprompt}"))'.format(**locals()) |
|
155 | 171 | msg_id = kc.execute(code, allow_stdin=True) |
|
156 | 172 | msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT) |
|
157 | 173 | nt.assert_equal(msg['header']['msg_type'], u'input_request') |
|
158 | 174 | content = msg['content'] |
|
159 | 175 | nt.assert_equal(content['prompt'], theprompt) |
|
160 | 176 | kc.input("1+1") |
|
161 | 177 | reply = kc.get_shell_msg(block=True, timeout=TIMEOUT) |
|
162 | 178 | nt.assert_equal(reply['content']['status'], 'ok') |
|
163 | 179 | stdout, stderr = assemble_output(iopub) |
|
164 | 180 | nt.assert_equal(stdout, "2\n") |
|
165 | 181 | |
|
166 | 182 | |
|
167 | 183 | def test_help_output(): |
|
168 | 184 | """ipython kernel --help-all works""" |
|
169 | 185 | tt.help_all_output_test('kernel') |
|
170 | 186 |
@@ -1,167 +1,170 | |||
|
1 | 1 | """utilities for testing IPython kernels""" |
|
2 | 2 | |
|
3 | 3 | #------------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2013 The IPython Development Team |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 7 | # the file COPYING, distributed as part of this software. |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #------------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #------------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import atexit |
|
15 | 15 | |
|
16 | 16 | from contextlib import contextmanager |
|
17 | 17 | from subprocess import PIPE |
|
18 | 18 | from Queue import Empty |
|
19 | 19 | |
|
20 | 20 | import nose.tools as nt |
|
21 | 21 | |
|
22 | 22 | from IPython.kernel import KernelManager |
|
23 | 23 | |
|
24 | 24 | #------------------------------------------------------------------------------- |
|
25 | 25 | # Globals |
|
26 | 26 | #------------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | STARTUP_TIMEOUT = 60 |
|
29 | 29 | TIMEOUT = 15 |
|
30 | 30 | |
|
31 | 31 | KM = None |
|
32 | 32 | KC = None |
|
33 | 33 | |
|
34 | 34 | #------------------------------------------------------------------------------- |
|
35 | 35 | # code |
|
36 | 36 | #------------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | |
|
39 | def start_new_kernel(): | |
|
39 | def start_new_kernel(argv=None): | |
|
40 | 40 | """start a new kernel, and return its Manager and Client""" |
|
41 | 41 | km = KernelManager() |
|
42 |
k |
|
|
42 | kwargs = dict(stdout=PIPE, stderr=PIPE) | |
|
43 | if argv: | |
|
44 | kwargs['extra_arguments'] = argv | |
|
45 | km.start_kernel(**kwargs) | |
|
43 | 46 | kc = km.client() |
|
44 | 47 | kc.start_channels() |
|
45 | 48 | |
|
46 | 49 | msg_id = kc.kernel_info() |
|
47 | 50 | kc.get_shell_msg(block=True, timeout=STARTUP_TIMEOUT) |
|
48 | 51 | flush_channels(kc) |
|
49 | 52 | return km, kc |
|
50 | 53 | |
|
51 | 54 | def flush_channels(kc=None): |
|
52 | 55 | """flush any messages waiting on the queue""" |
|
53 | 56 | from .test_message_spec import validate_message |
|
54 | 57 | |
|
55 | 58 | if kc is None: |
|
56 | 59 | kc = KC |
|
57 | 60 | for channel in (kc.shell_channel, kc.iopub_channel): |
|
58 | 61 | while True: |
|
59 | 62 | try: |
|
60 | 63 | msg = channel.get_msg(block=True, timeout=0.1) |
|
61 | 64 | except Empty: |
|
62 | 65 | break |
|
63 | 66 | else: |
|
64 | 67 | validate_message(msg) |
|
65 | 68 | |
|
66 | 69 | |
|
67 | 70 | def execute(code='', kc=None, **kwargs): |
|
68 | 71 | """wrapper for doing common steps for validating an execution request""" |
|
69 | 72 | from .test_message_spec import validate_message |
|
70 | 73 | if kc is None: |
|
71 | 74 | kc = KC |
|
72 | 75 | msg_id = kc.execute(code=code, **kwargs) |
|
73 | 76 | reply = kc.get_shell_msg(timeout=TIMEOUT) |
|
74 | 77 | validate_message(reply, 'execute_reply', msg_id) |
|
75 | 78 | busy = kc.get_iopub_msg(timeout=TIMEOUT) |
|
76 | 79 | validate_message(busy, 'status', msg_id) |
|
77 | 80 | nt.assert_equal(busy['content']['execution_state'], 'busy') |
|
78 | 81 | |
|
79 | 82 | if not kwargs.get('silent'): |
|
80 | 83 | pyin = kc.get_iopub_msg(timeout=TIMEOUT) |
|
81 | 84 | validate_message(pyin, 'pyin', msg_id) |
|
82 | 85 | nt.assert_equal(pyin['content']['code'], code) |
|
83 | 86 | |
|
84 | 87 | return msg_id, reply['content'] |
|
85 | 88 | |
|
86 | 89 | def start_global_kernel(): |
|
87 | 90 | """start the global kernel (if it isn't running) and return its client""" |
|
88 | 91 | global KM, KC |
|
89 | 92 | if KM is None: |
|
90 | 93 | KM, KC = start_new_kernel() |
|
91 | 94 | atexit.register(stop_global_kernel) |
|
92 | 95 | return KC |
|
93 | 96 | |
|
94 | 97 | @contextmanager |
|
95 | 98 | def kernel(): |
|
96 | 99 | """Context manager for the global kernel instance |
|
97 | 100 | |
|
98 | 101 | Should be used for most kernel tests |
|
99 | 102 | |
|
100 | 103 | Returns |
|
101 | 104 | ------- |
|
102 | 105 | kernel_client: connected KernelClient instance |
|
103 | 106 | """ |
|
104 | 107 | yield start_global_kernel() |
|
105 | 108 | |
|
106 | 109 | def uses_kernel(test_f): |
|
107 | 110 | """Decorator for tests that use the global kernel""" |
|
108 | 111 | def wrapped_test(): |
|
109 | 112 | with kernel() as kc: |
|
110 | 113 | test_f(kc) |
|
111 | 114 | wrapped_test.__doc__ = test_f.__doc__ |
|
112 | 115 | wrapped_test.__name__ = test_f.__name__ |
|
113 | 116 | return wrapped_test |
|
114 | 117 | |
|
115 | 118 | def stop_global_kernel(): |
|
116 | 119 | """Stop the global shared kernel instance, if it exists""" |
|
117 | 120 | global KM, KC |
|
118 | 121 | KC.stop_channels() |
|
119 | 122 | KC = None |
|
120 | 123 | if KM is None: |
|
121 | 124 | return |
|
122 | 125 | KM.shutdown_kernel(now=True) |
|
123 | 126 | KM = None |
|
124 | 127 | |
|
125 | 128 | @contextmanager |
|
126 | def new_kernel(): | |
|
129 | def new_kernel(argv=None): | |
|
127 | 130 | """Context manager for a new kernel in a subprocess |
|
128 | 131 | |
|
129 | 132 | Should only be used for tests where the kernel must not be re-used. |
|
130 | 133 | |
|
131 | 134 | Returns |
|
132 | 135 | ------- |
|
133 | 136 | kernel_client: connected KernelClient instance |
|
134 | 137 | """ |
|
135 | km, kc = start_new_kernel() | |
|
138 | km, kc = start_new_kernel(argv) | |
|
136 | 139 | try: |
|
137 | 140 | yield kc |
|
138 | 141 | finally: |
|
139 | 142 | kc.stop_channels() |
|
140 | 143 | km.shutdown_kernel(now=True) |
|
141 | 144 | |
|
142 | 145 | |
|
143 | 146 | def assemble_output(iopub): |
|
144 | 147 | """assemble stdout/err from an execution""" |
|
145 | 148 | stdout = '' |
|
146 | 149 | stderr = '' |
|
147 | 150 | while True: |
|
148 | 151 | msg = iopub.get_msg(block=True, timeout=1) |
|
149 | 152 | msg_type = msg['msg_type'] |
|
150 | 153 | content = msg['content'] |
|
151 | 154 | if msg_type == 'status' and content['execution_state'] == 'idle': |
|
152 | 155 | # idle message signals end of output |
|
153 | 156 | break |
|
154 | 157 | elif msg['msg_type'] == 'stream': |
|
155 | 158 | if content['name'] == 'stdout': |
|
156 | 159 | stdout += content['data'] |
|
157 | 160 | elif content['name'] == 'stderr': |
|
158 | 161 | stderr += content['data'] |
|
159 | 162 | else: |
|
160 | 163 | raise KeyError("bad stream: %r" % content['name']) |
|
161 | 164 | else: |
|
162 | 165 | # other output, ignored |
|
163 | 166 | pass |
|
164 | 167 | return stdout, stderr |
|
165 | 168 | |
|
166 | 169 | |
|
167 | 170 |
General Comments 0
You need to be logged in to leave comments.
Login now