MCPcopy Create free account
hub / github.com/ipython/ipython / ipexec

Function ipexec

IPython/testing/tools.py:169–219  ·  view source on GitHub ↗

Utility to call 'ipython filename'. Starts IPython with a minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extensi

(fname, options=None, commands=())

Source from the content-addressed store, hash-verified

167 return ipython_cmd
168
169def ipexec(fname, options=None, commands=()):
170 """Utility to call 'ipython filename'.
171
172 Starts IPython with a minimal and safe configuration to make startup as fast
173 as possible.
174
175 Note that this starts IPython in a subprocess!
176
177 Parameters
178 ----------
179 fname : str
180 Name of file to be executed (should have .py or .ipy extension).
181
182 options : optional, list
183 Extra command-line flags to be passed to IPython.
184
185 commands : optional, list
186 Commands to send in on stdin
187
188 Returns
189 -------
190 ``(stdout, stderr)`` of ipython subprocess.
191 """
192 if options is None: options = []
193
194 cmdargs = default_argv() + options
195
196 test_dir = os.path.dirname(__file__)
197
198 ipython_cmd = get_ipython_cmd()
199 # Absolute path for filename
200 full_fname = os.path.join(test_dir, fname)
201 full_cmd = ipython_cmd + cmdargs + ['--', full_fname]
202 env = os.environ.copy()
203 # FIXME: ignore all warnings in ipexec while we have shims
204 # should we keep suppressing warnings here, even after removing shims?
205 env['PYTHONWARNINGS'] = 'ignore'
206 # env.pop('PYTHONWARNINGS', None) # Avoid extraneous warnings appearing on stderr
207 for k, v in env.items():
208 # Debug a bizarre failure we've seen on Windows:
209 # TypeError: environment can only contain strings
210 if not isinstance(v, str):
211 print(k, v)
212 p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env)
213 out, err = p.communicate(input=py3compat.encode('\n'.join(commands)) or None)
214 out, err = py3compat.decode(out), py3compat.decode(err)
215 # `import readline` causes 'ESC[?1034h' to be output sometimes,
216 # so strip that out before doing comparisons
217 if out:
218 out = re.sub(r'\x1b\[[^h]+h', '', out)
219 return out, err
220
221
222def ipexec_validate(fname, expected_out, expected_err='',

Callers 1

ipexec_validateFunction · 0.85

Calls 3

default_argvFunction · 0.85
get_ipython_cmdFunction · 0.85
copyMethod · 0.45

Tested by

no test coverage detected