99
1010import logging
1111import os
12- import traceback
1312
1413logger = logging .getLogger (__name__ )
1514
@@ -72,11 +71,11 @@ def detect_best_shell():
7271 return 'code'
7372
7473
75- def get_bpython (options , extra_args ):
76- try :
77- from bpython import embed # NOQA F841
78- except ImportError :
79- return traceback . format_exc ()
74+ def get_bpython (options , extra_args = None ):
75+ if extra_args is None :
76+ extra_args = {}
77+
78+ from bpython import embed # NOQA F841
8079
8180 def launch_bpython ():
8281 imported_objects = get_launch_args (** options )
@@ -104,15 +103,11 @@ def launch_ipython():
104103
105104 return launch_ipython
106105 except ImportError :
107- str_exc = traceback .format_exc ()
108106 # IPython < 0.11
109107 # Explicitly pass an empty list as arguments, because otherwise
110108 # IPython would use sys.argv from this script.
111109 # Notebook not supported for IPython < 0.11.
112- try :
113- from IPython .Shell import IPShell
114- except ImportError :
115- return str_exc + "\n " + traceback .format_exc ()
110+ from IPython .Shell import IPShell
116111
117112 def launch_ipython ():
118113 imported_objects = get_launch_args (** options )
@@ -122,30 +117,26 @@ def launch_ipython():
122117 return launch_ipython
123118
124119
125- def get_ptpython (self , options ):
120+ def get_ptpython (options , vi_mode = False ):
126121 try :
127122 from ptpython .repl import embed , run_config
128123 except ImportError :
129- tb = traceback .format_exc ()
130- try : # prompt_toolkit < v0.27
131- from prompt_toolkit .contrib .repl import embed , run_config
132- except ImportError :
133- return tb
124+ from prompt_toolkit .contrib .repl import embed , run_config
134125
135- def run_ptpython ():
126+ def launch_ptpython ():
136127 imported_objects = get_launch_args (** options )
137128 history_filename = os .path .expanduser ('~/.ptpython_history' )
138129 embed (
139130 globals = imported_objects ,
140131 history_filename = history_filename ,
141- vi_mode = options [ ' vi_mode' ] ,
132+ vi_mode = vi_mode ,
142133 configure = run_config ,
143134 )
144135
145- return run_ptpython
136+ return launch_ptpython
146137
147138
148- def get_ptipython (options ):
139+ def get_ptipython (options , vi_mode = False ):
149140 """Based on django-extensions
150141
151142 Run renamed to launch, get_imported_objects renamed to get_launch_args
@@ -154,20 +145,17 @@ def get_ptipython(options):
154145 from ptpython .ipython import embed
155146 from ptpython .repl import run_config
156147 except ImportError :
157- tb = traceback .format_exc ()
158- try : # prompt_toolkit < v0.27
159- from prompt_toolkit .contrib .ipython import embed
160- from prompt_toolkit .contrib .repl import run_config
161- except ImportError :
162- return tb
148+ # prompt_toolkit < v0.27
149+ from prompt_toolkit .contrib .ipython import embed
150+ from prompt_toolkit .contrib .repl import run_config
163151
164152 def launch_ptipython ():
165153 imported_objects = get_launch_args (** options )
166154 history_filename = os .path .expanduser ('~/.ptpython_history' )
167155 embed (
168156 user_ns = imported_objects ,
169157 history_filename = history_filename ,
170- vi_mode = options [ ' vi_mode' ] ,
158+ vi_mode = vi_mode ,
171159 configure = run_config ,
172160 )
173161
@@ -226,30 +214,25 @@ def get_code(use_pythonrc, imported_objects):
226214 pythonrc_code = handle .read ()
227215 # Match the behavior of the cpython shell where an error in
228216 # PYTHONSTARTUP prints an exception and continues.
229- try :
230- exec (compile (pythonrc_code , pythonrc , 'exec' ), imported_objects )
231- except Exception :
232- import traceback
233-
234- traceback .print_exc ()
217+ exec (compile (pythonrc_code , pythonrc , 'exec' ), imported_objects )
235218
236219 def launch_code ():
237220 code .interact (local = imported_objects )
238221
239222 return launch_code
240223
241224
242- def launch (shell = 'best' , use_pythonrc = False , ** kwargs ):
225+ def launch (shell = 'best' , use_pythonrc = False , use_vi_mode = False , ** kwargs ):
243226 # Also allowing passing shell='code' to force using code.interact
244227 imported_objects = get_launch_args (** kwargs )
245228
246229 if shell == 'best' :
247230 shell = detect_best_shell ()
248231
249232 if shell == 'ptipython' :
250- launch = get_ptipython (options = kwargs )
233+ launch = get_ptipython (options = kwargs , vi_mode = use_vi_mode )
251234 elif shell == 'ptpython' :
252- launch = get_ptpython (options = kwargs )
235+ launch = get_ptpython (options = kwargs , vi_mode = use_vi_mode )
253236 elif shell == 'ipython' :
254237 launch = get_ipython (options = kwargs )
255238 elif shell == 'bpython' :
0 commit comments