|
@@
-1211,14
+1211,20
class InteractiveShell(Configurable, Magic):
|
|
1211
|
def init_history(self):
|
|
1211
|
def init_history(self):
|
|
1212
|
self.history_manager = HistoryManager(shell=self)
|
|
1212
|
self.history_manager = HistoryManager(shell=self)
|
|
1213
|
|
|
1213
|
|
|
1214
|
def savehist(self):
|
|
1214
|
def save_hist(self):
|
|
1215
|
"""Save input history to a file (via readline library)."""
|
|
1215
|
"""Save input history to a file (via readline library)."""
|
|
1216
|
self.history_manager.save_hist()
|
|
1216
|
self.history_manager.save_hist()
|
|
|
|
|
1217
|
|
|
|
|
|
1218
|
# For backwards compatibility
|
|
|
|
|
1219
|
savehist = save_hist
|
|
1217
|
|
|
1220
|
|
|
1218
|
def reloadhist(self):
|
|
1221
|
def reload_hist(self):
|
|
1219
|
"""Reload the input history from disk file."""
|
|
1222
|
"""Reload the input history from disk file."""
|
|
1220
|
self.history_manager.reload_hist()
|
|
1223
|
self.history_manager.reload_hist()
|
|
1221
|
|
|
1224
|
|
|
|
|
|
1225
|
# For backwards compatibility
|
|
|
|
|
1226
|
reloadhist = reload_hist
|
|
|
|
|
1227
|
|
|
1222
|
def history_saving_wrapper(self, func):
|
|
1228
|
def history_saving_wrapper(self, func):
|
|
1223
|
""" Wrap func for readline history saving
|
|
1229
|
""" Wrap func for readline history saving
|
|
1224
|
|
|
1230
|
|
|
@@
-1231,7
+1237,7
class InteractiveShell(Configurable, Magic):
|
|
1231
|
return func
|
|
1237
|
return func
|
|
1232
|
|
|
1238
|
|
|
1233
|
def wrapper():
|
|
1239
|
def wrapper():
|
|
1234
|
self.savehist()
|
|
1240
|
self.save_hist()
|
|
1235
|
try:
|
|
1241
|
try:
|
|
1236
|
func()
|
|
1242
|
func()
|
|
1237
|
finally:
|
|
1243
|
finally:
|
|
@@
-1269,7
+1275,7
class InteractiveShell(Configurable, Magic):
|
|
1269
|
|
|
1275
|
|
|
1270
|
Set a custom exception handler, which will be called if any of the
|
|
1276
|
Set a custom exception handler, which will be called if any of the
|
|
1271
|
exceptions in exc_tuple occur in the mainloop (specifically, in the
|
|
1277
|
exceptions in exc_tuple occur in the mainloop (specifically, in the
|
|
1272
|
runcode() method.
|
|
1278
|
run_code() method.
|
|
1273
|
|
|
1279
|
|
|
1274
|
Inputs:
|
|
1280
|
Inputs:
|
|
1275
|
|
|
1281
|
|
|
@@
-1464,8
+1470,8
class InteractiveShell(Configurable, Magic):
|
|
1464
|
self.has_readline = False
|
|
1470
|
self.has_readline = False
|
|
1465
|
self.readline = None
|
|
1471
|
self.readline = None
|
|
1466
|
# Set a number of methods that depend on readline to be no-op
|
|
1472
|
# Set a number of methods that depend on readline to be no-op
|
|
1467
|
self.savehist = no_op
|
|
1473
|
self.save_hist = no_op
|
|
1468
|
self.reloadhist = no_op
|
|
1474
|
self.reload_hist = no_op
|
|
1469
|
self.set_readline_completer = no_op
|
|
1475
|
self.set_readline_completer = no_op
|
|
1470
|
self.set_custom_completer = no_op
|
|
1476
|
self.set_custom_completer = no_op
|
|
1471
|
self.set_completer_frame = no_op
|
|
1477
|
self.set_completer_frame = no_op
|
|
@@
-1527,7
+1533,7
class InteractiveShell(Configurable, Magic):
|
|
1527
|
|
|
1533
|
|
|
1528
|
# If we have readline, we want our history saved upon ipython
|
|
1534
|
# If we have readline, we want our history saved upon ipython
|
|
1529
|
# exiting.
|
|
1535
|
# exiting.
|
|
1530
|
atexit.register(self.savehist)
|
|
1536
|
atexit.register(self.save_hist)
|
|
1531
|
|
|
1537
|
|
|
1532
|
# Configure auto-indent for all platforms
|
|
1538
|
# Configure auto-indent for all platforms
|
|
1533
|
self.set_autoindent(self.autoindent)
|
|
1539
|
self.set_autoindent(self.autoindent)
|
|
@@
-2075,8
+2081,8
class InteractiveShell(Configurable, Magic):
|
|
2075
|
# the main object).
|
|
2081
|
# the main object).
|
|
2076
|
# - do any logging of input
|
|
2082
|
# - do any logging of input
|
|
2077
|
# - update histories (raw/translated)
|
|
2083
|
# - update histories (raw/translated)
|
|
2078
|
# - then, call plain runsource (for single blocks, so displayhook is
|
|
2084
|
# - then, call plain run_source (for single blocks, so displayhook is
|
|
2079
|
# triggered) or runcode (for multiline blocks in exec mode).
|
|
2085
|
# triggered) or run_code (for multiline blocks in exec mode).
|
|
2080
|
#
|
|
2086
|
#
|
|
2081
|
# Once this is done, we'll be able to stop using runlines and we'll
|
|
2087
|
# Once this is done, we'll be able to stop using runlines and we'll
|
|
2082
|
# also have a much cleaner separation of logging, input history and
|
|
2088
|
# also have a much cleaner separation of logging, input history and
|
|
@@
-2125,13
+2131,13
class InteractiveShell(Configurable, Magic):
|
|
2125
|
|
|
2131
|
|
|
2126
|
# In multi-block input, if the last block is a simple (one-two
|
|
2132
|
# In multi-block input, if the last block is a simple (one-two
|
|
2127
|
# lines) expression, run it in single mode so it produces output.
|
|
2133
|
# lines) expression, run it in single mode so it produces output.
|
|
2128
|
# Otherwise just feed the whole thing to runcode. This seems like
|
|
2134
|
# Otherwise just feed the whole thing to run_code. This seems like
|
|
2129
|
# a reasonable usability design.
|
|
2135
|
# a reasonable usability design.
|
|
2130
|
last = blocks[-1]
|
|
2136
|
last = blocks[-1]
|
|
2131
|
last_nlines = len(last.splitlines())
|
|
2137
|
last_nlines = len(last.splitlines())
|
|
2132
|
|
|
2138
|
|
|
2133
|
# Note: below, whenever we call runcode, we must sync history
|
|
2139
|
# Note: below, whenever we call run_code, we must sync history
|
|
2134
|
# ourselves, because runcode is NOT meant to manage history at all.
|
|
2140
|
# ourselves, because run_code is NOT meant to manage history at all.
|
|
2135
|
if last_nlines < 2:
|
|
2141
|
if last_nlines < 2:
|
|
2136
|
# Here we consider the cell split between 'body' and 'last',
|
|
2142
|
# Here we consider the cell split between 'body' and 'last',
|
|
2137
|
# store all history and execute 'body', and if successful, then
|
|
2143
|
# store all history and execute 'body', and if successful, then
|
|
@@
-2139,14
+2145,14
class InteractiveShell(Configurable, Magic):
|
|
2139
|
|
|
2145
|
|
|
2140
|
# Get the main body to run as a cell
|
|
2146
|
# Get the main body to run as a cell
|
|
2141
|
ipy_body = ''.join(blocks[:-1])
|
|
2147
|
ipy_body = ''.join(blocks[:-1])
|
|
2142
|
retcode = self.runcode(ipy_body, post_execute=False)
|
|
2148
|
retcode = self.run_code(ipy_body, post_execute=False)
|
|
2143
|
if retcode==0:
|
|
2149
|
if retcode==0:
|
|
2144
|
# And the last expression via runlines so it produces output
|
|
2150
|
# And the last expression via runlines so it produces output
|
|
2145
|
self.run_one_block(last)
|
|
2151
|
self.run_one_block(last)
|
|
2146
|
else:
|
|
2152
|
else:
|
|
2147
|
# Run the whole cell as one entity, storing both raw and
|
|
2153
|
# Run the whole cell as one entity, storing both raw and
|
|
2148
|
# processed input in history
|
|
2154
|
# processed input in history
|
|
2149
|
self.runcode(ipy_cell)
|
|
2155
|
self.run_code(ipy_cell)
|
|
2150
|
|
|
2156
|
|
|
2151
|
# Each cell is a *single* input, regardless of how many lines it has
|
|
2157
|
# Each cell is a *single* input, regardless of how many lines it has
|
|
2152
|
self.execution_count += 1
|
|
2158
|
self.execution_count += 1
|
|
@@
-2160,7
+2166,7
class InteractiveShell(Configurable, Magic):
|
|
2160
|
if len(block.splitlines()) <= 1:
|
|
2166
|
if len(block.splitlines()) <= 1:
|
|
2161
|
out = self.run_single_line(block)
|
|
2167
|
out = self.run_single_line(block)
|
|
2162
|
else:
|
|
2168
|
else:
|
|
2163
|
out = self.runcode(block)
|
|
2169
|
out = self.run_code(block)
|
|
2164
|
return out
|
|
2170
|
return out
|
|
2165
|
|
|
2171
|
|
|
2166
|
def run_single_line(self, line):
|
|
2172
|
def run_single_line(self, line):
|
|
@@
-2173,7
+2179,7
class InteractiveShell(Configurable, Magic):
|
|
2173
|
It does not update history.
|
|
2179
|
It does not update history.
|
|
2174
|
"""
|
|
2180
|
"""
|
|
2175
|
tline = self.prefilter_manager.prefilter_line(line)
|
|
2181
|
tline = self.prefilter_manager.prefilter_line(line)
|
|
2176
|
return self.runsource(tline)
|
|
2182
|
return self.run_source(tline)
|
|
2177
|
|
|
2183
|
|
|
2178
|
def runlines(self, lines, clean=False):
|
|
2184
|
def runlines(self, lines, clean=False):
|
|
2179
|
"""Run a string of one or more lines of source.
|
|
2185
|
"""Run a string of one or more lines of source.
|
|
@@
-2192,7
+2198,7
class InteractiveShell(Configurable, Magic):
|
|
2192
|
|
|
2198
|
|
|
2193
|
# We must start with a clean buffer, in case this is run from an
|
|
2199
|
# We must start with a clean buffer, in case this is run from an
|
|
2194
|
# interactive IPython session (via a magic, for example).
|
|
2200
|
# interactive IPython session (via a magic, for example).
|
|
2195
|
self.resetbuffer()
|
|
2201
|
self.reset_buffer()
|
|
2196
|
lines = lines.splitlines()
|
|
2202
|
lines = lines.splitlines()
|
|
2197
|
|
|
2203
|
|
|
2198
|
# Since we will prefilter all lines, store the user's raw input too
|
|
2204
|
# Since we will prefilter all lines, store the user's raw input too
|
|
@@
-2209,7
+2215,7
class InteractiveShell(Configurable, Magic):
|
|
2209
|
|
|
2215
|
|
|
2210
|
if line or more:
|
|
2216
|
if line or more:
|
|
2211
|
more = self.push_line(prefilter_lines(line, more))
|
|
2217
|
more = self.push_line(prefilter_lines(line, more))
|
|
2212
|
# IPython's runsource returns None if there was an error
|
|
2218
|
# IPython's run_source returns None if there was an error
|
|
2213
|
# compiling the code. This allows us to stop processing
|
|
2219
|
# compiling the code. This allows us to stop processing
|
|
2214
|
# right away, so the user gets the error message at the
|
|
2220
|
# right away, so the user gets the error message at the
|
|
2215
|
# right place.
|
|
2221
|
# right place.
|
|
@@
-2220,7
+2226,7
class InteractiveShell(Configurable, Magic):
|
|
2220
|
if more:
|
|
2226
|
if more:
|
|
2221
|
self.push_line('\n')
|
|
2227
|
self.push_line('\n')
|
|
2222
|
|
|
2228
|
|
|
2223
|
def runsource(self, source, filename='<ipython console>', symbol='single'):
|
|
2229
|
def run_source(self, source, filename='<ipython console>', symbol='single'):
|
|
2224
|
"""Compile and run some source in the interpreter.
|
|
2230
|
"""Compile and run some source in the interpreter.
|
|
2225
|
|
|
2231
|
|
|
2226
|
Arguments are as for compile_command().
|
|
2232
|
Arguments are as for compile_command().
|
|
@@
-2235,7
+2241,7
class InteractiveShell(Configurable, Magic):
|
|
2235
|
compile_command() returned None. Nothing happens.
|
|
2241
|
compile_command() returned None. Nothing happens.
|
|
2236
|
|
|
2242
|
|
|
2237
|
3) The input is complete; compile_command() returned a code
|
|
2243
|
3) The input is complete; compile_command() returned a code
|
|
2238
|
object. The code is executed by calling self.runcode() (which
|
|
2244
|
object. The code is executed by calling self.run_code() (which
|
|
2239
|
also handles run-time exceptions, except for SystemExit).
|
|
2245
|
also handles run-time exceptions, except for SystemExit).
|
|
2240
|
|
|
2246
|
|
|
2241
|
The return value is:
|
|
2247
|
The return value is:
|
|
@@
-2271,12
+2277,15
class InteractiveShell(Configurable, Magic):
|
|
2271
|
# buffer attribute as '\n'.join(self.buffer).
|
|
2277
|
# buffer attribute as '\n'.join(self.buffer).
|
|
2272
|
self.code_to_run = code
|
|
2278
|
self.code_to_run = code
|
|
2273
|
# now actually execute the code object
|
|
2279
|
# now actually execute the code object
|
|
2274
|
if self.runcode(code) == 0:
|
|
2280
|
if self.run_code(code) == 0:
|
|
2275
|
return False
|
|
2281
|
return False
|
|
2276
|
else:
|
|
2282
|
else:
|
|
2277
|
return None
|
|
2283
|
return None
|
|
2278
|
|
|
2284
|
|
|
2279
|
def runcode(self, code_obj, post_execute=True):
|
|
2285
|
# For backwards compatibility
|
|
|
|
|
2286
|
runsource = run_source
|
|
|
|
|
2287
|
|
|
|
|
|
2288
|
def run_code(self, code_obj, post_execute=True):
|
|
2280
|
"""Execute a code object.
|
|
2289
|
"""Execute a code object.
|
|
2281
|
|
|
2290
|
|
|
2282
|
When an exception occurs, self.showtraceback() is called to display a
|
|
2291
|
When an exception occurs, self.showtraceback() is called to display a
|
|
@@
-2299,14
+2308,14
class InteractiveShell(Configurable, Magic):
|
|
2299
|
outflag = 1 # happens in more places, so it's easier as default
|
|
2308
|
outflag = 1 # happens in more places, so it's easier as default
|
|
2300
|
try:
|
|
2309
|
try:
|
|
2301
|
try:
|
|
2310
|
try:
|
|
2302
|
self.hooks.pre_runcode_hook()
|
|
2311
|
self.hooks.pre_run_code_hook()
|
|
2303
|
#rprint('Running code') # dbg
|
|
2312
|
#rprint('Running code') # dbg
|
|
2304
|
exec code_obj in self.user_global_ns, self.user_ns
|
|
2313
|
exec code_obj in self.user_global_ns, self.user_ns
|
|
2305
|
finally:
|
|
2314
|
finally:
|
|
2306
|
# Reset our crash handler in place
|
|
2315
|
# Reset our crash handler in place
|
|
2307
|
sys.excepthook = old_excepthook
|
|
2316
|
sys.excepthook = old_excepthook
|
|
2308
|
except SystemExit:
|
|
2317
|
except SystemExit:
|
|
2309
|
self.resetbuffer()
|
|
2318
|
self.reset_buffer()
|
|
2310
|
self.showtraceback(exception_only=True)
|
|
2319
|
self.showtraceback(exception_only=True)
|
|
2311
|
warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
|
|
2320
|
warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
|
|
2312
|
except self.custom_exceptions:
|
|
2321
|
except self.custom_exceptions:
|
|
@@
-2339,18
+2348,21
class InteractiveShell(Configurable, Magic):
|
|
2339
|
self.code_to_run = None
|
|
2348
|
self.code_to_run = None
|
|
2340
|
return outflag
|
|
2349
|
return outflag
|
|
2341
|
|
|
2350
|
|
|
|
|
|
2351
|
# For backwards compatibility
|
|
|
|
|
2352
|
runcode = run_code
|
|
|
|
|
2353
|
|
|
2342
|
def push_line(self, line):
|
|
2354
|
def push_line(self, line):
|
|
2343
|
"""Push a line to the interpreter.
|
|
2355
|
"""Push a line to the interpreter.
|
|
2344
|
|
|
2356
|
|
|
2345
|
The line should not have a trailing newline; it may have
|
|
2357
|
The line should not have a trailing newline; it may have
|
|
2346
|
internal newlines. The line is appended to a buffer and the
|
|
2358
|
internal newlines. The line is appended to a buffer and the
|
|
2347
|
interpreter's runsource() method is called with the
|
|
2359
|
interpreter's run_source() method is called with the
|
|
2348
|
concatenated contents of the buffer as source. If this
|
|
2360
|
concatenated contents of the buffer as source. If this
|
|
2349
|
indicates that the command was executed or invalid, the buffer
|
|
2361
|
indicates that the command was executed or invalid, the buffer
|
|
2350
|
is reset; otherwise, the command is incomplete, and the buffer
|
|
2362
|
is reset; otherwise, the command is incomplete, and the buffer
|
|
2351
|
is left as it was after the line was appended. The return
|
|
2363
|
is left as it was after the line was appended. The return
|
|
2352
|
value is 1 if more input is required, 0 if the line was dealt
|
|
2364
|
value is 1 if more input is required, 0 if the line was dealt
|
|
2353
|
with in some way (this is the same as runsource()).
|
|
2365
|
with in some way (this is the same as run_source()).
|
|
2354
|
"""
|
|
2366
|
"""
|
|
2355
|
|
|
2367
|
|
|
2356
|
# autoindent management should be done here, and not in the
|
|
2368
|
# autoindent management should be done here, and not in the
|
|
@@
-2361,20
+2373,23
class InteractiveShell(Configurable, Magic):
|
|
2361
|
#print 'push line: <%s>' % line # dbg
|
|
2373
|
#print 'push line: <%s>' % line # dbg
|
|
2362
|
self.buffer.append(line)
|
|
2374
|
self.buffer.append(line)
|
|
2363
|
full_source = '\n'.join(self.buffer)
|
|
2375
|
full_source = '\n'.join(self.buffer)
|
|
2364
|
more = self.runsource(full_source, self.filename)
|
|
2376
|
more = self.run_source(full_source, self.filename)
|
|
2365
|
if not more:
|
|
2377
|
if not more:
|
|
2366
|
self.history_manager.store_inputs('\n'.join(self.buffer_raw),
|
|
2378
|
self.history_manager.store_inputs('\n'.join(self.buffer_raw),
|
|
2367
|
full_source)
|
|
2379
|
full_source)
|
|
2368
|
self.resetbuffer()
|
|
2380
|
self.reset_buffer()
|
|
2369
|
self.execution_count += 1
|
|
2381
|
self.execution_count += 1
|
|
2370
|
return more
|
|
2382
|
return more
|
|
2371
|
|
|
2383
|
|
|
2372
|
def resetbuffer(self):
|
|
2384
|
def reset_buffer(self):
|
|
2373
|
"""Reset the input buffer."""
|
|
2385
|
"""Reset the input buffer."""
|
|
2374
|
self.buffer[:] = []
|
|
2386
|
self.buffer[:] = []
|
|
2375
|
self.buffer_raw[:] = []
|
|
2387
|
self.buffer_raw[:] = []
|
|
2376
|
self.input_splitter.reset()
|
|
2388
|
self.input_splitter.reset()
|
|
2377
|
|
|
2389
|
|
|
|
|
|
2390
|
# For backwards compatibility
|
|
|
|
|
2391
|
resetbuffer = reset_buffer
|
|
|
|
|
2392
|
|
|
2378
|
def _is_secondary_block_start(self, s):
|
|
2393
|
def _is_secondary_block_start(self, s):
|
|
2379
|
if not s.endswith(':'):
|
|
2394
|
if not s.endswith(':'):
|
|
2380
|
return False
|
|
2395
|
return False
|