Show More
@@ -0,0 +1,3 b'' | |||||
|
1 | The `--deep-reload` flag and the corresponding options to inject `dreload` or | |||
|
2 | `reload` into the interactive namespace have been removed. You have to | |||
|
3 | explicitly import `reload` from `IPython.lib.deepreload` to use it. |
@@ -0,0 +1,2 b'' | |||||
|
1 | IPython.utils.warn was deprecated in IPython 4.0, and has now been removed. | |||
|
2 | instead of Ipython.utils.warn inbuilt warning module is used. |
@@ -1,146 +1,152 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | IPython: tools for interactive and parallel computing in Python. |
|
3 | IPython: tools for interactive and parallel computing in Python. | |
4 |
|
4 | |||
5 | http://ipython.org |
|
5 | http://ipython.org | |
6 | """ |
|
6 | """ | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2008-2011, IPython Development Team. |
|
8 | # Copyright (c) 2008-2011, IPython Development Team. | |
9 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> |
|
9 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> | |
10 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> |
|
10 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> | |
11 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> |
|
11 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | from __future__ import absolute_import |
|
21 | from __future__ import absolute_import | |
22 |
|
22 | |||
23 | import os |
|
23 | import os | |
24 | import sys |
|
24 | import sys | |
25 | import warnings |
|
|||
26 |
|
25 | |||
27 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
28 | # Setup everything |
|
27 | # Setup everything | |
29 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
30 |
|
29 | |||
31 | # Don't forget to also update setup.py when this changes! |
|
30 | # Don't forget to also update setup.py when this changes! | |
32 |
|
|
31 | if sys.version_info < (3,3): | |
33 | if v[:2] < (3,3): |
|
32 | raise ImportError( | |
34 | raise ImportError('IPython requires Python version 3.3 or above.') |
|
33 | """ | |
35 | del v |
|
34 | IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2. | |
|
35 | When using Python 2.7, please install IPython 5.x LTS Long Term Support version. | |||
|
36 | Beginning with IPython 6.0, Python 3.3 and above is required. | |||
|
37 | ||||
|
38 | See IPython `README.rst` file for more information: | |||
|
39 | ||||
|
40 | https://github.com/ipython/ipython/blob/master/README.rst | |||
|
41 | ||||
|
42 | """) | |||
36 |
|
43 | |||
37 | # Make it easy to import extensions - they are always directly on pythonpath. |
|
44 | # Make it easy to import extensions - they are always directly on pythonpath. | |
38 | # Therefore, non-IPython modules can be added to extensions directory. |
|
45 | # Therefore, non-IPython modules can be added to extensions directory. | |
39 | # This should probably be in ipapp.py. |
|
46 | # This should probably be in ipapp.py. | |
40 | sys.path.append(os.path.join(os.path.dirname(__file__), "extensions")) |
|
47 | sys.path.append(os.path.join(os.path.dirname(__file__), "extensions")) | |
41 |
|
48 | |||
42 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
43 | # Setup the top level names |
|
50 | # Setup the top level names | |
44 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
45 |
|
52 | |||
46 | from .core.getipython import get_ipython |
|
53 | from .core.getipython import get_ipython | |
47 | from .core import release |
|
54 | from .core import release | |
48 | from .core.application import Application |
|
55 | from .core.application import Application | |
49 | from .terminal.embed import embed |
|
56 | from .terminal.embed import embed | |
50 |
|
57 | |||
51 | from .core.interactiveshell import InteractiveShell |
|
58 | from .core.interactiveshell import InteractiveShell | |
52 | from .testing import test |
|
59 | from .testing import test | |
53 | from .utils.sysinfo import sys_info |
|
60 | from .utils.sysinfo import sys_info | |
54 | from .utils.frame import extract_module_locals |
|
61 | from .utils.frame import extract_module_locals | |
55 |
|
62 | |||
56 | # Release data |
|
63 | # Release data | |
57 | __author__ = '%s <%s>' % (release.author, release.author_email) |
|
64 | __author__ = '%s <%s>' % (release.author, release.author_email) | |
58 | __license__ = release.license |
|
65 | __license__ = release.license | |
59 | __version__ = release.version |
|
66 | __version__ = release.version | |
60 | version_info = release.version_info |
|
67 | version_info = release.version_info | |
61 |
|
68 | |||
62 | def embed_kernel(module=None, local_ns=None, **kwargs): |
|
69 | def embed_kernel(module=None, local_ns=None, **kwargs): | |
63 | """Embed and start an IPython kernel in a given scope. |
|
70 | """Embed and start an IPython kernel in a given scope. | |
64 |
|
71 | |||
65 | If you don't want the kernel to initialize the namespace |
|
72 | If you don't want the kernel to initialize the namespace | |
66 | from the scope of the surrounding function, |
|
73 | from the scope of the surrounding function, | |
67 | and/or you want to load full IPython configuration, |
|
74 | and/or you want to load full IPython configuration, | |
68 | you probably want `IPython.start_kernel()` instead. |
|
75 | you probably want `IPython.start_kernel()` instead. | |
69 |
|
76 | |||
70 | Parameters |
|
77 | Parameters | |
71 | ---------- |
|
78 | ---------- | |
72 | module : ModuleType, optional |
|
79 | module : ModuleType, optional | |
73 | The module to load into IPython globals (default: caller) |
|
80 | The module to load into IPython globals (default: caller) | |
74 | local_ns : dict, optional |
|
81 | local_ns : dict, optional | |
75 | The namespace to load into IPython user namespace (default: caller) |
|
82 | The namespace to load into IPython user namespace (default: caller) | |
76 |
|
83 | |||
77 | kwargs : various, optional |
|
84 | kwargs : various, optional | |
78 | Further keyword args are relayed to the IPKernelApp constructor, |
|
85 | Further keyword args are relayed to the IPKernelApp constructor, | |
79 | allowing configuration of the Kernel. Will only have an effect |
|
86 | allowing configuration of the Kernel. Will only have an effect | |
80 | on the first embed_kernel call for a given process. |
|
87 | on the first embed_kernel call for a given process. | |
81 | """ |
|
88 | """ | |
82 |
|
89 | |||
83 | (caller_module, caller_locals) = extract_module_locals(1) |
|
90 | (caller_module, caller_locals) = extract_module_locals(1) | |
84 | if module is None: |
|
91 | if module is None: | |
85 | module = caller_module |
|
92 | module = caller_module | |
86 | if local_ns is None: |
|
93 | if local_ns is None: | |
87 | local_ns = caller_locals |
|
94 | local_ns = caller_locals | |
88 |
|
95 | |||
89 | # Only import .zmq when we really need it |
|
96 | # Only import .zmq when we really need it | |
90 | from ipykernel.embed import embed_kernel as real_embed_kernel |
|
97 | from ipykernel.embed import embed_kernel as real_embed_kernel | |
91 | real_embed_kernel(module=module, local_ns=local_ns, **kwargs) |
|
98 | real_embed_kernel(module=module, local_ns=local_ns, **kwargs) | |
92 |
|
99 | |||
93 | def start_ipython(argv=None, **kwargs): |
|
100 | def start_ipython(argv=None, **kwargs): | |
94 | """Launch a normal IPython instance (as opposed to embedded) |
|
101 | """Launch a normal IPython instance (as opposed to embedded) | |
95 |
|
102 | |||
96 | `IPython.embed()` puts a shell in a particular calling scope, |
|
103 | `IPython.embed()` puts a shell in a particular calling scope, | |
97 | such as a function or method for debugging purposes, |
|
104 | such as a function or method for debugging purposes, | |
98 | which is often not desirable. |
|
105 | which is often not desirable. | |
99 |
|
106 | |||
100 | `start_ipython()` does full, regular IPython initialization, |
|
107 | `start_ipython()` does full, regular IPython initialization, | |
101 | including loading startup files, configuration, etc. |
|
108 | including loading startup files, configuration, etc. | |
102 | much of which is skipped by `embed()`. |
|
109 | much of which is skipped by `embed()`. | |
103 |
|
110 | |||
104 | This is a public API method, and will survive implementation changes. |
|
111 | This is a public API method, and will survive implementation changes. | |
105 |
|
112 | |||
106 | Parameters |
|
113 | Parameters | |
107 | ---------- |
|
114 | ---------- | |
108 |
|
115 | |||
109 | argv : list or None, optional |
|
116 | argv : list or None, optional | |
110 | If unspecified or None, IPython will parse command-line options from sys.argv. |
|
117 | If unspecified or None, IPython will parse command-line options from sys.argv. | |
111 | To prevent any command-line parsing, pass an empty list: `argv=[]`. |
|
118 | To prevent any command-line parsing, pass an empty list: `argv=[]`. | |
112 | user_ns : dict, optional |
|
119 | user_ns : dict, optional | |
113 | specify this dictionary to initialize the IPython user namespace with particular values. |
|
120 | specify this dictionary to initialize the IPython user namespace with particular values. | |
114 | kwargs : various, optional |
|
121 | kwargs : various, optional | |
115 | Any other kwargs will be passed to the Application constructor, |
|
122 | Any other kwargs will be passed to the Application constructor, | |
116 | such as `config`. |
|
123 | such as `config`. | |
117 | """ |
|
124 | """ | |
118 | from IPython.terminal.ipapp import launch_new_instance |
|
125 | from IPython.terminal.ipapp import launch_new_instance | |
119 | return launch_new_instance(argv=argv, **kwargs) |
|
126 | return launch_new_instance(argv=argv, **kwargs) | |
120 |
|
127 | |||
121 | def start_kernel(argv=None, **kwargs): |
|
128 | def start_kernel(argv=None, **kwargs): | |
122 | """Launch a normal IPython kernel instance (as opposed to embedded) |
|
129 | """Launch a normal IPython kernel instance (as opposed to embedded) | |
123 |
|
130 | |||
124 | `IPython.embed_kernel()` puts a shell in a particular calling scope, |
|
131 | `IPython.embed_kernel()` puts a shell in a particular calling scope, | |
125 | such as a function or method for debugging purposes, |
|
132 | such as a function or method for debugging purposes, | |
126 | which is often not desirable. |
|
133 | which is often not desirable. | |
127 |
|
134 | |||
128 | `start_kernel()` does full, regular IPython initialization, |
|
135 | `start_kernel()` does full, regular IPython initialization, | |
129 | including loading startup files, configuration, etc. |
|
136 | including loading startup files, configuration, etc. | |
130 | much of which is skipped by `embed()`. |
|
137 | much of which is skipped by `embed()`. | |
131 |
|
138 | |||
132 | Parameters |
|
139 | Parameters | |
133 | ---------- |
|
140 | ---------- | |
134 |
|
141 | |||
135 | argv : list or None, optional |
|
142 | argv : list or None, optional | |
136 | If unspecified or None, IPython will parse command-line options from sys.argv. |
|
143 | If unspecified or None, IPython will parse command-line options from sys.argv. | |
137 | To prevent any command-line parsing, pass an empty list: `argv=[]`. |
|
144 | To prevent any command-line parsing, pass an empty list: `argv=[]`. | |
138 | user_ns : dict, optional |
|
145 | user_ns : dict, optional | |
139 | specify this dictionary to initialize the IPython user namespace with particular values. |
|
146 | specify this dictionary to initialize the IPython user namespace with particular values. | |
140 | kwargs : various, optional |
|
147 | kwargs : various, optional | |
141 | Any other kwargs will be passed to the Application constructor, |
|
148 | Any other kwargs will be passed to the Application constructor, | |
142 | such as `config`. |
|
149 | such as `config`. | |
143 | """ |
|
150 | """ | |
144 | from IPython.kernel.zmq.kernelapp import launch_new_instance |
|
151 | from IPython.kernel.zmq.kernelapp import launch_new_instance | |
145 | return launch_new_instance(argv=argv, **kwargs) |
|
152 | return launch_new_instance(argv=argv, **kwargs) | |
146 |
|
@@ -1,114 +1,103 b'' | |||||
1 | """ |
|
1 | """ | |
2 | A context manager for managing things injected into :mod:`__builtin__`. |
|
2 | A context manager for managing things injected into :mod:`__builtin__`. | |
3 |
|
3 | |||
4 | Authors: |
|
4 | Authors: | |
5 |
|
5 | |||
6 | * Brian Granger |
|
6 | * Brian Granger | |
7 | * Fernando Perez |
|
7 | * Fernando Perez | |
8 | """ |
|
8 | """ | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2010-2011 The IPython Development Team. |
|
10 | # Copyright (C) 2010-2011 The IPython Development Team. | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. |
|
12 | # Distributed under the terms of the BSD License. | |
13 | # |
|
13 | # | |
14 | # Complete license in the file COPYING.txt, distributed with this software. |
|
14 | # Complete license in the file COPYING.txt, distributed with this software. | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Imports |
|
18 | # Imports | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | from traitlets.config.configurable import Configurable |
|
21 | from traitlets.config.configurable import Configurable | |
22 |
|
22 | |||
23 | from IPython.utils.py3compat import builtin_mod, iteritems |
|
23 | from IPython.utils.py3compat import builtin_mod, iteritems | |
24 | from traitlets import Instance |
|
24 | from traitlets import Instance | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Classes and functions |
|
27 | # Classes and functions | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | class __BuiltinUndefined(object): pass |
|
30 | class __BuiltinUndefined(object): pass | |
31 | BuiltinUndefined = __BuiltinUndefined() |
|
31 | BuiltinUndefined = __BuiltinUndefined() | |
32 |
|
32 | |||
33 | class __HideBuiltin(object): pass |
|
33 | class __HideBuiltin(object): pass | |
34 | HideBuiltin = __HideBuiltin() |
|
34 | HideBuiltin = __HideBuiltin() | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class BuiltinTrap(Configurable): |
|
37 | class BuiltinTrap(Configurable): | |
38 |
|
38 | |||
39 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
39 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', | |
40 | allow_none=True) |
|
40 | allow_none=True) | |
41 |
|
41 | |||
42 | def __init__(self, shell=None): |
|
42 | def __init__(self, shell=None): | |
43 | super(BuiltinTrap, self).__init__(shell=shell, config=None) |
|
43 | super(BuiltinTrap, self).__init__(shell=shell, config=None) | |
44 | self._orig_builtins = {} |
|
44 | self._orig_builtins = {} | |
45 | # We define this to track if a single BuiltinTrap is nested. |
|
45 | # We define this to track if a single BuiltinTrap is nested. | |
46 | # Only turn off the trap when the outermost call to __exit__ is made. |
|
46 | # Only turn off the trap when the outermost call to __exit__ is made. | |
47 | self._nested_level = 0 |
|
47 | self._nested_level = 0 | |
48 | self.shell = shell |
|
48 | self.shell = shell | |
49 | # builtins we always add - if set to HideBuiltin, they will just |
|
49 | # builtins we always add - if set to HideBuiltin, they will just | |
50 | # be removed instead of being replaced by something else |
|
50 | # be removed instead of being replaced by something else | |
51 | self.auto_builtins = {'exit': HideBuiltin, |
|
51 | self.auto_builtins = {'exit': HideBuiltin, | |
52 | 'quit': HideBuiltin, |
|
52 | 'quit': HideBuiltin, | |
53 | 'get_ipython': self.shell.get_ipython, |
|
53 | 'get_ipython': self.shell.get_ipython, | |
54 | } |
|
54 | } | |
55 | # Recursive reload function |
|
|||
56 | try: |
|
|||
57 | from IPython.lib import deepreload |
|
|||
58 | if self.shell.deep_reload: |
|
|||
59 | from warnings import warn |
|
|||
60 | warn("Automatically replacing builtin `reload` by `deepreload.reload` is deprecated since IPython 4.0, please import `reload` explicitly from `IPython.lib.deepreload", DeprecationWarning) |
|
|||
61 | self.auto_builtins['reload'] = deepreload._dreload |
|
|||
62 | else: |
|
|||
63 | self.auto_builtins['dreload']= deepreload._dreload |
|
|||
64 | except ImportError: |
|
|||
65 | pass |
|
|||
66 |
|
55 | |||
67 | def __enter__(self): |
|
56 | def __enter__(self): | |
68 | if self._nested_level == 0: |
|
57 | if self._nested_level == 0: | |
69 | self.activate() |
|
58 | self.activate() | |
70 | self._nested_level += 1 |
|
59 | self._nested_level += 1 | |
71 | # I return self, so callers can use add_builtin in a with clause. |
|
60 | # I return self, so callers can use add_builtin in a with clause. | |
72 | return self |
|
61 | return self | |
73 |
|
62 | |||
74 | def __exit__(self, type, value, traceback): |
|
63 | def __exit__(self, type, value, traceback): | |
75 | if self._nested_level == 1: |
|
64 | if self._nested_level == 1: | |
76 | self.deactivate() |
|
65 | self.deactivate() | |
77 | self._nested_level -= 1 |
|
66 | self._nested_level -= 1 | |
78 | # Returning False will cause exceptions to propagate |
|
67 | # Returning False will cause exceptions to propagate | |
79 | return False |
|
68 | return False | |
80 |
|
69 | |||
81 | def add_builtin(self, key, value): |
|
70 | def add_builtin(self, key, value): | |
82 | """Add a builtin and save the original.""" |
|
71 | """Add a builtin and save the original.""" | |
83 | bdict = builtin_mod.__dict__ |
|
72 | bdict = builtin_mod.__dict__ | |
84 | orig = bdict.get(key, BuiltinUndefined) |
|
73 | orig = bdict.get(key, BuiltinUndefined) | |
85 | if value is HideBuiltin: |
|
74 | if value is HideBuiltin: | |
86 | if orig is not BuiltinUndefined: #same as 'key in bdict' |
|
75 | if orig is not BuiltinUndefined: #same as 'key in bdict' | |
87 | self._orig_builtins[key] = orig |
|
76 | self._orig_builtins[key] = orig | |
88 | del bdict[key] |
|
77 | del bdict[key] | |
89 | else: |
|
78 | else: | |
90 | self._orig_builtins[key] = orig |
|
79 | self._orig_builtins[key] = orig | |
91 | bdict[key] = value |
|
80 | bdict[key] = value | |
92 |
|
81 | |||
93 | def remove_builtin(self, key, orig): |
|
82 | def remove_builtin(self, key, orig): | |
94 | """Remove an added builtin and re-set the original.""" |
|
83 | """Remove an added builtin and re-set the original.""" | |
95 | if orig is BuiltinUndefined: |
|
84 | if orig is BuiltinUndefined: | |
96 | del builtin_mod.__dict__[key] |
|
85 | del builtin_mod.__dict__[key] | |
97 | else: |
|
86 | else: | |
98 | builtin_mod.__dict__[key] = orig |
|
87 | builtin_mod.__dict__[key] = orig | |
99 |
|
88 | |||
100 | def activate(self): |
|
89 | def activate(self): | |
101 | """Store ipython references in the __builtin__ namespace.""" |
|
90 | """Store ipython references in the __builtin__ namespace.""" | |
102 |
|
91 | |||
103 | add_builtin = self.add_builtin |
|
92 | add_builtin = self.add_builtin | |
104 | for name, func in iteritems(self.auto_builtins): |
|
93 | for name, func in iteritems(self.auto_builtins): | |
105 | add_builtin(name, func) |
|
94 | add_builtin(name, func) | |
106 |
|
95 | |||
107 | def deactivate(self): |
|
96 | def deactivate(self): | |
108 | """Remove any builtins which might have been added by add_builtins, or |
|
97 | """Remove any builtins which might have been added by add_builtins, or | |
109 | restore overwritten ones to their previous values.""" |
|
98 | restore overwritten ones to their previous values.""" | |
110 | remove_builtin = self.remove_builtin |
|
99 | remove_builtin = self.remove_builtin | |
111 | for key, val in iteritems(self._orig_builtins): |
|
100 | for key, val in iteritems(self._orig_builtins): | |
112 | remove_builtin(key, val) |
|
101 | remove_builtin(key, val) | |
113 | self._orig_builtins.clear() |
|
102 | self._orig_builtins.clear() | |
114 | self._builtins_added = False |
|
103 | self._builtins_added = False |
@@ -1,3245 +1,3230 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Main IPython class.""" |
|
2 | """Main IPython class.""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | from __future__ import absolute_import, print_function |
|
13 | from __future__ import absolute_import, print_function | |
14 |
|
14 | |||
15 | import __future__ |
|
15 | import __future__ | |
16 | import abc |
|
16 | import abc | |
17 | import ast |
|
17 | import ast | |
18 | import atexit |
|
18 | import atexit | |
19 | import functools |
|
19 | import functools | |
20 | import os |
|
20 | import os | |
21 | import re |
|
21 | import re | |
22 | import runpy |
|
22 | import runpy | |
23 | import sys |
|
23 | import sys | |
24 | import tempfile |
|
24 | import tempfile | |
25 | import traceback |
|
25 | import traceback | |
26 | import types |
|
26 | import types | |
27 | import subprocess |
|
27 | import subprocess | |
28 | import warnings |
|
28 | import warnings | |
29 | from io import open as io_open |
|
29 | from io import open as io_open | |
30 |
|
30 | |||
31 | from pickleshare import PickleShareDB |
|
31 | from pickleshare import PickleShareDB | |
32 |
|
32 | |||
33 | from traitlets.config.configurable import SingletonConfigurable |
|
33 | from traitlets.config.configurable import SingletonConfigurable | |
34 | from IPython.core import oinspect |
|
34 | from IPython.core import oinspect | |
35 | from IPython.core import magic |
|
35 | from IPython.core import magic | |
36 | from IPython.core import page |
|
36 | from IPython.core import page | |
37 | from IPython.core import prefilter |
|
37 | from IPython.core import prefilter | |
38 | from IPython.core import shadowns |
|
38 | from IPython.core import shadowns | |
39 | from IPython.core import ultratb |
|
39 | from IPython.core import ultratb | |
40 | from IPython.core.alias import Alias, AliasManager |
|
40 | from IPython.core.alias import Alias, AliasManager | |
41 | from IPython.core.autocall import ExitAutocall |
|
41 | from IPython.core.autocall import ExitAutocall | |
42 | from IPython.core.builtin_trap import BuiltinTrap |
|
42 | from IPython.core.builtin_trap import BuiltinTrap | |
43 | from IPython.core.events import EventManager, available_events |
|
43 | from IPython.core.events import EventManager, available_events | |
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython | |
45 | from IPython.core.debugger import Pdb |
|
45 | from IPython.core.debugger import Pdb | |
46 | from IPython.core.display_trap import DisplayTrap |
|
46 | from IPython.core.display_trap import DisplayTrap | |
47 | from IPython.core.displayhook import DisplayHook |
|
47 | from IPython.core.displayhook import DisplayHook | |
48 | from IPython.core.displaypub import DisplayPublisher |
|
48 | from IPython.core.displaypub import DisplayPublisher | |
49 | from IPython.core.error import InputRejected, UsageError |
|
49 | from IPython.core.error import InputRejected, UsageError | |
50 | from IPython.core.extensions import ExtensionManager |
|
50 | from IPython.core.extensions import ExtensionManager | |
51 | from IPython.core.formatters import DisplayFormatter |
|
51 | from IPython.core.formatters import DisplayFormatter | |
52 | from IPython.core.history import HistoryManager |
|
52 | from IPython.core.history import HistoryManager | |
53 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 |
|
53 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 | |
54 | from IPython.core.logger import Logger |
|
54 | from IPython.core.logger import Logger | |
55 | from IPython.core.macro import Macro |
|
55 | from IPython.core.macro import Macro | |
56 | from IPython.core.payload import PayloadManager |
|
56 | from IPython.core.payload import PayloadManager | |
57 | from IPython.core.prefilter import PrefilterManager |
|
57 | from IPython.core.prefilter import PrefilterManager | |
58 | from IPython.core.profiledir import ProfileDir |
|
58 | from IPython.core.profiledir import ProfileDir | |
59 | from IPython.core.usage import default_banner |
|
59 | from IPython.core.usage import default_banner | |
60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest |
|
60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest | |
61 | from IPython.utils import PyColorize |
|
61 | from IPython.utils import PyColorize | |
62 | from IPython.utils import io |
|
62 | from IPython.utils import io | |
63 | from IPython.utils import py3compat |
|
63 | from IPython.utils import py3compat | |
64 | from IPython.utils import openpy |
|
64 | from IPython.utils import openpy | |
65 | from IPython.utils.decorators import undoc |
|
65 | from IPython.utils.decorators import undoc | |
66 | from IPython.utils.io import ask_yes_no |
|
66 | from IPython.utils.io import ask_yes_no | |
67 | from IPython.utils.ipstruct import Struct |
|
67 | from IPython.utils.ipstruct import Struct | |
68 | from IPython.paths import get_ipython_dir |
|
68 | from IPython.paths import get_ipython_dir | |
69 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists |
|
69 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists | |
70 | from IPython.utils.process import system, getoutput |
|
70 | from IPython.utils.process import system, getoutput | |
71 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, |
|
71 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, | |
72 | with_metaclass, iteritems) |
|
72 | with_metaclass, iteritems) | |
73 | from IPython.utils.strdispatch import StrDispatch |
|
73 | from IPython.utils.strdispatch import StrDispatch | |
74 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
74 | from IPython.utils.syspathcontext import prepended_to_syspath | |
75 | from IPython.utils.text import format_screen, LSString, SList, DollarFormatter |
|
75 | from IPython.utils.text import format_screen, LSString, SList, DollarFormatter | |
76 | from IPython.utils.tempdir import TemporaryDirectory |
|
76 | from IPython.utils.tempdir import TemporaryDirectory | |
77 | from traitlets import ( |
|
77 | from traitlets import ( | |
78 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, |
|
78 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, | |
79 | observe, default, |
|
79 | observe, default, | |
80 | ) |
|
80 | ) | |
81 | from warnings import warn |
|
81 | from warnings import warn | |
82 | from logging import error |
|
82 | from logging import error | |
83 | import IPython.core.hooks |
|
83 | import IPython.core.hooks | |
84 |
|
84 | |||
85 | # NoOpContext is deprecated, but ipykernel imports it from here. |
|
85 | # NoOpContext is deprecated, but ipykernel imports it from here. | |
86 | # See https://github.com/ipython/ipykernel/issues/157 |
|
86 | # See https://github.com/ipython/ipykernel/issues/157 | |
87 | from IPython.utils.contexts import NoOpContext |
|
87 | from IPython.utils.contexts import NoOpContext | |
88 |
|
88 | |||
89 | try: |
|
89 | try: | |
90 | import docrepr.sphinxify as sphx |
|
90 | import docrepr.sphinxify as sphx | |
91 |
|
91 | |||
92 | def sphinxify(doc): |
|
92 | def sphinxify(doc): | |
93 | with TemporaryDirectory() as dirname: |
|
93 | with TemporaryDirectory() as dirname: | |
94 | return { |
|
94 | return { | |
95 | 'text/html': sphx.sphinxify(doc, dirname), |
|
95 | 'text/html': sphx.sphinxify(doc, dirname), | |
96 | 'text/plain': doc |
|
96 | 'text/plain': doc | |
97 | } |
|
97 | } | |
98 | except ImportError: |
|
98 | except ImportError: | |
99 | sphinxify = None |
|
99 | sphinxify = None | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | class ProvisionalWarning(DeprecationWarning): |
|
102 | class ProvisionalWarning(DeprecationWarning): | |
103 | """ |
|
103 | """ | |
104 | Warning class for unstable features |
|
104 | Warning class for unstable features | |
105 | """ |
|
105 | """ | |
106 | pass |
|
106 | pass | |
107 |
|
107 | |||
108 | #----------------------------------------------------------------------------- |
|
108 | #----------------------------------------------------------------------------- | |
109 | # Globals |
|
109 | # Globals | |
110 | #----------------------------------------------------------------------------- |
|
110 | #----------------------------------------------------------------------------- | |
111 |
|
111 | |||
112 | # compiled regexps for autoindent management |
|
112 | # compiled regexps for autoindent management | |
113 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
113 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
114 |
|
114 | |||
115 | #----------------------------------------------------------------------------- |
|
115 | #----------------------------------------------------------------------------- | |
116 | # Utilities |
|
116 | # Utilities | |
117 | #----------------------------------------------------------------------------- |
|
117 | #----------------------------------------------------------------------------- | |
118 |
|
118 | |||
119 | @undoc |
|
119 | @undoc | |
120 | def softspace(file, newvalue): |
|
120 | def softspace(file, newvalue): | |
121 | """Copied from code.py, to remove the dependency""" |
|
121 | """Copied from code.py, to remove the dependency""" | |
122 |
|
122 | |||
123 | oldvalue = 0 |
|
123 | oldvalue = 0 | |
124 | try: |
|
124 | try: | |
125 | oldvalue = file.softspace |
|
125 | oldvalue = file.softspace | |
126 | except AttributeError: |
|
126 | except AttributeError: | |
127 | pass |
|
127 | pass | |
128 | try: |
|
128 | try: | |
129 | file.softspace = newvalue |
|
129 | file.softspace = newvalue | |
130 | except (AttributeError, TypeError): |
|
130 | except (AttributeError, TypeError): | |
131 | # "attribute-less object" or "read-only attributes" |
|
131 | # "attribute-less object" or "read-only attributes" | |
132 | pass |
|
132 | pass | |
133 | return oldvalue |
|
133 | return oldvalue | |
134 |
|
134 | |||
135 | @undoc |
|
135 | @undoc | |
136 | def no_op(*a, **kw): pass |
|
136 | def no_op(*a, **kw): pass | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | class SpaceInInput(Exception): pass |
|
139 | class SpaceInInput(Exception): pass | |
140 |
|
140 | |||
141 |
|
141 | |||
142 | def get_default_colors(): |
|
142 | def get_default_colors(): | |
143 | "DEPRECATED" |
|
143 | "DEPRECATED" | |
144 | warn('get_default_color is Deprecated, and is `Neutral` on all platforms.', |
|
144 | warn('get_default_color is Deprecated, and is `Neutral` on all platforms.', | |
145 | DeprecationWarning, stacklevel=2) |
|
145 | DeprecationWarning, stacklevel=2) | |
146 | return 'Neutral' |
|
146 | return 'Neutral' | |
147 |
|
147 | |||
148 |
|
148 | |||
149 | class SeparateUnicode(Unicode): |
|
149 | class SeparateUnicode(Unicode): | |
150 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
150 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | |
151 |
|
151 | |||
152 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
152 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | |
153 | """ |
|
153 | """ | |
154 |
|
154 | |||
155 | def validate(self, obj, value): |
|
155 | def validate(self, obj, value): | |
156 | if value == '0': value = '' |
|
156 | if value == '0': value = '' | |
157 | value = value.replace('\\n','\n') |
|
157 | value = value.replace('\\n','\n') | |
158 | return super(SeparateUnicode, self).validate(obj, value) |
|
158 | return super(SeparateUnicode, self).validate(obj, value) | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | @undoc |
|
161 | @undoc | |
162 | class DummyMod(object): |
|
162 | class DummyMod(object): | |
163 | """A dummy module used for IPython's interactive module when |
|
163 | """A dummy module used for IPython's interactive module when | |
164 | a namespace must be assigned to the module's __dict__.""" |
|
164 | a namespace must be assigned to the module's __dict__.""" | |
165 | pass |
|
165 | pass | |
166 |
|
166 | |||
167 |
|
167 | |||
168 | class ExecutionResult(object): |
|
168 | class ExecutionResult(object): | |
169 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
169 | """The result of a call to :meth:`InteractiveShell.run_cell` | |
170 |
|
170 | |||
171 | Stores information about what took place. |
|
171 | Stores information about what took place. | |
172 | """ |
|
172 | """ | |
173 | execution_count = None |
|
173 | execution_count = None | |
174 | error_before_exec = None |
|
174 | error_before_exec = None | |
175 | error_in_exec = None |
|
175 | error_in_exec = None | |
176 | result = None |
|
176 | result = None | |
177 |
|
177 | |||
178 | @property |
|
178 | @property | |
179 | def success(self): |
|
179 | def success(self): | |
180 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
180 | return (self.error_before_exec is None) and (self.error_in_exec is None) | |
181 |
|
181 | |||
182 | def raise_error(self): |
|
182 | def raise_error(self): | |
183 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
183 | """Reraises error if `success` is `False`, otherwise does nothing""" | |
184 | if self.error_before_exec is not None: |
|
184 | if self.error_before_exec is not None: | |
185 | raise self.error_before_exec |
|
185 | raise self.error_before_exec | |
186 | if self.error_in_exec is not None: |
|
186 | if self.error_in_exec is not None: | |
187 | raise self.error_in_exec |
|
187 | raise self.error_in_exec | |
188 |
|
188 | |||
189 | def __repr__(self): |
|
189 | def __repr__(self): | |
190 | if sys.version_info > (3,): |
|
190 | if sys.version_info > (3,): | |
191 | name = self.__class__.__qualname__ |
|
191 | name = self.__class__.__qualname__ | |
192 | else: |
|
192 | else: | |
193 | name = self.__class__.__name__ |
|
193 | name = self.__class__.__name__ | |
194 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\ |
|
194 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s result=%s>' %\ | |
195 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result)) |
|
195 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.result)) | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | class InteractiveShell(SingletonConfigurable): |
|
198 | class InteractiveShell(SingletonConfigurable): | |
199 | """An enhanced, interactive shell for Python.""" |
|
199 | """An enhanced, interactive shell for Python.""" | |
200 |
|
200 | |||
201 | _instance = None |
|
201 | _instance = None | |
202 |
|
202 | |||
203 | ast_transformers = List([], help= |
|
203 | ast_transformers = List([], help= | |
204 | """ |
|
204 | """ | |
205 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
205 | A list of ast.NodeTransformer subclass instances, which will be applied | |
206 | to user input before code is run. |
|
206 | to user input before code is run. | |
207 | """ |
|
207 | """ | |
208 | ).tag(config=True) |
|
208 | ).tag(config=True) | |
209 |
|
209 | |||
210 | autocall = Enum((0,1,2), default_value=0, help= |
|
210 | autocall = Enum((0,1,2), default_value=0, help= | |
211 | """ |
|
211 | """ | |
212 | Make IPython automatically call any callable object even if you didn't |
|
212 | Make IPython automatically call any callable object even if you didn't | |
213 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
213 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
214 | automatically. The value can be '0' to disable the feature, '1' for |
|
214 | automatically. The value can be '0' to disable the feature, '1' for | |
215 | 'smart' autocall, where it is not applied if there are no more |
|
215 | 'smart' autocall, where it is not applied if there are no more | |
216 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
216 | arguments on the line, and '2' for 'full' autocall, where all callable | |
217 | objects are automatically called (even if no arguments are present). |
|
217 | objects are automatically called (even if no arguments are present). | |
218 | """ |
|
218 | """ | |
219 | ).tag(config=True) |
|
219 | ).tag(config=True) | |
220 | # TODO: remove all autoindent logic and put into frontends. |
|
220 | # TODO: remove all autoindent logic and put into frontends. | |
221 | # We can't do this yet because even runlines uses the autoindent. |
|
221 | # We can't do this yet because even runlines uses the autoindent. | |
222 | autoindent = Bool(True, help= |
|
222 | autoindent = Bool(True, help= | |
223 | """ |
|
223 | """ | |
224 | Autoindent IPython code entered interactively. |
|
224 | Autoindent IPython code entered interactively. | |
225 | """ |
|
225 | """ | |
226 | ).tag(config=True) |
|
226 | ).tag(config=True) | |
227 |
|
227 | |||
228 | automagic = Bool(True, help= |
|
228 | automagic = Bool(True, help= | |
229 | """ |
|
229 | """ | |
230 | Enable magic commands to be called without the leading %. |
|
230 | Enable magic commands to be called without the leading %. | |
231 | """ |
|
231 | """ | |
232 | ).tag(config=True) |
|
232 | ).tag(config=True) | |
233 |
|
233 | |||
234 | banner1 = Unicode(default_banner, |
|
234 | banner1 = Unicode(default_banner, | |
235 | help="""The part of the banner to be printed before the profile""" |
|
235 | help="""The part of the banner to be printed before the profile""" | |
236 | ).tag(config=True) |
|
236 | ).tag(config=True) | |
237 | banner2 = Unicode('', |
|
237 | banner2 = Unicode('', | |
238 | help="""The part of the banner to be printed after the profile""" |
|
238 | help="""The part of the banner to be printed after the profile""" | |
239 | ).tag(config=True) |
|
239 | ).tag(config=True) | |
240 |
|
240 | |||
241 | cache_size = Integer(1000, help= |
|
241 | cache_size = Integer(1000, help= | |
242 | """ |
|
242 | """ | |
243 | Set the size of the output cache. The default is 1000, you can |
|
243 | Set the size of the output cache. The default is 1000, you can | |
244 | change it permanently in your config file. Setting it to 0 completely |
|
244 | change it permanently in your config file. Setting it to 0 completely | |
245 | disables the caching system, and the minimum value accepted is 20 (if |
|
245 | disables the caching system, and the minimum value accepted is 20 (if | |
246 | you provide a value less than 20, it is reset to 0 and a warning is |
|
246 | you provide a value less than 20, it is reset to 0 and a warning is | |
247 | issued). This limit is defined because otherwise you'll spend more |
|
247 | issued). This limit is defined because otherwise you'll spend more | |
248 | time re-flushing a too small cache than working |
|
248 | time re-flushing a too small cache than working | |
249 | """ |
|
249 | """ | |
250 | ).tag(config=True) |
|
250 | ).tag(config=True) | |
251 | color_info = Bool(True, help= |
|
251 | color_info = Bool(True, help= | |
252 | """ |
|
252 | """ | |
253 | Use colors for displaying information about objects. Because this |
|
253 | Use colors for displaying information about objects. Because this | |
254 | information is passed through a pager (like 'less'), and some pagers |
|
254 | information is passed through a pager (like 'less'), and some pagers | |
255 | get confused with color codes, this capability can be turned off. |
|
255 | get confused with color codes, this capability can be turned off. | |
256 | """ |
|
256 | """ | |
257 | ).tag(config=True) |
|
257 | ).tag(config=True) | |
258 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), |
|
258 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), | |
259 | default_value='Neutral', |
|
259 | default_value='Neutral', | |
260 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." |
|
260 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." | |
261 | ).tag(config=True) |
|
261 | ).tag(config=True) | |
262 | debug = Bool(False).tag(config=True) |
|
262 | debug = Bool(False).tag(config=True) | |
263 | deep_reload = Bool(False, help= |
|
|||
264 | """ |
|
|||
265 | **Deprecated** |
|
|||
266 |
|
||||
267 | Will be removed in IPython 6.0 |
|
|||
268 |
|
||||
269 | Enable deep (recursive) reloading by default. IPython can use the |
|
|||
270 | deep_reload module which reloads changes in modules recursively (it |
|
|||
271 | replaces the reload() function, so you don't need to change anything to |
|
|||
272 | use it). `deep_reload` forces a full reload of modules whose code may |
|
|||
273 | have changed, which the default reload() function does not. When |
|
|||
274 | deep_reload is off, IPython will use the normal reload(), but |
|
|||
275 | deep_reload will still be available as dreload(). |
|
|||
276 | """ |
|
|||
277 | ).tag(config=True) |
|
|||
278 | disable_failing_post_execute = Bool(False, |
|
263 | disable_failing_post_execute = Bool(False, | |
279 | help="Don't call post-execute functions that have failed in the past." |
|
264 | help="Don't call post-execute functions that have failed in the past." | |
280 | ).tag(config=True) |
|
265 | ).tag(config=True) | |
281 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
266 | display_formatter = Instance(DisplayFormatter, allow_none=True) | |
282 | displayhook_class = Type(DisplayHook) |
|
267 | displayhook_class = Type(DisplayHook) | |
283 | display_pub_class = Type(DisplayPublisher) |
|
268 | display_pub_class = Type(DisplayPublisher) | |
284 |
|
269 | |||
285 | sphinxify_docstring = Bool(False, help= |
|
270 | sphinxify_docstring = Bool(False, help= | |
286 | """ |
|
271 | """ | |
287 | Enables rich html representation of docstrings. (This requires the |
|
272 | Enables rich html representation of docstrings. (This requires the | |
288 | docrepr module). |
|
273 | docrepr module). | |
289 | """).tag(config=True) |
|
274 | """).tag(config=True) | |
290 |
|
275 | |||
291 | @observe("sphinxify_docstring") |
|
276 | @observe("sphinxify_docstring") | |
292 | def _sphinxify_docstring_changed(self, change): |
|
277 | def _sphinxify_docstring_changed(self, change): | |
293 | if change['new']: |
|
278 | if change['new']: | |
294 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) |
|
279 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) | |
295 |
|
280 | |||
296 | enable_html_pager = Bool(False, help= |
|
281 | enable_html_pager = Bool(False, help= | |
297 | """ |
|
282 | """ | |
298 | (Provisional API) enables html representation in mime bundles sent |
|
283 | (Provisional API) enables html representation in mime bundles sent | |
299 | to pagers. |
|
284 | to pagers. | |
300 | """).tag(config=True) |
|
285 | """).tag(config=True) | |
301 |
|
286 | |||
302 | @observe("enable_html_pager") |
|
287 | @observe("enable_html_pager") | |
303 | def _enable_html_pager_changed(self, change): |
|
288 | def _enable_html_pager_changed(self, change): | |
304 | if change['new']: |
|
289 | if change['new']: | |
305 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) |
|
290 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) | |
306 |
|
291 | |||
307 | data_pub_class = None |
|
292 | data_pub_class = None | |
308 |
|
293 | |||
309 | exit_now = Bool(False) |
|
294 | exit_now = Bool(False) | |
310 | exiter = Instance(ExitAutocall) |
|
295 | exiter = Instance(ExitAutocall) | |
311 | @default('exiter') |
|
296 | @default('exiter') | |
312 | def _exiter_default(self): |
|
297 | def _exiter_default(self): | |
313 | return ExitAutocall(self) |
|
298 | return ExitAutocall(self) | |
314 | # Monotonically increasing execution counter |
|
299 | # Monotonically increasing execution counter | |
315 | execution_count = Integer(1) |
|
300 | execution_count = Integer(1) | |
316 | filename = Unicode("<ipython console>") |
|
301 | filename = Unicode("<ipython console>") | |
317 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
302 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
318 |
|
303 | |||
319 | # Input splitter, to transform input line by line and detect when a block |
|
304 | # Input splitter, to transform input line by line and detect when a block | |
320 | # is ready to be executed. |
|
305 | # is ready to be executed. | |
321 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
306 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
322 | (), {'line_input_checker': True}) |
|
307 | (), {'line_input_checker': True}) | |
323 |
|
308 | |||
324 | # This InputSplitter instance is used to transform completed cells before |
|
309 | # This InputSplitter instance is used to transform completed cells before | |
325 | # running them. It allows cell magics to contain blank lines. |
|
310 | # running them. It allows cell magics to contain blank lines. | |
326 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
311 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
327 | (), {'line_input_checker': False}) |
|
312 | (), {'line_input_checker': False}) | |
328 |
|
313 | |||
329 | logstart = Bool(False, help= |
|
314 | logstart = Bool(False, help= | |
330 | """ |
|
315 | """ | |
331 | Start logging to the default log file in overwrite mode. |
|
316 | Start logging to the default log file in overwrite mode. | |
332 | Use `logappend` to specify a log file to **append** logs to. |
|
317 | Use `logappend` to specify a log file to **append** logs to. | |
333 | """ |
|
318 | """ | |
334 | ).tag(config=True) |
|
319 | ).tag(config=True) | |
335 | logfile = Unicode('', help= |
|
320 | logfile = Unicode('', help= | |
336 | """ |
|
321 | """ | |
337 | The name of the logfile to use. |
|
322 | The name of the logfile to use. | |
338 | """ |
|
323 | """ | |
339 | ).tag(config=True) |
|
324 | ).tag(config=True) | |
340 | logappend = Unicode('', help= |
|
325 | logappend = Unicode('', help= | |
341 | """ |
|
326 | """ | |
342 | Start logging to the given file in append mode. |
|
327 | Start logging to the given file in append mode. | |
343 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
328 | Use `logfile` to specify a log file to **overwrite** logs to. | |
344 | """ |
|
329 | """ | |
345 | ).tag(config=True) |
|
330 | ).tag(config=True) | |
346 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
331 | object_info_string_level = Enum((0,1,2), default_value=0, | |
347 | ).tag(config=True) |
|
332 | ).tag(config=True) | |
348 | pdb = Bool(False, help= |
|
333 | pdb = Bool(False, help= | |
349 | """ |
|
334 | """ | |
350 | Automatically call the pdb debugger after every exception. |
|
335 | Automatically call the pdb debugger after every exception. | |
351 | """ |
|
336 | """ | |
352 | ).tag(config=True) |
|
337 | ).tag(config=True) | |
353 | display_page = Bool(False, |
|
338 | display_page = Bool(False, | |
354 | help="""If True, anything that would be passed to the pager |
|
339 | help="""If True, anything that would be passed to the pager | |
355 | will be displayed as regular output instead.""" |
|
340 | will be displayed as regular output instead.""" | |
356 | ).tag(config=True) |
|
341 | ).tag(config=True) | |
357 |
|
342 | |||
358 | # deprecated prompt traits: |
|
343 | # deprecated prompt traits: | |
359 |
|
344 | |||
360 | prompt_in1 = Unicode('In [\\#]: ', |
|
345 | prompt_in1 = Unicode('In [\\#]: ', | |
361 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
346 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." | |
362 | ).tag(config=True) |
|
347 | ).tag(config=True) | |
363 | prompt_in2 = Unicode(' .\\D.: ', |
|
348 | prompt_in2 = Unicode(' .\\D.: ', | |
364 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
349 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." | |
365 | ).tag(config=True) |
|
350 | ).tag(config=True) | |
366 | prompt_out = Unicode('Out[\\#]: ', |
|
351 | prompt_out = Unicode('Out[\\#]: ', | |
367 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
352 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." | |
368 | ).tag(config=True) |
|
353 | ).tag(config=True) | |
369 | prompts_pad_left = Bool(True, |
|
354 | prompts_pad_left = Bool(True, | |
370 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." |
|
355 | help="Deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly." | |
371 | ).tag(config=True) |
|
356 | ).tag(config=True) | |
372 |
|
357 | |||
373 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') |
|
358 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') | |
374 | def _prompt_trait_changed(self, change): |
|
359 | def _prompt_trait_changed(self, change): | |
375 | name = change['name'] |
|
360 | name = change['name'] | |
376 | warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format( |
|
361 | warn("InteractiveShell.{name} is deprecated since IPython 4.0 and ignored since 5.0, set TerminalInteractiveShell.prompts object directly.".format( | |
377 | name=name) |
|
362 | name=name) | |
378 | ) |
|
363 | ) | |
379 | # protect against weird cases where self.config may not exist: |
|
364 | # protect against weird cases where self.config may not exist: | |
380 |
|
365 | |||
381 | show_rewritten_input = Bool(True, |
|
366 | show_rewritten_input = Bool(True, | |
382 | help="Show rewritten input, e.g. for autocall." |
|
367 | help="Show rewritten input, e.g. for autocall." | |
383 | ).tag(config=True) |
|
368 | ).tag(config=True) | |
384 |
|
369 | |||
385 | quiet = Bool(False).tag(config=True) |
|
370 | quiet = Bool(False).tag(config=True) | |
386 |
|
371 | |||
387 | history_length = Integer(10000, |
|
372 | history_length = Integer(10000, | |
388 | help='Total length of command history' |
|
373 | help='Total length of command history' | |
389 | ).tag(config=True) |
|
374 | ).tag(config=True) | |
390 |
|
375 | |||
391 | history_load_length = Integer(1000, help= |
|
376 | history_load_length = Integer(1000, help= | |
392 | """ |
|
377 | """ | |
393 | The number of saved history entries to be loaded |
|
378 | The number of saved history entries to be loaded | |
394 | into the history buffer at startup. |
|
379 | into the history buffer at startup. | |
395 | """ |
|
380 | """ | |
396 | ).tag(config=True) |
|
381 | ).tag(config=True) | |
397 |
|
382 | |||
398 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
383 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
399 | default_value='last_expr', |
|
384 | default_value='last_expr', | |
400 | help=""" |
|
385 | help=""" | |
401 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
386 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
402 | run interactively (displaying output from expressions).""" |
|
387 | run interactively (displaying output from expressions).""" | |
403 | ).tag(config=True) |
|
388 | ).tag(config=True) | |
404 |
|
389 | |||
405 | # TODO: this part of prompt management should be moved to the frontends. |
|
390 | # TODO: this part of prompt management should be moved to the frontends. | |
406 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
391 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
407 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
392 | separate_in = SeparateUnicode('\n').tag(config=True) | |
408 | separate_out = SeparateUnicode('').tag(config=True) |
|
393 | separate_out = SeparateUnicode('').tag(config=True) | |
409 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
394 | separate_out2 = SeparateUnicode('').tag(config=True) | |
410 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
395 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
411 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
396 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
412 | default_value='Context').tag(config=True) |
|
397 | default_value='Context').tag(config=True) | |
413 |
|
398 | |||
414 | # Subcomponents of InteractiveShell |
|
399 | # Subcomponents of InteractiveShell | |
415 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
400 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) | |
416 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
401 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) | |
417 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
402 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) | |
418 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
403 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) | |
419 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
404 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) | |
420 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
405 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) | |
421 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
406 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) | |
422 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
407 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) | |
423 |
|
408 | |||
424 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
409 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) | |
425 | @property |
|
410 | @property | |
426 | def profile(self): |
|
411 | def profile(self): | |
427 | if self.profile_dir is not None: |
|
412 | if self.profile_dir is not None: | |
428 | name = os.path.basename(self.profile_dir.location) |
|
413 | name = os.path.basename(self.profile_dir.location) | |
429 | return name.replace('profile_','') |
|
414 | return name.replace('profile_','') | |
430 |
|
415 | |||
431 |
|
416 | |||
432 | # Private interface |
|
417 | # Private interface | |
433 | _post_execute = Dict() |
|
418 | _post_execute = Dict() | |
434 |
|
419 | |||
435 | # Tracks any GUI loop loaded for pylab |
|
420 | # Tracks any GUI loop loaded for pylab | |
436 | pylab_gui_select = None |
|
421 | pylab_gui_select = None | |
437 |
|
422 | |||
438 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') |
|
423 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') | |
439 |
|
424 | |||
440 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
425 | def __init__(self, ipython_dir=None, profile_dir=None, | |
441 | user_module=None, user_ns=None, |
|
426 | user_module=None, user_ns=None, | |
442 | custom_exceptions=((), None), **kwargs): |
|
427 | custom_exceptions=((), None), **kwargs): | |
443 |
|
428 | |||
444 | # This is where traits with a config_key argument are updated |
|
429 | # This is where traits with a config_key argument are updated | |
445 | # from the values on config. |
|
430 | # from the values on config. | |
446 | super(InteractiveShell, self).__init__(**kwargs) |
|
431 | super(InteractiveShell, self).__init__(**kwargs) | |
447 | if 'PromptManager' in self.config: |
|
432 | if 'PromptManager' in self.config: | |
448 | warn('As of IPython 5.0 `PromptManager` config will have no effect' |
|
433 | warn('As of IPython 5.0 `PromptManager` config will have no effect' | |
449 | ' and has been replaced by TerminalInteractiveShell.prompts_class') |
|
434 | ' and has been replaced by TerminalInteractiveShell.prompts_class') | |
450 | self.configurables = [self] |
|
435 | self.configurables = [self] | |
451 |
|
436 | |||
452 | # These are relatively independent and stateless |
|
437 | # These are relatively independent and stateless | |
453 | self.init_ipython_dir(ipython_dir) |
|
438 | self.init_ipython_dir(ipython_dir) | |
454 | self.init_profile_dir(profile_dir) |
|
439 | self.init_profile_dir(profile_dir) | |
455 | self.init_instance_attrs() |
|
440 | self.init_instance_attrs() | |
456 | self.init_environment() |
|
441 | self.init_environment() | |
457 |
|
442 | |||
458 | # Check if we're in a virtualenv, and set up sys.path. |
|
443 | # Check if we're in a virtualenv, and set up sys.path. | |
459 | self.init_virtualenv() |
|
444 | self.init_virtualenv() | |
460 |
|
445 | |||
461 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
446 | # Create namespaces (user_ns, user_global_ns, etc.) | |
462 | self.init_create_namespaces(user_module, user_ns) |
|
447 | self.init_create_namespaces(user_module, user_ns) | |
463 | # This has to be done after init_create_namespaces because it uses |
|
448 | # This has to be done after init_create_namespaces because it uses | |
464 | # something in self.user_ns, but before init_sys_modules, which |
|
449 | # something in self.user_ns, but before init_sys_modules, which | |
465 | # is the first thing to modify sys. |
|
450 | # is the first thing to modify sys. | |
466 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
451 | # TODO: When we override sys.stdout and sys.stderr before this class | |
467 | # is created, we are saving the overridden ones here. Not sure if this |
|
452 | # is created, we are saving the overridden ones here. Not sure if this | |
468 | # is what we want to do. |
|
453 | # is what we want to do. | |
469 | self.save_sys_module_state() |
|
454 | self.save_sys_module_state() | |
470 | self.init_sys_modules() |
|
455 | self.init_sys_modules() | |
471 |
|
456 | |||
472 | # While we're trying to have each part of the code directly access what |
|
457 | # While we're trying to have each part of the code directly access what | |
473 | # it needs without keeping redundant references to objects, we have too |
|
458 | # it needs without keeping redundant references to objects, we have too | |
474 | # much legacy code that expects ip.db to exist. |
|
459 | # much legacy code that expects ip.db to exist. | |
475 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
460 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
476 |
|
461 | |||
477 | self.init_history() |
|
462 | self.init_history() | |
478 | self.init_encoding() |
|
463 | self.init_encoding() | |
479 | self.init_prefilter() |
|
464 | self.init_prefilter() | |
480 |
|
465 | |||
481 | self.init_syntax_highlighting() |
|
466 | self.init_syntax_highlighting() | |
482 | self.init_hooks() |
|
467 | self.init_hooks() | |
483 | self.init_events() |
|
468 | self.init_events() | |
484 | self.init_pushd_popd_magic() |
|
469 | self.init_pushd_popd_magic() | |
485 | self.init_user_ns() |
|
470 | self.init_user_ns() | |
486 | self.init_logger() |
|
471 | self.init_logger() | |
487 | self.init_builtins() |
|
472 | self.init_builtins() | |
488 |
|
473 | |||
489 | # The following was in post_config_initialization |
|
474 | # The following was in post_config_initialization | |
490 | self.init_inspector() |
|
475 | self.init_inspector() | |
491 | if py3compat.PY3: |
|
476 | if py3compat.PY3: | |
492 | self.raw_input_original = input |
|
477 | self.raw_input_original = input | |
493 | else: |
|
478 | else: | |
494 | self.raw_input_original = raw_input |
|
479 | self.raw_input_original = raw_input | |
495 | self.init_completer() |
|
480 | self.init_completer() | |
496 | # TODO: init_io() needs to happen before init_traceback handlers |
|
481 | # TODO: init_io() needs to happen before init_traceback handlers | |
497 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
482 | # because the traceback handlers hardcode the stdout/stderr streams. | |
498 | # This logic in in debugger.Pdb and should eventually be changed. |
|
483 | # This logic in in debugger.Pdb and should eventually be changed. | |
499 | self.init_io() |
|
484 | self.init_io() | |
500 | self.init_traceback_handlers(custom_exceptions) |
|
485 | self.init_traceback_handlers(custom_exceptions) | |
501 | self.init_prompts() |
|
486 | self.init_prompts() | |
502 | self.init_display_formatter() |
|
487 | self.init_display_formatter() | |
503 | self.init_display_pub() |
|
488 | self.init_display_pub() | |
504 | self.init_data_pub() |
|
489 | self.init_data_pub() | |
505 | self.init_displayhook() |
|
490 | self.init_displayhook() | |
506 | self.init_magics() |
|
491 | self.init_magics() | |
507 | self.init_alias() |
|
492 | self.init_alias() | |
508 | self.init_logstart() |
|
493 | self.init_logstart() | |
509 | self.init_pdb() |
|
494 | self.init_pdb() | |
510 | self.init_extension_manager() |
|
495 | self.init_extension_manager() | |
511 | self.init_payload() |
|
496 | self.init_payload() | |
512 | self.init_deprecation_warnings() |
|
497 | self.init_deprecation_warnings() | |
513 | self.hooks.late_startup_hook() |
|
498 | self.hooks.late_startup_hook() | |
514 | self.events.trigger('shell_initialized', self) |
|
499 | self.events.trigger('shell_initialized', self) | |
515 | atexit.register(self.atexit_operations) |
|
500 | atexit.register(self.atexit_operations) | |
516 |
|
501 | |||
517 | def get_ipython(self): |
|
502 | def get_ipython(self): | |
518 | """Return the currently running IPython instance.""" |
|
503 | """Return the currently running IPython instance.""" | |
519 | return self |
|
504 | return self | |
520 |
|
505 | |||
521 | #------------------------------------------------------------------------- |
|
506 | #------------------------------------------------------------------------- | |
522 | # Trait changed handlers |
|
507 | # Trait changed handlers | |
523 | #------------------------------------------------------------------------- |
|
508 | #------------------------------------------------------------------------- | |
524 | @observe('ipython_dir') |
|
509 | @observe('ipython_dir') | |
525 | def _ipython_dir_changed(self, change): |
|
510 | def _ipython_dir_changed(self, change): | |
526 | ensure_dir_exists(change['new']) |
|
511 | ensure_dir_exists(change['new']) | |
527 |
|
512 | |||
528 | def set_autoindent(self,value=None): |
|
513 | def set_autoindent(self,value=None): | |
529 | """Set the autoindent flag. |
|
514 | """Set the autoindent flag. | |
530 |
|
515 | |||
531 | If called with no arguments, it acts as a toggle.""" |
|
516 | If called with no arguments, it acts as a toggle.""" | |
532 | if value is None: |
|
517 | if value is None: | |
533 | self.autoindent = not self.autoindent |
|
518 | self.autoindent = not self.autoindent | |
534 | else: |
|
519 | else: | |
535 | self.autoindent = value |
|
520 | self.autoindent = value | |
536 |
|
521 | |||
537 | #------------------------------------------------------------------------- |
|
522 | #------------------------------------------------------------------------- | |
538 | # init_* methods called by __init__ |
|
523 | # init_* methods called by __init__ | |
539 | #------------------------------------------------------------------------- |
|
524 | #------------------------------------------------------------------------- | |
540 |
|
525 | |||
541 | def init_ipython_dir(self, ipython_dir): |
|
526 | def init_ipython_dir(self, ipython_dir): | |
542 | if ipython_dir is not None: |
|
527 | if ipython_dir is not None: | |
543 | self.ipython_dir = ipython_dir |
|
528 | self.ipython_dir = ipython_dir | |
544 | return |
|
529 | return | |
545 |
|
530 | |||
546 | self.ipython_dir = get_ipython_dir() |
|
531 | self.ipython_dir = get_ipython_dir() | |
547 |
|
532 | |||
548 | def init_profile_dir(self, profile_dir): |
|
533 | def init_profile_dir(self, profile_dir): | |
549 | if profile_dir is not None: |
|
534 | if profile_dir is not None: | |
550 | self.profile_dir = profile_dir |
|
535 | self.profile_dir = profile_dir | |
551 | return |
|
536 | return | |
552 | self.profile_dir =\ |
|
537 | self.profile_dir =\ | |
553 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
538 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') | |
554 |
|
539 | |||
555 | def init_instance_attrs(self): |
|
540 | def init_instance_attrs(self): | |
556 | self.more = False |
|
541 | self.more = False | |
557 |
|
542 | |||
558 | # command compiler |
|
543 | # command compiler | |
559 | self.compile = CachingCompiler() |
|
544 | self.compile = CachingCompiler() | |
560 |
|
545 | |||
561 | # Make an empty namespace, which extension writers can rely on both |
|
546 | # Make an empty namespace, which extension writers can rely on both | |
562 | # existing and NEVER being used by ipython itself. This gives them a |
|
547 | # existing and NEVER being used by ipython itself. This gives them a | |
563 | # convenient location for storing additional information and state |
|
548 | # convenient location for storing additional information and state | |
564 | # their extensions may require, without fear of collisions with other |
|
549 | # their extensions may require, without fear of collisions with other | |
565 | # ipython names that may develop later. |
|
550 | # ipython names that may develop later. | |
566 | self.meta = Struct() |
|
551 | self.meta = Struct() | |
567 |
|
552 | |||
568 | # Temporary files used for various purposes. Deleted at exit. |
|
553 | # Temporary files used for various purposes. Deleted at exit. | |
569 | self.tempfiles = [] |
|
554 | self.tempfiles = [] | |
570 | self.tempdirs = [] |
|
555 | self.tempdirs = [] | |
571 |
|
556 | |||
572 | # keep track of where we started running (mainly for crash post-mortem) |
|
557 | # keep track of where we started running (mainly for crash post-mortem) | |
573 | # This is not being used anywhere currently. |
|
558 | # This is not being used anywhere currently. | |
574 | self.starting_dir = py3compat.getcwd() |
|
559 | self.starting_dir = py3compat.getcwd() | |
575 |
|
560 | |||
576 | # Indentation management |
|
561 | # Indentation management | |
577 | self.indent_current_nsp = 0 |
|
562 | self.indent_current_nsp = 0 | |
578 |
|
563 | |||
579 | # Dict to track post-execution functions that have been registered |
|
564 | # Dict to track post-execution functions that have been registered | |
580 | self._post_execute = {} |
|
565 | self._post_execute = {} | |
581 |
|
566 | |||
582 | def init_environment(self): |
|
567 | def init_environment(self): | |
583 | """Any changes we need to make to the user's environment.""" |
|
568 | """Any changes we need to make to the user's environment.""" | |
584 | pass |
|
569 | pass | |
585 |
|
570 | |||
586 | def init_encoding(self): |
|
571 | def init_encoding(self): | |
587 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
572 | # Get system encoding at startup time. Certain terminals (like Emacs | |
588 | # under Win32 have it set to None, and we need to have a known valid |
|
573 | # under Win32 have it set to None, and we need to have a known valid | |
589 | # encoding to use in the raw_input() method |
|
574 | # encoding to use in the raw_input() method | |
590 | try: |
|
575 | try: | |
591 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
576 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
592 | except AttributeError: |
|
577 | except AttributeError: | |
593 | self.stdin_encoding = 'ascii' |
|
578 | self.stdin_encoding = 'ascii' | |
594 |
|
579 | |||
595 | def init_syntax_highlighting(self): |
|
580 | def init_syntax_highlighting(self): | |
596 | # Python source parser/formatter for syntax highlighting |
|
581 | # Python source parser/formatter for syntax highlighting | |
597 | pyformat = PyColorize.Parser().format |
|
582 | pyformat = PyColorize.Parser().format | |
598 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
583 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
599 |
|
584 | |||
600 | def refresh_style(self): |
|
585 | def refresh_style(self): | |
601 | # No-op here, used in subclass |
|
586 | # No-op here, used in subclass | |
602 | pass |
|
587 | pass | |
603 |
|
588 | |||
604 | def init_pushd_popd_magic(self): |
|
589 | def init_pushd_popd_magic(self): | |
605 | # for pushd/popd management |
|
590 | # for pushd/popd management | |
606 | self.home_dir = get_home_dir() |
|
591 | self.home_dir = get_home_dir() | |
607 |
|
592 | |||
608 | self.dir_stack = [] |
|
593 | self.dir_stack = [] | |
609 |
|
594 | |||
610 | def init_logger(self): |
|
595 | def init_logger(self): | |
611 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
596 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
612 | logmode='rotate') |
|
597 | logmode='rotate') | |
613 |
|
598 | |||
614 | def init_logstart(self): |
|
599 | def init_logstart(self): | |
615 | """Initialize logging in case it was requested at the command line. |
|
600 | """Initialize logging in case it was requested at the command line. | |
616 | """ |
|
601 | """ | |
617 | if self.logappend: |
|
602 | if self.logappend: | |
618 | self.magic('logstart %s append' % self.logappend) |
|
603 | self.magic('logstart %s append' % self.logappend) | |
619 | elif self.logfile: |
|
604 | elif self.logfile: | |
620 | self.magic('logstart %s' % self.logfile) |
|
605 | self.magic('logstart %s' % self.logfile) | |
621 | elif self.logstart: |
|
606 | elif self.logstart: | |
622 | self.magic('logstart') |
|
607 | self.magic('logstart') | |
623 |
|
608 | |||
624 | def init_deprecation_warnings(self): |
|
609 | def init_deprecation_warnings(self): | |
625 | """ |
|
610 | """ | |
626 | register default filter for deprecation warning. |
|
611 | register default filter for deprecation warning. | |
627 |
|
612 | |||
628 | This will allow deprecation warning of function used interactively to show |
|
613 | This will allow deprecation warning of function used interactively to show | |
629 | warning to users, and still hide deprecation warning from libraries import. |
|
614 | warning to users, and still hide deprecation warning from libraries import. | |
630 | """ |
|
615 | """ | |
631 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) |
|
616 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) | |
632 |
|
617 | |||
633 | def init_builtins(self): |
|
618 | def init_builtins(self): | |
634 | # A single, static flag that we set to True. Its presence indicates |
|
619 | # A single, static flag that we set to True. Its presence indicates | |
635 | # that an IPython shell has been created, and we make no attempts at |
|
620 | # that an IPython shell has been created, and we make no attempts at | |
636 | # removing on exit or representing the existence of more than one |
|
621 | # removing on exit or representing the existence of more than one | |
637 | # IPython at a time. |
|
622 | # IPython at a time. | |
638 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
623 | builtin_mod.__dict__['__IPYTHON__'] = True | |
639 |
|
624 | |||
640 | self.builtin_trap = BuiltinTrap(shell=self) |
|
625 | self.builtin_trap = BuiltinTrap(shell=self) | |
641 |
|
626 | |||
642 | def init_inspector(self): |
|
627 | def init_inspector(self): | |
643 | # Object inspector |
|
628 | # Object inspector | |
644 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
629 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
645 | PyColorize.ANSICodeColors, |
|
630 | PyColorize.ANSICodeColors, | |
646 | 'NoColor', |
|
631 | 'NoColor', | |
647 | self.object_info_string_level) |
|
632 | self.object_info_string_level) | |
648 |
|
633 | |||
649 | def init_io(self): |
|
634 | def init_io(self): | |
650 | # This will just use sys.stdout and sys.stderr. If you want to |
|
635 | # This will just use sys.stdout and sys.stderr. If you want to | |
651 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
636 | # override sys.stdout and sys.stderr themselves, you need to do that | |
652 | # *before* instantiating this class, because io holds onto |
|
637 | # *before* instantiating this class, because io holds onto | |
653 | # references to the underlying streams. |
|
638 | # references to the underlying streams. | |
654 | # io.std* are deprecated, but don't show our own deprecation warnings |
|
639 | # io.std* are deprecated, but don't show our own deprecation warnings | |
655 | # during initialization of the deprecated API. |
|
640 | # during initialization of the deprecated API. | |
656 | with warnings.catch_warnings(): |
|
641 | with warnings.catch_warnings(): | |
657 | warnings.simplefilter('ignore', DeprecationWarning) |
|
642 | warnings.simplefilter('ignore', DeprecationWarning) | |
658 | io.stdout = io.IOStream(sys.stdout) |
|
643 | io.stdout = io.IOStream(sys.stdout) | |
659 | io.stderr = io.IOStream(sys.stderr) |
|
644 | io.stderr = io.IOStream(sys.stderr) | |
660 |
|
645 | |||
661 | def init_prompts(self): |
|
646 | def init_prompts(self): | |
662 | # Set system prompts, so that scripts can decide if they are running |
|
647 | # Set system prompts, so that scripts can decide if they are running | |
663 | # interactively. |
|
648 | # interactively. | |
664 | sys.ps1 = 'In : ' |
|
649 | sys.ps1 = 'In : ' | |
665 | sys.ps2 = '...: ' |
|
650 | sys.ps2 = '...: ' | |
666 | sys.ps3 = 'Out: ' |
|
651 | sys.ps3 = 'Out: ' | |
667 |
|
652 | |||
668 | def init_display_formatter(self): |
|
653 | def init_display_formatter(self): | |
669 | self.display_formatter = DisplayFormatter(parent=self) |
|
654 | self.display_formatter = DisplayFormatter(parent=self) | |
670 | self.configurables.append(self.display_formatter) |
|
655 | self.configurables.append(self.display_formatter) | |
671 |
|
656 | |||
672 | def init_display_pub(self): |
|
657 | def init_display_pub(self): | |
673 | self.display_pub = self.display_pub_class(parent=self) |
|
658 | self.display_pub = self.display_pub_class(parent=self) | |
674 | self.configurables.append(self.display_pub) |
|
659 | self.configurables.append(self.display_pub) | |
675 |
|
660 | |||
676 | def init_data_pub(self): |
|
661 | def init_data_pub(self): | |
677 | if not self.data_pub_class: |
|
662 | if not self.data_pub_class: | |
678 | self.data_pub = None |
|
663 | self.data_pub = None | |
679 | return |
|
664 | return | |
680 | self.data_pub = self.data_pub_class(parent=self) |
|
665 | self.data_pub = self.data_pub_class(parent=self) | |
681 | self.configurables.append(self.data_pub) |
|
666 | self.configurables.append(self.data_pub) | |
682 |
|
667 | |||
683 | def init_displayhook(self): |
|
668 | def init_displayhook(self): | |
684 | # Initialize displayhook, set in/out prompts and printing system |
|
669 | # Initialize displayhook, set in/out prompts and printing system | |
685 | self.displayhook = self.displayhook_class( |
|
670 | self.displayhook = self.displayhook_class( | |
686 | parent=self, |
|
671 | parent=self, | |
687 | shell=self, |
|
672 | shell=self, | |
688 | cache_size=self.cache_size, |
|
673 | cache_size=self.cache_size, | |
689 | ) |
|
674 | ) | |
690 | self.configurables.append(self.displayhook) |
|
675 | self.configurables.append(self.displayhook) | |
691 | # This is a context manager that installs/revmoes the displayhook at |
|
676 | # This is a context manager that installs/revmoes the displayhook at | |
692 | # the appropriate time. |
|
677 | # the appropriate time. | |
693 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
678 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
694 |
|
679 | |||
695 | def init_virtualenv(self): |
|
680 | def init_virtualenv(self): | |
696 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
681 | """Add a virtualenv to sys.path so the user can import modules from it. | |
697 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
682 | This isn't perfect: it doesn't use the Python interpreter with which the | |
698 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
683 | virtualenv was built, and it ignores the --no-site-packages option. A | |
699 | warning will appear suggesting the user installs IPython in the |
|
684 | warning will appear suggesting the user installs IPython in the | |
700 | virtualenv, but for many cases, it probably works well enough. |
|
685 | virtualenv, but for many cases, it probably works well enough. | |
701 |
|
686 | |||
702 | Adapted from code snippets online. |
|
687 | Adapted from code snippets online. | |
703 |
|
688 | |||
704 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
689 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
705 | """ |
|
690 | """ | |
706 | if 'VIRTUAL_ENV' not in os.environ: |
|
691 | if 'VIRTUAL_ENV' not in os.environ: | |
707 | # Not in a virtualenv |
|
692 | # Not in a virtualenv | |
708 | return |
|
693 | return | |
709 |
|
694 | |||
710 | # venv detection: |
|
695 | # venv detection: | |
711 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
696 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
712 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
697 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
713 | # So we just check every item in the symlink tree (generally <= 3) |
|
698 | # So we just check every item in the symlink tree (generally <= 3) | |
714 | p = os.path.normcase(sys.executable) |
|
699 | p = os.path.normcase(sys.executable) | |
715 | paths = [p] |
|
700 | paths = [p] | |
716 | while os.path.islink(p): |
|
701 | while os.path.islink(p): | |
717 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
|
702 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) | |
718 | paths.append(p) |
|
703 | paths.append(p) | |
719 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
|
704 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) | |
720 | if any(p.startswith(p_venv) for p in paths): |
|
705 | if any(p.startswith(p_venv) for p in paths): | |
721 | # Running properly in the virtualenv, don't need to do anything |
|
706 | # Running properly in the virtualenv, don't need to do anything | |
722 | return |
|
707 | return | |
723 |
|
708 | |||
724 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
709 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
725 | "install IPython inside the virtualenv.") |
|
710 | "install IPython inside the virtualenv.") | |
726 | if sys.platform == "win32": |
|
711 | if sys.platform == "win32": | |
727 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
712 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') | |
728 | else: |
|
713 | else: | |
729 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
714 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', | |
730 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
715 | 'python%d.%d' % sys.version_info[:2], 'site-packages') | |
731 |
|
716 | |||
732 | import site |
|
717 | import site | |
733 | sys.path.insert(0, virtual_env) |
|
718 | sys.path.insert(0, virtual_env) | |
734 | site.addsitedir(virtual_env) |
|
719 | site.addsitedir(virtual_env) | |
735 |
|
720 | |||
736 | #------------------------------------------------------------------------- |
|
721 | #------------------------------------------------------------------------- | |
737 | # Things related to injections into the sys module |
|
722 | # Things related to injections into the sys module | |
738 | #------------------------------------------------------------------------- |
|
723 | #------------------------------------------------------------------------- | |
739 |
|
724 | |||
740 | def save_sys_module_state(self): |
|
725 | def save_sys_module_state(self): | |
741 | """Save the state of hooks in the sys module. |
|
726 | """Save the state of hooks in the sys module. | |
742 |
|
727 | |||
743 | This has to be called after self.user_module is created. |
|
728 | This has to be called after self.user_module is created. | |
744 | """ |
|
729 | """ | |
745 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
730 | self._orig_sys_module_state = {'stdin': sys.stdin, | |
746 | 'stdout': sys.stdout, |
|
731 | 'stdout': sys.stdout, | |
747 | 'stderr': sys.stderr, |
|
732 | 'stderr': sys.stderr, | |
748 | 'excepthook': sys.excepthook} |
|
733 | 'excepthook': sys.excepthook} | |
749 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
734 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
750 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
735 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
751 |
|
736 | |||
752 | def restore_sys_module_state(self): |
|
737 | def restore_sys_module_state(self): | |
753 | """Restore the state of the sys module.""" |
|
738 | """Restore the state of the sys module.""" | |
754 | try: |
|
739 | try: | |
755 | for k, v in iteritems(self._orig_sys_module_state): |
|
740 | for k, v in iteritems(self._orig_sys_module_state): | |
756 | setattr(sys, k, v) |
|
741 | setattr(sys, k, v) | |
757 | except AttributeError: |
|
742 | except AttributeError: | |
758 | pass |
|
743 | pass | |
759 | # Reset what what done in self.init_sys_modules |
|
744 | # Reset what what done in self.init_sys_modules | |
760 | if self._orig_sys_modules_main_mod is not None: |
|
745 | if self._orig_sys_modules_main_mod is not None: | |
761 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
746 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
762 |
|
747 | |||
763 | #------------------------------------------------------------------------- |
|
748 | #------------------------------------------------------------------------- | |
764 | # Things related to the banner |
|
749 | # Things related to the banner | |
765 | #------------------------------------------------------------------------- |
|
750 | #------------------------------------------------------------------------- | |
766 |
|
751 | |||
767 | @property |
|
752 | @property | |
768 | def banner(self): |
|
753 | def banner(self): | |
769 | banner = self.banner1 |
|
754 | banner = self.banner1 | |
770 | if self.profile and self.profile != 'default': |
|
755 | if self.profile and self.profile != 'default': | |
771 | banner += '\nIPython profile: %s\n' % self.profile |
|
756 | banner += '\nIPython profile: %s\n' % self.profile | |
772 | if self.banner2: |
|
757 | if self.banner2: | |
773 | banner += '\n' + self.banner2 |
|
758 | banner += '\n' + self.banner2 | |
774 | return banner |
|
759 | return banner | |
775 |
|
760 | |||
776 | def show_banner(self, banner=None): |
|
761 | def show_banner(self, banner=None): | |
777 | if banner is None: |
|
762 | if banner is None: | |
778 | banner = self.banner |
|
763 | banner = self.banner | |
779 | sys.stdout.write(banner) |
|
764 | sys.stdout.write(banner) | |
780 |
|
765 | |||
781 | #------------------------------------------------------------------------- |
|
766 | #------------------------------------------------------------------------- | |
782 | # Things related to hooks |
|
767 | # Things related to hooks | |
783 | #------------------------------------------------------------------------- |
|
768 | #------------------------------------------------------------------------- | |
784 |
|
769 | |||
785 | def init_hooks(self): |
|
770 | def init_hooks(self): | |
786 | # hooks holds pointers used for user-side customizations |
|
771 | # hooks holds pointers used for user-side customizations | |
787 | self.hooks = Struct() |
|
772 | self.hooks = Struct() | |
788 |
|
773 | |||
789 | self.strdispatchers = {} |
|
774 | self.strdispatchers = {} | |
790 |
|
775 | |||
791 | # Set all default hooks, defined in the IPython.hooks module. |
|
776 | # Set all default hooks, defined in the IPython.hooks module. | |
792 | hooks = IPython.core.hooks |
|
777 | hooks = IPython.core.hooks | |
793 | for hook_name in hooks.__all__: |
|
778 | for hook_name in hooks.__all__: | |
794 | # default hooks have priority 100, i.e. low; user hooks should have |
|
779 | # default hooks have priority 100, i.e. low; user hooks should have | |
795 | # 0-100 priority |
|
780 | # 0-100 priority | |
796 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
781 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) | |
797 |
|
782 | |||
798 | if self.display_page: |
|
783 | if self.display_page: | |
799 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
784 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
800 |
|
785 | |||
801 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
786 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, | |
802 | _warn_deprecated=True): |
|
787 | _warn_deprecated=True): | |
803 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
788 | """set_hook(name,hook) -> sets an internal IPython hook. | |
804 |
|
789 | |||
805 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
790 | IPython exposes some of its internal API as user-modifiable hooks. By | |
806 | adding your function to one of these hooks, you can modify IPython's |
|
791 | adding your function to one of these hooks, you can modify IPython's | |
807 | behavior to call at runtime your own routines.""" |
|
792 | behavior to call at runtime your own routines.""" | |
808 |
|
793 | |||
809 | # At some point in the future, this should validate the hook before it |
|
794 | # At some point in the future, this should validate the hook before it | |
810 | # accepts it. Probably at least check that the hook takes the number |
|
795 | # accepts it. Probably at least check that the hook takes the number | |
811 | # of args it's supposed to. |
|
796 | # of args it's supposed to. | |
812 |
|
797 | |||
813 | f = types.MethodType(hook,self) |
|
798 | f = types.MethodType(hook,self) | |
814 |
|
799 | |||
815 | # check if the hook is for strdispatcher first |
|
800 | # check if the hook is for strdispatcher first | |
816 | if str_key is not None: |
|
801 | if str_key is not None: | |
817 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
802 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
818 | sdp.add_s(str_key, f, priority ) |
|
803 | sdp.add_s(str_key, f, priority ) | |
819 | self.strdispatchers[name] = sdp |
|
804 | self.strdispatchers[name] = sdp | |
820 | return |
|
805 | return | |
821 | if re_key is not None: |
|
806 | if re_key is not None: | |
822 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
807 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
823 | sdp.add_re(re.compile(re_key), f, priority ) |
|
808 | sdp.add_re(re.compile(re_key), f, priority ) | |
824 | self.strdispatchers[name] = sdp |
|
809 | self.strdispatchers[name] = sdp | |
825 | return |
|
810 | return | |
826 |
|
811 | |||
827 | dp = getattr(self.hooks, name, None) |
|
812 | dp = getattr(self.hooks, name, None) | |
828 | if name not in IPython.core.hooks.__all__: |
|
813 | if name not in IPython.core.hooks.__all__: | |
829 | print("Warning! Hook '%s' is not one of %s" % \ |
|
814 | print("Warning! Hook '%s' is not one of %s" % \ | |
830 | (name, IPython.core.hooks.__all__ )) |
|
815 | (name, IPython.core.hooks.__all__ )) | |
831 |
|
816 | |||
832 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
817 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): | |
833 | alternative = IPython.core.hooks.deprecated[name] |
|
818 | alternative = IPython.core.hooks.deprecated[name] | |
834 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) |
|
819 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) | |
835 |
|
820 | |||
836 | if not dp: |
|
821 | if not dp: | |
837 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
822 | dp = IPython.core.hooks.CommandChainDispatcher() | |
838 |
|
823 | |||
839 | try: |
|
824 | try: | |
840 | dp.add(f,priority) |
|
825 | dp.add(f,priority) | |
841 | except AttributeError: |
|
826 | except AttributeError: | |
842 | # it was not commandchain, plain old func - replace |
|
827 | # it was not commandchain, plain old func - replace | |
843 | dp = f |
|
828 | dp = f | |
844 |
|
829 | |||
845 | setattr(self.hooks,name, dp) |
|
830 | setattr(self.hooks,name, dp) | |
846 |
|
831 | |||
847 | #------------------------------------------------------------------------- |
|
832 | #------------------------------------------------------------------------- | |
848 | # Things related to events |
|
833 | # Things related to events | |
849 | #------------------------------------------------------------------------- |
|
834 | #------------------------------------------------------------------------- | |
850 |
|
835 | |||
851 | def init_events(self): |
|
836 | def init_events(self): | |
852 | self.events = EventManager(self, available_events) |
|
837 | self.events = EventManager(self, available_events) | |
853 |
|
838 | |||
854 | self.events.register("pre_execute", self._clear_warning_registry) |
|
839 | self.events.register("pre_execute", self._clear_warning_registry) | |
855 |
|
840 | |||
856 | def register_post_execute(self, func): |
|
841 | def register_post_execute(self, func): | |
857 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
842 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | |
858 |
|
843 | |||
859 | Register a function for calling after code execution. |
|
844 | Register a function for calling after code execution. | |
860 | """ |
|
845 | """ | |
861 | warn("ip.register_post_execute is deprecated, use " |
|
846 | warn("ip.register_post_execute is deprecated, use " | |
862 | "ip.events.register('post_run_cell', func) instead.") |
|
847 | "ip.events.register('post_run_cell', func) instead.") | |
863 | self.events.register('post_run_cell', func) |
|
848 | self.events.register('post_run_cell', func) | |
864 |
|
849 | |||
865 | def _clear_warning_registry(self): |
|
850 | def _clear_warning_registry(self): | |
866 | # clear the warning registry, so that different code blocks with |
|
851 | # clear the warning registry, so that different code blocks with | |
867 | # overlapping line number ranges don't cause spurious suppression of |
|
852 | # overlapping line number ranges don't cause spurious suppression of | |
868 | # warnings (see gh-6611 for details) |
|
853 | # warnings (see gh-6611 for details) | |
869 | if "__warningregistry__" in self.user_global_ns: |
|
854 | if "__warningregistry__" in self.user_global_ns: | |
870 | del self.user_global_ns["__warningregistry__"] |
|
855 | del self.user_global_ns["__warningregistry__"] | |
871 |
|
856 | |||
872 | #------------------------------------------------------------------------- |
|
857 | #------------------------------------------------------------------------- | |
873 | # Things related to the "main" module |
|
858 | # Things related to the "main" module | |
874 | #------------------------------------------------------------------------- |
|
859 | #------------------------------------------------------------------------- | |
875 |
|
860 | |||
876 | def new_main_mod(self, filename, modname): |
|
861 | def new_main_mod(self, filename, modname): | |
877 | """Return a new 'main' module object for user code execution. |
|
862 | """Return a new 'main' module object for user code execution. | |
878 |
|
863 | |||
879 | ``filename`` should be the path of the script which will be run in the |
|
864 | ``filename`` should be the path of the script which will be run in the | |
880 | module. Requests with the same filename will get the same module, with |
|
865 | module. Requests with the same filename will get the same module, with | |
881 | its namespace cleared. |
|
866 | its namespace cleared. | |
882 |
|
867 | |||
883 | ``modname`` should be the module name - normally either '__main__' or |
|
868 | ``modname`` should be the module name - normally either '__main__' or | |
884 | the basename of the file without the extension. |
|
869 | the basename of the file without the extension. | |
885 |
|
870 | |||
886 | When scripts are executed via %run, we must keep a reference to their |
|
871 | When scripts are executed via %run, we must keep a reference to their | |
887 | __main__ module around so that Python doesn't |
|
872 | __main__ module around so that Python doesn't | |
888 | clear it, rendering references to module globals useless. |
|
873 | clear it, rendering references to module globals useless. | |
889 |
|
874 | |||
890 | This method keeps said reference in a private dict, keyed by the |
|
875 | This method keeps said reference in a private dict, keyed by the | |
891 | absolute path of the script. This way, for multiple executions of the |
|
876 | absolute path of the script. This way, for multiple executions of the | |
892 | same script we only keep one copy of the namespace (the last one), |
|
877 | same script we only keep one copy of the namespace (the last one), | |
893 | thus preventing memory leaks from old references while allowing the |
|
878 | thus preventing memory leaks from old references while allowing the | |
894 | objects from the last execution to be accessible. |
|
879 | objects from the last execution to be accessible. | |
895 | """ |
|
880 | """ | |
896 | filename = os.path.abspath(filename) |
|
881 | filename = os.path.abspath(filename) | |
897 | try: |
|
882 | try: | |
898 | main_mod = self._main_mod_cache[filename] |
|
883 | main_mod = self._main_mod_cache[filename] | |
899 | except KeyError: |
|
884 | except KeyError: | |
900 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
885 | main_mod = self._main_mod_cache[filename] = types.ModuleType( | |
901 | py3compat.cast_bytes_py2(modname), |
|
886 | py3compat.cast_bytes_py2(modname), | |
902 | doc="Module created for script run in IPython") |
|
887 | doc="Module created for script run in IPython") | |
903 | else: |
|
888 | else: | |
904 | main_mod.__dict__.clear() |
|
889 | main_mod.__dict__.clear() | |
905 | main_mod.__name__ = modname |
|
890 | main_mod.__name__ = modname | |
906 |
|
891 | |||
907 | main_mod.__file__ = filename |
|
892 | main_mod.__file__ = filename | |
908 | # It seems pydoc (and perhaps others) needs any module instance to |
|
893 | # It seems pydoc (and perhaps others) needs any module instance to | |
909 | # implement a __nonzero__ method |
|
894 | # implement a __nonzero__ method | |
910 | main_mod.__nonzero__ = lambda : True |
|
895 | main_mod.__nonzero__ = lambda : True | |
911 |
|
896 | |||
912 | return main_mod |
|
897 | return main_mod | |
913 |
|
898 | |||
914 | def clear_main_mod_cache(self): |
|
899 | def clear_main_mod_cache(self): | |
915 | """Clear the cache of main modules. |
|
900 | """Clear the cache of main modules. | |
916 |
|
901 | |||
917 | Mainly for use by utilities like %reset. |
|
902 | Mainly for use by utilities like %reset. | |
918 |
|
903 | |||
919 | Examples |
|
904 | Examples | |
920 | -------- |
|
905 | -------- | |
921 |
|
906 | |||
922 | In [15]: import IPython |
|
907 | In [15]: import IPython | |
923 |
|
908 | |||
924 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
909 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
925 |
|
910 | |||
926 | In [17]: len(_ip._main_mod_cache) > 0 |
|
911 | In [17]: len(_ip._main_mod_cache) > 0 | |
927 | Out[17]: True |
|
912 | Out[17]: True | |
928 |
|
913 | |||
929 | In [18]: _ip.clear_main_mod_cache() |
|
914 | In [18]: _ip.clear_main_mod_cache() | |
930 |
|
915 | |||
931 | In [19]: len(_ip._main_mod_cache) == 0 |
|
916 | In [19]: len(_ip._main_mod_cache) == 0 | |
932 | Out[19]: True |
|
917 | Out[19]: True | |
933 | """ |
|
918 | """ | |
934 | self._main_mod_cache.clear() |
|
919 | self._main_mod_cache.clear() | |
935 |
|
920 | |||
936 | #------------------------------------------------------------------------- |
|
921 | #------------------------------------------------------------------------- | |
937 | # Things related to debugging |
|
922 | # Things related to debugging | |
938 | #------------------------------------------------------------------------- |
|
923 | #------------------------------------------------------------------------- | |
939 |
|
924 | |||
940 | def init_pdb(self): |
|
925 | def init_pdb(self): | |
941 | # Set calling of pdb on exceptions |
|
926 | # Set calling of pdb on exceptions | |
942 | # self.call_pdb is a property |
|
927 | # self.call_pdb is a property | |
943 | self.call_pdb = self.pdb |
|
928 | self.call_pdb = self.pdb | |
944 |
|
929 | |||
945 | def _get_call_pdb(self): |
|
930 | def _get_call_pdb(self): | |
946 | return self._call_pdb |
|
931 | return self._call_pdb | |
947 |
|
932 | |||
948 | def _set_call_pdb(self,val): |
|
933 | def _set_call_pdb(self,val): | |
949 |
|
934 | |||
950 | if val not in (0,1,False,True): |
|
935 | if val not in (0,1,False,True): | |
951 | raise ValueError('new call_pdb value must be boolean') |
|
936 | raise ValueError('new call_pdb value must be boolean') | |
952 |
|
937 | |||
953 | # store value in instance |
|
938 | # store value in instance | |
954 | self._call_pdb = val |
|
939 | self._call_pdb = val | |
955 |
|
940 | |||
956 | # notify the actual exception handlers |
|
941 | # notify the actual exception handlers | |
957 | self.InteractiveTB.call_pdb = val |
|
942 | self.InteractiveTB.call_pdb = val | |
958 |
|
943 | |||
959 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
944 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
960 | 'Control auto-activation of pdb at exceptions') |
|
945 | 'Control auto-activation of pdb at exceptions') | |
961 |
|
946 | |||
962 | def debugger(self,force=False): |
|
947 | def debugger(self,force=False): | |
963 | """Call the pdb debugger. |
|
948 | """Call the pdb debugger. | |
964 |
|
949 | |||
965 | Keywords: |
|
950 | Keywords: | |
966 |
|
951 | |||
967 | - force(False): by default, this routine checks the instance call_pdb |
|
952 | - force(False): by default, this routine checks the instance call_pdb | |
968 | flag and does not actually invoke the debugger if the flag is false. |
|
953 | flag and does not actually invoke the debugger if the flag is false. | |
969 | The 'force' option forces the debugger to activate even if the flag |
|
954 | The 'force' option forces the debugger to activate even if the flag | |
970 | is false. |
|
955 | is false. | |
971 | """ |
|
956 | """ | |
972 |
|
957 | |||
973 | if not (force or self.call_pdb): |
|
958 | if not (force or self.call_pdb): | |
974 | return |
|
959 | return | |
975 |
|
960 | |||
976 | if not hasattr(sys,'last_traceback'): |
|
961 | if not hasattr(sys,'last_traceback'): | |
977 | error('No traceback has been produced, nothing to debug.') |
|
962 | error('No traceback has been produced, nothing to debug.') | |
978 | return |
|
963 | return | |
979 |
|
964 | |||
980 | self.InteractiveTB.debugger(force=True) |
|
965 | self.InteractiveTB.debugger(force=True) | |
981 |
|
966 | |||
982 | #------------------------------------------------------------------------- |
|
967 | #------------------------------------------------------------------------- | |
983 | # Things related to IPython's various namespaces |
|
968 | # Things related to IPython's various namespaces | |
984 | #------------------------------------------------------------------------- |
|
969 | #------------------------------------------------------------------------- | |
985 | default_user_namespaces = True |
|
970 | default_user_namespaces = True | |
986 |
|
971 | |||
987 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
972 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
988 | # Create the namespace where the user will operate. user_ns is |
|
973 | # Create the namespace where the user will operate. user_ns is | |
989 | # normally the only one used, and it is passed to the exec calls as |
|
974 | # normally the only one used, and it is passed to the exec calls as | |
990 | # the locals argument. But we do carry a user_global_ns namespace |
|
975 | # the locals argument. But we do carry a user_global_ns namespace | |
991 | # given as the exec 'globals' argument, This is useful in embedding |
|
976 | # given as the exec 'globals' argument, This is useful in embedding | |
992 | # situations where the ipython shell opens in a context where the |
|
977 | # situations where the ipython shell opens in a context where the | |
993 | # distinction between locals and globals is meaningful. For |
|
978 | # distinction between locals and globals is meaningful. For | |
994 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
979 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
995 |
|
980 | |||
996 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
981 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
997 | # level as a dict instead of a module. This is a manual fix, but I |
|
982 | # level as a dict instead of a module. This is a manual fix, but I | |
998 | # should really track down where the problem is coming from. Alex |
|
983 | # should really track down where the problem is coming from. Alex | |
999 | # Schmolck reported this problem first. |
|
984 | # Schmolck reported this problem first. | |
1000 |
|
985 | |||
1001 | # A useful post by Alex Martelli on this topic: |
|
986 | # A useful post by Alex Martelli on this topic: | |
1002 | # Re: inconsistent value from __builtins__ |
|
987 | # Re: inconsistent value from __builtins__ | |
1003 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
988 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
1004 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
989 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
1005 | # Gruppen: comp.lang.python |
|
990 | # Gruppen: comp.lang.python | |
1006 |
|
991 | |||
1007 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
992 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
1008 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
993 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
1009 | # > <type 'dict'> |
|
994 | # > <type 'dict'> | |
1010 | # > >>> print type(__builtins__) |
|
995 | # > >>> print type(__builtins__) | |
1011 | # > <type 'module'> |
|
996 | # > <type 'module'> | |
1012 | # > Is this difference in return value intentional? |
|
997 | # > Is this difference in return value intentional? | |
1013 |
|
998 | |||
1014 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
999 | # Well, it's documented that '__builtins__' can be either a dictionary | |
1015 | # or a module, and it's been that way for a long time. Whether it's |
|
1000 | # or a module, and it's been that way for a long time. Whether it's | |
1016 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1001 | # intentional (or sensible), I don't know. In any case, the idea is | |
1017 | # that if you need to access the built-in namespace directly, you |
|
1002 | # that if you need to access the built-in namespace directly, you | |
1018 | # should start with "import __builtin__" (note, no 's') which will |
|
1003 | # should start with "import __builtin__" (note, no 's') which will | |
1019 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1004 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
1020 |
|
1005 | |||
1021 | # These routines return a properly built module and dict as needed by |
|
1006 | # These routines return a properly built module and dict as needed by | |
1022 | # the rest of the code, and can also be used by extension writers to |
|
1007 | # the rest of the code, and can also be used by extension writers to | |
1023 | # generate properly initialized namespaces. |
|
1008 | # generate properly initialized namespaces. | |
1024 | if (user_ns is not None) or (user_module is not None): |
|
1009 | if (user_ns is not None) or (user_module is not None): | |
1025 | self.default_user_namespaces = False |
|
1010 | self.default_user_namespaces = False | |
1026 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1011 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1027 |
|
1012 | |||
1028 | # A record of hidden variables we have added to the user namespace, so |
|
1013 | # A record of hidden variables we have added to the user namespace, so | |
1029 | # we can list later only variables defined in actual interactive use. |
|
1014 | # we can list later only variables defined in actual interactive use. | |
1030 | self.user_ns_hidden = {} |
|
1015 | self.user_ns_hidden = {} | |
1031 |
|
1016 | |||
1032 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1017 | # Now that FakeModule produces a real module, we've run into a nasty | |
1033 | # problem: after script execution (via %run), the module where the user |
|
1018 | # problem: after script execution (via %run), the module where the user | |
1034 | # code ran is deleted. Now that this object is a true module (needed |
|
1019 | # code ran is deleted. Now that this object is a true module (needed | |
1035 | # so doctest and other tools work correctly), the Python module |
|
1020 | # so doctest and other tools work correctly), the Python module | |
1036 | # teardown mechanism runs over it, and sets to None every variable |
|
1021 | # teardown mechanism runs over it, and sets to None every variable | |
1037 | # present in that module. Top-level references to objects from the |
|
1022 | # present in that module. Top-level references to objects from the | |
1038 | # script survive, because the user_ns is updated with them. However, |
|
1023 | # script survive, because the user_ns is updated with them. However, | |
1039 | # calling functions defined in the script that use other things from |
|
1024 | # calling functions defined in the script that use other things from | |
1040 | # the script will fail, because the function's closure had references |
|
1025 | # the script will fail, because the function's closure had references | |
1041 | # to the original objects, which are now all None. So we must protect |
|
1026 | # to the original objects, which are now all None. So we must protect | |
1042 | # these modules from deletion by keeping a cache. |
|
1027 | # these modules from deletion by keeping a cache. | |
1043 | # |
|
1028 | # | |
1044 | # To avoid keeping stale modules around (we only need the one from the |
|
1029 | # To avoid keeping stale modules around (we only need the one from the | |
1045 | # last run), we use a dict keyed with the full path to the script, so |
|
1030 | # last run), we use a dict keyed with the full path to the script, so | |
1046 | # only the last version of the module is held in the cache. Note, |
|
1031 | # only the last version of the module is held in the cache. Note, | |
1047 | # however, that we must cache the module *namespace contents* (their |
|
1032 | # however, that we must cache the module *namespace contents* (their | |
1048 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1033 | # __dict__). Because if we try to cache the actual modules, old ones | |
1049 | # (uncached) could be destroyed while still holding references (such as |
|
1034 | # (uncached) could be destroyed while still holding references (such as | |
1050 | # those held by GUI objects that tend to be long-lived)> |
|
1035 | # those held by GUI objects that tend to be long-lived)> | |
1051 | # |
|
1036 | # | |
1052 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1037 | # The %reset command will flush this cache. See the cache_main_mod() | |
1053 | # and clear_main_mod_cache() methods for details on use. |
|
1038 | # and clear_main_mod_cache() methods for details on use. | |
1054 |
|
1039 | |||
1055 | # This is the cache used for 'main' namespaces |
|
1040 | # This is the cache used for 'main' namespaces | |
1056 | self._main_mod_cache = {} |
|
1041 | self._main_mod_cache = {} | |
1057 |
|
1042 | |||
1058 | # A table holding all the namespaces IPython deals with, so that |
|
1043 | # A table holding all the namespaces IPython deals with, so that | |
1059 | # introspection facilities can search easily. |
|
1044 | # introspection facilities can search easily. | |
1060 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1045 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1061 | 'user_local':self.user_ns, |
|
1046 | 'user_local':self.user_ns, | |
1062 | 'builtin':builtin_mod.__dict__ |
|
1047 | 'builtin':builtin_mod.__dict__ | |
1063 | } |
|
1048 | } | |
1064 |
|
1049 | |||
1065 | @property |
|
1050 | @property | |
1066 | def user_global_ns(self): |
|
1051 | def user_global_ns(self): | |
1067 | return self.user_module.__dict__ |
|
1052 | return self.user_module.__dict__ | |
1068 |
|
1053 | |||
1069 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1054 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1070 | """Prepare the module and namespace in which user code will be run. |
|
1055 | """Prepare the module and namespace in which user code will be run. | |
1071 |
|
1056 | |||
1072 | When IPython is started normally, both parameters are None: a new module |
|
1057 | When IPython is started normally, both parameters are None: a new module | |
1073 | is created automatically, and its __dict__ used as the namespace. |
|
1058 | is created automatically, and its __dict__ used as the namespace. | |
1074 |
|
1059 | |||
1075 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1060 | If only user_module is provided, its __dict__ is used as the namespace. | |
1076 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1061 | If only user_ns is provided, a dummy module is created, and user_ns | |
1077 | becomes the global namespace. If both are provided (as they may be |
|
1062 | becomes the global namespace. If both are provided (as they may be | |
1078 | when embedding), user_ns is the local namespace, and user_module |
|
1063 | when embedding), user_ns is the local namespace, and user_module | |
1079 | provides the global namespace. |
|
1064 | provides the global namespace. | |
1080 |
|
1065 | |||
1081 | Parameters |
|
1066 | Parameters | |
1082 | ---------- |
|
1067 | ---------- | |
1083 | user_module : module, optional |
|
1068 | user_module : module, optional | |
1084 | The current user module in which IPython is being run. If None, |
|
1069 | The current user module in which IPython is being run. If None, | |
1085 | a clean module will be created. |
|
1070 | a clean module will be created. | |
1086 | user_ns : dict, optional |
|
1071 | user_ns : dict, optional | |
1087 | A namespace in which to run interactive commands. |
|
1072 | A namespace in which to run interactive commands. | |
1088 |
|
1073 | |||
1089 | Returns |
|
1074 | Returns | |
1090 | ------- |
|
1075 | ------- | |
1091 | A tuple of user_module and user_ns, each properly initialised. |
|
1076 | A tuple of user_module and user_ns, each properly initialised. | |
1092 | """ |
|
1077 | """ | |
1093 | if user_module is None and user_ns is not None: |
|
1078 | if user_module is None and user_ns is not None: | |
1094 | user_ns.setdefault("__name__", "__main__") |
|
1079 | user_ns.setdefault("__name__", "__main__") | |
1095 | user_module = DummyMod() |
|
1080 | user_module = DummyMod() | |
1096 | user_module.__dict__ = user_ns |
|
1081 | user_module.__dict__ = user_ns | |
1097 |
|
1082 | |||
1098 | if user_module is None: |
|
1083 | if user_module is None: | |
1099 | user_module = types.ModuleType("__main__", |
|
1084 | user_module = types.ModuleType("__main__", | |
1100 | doc="Automatically created module for IPython interactive environment") |
|
1085 | doc="Automatically created module for IPython interactive environment") | |
1101 |
|
1086 | |||
1102 | # We must ensure that __builtin__ (without the final 's') is always |
|
1087 | # We must ensure that __builtin__ (without the final 's') is always | |
1103 | # available and pointing to the __builtin__ *module*. For more details: |
|
1088 | # available and pointing to the __builtin__ *module*. For more details: | |
1104 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1089 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1105 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1090 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1106 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1091 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1107 |
|
1092 | |||
1108 | if user_ns is None: |
|
1093 | if user_ns is None: | |
1109 | user_ns = user_module.__dict__ |
|
1094 | user_ns = user_module.__dict__ | |
1110 |
|
1095 | |||
1111 | return user_module, user_ns |
|
1096 | return user_module, user_ns | |
1112 |
|
1097 | |||
1113 | def init_sys_modules(self): |
|
1098 | def init_sys_modules(self): | |
1114 | # We need to insert into sys.modules something that looks like a |
|
1099 | # We need to insert into sys.modules something that looks like a | |
1115 | # module but which accesses the IPython namespace, for shelve and |
|
1100 | # module but which accesses the IPython namespace, for shelve and | |
1116 | # pickle to work interactively. Normally they rely on getting |
|
1101 | # pickle to work interactively. Normally they rely on getting | |
1117 | # everything out of __main__, but for embedding purposes each IPython |
|
1102 | # everything out of __main__, but for embedding purposes each IPython | |
1118 | # instance has its own private namespace, so we can't go shoving |
|
1103 | # instance has its own private namespace, so we can't go shoving | |
1119 | # everything into __main__. |
|
1104 | # everything into __main__. | |
1120 |
|
1105 | |||
1121 | # note, however, that we should only do this for non-embedded |
|
1106 | # note, however, that we should only do this for non-embedded | |
1122 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1107 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1123 | # namespace. Embedded instances, on the other hand, should not do |
|
1108 | # namespace. Embedded instances, on the other hand, should not do | |
1124 | # this because they need to manage the user local/global namespaces |
|
1109 | # this because they need to manage the user local/global namespaces | |
1125 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1110 | # only, but they live within a 'normal' __main__ (meaning, they | |
1126 | # shouldn't overtake the execution environment of the script they're |
|
1111 | # shouldn't overtake the execution environment of the script they're | |
1127 | # embedded in). |
|
1112 | # embedded in). | |
1128 |
|
1113 | |||
1129 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1114 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1130 | main_name = self.user_module.__name__ |
|
1115 | main_name = self.user_module.__name__ | |
1131 | sys.modules[main_name] = self.user_module |
|
1116 | sys.modules[main_name] = self.user_module | |
1132 |
|
1117 | |||
1133 | def init_user_ns(self): |
|
1118 | def init_user_ns(self): | |
1134 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1119 | """Initialize all user-visible namespaces to their minimum defaults. | |
1135 |
|
1120 | |||
1136 | Certain history lists are also initialized here, as they effectively |
|
1121 | Certain history lists are also initialized here, as they effectively | |
1137 | act as user namespaces. |
|
1122 | act as user namespaces. | |
1138 |
|
1123 | |||
1139 | Notes |
|
1124 | Notes | |
1140 | ----- |
|
1125 | ----- | |
1141 | All data structures here are only filled in, they are NOT reset by this |
|
1126 | All data structures here are only filled in, they are NOT reset by this | |
1142 | method. If they were not empty before, data will simply be added to |
|
1127 | method. If they were not empty before, data will simply be added to | |
1143 | therm. |
|
1128 | therm. | |
1144 | """ |
|
1129 | """ | |
1145 | # This function works in two parts: first we put a few things in |
|
1130 | # This function works in two parts: first we put a few things in | |
1146 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1131 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1147 | # initial variables aren't shown by %who. After the sync, we add the |
|
1132 | # initial variables aren't shown by %who. After the sync, we add the | |
1148 | # rest of what we *do* want the user to see with %who even on a new |
|
1133 | # rest of what we *do* want the user to see with %who even on a new | |
1149 | # session (probably nothing, so they really only see their own stuff) |
|
1134 | # session (probably nothing, so they really only see their own stuff) | |
1150 |
|
1135 | |||
1151 | # The user dict must *always* have a __builtin__ reference to the |
|
1136 | # The user dict must *always* have a __builtin__ reference to the | |
1152 | # Python standard __builtin__ namespace, which must be imported. |
|
1137 | # Python standard __builtin__ namespace, which must be imported. | |
1153 | # This is so that certain operations in prompt evaluation can be |
|
1138 | # This is so that certain operations in prompt evaluation can be | |
1154 | # reliably executed with builtins. Note that we can NOT use |
|
1139 | # reliably executed with builtins. Note that we can NOT use | |
1155 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1140 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1156 | # module, and can even mutate at runtime, depending on the context |
|
1141 | # module, and can even mutate at runtime, depending on the context | |
1157 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1142 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1158 | # always a module object, though it must be explicitly imported. |
|
1143 | # always a module object, though it must be explicitly imported. | |
1159 |
|
1144 | |||
1160 | # For more details: |
|
1145 | # For more details: | |
1161 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1146 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1162 | ns = dict() |
|
1147 | ns = dict() | |
1163 |
|
1148 | |||
1164 | # make global variables for user access to the histories |
|
1149 | # make global variables for user access to the histories | |
1165 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1150 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1166 | ns['_oh'] = self.history_manager.output_hist |
|
1151 | ns['_oh'] = self.history_manager.output_hist | |
1167 | ns['_dh'] = self.history_manager.dir_hist |
|
1152 | ns['_dh'] = self.history_manager.dir_hist | |
1168 |
|
1153 | |||
1169 | ns['_sh'] = shadowns |
|
1154 | ns['_sh'] = shadowns | |
1170 |
|
1155 | |||
1171 | # user aliases to input and output histories. These shouldn't show up |
|
1156 | # user aliases to input and output histories. These shouldn't show up | |
1172 | # in %who, as they can have very large reprs. |
|
1157 | # in %who, as they can have very large reprs. | |
1173 | ns['In'] = self.history_manager.input_hist_parsed |
|
1158 | ns['In'] = self.history_manager.input_hist_parsed | |
1174 | ns['Out'] = self.history_manager.output_hist |
|
1159 | ns['Out'] = self.history_manager.output_hist | |
1175 |
|
1160 | |||
1176 | # Store myself as the public api!!! |
|
1161 | # Store myself as the public api!!! | |
1177 | ns['get_ipython'] = self.get_ipython |
|
1162 | ns['get_ipython'] = self.get_ipython | |
1178 |
|
1163 | |||
1179 | ns['exit'] = self.exiter |
|
1164 | ns['exit'] = self.exiter | |
1180 | ns['quit'] = self.exiter |
|
1165 | ns['quit'] = self.exiter | |
1181 |
|
1166 | |||
1182 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1167 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1183 | # by %who |
|
1168 | # by %who | |
1184 | self.user_ns_hidden.update(ns) |
|
1169 | self.user_ns_hidden.update(ns) | |
1185 |
|
1170 | |||
1186 | # Anything put into ns now would show up in %who. Think twice before |
|
1171 | # Anything put into ns now would show up in %who. Think twice before | |
1187 | # putting anything here, as we really want %who to show the user their |
|
1172 | # putting anything here, as we really want %who to show the user their | |
1188 | # stuff, not our variables. |
|
1173 | # stuff, not our variables. | |
1189 |
|
1174 | |||
1190 | # Finally, update the real user's namespace |
|
1175 | # Finally, update the real user's namespace | |
1191 | self.user_ns.update(ns) |
|
1176 | self.user_ns.update(ns) | |
1192 |
|
1177 | |||
1193 | @property |
|
1178 | @property | |
1194 | def all_ns_refs(self): |
|
1179 | def all_ns_refs(self): | |
1195 | """Get a list of references to all the namespace dictionaries in which |
|
1180 | """Get a list of references to all the namespace dictionaries in which | |
1196 | IPython might store a user-created object. |
|
1181 | IPython might store a user-created object. | |
1197 |
|
1182 | |||
1198 | Note that this does not include the displayhook, which also caches |
|
1183 | Note that this does not include the displayhook, which also caches | |
1199 | objects from the output.""" |
|
1184 | objects from the output.""" | |
1200 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1185 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1201 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1186 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1202 |
|
1187 | |||
1203 | def reset(self, new_session=True): |
|
1188 | def reset(self, new_session=True): | |
1204 | """Clear all internal namespaces, and attempt to release references to |
|
1189 | """Clear all internal namespaces, and attempt to release references to | |
1205 | user objects. |
|
1190 | user objects. | |
1206 |
|
1191 | |||
1207 | If new_session is True, a new history session will be opened. |
|
1192 | If new_session is True, a new history session will be opened. | |
1208 | """ |
|
1193 | """ | |
1209 | # Clear histories |
|
1194 | # Clear histories | |
1210 | self.history_manager.reset(new_session) |
|
1195 | self.history_manager.reset(new_session) | |
1211 | # Reset counter used to index all histories |
|
1196 | # Reset counter used to index all histories | |
1212 | if new_session: |
|
1197 | if new_session: | |
1213 | self.execution_count = 1 |
|
1198 | self.execution_count = 1 | |
1214 |
|
1199 | |||
1215 | # Flush cached output items |
|
1200 | # Flush cached output items | |
1216 | if self.displayhook.do_full_cache: |
|
1201 | if self.displayhook.do_full_cache: | |
1217 | self.displayhook.flush() |
|
1202 | self.displayhook.flush() | |
1218 |
|
1203 | |||
1219 | # The main execution namespaces must be cleared very carefully, |
|
1204 | # The main execution namespaces must be cleared very carefully, | |
1220 | # skipping the deletion of the builtin-related keys, because doing so |
|
1205 | # skipping the deletion of the builtin-related keys, because doing so | |
1221 | # would cause errors in many object's __del__ methods. |
|
1206 | # would cause errors in many object's __del__ methods. | |
1222 | if self.user_ns is not self.user_global_ns: |
|
1207 | if self.user_ns is not self.user_global_ns: | |
1223 | self.user_ns.clear() |
|
1208 | self.user_ns.clear() | |
1224 | ns = self.user_global_ns |
|
1209 | ns = self.user_global_ns | |
1225 | drop_keys = set(ns.keys()) |
|
1210 | drop_keys = set(ns.keys()) | |
1226 | drop_keys.discard('__builtin__') |
|
1211 | drop_keys.discard('__builtin__') | |
1227 | drop_keys.discard('__builtins__') |
|
1212 | drop_keys.discard('__builtins__') | |
1228 | drop_keys.discard('__name__') |
|
1213 | drop_keys.discard('__name__') | |
1229 | for k in drop_keys: |
|
1214 | for k in drop_keys: | |
1230 | del ns[k] |
|
1215 | del ns[k] | |
1231 |
|
1216 | |||
1232 | self.user_ns_hidden.clear() |
|
1217 | self.user_ns_hidden.clear() | |
1233 |
|
1218 | |||
1234 | # Restore the user namespaces to minimal usability |
|
1219 | # Restore the user namespaces to minimal usability | |
1235 | self.init_user_ns() |
|
1220 | self.init_user_ns() | |
1236 |
|
1221 | |||
1237 | # Restore the default and user aliases |
|
1222 | # Restore the default and user aliases | |
1238 | self.alias_manager.clear_aliases() |
|
1223 | self.alias_manager.clear_aliases() | |
1239 | self.alias_manager.init_aliases() |
|
1224 | self.alias_manager.init_aliases() | |
1240 |
|
1225 | |||
1241 | # Flush the private list of module references kept for script |
|
1226 | # Flush the private list of module references kept for script | |
1242 | # execution protection |
|
1227 | # execution protection | |
1243 | self.clear_main_mod_cache() |
|
1228 | self.clear_main_mod_cache() | |
1244 |
|
1229 | |||
1245 | def del_var(self, varname, by_name=False): |
|
1230 | def del_var(self, varname, by_name=False): | |
1246 | """Delete a variable from the various namespaces, so that, as |
|
1231 | """Delete a variable from the various namespaces, so that, as | |
1247 | far as possible, we're not keeping any hidden references to it. |
|
1232 | far as possible, we're not keeping any hidden references to it. | |
1248 |
|
1233 | |||
1249 | Parameters |
|
1234 | Parameters | |
1250 | ---------- |
|
1235 | ---------- | |
1251 | varname : str |
|
1236 | varname : str | |
1252 | The name of the variable to delete. |
|
1237 | The name of the variable to delete. | |
1253 | by_name : bool |
|
1238 | by_name : bool | |
1254 | If True, delete variables with the given name in each |
|
1239 | If True, delete variables with the given name in each | |
1255 | namespace. If False (default), find the variable in the user |
|
1240 | namespace. If False (default), find the variable in the user | |
1256 | namespace, and delete references to it. |
|
1241 | namespace, and delete references to it. | |
1257 | """ |
|
1242 | """ | |
1258 | if varname in ('__builtin__', '__builtins__'): |
|
1243 | if varname in ('__builtin__', '__builtins__'): | |
1259 | raise ValueError("Refusing to delete %s" % varname) |
|
1244 | raise ValueError("Refusing to delete %s" % varname) | |
1260 |
|
1245 | |||
1261 | ns_refs = self.all_ns_refs |
|
1246 | ns_refs = self.all_ns_refs | |
1262 |
|
1247 | |||
1263 | if by_name: # Delete by name |
|
1248 | if by_name: # Delete by name | |
1264 | for ns in ns_refs: |
|
1249 | for ns in ns_refs: | |
1265 | try: |
|
1250 | try: | |
1266 | del ns[varname] |
|
1251 | del ns[varname] | |
1267 | except KeyError: |
|
1252 | except KeyError: | |
1268 | pass |
|
1253 | pass | |
1269 | else: # Delete by object |
|
1254 | else: # Delete by object | |
1270 | try: |
|
1255 | try: | |
1271 | obj = self.user_ns[varname] |
|
1256 | obj = self.user_ns[varname] | |
1272 | except KeyError: |
|
1257 | except KeyError: | |
1273 | raise NameError("name '%s' is not defined" % varname) |
|
1258 | raise NameError("name '%s' is not defined" % varname) | |
1274 | # Also check in output history |
|
1259 | # Also check in output history | |
1275 | ns_refs.append(self.history_manager.output_hist) |
|
1260 | ns_refs.append(self.history_manager.output_hist) | |
1276 | for ns in ns_refs: |
|
1261 | for ns in ns_refs: | |
1277 | to_delete = [n for n, o in iteritems(ns) if o is obj] |
|
1262 | to_delete = [n for n, o in iteritems(ns) if o is obj] | |
1278 | for name in to_delete: |
|
1263 | for name in to_delete: | |
1279 | del ns[name] |
|
1264 | del ns[name] | |
1280 |
|
1265 | |||
1281 | # displayhook keeps extra references, but not in a dictionary |
|
1266 | # displayhook keeps extra references, but not in a dictionary | |
1282 | for name in ('_', '__', '___'): |
|
1267 | for name in ('_', '__', '___'): | |
1283 | if getattr(self.displayhook, name) is obj: |
|
1268 | if getattr(self.displayhook, name) is obj: | |
1284 | setattr(self.displayhook, name, None) |
|
1269 | setattr(self.displayhook, name, None) | |
1285 |
|
1270 | |||
1286 | def reset_selective(self, regex=None): |
|
1271 | def reset_selective(self, regex=None): | |
1287 | """Clear selective variables from internal namespaces based on a |
|
1272 | """Clear selective variables from internal namespaces based on a | |
1288 | specified regular expression. |
|
1273 | specified regular expression. | |
1289 |
|
1274 | |||
1290 | Parameters |
|
1275 | Parameters | |
1291 | ---------- |
|
1276 | ---------- | |
1292 | regex : string or compiled pattern, optional |
|
1277 | regex : string or compiled pattern, optional | |
1293 | A regular expression pattern that will be used in searching |
|
1278 | A regular expression pattern that will be used in searching | |
1294 | variable names in the users namespaces. |
|
1279 | variable names in the users namespaces. | |
1295 | """ |
|
1280 | """ | |
1296 | if regex is not None: |
|
1281 | if regex is not None: | |
1297 | try: |
|
1282 | try: | |
1298 | m = re.compile(regex) |
|
1283 | m = re.compile(regex) | |
1299 | except TypeError: |
|
1284 | except TypeError: | |
1300 | raise TypeError('regex must be a string or compiled pattern') |
|
1285 | raise TypeError('regex must be a string or compiled pattern') | |
1301 | # Search for keys in each namespace that match the given regex |
|
1286 | # Search for keys in each namespace that match the given regex | |
1302 | # If a match is found, delete the key/value pair. |
|
1287 | # If a match is found, delete the key/value pair. | |
1303 | for ns in self.all_ns_refs: |
|
1288 | for ns in self.all_ns_refs: | |
1304 | for var in ns: |
|
1289 | for var in ns: | |
1305 | if m.search(var): |
|
1290 | if m.search(var): | |
1306 | del ns[var] |
|
1291 | del ns[var] | |
1307 |
|
1292 | |||
1308 | def push(self, variables, interactive=True): |
|
1293 | def push(self, variables, interactive=True): | |
1309 | """Inject a group of variables into the IPython user namespace. |
|
1294 | """Inject a group of variables into the IPython user namespace. | |
1310 |
|
1295 | |||
1311 | Parameters |
|
1296 | Parameters | |
1312 | ---------- |
|
1297 | ---------- | |
1313 | variables : dict, str or list/tuple of str |
|
1298 | variables : dict, str or list/tuple of str | |
1314 | The variables to inject into the user's namespace. If a dict, a |
|
1299 | The variables to inject into the user's namespace. If a dict, a | |
1315 | simple update is done. If a str, the string is assumed to have |
|
1300 | simple update is done. If a str, the string is assumed to have | |
1316 | variable names separated by spaces. A list/tuple of str can also |
|
1301 | variable names separated by spaces. A list/tuple of str can also | |
1317 | be used to give the variable names. If just the variable names are |
|
1302 | be used to give the variable names. If just the variable names are | |
1318 | give (list/tuple/str) then the variable values looked up in the |
|
1303 | give (list/tuple/str) then the variable values looked up in the | |
1319 | callers frame. |
|
1304 | callers frame. | |
1320 | interactive : bool |
|
1305 | interactive : bool | |
1321 | If True (default), the variables will be listed with the ``who`` |
|
1306 | If True (default), the variables will be listed with the ``who`` | |
1322 | magic. |
|
1307 | magic. | |
1323 | """ |
|
1308 | """ | |
1324 | vdict = None |
|
1309 | vdict = None | |
1325 |
|
1310 | |||
1326 | # We need a dict of name/value pairs to do namespace updates. |
|
1311 | # We need a dict of name/value pairs to do namespace updates. | |
1327 | if isinstance(variables, dict): |
|
1312 | if isinstance(variables, dict): | |
1328 | vdict = variables |
|
1313 | vdict = variables | |
1329 | elif isinstance(variables, string_types+(list, tuple)): |
|
1314 | elif isinstance(variables, string_types+(list, tuple)): | |
1330 | if isinstance(variables, string_types): |
|
1315 | if isinstance(variables, string_types): | |
1331 | vlist = variables.split() |
|
1316 | vlist = variables.split() | |
1332 | else: |
|
1317 | else: | |
1333 | vlist = variables |
|
1318 | vlist = variables | |
1334 | vdict = {} |
|
1319 | vdict = {} | |
1335 | cf = sys._getframe(1) |
|
1320 | cf = sys._getframe(1) | |
1336 | for name in vlist: |
|
1321 | for name in vlist: | |
1337 | try: |
|
1322 | try: | |
1338 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1323 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1339 | except: |
|
1324 | except: | |
1340 | print('Could not get variable %s from %s' % |
|
1325 | print('Could not get variable %s from %s' % | |
1341 | (name,cf.f_code.co_name)) |
|
1326 | (name,cf.f_code.co_name)) | |
1342 | else: |
|
1327 | else: | |
1343 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1328 | raise ValueError('variables must be a dict/str/list/tuple') | |
1344 |
|
1329 | |||
1345 | # Propagate variables to user namespace |
|
1330 | # Propagate variables to user namespace | |
1346 | self.user_ns.update(vdict) |
|
1331 | self.user_ns.update(vdict) | |
1347 |
|
1332 | |||
1348 | # And configure interactive visibility |
|
1333 | # And configure interactive visibility | |
1349 | user_ns_hidden = self.user_ns_hidden |
|
1334 | user_ns_hidden = self.user_ns_hidden | |
1350 | if interactive: |
|
1335 | if interactive: | |
1351 | for name in vdict: |
|
1336 | for name in vdict: | |
1352 | user_ns_hidden.pop(name, None) |
|
1337 | user_ns_hidden.pop(name, None) | |
1353 | else: |
|
1338 | else: | |
1354 | user_ns_hidden.update(vdict) |
|
1339 | user_ns_hidden.update(vdict) | |
1355 |
|
1340 | |||
1356 | def drop_by_id(self, variables): |
|
1341 | def drop_by_id(self, variables): | |
1357 | """Remove a dict of variables from the user namespace, if they are the |
|
1342 | """Remove a dict of variables from the user namespace, if they are the | |
1358 | same as the values in the dictionary. |
|
1343 | same as the values in the dictionary. | |
1359 |
|
1344 | |||
1360 | This is intended for use by extensions: variables that they've added can |
|
1345 | This is intended for use by extensions: variables that they've added can | |
1361 | be taken back out if they are unloaded, without removing any that the |
|
1346 | be taken back out if they are unloaded, without removing any that the | |
1362 | user has overwritten. |
|
1347 | user has overwritten. | |
1363 |
|
1348 | |||
1364 | Parameters |
|
1349 | Parameters | |
1365 | ---------- |
|
1350 | ---------- | |
1366 | variables : dict |
|
1351 | variables : dict | |
1367 | A dictionary mapping object names (as strings) to the objects. |
|
1352 | A dictionary mapping object names (as strings) to the objects. | |
1368 | """ |
|
1353 | """ | |
1369 | for name, obj in iteritems(variables): |
|
1354 | for name, obj in iteritems(variables): | |
1370 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1355 | if name in self.user_ns and self.user_ns[name] is obj: | |
1371 | del self.user_ns[name] |
|
1356 | del self.user_ns[name] | |
1372 | self.user_ns_hidden.pop(name, None) |
|
1357 | self.user_ns_hidden.pop(name, None) | |
1373 |
|
1358 | |||
1374 | #------------------------------------------------------------------------- |
|
1359 | #------------------------------------------------------------------------- | |
1375 | # Things related to object introspection |
|
1360 | # Things related to object introspection | |
1376 | #------------------------------------------------------------------------- |
|
1361 | #------------------------------------------------------------------------- | |
1377 |
|
1362 | |||
1378 | def _ofind(self, oname, namespaces=None): |
|
1363 | def _ofind(self, oname, namespaces=None): | |
1379 | """Find an object in the available namespaces. |
|
1364 | """Find an object in the available namespaces. | |
1380 |
|
1365 | |||
1381 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1366 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1382 |
|
1367 | |||
1383 | Has special code to detect magic functions. |
|
1368 | Has special code to detect magic functions. | |
1384 | """ |
|
1369 | """ | |
1385 | oname = oname.strip() |
|
1370 | oname = oname.strip() | |
1386 | #print '1- oname: <%r>' % oname # dbg |
|
1371 | #print '1- oname: <%r>' % oname # dbg | |
1387 | if not oname.startswith(ESC_MAGIC) and \ |
|
1372 | if not oname.startswith(ESC_MAGIC) and \ | |
1388 | not oname.startswith(ESC_MAGIC2) and \ |
|
1373 | not oname.startswith(ESC_MAGIC2) and \ | |
1389 | not py3compat.isidentifier(oname, dotted=True): |
|
1374 | not py3compat.isidentifier(oname, dotted=True): | |
1390 | return dict(found=False) |
|
1375 | return dict(found=False) | |
1391 |
|
1376 | |||
1392 | if namespaces is None: |
|
1377 | if namespaces is None: | |
1393 | # Namespaces to search in: |
|
1378 | # Namespaces to search in: | |
1394 | # Put them in a list. The order is important so that we |
|
1379 | # Put them in a list. The order is important so that we | |
1395 | # find things in the same order that Python finds them. |
|
1380 | # find things in the same order that Python finds them. | |
1396 | namespaces = [ ('Interactive', self.user_ns), |
|
1381 | namespaces = [ ('Interactive', self.user_ns), | |
1397 | ('Interactive (global)', self.user_global_ns), |
|
1382 | ('Interactive (global)', self.user_global_ns), | |
1398 | ('Python builtin', builtin_mod.__dict__), |
|
1383 | ('Python builtin', builtin_mod.__dict__), | |
1399 | ] |
|
1384 | ] | |
1400 |
|
1385 | |||
1401 | # initialize results to 'null' |
|
1386 | # initialize results to 'null' | |
1402 | found = False; obj = None; ospace = None; |
|
1387 | found = False; obj = None; ospace = None; | |
1403 | ismagic = False; isalias = False; parent = None |
|
1388 | ismagic = False; isalias = False; parent = None | |
1404 |
|
1389 | |||
1405 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1390 | # We need to special-case 'print', which as of python2.6 registers as a | |
1406 | # function but should only be treated as one if print_function was |
|
1391 | # function but should only be treated as one if print_function was | |
1407 | # loaded with a future import. In this case, just bail. |
|
1392 | # loaded with a future import. In this case, just bail. | |
1408 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1393 | if (oname == 'print' and not py3compat.PY3 and not \ | |
1409 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1394 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): | |
1410 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1395 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1411 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1396 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1412 |
|
1397 | |||
1413 | # Look for the given name by splitting it in parts. If the head is |
|
1398 | # Look for the given name by splitting it in parts. If the head is | |
1414 | # found, then we look for all the remaining parts as members, and only |
|
1399 | # found, then we look for all the remaining parts as members, and only | |
1415 | # declare success if we can find them all. |
|
1400 | # declare success if we can find them all. | |
1416 | oname_parts = oname.split('.') |
|
1401 | oname_parts = oname.split('.') | |
1417 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1402 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1418 | for nsname,ns in namespaces: |
|
1403 | for nsname,ns in namespaces: | |
1419 | try: |
|
1404 | try: | |
1420 | obj = ns[oname_head] |
|
1405 | obj = ns[oname_head] | |
1421 | except KeyError: |
|
1406 | except KeyError: | |
1422 | continue |
|
1407 | continue | |
1423 | else: |
|
1408 | else: | |
1424 | #print 'oname_rest:', oname_rest # dbg |
|
1409 | #print 'oname_rest:', oname_rest # dbg | |
1425 | for idx, part in enumerate(oname_rest): |
|
1410 | for idx, part in enumerate(oname_rest): | |
1426 | try: |
|
1411 | try: | |
1427 | parent = obj |
|
1412 | parent = obj | |
1428 | # The last part is looked up in a special way to avoid |
|
1413 | # The last part is looked up in a special way to avoid | |
1429 | # descriptor invocation as it may raise or have side |
|
1414 | # descriptor invocation as it may raise or have side | |
1430 | # effects. |
|
1415 | # effects. | |
1431 | if idx == len(oname_rest) - 1: |
|
1416 | if idx == len(oname_rest) - 1: | |
1432 | obj = self._getattr_property(obj, part) |
|
1417 | obj = self._getattr_property(obj, part) | |
1433 | else: |
|
1418 | else: | |
1434 | obj = getattr(obj, part) |
|
1419 | obj = getattr(obj, part) | |
1435 | except: |
|
1420 | except: | |
1436 | # Blanket except b/c some badly implemented objects |
|
1421 | # Blanket except b/c some badly implemented objects | |
1437 | # allow __getattr__ to raise exceptions other than |
|
1422 | # allow __getattr__ to raise exceptions other than | |
1438 | # AttributeError, which then crashes IPython. |
|
1423 | # AttributeError, which then crashes IPython. | |
1439 | break |
|
1424 | break | |
1440 | else: |
|
1425 | else: | |
1441 | # If we finish the for loop (no break), we got all members |
|
1426 | # If we finish the for loop (no break), we got all members | |
1442 | found = True |
|
1427 | found = True | |
1443 | ospace = nsname |
|
1428 | ospace = nsname | |
1444 | break # namespace loop |
|
1429 | break # namespace loop | |
1445 |
|
1430 | |||
1446 | # Try to see if it's magic |
|
1431 | # Try to see if it's magic | |
1447 | if not found: |
|
1432 | if not found: | |
1448 | obj = None |
|
1433 | obj = None | |
1449 | if oname.startswith(ESC_MAGIC2): |
|
1434 | if oname.startswith(ESC_MAGIC2): | |
1450 | oname = oname.lstrip(ESC_MAGIC2) |
|
1435 | oname = oname.lstrip(ESC_MAGIC2) | |
1451 | obj = self.find_cell_magic(oname) |
|
1436 | obj = self.find_cell_magic(oname) | |
1452 | elif oname.startswith(ESC_MAGIC): |
|
1437 | elif oname.startswith(ESC_MAGIC): | |
1453 | oname = oname.lstrip(ESC_MAGIC) |
|
1438 | oname = oname.lstrip(ESC_MAGIC) | |
1454 | obj = self.find_line_magic(oname) |
|
1439 | obj = self.find_line_magic(oname) | |
1455 | else: |
|
1440 | else: | |
1456 | # search without prefix, so run? will find %run? |
|
1441 | # search without prefix, so run? will find %run? | |
1457 | obj = self.find_line_magic(oname) |
|
1442 | obj = self.find_line_magic(oname) | |
1458 | if obj is None: |
|
1443 | if obj is None: | |
1459 | obj = self.find_cell_magic(oname) |
|
1444 | obj = self.find_cell_magic(oname) | |
1460 | if obj is not None: |
|
1445 | if obj is not None: | |
1461 | found = True |
|
1446 | found = True | |
1462 | ospace = 'IPython internal' |
|
1447 | ospace = 'IPython internal' | |
1463 | ismagic = True |
|
1448 | ismagic = True | |
1464 | isalias = isinstance(obj, Alias) |
|
1449 | isalias = isinstance(obj, Alias) | |
1465 |
|
1450 | |||
1466 | # Last try: special-case some literals like '', [], {}, etc: |
|
1451 | # Last try: special-case some literals like '', [], {}, etc: | |
1467 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1452 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1468 | obj = eval(oname_head) |
|
1453 | obj = eval(oname_head) | |
1469 | found = True |
|
1454 | found = True | |
1470 | ospace = 'Interactive' |
|
1455 | ospace = 'Interactive' | |
1471 |
|
1456 | |||
1472 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1457 | return {'found':found, 'obj':obj, 'namespace':ospace, | |
1473 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1458 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} | |
1474 |
|
1459 | |||
1475 | @staticmethod |
|
1460 | @staticmethod | |
1476 | def _getattr_property(obj, attrname): |
|
1461 | def _getattr_property(obj, attrname): | |
1477 | """Property-aware getattr to use in object finding. |
|
1462 | """Property-aware getattr to use in object finding. | |
1478 |
|
1463 | |||
1479 | If attrname represents a property, return it unevaluated (in case it has |
|
1464 | If attrname represents a property, return it unevaluated (in case it has | |
1480 | side effects or raises an error. |
|
1465 | side effects or raises an error. | |
1481 |
|
1466 | |||
1482 | """ |
|
1467 | """ | |
1483 | if not isinstance(obj, type): |
|
1468 | if not isinstance(obj, type): | |
1484 | try: |
|
1469 | try: | |
1485 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1470 | # `getattr(type(obj), attrname)` is not guaranteed to return | |
1486 | # `obj`, but does so for property: |
|
1471 | # `obj`, but does so for property: | |
1487 | # |
|
1472 | # | |
1488 | # property.__get__(self, None, cls) -> self |
|
1473 | # property.__get__(self, None, cls) -> self | |
1489 | # |
|
1474 | # | |
1490 | # The universal alternative is to traverse the mro manually |
|
1475 | # The universal alternative is to traverse the mro manually | |
1491 | # searching for attrname in class dicts. |
|
1476 | # searching for attrname in class dicts. | |
1492 | attr = getattr(type(obj), attrname) |
|
1477 | attr = getattr(type(obj), attrname) | |
1493 | except AttributeError: |
|
1478 | except AttributeError: | |
1494 | pass |
|
1479 | pass | |
1495 | else: |
|
1480 | else: | |
1496 | # This relies on the fact that data descriptors (with both |
|
1481 | # This relies on the fact that data descriptors (with both | |
1497 | # __get__ & __set__ magic methods) take precedence over |
|
1482 | # __get__ & __set__ magic methods) take precedence over | |
1498 | # instance-level attributes: |
|
1483 | # instance-level attributes: | |
1499 | # |
|
1484 | # | |
1500 | # class A(object): |
|
1485 | # class A(object): | |
1501 | # @property |
|
1486 | # @property | |
1502 | # def foobar(self): return 123 |
|
1487 | # def foobar(self): return 123 | |
1503 | # a = A() |
|
1488 | # a = A() | |
1504 | # a.__dict__['foobar'] = 345 |
|
1489 | # a.__dict__['foobar'] = 345 | |
1505 | # a.foobar # == 123 |
|
1490 | # a.foobar # == 123 | |
1506 | # |
|
1491 | # | |
1507 | # So, a property may be returned right away. |
|
1492 | # So, a property may be returned right away. | |
1508 | if isinstance(attr, property): |
|
1493 | if isinstance(attr, property): | |
1509 | return attr |
|
1494 | return attr | |
1510 |
|
1495 | |||
1511 | # Nothing helped, fall back. |
|
1496 | # Nothing helped, fall back. | |
1512 | return getattr(obj, attrname) |
|
1497 | return getattr(obj, attrname) | |
1513 |
|
1498 | |||
1514 | def _object_find(self, oname, namespaces=None): |
|
1499 | def _object_find(self, oname, namespaces=None): | |
1515 | """Find an object and return a struct with info about it.""" |
|
1500 | """Find an object and return a struct with info about it.""" | |
1516 | return Struct(self._ofind(oname, namespaces)) |
|
1501 | return Struct(self._ofind(oname, namespaces)) | |
1517 |
|
1502 | |||
1518 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1503 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1519 | """Generic interface to the inspector system. |
|
1504 | """Generic interface to the inspector system. | |
1520 |
|
1505 | |||
1521 | This function is meant to be called by pdef, pdoc & friends. |
|
1506 | This function is meant to be called by pdef, pdoc & friends. | |
1522 | """ |
|
1507 | """ | |
1523 | info = self._object_find(oname, namespaces) |
|
1508 | info = self._object_find(oname, namespaces) | |
1524 | docformat = sphinxify if self.sphinxify_docstring else None |
|
1509 | docformat = sphinxify if self.sphinxify_docstring else None | |
1525 | if info.found: |
|
1510 | if info.found: | |
1526 | pmethod = getattr(self.inspector, meth) |
|
1511 | pmethod = getattr(self.inspector, meth) | |
1527 | # TODO: only apply format_screen to the plain/text repr of the mime |
|
1512 | # TODO: only apply format_screen to the plain/text repr of the mime | |
1528 | # bundle. |
|
1513 | # bundle. | |
1529 | formatter = format_screen if info.ismagic else docformat |
|
1514 | formatter = format_screen if info.ismagic else docformat | |
1530 | if meth == 'pdoc': |
|
1515 | if meth == 'pdoc': | |
1531 | pmethod(info.obj, oname, formatter) |
|
1516 | pmethod(info.obj, oname, formatter) | |
1532 | elif meth == 'pinfo': |
|
1517 | elif meth == 'pinfo': | |
1533 | pmethod(info.obj, oname, formatter, info, |
|
1518 | pmethod(info.obj, oname, formatter, info, | |
1534 | enable_html_pager=self.enable_html_pager, **kw) |
|
1519 | enable_html_pager=self.enable_html_pager, **kw) | |
1535 | else: |
|
1520 | else: | |
1536 | pmethod(info.obj, oname) |
|
1521 | pmethod(info.obj, oname) | |
1537 | else: |
|
1522 | else: | |
1538 | print('Object `%s` not found.' % oname) |
|
1523 | print('Object `%s` not found.' % oname) | |
1539 | return 'not found' # so callers can take other action |
|
1524 | return 'not found' # so callers can take other action | |
1540 |
|
1525 | |||
1541 | def object_inspect(self, oname, detail_level=0): |
|
1526 | def object_inspect(self, oname, detail_level=0): | |
1542 | """Get object info about oname""" |
|
1527 | """Get object info about oname""" | |
1543 | with self.builtin_trap: |
|
1528 | with self.builtin_trap: | |
1544 | info = self._object_find(oname) |
|
1529 | info = self._object_find(oname) | |
1545 | if info.found: |
|
1530 | if info.found: | |
1546 | return self.inspector.info(info.obj, oname, info=info, |
|
1531 | return self.inspector.info(info.obj, oname, info=info, | |
1547 | detail_level=detail_level |
|
1532 | detail_level=detail_level | |
1548 | ) |
|
1533 | ) | |
1549 | else: |
|
1534 | else: | |
1550 | return oinspect.object_info(name=oname, found=False) |
|
1535 | return oinspect.object_info(name=oname, found=False) | |
1551 |
|
1536 | |||
1552 | def object_inspect_text(self, oname, detail_level=0): |
|
1537 | def object_inspect_text(self, oname, detail_level=0): | |
1553 | """Get object info as formatted text""" |
|
1538 | """Get object info as formatted text""" | |
1554 | return self.object_inspect_mime(oname, detail_level)['text/plain'] |
|
1539 | return self.object_inspect_mime(oname, detail_level)['text/plain'] | |
1555 |
|
1540 | |||
1556 | def object_inspect_mime(self, oname, detail_level=0): |
|
1541 | def object_inspect_mime(self, oname, detail_level=0): | |
1557 | """Get object info as a mimebundle of formatted representations. |
|
1542 | """Get object info as a mimebundle of formatted representations. | |
1558 |
|
1543 | |||
1559 | A mimebundle is a dictionary, keyed by mime-type. |
|
1544 | A mimebundle is a dictionary, keyed by mime-type. | |
1560 | It must always have the key `'text/plain'`. |
|
1545 | It must always have the key `'text/plain'`. | |
1561 | """ |
|
1546 | """ | |
1562 | with self.builtin_trap: |
|
1547 | with self.builtin_trap: | |
1563 | info = self._object_find(oname) |
|
1548 | info = self._object_find(oname) | |
1564 | if info.found: |
|
1549 | if info.found: | |
1565 | return self.inspector._get_info(info.obj, oname, info=info, |
|
1550 | return self.inspector._get_info(info.obj, oname, info=info, | |
1566 | detail_level=detail_level |
|
1551 | detail_level=detail_level | |
1567 | ) |
|
1552 | ) | |
1568 | else: |
|
1553 | else: | |
1569 | raise KeyError(oname) |
|
1554 | raise KeyError(oname) | |
1570 |
|
1555 | |||
1571 | #------------------------------------------------------------------------- |
|
1556 | #------------------------------------------------------------------------- | |
1572 | # Things related to history management |
|
1557 | # Things related to history management | |
1573 | #------------------------------------------------------------------------- |
|
1558 | #------------------------------------------------------------------------- | |
1574 |
|
1559 | |||
1575 | def init_history(self): |
|
1560 | def init_history(self): | |
1576 | """Sets up the command history, and starts regular autosaves.""" |
|
1561 | """Sets up the command history, and starts regular autosaves.""" | |
1577 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1562 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1578 | self.configurables.append(self.history_manager) |
|
1563 | self.configurables.append(self.history_manager) | |
1579 |
|
1564 | |||
1580 | #------------------------------------------------------------------------- |
|
1565 | #------------------------------------------------------------------------- | |
1581 | # Things related to exception handling and tracebacks (not debugging) |
|
1566 | # Things related to exception handling and tracebacks (not debugging) | |
1582 | #------------------------------------------------------------------------- |
|
1567 | #------------------------------------------------------------------------- | |
1583 |
|
1568 | |||
1584 | debugger_cls = Pdb |
|
1569 | debugger_cls = Pdb | |
1585 |
|
1570 | |||
1586 | def init_traceback_handlers(self, custom_exceptions): |
|
1571 | def init_traceback_handlers(self, custom_exceptions): | |
1587 | # Syntax error handler. |
|
1572 | # Syntax error handler. | |
1588 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1573 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') | |
1589 |
|
1574 | |||
1590 | # The interactive one is initialized with an offset, meaning we always |
|
1575 | # The interactive one is initialized with an offset, meaning we always | |
1591 | # want to remove the topmost item in the traceback, which is our own |
|
1576 | # want to remove the topmost item in the traceback, which is our own | |
1592 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1577 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
1593 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1578 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1594 | color_scheme='NoColor', |
|
1579 | color_scheme='NoColor', | |
1595 | tb_offset = 1, |
|
1580 | tb_offset = 1, | |
1596 | check_cache=check_linecache_ipython, |
|
1581 | check_cache=check_linecache_ipython, | |
1597 | debugger_cls=self.debugger_cls) |
|
1582 | debugger_cls=self.debugger_cls) | |
1598 |
|
1583 | |||
1599 | # The instance will store a pointer to the system-wide exception hook, |
|
1584 | # The instance will store a pointer to the system-wide exception hook, | |
1600 | # so that runtime code (such as magics) can access it. This is because |
|
1585 | # so that runtime code (such as magics) can access it. This is because | |
1601 | # during the read-eval loop, it may get temporarily overwritten. |
|
1586 | # during the read-eval loop, it may get temporarily overwritten. | |
1602 | self.sys_excepthook = sys.excepthook |
|
1587 | self.sys_excepthook = sys.excepthook | |
1603 |
|
1588 | |||
1604 | # and add any custom exception handlers the user may have specified |
|
1589 | # and add any custom exception handlers the user may have specified | |
1605 | self.set_custom_exc(*custom_exceptions) |
|
1590 | self.set_custom_exc(*custom_exceptions) | |
1606 |
|
1591 | |||
1607 | # Set the exception mode |
|
1592 | # Set the exception mode | |
1608 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1593 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1609 |
|
1594 | |||
1610 | def set_custom_exc(self, exc_tuple, handler): |
|
1595 | def set_custom_exc(self, exc_tuple, handler): | |
1611 | """set_custom_exc(exc_tuple, handler) |
|
1596 | """set_custom_exc(exc_tuple, handler) | |
1612 |
|
1597 | |||
1613 | Set a custom exception handler, which will be called if any of the |
|
1598 | Set a custom exception handler, which will be called if any of the | |
1614 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1599 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1615 | run_code() method). |
|
1600 | run_code() method). | |
1616 |
|
1601 | |||
1617 | Parameters |
|
1602 | Parameters | |
1618 | ---------- |
|
1603 | ---------- | |
1619 |
|
1604 | |||
1620 | exc_tuple : tuple of exception classes |
|
1605 | exc_tuple : tuple of exception classes | |
1621 | A *tuple* of exception classes, for which to call the defined |
|
1606 | A *tuple* of exception classes, for which to call the defined | |
1622 | handler. It is very important that you use a tuple, and NOT A |
|
1607 | handler. It is very important that you use a tuple, and NOT A | |
1623 | LIST here, because of the way Python's except statement works. If |
|
1608 | LIST here, because of the way Python's except statement works. If | |
1624 | you only want to trap a single exception, use a singleton tuple:: |
|
1609 | you only want to trap a single exception, use a singleton tuple:: | |
1625 |
|
1610 | |||
1626 | exc_tuple == (MyCustomException,) |
|
1611 | exc_tuple == (MyCustomException,) | |
1627 |
|
1612 | |||
1628 | handler : callable |
|
1613 | handler : callable | |
1629 | handler must have the following signature:: |
|
1614 | handler must have the following signature:: | |
1630 |
|
1615 | |||
1631 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1616 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1632 | ... |
|
1617 | ... | |
1633 | return structured_traceback |
|
1618 | return structured_traceback | |
1634 |
|
1619 | |||
1635 | Your handler must return a structured traceback (a list of strings), |
|
1620 | Your handler must return a structured traceback (a list of strings), | |
1636 | or None. |
|
1621 | or None. | |
1637 |
|
1622 | |||
1638 | This will be made into an instance method (via types.MethodType) |
|
1623 | This will be made into an instance method (via types.MethodType) | |
1639 | of IPython itself, and it will be called if any of the exceptions |
|
1624 | of IPython itself, and it will be called if any of the exceptions | |
1640 | listed in the exc_tuple are caught. If the handler is None, an |
|
1625 | listed in the exc_tuple are caught. If the handler is None, an | |
1641 | internal basic one is used, which just prints basic info. |
|
1626 | internal basic one is used, which just prints basic info. | |
1642 |
|
1627 | |||
1643 | To protect IPython from crashes, if your handler ever raises an |
|
1628 | To protect IPython from crashes, if your handler ever raises an | |
1644 | exception or returns an invalid result, it will be immediately |
|
1629 | exception or returns an invalid result, it will be immediately | |
1645 | disabled. |
|
1630 | disabled. | |
1646 |
|
1631 | |||
1647 | WARNING: by putting in your own exception handler into IPython's main |
|
1632 | WARNING: by putting in your own exception handler into IPython's main | |
1648 | execution loop, you run a very good chance of nasty crashes. This |
|
1633 | execution loop, you run a very good chance of nasty crashes. This | |
1649 | facility should only be used if you really know what you are doing.""" |
|
1634 | facility should only be used if you really know what you are doing.""" | |
1650 |
|
1635 | |||
1651 | assert type(exc_tuple)==type(()) , \ |
|
1636 | assert type(exc_tuple)==type(()) , \ | |
1652 | "The custom exceptions must be given AS A TUPLE." |
|
1637 | "The custom exceptions must be given AS A TUPLE." | |
1653 |
|
1638 | |||
1654 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
|
1639 | def dummy_handler(self, etype, value, tb, tb_offset=None): | |
1655 | print('*** Simple custom exception handler ***') |
|
1640 | print('*** Simple custom exception handler ***') | |
1656 | print('Exception type :',etype) |
|
1641 | print('Exception type :',etype) | |
1657 | print('Exception value:',value) |
|
1642 | print('Exception value:',value) | |
1658 | print('Traceback :',tb) |
|
1643 | print('Traceback :',tb) | |
1659 | #print 'Source code :','\n'.join(self.buffer) |
|
1644 | #print 'Source code :','\n'.join(self.buffer) | |
1660 |
|
1645 | |||
1661 | def validate_stb(stb): |
|
1646 | def validate_stb(stb): | |
1662 | """validate structured traceback return type |
|
1647 | """validate structured traceback return type | |
1663 |
|
1648 | |||
1664 | return type of CustomTB *should* be a list of strings, but allow |
|
1649 | return type of CustomTB *should* be a list of strings, but allow | |
1665 | single strings or None, which are harmless. |
|
1650 | single strings or None, which are harmless. | |
1666 |
|
1651 | |||
1667 | This function will *always* return a list of strings, |
|
1652 | This function will *always* return a list of strings, | |
1668 | and will raise a TypeError if stb is inappropriate. |
|
1653 | and will raise a TypeError if stb is inappropriate. | |
1669 | """ |
|
1654 | """ | |
1670 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1655 | msg = "CustomTB must return list of strings, not %r" % stb | |
1671 | if stb is None: |
|
1656 | if stb is None: | |
1672 | return [] |
|
1657 | return [] | |
1673 | elif isinstance(stb, string_types): |
|
1658 | elif isinstance(stb, string_types): | |
1674 | return [stb] |
|
1659 | return [stb] | |
1675 | elif not isinstance(stb, list): |
|
1660 | elif not isinstance(stb, list): | |
1676 | raise TypeError(msg) |
|
1661 | raise TypeError(msg) | |
1677 | # it's a list |
|
1662 | # it's a list | |
1678 | for line in stb: |
|
1663 | for line in stb: | |
1679 | # check every element |
|
1664 | # check every element | |
1680 | if not isinstance(line, string_types): |
|
1665 | if not isinstance(line, string_types): | |
1681 | raise TypeError(msg) |
|
1666 | raise TypeError(msg) | |
1682 | return stb |
|
1667 | return stb | |
1683 |
|
1668 | |||
1684 | if handler is None: |
|
1669 | if handler is None: | |
1685 | wrapped = dummy_handler |
|
1670 | wrapped = dummy_handler | |
1686 | else: |
|
1671 | else: | |
1687 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1672 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1688 | """wrap CustomTB handler, to protect IPython from user code |
|
1673 | """wrap CustomTB handler, to protect IPython from user code | |
1689 |
|
1674 | |||
1690 | This makes it harder (but not impossible) for custom exception |
|
1675 | This makes it harder (but not impossible) for custom exception | |
1691 | handlers to crash IPython. |
|
1676 | handlers to crash IPython. | |
1692 | """ |
|
1677 | """ | |
1693 | try: |
|
1678 | try: | |
1694 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1679 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1695 | return validate_stb(stb) |
|
1680 | return validate_stb(stb) | |
1696 | except: |
|
1681 | except: | |
1697 | # clear custom handler immediately |
|
1682 | # clear custom handler immediately | |
1698 | self.set_custom_exc((), None) |
|
1683 | self.set_custom_exc((), None) | |
1699 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1684 | print("Custom TB Handler failed, unregistering", file=sys.stderr) | |
1700 | # show the exception in handler first |
|
1685 | # show the exception in handler first | |
1701 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1686 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1702 | print(self.InteractiveTB.stb2text(stb)) |
|
1687 | print(self.InteractiveTB.stb2text(stb)) | |
1703 | print("The original exception:") |
|
1688 | print("The original exception:") | |
1704 | stb = self.InteractiveTB.structured_traceback( |
|
1689 | stb = self.InteractiveTB.structured_traceback( | |
1705 | (etype,value,tb), tb_offset=tb_offset |
|
1690 | (etype,value,tb), tb_offset=tb_offset | |
1706 | ) |
|
1691 | ) | |
1707 | return stb |
|
1692 | return stb | |
1708 |
|
1693 | |||
1709 | self.CustomTB = types.MethodType(wrapped,self) |
|
1694 | self.CustomTB = types.MethodType(wrapped,self) | |
1710 | self.custom_exceptions = exc_tuple |
|
1695 | self.custom_exceptions = exc_tuple | |
1711 |
|
1696 | |||
1712 | def excepthook(self, etype, value, tb): |
|
1697 | def excepthook(self, etype, value, tb): | |
1713 | """One more defense for GUI apps that call sys.excepthook. |
|
1698 | """One more defense for GUI apps that call sys.excepthook. | |
1714 |
|
1699 | |||
1715 | GUI frameworks like wxPython trap exceptions and call |
|
1700 | GUI frameworks like wxPython trap exceptions and call | |
1716 | sys.excepthook themselves. I guess this is a feature that |
|
1701 | sys.excepthook themselves. I guess this is a feature that | |
1717 | enables them to keep running after exceptions that would |
|
1702 | enables them to keep running after exceptions that would | |
1718 | otherwise kill their mainloop. This is a bother for IPython |
|
1703 | otherwise kill their mainloop. This is a bother for IPython | |
1719 | which excepts to catch all of the program exceptions with a try: |
|
1704 | which excepts to catch all of the program exceptions with a try: | |
1720 | except: statement. |
|
1705 | except: statement. | |
1721 |
|
1706 | |||
1722 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1707 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1723 | any app directly invokes sys.excepthook, it will look to the user like |
|
1708 | any app directly invokes sys.excepthook, it will look to the user like | |
1724 | IPython crashed. In order to work around this, we can disable the |
|
1709 | IPython crashed. In order to work around this, we can disable the | |
1725 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1710 | CrashHandler and replace it with this excepthook instead, which prints a | |
1726 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1711 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1727 | call sys.excepthook will generate a regular-looking exception from |
|
1712 | call sys.excepthook will generate a regular-looking exception from | |
1728 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1713 | IPython, and the CrashHandler will only be triggered by real IPython | |
1729 | crashes. |
|
1714 | crashes. | |
1730 |
|
1715 | |||
1731 | This hook should be used sparingly, only in places which are not likely |
|
1716 | This hook should be used sparingly, only in places which are not likely | |
1732 | to be true IPython errors. |
|
1717 | to be true IPython errors. | |
1733 | """ |
|
1718 | """ | |
1734 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1719 | self.showtraceback((etype, value, tb), tb_offset=0) | |
1735 |
|
1720 | |||
1736 | def _get_exc_info(self, exc_tuple=None): |
|
1721 | def _get_exc_info(self, exc_tuple=None): | |
1737 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1722 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1738 |
|
1723 | |||
1739 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1724 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1740 | from whichever source. |
|
1725 | from whichever source. | |
1741 |
|
1726 | |||
1742 | raises ValueError if none of these contain any information |
|
1727 | raises ValueError if none of these contain any information | |
1743 | """ |
|
1728 | """ | |
1744 | if exc_tuple is None: |
|
1729 | if exc_tuple is None: | |
1745 | etype, value, tb = sys.exc_info() |
|
1730 | etype, value, tb = sys.exc_info() | |
1746 | else: |
|
1731 | else: | |
1747 | etype, value, tb = exc_tuple |
|
1732 | etype, value, tb = exc_tuple | |
1748 |
|
1733 | |||
1749 | if etype is None: |
|
1734 | if etype is None: | |
1750 | if hasattr(sys, 'last_type'): |
|
1735 | if hasattr(sys, 'last_type'): | |
1751 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1736 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1752 | sys.last_traceback |
|
1737 | sys.last_traceback | |
1753 |
|
1738 | |||
1754 | if etype is None: |
|
1739 | if etype is None: | |
1755 | raise ValueError("No exception to find") |
|
1740 | raise ValueError("No exception to find") | |
1756 |
|
1741 | |||
1757 | # Now store the exception info in sys.last_type etc. |
|
1742 | # Now store the exception info in sys.last_type etc. | |
1758 | # WARNING: these variables are somewhat deprecated and not |
|
1743 | # WARNING: these variables are somewhat deprecated and not | |
1759 | # necessarily safe to use in a threaded environment, but tools |
|
1744 | # necessarily safe to use in a threaded environment, but tools | |
1760 | # like pdb depend on their existence, so let's set them. If we |
|
1745 | # like pdb depend on their existence, so let's set them. If we | |
1761 | # find problems in the field, we'll need to revisit their use. |
|
1746 | # find problems in the field, we'll need to revisit their use. | |
1762 | sys.last_type = etype |
|
1747 | sys.last_type = etype | |
1763 | sys.last_value = value |
|
1748 | sys.last_value = value | |
1764 | sys.last_traceback = tb |
|
1749 | sys.last_traceback = tb | |
1765 |
|
1750 | |||
1766 | return etype, value, tb |
|
1751 | return etype, value, tb | |
1767 |
|
1752 | |||
1768 | def show_usage_error(self, exc): |
|
1753 | def show_usage_error(self, exc): | |
1769 | """Show a short message for UsageErrors |
|
1754 | """Show a short message for UsageErrors | |
1770 |
|
1755 | |||
1771 | These are special exceptions that shouldn't show a traceback. |
|
1756 | These are special exceptions that shouldn't show a traceback. | |
1772 | """ |
|
1757 | """ | |
1773 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1758 | print("UsageError: %s" % exc, file=sys.stderr) | |
1774 |
|
1759 | |||
1775 | def get_exception_only(self, exc_tuple=None): |
|
1760 | def get_exception_only(self, exc_tuple=None): | |
1776 | """ |
|
1761 | """ | |
1777 | Return as a string (ending with a newline) the exception that |
|
1762 | Return as a string (ending with a newline) the exception that | |
1778 | just occurred, without any traceback. |
|
1763 | just occurred, without any traceback. | |
1779 | """ |
|
1764 | """ | |
1780 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1765 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1781 | msg = traceback.format_exception_only(etype, value) |
|
1766 | msg = traceback.format_exception_only(etype, value) | |
1782 | return ''.join(msg) |
|
1767 | return ''.join(msg) | |
1783 |
|
1768 | |||
1784 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1769 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, | |
1785 | exception_only=False): |
|
1770 | exception_only=False): | |
1786 | """Display the exception that just occurred. |
|
1771 | """Display the exception that just occurred. | |
1787 |
|
1772 | |||
1788 | If nothing is known about the exception, this is the method which |
|
1773 | If nothing is known about the exception, this is the method which | |
1789 | should be used throughout the code for presenting user tracebacks, |
|
1774 | should be used throughout the code for presenting user tracebacks, | |
1790 | rather than directly invoking the InteractiveTB object. |
|
1775 | rather than directly invoking the InteractiveTB object. | |
1791 |
|
1776 | |||
1792 | A specific showsyntaxerror() also exists, but this method can take |
|
1777 | A specific showsyntaxerror() also exists, but this method can take | |
1793 | care of calling it if needed, so unless you are explicitly catching a |
|
1778 | care of calling it if needed, so unless you are explicitly catching a | |
1794 | SyntaxError exception, don't try to analyze the stack manually and |
|
1779 | SyntaxError exception, don't try to analyze the stack manually and | |
1795 | simply call this method.""" |
|
1780 | simply call this method.""" | |
1796 |
|
1781 | |||
1797 | try: |
|
1782 | try: | |
1798 | try: |
|
1783 | try: | |
1799 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1784 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1800 | except ValueError: |
|
1785 | except ValueError: | |
1801 | print('No traceback available to show.', file=sys.stderr) |
|
1786 | print('No traceback available to show.', file=sys.stderr) | |
1802 | return |
|
1787 | return | |
1803 |
|
1788 | |||
1804 | if issubclass(etype, SyntaxError): |
|
1789 | if issubclass(etype, SyntaxError): | |
1805 | # Though this won't be called by syntax errors in the input |
|
1790 | # Though this won't be called by syntax errors in the input | |
1806 | # line, there may be SyntaxError cases with imported code. |
|
1791 | # line, there may be SyntaxError cases with imported code. | |
1807 | self.showsyntaxerror(filename) |
|
1792 | self.showsyntaxerror(filename) | |
1808 | elif etype is UsageError: |
|
1793 | elif etype is UsageError: | |
1809 | self.show_usage_error(value) |
|
1794 | self.show_usage_error(value) | |
1810 | else: |
|
1795 | else: | |
1811 | if exception_only: |
|
1796 | if exception_only: | |
1812 | stb = ['An exception has occurred, use %tb to see ' |
|
1797 | stb = ['An exception has occurred, use %tb to see ' | |
1813 | 'the full traceback.\n'] |
|
1798 | 'the full traceback.\n'] | |
1814 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1799 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1815 | value)) |
|
1800 | value)) | |
1816 | else: |
|
1801 | else: | |
1817 | try: |
|
1802 | try: | |
1818 | # Exception classes can customise their traceback - we |
|
1803 | # Exception classes can customise their traceback - we | |
1819 | # use this in IPython.parallel for exceptions occurring |
|
1804 | # use this in IPython.parallel for exceptions occurring | |
1820 | # in the engines. This should return a list of strings. |
|
1805 | # in the engines. This should return a list of strings. | |
1821 | stb = value._render_traceback_() |
|
1806 | stb = value._render_traceback_() | |
1822 | except Exception: |
|
1807 | except Exception: | |
1823 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1808 | stb = self.InteractiveTB.structured_traceback(etype, | |
1824 | value, tb, tb_offset=tb_offset) |
|
1809 | value, tb, tb_offset=tb_offset) | |
1825 |
|
1810 | |||
1826 | self._showtraceback(etype, value, stb) |
|
1811 | self._showtraceback(etype, value, stb) | |
1827 | if self.call_pdb: |
|
1812 | if self.call_pdb: | |
1828 | # drop into debugger |
|
1813 | # drop into debugger | |
1829 | self.debugger(force=True) |
|
1814 | self.debugger(force=True) | |
1830 | return |
|
1815 | return | |
1831 |
|
1816 | |||
1832 | # Actually show the traceback |
|
1817 | # Actually show the traceback | |
1833 | self._showtraceback(etype, value, stb) |
|
1818 | self._showtraceback(etype, value, stb) | |
1834 |
|
1819 | |||
1835 | except KeyboardInterrupt: |
|
1820 | except KeyboardInterrupt: | |
1836 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1821 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
1837 |
|
1822 | |||
1838 | def _showtraceback(self, etype, evalue, stb): |
|
1823 | def _showtraceback(self, etype, evalue, stb): | |
1839 | """Actually show a traceback. |
|
1824 | """Actually show a traceback. | |
1840 |
|
1825 | |||
1841 | Subclasses may override this method to put the traceback on a different |
|
1826 | Subclasses may override this method to put the traceback on a different | |
1842 | place, like a side channel. |
|
1827 | place, like a side channel. | |
1843 | """ |
|
1828 | """ | |
1844 | print(self.InteractiveTB.stb2text(stb)) |
|
1829 | print(self.InteractiveTB.stb2text(stb)) | |
1845 |
|
1830 | |||
1846 | def showsyntaxerror(self, filename=None): |
|
1831 | def showsyntaxerror(self, filename=None): | |
1847 | """Display the syntax error that just occurred. |
|
1832 | """Display the syntax error that just occurred. | |
1848 |
|
1833 | |||
1849 | This doesn't display a stack trace because there isn't one. |
|
1834 | This doesn't display a stack trace because there isn't one. | |
1850 |
|
1835 | |||
1851 | If a filename is given, it is stuffed in the exception instead |
|
1836 | If a filename is given, it is stuffed in the exception instead | |
1852 | of what was there before (because Python's parser always uses |
|
1837 | of what was there before (because Python's parser always uses | |
1853 | "<string>" when reading from a string). |
|
1838 | "<string>" when reading from a string). | |
1854 | """ |
|
1839 | """ | |
1855 | etype, value, last_traceback = self._get_exc_info() |
|
1840 | etype, value, last_traceback = self._get_exc_info() | |
1856 |
|
1841 | |||
1857 | if filename and issubclass(etype, SyntaxError): |
|
1842 | if filename and issubclass(etype, SyntaxError): | |
1858 | try: |
|
1843 | try: | |
1859 | value.filename = filename |
|
1844 | value.filename = filename | |
1860 | except: |
|
1845 | except: | |
1861 | # Not the format we expect; leave it alone |
|
1846 | # Not the format we expect; leave it alone | |
1862 | pass |
|
1847 | pass | |
1863 |
|
1848 | |||
1864 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1849 | stb = self.SyntaxTB.structured_traceback(etype, value, []) | |
1865 | self._showtraceback(etype, value, stb) |
|
1850 | self._showtraceback(etype, value, stb) | |
1866 |
|
1851 | |||
1867 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1852 | # This is overridden in TerminalInteractiveShell to show a message about | |
1868 | # the %paste magic. |
|
1853 | # the %paste magic. | |
1869 | def showindentationerror(self): |
|
1854 | def showindentationerror(self): | |
1870 | """Called by run_cell when there's an IndentationError in code entered |
|
1855 | """Called by run_cell when there's an IndentationError in code entered | |
1871 | at the prompt. |
|
1856 | at the prompt. | |
1872 |
|
1857 | |||
1873 | This is overridden in TerminalInteractiveShell to show a message about |
|
1858 | This is overridden in TerminalInteractiveShell to show a message about | |
1874 | the %paste magic.""" |
|
1859 | the %paste magic.""" | |
1875 | self.showsyntaxerror() |
|
1860 | self.showsyntaxerror() | |
1876 |
|
1861 | |||
1877 | #------------------------------------------------------------------------- |
|
1862 | #------------------------------------------------------------------------- | |
1878 | # Things related to readline |
|
1863 | # Things related to readline | |
1879 | #------------------------------------------------------------------------- |
|
1864 | #------------------------------------------------------------------------- | |
1880 |
|
1865 | |||
1881 | def init_readline(self): |
|
1866 | def init_readline(self): | |
1882 | """DEPRECATED |
|
1867 | """DEPRECATED | |
1883 |
|
1868 | |||
1884 | Moved to terminal subclass, here only to simplify the init logic.""" |
|
1869 | Moved to terminal subclass, here only to simplify the init logic.""" | |
1885 | # Set a number of methods that depend on readline to be no-op |
|
1870 | # Set a number of methods that depend on readline to be no-op | |
1886 | warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated', |
|
1871 | warnings.warn('`init_readline` is no-op since IPython 5.0 and is Deprecated', | |
1887 | DeprecationWarning, stacklevel=2) |
|
1872 | DeprecationWarning, stacklevel=2) | |
1888 | self.set_custom_completer = no_op |
|
1873 | self.set_custom_completer = no_op | |
1889 |
|
1874 | |||
1890 | @skip_doctest |
|
1875 | @skip_doctest | |
1891 | def set_next_input(self, s, replace=False): |
|
1876 | def set_next_input(self, s, replace=False): | |
1892 | """ Sets the 'default' input string for the next command line. |
|
1877 | """ Sets the 'default' input string for the next command line. | |
1893 |
|
1878 | |||
1894 | Example:: |
|
1879 | Example:: | |
1895 |
|
1880 | |||
1896 | In [1]: _ip.set_next_input("Hello Word") |
|
1881 | In [1]: _ip.set_next_input("Hello Word") | |
1897 | In [2]: Hello Word_ # cursor is here |
|
1882 | In [2]: Hello Word_ # cursor is here | |
1898 | """ |
|
1883 | """ | |
1899 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1884 | self.rl_next_input = py3compat.cast_bytes_py2(s) | |
1900 |
|
1885 | |||
1901 | def _indent_current_str(self): |
|
1886 | def _indent_current_str(self): | |
1902 | """return the current level of indentation as a string""" |
|
1887 | """return the current level of indentation as a string""" | |
1903 | return self.input_splitter.indent_spaces * ' ' |
|
1888 | return self.input_splitter.indent_spaces * ' ' | |
1904 |
|
1889 | |||
1905 | #------------------------------------------------------------------------- |
|
1890 | #------------------------------------------------------------------------- | |
1906 | # Things related to text completion |
|
1891 | # Things related to text completion | |
1907 | #------------------------------------------------------------------------- |
|
1892 | #------------------------------------------------------------------------- | |
1908 |
|
1893 | |||
1909 | def init_completer(self): |
|
1894 | def init_completer(self): | |
1910 | """Initialize the completion machinery. |
|
1895 | """Initialize the completion machinery. | |
1911 |
|
1896 | |||
1912 | This creates completion machinery that can be used by client code, |
|
1897 | This creates completion machinery that can be used by client code, | |
1913 | either interactively in-process (typically triggered by the readline |
|
1898 | either interactively in-process (typically triggered by the readline | |
1914 | library), programmatically (such as in test suites) or out-of-process |
|
1899 | library), programmatically (such as in test suites) or out-of-process | |
1915 | (typically over the network by remote frontends). |
|
1900 | (typically over the network by remote frontends). | |
1916 | """ |
|
1901 | """ | |
1917 | from IPython.core.completer import IPCompleter |
|
1902 | from IPython.core.completer import IPCompleter | |
1918 | from IPython.core.completerlib import (module_completer, |
|
1903 | from IPython.core.completerlib import (module_completer, | |
1919 | magic_run_completer, cd_completer, reset_completer) |
|
1904 | magic_run_completer, cd_completer, reset_completer) | |
1920 |
|
1905 | |||
1921 | self.Completer = IPCompleter(shell=self, |
|
1906 | self.Completer = IPCompleter(shell=self, | |
1922 | namespace=self.user_ns, |
|
1907 | namespace=self.user_ns, | |
1923 | global_namespace=self.user_global_ns, |
|
1908 | global_namespace=self.user_global_ns, | |
1924 | use_readline=False, |
|
1909 | use_readline=False, | |
1925 | parent=self, |
|
1910 | parent=self, | |
1926 | ) |
|
1911 | ) | |
1927 | self.configurables.append(self.Completer) |
|
1912 | self.configurables.append(self.Completer) | |
1928 |
|
1913 | |||
1929 | # Add custom completers to the basic ones built into IPCompleter |
|
1914 | # Add custom completers to the basic ones built into IPCompleter | |
1930 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1915 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
1931 | self.strdispatchers['complete_command'] = sdisp |
|
1916 | self.strdispatchers['complete_command'] = sdisp | |
1932 | self.Completer.custom_completers = sdisp |
|
1917 | self.Completer.custom_completers = sdisp | |
1933 |
|
1918 | |||
1934 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1919 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
1935 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1920 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
1936 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
1921 | self.set_hook('complete_command', module_completer, str_key = '%aimport') | |
1937 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1922 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
1938 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1923 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
1939 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1924 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
1940 |
|
1925 | |||
1941 |
|
1926 | |||
1942 | @skip_doctest_py2 |
|
1927 | @skip_doctest_py2 | |
1943 | def complete(self, text, line=None, cursor_pos=None): |
|
1928 | def complete(self, text, line=None, cursor_pos=None): | |
1944 | """Return the completed text and a list of completions. |
|
1929 | """Return the completed text and a list of completions. | |
1945 |
|
1930 | |||
1946 | Parameters |
|
1931 | Parameters | |
1947 | ---------- |
|
1932 | ---------- | |
1948 |
|
1933 | |||
1949 | text : string |
|
1934 | text : string | |
1950 | A string of text to be completed on. It can be given as empty and |
|
1935 | A string of text to be completed on. It can be given as empty and | |
1951 | instead a line/position pair are given. In this case, the |
|
1936 | instead a line/position pair are given. In this case, the | |
1952 | completer itself will split the line like readline does. |
|
1937 | completer itself will split the line like readline does. | |
1953 |
|
1938 | |||
1954 | line : string, optional |
|
1939 | line : string, optional | |
1955 | The complete line that text is part of. |
|
1940 | The complete line that text is part of. | |
1956 |
|
1941 | |||
1957 | cursor_pos : int, optional |
|
1942 | cursor_pos : int, optional | |
1958 | The position of the cursor on the input line. |
|
1943 | The position of the cursor on the input line. | |
1959 |
|
1944 | |||
1960 | Returns |
|
1945 | Returns | |
1961 | ------- |
|
1946 | ------- | |
1962 | text : string |
|
1947 | text : string | |
1963 | The actual text that was completed. |
|
1948 | The actual text that was completed. | |
1964 |
|
1949 | |||
1965 | matches : list |
|
1950 | matches : list | |
1966 | A sorted list with all possible completions. |
|
1951 | A sorted list with all possible completions. | |
1967 |
|
1952 | |||
1968 | The optional arguments allow the completion to take more context into |
|
1953 | The optional arguments allow the completion to take more context into | |
1969 | account, and are part of the low-level completion API. |
|
1954 | account, and are part of the low-level completion API. | |
1970 |
|
1955 | |||
1971 | This is a wrapper around the completion mechanism, similar to what |
|
1956 | This is a wrapper around the completion mechanism, similar to what | |
1972 | readline does at the command line when the TAB key is hit. By |
|
1957 | readline does at the command line when the TAB key is hit. By | |
1973 | exposing it as a method, it can be used by other non-readline |
|
1958 | exposing it as a method, it can be used by other non-readline | |
1974 | environments (such as GUIs) for text completion. |
|
1959 | environments (such as GUIs) for text completion. | |
1975 |
|
1960 | |||
1976 | Simple usage example: |
|
1961 | Simple usage example: | |
1977 |
|
1962 | |||
1978 | In [1]: x = 'hello' |
|
1963 | In [1]: x = 'hello' | |
1979 |
|
1964 | |||
1980 | In [2]: _ip.complete('x.l') |
|
1965 | In [2]: _ip.complete('x.l') | |
1981 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1966 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
1982 | """ |
|
1967 | """ | |
1983 |
|
1968 | |||
1984 | # Inject names into __builtin__ so we can complete on the added names. |
|
1969 | # Inject names into __builtin__ so we can complete on the added names. | |
1985 | with self.builtin_trap: |
|
1970 | with self.builtin_trap: | |
1986 | return self.Completer.complete(text, line, cursor_pos) |
|
1971 | return self.Completer.complete(text, line, cursor_pos) | |
1987 |
|
1972 | |||
1988 | def set_custom_completer(self, completer, pos=0): |
|
1973 | def set_custom_completer(self, completer, pos=0): | |
1989 | """Adds a new custom completer function. |
|
1974 | """Adds a new custom completer function. | |
1990 |
|
1975 | |||
1991 | The position argument (defaults to 0) is the index in the completers |
|
1976 | The position argument (defaults to 0) is the index in the completers | |
1992 | list where you want the completer to be inserted.""" |
|
1977 | list where you want the completer to be inserted.""" | |
1993 |
|
1978 | |||
1994 | newcomp = types.MethodType(completer,self.Completer) |
|
1979 | newcomp = types.MethodType(completer,self.Completer) | |
1995 | self.Completer.matchers.insert(pos,newcomp) |
|
1980 | self.Completer.matchers.insert(pos,newcomp) | |
1996 |
|
1981 | |||
1997 | def set_completer_frame(self, frame=None): |
|
1982 | def set_completer_frame(self, frame=None): | |
1998 | """Set the frame of the completer.""" |
|
1983 | """Set the frame of the completer.""" | |
1999 | if frame: |
|
1984 | if frame: | |
2000 | self.Completer.namespace = frame.f_locals |
|
1985 | self.Completer.namespace = frame.f_locals | |
2001 | self.Completer.global_namespace = frame.f_globals |
|
1986 | self.Completer.global_namespace = frame.f_globals | |
2002 | else: |
|
1987 | else: | |
2003 | self.Completer.namespace = self.user_ns |
|
1988 | self.Completer.namespace = self.user_ns | |
2004 | self.Completer.global_namespace = self.user_global_ns |
|
1989 | self.Completer.global_namespace = self.user_global_ns | |
2005 |
|
1990 | |||
2006 | #------------------------------------------------------------------------- |
|
1991 | #------------------------------------------------------------------------- | |
2007 | # Things related to magics |
|
1992 | # Things related to magics | |
2008 | #------------------------------------------------------------------------- |
|
1993 | #------------------------------------------------------------------------- | |
2009 |
|
1994 | |||
2010 | def init_magics(self): |
|
1995 | def init_magics(self): | |
2011 | from IPython.core import magics as m |
|
1996 | from IPython.core import magics as m | |
2012 | self.magics_manager = magic.MagicsManager(shell=self, |
|
1997 | self.magics_manager = magic.MagicsManager(shell=self, | |
2013 | parent=self, |
|
1998 | parent=self, | |
2014 | user_magics=m.UserMagics(self)) |
|
1999 | user_magics=m.UserMagics(self)) | |
2015 | self.configurables.append(self.magics_manager) |
|
2000 | self.configurables.append(self.magics_manager) | |
2016 |
|
2001 | |||
2017 | # Expose as public API from the magics manager |
|
2002 | # Expose as public API from the magics manager | |
2018 | self.register_magics = self.magics_manager.register |
|
2003 | self.register_magics = self.magics_manager.register | |
2019 |
|
2004 | |||
2020 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2005 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2021 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2006 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, | |
2022 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2007 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2023 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2008 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, | |
2024 | ) |
|
2009 | ) | |
2025 |
|
2010 | |||
2026 | # Register Magic Aliases |
|
2011 | # Register Magic Aliases | |
2027 | mman = self.magics_manager |
|
2012 | mman = self.magics_manager | |
2028 | # FIXME: magic aliases should be defined by the Magics classes |
|
2013 | # FIXME: magic aliases should be defined by the Magics classes | |
2029 | # or in MagicsManager, not here |
|
2014 | # or in MagicsManager, not here | |
2030 | mman.register_alias('ed', 'edit') |
|
2015 | mman.register_alias('ed', 'edit') | |
2031 | mman.register_alias('hist', 'history') |
|
2016 | mman.register_alias('hist', 'history') | |
2032 | mman.register_alias('rep', 'recall') |
|
2017 | mman.register_alias('rep', 'recall') | |
2033 | mman.register_alias('SVG', 'svg', 'cell') |
|
2018 | mman.register_alias('SVG', 'svg', 'cell') | |
2034 | mman.register_alias('HTML', 'html', 'cell') |
|
2019 | mman.register_alias('HTML', 'html', 'cell') | |
2035 | mman.register_alias('file', 'writefile', 'cell') |
|
2020 | mman.register_alias('file', 'writefile', 'cell') | |
2036 |
|
2021 | |||
2037 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2022 | # FIXME: Move the color initialization to the DisplayHook, which | |
2038 | # should be split into a prompt manager and displayhook. We probably |
|
2023 | # should be split into a prompt manager and displayhook. We probably | |
2039 | # even need a centralize colors management object. |
|
2024 | # even need a centralize colors management object. | |
2040 | self.magic('colors %s' % self.colors) |
|
2025 | self.magic('colors %s' % self.colors) | |
2041 |
|
2026 | |||
2042 | # Defined here so that it's included in the documentation |
|
2027 | # Defined here so that it's included in the documentation | |
2043 | @functools.wraps(magic.MagicsManager.register_function) |
|
2028 | @functools.wraps(magic.MagicsManager.register_function) | |
2044 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2029 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2045 | self.magics_manager.register_function(func, |
|
2030 | self.magics_manager.register_function(func, | |
2046 | magic_kind=magic_kind, magic_name=magic_name) |
|
2031 | magic_kind=magic_kind, magic_name=magic_name) | |
2047 |
|
2032 | |||
2048 | def run_line_magic(self, magic_name, line): |
|
2033 | def run_line_magic(self, magic_name, line): | |
2049 | """Execute the given line magic. |
|
2034 | """Execute the given line magic. | |
2050 |
|
2035 | |||
2051 | Parameters |
|
2036 | Parameters | |
2052 | ---------- |
|
2037 | ---------- | |
2053 | magic_name : str |
|
2038 | magic_name : str | |
2054 | Name of the desired magic function, without '%' prefix. |
|
2039 | Name of the desired magic function, without '%' prefix. | |
2055 |
|
2040 | |||
2056 | line : str |
|
2041 | line : str | |
2057 | The rest of the input line as a single string. |
|
2042 | The rest of the input line as a single string. | |
2058 | """ |
|
2043 | """ | |
2059 | fn = self.find_line_magic(magic_name) |
|
2044 | fn = self.find_line_magic(magic_name) | |
2060 | if fn is None: |
|
2045 | if fn is None: | |
2061 | cm = self.find_cell_magic(magic_name) |
|
2046 | cm = self.find_cell_magic(magic_name) | |
2062 | etpl = "Line magic function `%%%s` not found%s." |
|
2047 | etpl = "Line magic function `%%%s` not found%s." | |
2063 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2048 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2064 | 'did you mean that instead?)' % magic_name ) |
|
2049 | 'did you mean that instead?)' % magic_name ) | |
2065 | error(etpl % (magic_name, extra)) |
|
2050 | error(etpl % (magic_name, extra)) | |
2066 | else: |
|
2051 | else: | |
2067 | # Note: this is the distance in the stack to the user's frame. |
|
2052 | # Note: this is the distance in the stack to the user's frame. | |
2068 | # This will need to be updated if the internal calling logic gets |
|
2053 | # This will need to be updated if the internal calling logic gets | |
2069 | # refactored, or else we'll be expanding the wrong variables. |
|
2054 | # refactored, or else we'll be expanding the wrong variables. | |
2070 | stack_depth = 2 |
|
2055 | stack_depth = 2 | |
2071 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2056 | magic_arg_s = self.var_expand(line, stack_depth) | |
2072 | # Put magic args in a list so we can call with f(*a) syntax |
|
2057 | # Put magic args in a list so we can call with f(*a) syntax | |
2073 | args = [magic_arg_s] |
|
2058 | args = [magic_arg_s] | |
2074 | kwargs = {} |
|
2059 | kwargs = {} | |
2075 | # Grab local namespace if we need it: |
|
2060 | # Grab local namespace if we need it: | |
2076 | if getattr(fn, "needs_local_scope", False): |
|
2061 | if getattr(fn, "needs_local_scope", False): | |
2077 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2062 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals | |
2078 | with self.builtin_trap: |
|
2063 | with self.builtin_trap: | |
2079 | result = fn(*args,**kwargs) |
|
2064 | result = fn(*args,**kwargs) | |
2080 | return result |
|
2065 | return result | |
2081 |
|
2066 | |||
2082 | def run_cell_magic(self, magic_name, line, cell): |
|
2067 | def run_cell_magic(self, magic_name, line, cell): | |
2083 | """Execute the given cell magic. |
|
2068 | """Execute the given cell magic. | |
2084 |
|
2069 | |||
2085 | Parameters |
|
2070 | Parameters | |
2086 | ---------- |
|
2071 | ---------- | |
2087 | magic_name : str |
|
2072 | magic_name : str | |
2088 | Name of the desired magic function, without '%' prefix. |
|
2073 | Name of the desired magic function, without '%' prefix. | |
2089 |
|
2074 | |||
2090 | line : str |
|
2075 | line : str | |
2091 | The rest of the first input line as a single string. |
|
2076 | The rest of the first input line as a single string. | |
2092 |
|
2077 | |||
2093 | cell : str |
|
2078 | cell : str | |
2094 | The body of the cell as a (possibly multiline) string. |
|
2079 | The body of the cell as a (possibly multiline) string. | |
2095 | """ |
|
2080 | """ | |
2096 | fn = self.find_cell_magic(magic_name) |
|
2081 | fn = self.find_cell_magic(magic_name) | |
2097 | if fn is None: |
|
2082 | if fn is None: | |
2098 | lm = self.find_line_magic(magic_name) |
|
2083 | lm = self.find_line_magic(magic_name) | |
2099 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2084 | etpl = "Cell magic `%%{0}` not found{1}." | |
2100 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2085 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2101 | 'did you mean that instead?)'.format(magic_name)) |
|
2086 | 'did you mean that instead?)'.format(magic_name)) | |
2102 | error(etpl.format(magic_name, extra)) |
|
2087 | error(etpl.format(magic_name, extra)) | |
2103 | elif cell == '': |
|
2088 | elif cell == '': | |
2104 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2089 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2105 | if self.find_line_magic(magic_name) is not None: |
|
2090 | if self.find_line_magic(magic_name) is not None: | |
2106 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2091 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2107 | raise UsageError(message) |
|
2092 | raise UsageError(message) | |
2108 | else: |
|
2093 | else: | |
2109 | # Note: this is the distance in the stack to the user's frame. |
|
2094 | # Note: this is the distance in the stack to the user's frame. | |
2110 | # This will need to be updated if the internal calling logic gets |
|
2095 | # This will need to be updated if the internal calling logic gets | |
2111 | # refactored, or else we'll be expanding the wrong variables. |
|
2096 | # refactored, or else we'll be expanding the wrong variables. | |
2112 | stack_depth = 2 |
|
2097 | stack_depth = 2 | |
2113 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2098 | magic_arg_s = self.var_expand(line, stack_depth) | |
2114 | with self.builtin_trap: |
|
2099 | with self.builtin_trap: | |
2115 | result = fn(magic_arg_s, cell) |
|
2100 | result = fn(magic_arg_s, cell) | |
2116 | return result |
|
2101 | return result | |
2117 |
|
2102 | |||
2118 | def find_line_magic(self, magic_name): |
|
2103 | def find_line_magic(self, magic_name): | |
2119 | """Find and return a line magic by name. |
|
2104 | """Find and return a line magic by name. | |
2120 |
|
2105 | |||
2121 | Returns None if the magic isn't found.""" |
|
2106 | Returns None if the magic isn't found.""" | |
2122 | return self.magics_manager.magics['line'].get(magic_name) |
|
2107 | return self.magics_manager.magics['line'].get(magic_name) | |
2123 |
|
2108 | |||
2124 | def find_cell_magic(self, magic_name): |
|
2109 | def find_cell_magic(self, magic_name): | |
2125 | """Find and return a cell magic by name. |
|
2110 | """Find and return a cell magic by name. | |
2126 |
|
2111 | |||
2127 | Returns None if the magic isn't found.""" |
|
2112 | Returns None if the magic isn't found.""" | |
2128 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2113 | return self.magics_manager.magics['cell'].get(magic_name) | |
2129 |
|
2114 | |||
2130 | def find_magic(self, magic_name, magic_kind='line'): |
|
2115 | def find_magic(self, magic_name, magic_kind='line'): | |
2131 | """Find and return a magic of the given type by name. |
|
2116 | """Find and return a magic of the given type by name. | |
2132 |
|
2117 | |||
2133 | Returns None if the magic isn't found.""" |
|
2118 | Returns None if the magic isn't found.""" | |
2134 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2119 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2135 |
|
2120 | |||
2136 | def magic(self, arg_s): |
|
2121 | def magic(self, arg_s): | |
2137 | """DEPRECATED. Use run_line_magic() instead. |
|
2122 | """DEPRECATED. Use run_line_magic() instead. | |
2138 |
|
2123 | |||
2139 | Call a magic function by name. |
|
2124 | Call a magic function by name. | |
2140 |
|
2125 | |||
2141 | Input: a string containing the name of the magic function to call and |
|
2126 | Input: a string containing the name of the magic function to call and | |
2142 | any additional arguments to be passed to the magic. |
|
2127 | any additional arguments to be passed to the magic. | |
2143 |
|
2128 | |||
2144 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2129 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2145 | prompt: |
|
2130 | prompt: | |
2146 |
|
2131 | |||
2147 | In[1]: %name -opt foo bar |
|
2132 | In[1]: %name -opt foo bar | |
2148 |
|
2133 | |||
2149 | To call a magic without arguments, simply use magic('name'). |
|
2134 | To call a magic without arguments, simply use magic('name'). | |
2150 |
|
2135 | |||
2151 | This provides a proper Python function to call IPython's magics in any |
|
2136 | This provides a proper Python function to call IPython's magics in any | |
2152 | valid Python code you can type at the interpreter, including loops and |
|
2137 | valid Python code you can type at the interpreter, including loops and | |
2153 | compound statements. |
|
2138 | compound statements. | |
2154 | """ |
|
2139 | """ | |
2155 | # TODO: should we issue a loud deprecation warning here? |
|
2140 | # TODO: should we issue a loud deprecation warning here? | |
2156 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2141 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2157 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2142 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2158 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2143 | return self.run_line_magic(magic_name, magic_arg_s) | |
2159 |
|
2144 | |||
2160 | #------------------------------------------------------------------------- |
|
2145 | #------------------------------------------------------------------------- | |
2161 | # Things related to macros |
|
2146 | # Things related to macros | |
2162 | #------------------------------------------------------------------------- |
|
2147 | #------------------------------------------------------------------------- | |
2163 |
|
2148 | |||
2164 | def define_macro(self, name, themacro): |
|
2149 | def define_macro(self, name, themacro): | |
2165 | """Define a new macro |
|
2150 | """Define a new macro | |
2166 |
|
2151 | |||
2167 | Parameters |
|
2152 | Parameters | |
2168 | ---------- |
|
2153 | ---------- | |
2169 | name : str |
|
2154 | name : str | |
2170 | The name of the macro. |
|
2155 | The name of the macro. | |
2171 | themacro : str or Macro |
|
2156 | themacro : str or Macro | |
2172 | The action to do upon invoking the macro. If a string, a new |
|
2157 | The action to do upon invoking the macro. If a string, a new | |
2173 | Macro object is created by passing the string to it. |
|
2158 | Macro object is created by passing the string to it. | |
2174 | """ |
|
2159 | """ | |
2175 |
|
2160 | |||
2176 | from IPython.core import macro |
|
2161 | from IPython.core import macro | |
2177 |
|
2162 | |||
2178 | if isinstance(themacro, string_types): |
|
2163 | if isinstance(themacro, string_types): | |
2179 | themacro = macro.Macro(themacro) |
|
2164 | themacro = macro.Macro(themacro) | |
2180 | if not isinstance(themacro, macro.Macro): |
|
2165 | if not isinstance(themacro, macro.Macro): | |
2181 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2166 | raise ValueError('A macro must be a string or a Macro instance.') | |
2182 | self.user_ns[name] = themacro |
|
2167 | self.user_ns[name] = themacro | |
2183 |
|
2168 | |||
2184 | #------------------------------------------------------------------------- |
|
2169 | #------------------------------------------------------------------------- | |
2185 | # Things related to the running of system commands |
|
2170 | # Things related to the running of system commands | |
2186 | #------------------------------------------------------------------------- |
|
2171 | #------------------------------------------------------------------------- | |
2187 |
|
2172 | |||
2188 | def system_piped(self, cmd): |
|
2173 | def system_piped(self, cmd): | |
2189 | """Call the given cmd in a subprocess, piping stdout/err |
|
2174 | """Call the given cmd in a subprocess, piping stdout/err | |
2190 |
|
2175 | |||
2191 | Parameters |
|
2176 | Parameters | |
2192 | ---------- |
|
2177 | ---------- | |
2193 | cmd : str |
|
2178 | cmd : str | |
2194 | Command to execute (can not end in '&', as background processes are |
|
2179 | Command to execute (can not end in '&', as background processes are | |
2195 | not supported. Should not be a command that expects input |
|
2180 | not supported. Should not be a command that expects input | |
2196 | other than simple text. |
|
2181 | other than simple text. | |
2197 | """ |
|
2182 | """ | |
2198 | if cmd.rstrip().endswith('&'): |
|
2183 | if cmd.rstrip().endswith('&'): | |
2199 | # this is *far* from a rigorous test |
|
2184 | # this is *far* from a rigorous test | |
2200 | # We do not support backgrounding processes because we either use |
|
2185 | # We do not support backgrounding processes because we either use | |
2201 | # pexpect or pipes to read from. Users can always just call |
|
2186 | # pexpect or pipes to read from. Users can always just call | |
2202 | # os.system() or use ip.system=ip.system_raw |
|
2187 | # os.system() or use ip.system=ip.system_raw | |
2203 | # if they really want a background process. |
|
2188 | # if they really want a background process. | |
2204 | raise OSError("Background processes not supported.") |
|
2189 | raise OSError("Background processes not supported.") | |
2205 |
|
2190 | |||
2206 | # we explicitly do NOT return the subprocess status code, because |
|
2191 | # we explicitly do NOT return the subprocess status code, because | |
2207 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2192 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2208 | # Instead, we store the exit_code in user_ns. |
|
2193 | # Instead, we store the exit_code in user_ns. | |
2209 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2194 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2210 |
|
2195 | |||
2211 | def system_raw(self, cmd): |
|
2196 | def system_raw(self, cmd): | |
2212 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2197 | """Call the given cmd in a subprocess using os.system on Windows or | |
2213 | subprocess.call using the system shell on other platforms. |
|
2198 | subprocess.call using the system shell on other platforms. | |
2214 |
|
2199 | |||
2215 | Parameters |
|
2200 | Parameters | |
2216 | ---------- |
|
2201 | ---------- | |
2217 | cmd : str |
|
2202 | cmd : str | |
2218 | Command to execute. |
|
2203 | Command to execute. | |
2219 | """ |
|
2204 | """ | |
2220 | cmd = self.var_expand(cmd, depth=1) |
|
2205 | cmd = self.var_expand(cmd, depth=1) | |
2221 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2206 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2222 | if sys.platform == 'win32': |
|
2207 | if sys.platform == 'win32': | |
2223 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2208 | from IPython.utils._process_win32 import AvoidUNCPath | |
2224 | with AvoidUNCPath() as path: |
|
2209 | with AvoidUNCPath() as path: | |
2225 | if path is not None: |
|
2210 | if path is not None: | |
2226 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2211 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2227 | cmd = py3compat.unicode_to_str(cmd) |
|
2212 | cmd = py3compat.unicode_to_str(cmd) | |
2228 | try: |
|
2213 | try: | |
2229 | ec = os.system(cmd) |
|
2214 | ec = os.system(cmd) | |
2230 | except KeyboardInterrupt: |
|
2215 | except KeyboardInterrupt: | |
2231 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2216 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2232 | ec = -2 |
|
2217 | ec = -2 | |
2233 | else: |
|
2218 | else: | |
2234 | cmd = py3compat.unicode_to_str(cmd) |
|
2219 | cmd = py3compat.unicode_to_str(cmd) | |
2235 | # For posix the result of the subprocess.call() below is an exit |
|
2220 | # For posix the result of the subprocess.call() below is an exit | |
2236 | # code, which by convention is zero for success, positive for |
|
2221 | # code, which by convention is zero for success, positive for | |
2237 | # program failure. Exit codes above 128 are reserved for signals, |
|
2222 | # program failure. Exit codes above 128 are reserved for signals, | |
2238 | # and the formula for converting a signal to an exit code is usually |
|
2223 | # and the formula for converting a signal to an exit code is usually | |
2239 | # signal_number+128. To more easily differentiate between exit |
|
2224 | # signal_number+128. To more easily differentiate between exit | |
2240 | # codes and signals, ipython uses negative numbers. For instance |
|
2225 | # codes and signals, ipython uses negative numbers. For instance | |
2241 | # since control-c is signal 2 but exit code 130, ipython's |
|
2226 | # since control-c is signal 2 but exit code 130, ipython's | |
2242 | # _exit_code variable will read -2. Note that some shells like |
|
2227 | # _exit_code variable will read -2. Note that some shells like | |
2243 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2228 | # csh and fish don't follow sh/bash conventions for exit codes. | |
2244 | executable = os.environ.get('SHELL', None) |
|
2229 | executable = os.environ.get('SHELL', None) | |
2245 | try: |
|
2230 | try: | |
2246 | # Use env shell instead of default /bin/sh |
|
2231 | # Use env shell instead of default /bin/sh | |
2247 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2232 | ec = subprocess.call(cmd, shell=True, executable=executable) | |
2248 | except KeyboardInterrupt: |
|
2233 | except KeyboardInterrupt: | |
2249 | # intercept control-C; a long traceback is not useful here |
|
2234 | # intercept control-C; a long traceback is not useful here | |
2250 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2235 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2251 | ec = 130 |
|
2236 | ec = 130 | |
2252 | if ec > 128: |
|
2237 | if ec > 128: | |
2253 | ec = -(ec - 128) |
|
2238 | ec = -(ec - 128) | |
2254 |
|
2239 | |||
2255 | # We explicitly do NOT return the subprocess status code, because |
|
2240 | # We explicitly do NOT return the subprocess status code, because | |
2256 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2241 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2257 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2242 | # Instead, we store the exit_code in user_ns. Note the semantics | |
2258 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2243 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, | |
2259 | # but raising SystemExit(_exit_code) will give status 254! |
|
2244 | # but raising SystemExit(_exit_code) will give status 254! | |
2260 | self.user_ns['_exit_code'] = ec |
|
2245 | self.user_ns['_exit_code'] = ec | |
2261 |
|
2246 | |||
2262 | # use piped system by default, because it is better behaved |
|
2247 | # use piped system by default, because it is better behaved | |
2263 | system = system_piped |
|
2248 | system = system_piped | |
2264 |
|
2249 | |||
2265 | def getoutput(self, cmd, split=True, depth=0): |
|
2250 | def getoutput(self, cmd, split=True, depth=0): | |
2266 | """Get output (possibly including stderr) from a subprocess. |
|
2251 | """Get output (possibly including stderr) from a subprocess. | |
2267 |
|
2252 | |||
2268 | Parameters |
|
2253 | Parameters | |
2269 | ---------- |
|
2254 | ---------- | |
2270 | cmd : str |
|
2255 | cmd : str | |
2271 | Command to execute (can not end in '&', as background processes are |
|
2256 | Command to execute (can not end in '&', as background processes are | |
2272 | not supported. |
|
2257 | not supported. | |
2273 | split : bool, optional |
|
2258 | split : bool, optional | |
2274 | If True, split the output into an IPython SList. Otherwise, an |
|
2259 | If True, split the output into an IPython SList. Otherwise, an | |
2275 | IPython LSString is returned. These are objects similar to normal |
|
2260 | IPython LSString is returned. These are objects similar to normal | |
2276 | lists and strings, with a few convenience attributes for easier |
|
2261 | lists and strings, with a few convenience attributes for easier | |
2277 | manipulation of line-based output. You can use '?' on them for |
|
2262 | manipulation of line-based output. You can use '?' on them for | |
2278 | details. |
|
2263 | details. | |
2279 | depth : int, optional |
|
2264 | depth : int, optional | |
2280 | How many frames above the caller are the local variables which should |
|
2265 | How many frames above the caller are the local variables which should | |
2281 | be expanded in the command string? The default (0) assumes that the |
|
2266 | be expanded in the command string? The default (0) assumes that the | |
2282 | expansion variables are in the stack frame calling this function. |
|
2267 | expansion variables are in the stack frame calling this function. | |
2283 | """ |
|
2268 | """ | |
2284 | if cmd.rstrip().endswith('&'): |
|
2269 | if cmd.rstrip().endswith('&'): | |
2285 | # this is *far* from a rigorous test |
|
2270 | # this is *far* from a rigorous test | |
2286 | raise OSError("Background processes not supported.") |
|
2271 | raise OSError("Background processes not supported.") | |
2287 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2272 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2288 | if split: |
|
2273 | if split: | |
2289 | out = SList(out.splitlines()) |
|
2274 | out = SList(out.splitlines()) | |
2290 | else: |
|
2275 | else: | |
2291 | out = LSString(out) |
|
2276 | out = LSString(out) | |
2292 | return out |
|
2277 | return out | |
2293 |
|
2278 | |||
2294 | #------------------------------------------------------------------------- |
|
2279 | #------------------------------------------------------------------------- | |
2295 | # Things related to aliases |
|
2280 | # Things related to aliases | |
2296 | #------------------------------------------------------------------------- |
|
2281 | #------------------------------------------------------------------------- | |
2297 |
|
2282 | |||
2298 | def init_alias(self): |
|
2283 | def init_alias(self): | |
2299 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2284 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2300 | self.configurables.append(self.alias_manager) |
|
2285 | self.configurables.append(self.alias_manager) | |
2301 |
|
2286 | |||
2302 | #------------------------------------------------------------------------- |
|
2287 | #------------------------------------------------------------------------- | |
2303 | # Things related to extensions |
|
2288 | # Things related to extensions | |
2304 | #------------------------------------------------------------------------- |
|
2289 | #------------------------------------------------------------------------- | |
2305 |
|
2290 | |||
2306 | def init_extension_manager(self): |
|
2291 | def init_extension_manager(self): | |
2307 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2292 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2308 | self.configurables.append(self.extension_manager) |
|
2293 | self.configurables.append(self.extension_manager) | |
2309 |
|
2294 | |||
2310 | #------------------------------------------------------------------------- |
|
2295 | #------------------------------------------------------------------------- | |
2311 | # Things related to payloads |
|
2296 | # Things related to payloads | |
2312 | #------------------------------------------------------------------------- |
|
2297 | #------------------------------------------------------------------------- | |
2313 |
|
2298 | |||
2314 | def init_payload(self): |
|
2299 | def init_payload(self): | |
2315 | self.payload_manager = PayloadManager(parent=self) |
|
2300 | self.payload_manager = PayloadManager(parent=self) | |
2316 | self.configurables.append(self.payload_manager) |
|
2301 | self.configurables.append(self.payload_manager) | |
2317 |
|
2302 | |||
2318 | #------------------------------------------------------------------------- |
|
2303 | #------------------------------------------------------------------------- | |
2319 | # Things related to the prefilter |
|
2304 | # Things related to the prefilter | |
2320 | #------------------------------------------------------------------------- |
|
2305 | #------------------------------------------------------------------------- | |
2321 |
|
2306 | |||
2322 | def init_prefilter(self): |
|
2307 | def init_prefilter(self): | |
2323 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2308 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2324 | self.configurables.append(self.prefilter_manager) |
|
2309 | self.configurables.append(self.prefilter_manager) | |
2325 | # Ultimately this will be refactored in the new interpreter code, but |
|
2310 | # Ultimately this will be refactored in the new interpreter code, but | |
2326 | # for now, we should expose the main prefilter method (there's legacy |
|
2311 | # for now, we should expose the main prefilter method (there's legacy | |
2327 | # code out there that may rely on this). |
|
2312 | # code out there that may rely on this). | |
2328 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2313 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2329 |
|
2314 | |||
2330 | def auto_rewrite_input(self, cmd): |
|
2315 | def auto_rewrite_input(self, cmd): | |
2331 | """Print to the screen the rewritten form of the user's command. |
|
2316 | """Print to the screen the rewritten form of the user's command. | |
2332 |
|
2317 | |||
2333 | This shows visual feedback by rewriting input lines that cause |
|
2318 | This shows visual feedback by rewriting input lines that cause | |
2334 | automatic calling to kick in, like:: |
|
2319 | automatic calling to kick in, like:: | |
2335 |
|
2320 | |||
2336 | /f x |
|
2321 | /f x | |
2337 |
|
2322 | |||
2338 | into:: |
|
2323 | into:: | |
2339 |
|
2324 | |||
2340 | ------> f(x) |
|
2325 | ------> f(x) | |
2341 |
|
2326 | |||
2342 | after the user's input prompt. This helps the user understand that the |
|
2327 | after the user's input prompt. This helps the user understand that the | |
2343 | input line was transformed automatically by IPython. |
|
2328 | input line was transformed automatically by IPython. | |
2344 | """ |
|
2329 | """ | |
2345 | if not self.show_rewritten_input: |
|
2330 | if not self.show_rewritten_input: | |
2346 | return |
|
2331 | return | |
2347 |
|
2332 | |||
2348 | # This is overridden in TerminalInteractiveShell to use fancy prompts |
|
2333 | # This is overridden in TerminalInteractiveShell to use fancy prompts | |
2349 | print("------> " + cmd) |
|
2334 | print("------> " + cmd) | |
2350 |
|
2335 | |||
2351 | #------------------------------------------------------------------------- |
|
2336 | #------------------------------------------------------------------------- | |
2352 | # Things related to extracting values/expressions from kernel and user_ns |
|
2337 | # Things related to extracting values/expressions from kernel and user_ns | |
2353 | #------------------------------------------------------------------------- |
|
2338 | #------------------------------------------------------------------------- | |
2354 |
|
2339 | |||
2355 | def _user_obj_error(self): |
|
2340 | def _user_obj_error(self): | |
2356 | """return simple exception dict |
|
2341 | """return simple exception dict | |
2357 |
|
2342 | |||
2358 | for use in user_expressions |
|
2343 | for use in user_expressions | |
2359 | """ |
|
2344 | """ | |
2360 |
|
2345 | |||
2361 | etype, evalue, tb = self._get_exc_info() |
|
2346 | etype, evalue, tb = self._get_exc_info() | |
2362 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2347 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2363 |
|
2348 | |||
2364 | exc_info = { |
|
2349 | exc_info = { | |
2365 | u'status' : 'error', |
|
2350 | u'status' : 'error', | |
2366 | u'traceback' : stb, |
|
2351 | u'traceback' : stb, | |
2367 | u'ename' : unicode_type(etype.__name__), |
|
2352 | u'ename' : unicode_type(etype.__name__), | |
2368 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2353 | u'evalue' : py3compat.safe_unicode(evalue), | |
2369 | } |
|
2354 | } | |
2370 |
|
2355 | |||
2371 | return exc_info |
|
2356 | return exc_info | |
2372 |
|
2357 | |||
2373 | def _format_user_obj(self, obj): |
|
2358 | def _format_user_obj(self, obj): | |
2374 | """format a user object to display dict |
|
2359 | """format a user object to display dict | |
2375 |
|
2360 | |||
2376 | for use in user_expressions |
|
2361 | for use in user_expressions | |
2377 | """ |
|
2362 | """ | |
2378 |
|
2363 | |||
2379 | data, md = self.display_formatter.format(obj) |
|
2364 | data, md = self.display_formatter.format(obj) | |
2380 | value = { |
|
2365 | value = { | |
2381 | 'status' : 'ok', |
|
2366 | 'status' : 'ok', | |
2382 | 'data' : data, |
|
2367 | 'data' : data, | |
2383 | 'metadata' : md, |
|
2368 | 'metadata' : md, | |
2384 | } |
|
2369 | } | |
2385 | return value |
|
2370 | return value | |
2386 |
|
2371 | |||
2387 | def user_expressions(self, expressions): |
|
2372 | def user_expressions(self, expressions): | |
2388 | """Evaluate a dict of expressions in the user's namespace. |
|
2373 | """Evaluate a dict of expressions in the user's namespace. | |
2389 |
|
2374 | |||
2390 | Parameters |
|
2375 | Parameters | |
2391 | ---------- |
|
2376 | ---------- | |
2392 | expressions : dict |
|
2377 | expressions : dict | |
2393 | A dict with string keys and string values. The expression values |
|
2378 | A dict with string keys and string values. The expression values | |
2394 | should be valid Python expressions, each of which will be evaluated |
|
2379 | should be valid Python expressions, each of which will be evaluated | |
2395 | in the user namespace. |
|
2380 | in the user namespace. | |
2396 |
|
2381 | |||
2397 | Returns |
|
2382 | Returns | |
2398 | ------- |
|
2383 | ------- | |
2399 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2384 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2400 | display_data of each value. |
|
2385 | display_data of each value. | |
2401 | """ |
|
2386 | """ | |
2402 | out = {} |
|
2387 | out = {} | |
2403 | user_ns = self.user_ns |
|
2388 | user_ns = self.user_ns | |
2404 | global_ns = self.user_global_ns |
|
2389 | global_ns = self.user_global_ns | |
2405 |
|
2390 | |||
2406 | for key, expr in iteritems(expressions): |
|
2391 | for key, expr in iteritems(expressions): | |
2407 | try: |
|
2392 | try: | |
2408 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2393 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2409 | except: |
|
2394 | except: | |
2410 | value = self._user_obj_error() |
|
2395 | value = self._user_obj_error() | |
2411 | out[key] = value |
|
2396 | out[key] = value | |
2412 | return out |
|
2397 | return out | |
2413 |
|
2398 | |||
2414 | #------------------------------------------------------------------------- |
|
2399 | #------------------------------------------------------------------------- | |
2415 | # Things related to the running of code |
|
2400 | # Things related to the running of code | |
2416 | #------------------------------------------------------------------------- |
|
2401 | #------------------------------------------------------------------------- | |
2417 |
|
2402 | |||
2418 | def ex(self, cmd): |
|
2403 | def ex(self, cmd): | |
2419 | """Execute a normal python statement in user namespace.""" |
|
2404 | """Execute a normal python statement in user namespace.""" | |
2420 | with self.builtin_trap: |
|
2405 | with self.builtin_trap: | |
2421 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2406 | exec(cmd, self.user_global_ns, self.user_ns) | |
2422 |
|
2407 | |||
2423 | def ev(self, expr): |
|
2408 | def ev(self, expr): | |
2424 | """Evaluate python expression expr in user namespace. |
|
2409 | """Evaluate python expression expr in user namespace. | |
2425 |
|
2410 | |||
2426 | Returns the result of evaluation |
|
2411 | Returns the result of evaluation | |
2427 | """ |
|
2412 | """ | |
2428 | with self.builtin_trap: |
|
2413 | with self.builtin_trap: | |
2429 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2414 | return eval(expr, self.user_global_ns, self.user_ns) | |
2430 |
|
2415 | |||
2431 | def safe_execfile(self, fname, *where, **kw): |
|
2416 | def safe_execfile(self, fname, *where, **kw): | |
2432 | """A safe version of the builtin execfile(). |
|
2417 | """A safe version of the builtin execfile(). | |
2433 |
|
2418 | |||
2434 | This version will never throw an exception, but instead print |
|
2419 | This version will never throw an exception, but instead print | |
2435 | helpful error messages to the screen. This only works on pure |
|
2420 | helpful error messages to the screen. This only works on pure | |
2436 | Python files with the .py extension. |
|
2421 | Python files with the .py extension. | |
2437 |
|
2422 | |||
2438 | Parameters |
|
2423 | Parameters | |
2439 | ---------- |
|
2424 | ---------- | |
2440 | fname : string |
|
2425 | fname : string | |
2441 | The name of the file to be executed. |
|
2426 | The name of the file to be executed. | |
2442 | where : tuple |
|
2427 | where : tuple | |
2443 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2428 | One or two namespaces, passed to execfile() as (globals,locals). | |
2444 | If only one is given, it is passed as both. |
|
2429 | If only one is given, it is passed as both. | |
2445 | exit_ignore : bool (False) |
|
2430 | exit_ignore : bool (False) | |
2446 | If True, then silence SystemExit for non-zero status (it is always |
|
2431 | If True, then silence SystemExit for non-zero status (it is always | |
2447 | silenced for zero status, as it is so common). |
|
2432 | silenced for zero status, as it is so common). | |
2448 | raise_exceptions : bool (False) |
|
2433 | raise_exceptions : bool (False) | |
2449 | If True raise exceptions everywhere. Meant for testing. |
|
2434 | If True raise exceptions everywhere. Meant for testing. | |
2450 | shell_futures : bool (False) |
|
2435 | shell_futures : bool (False) | |
2451 | If True, the code will share future statements with the interactive |
|
2436 | If True, the code will share future statements with the interactive | |
2452 | shell. It will both be affected by previous __future__ imports, and |
|
2437 | shell. It will both be affected by previous __future__ imports, and | |
2453 | any __future__ imports in the code will affect the shell. If False, |
|
2438 | any __future__ imports in the code will affect the shell. If False, | |
2454 | __future__ imports are not shared in either direction. |
|
2439 | __future__ imports are not shared in either direction. | |
2455 |
|
2440 | |||
2456 | """ |
|
2441 | """ | |
2457 | kw.setdefault('exit_ignore', False) |
|
2442 | kw.setdefault('exit_ignore', False) | |
2458 | kw.setdefault('raise_exceptions', False) |
|
2443 | kw.setdefault('raise_exceptions', False) | |
2459 | kw.setdefault('shell_futures', False) |
|
2444 | kw.setdefault('shell_futures', False) | |
2460 |
|
2445 | |||
2461 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2446 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2462 |
|
2447 | |||
2463 | # Make sure we can open the file |
|
2448 | # Make sure we can open the file | |
2464 | try: |
|
2449 | try: | |
2465 | with open(fname): |
|
2450 | with open(fname): | |
2466 | pass |
|
2451 | pass | |
2467 | except: |
|
2452 | except: | |
2468 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2453 | warn('Could not open file <%s> for safe execution.' % fname) | |
2469 | return |
|
2454 | return | |
2470 |
|
2455 | |||
2471 | # Find things also in current directory. This is needed to mimic the |
|
2456 | # Find things also in current directory. This is needed to mimic the | |
2472 | # behavior of running a script from the system command line, where |
|
2457 | # behavior of running a script from the system command line, where | |
2473 | # Python inserts the script's directory into sys.path |
|
2458 | # Python inserts the script's directory into sys.path | |
2474 | dname = os.path.dirname(fname) |
|
2459 | dname = os.path.dirname(fname) | |
2475 |
|
2460 | |||
2476 | with prepended_to_syspath(dname), self.builtin_trap: |
|
2461 | with prepended_to_syspath(dname), self.builtin_trap: | |
2477 | try: |
|
2462 | try: | |
2478 | glob, loc = (where + (None, ))[:2] |
|
2463 | glob, loc = (where + (None, ))[:2] | |
2479 | py3compat.execfile( |
|
2464 | py3compat.execfile( | |
2480 | fname, glob, loc, |
|
2465 | fname, glob, loc, | |
2481 | self.compile if kw['shell_futures'] else None) |
|
2466 | self.compile if kw['shell_futures'] else None) | |
2482 | except SystemExit as status: |
|
2467 | except SystemExit as status: | |
2483 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2468 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2484 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2469 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2485 | # these are considered normal by the OS: |
|
2470 | # these are considered normal by the OS: | |
2486 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2471 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2487 | # 0 |
|
2472 | # 0 | |
2488 | # > python -c'import sys;sys.exit()'; echo $? |
|
2473 | # > python -c'import sys;sys.exit()'; echo $? | |
2489 | # 0 |
|
2474 | # 0 | |
2490 | # For other exit status, we show the exception unless |
|
2475 | # For other exit status, we show the exception unless | |
2491 | # explicitly silenced, but only in short form. |
|
2476 | # explicitly silenced, but only in short form. | |
2492 | if status.code: |
|
2477 | if status.code: | |
2493 | if kw['raise_exceptions']: |
|
2478 | if kw['raise_exceptions']: | |
2494 | raise |
|
2479 | raise | |
2495 | if not kw['exit_ignore']: |
|
2480 | if not kw['exit_ignore']: | |
2496 | self.showtraceback(exception_only=True) |
|
2481 | self.showtraceback(exception_only=True) | |
2497 | except: |
|
2482 | except: | |
2498 | if kw['raise_exceptions']: |
|
2483 | if kw['raise_exceptions']: | |
2499 | raise |
|
2484 | raise | |
2500 | # tb offset is 2 because we wrap execfile |
|
2485 | # tb offset is 2 because we wrap execfile | |
2501 | self.showtraceback(tb_offset=2) |
|
2486 | self.showtraceback(tb_offset=2) | |
2502 |
|
2487 | |||
2503 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2488 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): | |
2504 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2489 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | |
2505 |
|
2490 | |||
2506 | Parameters |
|
2491 | Parameters | |
2507 | ---------- |
|
2492 | ---------- | |
2508 | fname : str |
|
2493 | fname : str | |
2509 | The name of the file to execute. The filename must have a |
|
2494 | The name of the file to execute. The filename must have a | |
2510 | .ipy or .ipynb extension. |
|
2495 | .ipy or .ipynb extension. | |
2511 | shell_futures : bool (False) |
|
2496 | shell_futures : bool (False) | |
2512 | If True, the code will share future statements with the interactive |
|
2497 | If True, the code will share future statements with the interactive | |
2513 | shell. It will both be affected by previous __future__ imports, and |
|
2498 | shell. It will both be affected by previous __future__ imports, and | |
2514 | any __future__ imports in the code will affect the shell. If False, |
|
2499 | any __future__ imports in the code will affect the shell. If False, | |
2515 | __future__ imports are not shared in either direction. |
|
2500 | __future__ imports are not shared in either direction. | |
2516 | raise_exceptions : bool (False) |
|
2501 | raise_exceptions : bool (False) | |
2517 | If True raise exceptions everywhere. Meant for testing. |
|
2502 | If True raise exceptions everywhere. Meant for testing. | |
2518 | """ |
|
2503 | """ | |
2519 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2504 | fname = os.path.abspath(os.path.expanduser(fname)) | |
2520 |
|
2505 | |||
2521 | # Make sure we can open the file |
|
2506 | # Make sure we can open the file | |
2522 | try: |
|
2507 | try: | |
2523 | with open(fname): |
|
2508 | with open(fname): | |
2524 | pass |
|
2509 | pass | |
2525 | except: |
|
2510 | except: | |
2526 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2511 | warn('Could not open file <%s> for safe execution.' % fname) | |
2527 | return |
|
2512 | return | |
2528 |
|
2513 | |||
2529 | # Find things also in current directory. This is needed to mimic the |
|
2514 | # Find things also in current directory. This is needed to mimic the | |
2530 | # behavior of running a script from the system command line, where |
|
2515 | # behavior of running a script from the system command line, where | |
2531 | # Python inserts the script's directory into sys.path |
|
2516 | # Python inserts the script's directory into sys.path | |
2532 | dname = os.path.dirname(fname) |
|
2517 | dname = os.path.dirname(fname) | |
2533 |
|
2518 | |||
2534 | def get_cells(): |
|
2519 | def get_cells(): | |
2535 | """generator for sequence of code blocks to run""" |
|
2520 | """generator for sequence of code blocks to run""" | |
2536 | if fname.endswith('.ipynb'): |
|
2521 | if fname.endswith('.ipynb'): | |
2537 | from nbformat import read |
|
2522 | from nbformat import read | |
2538 | with io_open(fname) as f: |
|
2523 | with io_open(fname) as f: | |
2539 | nb = read(f, as_version=4) |
|
2524 | nb = read(f, as_version=4) | |
2540 | if not nb.cells: |
|
2525 | if not nb.cells: | |
2541 | return |
|
2526 | return | |
2542 | for cell in nb.cells: |
|
2527 | for cell in nb.cells: | |
2543 | if cell.cell_type == 'code': |
|
2528 | if cell.cell_type == 'code': | |
2544 | yield cell.source |
|
2529 | yield cell.source | |
2545 | else: |
|
2530 | else: | |
2546 | with open(fname) as f: |
|
2531 | with open(fname) as f: | |
2547 | yield f.read() |
|
2532 | yield f.read() | |
2548 |
|
2533 | |||
2549 | with prepended_to_syspath(dname): |
|
2534 | with prepended_to_syspath(dname): | |
2550 | try: |
|
2535 | try: | |
2551 | for cell in get_cells(): |
|
2536 | for cell in get_cells(): | |
2552 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2537 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) | |
2553 | if raise_exceptions: |
|
2538 | if raise_exceptions: | |
2554 | result.raise_error() |
|
2539 | result.raise_error() | |
2555 | elif not result.success: |
|
2540 | elif not result.success: | |
2556 | break |
|
2541 | break | |
2557 | except: |
|
2542 | except: | |
2558 | if raise_exceptions: |
|
2543 | if raise_exceptions: | |
2559 | raise |
|
2544 | raise | |
2560 | self.showtraceback() |
|
2545 | self.showtraceback() | |
2561 | warn('Unknown failure executing file: <%s>' % fname) |
|
2546 | warn('Unknown failure executing file: <%s>' % fname) | |
2562 |
|
2547 | |||
2563 | def safe_run_module(self, mod_name, where): |
|
2548 | def safe_run_module(self, mod_name, where): | |
2564 | """A safe version of runpy.run_module(). |
|
2549 | """A safe version of runpy.run_module(). | |
2565 |
|
2550 | |||
2566 | This version will never throw an exception, but instead print |
|
2551 | This version will never throw an exception, but instead print | |
2567 | helpful error messages to the screen. |
|
2552 | helpful error messages to the screen. | |
2568 |
|
2553 | |||
2569 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2554 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2570 |
|
2555 | |||
2571 | Parameters |
|
2556 | Parameters | |
2572 | ---------- |
|
2557 | ---------- | |
2573 | mod_name : string |
|
2558 | mod_name : string | |
2574 | The name of the module to be executed. |
|
2559 | The name of the module to be executed. | |
2575 | where : dict |
|
2560 | where : dict | |
2576 | The globals namespace. |
|
2561 | The globals namespace. | |
2577 | """ |
|
2562 | """ | |
2578 | try: |
|
2563 | try: | |
2579 | try: |
|
2564 | try: | |
2580 | where.update( |
|
2565 | where.update( | |
2581 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2566 | runpy.run_module(str(mod_name), run_name="__main__", | |
2582 | alter_sys=True) |
|
2567 | alter_sys=True) | |
2583 | ) |
|
2568 | ) | |
2584 | except SystemExit as status: |
|
2569 | except SystemExit as status: | |
2585 | if status.code: |
|
2570 | if status.code: | |
2586 | raise |
|
2571 | raise | |
2587 | except: |
|
2572 | except: | |
2588 | self.showtraceback() |
|
2573 | self.showtraceback() | |
2589 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2574 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2590 |
|
2575 | |||
2591 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2576 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | |
2592 | """Run a complete IPython cell. |
|
2577 | """Run a complete IPython cell. | |
2593 |
|
2578 | |||
2594 | Parameters |
|
2579 | Parameters | |
2595 | ---------- |
|
2580 | ---------- | |
2596 | raw_cell : str |
|
2581 | raw_cell : str | |
2597 | The code (including IPython code such as %magic functions) to run. |
|
2582 | The code (including IPython code such as %magic functions) to run. | |
2598 | store_history : bool |
|
2583 | store_history : bool | |
2599 | If True, the raw and translated cell will be stored in IPython's |
|
2584 | If True, the raw and translated cell will be stored in IPython's | |
2600 | history. For user code calling back into IPython's machinery, this |
|
2585 | history. For user code calling back into IPython's machinery, this | |
2601 | should be set to False. |
|
2586 | should be set to False. | |
2602 | silent : bool |
|
2587 | silent : bool | |
2603 | If True, avoid side-effects, such as implicit displayhooks and |
|
2588 | If True, avoid side-effects, such as implicit displayhooks and | |
2604 | and logging. silent=True forces store_history=False. |
|
2589 | and logging. silent=True forces store_history=False. | |
2605 | shell_futures : bool |
|
2590 | shell_futures : bool | |
2606 | If True, the code will share future statements with the interactive |
|
2591 | If True, the code will share future statements with the interactive | |
2607 | shell. It will both be affected by previous __future__ imports, and |
|
2592 | shell. It will both be affected by previous __future__ imports, and | |
2608 | any __future__ imports in the code will affect the shell. If False, |
|
2593 | any __future__ imports in the code will affect the shell. If False, | |
2609 | __future__ imports are not shared in either direction. |
|
2594 | __future__ imports are not shared in either direction. | |
2610 |
|
2595 | |||
2611 | Returns |
|
2596 | Returns | |
2612 | ------- |
|
2597 | ------- | |
2613 | result : :class:`ExecutionResult` |
|
2598 | result : :class:`ExecutionResult` | |
2614 | """ |
|
2599 | """ | |
2615 | result = ExecutionResult() |
|
2600 | result = ExecutionResult() | |
2616 |
|
2601 | |||
2617 | if (not raw_cell) or raw_cell.isspace(): |
|
2602 | if (not raw_cell) or raw_cell.isspace(): | |
2618 | self.last_execution_succeeded = True |
|
2603 | self.last_execution_succeeded = True | |
2619 | return result |
|
2604 | return result | |
2620 |
|
2605 | |||
2621 | if silent: |
|
2606 | if silent: | |
2622 | store_history = False |
|
2607 | store_history = False | |
2623 |
|
2608 | |||
2624 | if store_history: |
|
2609 | if store_history: | |
2625 | result.execution_count = self.execution_count |
|
2610 | result.execution_count = self.execution_count | |
2626 |
|
2611 | |||
2627 | def error_before_exec(value): |
|
2612 | def error_before_exec(value): | |
2628 | result.error_before_exec = value |
|
2613 | result.error_before_exec = value | |
2629 | self.last_execution_succeeded = False |
|
2614 | self.last_execution_succeeded = False | |
2630 | return result |
|
2615 | return result | |
2631 |
|
2616 | |||
2632 | self.events.trigger('pre_execute') |
|
2617 | self.events.trigger('pre_execute') | |
2633 | if not silent: |
|
2618 | if not silent: | |
2634 | self.events.trigger('pre_run_cell') |
|
2619 | self.events.trigger('pre_run_cell') | |
2635 |
|
2620 | |||
2636 | # If any of our input transformation (input_transformer_manager or |
|
2621 | # If any of our input transformation (input_transformer_manager or | |
2637 | # prefilter_manager) raises an exception, we store it in this variable |
|
2622 | # prefilter_manager) raises an exception, we store it in this variable | |
2638 | # so that we can display the error after logging the input and storing |
|
2623 | # so that we can display the error after logging the input and storing | |
2639 | # it in the history. |
|
2624 | # it in the history. | |
2640 | preprocessing_exc_tuple = None |
|
2625 | preprocessing_exc_tuple = None | |
2641 | try: |
|
2626 | try: | |
2642 | # Static input transformations |
|
2627 | # Static input transformations | |
2643 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
2628 | cell = self.input_transformer_manager.transform_cell(raw_cell) | |
2644 | except SyntaxError: |
|
2629 | except SyntaxError: | |
2645 | preprocessing_exc_tuple = sys.exc_info() |
|
2630 | preprocessing_exc_tuple = sys.exc_info() | |
2646 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2631 | cell = raw_cell # cell has to exist so it can be stored/logged | |
2647 | else: |
|
2632 | else: | |
2648 | if len(cell.splitlines()) == 1: |
|
2633 | if len(cell.splitlines()) == 1: | |
2649 | # Dynamic transformations - only applied for single line commands |
|
2634 | # Dynamic transformations - only applied for single line commands | |
2650 | with self.builtin_trap: |
|
2635 | with self.builtin_trap: | |
2651 | try: |
|
2636 | try: | |
2652 | # use prefilter_lines to handle trailing newlines |
|
2637 | # use prefilter_lines to handle trailing newlines | |
2653 | # restore trailing newline for ast.parse |
|
2638 | # restore trailing newline for ast.parse | |
2654 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2639 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
2655 | except Exception: |
|
2640 | except Exception: | |
2656 | # don't allow prefilter errors to crash IPython |
|
2641 | # don't allow prefilter errors to crash IPython | |
2657 | preprocessing_exc_tuple = sys.exc_info() |
|
2642 | preprocessing_exc_tuple = sys.exc_info() | |
2658 |
|
2643 | |||
2659 | # Store raw and processed history |
|
2644 | # Store raw and processed history | |
2660 | if store_history: |
|
2645 | if store_history: | |
2661 | self.history_manager.store_inputs(self.execution_count, |
|
2646 | self.history_manager.store_inputs(self.execution_count, | |
2662 | cell, raw_cell) |
|
2647 | cell, raw_cell) | |
2663 | if not silent: |
|
2648 | if not silent: | |
2664 | self.logger.log(cell, raw_cell) |
|
2649 | self.logger.log(cell, raw_cell) | |
2665 |
|
2650 | |||
2666 | # Display the exception if input processing failed. |
|
2651 | # Display the exception if input processing failed. | |
2667 | if preprocessing_exc_tuple is not None: |
|
2652 | if preprocessing_exc_tuple is not None: | |
2668 | self.showtraceback(preprocessing_exc_tuple) |
|
2653 | self.showtraceback(preprocessing_exc_tuple) | |
2669 | if store_history: |
|
2654 | if store_history: | |
2670 | self.execution_count += 1 |
|
2655 | self.execution_count += 1 | |
2671 | return error_before_exec(preprocessing_exc_tuple[2]) |
|
2656 | return error_before_exec(preprocessing_exc_tuple[2]) | |
2672 |
|
2657 | |||
2673 | # Our own compiler remembers the __future__ environment. If we want to |
|
2658 | # Our own compiler remembers the __future__ environment. If we want to | |
2674 | # run code with a separate __future__ environment, use the default |
|
2659 | # run code with a separate __future__ environment, use the default | |
2675 | # compiler |
|
2660 | # compiler | |
2676 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2661 | compiler = self.compile if shell_futures else CachingCompiler() | |
2677 |
|
2662 | |||
2678 | with self.builtin_trap: |
|
2663 | with self.builtin_trap: | |
2679 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2664 | cell_name = self.compile.cache(cell, self.execution_count) | |
2680 |
|
2665 | |||
2681 | with self.display_trap: |
|
2666 | with self.display_trap: | |
2682 | # Compile to bytecode |
|
2667 | # Compile to bytecode | |
2683 | try: |
|
2668 | try: | |
2684 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2669 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
2685 | except self.custom_exceptions as e: |
|
2670 | except self.custom_exceptions as e: | |
2686 | etype, value, tb = sys.exc_info() |
|
2671 | etype, value, tb = sys.exc_info() | |
2687 | self.CustomTB(etype, value, tb) |
|
2672 | self.CustomTB(etype, value, tb) | |
2688 | return error_before_exec(e) |
|
2673 | return error_before_exec(e) | |
2689 | except IndentationError as e: |
|
2674 | except IndentationError as e: | |
2690 | self.showindentationerror() |
|
2675 | self.showindentationerror() | |
2691 | if store_history: |
|
2676 | if store_history: | |
2692 | self.execution_count += 1 |
|
2677 | self.execution_count += 1 | |
2693 | return error_before_exec(e) |
|
2678 | return error_before_exec(e) | |
2694 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2679 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
2695 | MemoryError) as e: |
|
2680 | MemoryError) as e: | |
2696 | self.showsyntaxerror() |
|
2681 | self.showsyntaxerror() | |
2697 | if store_history: |
|
2682 | if store_history: | |
2698 | self.execution_count += 1 |
|
2683 | self.execution_count += 1 | |
2699 | return error_before_exec(e) |
|
2684 | return error_before_exec(e) | |
2700 |
|
2685 | |||
2701 | # Apply AST transformations |
|
2686 | # Apply AST transformations | |
2702 | try: |
|
2687 | try: | |
2703 | code_ast = self.transform_ast(code_ast) |
|
2688 | code_ast = self.transform_ast(code_ast) | |
2704 | except InputRejected as e: |
|
2689 | except InputRejected as e: | |
2705 | self.showtraceback() |
|
2690 | self.showtraceback() | |
2706 | if store_history: |
|
2691 | if store_history: | |
2707 | self.execution_count += 1 |
|
2692 | self.execution_count += 1 | |
2708 | return error_before_exec(e) |
|
2693 | return error_before_exec(e) | |
2709 |
|
2694 | |||
2710 | # Give the displayhook a reference to our ExecutionResult so it |
|
2695 | # Give the displayhook a reference to our ExecutionResult so it | |
2711 | # can fill in the output value. |
|
2696 | # can fill in the output value. | |
2712 | self.displayhook.exec_result = result |
|
2697 | self.displayhook.exec_result = result | |
2713 |
|
2698 | |||
2714 | # Execute the user code |
|
2699 | # Execute the user code | |
2715 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2700 | interactivity = "none" if silent else self.ast_node_interactivity | |
2716 | has_raised = self.run_ast_nodes(code_ast.body, cell_name, |
|
2701 | has_raised = self.run_ast_nodes(code_ast.body, cell_name, | |
2717 | interactivity=interactivity, compiler=compiler, result=result) |
|
2702 | interactivity=interactivity, compiler=compiler, result=result) | |
2718 |
|
2703 | |||
2719 | self.last_execution_succeeded = not has_raised |
|
2704 | self.last_execution_succeeded = not has_raised | |
2720 |
|
2705 | |||
2721 | # Reset this so later displayed values do not modify the |
|
2706 | # Reset this so later displayed values do not modify the | |
2722 | # ExecutionResult |
|
2707 | # ExecutionResult | |
2723 | self.displayhook.exec_result = None |
|
2708 | self.displayhook.exec_result = None | |
2724 |
|
2709 | |||
2725 | self.events.trigger('post_execute') |
|
2710 | self.events.trigger('post_execute') | |
2726 | if not silent: |
|
2711 | if not silent: | |
2727 | self.events.trigger('post_run_cell') |
|
2712 | self.events.trigger('post_run_cell') | |
2728 |
|
2713 | |||
2729 | if store_history: |
|
2714 | if store_history: | |
2730 | # Write output to the database. Does nothing unless |
|
2715 | # Write output to the database. Does nothing unless | |
2731 | # history output logging is enabled. |
|
2716 | # history output logging is enabled. | |
2732 | self.history_manager.store_output(self.execution_count) |
|
2717 | self.history_manager.store_output(self.execution_count) | |
2733 | # Each cell is a *single* input, regardless of how many lines it has |
|
2718 | # Each cell is a *single* input, regardless of how many lines it has | |
2734 | self.execution_count += 1 |
|
2719 | self.execution_count += 1 | |
2735 |
|
2720 | |||
2736 | return result |
|
2721 | return result | |
2737 |
|
2722 | |||
2738 | def transform_ast(self, node): |
|
2723 | def transform_ast(self, node): | |
2739 | """Apply the AST transformations from self.ast_transformers |
|
2724 | """Apply the AST transformations from self.ast_transformers | |
2740 |
|
2725 | |||
2741 | Parameters |
|
2726 | Parameters | |
2742 | ---------- |
|
2727 | ---------- | |
2743 | node : ast.Node |
|
2728 | node : ast.Node | |
2744 | The root node to be transformed. Typically called with the ast.Module |
|
2729 | The root node to be transformed. Typically called with the ast.Module | |
2745 | produced by parsing user input. |
|
2730 | produced by parsing user input. | |
2746 |
|
2731 | |||
2747 | Returns |
|
2732 | Returns | |
2748 | ------- |
|
2733 | ------- | |
2749 | An ast.Node corresponding to the node it was called with. Note that it |
|
2734 | An ast.Node corresponding to the node it was called with. Note that it | |
2750 | may also modify the passed object, so don't rely on references to the |
|
2735 | may also modify the passed object, so don't rely on references to the | |
2751 | original AST. |
|
2736 | original AST. | |
2752 | """ |
|
2737 | """ | |
2753 | for transformer in self.ast_transformers: |
|
2738 | for transformer in self.ast_transformers: | |
2754 | try: |
|
2739 | try: | |
2755 | node = transformer.visit(node) |
|
2740 | node = transformer.visit(node) | |
2756 | except InputRejected: |
|
2741 | except InputRejected: | |
2757 | # User-supplied AST transformers can reject an input by raising |
|
2742 | # User-supplied AST transformers can reject an input by raising | |
2758 | # an InputRejected. Short-circuit in this case so that we |
|
2743 | # an InputRejected. Short-circuit in this case so that we | |
2759 | # don't unregister the transform. |
|
2744 | # don't unregister the transform. | |
2760 | raise |
|
2745 | raise | |
2761 | except Exception: |
|
2746 | except Exception: | |
2762 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2747 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
2763 | self.ast_transformers.remove(transformer) |
|
2748 | self.ast_transformers.remove(transformer) | |
2764 |
|
2749 | |||
2765 | if self.ast_transformers: |
|
2750 | if self.ast_transformers: | |
2766 | ast.fix_missing_locations(node) |
|
2751 | ast.fix_missing_locations(node) | |
2767 | return node |
|
2752 | return node | |
2768 |
|
2753 | |||
2769 |
|
2754 | |||
2770 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', |
|
2755 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', | |
2771 | compiler=compile, result=None): |
|
2756 | compiler=compile, result=None): | |
2772 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2757 | """Run a sequence of AST nodes. The execution mode depends on the | |
2773 | interactivity parameter. |
|
2758 | interactivity parameter. | |
2774 |
|
2759 | |||
2775 | Parameters |
|
2760 | Parameters | |
2776 | ---------- |
|
2761 | ---------- | |
2777 | nodelist : list |
|
2762 | nodelist : list | |
2778 | A sequence of AST nodes to run. |
|
2763 | A sequence of AST nodes to run. | |
2779 | cell_name : str |
|
2764 | cell_name : str | |
2780 | Will be passed to the compiler as the filename of the cell. Typically |
|
2765 | Will be passed to the compiler as the filename of the cell. Typically | |
2781 | the value returned by ip.compile.cache(cell). |
|
2766 | the value returned by ip.compile.cache(cell). | |
2782 | interactivity : str |
|
2767 | interactivity : str | |
2783 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2768 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be | |
2784 | run interactively (displaying output from expressions). 'last_expr' |
|
2769 | run interactively (displaying output from expressions). 'last_expr' | |
2785 | will run the last node interactively only if it is an expression (i.e. |
|
2770 | will run the last node interactively only if it is an expression (i.e. | |
2786 | expressions in loops or other blocks are not displayed. Other values |
|
2771 | expressions in loops or other blocks are not displayed. Other values | |
2787 | for this parameter will raise a ValueError. |
|
2772 | for this parameter will raise a ValueError. | |
2788 | compiler : callable |
|
2773 | compiler : callable | |
2789 | A function with the same interface as the built-in compile(), to turn |
|
2774 | A function with the same interface as the built-in compile(), to turn | |
2790 | the AST nodes into code objects. Default is the built-in compile(). |
|
2775 | the AST nodes into code objects. Default is the built-in compile(). | |
2791 | result : ExecutionResult, optional |
|
2776 | result : ExecutionResult, optional | |
2792 | An object to store exceptions that occur during execution. |
|
2777 | An object to store exceptions that occur during execution. | |
2793 |
|
2778 | |||
2794 | Returns |
|
2779 | Returns | |
2795 | ------- |
|
2780 | ------- | |
2796 | True if an exception occurred while running code, False if it finished |
|
2781 | True if an exception occurred while running code, False if it finished | |
2797 | running. |
|
2782 | running. | |
2798 | """ |
|
2783 | """ | |
2799 | if not nodelist: |
|
2784 | if not nodelist: | |
2800 | return |
|
2785 | return | |
2801 |
|
2786 | |||
2802 | if interactivity == 'last_expr': |
|
2787 | if interactivity == 'last_expr': | |
2803 | if isinstance(nodelist[-1], ast.Expr): |
|
2788 | if isinstance(nodelist[-1], ast.Expr): | |
2804 | interactivity = "last" |
|
2789 | interactivity = "last" | |
2805 | else: |
|
2790 | else: | |
2806 | interactivity = "none" |
|
2791 | interactivity = "none" | |
2807 |
|
2792 | |||
2808 | if interactivity == 'none': |
|
2793 | if interactivity == 'none': | |
2809 | to_run_exec, to_run_interactive = nodelist, [] |
|
2794 | to_run_exec, to_run_interactive = nodelist, [] | |
2810 | elif interactivity == 'last': |
|
2795 | elif interactivity == 'last': | |
2811 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2796 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
2812 | elif interactivity == 'all': |
|
2797 | elif interactivity == 'all': | |
2813 | to_run_exec, to_run_interactive = [], nodelist |
|
2798 | to_run_exec, to_run_interactive = [], nodelist | |
2814 | else: |
|
2799 | else: | |
2815 | raise ValueError("Interactivity was %r" % interactivity) |
|
2800 | raise ValueError("Interactivity was %r" % interactivity) | |
2816 |
|
2801 | |||
2817 | try: |
|
2802 | try: | |
2818 | for i, node in enumerate(to_run_exec): |
|
2803 | for i, node in enumerate(to_run_exec): | |
2819 | mod = ast.Module([node]) |
|
2804 | mod = ast.Module([node]) | |
2820 | code = compiler(mod, cell_name, "exec") |
|
2805 | code = compiler(mod, cell_name, "exec") | |
2821 | if self.run_code(code, result): |
|
2806 | if self.run_code(code, result): | |
2822 | return True |
|
2807 | return True | |
2823 |
|
2808 | |||
2824 | for i, node in enumerate(to_run_interactive): |
|
2809 | for i, node in enumerate(to_run_interactive): | |
2825 | mod = ast.Interactive([node]) |
|
2810 | mod = ast.Interactive([node]) | |
2826 | code = compiler(mod, cell_name, "single") |
|
2811 | code = compiler(mod, cell_name, "single") | |
2827 | if self.run_code(code, result): |
|
2812 | if self.run_code(code, result): | |
2828 | return True |
|
2813 | return True | |
2829 |
|
2814 | |||
2830 | # Flush softspace |
|
2815 | # Flush softspace | |
2831 | if softspace(sys.stdout, 0): |
|
2816 | if softspace(sys.stdout, 0): | |
2832 | print() |
|
2817 | print() | |
2833 |
|
2818 | |||
2834 | except: |
|
2819 | except: | |
2835 | # It's possible to have exceptions raised here, typically by |
|
2820 | # It's possible to have exceptions raised here, typically by | |
2836 | # compilation of odd code (such as a naked 'return' outside a |
|
2821 | # compilation of odd code (such as a naked 'return' outside a | |
2837 | # function) that did parse but isn't valid. Typically the exception |
|
2822 | # function) that did parse but isn't valid. Typically the exception | |
2838 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2823 | # is a SyntaxError, but it's safest just to catch anything and show | |
2839 | # the user a traceback. |
|
2824 | # the user a traceback. | |
2840 |
|
2825 | |||
2841 | # We do only one try/except outside the loop to minimize the impact |
|
2826 | # We do only one try/except outside the loop to minimize the impact | |
2842 | # on runtime, and also because if any node in the node list is |
|
2827 | # on runtime, and also because if any node in the node list is | |
2843 | # broken, we should stop execution completely. |
|
2828 | # broken, we should stop execution completely. | |
2844 | if result: |
|
2829 | if result: | |
2845 | result.error_before_exec = sys.exc_info()[1] |
|
2830 | result.error_before_exec = sys.exc_info()[1] | |
2846 | self.showtraceback() |
|
2831 | self.showtraceback() | |
2847 | return True |
|
2832 | return True | |
2848 |
|
2833 | |||
2849 | return False |
|
2834 | return False | |
2850 |
|
2835 | |||
2851 | def run_code(self, code_obj, result=None): |
|
2836 | def run_code(self, code_obj, result=None): | |
2852 | """Execute a code object. |
|
2837 | """Execute a code object. | |
2853 |
|
2838 | |||
2854 | When an exception occurs, self.showtraceback() is called to display a |
|
2839 | When an exception occurs, self.showtraceback() is called to display a | |
2855 | traceback. |
|
2840 | traceback. | |
2856 |
|
2841 | |||
2857 | Parameters |
|
2842 | Parameters | |
2858 | ---------- |
|
2843 | ---------- | |
2859 | code_obj : code object |
|
2844 | code_obj : code object | |
2860 | A compiled code object, to be executed |
|
2845 | A compiled code object, to be executed | |
2861 | result : ExecutionResult, optional |
|
2846 | result : ExecutionResult, optional | |
2862 | An object to store exceptions that occur during execution. |
|
2847 | An object to store exceptions that occur during execution. | |
2863 |
|
2848 | |||
2864 | Returns |
|
2849 | Returns | |
2865 | ------- |
|
2850 | ------- | |
2866 | False : successful execution. |
|
2851 | False : successful execution. | |
2867 | True : an error occurred. |
|
2852 | True : an error occurred. | |
2868 | """ |
|
2853 | """ | |
2869 | # Set our own excepthook in case the user code tries to call it |
|
2854 | # Set our own excepthook in case the user code tries to call it | |
2870 | # directly, so that the IPython crash handler doesn't get triggered |
|
2855 | # directly, so that the IPython crash handler doesn't get triggered | |
2871 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
2856 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook | |
2872 |
|
2857 | |||
2873 | # we save the original sys.excepthook in the instance, in case config |
|
2858 | # we save the original sys.excepthook in the instance, in case config | |
2874 | # code (such as magics) needs access to it. |
|
2859 | # code (such as magics) needs access to it. | |
2875 | self.sys_excepthook = old_excepthook |
|
2860 | self.sys_excepthook = old_excepthook | |
2876 | outflag = 1 # happens in more places, so it's easier as default |
|
2861 | outflag = 1 # happens in more places, so it's easier as default | |
2877 | try: |
|
2862 | try: | |
2878 | try: |
|
2863 | try: | |
2879 | self.hooks.pre_run_code_hook() |
|
2864 | self.hooks.pre_run_code_hook() | |
2880 | #rprint('Running code', repr(code_obj)) # dbg |
|
2865 | #rprint('Running code', repr(code_obj)) # dbg | |
2881 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
2866 | exec(code_obj, self.user_global_ns, self.user_ns) | |
2882 | finally: |
|
2867 | finally: | |
2883 | # Reset our crash handler in place |
|
2868 | # Reset our crash handler in place | |
2884 | sys.excepthook = old_excepthook |
|
2869 | sys.excepthook = old_excepthook | |
2885 | except SystemExit as e: |
|
2870 | except SystemExit as e: | |
2886 | if result is not None: |
|
2871 | if result is not None: | |
2887 | result.error_in_exec = e |
|
2872 | result.error_in_exec = e | |
2888 | self.showtraceback(exception_only=True) |
|
2873 | self.showtraceback(exception_only=True) | |
2889 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) |
|
2874 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) | |
2890 | except self.custom_exceptions: |
|
2875 | except self.custom_exceptions: | |
2891 | etype, value, tb = sys.exc_info() |
|
2876 | etype, value, tb = sys.exc_info() | |
2892 | if result is not None: |
|
2877 | if result is not None: | |
2893 | result.error_in_exec = value |
|
2878 | result.error_in_exec = value | |
2894 | self.CustomTB(etype, value, tb) |
|
2879 | self.CustomTB(etype, value, tb) | |
2895 | except: |
|
2880 | except: | |
2896 | if result is not None: |
|
2881 | if result is not None: | |
2897 | result.error_in_exec = sys.exc_info()[1] |
|
2882 | result.error_in_exec = sys.exc_info()[1] | |
2898 | self.showtraceback() |
|
2883 | self.showtraceback() | |
2899 | else: |
|
2884 | else: | |
2900 | outflag = 0 |
|
2885 | outflag = 0 | |
2901 | return outflag |
|
2886 | return outflag | |
2902 |
|
2887 | |||
2903 | # For backwards compatibility |
|
2888 | # For backwards compatibility | |
2904 | runcode = run_code |
|
2889 | runcode = run_code | |
2905 |
|
2890 | |||
2906 | #------------------------------------------------------------------------- |
|
2891 | #------------------------------------------------------------------------- | |
2907 | # Things related to GUI support and pylab |
|
2892 | # Things related to GUI support and pylab | |
2908 | #------------------------------------------------------------------------- |
|
2893 | #------------------------------------------------------------------------- | |
2909 |
|
2894 | |||
2910 | def enable_gui(self, gui=None): |
|
2895 | def enable_gui(self, gui=None): | |
2911 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2896 | raise NotImplementedError('Implement enable_gui in a subclass') | |
2912 |
|
2897 | |||
2913 | def enable_matplotlib(self, gui=None): |
|
2898 | def enable_matplotlib(self, gui=None): | |
2914 | """Enable interactive matplotlib and inline figure support. |
|
2899 | """Enable interactive matplotlib and inline figure support. | |
2915 |
|
2900 | |||
2916 | This takes the following steps: |
|
2901 | This takes the following steps: | |
2917 |
|
2902 | |||
2918 | 1. select the appropriate eventloop and matplotlib backend |
|
2903 | 1. select the appropriate eventloop and matplotlib backend | |
2919 | 2. set up matplotlib for interactive use with that backend |
|
2904 | 2. set up matplotlib for interactive use with that backend | |
2920 | 3. configure formatters for inline figure display |
|
2905 | 3. configure formatters for inline figure display | |
2921 | 4. enable the selected gui eventloop |
|
2906 | 4. enable the selected gui eventloop | |
2922 |
|
2907 | |||
2923 | Parameters |
|
2908 | Parameters | |
2924 | ---------- |
|
2909 | ---------- | |
2925 | gui : optional, string |
|
2910 | gui : optional, string | |
2926 | If given, dictates the choice of matplotlib GUI backend to use |
|
2911 | If given, dictates the choice of matplotlib GUI backend to use | |
2927 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2912 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2928 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2913 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2929 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2914 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2930 | user's matplotlibrc configuration file). Note that not all backends |
|
2915 | user's matplotlibrc configuration file). Note that not all backends | |
2931 | make sense in all contexts, for example a terminal ipython can't |
|
2916 | make sense in all contexts, for example a terminal ipython can't | |
2932 | display figures inline. |
|
2917 | display figures inline. | |
2933 | """ |
|
2918 | """ | |
2934 | from IPython.core import pylabtools as pt |
|
2919 | from IPython.core import pylabtools as pt | |
2935 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
2920 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
2936 |
|
2921 | |||
2937 | if gui != 'inline': |
|
2922 | if gui != 'inline': | |
2938 | # If we have our first gui selection, store it |
|
2923 | # If we have our first gui selection, store it | |
2939 | if self.pylab_gui_select is None: |
|
2924 | if self.pylab_gui_select is None: | |
2940 | self.pylab_gui_select = gui |
|
2925 | self.pylab_gui_select = gui | |
2941 | # Otherwise if they are different |
|
2926 | # Otherwise if they are different | |
2942 | elif gui != self.pylab_gui_select: |
|
2927 | elif gui != self.pylab_gui_select: | |
2943 | print ('Warning: Cannot change to a different GUI toolkit: %s.' |
|
2928 | print ('Warning: Cannot change to a different GUI toolkit: %s.' | |
2944 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
2929 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
2945 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
2930 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
2946 |
|
2931 | |||
2947 | pt.activate_matplotlib(backend) |
|
2932 | pt.activate_matplotlib(backend) | |
2948 | pt.configure_inline_support(self, backend) |
|
2933 | pt.configure_inline_support(self, backend) | |
2949 |
|
2934 | |||
2950 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2935 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2951 | # plot updates into account |
|
2936 | # plot updates into account | |
2952 | self.enable_gui(gui) |
|
2937 | self.enable_gui(gui) | |
2953 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2938 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
2954 | pt.mpl_runner(self.safe_execfile) |
|
2939 | pt.mpl_runner(self.safe_execfile) | |
2955 |
|
2940 | |||
2956 | return gui, backend |
|
2941 | return gui, backend | |
2957 |
|
2942 | |||
2958 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
2943 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
2959 | """Activate pylab support at runtime. |
|
2944 | """Activate pylab support at runtime. | |
2960 |
|
2945 | |||
2961 | This turns on support for matplotlib, preloads into the interactive |
|
2946 | This turns on support for matplotlib, preloads into the interactive | |
2962 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2947 | namespace all of numpy and pylab, and configures IPython to correctly | |
2963 | interact with the GUI event loop. The GUI backend to be used can be |
|
2948 | interact with the GUI event loop. The GUI backend to be used can be | |
2964 | optionally selected with the optional ``gui`` argument. |
|
2949 | optionally selected with the optional ``gui`` argument. | |
2965 |
|
2950 | |||
2966 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
2951 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
2967 |
|
2952 | |||
2968 | Parameters |
|
2953 | Parameters | |
2969 | ---------- |
|
2954 | ---------- | |
2970 | gui : optional, string |
|
2955 | gui : optional, string | |
2971 | If given, dictates the choice of matplotlib GUI backend to use |
|
2956 | If given, dictates the choice of matplotlib GUI backend to use | |
2972 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2957 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
2973 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2958 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
2974 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2959 | matplotlib (as dictated by the matplotlib build-time options plus the | |
2975 | user's matplotlibrc configuration file). Note that not all backends |
|
2960 | user's matplotlibrc configuration file). Note that not all backends | |
2976 | make sense in all contexts, for example a terminal ipython can't |
|
2961 | make sense in all contexts, for example a terminal ipython can't | |
2977 | display figures inline. |
|
2962 | display figures inline. | |
2978 | import_all : optional, bool, default: True |
|
2963 | import_all : optional, bool, default: True | |
2979 | Whether to do `from numpy import *` and `from pylab import *` |
|
2964 | Whether to do `from numpy import *` and `from pylab import *` | |
2980 | in addition to module imports. |
|
2965 | in addition to module imports. | |
2981 | welcome_message : deprecated |
|
2966 | welcome_message : deprecated | |
2982 | This argument is ignored, no welcome message will be displayed. |
|
2967 | This argument is ignored, no welcome message will be displayed. | |
2983 | """ |
|
2968 | """ | |
2984 | from IPython.core.pylabtools import import_pylab |
|
2969 | from IPython.core.pylabtools import import_pylab | |
2985 |
|
2970 | |||
2986 | gui, backend = self.enable_matplotlib(gui) |
|
2971 | gui, backend = self.enable_matplotlib(gui) | |
2987 |
|
2972 | |||
2988 | # We want to prevent the loading of pylab to pollute the user's |
|
2973 | # We want to prevent the loading of pylab to pollute the user's | |
2989 | # namespace as shown by the %who* magics, so we execute the activation |
|
2974 | # namespace as shown by the %who* magics, so we execute the activation | |
2990 | # code in an empty namespace, and we update *both* user_ns and |
|
2975 | # code in an empty namespace, and we update *both* user_ns and | |
2991 | # user_ns_hidden with this information. |
|
2976 | # user_ns_hidden with this information. | |
2992 | ns = {} |
|
2977 | ns = {} | |
2993 | import_pylab(ns, import_all) |
|
2978 | import_pylab(ns, import_all) | |
2994 | # warn about clobbered names |
|
2979 | # warn about clobbered names | |
2995 | ignored = {"__builtins__"} |
|
2980 | ignored = {"__builtins__"} | |
2996 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
2981 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
2997 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
2982 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
2998 | self.user_ns.update(ns) |
|
2983 | self.user_ns.update(ns) | |
2999 | self.user_ns_hidden.update(ns) |
|
2984 | self.user_ns_hidden.update(ns) | |
3000 | return gui, backend, clobbered |
|
2985 | return gui, backend, clobbered | |
3001 |
|
2986 | |||
3002 | #------------------------------------------------------------------------- |
|
2987 | #------------------------------------------------------------------------- | |
3003 | # Utilities |
|
2988 | # Utilities | |
3004 | #------------------------------------------------------------------------- |
|
2989 | #------------------------------------------------------------------------- | |
3005 |
|
2990 | |||
3006 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
2991 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
3007 | """Expand python variables in a string. |
|
2992 | """Expand python variables in a string. | |
3008 |
|
2993 | |||
3009 | The depth argument indicates how many frames above the caller should |
|
2994 | The depth argument indicates how many frames above the caller should | |
3010 | be walked to look for the local namespace where to expand variables. |
|
2995 | be walked to look for the local namespace where to expand variables. | |
3011 |
|
2996 | |||
3012 | The global namespace for expansion is always the user's interactive |
|
2997 | The global namespace for expansion is always the user's interactive | |
3013 | namespace. |
|
2998 | namespace. | |
3014 | """ |
|
2999 | """ | |
3015 | ns = self.user_ns.copy() |
|
3000 | ns = self.user_ns.copy() | |
3016 | try: |
|
3001 | try: | |
3017 | frame = sys._getframe(depth+1) |
|
3002 | frame = sys._getframe(depth+1) | |
3018 | except ValueError: |
|
3003 | except ValueError: | |
3019 | # This is thrown if there aren't that many frames on the stack, |
|
3004 | # This is thrown if there aren't that many frames on the stack, | |
3020 | # e.g. if a script called run_line_magic() directly. |
|
3005 | # e.g. if a script called run_line_magic() directly. | |
3021 | pass |
|
3006 | pass | |
3022 | else: |
|
3007 | else: | |
3023 | ns.update(frame.f_locals) |
|
3008 | ns.update(frame.f_locals) | |
3024 |
|
3009 | |||
3025 | try: |
|
3010 | try: | |
3026 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3011 | # We have to use .vformat() here, because 'self' is a valid and common | |
3027 | # name, and expanding **ns for .format() would make it collide with |
|
3012 | # name, and expanding **ns for .format() would make it collide with | |
3028 | # the 'self' argument of the method. |
|
3013 | # the 'self' argument of the method. | |
3029 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3014 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
3030 | except Exception: |
|
3015 | except Exception: | |
3031 | # if formatter couldn't format, just let it go untransformed |
|
3016 | # if formatter couldn't format, just let it go untransformed | |
3032 | pass |
|
3017 | pass | |
3033 | return cmd |
|
3018 | return cmd | |
3034 |
|
3019 | |||
3035 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3020 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
3036 | """Make a new tempfile and return its filename. |
|
3021 | """Make a new tempfile and return its filename. | |
3037 |
|
3022 | |||
3038 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3023 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | |
3039 | but it registers the created filename internally so ipython cleans it up |
|
3024 | but it registers the created filename internally so ipython cleans it up | |
3040 | at exit time. |
|
3025 | at exit time. | |
3041 |
|
3026 | |||
3042 | Optional inputs: |
|
3027 | Optional inputs: | |
3043 |
|
3028 | |||
3044 | - data(None): if data is given, it gets written out to the temp file |
|
3029 | - data(None): if data is given, it gets written out to the temp file | |
3045 | immediately, and the file is closed again.""" |
|
3030 | immediately, and the file is closed again.""" | |
3046 |
|
3031 | |||
3047 | dirname = tempfile.mkdtemp(prefix=prefix) |
|
3032 | dirname = tempfile.mkdtemp(prefix=prefix) | |
3048 | self.tempdirs.append(dirname) |
|
3033 | self.tempdirs.append(dirname) | |
3049 |
|
3034 | |||
3050 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) |
|
3035 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) | |
3051 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3036 | os.close(handle) # On Windows, there can only be one open handle on a file | |
3052 | self.tempfiles.append(filename) |
|
3037 | self.tempfiles.append(filename) | |
3053 |
|
3038 | |||
3054 | if data: |
|
3039 | if data: | |
3055 | tmp_file = open(filename,'w') |
|
3040 | tmp_file = open(filename,'w') | |
3056 | tmp_file.write(data) |
|
3041 | tmp_file.write(data) | |
3057 | tmp_file.close() |
|
3042 | tmp_file.close() | |
3058 | return filename |
|
3043 | return filename | |
3059 |
|
3044 | |||
3060 | @undoc |
|
3045 | @undoc | |
3061 | def write(self,data): |
|
3046 | def write(self,data): | |
3062 | """DEPRECATED: Write a string to the default output""" |
|
3047 | """DEPRECATED: Write a string to the default output""" | |
3063 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', |
|
3048 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', | |
3064 | DeprecationWarning, stacklevel=2) |
|
3049 | DeprecationWarning, stacklevel=2) | |
3065 | sys.stdout.write(data) |
|
3050 | sys.stdout.write(data) | |
3066 |
|
3051 | |||
3067 | @undoc |
|
3052 | @undoc | |
3068 | def write_err(self,data): |
|
3053 | def write_err(self,data): | |
3069 | """DEPRECATED: Write a string to the default error output""" |
|
3054 | """DEPRECATED: Write a string to the default error output""" | |
3070 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', |
|
3055 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', | |
3071 | DeprecationWarning, stacklevel=2) |
|
3056 | DeprecationWarning, stacklevel=2) | |
3072 | sys.stderr.write(data) |
|
3057 | sys.stderr.write(data) | |
3073 |
|
3058 | |||
3074 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3059 | def ask_yes_no(self, prompt, default=None, interrupt=None): | |
3075 | if self.quiet: |
|
3060 | if self.quiet: | |
3076 | return True |
|
3061 | return True | |
3077 | return ask_yes_no(prompt,default,interrupt) |
|
3062 | return ask_yes_no(prompt,default,interrupt) | |
3078 |
|
3063 | |||
3079 | def show_usage(self): |
|
3064 | def show_usage(self): | |
3080 | """Show a usage message""" |
|
3065 | """Show a usage message""" | |
3081 | page.page(IPython.core.usage.interactive_usage) |
|
3066 | page.page(IPython.core.usage.interactive_usage) | |
3082 |
|
3067 | |||
3083 | def extract_input_lines(self, range_str, raw=False): |
|
3068 | def extract_input_lines(self, range_str, raw=False): | |
3084 | """Return as a string a set of input history slices. |
|
3069 | """Return as a string a set of input history slices. | |
3085 |
|
3070 | |||
3086 | Parameters |
|
3071 | Parameters | |
3087 | ---------- |
|
3072 | ---------- | |
3088 | range_str : string |
|
3073 | range_str : string | |
3089 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3074 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3090 | since this function is for use by magic functions which get their |
|
3075 | since this function is for use by magic functions which get their | |
3091 | arguments as strings. The number before the / is the session |
|
3076 | arguments as strings. The number before the / is the session | |
3092 | number: ~n goes n back from the current session. |
|
3077 | number: ~n goes n back from the current session. | |
3093 |
|
3078 | |||
3094 | raw : bool, optional |
|
3079 | raw : bool, optional | |
3095 | By default, the processed input is used. If this is true, the raw |
|
3080 | By default, the processed input is used. If this is true, the raw | |
3096 | input history is used instead. |
|
3081 | input history is used instead. | |
3097 |
|
3082 | |||
3098 | Notes |
|
3083 | Notes | |
3099 | ----- |
|
3084 | ----- | |
3100 |
|
3085 | |||
3101 | Slices can be described with two notations: |
|
3086 | Slices can be described with two notations: | |
3102 |
|
3087 | |||
3103 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3088 | * ``N:M`` -> standard python form, means including items N...(M-1). | |
3104 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3089 | * ``N-M`` -> include items N..M (closed endpoint). | |
3105 | """ |
|
3090 | """ | |
3106 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3091 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3107 | return "\n".join(x for _, _, x in lines) |
|
3092 | return "\n".join(x for _, _, x in lines) | |
3108 |
|
3093 | |||
3109 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3094 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3110 | """Get a code string from history, file, url, or a string or macro. |
|
3095 | """Get a code string from history, file, url, or a string or macro. | |
3111 |
|
3096 | |||
3112 | This is mainly used by magic functions. |
|
3097 | This is mainly used by magic functions. | |
3113 |
|
3098 | |||
3114 | Parameters |
|
3099 | Parameters | |
3115 | ---------- |
|
3100 | ---------- | |
3116 |
|
3101 | |||
3117 | target : str |
|
3102 | target : str | |
3118 |
|
3103 | |||
3119 | A string specifying code to retrieve. This will be tried respectively |
|
3104 | A string specifying code to retrieve. This will be tried respectively | |
3120 | as: ranges of input history (see %history for syntax), url, |
|
3105 | as: ranges of input history (see %history for syntax), url, | |
3121 | corresponding .py file, filename, or an expression evaluating to a |
|
3106 | corresponding .py file, filename, or an expression evaluating to a | |
3122 | string or Macro in the user namespace. |
|
3107 | string or Macro in the user namespace. | |
3123 |
|
3108 | |||
3124 | raw : bool |
|
3109 | raw : bool | |
3125 | If true (default), retrieve raw history. Has no effect on the other |
|
3110 | If true (default), retrieve raw history. Has no effect on the other | |
3126 | retrieval mechanisms. |
|
3111 | retrieval mechanisms. | |
3127 |
|
3112 | |||
3128 | py_only : bool (default False) |
|
3113 | py_only : bool (default False) | |
3129 | Only try to fetch python code, do not try alternative methods to decode file |
|
3114 | Only try to fetch python code, do not try alternative methods to decode file | |
3130 | if unicode fails. |
|
3115 | if unicode fails. | |
3131 |
|
3116 | |||
3132 | Returns |
|
3117 | Returns | |
3133 | ------- |
|
3118 | ------- | |
3134 | A string of code. |
|
3119 | A string of code. | |
3135 |
|
3120 | |||
3136 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3121 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3137 | to an object of another type. In each case, .args[0] is a printable |
|
3122 | to an object of another type. In each case, .args[0] is a printable | |
3138 | message. |
|
3123 | message. | |
3139 | """ |
|
3124 | """ | |
3140 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3125 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3141 | if code: |
|
3126 | if code: | |
3142 | return code |
|
3127 | return code | |
3143 | try: |
|
3128 | try: | |
3144 | if target.startswith(('http://', 'https://')): |
|
3129 | if target.startswith(('http://', 'https://')): | |
3145 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) |
|
3130 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) | |
3146 | except UnicodeDecodeError: |
|
3131 | except UnicodeDecodeError: | |
3147 | if not py_only : |
|
3132 | if not py_only : | |
3148 | # Deferred import |
|
3133 | # Deferred import | |
3149 | try: |
|
3134 | try: | |
3150 | from urllib.request import urlopen # Py3 |
|
3135 | from urllib.request import urlopen # Py3 | |
3151 | except ImportError: |
|
3136 | except ImportError: | |
3152 | from urllib import urlopen |
|
3137 | from urllib import urlopen | |
3153 | response = urlopen(target) |
|
3138 | response = urlopen(target) | |
3154 | return response.read().decode('latin1') |
|
3139 | return response.read().decode('latin1') | |
3155 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3140 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3156 |
|
3141 | |||
3157 | potential_target = [target] |
|
3142 | potential_target = [target] | |
3158 | try : |
|
3143 | try : | |
3159 | potential_target.insert(0,get_py_filename(target)) |
|
3144 | potential_target.insert(0,get_py_filename(target)) | |
3160 | except IOError: |
|
3145 | except IOError: | |
3161 | pass |
|
3146 | pass | |
3162 |
|
3147 | |||
3163 | for tgt in potential_target : |
|
3148 | for tgt in potential_target : | |
3164 | if os.path.isfile(tgt): # Read file |
|
3149 | if os.path.isfile(tgt): # Read file | |
3165 | try : |
|
3150 | try : | |
3166 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3151 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3167 | except UnicodeDecodeError : |
|
3152 | except UnicodeDecodeError : | |
3168 | if not py_only : |
|
3153 | if not py_only : | |
3169 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3154 | with io_open(tgt,'r', encoding='latin1') as f : | |
3170 | return f.read() |
|
3155 | return f.read() | |
3171 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3156 | raise ValueError(("'%s' seem to be unreadable.") % target) | |
3172 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3157 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3173 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3158 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3174 |
|
3159 | |||
3175 | if search_ns: |
|
3160 | if search_ns: | |
3176 | # Inspect namespace to load object source |
|
3161 | # Inspect namespace to load object source | |
3177 | object_info = self.object_inspect(target, detail_level=1) |
|
3162 | object_info = self.object_inspect(target, detail_level=1) | |
3178 | if object_info['found'] and object_info['source']: |
|
3163 | if object_info['found'] and object_info['source']: | |
3179 | return object_info['source'] |
|
3164 | return object_info['source'] | |
3180 |
|
3165 | |||
3181 | try: # User namespace |
|
3166 | try: # User namespace | |
3182 | codeobj = eval(target, self.user_ns) |
|
3167 | codeobj = eval(target, self.user_ns) | |
3183 | except Exception: |
|
3168 | except Exception: | |
3184 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3169 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3185 | "nor in the user namespace.") % target) |
|
3170 | "nor in the user namespace.") % target) | |
3186 |
|
3171 | |||
3187 | if isinstance(codeobj, string_types): |
|
3172 | if isinstance(codeobj, string_types): | |
3188 | return codeobj |
|
3173 | return codeobj | |
3189 | elif isinstance(codeobj, Macro): |
|
3174 | elif isinstance(codeobj, Macro): | |
3190 | return codeobj.value |
|
3175 | return codeobj.value | |
3191 |
|
3176 | |||
3192 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3177 | raise TypeError("%s is neither a string nor a macro." % target, | |
3193 | codeobj) |
|
3178 | codeobj) | |
3194 |
|
3179 | |||
3195 | #------------------------------------------------------------------------- |
|
3180 | #------------------------------------------------------------------------- | |
3196 | # Things related to IPython exiting |
|
3181 | # Things related to IPython exiting | |
3197 | #------------------------------------------------------------------------- |
|
3182 | #------------------------------------------------------------------------- | |
3198 | def atexit_operations(self): |
|
3183 | def atexit_operations(self): | |
3199 | """This will be executed at the time of exit. |
|
3184 | """This will be executed at the time of exit. | |
3200 |
|
3185 | |||
3201 | Cleanup operations and saving of persistent data that is done |
|
3186 | Cleanup operations and saving of persistent data that is done | |
3202 | unconditionally by IPython should be performed here. |
|
3187 | unconditionally by IPython should be performed here. | |
3203 |
|
3188 | |||
3204 | For things that may depend on startup flags or platform specifics (such |
|
3189 | For things that may depend on startup flags or platform specifics (such | |
3205 | as having readline or not), register a separate atexit function in the |
|
3190 | as having readline or not), register a separate atexit function in the | |
3206 | code that has the appropriate information, rather than trying to |
|
3191 | code that has the appropriate information, rather than trying to | |
3207 | clutter |
|
3192 | clutter | |
3208 | """ |
|
3193 | """ | |
3209 | # Close the history session (this stores the end time and line count) |
|
3194 | # Close the history session (this stores the end time and line count) | |
3210 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3195 | # this must be *before* the tempfile cleanup, in case of temporary | |
3211 | # history db |
|
3196 | # history db | |
3212 | self.history_manager.end_session() |
|
3197 | self.history_manager.end_session() | |
3213 |
|
3198 | |||
3214 | # Cleanup all tempfiles and folders left around |
|
3199 | # Cleanup all tempfiles and folders left around | |
3215 | for tfile in self.tempfiles: |
|
3200 | for tfile in self.tempfiles: | |
3216 | try: |
|
3201 | try: | |
3217 | os.unlink(tfile) |
|
3202 | os.unlink(tfile) | |
3218 | except OSError: |
|
3203 | except OSError: | |
3219 | pass |
|
3204 | pass | |
3220 |
|
3205 | |||
3221 | for tdir in self.tempdirs: |
|
3206 | for tdir in self.tempdirs: | |
3222 | try: |
|
3207 | try: | |
3223 | os.rmdir(tdir) |
|
3208 | os.rmdir(tdir) | |
3224 | except OSError: |
|
3209 | except OSError: | |
3225 | pass |
|
3210 | pass | |
3226 |
|
3211 | |||
3227 | # Clear all user namespaces to release all references cleanly. |
|
3212 | # Clear all user namespaces to release all references cleanly. | |
3228 | self.reset(new_session=False) |
|
3213 | self.reset(new_session=False) | |
3229 |
|
3214 | |||
3230 | # Run user hooks |
|
3215 | # Run user hooks | |
3231 | self.hooks.shutdown_hook() |
|
3216 | self.hooks.shutdown_hook() | |
3232 |
|
3217 | |||
3233 | def cleanup(self): |
|
3218 | def cleanup(self): | |
3234 | self.restore_sys_module_state() |
|
3219 | self.restore_sys_module_state() | |
3235 |
|
3220 | |||
3236 |
|
3221 | |||
3237 | # Overridden in terminal subclass to change prompts |
|
3222 | # Overridden in terminal subclass to change prompts | |
3238 | def switch_doctest_mode(self, mode): |
|
3223 | def switch_doctest_mode(self, mode): | |
3239 | pass |
|
3224 | pass | |
3240 |
|
3225 | |||
3241 |
|
3226 | |||
3242 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): |
|
3227 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): | |
3243 | """An abstract base class for InteractiveShell.""" |
|
3228 | """An abstract base class for InteractiveShell.""" | |
3244 |
|
3229 | |||
3245 | InteractiveShellABC.register(InteractiveShell) |
|
3230 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,204 +1,204 b'' | |||||
1 | import errno |
|
1 | import errno | |
2 | import os |
|
2 | import os | |
3 | import shutil |
|
3 | import shutil | |
4 | import sys |
|
4 | import sys | |
5 | import tempfile |
|
5 | import tempfile | |
6 | import warnings |
|
6 | import warnings | |
7 |
|
7 | |||
8 | try: # Python 3 |
|
8 | try: # Python 3 | |
9 | from unittest.mock import patch |
|
9 | from unittest.mock import patch | |
10 | except ImportError: # Python 2 |
|
10 | except ImportError: # Python 2 | |
11 | from mock import patch |
|
11 | from mock import patch | |
12 |
|
12 | |||
13 | import nose.tools as nt |
|
13 | import nose.tools as nt | |
14 | from testpath import modified_env, assert_isdir, assert_isfile |
|
14 | from testpath import modified_env, assert_isdir, assert_isfile | |
15 |
|
15 | |||
16 | from IPython import paths |
|
16 | from IPython import paths | |
17 | from IPython.testing.decorators import skip_win32 |
|
17 | from IPython.testing.decorators import skip_win32 | |
18 | from IPython.utils.tempdir import TemporaryDirectory |
|
18 | from IPython.utils.tempdir import TemporaryDirectory | |
19 |
|
19 | |||
20 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
20 | TMP_TEST_DIR = os.path.realpath(tempfile.mkdtemp()) | |
21 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") |
|
21 | HOME_TEST_DIR = os.path.join(TMP_TEST_DIR, "home_test_dir") | |
22 | XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") |
|
22 | XDG_TEST_DIR = os.path.join(HOME_TEST_DIR, "xdg_test_dir") | |
23 | XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") |
|
23 | XDG_CACHE_DIR = os.path.join(HOME_TEST_DIR, "xdg_cache_dir") | |
24 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') |
|
24 | IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython') | |
25 |
|
25 | |||
26 | def setup(): |
|
26 | def setup(): | |
27 | """Setup testenvironment for the module: |
|
27 | """Setup testenvironment for the module: | |
28 |
|
28 | |||
29 | - Adds dummy home dir tree |
|
29 | - Adds dummy home dir tree | |
30 | """ |
|
30 | """ | |
31 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
31 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
32 | # problem because that exception is only defined on Windows... |
|
32 | # problem because that exception is only defined on Windows... | |
33 | os.makedirs(IP_TEST_DIR) |
|
33 | os.makedirs(IP_TEST_DIR) | |
34 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
34 | os.makedirs(os.path.join(XDG_TEST_DIR, 'ipython')) | |
35 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) |
|
35 | os.makedirs(os.path.join(XDG_CACHE_DIR, 'ipython')) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def teardown(): |
|
38 | def teardown(): | |
39 | """Teardown testenvironment for the module: |
|
39 | """Teardown testenvironment for the module: | |
40 |
|
40 | |||
41 | - Remove dummy home dir tree |
|
41 | - Remove dummy home dir tree | |
42 | """ |
|
42 | """ | |
43 | # Note: we remove the parent test dir, which is the root of all test |
|
43 | # Note: we remove the parent test dir, which is the root of all test | |
44 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
44 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
45 | # that non-empty directories are all recursively removed. |
|
45 | # that non-empty directories are all recursively removed. | |
46 | shutil.rmtree(TMP_TEST_DIR) |
|
46 | shutil.rmtree(TMP_TEST_DIR) | |
47 |
|
47 | |||
48 | def patch_get_home_dir(dirpath): |
|
48 | def patch_get_home_dir(dirpath): | |
49 | return patch.object(paths, 'get_home_dir', return_value=dirpath) |
|
49 | return patch.object(paths, 'get_home_dir', return_value=dirpath) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | def test_get_ipython_dir_1(): |
|
52 | def test_get_ipython_dir_1(): | |
53 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
53 | """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
54 | env_ipdir = os.path.join("someplace", ".ipython") |
|
54 | env_ipdir = os.path.join("someplace", ".ipython") | |
55 | with patch.object(paths, '_writable_dir', return_value=True), \ |
|
55 | with patch.object(paths, '_writable_dir', return_value=True), \ | |
56 | modified_env({'IPYTHONDIR': env_ipdir}): |
|
56 | modified_env({'IPYTHONDIR': env_ipdir}): | |
57 | ipdir = paths.get_ipython_dir() |
|
57 | ipdir = paths.get_ipython_dir() | |
58 |
|
58 | |||
59 | nt.assert_equal(ipdir, env_ipdir) |
|
59 | nt.assert_equal(ipdir, env_ipdir) | |
60 |
|
60 | |||
61 | def test_get_ipython_dir_2(): |
|
61 | def test_get_ipython_dir_2(): | |
62 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" |
|
62 | """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" | |
63 | with patch_get_home_dir('someplace'), \ |
|
63 | with patch_get_home_dir('someplace'), \ | |
64 | patch.object(paths, 'get_xdg_dir', return_value=None), \ |
|
64 | patch.object(paths, 'get_xdg_dir', return_value=None), \ | |
65 | patch.object(paths, '_writable_dir', return_value=True), \ |
|
65 | patch.object(paths, '_writable_dir', return_value=True), \ | |
66 | patch('os.name', "posix"), \ |
|
66 | patch('os.name', "posix"), \ | |
67 | modified_env({'IPYTHON_DIR': None, |
|
67 | modified_env({'IPYTHON_DIR': None, | |
68 | 'IPYTHONDIR': None, |
|
68 | 'IPYTHONDIR': None, | |
69 | 'XDG_CONFIG_HOME': None |
|
69 | 'XDG_CONFIG_HOME': None | |
70 | }): |
|
70 | }): | |
71 | ipdir = paths.get_ipython_dir() |
|
71 | ipdir = paths.get_ipython_dir() | |
72 |
|
72 | |||
73 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) |
|
73 | nt.assert_equal(ipdir, os.path.join("someplace", ".ipython")) | |
74 |
|
74 | |||
75 | def test_get_ipython_dir_3(): |
|
75 | def test_get_ipython_dir_3(): | |
76 | """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" |
|
76 | """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" | |
77 | tmphome = TemporaryDirectory() |
|
77 | tmphome = TemporaryDirectory() | |
78 | try: |
|
78 | try: | |
79 | with patch_get_home_dir(tmphome.name), \ |
|
79 | with patch_get_home_dir(tmphome.name), \ | |
80 | patch('os.name', 'posix'), \ |
|
80 | patch('os.name', 'posix'), \ | |
81 | modified_env({ |
|
81 | modified_env({ | |
82 | 'IPYTHON_DIR': None, |
|
82 | 'IPYTHON_DIR': None, | |
83 | 'IPYTHONDIR': None, |
|
83 | 'IPYTHONDIR': None, | |
84 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
84 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
85 | }), warnings.catch_warnings(record=True) as w: |
|
85 | }), warnings.catch_warnings(record=True) as w: | |
86 | ipdir = paths.get_ipython_dir() |
|
86 | ipdir = paths.get_ipython_dir() | |
87 |
|
87 | |||
88 | nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) |
|
88 | nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) | |
89 | if sys.platform != 'darwin': |
|
89 | if sys.platform != 'darwin': | |
90 | nt.assert_equal(len(w), 1) |
|
90 | nt.assert_equal(len(w), 1) | |
91 | nt.assert_in('Moving', str(w[0])) |
|
91 | nt.assert_in('Moving', str(w[0])) | |
92 | finally: |
|
92 | finally: | |
93 | tmphome.cleanup() |
|
93 | tmphome.cleanup() | |
94 |
|
94 | |||
95 | def test_get_ipython_dir_4(): |
|
95 | def test_get_ipython_dir_4(): | |
96 | """test_get_ipython_dir_4, warn if XDG and home both exist.""" |
|
96 | """test_get_ipython_dir_4, warn if XDG and home both exist.""" | |
97 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
97 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
98 | patch('os.name', 'posix'): |
|
98 | patch('os.name', 'posix'): | |
99 | try: |
|
99 | try: | |
100 | os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
100 | os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
101 | except OSError as e: |
|
101 | except OSError as e: | |
102 | if e.errno != errno.EEXIST: |
|
102 | if e.errno != errno.EEXIST: | |
103 | raise |
|
103 | raise | |
104 |
|
104 | |||
105 |
|
105 | |||
106 | with modified_env({ |
|
106 | with modified_env({ | |
107 | 'IPYTHON_DIR': None, |
|
107 | 'IPYTHON_DIR': None, | |
108 | 'IPYTHONDIR': None, |
|
108 | 'IPYTHONDIR': None, | |
109 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
109 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
110 | }), warnings.catch_warnings(record=True) as w: |
|
110 | }), warnings.catch_warnings(record=True) as w: | |
111 | ipdir = paths.get_ipython_dir() |
|
111 | ipdir = paths.get_ipython_dir() | |
112 |
|
112 | |||
113 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) |
|
113 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) | |
114 | if sys.platform != 'darwin': |
|
114 | if sys.platform != 'darwin': | |
115 | nt.assert_equal(len(w), 1) |
|
115 | nt.assert_equal(len(w), 1) | |
116 | nt.assert_in('Ignoring', str(w[0])) |
|
116 | nt.assert_in('Ignoring', str(w[0])) | |
117 |
|
117 | |||
118 | def test_get_ipython_dir_5(): |
|
118 | def test_get_ipython_dir_5(): | |
119 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" |
|
119 | """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" | |
120 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
120 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
121 | patch('os.name', 'posix'): |
|
121 | patch('os.name', 'posix'): | |
122 | try: |
|
122 | try: | |
123 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) |
|
123 | os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) | |
124 | except OSError as e: |
|
124 | except OSError as e: | |
125 | if e.errno != errno.ENOENT: |
|
125 | if e.errno != errno.ENOENT: | |
126 | raise |
|
126 | raise | |
127 |
|
127 | |||
128 | with modified_env({ |
|
128 | with modified_env({ | |
129 | 'IPYTHON_DIR': None, |
|
129 | 'IPYTHON_DIR': None, | |
130 | 'IPYTHONDIR': None, |
|
130 | 'IPYTHONDIR': None, | |
131 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, |
|
131 | 'XDG_CONFIG_HOME': XDG_TEST_DIR, | |
132 | }): |
|
132 | }): | |
133 | ipdir = paths.get_ipython_dir() |
|
133 | ipdir = paths.get_ipython_dir() | |
134 |
|
134 | |||
135 | nt.assert_equal(ipdir, IP_TEST_DIR) |
|
135 | nt.assert_equal(ipdir, IP_TEST_DIR) | |
136 |
|
136 | |||
137 | def test_get_ipython_dir_6(): |
|
137 | def test_get_ipython_dir_6(): | |
138 | """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" |
|
138 | """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" | |
139 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') |
|
139 | xdg = os.path.join(HOME_TEST_DIR, 'somexdg') | |
140 | os.mkdir(xdg) |
|
140 | os.mkdir(xdg) | |
141 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) |
|
141 | shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) | |
142 | print(paths._writable_dir) |
|
142 | print(paths._writable_dir) | |
143 | with patch_get_home_dir(HOME_TEST_DIR), \ |
|
143 | with patch_get_home_dir(HOME_TEST_DIR), \ | |
144 | patch.object(paths, 'get_xdg_dir', return_value=xdg), \ |
|
144 | patch.object(paths, 'get_xdg_dir', return_value=xdg), \ | |
145 | patch('os.name', 'posix'), \ |
|
145 | patch('os.name', 'posix'), \ | |
146 | modified_env({ |
|
146 | modified_env({ | |
147 | 'IPYTHON_DIR': None, |
|
147 | 'IPYTHON_DIR': None, | |
148 | 'IPYTHONDIR': None, |
|
148 | 'IPYTHONDIR': None, | |
149 | 'XDG_CONFIG_HOME': None, |
|
149 | 'XDG_CONFIG_HOME': None, | |
150 | }), warnings.catch_warnings(record=True) as w: |
|
150 | }), warnings.catch_warnings(record=True) as w: | |
151 | ipdir = paths.get_ipython_dir() |
|
151 | ipdir = paths.get_ipython_dir() | |
152 |
|
152 | |||
153 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) |
|
153 | nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) | |
154 | nt.assert_equal(len(w), 0) |
|
154 | nt.assert_equal(len(w), 0) | |
155 |
|
155 | |||
156 | def test_get_ipython_dir_7(): |
|
156 | def test_get_ipython_dir_7(): | |
157 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" |
|
157 | """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" | |
158 | home_dir = os.path.normpath(os.path.expanduser('~')) |
|
158 | home_dir = os.path.normpath(os.path.expanduser('~')) | |
159 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ |
|
159 | with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ | |
160 | patch.object(paths, '_writable_dir', return_value=True): |
|
160 | patch.object(paths, '_writable_dir', return_value=True): | |
161 | ipdir = paths.get_ipython_dir() |
|
161 | ipdir = paths.get_ipython_dir() | |
162 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) |
|
162 | nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere')) | |
163 |
|
163 | |||
164 | @skip_win32 |
|
164 | @skip_win32 | |
165 | def test_get_ipython_dir_8(): |
|
165 | def test_get_ipython_dir_8(): | |
166 | """test_get_ipython_dir_8, test / home directory""" |
|
166 | """test_get_ipython_dir_8, test / home directory""" | |
167 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ |
|
167 | with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ | |
168 | patch.object(paths, 'get_xdg_dir', return_value=None), \ |
|
168 | patch.object(paths, 'get_xdg_dir', return_value=None), \ | |
169 | modified_env({ |
|
169 | modified_env({ | |
170 | 'IPYTHON_DIR': None, |
|
170 | 'IPYTHON_DIR': None, | |
171 | 'IPYTHONDIR': None, |
|
171 | 'IPYTHONDIR': None, | |
172 | 'HOME': '/', |
|
172 | 'HOME': '/', | |
173 | }): |
|
173 | }): | |
174 | nt.assert_equal(paths.get_ipython_dir(), '/.ipython') |
|
174 | nt.assert_equal(paths.get_ipython_dir(), '/.ipython') | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | def test_get_ipython_cache_dir(): |
|
177 | def test_get_ipython_cache_dir(): | |
178 | with modified_env({'HOME': HOME_TEST_DIR}): |
|
178 | with modified_env({'HOME': HOME_TEST_DIR}): | |
179 | if os.name == 'posix' and sys.platform != 'darwin': |
|
179 | if os.name == 'posix' and sys.platform != 'darwin': | |
180 | # test default |
|
180 | # test default | |
181 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) |
|
181 | os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) | |
182 | with modified_env({'XDG_CACHE_HOME': None}): |
|
182 | with modified_env({'XDG_CACHE_HOME': None}): | |
183 | ipdir = paths.get_ipython_cache_dir() |
|
183 | ipdir = paths.get_ipython_cache_dir() | |
184 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), |
|
184 | nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), | |
185 | ipdir) |
|
185 | ipdir) | |
186 | assert_isdir(ipdir) |
|
186 | assert_isdir(ipdir) | |
187 |
|
187 | |||
188 | # test env override |
|
188 | # test env override | |
189 | with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): |
|
189 | with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): | |
190 | ipdir = paths.get_ipython_cache_dir() |
|
190 | ipdir = paths.get_ipython_cache_dir() | |
191 | assert_isdir(ipdir) |
|
191 | assert_isdir(ipdir) | |
192 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) |
|
192 | nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) | |
193 | else: |
|
193 | else: | |
194 | nt.assert_equal(paths.get_ipython_cache_dir(), |
|
194 | nt.assert_equal(paths.get_ipython_cache_dir(), | |
195 | paths.get_ipython_dir()) |
|
195 | paths.get_ipython_dir()) | |
196 |
|
196 | |||
197 | def test_get_ipython_package_dir(): |
|
197 | def test_get_ipython_package_dir(): | |
198 | ipdir = paths.get_ipython_package_dir() |
|
198 | ipdir = paths.get_ipython_package_dir() | |
199 | assert_isdir(ipdir) |
|
199 | assert_isdir(ipdir) | |
200 |
|
200 | |||
201 |
|
201 | |||
202 | def test_get_ipython_module_path(): |
|
202 | def test_get_ipython_module_path(): | |
203 | ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') |
|
203 | ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') | |
204 | assert_isfile(ipapp_path) |
|
204 | assert_isfile(ipapp_path) |
@@ -1,361 +1,343 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Provides a reload() function that acts recursively. |
|
3 | Provides a reload() function that acts recursively. | |
4 |
|
4 | |||
5 | Python's normal :func:`python:reload` function only reloads the module that it's |
|
5 | Python's normal :func:`python:reload` function only reloads the module that it's | |
6 | passed. The :func:`reload` function in this module also reloads everything |
|
6 | passed. The :func:`reload` function in this module also reloads everything | |
7 | imported from that module, which is useful when you're changing files deep |
|
7 | imported from that module, which is useful when you're changing files deep | |
8 | inside a package. |
|
8 | inside a package. | |
9 |
|
9 | |||
10 | To use this as your default reload function, type this for Python 2:: |
|
10 | To use this as your default reload function, type this for Python 2:: | |
11 |
|
11 | |||
12 | import __builtin__ |
|
12 | import __builtin__ | |
13 | from IPython.lib import deepreload |
|
13 | from IPython.lib import deepreload | |
14 | __builtin__.reload = deepreload.reload |
|
14 | __builtin__.reload = deepreload.reload | |
15 |
|
15 | |||
16 | Or this for Python 3:: |
|
16 | Or this for Python 3:: | |
17 |
|
17 | |||
18 | import builtins |
|
18 | import builtins | |
19 | from IPython.lib import deepreload |
|
19 | from IPython.lib import deepreload | |
20 | builtins.reload = deepreload.reload |
|
20 | builtins.reload = deepreload.reload | |
21 |
|
21 | |||
22 | A reference to the original :func:`python:reload` is stored in this module as |
|
22 | A reference to the original :func:`python:reload` is stored in this module as | |
23 | :data:`original_reload`, so you can restore it later. |
|
23 | :data:`original_reload`, so you can restore it later. | |
24 |
|
24 | |||
25 | This code is almost entirely based on knee.py, which is a Python |
|
25 | This code is almost entirely based on knee.py, which is a Python | |
26 | re-implementation of hierarchical module import. |
|
26 | re-implementation of hierarchical module import. | |
27 | """ |
|
27 | """ | |
28 | from __future__ import print_function |
|
28 | from __future__ import print_function | |
29 | #***************************************************************************** |
|
29 | #***************************************************************************** | |
30 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
30 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
31 | # |
|
31 | # | |
32 | # Distributed under the terms of the BSD License. The full license is in |
|
32 | # Distributed under the terms of the BSD License. The full license is in | |
33 | # the file COPYING, distributed as part of this software. |
|
33 | # the file COPYING, distributed as part of this software. | |
34 | #***************************************************************************** |
|
34 | #***************************************************************************** | |
35 |
|
35 | |||
36 | from contextlib import contextmanager |
|
36 | from contextlib import contextmanager | |
37 | import imp |
|
37 | import imp | |
38 | import sys |
|
38 | import sys | |
39 |
|
39 | |||
40 | from types import ModuleType |
|
40 | from types import ModuleType | |
41 | from warnings import warn |
|
41 | from warnings import warn | |
42 |
|
42 | |||
43 | from IPython.utils.py3compat import builtin_mod, builtin_mod_name |
|
43 | from IPython.utils.py3compat import builtin_mod, builtin_mod_name | |
44 |
|
44 | |||
45 | original_import = builtin_mod.__import__ |
|
45 | original_import = builtin_mod.__import__ | |
46 |
|
46 | |||
47 | @contextmanager |
|
47 | @contextmanager | |
48 | def replace_import_hook(new_import): |
|
48 | def replace_import_hook(new_import): | |
49 | saved_import = builtin_mod.__import__ |
|
49 | saved_import = builtin_mod.__import__ | |
50 | builtin_mod.__import__ = new_import |
|
50 | builtin_mod.__import__ = new_import | |
51 | try: |
|
51 | try: | |
52 | yield |
|
52 | yield | |
53 | finally: |
|
53 | finally: | |
54 | builtin_mod.__import__ = saved_import |
|
54 | builtin_mod.__import__ = saved_import | |
55 |
|
55 | |||
56 | def get_parent(globals, level): |
|
56 | def get_parent(globals, level): | |
57 | """ |
|
57 | """ | |
58 | parent, name = get_parent(globals, level) |
|
58 | parent, name = get_parent(globals, level) | |
59 |
|
59 | |||
60 | Return the package that an import is being performed in. If globals comes |
|
60 | Return the package that an import is being performed in. If globals comes | |
61 | from the module foo.bar.bat (not itself a package), this returns the |
|
61 | from the module foo.bar.bat (not itself a package), this returns the | |
62 | sys.modules entry for foo.bar. If globals is from a package's __init__.py, |
|
62 | sys.modules entry for foo.bar. If globals is from a package's __init__.py, | |
63 | the package's entry in sys.modules is returned. |
|
63 | the package's entry in sys.modules is returned. | |
64 |
|
64 | |||
65 | If globals doesn't come from a package or a module in a package, or a |
|
65 | If globals doesn't come from a package or a module in a package, or a | |
66 | corresponding entry is not found in sys.modules, None is returned. |
|
66 | corresponding entry is not found in sys.modules, None is returned. | |
67 | """ |
|
67 | """ | |
68 | orig_level = level |
|
68 | orig_level = level | |
69 |
|
69 | |||
70 | if not level or not isinstance(globals, dict): |
|
70 | if not level or not isinstance(globals, dict): | |
71 | return None, '' |
|
71 | return None, '' | |
72 |
|
72 | |||
73 | pkgname = globals.get('__package__', None) |
|
73 | pkgname = globals.get('__package__', None) | |
74 |
|
74 | |||
75 | if pkgname is not None: |
|
75 | if pkgname is not None: | |
76 | # __package__ is set, so use it |
|
76 | # __package__ is set, so use it | |
77 | if not hasattr(pkgname, 'rindex'): |
|
77 | if not hasattr(pkgname, 'rindex'): | |
78 | raise ValueError('__package__ set to non-string') |
|
78 | raise ValueError('__package__ set to non-string') | |
79 | if len(pkgname) == 0: |
|
79 | if len(pkgname) == 0: | |
80 | if level > 0: |
|
80 | if level > 0: | |
81 | raise ValueError('Attempted relative import in non-package') |
|
81 | raise ValueError('Attempted relative import in non-package') | |
82 | return None, '' |
|
82 | return None, '' | |
83 | name = pkgname |
|
83 | name = pkgname | |
84 | else: |
|
84 | else: | |
85 | # __package__ not set, so figure it out and set it |
|
85 | # __package__ not set, so figure it out and set it | |
86 | if '__name__' not in globals: |
|
86 | if '__name__' not in globals: | |
87 | return None, '' |
|
87 | return None, '' | |
88 | modname = globals['__name__'] |
|
88 | modname = globals['__name__'] | |
89 |
|
89 | |||
90 | if '__path__' in globals: |
|
90 | if '__path__' in globals: | |
91 | # __path__ is set, so modname is already the package name |
|
91 | # __path__ is set, so modname is already the package name | |
92 | globals['__package__'] = name = modname |
|
92 | globals['__package__'] = name = modname | |
93 | else: |
|
93 | else: | |
94 | # Normal module, so work out the package name if any |
|
94 | # Normal module, so work out the package name if any | |
95 | lastdot = modname.rfind('.') |
|
95 | lastdot = modname.rfind('.') | |
96 | if lastdot < 0 < level: |
|
96 | if lastdot < 0 < level: | |
97 | raise ValueError("Attempted relative import in non-package") |
|
97 | raise ValueError("Attempted relative import in non-package") | |
98 | if lastdot < 0: |
|
98 | if lastdot < 0: | |
99 | globals['__package__'] = None |
|
99 | globals['__package__'] = None | |
100 | return None, '' |
|
100 | return None, '' | |
101 | globals['__package__'] = name = modname[:lastdot] |
|
101 | globals['__package__'] = name = modname[:lastdot] | |
102 |
|
102 | |||
103 | dot = len(name) |
|
103 | dot = len(name) | |
104 | for x in range(level, 1, -1): |
|
104 | for x in range(level, 1, -1): | |
105 | try: |
|
105 | try: | |
106 | dot = name.rindex('.', 0, dot) |
|
106 | dot = name.rindex('.', 0, dot) | |
107 | except ValueError: |
|
107 | except ValueError: | |
108 | raise ValueError("attempted relative import beyond top-level " |
|
108 | raise ValueError("attempted relative import beyond top-level " | |
109 | "package") |
|
109 | "package") | |
110 | name = name[:dot] |
|
110 | name = name[:dot] | |
111 |
|
111 | |||
112 | try: |
|
112 | try: | |
113 | parent = sys.modules[name] |
|
113 | parent = sys.modules[name] | |
114 | except: |
|
114 | except: | |
115 | if orig_level < 1: |
|
115 | if orig_level < 1: | |
116 | warn("Parent module '%.200s' not found while handling absolute " |
|
116 | warn("Parent module '%.200s' not found while handling absolute " | |
117 | "import" % name) |
|
117 | "import" % name) | |
118 | parent = None |
|
118 | parent = None | |
119 | else: |
|
119 | else: | |
120 | raise SystemError("Parent module '%.200s' not loaded, cannot " |
|
120 | raise SystemError("Parent module '%.200s' not loaded, cannot " | |
121 | "perform relative import" % name) |
|
121 | "perform relative import" % name) | |
122 |
|
122 | |||
123 | # We expect, but can't guarantee, if parent != None, that: |
|
123 | # We expect, but can't guarantee, if parent != None, that: | |
124 | # - parent.__name__ == name |
|
124 | # - parent.__name__ == name | |
125 | # - parent.__dict__ is globals |
|
125 | # - parent.__dict__ is globals | |
126 | # If this is violated... Who cares? |
|
126 | # If this is violated... Who cares? | |
127 | return parent, name |
|
127 | return parent, name | |
128 |
|
128 | |||
129 | def load_next(mod, altmod, name, buf): |
|
129 | def load_next(mod, altmod, name, buf): | |
130 | """ |
|
130 | """ | |
131 | mod, name, buf = load_next(mod, altmod, name, buf) |
|
131 | mod, name, buf = load_next(mod, altmod, name, buf) | |
132 |
|
132 | |||
133 | altmod is either None or same as mod |
|
133 | altmod is either None or same as mod | |
134 | """ |
|
134 | """ | |
135 |
|
135 | |||
136 | if len(name) == 0: |
|
136 | if len(name) == 0: | |
137 | # completely empty module name should only happen in |
|
137 | # completely empty module name should only happen in | |
138 | # 'from . import' (or '__import__("")') |
|
138 | # 'from . import' (or '__import__("")') | |
139 | return mod, None, buf |
|
139 | return mod, None, buf | |
140 |
|
140 | |||
141 | dot = name.find('.') |
|
141 | dot = name.find('.') | |
142 | if dot == 0: |
|
142 | if dot == 0: | |
143 | raise ValueError('Empty module name') |
|
143 | raise ValueError('Empty module name') | |
144 |
|
144 | |||
145 | if dot < 0: |
|
145 | if dot < 0: | |
146 | subname = name |
|
146 | subname = name | |
147 | next = None |
|
147 | next = None | |
148 | else: |
|
148 | else: | |
149 | subname = name[:dot] |
|
149 | subname = name[:dot] | |
150 | next = name[dot+1:] |
|
150 | next = name[dot+1:] | |
151 |
|
151 | |||
152 | if buf != '': |
|
152 | if buf != '': | |
153 | buf += '.' |
|
153 | buf += '.' | |
154 | buf += subname |
|
154 | buf += subname | |
155 |
|
155 | |||
156 | result = import_submodule(mod, subname, buf) |
|
156 | result = import_submodule(mod, subname, buf) | |
157 | if result is None and mod != altmod: |
|
157 | if result is None and mod != altmod: | |
158 | result = import_submodule(altmod, subname, subname) |
|
158 | result = import_submodule(altmod, subname, subname) | |
159 | if result is not None: |
|
159 | if result is not None: | |
160 | buf = subname |
|
160 | buf = subname | |
161 |
|
161 | |||
162 | if result is None: |
|
162 | if result is None: | |
163 | raise ImportError("No module named %.200s" % name) |
|
163 | raise ImportError("No module named %.200s" % name) | |
164 |
|
164 | |||
165 | return result, next, buf |
|
165 | return result, next, buf | |
166 |
|
166 | |||
167 | # Need to keep track of what we've already reloaded to prevent cyclic evil |
|
167 | # Need to keep track of what we've already reloaded to prevent cyclic evil | |
168 | found_now = {} |
|
168 | found_now = {} | |
169 |
|
169 | |||
170 | def import_submodule(mod, subname, fullname): |
|
170 | def import_submodule(mod, subname, fullname): | |
171 | """m = import_submodule(mod, subname, fullname)""" |
|
171 | """m = import_submodule(mod, subname, fullname)""" | |
172 | # Require: |
|
172 | # Require: | |
173 | # if mod == None: subname == fullname |
|
173 | # if mod == None: subname == fullname | |
174 | # else: mod.__name__ + "." + subname == fullname |
|
174 | # else: mod.__name__ + "." + subname == fullname | |
175 |
|
175 | |||
176 | global found_now |
|
176 | global found_now | |
177 | if fullname in found_now and fullname in sys.modules: |
|
177 | if fullname in found_now and fullname in sys.modules: | |
178 | m = sys.modules[fullname] |
|
178 | m = sys.modules[fullname] | |
179 | else: |
|
179 | else: | |
180 | print('Reloading', fullname) |
|
180 | print('Reloading', fullname) | |
181 | found_now[fullname] = 1 |
|
181 | found_now[fullname] = 1 | |
182 | oldm = sys.modules.get(fullname, None) |
|
182 | oldm = sys.modules.get(fullname, None) | |
183 |
|
183 | |||
184 | if mod is None: |
|
184 | if mod is None: | |
185 | path = None |
|
185 | path = None | |
186 | elif hasattr(mod, '__path__'): |
|
186 | elif hasattr(mod, '__path__'): | |
187 | path = mod.__path__ |
|
187 | path = mod.__path__ | |
188 | else: |
|
188 | else: | |
189 | return None |
|
189 | return None | |
190 |
|
190 | |||
191 | try: |
|
191 | try: | |
192 | # This appears to be necessary on Python 3, because imp.find_module() |
|
192 | # This appears to be necessary on Python 3, because imp.find_module() | |
193 | # tries to import standard libraries (like io) itself, and we don't |
|
193 | # tries to import standard libraries (like io) itself, and we don't | |
194 | # want them to be processed by our deep_import_hook. |
|
194 | # want them to be processed by our deep_import_hook. | |
195 | with replace_import_hook(original_import): |
|
195 | with replace_import_hook(original_import): | |
196 | fp, filename, stuff = imp.find_module(subname, path) |
|
196 | fp, filename, stuff = imp.find_module(subname, path) | |
197 | except ImportError: |
|
197 | except ImportError: | |
198 | return None |
|
198 | return None | |
199 |
|
199 | |||
200 | try: |
|
200 | try: | |
201 | m = imp.load_module(fullname, fp, filename, stuff) |
|
201 | m = imp.load_module(fullname, fp, filename, stuff) | |
202 | except: |
|
202 | except: | |
203 | # load_module probably removed name from modules because of |
|
203 | # load_module probably removed name from modules because of | |
204 | # the error. Put back the original module object. |
|
204 | # the error. Put back the original module object. | |
205 | if oldm: |
|
205 | if oldm: | |
206 | sys.modules[fullname] = oldm |
|
206 | sys.modules[fullname] = oldm | |
207 | raise |
|
207 | raise | |
208 | finally: |
|
208 | finally: | |
209 | if fp: fp.close() |
|
209 | if fp: fp.close() | |
210 |
|
210 | |||
211 | add_submodule(mod, m, fullname, subname) |
|
211 | add_submodule(mod, m, fullname, subname) | |
212 |
|
212 | |||
213 | return m |
|
213 | return m | |
214 |
|
214 | |||
215 | def add_submodule(mod, submod, fullname, subname): |
|
215 | def add_submodule(mod, submod, fullname, subname): | |
216 | """mod.{subname} = submod""" |
|
216 | """mod.{subname} = submod""" | |
217 | if mod is None: |
|
217 | if mod is None: | |
218 | return #Nothing to do here. |
|
218 | return #Nothing to do here. | |
219 |
|
219 | |||
220 | if submod is None: |
|
220 | if submod is None: | |
221 | submod = sys.modules[fullname] |
|
221 | submod = sys.modules[fullname] | |
222 |
|
222 | |||
223 | setattr(mod, subname, submod) |
|
223 | setattr(mod, subname, submod) | |
224 |
|
224 | |||
225 | return |
|
225 | return | |
226 |
|
226 | |||
227 | def ensure_fromlist(mod, fromlist, buf, recursive): |
|
227 | def ensure_fromlist(mod, fromlist, buf, recursive): | |
228 | """Handle 'from module import a, b, c' imports.""" |
|
228 | """Handle 'from module import a, b, c' imports.""" | |
229 | if not hasattr(mod, '__path__'): |
|
229 | if not hasattr(mod, '__path__'): | |
230 | return |
|
230 | return | |
231 | for item in fromlist: |
|
231 | for item in fromlist: | |
232 | if not hasattr(item, 'rindex'): |
|
232 | if not hasattr(item, 'rindex'): | |
233 | raise TypeError("Item in ``from list'' not a string") |
|
233 | raise TypeError("Item in ``from list'' not a string") | |
234 | if item == '*': |
|
234 | if item == '*': | |
235 | if recursive: |
|
235 | if recursive: | |
236 | continue # avoid endless recursion |
|
236 | continue # avoid endless recursion | |
237 | try: |
|
237 | try: | |
238 | all = mod.__all__ |
|
238 | all = mod.__all__ | |
239 | except AttributeError: |
|
239 | except AttributeError: | |
240 | pass |
|
240 | pass | |
241 | else: |
|
241 | else: | |
242 | ret = ensure_fromlist(mod, all, buf, 1) |
|
242 | ret = ensure_fromlist(mod, all, buf, 1) | |
243 | if not ret: |
|
243 | if not ret: | |
244 | return 0 |
|
244 | return 0 | |
245 | elif not hasattr(mod, item): |
|
245 | elif not hasattr(mod, item): | |
246 | import_submodule(mod, item, buf + '.' + item) |
|
246 | import_submodule(mod, item, buf + '.' + item) | |
247 |
|
247 | |||
248 | def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1): |
|
248 | def deep_import_hook(name, globals=None, locals=None, fromlist=None, level=-1): | |
249 | """Replacement for __import__()""" |
|
249 | """Replacement for __import__()""" | |
250 | parent, buf = get_parent(globals, level) |
|
250 | parent, buf = get_parent(globals, level) | |
251 |
|
251 | |||
252 | head, name, buf = load_next(parent, None if level < 0 else parent, name, buf) |
|
252 | head, name, buf = load_next(parent, None if level < 0 else parent, name, buf) | |
253 |
|
253 | |||
254 | tail = head |
|
254 | tail = head | |
255 | while name: |
|
255 | while name: | |
256 | tail, name, buf = load_next(tail, tail, name, buf) |
|
256 | tail, name, buf = load_next(tail, tail, name, buf) | |
257 |
|
257 | |||
258 | # If tail is None, both get_parent and load_next found |
|
258 | # If tail is None, both get_parent and load_next found | |
259 | # an empty module name: someone called __import__("") or |
|
259 | # an empty module name: someone called __import__("") or | |
260 | # doctored faulty bytecode |
|
260 | # doctored faulty bytecode | |
261 | if tail is None: |
|
261 | if tail is None: | |
262 | raise ValueError('Empty module name') |
|
262 | raise ValueError('Empty module name') | |
263 |
|
263 | |||
264 | if not fromlist: |
|
264 | if not fromlist: | |
265 | return head |
|
265 | return head | |
266 |
|
266 | |||
267 | ensure_fromlist(tail, fromlist, buf, 0) |
|
267 | ensure_fromlist(tail, fromlist, buf, 0) | |
268 | return tail |
|
268 | return tail | |
269 |
|
269 | |||
270 | modules_reloading = {} |
|
270 | modules_reloading = {} | |
271 |
|
271 | |||
272 | def deep_reload_hook(m): |
|
272 | def deep_reload_hook(m): | |
273 | """Replacement for reload().""" |
|
273 | """Replacement for reload().""" | |
274 | if not isinstance(m, ModuleType): |
|
274 | if not isinstance(m, ModuleType): | |
275 | raise TypeError("reload() argument must be module") |
|
275 | raise TypeError("reload() argument must be module") | |
276 |
|
276 | |||
277 | name = m.__name__ |
|
277 | name = m.__name__ | |
278 |
|
278 | |||
279 | if name not in sys.modules: |
|
279 | if name not in sys.modules: | |
280 | raise ImportError("reload(): module %.200s not in sys.modules" % name) |
|
280 | raise ImportError("reload(): module %.200s not in sys.modules" % name) | |
281 |
|
281 | |||
282 | global modules_reloading |
|
282 | global modules_reloading | |
283 | try: |
|
283 | try: | |
284 | return modules_reloading[name] |
|
284 | return modules_reloading[name] | |
285 | except: |
|
285 | except: | |
286 | modules_reloading[name] = m |
|
286 | modules_reloading[name] = m | |
287 |
|
287 | |||
288 | dot = name.rfind('.') |
|
288 | dot = name.rfind('.') | |
289 | if dot < 0: |
|
289 | if dot < 0: | |
290 | subname = name |
|
290 | subname = name | |
291 | path = None |
|
291 | path = None | |
292 | else: |
|
292 | else: | |
293 | try: |
|
293 | try: | |
294 | parent = sys.modules[name[:dot]] |
|
294 | parent = sys.modules[name[:dot]] | |
295 | except KeyError: |
|
295 | except KeyError: | |
296 | modules_reloading.clear() |
|
296 | modules_reloading.clear() | |
297 | raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot]) |
|
297 | raise ImportError("reload(): parent %.200s not in sys.modules" % name[:dot]) | |
298 | subname = name[dot+1:] |
|
298 | subname = name[dot+1:] | |
299 | path = getattr(parent, "__path__", None) |
|
299 | path = getattr(parent, "__path__", None) | |
300 |
|
300 | |||
301 | try: |
|
301 | try: | |
302 | # This appears to be necessary on Python 3, because imp.find_module() |
|
302 | # This appears to be necessary on Python 3, because imp.find_module() | |
303 | # tries to import standard libraries (like io) itself, and we don't |
|
303 | # tries to import standard libraries (like io) itself, and we don't | |
304 | # want them to be processed by our deep_import_hook. |
|
304 | # want them to be processed by our deep_import_hook. | |
305 | with replace_import_hook(original_import): |
|
305 | with replace_import_hook(original_import): | |
306 | fp, filename, stuff = imp.find_module(subname, path) |
|
306 | fp, filename, stuff = imp.find_module(subname, path) | |
307 | finally: |
|
307 | finally: | |
308 | modules_reloading.clear() |
|
308 | modules_reloading.clear() | |
309 |
|
309 | |||
310 | try: |
|
310 | try: | |
311 | newm = imp.load_module(name, fp, filename, stuff) |
|
311 | newm = imp.load_module(name, fp, filename, stuff) | |
312 | except: |
|
312 | except: | |
313 | # load_module probably removed name from modules because of |
|
313 | # load_module probably removed name from modules because of | |
314 | # the error. Put back the original module object. |
|
314 | # the error. Put back the original module object. | |
315 | sys.modules[name] = m |
|
315 | sys.modules[name] = m | |
316 | raise |
|
316 | raise | |
317 | finally: |
|
317 | finally: | |
318 | if fp: fp.close() |
|
318 | if fp: fp.close() | |
319 |
|
319 | |||
320 | modules_reloading.clear() |
|
320 | modules_reloading.clear() | |
321 | return newm |
|
321 | return newm | |
322 |
|
322 | |||
323 | # Save the original hooks |
|
323 | # Save the original hooks | |
324 | try: |
|
324 | try: | |
325 | original_reload = builtin_mod.reload |
|
325 | original_reload = builtin_mod.reload | |
326 | except AttributeError: |
|
326 | except AttributeError: | |
327 | original_reload = imp.reload # Python 3 |
|
327 | original_reload = imp.reload # Python 3 | |
328 |
|
328 | |||
329 | # Replacement for reload() |
|
329 | # Replacement for reload() | |
330 | def reload(module, exclude=('sys', 'os.path', builtin_mod_name, '__main__')): |
|
330 | def reload(module, exclude=('sys', 'os.path', builtin_mod_name, '__main__')): | |
331 | """Recursively reload all modules used in the given module. Optionally |
|
331 | """Recursively reload all modules used in the given module. Optionally | |
332 | takes a list of modules to exclude from reloading. The default exclude |
|
332 | takes a list of modules to exclude from reloading. The default exclude | |
333 | list contains sys, __main__, and __builtin__, to prevent, e.g., resetting |
|
333 | list contains sys, __main__, and __builtin__, to prevent, e.g., resetting | |
334 | display, exception, and io hooks. |
|
334 | display, exception, and io hooks. | |
335 | """ |
|
335 | """ | |
336 | global found_now |
|
336 | global found_now | |
337 | for i in exclude: |
|
337 | for i in exclude: | |
338 | found_now[i] = 1 |
|
338 | found_now[i] = 1 | |
339 | try: |
|
339 | try: | |
340 | with replace_import_hook(deep_import_hook): |
|
340 | with replace_import_hook(deep_import_hook): | |
341 | return deep_reload_hook(module) |
|
341 | return deep_reload_hook(module) | |
342 | finally: |
|
342 | finally: | |
343 | found_now = {} |
|
343 | found_now = {} | |
344 |
|
||||
345 |
|
||||
346 | def _dreload(module, **kwargs): |
|
|||
347 | """ |
|
|||
348 | **deprecated** |
|
|||
349 |
|
||||
350 | import reload explicitly from `IPython.lib.deepreload` to use it |
|
|||
351 |
|
||||
352 | """ |
|
|||
353 | # this was marked as deprecated and for 5.0 removal, but |
|
|||
354 | # IPython.core_builtin_trap have a Deprecation warning for 6.0, so cannot |
|
|||
355 | # remove that now. |
|
|||
356 | warn(""" |
|
|||
357 | injecting `dreload` in interactive namespace is deprecated since IPython 4.0. |
|
|||
358 | Please import `reload` explicitly from `IPython.lib.deepreload`. |
|
|||
359 | """, DeprecationWarning, stacklevel=2) |
|
|||
360 | reload(module, **kwargs) |
|
|||
361 |
|
@@ -1,488 +1,493 b'' | |||||
1 | """IPython terminal interface using prompt_toolkit""" |
|
1 | """IPython terminal interface using prompt_toolkit""" | |
2 | from __future__ import print_function |
|
2 | from __future__ import print_function | |
3 |
|
3 | |||
4 | import os |
|
4 | import os | |
5 | import sys |
|
5 | import sys | |
6 | import warnings |
|
6 | import warnings | |
7 | from warnings import warn |
|
7 | from warnings import warn | |
8 |
|
8 | |||
9 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC |
|
9 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC | |
10 | from IPython.utils import io |
|
10 | from IPython.utils import io | |
11 | from IPython.utils.py3compat import PY3, cast_unicode_py2, input |
|
11 | from IPython.utils.py3compat import PY3, cast_unicode_py2, input, string_types | |
12 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
12 | from IPython.utils.terminal import toggle_set_term_title, set_term_title | |
13 | from IPython.utils.process import abbrev_cwd |
|
13 | from IPython.utils.process import abbrev_cwd | |
14 | from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum |
|
14 | from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union | |
15 |
|
15 | |||
16 | from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
|
16 | from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode | |
17 | from prompt_toolkit.filters import (HasFocus, Condition, IsDone) |
|
17 | from prompt_toolkit.filters import (HasFocus, Condition, IsDone) | |
18 | from prompt_toolkit.history import InMemoryHistory |
|
18 | from prompt_toolkit.history import InMemoryHistory | |
19 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output |
|
19 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout, create_output | |
20 | from prompt_toolkit.interface import CommandLineInterface |
|
20 | from prompt_toolkit.interface import CommandLineInterface | |
21 | from prompt_toolkit.key_binding.manager import KeyBindingManager |
|
21 | from prompt_toolkit.key_binding.manager import KeyBindingManager | |
22 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor |
|
22 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor | |
23 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle |
|
23 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle | |
24 |
|
24 | |||
25 | from pygments.styles import get_style_by_name, get_all_styles |
|
25 | from pygments.styles import get_style_by_name, get_all_styles | |
|
26 | from pygments.style import Style | |||
26 | from pygments.token import Token |
|
27 | from pygments.token import Token | |
27 |
|
28 | |||
28 | from .debugger import TerminalPdb, Pdb |
|
29 | from .debugger import TerminalPdb, Pdb | |
29 | from .magics import TerminalMagics |
|
30 | from .magics import TerminalMagics | |
30 | from .pt_inputhooks import get_inputhook_func |
|
31 | from .pt_inputhooks import get_inputhook_func | |
31 | from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook |
|
32 | from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook | |
32 | from .ptutils import IPythonPTCompleter, IPythonPTLexer |
|
33 | from .ptutils import IPythonPTCompleter, IPythonPTLexer | |
33 | from .shortcuts import register_ipython_shortcuts |
|
34 | from .shortcuts import register_ipython_shortcuts | |
34 |
|
35 | |||
35 | DISPLAY_BANNER_DEPRECATED = object() |
|
36 | DISPLAY_BANNER_DEPRECATED = object() | |
36 |
|
37 | |||
37 |
|
38 | |||
38 | from pygments.style import Style |
|
39 | from pygments.style import Style | |
39 |
|
40 | |||
40 | class _NoStyle(Style): pass |
|
41 | class _NoStyle(Style): pass | |
41 |
|
42 | |||
42 |
|
43 | |||
43 |
|
44 | |||
44 | _style_overrides_light_bg = { |
|
45 | _style_overrides_light_bg = { | |
45 | Token.Prompt: '#0000ff', |
|
46 | Token.Prompt: '#0000ff', | |
46 | Token.PromptNum: '#0000ee bold', |
|
47 | Token.PromptNum: '#0000ee bold', | |
47 | Token.OutPrompt: '#cc0000', |
|
48 | Token.OutPrompt: '#cc0000', | |
48 | Token.OutPromptNum: '#bb0000 bold', |
|
49 | Token.OutPromptNum: '#bb0000 bold', | |
49 | } |
|
50 | } | |
50 |
|
51 | |||
51 | _style_overrides_linux = { |
|
52 | _style_overrides_linux = { | |
52 | Token.Prompt: '#00cc00', |
|
53 | Token.Prompt: '#00cc00', | |
53 | Token.PromptNum: '#00bb00 bold', |
|
54 | Token.PromptNum: '#00bb00 bold', | |
54 | Token.OutPrompt: '#cc0000', |
|
55 | Token.OutPrompt: '#cc0000', | |
55 | Token.OutPromptNum: '#bb0000 bold', |
|
56 | Token.OutPromptNum: '#bb0000 bold', | |
56 | } |
|
57 | } | |
57 |
|
58 | |||
58 |
|
59 | |||
59 |
|
60 | |||
60 | def get_default_editor(): |
|
61 | def get_default_editor(): | |
61 | try: |
|
62 | try: | |
62 | ed = os.environ['EDITOR'] |
|
63 | ed = os.environ['EDITOR'] | |
63 | if not PY3: |
|
64 | if not PY3: | |
64 | ed = ed.decode() |
|
65 | ed = ed.decode() | |
65 | return ed |
|
66 | return ed | |
66 | except KeyError: |
|
67 | except KeyError: | |
67 | pass |
|
68 | pass | |
68 | except UnicodeError: |
|
69 | except UnicodeError: | |
69 | warn("$EDITOR environment variable is not pure ASCII. Using platform " |
|
70 | warn("$EDITOR environment variable is not pure ASCII. Using platform " | |
70 | "default editor.") |
|
71 | "default editor.") | |
71 |
|
72 | |||
72 | if os.name == 'posix': |
|
73 | if os.name == 'posix': | |
73 | return 'vi' # the only one guaranteed to be there! |
|
74 | return 'vi' # the only one guaranteed to be there! | |
74 | else: |
|
75 | else: | |
75 | return 'notepad' # same in Windows! |
|
76 | return 'notepad' # same in Windows! | |
76 |
|
77 | |||
77 | # conservatively check for tty |
|
78 | # conservatively check for tty | |
78 | # overridden streams can result in things like: |
|
79 | # overridden streams can result in things like: | |
79 | # - sys.stdin = None |
|
80 | # - sys.stdin = None | |
80 | # - no isatty method |
|
81 | # - no isatty method | |
81 | for _name in ('stdin', 'stdout', 'stderr'): |
|
82 | for _name in ('stdin', 'stdout', 'stderr'): | |
82 | _stream = getattr(sys, _name) |
|
83 | _stream = getattr(sys, _name) | |
83 | if not _stream or not hasattr(_stream, 'isatty') or not _stream.isatty(): |
|
84 | if not _stream or not hasattr(_stream, 'isatty') or not _stream.isatty(): | |
84 | _is_tty = False |
|
85 | _is_tty = False | |
85 | break |
|
86 | break | |
86 | else: |
|
87 | else: | |
87 | _is_tty = True |
|
88 | _is_tty = True | |
88 |
|
89 | |||
89 |
|
90 | |||
90 | _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty) |
|
91 | _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty) | |
91 |
|
92 | |||
92 | class TerminalInteractiveShell(InteractiveShell): |
|
93 | class TerminalInteractiveShell(InteractiveShell): | |
93 | space_for_menu = Integer(6, help='Number of line at the bottom of the screen ' |
|
94 | space_for_menu = Integer(6, help='Number of line at the bottom of the screen ' | |
94 | 'to reserve for the completion menu' |
|
95 | 'to reserve for the completion menu' | |
95 | ).tag(config=True) |
|
96 | ).tag(config=True) | |
96 |
|
97 | |||
97 | def _space_for_menu_changed(self, old, new): |
|
98 | def _space_for_menu_changed(self, old, new): | |
98 | self._update_layout() |
|
99 | self._update_layout() | |
99 |
|
100 | |||
100 | pt_cli = None |
|
101 | pt_cli = None | |
101 | debugger_history = None |
|
102 | debugger_history = None | |
102 | _pt_app = None |
|
103 | _pt_app = None | |
103 |
|
104 | |||
104 | simple_prompt = Bool(_use_simple_prompt, |
|
105 | simple_prompt = Bool(_use_simple_prompt, | |
105 | help="""Use `raw_input` for the REPL, without completion, multiline input, and prompt colors. |
|
106 | help="""Use `raw_input` for the REPL, without completion, multiline input, and prompt colors. | |
106 |
|
107 | |||
107 | Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are: |
|
108 | Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are: | |
108 | IPython own testing machinery, and emacs inferior-shell integration through elpy. |
|
109 | IPython own testing machinery, and emacs inferior-shell integration through elpy. | |
109 |
|
110 | |||
110 | This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT` |
|
111 | This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT` | |
111 | environment variable is set, or the current terminal is not a tty. |
|
112 | environment variable is set, or the current terminal is not a tty. | |
112 |
|
113 | |||
113 | """ |
|
114 | """ | |
114 | ).tag(config=True) |
|
115 | ).tag(config=True) | |
115 |
|
116 | |||
116 | @property |
|
117 | @property | |
117 | def debugger_cls(self): |
|
118 | def debugger_cls(self): | |
118 | return Pdb if self.simple_prompt else TerminalPdb |
|
119 | return Pdb if self.simple_prompt else TerminalPdb | |
119 |
|
120 | |||
120 | confirm_exit = Bool(True, |
|
121 | confirm_exit = Bool(True, | |
121 | help=""" |
|
122 | help=""" | |
122 | Set to confirm when you try to exit IPython with an EOF (Control-D |
|
123 | Set to confirm when you try to exit IPython with an EOF (Control-D | |
123 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
124 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
124 | you can force a direct exit without any confirmation.""", |
|
125 | you can force a direct exit without any confirmation.""", | |
125 | ).tag(config=True) |
|
126 | ).tag(config=True) | |
126 |
|
127 | |||
127 | editing_mode = Unicode('emacs', |
|
128 | editing_mode = Unicode('emacs', | |
128 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", |
|
129 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", | |
129 | ).tag(config=True) |
|
130 | ).tag(config=True) | |
130 |
|
131 | |||
131 | mouse_support = Bool(False, |
|
132 | mouse_support = Bool(False, | |
132 | help="Enable mouse support in the prompt" |
|
133 | help="Enable mouse support in the prompt" | |
133 | ).tag(config=True) |
|
134 | ).tag(config=True) | |
134 |
|
135 | |||
135 | highlighting_style = Unicode('legacy', |
|
136 | highlighting_style = Union([Unicode('legacy'), Type(klass=Style)], | |
136 |
|
|
137 | help="""The name or class of a Pygments style to use for syntax | |
|
138 | highlighting: \n %s""" % ', '.join(get_all_styles()) | |||
137 | ).tag(config=True) |
|
139 | ).tag(config=True) | |
138 |
|
140 | |||
139 |
|
141 | |||
140 | @observe('highlighting_style') |
|
142 | @observe('highlighting_style') | |
141 | @observe('colors') |
|
143 | @observe('colors') | |
142 | def _highlighting_style_changed(self, change): |
|
144 | def _highlighting_style_changed(self, change): | |
143 | self.refresh_style() |
|
145 | self.refresh_style() | |
144 |
|
146 | |||
145 | def refresh_style(self): |
|
147 | def refresh_style(self): | |
146 | self._style = self._make_style_from_name(self.highlighting_style) |
|
148 | self._style = self._make_style_from_name_or_cls(self.highlighting_style) | |
147 |
|
149 | |||
148 |
|
150 | |||
149 | highlighting_style_overrides = Dict( |
|
151 | highlighting_style_overrides = Dict( | |
150 | help="Override highlighting format for specific tokens" |
|
152 | help="Override highlighting format for specific tokens" | |
151 | ).tag(config=True) |
|
153 | ).tag(config=True) | |
152 |
|
154 | |||
153 | true_color = Bool(False, |
|
155 | true_color = Bool(False, | |
154 | help=("Use 24bit colors instead of 256 colors in prompt highlighting. " |
|
156 | help=("Use 24bit colors instead of 256 colors in prompt highlighting. " | |
155 | "If your terminal supports true color, the following command " |
|
157 | "If your terminal supports true color, the following command " | |
156 | "should print 'TRUECOLOR' in orange: " |
|
158 | "should print 'TRUECOLOR' in orange: " | |
157 | "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"") |
|
159 | "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"") | |
158 | ).tag(config=True) |
|
160 | ).tag(config=True) | |
159 |
|
161 | |||
160 | editor = Unicode(get_default_editor(), |
|
162 | editor = Unicode(get_default_editor(), | |
161 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." |
|
163 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." | |
162 | ).tag(config=True) |
|
164 | ).tag(config=True) | |
163 |
|
165 | |||
164 | prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True) |
|
166 | prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True) | |
165 |
|
167 | |||
166 | prompts = Instance(Prompts) |
|
168 | prompts = Instance(Prompts) | |
167 |
|
169 | |||
168 | @default('prompts') |
|
170 | @default('prompts') | |
169 | def _prompts_default(self): |
|
171 | def _prompts_default(self): | |
170 | return self.prompts_class(self) |
|
172 | return self.prompts_class(self) | |
171 |
|
173 | |||
172 | @observe('prompts') |
|
174 | @observe('prompts') | |
173 | def _(self, change): |
|
175 | def _(self, change): | |
174 | self._update_layout() |
|
176 | self._update_layout() | |
175 |
|
177 | |||
176 | @default('displayhook_class') |
|
178 | @default('displayhook_class') | |
177 | def _displayhook_class_default(self): |
|
179 | def _displayhook_class_default(self): | |
178 | return RichPromptDisplayHook |
|
180 | return RichPromptDisplayHook | |
179 |
|
181 | |||
180 | term_title = Bool(True, |
|
182 | term_title = Bool(True, | |
181 | help="Automatically set the terminal title" |
|
183 | help="Automatically set the terminal title" | |
182 | ).tag(config=True) |
|
184 | ).tag(config=True) | |
183 |
|
185 | |||
184 | display_completions = Enum(('column', 'multicolumn','readlinelike'), |
|
186 | display_completions = Enum(('column', 'multicolumn','readlinelike'), | |
185 | help= ( "Options for displaying tab completions, 'column', 'multicolumn', and " |
|
187 | help= ( "Options for displaying tab completions, 'column', 'multicolumn', and " | |
186 | "'readlinelike'. These options are for `prompt_toolkit`, see " |
|
188 | "'readlinelike'. These options are for `prompt_toolkit`, see " | |
187 | "`prompt_toolkit` documentation for more information." |
|
189 | "`prompt_toolkit` documentation for more information." | |
188 | ), |
|
190 | ), | |
189 | default_value='multicolumn').tag(config=True) |
|
191 | default_value='multicolumn').tag(config=True) | |
190 |
|
192 | |||
191 | highlight_matching_brackets = Bool(True, |
|
193 | highlight_matching_brackets = Bool(True, | |
192 | help="Highlight matching brackets .", |
|
194 | help="Highlight matching brackets .", | |
193 | ).tag(config=True) |
|
195 | ).tag(config=True) | |
194 |
|
196 | |||
195 | @observe('term_title') |
|
197 | @observe('term_title') | |
196 | def init_term_title(self, change=None): |
|
198 | def init_term_title(self, change=None): | |
197 | # Enable or disable the terminal title. |
|
199 | # Enable or disable the terminal title. | |
198 | if self.term_title: |
|
200 | if self.term_title: | |
199 | toggle_set_term_title(True) |
|
201 | toggle_set_term_title(True) | |
200 | set_term_title('IPython: ' + abbrev_cwd()) |
|
202 | set_term_title('IPython: ' + abbrev_cwd()) | |
201 | else: |
|
203 | else: | |
202 | toggle_set_term_title(False) |
|
204 | toggle_set_term_title(False) | |
203 |
|
205 | |||
204 | def init_display_formatter(self): |
|
206 | def init_display_formatter(self): | |
205 | super(TerminalInteractiveShell, self).init_display_formatter() |
|
207 | super(TerminalInteractiveShell, self).init_display_formatter() | |
206 | # terminal only supports plain text |
|
208 | # terminal only supports plain text | |
207 | self.display_formatter.active_types = ['text/plain'] |
|
209 | self.display_formatter.active_types = ['text/plain'] | |
208 |
|
210 | |||
209 | def init_prompt_toolkit_cli(self): |
|
211 | def init_prompt_toolkit_cli(self): | |
210 | if self.simple_prompt: |
|
212 | if self.simple_prompt: | |
211 | # Fall back to plain non-interactive output for tests. |
|
213 | # Fall back to plain non-interactive output for tests. | |
212 | # This is very limited, and only accepts a single line. |
|
214 | # This is very limited, and only accepts a single line. | |
213 | def prompt(): |
|
215 | def prompt(): | |
214 | return cast_unicode_py2(input('In [%d]: ' % self.execution_count)) |
|
216 | return cast_unicode_py2(input('In [%d]: ' % self.execution_count)) | |
215 | self.prompt_for_code = prompt |
|
217 | self.prompt_for_code = prompt | |
216 | return |
|
218 | return | |
217 |
|
219 | |||
218 | # Set up keyboard shortcuts |
|
220 | # Set up keyboard shortcuts | |
219 | kbmanager = KeyBindingManager.for_prompt() |
|
221 | kbmanager = KeyBindingManager.for_prompt() | |
220 | register_ipython_shortcuts(kbmanager.registry, self) |
|
222 | register_ipython_shortcuts(kbmanager.registry, self) | |
221 |
|
223 | |||
222 | # Pre-populate history from IPython's history database |
|
224 | # Pre-populate history from IPython's history database | |
223 | history = InMemoryHistory() |
|
225 | history = InMemoryHistory() | |
224 | last_cell = u"" |
|
226 | last_cell = u"" | |
225 | for __, ___, cell in self.history_manager.get_tail(self.history_load_length, |
|
227 | for __, ___, cell in self.history_manager.get_tail(self.history_load_length, | |
226 | include_latest=True): |
|
228 | include_latest=True): | |
227 | # Ignore blank lines and consecutive duplicates |
|
229 | # Ignore blank lines and consecutive duplicates | |
228 | cell = cell.rstrip() |
|
230 | cell = cell.rstrip() | |
229 | if cell and (cell != last_cell): |
|
231 | if cell and (cell != last_cell): | |
230 | history.append(cell) |
|
232 | history.append(cell) | |
231 |
|
233 | |||
232 | self._style = self._make_style_from_name(self.highlighting_style) |
|
234 | self._style = self._make_style_from_name_or_cls(self.highlighting_style) | |
233 | style = DynamicStyle(lambda: self._style) |
|
235 | style = DynamicStyle(lambda: self._style) | |
234 |
|
236 | |||
235 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) |
|
237 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) | |
236 |
|
238 | |||
237 | self._pt_app = create_prompt_application( |
|
239 | self._pt_app = create_prompt_application( | |
238 | editing_mode=editing_mode, |
|
240 | editing_mode=editing_mode, | |
239 | key_bindings_registry=kbmanager.registry, |
|
241 | key_bindings_registry=kbmanager.registry, | |
240 | history=history, |
|
242 | history=history, | |
241 | completer=IPythonPTCompleter(shell=self), |
|
243 | completer=IPythonPTCompleter(shell=self), | |
242 | enable_history_search=True, |
|
244 | enable_history_search=True, | |
243 | style=style, |
|
245 | style=style, | |
244 | mouse_support=self.mouse_support, |
|
246 | mouse_support=self.mouse_support, | |
245 | **self._layout_options() |
|
247 | **self._layout_options() | |
246 | ) |
|
248 | ) | |
247 | self._eventloop = create_eventloop(self.inputhook) |
|
249 | self._eventloop = create_eventloop(self.inputhook) | |
248 | self.pt_cli = CommandLineInterface( |
|
250 | self.pt_cli = CommandLineInterface( | |
249 | self._pt_app, eventloop=self._eventloop, |
|
251 | self._pt_app, eventloop=self._eventloop, | |
250 | output=create_output(true_color=self.true_color)) |
|
252 | output=create_output(true_color=self.true_color)) | |
251 |
|
253 | |||
252 | def _make_style_from_name(self, name): |
|
254 | def _make_style_from_name_or_cls(self, name_or_cls): | |
253 | """ |
|
255 | """ | |
254 | Small wrapper that make an IPython compatible style from a style name |
|
256 | Small wrapper that make an IPython compatible style from a style name | |
255 |
|
257 | |||
256 | We need that to add style for prompt ... etc. |
|
258 | We need that to add style for prompt ... etc. | |
257 | """ |
|
259 | """ | |
258 | style_overrides = {} |
|
260 | style_overrides = {} | |
259 | if name == 'legacy': |
|
261 | if name_or_cls == 'legacy': | |
260 | legacy = self.colors.lower() |
|
262 | legacy = self.colors.lower() | |
261 | if legacy == 'linux': |
|
263 | if legacy == 'linux': | |
262 | style_cls = get_style_by_name('monokai') |
|
264 | style_cls = get_style_by_name('monokai') | |
263 | style_overrides = _style_overrides_linux |
|
265 | style_overrides = _style_overrides_linux | |
264 | elif legacy == 'lightbg': |
|
266 | elif legacy == 'lightbg': | |
265 | style_overrides = _style_overrides_light_bg |
|
267 | style_overrides = _style_overrides_light_bg | |
266 | style_cls = get_style_by_name('pastie') |
|
268 | style_cls = get_style_by_name('pastie') | |
267 | elif legacy == 'neutral': |
|
269 | elif legacy == 'neutral': | |
268 | # The default theme needs to be visible on both a dark background |
|
270 | # The default theme needs to be visible on both a dark background | |
269 | # and a light background, because we can't tell what the terminal |
|
271 | # and a light background, because we can't tell what the terminal | |
270 | # looks like. These tweaks to the default theme help with that. |
|
272 | # looks like. These tweaks to the default theme help with that. | |
271 | style_cls = get_style_by_name('default') |
|
273 | style_cls = get_style_by_name('default') | |
272 | style_overrides.update({ |
|
274 | style_overrides.update({ | |
273 | Token.Number: '#007700', |
|
275 | Token.Number: '#007700', | |
274 | Token.Operator: 'noinherit', |
|
276 | Token.Operator: 'noinherit', | |
275 | Token.String: '#BB6622', |
|
277 | Token.String: '#BB6622', | |
276 | Token.Name.Function: '#2080D0', |
|
278 | Token.Name.Function: '#2080D0', | |
277 | Token.Name.Class: 'bold #2080D0', |
|
279 | Token.Name.Class: 'bold #2080D0', | |
278 | Token.Name.Namespace: 'bold #2080D0', |
|
280 | Token.Name.Namespace: 'bold #2080D0', | |
279 | Token.Prompt: '#009900', |
|
281 | Token.Prompt: '#009900', | |
280 | Token.PromptNum: '#00ff00 bold', |
|
282 | Token.PromptNum: '#00ff00 bold', | |
281 | Token.OutPrompt: '#990000', |
|
283 | Token.OutPrompt: '#990000', | |
282 | Token.OutPromptNum: '#ff0000 bold', |
|
284 | Token.OutPromptNum: '#ff0000 bold', | |
283 | }) |
|
285 | }) | |
284 | elif legacy =='nocolor': |
|
286 | elif legacy =='nocolor': | |
285 | style_cls=_NoStyle |
|
287 | style_cls=_NoStyle | |
286 | style_overrides = {} |
|
288 | style_overrides = {} | |
287 | else : |
|
289 | else : | |
288 | raise ValueError('Got unknown colors: ', legacy) |
|
290 | raise ValueError('Got unknown colors: ', legacy) | |
289 | else : |
|
291 | else : | |
290 | style_cls = get_style_by_name(name) |
|
292 | if isinstance(name_or_cls, string_types): | |
|
293 | style_cls = get_style_by_name(name_or_cls) | |||
|
294 | else: | |||
|
295 | style_cls = name_or_cls | |||
291 | style_overrides = { |
|
296 | style_overrides = { | |
292 | Token.Prompt: '#009900', |
|
297 | Token.Prompt: '#009900', | |
293 | Token.PromptNum: '#00ff00 bold', |
|
298 | Token.PromptNum: '#00ff00 bold', | |
294 | Token.OutPrompt: '#990000', |
|
299 | Token.OutPrompt: '#990000', | |
295 | Token.OutPromptNum: '#ff0000 bold', |
|
300 | Token.OutPromptNum: '#ff0000 bold', | |
296 | } |
|
301 | } | |
297 | style_overrides.update(self.highlighting_style_overrides) |
|
302 | style_overrides.update(self.highlighting_style_overrides) | |
298 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, |
|
303 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, | |
299 | style_dict=style_overrides) |
|
304 | style_dict=style_overrides) | |
300 |
|
305 | |||
301 | return style |
|
306 | return style | |
302 |
|
307 | |||
303 | def _layout_options(self): |
|
308 | def _layout_options(self): | |
304 | """ |
|
309 | """ | |
305 | Return the current layout option for the current Terminal InteractiveShell |
|
310 | Return the current layout option for the current Terminal InteractiveShell | |
306 | """ |
|
311 | """ | |
307 | return { |
|
312 | return { | |
308 | 'lexer':IPythonPTLexer(), |
|
313 | 'lexer':IPythonPTLexer(), | |
309 | 'reserve_space_for_menu':self.space_for_menu, |
|
314 | 'reserve_space_for_menu':self.space_for_menu, | |
310 | 'get_prompt_tokens':self.prompts.in_prompt_tokens, |
|
315 | 'get_prompt_tokens':self.prompts.in_prompt_tokens, | |
311 | 'get_continuation_tokens':self.prompts.continuation_prompt_tokens, |
|
316 | 'get_continuation_tokens':self.prompts.continuation_prompt_tokens, | |
312 | 'multiline':True, |
|
317 | 'multiline':True, | |
313 | 'display_completions_in_columns': (self.display_completions == 'multicolumn'), |
|
318 | 'display_completions_in_columns': (self.display_completions == 'multicolumn'), | |
314 |
|
319 | |||
315 | # Highlight matching brackets, but only when this setting is |
|
320 | # Highlight matching brackets, but only when this setting is | |
316 | # enabled, and only when the DEFAULT_BUFFER has the focus. |
|
321 | # enabled, and only when the DEFAULT_BUFFER has the focus. | |
317 | 'extra_input_processors': [ConditionalProcessor( |
|
322 | 'extra_input_processors': [ConditionalProcessor( | |
318 | processor=HighlightMatchingBracketProcessor(chars='[](){}'), |
|
323 | processor=HighlightMatchingBracketProcessor(chars='[](){}'), | |
319 | filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & |
|
324 | filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & | |
320 | Condition(lambda cli: self.highlight_matching_brackets))], |
|
325 | Condition(lambda cli: self.highlight_matching_brackets))], | |
321 | } |
|
326 | } | |
322 |
|
327 | |||
323 | def _update_layout(self): |
|
328 | def _update_layout(self): | |
324 | """ |
|
329 | """ | |
325 | Ask for a re computation of the application layout, if for example , |
|
330 | Ask for a re computation of the application layout, if for example , | |
326 | some configuration options have changed. |
|
331 | some configuration options have changed. | |
327 | """ |
|
332 | """ | |
328 | if self._pt_app: |
|
333 | if self._pt_app: | |
329 | self._pt_app.layout = create_prompt_layout(**self._layout_options()) |
|
334 | self._pt_app.layout = create_prompt_layout(**self._layout_options()) | |
330 |
|
335 | |||
331 | def prompt_for_code(self): |
|
336 | def prompt_for_code(self): | |
332 | document = self.pt_cli.run( |
|
337 | document = self.pt_cli.run( | |
333 | pre_run=self.pre_prompt, reset_current_buffer=True) |
|
338 | pre_run=self.pre_prompt, reset_current_buffer=True) | |
334 | return document.text |
|
339 | return document.text | |
335 |
|
340 | |||
336 | def enable_win_unicode_console(self): |
|
341 | def enable_win_unicode_console(self): | |
337 | import win_unicode_console |
|
342 | import win_unicode_console | |
338 |
|
343 | |||
339 | if PY3: |
|
344 | if PY3: | |
340 | win_unicode_console.enable() |
|
345 | win_unicode_console.enable() | |
341 | else: |
|
346 | else: | |
342 | # https://github.com/ipython/ipython/issues/9768 |
|
347 | # https://github.com/ipython/ipython/issues/9768 | |
343 | from win_unicode_console.streams import (TextStreamWrapper, |
|
348 | from win_unicode_console.streams import (TextStreamWrapper, | |
344 | stdout_text_transcoded, stderr_text_transcoded) |
|
349 | stdout_text_transcoded, stderr_text_transcoded) | |
345 |
|
350 | |||
346 | class LenientStrStreamWrapper(TextStreamWrapper): |
|
351 | class LenientStrStreamWrapper(TextStreamWrapper): | |
347 | def write(self, s): |
|
352 | def write(self, s): | |
348 | if isinstance(s, bytes): |
|
353 | if isinstance(s, bytes): | |
349 | s = s.decode(self.encoding, 'replace') |
|
354 | s = s.decode(self.encoding, 'replace') | |
350 |
|
355 | |||
351 | self.base.write(s) |
|
356 | self.base.write(s) | |
352 |
|
357 | |||
353 | stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded) |
|
358 | stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded) | |
354 | stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded) |
|
359 | stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded) | |
355 |
|
360 | |||
356 | win_unicode_console.enable(stdout=stdout_text_str, |
|
361 | win_unicode_console.enable(stdout=stdout_text_str, | |
357 | stderr=stderr_text_str) |
|
362 | stderr=stderr_text_str) | |
358 |
|
363 | |||
359 | def init_io(self): |
|
364 | def init_io(self): | |
360 | if sys.platform not in {'win32', 'cli'}: |
|
365 | if sys.platform not in {'win32', 'cli'}: | |
361 | return |
|
366 | return | |
362 |
|
367 | |||
363 | self.enable_win_unicode_console() |
|
368 | self.enable_win_unicode_console() | |
364 |
|
369 | |||
365 | import colorama |
|
370 | import colorama | |
366 | colorama.init() |
|
371 | colorama.init() | |
367 |
|
372 | |||
368 | # For some reason we make these wrappers around stdout/stderr. |
|
373 | # For some reason we make these wrappers around stdout/stderr. | |
369 | # For now, we need to reset them so all output gets coloured. |
|
374 | # For now, we need to reset them so all output gets coloured. | |
370 | # https://github.com/ipython/ipython/issues/8669 |
|
375 | # https://github.com/ipython/ipython/issues/8669 | |
371 | # io.std* are deprecated, but don't show our own deprecation warnings |
|
376 | # io.std* are deprecated, but don't show our own deprecation warnings | |
372 | # during initialization of the deprecated API. |
|
377 | # during initialization of the deprecated API. | |
373 | with warnings.catch_warnings(): |
|
378 | with warnings.catch_warnings(): | |
374 | warnings.simplefilter('ignore', DeprecationWarning) |
|
379 | warnings.simplefilter('ignore', DeprecationWarning) | |
375 | io.stdout = io.IOStream(sys.stdout) |
|
380 | io.stdout = io.IOStream(sys.stdout) | |
376 | io.stderr = io.IOStream(sys.stderr) |
|
381 | io.stderr = io.IOStream(sys.stderr) | |
377 |
|
382 | |||
378 | def init_magics(self): |
|
383 | def init_magics(self): | |
379 | super(TerminalInteractiveShell, self).init_magics() |
|
384 | super(TerminalInteractiveShell, self).init_magics() | |
380 | self.register_magics(TerminalMagics) |
|
385 | self.register_magics(TerminalMagics) | |
381 |
|
386 | |||
382 | def init_alias(self): |
|
387 | def init_alias(self): | |
383 | # The parent class defines aliases that can be safely used with any |
|
388 | # The parent class defines aliases that can be safely used with any | |
384 | # frontend. |
|
389 | # frontend. | |
385 | super(TerminalInteractiveShell, self).init_alias() |
|
390 | super(TerminalInteractiveShell, self).init_alias() | |
386 |
|
391 | |||
387 | # Now define aliases that only make sense on the terminal, because they |
|
392 | # Now define aliases that only make sense on the terminal, because they | |
388 | # need direct access to the console in a way that we can't emulate in |
|
393 | # need direct access to the console in a way that we can't emulate in | |
389 | # GUI or web frontend |
|
394 | # GUI or web frontend | |
390 | if os.name == 'posix': |
|
395 | if os.name == 'posix': | |
391 | for cmd in ['clear', 'more', 'less', 'man']: |
|
396 | for cmd in ['clear', 'more', 'less', 'man']: | |
392 | self.alias_manager.soft_define_alias(cmd, cmd) |
|
397 | self.alias_manager.soft_define_alias(cmd, cmd) | |
393 |
|
398 | |||
394 |
|
399 | |||
395 | def __init__(self, *args, **kwargs): |
|
400 | def __init__(self, *args, **kwargs): | |
396 | super(TerminalInteractiveShell, self).__init__(*args, **kwargs) |
|
401 | super(TerminalInteractiveShell, self).__init__(*args, **kwargs) | |
397 | self.init_prompt_toolkit_cli() |
|
402 | self.init_prompt_toolkit_cli() | |
398 | self.init_term_title() |
|
403 | self.init_term_title() | |
399 | self.keep_running = True |
|
404 | self.keep_running = True | |
400 |
|
405 | |||
401 | self.debugger_history = InMemoryHistory() |
|
406 | self.debugger_history = InMemoryHistory() | |
402 |
|
407 | |||
403 | def ask_exit(self): |
|
408 | def ask_exit(self): | |
404 | self.keep_running = False |
|
409 | self.keep_running = False | |
405 |
|
410 | |||
406 | rl_next_input = None |
|
411 | rl_next_input = None | |
407 |
|
412 | |||
408 | def pre_prompt(self): |
|
413 | def pre_prompt(self): | |
409 | if self.rl_next_input: |
|
414 | if self.rl_next_input: | |
410 | self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input) |
|
415 | self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input) | |
411 | self.rl_next_input = None |
|
416 | self.rl_next_input = None | |
412 |
|
417 | |||
413 | def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED): |
|
418 | def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED): | |
414 |
|
419 | |||
415 | if display_banner is not DISPLAY_BANNER_DEPRECATED: |
|
420 | if display_banner is not DISPLAY_BANNER_DEPRECATED: | |
416 | warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) |
|
421 | warn('interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) | |
417 |
|
422 | |||
418 | self.keep_running = True |
|
423 | self.keep_running = True | |
419 | while self.keep_running: |
|
424 | while self.keep_running: | |
420 | print(self.separate_in, end='') |
|
425 | print(self.separate_in, end='') | |
421 |
|
426 | |||
422 | try: |
|
427 | try: | |
423 | code = self.prompt_for_code() |
|
428 | code = self.prompt_for_code() | |
424 | except EOFError: |
|
429 | except EOFError: | |
425 | if (not self.confirm_exit) \ |
|
430 | if (not self.confirm_exit) \ | |
426 | or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): |
|
431 | or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): | |
427 | self.ask_exit() |
|
432 | self.ask_exit() | |
428 |
|
433 | |||
429 | else: |
|
434 | else: | |
430 | if code: |
|
435 | if code: | |
431 | self.run_cell(code, store_history=True) |
|
436 | self.run_cell(code, store_history=True) | |
432 |
|
437 | |||
433 | def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED): |
|
438 | def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED): | |
434 | # An extra layer of protection in case someone mashing Ctrl-C breaks |
|
439 | # An extra layer of protection in case someone mashing Ctrl-C breaks | |
435 | # out of our internal code. |
|
440 | # out of our internal code. | |
436 | if display_banner is not DISPLAY_BANNER_DEPRECATED: |
|
441 | if display_banner is not DISPLAY_BANNER_DEPRECATED: | |
437 | warn('mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) |
|
442 | warn('mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) | |
438 | while True: |
|
443 | while True: | |
439 | try: |
|
444 | try: | |
440 | self.interact() |
|
445 | self.interact() | |
441 | break |
|
446 | break | |
442 | except KeyboardInterrupt: |
|
447 | except KeyboardInterrupt: | |
443 | print("\nKeyboardInterrupt escaped interact()\n") |
|
448 | print("\nKeyboardInterrupt escaped interact()\n") | |
444 |
|
449 | |||
445 | _inputhook = None |
|
450 | _inputhook = None | |
446 | def inputhook(self, context): |
|
451 | def inputhook(self, context): | |
447 | if self._inputhook is not None: |
|
452 | if self._inputhook is not None: | |
448 | self._inputhook(context) |
|
453 | self._inputhook(context) | |
449 |
|
454 | |||
450 | def enable_gui(self, gui=None): |
|
455 | def enable_gui(self, gui=None): | |
451 | if gui: |
|
456 | if gui: | |
452 | self._inputhook = get_inputhook_func(gui) |
|
457 | self._inputhook = get_inputhook_func(gui) | |
453 | else: |
|
458 | else: | |
454 | self._inputhook = None |
|
459 | self._inputhook = None | |
455 |
|
460 | |||
456 | # Run !system commands directly, not through pipes, so terminal programs |
|
461 | # Run !system commands directly, not through pipes, so terminal programs | |
457 | # work correctly. |
|
462 | # work correctly. | |
458 | system = InteractiveShell.system_raw |
|
463 | system = InteractiveShell.system_raw | |
459 |
|
464 | |||
460 | def auto_rewrite_input(self, cmd): |
|
465 | def auto_rewrite_input(self, cmd): | |
461 | """Overridden from the parent class to use fancy rewriting prompt""" |
|
466 | """Overridden from the parent class to use fancy rewriting prompt""" | |
462 | if not self.show_rewritten_input: |
|
467 | if not self.show_rewritten_input: | |
463 | return |
|
468 | return | |
464 |
|
469 | |||
465 | tokens = self.prompts.rewrite_prompt_tokens() |
|
470 | tokens = self.prompts.rewrite_prompt_tokens() | |
466 | if self.pt_cli: |
|
471 | if self.pt_cli: | |
467 | self.pt_cli.print_tokens(tokens) |
|
472 | self.pt_cli.print_tokens(tokens) | |
468 | print(cmd) |
|
473 | print(cmd) | |
469 | else: |
|
474 | else: | |
470 | prompt = ''.join(s for t, s in tokens) |
|
475 | prompt = ''.join(s for t, s in tokens) | |
471 | print(prompt, cmd, sep='') |
|
476 | print(prompt, cmd, sep='') | |
472 |
|
477 | |||
473 | _prompts_before = None |
|
478 | _prompts_before = None | |
474 | def switch_doctest_mode(self, mode): |
|
479 | def switch_doctest_mode(self, mode): | |
475 | """Switch prompts to classic for %doctest_mode""" |
|
480 | """Switch prompts to classic for %doctest_mode""" | |
476 | if mode: |
|
481 | if mode: | |
477 | self._prompts_before = self.prompts |
|
482 | self._prompts_before = self.prompts | |
478 | self.prompts = ClassicPrompts(self) |
|
483 | self.prompts = ClassicPrompts(self) | |
479 | elif self._prompts_before: |
|
484 | elif self._prompts_before: | |
480 | self.prompts = self._prompts_before |
|
485 | self.prompts = self._prompts_before | |
481 | self._prompts_before = None |
|
486 | self._prompts_before = None | |
482 | self._update_layout() |
|
487 | self._update_layout() | |
483 |
|
488 | |||
484 |
|
489 | |||
485 | InteractiveShellABC.register(TerminalInteractiveShell) |
|
490 | InteractiveShellABC.register(TerminalInteractiveShell) | |
486 |
|
491 | |||
487 | if __name__ == '__main__': |
|
492 | if __name__ == '__main__': | |
488 | TerminalInteractiveShell.instance().interact() |
|
493 | TerminalInteractiveShell.instance().interact() |
@@ -1,74 +1,74 b'' | |||||
1 | """Terminal input and output prompts.""" |
|
1 | """Terminal input and output prompts.""" | |
2 | from __future__ import print_function |
|
2 | from __future__ import print_function | |
3 |
|
3 | |||
4 | from pygments.token import Token |
|
4 | from pygments.token import Token | |
5 | import sys |
|
5 | import sys | |
6 |
|
6 | |||
7 | from IPython.core.displayhook import DisplayHook |
|
7 | from IPython.core.displayhook import DisplayHook | |
8 |
|
8 | |||
9 | from prompt_toolkit.layout.utils import token_list_width |
|
9 | from prompt_toolkit.layout.utils import token_list_width | |
10 |
|
10 | |||
11 | class Prompts(object): |
|
11 | class Prompts(object): | |
12 | def __init__(self, shell): |
|
12 | def __init__(self, shell): | |
13 | self.shell = shell |
|
13 | self.shell = shell | |
14 |
|
14 | |||
15 | def in_prompt_tokens(self, cli=None): |
|
15 | def in_prompt_tokens(self, cli=None): | |
16 | return [ |
|
16 | return [ | |
17 | (Token.Prompt, 'In ['), |
|
17 | (Token.Prompt, 'In ['), | |
18 | (Token.PromptNum, str(self.shell.execution_count)), |
|
18 | (Token.PromptNum, str(self.shell.execution_count)), | |
19 | (Token.Prompt, ']: '), |
|
19 | (Token.Prompt, ']: '), | |
20 | ] |
|
20 | ] | |
21 |
|
21 | |||
22 | def _width(self): |
|
22 | def _width(self): | |
23 | return token_list_width(self.in_prompt_tokens()) |
|
23 | return token_list_width(self.in_prompt_tokens()) | |
24 |
|
24 | |||
25 | def continuation_prompt_tokens(self, cli=None, width=None): |
|
25 | def continuation_prompt_tokens(self, cli=None, width=None): | |
26 | if width is None: |
|
26 | if width is None: | |
27 | width = self._width() |
|
27 | width = self._width() | |
28 | return [ |
|
28 | return [ | |
29 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
29 | (Token.Prompt, (' ' * (width - 5)) + '...: '), | |
30 | ] |
|
30 | ] | |
31 |
|
31 | |||
32 | def rewrite_prompt_tokens(self): |
|
32 | def rewrite_prompt_tokens(self): | |
33 | width = self._width() |
|
33 | width = self._width() | |
34 | return [ |
|
34 | return [ | |
35 | (Token.Prompt, ('-' * (width - 2)) + '> '), |
|
35 | (Token.Prompt, ('-' * (width - 2)) + '> '), | |
36 | ] |
|
36 | ] | |
37 |
|
37 | |||
38 | def out_prompt_tokens(self): |
|
38 | def out_prompt_tokens(self): | |
39 | return [ |
|
39 | return [ | |
40 | (Token.OutPrompt, 'Out['), |
|
40 | (Token.OutPrompt, 'Out['), | |
41 | (Token.OutPromptNum, str(self.shell.execution_count)), |
|
41 | (Token.OutPromptNum, str(self.shell.execution_count)), | |
42 | (Token.OutPrompt, ']: '), |
|
42 | (Token.OutPrompt, ']: '), | |
43 | ] |
|
43 | ] | |
44 |
|
44 | |||
45 | class ClassicPrompts(Prompts): |
|
45 | class ClassicPrompts(Prompts): | |
46 | def in_prompt_tokens(self, cli=None): |
|
46 | def in_prompt_tokens(self, cli=None): | |
47 | return [ |
|
47 | return [ | |
48 | (Token.Prompt, '>>> '), |
|
48 | (Token.Prompt, '>>> '), | |
49 | ] |
|
49 | ] | |
50 |
|
50 | |||
51 | def continuation_prompt_tokens(self, cli=None, width=None): |
|
51 | def continuation_prompt_tokens(self, cli=None, width=None): | |
52 | return [ |
|
52 | return [ | |
53 | (Token.Prompt, '... ') |
|
53 | (Token.Prompt, '... ') | |
54 | ] |
|
54 | ] | |
55 |
|
55 | |||
56 | def rewrite_prompt_tokens(self): |
|
56 | def rewrite_prompt_tokens(self): | |
57 | return [] |
|
57 | return [] | |
58 |
|
58 | |||
59 | def out_prompt_tokens(self): |
|
59 | def out_prompt_tokens(self): | |
60 | return [] |
|
60 | return [] | |
61 |
|
61 | |||
62 | class RichPromptDisplayHook(DisplayHook): |
|
62 | class RichPromptDisplayHook(DisplayHook): | |
63 | """Subclass of base display hook using coloured prompt""" |
|
63 | """Subclass of base display hook using coloured prompt""" | |
64 | def write_output_prompt(self): |
|
64 | def write_output_prompt(self): | |
65 | sys.stdout.write(self.shell.separate_out) |
|
65 | sys.stdout.write(self.shell.separate_out) | |
66 | self.prompt_end_newline = False |
|
66 | self.prompt_end_newline = False | |
67 | if self.do_full_cache: |
|
67 | if self.do_full_cache: | |
68 | tokens = self.shell.prompts.out_prompt_tokens() |
|
68 | tokens = self.shell.prompts.out_prompt_tokens() | |
69 | if tokens and tokens[-1][1].endswith('\n'): |
|
69 | if tokens and tokens[-1][1].endswith('\n'): | |
70 | self.prompt_end_newline = True |
|
70 | self.prompt_end_newline = True | |
71 | if self.shell.pt_cli: |
|
71 | if self.shell.pt_cli: | |
72 | self.shell.pt_cli.print_tokens(tokens) |
|
72 | self.shell.pt_cli.print_tokens(tokens) | |
73 | else: |
|
73 | else: | |
74 |
|
|
74 | sys.stdout.write(''.join(s for t, s in tokens)) |
@@ -1,51 +1,87 b'' | |||||
1 | .. image:: https://codecov.io/github/ipython/ipython/coverage.svg?branch=master |
|
1 | .. image:: https://codecov.io/github/ipython/ipython/coverage.svg?branch=master | |
2 | :target: https://codecov.io/github/ipython/ipython?branch=master |
|
2 | :target: https://codecov.io/github/ipython/ipython?branch=master | |
3 |
|
3 | |||
4 | .. image:: https://img.shields.io/pypi/dm/IPython.svg |
|
4 | .. image:: https://img.shields.io/pypi/dm/IPython.svg | |
5 | :target: https://pypi.python.org/pypi/ipython |
|
5 | :target: https://pypi.python.org/pypi/ipython | |
6 |
|
6 | |||
7 | .. image:: https://img.shields.io/pypi/v/IPython.svg |
|
7 | .. image:: https://img.shields.io/pypi/v/IPython.svg | |
8 | :target: https://pypi.python.org/pypi/ipython |
|
8 | :target: https://pypi.python.org/pypi/ipython | |
9 |
|
9 | |||
10 | .. image:: https://img.shields.io/travis/ipython/ipython.svg |
|
10 | .. image:: https://img.shields.io/travis/ipython/ipython.svg | |
11 | :target: https://travis-ci.org/ipython/ipython |
|
11 | :target: https://travis-ci.org/ipython/ipython | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | =========================================== |
|
14 | =========================================== | |
15 | IPython: Productive Interactive Computing |
|
15 | IPython: Productive Interactive Computing | |
16 | =========================================== |
|
16 | =========================================== | |
17 |
|
17 | |||
18 | Overview |
|
18 | Overview | |
19 | ======== |
|
19 | ======== | |
20 |
|
20 | |||
21 | Welcome to IPython. Our full documentation is available on `ipython.readthedocs.io |
|
21 | Welcome to IPython. Our full documentation is available on `ipython.readthedocs.io | |
22 | <https://ipython.readthedocs.io/en/stable/>`_ and contain information on how to install, use |
|
22 | <https://ipython.readthedocs.io/en/stable/>`_ and contains information on how to install, use and | |
23 | contribute to the project. |
|
23 | contribute to the project. | |
24 |
|
24 | |||
25 | Officially, IPython requires Python version 3.3 and above. |
|
25 | Officially, IPython requires Python version 3.3 and above. | |
26 | IPython 5.x is the last IPython version to support Python 2.7. |
|
26 | IPython 5.x is the last IPython version to support Python 2.7. | |
27 |
|
27 | |||
28 | The Notebook, Qt console and a number of other pieces are now parts of *Jupyter*. |
|
28 | The Notebook, Qt console and a number of other pieces are now parts of *Jupyter*. | |
29 | See the `Jupyter installation docs <http://jupyter.readthedocs.io/en/latest/install.html>`__ |
|
29 | See the `Jupyter installation docs <http://jupyter.readthedocs.io/en/latest/install.html>`__ | |
30 | if you want to use these. |
|
30 | if you want to use these. | |
31 |
|
31 | |||
32 |
|
32 | |||
33 |
|
33 | |||
34 |
|
34 | |||
35 |
Develop |
|
35 | Development and Instant running | |
36 |
=============================== |
|
36 | =============================== | |
37 |
|
37 | |||
38 | You can find the latest version of the development documentation on `readthedocs |
|
38 | You can find the latest version of the development documentation on `readthedocs | |
39 |
<http://ipython.readthedocs.io/en/latest/>`_. |
|
39 | <http://ipython.readthedocs.io/en/latest/>`_. | |
40 |
|
40 | |||
41 | You can run IPython from this directory without even installing it system-wide |
|
41 | You can run IPython from this directory without even installing it system-wide | |
42 | by typing at the terminal:: |
|
42 | by typing at the terminal:: | |
43 |
|
43 | |||
44 | $ python -m IPython |
|
44 | $ python -m IPython | |
45 |
|
45 | |||
46 |
Or see the `develop |
|
46 | Or see the `development installation docs | |
47 | <http://ipython.readthedocs.io/en/latest/install/install.html#installing-the-development-version>`_ |
|
47 | <http://ipython.readthedocs.io/en/latest/install/install.html#installing-the-development-version>`_ | |
48 |
for the latest revision on read the docs. |
|
48 | for the latest revision on read the docs. | |
49 |
|
49 | |||
50 | Documentation and installation instructions for older version of IPython can be |
|
50 | Documentation and installation instructions for older version of IPython can be | |
51 | found on the `IPython website <http://ipython.org/documentation.html>`_ |
|
51 | found on the `IPython website <http://ipython.org/documentation.html>`_ | |
|
52 | ||||
|
53 | ||||
|
54 | ||||
|
55 | IPython requires Python version 3 or above | |||
|
56 | ========================================== | |||
|
57 | ||||
|
58 | Starting with version 6.0, IPython does not support Python 2.7, 3.0, 3.1, or | |||
|
59 | 3.2. | |||
|
60 | ||||
|
61 | For a version compatible with Python 2.7, please install the 5.x LTS Long Term | |||
|
62 | Support version. | |||
|
63 | ||||
|
64 | If you are encountering this error message you are likely trying to install or | |||
|
65 | use IPython from source. You need to checkout the remote 5.x branch. If you are | |||
|
66 | using git the following should work: | |||
|
67 | ||||
|
68 | $ git fetch origin | |||
|
69 | $ git checkout -b origin/5.x | |||
|
70 | ||||
|
71 | If you encounter this error message with a regular install of IPython, then you | |||
|
72 | likely need to update your package manager, for example if you are using `pip` | |||
|
73 | check the version of pip with | |||
|
74 | ||||
|
75 | $ pip --version | |||
|
76 | ||||
|
77 | You will need to update pip to the version 8.2 or greater. If you are not using | |||
|
78 | pip, please inquiry with the maintainers of the package for your package | |||
|
79 | manager. | |||
|
80 | ||||
|
81 | For more information see one of our blog posts: | |||
|
82 | ||||
|
83 | http://blog.jupyter.org/2016/07/08/ipython-5-0-released/ | |||
|
84 | ||||
|
85 | As well as the following Pull-Request for discussion: | |||
|
86 | ||||
|
87 | https://github.com/ipython/ipython/pull/9900 |
@@ -1,229 +1,230 b'' | |||||
1 | ======================= |
|
1 | ======================= | |
2 | Specific config details |
|
2 | Specific config details | |
3 | ======================= |
|
3 | ======================= | |
4 |
|
4 | |||
5 | .. _custom_prompts: |
|
5 | .. _custom_prompts: | |
6 |
|
6 | |||
7 | Custom Prompts |
|
7 | Custom Prompts | |
8 | ============== |
|
8 | ============== | |
9 |
|
9 | |||
10 | .. versionchanged:: 5.0 |
|
10 | .. versionchanged:: 5.0 | |
11 |
|
11 | |||
12 | From IPython 5, prompts are produced as a list of Pygments tokens, which are |
|
12 | From IPython 5, prompts are produced as a list of Pygments tokens, which are | |
13 | tuples of (token_type, text). You can customise prompts by writing a method |
|
13 | tuples of (token_type, text). You can customise prompts by writing a method | |
14 | which generates a list of tokens. |
|
14 | which generates a list of tokens. | |
15 |
|
15 | |||
16 | There are four kinds of prompt: |
|
16 | There are four kinds of prompt: | |
17 |
|
17 | |||
18 | * The **in** prompt is shown before the first line of input |
|
18 | * The **in** prompt is shown before the first line of input | |
19 | (default like ``In [1]:``). |
|
19 | (default like ``In [1]:``). | |
20 | * The **continuation** prompt is shown before further lines of input |
|
20 | * The **continuation** prompt is shown before further lines of input | |
21 | (default like ``...:``). |
|
21 | (default like ``...:``). | |
22 | * The **rewrite** prompt is shown to highlight how special syntax has been |
|
22 | * The **rewrite** prompt is shown to highlight how special syntax has been | |
23 | interpreted (default like ``----->``). |
|
23 | interpreted (default like ``----->``). | |
24 | * The **out** prompt is shown before the result from evaluating the input |
|
24 | * The **out** prompt is shown before the result from evaluating the input | |
25 | (default like ``Out[1]:``). |
|
25 | (default like ``Out[1]:``). | |
26 |
|
26 | |||
27 | Custom prompts are supplied together as a class. If you want to customise only |
|
27 | Custom prompts are supplied together as a class. If you want to customise only | |
28 | some of the prompts, inherit from :class:`IPython.terminal.prompts.Prompts`, |
|
28 | some of the prompts, inherit from :class:`IPython.terminal.prompts.Prompts`, | |
29 | which defines the defaults. The required interface is like this: |
|
29 | which defines the defaults. The required interface is like this: | |
30 |
|
30 | |||
31 | .. class:: MyPrompts(shell) |
|
31 | .. class:: MyPrompts(shell) | |
32 |
|
32 | |||
33 | Prompt style definition. *shell* is a reference to the |
|
33 | Prompt style definition. *shell* is a reference to the | |
34 | :class:`~.TerminalInteractiveShell` instance. |
|
34 | :class:`~.TerminalInteractiveShell` instance. | |
35 |
|
35 | |||
36 | .. method:: in_prompt_tokens(cli=None) |
|
36 | .. method:: in_prompt_tokens(cli=None) | |
37 | continuation_prompt_tokens(self, cli=None, width=None) |
|
37 | continuation_prompt_tokens(self, cli=None, width=None) | |
38 | rewrite_prompt_tokens() |
|
38 | rewrite_prompt_tokens() | |
39 | out_prompt_tokens() |
|
39 | out_prompt_tokens() | |
40 |
|
40 | |||
41 | Return the respective prompts as lists of ``(token_type, text)`` tuples. |
|
41 | Return the respective prompts as lists of ``(token_type, text)`` tuples. | |
42 |
|
42 | |||
43 | For continuation prompts, *width* is an integer representing the width of |
|
43 | For continuation prompts, *width* is an integer representing the width of | |
44 | the prompt area in terminal columns. |
|
44 | the prompt area in terminal columns. | |
45 |
|
45 | |||
46 | *cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance. |
|
46 | *cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance. | |
47 | This is mainly for compatibility with the API prompt_toolkit expects. |
|
47 | This is mainly for compatibility with the API prompt_toolkit expects. | |
48 |
|
48 | |||
49 | Here is an example Prompt class that will show the current working directory |
|
49 | Here is an example Prompt class that will show the current working directory | |
50 | in the input prompt: |
|
50 | in the input prompt: | |
51 |
|
51 | |||
52 | .. code-block:: python |
|
52 | .. code-block:: python | |
53 |
|
53 | |||
54 | from IPython.terminal.prompts import Prompts, Token |
|
54 | from IPython.terminal.prompts import Prompts, Token | |
55 | import os |
|
55 | import os | |
56 |
|
56 | |||
57 | class MyPrompt(Prompts): |
|
57 | class MyPrompt(Prompts): | |
58 | def in_prompt_tokens(self, cli=None): |
|
58 | def in_prompt_tokens(self, cli=None): | |
59 | return [(Token, os.getcwd()), |
|
59 | return [(Token, os.getcwd()), | |
60 | (Token.Prompt, ' >>>')] |
|
60 | (Token.Prompt, ' >>>')] | |
61 |
|
61 | |||
62 | To set the new prompt, assign it to the ``prompts`` attribute of the IPython |
|
62 | To set the new prompt, assign it to the ``prompts`` attribute of the IPython | |
63 | shell: |
|
63 | shell: | |
64 |
|
64 | |||
65 | .. code-block:: python |
|
65 | .. code-block:: python | |
66 |
|
66 | |||
67 | In [2]: ip = get_ipython() |
|
67 | In [2]: ip = get_ipython() | |
68 | ...: ip.prompts = MyPrompt(ip) |
|
68 | ...: ip.prompts = MyPrompt(ip) | |
69 |
|
69 | |||
70 | /home/bob >>> # it works |
|
70 | /home/bob >>> # it works | |
71 |
|
71 | |||
72 | See ``IPython/example/utils/cwd_prompt.py`` for an example of how to write an |
|
72 | See ``IPython/example/utils/cwd_prompt.py`` for an example of how to write an | |
73 | extensions to customise prompts. |
|
73 | extensions to customise prompts. | |
74 |
|
74 | |||
75 | Inside IPython or in a startup script, you can use a custom prompts class |
|
75 | Inside IPython or in a startup script, you can use a custom prompts class | |
76 | by setting ``get_ipython().prompts`` to an *instance* of the class. |
|
76 | by setting ``get_ipython().prompts`` to an *instance* of the class. | |
77 | In configuration, ``TerminalInteractiveShell.prompts_class`` may be set to |
|
77 | In configuration, ``TerminalInteractiveShell.prompts_class`` may be set to | |
78 | either the class object, or a string of its full importable name. |
|
78 | either the class object, or a string of its full importable name. | |
79 |
|
79 | |||
80 | To include invisible terminal control sequences in a prompt, use |
|
80 | To include invisible terminal control sequences in a prompt, use | |
81 | ``Token.ZeroWidthEscape`` as the token type. Tokens with this type are ignored |
|
81 | ``Token.ZeroWidthEscape`` as the token type. Tokens with this type are ignored | |
82 | when calculating the width. |
|
82 | when calculating the width. | |
83 |
|
83 | |||
84 | Colours in the prompt are determined by the token types and the highlighting |
|
84 | Colours in the prompt are determined by the token types and the highlighting | |
85 | style; see below for more details. The tokens used in the default prompts are |
|
85 | style; see below for more details. The tokens used in the default prompts are | |
86 | ``Prompt``, ``PromptNum``, ``OutPrompt`` and ``OutPromptNum``. |
|
86 | ``Prompt``, ``PromptNum``, ``OutPrompt`` and ``OutPromptNum``. | |
87 |
|
87 | |||
88 | .. _termcolour: |
|
88 | .. _termcolour: | |
89 |
|
89 | |||
90 | Terminal Colors |
|
90 | Terminal Colors | |
91 | =============== |
|
91 | =============== | |
92 |
|
92 | |||
93 | .. versionchanged:: 5.0 |
|
93 | .. versionchanged:: 5.0 | |
94 |
|
94 | |||
95 | There are two main configuration options controlling colours. |
|
95 | There are two main configuration options controlling colours. | |
96 |
|
96 | |||
97 | ``InteractiveShell.colors`` sets the colour of tracebacks and object info (the |
|
97 | ``InteractiveShell.colors`` sets the colour of tracebacks and object info (the | |
98 | output from e.g. ``zip?``). It may also affect other things if the option below |
|
98 | output from e.g. ``zip?``). It may also affect other things if the option below | |
99 | is set to ``'legacy'``. It has four case-insensitive values: |
|
99 | is set to ``'legacy'``. It has four case-insensitive values: | |
100 | ``'nocolor', 'neutral', 'linux', 'lightbg'``. The default is *neutral*, which |
|
100 | ``'nocolor', 'neutral', 'linux', 'lightbg'``. The default is *neutral*, which | |
101 | should be legible on either dark or light terminal backgrounds. *linux* is |
|
101 | should be legible on either dark or light terminal backgrounds. *linux* is | |
102 | optimised for dark backgrounds and *lightbg* for light ones. |
|
102 | optimised for dark backgrounds and *lightbg* for light ones. | |
103 |
|
103 | |||
104 |
``TerminalInteractiveShell.highlighting_style`` determines prompt colours and |
|
104 | ``TerminalInteractiveShell.highlighting_style`` determines prompt colours and | |
105 |
highlighting. It takes the name |
|
105 | syntax highlighting. It takes the name (as a string) or class (as a subclass of | |
106 | value ``'legacy'`` to pick a style in accordance with ``InteractiveShell.colors``. |
|
106 | ``pygments.style.Style``) of a Pygments style, or the special value ``'legacy'`` | |
|
107 | to pick a style in accordance with ``InteractiveShell.colors``. | |||
107 |
|
108 | |||
108 | You can see the Pygments styles available on your system by running:: |
|
109 | You can see the Pygments styles available on your system by running:: | |
109 |
|
110 | |||
110 | import pygments |
|
111 | import pygments | |
111 | list(pygments.styles.get_all_styles()) |
|
112 | list(pygments.styles.get_all_styles()) | |
112 |
|
113 | |||
113 | Additionally, ``TerminalInteractiveShell.highlighting_style_overrides`` can override |
|
114 | Additionally, ``TerminalInteractiveShell.highlighting_style_overrides`` can override | |
114 | specific styles in the highlighting. It should be a dictionary mapping Pygments |
|
115 | specific styles in the highlighting. It should be a dictionary mapping Pygments | |
115 | token types to strings defining the style. See `Pygments' documentation |
|
116 | token types to strings defining the style. See `Pygments' documentation | |
116 | <http://pygments.org/docs/styles/#creating-own-styles>`__ for the language used |
|
117 | <http://pygments.org/docs/styles/#creating-own-styles>`__ for the language used | |
117 | to define styles. |
|
118 | to define styles. | |
118 |
|
119 | |||
119 | Colors in the pager |
|
120 | Colors in the pager | |
120 | ------------------- |
|
121 | ------------------- | |
121 |
|
122 | |||
122 | On some systems, the default pager has problems with ANSI colour codes. |
|
123 | On some systems, the default pager has problems with ANSI colour codes. | |
123 | To configure your default pager to allow these: |
|
124 | To configure your default pager to allow these: | |
124 |
|
125 | |||
125 | 1. Set the environment PAGER variable to ``less``. |
|
126 | 1. Set the environment PAGER variable to ``less``. | |
126 | 2. Set the environment LESS variable to ``-r`` (plus any other options |
|
127 | 2. Set the environment LESS variable to ``-r`` (plus any other options | |
127 | you always want to pass to less by default). This tells less to |
|
128 | you always want to pass to less by default). This tells less to | |
128 | properly interpret control sequences, which is how color |
|
129 | properly interpret control sequences, which is how color | |
129 | information is given to your terminal. |
|
130 | information is given to your terminal. | |
130 |
|
131 | |||
131 | .. _editors: |
|
132 | .. _editors: | |
132 |
|
133 | |||
133 | Editor configuration |
|
134 | Editor configuration | |
134 | ==================== |
|
135 | ==================== | |
135 |
|
136 | |||
136 | IPython can integrate with text editors in a number of different ways: |
|
137 | IPython can integrate with text editors in a number of different ways: | |
137 |
|
138 | |||
138 | * Editors (such as `(X)Emacs`_, vim_ and TextMate_) can |
|
139 | * Editors (such as `(X)Emacs`_, vim_ and TextMate_) can | |
139 | send code to IPython for execution. |
|
140 | send code to IPython for execution. | |
140 |
|
141 | |||
141 | * IPython's ``%edit`` magic command can open an editor of choice to edit |
|
142 | * IPython's ``%edit`` magic command can open an editor of choice to edit | |
142 | a code block. |
|
143 | a code block. | |
143 |
|
144 | |||
144 | The %edit command (and its alias %ed) will invoke the editor set in your |
|
145 | The %edit command (and its alias %ed) will invoke the editor set in your | |
145 | environment as :envvar:`EDITOR`. If this variable is not set, it will default |
|
146 | environment as :envvar:`EDITOR`. If this variable is not set, it will default | |
146 | to vi under Linux/Unix and to notepad under Windows. You may want to set this |
|
147 | to vi under Linux/Unix and to notepad under Windows. You may want to set this | |
147 | variable properly and to a lightweight editor which doesn't take too long to |
|
148 | variable properly and to a lightweight editor which doesn't take too long to | |
148 | start (that is, something other than a new instance of Emacs). This way you |
|
149 | start (that is, something other than a new instance of Emacs). This way you | |
149 | can edit multi-line code quickly and with the power of a real editor right |
|
150 | can edit multi-line code quickly and with the power of a real editor right | |
150 | inside IPython. |
|
151 | inside IPython. | |
151 |
|
152 | |||
152 | You can also control the editor by setting :attr:`TerminalInteractiveShell.editor` |
|
153 | You can also control the editor by setting :attr:`TerminalInteractiveShell.editor` | |
153 | in :file:`ipython_config.py`. |
|
154 | in :file:`ipython_config.py`. | |
154 |
|
155 | |||
155 | Vim |
|
156 | Vim | |
156 | --- |
|
157 | --- | |
157 |
|
158 | |||
158 | Paul Ivanov's `vim-ipython <https://github.com/ivanov/vim-ipython>`_ provides |
|
159 | Paul Ivanov's `vim-ipython <https://github.com/ivanov/vim-ipython>`_ provides | |
159 | powerful IPython integration for vim. |
|
160 | powerful IPython integration for vim. | |
160 |
|
161 | |||
161 | .. _emacs: |
|
162 | .. _emacs: | |
162 |
|
163 | |||
163 | (X)Emacs |
|
164 | (X)Emacs | |
164 | -------- |
|
165 | -------- | |
165 |
|
166 | |||
166 | If you are a dedicated Emacs user, and want to use Emacs when IPython's |
|
167 | If you are a dedicated Emacs user, and want to use Emacs when IPython's | |
167 | ``%edit`` magic command is called you should set up the Emacs server so that |
|
168 | ``%edit`` magic command is called you should set up the Emacs server so that | |
168 | new requests are handled by the original process. This means that almost no |
|
169 | new requests are handled by the original process. This means that almost no | |
169 | time is spent in handling the request (assuming an Emacs process is already |
|
170 | time is spent in handling the request (assuming an Emacs process is already | |
170 | running). For this to work, you need to set your EDITOR environment variable |
|
171 | running). For this to work, you need to set your EDITOR environment variable | |
171 | to 'emacsclient'. The code below, supplied by Francois Pinard, can then be |
|
172 | to 'emacsclient'. The code below, supplied by Francois Pinard, can then be | |
172 | used in your :file:`.emacs` file to enable the server: |
|
173 | used in your :file:`.emacs` file to enable the server: | |
173 |
|
174 | |||
174 | .. code-block:: common-lisp |
|
175 | .. code-block:: common-lisp | |
175 |
|
176 | |||
176 | (defvar server-buffer-clients) |
|
177 | (defvar server-buffer-clients) | |
177 | (when (and (fboundp 'server-start) (string-equal (getenv "TERM") 'xterm)) |
|
178 | (when (and (fboundp 'server-start) (string-equal (getenv "TERM") 'xterm)) | |
178 | (server-start) |
|
179 | (server-start) | |
179 | (defun fp-kill-server-with-buffer-routine () |
|
180 | (defun fp-kill-server-with-buffer-routine () | |
180 | (and server-buffer-clients (server-done))) |
|
181 | (and server-buffer-clients (server-done))) | |
181 | (add-hook 'kill-buffer-hook 'fp-kill-server-with-buffer-routine)) |
|
182 | (add-hook 'kill-buffer-hook 'fp-kill-server-with-buffer-routine)) | |
182 |
|
183 | |||
183 | Thanks to the work of Alexander Schmolck and Prabhu Ramachandran, |
|
184 | Thanks to the work of Alexander Schmolck and Prabhu Ramachandran, | |
184 | currently (X)Emacs and IPython get along very well in other ways. |
|
185 | currently (X)Emacs and IPython get along very well in other ways. | |
185 |
|
186 | |||
186 | With (X)EMacs >= 24, You can enable IPython in python-mode with: |
|
187 | With (X)EMacs >= 24, You can enable IPython in python-mode with: | |
187 |
|
188 | |||
188 | .. code-block:: common-lisp |
|
189 | .. code-block:: common-lisp | |
189 |
|
190 | |||
190 | (require 'python) |
|
191 | (require 'python) | |
191 | (setq python-shell-interpreter "ipython") |
|
192 | (setq python-shell-interpreter "ipython") | |
192 |
|
193 | |||
193 | .. _`(X)Emacs`: http://www.gnu.org/software/emacs/ |
|
194 | .. _`(X)Emacs`: http://www.gnu.org/software/emacs/ | |
194 | .. _TextMate: http://macromates.com/ |
|
195 | .. _TextMate: http://macromates.com/ | |
195 | .. _vim: http://www.vim.org/ |
|
196 | .. _vim: http://www.vim.org/ | |
196 |
|
197 | |||
197 | .. _custom_keyboard_shortcuts |
|
198 | .. _custom_keyboard_shortcuts | |
198 |
|
199 | |||
199 | Keyboard Shortcuts |
|
200 | Keyboard Shortcuts | |
200 | ================== |
|
201 | ================== | |
201 |
|
202 | |||
202 | .. versionchanged:: 5.0 |
|
203 | .. versionchanged:: 5.0 | |
203 |
|
204 | |||
204 | You can customise keyboard shortcuts for terminal IPython. Put code like this in |
|
205 | You can customise keyboard shortcuts for terminal IPython. Put code like this in | |
205 | a :ref:`startup file <startup_files>`:: |
|
206 | a :ref:`startup file <startup_files>`:: | |
206 |
|
207 | |||
207 | from IPython import get_ipython |
|
208 | from IPython import get_ipython | |
208 | from prompt_toolkit.enums import DEFAULT_BUFFER |
|
209 | from prompt_toolkit.enums import DEFAULT_BUFFER | |
209 | from prompt_toolkit.keys import Keys |
|
210 | from prompt_toolkit.keys import Keys | |
210 | from prompt_toolkit.filters import HasFocus, HasSelection, ViInsertMode, EmacsInsertMode |
|
211 | from prompt_toolkit.filters import HasFocus, HasSelection, ViInsertMode, EmacsInsertMode | |
211 |
|
212 | |||
212 | ip = get_ipython() |
|
213 | ip = get_ipython() | |
213 | insert_mode = ViInsertMode() | EmacsInsertMode() |
|
214 | insert_mode = ViInsertMode() | EmacsInsertMode() | |
214 |
|
215 | |||
215 | def insert_unexpected(event): |
|
216 | def insert_unexpected(event): | |
216 | buf = event.current_buffer |
|
217 | buf = event.current_buffer | |
217 | buf.insert_text('The Spanish Inquisition') |
|
218 | buf.insert_text('The Spanish Inquisition') | |
218 |
|
219 | |||
219 | # Register the shortcut if IPython is using prompt_toolkit |
|
220 | # Register the shortcut if IPython is using prompt_toolkit | |
220 | if getattr(ip, 'pt_cli'): |
|
221 | if getattr(ip, 'pt_cli'): | |
221 | registry = ip.pt_cli.application.key_bindings_registry |
|
222 | registry = ip.pt_cli.application.key_bindings_registry | |
222 | registry.add_binding(Keys.ControlN, |
|
223 | registry.add_binding(Keys.ControlN, | |
223 | filter=(HasFocus(DEFAULT_BUFFER) |
|
224 | filter=(HasFocus(DEFAULT_BUFFER) | |
224 | & ~HasSelection() |
|
225 | & ~HasSelection() | |
225 | & insert_mode))(insert_unexpected) |
|
226 | & insert_mode))(insert_unexpected) | |
226 |
|
227 | |||
227 | For more information on filters and what you can do with the ``event`` object, |
|
228 | For more information on filters and what you can do with the ``event`` object, | |
228 | `see the prompt_toolkit docs |
|
229 | `see the prompt_toolkit docs | |
229 | <http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-custom-key-bindings>`__. |
|
230 | <http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-custom-key-bindings>`__. |
@@ -1,289 +1,298 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # -*- coding: utf-8 -*- |
|
2 | # -*- coding: utf-8 -*- | |
3 | """Setup script for IPython. |
|
3 | """Setup script for IPython. | |
4 |
|
4 | |||
5 | Under Posix environments it works like a typical setup.py script. |
|
5 | Under Posix environments it works like a typical setup.py script. | |
6 | Under Windows, the command sdist is not supported, since IPython |
|
6 | Under Windows, the command sdist is not supported, since IPython | |
7 | requires utilities which are not available under Windows.""" |
|
7 | requires utilities which are not available under Windows.""" | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (c) 2008-2011, IPython Development Team. |
|
10 | # Copyright (c) 2008-2011, IPython Development Team. | |
11 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> |
|
11 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> | |
12 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> |
|
12 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> | |
13 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> |
|
13 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the Modified BSD License. |
|
15 | # Distributed under the terms of the Modified BSD License. | |
16 | # |
|
16 | # | |
17 | # The full license is in the file COPYING.rst, distributed with this software. |
|
17 | # The full license is in the file COPYING.rst, distributed with this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Minimal Python version sanity check |
|
21 | # Minimal Python version sanity check | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | from __future__ import print_function |
|
23 | from __future__ import print_function | |
24 |
|
24 | |||
25 | import sys |
|
25 | import sys | |
26 |
|
26 | |||
27 | # This check is also made in IPython/__init__, don't forget to update both when |
|
27 | # This check is also made in IPython/__init__, don't forget to update both when | |
28 | # changing Python version requirements. |
|
28 | # changing Python version requirements. | |
29 |
|
|
29 | if sys.version_info < (3,3): | |
30 | if v[:2] < (3,3): |
|
30 | error = """ | |
31 | error = "ERROR: IPython requires Python version 3.3 or above." |
|
31 | IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2. | |
|
32 | When using Python 2.7, please install IPython 5.x LTS Long Term Support version. | |||
|
33 | Beginning with IPython 6.0, Python 3.3 and above is required. | |||
|
34 | ||||
|
35 | See IPython `README.rst` file for more information: | |||
|
36 | ||||
|
37 | https://github.com/ipython/ipython/blob/master/README.rst | |||
|
38 | ||||
|
39 | """ | |||
|
40 | ||||
32 | print(error, file=sys.stderr) |
|
41 | print(error, file=sys.stderr) | |
33 | sys.exit(1) |
|
42 | sys.exit(1) | |
34 |
|
43 | |||
35 | PY3 = (sys.version_info[0] >= 3) |
|
44 | PY3 = (sys.version_info[0] >= 3) | |
36 |
|
45 | |||
37 | # At least we're on the python version we need, move on. |
|
46 | # At least we're on the python version we need, move on. | |
38 |
|
47 | |||
39 | #------------------------------------------------------------------------------- |
|
48 | #------------------------------------------------------------------------------- | |
40 | # Imports |
|
49 | # Imports | |
41 | #------------------------------------------------------------------------------- |
|
50 | #------------------------------------------------------------------------------- | |
42 |
|
51 | |||
43 | # Stdlib imports |
|
52 | # Stdlib imports | |
44 | import os |
|
53 | import os | |
45 |
|
54 | |||
46 | from glob import glob |
|
55 | from glob import glob | |
47 |
|
56 | |||
48 | # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly |
|
57 | # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly | |
49 | # update it when the contents of directories change. |
|
58 | # update it when the contents of directories change. | |
50 | if os.path.exists('MANIFEST'): os.remove('MANIFEST') |
|
59 | if os.path.exists('MANIFEST'): os.remove('MANIFEST') | |
51 |
|
60 | |||
52 | from distutils.core import setup |
|
61 | from distutils.core import setup | |
53 |
|
62 | |||
54 | # Our own imports |
|
63 | # Our own imports | |
55 | from setupbase import target_update |
|
64 | from setupbase import target_update | |
56 |
|
65 | |||
57 | from setupbase import ( |
|
66 | from setupbase import ( | |
58 | setup_args, |
|
67 | setup_args, | |
59 | find_packages, |
|
68 | find_packages, | |
60 | find_package_data, |
|
69 | find_package_data, | |
61 | check_package_data_first, |
|
70 | check_package_data_first, | |
62 | find_entry_points, |
|
71 | find_entry_points, | |
63 | build_scripts_entrypt, |
|
72 | build_scripts_entrypt, | |
64 | find_data_files, |
|
73 | find_data_files, | |
65 | git_prebuild, |
|
74 | git_prebuild, | |
66 | install_symlinked, |
|
75 | install_symlinked, | |
67 | install_lib_symlink, |
|
76 | install_lib_symlink, | |
68 | install_scripts_for_symlink, |
|
77 | install_scripts_for_symlink, | |
69 | unsymlink, |
|
78 | unsymlink, | |
70 | ) |
|
79 | ) | |
71 |
|
80 | |||
72 | isfile = os.path.isfile |
|
81 | isfile = os.path.isfile | |
73 | pjoin = os.path.join |
|
82 | pjoin = os.path.join | |
74 |
|
83 | |||
75 | #------------------------------------------------------------------------------- |
|
84 | #------------------------------------------------------------------------------- | |
76 | # Handle OS specific things |
|
85 | # Handle OS specific things | |
77 | #------------------------------------------------------------------------------- |
|
86 | #------------------------------------------------------------------------------- | |
78 |
|
87 | |||
79 | if os.name in ('nt','dos'): |
|
88 | if os.name in ('nt','dos'): | |
80 | os_name = 'windows' |
|
89 | os_name = 'windows' | |
81 | else: |
|
90 | else: | |
82 | os_name = os.name |
|
91 | os_name = os.name | |
83 |
|
92 | |||
84 | # Under Windows, 'sdist' has not been supported. Now that the docs build with |
|
93 | # Under Windows, 'sdist' has not been supported. Now that the docs build with | |
85 | # Sphinx it might work, but let's not turn it on until someone confirms that it |
|
94 | # Sphinx it might work, but let's not turn it on until someone confirms that it | |
86 | # actually works. |
|
95 | # actually works. | |
87 | if os_name == 'windows' and 'sdist' in sys.argv: |
|
96 | if os_name == 'windows' and 'sdist' in sys.argv: | |
88 | print('The sdist command is not available under Windows. Exiting.') |
|
97 | print('The sdist command is not available under Windows. Exiting.') | |
89 | sys.exit(1) |
|
98 | sys.exit(1) | |
90 |
|
99 | |||
91 |
|
100 | |||
92 | #------------------------------------------------------------------------------- |
|
101 | #------------------------------------------------------------------------------- | |
93 | # Things related to the IPython documentation |
|
102 | # Things related to the IPython documentation | |
94 | #------------------------------------------------------------------------------- |
|
103 | #------------------------------------------------------------------------------- | |
95 |
|
104 | |||
96 | # update the manuals when building a source dist |
|
105 | # update the manuals when building a source dist | |
97 | if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'): |
|
106 | if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'): | |
98 |
|
107 | |||
99 | # List of things to be updated. Each entry is a triplet of args for |
|
108 | # List of things to be updated. Each entry is a triplet of args for | |
100 | # target_update() |
|
109 | # target_update() | |
101 | to_update = [ |
|
110 | to_update = [ | |
102 | ('docs/man/ipython.1.gz', |
|
111 | ('docs/man/ipython.1.gz', | |
103 | ['docs/man/ipython.1'], |
|
112 | ['docs/man/ipython.1'], | |
104 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz'), |
|
113 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz'), | |
105 | ] |
|
114 | ] | |
106 |
|
115 | |||
107 |
|
116 | |||
108 | [ target_update(*t) for t in to_update ] |
|
117 | [ target_update(*t) for t in to_update ] | |
109 |
|
118 | |||
110 | #--------------------------------------------------------------------------- |
|
119 | #--------------------------------------------------------------------------- | |
111 | # Find all the packages, package data, and data_files |
|
120 | # Find all the packages, package data, and data_files | |
112 | #--------------------------------------------------------------------------- |
|
121 | #--------------------------------------------------------------------------- | |
113 |
|
122 | |||
114 | packages = find_packages() |
|
123 | packages = find_packages() | |
115 | package_data = find_package_data() |
|
124 | package_data = find_package_data() | |
116 |
|
125 | |||
117 | data_files = find_data_files() |
|
126 | data_files = find_data_files() | |
118 |
|
127 | |||
119 | setup_args['packages'] = packages |
|
128 | setup_args['packages'] = packages | |
120 | setup_args['package_data'] = package_data |
|
129 | setup_args['package_data'] = package_data | |
121 | setup_args['data_files'] = data_files |
|
130 | setup_args['data_files'] = data_files | |
122 |
|
131 | |||
123 | #--------------------------------------------------------------------------- |
|
132 | #--------------------------------------------------------------------------- | |
124 | # custom distutils commands |
|
133 | # custom distutils commands | |
125 | #--------------------------------------------------------------------------- |
|
134 | #--------------------------------------------------------------------------- | |
126 | # imports here, so they are after setuptools import if there was one |
|
135 | # imports here, so they are after setuptools import if there was one | |
127 | from distutils.command.sdist import sdist |
|
136 | from distutils.command.sdist import sdist | |
128 | from distutils.command.upload import upload |
|
137 | from distutils.command.upload import upload | |
129 |
|
138 | |||
130 | class UploadWindowsInstallers(upload): |
|
139 | class UploadWindowsInstallers(upload): | |
131 |
|
140 | |||
132 | description = "Upload Windows installers to PyPI (only used from tools/release_windows.py)" |
|
141 | description = "Upload Windows installers to PyPI (only used from tools/release_windows.py)" | |
133 | user_options = upload.user_options + [ |
|
142 | user_options = upload.user_options + [ | |
134 | ('files=', 'f', 'exe file (or glob) to upload') |
|
143 | ('files=', 'f', 'exe file (or glob) to upload') | |
135 | ] |
|
144 | ] | |
136 | def initialize_options(self): |
|
145 | def initialize_options(self): | |
137 | upload.initialize_options(self) |
|
146 | upload.initialize_options(self) | |
138 | meta = self.distribution.metadata |
|
147 | meta = self.distribution.metadata | |
139 | base = '{name}-{version}'.format( |
|
148 | base = '{name}-{version}'.format( | |
140 | name=meta.get_name(), |
|
149 | name=meta.get_name(), | |
141 | version=meta.get_version() |
|
150 | version=meta.get_version() | |
142 | ) |
|
151 | ) | |
143 | self.files = os.path.join('dist', '%s.*.exe' % base) |
|
152 | self.files = os.path.join('dist', '%s.*.exe' % base) | |
144 |
|
153 | |||
145 | def run(self): |
|
154 | def run(self): | |
146 | for dist_file in glob(self.files): |
|
155 | for dist_file in glob(self.files): | |
147 | self.upload_file('bdist_wininst', 'any', dist_file) |
|
156 | self.upload_file('bdist_wininst', 'any', dist_file) | |
148 |
|
157 | |||
149 | setup_args['cmdclass'] = { |
|
158 | setup_args['cmdclass'] = { | |
150 | 'build_py': \ |
|
159 | 'build_py': \ | |
151 | check_package_data_first(git_prebuild('IPython')), |
|
160 | check_package_data_first(git_prebuild('IPython')), | |
152 | 'sdist' : git_prebuild('IPython', sdist), |
|
161 | 'sdist' : git_prebuild('IPython', sdist), | |
153 | 'upload_wininst' : UploadWindowsInstallers, |
|
162 | 'upload_wininst' : UploadWindowsInstallers, | |
154 | 'symlink': install_symlinked, |
|
163 | 'symlink': install_symlinked, | |
155 | 'install_lib_symlink': install_lib_symlink, |
|
164 | 'install_lib_symlink': install_lib_symlink, | |
156 | 'install_scripts_sym': install_scripts_for_symlink, |
|
165 | 'install_scripts_sym': install_scripts_for_symlink, | |
157 | 'unsymlink': unsymlink, |
|
166 | 'unsymlink': unsymlink, | |
158 | } |
|
167 | } | |
159 |
|
168 | |||
160 |
|
169 | |||
161 | #--------------------------------------------------------------------------- |
|
170 | #--------------------------------------------------------------------------- | |
162 | # Handle scripts, dependencies, and setuptools specific things |
|
171 | # Handle scripts, dependencies, and setuptools specific things | |
163 | #--------------------------------------------------------------------------- |
|
172 | #--------------------------------------------------------------------------- | |
164 |
|
173 | |||
165 | # For some commands, use setuptools. Note that we do NOT list install here! |
|
174 | # For some commands, use setuptools. Note that we do NOT list install here! | |
166 | # If you want a setuptools-enhanced install, just run 'setupegg.py install' |
|
175 | # If you want a setuptools-enhanced install, just run 'setupegg.py install' | |
167 | needs_setuptools = set(('develop', 'release', 'bdist_egg', 'bdist_rpm', |
|
176 | needs_setuptools = set(('develop', 'release', 'bdist_egg', 'bdist_rpm', | |
168 | 'bdist', 'bdist_dumb', 'bdist_wininst', 'bdist_wheel', |
|
177 | 'bdist', 'bdist_dumb', 'bdist_wininst', 'bdist_wheel', | |
169 | 'egg_info', 'easy_install', 'upload', 'install_egg_info', |
|
178 | 'egg_info', 'easy_install', 'upload', 'install_egg_info', | |
170 | )) |
|
179 | )) | |
171 |
|
180 | |||
172 | if len(needs_setuptools.intersection(sys.argv)) > 0: |
|
181 | if len(needs_setuptools.intersection(sys.argv)) > 0: | |
173 | import setuptools |
|
182 | import setuptools | |
174 |
|
183 | |||
175 | # This dict is used for passing extra arguments that are setuptools |
|
184 | # This dict is used for passing extra arguments that are setuptools | |
176 | # specific to setup |
|
185 | # specific to setup | |
177 | setuptools_extra_args = {} |
|
186 | setuptools_extra_args = {} | |
178 |
|
187 | |||
179 | # setuptools requirements |
|
188 | # setuptools requirements | |
180 |
|
189 | |||
181 | extras_require = dict( |
|
190 | extras_require = dict( | |
182 | parallel = ['ipyparallel'], |
|
191 | parallel = ['ipyparallel'], | |
183 | qtconsole = ['qtconsole'], |
|
192 | qtconsole = ['qtconsole'], | |
184 | doc = ['Sphinx>=1.3'], |
|
193 | doc = ['Sphinx>=1.3'], | |
185 | test = ['nose>=0.10.1', 'requests', 'testpath', 'pygments', 'nbformat', 'ipykernel', 'numpy'], |
|
194 | test = ['nose>=0.10.1', 'requests', 'testpath', 'pygments', 'nbformat', 'ipykernel', 'numpy'], | |
186 | terminal = [], |
|
195 | terminal = [], | |
187 | kernel = ['ipykernel'], |
|
196 | kernel = ['ipykernel'], | |
188 | nbformat = ['nbformat'], |
|
197 | nbformat = ['nbformat'], | |
189 | notebook = ['notebook', 'ipywidgets'], |
|
198 | notebook = ['notebook', 'ipywidgets'], | |
190 | nbconvert = ['nbconvert'], |
|
199 | nbconvert = ['nbconvert'], | |
191 | ) |
|
200 | ) | |
192 |
|
201 | |||
193 | install_requires = [ |
|
202 | install_requires = [ | |
194 | 'setuptools>=18.5', |
|
203 | 'setuptools>=18.5', | |
195 | 'decorator', |
|
204 | 'decorator', | |
196 | 'pickleshare', |
|
205 | 'pickleshare', | |
197 | 'simplegeneric>0.8', |
|
206 | 'simplegeneric>0.8', | |
198 | 'traitlets>=4.2', |
|
207 | 'traitlets>=4.2', | |
199 | 'prompt_toolkit>=1.0.3,<2.0.0', |
|
208 | 'prompt_toolkit>=1.0.3,<2.0.0', | |
200 | 'pygments', |
|
209 | 'pygments', | |
201 | ] |
|
210 | ] | |
202 |
|
211 | |||
203 | # Platform-specific dependencies: |
|
212 | # Platform-specific dependencies: | |
204 | # This is the correct way to specify these, |
|
213 | # This is the correct way to specify these, | |
205 | # but requires pip >= 6. pip < 6 ignores these. |
|
214 | # but requires pip >= 6. pip < 6 ignores these. | |
206 |
|
215 | |||
207 | extras_require.update({ |
|
216 | extras_require.update({ | |
208 | ':python_version == "2.7"': ['backports.shutil_get_terminal_size'], |
|
217 | ':python_version == "2.7"': ['backports.shutil_get_terminal_size'], | |
209 | ':python_version == "2.7" or python_version == "3.3"': ['pathlib2'], |
|
218 | ':python_version == "2.7" or python_version == "3.3"': ['pathlib2'], | |
210 | ':sys_platform != "win32"': ['pexpect'], |
|
219 | ':sys_platform != "win32"': ['pexpect'], | |
211 | ':sys_platform == "darwin"': ['appnope'], |
|
220 | ':sys_platform == "darwin"': ['appnope'], | |
212 | ':sys_platform == "win32"': ['colorama', 'win_unicode_console>=0.5'], |
|
221 | ':sys_platform == "win32"': ['colorama', 'win_unicode_console>=0.5'], | |
213 | 'test:python_version == "2.7"': ['mock'], |
|
222 | 'test:python_version == "2.7"': ['mock'], | |
214 | }) |
|
223 | }) | |
215 | # FIXME: re-specify above platform dependencies for pip < 6 |
|
224 | # FIXME: re-specify above platform dependencies for pip < 6 | |
216 | # These would result in non-portable bdists. |
|
225 | # These would result in non-portable bdists. | |
217 | if not any(arg.startswith('bdist') for arg in sys.argv): |
|
226 | if not any(arg.startswith('bdist') for arg in sys.argv): | |
218 | if sys.version_info < (3, 3): |
|
227 | if sys.version_info < (3, 3): | |
219 | extras_require['test'].append('mock') |
|
228 | extras_require['test'].append('mock') | |
220 |
|
229 | |||
221 | if sys.platform == 'darwin': |
|
230 | if sys.platform == 'darwin': | |
222 | install_requires.extend(['appnope']) |
|
231 | install_requires.extend(['appnope']) | |
223 |
|
232 | |||
224 | if not sys.platform.startswith('win'): |
|
233 | if not sys.platform.startswith('win'): | |
225 | install_requires.append('pexpect') |
|
234 | install_requires.append('pexpect') | |
226 |
|
235 | |||
227 | # workaround pypa/setuptools#147, where setuptools misspells |
|
236 | # workaround pypa/setuptools#147, where setuptools misspells | |
228 | # platform_python_implementation as python_implementation |
|
237 | # platform_python_implementation as python_implementation | |
229 | if 'setuptools' in sys.modules: |
|
238 | if 'setuptools' in sys.modules: | |
230 | for key in list(extras_require): |
|
239 | for key in list(extras_require): | |
231 | if 'platform_python_implementation' in key: |
|
240 | if 'platform_python_implementation' in key: | |
232 | new_key = key.replace('platform_python_implementation', 'python_implementation') |
|
241 | new_key = key.replace('platform_python_implementation', 'python_implementation') | |
233 | extras_require[new_key] = extras_require.pop(key) |
|
242 | extras_require[new_key] = extras_require.pop(key) | |
234 |
|
243 | |||
235 | everything = set() |
|
244 | everything = set() | |
236 | for key, deps in extras_require.items(): |
|
245 | for key, deps in extras_require.items(): | |
237 | if ':' not in key: |
|
246 | if ':' not in key: | |
238 | everything.update(deps) |
|
247 | everything.update(deps) | |
239 | extras_require['all'] = everything |
|
248 | extras_require['all'] = everything | |
240 |
|
249 | |||
241 | if 'setuptools' in sys.modules: |
|
250 | if 'setuptools' in sys.modules: | |
242 | setuptools_extra_args['python_requires'] = '>=3.3' |
|
251 | setuptools_extra_args['python_requires'] = '>=3.3' | |
243 | setuptools_extra_args['zip_safe'] = False |
|
252 | setuptools_extra_args['zip_safe'] = False | |
244 | setuptools_extra_args['entry_points'] = { |
|
253 | setuptools_extra_args['entry_points'] = { | |
245 | 'console_scripts': find_entry_points(), |
|
254 | 'console_scripts': find_entry_points(), | |
246 | 'pygments.lexers': [ |
|
255 | 'pygments.lexers': [ | |
247 | 'ipythonconsole = IPython.lib.lexers:IPythonConsoleLexer', |
|
256 | 'ipythonconsole = IPython.lib.lexers:IPythonConsoleLexer', | |
248 | 'ipython = IPython.lib.lexers:IPythonLexer', |
|
257 | 'ipython = IPython.lib.lexers:IPythonLexer', | |
249 | 'ipython3 = IPython.lib.lexers:IPython3Lexer', |
|
258 | 'ipython3 = IPython.lib.lexers:IPython3Lexer', | |
250 | ], |
|
259 | ], | |
251 | } |
|
260 | } | |
252 | setup_args['extras_require'] = extras_require |
|
261 | setup_args['extras_require'] = extras_require | |
253 | requires = setup_args['install_requires'] = install_requires |
|
262 | requires = setup_args['install_requires'] = install_requires | |
254 |
|
263 | |||
255 | # Script to be run by the windows binary installer after the default setup |
|
264 | # Script to be run by the windows binary installer after the default setup | |
256 | # routine, to add shortcuts and similar windows-only things. Windows |
|
265 | # routine, to add shortcuts and similar windows-only things. Windows | |
257 | # post-install scripts MUST reside in the scripts/ dir, otherwise distutils |
|
266 | # post-install scripts MUST reside in the scripts/ dir, otherwise distutils | |
258 | # doesn't find them. |
|
267 | # doesn't find them. | |
259 | if 'bdist_wininst' in sys.argv: |
|
268 | if 'bdist_wininst' in sys.argv: | |
260 | if len(sys.argv) > 2 and \ |
|
269 | if len(sys.argv) > 2 and \ | |
261 | ('sdist' in sys.argv or 'bdist_rpm' in sys.argv): |
|
270 | ('sdist' in sys.argv or 'bdist_rpm' in sys.argv): | |
262 | print("ERROR: bdist_wininst must be run alone. Exiting.", file=sys.stderr) |
|
271 | print("ERROR: bdist_wininst must be run alone. Exiting.", file=sys.stderr) | |
263 | sys.exit(1) |
|
272 | sys.exit(1) | |
264 | setup_args['data_files'].append( |
|
273 | setup_args['data_files'].append( | |
265 | ['Scripts', ('scripts/ipython.ico', 'scripts/ipython_nb.ico')]) |
|
274 | ['Scripts', ('scripts/ipython.ico', 'scripts/ipython_nb.ico')]) | |
266 | setup_args['scripts'] = [pjoin('scripts','ipython_win_post_install.py')] |
|
275 | setup_args['scripts'] = [pjoin('scripts','ipython_win_post_install.py')] | |
267 | setup_args['options'] = {"bdist_wininst": |
|
276 | setup_args['options'] = {"bdist_wininst": | |
268 | {"install_script": |
|
277 | {"install_script": | |
269 | "ipython_win_post_install.py"}} |
|
278 | "ipython_win_post_install.py"}} | |
270 |
|
279 | |||
271 | else: |
|
280 | else: | |
272 | # scripts has to be a non-empty list, or install_scripts isn't called |
|
281 | # scripts has to be a non-empty list, or install_scripts isn't called | |
273 | setup_args['scripts'] = [e.split('=')[0].strip() for e in find_entry_points()] |
|
282 | setup_args['scripts'] = [e.split('=')[0].strip() for e in find_entry_points()] | |
274 |
|
283 | |||
275 | setup_args['cmdclass']['build_scripts'] = build_scripts_entrypt |
|
284 | setup_args['cmdclass']['build_scripts'] = build_scripts_entrypt | |
276 |
|
285 | |||
277 | #--------------------------------------------------------------------------- |
|
286 | #--------------------------------------------------------------------------- | |
278 | # Do the actual setup now |
|
287 | # Do the actual setup now | |
279 | #--------------------------------------------------------------------------- |
|
288 | #--------------------------------------------------------------------------- | |
280 |
|
289 | |||
281 | setup_args.update(setuptools_extra_args) |
|
290 | setup_args.update(setuptools_extra_args) | |
282 |
|
291 | |||
283 |
|
292 | |||
284 |
|
293 | |||
285 | def main(): |
|
294 | def main(): | |
286 | setup(**setup_args) |
|
295 | setup(**setup_args) | |
287 |
|
296 | |||
288 | if __name__ == '__main__': |
|
297 | if __name__ == '__main__': | |
289 | main() |
|
298 | main() |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now