##// END OF EJS Templates
Prototype async REPL using IPython, take III...
Matthias Bussonnier -
Show More
@@ -0,0 +1,88
1 """
2 Async helper function that are invalid syntax on Python 3.5 and below.
3
4 Known limitation and possible improvement.
5
6 Top level code that contain a return statement (instead of, or in addition to
7 await) will be detected as requiring being wrapped in async calls. This should
8 be prevented as early return will not work.
9 """
10
11
12
13 import ast
14 import sys
15 import inspect
16 from textwrap import dedent, indent
17 from types import CodeType
18
19
20 def _asyncio_runner(coro):
21 """
22 Handler for asyncio autoawait
23 """
24 import asyncio
25 return asyncio.get_event_loop().run_until_complete(coro)
26
27
28 def _curio_runner(coroutine):
29 """
30 handler for curio autoawait
31 """
32 import curio
33 return curio.run(coroutine)
34
35
36 if sys.version_info > (3, 5):
37 # nose refuses to avoid this file and async def is invalidsyntax
38 s = dedent('''
39 def _trio_runner(function):
40 import trio
41 async def loc(coro):
42 """
43 We need the dummy no-op async def to protect from
44 trio's internal. See https://github.com/python-trio/trio/issues/89
45 """
46 return await coro
47 return trio.run(loc, function)
48 ''')
49 exec(s, globals(), locals())
50
51
52 def _asyncify(code: str) -> str:
53 """wrap code in async def definition.
54
55 And setup a bit of context to run it later.
56 """
57 res = dedent("""
58 async def __wrapper__():
59 try:
60 {usercode}
61 finally:
62 locals()
63 """).format(usercode=indent(code, ' ' * 8)[8:])
64 return res
65
66
67 def _should_be_async(cell: str) -> bool:
68 """Detect if a block of code need to be wrapped in an `async def`
69
70 Attempt to parse the block of code, it it compile we're fine.
71 Otherwise we wrap if and try to compile.
72
73 If it works, assume it should be async. Otherwise Return False.
74
75 Not handled yet: If the block of code has a return statement as the top
76 level, it will be seen as async. This is a know limitation.
77 """
78
79 try:
80 ast.parse(cell)
81 return False
82 except SyntaxError:
83 try:
84 ast.parse(_asyncify(cell))
85 except SyntaxError:
86 return False
87 return True
88 return False
@@ -0,0 +1,52
1 """
2 Test for async helpers.
3
4 Should only trigger on python 3.5+ or will have syntax errors.
5 """
6
7 import sys
8 import nose.tools as nt
9 from textwrap import dedent
10 from unittest import TestCase
11
12 ip = get_ipython()
13 iprc = lambda x: ip.run_cell(dedent(x))
14
15 if sys.version_info > (3,5):
16 from IPython.core.async_helpers import _should_be_async
17
18 class AsyncTest(TestCase):
19
20 def test_should_be_async(self):
21 nt.assert_false(_should_be_async("False"))
22 nt.assert_true(_should_be_async("await bar()"))
23 nt.assert_true(_should_be_async("x = await bar()"))
24 nt.assert_false(_should_be_async(dedent("""
25 async def awaitable():
26 pass
27 """)))
28
29 def test_execute(self):
30 iprc("""
31 import asyncio
32 await asyncio.sleep(0.001)
33 """)
34
35 def test_autoawait(self):
36 ip.run_cell('%autoawait False')
37 ip.run_cell('%autoawait True')
38 iprc('''
39 from asyncio import sleep
40 await.sleep(0.1)
41 ''')
42
43 def test_autoawait_curio(self):
44 ip.run_cell('%autoawait curio')
45
46 def test_autoawait_trio(self):
47 ip.run_cell('%autoawait trio')
48
49 def tearDown(self):
50 ip.loop_runner = 'asyncio'
51
52
@@ -0,0 +1,186
1
2 .. autoawait:
3
4 Asynchronous in REPL: Autoawait
5 ===============================
6
7 Starting with IPython 6.0, and when user Python 3.6 and above, IPython offer the
8 ability to run asynchronous code from the REPL. constructs which are
9 :exc:`SyntaxError` s in the Python REPL can be used seamlessly in IPython.
10
11 When a supported libray is used, IPython will automatically `await` Futures
12 and Coroutines in the REPL. This will happen if an :ref:`await <await>` (or `async`) is
13 use at top level scope, or if any structure valid only in `async def
14 <https://docs.python.org/3/reference/compound_stmts.html#async-def>`_ function
15 context are present. For example, the following being a syntax error in the
16 Python REPL::
17
18 Python 3.6.0
19 [GCC 4.2.1]
20 Type "help", "copyright", "credits" or "license" for more information.
21 >>> import aiohttp
22 >>> result = aiohttp.get('https://api.github.com')
23 >>> response = await result
24 File "<stdin>", line 1
25 response = await result
26 ^
27 SyntaxError: invalid syntax
28
29 Should behave as expected in the IPython REPL::
30
31 Python 3.6.0
32 Type 'copyright', 'credits' or 'license' for more information
33 IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help.
34
35 In [1]: import aiohttp
36 ...: result = aiohttp.get('https://api.github.com')
37
38 In [2]: response = await result
39 <pause for a few 100s ms>
40
41 In [3]: await response.json()
42 Out[3]:
43 {'authorizations_url': 'https://api.github.com/authorizations',
44 'code_search_url': 'https://api.github.com/search/code?q={query}...',
45 ...
46 }
47
48
49 You can use the ``c.InteractiveShell.autoawait`` configuration option and set it
50 to :any:`False` to deactivate automatic wrapping of asynchronous code. You can also
51 use the :magic:`%autoawait` magic to toggle the behavior at runtime::
52
53 In [1]: %autoawait False
54
55 In [2]: %autoawait
56 IPython autoawait is `Off`, and set to use `IPython.core.interactiveshell._asyncio_runner`
57
58
59
60 By default IPython will assume integration with Python's provided
61 :mod:`asyncio`, but integration with other libraries is provided. In particular
62 we provide experimental integration with the ``curio`` and ``trio`` library.
63
64 You can switch current integration by using the
65 ``c.InteractiveShell.loop_runner`` option or the ``autoawait <name
66 integration>`` magic.
67
68 For example::
69
70 In [1]: %autoawait trio
71
72 In [2]: import trio
73
74 In [3]: async def child(i):
75 ...: print(" child %s goes to sleep"%i)
76 ...: await trio.sleep(2)
77 ...: print(" child %s wakes up"%i)
78
79 In [4]: print('parent start')
80 ...: async with trio.open_nursery() as n:
81 ...: for i in range(5):
82 ...: n.spawn(child, i)
83 ...: print('parent end')
84 parent start
85 child 2 goes to sleep
86 child 0 goes to sleep
87 child 3 goes to sleep
88 child 1 goes to sleep
89 child 4 goes to sleep
90 <about 2 seconds pause>
91 child 2 wakes up
92 child 1 wakes up
93 child 0 wakes up
94 child 3 wakes up
95 child 4 wakes up
96 parent end
97
98
99 In the above example, ``async with`` at top level scope is a syntax error in
100 Python.
101
102 Using this mode can have unexpected consequences if used in interaction with
103 other features of IPython and various registered extensions. In particular if you
104 are a direct or indirect user of the AST transformers, these may not apply to
105 your code.
106
107 The default loop, or runner does not run in the background, so top level
108 asynchronous code must finish for the REPL to allow you to enter more code. As
109 with usual Python semantic, the awaitables are started only when awaited for the
110 first time. That is to say, in first example, no network request is done between
111 ``In[1]`` and ``In[2]``.
112
113
114 Internals
115 =========
116
117 As running asynchronous code is not supported in interactive REPL as of Python
118 3.6 we have to rely to a number of complex workaround to allow this to happen.
119 It is interesting to understand how this works in order to understand potential
120 bugs, or provide a custom runner.
121
122 Among the many approaches that are at our disposition, we find only one that
123 suited out need. Under the hood we :ct the code object from a async-def function
124 and run it in global namesapace after modifying the ``__code__`` object.::
125
126 async def inner_async():
127 locals().update(**global_namespace)
128 #
129 # here is user code
130 #
131 return last_user_statement
132 codeobj = modify(inner_async.__code__)
133 coroutine = eval(codeobj, user_ns)
134 display(loop_runner(coroutine))
135
136
137
138 The first thing you'll notice is that unlike classical ``exec``, there is only
139 one name space. Second, user code runs in a function scope, and not a module
140 scope.
141
142 On top of the above there are significant modification to the AST of
143 ``function``, and ``loop_runner`` can be arbitrary complex. So there is a
144 significant overhead to this kind of code.
145
146 By default the generated coroutine function will be consumed by Asyncio's
147 ``loop_runner = asyncio.get_evenloop().run_until_complete()`` method. It is
148 though possible to provide your own.
149
150 A loop runner is a *synchronous* function responsible from running a coroutine
151 object.
152
153 The runner is responsible from ensuring that ``coroutine`` run to completion,
154 and should return the result of executing the coroutine. Let's write a
155 runner for ``trio`` that print a message when used as an exercise, ``trio`` is
156 special as it usually prefer to run a function object and make a coroutine by
157 itself, we can get around this limitation by wrapping it in an async-def without
158 parameters and passing this value to ``trio``::
159
160
161 In [1]: import trio
162 ...: from types import CoroutineType
163 ...:
164 ...: def trio_runner(coro:CoroutineType):
165 ...: print('running asynchronous code')
166 ...: async def corowrap(coro):
167 ...: return await coro
168 ...: return trio.run(corowrap, coro)
169
170 We can set it up by passing it to ``%autoawait``::
171
172 In [2]: %autoawait trio_runner
173
174 In [3]: async def async_hello(name):
175 ...: await trio.sleep(1)
176 ...: print(f'Hello {name} world !')
177 ...: await trio.sleep(1)
178
179 In [4]: await async_hello('async')
180 running asynchronous code
181 Hello async world !
182
183
184 Asynchronous programming in python (and in particular in the REPL) is still a
185 relatively young subject. Feel free to contribute improvements to this codebase
186 and give us feedback.
@@ -0,0 +1,55
1 Await REPL
2 ----------
3
4 :ghpull:`10390` introduced the ability to ``await`` Futures and
5 Coroutines in the REPL. For example::
6
7 Python 3.6.0
8 Type 'copyright', 'credits' or 'license' for more information
9 IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help.
10
11 In [1]: import aiohttp
12 ...: result = aiohttp.get('https://api.github.com')
13
14 In [2]: response = await result
15 <pause for a few 100s ms>
16
17 In [3]: await response.json()
18 Out[3]:
19 {'authorizations_url': 'https://api.github.com/authorizations',
20 'code_search_url': 'https://api.github.com/search/code?q={query}{&page,per_page,sort,order}',
21 ...
22 }
23
24
25 Integration is by default with `asyncio`, but other libraries can be configured,
26 like ``curio`` or ``trio``, to improve concurrency in the REPL::
27
28 In [1]: %autoawait trio
29
30 In [2]: import trio
31
32 In [3]: async def child(i):
33 ...: print(" child %s goes to sleep"%i)
34 ...: await trio.sleep(2)
35 ...: print(" child %s wakes up"%i)
36
37 In [4]: print('parent start')
38 ...: async with trio.open_nursery() as n:
39 ...: for i in range(3):
40 ...: n.spawn(child, i)
41 ...: print('parent end')
42 parent start
43 child 2 goes to sleep
44 child 0 goes to sleep
45 child 1 goes to sleep
46 <about 2 seconds pause>
47 child 2 wakes up
48 child 1 wakes up
49 child 0 wakes up
50 parent end
51
52 See :ref:`autoawait` for more information.
53
54
55
@@ -1,3344 +1,3561
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13
14 14 import abc
15 15 import ast
16 import asyncio
16 17 import atexit
17 18 import builtins as builtin_mod
18 19 import functools
19 20 import os
20 21 import re
21 22 import runpy
22 23 import sys
23 24 import tempfile
24 25 import traceback
25 26 import types
26 27 import subprocess
27 28 import warnings
28 29 from io import open as io_open
29 30
30 31 from pickleshare import PickleShareDB
31 32
32 33 from traitlets.config.configurable import SingletonConfigurable
34 from traitlets.utils.importstring import import_item
33 35 from IPython.core import oinspect
34 36 from IPython.core import magic
35 37 from IPython.core import page
36 38 from IPython.core import prefilter
37 39 from IPython.core import ultratb
38 40 from IPython.core.alias import Alias, AliasManager
39 41 from IPython.core.autocall import ExitAutocall
40 42 from IPython.core.builtin_trap import BuiltinTrap
41 43 from IPython.core.events import EventManager, available_events
42 44 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
43 45 from IPython.core.debugger import Pdb
44 46 from IPython.core.display_trap import DisplayTrap
45 47 from IPython.core.displayhook import DisplayHook
46 48 from IPython.core.displaypub import DisplayPublisher
47 49 from IPython.core.error import InputRejected, UsageError
48 50 from IPython.core.extensions import ExtensionManager
49 51 from IPython.core.formatters import DisplayFormatter
50 52 from IPython.core.history import HistoryManager
51 53 from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2
52 54 from IPython.core.logger import Logger
53 55 from IPython.core.macro import Macro
54 56 from IPython.core.payload import PayloadManager
55 57 from IPython.core.prefilter import PrefilterManager
56 58 from IPython.core.profiledir import ProfileDir
57 59 from IPython.core.usage import default_banner
58 60 from IPython.display import display
59 61 from IPython.testing.skipdoctest import skip_doctest
60 62 from IPython.utils import PyColorize
61 63 from IPython.utils import io
62 64 from IPython.utils import py3compat
63 65 from IPython.utils import openpy
64 66 from IPython.utils.decorators import undoc
65 67 from IPython.utils.io import ask_yes_no
66 68 from IPython.utils.ipstruct import Struct
67 69 from IPython.paths import get_ipython_dir
68 70 from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists
69 71 from IPython.utils.process import system, getoutput
70 72 from IPython.utils.strdispatch import StrDispatch
71 73 from IPython.utils.syspathcontext import prepended_to_syspath
72 74 from IPython.utils.text import format_screen, LSString, SList, DollarFormatter
73 75 from IPython.utils.tempdir import TemporaryDirectory
74 76 from traitlets import (
75 77 Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type,
76 observe, default,
78 observe, default, validate, Any
77 79 )
78 80 from warnings import warn
79 81 from logging import error
80 82 import IPython.core.hooks
81 83
82 84 from typing import List as ListType
83 85 from ast import AST
84 86
85 87 # NoOpContext is deprecated, but ipykernel imports it from here.
86 88 # See https://github.com/ipython/ipykernel/issues/157
87 89 from IPython.utils.contexts import NoOpContext
88 90
89 91 try:
90 92 import docrepr.sphinxify as sphx
91 93
92 94 def sphinxify(doc):
93 95 with TemporaryDirectory() as dirname:
94 96 return {
95 97 'text/html': sphx.sphinxify(doc, dirname),
96 98 'text/plain': doc
97 99 }
98 100 except ImportError:
99 101 sphinxify = None
100 102
101 103
102 104 class ProvisionalWarning(DeprecationWarning):
103 105 """
104 106 Warning class for unstable features
105 107 """
106 108 pass
107 109
108 110 if sys.version_info > (3,6):
109 111 _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign)
110 112 _single_targets_nodes = (ast.AugAssign, ast.AnnAssign)
111 113 else:
112 114 _assign_nodes = (ast.AugAssign, ast.Assign )
113 115 _single_targets_nodes = (ast.AugAssign, )
114 116
115 117 #-----------------------------------------------------------------------------
118 # Await Helpers
119 #-----------------------------------------------------------------------------
120
121 def removed_co_newlocals(function:types.FunctionType) -> types.FunctionType:
122 """Return a function that do not create a new local scope.
123
124 Given a function, create a clone of this function where the co_newlocal flag
125 has been removed, making this function code actually run in the sourounding
126 scope.
127
128 We need this in order to run asynchronous code in user level namespace.
129 """
130 from types import CodeType, FunctionType
131 CO_NEWLOCALS = 0x0002
132 code = function.__code__
133 new_code = CodeType(
134 code.co_argcount,
135 code.co_kwonlyargcount,
136 code.co_nlocals,
137 code.co_stacksize,
138 code.co_flags & ~CO_NEWLOCALS,
139 code.co_code,
140 code.co_consts,
141 code.co_names,
142 code.co_varnames,
143 code.co_filename,
144 code.co_name,
145 code.co_firstlineno,
146 code.co_lnotab,
147 code.co_freevars,
148 code.co_cellvars
149 )
150 return FunctionType(new_code, globals(), function.__name__, function.__defaults__)
151
152
153 if sys.version_info > (3,5):
154 from .async_helpers import (_asyncio_runner, _curio_runner, _trio_runner,
155 _should_be_async, _asyncify
156 )
157 else :
158 _asyncio_runner = _curio_runner = _trio_runner = None
159
160 def _should_be_async(whatever:str)->bool:
161 return False
162
163
164 def _ast_asyncify(cell:str, wrapper_name:str) -> ast.Module:
165 """
166 Parse a cell with top-level await and modify the AST to be able to run it later.
167
168 Parameter
169 ---------
170
171 cell: str
172 The code cell to asyncronify
173 wrapper_name: str
174 The name of the function to be used to wrap the passed `cell`. It is
175 advised to **not** use a python identifier in order to not pollute the
176 global namespace in which the function will be ran.
177
178 Return
179 ------
180
181 A module object AST containing **one** function named `wrapper_name`.
182
183 The given code is wrapped in a async-def function, parsed into an AST, and
184 the resulting function definition AST is modified to return the last
185 expression.
186
187 The last expression or await node is moved into a return statement at the
188 end of the function, and removed from its original location. If the last
189 node is not Expr or Await nothing is done.
190
191 The function `__code__` will need to be later modified (by
192 ``removed_co_newlocals``) in a subsequent step to not create new `locals()`
193 meaning that the local and global scope are the same, ie as if the body of
194 the function was at module level.
195
196 Lastly a call to `locals()` is made just before the last expression of the
197 function, or just after the last assignment or statement to make sure the
198 global dict is updated as python function work with a local fast cache which
199 is updated only on `local()` calls.
200 """
201
202 from ast import Expr, Await, Return
203 tree = ast.parse(_asyncify(cell))
204
205 function_def = tree.body[0]
206 function_def.name = wrapper_name
207 try_block = function_def.body[0]
208 lastexpr = try_block.body[-1]
209 if isinstance(lastexpr, (Expr, Await)):
210 try_block.body[-1] = Return(lastexpr.value)
211 ast.fix_missing_locations(tree)
212 return tree
213 #-----------------------------------------------------------------------------
116 214 # Globals
117 215 #-----------------------------------------------------------------------------
118 216
119 217 # compiled regexps for autoindent management
120 218 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
121 219
122 220 #-----------------------------------------------------------------------------
123 221 # Utilities
124 222 #-----------------------------------------------------------------------------
125 223
126 224 @undoc
127 225 def softspace(file, newvalue):
128 226 """Copied from code.py, to remove the dependency"""
129 227
130 228 oldvalue = 0
131 229 try:
132 230 oldvalue = file.softspace
133 231 except AttributeError:
134 232 pass
135 233 try:
136 234 file.softspace = newvalue
137 235 except (AttributeError, TypeError):
138 236 # "attribute-less object" or "read-only attributes"
139 237 pass
140 238 return oldvalue
141 239
142 240 @undoc
143 241 def no_op(*a, **kw):
144 242 pass
145 243
146 244
147 245 class SpaceInInput(Exception): pass
148 246
149 247
150 248 def get_default_colors():
151 249 "DEPRECATED"
152 250 warn('get_default_color is deprecated since IPython 5.0, and returns `Neutral` on all platforms.',
153 251 DeprecationWarning, stacklevel=2)
154 252 return 'Neutral'
155 253
156 254
157 255 class SeparateUnicode(Unicode):
158 256 r"""A Unicode subclass to validate separate_in, separate_out, etc.
159 257
160 258 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
161 259 """
162 260
163 261 def validate(self, obj, value):
164 262 if value == '0': value = ''
165 263 value = value.replace('\\n','\n')
166 264 return super(SeparateUnicode, self).validate(obj, value)
167 265
168 266
169 267 @undoc
170 268 class DummyMod(object):
171 269 """A dummy module used for IPython's interactive module when
172 270 a namespace must be assigned to the module's __dict__."""
173 271 __spec__ = None
174 272
175 273
176 274 class ExecutionInfo(object):
177 275 """The arguments used for a call to :meth:`InteractiveShell.run_cell`
178 276
179 277 Stores information about what is going to happen.
180 278 """
181 279 raw_cell = None
182 280 store_history = False
183 281 silent = False
184 282 shell_futures = True
185 283
186 284 def __init__(self, raw_cell, store_history, silent, shell_futures):
187 285 self.raw_cell = raw_cell
188 286 self.store_history = store_history
189 287 self.silent = silent
190 288 self.shell_futures = shell_futures
191 289
192 290 def __repr__(self):
193 291 name = self.__class__.__qualname__
194 292 raw_cell = ((self.raw_cell[:50] + '..')
195 293 if len(self.raw_cell) > 50 else self.raw_cell)
196 294 return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s>' %\
197 295 (name, id(self), raw_cell, self.store_history, self.silent, self.shell_futures)
198 296
199 297
200 298 class ExecutionResult(object):
201 299 """The result of a call to :meth:`InteractiveShell.run_cell`
202 300
203 301 Stores information about what took place.
204 302 """
205 303 execution_count = None
206 304 error_before_exec = None
207 305 error_in_exec = None
208 306 info = None
209 307 result = None
210 308
211 309 def __init__(self, info):
212 310 self.info = info
213 311
214 312 @property
215 313 def success(self):
216 314 return (self.error_before_exec is None) and (self.error_in_exec is None)
217 315
218 316 def raise_error(self):
219 317 """Reraises error if `success` is `False`, otherwise does nothing"""
220 318 if self.error_before_exec is not None:
221 319 raise self.error_before_exec
222 320 if self.error_in_exec is not None:
223 321 raise self.error_in_exec
224 322
225 323 def __repr__(self):
226 324 name = self.__class__.__qualname__
227 325 return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\
228 326 (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result))
229 327
230 328
231 329 class InteractiveShell(SingletonConfigurable):
232 330 """An enhanced, interactive shell for Python."""
233 331
234 332 _instance = None
235 333
236 334 ast_transformers = List([], help=
237 335 """
238 336 A list of ast.NodeTransformer subclass instances, which will be applied
239 337 to user input before code is run.
240 338 """
241 339 ).tag(config=True)
242 340
243 341 autocall = Enum((0,1,2), default_value=0, help=
244 342 """
245 343 Make IPython automatically call any callable object even if you didn't
246 344 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
247 345 automatically. The value can be '0' to disable the feature, '1' for
248 346 'smart' autocall, where it is not applied if there are no more
249 347 arguments on the line, and '2' for 'full' autocall, where all callable
250 348 objects are automatically called (even if no arguments are present).
251 349 """
252 350 ).tag(config=True)
253 351 # TODO: remove all autoindent logic and put into frontends.
254 352 # We can't do this yet because even runlines uses the autoindent.
255 353 autoindent = Bool(True, help=
256 354 """
257 355 Autoindent IPython code entered interactively.
258 356 """
259 357 ).tag(config=True)
260 358
359 autoawait = Bool(True, help=
360 """
361 Automatically run await statement in the top level repl.
362 """
363 ).tag(config=True)
364
365 loop_runner_map ={
366 'asyncio':_asyncio_runner,
367 'curio':_curio_runner,
368 'trio':_trio_runner,
369 }
370
371 loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner",
372 allow_none=True,
373 help="""Select the loop runner that will be used to execute top-level asynchronous code"""
374 ).tag(config=True)
375
376 @default('loop_runner')
377 def _default_loop_runner(self):
378 return import_item("IPython.core.interactiveshell._asyncio_runner")
379
380 @validate('loop_runner')
381 def _import_runner(self, proposal):
382 if isinstance(proposal.value, str):
383 if proposal.value in self.loop_runner_map:
384 return self.loop_runner_map[proposal.value]
385 runner = import_item(proposal.value)
386 if not callable(runner):
387 raise ValueError('loop_runner must be callable')
388 return runner
389 if not callable(proposal.value):
390 raise ValueError('loop_runner must be callable')
391 return proposal.value
392
261 393 automagic = Bool(True, help=
262 394 """
263 395 Enable magic commands to be called without the leading %.
264 396 """
265 397 ).tag(config=True)
266 398
267 399 banner1 = Unicode(default_banner,
268 400 help="""The part of the banner to be printed before the profile"""
269 401 ).tag(config=True)
270 402 banner2 = Unicode('',
271 403 help="""The part of the banner to be printed after the profile"""
272 404 ).tag(config=True)
273 405
274 406 cache_size = Integer(1000, help=
275 407 """
276 408 Set the size of the output cache. The default is 1000, you can
277 409 change it permanently in your config file. Setting it to 0 completely
278 410 disables the caching system, and the minimum value accepted is 3 (if
279 411 you provide a value less than 3, it is reset to 0 and a warning is
280 412 issued). This limit is defined because otherwise you'll spend more
281 413 time re-flushing a too small cache than working
282 414 """
283 415 ).tag(config=True)
284 416 color_info = Bool(True, help=
285 417 """
286 418 Use colors for displaying information about objects. Because this
287 419 information is passed through a pager (like 'less'), and some pagers
288 420 get confused with color codes, this capability can be turned off.
289 421 """
290 422 ).tag(config=True)
291 423 colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'),
292 424 default_value='Neutral',
293 425 help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)."
294 426 ).tag(config=True)
295 427 debug = Bool(False).tag(config=True)
296 428 disable_failing_post_execute = Bool(False,
297 429 help="Don't call post-execute functions that have failed in the past."
298 430 ).tag(config=True)
299 431 display_formatter = Instance(DisplayFormatter, allow_none=True)
300 432 displayhook_class = Type(DisplayHook)
301 433 display_pub_class = Type(DisplayPublisher)
302 434
303 435 sphinxify_docstring = Bool(False, help=
304 436 """
305 437 Enables rich html representation of docstrings. (This requires the
306 438 docrepr module).
307 439 """).tag(config=True)
308 440
309 441 @observe("sphinxify_docstring")
310 442 def _sphinxify_docstring_changed(self, change):
311 443 if change['new']:
312 444 warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning)
313 445
314 446 enable_html_pager = Bool(False, help=
315 447 """
316 448 (Provisional API) enables html representation in mime bundles sent
317 449 to pagers.
318 450 """).tag(config=True)
319 451
320 452 @observe("enable_html_pager")
321 453 def _enable_html_pager_changed(self, change):
322 454 if change['new']:
323 455 warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning)
324 456
325 457 data_pub_class = None
326 458
327 459 exit_now = Bool(False)
328 460 exiter = Instance(ExitAutocall)
329 461 @default('exiter')
330 462 def _exiter_default(self):
331 463 return ExitAutocall(self)
332 464 # Monotonically increasing execution counter
333 465 execution_count = Integer(1)
334 466 filename = Unicode("<ipython console>")
335 467 ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__
336 468
337 469 # Input splitter, to transform input line by line and detect when a block
338 470 # is ready to be executed.
339 471 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
340 472 (), {'line_input_checker': True})
341 473
342 474 # This InputSplitter instance is used to transform completed cells before
343 475 # running them. It allows cell magics to contain blank lines.
344 476 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
345 477 (), {'line_input_checker': False})
346 478
347 479 logstart = Bool(False, help=
348 480 """
349 481 Start logging to the default log file in overwrite mode.
350 482 Use `logappend` to specify a log file to **append** logs to.
351 483 """
352 484 ).tag(config=True)
353 485 logfile = Unicode('', help=
354 486 """
355 487 The name of the logfile to use.
356 488 """
357 489 ).tag(config=True)
358 490 logappend = Unicode('', help=
359 491 """
360 492 Start logging to the given file in append mode.
361 493 Use `logfile` to specify a log file to **overwrite** logs to.
362 494 """
363 495 ).tag(config=True)
364 496 object_info_string_level = Enum((0,1,2), default_value=0,
365 497 ).tag(config=True)
366 498 pdb = Bool(False, help=
367 499 """
368 500 Automatically call the pdb debugger after every exception.
369 501 """
370 502 ).tag(config=True)
371 503 display_page = Bool(False,
372 504 help="""If True, anything that would be passed to the pager
373 505 will be displayed as regular output instead."""
374 506 ).tag(config=True)
375 507
376 508 # deprecated prompt traits:
377 509
378 510 prompt_in1 = Unicode('In [\\#]: ',
379 511 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
380 512 ).tag(config=True)
381 513 prompt_in2 = Unicode(' .\\D.: ',
382 514 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
383 515 ).tag(config=True)
384 516 prompt_out = Unicode('Out[\\#]: ',
385 517 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
386 518 ).tag(config=True)
387 519 prompts_pad_left = Bool(True,
388 520 help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly."
389 521 ).tag(config=True)
390 522
391 523 @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left')
392 524 def _prompt_trait_changed(self, change):
393 525 name = change['name']
394 526 warn("InteractiveShell.{name} is deprecated since IPython 4.0"
395 527 " and ignored since 5.0, set TerminalInteractiveShell.prompts"
396 528 " object directly.".format(name=name))
397 529
398 530 # protect against weird cases where self.config may not exist:
399 531
400 532 show_rewritten_input = Bool(True,
401 533 help="Show rewritten input, e.g. for autocall."
402 534 ).tag(config=True)
403 535
404 536 quiet = Bool(False).tag(config=True)
405 537
406 538 history_length = Integer(10000,
407 539 help='Total length of command history'
408 540 ).tag(config=True)
409 541
410 542 history_load_length = Integer(1000, help=
411 543 """
412 544 The number of saved history entries to be loaded
413 545 into the history buffer at startup.
414 546 """
415 547 ).tag(config=True)
416 548
417 549 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'],
418 550 default_value='last_expr',
419 551 help="""
420 552 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying
421 553 which nodes should be run interactively (displaying output from expressions).
422 554 """
423 555 ).tag(config=True)
424 556
425 557 # TODO: this part of prompt management should be moved to the frontends.
426 558 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
427 559 separate_in = SeparateUnicode('\n').tag(config=True)
428 560 separate_out = SeparateUnicode('').tag(config=True)
429 561 separate_out2 = SeparateUnicode('').tag(config=True)
430 562 wildcards_case_sensitive = Bool(True).tag(config=True)
431 563 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
432 564 default_value='Context',
433 565 help="Switch modes for the IPython exception handlers."
434 566 ).tag(config=True)
435 567
436 568 # Subcomponents of InteractiveShell
437 569 alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True)
438 570 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True)
439 571 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True)
440 572 display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True)
441 573 extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True)
442 574 payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True)
443 575 history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True)
444 576 magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True)
445 577
446 578 profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True)
447 579 @property
448 580 def profile(self):
449 581 if self.profile_dir is not None:
450 582 name = os.path.basename(self.profile_dir.location)
451 583 return name.replace('profile_','')
452 584
453 585
454 586 # Private interface
455 587 _post_execute = Dict()
456 588
457 589 # Tracks any GUI loop loaded for pylab
458 590 pylab_gui_select = None
459 591
460 592 last_execution_succeeded = Bool(True, help='Did last executed command succeeded')
461 593
462 594 last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True)
463 595
464 596 def __init__(self, ipython_dir=None, profile_dir=None,
465 597 user_module=None, user_ns=None,
466 598 custom_exceptions=((), None), **kwargs):
467 599
468 600 # This is where traits with a config_key argument are updated
469 601 # from the values on config.
470 602 super(InteractiveShell, self).__init__(**kwargs)
471 603 if 'PromptManager' in self.config:
472 604 warn('As of IPython 5.0 `PromptManager` config will have no effect'
473 605 ' and has been replaced by TerminalInteractiveShell.prompts_class')
474 606 self.configurables = [self]
475 607
476 608 # These are relatively independent and stateless
477 609 self.init_ipython_dir(ipython_dir)
478 610 self.init_profile_dir(profile_dir)
479 611 self.init_instance_attrs()
480 612 self.init_environment()
481 613
482 614 # Check if we're in a virtualenv, and set up sys.path.
483 615 self.init_virtualenv()
484 616
485 617 # Create namespaces (user_ns, user_global_ns, etc.)
486 618 self.init_create_namespaces(user_module, user_ns)
487 619 # This has to be done after init_create_namespaces because it uses
488 620 # something in self.user_ns, but before init_sys_modules, which
489 621 # is the first thing to modify sys.
490 622 # TODO: When we override sys.stdout and sys.stderr before this class
491 623 # is created, we are saving the overridden ones here. Not sure if this
492 624 # is what we want to do.
493 625 self.save_sys_module_state()
494 626 self.init_sys_modules()
495 627
496 628 # While we're trying to have each part of the code directly access what
497 629 # it needs without keeping redundant references to objects, we have too
498 630 # much legacy code that expects ip.db to exist.
499 631 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
500 632
501 633 self.init_history()
502 634 self.init_encoding()
503 635 self.init_prefilter()
504 636
505 637 self.init_syntax_highlighting()
506 638 self.init_hooks()
507 639 self.init_events()
508 640 self.init_pushd_popd_magic()
509 641 self.init_user_ns()
510 642 self.init_logger()
511 643 self.init_builtins()
512 644
513 645 # The following was in post_config_initialization
514 646 self.init_inspector()
515 647 self.raw_input_original = input
516 648 self.init_completer()
517 649 # TODO: init_io() needs to happen before init_traceback handlers
518 650 # because the traceback handlers hardcode the stdout/stderr streams.
519 651 # This logic in in debugger.Pdb and should eventually be changed.
520 652 self.init_io()
521 653 self.init_traceback_handlers(custom_exceptions)
522 654 self.init_prompts()
523 655 self.init_display_formatter()
524 656 self.init_display_pub()
525 657 self.init_data_pub()
526 658 self.init_displayhook()
527 659 self.init_magics()
528 660 self.init_alias()
529 661 self.init_logstart()
530 662 self.init_pdb()
531 663 self.init_extension_manager()
532 664 self.init_payload()
533 665 self.init_deprecation_warnings()
534 666 self.hooks.late_startup_hook()
535 667 self.events.trigger('shell_initialized', self)
536 668 atexit.register(self.atexit_operations)
537 669
538 670 def get_ipython(self):
539 671 """Return the currently running IPython instance."""
540 672 return self
541 673
542 674 #-------------------------------------------------------------------------
543 675 # Trait changed handlers
544 676 #-------------------------------------------------------------------------
545 677 @observe('ipython_dir')
546 678 def _ipython_dir_changed(self, change):
547 679 ensure_dir_exists(change['new'])
548 680
549 681 def set_autoindent(self,value=None):
550 682 """Set the autoindent flag.
551 683
552 684 If called with no arguments, it acts as a toggle."""
553 685 if value is None:
554 686 self.autoindent = not self.autoindent
555 687 else:
556 688 self.autoindent = value
557 689
558 690 #-------------------------------------------------------------------------
559 691 # init_* methods called by __init__
560 692 #-------------------------------------------------------------------------
561 693
562 694 def init_ipython_dir(self, ipython_dir):
563 695 if ipython_dir is not None:
564 696 self.ipython_dir = ipython_dir
565 697 return
566 698
567 699 self.ipython_dir = get_ipython_dir()
568 700
569 701 def init_profile_dir(self, profile_dir):
570 702 if profile_dir is not None:
571 703 self.profile_dir = profile_dir
572 704 return
573 705 self.profile_dir =\
574 706 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
575 707
576 708 def init_instance_attrs(self):
577 709 self.more = False
578 710
579 711 # command compiler
580 712 self.compile = CachingCompiler()
581 713
582 714 # Make an empty namespace, which extension writers can rely on both
583 715 # existing and NEVER being used by ipython itself. This gives them a
584 716 # convenient location for storing additional information and state
585 717 # their extensions may require, without fear of collisions with other
586 718 # ipython names that may develop later.
587 719 self.meta = Struct()
588 720
589 721 # Temporary files used for various purposes. Deleted at exit.
590 722 self.tempfiles = []
591 723 self.tempdirs = []
592 724
593 725 # keep track of where we started running (mainly for crash post-mortem)
594 726 # This is not being used anywhere currently.
595 727 self.starting_dir = os.getcwd()
596 728
597 729 # Indentation management
598 730 self.indent_current_nsp = 0
599 731
600 732 # Dict to track post-execution functions that have been registered
601 733 self._post_execute = {}
602 734
603 735 def init_environment(self):
604 736 """Any changes we need to make to the user's environment."""
605 737 pass
606 738
607 739 def init_encoding(self):
608 740 # Get system encoding at startup time. Certain terminals (like Emacs
609 741 # under Win32 have it set to None, and we need to have a known valid
610 742 # encoding to use in the raw_input() method
611 743 try:
612 744 self.stdin_encoding = sys.stdin.encoding or 'ascii'
613 745 except AttributeError:
614 746 self.stdin_encoding = 'ascii'
615 747
616 748
617 749 @observe('colors')
618 750 def init_syntax_highlighting(self, changes=None):
619 751 # Python source parser/formatter for syntax highlighting
620 752 pyformat = PyColorize.Parser(style=self.colors, parent=self).format
621 753 self.pycolorize = lambda src: pyformat(src,'str')
622 754
623 755 def refresh_style(self):
624 756 # No-op here, used in subclass
625 757 pass
626 758
627 759 def init_pushd_popd_magic(self):
628 760 # for pushd/popd management
629 761 self.home_dir = get_home_dir()
630 762
631 763 self.dir_stack = []
632 764
633 765 def init_logger(self):
634 766 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
635 767 logmode='rotate')
636 768
637 769 def init_logstart(self):
638 770 """Initialize logging in case it was requested at the command line.
639 771 """
640 772 if self.logappend:
641 773 self.magic('logstart %s append' % self.logappend)
642 774 elif self.logfile:
643 775 self.magic('logstart %s' % self.logfile)
644 776 elif self.logstart:
645 777 self.magic('logstart')
646 778
647 779 def init_deprecation_warnings(self):
648 780 """
649 781 register default filter for deprecation warning.
650 782
651 783 This will allow deprecation warning of function used interactively to show
652 784 warning to users, and still hide deprecation warning from libraries import.
653 785 """
654 786 warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__"))
655 787
656 788 def init_builtins(self):
657 789 # A single, static flag that we set to True. Its presence indicates
658 790 # that an IPython shell has been created, and we make no attempts at
659 791 # removing on exit or representing the existence of more than one
660 792 # IPython at a time.
661 793 builtin_mod.__dict__['__IPYTHON__'] = True
662 794 builtin_mod.__dict__['display'] = display
663 795
664 796 self.builtin_trap = BuiltinTrap(shell=self)
665 797
666 798 @observe('colors')
667 799 def init_inspector(self, changes=None):
668 800 # Object inspector
669 801 self.inspector = oinspect.Inspector(oinspect.InspectColors,
670 802 PyColorize.ANSICodeColors,
671 803 self.colors,
672 804 self.object_info_string_level)
673 805
674 806 def init_io(self):
675 807 # This will just use sys.stdout and sys.stderr. If you want to
676 808 # override sys.stdout and sys.stderr themselves, you need to do that
677 809 # *before* instantiating this class, because io holds onto
678 810 # references to the underlying streams.
679 811 # io.std* are deprecated, but don't show our own deprecation warnings
680 812 # during initialization of the deprecated API.
681 813 with warnings.catch_warnings():
682 814 warnings.simplefilter('ignore', DeprecationWarning)
683 815 io.stdout = io.IOStream(sys.stdout)
684 816 io.stderr = io.IOStream(sys.stderr)
685 817
686 818 def init_prompts(self):
687 819 # Set system prompts, so that scripts can decide if they are running
688 820 # interactively.
689 821 sys.ps1 = 'In : '
690 822 sys.ps2 = '...: '
691 823 sys.ps3 = 'Out: '
692 824
693 825 def init_display_formatter(self):
694 826 self.display_formatter = DisplayFormatter(parent=self)
695 827 self.configurables.append(self.display_formatter)
696 828
697 829 def init_display_pub(self):
698 830 self.display_pub = self.display_pub_class(parent=self)
699 831 self.configurables.append(self.display_pub)
700 832
701 833 def init_data_pub(self):
702 834 if not self.data_pub_class:
703 835 self.data_pub = None
704 836 return
705 837 self.data_pub = self.data_pub_class(parent=self)
706 838 self.configurables.append(self.data_pub)
707 839
708 840 def init_displayhook(self):
709 841 # Initialize displayhook, set in/out prompts and printing system
710 842 self.displayhook = self.displayhook_class(
711 843 parent=self,
712 844 shell=self,
713 845 cache_size=self.cache_size,
714 846 )
715 847 self.configurables.append(self.displayhook)
716 848 # This is a context manager that installs/revmoes the displayhook at
717 849 # the appropriate time.
718 850 self.display_trap = DisplayTrap(hook=self.displayhook)
719 851
720 852 def init_virtualenv(self):
721 853 """Add a virtualenv to sys.path so the user can import modules from it.
722 854 This isn't perfect: it doesn't use the Python interpreter with which the
723 855 virtualenv was built, and it ignores the --no-site-packages option. A
724 856 warning will appear suggesting the user installs IPython in the
725 857 virtualenv, but for many cases, it probably works well enough.
726 858
727 859 Adapted from code snippets online.
728 860
729 861 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
730 862 """
731 863 if 'VIRTUAL_ENV' not in os.environ:
732 864 # Not in a virtualenv
733 865 return
734 866
735 867 p = os.path.normcase(sys.executable)
736 868 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
737 869
738 870 # executable path should end like /bin/python or \\scripts\\python.exe
739 871 p_exe_up2 = os.path.dirname(os.path.dirname(p))
740 872 if p_exe_up2 and os.path.exists(p_venv) and os.path.samefile(p_exe_up2, p_venv):
741 873 # Our exe is inside the virtualenv, don't need to do anything.
742 874 return
743 875
744 876 # fallback venv detection:
745 877 # stdlib venv may symlink sys.executable, so we can't use realpath.
746 878 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
747 879 # So we just check every item in the symlink tree (generally <= 3)
748 880 paths = [p]
749 881 while os.path.islink(p):
750 882 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
751 883 paths.append(p)
752 884
753 885 # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
754 886 if p_venv.startswith('\\cygdrive'):
755 887 p_venv = p_venv[11:]
756 888 elif len(p_venv) >= 2 and p_venv[1] == ':':
757 889 p_venv = p_venv[2:]
758 890
759 891 if any(p_venv in p for p in paths):
760 892 # Running properly in the virtualenv, don't need to do anything
761 893 return
762 894
763 895 warn("Attempting to work in a virtualenv. If you encounter problems, please "
764 896 "install IPython inside the virtualenv.")
765 897 if sys.platform == "win32":
766 898 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
767 899 else:
768 900 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
769 901 'python%d.%d' % sys.version_info[:2], 'site-packages')
770 902
771 903 import site
772 904 sys.path.insert(0, virtual_env)
773 905 site.addsitedir(virtual_env)
774 906
775 907 #-------------------------------------------------------------------------
776 908 # Things related to injections into the sys module
777 909 #-------------------------------------------------------------------------
778 910
779 911 def save_sys_module_state(self):
780 912 """Save the state of hooks in the sys module.
781 913
782 914 This has to be called after self.user_module is created.
783 915 """
784 916 self._orig_sys_module_state = {'stdin': sys.stdin,
785 917 'stdout': sys.stdout,
786 918 'stderr': sys.stderr,
787 919 'excepthook': sys.excepthook}
788 920 self._orig_sys_modules_main_name = self.user_module.__name__
789 921 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
790 922
791 923 def restore_sys_module_state(self):
792 924 """Restore the state of the sys module."""
793 925 try:
794 926 for k, v in self._orig_sys_module_state.items():
795 927 setattr(sys, k, v)
796 928 except AttributeError:
797 929 pass
798 930 # Reset what what done in self.init_sys_modules
799 931 if self._orig_sys_modules_main_mod is not None:
800 932 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
801 933
802 934 #-------------------------------------------------------------------------
803 935 # Things related to the banner
804 936 #-------------------------------------------------------------------------
805 937
806 938 @property
807 939 def banner(self):
808 940 banner = self.banner1
809 941 if self.profile and self.profile != 'default':
810 942 banner += '\nIPython profile: %s\n' % self.profile
811 943 if self.banner2:
812 944 banner += '\n' + self.banner2
813 945 return banner
814 946
815 947 def show_banner(self, banner=None):
816 948 if banner is None:
817 949 banner = self.banner
818 950 sys.stdout.write(banner)
819 951
820 952 #-------------------------------------------------------------------------
821 953 # Things related to hooks
822 954 #-------------------------------------------------------------------------
823 955
824 956 def init_hooks(self):
825 957 # hooks holds pointers used for user-side customizations
826 958 self.hooks = Struct()
827 959
828 960 self.strdispatchers = {}
829 961
830 962 # Set all default hooks, defined in the IPython.hooks module.
831 963 hooks = IPython.core.hooks
832 964 for hook_name in hooks.__all__:
833 965 # default hooks have priority 100, i.e. low; user hooks should have
834 966 # 0-100 priority
835 967 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
836 968
837 969 if self.display_page:
838 970 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
839 971
840 972 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
841 973 _warn_deprecated=True):
842 974 """set_hook(name,hook) -> sets an internal IPython hook.
843 975
844 976 IPython exposes some of its internal API as user-modifiable hooks. By
845 977 adding your function to one of these hooks, you can modify IPython's
846 978 behavior to call at runtime your own routines."""
847 979
848 980 # At some point in the future, this should validate the hook before it
849 981 # accepts it. Probably at least check that the hook takes the number
850 982 # of args it's supposed to.
851 983
852 984 f = types.MethodType(hook,self)
853 985
854 986 # check if the hook is for strdispatcher first
855 987 if str_key is not None:
856 988 sdp = self.strdispatchers.get(name, StrDispatch())
857 989 sdp.add_s(str_key, f, priority )
858 990 self.strdispatchers[name] = sdp
859 991 return
860 992 if re_key is not None:
861 993 sdp = self.strdispatchers.get(name, StrDispatch())
862 994 sdp.add_re(re.compile(re_key), f, priority )
863 995 self.strdispatchers[name] = sdp
864 996 return
865 997
866 998 dp = getattr(self.hooks, name, None)
867 999 if name not in IPython.core.hooks.__all__:
868 1000 print("Warning! Hook '%s' is not one of %s" % \
869 1001 (name, IPython.core.hooks.__all__ ))
870 1002
871 1003 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
872 1004 alternative = IPython.core.hooks.deprecated[name]
873 1005 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative), stacklevel=2)
874 1006
875 1007 if not dp:
876 1008 dp = IPython.core.hooks.CommandChainDispatcher()
877 1009
878 1010 try:
879 1011 dp.add(f,priority)
880 1012 except AttributeError:
881 1013 # it was not commandchain, plain old func - replace
882 1014 dp = f
883 1015
884 1016 setattr(self.hooks,name, dp)
885 1017
886 1018 #-------------------------------------------------------------------------
887 1019 # Things related to events
888 1020 #-------------------------------------------------------------------------
889 1021
890 1022 def init_events(self):
891 1023 self.events = EventManager(self, available_events)
892 1024
893 1025 self.events.register("pre_execute", self._clear_warning_registry)
894 1026
895 1027 def register_post_execute(self, func):
896 1028 """DEPRECATED: Use ip.events.register('post_run_cell', func)
897 1029
898 1030 Register a function for calling after code execution.
899 1031 """
900 1032 warn("ip.register_post_execute is deprecated, use "
901 1033 "ip.events.register('post_run_cell', func) instead.", stacklevel=2)
902 1034 self.events.register('post_run_cell', func)
903 1035
904 1036 def _clear_warning_registry(self):
905 1037 # clear the warning registry, so that different code blocks with
906 1038 # overlapping line number ranges don't cause spurious suppression of
907 1039 # warnings (see gh-6611 for details)
908 1040 if "__warningregistry__" in self.user_global_ns:
909 1041 del self.user_global_ns["__warningregistry__"]
910 1042
911 1043 #-------------------------------------------------------------------------
912 1044 # Things related to the "main" module
913 1045 #-------------------------------------------------------------------------
914 1046
915 1047 def new_main_mod(self, filename, modname):
916 1048 """Return a new 'main' module object for user code execution.
917 1049
918 1050 ``filename`` should be the path of the script which will be run in the
919 1051 module. Requests with the same filename will get the same module, with
920 1052 its namespace cleared.
921 1053
922 1054 ``modname`` should be the module name - normally either '__main__' or
923 1055 the basename of the file without the extension.
924 1056
925 1057 When scripts are executed via %run, we must keep a reference to their
926 1058 __main__ module around so that Python doesn't
927 1059 clear it, rendering references to module globals useless.
928 1060
929 1061 This method keeps said reference in a private dict, keyed by the
930 1062 absolute path of the script. This way, for multiple executions of the
931 1063 same script we only keep one copy of the namespace (the last one),
932 1064 thus preventing memory leaks from old references while allowing the
933 1065 objects from the last execution to be accessible.
934 1066 """
935 1067 filename = os.path.abspath(filename)
936 1068 try:
937 1069 main_mod = self._main_mod_cache[filename]
938 1070 except KeyError:
939 1071 main_mod = self._main_mod_cache[filename] = types.ModuleType(
940 1072 modname,
941 1073 doc="Module created for script run in IPython")
942 1074 else:
943 1075 main_mod.__dict__.clear()
944 1076 main_mod.__name__ = modname
945 1077
946 1078 main_mod.__file__ = filename
947 1079 # It seems pydoc (and perhaps others) needs any module instance to
948 1080 # implement a __nonzero__ method
949 1081 main_mod.__nonzero__ = lambda : True
950 1082
951 1083 return main_mod
952 1084
953 1085 def clear_main_mod_cache(self):
954 1086 """Clear the cache of main modules.
955 1087
956 1088 Mainly for use by utilities like %reset.
957 1089
958 1090 Examples
959 1091 --------
960 1092
961 1093 In [15]: import IPython
962 1094
963 1095 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
964 1096
965 1097 In [17]: len(_ip._main_mod_cache) > 0
966 1098 Out[17]: True
967 1099
968 1100 In [18]: _ip.clear_main_mod_cache()
969 1101
970 1102 In [19]: len(_ip._main_mod_cache) == 0
971 1103 Out[19]: True
972 1104 """
973 1105 self._main_mod_cache.clear()
974 1106
975 1107 #-------------------------------------------------------------------------
976 1108 # Things related to debugging
977 1109 #-------------------------------------------------------------------------
978 1110
979 1111 def init_pdb(self):
980 1112 # Set calling of pdb on exceptions
981 1113 # self.call_pdb is a property
982 1114 self.call_pdb = self.pdb
983 1115
984 1116 def _get_call_pdb(self):
985 1117 return self._call_pdb
986 1118
987 1119 def _set_call_pdb(self,val):
988 1120
989 1121 if val not in (0,1,False,True):
990 1122 raise ValueError('new call_pdb value must be boolean')
991 1123
992 1124 # store value in instance
993 1125 self._call_pdb = val
994 1126
995 1127 # notify the actual exception handlers
996 1128 self.InteractiveTB.call_pdb = val
997 1129
998 1130 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
999 1131 'Control auto-activation of pdb at exceptions')
1000 1132
1001 1133 def debugger(self,force=False):
1002 1134 """Call the pdb debugger.
1003 1135
1004 1136 Keywords:
1005 1137
1006 1138 - force(False): by default, this routine checks the instance call_pdb
1007 1139 flag and does not actually invoke the debugger if the flag is false.
1008 1140 The 'force' option forces the debugger to activate even if the flag
1009 1141 is false.
1010 1142 """
1011 1143
1012 1144 if not (force or self.call_pdb):
1013 1145 return
1014 1146
1015 1147 if not hasattr(sys,'last_traceback'):
1016 1148 error('No traceback has been produced, nothing to debug.')
1017 1149 return
1018 1150
1019 1151 self.InteractiveTB.debugger(force=True)
1020 1152
1021 1153 #-------------------------------------------------------------------------
1022 1154 # Things related to IPython's various namespaces
1023 1155 #-------------------------------------------------------------------------
1024 1156 default_user_namespaces = True
1025 1157
1026 1158 def init_create_namespaces(self, user_module=None, user_ns=None):
1027 1159 # Create the namespace where the user will operate. user_ns is
1028 1160 # normally the only one used, and it is passed to the exec calls as
1029 1161 # the locals argument. But we do carry a user_global_ns namespace
1030 1162 # given as the exec 'globals' argument, This is useful in embedding
1031 1163 # situations where the ipython shell opens in a context where the
1032 1164 # distinction between locals and globals is meaningful. For
1033 1165 # non-embedded contexts, it is just the same object as the user_ns dict.
1034 1166
1035 1167 # FIXME. For some strange reason, __builtins__ is showing up at user
1036 1168 # level as a dict instead of a module. This is a manual fix, but I
1037 1169 # should really track down where the problem is coming from. Alex
1038 1170 # Schmolck reported this problem first.
1039 1171
1040 1172 # A useful post by Alex Martelli on this topic:
1041 1173 # Re: inconsistent value from __builtins__
1042 1174 # Von: Alex Martelli <aleaxit@yahoo.com>
1043 1175 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1044 1176 # Gruppen: comp.lang.python
1045 1177
1046 1178 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1047 1179 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1048 1180 # > <type 'dict'>
1049 1181 # > >>> print type(__builtins__)
1050 1182 # > <type 'module'>
1051 1183 # > Is this difference in return value intentional?
1052 1184
1053 1185 # Well, it's documented that '__builtins__' can be either a dictionary
1054 1186 # or a module, and it's been that way for a long time. Whether it's
1055 1187 # intentional (or sensible), I don't know. In any case, the idea is
1056 1188 # that if you need to access the built-in namespace directly, you
1057 1189 # should start with "import __builtin__" (note, no 's') which will
1058 1190 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1059 1191
1060 1192 # These routines return a properly built module and dict as needed by
1061 1193 # the rest of the code, and can also be used by extension writers to
1062 1194 # generate properly initialized namespaces.
1063 1195 if (user_ns is not None) or (user_module is not None):
1064 1196 self.default_user_namespaces = False
1065 1197 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1066 1198
1067 1199 # A record of hidden variables we have added to the user namespace, so
1068 1200 # we can list later only variables defined in actual interactive use.
1069 1201 self.user_ns_hidden = {}
1070 1202
1071 1203 # Now that FakeModule produces a real module, we've run into a nasty
1072 1204 # problem: after script execution (via %run), the module where the user
1073 1205 # code ran is deleted. Now that this object is a true module (needed
1074 1206 # so doctest and other tools work correctly), the Python module
1075 1207 # teardown mechanism runs over it, and sets to None every variable
1076 1208 # present in that module. Top-level references to objects from the
1077 1209 # script survive, because the user_ns is updated with them. However,
1078 1210 # calling functions defined in the script that use other things from
1079 1211 # the script will fail, because the function's closure had references
1080 1212 # to the original objects, which are now all None. So we must protect
1081 1213 # these modules from deletion by keeping a cache.
1082 1214 #
1083 1215 # To avoid keeping stale modules around (we only need the one from the
1084 1216 # last run), we use a dict keyed with the full path to the script, so
1085 1217 # only the last version of the module is held in the cache. Note,
1086 1218 # however, that we must cache the module *namespace contents* (their
1087 1219 # __dict__). Because if we try to cache the actual modules, old ones
1088 1220 # (uncached) could be destroyed while still holding references (such as
1089 1221 # those held by GUI objects that tend to be long-lived)>
1090 1222 #
1091 1223 # The %reset command will flush this cache. See the cache_main_mod()
1092 1224 # and clear_main_mod_cache() methods for details on use.
1093 1225
1094 1226 # This is the cache used for 'main' namespaces
1095 1227 self._main_mod_cache = {}
1096 1228
1097 1229 # A table holding all the namespaces IPython deals with, so that
1098 1230 # introspection facilities can search easily.
1099 1231 self.ns_table = {'user_global':self.user_module.__dict__,
1100 1232 'user_local':self.user_ns,
1101 1233 'builtin':builtin_mod.__dict__
1102 1234 }
1103 1235
1104 1236 @property
1105 1237 def user_global_ns(self):
1106 1238 return self.user_module.__dict__
1107 1239
1108 1240 def prepare_user_module(self, user_module=None, user_ns=None):
1109 1241 """Prepare the module and namespace in which user code will be run.
1110 1242
1111 1243 When IPython is started normally, both parameters are None: a new module
1112 1244 is created automatically, and its __dict__ used as the namespace.
1113 1245
1114 1246 If only user_module is provided, its __dict__ is used as the namespace.
1115 1247 If only user_ns is provided, a dummy module is created, and user_ns
1116 1248 becomes the global namespace. If both are provided (as they may be
1117 1249 when embedding), user_ns is the local namespace, and user_module
1118 1250 provides the global namespace.
1119 1251
1120 1252 Parameters
1121 1253 ----------
1122 1254 user_module : module, optional
1123 1255 The current user module in which IPython is being run. If None,
1124 1256 a clean module will be created.
1125 1257 user_ns : dict, optional
1126 1258 A namespace in which to run interactive commands.
1127 1259
1128 1260 Returns
1129 1261 -------
1130 1262 A tuple of user_module and user_ns, each properly initialised.
1131 1263 """
1132 1264 if user_module is None and user_ns is not None:
1133 1265 user_ns.setdefault("__name__", "__main__")
1134 1266 user_module = DummyMod()
1135 1267 user_module.__dict__ = user_ns
1136 1268
1137 1269 if user_module is None:
1138 1270 user_module = types.ModuleType("__main__",
1139 1271 doc="Automatically created module for IPython interactive environment")
1140 1272
1141 1273 # We must ensure that __builtin__ (without the final 's') is always
1142 1274 # available and pointing to the __builtin__ *module*. For more details:
1143 1275 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1144 1276 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1145 1277 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1146 1278
1147 1279 if user_ns is None:
1148 1280 user_ns = user_module.__dict__
1149 1281
1150 1282 return user_module, user_ns
1151 1283
1152 1284 def init_sys_modules(self):
1153 1285 # We need to insert into sys.modules something that looks like a
1154 1286 # module but which accesses the IPython namespace, for shelve and
1155 1287 # pickle to work interactively. Normally they rely on getting
1156 1288 # everything out of __main__, but for embedding purposes each IPython
1157 1289 # instance has its own private namespace, so we can't go shoving
1158 1290 # everything into __main__.
1159 1291
1160 1292 # note, however, that we should only do this for non-embedded
1161 1293 # ipythons, which really mimic the __main__.__dict__ with their own
1162 1294 # namespace. Embedded instances, on the other hand, should not do
1163 1295 # this because they need to manage the user local/global namespaces
1164 1296 # only, but they live within a 'normal' __main__ (meaning, they
1165 1297 # shouldn't overtake the execution environment of the script they're
1166 1298 # embedded in).
1167 1299
1168 1300 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1169 1301 main_name = self.user_module.__name__
1170 1302 sys.modules[main_name] = self.user_module
1171 1303
1172 1304 def init_user_ns(self):
1173 1305 """Initialize all user-visible namespaces to their minimum defaults.
1174 1306
1175 1307 Certain history lists are also initialized here, as they effectively
1176 1308 act as user namespaces.
1177 1309
1178 1310 Notes
1179 1311 -----
1180 1312 All data structures here are only filled in, they are NOT reset by this
1181 1313 method. If they were not empty before, data will simply be added to
1182 1314 them.
1183 1315 """
1184 1316 # This function works in two parts: first we put a few things in
1185 1317 # user_ns, and we sync that contents into user_ns_hidden so that these
1186 1318 # initial variables aren't shown by %who. After the sync, we add the
1187 1319 # rest of what we *do* want the user to see with %who even on a new
1188 1320 # session (probably nothing, so they really only see their own stuff)
1189 1321
1190 1322 # The user dict must *always* have a __builtin__ reference to the
1191 1323 # Python standard __builtin__ namespace, which must be imported.
1192 1324 # This is so that certain operations in prompt evaluation can be
1193 1325 # reliably executed with builtins. Note that we can NOT use
1194 1326 # __builtins__ (note the 's'), because that can either be a dict or a
1195 1327 # module, and can even mutate at runtime, depending on the context
1196 1328 # (Python makes no guarantees on it). In contrast, __builtin__ is
1197 1329 # always a module object, though it must be explicitly imported.
1198 1330
1199 1331 # For more details:
1200 1332 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1201 1333 ns = {}
1202 1334
1203 1335 # make global variables for user access to the histories
1204 1336 ns['_ih'] = self.history_manager.input_hist_parsed
1205 1337 ns['_oh'] = self.history_manager.output_hist
1206 1338 ns['_dh'] = self.history_manager.dir_hist
1207 1339
1208 1340 # user aliases to input and output histories. These shouldn't show up
1209 1341 # in %who, as they can have very large reprs.
1210 1342 ns['In'] = self.history_manager.input_hist_parsed
1211 1343 ns['Out'] = self.history_manager.output_hist
1212 1344
1213 1345 # Store myself as the public api!!!
1214 1346 ns['get_ipython'] = self.get_ipython
1215 1347
1216 1348 ns['exit'] = self.exiter
1217 1349 ns['quit'] = self.exiter
1218 1350
1219 1351 # Sync what we've added so far to user_ns_hidden so these aren't seen
1220 1352 # by %who
1221 1353 self.user_ns_hidden.update(ns)
1222 1354
1223 1355 # Anything put into ns now would show up in %who. Think twice before
1224 1356 # putting anything here, as we really want %who to show the user their
1225 1357 # stuff, not our variables.
1226 1358
1227 1359 # Finally, update the real user's namespace
1228 1360 self.user_ns.update(ns)
1229 1361
1230 1362 @property
1231 1363 def all_ns_refs(self):
1232 1364 """Get a list of references to all the namespace dictionaries in which
1233 1365 IPython might store a user-created object.
1234 1366
1235 1367 Note that this does not include the displayhook, which also caches
1236 1368 objects from the output."""
1237 1369 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1238 1370 [m.__dict__ for m in self._main_mod_cache.values()]
1239 1371
1240 1372 def reset(self, new_session=True):
1241 1373 """Clear all internal namespaces, and attempt to release references to
1242 1374 user objects.
1243 1375
1244 1376 If new_session is True, a new history session will be opened.
1245 1377 """
1246 1378 # Clear histories
1247 1379 self.history_manager.reset(new_session)
1248 1380 # Reset counter used to index all histories
1249 1381 if new_session:
1250 1382 self.execution_count = 1
1251 1383
1252 1384 # Reset last execution result
1253 1385 self.last_execution_succeeded = True
1254 1386 self.last_execution_result = None
1255 1387
1256 1388 # Flush cached output items
1257 1389 if self.displayhook.do_full_cache:
1258 1390 self.displayhook.flush()
1259 1391
1260 1392 # The main execution namespaces must be cleared very carefully,
1261 1393 # skipping the deletion of the builtin-related keys, because doing so
1262 1394 # would cause errors in many object's __del__ methods.
1263 1395 if self.user_ns is not self.user_global_ns:
1264 1396 self.user_ns.clear()
1265 1397 ns = self.user_global_ns
1266 1398 drop_keys = set(ns.keys())
1267 1399 drop_keys.discard('__builtin__')
1268 1400 drop_keys.discard('__builtins__')
1269 1401 drop_keys.discard('__name__')
1270 1402 for k in drop_keys:
1271 1403 del ns[k]
1272 1404
1273 1405 self.user_ns_hidden.clear()
1274 1406
1275 1407 # Restore the user namespaces to minimal usability
1276 1408 self.init_user_ns()
1277 1409
1278 1410 # Restore the default and user aliases
1279 1411 self.alias_manager.clear_aliases()
1280 1412 self.alias_manager.init_aliases()
1281 1413
1282 1414 # Flush the private list of module references kept for script
1283 1415 # execution protection
1284 1416 self.clear_main_mod_cache()
1285 1417
1286 1418 def del_var(self, varname, by_name=False):
1287 1419 """Delete a variable from the various namespaces, so that, as
1288 1420 far as possible, we're not keeping any hidden references to it.
1289 1421
1290 1422 Parameters
1291 1423 ----------
1292 1424 varname : str
1293 1425 The name of the variable to delete.
1294 1426 by_name : bool
1295 1427 If True, delete variables with the given name in each
1296 1428 namespace. If False (default), find the variable in the user
1297 1429 namespace, and delete references to it.
1298 1430 """
1299 1431 if varname in ('__builtin__', '__builtins__'):
1300 1432 raise ValueError("Refusing to delete %s" % varname)
1301 1433
1302 1434 ns_refs = self.all_ns_refs
1303 1435
1304 1436 if by_name: # Delete by name
1305 1437 for ns in ns_refs:
1306 1438 try:
1307 1439 del ns[varname]
1308 1440 except KeyError:
1309 1441 pass
1310 1442 else: # Delete by object
1311 1443 try:
1312 1444 obj = self.user_ns[varname]
1313 1445 except KeyError:
1314 1446 raise NameError("name '%s' is not defined" % varname)
1315 1447 # Also check in output history
1316 1448 ns_refs.append(self.history_manager.output_hist)
1317 1449 for ns in ns_refs:
1318 1450 to_delete = [n for n, o in ns.items() if o is obj]
1319 1451 for name in to_delete:
1320 1452 del ns[name]
1321 1453
1322 1454 # Ensure it is removed from the last execution result
1323 1455 if self.last_execution_result.result is obj:
1324 1456 self.last_execution_result = None
1325 1457
1326 1458 # displayhook keeps extra references, but not in a dictionary
1327 1459 for name in ('_', '__', '___'):
1328 1460 if getattr(self.displayhook, name) is obj:
1329 1461 setattr(self.displayhook, name, None)
1330 1462
1331 1463 def reset_selective(self, regex=None):
1332 1464 """Clear selective variables from internal namespaces based on a
1333 1465 specified regular expression.
1334 1466
1335 1467 Parameters
1336 1468 ----------
1337 1469 regex : string or compiled pattern, optional
1338 1470 A regular expression pattern that will be used in searching
1339 1471 variable names in the users namespaces.
1340 1472 """
1341 1473 if regex is not None:
1342 1474 try:
1343 1475 m = re.compile(regex)
1344 1476 except TypeError:
1345 1477 raise TypeError('regex must be a string or compiled pattern')
1346 1478 # Search for keys in each namespace that match the given regex
1347 1479 # If a match is found, delete the key/value pair.
1348 1480 for ns in self.all_ns_refs:
1349 1481 for var in ns:
1350 1482 if m.search(var):
1351 1483 del ns[var]
1352 1484
1353 1485 def push(self, variables, interactive=True):
1354 1486 """Inject a group of variables into the IPython user namespace.
1355 1487
1356 1488 Parameters
1357 1489 ----------
1358 1490 variables : dict, str or list/tuple of str
1359 1491 The variables to inject into the user's namespace. If a dict, a
1360 1492 simple update is done. If a str, the string is assumed to have
1361 1493 variable names separated by spaces. A list/tuple of str can also
1362 1494 be used to give the variable names. If just the variable names are
1363 1495 give (list/tuple/str) then the variable values looked up in the
1364 1496 callers frame.
1365 1497 interactive : bool
1366 1498 If True (default), the variables will be listed with the ``who``
1367 1499 magic.
1368 1500 """
1369 1501 vdict = None
1370 1502
1371 1503 # We need a dict of name/value pairs to do namespace updates.
1372 1504 if isinstance(variables, dict):
1373 1505 vdict = variables
1374 1506 elif isinstance(variables, (str, list, tuple)):
1375 1507 if isinstance(variables, str):
1376 1508 vlist = variables.split()
1377 1509 else:
1378 1510 vlist = variables
1379 1511 vdict = {}
1380 1512 cf = sys._getframe(1)
1381 1513 for name in vlist:
1382 1514 try:
1383 1515 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1384 1516 except:
1385 1517 print('Could not get variable %s from %s' %
1386 1518 (name,cf.f_code.co_name))
1387 1519 else:
1388 1520 raise ValueError('variables must be a dict/str/list/tuple')
1389 1521
1390 1522 # Propagate variables to user namespace
1391 1523 self.user_ns.update(vdict)
1392 1524
1393 1525 # And configure interactive visibility
1394 1526 user_ns_hidden = self.user_ns_hidden
1395 1527 if interactive:
1396 1528 for name in vdict:
1397 1529 user_ns_hidden.pop(name, None)
1398 1530 else:
1399 1531 user_ns_hidden.update(vdict)
1400 1532
1401 1533 def drop_by_id(self, variables):
1402 1534 """Remove a dict of variables from the user namespace, if they are the
1403 1535 same as the values in the dictionary.
1404 1536
1405 1537 This is intended for use by extensions: variables that they've added can
1406 1538 be taken back out if they are unloaded, without removing any that the
1407 1539 user has overwritten.
1408 1540
1409 1541 Parameters
1410 1542 ----------
1411 1543 variables : dict
1412 1544 A dictionary mapping object names (as strings) to the objects.
1413 1545 """
1414 1546 for name, obj in variables.items():
1415 1547 if name in self.user_ns and self.user_ns[name] is obj:
1416 1548 del self.user_ns[name]
1417 1549 self.user_ns_hidden.pop(name, None)
1418 1550
1419 1551 #-------------------------------------------------------------------------
1420 1552 # Things related to object introspection
1421 1553 #-------------------------------------------------------------------------
1422 1554
1423 1555 def _ofind(self, oname, namespaces=None):
1424 1556 """Find an object in the available namespaces.
1425 1557
1426 1558 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1427 1559
1428 1560 Has special code to detect magic functions.
1429 1561 """
1430 1562 oname = oname.strip()
1431 1563 if not oname.startswith(ESC_MAGIC) and \
1432 1564 not oname.startswith(ESC_MAGIC2) and \
1433 1565 not all(a.isidentifier() for a in oname.split(".")):
1434 1566 return {'found': False}
1435 1567
1436 1568 if namespaces is None:
1437 1569 # Namespaces to search in:
1438 1570 # Put them in a list. The order is important so that we
1439 1571 # find things in the same order that Python finds them.
1440 1572 namespaces = [ ('Interactive', self.user_ns),
1441 1573 ('Interactive (global)', self.user_global_ns),
1442 1574 ('Python builtin', builtin_mod.__dict__),
1443 1575 ]
1444 1576
1445 1577 ismagic = False
1446 1578 isalias = False
1447 1579 found = False
1448 1580 ospace = None
1449 1581 parent = None
1450 1582 obj = None
1451 1583
1584
1452 1585 # Look for the given name by splitting it in parts. If the head is
1453 1586 # found, then we look for all the remaining parts as members, and only
1454 1587 # declare success if we can find them all.
1455 1588 oname_parts = oname.split('.')
1456 1589 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1457 1590 for nsname,ns in namespaces:
1458 1591 try:
1459 1592 obj = ns[oname_head]
1460 1593 except KeyError:
1461 1594 continue
1462 1595 else:
1463 1596 for idx, part in enumerate(oname_rest):
1464 1597 try:
1465 1598 parent = obj
1466 1599 # The last part is looked up in a special way to avoid
1467 1600 # descriptor invocation as it may raise or have side
1468 1601 # effects.
1469 1602 if idx == len(oname_rest) - 1:
1470 1603 obj = self._getattr_property(obj, part)
1471 1604 else:
1472 1605 obj = getattr(obj, part)
1473 1606 except:
1474 1607 # Blanket except b/c some badly implemented objects
1475 1608 # allow __getattr__ to raise exceptions other than
1476 1609 # AttributeError, which then crashes IPython.
1477 1610 break
1478 1611 else:
1479 1612 # If we finish the for loop (no break), we got all members
1480 1613 found = True
1481 1614 ospace = nsname
1482 1615 break # namespace loop
1483 1616
1484 1617 # Try to see if it's magic
1485 1618 if not found:
1486 1619 obj = None
1487 1620 if oname.startswith(ESC_MAGIC2):
1488 1621 oname = oname.lstrip(ESC_MAGIC2)
1489 1622 obj = self.find_cell_magic(oname)
1490 1623 elif oname.startswith(ESC_MAGIC):
1491 1624 oname = oname.lstrip(ESC_MAGIC)
1492 1625 obj = self.find_line_magic(oname)
1493 1626 else:
1494 1627 # search without prefix, so run? will find %run?
1495 1628 obj = self.find_line_magic(oname)
1496 1629 if obj is None:
1497 1630 obj = self.find_cell_magic(oname)
1498 1631 if obj is not None:
1499 1632 found = True
1500 1633 ospace = 'IPython internal'
1501 1634 ismagic = True
1502 1635 isalias = isinstance(obj, Alias)
1503 1636
1504 1637 # Last try: special-case some literals like '', [], {}, etc:
1505 1638 if not found and oname_head in ["''",'""','[]','{}','()']:
1506 1639 obj = eval(oname_head)
1507 1640 found = True
1508 1641 ospace = 'Interactive'
1509 1642
1510 1643 return {
1511 1644 'obj':obj,
1512 1645 'found':found,
1513 1646 'parent':parent,
1514 1647 'ismagic':ismagic,
1515 1648 'isalias':isalias,
1516 1649 'namespace':ospace
1517 1650 }
1518 1651
1519 1652 @staticmethod
1520 1653 def _getattr_property(obj, attrname):
1521 1654 """Property-aware getattr to use in object finding.
1522 1655
1523 1656 If attrname represents a property, return it unevaluated (in case it has
1524 1657 side effects or raises an error.
1525 1658
1526 1659 """
1527 1660 if not isinstance(obj, type):
1528 1661 try:
1529 1662 # `getattr(type(obj), attrname)` is not guaranteed to return
1530 1663 # `obj`, but does so for property:
1531 1664 #
1532 1665 # property.__get__(self, None, cls) -> self
1533 1666 #
1534 1667 # The universal alternative is to traverse the mro manually
1535 1668 # searching for attrname in class dicts.
1536 1669 attr = getattr(type(obj), attrname)
1537 1670 except AttributeError:
1538 1671 pass
1539 1672 else:
1540 1673 # This relies on the fact that data descriptors (with both
1541 1674 # __get__ & __set__ magic methods) take precedence over
1542 1675 # instance-level attributes:
1543 1676 #
1544 1677 # class A(object):
1545 1678 # @property
1546 1679 # def foobar(self): return 123
1547 1680 # a = A()
1548 1681 # a.__dict__['foobar'] = 345
1549 1682 # a.foobar # == 123
1550 1683 #
1551 1684 # So, a property may be returned right away.
1552 1685 if isinstance(attr, property):
1553 1686 return attr
1554 1687
1555 1688 # Nothing helped, fall back.
1556 1689 return getattr(obj, attrname)
1557 1690
1558 1691 def _object_find(self, oname, namespaces=None):
1559 1692 """Find an object and return a struct with info about it."""
1560 1693 return Struct(self._ofind(oname, namespaces))
1561 1694
1562 1695 def _inspect(self, meth, oname, namespaces=None, **kw):
1563 1696 """Generic interface to the inspector system.
1564 1697
1565 1698 This function is meant to be called by pdef, pdoc & friends.
1566 1699 """
1567 1700 info = self._object_find(oname, namespaces)
1568 1701 docformat = sphinxify if self.sphinxify_docstring else None
1569 1702 if info.found:
1570 1703 pmethod = getattr(self.inspector, meth)
1571 1704 # TODO: only apply format_screen to the plain/text repr of the mime
1572 1705 # bundle.
1573 1706 formatter = format_screen if info.ismagic else docformat
1574 1707 if meth == 'pdoc':
1575 1708 pmethod(info.obj, oname, formatter)
1576 1709 elif meth == 'pinfo':
1577 1710 pmethod(info.obj, oname, formatter, info,
1578 1711 enable_html_pager=self.enable_html_pager, **kw)
1579 1712 else:
1580 1713 pmethod(info.obj, oname)
1581 1714 else:
1582 1715 print('Object `%s` not found.' % oname)
1583 1716 return 'not found' # so callers can take other action
1584 1717
1585 1718 def object_inspect(self, oname, detail_level=0):
1586 1719 """Get object info about oname"""
1587 1720 with self.builtin_trap:
1588 1721 info = self._object_find(oname)
1589 1722 if info.found:
1590 1723 return self.inspector.info(info.obj, oname, info=info,
1591 1724 detail_level=detail_level
1592 1725 )
1593 1726 else:
1594 1727 return oinspect.object_info(name=oname, found=False)
1595 1728
1596 1729 def object_inspect_text(self, oname, detail_level=0):
1597 1730 """Get object info as formatted text"""
1598 1731 return self.object_inspect_mime(oname, detail_level)['text/plain']
1599 1732
1600 1733 def object_inspect_mime(self, oname, detail_level=0):
1601 1734 """Get object info as a mimebundle of formatted representations.
1602 1735
1603 1736 A mimebundle is a dictionary, keyed by mime-type.
1604 1737 It must always have the key `'text/plain'`.
1605 1738 """
1606 1739 with self.builtin_trap:
1607 1740 info = self._object_find(oname)
1608 1741 if info.found:
1609 1742 return self.inspector._get_info(info.obj, oname, info=info,
1610 1743 detail_level=detail_level
1611 1744 )
1612 1745 else:
1613 1746 raise KeyError(oname)
1614 1747
1615 1748 #-------------------------------------------------------------------------
1616 1749 # Things related to history management
1617 1750 #-------------------------------------------------------------------------
1618 1751
1619 1752 def init_history(self):
1620 1753 """Sets up the command history, and starts regular autosaves."""
1621 1754 self.history_manager = HistoryManager(shell=self, parent=self)
1622 1755 self.configurables.append(self.history_manager)
1623 1756
1624 1757 #-------------------------------------------------------------------------
1625 1758 # Things related to exception handling and tracebacks (not debugging)
1626 1759 #-------------------------------------------------------------------------
1627 1760
1628 1761 debugger_cls = Pdb
1629 1762
1630 1763 def init_traceback_handlers(self, custom_exceptions):
1631 1764 # Syntax error handler.
1632 1765 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self)
1633 1766
1634 1767 # The interactive one is initialized with an offset, meaning we always
1635 1768 # want to remove the topmost item in the traceback, which is our own
1636 1769 # internal code. Valid modes: ['Plain','Context','Verbose']
1637 1770 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1638 1771 color_scheme='NoColor',
1639 1772 tb_offset = 1,
1640 1773 check_cache=check_linecache_ipython,
1641 1774 debugger_cls=self.debugger_cls, parent=self)
1642 1775
1643 1776 # The instance will store a pointer to the system-wide exception hook,
1644 1777 # so that runtime code (such as magics) can access it. This is because
1645 1778 # during the read-eval loop, it may get temporarily overwritten.
1646 1779 self.sys_excepthook = sys.excepthook
1647 1780
1648 1781 # and add any custom exception handlers the user may have specified
1649 1782 self.set_custom_exc(*custom_exceptions)
1650 1783
1651 1784 # Set the exception mode
1652 1785 self.InteractiveTB.set_mode(mode=self.xmode)
1653 1786
1654 1787 def set_custom_exc(self, exc_tuple, handler):
1655 1788 """set_custom_exc(exc_tuple, handler)
1656 1789
1657 1790 Set a custom exception handler, which will be called if any of the
1658 1791 exceptions in exc_tuple occur in the mainloop (specifically, in the
1659 1792 run_code() method).
1660 1793
1661 1794 Parameters
1662 1795 ----------
1663 1796
1664 1797 exc_tuple : tuple of exception classes
1665 1798 A *tuple* of exception classes, for which to call the defined
1666 1799 handler. It is very important that you use a tuple, and NOT A
1667 1800 LIST here, because of the way Python's except statement works. If
1668 1801 you only want to trap a single exception, use a singleton tuple::
1669 1802
1670 1803 exc_tuple == (MyCustomException,)
1671 1804
1672 1805 handler : callable
1673 1806 handler must have the following signature::
1674 1807
1675 1808 def my_handler(self, etype, value, tb, tb_offset=None):
1676 1809 ...
1677 1810 return structured_traceback
1678 1811
1679 1812 Your handler must return a structured traceback (a list of strings),
1680 1813 or None.
1681 1814
1682 1815 This will be made into an instance method (via types.MethodType)
1683 1816 of IPython itself, and it will be called if any of the exceptions
1684 1817 listed in the exc_tuple are caught. If the handler is None, an
1685 1818 internal basic one is used, which just prints basic info.
1686 1819
1687 1820 To protect IPython from crashes, if your handler ever raises an
1688 1821 exception or returns an invalid result, it will be immediately
1689 1822 disabled.
1690 1823
1691 1824 WARNING: by putting in your own exception handler into IPython's main
1692 1825 execution loop, you run a very good chance of nasty crashes. This
1693 1826 facility should only be used if you really know what you are doing."""
1694 1827 if not isinstance(exc_tuple, tuple):
1695 1828 raise TypeError("The custom exceptions must be given as a tuple.")
1696 1829
1697 1830 def dummy_handler(self, etype, value, tb, tb_offset=None):
1698 1831 print('*** Simple custom exception handler ***')
1699 1832 print('Exception type :', etype)
1700 1833 print('Exception value:', value)
1701 1834 print('Traceback :', tb)
1702 1835
1703 1836 def validate_stb(stb):
1704 1837 """validate structured traceback return type
1705 1838
1706 1839 return type of CustomTB *should* be a list of strings, but allow
1707 1840 single strings or None, which are harmless.
1708 1841
1709 1842 This function will *always* return a list of strings,
1710 1843 and will raise a TypeError if stb is inappropriate.
1711 1844 """
1712 1845 msg = "CustomTB must return list of strings, not %r" % stb
1713 1846 if stb is None:
1714 1847 return []
1715 1848 elif isinstance(stb, str):
1716 1849 return [stb]
1717 1850 elif not isinstance(stb, list):
1718 1851 raise TypeError(msg)
1719 1852 # it's a list
1720 1853 for line in stb:
1721 1854 # check every element
1722 1855 if not isinstance(line, str):
1723 1856 raise TypeError(msg)
1724 1857 return stb
1725 1858
1726 1859 if handler is None:
1727 1860 wrapped = dummy_handler
1728 1861 else:
1729 1862 def wrapped(self,etype,value,tb,tb_offset=None):
1730 1863 """wrap CustomTB handler, to protect IPython from user code
1731 1864
1732 1865 This makes it harder (but not impossible) for custom exception
1733 1866 handlers to crash IPython.
1734 1867 """
1735 1868 try:
1736 1869 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1737 1870 return validate_stb(stb)
1738 1871 except:
1739 1872 # clear custom handler immediately
1740 1873 self.set_custom_exc((), None)
1741 1874 print("Custom TB Handler failed, unregistering", file=sys.stderr)
1742 1875 # show the exception in handler first
1743 1876 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1744 1877 print(self.InteractiveTB.stb2text(stb))
1745 1878 print("The original exception:")
1746 1879 stb = self.InteractiveTB.structured_traceback(
1747 1880 (etype,value,tb), tb_offset=tb_offset
1748 1881 )
1749 1882 return stb
1750 1883
1751 1884 self.CustomTB = types.MethodType(wrapped,self)
1752 1885 self.custom_exceptions = exc_tuple
1753 1886
1754 1887 def excepthook(self, etype, value, tb):
1755 1888 """One more defense for GUI apps that call sys.excepthook.
1756 1889
1757 1890 GUI frameworks like wxPython trap exceptions and call
1758 1891 sys.excepthook themselves. I guess this is a feature that
1759 1892 enables them to keep running after exceptions that would
1760 1893 otherwise kill their mainloop. This is a bother for IPython
1761 1894 which excepts to catch all of the program exceptions with a try:
1762 1895 except: statement.
1763 1896
1764 1897 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1765 1898 any app directly invokes sys.excepthook, it will look to the user like
1766 1899 IPython crashed. In order to work around this, we can disable the
1767 1900 CrashHandler and replace it with this excepthook instead, which prints a
1768 1901 regular traceback using our InteractiveTB. In this fashion, apps which
1769 1902 call sys.excepthook will generate a regular-looking exception from
1770 1903 IPython, and the CrashHandler will only be triggered by real IPython
1771 1904 crashes.
1772 1905
1773 1906 This hook should be used sparingly, only in places which are not likely
1774 1907 to be true IPython errors.
1775 1908 """
1776 1909 self.showtraceback((etype, value, tb), tb_offset=0)
1777 1910
1778 1911 def _get_exc_info(self, exc_tuple=None):
1779 1912 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1780 1913
1781 1914 Ensures sys.last_type,value,traceback hold the exc_info we found,
1782 1915 from whichever source.
1783 1916
1784 1917 raises ValueError if none of these contain any information
1785 1918 """
1786 1919 if exc_tuple is None:
1787 1920 etype, value, tb = sys.exc_info()
1788 1921 else:
1789 1922 etype, value, tb = exc_tuple
1790 1923
1791 1924 if etype is None:
1792 1925 if hasattr(sys, 'last_type'):
1793 1926 etype, value, tb = sys.last_type, sys.last_value, \
1794 1927 sys.last_traceback
1795 1928
1796 1929 if etype is None:
1797 1930 raise ValueError("No exception to find")
1798 1931
1799 1932 # Now store the exception info in sys.last_type etc.
1800 1933 # WARNING: these variables are somewhat deprecated and not
1801 1934 # necessarily safe to use in a threaded environment, but tools
1802 1935 # like pdb depend on their existence, so let's set them. If we
1803 1936 # find problems in the field, we'll need to revisit their use.
1804 1937 sys.last_type = etype
1805 1938 sys.last_value = value
1806 1939 sys.last_traceback = tb
1807 1940
1808 1941 return etype, value, tb
1809 1942
1810 1943 def show_usage_error(self, exc):
1811 1944 """Show a short message for UsageErrors
1812 1945
1813 1946 These are special exceptions that shouldn't show a traceback.
1814 1947 """
1815 1948 print("UsageError: %s" % exc, file=sys.stderr)
1816 1949
1817 1950 def get_exception_only(self, exc_tuple=None):
1818 1951 """
1819 1952 Return as a string (ending with a newline) the exception that
1820 1953 just occurred, without any traceback.
1821 1954 """
1822 1955 etype, value, tb = self._get_exc_info(exc_tuple)
1823 1956 msg = traceback.format_exception_only(etype, value)
1824 1957 return ''.join(msg)
1825 1958
1826 1959 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1827 1960 exception_only=False, running_compiled_code=False):
1828 1961 """Display the exception that just occurred.
1829 1962
1830 1963 If nothing is known about the exception, this is the method which
1831 1964 should be used throughout the code for presenting user tracebacks,
1832 1965 rather than directly invoking the InteractiveTB object.
1833 1966
1834 1967 A specific showsyntaxerror() also exists, but this method can take
1835 1968 care of calling it if needed, so unless you are explicitly catching a
1836 1969 SyntaxError exception, don't try to analyze the stack manually and
1837 1970 simply call this method."""
1838 1971
1839 1972 try:
1840 1973 try:
1841 1974 etype, value, tb = self._get_exc_info(exc_tuple)
1842 1975 except ValueError:
1843 1976 print('No traceback available to show.', file=sys.stderr)
1844 1977 return
1845 1978
1846 1979 if issubclass(etype, SyntaxError):
1847 1980 # Though this won't be called by syntax errors in the input
1848 1981 # line, there may be SyntaxError cases with imported code.
1849 1982 self.showsyntaxerror(filename, running_compiled_code)
1850 1983 elif etype is UsageError:
1851 1984 self.show_usage_error(value)
1852 1985 else:
1853 1986 if exception_only:
1854 1987 stb = ['An exception has occurred, use %tb to see '
1855 1988 'the full traceback.\n']
1856 1989 stb.extend(self.InteractiveTB.get_exception_only(etype,
1857 1990 value))
1858 1991 else:
1859 1992 try:
1860 1993 # Exception classes can customise their traceback - we
1861 1994 # use this in IPython.parallel for exceptions occurring
1862 1995 # in the engines. This should return a list of strings.
1863 1996 stb = value._render_traceback_()
1864 1997 except Exception:
1865 1998 stb = self.InteractiveTB.structured_traceback(etype,
1866 1999 value, tb, tb_offset=tb_offset)
1867 2000
1868 2001 self._showtraceback(etype, value, stb)
1869 2002 if self.call_pdb:
1870 2003 # drop into debugger
1871 2004 self.debugger(force=True)
1872 2005 return
1873 2006
1874 2007 # Actually show the traceback
1875 2008 self._showtraceback(etype, value, stb)
1876 2009
1877 2010 except KeyboardInterrupt:
1878 2011 print('\n' + self.get_exception_only(), file=sys.stderr)
1879 2012
1880 2013 def _showtraceback(self, etype, evalue, stb):
1881 2014 """Actually show a traceback.
1882 2015
1883 2016 Subclasses may override this method to put the traceback on a different
1884 2017 place, like a side channel.
1885 2018 """
1886 2019 print(self.InteractiveTB.stb2text(stb))
1887 2020
1888 2021 def showsyntaxerror(self, filename=None, running_compiled_code=False):
1889 2022 """Display the syntax error that just occurred.
1890 2023
1891 2024 This doesn't display a stack trace because there isn't one.
1892 2025
1893 2026 If a filename is given, it is stuffed in the exception instead
1894 2027 of what was there before (because Python's parser always uses
1895 2028 "<string>" when reading from a string).
1896 2029
1897 2030 If the syntax error occurred when running a compiled code (i.e. running_compile_code=True),
1898 2031 longer stack trace will be displayed.
1899 2032 """
1900 2033 etype, value, last_traceback = self._get_exc_info()
1901 2034
1902 2035 if filename and issubclass(etype, SyntaxError):
1903 2036 try:
1904 2037 value.filename = filename
1905 2038 except:
1906 2039 # Not the format we expect; leave it alone
1907 2040 pass
1908 2041
1909 2042 # If the error occurred when executing compiled code, we should provide full stacktrace.
1910 2043 elist = traceback.extract_tb(last_traceback) if running_compiled_code else []
1911 2044 stb = self.SyntaxTB.structured_traceback(etype, value, elist)
1912 2045 self._showtraceback(etype, value, stb)
1913 2046
1914 2047 # This is overridden in TerminalInteractiveShell to show a message about
1915 2048 # the %paste magic.
1916 2049 def showindentationerror(self):
1917 2050 """Called by _run_cell when there's an IndentationError in code entered
1918 2051 at the prompt.
1919 2052
1920 2053 This is overridden in TerminalInteractiveShell to show a message about
1921 2054 the %paste magic."""
1922 2055 self.showsyntaxerror()
1923 2056
1924 2057 #-------------------------------------------------------------------------
1925 2058 # Things related to readline
1926 2059 #-------------------------------------------------------------------------
1927 2060
1928 2061 def init_readline(self):
1929 2062 """DEPRECATED
1930 2063
1931 2064 Moved to terminal subclass, here only to simplify the init logic."""
1932 2065 # Set a number of methods that depend on readline to be no-op
1933 2066 warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated',
1934 2067 DeprecationWarning, stacklevel=2)
1935 2068 self.set_custom_completer = no_op
1936 2069
1937 2070 @skip_doctest
1938 2071 def set_next_input(self, s, replace=False):
1939 2072 """ Sets the 'default' input string for the next command line.
1940 2073
1941 2074 Example::
1942 2075
1943 2076 In [1]: _ip.set_next_input("Hello Word")
1944 2077 In [2]: Hello Word_ # cursor is here
1945 2078 """
1946 2079 self.rl_next_input = s
1947 2080
1948 2081 def _indent_current_str(self):
1949 2082 """return the current level of indentation as a string"""
1950 2083 return self.input_splitter.get_indent_spaces() * ' '
1951 2084
1952 2085 #-------------------------------------------------------------------------
1953 2086 # Things related to text completion
1954 2087 #-------------------------------------------------------------------------
1955 2088
1956 2089 def init_completer(self):
1957 2090 """Initialize the completion machinery.
1958 2091
1959 2092 This creates completion machinery that can be used by client code,
1960 2093 either interactively in-process (typically triggered by the readline
1961 2094 library), programmatically (such as in test suites) or out-of-process
1962 2095 (typically over the network by remote frontends).
1963 2096 """
1964 2097 from IPython.core.completer import IPCompleter
1965 2098 from IPython.core.completerlib import (module_completer,
1966 2099 magic_run_completer, cd_completer, reset_completer)
1967 2100
1968 2101 self.Completer = IPCompleter(shell=self,
1969 2102 namespace=self.user_ns,
1970 2103 global_namespace=self.user_global_ns,
1971 2104 parent=self,
1972 2105 )
1973 2106 self.configurables.append(self.Completer)
1974 2107
1975 2108 # Add custom completers to the basic ones built into IPCompleter
1976 2109 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1977 2110 self.strdispatchers['complete_command'] = sdisp
1978 2111 self.Completer.custom_completers = sdisp
1979 2112
1980 2113 self.set_hook('complete_command', module_completer, str_key = 'import')
1981 2114 self.set_hook('complete_command', module_completer, str_key = 'from')
1982 2115 self.set_hook('complete_command', module_completer, str_key = '%aimport')
1983 2116 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1984 2117 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1985 2118 self.set_hook('complete_command', reset_completer, str_key = '%reset')
1986 2119
1987
1988 2120 @skip_doctest
1989 2121 def complete(self, text, line=None, cursor_pos=None):
1990 2122 """Return the completed text and a list of completions.
1991 2123
1992 2124 Parameters
1993 2125 ----------
1994 2126
1995 2127 text : string
1996 2128 A string of text to be completed on. It can be given as empty and
1997 2129 instead a line/position pair are given. In this case, the
1998 2130 completer itself will split the line like readline does.
1999 2131
2000 2132 line : string, optional
2001 2133 The complete line that text is part of.
2002 2134
2003 2135 cursor_pos : int, optional
2004 2136 The position of the cursor on the input line.
2005 2137
2006 2138 Returns
2007 2139 -------
2008 2140 text : string
2009 2141 The actual text that was completed.
2010 2142
2011 2143 matches : list
2012 2144 A sorted list with all possible completions.
2013 2145
2014 2146 The optional arguments allow the completion to take more context into
2015 2147 account, and are part of the low-level completion API.
2016 2148
2017 2149 This is a wrapper around the completion mechanism, similar to what
2018 2150 readline does at the command line when the TAB key is hit. By
2019 2151 exposing it as a method, it can be used by other non-readline
2020 2152 environments (such as GUIs) for text completion.
2021 2153
2022 2154 Simple usage example:
2023 2155
2024 2156 In [1]: x = 'hello'
2025 2157
2026 2158 In [2]: _ip.complete('x.l')
2027 2159 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2028 2160 """
2029 2161
2030 2162 # Inject names into __builtin__ so we can complete on the added names.
2031 2163 with self.builtin_trap:
2032 2164 return self.Completer.complete(text, line, cursor_pos)
2033 2165
2034 2166 def set_custom_completer(self, completer, pos=0):
2035 2167 """Adds a new custom completer function.
2036 2168
2037 2169 The position argument (defaults to 0) is the index in the completers
2038 2170 list where you want the completer to be inserted."""
2039 2171
2040 2172 newcomp = types.MethodType(completer,self.Completer)
2041 2173 self.Completer.matchers.insert(pos,newcomp)
2042 2174
2043 2175 def set_completer_frame(self, frame=None):
2044 2176 """Set the frame of the completer."""
2045 2177 if frame:
2046 2178 self.Completer.namespace = frame.f_locals
2047 2179 self.Completer.global_namespace = frame.f_globals
2048 2180 else:
2049 2181 self.Completer.namespace = self.user_ns
2050 2182 self.Completer.global_namespace = self.user_global_ns
2051 2183
2052 2184 #-------------------------------------------------------------------------
2053 2185 # Things related to magics
2054 2186 #-------------------------------------------------------------------------
2055 2187
2056 2188 def init_magics(self):
2057 2189 from IPython.core import magics as m
2058 2190 self.magics_manager = magic.MagicsManager(shell=self,
2059 2191 parent=self,
2060 2192 user_magics=m.UserMagics(self))
2061 2193 self.configurables.append(self.magics_manager)
2062 2194
2063 2195 # Expose as public API from the magics manager
2064 2196 self.register_magics = self.magics_manager.register
2065 2197
2066 2198 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2067 2199 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2068 2200 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2069 2201 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2070 2202 )
2071 2203
2072 2204 # Register Magic Aliases
2073 2205 mman = self.magics_manager
2074 2206 # FIXME: magic aliases should be defined by the Magics classes
2075 2207 # or in MagicsManager, not here
2076 2208 mman.register_alias('ed', 'edit')
2077 2209 mman.register_alias('hist', 'history')
2078 2210 mman.register_alias('rep', 'recall')
2079 2211 mman.register_alias('SVG', 'svg', 'cell')
2080 2212 mman.register_alias('HTML', 'html', 'cell')
2081 2213 mman.register_alias('file', 'writefile', 'cell')
2082 2214
2083 2215 # FIXME: Move the color initialization to the DisplayHook, which
2084 2216 # should be split into a prompt manager and displayhook. We probably
2085 2217 # even need a centralize colors management object.
2086 2218 self.run_line_magic('colors', self.colors)
2087 2219
2088 2220 # Defined here so that it's included in the documentation
2089 2221 @functools.wraps(magic.MagicsManager.register_function)
2090 2222 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2091 2223 self.magics_manager.register_function(func,
2092 2224 magic_kind=magic_kind, magic_name=magic_name)
2093 2225
2094 2226 def run_line_magic(self, magic_name, line, _stack_depth=1):
2095 2227 """Execute the given line magic.
2096 2228
2097 2229 Parameters
2098 2230 ----------
2099 2231 magic_name : str
2100 2232 Name of the desired magic function, without '%' prefix.
2101 2233
2102 2234 line : str
2103 2235 The rest of the input line as a single string.
2104 2236
2105 2237 _stack_depth : int
2106 2238 If run_line_magic() is called from magic() then _stack_depth=2.
2107 2239 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2108 2240 """
2109 2241 fn = self.find_line_magic(magic_name)
2110 2242 if fn is None:
2111 2243 cm = self.find_cell_magic(magic_name)
2112 2244 etpl = "Line magic function `%%%s` not found%s."
2113 2245 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2114 2246 'did you mean that instead?)' % magic_name )
2115 2247 raise UsageError(etpl % (magic_name, extra))
2116 2248 else:
2117 2249 # Note: this is the distance in the stack to the user's frame.
2118 2250 # This will need to be updated if the internal calling logic gets
2119 2251 # refactored, or else we'll be expanding the wrong variables.
2120 2252
2121 2253 # Determine stack_depth depending on where run_line_magic() has been called
2122 2254 stack_depth = _stack_depth
2123 2255 magic_arg_s = self.var_expand(line, stack_depth)
2124 2256 # Put magic args in a list so we can call with f(*a) syntax
2125 2257 args = [magic_arg_s]
2126 2258 kwargs = {}
2127 2259 # Grab local namespace if we need it:
2128 2260 if getattr(fn, "needs_local_scope", False):
2129 2261 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2130 2262 with self.builtin_trap:
2131 2263 result = fn(*args,**kwargs)
2132 2264 return result
2133 2265
2134 2266 def run_cell_magic(self, magic_name, line, cell):
2135 2267 """Execute the given cell magic.
2136 2268
2137 2269 Parameters
2138 2270 ----------
2139 2271 magic_name : str
2140 2272 Name of the desired magic function, without '%' prefix.
2141 2273
2142 2274 line : str
2143 2275 The rest of the first input line as a single string.
2144 2276
2145 2277 cell : str
2146 2278 The body of the cell as a (possibly multiline) string.
2147 2279 """
2148 2280 fn = self.find_cell_magic(magic_name)
2149 2281 if fn is None:
2150 2282 lm = self.find_line_magic(magic_name)
2151 2283 etpl = "Cell magic `%%{0}` not found{1}."
2152 2284 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2153 2285 'did you mean that instead?)'.format(magic_name))
2154 2286 raise UsageError(etpl.format(magic_name, extra))
2155 2287 elif cell == '':
2156 2288 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2157 2289 if self.find_line_magic(magic_name) is not None:
2158 2290 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2159 2291 raise UsageError(message)
2160 2292 else:
2161 2293 # Note: this is the distance in the stack to the user's frame.
2162 2294 # This will need to be updated if the internal calling logic gets
2163 2295 # refactored, or else we'll be expanding the wrong variables.
2164 2296 stack_depth = 2
2165 2297 magic_arg_s = self.var_expand(line, stack_depth)
2166 2298 with self.builtin_trap:
2167 2299 result = fn(magic_arg_s, cell)
2168 2300 return result
2169 2301
2170 2302 def find_line_magic(self, magic_name):
2171 2303 """Find and return a line magic by name.
2172 2304
2173 2305 Returns None if the magic isn't found."""
2174 2306 return self.magics_manager.magics['line'].get(magic_name)
2175 2307
2176 2308 def find_cell_magic(self, magic_name):
2177 2309 """Find and return a cell magic by name.
2178 2310
2179 2311 Returns None if the magic isn't found."""
2180 2312 return self.magics_manager.magics['cell'].get(magic_name)
2181 2313
2182 2314 def find_magic(self, magic_name, magic_kind='line'):
2183 2315 """Find and return a magic of the given type by name.
2184 2316
2185 2317 Returns None if the magic isn't found."""
2186 2318 return self.magics_manager.magics[magic_kind].get(magic_name)
2187 2319
2188 2320 def magic(self, arg_s):
2189 2321 """DEPRECATED. Use run_line_magic() instead.
2190 2322
2191 2323 Call a magic function by name.
2192 2324
2193 2325 Input: a string containing the name of the magic function to call and
2194 2326 any additional arguments to be passed to the magic.
2195 2327
2196 2328 magic('name -opt foo bar') is equivalent to typing at the ipython
2197 2329 prompt:
2198 2330
2199 2331 In[1]: %name -opt foo bar
2200 2332
2201 2333 To call a magic without arguments, simply use magic('name').
2202 2334
2203 2335 This provides a proper Python function to call IPython's magics in any
2204 2336 valid Python code you can type at the interpreter, including loops and
2205 2337 compound statements.
2206 2338 """
2207 2339 # TODO: should we issue a loud deprecation warning here?
2208 2340 magic_name, _, magic_arg_s = arg_s.partition(' ')
2209 2341 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2210 2342 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2211 2343
2212 2344 #-------------------------------------------------------------------------
2213 2345 # Things related to macros
2214 2346 #-------------------------------------------------------------------------
2215 2347
2216 2348 def define_macro(self, name, themacro):
2217 2349 """Define a new macro
2218 2350
2219 2351 Parameters
2220 2352 ----------
2221 2353 name : str
2222 2354 The name of the macro.
2223 2355 themacro : str or Macro
2224 2356 The action to do upon invoking the macro. If a string, a new
2225 2357 Macro object is created by passing the string to it.
2226 2358 """
2227 2359
2228 2360 from IPython.core import macro
2229 2361
2230 2362 if isinstance(themacro, str):
2231 2363 themacro = macro.Macro(themacro)
2232 2364 if not isinstance(themacro, macro.Macro):
2233 2365 raise ValueError('A macro must be a string or a Macro instance.')
2234 2366 self.user_ns[name] = themacro
2235 2367
2236 2368 #-------------------------------------------------------------------------
2237 2369 # Things related to the running of system commands
2238 2370 #-------------------------------------------------------------------------
2239 2371
2240 2372 def system_piped(self, cmd):
2241 2373 """Call the given cmd in a subprocess, piping stdout/err
2242 2374
2243 2375 Parameters
2244 2376 ----------
2245 2377 cmd : str
2246 2378 Command to execute (can not end in '&', as background processes are
2247 2379 not supported. Should not be a command that expects input
2248 2380 other than simple text.
2249 2381 """
2250 2382 if cmd.rstrip().endswith('&'):
2251 2383 # this is *far* from a rigorous test
2252 2384 # We do not support backgrounding processes because we either use
2253 2385 # pexpect or pipes to read from. Users can always just call
2254 2386 # os.system() or use ip.system=ip.system_raw
2255 2387 # if they really want a background process.
2256 2388 raise OSError("Background processes not supported.")
2257 2389
2258 2390 # we explicitly do NOT return the subprocess status code, because
2259 2391 # a non-None value would trigger :func:`sys.displayhook` calls.
2260 2392 # Instead, we store the exit_code in user_ns.
2261 2393 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2262 2394
2263 2395 def system_raw(self, cmd):
2264 2396 """Call the given cmd in a subprocess using os.system on Windows or
2265 2397 subprocess.call using the system shell on other platforms.
2266 2398
2267 2399 Parameters
2268 2400 ----------
2269 2401 cmd : str
2270 2402 Command to execute.
2271 2403 """
2272 2404 cmd = self.var_expand(cmd, depth=1)
2273 2405 # protect os.system from UNC paths on Windows, which it can't handle:
2274 2406 if sys.platform == 'win32':
2275 2407 from IPython.utils._process_win32 import AvoidUNCPath
2276 2408 with AvoidUNCPath() as path:
2277 2409 if path is not None:
2278 2410 cmd = '"pushd %s &&"%s' % (path, cmd)
2279 2411 try:
2280 2412 ec = os.system(cmd)
2281 2413 except KeyboardInterrupt:
2282 2414 print('\n' + self.get_exception_only(), file=sys.stderr)
2283 2415 ec = -2
2284 2416 else:
2285 2417 # For posix the result of the subprocess.call() below is an exit
2286 2418 # code, which by convention is zero for success, positive for
2287 2419 # program failure. Exit codes above 128 are reserved for signals,
2288 2420 # and the formula for converting a signal to an exit code is usually
2289 2421 # signal_number+128. To more easily differentiate between exit
2290 2422 # codes and signals, ipython uses negative numbers. For instance
2291 2423 # since control-c is signal 2 but exit code 130, ipython's
2292 2424 # _exit_code variable will read -2. Note that some shells like
2293 2425 # csh and fish don't follow sh/bash conventions for exit codes.
2294 2426 executable = os.environ.get('SHELL', None)
2295 2427 try:
2296 2428 # Use env shell instead of default /bin/sh
2297 2429 ec = subprocess.call(cmd, shell=True, executable=executable)
2298 2430 except KeyboardInterrupt:
2299 2431 # intercept control-C; a long traceback is not useful here
2300 2432 print('\n' + self.get_exception_only(), file=sys.stderr)
2301 2433 ec = 130
2302 2434 if ec > 128:
2303 2435 ec = -(ec - 128)
2304 2436
2305 2437 # We explicitly do NOT return the subprocess status code, because
2306 2438 # a non-None value would trigger :func:`sys.displayhook` calls.
2307 2439 # Instead, we store the exit_code in user_ns. Note the semantics
2308 2440 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2309 2441 # but raising SystemExit(_exit_code) will give status 254!
2310 2442 self.user_ns['_exit_code'] = ec
2311 2443
2312 2444 # use piped system by default, because it is better behaved
2313 2445 system = system_piped
2314 2446
2315 2447 def getoutput(self, cmd, split=True, depth=0):
2316 2448 """Get output (possibly including stderr) from a subprocess.
2317 2449
2318 2450 Parameters
2319 2451 ----------
2320 2452 cmd : str
2321 2453 Command to execute (can not end in '&', as background processes are
2322 2454 not supported.
2323 2455 split : bool, optional
2324 2456 If True, split the output into an IPython SList. Otherwise, an
2325 2457 IPython LSString is returned. These are objects similar to normal
2326 2458 lists and strings, with a few convenience attributes for easier
2327 2459 manipulation of line-based output. You can use '?' on them for
2328 2460 details.
2329 2461 depth : int, optional
2330 2462 How many frames above the caller are the local variables which should
2331 2463 be expanded in the command string? The default (0) assumes that the
2332 2464 expansion variables are in the stack frame calling this function.
2333 2465 """
2334 2466 if cmd.rstrip().endswith('&'):
2335 2467 # this is *far* from a rigorous test
2336 2468 raise OSError("Background processes not supported.")
2337 2469 out = getoutput(self.var_expand(cmd, depth=depth+1))
2338 2470 if split:
2339 2471 out = SList(out.splitlines())
2340 2472 else:
2341 2473 out = LSString(out)
2342 2474 return out
2343 2475
2344 2476 #-------------------------------------------------------------------------
2345 2477 # Things related to aliases
2346 2478 #-------------------------------------------------------------------------
2347 2479
2348 2480 def init_alias(self):
2349 2481 self.alias_manager = AliasManager(shell=self, parent=self)
2350 2482 self.configurables.append(self.alias_manager)
2351 2483
2352 2484 #-------------------------------------------------------------------------
2353 2485 # Things related to extensions
2354 2486 #-------------------------------------------------------------------------
2355 2487
2356 2488 def init_extension_manager(self):
2357 2489 self.extension_manager = ExtensionManager(shell=self, parent=self)
2358 2490 self.configurables.append(self.extension_manager)
2359 2491
2360 2492 #-------------------------------------------------------------------------
2361 2493 # Things related to payloads
2362 2494 #-------------------------------------------------------------------------
2363 2495
2364 2496 def init_payload(self):
2365 2497 self.payload_manager = PayloadManager(parent=self)
2366 2498 self.configurables.append(self.payload_manager)
2367 2499
2368 2500 #-------------------------------------------------------------------------
2369 2501 # Things related to the prefilter
2370 2502 #-------------------------------------------------------------------------
2371 2503
2372 2504 def init_prefilter(self):
2373 2505 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2374 2506 self.configurables.append(self.prefilter_manager)
2375 2507 # Ultimately this will be refactored in the new interpreter code, but
2376 2508 # for now, we should expose the main prefilter method (there's legacy
2377 2509 # code out there that may rely on this).
2378 2510 self.prefilter = self.prefilter_manager.prefilter_lines
2379 2511
2380 2512 def auto_rewrite_input(self, cmd):
2381 2513 """Print to the screen the rewritten form of the user's command.
2382 2514
2383 2515 This shows visual feedback by rewriting input lines that cause
2384 2516 automatic calling to kick in, like::
2385 2517
2386 2518 /f x
2387 2519
2388 2520 into::
2389 2521
2390 2522 ------> f(x)
2391 2523
2392 2524 after the user's input prompt. This helps the user understand that the
2393 2525 input line was transformed automatically by IPython.
2394 2526 """
2395 2527 if not self.show_rewritten_input:
2396 2528 return
2397 2529
2398 2530 # This is overridden in TerminalInteractiveShell to use fancy prompts
2399 2531 print("------> " + cmd)
2400 2532
2401 2533 #-------------------------------------------------------------------------
2402 2534 # Things related to extracting values/expressions from kernel and user_ns
2403 2535 #-------------------------------------------------------------------------
2404 2536
2405 2537 def _user_obj_error(self):
2406 2538 """return simple exception dict
2407 2539
2408 2540 for use in user_expressions
2409 2541 """
2410 2542
2411 2543 etype, evalue, tb = self._get_exc_info()
2412 2544 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2413 2545
2414 2546 exc_info = {
2415 2547 u'status' : 'error',
2416 2548 u'traceback' : stb,
2417 2549 u'ename' : etype.__name__,
2418 2550 u'evalue' : py3compat.safe_unicode(evalue),
2419 2551 }
2420 2552
2421 2553 return exc_info
2422 2554
2423 2555 def _format_user_obj(self, obj):
2424 2556 """format a user object to display dict
2425 2557
2426 2558 for use in user_expressions
2427 2559 """
2428 2560
2429 2561 data, md = self.display_formatter.format(obj)
2430 2562 value = {
2431 2563 'status' : 'ok',
2432 2564 'data' : data,
2433 2565 'metadata' : md,
2434 2566 }
2435 2567 return value
2436 2568
2437 2569 def user_expressions(self, expressions):
2438 2570 """Evaluate a dict of expressions in the user's namespace.
2439 2571
2440 2572 Parameters
2441 2573 ----------
2442 2574 expressions : dict
2443 2575 A dict with string keys and string values. The expression values
2444 2576 should be valid Python expressions, each of which will be evaluated
2445 2577 in the user namespace.
2446 2578
2447 2579 Returns
2448 2580 -------
2449 2581 A dict, keyed like the input expressions dict, with the rich mime-typed
2450 2582 display_data of each value.
2451 2583 """
2452 2584 out = {}
2453 2585 user_ns = self.user_ns
2454 2586 global_ns = self.user_global_ns
2455 2587
2456 2588 for key, expr in expressions.items():
2457 2589 try:
2458 2590 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2459 2591 except:
2460 2592 value = self._user_obj_error()
2461 2593 out[key] = value
2462 2594 return out
2463 2595
2464 2596 #-------------------------------------------------------------------------
2465 2597 # Things related to the running of code
2466 2598 #-------------------------------------------------------------------------
2467 2599
2468 2600 def ex(self, cmd):
2469 2601 """Execute a normal python statement in user namespace."""
2470 2602 with self.builtin_trap:
2471 2603 exec(cmd, self.user_global_ns, self.user_ns)
2472 2604
2473 2605 def ev(self, expr):
2474 2606 """Evaluate python expression expr in user namespace.
2475 2607
2476 2608 Returns the result of evaluation
2477 2609 """
2478 2610 with self.builtin_trap:
2479 2611 return eval(expr, self.user_global_ns, self.user_ns)
2480 2612
2481 2613 def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False):
2482 2614 """A safe version of the builtin execfile().
2483 2615
2484 2616 This version will never throw an exception, but instead print
2485 2617 helpful error messages to the screen. This only works on pure
2486 2618 Python files with the .py extension.
2487 2619
2488 2620 Parameters
2489 2621 ----------
2490 2622 fname : string
2491 2623 The name of the file to be executed.
2492 2624 where : tuple
2493 2625 One or two namespaces, passed to execfile() as (globals,locals).
2494 2626 If only one is given, it is passed as both.
2495 2627 exit_ignore : bool (False)
2496 2628 If True, then silence SystemExit for non-zero status (it is always
2497 2629 silenced for zero status, as it is so common).
2498 2630 raise_exceptions : bool (False)
2499 2631 If True raise exceptions everywhere. Meant for testing.
2500 2632 shell_futures : bool (False)
2501 2633 If True, the code will share future statements with the interactive
2502 2634 shell. It will both be affected by previous __future__ imports, and
2503 2635 any __future__ imports in the code will affect the shell. If False,
2504 2636 __future__ imports are not shared in either direction.
2505 2637
2506 2638 """
2507 2639 fname = os.path.abspath(os.path.expanduser(fname))
2508 2640
2509 2641 # Make sure we can open the file
2510 2642 try:
2511 2643 with open(fname):
2512 2644 pass
2513 2645 except:
2514 2646 warn('Could not open file <%s> for safe execution.' % fname)
2515 2647 return
2516 2648
2517 2649 # Find things also in current directory. This is needed to mimic the
2518 2650 # behavior of running a script from the system command line, where
2519 2651 # Python inserts the script's directory into sys.path
2520 2652 dname = os.path.dirname(fname)
2521 2653
2522 2654 with prepended_to_syspath(dname), self.builtin_trap:
2523 2655 try:
2524 2656 glob, loc = (where + (None, ))[:2]
2525 2657 py3compat.execfile(
2526 2658 fname, glob, loc,
2527 2659 self.compile if shell_futures else None)
2528 2660 except SystemExit as status:
2529 2661 # If the call was made with 0 or None exit status (sys.exit(0)
2530 2662 # or sys.exit() ), don't bother showing a traceback, as both of
2531 2663 # these are considered normal by the OS:
2532 2664 # > python -c'import sys;sys.exit(0)'; echo $?
2533 2665 # 0
2534 2666 # > python -c'import sys;sys.exit()'; echo $?
2535 2667 # 0
2536 2668 # For other exit status, we show the exception unless
2537 2669 # explicitly silenced, but only in short form.
2538 2670 if status.code:
2539 2671 if raise_exceptions:
2540 2672 raise
2541 2673 if not exit_ignore:
2542 2674 self.showtraceback(exception_only=True)
2543 2675 except:
2544 2676 if raise_exceptions:
2545 2677 raise
2546 2678 # tb offset is 2 because we wrap execfile
2547 2679 self.showtraceback(tb_offset=2)
2548 2680
2549 2681 def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):
2550 2682 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2551 2683
2552 2684 Parameters
2553 2685 ----------
2554 2686 fname : str
2555 2687 The name of the file to execute. The filename must have a
2556 2688 .ipy or .ipynb extension.
2557 2689 shell_futures : bool (False)
2558 2690 If True, the code will share future statements with the interactive
2559 2691 shell. It will both be affected by previous __future__ imports, and
2560 2692 any __future__ imports in the code will affect the shell. If False,
2561 2693 __future__ imports are not shared in either direction.
2562 2694 raise_exceptions : bool (False)
2563 2695 If True raise exceptions everywhere. Meant for testing.
2564 2696 """
2565 2697 fname = os.path.abspath(os.path.expanduser(fname))
2566 2698
2567 2699 # Make sure we can open the file
2568 2700 try:
2569 2701 with open(fname):
2570 2702 pass
2571 2703 except:
2572 2704 warn('Could not open file <%s> for safe execution.' % fname)
2573 2705 return
2574 2706
2575 2707 # Find things also in current directory. This is needed to mimic the
2576 2708 # behavior of running a script from the system command line, where
2577 2709 # Python inserts the script's directory into sys.path
2578 2710 dname = os.path.dirname(fname)
2579 2711
2580 2712 def get_cells():
2581 2713 """generator for sequence of code blocks to run"""
2582 2714 if fname.endswith('.ipynb'):
2583 2715 from nbformat import read
2584 2716 nb = read(fname, as_version=4)
2585 2717 if not nb.cells:
2586 2718 return
2587 2719 for cell in nb.cells:
2588 2720 if cell.cell_type == 'code':
2589 2721 yield cell.source
2590 2722 else:
2591 2723 with open(fname) as f:
2592 2724 yield f.read()
2593 2725
2594 2726 with prepended_to_syspath(dname):
2595 2727 try:
2596 2728 for cell in get_cells():
2597 2729 result = self.run_cell(cell, silent=True, shell_futures=shell_futures)
2598 2730 if raise_exceptions:
2599 2731 result.raise_error()
2600 2732 elif not result.success:
2601 2733 break
2602 2734 except:
2603 2735 if raise_exceptions:
2604 2736 raise
2605 2737 self.showtraceback()
2606 2738 warn('Unknown failure executing file: <%s>' % fname)
2607 2739
2608 2740 def safe_run_module(self, mod_name, where):
2609 2741 """A safe version of runpy.run_module().
2610 2742
2611 2743 This version will never throw an exception, but instead print
2612 2744 helpful error messages to the screen.
2613 2745
2614 2746 `SystemExit` exceptions with status code 0 or None are ignored.
2615 2747
2616 2748 Parameters
2617 2749 ----------
2618 2750 mod_name : string
2619 2751 The name of the module to be executed.
2620 2752 where : dict
2621 2753 The globals namespace.
2622 2754 """
2623 2755 try:
2624 2756 try:
2625 2757 where.update(
2626 2758 runpy.run_module(str(mod_name), run_name="__main__",
2627 2759 alter_sys=True)
2628 2760 )
2629 2761 except SystemExit as status:
2630 2762 if status.code:
2631 2763 raise
2632 2764 except:
2633 2765 self.showtraceback()
2634 2766 warn('Unknown failure executing module: <%s>' % mod_name)
2635 2767
2636 2768 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2637 2769 """Run a complete IPython cell.
2638 2770
2639 2771 Parameters
2640 2772 ----------
2641 2773 raw_cell : str
2642 2774 The code (including IPython code such as %magic functions) to run.
2643 2775 store_history : bool
2644 2776 If True, the raw and translated cell will be stored in IPython's
2645 2777 history. For user code calling back into IPython's machinery, this
2646 2778 should be set to False.
2647 2779 silent : bool
2648 2780 If True, avoid side-effects, such as implicit displayhooks and
2649 2781 and logging. silent=True forces store_history=False.
2650 2782 shell_futures : bool
2651 2783 If True, the code will share future statements with the interactive
2652 2784 shell. It will both be affected by previous __future__ imports, and
2653 2785 any __future__ imports in the code will affect the shell. If False,
2654 2786 __future__ imports are not shared in either direction.
2655 2787
2656 2788 Returns
2657 2789 -------
2658 2790 result : :class:`ExecutionResult`
2659 2791 """
2660 2792 try:
2661 2793 result = self._run_cell(
2662 2794 raw_cell, store_history, silent, shell_futures)
2663 2795 finally:
2664 2796 self.events.trigger('post_execute')
2665 2797 if not silent:
2666 2798 self.events.trigger('post_run_cell', result)
2667 2799 return result
2668 2800
2669 2801 def _run_cell(self, raw_cell, store_history, silent, shell_futures):
2670 """Internal method to run a complete IPython cell.
2802 """Internal method to run a complete IPython cell."""
2803 return self.loop_runner(
2804 self.run_cell_async(
2805 raw_cell,
2806 store_history=store_history,
2807 silent=silent,
2808 shell_futures=shell_futures,
2809 )
2810 )
2811
2812 @asyncio.coroutine
2813 def run_cell_async(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2814 """Run a complete IPython cell asynchronously.
2671 2815
2672 2816 Parameters
2673 2817 ----------
2674 2818 raw_cell : str
2819 The code (including IPython code such as %magic functions) to run.
2675 2820 store_history : bool
2821 If True, the raw and translated cell will be stored in IPython's
2822 history. For user code calling back into IPython's machinery, this
2823 should be set to False.
2676 2824 silent : bool
2825 If True, avoid side-effects, such as implicit displayhooks and
2826 and logging. silent=True forces store_history=False.
2677 2827 shell_futures : bool
2828 If True, the code will share future statements with the interactive
2829 shell. It will both be affected by previous __future__ imports, and
2830 any __future__ imports in the code will affect the shell. If False,
2831 __future__ imports are not shared in either direction.
2678 2832
2679 2833 Returns
2680 2834 -------
2681 2835 result : :class:`ExecutionResult`
2682 2836 """
2683 2837 info = ExecutionInfo(
2684 2838 raw_cell, store_history, silent, shell_futures)
2685 2839 result = ExecutionResult(info)
2686 2840
2687 2841 if (not raw_cell) or raw_cell.isspace():
2688 2842 self.last_execution_succeeded = True
2689 2843 self.last_execution_result = result
2690 2844 return result
2691 2845
2692 2846 if silent:
2693 2847 store_history = False
2694 2848
2695 2849 if store_history:
2696 2850 result.execution_count = self.execution_count
2697 2851
2698 2852 def error_before_exec(value):
2699 2853 if store_history:
2700 2854 self.execution_count += 1
2701 2855 result.error_before_exec = value
2702 2856 self.last_execution_succeeded = False
2703 2857 self.last_execution_result = result
2704 2858 return result
2705 2859
2706 2860 self.events.trigger('pre_execute')
2707 2861 if not silent:
2708 2862 self.events.trigger('pre_run_cell', info)
2709 2863
2710 2864 # If any of our input transformation (input_transformer_manager or
2711 2865 # prefilter_manager) raises an exception, we store it in this variable
2712 2866 # so that we can display the error after logging the input and storing
2713 2867 # it in the history.
2714 2868 preprocessing_exc_tuple = None
2715 2869 try:
2716 2870 # Static input transformations
2717 2871 cell = self.input_transformer_manager.transform_cell(raw_cell)
2718 2872 except SyntaxError:
2719 2873 preprocessing_exc_tuple = sys.exc_info()
2720 2874 cell = raw_cell # cell has to exist so it can be stored/logged
2721 2875 else:
2722 2876 if len(cell.splitlines()) == 1:
2723 2877 # Dynamic transformations - only applied for single line commands
2724 2878 with self.builtin_trap:
2725 2879 try:
2726 2880 # use prefilter_lines to handle trailing newlines
2727 2881 # restore trailing newline for ast.parse
2728 2882 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2729 2883 except Exception:
2730 2884 # don't allow prefilter errors to crash IPython
2731 2885 preprocessing_exc_tuple = sys.exc_info()
2732 2886
2733 2887 # Store raw and processed history
2734 2888 if store_history:
2735 2889 self.history_manager.store_inputs(self.execution_count,
2736 2890 cell, raw_cell)
2737 2891 if not silent:
2738 2892 self.logger.log(cell, raw_cell)
2739 2893
2740 2894 # Display the exception if input processing failed.
2741 2895 if preprocessing_exc_tuple is not None:
2742 2896 self.showtraceback(preprocessing_exc_tuple)
2743 2897 if store_history:
2744 2898 self.execution_count += 1
2745 2899 return error_before_exec(preprocessing_exc_tuple[2])
2746 2900
2747 2901 # Our own compiler remembers the __future__ environment. If we want to
2748 2902 # run code with a separate __future__ environment, use the default
2749 2903 # compiler
2750 2904 compiler = self.compile if shell_futures else CachingCompiler()
2751 2905
2906 _run_async = False
2907
2752 2908 with self.builtin_trap:
2753 2909 cell_name = self.compile.cache(cell, self.execution_count)
2754 2910
2755 2911 with self.display_trap:
2756 2912 # Compile to bytecode
2757 2913 try:
2758 code_ast = compiler.ast_parse(cell, filename=cell_name)
2914 if self.autoawait and _should_be_async(cell):
2915 # the code AST below will not be user code: we wrap it
2916 # in an `async def`. This will likely make some AST
2917 # transformer below miss some transform opportunity and
2918 # introduce a small coupling to run_code (in which we
2919 # bake some assumptions of what _ast_asyncify returns.
2920 # they are ways around (like grafting part of the ast
2921 # later:
2922 # - Here, return code_ast.body[0].body[1:-1], as well
2923 # as last expression in return statement which is
2924 # the user code part.
2925 # - Let it go through the AST transformers, and graft
2926 # - it back after the AST transform
2927 # But that seem unreasonable, at least while we
2928 # do not need it.
2929 code_ast = _ast_asyncify(cell, 'async-def-wrapper')
2930 _run_async = True
2931 else:
2932 code_ast = compiler.ast_parse(cell, filename=cell_name)
2759 2933 except self.custom_exceptions as e:
2760 2934 etype, value, tb = sys.exc_info()
2761 2935 self.CustomTB(etype, value, tb)
2762 2936 return error_before_exec(e)
2763 2937 except IndentationError as e:
2764 2938 self.showindentationerror()
2765 2939 return error_before_exec(e)
2766 2940 except (OverflowError, SyntaxError, ValueError, TypeError,
2767 2941 MemoryError) as e:
2768 2942 self.showsyntaxerror()
2769 2943 return error_before_exec(e)
2770 2944
2771 2945 # Apply AST transformations
2772 2946 try:
2773 2947 code_ast = self.transform_ast(code_ast)
2774 2948 except InputRejected as e:
2775 2949 self.showtraceback()
2776 2950 return error_before_exec(e)
2777 2951
2778 2952 # Give the displayhook a reference to our ExecutionResult so it
2779 2953 # can fill in the output value.
2780 2954 self.displayhook.exec_result = result
2781 2955
2782 2956 # Execute the user code
2783 interactivity = 'none' if silent else self.ast_node_interactivity
2784 has_raised = self.run_ast_nodes(code_ast.body, cell_name,
2785 interactivity=interactivity, compiler=compiler, result=result)
2957 interactivity = "none" if silent else self.ast_node_interactivity
2958 if _run_async:
2959 interactivity = 'async'
2960 has_raised = yield from self.run_ast_nodes(code_ast.body, cell_name,
2961 interactivity=interactivity, compiler=compiler, result=result)
2786 2962
2787 2963 self.last_execution_succeeded = not has_raised
2788 2964 self.last_execution_result = result
2789 2965
2790 2966 # Reset this so later displayed values do not modify the
2791 2967 # ExecutionResult
2792 2968 self.displayhook.exec_result = None
2793 2969
2794 2970 if store_history:
2795 2971 # Write output to the database. Does nothing unless
2796 2972 # history output logging is enabled.
2797 2973 self.history_manager.store_output(self.execution_count)
2798 2974 # Each cell is a *single* input, regardless of how many lines it has
2799 2975 self.execution_count += 1
2800 2976
2801 2977 return result
2802 2978
2803 2979 def transform_ast(self, node):
2804 2980 """Apply the AST transformations from self.ast_transformers
2805 2981
2806 2982 Parameters
2807 2983 ----------
2808 2984 node : ast.Node
2809 2985 The root node to be transformed. Typically called with the ast.Module
2810 2986 produced by parsing user input.
2811 2987
2812 2988 Returns
2813 2989 -------
2814 2990 An ast.Node corresponding to the node it was called with. Note that it
2815 2991 may also modify the passed object, so don't rely on references to the
2816 2992 original AST.
2817 2993 """
2818 2994 for transformer in self.ast_transformers:
2819 2995 try:
2820 2996 node = transformer.visit(node)
2821 2997 except InputRejected:
2822 2998 # User-supplied AST transformers can reject an input by raising
2823 2999 # an InputRejected. Short-circuit in this case so that we
2824 3000 # don't unregister the transform.
2825 3001 raise
2826 3002 except Exception:
2827 3003 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2828 3004 self.ast_transformers.remove(transformer)
2829
3005
2830 3006 if self.ast_transformers:
2831 3007 ast.fix_missing_locations(node)
2832 3008 return node
2833
2834 3009
3010 @asyncio.coroutine
2835 3011 def run_ast_nodes(self, nodelist:ListType[AST], cell_name:str, interactivity='last_expr',
2836 3012 compiler=compile, result=None):
2837 3013 """Run a sequence of AST nodes. The execution mode depends on the
2838 3014 interactivity parameter.
2839 3015
2840 3016 Parameters
2841 3017 ----------
2842 3018 nodelist : list
2843 3019 A sequence of AST nodes to run.
2844 3020 cell_name : str
2845 3021 Will be passed to the compiler as the filename of the cell. Typically
2846 3022 the value returned by ip.compile.cache(cell).
2847 3023 interactivity : str
2848 3024 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none',
2849 3025 specifying which nodes should be run interactively (displaying output
2850 3026 from expressions). 'last_expr' will run the last node interactively
2851 3027 only if it is an expression (i.e. expressions in loops or other blocks
2852 3028 are not displayed) 'last_expr_or_assign' will run the last expression
2853 3029 or the last assignment. Other values for this parameter will raise a
2854 3030 ValueError.
3031
3032 Experimental value: 'async' Will try to run top level interactive
3033 async/await code in default runner, this will not respect the
3034 interactivty setting and will only run the last node if it is an
3035 expression.
3036
2855 3037 compiler : callable
2856 3038 A function with the same interface as the built-in compile(), to turn
2857 3039 the AST nodes into code objects. Default is the built-in compile().
2858 3040 result : ExecutionResult, optional
2859 3041 An object to store exceptions that occur during execution.
2860 3042
2861 3043 Returns
2862 3044 -------
2863 3045 True if an exception occurred while running code, False if it finished
2864 3046 running.
2865 3047 """
2866 3048 if not nodelist:
2867 3049 return
2868 3050 if interactivity == 'last_expr_or_assign':
2869 3051 if isinstance(nodelist[-1], _assign_nodes):
2870 3052 asg = nodelist[-1]
2871 3053 if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
2872 3054 target = asg.targets[0]
2873 3055 elif isinstance(asg, _single_targets_nodes):
2874 3056 target = asg.target
2875 3057 else:
2876 3058 target = None
2877 3059 if isinstance(target, ast.Name):
2878 3060 nnode = ast.Expr(ast.Name(target.id, ast.Load()))
2879 3061 ast.fix_missing_locations(nnode)
2880 3062 nodelist.append(nnode)
2881 3063 interactivity = 'last_expr'
2882 3064
3065 _async = False
2883 3066 if interactivity == 'last_expr':
2884 3067 if isinstance(nodelist[-1], ast.Expr):
2885 3068 interactivity = "last"
2886 3069 else:
2887 3070 interactivity = "none"
2888 3071
2889 3072 if interactivity == 'none':
2890 3073 to_run_exec, to_run_interactive = nodelist, []
2891 3074 elif interactivity == 'last':
2892 3075 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2893 3076 elif interactivity == 'all':
2894 3077 to_run_exec, to_run_interactive = [], nodelist
3078 elif interactivity == 'async':
3079 _async = True
2895 3080 else:
2896 3081 raise ValueError("Interactivity was %r" % interactivity)
2897 3082 try:
2898 for i, node in enumerate(to_run_exec):
2899 mod = ast.Module([node])
2900 code = compiler(mod, cell_name, "exec")
2901 if self.run_code(code, result):
2902 return True
2903
2904 for i, node in enumerate(to_run_interactive):
2905 mod = ast.Interactive([node])
2906 code = compiler(mod, cell_name, "single")
2907 if self.run_code(code, result):
3083 if _async:
3084 # If interactivity is async the semantics of run_code are
3085 # completely different Skip usual machinery.
3086 mod = ast.Module(nodelist)
3087 async_wrapper_code = compiler(mod, 'cell_name', 'exec')
3088 exec(async_wrapper_code, self.user_global_ns, self.user_ns)
3089 async_code = removed_co_newlocals(self.user_ns.pop('async-def-wrapper')).__code__
3090 if (yield from self.run_code(async_code, result, async_=True)):
2908 3091 return True
3092 else:
3093 for i, node in enumerate(to_run_exec):
3094 mod = ast.Module([node])
3095 code = compiler(mod, cell_name, "exec")
3096 if (yield from self.run_code(code, result)):
3097 return True
3098
3099 for i, node in enumerate(to_run_interactive):
3100 mod = ast.Interactive([node])
3101 code = compiler(mod, cell_name, "single")
3102 if (yield from self.run_code(code, result)):
3103 return True
2909 3104
2910 3105 # Flush softspace
2911 3106 if softspace(sys.stdout, 0):
2912 3107 print()
2913 3108
2914 3109 except:
2915 3110 # It's possible to have exceptions raised here, typically by
2916 3111 # compilation of odd code (such as a naked 'return' outside a
2917 3112 # function) that did parse but isn't valid. Typically the exception
2918 3113 # is a SyntaxError, but it's safest just to catch anything and show
2919 3114 # the user a traceback.
2920 3115
2921 3116 # We do only one try/except outside the loop to minimize the impact
2922 3117 # on runtime, and also because if any node in the node list is
2923 3118 # broken, we should stop execution completely.
2924 3119 if result:
2925 3120 result.error_before_exec = sys.exc_info()[1]
2926 3121 self.showtraceback()
2927 3122 return True
2928 3123
2929 3124 return False
2930 3125
2931 def run_code(self, code_obj, result=None):
3126 def _async_exec(self, code_obj: types.CodeType, user_ns: dict):
3127 """
3128 Evaluate an asynchronous code object using a code runner
3129
3130 Fake asynchronous execution of code_object in a namespace via a proxy namespace.
3131
3132 Returns coroutine object, which can be executed via async loop runner
3133
3134 WARNING: The semantics of `async_exec` are quite different from `exec`,
3135 in particular you can only pass a single namespace. It also return a
3136 handle to the value of the last things returned by code_object.
3137 """
3138
3139 return eval(code_obj, user_ns)
3140
3141 @asyncio.coroutine
3142 def run_code(self, code_obj, result=None, *, async_=False):
2932 3143 """Execute a code object.
2933 3144
2934 3145 When an exception occurs, self.showtraceback() is called to display a
2935 3146 traceback.
2936 3147
2937 3148 Parameters
2938 3149 ----------
2939 3150 code_obj : code object
2940 3151 A compiled code object, to be executed
2941 3152 result : ExecutionResult, optional
2942 3153 An object to store exceptions that occur during execution.
3154 async_ : Bool (Experimental)
3155 Attempt to run top-level asynchronous code in a default loop.
2943 3156
2944 3157 Returns
2945 3158 -------
2946 3159 False : successful execution.
2947 3160 True : an error occurred.
2948 3161 """
2949 3162 # Set our own excepthook in case the user code tries to call it
2950 3163 # directly, so that the IPython crash handler doesn't get triggered
2951 3164 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2952 3165
2953 3166 # we save the original sys.excepthook in the instance, in case config
2954 3167 # code (such as magics) needs access to it.
2955 3168 self.sys_excepthook = old_excepthook
2956 3169 outflag = True # happens in more places, so it's easier as default
2957 3170 try:
2958 3171 try:
2959 3172 self.hooks.pre_run_code_hook()
2960 #rprint('Running code', repr(code_obj)) # dbg
2961 exec(code_obj, self.user_global_ns, self.user_ns)
3173 if async_:
3174 last_expr = (yield from self._async_exec(code_obj, self.user_ns))
3175 code = compile('last_expr', 'fake', "single")
3176 exec(code, {'last_expr': last_expr})
3177 else:
3178 exec(code_obj, self.user_global_ns, self.user_ns)
2962 3179 finally:
2963 3180 # Reset our crash handler in place
2964 3181 sys.excepthook = old_excepthook
2965 3182 except SystemExit as e:
2966 3183 if result is not None:
2967 3184 result.error_in_exec = e
2968 3185 self.showtraceback(exception_only=True)
2969 3186 warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
2970 3187 except self.custom_exceptions:
2971 3188 etype, value, tb = sys.exc_info()
2972 3189 if result is not None:
2973 3190 result.error_in_exec = value
2974 3191 self.CustomTB(etype, value, tb)
2975 3192 except:
2976 3193 if result is not None:
2977 3194 result.error_in_exec = sys.exc_info()[1]
2978 3195 self.showtraceback(running_compiled_code=True)
2979 3196 else:
2980 3197 outflag = False
2981 3198 return outflag
2982 3199
2983 3200 # For backwards compatibility
2984 3201 runcode = run_code
2985 3202
2986 3203 def check_complete(self, code):
2987 3204 """Return whether a block of code is ready to execute, or should be continued
2988 3205
2989 3206 Parameters
2990 3207 ----------
2991 3208 source : string
2992 3209 Python input code, which can be multiline.
2993 3210
2994 3211 Returns
2995 3212 -------
2996 3213 status : str
2997 3214 One of 'complete', 'incomplete', or 'invalid' if source is not a
2998 3215 prefix of valid code.
2999 3216 indent : str
3000 3217 When status is 'incomplete', this is some whitespace to insert on
3001 3218 the next line of the prompt.
3002 3219 """
3003 3220 status, nspaces = self.input_splitter.check_complete(code)
3004 3221 return status, ' ' * (nspaces or 0)
3005 3222
3006 3223 #-------------------------------------------------------------------------
3007 3224 # Things related to GUI support and pylab
3008 3225 #-------------------------------------------------------------------------
3009 3226
3010 3227 active_eventloop = None
3011 3228
3012 3229 def enable_gui(self, gui=None):
3013 3230 raise NotImplementedError('Implement enable_gui in a subclass')
3014 3231
3015 3232 def enable_matplotlib(self, gui=None):
3016 3233 """Enable interactive matplotlib and inline figure support.
3017 3234
3018 3235 This takes the following steps:
3019 3236
3020 3237 1. select the appropriate eventloop and matplotlib backend
3021 3238 2. set up matplotlib for interactive use with that backend
3022 3239 3. configure formatters for inline figure display
3023 3240 4. enable the selected gui eventloop
3024 3241
3025 3242 Parameters
3026 3243 ----------
3027 3244 gui : optional, string
3028 3245 If given, dictates the choice of matplotlib GUI backend to use
3029 3246 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3030 3247 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3031 3248 matplotlib (as dictated by the matplotlib build-time options plus the
3032 3249 user's matplotlibrc configuration file). Note that not all backends
3033 3250 make sense in all contexts, for example a terminal ipython can't
3034 3251 display figures inline.
3035 3252 """
3036 3253 from IPython.core import pylabtools as pt
3037 3254 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3038 3255
3039 3256 if gui != 'inline':
3040 3257 # If we have our first gui selection, store it
3041 3258 if self.pylab_gui_select is None:
3042 3259 self.pylab_gui_select = gui
3043 3260 # Otherwise if they are different
3044 3261 elif gui != self.pylab_gui_select:
3045 3262 print('Warning: Cannot change to a different GUI toolkit: %s.'
3046 3263 ' Using %s instead.' % (gui, self.pylab_gui_select))
3047 3264 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3048 3265
3049 3266 pt.activate_matplotlib(backend)
3050 3267 pt.configure_inline_support(self, backend)
3051 3268
3052 3269 # Now we must activate the gui pylab wants to use, and fix %run to take
3053 3270 # plot updates into account
3054 3271 self.enable_gui(gui)
3055 3272 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3056 3273 pt.mpl_runner(self.safe_execfile)
3057 3274
3058 3275 return gui, backend
3059 3276
3060 3277 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3061 3278 """Activate pylab support at runtime.
3062 3279
3063 3280 This turns on support for matplotlib, preloads into the interactive
3064 3281 namespace all of numpy and pylab, and configures IPython to correctly
3065 3282 interact with the GUI event loop. The GUI backend to be used can be
3066 3283 optionally selected with the optional ``gui`` argument.
3067 3284
3068 3285 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3069 3286
3070 3287 Parameters
3071 3288 ----------
3072 3289 gui : optional, string
3073 3290 If given, dictates the choice of matplotlib GUI backend to use
3074 3291 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3075 3292 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3076 3293 matplotlib (as dictated by the matplotlib build-time options plus the
3077 3294 user's matplotlibrc configuration file). Note that not all backends
3078 3295 make sense in all contexts, for example a terminal ipython can't
3079 3296 display figures inline.
3080 3297 import_all : optional, bool, default: True
3081 3298 Whether to do `from numpy import *` and `from pylab import *`
3082 3299 in addition to module imports.
3083 3300 welcome_message : deprecated
3084 3301 This argument is ignored, no welcome message will be displayed.
3085 3302 """
3086 3303 from IPython.core.pylabtools import import_pylab
3087 3304
3088 3305 gui, backend = self.enable_matplotlib(gui)
3089 3306
3090 3307 # We want to prevent the loading of pylab to pollute the user's
3091 3308 # namespace as shown by the %who* magics, so we execute the activation
3092 3309 # code in an empty namespace, and we update *both* user_ns and
3093 3310 # user_ns_hidden with this information.
3094 3311 ns = {}
3095 3312 import_pylab(ns, import_all)
3096 3313 # warn about clobbered names
3097 3314 ignored = {"__builtins__"}
3098 3315 both = set(ns).intersection(self.user_ns).difference(ignored)
3099 3316 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3100 3317 self.user_ns.update(ns)
3101 3318 self.user_ns_hidden.update(ns)
3102 3319 return gui, backend, clobbered
3103 3320
3104 3321 #-------------------------------------------------------------------------
3105 3322 # Utilities
3106 3323 #-------------------------------------------------------------------------
3107 3324
3108 3325 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3109 3326 """Expand python variables in a string.
3110 3327
3111 3328 The depth argument indicates how many frames above the caller should
3112 3329 be walked to look for the local namespace where to expand variables.
3113 3330
3114 3331 The global namespace for expansion is always the user's interactive
3115 3332 namespace.
3116 3333 """
3117 3334 ns = self.user_ns.copy()
3118 3335 try:
3119 3336 frame = sys._getframe(depth+1)
3120 3337 except ValueError:
3121 3338 # This is thrown if there aren't that many frames on the stack,
3122 3339 # e.g. if a script called run_line_magic() directly.
3123 3340 pass
3124 3341 else:
3125 3342 ns.update(frame.f_locals)
3126 3343
3127 3344 try:
3128 3345 # We have to use .vformat() here, because 'self' is a valid and common
3129 3346 # name, and expanding **ns for .format() would make it collide with
3130 3347 # the 'self' argument of the method.
3131 3348 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3132 3349 except Exception:
3133 3350 # if formatter couldn't format, just let it go untransformed
3134 3351 pass
3135 3352 return cmd
3136 3353
3137 3354 def mktempfile(self, data=None, prefix='ipython_edit_'):
3138 3355 """Make a new tempfile and return its filename.
3139 3356
3140 3357 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3141 3358 but it registers the created filename internally so ipython cleans it up
3142 3359 at exit time.
3143 3360
3144 3361 Optional inputs:
3145 3362
3146 3363 - data(None): if data is given, it gets written out to the temp file
3147 3364 immediately, and the file is closed again."""
3148 3365
3149 3366 dirname = tempfile.mkdtemp(prefix=prefix)
3150 3367 self.tempdirs.append(dirname)
3151 3368
3152 3369 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3153 3370 os.close(handle) # On Windows, there can only be one open handle on a file
3154 3371 self.tempfiles.append(filename)
3155 3372
3156 3373 if data:
3157 3374 tmp_file = open(filename,'w')
3158 3375 tmp_file.write(data)
3159 3376 tmp_file.close()
3160 3377 return filename
3161 3378
3162 3379 @undoc
3163 3380 def write(self,data):
3164 3381 """DEPRECATED: Write a string to the default output"""
3165 3382 warn('InteractiveShell.write() is deprecated, use sys.stdout instead',
3166 3383 DeprecationWarning, stacklevel=2)
3167 3384 sys.stdout.write(data)
3168 3385
3169 3386 @undoc
3170 3387 def write_err(self,data):
3171 3388 """DEPRECATED: Write a string to the default error output"""
3172 3389 warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead',
3173 3390 DeprecationWarning, stacklevel=2)
3174 3391 sys.stderr.write(data)
3175 3392
3176 3393 def ask_yes_no(self, prompt, default=None, interrupt=None):
3177 3394 if self.quiet:
3178 3395 return True
3179 3396 return ask_yes_no(prompt,default,interrupt)
3180 3397
3181 3398 def show_usage(self):
3182 3399 """Show a usage message"""
3183 3400 page.page(IPython.core.usage.interactive_usage)
3184 3401
3185 3402 def extract_input_lines(self, range_str, raw=False):
3186 3403 """Return as a string a set of input history slices.
3187 3404
3188 3405 Parameters
3189 3406 ----------
3190 3407 range_str : string
3191 3408 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3192 3409 since this function is for use by magic functions which get their
3193 3410 arguments as strings. The number before the / is the session
3194 3411 number: ~n goes n back from the current session.
3195 3412
3196 3413 raw : bool, optional
3197 3414 By default, the processed input is used. If this is true, the raw
3198 3415 input history is used instead.
3199 3416
3200 3417 Notes
3201 3418 -----
3202 3419
3203 3420 Slices can be described with two notations:
3204 3421
3205 3422 * ``N:M`` -> standard python form, means including items N...(M-1).
3206 3423 * ``N-M`` -> include items N..M (closed endpoint).
3207 3424 """
3208 3425 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3209 3426 return "\n".join(x for _, _, x in lines)
3210 3427
3211 3428 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3212 3429 """Get a code string from history, file, url, or a string or macro.
3213 3430
3214 3431 This is mainly used by magic functions.
3215 3432
3216 3433 Parameters
3217 3434 ----------
3218 3435
3219 3436 target : str
3220 3437
3221 3438 A string specifying code to retrieve. This will be tried respectively
3222 3439 as: ranges of input history (see %history for syntax), url,
3223 3440 corresponding .py file, filename, or an expression evaluating to a
3224 3441 string or Macro in the user namespace.
3225 3442
3226 3443 raw : bool
3227 3444 If true (default), retrieve raw history. Has no effect on the other
3228 3445 retrieval mechanisms.
3229 3446
3230 3447 py_only : bool (default False)
3231 3448 Only try to fetch python code, do not try alternative methods to decode file
3232 3449 if unicode fails.
3233 3450
3234 3451 Returns
3235 3452 -------
3236 3453 A string of code.
3237 3454
3238 3455 ValueError is raised if nothing is found, and TypeError if it evaluates
3239 3456 to an object of another type. In each case, .args[0] is a printable
3240 3457 message.
3241 3458 """
3242 3459 code = self.extract_input_lines(target, raw=raw) # Grab history
3243 3460 if code:
3244 3461 return code
3245 3462 try:
3246 3463 if target.startswith(('http://', 'https://')):
3247 3464 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3248 3465 except UnicodeDecodeError:
3249 3466 if not py_only :
3250 3467 # Deferred import
3251 3468 from urllib.request import urlopen
3252 3469 response = urlopen(target)
3253 3470 return response.read().decode('latin1')
3254 3471 raise ValueError(("'%s' seem to be unreadable.") % target)
3255 3472
3256 3473 potential_target = [target]
3257 3474 try :
3258 3475 potential_target.insert(0,get_py_filename(target))
3259 3476 except IOError:
3260 3477 pass
3261 3478
3262 3479 for tgt in potential_target :
3263 3480 if os.path.isfile(tgt): # Read file
3264 3481 try :
3265 3482 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3266 3483 except UnicodeDecodeError :
3267 3484 if not py_only :
3268 3485 with io_open(tgt,'r', encoding='latin1') as f :
3269 3486 return f.read()
3270 3487 raise ValueError(("'%s' seem to be unreadable.") % target)
3271 3488 elif os.path.isdir(os.path.expanduser(tgt)):
3272 3489 raise ValueError("'%s' is a directory, not a regular file." % target)
3273 3490
3274 3491 if search_ns:
3275 3492 # Inspect namespace to load object source
3276 3493 object_info = self.object_inspect(target, detail_level=1)
3277 3494 if object_info['found'] and object_info['source']:
3278 3495 return object_info['source']
3279 3496
3280 3497 try: # User namespace
3281 3498 codeobj = eval(target, self.user_ns)
3282 3499 except Exception:
3283 3500 raise ValueError(("'%s' was not found in history, as a file, url, "
3284 3501 "nor in the user namespace.") % target)
3285 3502
3286 3503 if isinstance(codeobj, str):
3287 3504 return codeobj
3288 3505 elif isinstance(codeobj, Macro):
3289 3506 return codeobj.value
3290 3507
3291 3508 raise TypeError("%s is neither a string nor a macro." % target,
3292 3509 codeobj)
3293 3510
3294 3511 #-------------------------------------------------------------------------
3295 3512 # Things related to IPython exiting
3296 3513 #-------------------------------------------------------------------------
3297 3514 def atexit_operations(self):
3298 3515 """This will be executed at the time of exit.
3299 3516
3300 3517 Cleanup operations and saving of persistent data that is done
3301 3518 unconditionally by IPython should be performed here.
3302 3519
3303 3520 For things that may depend on startup flags or platform specifics (such
3304 3521 as having readline or not), register a separate atexit function in the
3305 3522 code that has the appropriate information, rather than trying to
3306 3523 clutter
3307 3524 """
3308 3525 # Close the history session (this stores the end time and line count)
3309 3526 # this must be *before* the tempfile cleanup, in case of temporary
3310 3527 # history db
3311 3528 self.history_manager.end_session()
3312 3529
3313 3530 # Cleanup all tempfiles and folders left around
3314 3531 for tfile in self.tempfiles:
3315 3532 try:
3316 3533 os.unlink(tfile)
3317 3534 except OSError:
3318 3535 pass
3319 3536
3320 3537 for tdir in self.tempdirs:
3321 3538 try:
3322 3539 os.rmdir(tdir)
3323 3540 except OSError:
3324 3541 pass
3325 3542
3326 3543 # Clear all user namespaces to release all references cleanly.
3327 3544 self.reset(new_session=False)
3328 3545
3329 3546 # Run user hooks
3330 3547 self.hooks.shutdown_hook()
3331 3548
3332 3549 def cleanup(self):
3333 3550 self.restore_sys_module_state()
3334 3551
3335 3552
3336 3553 # Overridden in terminal subclass to change prompts
3337 3554 def switch_doctest_mode(self, mode):
3338 3555 pass
3339 3556
3340 3557
3341 3558 class InteractiveShellABC(metaclass=abc.ABCMeta):
3342 3559 """An abstract base class for InteractiveShell."""
3343 3560
3344 3561 InteractiveShellABC.register(InteractiveShell)
@@ -1,599 +1,658
1 1 """Implementation of basic magic functions."""
2 2
3 3
4 4 import argparse
5 import textwrap
5 from logging import error
6 6 import io
7 import sys
8 7 from pprint import pformat
8 import textwrap
9 import sys
10 from warnings import warn
9 11
12 from traitlets.utils.importstring import import_item
10 13 from IPython.core import magic_arguments, page
11 14 from IPython.core.error import UsageError
12 15 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
13 16 from IPython.utils.text import format_screen, dedent, indent
14 17 from IPython.testing.skipdoctest import skip_doctest
15 18 from IPython.utils.ipstruct import Struct
16 from warnings import warn
17 from logging import error
18 19
19 20
20 21 class MagicsDisplay(object):
21 22 def __init__(self, magics_manager, ignore=None):
22 23 self.ignore = ignore if ignore else []
23 24 self.magics_manager = magics_manager
24 25
25 26 def _lsmagic(self):
26 27 """The main implementation of the %lsmagic"""
27 28 mesc = magic_escapes['line']
28 29 cesc = magic_escapes['cell']
29 30 mman = self.magics_manager
30 31 magics = mman.lsmagic()
31 32 out = ['Available line magics:',
32 33 mesc + (' '+mesc).join(sorted([m for m,v in magics['line'].items() if (v not in self.ignore)])),
33 34 '',
34 35 'Available cell magics:',
35 36 cesc + (' '+cesc).join(sorted([m for m,v in magics['cell'].items() if (v not in self.ignore)])),
36 37 '',
37 38 mman.auto_status()]
38 39 return '\n'.join(out)
39 40
40 41 def _repr_pretty_(self, p, cycle):
41 42 p.text(self._lsmagic())
42 43
43 44 def __str__(self):
44 45 return self._lsmagic()
45 46
46 47 def _jsonable(self):
47 48 """turn magics dict into jsonable dict of the same structure
48 49
49 50 replaces object instances with their class names as strings
50 51 """
51 52 magic_dict = {}
52 53 mman = self.magics_manager
53 54 magics = mman.lsmagic()
54 55 for key, subdict in magics.items():
55 56 d = {}
56 57 magic_dict[key] = d
57 58 for name, obj in subdict.items():
58 59 try:
59 60 classname = obj.__self__.__class__.__name__
60 61 except AttributeError:
61 62 classname = 'Other'
62 63
63 64 d[name] = classname
64 65 return magic_dict
65 66
66 67 def _repr_json_(self):
67 68 return self._jsonable()
68 69
69 70
70 71 @magics_class
71 72 class BasicMagics(Magics):
72 73 """Magics that provide central IPython functionality.
73 74
74 75 These are various magics that don't fit into specific categories but that
75 76 are all part of the base 'IPython experience'."""
76 77
77 78 @magic_arguments.magic_arguments()
78 79 @magic_arguments.argument(
79 80 '-l', '--line', action='store_true',
80 81 help="""Create a line magic alias."""
81 82 )
82 83 @magic_arguments.argument(
83 84 '-c', '--cell', action='store_true',
84 85 help="""Create a cell magic alias."""
85 86 )
86 87 @magic_arguments.argument(
87 88 'name',
88 89 help="""Name of the magic to be created."""
89 90 )
90 91 @magic_arguments.argument(
91 92 'target',
92 93 help="""Name of the existing line or cell magic."""
93 94 )
94 95 @magic_arguments.argument(
95 96 '-p', '--params', default=None,
96 97 help="""Parameters passed to the magic function."""
97 98 )
98 99 @line_magic
99 100 def alias_magic(self, line=''):
100 101 """Create an alias for an existing line or cell magic.
101 102
102 103 Examples
103 104 --------
104 105 ::
105 106
106 107 In [1]: %alias_magic t timeit
107 108 Created `%t` as an alias for `%timeit`.
108 109 Created `%%t` as an alias for `%%timeit`.
109 110
110 111 In [2]: %t -n1 pass
111 112 1 loops, best of 3: 954 ns per loop
112 113
113 114 In [3]: %%t -n1
114 115 ...: pass
115 116 ...:
116 117 1 loops, best of 3: 954 ns per loop
117 118
118 119 In [4]: %alias_magic --cell whereami pwd
119 120 UsageError: Cell magic function `%%pwd` not found.
120 121 In [5]: %alias_magic --line whereami pwd
121 122 Created `%whereami` as an alias for `%pwd`.
122 123
123 124 In [6]: %whereami
124 125 Out[6]: u'/home/testuser'
125 126
126 127 In [7]: %alias_magic h history -p "-l 30" --line
127 128 Created `%h` as an alias for `%history -l 30`.
128 129 """
129 130
130 131 args = magic_arguments.parse_argstring(self.alias_magic, line)
131 132 shell = self.shell
132 133 mman = self.shell.magics_manager
133 134 escs = ''.join(magic_escapes.values())
134 135
135 136 target = args.target.lstrip(escs)
136 137 name = args.name.lstrip(escs)
137 138
138 139 params = args.params
139 140 if (params and
140 141 ((params.startswith('"') and params.endswith('"'))
141 142 or (params.startswith("'") and params.endswith("'")))):
142 143 params = params[1:-1]
143 144
144 145 # Find the requested magics.
145 146 m_line = shell.find_magic(target, 'line')
146 147 m_cell = shell.find_magic(target, 'cell')
147 148 if args.line and m_line is None:
148 149 raise UsageError('Line magic function `%s%s` not found.' %
149 150 (magic_escapes['line'], target))
150 151 if args.cell and m_cell is None:
151 152 raise UsageError('Cell magic function `%s%s` not found.' %
152 153 (magic_escapes['cell'], target))
153 154
154 155 # If --line and --cell are not specified, default to the ones
155 156 # that are available.
156 157 if not args.line and not args.cell:
157 158 if not m_line and not m_cell:
158 159 raise UsageError(
159 160 'No line or cell magic with name `%s` found.' % target
160 161 )
161 162 args.line = bool(m_line)
162 163 args.cell = bool(m_cell)
163 164
164 165 params_str = "" if params is None else " " + params
165 166
166 167 if args.line:
167 168 mman.register_alias(name, target, 'line', params)
168 169 print('Created `%s%s` as an alias for `%s%s%s`.' % (
169 170 magic_escapes['line'], name,
170 171 magic_escapes['line'], target, params_str))
171 172
172 173 if args.cell:
173 174 mman.register_alias(name, target, 'cell', params)
174 175 print('Created `%s%s` as an alias for `%s%s%s`.' % (
175 176 magic_escapes['cell'], name,
176 177 magic_escapes['cell'], target, params_str))
177 178
178 179 @line_magic
179 180 def lsmagic(self, parameter_s=''):
180 181 """List currently available magic functions."""
181 182 return MagicsDisplay(self.shell.magics_manager, ignore=[self.pip])
182 183
183 184 def _magic_docs(self, brief=False, rest=False):
184 185 """Return docstrings from magic functions."""
185 186 mman = self.shell.magics_manager
186 187 docs = mman.lsmagic_docs(brief, missing='No documentation')
187 188
188 189 if rest:
189 190 format_string = '**%s%s**::\n\n%s\n\n'
190 191 else:
191 192 format_string = '%s%s:\n%s\n'
192 193
193 194 return ''.join(
194 195 [format_string % (magic_escapes['line'], fname,
195 196 indent(dedent(fndoc)))
196 197 for fname, fndoc in sorted(docs['line'].items())]
197 198 +
198 199 [format_string % (magic_escapes['cell'], fname,
199 200 indent(dedent(fndoc)))
200 201 for fname, fndoc in sorted(docs['cell'].items())]
201 202 )
202 203
203 204 @line_magic
204 205 def magic(self, parameter_s=''):
205 206 """Print information about the magic function system.
206 207
207 208 Supported formats: -latex, -brief, -rest
208 209 """
209 210
210 211 mode = ''
211 212 try:
212 213 mode = parameter_s.split()[0][1:]
213 214 except IndexError:
214 215 pass
215 216
216 217 brief = (mode == 'brief')
217 218 rest = (mode == 'rest')
218 219 magic_docs = self._magic_docs(brief, rest)
219 220
220 221 if mode == 'latex':
221 222 print(self.format_latex(magic_docs))
222 223 return
223 224 else:
224 225 magic_docs = format_screen(magic_docs)
225 226
226 227 out = ["""
227 228 IPython's 'magic' functions
228 229 ===========================
229 230
230 231 The magic function system provides a series of functions which allow you to
231 232 control the behavior of IPython itself, plus a lot of system-type
232 233 features. There are two kinds of magics, line-oriented and cell-oriented.
233 234
234 235 Line magics are prefixed with the % character and work much like OS
235 236 command-line calls: they get as an argument the rest of the line, where
236 237 arguments are passed without parentheses or quotes. For example, this will
237 238 time the given statement::
238 239
239 240 %timeit range(1000)
240 241
241 242 Cell magics are prefixed with a double %%, and they are functions that get as
242 243 an argument not only the rest of the line, but also the lines below it in a
243 244 separate argument. These magics are called with two arguments: the rest of the
244 245 call line and the body of the cell, consisting of the lines below the first.
245 246 For example::
246 247
247 248 %%timeit x = numpy.random.randn((100, 100))
248 249 numpy.linalg.svd(x)
249 250
250 251 will time the execution of the numpy svd routine, running the assignment of x
251 252 as part of the setup phase, which is not timed.
252 253
253 254 In a line-oriented client (the terminal or Qt console IPython), starting a new
254 255 input with %% will automatically enter cell mode, and IPython will continue
255 256 reading input until a blank line is given. In the notebook, simply type the
256 257 whole cell as one entity, but keep in mind that the %% escape can only be at
257 258 the very start of the cell.
258 259
259 260 NOTE: If you have 'automagic' enabled (via the command line option or with the
260 261 %automagic function), you don't need to type in the % explicitly for line
261 262 magics; cell magics always require an explicit '%%' escape. By default,
262 263 IPython ships with automagic on, so you should only rarely need the % escape.
263 264
264 265 Example: typing '%cd mydir' (without the quotes) changes your working directory
265 266 to 'mydir', if it exists.
266 267
267 268 For a list of the available magic functions, use %lsmagic. For a description
268 269 of any of them, type %magic_name?, e.g. '%cd?'.
269 270
270 271 Currently the magic system has the following functions:""",
271 272 magic_docs,
272 273 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
273 274 str(self.lsmagic()),
274 275 ]
275 276 page.page('\n'.join(out))
276 277
277 278
278 279 @line_magic
279 280 def page(self, parameter_s=''):
280 281 """Pretty print the object and display it through a pager.
281 282
282 283 %page [options] OBJECT
283 284
284 285 If no object is given, use _ (last output).
285 286
286 287 Options:
287 288
288 289 -r: page str(object), don't pretty-print it."""
289 290
290 291 # After a function contributed by Olivier Aubert, slightly modified.
291 292
292 293 # Process options/args
293 294 opts, args = self.parse_options(parameter_s, 'r')
294 295 raw = 'r' in opts
295 296
296 297 oname = args and args or '_'
297 298 info = self.shell._ofind(oname)
298 299 if info['found']:
299 300 txt = (raw and str or pformat)( info['obj'] )
300 301 page.page(txt)
301 302 else:
302 303 print('Object `%s` not found' % oname)
303 304
304 305 @line_magic
305 306 def pprint(self, parameter_s=''):
306 307 """Toggle pretty printing on/off."""
307 308 ptformatter = self.shell.display_formatter.formatters['text/plain']
308 309 ptformatter.pprint = bool(1 - ptformatter.pprint)
309 310 print('Pretty printing has been turned',
310 311 ['OFF','ON'][ptformatter.pprint])
311 312
312 313 @line_magic
313 314 def colors(self, parameter_s=''):
314 315 """Switch color scheme for prompts, info system and exception handlers.
315 316
316 317 Currently implemented schemes: NoColor, Linux, LightBG.
317 318
318 319 Color scheme names are not case-sensitive.
319 320
320 321 Examples
321 322 --------
322 323 To get a plain black and white terminal::
323 324
324 325 %colors nocolor
325 326 """
326 327 def color_switch_err(name):
327 328 warn('Error changing %s color schemes.\n%s' %
328 329 (name, sys.exc_info()[1]), stacklevel=2)
329 330
330 331
331 332 new_scheme = parameter_s.strip()
332 333 if not new_scheme:
333 334 raise UsageError(
334 335 "%colors: you must specify a color scheme. See '%colors?'")
335 336 # local shortcut
336 337 shell = self.shell
337 338
338 339 # Set shell colour scheme
339 340 try:
340 341 shell.colors = new_scheme
341 342 shell.refresh_style()
342 343 except:
343 344 color_switch_err('shell')
344 345
345 346 # Set exception colors
346 347 try:
347 348 shell.InteractiveTB.set_colors(scheme = new_scheme)
348 349 shell.SyntaxTB.set_colors(scheme = new_scheme)
349 350 except:
350 351 color_switch_err('exception')
351 352
352 353 # Set info (for 'object?') colors
353 354 if shell.color_info:
354 355 try:
355 356 shell.inspector.set_active_scheme(new_scheme)
356 357 except:
357 358 color_switch_err('object inspector')
358 359 else:
359 360 shell.inspector.set_active_scheme('NoColor')
360 361
361 362 @line_magic
362 363 def xmode(self, parameter_s=''):
363 364 """Switch modes for the exception handlers.
364 365
365 366 Valid modes: Plain, Context and Verbose.
366 367
367 368 If called without arguments, acts as a toggle."""
368 369
369 370 def xmode_switch_err(name):
370 371 warn('Error changing %s exception modes.\n%s' %
371 372 (name,sys.exc_info()[1]))
372 373
373 374 shell = self.shell
374 375 new_mode = parameter_s.strip().capitalize()
375 376 try:
376 377 shell.InteractiveTB.set_mode(mode=new_mode)
377 378 print('Exception reporting mode:',shell.InteractiveTB.mode)
378 379 except:
379 380 xmode_switch_err('user')
380 381
381 382 @line_magic
383 def autoawait(self, parameter_s):
384 """
385 Allow to change the status of the autoawait option.
386
387 This allow you to set a specific asynchronous code runner.
388
389 If no value is passed, print the currently used asynchronous integration
390 and whether it is activated.
391
392 It can take a number of value evaluated in the following order:
393
394 - False/false/off deactivate autoawait integration
395 - True/true/on activate autoawait integration using configured default
396 loop
397 - asyncio/curio/trio activate autoawait integration and use integration
398 with said library.
399
400 If the passed parameter does not match any of the above and is a python
401 identifier, get said object from user namespace and set it as the
402 runner, and activate autoawait.
403
404 If the object is a fully qualified object name, attempt to import it and
405 set it as the runner, and activate autoawait."""
406
407 param = parameter_s.strip()
408 d = {True: "on", False: "off"}
409
410 if not param:
411 print("IPython autoawait is `{}`, and set to use `{}`".format(
412 d[self.shell.autoawait],
413 self.shell.loop_runner
414 ))
415 return None
416
417 if param.lower() in ('false', 'off'):
418 self.shell.autoawait = False
419 return None
420 if param.lower() in ('true', 'on'):
421 self.shell.autoawait = True
422 return None
423
424 if param in self.shell.loop_runner_map:
425 self.shell.loop_runner = param
426 self.shell.autoawait = True
427 return None
428
429 if param in self.shell.user_ns :
430 self.shell.loop_runner = self.shell.user_ns[param]
431 self.shell.autoawait = True
432 return None
433
434 runner = import_item(param)
435
436 self.shell.loop_runner = runner
437 self.shell.autoawait = True
438
439
440 @line_magic
382 441 def pip(self, args=''):
383 442 """
384 443 Intercept usage of ``pip`` in IPython and direct user to run command outside of IPython.
385 444 """
386 445 print(textwrap.dedent('''
387 446 The following command must be run outside of the IPython shell:
388 447
389 448 $ pip {args}
390 449
391 450 The Python package manager (pip) can only be used from outside of IPython.
392 451 Please reissue the `pip` command in a separate terminal or command prompt.
393 452
394 453 See the Python documentation for more information on how to install packages:
395 454
396 455 https://docs.python.org/3/installing/'''.format(args=args)))
397 456
398 457 @line_magic
399 458 def quickref(self, arg):
400 459 """ Show a quick reference sheet """
401 460 from IPython.core.usage import quick_reference
402 461 qr = quick_reference + self._magic_docs(brief=True)
403 462 page.page(qr)
404 463
405 464 @line_magic
406 465 def doctest_mode(self, parameter_s=''):
407 466 """Toggle doctest mode on and off.
408 467
409 468 This mode is intended to make IPython behave as much as possible like a
410 469 plain Python shell, from the perspective of how its prompts, exceptions
411 470 and output look. This makes it easy to copy and paste parts of a
412 471 session into doctests. It does so by:
413 472
414 473 - Changing the prompts to the classic ``>>>`` ones.
415 474 - Changing the exception reporting mode to 'Plain'.
416 475 - Disabling pretty-printing of output.
417 476
418 477 Note that IPython also supports the pasting of code snippets that have
419 478 leading '>>>' and '...' prompts in them. This means that you can paste
420 479 doctests from files or docstrings (even if they have leading
421 480 whitespace), and the code will execute correctly. You can then use
422 481 '%history -t' to see the translated history; this will give you the
423 482 input after removal of all the leading prompts and whitespace, which
424 483 can be pasted back into an editor.
425 484
426 485 With these features, you can switch into this mode easily whenever you
427 486 need to do testing and changes to doctests, without having to leave
428 487 your existing IPython session.
429 488 """
430 489
431 490 # Shorthands
432 491 shell = self.shell
433 492 meta = shell.meta
434 493 disp_formatter = self.shell.display_formatter
435 494 ptformatter = disp_formatter.formatters['text/plain']
436 495 # dstore is a data store kept in the instance metadata bag to track any
437 496 # changes we make, so we can undo them later.
438 497 dstore = meta.setdefault('doctest_mode',Struct())
439 498 save_dstore = dstore.setdefault
440 499
441 500 # save a few values we'll need to recover later
442 501 mode = save_dstore('mode',False)
443 502 save_dstore('rc_pprint',ptformatter.pprint)
444 503 save_dstore('xmode',shell.InteractiveTB.mode)
445 504 save_dstore('rc_separate_out',shell.separate_out)
446 505 save_dstore('rc_separate_out2',shell.separate_out2)
447 506 save_dstore('rc_separate_in',shell.separate_in)
448 507 save_dstore('rc_active_types',disp_formatter.active_types)
449 508
450 509 if not mode:
451 510 # turn on
452 511
453 512 # Prompt separators like plain python
454 513 shell.separate_in = ''
455 514 shell.separate_out = ''
456 515 shell.separate_out2 = ''
457 516
458 517
459 518 ptformatter.pprint = False
460 519 disp_formatter.active_types = ['text/plain']
461 520
462 521 shell.magic('xmode Plain')
463 522 else:
464 523 # turn off
465 524 shell.separate_in = dstore.rc_separate_in
466 525
467 526 shell.separate_out = dstore.rc_separate_out
468 527 shell.separate_out2 = dstore.rc_separate_out2
469 528
470 529 ptformatter.pprint = dstore.rc_pprint
471 530 disp_formatter.active_types = dstore.rc_active_types
472 531
473 532 shell.magic('xmode ' + dstore.xmode)
474 533
475 534 # mode here is the state before we switch; switch_doctest_mode takes
476 535 # the mode we're switching to.
477 536 shell.switch_doctest_mode(not mode)
478 537
479 538 # Store new mode and inform
480 539 dstore.mode = bool(not mode)
481 540 mode_label = ['OFF','ON'][dstore.mode]
482 541 print('Doctest mode is:', mode_label)
483 542
484 543 @line_magic
485 544 def gui(self, parameter_s=''):
486 545 """Enable or disable IPython GUI event loop integration.
487 546
488 547 %gui [GUINAME]
489 548
490 549 This magic replaces IPython's threaded shells that were activated
491 550 using the (pylab/wthread/etc.) command line flags. GUI toolkits
492 551 can now be enabled at runtime and keyboard
493 552 interrupts should work without any problems. The following toolkits
494 553 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
495 554
496 555 %gui wx # enable wxPython event loop integration
497 556 %gui qt4|qt # enable PyQt4 event loop integration
498 557 %gui qt5 # enable PyQt5 event loop integration
499 558 %gui gtk # enable PyGTK event loop integration
500 559 %gui gtk3 # enable Gtk3 event loop integration
501 560 %gui tk # enable Tk event loop integration
502 561 %gui osx # enable Cocoa event loop integration
503 562 # (requires %matplotlib 1.1)
504 563 %gui # disable all event loop integration
505 564
506 565 WARNING: after any of these has been called you can simply create
507 566 an application object, but DO NOT start the event loop yourself, as
508 567 we have already handled that.
509 568 """
510 569 opts, arg = self.parse_options(parameter_s, '')
511 570 if arg=='': arg = None
512 571 try:
513 572 return self.shell.enable_gui(arg)
514 573 except Exception as e:
515 574 # print simple error message, rather than traceback if we can't
516 575 # hook up the GUI
517 576 error(str(e))
518 577
519 578 @skip_doctest
520 579 @line_magic
521 580 def precision(self, s=''):
522 581 """Set floating point precision for pretty printing.
523 582
524 583 Can set either integer precision or a format string.
525 584
526 585 If numpy has been imported and precision is an int,
527 586 numpy display precision will also be set, via ``numpy.set_printoptions``.
528 587
529 588 If no argument is given, defaults will be restored.
530 589
531 590 Examples
532 591 --------
533 592 ::
534 593
535 594 In [1]: from math import pi
536 595
537 596 In [2]: %precision 3
538 597 Out[2]: u'%.3f'
539 598
540 599 In [3]: pi
541 600 Out[3]: 3.142
542 601
543 602 In [4]: %precision %i
544 603 Out[4]: u'%i'
545 604
546 605 In [5]: pi
547 606 Out[5]: 3
548 607
549 608 In [6]: %precision %e
550 609 Out[6]: u'%e'
551 610
552 611 In [7]: pi**10
553 612 Out[7]: 9.364805e+04
554 613
555 614 In [8]: %precision
556 615 Out[8]: u'%r'
557 616
558 617 In [9]: pi**10
559 618 Out[9]: 93648.047476082982
560 619 """
561 620 ptformatter = self.shell.display_formatter.formatters['text/plain']
562 621 ptformatter.float_precision = s
563 622 return ptformatter.float_format
564 623
565 624 @magic_arguments.magic_arguments()
566 625 @magic_arguments.argument(
567 626 '-e', '--export', action='store_true', default=False,
568 627 help=argparse.SUPPRESS
569 628 )
570 629 @magic_arguments.argument(
571 630 'filename', type=str,
572 631 help='Notebook name or filename'
573 632 )
574 633 @line_magic
575 634 def notebook(self, s):
576 635 """Export and convert IPython notebooks.
577 636
578 637 This function can export the current IPython history to a notebook file.
579 638 For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
580 639
581 640 The -e or --export flag is deprecated in IPython 5.2, and will be
582 641 removed in the future.
583 642 """
584 643 args = magic_arguments.parse_argstring(self.notebook, s)
585 644
586 645 from nbformat import write, v4
587 646
588 647 cells = []
589 648 hist = list(self.shell.history_manager.get_range())
590 649 if(len(hist)<=1):
591 650 raise ValueError('History is empty, cannot export')
592 651 for session, execution_count, source in hist[:-1]:
593 652 cells.append(v4.new_code_cell(
594 653 execution_count=execution_count,
595 654 source=source
596 655 ))
597 656 nb = v4.new_notebook(cells=cells)
598 657 with io.open(args.filename, 'w', encoding='utf-8') as f:
599 658 write(nb, f, version=4)
@@ -1,396 +1,417
1 1 # encoding: utf-8
2 2 """
3 3 An embedded IPython shell.
4 4 """
5 5 # Copyright (c) IPython Development Team.
6 6 # Distributed under the terms of the Modified BSD License.
7 7
8 8
9 9 import sys
10 10 import warnings
11 11
12 12 from IPython.core import ultratb, compilerop
13 13 from IPython.core import magic_arguments
14 14 from IPython.core.magic import Magics, magics_class, line_magic
15 15 from IPython.core.interactiveshell import DummyMod, InteractiveShell
16 16 from IPython.terminal.interactiveshell import TerminalInteractiveShell
17 17 from IPython.terminal.ipapp import load_default_config
18 18
19 19 from traitlets import Bool, CBool, Unicode
20 20 from IPython.utils.io import ask_yes_no
21 21
22 from contextlib import contextmanager
23
24 _sentinel = object()
25 @contextmanager
26 def new_context():
27 import trio._core._run as tcr
28 old_runner = getattr(tcr.GLOBAL_RUN_CONTEXT, 'runner', _sentinel)
29 old_task = getattr(tcr.GLOBAL_RUN_CONTEXT, 'task', None)
30 if old_runner is not _sentinel:
31 del tcr.GLOBAL_RUN_CONTEXT.runner
32 tcr.GLOBAL_RUN_CONTEXT.task = None
33 yield
34 if old_runner is not _sentinel:
35 tcr.GLOBAL_RUN_CONTEXT.runner = old_runner
36 tcr.GLOBAL_RUN_CONTEXT.task = old_task
37
38
22 39 class KillEmbedded(Exception):pass
23 40
24 41 # kept for backward compatibility as IPython 6 was released with
25 42 # the typo. See https://github.com/ipython/ipython/pull/10706
26 43 KillEmbeded = KillEmbedded
27 44
28 45 # This is an additional magic that is exposed in embedded shells.
29 46 @magics_class
30 47 class EmbeddedMagics(Magics):
31 48
32 49 @line_magic
33 50 @magic_arguments.magic_arguments()
34 51 @magic_arguments.argument('-i', '--instance', action='store_true',
35 52 help='Kill instance instead of call location')
36 53 @magic_arguments.argument('-x', '--exit', action='store_true',
37 54 help='Also exit the current session')
38 55 @magic_arguments.argument('-y', '--yes', action='store_true',
39 56 help='Do not ask confirmation')
40 57 def kill_embedded(self, parameter_s=''):
41 58 """%kill_embedded : deactivate for good the current embedded IPython
42 59
43 60 This function (after asking for confirmation) sets an internal flag so
44 61 that an embedded IPython will never activate again for the given call
45 62 location. This is useful to permanently disable a shell that is being
46 63 called inside a loop: once you've figured out what you needed from it,
47 64 you may then kill it and the program will then continue to run without
48 65 the interactive shell interfering again.
49 66
50 67
51 68 Kill Instance Option:
52 69
53 70 If for some reasons you need to kill the location where the instance
54 71 is created and not called, for example if you create a single
55 72 instance in one place and debug in many locations, you can use the
56 73 ``--instance`` option to kill this specific instance. Like for the
57 74 ``call location`` killing an "instance" should work even if it is
58 75 recreated within a loop.
59 76
60 77 .. note::
61 78
62 79 This was the default behavior before IPython 5.2
63 80
64 81 """
65 82
66 83 args = magic_arguments.parse_argstring(self.kill_embedded, parameter_s)
67 84 print(args)
68 85 if args.instance:
69 86 # let no ask
70 87 if not args.yes:
71 88 kill = ask_yes_no(
72 89 "Are you sure you want to kill this embedded instance? [y/N] ", 'n')
73 90 else:
74 91 kill = True
75 92 if kill:
76 93 self.shell._disable_init_location()
77 94 print("This embedded IPython instance will not reactivate anymore "
78 95 "once you exit.")
79 96 else:
80 97 if not args.yes:
81 98 kill = ask_yes_no(
82 99 "Are you sure you want to kill this embedded call_location? [y/N] ", 'n')
83 100 else:
84 101 kill = True
85 102 if kill:
86 103 self.shell.embedded_active = False
87 104 print("This embedded IPython call location will not reactivate anymore "
88 105 "once you exit.")
89 106
90 107 if args.exit:
91 108 # Ask-exit does not really ask, it just set internals flags to exit
92 109 # on next loop.
93 110 self.shell.ask_exit()
94 111
95 112
96 113 @line_magic
97 114 def exit_raise(self, parameter_s=''):
98 115 """%exit_raise Make the current embedded kernel exit and raise and exception.
99 116
100 117 This function sets an internal flag so that an embedded IPython will
101 118 raise a `IPython.terminal.embed.KillEmbedded` Exception on exit, and then exit the current I. This is
102 119 useful to permanently exit a loop that create IPython embed instance.
103 120 """
104 121
105 122 self.shell.should_raise = True
106 123 self.shell.ask_exit()
107 124
108 125
109 126
110 127 class InteractiveShellEmbed(TerminalInteractiveShell):
111 128
112 129 dummy_mode = Bool(False)
113 130 exit_msg = Unicode('')
114 131 embedded = CBool(True)
115 132 should_raise = CBool(False)
116 133 # Like the base class display_banner is not configurable, but here it
117 134 # is True by default.
118 135 display_banner = CBool(True)
119 136 exit_msg = Unicode()
120 137
121 138 # When embedding, by default we don't change the terminal title
122 139 term_title = Bool(False,
123 140 help="Automatically set the terminal title"
124 141 ).tag(config=True)
125 142
126 143 _inactive_locations = set()
127 144
128 145 @property
129 146 def embedded_active(self):
130 147 return (self._call_location_id not in InteractiveShellEmbed._inactive_locations)\
131 148 and (self._init_location_id not in InteractiveShellEmbed._inactive_locations)
132 149
133 150 def _disable_init_location(self):
134 151 """Disable the current Instance creation location"""
135 152 InteractiveShellEmbed._inactive_locations.add(self._init_location_id)
136 153
137 154 @embedded_active.setter
138 155 def embedded_active(self, value):
139 156 if value:
140 157 InteractiveShellEmbed._inactive_locations.discard(
141 158 self._call_location_id)
142 159 InteractiveShellEmbed._inactive_locations.discard(
143 160 self._init_location_id)
144 161 else:
145 162 InteractiveShellEmbed._inactive_locations.add(
146 163 self._call_location_id)
147 164
148 165 def __init__(self, **kw):
149 166 if kw.get('user_global_ns', None) is not None:
150 167 raise DeprecationWarning(
151 168 "Key word argument `user_global_ns` has been replaced by `user_module` since IPython 4.0.")
152 169
153 170 clid = kw.pop('_init_location_id', None)
154 171 if not clid:
155 172 frame = sys._getframe(1)
156 173 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
157 174 self._init_location_id = clid
158 175
159 176 super(InteractiveShellEmbed,self).__init__(**kw)
160 177
161 178 # don't use the ipython crash handler so that user exceptions aren't
162 179 # trapped
163 180 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
164 181 mode=self.xmode,
165 182 call_pdb=self.pdb)
166 183
167 184 def init_sys_modules(self):
168 185 """
169 186 Explicitly overwrite :mod:`IPython.core.interactiveshell` to do nothing.
170 187 """
171 188 pass
172 189
173 190 def init_magics(self):
174 191 super(InteractiveShellEmbed, self).init_magics()
175 192 self.register_magics(EmbeddedMagics)
176 193
177 194 def __call__(self, header='', local_ns=None, module=None, dummy=None,
178 195 stack_depth=1, global_ns=None, compile_flags=None, **kw):
179 196 """Activate the interactive interpreter.
180 197
181 198 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
182 199 the interpreter shell with the given local and global namespaces, and
183 200 optionally print a header string at startup.
184 201
185 202 The shell can be globally activated/deactivated using the
186 203 dummy_mode attribute. This allows you to turn off a shell used
187 204 for debugging globally.
188 205
189 206 However, *each* time you call the shell you can override the current
190 207 state of dummy_mode with the optional keyword parameter 'dummy'. For
191 208 example, if you set dummy mode on with IPShell.dummy_mode = True, you
192 209 can still have a specific call work by making it as IPShell(dummy=False).
193 210 """
194 211
195 212 # we are called, set the underlying interactiveshell not to exit.
196 213 self.keep_running = True
197 214
198 215 # If the user has turned it off, go away
199 216 clid = kw.pop('_call_location_id', None)
200 217 if not clid:
201 218 frame = sys._getframe(1)
202 219 clid = '%s:%s' % (frame.f_code.co_filename, frame.f_lineno)
203 220 self._call_location_id = clid
204 221
205 222 if not self.embedded_active:
206 223 return
207 224
208 225 # Normal exits from interactive mode set this flag, so the shell can't
209 226 # re-enter (it checks this variable at the start of interactive mode).
210 227 self.exit_now = False
211 228
212 229 # Allow the dummy parameter to override the global __dummy_mode
213 230 if dummy or (dummy != 0 and self.dummy_mode):
214 231 return
215 232
216 233 # self.banner is auto computed
217 234 if header:
218 235 self.old_banner2 = self.banner2
219 236 self.banner2 = self.banner2 + '\n' + header + '\n'
220 237 else:
221 238 self.old_banner2 = ''
222 239
223 240 if self.display_banner:
224 241 self.show_banner()
225 242
226 243 # Call the embedding code with a stack depth of 1 so it can skip over
227 244 # our call and get the original caller's namespaces.
228 245 self.mainloop(local_ns, module, stack_depth=stack_depth,
229 246 global_ns=global_ns, compile_flags=compile_flags)
230 247
231 248 self.banner2 = self.old_banner2
232 249
233 250 if self.exit_msg is not None:
234 251 print(self.exit_msg)
235 252
236 253 if self.should_raise:
237 254 raise KillEmbedded('Embedded IPython raising error, as user requested.')
238 255
239 256
240 257 def mainloop(self, local_ns=None, module=None, stack_depth=0,
241 258 display_banner=None, global_ns=None, compile_flags=None):
242 259 """Embeds IPython into a running python program.
243 260
244 261 Parameters
245 262 ----------
246 263
247 264 local_ns, module
248 265 Working local namespace (a dict) and module (a module or similar
249 266 object). If given as None, they are automatically taken from the scope
250 267 where the shell was called, so that program variables become visible.
251 268
252 269 stack_depth : int
253 270 How many levels in the stack to go to looking for namespaces (when
254 271 local_ns or module is None). This allows an intermediate caller to
255 272 make sure that this function gets the namespace from the intended
256 273 level in the stack. By default (0) it will get its locals and globals
257 274 from the immediate caller.
258 275
259 276 compile_flags
260 277 A bit field identifying the __future__ features
261 278 that are enabled, as passed to the builtin :func:`compile` function.
262 279 If given as None, they are automatically taken from the scope where
263 280 the shell was called.
264 281
265 282 """
266 283
267 284 if (global_ns is not None) and (module is None):
268 285 raise DeprecationWarning("'global_ns' keyword argument is deprecated, and has been removed in IPython 5.0 use `module` keyword argument instead.")
269 286
270 287 if (display_banner is not None):
271 288 warnings.warn("The display_banner parameter is deprecated since IPython 4.0", DeprecationWarning)
272 289
273 290 # Get locals and globals from caller
274 291 if ((local_ns is None or module is None or compile_flags is None)
275 292 and self.default_user_namespaces):
276 293 call_frame = sys._getframe(stack_depth).f_back
277 294
278 295 if local_ns is None:
279 296 local_ns = call_frame.f_locals
280 297 if module is None:
281 298 global_ns = call_frame.f_globals
282 299 try:
283 300 module = sys.modules[global_ns['__name__']]
284 301 except KeyError:
285 302 warnings.warn("Failed to get module %s" % \
286 303 global_ns.get('__name__', 'unknown module')
287 304 )
288 305 module = DummyMod()
289 306 module.__dict__ = global_ns
290 307 if compile_flags is None:
291 308 compile_flags = (call_frame.f_code.co_flags &
292 309 compilerop.PyCF_MASK)
293 310
294 311 # Save original namespace and module so we can restore them after
295 312 # embedding; otherwise the shell doesn't shut down correctly.
296 313 orig_user_module = self.user_module
297 314 orig_user_ns = self.user_ns
298 315 orig_compile_flags = self.compile.flags
299 316
300 317 # Update namespaces and fire up interpreter
301 318
302 319 # The global one is easy, we can just throw it in
303 320 if module is not None:
304 321 self.user_module = module
305 322
306 323 # But the user/local one is tricky: ipython needs it to store internal
307 324 # data, but we also need the locals. We'll throw our hidden variables
308 325 # like _ih and get_ipython() into the local namespace, but delete them
309 326 # later.
310 327 if local_ns is not None:
311 328 reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()}
312 329 self.user_ns = reentrant_local_ns
313 330 self.init_user_ns()
314 331
315 332 # Compiler flags
316 333 if compile_flags is not None:
317 334 self.compile.flags = compile_flags
318 335
319 336 # make sure the tab-completer has the correct frame information, so it
320 337 # actually completes using the frame's locals/globals
321 338 self.set_completer_frame()
322 339
323 340 with self.builtin_trap, self.display_trap:
324 341 self.interact()
325 342
326 343 # now, purge out the local namespace of IPython's hidden variables.
327 344 if local_ns is not None:
328 345 local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()})
329 346
330 347
331 348 # Restore original namespace so shell can shut down when we exit.
332 349 self.user_module = orig_user_module
333 350 self.user_ns = orig_user_ns
334 351 self.compile.flags = orig_compile_flags
335 352
336 353
337 354 def embed(**kwargs):
338 355 """Call this to embed IPython at the current point in your program.
339 356
340 357 The first invocation of this will create an :class:`InteractiveShellEmbed`
341 358 instance and then call it. Consecutive calls just call the already
342 359 created instance.
343 360
344 361 If you don't want the kernel to initialize the namespace
345 362 from the scope of the surrounding function,
346 363 and/or you want to load full IPython configuration,
347 364 you probably want `IPython.start_ipython()` instead.
348 365
349 366 Here is a simple example::
350 367
351 368 from IPython import embed
352 369 a = 10
353 370 b = 20
354 371 embed(header='First time')
355 372 c = 30
356 373 d = 40
357 374 embed()
358 375
359 376 Full customization can be done by passing a :class:`Config` in as the
360 377 config argument.
361 378 """
362 379 config = kwargs.get('config')
363 380 header = kwargs.pop('header', u'')
364 381 compile_flags = kwargs.pop('compile_flags', None)
365 382 if config is None:
366 383 config = load_default_config()
367 384 config.InteractiveShellEmbed = config.TerminalInteractiveShell
368 385 kwargs['config'] = config
386 using = kwargs.get('using', 'trio')
387 if using :
388 kwargs['config'].update({'TerminalInteractiveShell':{'loop_runner':using, 'colors':'NoColor'}})
369 389 #save ps1/ps2 if defined
370 390 ps1 = None
371 391 ps2 = None
372 392 try:
373 393 ps1 = sys.ps1
374 394 ps2 = sys.ps2
375 395 except AttributeError:
376 396 pass
377 397 #save previous instance
378 398 saved_shell_instance = InteractiveShell._instance
379 399 if saved_shell_instance is not None:
380 400 cls = type(saved_shell_instance)
381 401 cls.clear_instance()
382 402 frame = sys._getframe(1)
383 shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
384 frame.f_code.co_filename, frame.f_lineno), **kwargs)
385 shell(header=header, stack_depth=2, compile_flags=compile_flags,
386 _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
387 InteractiveShellEmbed.clear_instance()
403 with new_context():
404 shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % (
405 frame.f_code.co_filename, frame.f_lineno), **kwargs)
406 shell(header=header, stack_depth=2, compile_flags=compile_flags,
407 _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
408 InteractiveShellEmbed.clear_instance()
388 409 #restore previous instance
389 410 if saved_shell_instance is not None:
390 411 cls = type(saved_shell_instance)
391 412 cls.clear_instance()
392 413 for subclass in cls._walk_mro():
393 414 subclass._instance = saved_shell_instance
394 415 if ps1 is not None:
395 416 sys.ps1 = ps1
396 417 sys.ps2 = ps2
@@ -1,132 +1,135
1 1 """Test embedding of IPython"""
2 2
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2013 The IPython Development Team
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13
14 14 import os
15 15 import subprocess
16 16 import sys
17 17 import nose.tools as nt
18 18 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
19 19 from IPython.testing.decorators import skip_win32
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Tests
23 23 #-----------------------------------------------------------------------------
24 24
25 25
26 26 _sample_embed = b"""
27 27 import IPython
28 28
29 29 a = 3
30 30 b = 14
31 31 print(a, '.', b)
32 32
33 33 IPython.embed()
34 34
35 35 print('bye!')
36 36 """
37 37
38 38 _exit = b"exit\r"
39 39
40 40 def test_ipython_embed():
41 41 """test that `IPython.embed()` works"""
42 42 with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
43 43 f.write(_sample_embed)
44 44 f.flush()
45 45 f.close() # otherwise msft won't be able to read the file
46 46
47 47 # run `python file_with_embed.py`
48 48 cmd = [sys.executable, f.name]
49 49 env = os.environ.copy()
50 50 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
51 51
52 52 p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE,
53 53 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54 54 out, err = p.communicate(_exit)
55 55 std = out.decode('UTF-8')
56 56
57 57 nt.assert_equal(p.returncode, 0)
58 58 nt.assert_in('3 . 14', std)
59 59 if os.name != 'nt':
60 60 # TODO: Fix up our different stdout references, see issue gh-14
61 61 nt.assert_in('IPython', std)
62 62 nt.assert_in('bye!', std)
63 63
64 64 @skip_win32
65 65 def test_nest_embed():
66 66 """test that `IPython.embed()` is nestable"""
67 67 import pexpect
68 68 ipy_prompt = r']:' #ansi color codes give problems matching beyond this
69 69 env = os.environ.copy()
70 70 env['IPY_TEST_SIMPLE_PROMPT'] = '1'
71 71
72 72
73 73 child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'],
74 74 env=env)
75 child.timeout = 5
75 76 child.expect(ipy_prompt)
76 77 child.sendline("import IPython")
77 78 child.expect(ipy_prompt)
78 79 child.sendline("ip0 = get_ipython()")
79 80 #enter first nested embed
80 81 child.sendline("IPython.embed()")
81 82 #skip the banner until we get to a prompt
82 83 try:
83 84 prompted = -1
84 85 while prompted != 0:
85 86 prompted = child.expect([ipy_prompt, '\r\n'])
86 87 except pexpect.TIMEOUT as e:
87 88 print(e)
88 89 #child.interact()
89 child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt)
90 child.sendline("embed1 = get_ipython()")
91 child.expect(ipy_prompt)
90 92 child.sendline("print('true' if embed1 is not ip0 else 'false')")
91 93 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
92 94 child.expect(ipy_prompt)
93 95 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
94 96 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
95 97 child.expect(ipy_prompt)
96 98 #enter second nested embed
97 99 child.sendline("IPython.embed()")
98 100 #skip the banner until we get to a prompt
99 101 try:
100 102 prompted = -1
101 103 while prompted != 0:
102 104 prompted = child.expect([ipy_prompt, '\r\n'])
103 105 except pexpect.TIMEOUT as e:
104 106 print(e)
105 107 #child.interact()
106 child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt)
108 child.sendline("embed2 = get_ipython()")
109 child.expect(ipy_prompt)
107 110 child.sendline("print('true' if embed2 is not embed1 else 'false')")
108 111 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
109 112 child.expect(ipy_prompt)
110 113 child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')")
111 114 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
112 115 child.expect(ipy_prompt)
113 116 child.sendline('exit')
114 117 #back at first embed
115 118 child.expect(ipy_prompt)
116 119 child.sendline("print('true' if get_ipython() is embed1 else 'false')")
117 120 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
118 121 child.expect(ipy_prompt)
119 122 child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
120 123 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
121 124 child.expect(ipy_prompt)
122 125 child.sendline('exit')
123 126 #back at launching scope
124 127 child.expect(ipy_prompt)
125 128 child.sendline("print('true' if get_ipython() is ip0 else 'false')")
126 129 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
127 130 child.expect(ipy_prompt)
128 131 child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')")
129 132 assert(child.expect(['true\r\n', 'false\r\n']) == 0)
130 133 child.expect(ipy_prompt)
131 134 child.sendline('exit')
132 135 child.close()
@@ -1,298 +1,299
1 1 # -*- coding: utf-8 -*-
2 2 #
3 3 # IPython documentation build configuration file.
4 4
5 5 # NOTE: This file has been edited manually from the auto-generated one from
6 6 # sphinx. Do NOT delete and re-generate. If any changes from sphinx are
7 7 # needed, generate a scratch one and merge by hand any new fields needed.
8 8
9 9 #
10 10 # This file is execfile()d with the current directory set to its containing dir.
11 11 #
12 12 # The contents of this file are pickled, so don't put values in the namespace
13 13 # that aren't pickleable (module imports are okay, they're removed automatically).
14 14 #
15 15 # All configuration values have a default value; values that are commented out
16 16 # serve to show the default value.
17 17
18 18 import sys, os
19 19
20 20 # http://read-the-docs.readthedocs.io/en/latest/faq.html
21 21 ON_RTD = os.environ.get('READTHEDOCS', None) == 'True'
22 22
23 23 if ON_RTD:
24 24 tags.add('rtd')
25 25
26 26 # RTD doesn't use the Makefile, so re-run autogen_{things}.py here.
27 27 for name in ('config', 'api', 'magics', 'shortcuts'):
28 28 fname = 'autogen_{}.py'.format(name)
29 29 fpath = os.path.abspath(os.path.join('..', fname))
30 30 with open(fpath) as f:
31 31 exec(compile(f.read(), fname, 'exec'), {
32 32 '__file__': fpath,
33 33 '__name__': '__main__',
34 34 })
35 35 else:
36 36 import sphinx_rtd_theme
37 37 html_theme = "sphinx_rtd_theme"
38 38 html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
39 39
40 40 # If your extensions are in another directory, add it here. If the directory
41 41 # is relative to the documentation root, use os.path.abspath to make it
42 42 # absolute, like shown here.
43 43 sys.path.insert(0, os.path.abspath('../sphinxext'))
44 44
45 45 # We load the ipython release info into a dict by explicit execution
46 46 iprelease = {}
47 47 exec(compile(open('../../IPython/core/release.py').read(), '../../IPython/core/release.py', 'exec'),iprelease)
48 48
49 49 # General configuration
50 50 # ---------------------
51 51
52 52 # Add any Sphinx extension module names here, as strings. They can be extensions
53 53 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
54 54 extensions = [
55 55 'sphinx.ext.autodoc',
56 56 'sphinx.ext.autosummary',
57 57 'sphinx.ext.doctest',
58 58 'sphinx.ext.inheritance_diagram',
59 59 'sphinx.ext.intersphinx',
60 60 'sphinx.ext.graphviz',
61 61 'IPython.sphinxext.ipython_console_highlighting',
62 62 'IPython.sphinxext.ipython_directive',
63 63 'sphinx.ext.napoleon', # to preprocess docstrings
64 64 'github', # for easy GitHub links
65 65 'magics',
66 66 'configtraits',
67 67 ]
68 68
69 69 if ON_RTD:
70 70 # Remove extensions not currently supported on RTD
71 71 extensions.remove('IPython.sphinxext.ipython_directive')
72 72 extensions.remove('IPython.sphinxext.ipython_console_highlighting')
73 73
74 74 # Add any paths that contain templates here, relative to this directory.
75 75 templates_path = ['_templates']
76 76
77 77 # The suffix of source filenames.
78 78 source_suffix = '.rst'
79 79
80 80 rst_prolog = ''
81 81
82 82 def is_stable(extra):
83 83 for ext in {'dev', 'b', 'rc'}:
84 84 if ext in extra:
85 85 return False
86 86 return True
87 87
88 88 if is_stable(iprelease['_version_extra']):
89 89 tags.add('ipystable')
90 90 else:
91 91 tags.add('ipydev')
92 92 rst_prolog += """
93 93 .. warning::
94 94
95 95 This documentation covers a development version of IPython. The development
96 96 version may differ significantly from the latest stable release.
97 97 """
98 98
99 99 rst_prolog += """
100 100 .. important::
101 101
102 102 This documentation covers IPython versions 6.0 and higher. Beginning with
103 103 version 6.0, IPython stopped supporting compatibility with Python versions
104 104 lower than 3.3 including all versions of Python 2.7.
105 105
106 106 If you are looking for an IPython version compatible with Python 2.7,
107 107 please use the IPython 5.x LTS release and refer to its documentation (LTS
108 108 is the long term support release).
109 109
110 110 """
111 111
112 112 # The master toctree document.
113 113 master_doc = 'index'
114 114
115 115 # General substitutions.
116 116 project = 'IPython'
117 117 copyright = 'The IPython Development Team'
118 118
119 119 # ghissue config
120 120 github_project_url = "https://github.com/ipython/ipython"
121 121
122 122 # numpydoc config
123 123 numpydoc_show_class_members = False # Otherwise Sphinx emits thousands of warnings
124 124 numpydoc_class_members_toctree = False
125 125
126 126 # The default replacements for |version| and |release|, also used in various
127 127 # other places throughout the built documents.
128 128 #
129 129 # The full version, including alpha/beta/rc tags.
130 130 release = "%s" % iprelease['version']
131 131 # Just the X.Y.Z part, no '-dev'
132 132 version = iprelease['version'].split('-', 1)[0]
133 133
134 134
135 135 # There are two options for replacing |today|: either, you set today to some
136 136 # non-false value, then it is used:
137 137 #today = ''
138 138 # Else, today_fmt is used as the format for a strftime call.
139 139 today_fmt = '%B %d, %Y'
140 140
141 141 # List of documents that shouldn't be included in the build.
142 142 #unused_docs = []
143 143
144 144 # Exclude these glob-style patterns when looking for source files. They are
145 145 # relative to the source/ directory.
146 exclude_patterns = ['whatsnew/pr']
146 exclude_patterns = ['whatsnew/pr/antigravity-feature.*',
147 'whatsnew/pr/incompat-switching-to-perl.*']
147 148
148 149
149 150 # If true, '()' will be appended to :func: etc. cross-reference text.
150 151 #add_function_parentheses = True
151 152
152 153 # If true, the current module name will be prepended to all description
153 154 # unit titles (such as .. function::).
154 155 #add_module_names = True
155 156
156 157 # If true, sectionauthor and moduleauthor directives will be shown in the
157 158 # output. They are ignored by default.
158 159 #show_authors = False
159 160
160 161 # The name of the Pygments (syntax highlighting) style to use.
161 162 pygments_style = 'sphinx'
162 163
163 164 # Set the default role so we can use `foo` instead of ``foo``
164 165 default_role = 'literal'
165 166
166 167 # Options for HTML output
167 168 # -----------------------
168 169
169 170 # The style sheet to use for HTML and HTML Help pages. A file of that name
170 171 # must exist either in Sphinx' static/ path, or in one of the custom paths
171 172 # given in html_static_path.
172 173 # html_style = 'default.css'
173 174
174 175
175 176 # The name for this set of Sphinx documents. If None, it defaults to
176 177 # "<project> v<release> documentation".
177 178 #html_title = None
178 179
179 180 # The name of an image file (within the static path) to place at the top of
180 181 # the sidebar.
181 182 #html_logo = None
182 183
183 184 # Add any paths that contain custom static files (such as style sheets) here,
184 185 # relative to this directory. They are copied after the builtin static files,
185 186 # so a file named "default.css" will overwrite the builtin "default.css".
186 187 html_static_path = ['_static']
187 188
188 189 # Favicon needs the directory name
189 190 html_favicon = '_static/favicon.ico'
190 191 # If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
191 192 # using the given strftime format.
192 193 html_last_updated_fmt = '%b %d, %Y'
193 194
194 195 # If true, SmartyPants will be used to convert quotes and dashes to
195 196 # typographically correct entities.
196 197 #html_use_smartypants = True
197 198
198 199 # Custom sidebar templates, maps document names to template names.
199 200 #html_sidebars = {}
200 201
201 202 # Additional templates that should be rendered to pages, maps page names to
202 203 # template names.
203 204 html_additional_pages = {
204 205 'interactive/htmlnotebook': 'notebook_redirect.html',
205 206 'interactive/notebook': 'notebook_redirect.html',
206 207 'interactive/nbconvert': 'notebook_redirect.html',
207 208 'interactive/public_server': 'notebook_redirect.html',
208 209 }
209 210
210 211 # If false, no module index is generated.
211 212 #html_use_modindex = True
212 213
213 214 # If true, the reST sources are included in the HTML build as _sources/<name>.
214 215 #html_copy_source = True
215 216
216 217 # If true, an OpenSearch description file will be output, and all pages will
217 218 # contain a <link> tag referring to it. The value of this option must be the
218 219 # base URL from which the finished HTML is served.
219 220 #html_use_opensearch = ''
220 221
221 222 # If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
222 223 #html_file_suffix = ''
223 224
224 225 # Output file base name for HTML help builder.
225 226 htmlhelp_basename = 'ipythondoc'
226 227
227 228 intersphinx_mapping = {'python': ('https://docs.python.org/3/', None),
228 229 'rpy2': ('https://rpy2.readthedocs.io/en/version_2.8.x/', None),
229 230 'traitlets': ('https://traitlets.readthedocs.io/en/latest/', None),
230 231 'jupyterclient': ('https://jupyter-client.readthedocs.io/en/latest/', None),
231 232 'ipyparallel': ('https://ipyparallel.readthedocs.io/en/latest/', None),
232 233 'jupyter': ('https://jupyter.readthedocs.io/en/latest/', None),
233 234 'jedi': ('https://jedi.readthedocs.io/en/latest/', None),
234 235 'traitlets': ('https://traitlets.readthedocs.io/en/latest/', None),
235 236 'ipykernel': ('https://ipykernel.readthedocs.io/en/latest/', None),
236 237 'prompt_toolkit' : ('https://python-prompt-toolkit.readthedocs.io/en/stable/', None),
237 238 'ipywidgets': ('https://ipywidgets.readthedocs.io/en/stable/', None),
238 239 'ipyparallel': ('https://ipyparallel.readthedocs.io/en/stable/', None)
239 240 }
240 241
241 242 # Options for LaTeX output
242 243 # ------------------------
243 244
244 245 # The paper size ('letter' or 'a4').
245 246 latex_paper_size = 'letter'
246 247
247 248 # The font size ('10pt', '11pt' or '12pt').
248 249 latex_font_size = '11pt'
249 250
250 251 # Grouping the document tree into LaTeX files. List of tuples
251 252 # (source start file, target name, title, author, document class [howto/manual]).
252 253
253 254 latex_documents = [
254 255 ('index', 'ipython.tex', 'IPython Documentation',
255 256 u"""The IPython Development Team""", 'manual', True),
256 257 ('parallel/winhpc_index', 'winhpc_whitepaper.tex',
257 258 'Using IPython on Windows HPC Server 2008',
258 259 u"Brian E. Granger", 'manual', True)
259 260 ]
260 261
261 262 # The name of an image file (relative to this directory) to place at the top of
262 263 # the title page.
263 264 #latex_logo = None
264 265
265 266 # For "manual" documents, if this is true, then toplevel headings are parts,
266 267 # not chapters.
267 268 #latex_use_parts = False
268 269
269 270 # Additional stuff for the LaTeX preamble.
270 271 #latex_preamble = ''
271 272
272 273 # Documents to append as an appendix to all manuals.
273 274 #latex_appendices = []
274 275
275 276 # If false, no module index is generated.
276 277 latex_use_modindex = True
277 278
278 279
279 280 # Options for texinfo output
280 281 # --------------------------
281 282
282 283 texinfo_documents = [
283 284 (master_doc, 'ipython', 'IPython Documentation',
284 285 'The IPython Development Team',
285 286 'IPython',
286 287 'IPython Documentation',
287 288 'Programming',
288 289 1),
289 290 ]
290 291
291 292 modindex_common_prefix = ['IPython.']
292 293
293 294
294 295 # Cleanup
295 296 # -------
296 297 # delete release info to avoid pickling errors from sphinx
297 298
298 299 del iprelease
@@ -1,31 +1,32
1 1 ========
2 2 Tutorial
3 3 ========
4 4
5 5 This section of IPython documentation will walk you through most of the IPython
6 6 functionality. You do not need to have any deep knowledge of Python to read this
7 7 tutorial, though some sections might make slightly more sense if you have already
8 8 done some work in the classic Python REPL.
9 9
10 10 .. note::
11 11
12 12 Some part of this documentation are more than a decade old so might be out
13 13 of date, we welcome any report of inaccuracy, and Pull Requests that make
14 14 that up to date.
15 15
16 16 .. toctree::
17 17 :maxdepth: 2
18 18 :hidden:
19 19
20 20 tutorial
21 21 plotting
22 22 reference
23 23 shell
24 autoawait
24 25 tips
25 26 python-ipython-diff
26 27 magics
27 28
28 29 .. seealso::
29 30
30 31 `A Qt Console for Jupyter <https://jupyter.org/qtconsole/>`__
31 32 `The Jupyter Notebook <http://jupyter-notebook.readthedocs.io/en/latest/>`__
@@ -1,21 +1,150
1 1 =====================
2 2 Development version
3 3 =====================
4 4
5 5 This document describes in-flight development work.
6 6
7 7 .. warning::
8 8
9 9 Please do not edit this file by hand (doing so will likely cause merge
10 10 conflicts for other Pull Requests). Instead, create a new file in the
11 11 `docs/source/whatsnew/pr` folder
12 12
13 13
14 Released .... ...., 2017
15
16
17 Need to be updated:
18
19 .. toctree::
20 :maxdepth: 2
21 :glob:
22
23 pr/*
24
25 IPython 6 feature a major improvement in the completion machinery which is now
26 capable of completing non-executed code. It is also the first version of IPython
27 to stop compatibility with Python 2, which is still supported on the bugfix only
28 5.x branch. Read below to have a non-exhaustive list of new features.
29
30 Make sure you have pip > 9.0 before upgrading.
31 You should be able to update by using:
32
33 .. code::
34
35 pip install ipython --upgrade
36
37 New completion API and Interface
38 --------------------------------
39
40 The completer Completion API has seen an overhaul, and the new completer have
41 plenty of improvement both from the end users of terminal IPython or for
42 consumers of the API.
43
44 This new API is capable of pulling completions from :any:`jedi`, thus allowing
45 type inference on non-executed code. If :any:`jedi` is installed completion like
46 the following are now becoming possible without code evaluation:
47
48 >>> data = ['Number of users', 123_456]
49 ... data[0].<tab>
50
51 That is to say, IPython is now capable of inferring that `data[0]` is a string,
52 and will suggest completions like `.capitalize`. The completion power of IPython
53 will increase with new Jedi releases, and a number of bugs and more completions
54 are already available on development version of :any:`jedi` if you are curious.
55
56 With the help of prompt toolkit, types of completions can be shown in the
57 completer interface:
58
59 .. image:: ../_images/jedi_type_inference_60.png
60 :alt: Jedi showing ability to do type inference
61 :align: center
62 :width: 400px
63 :target: ../_images/jedi_type_inference_60.png
64
65 The appearance of the completer is controlled by the
66 ``c.TerminalInteractiveShell.display_completions`` option that will show the
67 type differently depending on the value among ``'column'``, ``'multicolumn'``
68 and ``'readlinelike'``
69
70 The use of Jedi also full fill a number of request and fix a number of bugs
71 like case insensitive completion, completion after division operator: See
72 :ghpull:`10182`.
73
74 Extra patches and updates will be needed to the :mod:`ipykernel` package for
75 this feature to be available to other clients like jupyter Notebook, Lab,
76 Nteract, Hydrogen...
77
78 The use of Jedi can is barely noticeable on recent enough machines, but can be
79 feel on older ones, in cases were Jedi behavior need to be adjusted, the amount
80 of time given to Jedi to compute type inference can be adjusted with
81 ``c.IPCompleter.jedi_compute_type_timeout``, with object whose type were not
82 inferred will be shown as ``<unknown>``. Jedi can also be completely deactivated
83 by using the ``c.Completer.use_jedi=False`` option.
84
85
86 The old ``Completer.complete()`` API is waiting deprecation and should be
87 replaced replaced by ``Completer.completions()`` in a near future. Feedback on
88 the current state of the API and suggestions welcome.
89
90 Python 3 only codebase
91 ----------------------
92
93 One of the large challenges in IPython 6.0 has been the adoption of a pure
94 Python 3 code base, which lead us to great length to upstream patches in pip,
95 pypi and warehouse to make sure Python 2 system still upgrade to the latest
96 compatible Python version compatible.
97
98 We remind our Python 2 users that IPython 5 is still compatible with Python 2.7,
99 still maintained and get regular releases. Using pip 9+, upgrading IPython will
100 automatically upgrade to the latest version compatible with your system.
101
102 .. warning::
103
104 If you are on a system using an older verison of pip on Python 2, pip may
105 still install IPython 6.0 on your system, and IPython will refuse to start.
106 You can fix this by ugrading pip, and reinstalling ipython, or forcing pip to
107 install an earlier version: ``pip install 'ipython<6'``
108
109 The ability to use only Python 3 on the code base of IPython has bring a number
110 of advantage. Most of the newly written code make use of `optional function type
111 anotation <https://www.python.org/dev/peps/pep-0484/>`_ leading to clearer code
112 and better documentation.
113
114 The total size of the repository has also for a first time between releases
115 (excluding the big split for 4.0) decreased by about 1500 lines, potentially
116 quite a bit more codewide as some documents like this one are append only and
117 are about 300 lines long.
118
119 The removal as of Python2/Python3 shim layer has made the code quite clearer and
120 more idiomatic in a number of location, and much friendlier to work with and
121 understand. We hope to further embrace Python 3 capability in the next release
122 cycle and introduce more of the Python 3 only idioms (yield from, kwarg only,
123 general unpacking) in the code base of IPython, and see if we can take advantage
124 of these as well to improve user experience with better error messages and
125 hints.
126
127
128 Miscs improvements
129 ------------------
130
131
132 - The :cellmagic:`capture` magic can now capture the result of a cell (from an
133 expression on the last line), as well as printed and displayed output.
134 :ghpull:`9851`.
135
136 - Pressing Ctrl-Z in the terminal debugger now suspends IPython, as it already
137 does in the main terminal prompt.
138
139 - autoreload can now reload ``Enum``. See :ghissue:`10232` and :ghpull:`10316`
140
141 - IPython.display has gained a :any:`GeoJSON <IPython.display.GeoJSON>` object.
142 :ghpull:`10288` and :ghpull:`10253`
14 143
15 144 .. DO NOT EDIT THIS LINE BEFORE RELEASE. FEATURE INSERTION POINT.
16 145
17 146
18 147 Backwards incompatible changes
19 148 ------------------------------
20 149
21 150 .. DO NOT EDIT THIS LINE BEFORE RELEASE. INCOMPAT INSERTION POINT.
@@ -1,264 +1,265
1 1 #!/usr/bin/env python3
2 2 # -*- coding: utf-8 -*-
3 3 """Setup script for IPython.
4 4
5 5 Under Posix environments it works like a typical setup.py script.
6 6 Under Windows, the command sdist is not supported, since IPython
7 7 requires utilities which are not available under Windows."""
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (c) 2008-2011, IPython Development Team.
11 11 # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
12 12 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
13 13 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
14 14 #
15 15 # Distributed under the terms of the Modified BSD License.
16 16 #
17 17 # The full license is in the file COPYING.rst, distributed with this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 from __future__ import print_function
21 21
22 22 import os
23 23 import sys
24 24
25 25 # **Python version check**
26 26 #
27 27 # This check is also made in IPython/__init__, don't forget to update both when
28 28 # changing Python version requirements.
29 29 if sys.version_info < (3, 4):
30 30 pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.'
31 31 try:
32 32 import pip
33 33 pip_version = tuple([int(x) for x in pip.__version__.split('.')[:3]])
34 34 if pip_version < (9, 0, 1) :
35 35 pip_message = 'Your pip version is out of date, please install pip >= 9.0.1. '\
36 36 'pip {} detected.'.format(pip.__version__)
37 37 else:
38 38 # pip is new enough - it must be something else
39 39 pip_message = ''
40 40 except Exception:
41 41 pass
42 42
43 43
44 44 error = """
45 45 IPython 7.0+ supports Python 3.4 and above.
46 46 When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
47 47 Python 3.3 was supported up to IPython 6.x.
48 48
49 49 See IPython `README.rst` file for more information:
50 50
51 51 https://github.com/ipython/ipython/blob/master/README.rst
52 52
53 53 Python {py} detected.
54 54 {pip}
55 55 """.format(py=sys.version_info, pip=pip_message )
56 56
57 57 print(error, file=sys.stderr)
58 58 sys.exit(1)
59 59
60 60 # At least we're on the python version we need, move on.
61 61
62 62 # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
63 63 # update it when the contents of directories change.
64 64 if os.path.exists('MANIFEST'): os.remove('MANIFEST')
65 65
66 66 from distutils.core import setup
67 67
68 68 # Our own imports
69 69 from setupbase import target_update
70 70
71 71 from setupbase import (
72 72 setup_args,
73 73 find_packages,
74 74 find_package_data,
75 75 check_package_data_first,
76 76 find_entry_points,
77 77 build_scripts_entrypt,
78 78 find_data_files,
79 79 git_prebuild,
80 80 install_symlinked,
81 81 install_lib_symlink,
82 82 install_scripts_for_symlink,
83 83 unsymlink,
84 84 )
85 85
86 86 isfile = os.path.isfile
87 87 pjoin = os.path.join
88 88
89 89 #-------------------------------------------------------------------------------
90 90 # Handle OS specific things
91 91 #-------------------------------------------------------------------------------
92 92
93 93 if os.name in ('nt','dos'):
94 94 os_name = 'windows'
95 95 else:
96 96 os_name = os.name
97 97
98 98 # Under Windows, 'sdist' has not been supported. Now that the docs build with
99 99 # Sphinx it might work, but let's not turn it on until someone confirms that it
100 100 # actually works.
101 101 if os_name == 'windows' and 'sdist' in sys.argv:
102 102 print('The sdist command is not available under Windows. Exiting.')
103 103 sys.exit(1)
104 104
105 105
106 106 #-------------------------------------------------------------------------------
107 107 # Things related to the IPython documentation
108 108 #-------------------------------------------------------------------------------
109 109
110 110 # update the manuals when building a source dist
111 111 if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'):
112 112
113 113 # List of things to be updated. Each entry is a triplet of args for
114 114 # target_update()
115 115 to_update = [
116 116 ('docs/man/ipython.1.gz',
117 117 ['docs/man/ipython.1'],
118 118 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz'),
119 119 ]
120 120
121 121
122 122 [ target_update(*t) for t in to_update ]
123 123
124 124 #---------------------------------------------------------------------------
125 125 # Find all the packages, package data, and data_files
126 126 #---------------------------------------------------------------------------
127 127
128 128 packages = find_packages()
129 129 package_data = find_package_data()
130 130
131 131 data_files = find_data_files()
132 132
133 133 setup_args['packages'] = packages
134 134 setup_args['package_data'] = package_data
135 135 setup_args['data_files'] = data_files
136 136
137 137 #---------------------------------------------------------------------------
138 138 # custom distutils commands
139 139 #---------------------------------------------------------------------------
140 140 # imports here, so they are after setuptools import if there was one
141 141 from distutils.command.sdist import sdist
142 142
143 143 setup_args['cmdclass'] = {
144 144 'build_py': \
145 145 check_package_data_first(git_prebuild('IPython')),
146 146 'sdist' : git_prebuild('IPython', sdist),
147 147 'symlink': install_symlinked,
148 148 'install_lib_symlink': install_lib_symlink,
149 149 'install_scripts_sym': install_scripts_for_symlink,
150 150 'unsymlink': unsymlink,
151 151 }
152 152
153 153
154 154 #---------------------------------------------------------------------------
155 155 # Handle scripts, dependencies, and setuptools specific things
156 156 #---------------------------------------------------------------------------
157 157
158 158 # For some commands, use setuptools. Note that we do NOT list install here!
159 159 # If you want a setuptools-enhanced install, just run 'setupegg.py install'
160 160 needs_setuptools = {'develop', 'release', 'bdist_egg', 'bdist_rpm',
161 161 'bdist', 'bdist_dumb', 'bdist_wininst', 'bdist_wheel',
162 162 'egg_info', 'easy_install', 'upload', 'install_egg_info',
163 163 }
164 164
165 165 if len(needs_setuptools.intersection(sys.argv)) > 0:
166 166 import setuptools
167 167
168 168 # This dict is used for passing extra arguments that are setuptools
169 169 # specific to setup
170 170 setuptools_extra_args = {}
171 171
172 172 # setuptools requirements
173 173
174 174 extras_require = dict(
175 175 parallel = ['ipyparallel'],
176 176 qtconsole = ['qtconsole'],
177 177 doc = ['Sphinx>=1.3'],
178 178 test = ['nose>=0.10.1', 'requests', 'testpath', 'pygments', 'nbformat', 'ipykernel', 'numpy'],
179 179 terminal = [],
180 180 kernel = ['ipykernel'],
181 181 nbformat = ['nbformat'],
182 182 notebook = ['notebook', 'ipywidgets'],
183 183 nbconvert = ['nbconvert'],
184 184 )
185 185
186 186 install_requires = [
187 187 'setuptools>=18.5',
188 188 'jedi>=0.10',
189 189 'decorator',
190 190 'pickleshare',
191 191 'simplegeneric>0.8',
192 192 'traitlets>=4.2',
193 193 'prompt_toolkit>=2.0.0,<2.1.0',
194 194 'pygments',
195 195 'backcall',
196 196 ]
197 197
198 198 # Platform-specific dependencies:
199 199 # This is the correct way to specify these,
200 200 # but requires pip >= 6. pip < 6 ignores these.
201 201
202 202 extras_require.update({
203 203 ':python_version == "3.4"': ['typing'],
204 ':python_version >= "3.5"': ['trio', 'curio'],
204 205 ':sys_platform != "win32"': ['pexpect'],
205 206 ':sys_platform == "darwin"': ['appnope'],
206 207 ':sys_platform == "win32"': ['colorama'],
207 208 ':sys_platform == "win32" and python_version < "3.6"': ['win_unicode_console>=0.5'],
208 209 })
209 210 # FIXME: re-specify above platform dependencies for pip < 6
210 211 # These would result in non-portable bdists.
211 212 if not any(arg.startswith('bdist') for arg in sys.argv):
212 213 if sys.platform == 'darwin':
213 214 install_requires.extend(['appnope'])
214 215
215 216 if not sys.platform.startswith('win'):
216 217 install_requires.append('pexpect')
217 218
218 219 # workaround pypa/setuptools#147, where setuptools misspells
219 220 # platform_python_implementation as python_implementation
220 221 if 'setuptools' in sys.modules:
221 222 for key in list(extras_require):
222 223 if 'platform_python_implementation' in key:
223 224 new_key = key.replace('platform_python_implementation', 'python_implementation')
224 225 extras_require[new_key] = extras_require.pop(key)
225 226
226 227 everything = set()
227 228 for key, deps in extras_require.items():
228 229 if ':' not in key:
229 230 everything.update(deps)
230 231 extras_require['all'] = everything
231 232
232 233 if 'setuptools' in sys.modules:
233 234 setuptools_extra_args['python_requires'] = '>=3.4'
234 235 setuptools_extra_args['zip_safe'] = False
235 236 setuptools_extra_args['entry_points'] = {
236 237 'console_scripts': find_entry_points(),
237 238 'pygments.lexers': [
238 239 'ipythonconsole = IPython.lib.lexers:IPythonConsoleLexer',
239 240 'ipython = IPython.lib.lexers:IPythonLexer',
240 241 'ipython3 = IPython.lib.lexers:IPython3Lexer',
241 242 ],
242 243 }
243 244 setup_args['extras_require'] = extras_require
244 245 setup_args['install_requires'] = install_requires
245 246
246 247 else:
247 248 # scripts has to be a non-empty list, or install_scripts isn't called
248 249 setup_args['scripts'] = [e.split('=')[0].strip() for e in find_entry_points()]
249 250
250 251 setup_args['cmdclass']['build_scripts'] = build_scripts_entrypt
251 252
252 253 #---------------------------------------------------------------------------
253 254 # Do the actual setup now
254 255 #---------------------------------------------------------------------------
255 256
256 257 setup_args.update(setuptools_extra_args)
257 258
258 259
259 260
260 261 def main():
261 262 setup(**setup_args)
262 263
263 264 if __name__ == '__main__':
264 265 main()
General Comments 0
You need to be logged in to leave comments. Login now