9
9
10
10
import logging
11
11
import os
12
- import traceback
13
12
14
13
logger = logging .getLogger (__name__ )
15
14
@@ -72,11 +71,11 @@ def detect_best_shell():
72
71
return 'code'
73
72
74
73
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
80
79
81
80
def launch_bpython ():
82
81
imported_objects = get_launch_args (** options )
@@ -104,15 +103,11 @@ def launch_ipython():
104
103
105
104
return launch_ipython
106
105
except ImportError :
107
- str_exc = traceback .format_exc ()
108
106
# IPython < 0.11
109
107
# Explicitly pass an empty list as arguments, because otherwise
110
108
# IPython would use sys.argv from this script.
111
109
# 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
116
111
117
112
def launch_ipython ():
118
113
imported_objects = get_launch_args (** options )
@@ -122,30 +117,26 @@ def launch_ipython():
122
117
return launch_ipython
123
118
124
119
125
- def get_ptpython (self , options ):
120
+ def get_ptpython (options , vi_mode = False ):
126
121
try :
127
122
from ptpython .repl import embed , run_config
128
123
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
134
125
135
- def run_ptpython ():
126
+ def launch_ptpython ():
136
127
imported_objects = get_launch_args (** options )
137
128
history_filename = os .path .expanduser ('~/.ptpython_history' )
138
129
embed (
139
130
globals = imported_objects ,
140
131
history_filename = history_filename ,
141
- vi_mode = options [ ' vi_mode' ] ,
132
+ vi_mode = vi_mode ,
142
133
configure = run_config ,
143
134
)
144
135
145
- return run_ptpython
136
+ return launch_ptpython
146
137
147
138
148
- def get_ptipython (options ):
139
+ def get_ptipython (options , vi_mode = False ):
149
140
"""Based on django-extensions
150
141
151
142
Run renamed to launch, get_imported_objects renamed to get_launch_args
@@ -154,20 +145,17 @@ def get_ptipython(options):
154
145
from ptpython .ipython import embed
155
146
from ptpython .repl import run_config
156
147
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
163
151
164
152
def launch_ptipython ():
165
153
imported_objects = get_launch_args (** options )
166
154
history_filename = os .path .expanduser ('~/.ptpython_history' )
167
155
embed (
168
156
user_ns = imported_objects ,
169
157
history_filename = history_filename ,
170
- vi_mode = options [ ' vi_mode' ] ,
158
+ vi_mode = vi_mode ,
171
159
configure = run_config ,
172
160
)
173
161
@@ -226,30 +214,25 @@ def get_code(use_pythonrc, imported_objects):
226
214
pythonrc_code = handle .read ()
227
215
# Match the behavior of the cpython shell where an error in
228
216
# 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 )
235
218
236
219
def launch_code ():
237
220
code .interact (local = imported_objects )
238
221
239
222
return launch_code
240
223
241
224
242
- def launch (shell = 'best' , use_pythonrc = False , ** kwargs ):
225
+ def launch (shell = 'best' , use_pythonrc = False , use_vi_mode = False , ** kwargs ):
243
226
# Also allowing passing shell='code' to force using code.interact
244
227
imported_objects = get_launch_args (** kwargs )
245
228
246
229
if shell == 'best' :
247
230
shell = detect_best_shell ()
248
231
249
232
if shell == 'ptipython' :
250
- launch = get_ptipython (options = kwargs )
233
+ launch = get_ptipython (options = kwargs , vi_mode = use_vi_mode )
251
234
elif shell == 'ptpython' :
252
- launch = get_ptpython (options = kwargs )
235
+ launch = get_ptpython (options = kwargs , vi_mode = use_vi_mode )
253
236
elif shell == 'ipython' :
254
237
launch = get_ipython (options = kwargs )
255
238
elif shell == 'bpython' :
0 commit comments