|
3 | 3 |
|
4 | 4 | from __future__ import print_function |
5 | 5 |
|
6 | | -import warnings |
7 | 6 | import sys |
| 7 | +import types |
| 8 | +import warnings |
8 | 9 |
|
9 | 10 | try: |
10 | 11 | from importlib import reload |
@@ -147,6 +148,25 @@ def wrapped(*args, **kwargs): |
147 | 148 | return wrapped |
148 | 149 |
|
149 | 150 |
|
| 151 | +def get_api(main): |
| 152 | + if main is None: |
| 153 | + return None |
| 154 | + return main._JuliaNameSpace__julia |
| 155 | + |
| 156 | + |
| 157 | +def get_main(**kwargs): |
| 158 | + """ |
| 159 | + Create or get cached `Main`. |
| 160 | +
|
| 161 | + Caching is required to avoid re-writing to `_Main` when re-entering |
| 162 | + to IPython session (where `user_ns` would be ignored). |
| 163 | + """ |
| 164 | + global _Main |
| 165 | + if _Main is None: |
| 166 | + _Main = JuliaNameSpace(JuliaAPI(**kwargs)) |
| 167 | + return _Main |
| 168 | + |
| 169 | + |
150 | 170 | def ipython_options(**kwargs): |
151 | 171 | global _Main |
152 | 172 | from traitlets.config import Config |
@@ -198,12 +218,27 @@ def customized_ipython(**kwargs): |
198 | 218 |
|
199 | 219 | def revise(): |
200 | 220 | """Ad-hoc hot reload.""" |
| 221 | + |
| 222 | + Main = _Main |
| 223 | + |
201 | 224 | import replhelper |
202 | 225 | reload(replhelper.core) |
203 | 226 |
|
204 | | - if _Main is not None: |
205 | | - _Main.__class__ = replhelper.core.JuliaNameSpace |
206 | | - _Main._JuliaNameSpace__julia.__class__ = replhelper.core.JuliaAPI |
207 | | - replhelper.core._Main = _Main |
| 227 | + if Main is not None: |
| 228 | + Main.__class__ = replhelper.core.JuliaNameSpace |
| 229 | + Main._JuliaNameSpace__julia.__class__ = replhelper.core.JuliaAPI |
| 230 | + replhelper.core._Main = Main |
| 231 | + |
| 232 | + try: |
| 233 | + replhelper.tests |
| 234 | + except AttributeError: |
| 235 | + return |
| 236 | + |
| 237 | + # *Try* reloading modules `replhelper.tests.*`. If there are |
| 238 | + # dependencies between those modules, it's not going to work. |
| 239 | + for (name, module) in sorted(vars(replhelper.tests).items(), |
| 240 | + key=lambda pair: pair[0]): |
| 241 | + if isinstance(module, types.ModuleType): |
| 242 | + reload(module) |
208 | 243 |
|
209 | 244 | reload(replhelper) |
0 commit comments