test_kernel.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # coding: utf-8
  2. """test the IPython Kernel"""
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import io
  6. import os.path
  7. import sys
  8. import time
  9. import nose.tools as nt
  10. from IPython.testing import decorators as dec, tools as tt
  11. from ipython_genutils import py3compat
  12. from IPython.paths import locate_profile
  13. from ipython_genutils.tempdir import TemporaryDirectory
  14. from .utils import (
  15. new_kernel, kernel, TIMEOUT, assemble_output, execute,
  16. flush_channels, wait_for_idle)
  17. def _check_master(kc, expected=True, stream="stdout"):
  18. execute(kc=kc, code="import sys")
  19. flush_channels(kc)
  20. msg_id, content = execute(kc=kc, code="print (sys.%s._is_master_process())" % stream)
  21. stdout, stderr = assemble_output(kc.iopub_channel)
  22. assert stdout.strip() == repr(expected)
  23. def _check_status(content):
  24. """If status=error, show the traceback"""
  25. if content['status'] == 'error':
  26. assert False, ''.join(['\n'] + content['traceback'])
  27. # printing tests
  28. def test_simple_print():
  29. """simple print statement in kernel"""
  30. with kernel() as kc:
  31. iopub = kc.iopub_channel
  32. msg_id, content = execute(kc=kc, code="print ('hi')")
  33. stdout, stderr = assemble_output(iopub)
  34. assert stdout == 'hi\n'
  35. assert stderr == ''
  36. _check_master(kc, expected=True)
  37. def test_sys_path():
  38. """test that sys.path doesn't get messed up by default"""
  39. with kernel() as kc:
  40. msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))")
  41. stdout, stderr = assemble_output(kc.iopub_channel)
  42. assert stdout == "''\n"
  43. def test_sys_path_profile_dir():
  44. """test that sys.path doesn't get messed up when `--profile-dir` is specified"""
  45. with new_kernel(['--profile-dir', locate_profile('default')]) as kc:
  46. msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))")
  47. stdout, stderr = assemble_output(kc.iopub_channel)
  48. assert stdout == "''\n"
  49. @dec.skipif(sys.platform == 'win32', "subprocess prints fail on Windows")
  50. def test_subprocess_print():
  51. """printing from forked mp.Process"""
  52. with new_kernel() as kc:
  53. iopub = kc.iopub_channel
  54. _check_master(kc, expected=True)
  55. flush_channels(kc)
  56. np = 5
  57. code = '\n'.join([
  58. "from __future__ import print_function",
  59. "import time",
  60. "import multiprocessing as mp",
  61. "pool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]" % np,
  62. "for p in pool: p.start()",
  63. "for p in pool: p.join()",
  64. "time.sleep(0.5),"
  65. ])
  66. msg_id, content = execute(kc=kc, code=code)
  67. stdout, stderr = assemble_output(iopub)
  68. nt.assert_equal(stdout.count("hello"), np, stdout)
  69. for n in range(np):
  70. nt.assert_equal(stdout.count(str(n)), 1, stdout)
  71. assert stderr == ''
  72. _check_master(kc, expected=True)
  73. _check_master(kc, expected=True, stream="stderr")
  74. def test_subprocess_noprint():
  75. """mp.Process without print doesn't trigger iostream mp_mode"""
  76. with kernel() as kc:
  77. iopub = kc.iopub_channel
  78. np = 5
  79. code = '\n'.join([
  80. "import multiprocessing as mp",
  81. "pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]" % np,
  82. "for p in pool: p.start()",
  83. "for p in pool: p.join()"
  84. ])
  85. msg_id, content = execute(kc=kc, code=code)
  86. stdout, stderr = assemble_output(iopub)
  87. assert stdout == ''
  88. assert stderr == ''
  89. _check_master(kc, expected=True)
  90. _check_master(kc, expected=True, stream="stderr")
  91. @dec.skipif(sys.platform == 'win32', "subprocess prints fail on Windows")
  92. def test_subprocess_error():
  93. """error in mp.Process doesn't crash"""
  94. with new_kernel() as kc:
  95. iopub = kc.iopub_channel
  96. code = '\n'.join([
  97. "import multiprocessing as mp",
  98. "p = mp.Process(target=int, args=('hi',))",
  99. "p.start()",
  100. "p.join()",
  101. ])
  102. msg_id, content = execute(kc=kc, code=code)
  103. stdout, stderr = assemble_output(iopub)
  104. assert stdout == ''
  105. assert "ValueError" in stderr
  106. _check_master(kc, expected=True)
  107. _check_master(kc, expected=True, stream="stderr")
  108. # raw_input tests
  109. def test_raw_input():
  110. """test [raw_]input"""
  111. with kernel() as kc:
  112. iopub = kc.iopub_channel
  113. input_f = "input" if py3compat.PY3 else "raw_input"
  114. theprompt = "prompt> "
  115. code = 'print({input_f}("{theprompt}"))'.format(**locals())
  116. msg_id = kc.execute(code, allow_stdin=True)
  117. msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT)
  118. assert msg['header']['msg_type'] == u'input_request'
  119. content = msg['content']
  120. assert content['prompt'] == theprompt
  121. text = "some text"
  122. kc.input(text)
  123. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  124. assert reply['content']['status'] == 'ok'
  125. stdout, stderr = assemble_output(iopub)
  126. assert stdout == text + "\n"
  127. @dec.skipif(py3compat.PY3)
  128. def test_eval_input():
  129. """test input() on Python 2"""
  130. with kernel() as kc:
  131. iopub = kc.iopub_channel
  132. input_f = "input" if py3compat.PY3 else "raw_input"
  133. theprompt = "prompt> "
  134. code = 'print(input("{theprompt}"))'.format(**locals())
  135. msg_id = kc.execute(code, allow_stdin=True)
  136. msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT)
  137. assert msg['header']['msg_type'] == u'input_request'
  138. content = msg['content']
  139. assert content['prompt'] == theprompt
  140. kc.input("1+1")
  141. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  142. assert reply['content']['status'] == 'ok'
  143. stdout, stderr = assemble_output(iopub)
  144. assert stdout == "2\n"
  145. def test_save_history():
  146. # Saving history from the kernel with %hist -f was failing because of
  147. # unicode problems on Python 2.
  148. with kernel() as kc, TemporaryDirectory() as td:
  149. file = os.path.join(td, 'hist.out')
  150. execute(u'a=1', kc=kc)
  151. wait_for_idle(kc)
  152. execute(u'b=u"abcþ"', kc=kc)
  153. wait_for_idle(kc)
  154. _, reply = execute("%hist -f " + file, kc=kc)
  155. assert reply['status'] == 'ok'
  156. with io.open(file, encoding='utf-8') as f:
  157. content = f.read()
  158. assert u'a=1' in content
  159. assert u'b=u"abcþ"' in content
  160. @dec.skip_without('faulthandler')
  161. def test_smoke_faulthandler():
  162. with kernel() as kc:
  163. # Note: faulthandler.register is not available on windows.
  164. code = u'\n'.join([
  165. 'import sys',
  166. 'import faulthandler',
  167. 'import signal',
  168. 'faulthandler.enable()',
  169. 'if not sys.platform.startswith("win32"):',
  170. ' faulthandler.register(signal.SIGTERM)'])
  171. _, reply = execute(code, kc=kc)
  172. nt.assert_equal(reply['status'], 'ok', reply.get('traceback', ''))
  173. def test_help_output():
  174. """ipython kernel --help-all works"""
  175. tt.help_all_output_test('kernel')
  176. def test_is_complete():
  177. with kernel() as kc:
  178. # There are more test cases for this in core - here we just check
  179. # that the kernel exposes the interface correctly.
  180. kc.is_complete('2+2')
  181. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  182. assert reply['content']['status'] == 'complete'
  183. # SyntaxError
  184. kc.is_complete('raise = 2')
  185. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  186. assert reply['content']['status'] == 'invalid'
  187. kc.is_complete('a = [1,\n2,')
  188. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  189. assert reply['content']['status'] == 'incomplete'
  190. assert reply['content']['indent'] == ''
  191. # Cell magic ends on two blank lines for console UIs
  192. kc.is_complete('%%timeit\na\n\n')
  193. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  194. assert reply['content']['status'] == 'complete'
  195. def test_complete():
  196. with kernel() as kc:
  197. execute(u'a = 1', kc=kc)
  198. wait_for_idle(kc)
  199. cell = 'import IPython\nb = a.'
  200. kc.complete(cell)
  201. reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
  202. c = reply['content']
  203. assert c['status'] == 'ok'
  204. assert c['cursor_start'] == cell.find('a.')
  205. assert c['cursor_end'] == cell.find('a.') + 2
  206. matches = c['matches']
  207. nt.assert_greater(len(matches), 0)
  208. for match in matches:
  209. assert match[:2] == 'a.'
  210. @dec.skip_without('matplotlib')
  211. def test_matplotlib_inline_on_import():
  212. with kernel() as kc:
  213. cell = '\n'.join([
  214. 'import matplotlib, matplotlib.pyplot as plt',
  215. 'backend = matplotlib.get_backend()'
  216. ])
  217. _, reply = execute(cell,
  218. user_expressions={'backend': 'backend'},
  219. kc=kc)
  220. _check_status(reply)
  221. backend_bundle = reply['user_expressions']['backend']
  222. _check_status(backend_bundle)
  223. assert 'backend_inline' in backend_bundle['data']['text/plain']
  224. def test_shutdown():
  225. """Kernel exits after polite shutdown_request"""
  226. with new_kernel() as kc:
  227. km = kc.parent
  228. execute(u'a = 1', kc=kc)
  229. wait_for_idle(kc)
  230. kc.shutdown()
  231. for i in range(100): # 10s timeout
  232. if km.is_alive():
  233. time.sleep(.1)
  234. else:
  235. break
  236. assert not km.is_alive()