Show More
@@ -1,136 +1,137 | |||
|
1 | 1 | """Test embedding of IPython""" |
|
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 os |
|
15 | 15 | import subprocess |
|
16 | 16 | import sys |
|
17 | import nose.tools as nt | |
|
17 | ||
|
18 | 18 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
19 | 19 | from IPython.testing.decorators import skip_win32 |
|
20 | 20 | from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Tests |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | _sample_embed = b""" |
|
28 | 28 | import IPython |
|
29 | 29 | |
|
30 | 30 | a = 3 |
|
31 | 31 | b = 14 |
|
32 | 32 | print(a, '.', b) |
|
33 | 33 | |
|
34 | 34 | IPython.embed() |
|
35 | 35 | |
|
36 | 36 | print('bye!') |
|
37 | 37 | """ |
|
38 | 38 | |
|
39 | 39 | _exit = b"exit\r" |
|
40 | 40 | |
|
41 | 41 | def test_ipython_embed(): |
|
42 | 42 | """test that `IPython.embed()` works""" |
|
43 | 43 | with NamedFileInTemporaryDirectory('file_with_embed.py') as f: |
|
44 | 44 | f.write(_sample_embed) |
|
45 | 45 | f.flush() |
|
46 | 46 | f.close() # otherwise msft won't be able to read the file |
|
47 | 47 | |
|
48 | 48 | # run `python file_with_embed.py` |
|
49 | 49 | cmd = [sys.executable, f.name] |
|
50 | 50 | env = os.environ.copy() |
|
51 | 51 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' |
|
52 | 52 | |
|
53 | 53 | p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE, |
|
54 | 54 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
55 | 55 | out, err = p.communicate(_exit) |
|
56 | 56 | std = out.decode('UTF-8') |
|
57 | 57 | |
|
58 |
|
|
|
59 |
|
|
|
60 |
if os.name != |
|
|
58 | assert p.returncode == 0 | |
|
59 | assert "3 . 14" in std | |
|
60 | if os.name != "nt": | |
|
61 | 61 | # TODO: Fix up our different stdout references, see issue gh-14 |
|
62 |
|
|
|
63 |
|
|
|
62 | assert "IPython" in std | |
|
63 | assert "bye!" in std | |
|
64 | ||
|
64 | 65 | |
|
65 | 66 | @skip_win32 |
|
66 | 67 | def test_nest_embed(): |
|
67 | 68 | """test that `IPython.embed()` is nestable""" |
|
68 | 69 | import pexpect |
|
69 | 70 | ipy_prompt = r']:' #ansi color codes give problems matching beyond this |
|
70 | 71 | env = os.environ.copy() |
|
71 | 72 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' |
|
72 | 73 | |
|
73 | 74 | |
|
74 | 75 | child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'], |
|
75 | 76 | env=env) |
|
76 | 77 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
|
77 | 78 | child.expect(ipy_prompt) |
|
78 | 79 | child.sendline("import IPython") |
|
79 | 80 | child.expect(ipy_prompt) |
|
80 | 81 | child.sendline("ip0 = get_ipython()") |
|
81 | 82 | #enter first nested embed |
|
82 | 83 | child.sendline("IPython.embed()") |
|
83 | 84 | #skip the banner until we get to a prompt |
|
84 | 85 | try: |
|
85 | 86 | prompted = -1 |
|
86 | 87 | while prompted != 0: |
|
87 | 88 | prompted = child.expect([ipy_prompt, '\r\n']) |
|
88 | 89 | except pexpect.TIMEOUT as e: |
|
89 | 90 | print(e) |
|
90 | 91 | #child.interact() |
|
91 | 92 | child.sendline("embed1 = get_ipython()") |
|
92 | 93 | child.expect(ipy_prompt) |
|
93 | 94 | child.sendline("print('true' if embed1 is not ip0 else 'false')") |
|
94 | 95 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
95 | 96 | child.expect(ipy_prompt) |
|
96 | 97 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |
|
97 | 98 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
98 | 99 | child.expect(ipy_prompt) |
|
99 | 100 | #enter second nested embed |
|
100 | 101 | child.sendline("IPython.embed()") |
|
101 | 102 | #skip the banner until we get to a prompt |
|
102 | 103 | try: |
|
103 | 104 | prompted = -1 |
|
104 | 105 | while prompted != 0: |
|
105 | 106 | prompted = child.expect([ipy_prompt, '\r\n']) |
|
106 | 107 | except pexpect.TIMEOUT as e: |
|
107 | 108 | print(e) |
|
108 | 109 | #child.interact() |
|
109 | 110 | child.sendline("embed2 = get_ipython()") |
|
110 | 111 | child.expect(ipy_prompt) |
|
111 | 112 | child.sendline("print('true' if embed2 is not embed1 else 'false')") |
|
112 | 113 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
113 | 114 | child.expect(ipy_prompt) |
|
114 | 115 | child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')") |
|
115 | 116 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
116 | 117 | child.expect(ipy_prompt) |
|
117 | 118 | child.sendline('exit') |
|
118 | 119 | #back at first embed |
|
119 | 120 | child.expect(ipy_prompt) |
|
120 | 121 | child.sendline("print('true' if get_ipython() is embed1 else 'false')") |
|
121 | 122 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
122 | 123 | child.expect(ipy_prompt) |
|
123 | 124 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |
|
124 | 125 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
125 | 126 | child.expect(ipy_prompt) |
|
126 | 127 | child.sendline('exit') |
|
127 | 128 | #back at launching scope |
|
128 | 129 | child.expect(ipy_prompt) |
|
129 | 130 | child.sendline("print('true' if get_ipython() is ip0 else 'false')") |
|
130 | 131 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
131 | 132 | child.expect(ipy_prompt) |
|
132 | 133 | child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')") |
|
133 | 134 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
|
134 | 135 | child.expect(ipy_prompt) |
|
135 | 136 | child.sendline('exit') |
|
136 | 137 | child.close() |
General Comments 0
You need to be logged in to leave comments.
Login now