55
66import sys
77import types
8- import warnings
98
109try :
1110 from importlib import reload
@@ -78,80 +77,6 @@ def __dir__(self):
7877 # well in Python REPLs like IPython.
7978
8079
81- instruction_template = """
82-
83- Python package "{package}" cannot be imported from Python interpreter
84- {python}.
85- {additional_message}
86- Use your favorite method to install "{need_install}" or run the following
87- command in Julia (which *tries* to the right thing):
88-
89- IPython.install_dependency("{need_install}")
90-
91- It prints the installation command to be executed and prompts your
92- input (yes/no) before really executing it.
93- """
94-
95- ipython_dependency_missing = """
96- IPython (version: {IPython.__version__}) is importable but {dependency}
97- cannot be imported. It is very strange and I'm not sure what is the
98- best instruction here. Updating IPython could help.
99- """
100-
101-
102- def make_instruction (package , need_install = None , ** kwargs ):
103- return instruction_template .format (** dict (dict (
104- package = package ,
105- need_install = need_install or package .lower (),
106- python = sys .executable ,
107- additional_message = '' ,
108- ), ** kwargs ))
109-
110-
111- def make_dependency_missing_instruction (IPython , dependency ):
112- return make_instruction (
113- dependency ,
114- need_install = 'ipython' ,
115- additional_message = ipython_dependency_missing .format (
116- IPython = IPython ,
117- dependency = dependency ,
118- ))
119-
120-
121- def package_name (err ):
122- try :
123- return err .name
124- except AttributeError :
125- # Python 2 support:
126- prefix = 'No module named '
127- message = str (err )
128- if message .startswith (prefix ):
129- return message [len (prefix ):].rstrip ()
130- raise ValueError ('Cannot determine missing package for error {}'
131- .format (err ))
132-
133-
134- def print_instruction_on_import_error (f ):
135- def wrapped (* args , ** kwargs ):
136- try :
137- return f (* args , ** kwargs )
138- except ImportError as err :
139- name = package_name (err )
140- if name in ('IPython' , 'julia' ):
141- print (make_instruction (name ))
142- return
143- if name == 'traitlets' :
144- try :
145- import IPython
146- except ImportError :
147- print (make_instruction ('IPython' ))
148- return
149- print (make_dependency_missing_instruction (IPython , name ))
150- return
151- raise
152- return wrapped
153-
154-
15580def get_api (main ):
15681 if main is None :
15782 return None
@@ -175,60 +100,6 @@ def get_main(**kwargs):
175100 return _Main
176101
177102
178- def ipython_options (** kwargs ):
179- global _Main
180- from traitlets .config import Config
181-
182- _Main = Main = JuliaNameSpace (JuliaAPI (** kwargs ))
183- user_ns = dict (
184- Main = Main ,
185- )
186-
187- c = Config ()
188- c .TerminalIPythonApp .display_banner = False
189- c .TerminalInteractiveShell .confirm_exit = False
190-
191- from . import ipyext
192- c .InteractiveShellApp .extensions = [
193- ipyext .__name__ ,
194- ]
195-
196- return dict (user_ns = user_ns , config = c )
197-
198-
199- segfault_warning = """\
200- Segmentation fault warning.
201-
202- You are using IPython version {IPython.__version__} which is known to
203- cause segmentation fault with tab completion. For segfault-free
204- IPython, upgrade to version 7 or above (which may still be in
205- development stage depending on the time you read this message).
206- Note also that IPython releases after 5.x do not support Python 2.
207-
208- If you need to install development version of IPython and understand
209- what would happen to your Python environment by doing so, executing
210- the following command in Julia may help:
211-
212- IPython.install_dependency("ipython-dev")
213-
214- It prints the installation command to be executed and prompts your
215- input (yes/no) before really executing it.
216- """
217-
218- segfault_warned = False
219-
220-
221- @print_instruction_on_import_error
222- def customized_ipython (** kwargs ):
223- global segfault_warned
224- import IPython
225- print ()
226- if int (IPython .__version__ .split ('.' , 1 )[0 ]) < 7 and not segfault_warned :
227- warnings .warn (segfault_warning .format (** vars ()))
228- segfault_warned = True
229- IPython .start_ipython (** ipython_options (** kwargs ))
230-
231-
232103def revise ():
233104 """Ad-hoc hot reload."""
234105
0 commit comments