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