Show More
@@ -1,486 +1,476 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | An application for IPython. |
|
3 | An application for IPython. | |
4 |
|
4 | |||
5 | All top-level applications should use the classes in this module for |
|
5 | All top-level applications should use the classes in this module for | |
6 | handling configuration and creating configurables. |
|
6 | handling configuration and creating configurables. | |
7 |
|
7 | |||
8 | The job of an :class:`Application` is to create the master configuration |
|
8 | The job of an :class:`Application` is to create the master configuration | |
9 | object and then create the configurable objects, passing the config to them. |
|
9 | object and then create the configurable objects, passing the config to them. | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | # Copyright (c) IPython Development Team. |
|
12 | # Copyright (c) IPython Development Team. | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 |
|
14 | |||
15 | import atexit |
|
15 | import atexit | |
16 | from copy import deepcopy |
|
16 | from copy import deepcopy | |
17 | import glob |
|
17 | import glob | |
18 | import logging |
|
18 | import logging | |
19 | import os |
|
19 | import os | |
20 | import shutil |
|
20 | import shutil | |
21 | import sys |
|
21 | import sys | |
22 |
|
22 | |||
23 | from pathlib import Path |
|
23 | from pathlib import Path | |
24 |
|
24 | |||
25 | from traitlets.config.application import Application, catch_config_error |
|
25 | from traitlets.config.application import Application, catch_config_error | |
26 | from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader |
|
26 | from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader | |
27 | from IPython.core import release, crashhandler |
|
27 | from IPython.core import release, crashhandler | |
28 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
28 | from IPython.core.profiledir import ProfileDir, ProfileDirError | |
29 | from IPython.paths import get_ipython_dir, get_ipython_package_dir |
|
29 | from IPython.paths import get_ipython_dir, get_ipython_package_dir | |
30 | from IPython.utils.path import ensure_dir_exists |
|
30 | from IPython.utils.path import ensure_dir_exists | |
31 | from traitlets import ( |
|
31 | from traitlets import ( | |
32 | List, Unicode, Type, Bool, Set, Instance, Undefined, |
|
32 | List, Unicode, Type, Bool, Set, Instance, Undefined, | |
33 | default, observe, |
|
33 | default, observe, | |
34 | ) |
|
34 | ) | |
35 |
|
35 | |||
36 | if os.name == "nt": |
|
36 | if os.name == "nt": | |
37 | programdata = Path(os.environ.get("PROGRAMDATA", None)) |
|
37 | programdata = Path(os.environ.get("PROGRAMDATA", None)) | |
38 | if programdata: |
|
38 | if programdata: | |
39 | SYSTEM_CONFIG_DIRS = [str(programdata / "ipython")] |
|
39 | SYSTEM_CONFIG_DIRS = [str(programdata / "ipython")] | |
40 | else: # PROGRAMDATA is not defined by default on XP. |
|
40 | else: # PROGRAMDATA is not defined by default on XP. | |
41 | SYSTEM_CONFIG_DIRS = [] |
|
41 | SYSTEM_CONFIG_DIRS = [] | |
42 | else: |
|
42 | else: | |
43 | SYSTEM_CONFIG_DIRS = [ |
|
43 | SYSTEM_CONFIG_DIRS = [ | |
44 | "/usr/local/etc/ipython", |
|
44 | "/usr/local/etc/ipython", | |
45 | "/etc/ipython", |
|
45 | "/etc/ipython", | |
46 | ] |
|
46 | ] | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | ENV_CONFIG_DIRS = [] |
|
49 | ENV_CONFIG_DIRS = [] | |
50 | _env_config_dir = os.path.join(sys.prefix, 'etc', 'ipython') |
|
50 | _env_config_dir = os.path.join(sys.prefix, 'etc', 'ipython') | |
51 | if _env_config_dir not in SYSTEM_CONFIG_DIRS: |
|
51 | if _env_config_dir not in SYSTEM_CONFIG_DIRS: | |
52 | # only add ENV_CONFIG if sys.prefix is not already included |
|
52 | # only add ENV_CONFIG if sys.prefix is not already included | |
53 | ENV_CONFIG_DIRS.append(_env_config_dir) |
|
53 | ENV_CONFIG_DIRS.append(_env_config_dir) | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | _envvar = os.environ.get('IPYTHON_SUPPRESS_CONFIG_ERRORS') |
|
56 | _envvar = os.environ.get('IPYTHON_SUPPRESS_CONFIG_ERRORS') | |
57 | if _envvar in {None, ''}: |
|
57 | if _envvar in {None, ''}: | |
58 | IPYTHON_SUPPRESS_CONFIG_ERRORS = None |
|
58 | IPYTHON_SUPPRESS_CONFIG_ERRORS = None | |
59 | else: |
|
59 | else: | |
60 | if _envvar.lower() in {'1','true'}: |
|
60 | if _envvar.lower() in {'1','true'}: | |
61 | IPYTHON_SUPPRESS_CONFIG_ERRORS = True |
|
61 | IPYTHON_SUPPRESS_CONFIG_ERRORS = True | |
62 | elif _envvar.lower() in {'0','false'} : |
|
62 | elif _envvar.lower() in {'0','false'} : | |
63 | IPYTHON_SUPPRESS_CONFIG_ERRORS = False |
|
63 | IPYTHON_SUPPRESS_CONFIG_ERRORS = False | |
64 | else: |
|
64 | else: | |
65 | sys.exit("Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar ) |
|
65 | sys.exit("Unsupported value for environment variable: 'IPYTHON_SUPPRESS_CONFIG_ERRORS' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar ) | |
66 |
|
66 | |||
67 | # aliases and flags |
|
67 | # aliases and flags | |
68 |
|
68 | |||
69 | base_aliases = {} |
|
69 | base_aliases = {} | |
70 | if isinstance(Application.aliases, dict): |
|
70 | if isinstance(Application.aliases, dict): | |
71 | # traitlets 5 |
|
71 | # traitlets 5 | |
72 | base_aliases.update(Application.aliases) |
|
72 | base_aliases.update(Application.aliases) | |
73 | base_aliases.update( |
|
73 | base_aliases.update( | |
74 | { |
|
74 | { | |
75 | "profile-dir": "ProfileDir.location", |
|
75 | "profile-dir": "ProfileDir.location", | |
76 | "profile": "BaseIPythonApplication.profile", |
|
76 | "profile": "BaseIPythonApplication.profile", | |
77 | "ipython-dir": "BaseIPythonApplication.ipython_dir", |
|
77 | "ipython-dir": "BaseIPythonApplication.ipython_dir", | |
78 | "log-level": "Application.log_level", |
|
78 | "log-level": "Application.log_level", | |
79 | "config": "BaseIPythonApplication.extra_config_file", |
|
79 | "config": "BaseIPythonApplication.extra_config_file", | |
80 | } |
|
80 | } | |
81 | ) |
|
81 | ) | |
82 |
|
82 | |||
83 | base_flags = dict() |
|
83 | base_flags = dict() | |
84 | if isinstance(Application.flags, dict): |
|
84 | if isinstance(Application.flags, dict): | |
85 | # traitlets 5 |
|
85 | # traitlets 5 | |
86 | base_flags.update(Application.flags) |
|
86 | base_flags.update(Application.flags) | |
87 | base_flags.update( |
|
87 | base_flags.update( | |
88 | dict( |
|
88 | dict( | |
89 | debug=( |
|
89 | debug=( | |
90 | {"Application": {"log_level": logging.DEBUG}}, |
|
90 | {"Application": {"log_level": logging.DEBUG}}, | |
91 | "set log level to logging.DEBUG (maximize logging output)", |
|
91 | "set log level to logging.DEBUG (maximize logging output)", | |
92 | ), |
|
92 | ), | |
93 | quiet=( |
|
93 | quiet=( | |
94 | {"Application": {"log_level": logging.CRITICAL}}, |
|
94 | {"Application": {"log_level": logging.CRITICAL}}, | |
95 | "set log level to logging.CRITICAL (minimize logging output)", |
|
95 | "set log level to logging.CRITICAL (minimize logging output)", | |
96 | ), |
|
96 | ), | |
97 | init=( |
|
97 | init=( | |
98 | { |
|
98 | { | |
99 | "BaseIPythonApplication": { |
|
99 | "BaseIPythonApplication": { | |
100 | "copy_config_files": True, |
|
100 | "copy_config_files": True, | |
101 | "auto_create": True, |
|
101 | "auto_create": True, | |
102 | } |
|
102 | } | |
103 | }, |
|
103 | }, | |
104 | """Initialize profile with default config files. This is equivalent |
|
104 | """Initialize profile with default config files. This is equivalent | |
105 | to running `ipython profile create <profile>` prior to startup. |
|
105 | to running `ipython profile create <profile>` prior to startup. | |
106 | """, |
|
106 | """, | |
107 | ), |
|
107 | ), | |
108 | ) |
|
108 | ) | |
109 | ) |
|
109 | ) | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | class ProfileAwareConfigLoader(PyFileConfigLoader): |
|
112 | class ProfileAwareConfigLoader(PyFileConfigLoader): | |
113 | """A Python file config loader that is aware of IPython profiles.""" |
|
113 | """A Python file config loader that is aware of IPython profiles.""" | |
114 | def load_subconfig(self, fname, path=None, profile=None): |
|
114 | def load_subconfig(self, fname, path=None, profile=None): | |
115 | if profile is not None: |
|
115 | if profile is not None: | |
116 | try: |
|
116 | try: | |
117 | profile_dir = ProfileDir.find_profile_dir_by_name( |
|
117 | profile_dir = ProfileDir.find_profile_dir_by_name( | |
118 | get_ipython_dir(), |
|
118 | get_ipython_dir(), | |
119 | profile, |
|
119 | profile, | |
120 | ) |
|
120 | ) | |
121 | except ProfileDirError: |
|
121 | except ProfileDirError: | |
122 | return |
|
122 | return | |
123 | path = profile_dir.location |
|
123 | path = profile_dir.location | |
124 | return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path) |
|
124 | return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path) | |
125 |
|
125 | |||
126 | class BaseIPythonApplication(Application): |
|
126 | class BaseIPythonApplication(Application): | |
127 |
|
127 | |||
128 | name = u'ipython' |
|
128 | name = u'ipython' | |
129 | description = Unicode(u'IPython: an enhanced interactive Python shell.') |
|
129 | description = Unicode(u'IPython: an enhanced interactive Python shell.') | |
130 | version = Unicode(release.version) |
|
130 | version = Unicode(release.version) | |
131 |
|
131 | |||
132 | aliases = base_aliases |
|
132 | aliases = base_aliases | |
133 | flags = base_flags |
|
133 | flags = base_flags | |
134 | classes = List([ProfileDir]) |
|
134 | classes = List([ProfileDir]) | |
135 |
|
135 | |||
136 | # enable `load_subconfig('cfg.py', profile='name')` |
|
136 | # enable `load_subconfig('cfg.py', profile='name')` | |
137 | python_config_loader_class = ProfileAwareConfigLoader |
|
137 | python_config_loader_class = ProfileAwareConfigLoader | |
138 |
|
138 | |||
139 | # Track whether the config_file has changed, |
|
139 | # Track whether the config_file has changed, | |
140 | # because some logic happens only if we aren't using the default. |
|
140 | # because some logic happens only if we aren't using the default. | |
141 | config_file_specified = Set() |
|
141 | config_file_specified = Set() | |
142 |
|
142 | |||
143 | config_file_name = Unicode() |
|
143 | config_file_name = Unicode() | |
144 | @default('config_file_name') |
|
144 | @default('config_file_name') | |
145 | def _config_file_name_default(self): |
|
145 | def _config_file_name_default(self): | |
146 | return self.name.replace('-','_') + u'_config.py' |
|
146 | return self.name.replace('-','_') + u'_config.py' | |
147 | @observe('config_file_name') |
|
147 | @observe('config_file_name') | |
148 | def _config_file_name_changed(self, change): |
|
148 | def _config_file_name_changed(self, change): | |
149 | if change['new'] != change['old']: |
|
149 | if change['new'] != change['old']: | |
150 | self.config_file_specified.add(change['new']) |
|
150 | self.config_file_specified.add(change['new']) | |
151 |
|
151 | |||
152 | # The directory that contains IPython's builtin profiles. |
|
152 | # The directory that contains IPython's builtin profiles. | |
153 | builtin_profile_dir = Unicode( |
|
153 | builtin_profile_dir = Unicode( | |
154 | os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default') |
|
154 | os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default') | |
155 | ) |
|
155 | ) | |
156 |
|
156 | |||
157 | config_file_paths = List(Unicode()) |
|
157 | config_file_paths = List(Unicode()) | |
158 | @default('config_file_paths') |
|
158 | @default('config_file_paths') | |
159 | def _config_file_paths_default(self): |
|
159 | def _config_file_paths_default(self): | |
160 | return [os.getcwd()] |
|
160 | return [os.getcwd()] | |
161 |
|
161 | |||
162 | extra_config_file = Unicode( |
|
162 | extra_config_file = Unicode( | |
163 | help="""Path to an extra config file to load. |
|
163 | help="""Path to an extra config file to load. | |
164 |
|
164 | |||
165 | If specified, load this config file in addition to any other IPython config. |
|
165 | If specified, load this config file in addition to any other IPython config. | |
166 | """).tag(config=True) |
|
166 | """).tag(config=True) | |
167 | @observe('extra_config_file') |
|
167 | @observe('extra_config_file') | |
168 | def _extra_config_file_changed(self, change): |
|
168 | def _extra_config_file_changed(self, change): | |
169 | old = change['old'] |
|
169 | old = change['old'] | |
170 | new = change['new'] |
|
170 | new = change['new'] | |
171 | try: |
|
171 | try: | |
172 | self.config_files.remove(old) |
|
172 | self.config_files.remove(old) | |
173 | except ValueError: |
|
173 | except ValueError: | |
174 | pass |
|
174 | pass | |
175 | self.config_file_specified.add(new) |
|
175 | self.config_file_specified.add(new) | |
176 | self.config_files.append(new) |
|
176 | self.config_files.append(new) | |
177 |
|
177 | |||
178 | profile = Unicode(u'default', |
|
178 | profile = Unicode(u'default', | |
179 | help="""The IPython profile to use.""" |
|
179 | help="""The IPython profile to use.""" | |
180 | ).tag(config=True) |
|
180 | ).tag(config=True) | |
181 |
|
181 | |||
182 | @observe('profile') |
|
182 | @observe('profile') | |
183 | def _profile_changed(self, change): |
|
183 | def _profile_changed(self, change): | |
184 | self.builtin_profile_dir = os.path.join( |
|
184 | self.builtin_profile_dir = os.path.join( | |
185 | get_ipython_package_dir(), u'config', u'profile', change['new'] |
|
185 | get_ipython_package_dir(), u'config', u'profile', change['new'] | |
186 | ) |
|
186 | ) | |
187 |
|
187 | |||
188 | ipython_dir = Unicode( |
|
188 | ipython_dir = Unicode( | |
189 | help=""" |
|
189 | help=""" | |
190 | The name of the IPython directory. This directory is used for logging |
|
190 | The name of the IPython directory. This directory is used for logging | |
191 | configuration (through profiles), history storage, etc. The default |
|
191 | configuration (through profiles), history storage, etc. The default | |
192 | is usually $HOME/.ipython. This option can also be specified through |
|
192 | is usually $HOME/.ipython. This option can also be specified through | |
193 | the environment variable IPYTHONDIR. |
|
193 | the environment variable IPYTHONDIR. | |
194 | """ |
|
194 | """ | |
195 | ).tag(config=True) |
|
195 | ).tag(config=True) | |
196 | @default('ipython_dir') |
|
196 | @default('ipython_dir') | |
197 | def _ipython_dir_default(self): |
|
197 | def _ipython_dir_default(self): | |
198 | d = get_ipython_dir() |
|
198 | d = get_ipython_dir() | |
199 | self._ipython_dir_changed({ |
|
199 | self._ipython_dir_changed({ | |
200 | 'name': 'ipython_dir', |
|
200 | 'name': 'ipython_dir', | |
201 | 'old': d, |
|
201 | 'old': d, | |
202 | 'new': d, |
|
202 | 'new': d, | |
203 | }) |
|
203 | }) | |
204 | return d |
|
204 | return d | |
205 |
|
205 | |||
206 | _in_init_profile_dir = False |
|
206 | _in_init_profile_dir = False | |
207 | profile_dir = Instance(ProfileDir, allow_none=True) |
|
207 | profile_dir = Instance(ProfileDir, allow_none=True) | |
208 | @default('profile_dir') |
|
208 | @default('profile_dir') | |
209 | def _profile_dir_default(self): |
|
209 | def _profile_dir_default(self): | |
210 | # avoid recursion |
|
210 | # avoid recursion | |
211 | if self._in_init_profile_dir: |
|
211 | if self._in_init_profile_dir: | |
212 | return |
|
212 | return | |
213 | # profile_dir requested early, force initialization |
|
213 | # profile_dir requested early, force initialization | |
214 | self.init_profile_dir() |
|
214 | self.init_profile_dir() | |
215 | return self.profile_dir |
|
215 | return self.profile_dir | |
216 |
|
216 | |||
217 | overwrite = Bool(False, |
|
217 | overwrite = Bool(False, | |
218 | help="""Whether to overwrite existing config files when copying""" |
|
218 | help="""Whether to overwrite existing config files when copying""" | |
219 | ).tag(config=True) |
|
219 | ).tag(config=True) | |
220 | auto_create = Bool(False, |
|
220 | auto_create = Bool(False, | |
221 | help="""Whether to create profile dir if it doesn't exist""" |
|
221 | help="""Whether to create profile dir if it doesn't exist""" | |
222 | ).tag(config=True) |
|
222 | ).tag(config=True) | |
223 |
|
223 | |||
224 | config_files = List(Unicode()) |
|
224 | config_files = List(Unicode()) | |
225 | @default('config_files') |
|
225 | @default('config_files') | |
226 | def _config_files_default(self): |
|
226 | def _config_files_default(self): | |
227 | return [self.config_file_name] |
|
227 | return [self.config_file_name] | |
228 |
|
228 | |||
229 | copy_config_files = Bool(False, |
|
229 | copy_config_files = Bool(False, | |
230 | help="""Whether to install the default config files into the profile dir. |
|
230 | help="""Whether to install the default config files into the profile dir. | |
231 | If a new profile is being created, and IPython contains config files for that |
|
231 | If a new profile is being created, and IPython contains config files for that | |
232 | profile, then they will be staged into the new directory. Otherwise, |
|
232 | profile, then they will be staged into the new directory. Otherwise, | |
233 | default config files will be automatically generated. |
|
233 | default config files will be automatically generated. | |
234 | """).tag(config=True) |
|
234 | """).tag(config=True) | |
235 |
|
235 | |||
236 | verbose_crash = Bool(False, |
|
236 | verbose_crash = Bool(False, | |
237 | help="""Create a massive crash report when IPython encounters what may be an |
|
237 | help="""Create a massive crash report when IPython encounters what may be an | |
238 | internal error. The default is to append a short message to the |
|
238 | internal error. The default is to append a short message to the | |
239 | usual traceback""").tag(config=True) |
|
239 | usual traceback""").tag(config=True) | |
240 |
|
240 | |||
241 | # The class to use as the crash handler. |
|
241 | # The class to use as the crash handler. | |
242 | crash_handler_class = Type(crashhandler.CrashHandler) |
|
242 | crash_handler_class = Type(crashhandler.CrashHandler) | |
243 |
|
243 | |||
244 | @catch_config_error |
|
244 | @catch_config_error | |
245 | def __init__(self, **kwargs): |
|
245 | def __init__(self, **kwargs): | |
246 | super(BaseIPythonApplication, self).__init__(**kwargs) |
|
246 | super(BaseIPythonApplication, self).__init__(**kwargs) | |
247 | # ensure current working directory exists |
|
247 | # ensure current working directory exists | |
248 | try: |
|
248 | try: | |
249 | os.getcwd() |
|
249 | os.getcwd() | |
250 | except: |
|
250 | except: | |
251 | # exit if cwd doesn't exist |
|
251 | # exit if cwd doesn't exist | |
252 | self.log.error("Current working directory doesn't exist.") |
|
252 | self.log.error("Current working directory doesn't exist.") | |
253 | self.exit(1) |
|
253 | self.exit(1) | |
254 |
|
254 | |||
255 | #------------------------------------------------------------------------- |
|
255 | #------------------------------------------------------------------------- | |
256 | # Various stages of Application creation |
|
256 | # Various stages of Application creation | |
257 | #------------------------------------------------------------------------- |
|
257 | #------------------------------------------------------------------------- | |
258 |
|
258 | |||
259 | deprecated_subcommands = {} |
|
|||
260 |
|
||||
261 | def initialize_subcommand(self, subc, argv=None): |
|
|||
262 | if subc in self.deprecated_subcommands: |
|
|||
263 | self.log.warning("Subcommand `ipython {sub}` is deprecated and will be removed " |
|
|||
264 | "in future versions.".format(sub=subc)) |
|
|||
265 | self.log.warning("You likely want to use `jupyter {sub}` in the " |
|
|||
266 | "future".format(sub=subc)) |
|
|||
267 | return super(BaseIPythonApplication, self).initialize_subcommand(subc, argv) |
|
|||
268 |
|
||||
269 | def init_crash_handler(self): |
|
259 | def init_crash_handler(self): | |
270 | """Create a crash handler, typically setting sys.excepthook to it.""" |
|
260 | """Create a crash handler, typically setting sys.excepthook to it.""" | |
271 | self.crash_handler = self.crash_handler_class(self) |
|
261 | self.crash_handler = self.crash_handler_class(self) | |
272 | sys.excepthook = self.excepthook |
|
262 | sys.excepthook = self.excepthook | |
273 | def unset_crashhandler(): |
|
263 | def unset_crashhandler(): | |
274 | sys.excepthook = sys.__excepthook__ |
|
264 | sys.excepthook = sys.__excepthook__ | |
275 | atexit.register(unset_crashhandler) |
|
265 | atexit.register(unset_crashhandler) | |
276 |
|
266 | |||
277 | def excepthook(self, etype, evalue, tb): |
|
267 | def excepthook(self, etype, evalue, tb): | |
278 | """this is sys.excepthook after init_crashhandler |
|
268 | """this is sys.excepthook after init_crashhandler | |
279 |
|
269 | |||
280 | set self.verbose_crash=True to use our full crashhandler, instead of |
|
270 | set self.verbose_crash=True to use our full crashhandler, instead of | |
281 | a regular traceback with a short message (crash_handler_lite) |
|
271 | a regular traceback with a short message (crash_handler_lite) | |
282 | """ |
|
272 | """ | |
283 |
|
273 | |||
284 | if self.verbose_crash: |
|
274 | if self.verbose_crash: | |
285 | return self.crash_handler(etype, evalue, tb) |
|
275 | return self.crash_handler(etype, evalue, tb) | |
286 | else: |
|
276 | else: | |
287 | return crashhandler.crash_handler_lite(etype, evalue, tb) |
|
277 | return crashhandler.crash_handler_lite(etype, evalue, tb) | |
288 |
|
278 | |||
289 | @observe('ipython_dir') |
|
279 | @observe('ipython_dir') | |
290 | def _ipython_dir_changed(self, change): |
|
280 | def _ipython_dir_changed(self, change): | |
291 | old = change['old'] |
|
281 | old = change['old'] | |
292 | new = change['new'] |
|
282 | new = change['new'] | |
293 | if old is not Undefined: |
|
283 | if old is not Undefined: | |
294 | str_old = os.path.abspath(old) |
|
284 | str_old = os.path.abspath(old) | |
295 | if str_old in sys.path: |
|
285 | if str_old in sys.path: | |
296 | sys.path.remove(str_old) |
|
286 | sys.path.remove(str_old) | |
297 | str_path = os.path.abspath(new) |
|
287 | str_path = os.path.abspath(new) | |
298 | sys.path.append(str_path) |
|
288 | sys.path.append(str_path) | |
299 | ensure_dir_exists(new) |
|
289 | ensure_dir_exists(new) | |
300 | readme = os.path.join(new, 'README') |
|
290 | readme = os.path.join(new, 'README') | |
301 | readme_src = os.path.join(get_ipython_package_dir(), u'config', u'profile', 'README') |
|
291 | readme_src = os.path.join(get_ipython_package_dir(), u'config', u'profile', 'README') | |
302 | if not os.path.exists(readme) and os.path.exists(readme_src): |
|
292 | if not os.path.exists(readme) and os.path.exists(readme_src): | |
303 | shutil.copy(readme_src, readme) |
|
293 | shutil.copy(readme_src, readme) | |
304 | for d in ('extensions', 'nbextensions'): |
|
294 | for d in ('extensions', 'nbextensions'): | |
305 | path = os.path.join(new, d) |
|
295 | path = os.path.join(new, d) | |
306 | try: |
|
296 | try: | |
307 | ensure_dir_exists(path) |
|
297 | ensure_dir_exists(path) | |
308 | except OSError as e: |
|
298 | except OSError as e: | |
309 | # this will not be EEXIST |
|
299 | # this will not be EEXIST | |
310 | self.log.error("couldn't create path %s: %s", path, e) |
|
300 | self.log.error("couldn't create path %s: %s", path, e) | |
311 | self.log.debug("IPYTHONDIR set to: %s" % new) |
|
301 | self.log.debug("IPYTHONDIR set to: %s" % new) | |
312 |
|
302 | |||
313 | def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS): |
|
303 | def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS): | |
314 | """Load the config file. |
|
304 | """Load the config file. | |
315 |
|
305 | |||
316 | By default, errors in loading config are handled, and a warning |
|
306 | By default, errors in loading config are handled, and a warning | |
317 | printed on screen. For testing, the suppress_errors option is set |
|
307 | printed on screen. For testing, the suppress_errors option is set | |
318 | to False, so errors will make tests fail. |
|
308 | to False, so errors will make tests fail. | |
319 |
|
309 | |||
320 | `suppress_errors` default value is to be `None` in which case the |
|
310 | `suppress_errors` default value is to be `None` in which case the | |
321 | behavior default to the one of `traitlets.Application`. |
|
311 | behavior default to the one of `traitlets.Application`. | |
322 |
|
312 | |||
323 | The default value can be set : |
|
313 | The default value can be set : | |
324 | - to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive). |
|
314 | - to `False` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '0', or 'false' (case insensitive). | |
325 | - to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive). |
|
315 | - to `True` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '1' or 'true' (case insensitive). | |
326 | - to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset. |
|
316 | - to `None` by setting 'IPYTHON_SUPPRESS_CONFIG_ERRORS' environment variable to '' (empty string) or leaving it unset. | |
327 |
|
317 | |||
328 | Any other value are invalid, and will make IPython exit with a non-zero return code. |
|
318 | Any other value are invalid, and will make IPython exit with a non-zero return code. | |
329 | """ |
|
319 | """ | |
330 |
|
320 | |||
331 |
|
321 | |||
332 | self.log.debug("Searching path %s for config files", self.config_file_paths) |
|
322 | self.log.debug("Searching path %s for config files", self.config_file_paths) | |
333 | base_config = 'ipython_config.py' |
|
323 | base_config = 'ipython_config.py' | |
334 | self.log.debug("Attempting to load config file: %s" % |
|
324 | self.log.debug("Attempting to load config file: %s" % | |
335 | base_config) |
|
325 | base_config) | |
336 | try: |
|
326 | try: | |
337 | if suppress_errors is not None: |
|
327 | if suppress_errors is not None: | |
338 | old_value = Application.raise_config_file_errors |
|
328 | old_value = Application.raise_config_file_errors | |
339 | Application.raise_config_file_errors = not suppress_errors; |
|
329 | Application.raise_config_file_errors = not suppress_errors; | |
340 | Application.load_config_file( |
|
330 | Application.load_config_file( | |
341 | self, |
|
331 | self, | |
342 | base_config, |
|
332 | base_config, | |
343 | path=self.config_file_paths |
|
333 | path=self.config_file_paths | |
344 | ) |
|
334 | ) | |
345 | except ConfigFileNotFound: |
|
335 | except ConfigFileNotFound: | |
346 | # ignore errors loading parent |
|
336 | # ignore errors loading parent | |
347 | self.log.debug("Config file %s not found", base_config) |
|
337 | self.log.debug("Config file %s not found", base_config) | |
348 | pass |
|
338 | pass | |
349 | if suppress_errors is not None: |
|
339 | if suppress_errors is not None: | |
350 | Application.raise_config_file_errors = old_value |
|
340 | Application.raise_config_file_errors = old_value | |
351 |
|
341 | |||
352 | for config_file_name in self.config_files: |
|
342 | for config_file_name in self.config_files: | |
353 | if not config_file_name or config_file_name == base_config: |
|
343 | if not config_file_name or config_file_name == base_config: | |
354 | continue |
|
344 | continue | |
355 | self.log.debug("Attempting to load config file: %s" % |
|
345 | self.log.debug("Attempting to load config file: %s" % | |
356 | self.config_file_name) |
|
346 | self.config_file_name) | |
357 | try: |
|
347 | try: | |
358 | Application.load_config_file( |
|
348 | Application.load_config_file( | |
359 | self, |
|
349 | self, | |
360 | config_file_name, |
|
350 | config_file_name, | |
361 | path=self.config_file_paths |
|
351 | path=self.config_file_paths | |
362 | ) |
|
352 | ) | |
363 | except ConfigFileNotFound: |
|
353 | except ConfigFileNotFound: | |
364 | # Only warn if the default config file was NOT being used. |
|
354 | # Only warn if the default config file was NOT being used. | |
365 | if config_file_name in self.config_file_specified: |
|
355 | if config_file_name in self.config_file_specified: | |
366 | msg = self.log.warning |
|
356 | msg = self.log.warning | |
367 | else: |
|
357 | else: | |
368 | msg = self.log.debug |
|
358 | msg = self.log.debug | |
369 | msg("Config file not found, skipping: %s", config_file_name) |
|
359 | msg("Config file not found, skipping: %s", config_file_name) | |
370 | except Exception: |
|
360 | except Exception: | |
371 | # For testing purposes. |
|
361 | # For testing purposes. | |
372 | if not suppress_errors: |
|
362 | if not suppress_errors: | |
373 | raise |
|
363 | raise | |
374 | self.log.warning("Error loading config file: %s" % |
|
364 | self.log.warning("Error loading config file: %s" % | |
375 | self.config_file_name, exc_info=True) |
|
365 | self.config_file_name, exc_info=True) | |
376 |
|
366 | |||
377 | def init_profile_dir(self): |
|
367 | def init_profile_dir(self): | |
378 | """initialize the profile dir""" |
|
368 | """initialize the profile dir""" | |
379 | self._in_init_profile_dir = True |
|
369 | self._in_init_profile_dir = True | |
380 | if self.profile_dir is not None: |
|
370 | if self.profile_dir is not None: | |
381 | # already ran |
|
371 | # already ran | |
382 | return |
|
372 | return | |
383 | if 'ProfileDir.location' not in self.config: |
|
373 | if 'ProfileDir.location' not in self.config: | |
384 | # location not specified, find by profile name |
|
374 | # location not specified, find by profile name | |
385 | try: |
|
375 | try: | |
386 | p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
376 | p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config) | |
387 | except ProfileDirError: |
|
377 | except ProfileDirError: | |
388 | # not found, maybe create it (always create default profile) |
|
378 | # not found, maybe create it (always create default profile) | |
389 | if self.auto_create or self.profile == 'default': |
|
379 | if self.auto_create or self.profile == 'default': | |
390 | try: |
|
380 | try: | |
391 | p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
381 | p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config) | |
392 | except ProfileDirError: |
|
382 | except ProfileDirError: | |
393 | self.log.fatal("Could not create profile: %r"%self.profile) |
|
383 | self.log.fatal("Could not create profile: %r"%self.profile) | |
394 | self.exit(1) |
|
384 | self.exit(1) | |
395 | else: |
|
385 | else: | |
396 | self.log.info("Created profile dir: %r"%p.location) |
|
386 | self.log.info("Created profile dir: %r"%p.location) | |
397 | else: |
|
387 | else: | |
398 | self.log.fatal("Profile %r not found."%self.profile) |
|
388 | self.log.fatal("Profile %r not found."%self.profile) | |
399 | self.exit(1) |
|
389 | self.exit(1) | |
400 | else: |
|
390 | else: | |
401 | self.log.debug(f"Using existing profile dir: {p.location!r}") |
|
391 | self.log.debug(f"Using existing profile dir: {p.location!r}") | |
402 | else: |
|
392 | else: | |
403 | location = self.config.ProfileDir.location |
|
393 | location = self.config.ProfileDir.location | |
404 | # location is fully specified |
|
394 | # location is fully specified | |
405 | try: |
|
395 | try: | |
406 | p = ProfileDir.find_profile_dir(location, self.config) |
|
396 | p = ProfileDir.find_profile_dir(location, self.config) | |
407 | except ProfileDirError: |
|
397 | except ProfileDirError: | |
408 | # not found, maybe create it |
|
398 | # not found, maybe create it | |
409 | if self.auto_create: |
|
399 | if self.auto_create: | |
410 | try: |
|
400 | try: | |
411 | p = ProfileDir.create_profile_dir(location, self.config) |
|
401 | p = ProfileDir.create_profile_dir(location, self.config) | |
412 | except ProfileDirError: |
|
402 | except ProfileDirError: | |
413 | self.log.fatal("Could not create profile directory: %r"%location) |
|
403 | self.log.fatal("Could not create profile directory: %r"%location) | |
414 | self.exit(1) |
|
404 | self.exit(1) | |
415 | else: |
|
405 | else: | |
416 | self.log.debug("Creating new profile dir: %r"%location) |
|
406 | self.log.debug("Creating new profile dir: %r"%location) | |
417 | else: |
|
407 | else: | |
418 | self.log.fatal("Profile directory %r not found."%location) |
|
408 | self.log.fatal("Profile directory %r not found."%location) | |
419 | self.exit(1) |
|
409 | self.exit(1) | |
420 | else: |
|
410 | else: | |
421 | self.log.debug(f"Using existing profile dir: {p.location!r}") |
|
411 | self.log.debug(f"Using existing profile dir: {p.location!r}") | |
422 | # if profile_dir is specified explicitly, set profile name |
|
412 | # if profile_dir is specified explicitly, set profile name | |
423 | dir_name = os.path.basename(p.location) |
|
413 | dir_name = os.path.basename(p.location) | |
424 | if dir_name.startswith('profile_'): |
|
414 | if dir_name.startswith('profile_'): | |
425 | self.profile = dir_name[8:] |
|
415 | self.profile = dir_name[8:] | |
426 |
|
416 | |||
427 | self.profile_dir = p |
|
417 | self.profile_dir = p | |
428 | self.config_file_paths.append(p.location) |
|
418 | self.config_file_paths.append(p.location) | |
429 | self._in_init_profile_dir = False |
|
419 | self._in_init_profile_dir = False | |
430 |
|
420 | |||
431 | def init_config_files(self): |
|
421 | def init_config_files(self): | |
432 | """[optionally] copy default config files into profile dir.""" |
|
422 | """[optionally] copy default config files into profile dir.""" | |
433 | self.config_file_paths.extend(ENV_CONFIG_DIRS) |
|
423 | self.config_file_paths.extend(ENV_CONFIG_DIRS) | |
434 | self.config_file_paths.extend(SYSTEM_CONFIG_DIRS) |
|
424 | self.config_file_paths.extend(SYSTEM_CONFIG_DIRS) | |
435 | # copy config files |
|
425 | # copy config files | |
436 | path = Path(self.builtin_profile_dir) |
|
426 | path = Path(self.builtin_profile_dir) | |
437 | if self.copy_config_files: |
|
427 | if self.copy_config_files: | |
438 | src = self.profile |
|
428 | src = self.profile | |
439 |
|
429 | |||
440 | cfg = self.config_file_name |
|
430 | cfg = self.config_file_name | |
441 | if path and (path / cfg).exists(): |
|
431 | if path and (path / cfg).exists(): | |
442 | self.log.warning( |
|
432 | self.log.warning( | |
443 | "Staging %r from %s into %r [overwrite=%s]" |
|
433 | "Staging %r from %s into %r [overwrite=%s]" | |
444 | % (cfg, src, self.profile_dir.location, self.overwrite) |
|
434 | % (cfg, src, self.profile_dir.location, self.overwrite) | |
445 | ) |
|
435 | ) | |
446 | self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite) |
|
436 | self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite) | |
447 | else: |
|
437 | else: | |
448 | self.stage_default_config_file() |
|
438 | self.stage_default_config_file() | |
449 | else: |
|
439 | else: | |
450 | # Still stage *bundled* config files, but not generated ones |
|
440 | # Still stage *bundled* config files, but not generated ones | |
451 | # This is necessary for `ipython profile=sympy` to load the profile |
|
441 | # This is necessary for `ipython profile=sympy` to load the profile | |
452 | # on the first go |
|
442 | # on the first go | |
453 | files = path.glob("*.py") |
|
443 | files = path.glob("*.py") | |
454 | for fullpath in files: |
|
444 | for fullpath in files: | |
455 | cfg = fullpath.name |
|
445 | cfg = fullpath.name | |
456 | if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False): |
|
446 | if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False): | |
457 | # file was copied |
|
447 | # file was copied | |
458 | self.log.warning("Staging bundled %s from %s into %r"%( |
|
448 | self.log.warning("Staging bundled %s from %s into %r"%( | |
459 | cfg, self.profile, self.profile_dir.location) |
|
449 | cfg, self.profile, self.profile_dir.location) | |
460 | ) |
|
450 | ) | |
461 |
|
451 | |||
462 |
|
452 | |||
463 | def stage_default_config_file(self): |
|
453 | def stage_default_config_file(self): | |
464 | """auto generate default config file, and stage it into the profile.""" |
|
454 | """auto generate default config file, and stage it into the profile.""" | |
465 | s = self.generate_config_file() |
|
455 | s = self.generate_config_file() | |
466 | config_file = Path(self.profile_dir.location) / self.config_file_name |
|
456 | config_file = Path(self.profile_dir.location) / self.config_file_name | |
467 | if self.overwrite or not config_file.exists(): |
|
457 | if self.overwrite or not config_file.exists(): | |
468 | self.log.warning("Generating default config file: %r" % (config_file)) |
|
458 | self.log.warning("Generating default config file: %r" % (config_file)) | |
469 | config_file.write_text(s) |
|
459 | config_file.write_text(s) | |
470 |
|
460 | |||
471 | @catch_config_error |
|
461 | @catch_config_error | |
472 | def initialize(self, argv=None): |
|
462 | def initialize(self, argv=None): | |
473 | # don't hook up crash handler before parsing command-line |
|
463 | # don't hook up crash handler before parsing command-line | |
474 | self.parse_command_line(argv) |
|
464 | self.parse_command_line(argv) | |
475 | self.init_crash_handler() |
|
465 | self.init_crash_handler() | |
476 | if self.subapp is not None: |
|
466 | if self.subapp is not None: | |
477 | # stop here if subapp is taking over |
|
467 | # stop here if subapp is taking over | |
478 | return |
|
468 | return | |
479 | # save a copy of CLI config to re-load after config files |
|
469 | # save a copy of CLI config to re-load after config files | |
480 | # so that it has highest priority |
|
470 | # so that it has highest priority | |
481 | cl_config = deepcopy(self.config) |
|
471 | cl_config = deepcopy(self.config) | |
482 | self.init_profile_dir() |
|
472 | self.init_profile_dir() | |
483 | self.init_config_files() |
|
473 | self.init_config_files() | |
484 | self.load_config_file() |
|
474 | self.load_config_file() | |
485 | # enforce cl-opts override configfile opts: |
|
475 | # enforce cl-opts override configfile opts: | |
486 | self.update_config(cl_config) |
|
476 | self.update_config(cl_config) |
@@ -1,1010 +1,1003 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Pdb debugger class. |
|
3 | Pdb debugger class. | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | This is an extension to PDB which adds a number of new features. |
|
6 | This is an extension to PDB which adds a number of new features. | |
7 | Note that there is also the `IPython.terminal.debugger` class which provides UI |
|
7 | Note that there is also the `IPython.terminal.debugger` class which provides UI | |
8 | improvements. |
|
8 | improvements. | |
9 |
|
9 | |||
10 | We also strongly recommend to use this via the `ipdb` package, which provides |
|
10 | We also strongly recommend to use this via the `ipdb` package, which provides | |
11 | extra configuration options. |
|
11 | extra configuration options. | |
12 |
|
12 | |||
13 | Among other things, this subclass of PDB: |
|
13 | Among other things, this subclass of PDB: | |
14 | - supports many IPython magics like pdef/psource |
|
14 | - supports many IPython magics like pdef/psource | |
15 | - hide frames in tracebacks based on `__tracebackhide__` |
|
15 | - hide frames in tracebacks based on `__tracebackhide__` | |
16 | - allows to skip frames based on `__debuggerskip__` |
|
16 | - allows to skip frames based on `__debuggerskip__` | |
17 |
|
17 | |||
18 | The skipping and hiding frames are configurable via the `skip_predicates` |
|
18 | The skipping and hiding frames are configurable via the `skip_predicates` | |
19 | command. |
|
19 | command. | |
20 |
|
20 | |||
21 | By default, frames from readonly files will be hidden, frames containing |
|
21 | By default, frames from readonly files will be hidden, frames containing | |
22 | ``__tracebackhide__=True`` will be hidden. |
|
22 | ``__tracebackhide__=True`` will be hidden. | |
23 |
|
23 | |||
24 | Frames containing ``__debuggerskip__`` will be stepped over, frames who's parent |
|
24 | Frames containing ``__debuggerskip__`` will be stepped over, frames who's parent | |
25 | frames value of ``__debuggerskip__`` is ``True`` will be skipped. |
|
25 | frames value of ``__debuggerskip__`` is ``True`` will be skipped. | |
26 |
|
26 | |||
27 | >>> def helpers_helper(): |
|
27 | >>> def helpers_helper(): | |
28 | ... pass |
|
28 | ... pass | |
29 | ... |
|
29 | ... | |
30 | ... def helper_1(): |
|
30 | ... def helper_1(): | |
31 | ... print("don't step in me") |
|
31 | ... print("don't step in me") | |
32 | ... helpers_helpers() # will be stepped over unless breakpoint set. |
|
32 | ... helpers_helpers() # will be stepped over unless breakpoint set. | |
33 | ... |
|
33 | ... | |
34 | ... |
|
34 | ... | |
35 | ... def helper_2(): |
|
35 | ... def helper_2(): | |
36 | ... print("in me neither") |
|
36 | ... print("in me neither") | |
37 | ... |
|
37 | ... | |
38 |
|
38 | |||
39 | One can define a decorator that wraps a function between the two helpers: |
|
39 | One can define a decorator that wraps a function between the two helpers: | |
40 |
|
40 | |||
41 | >>> def pdb_skipped_decorator(function): |
|
41 | >>> def pdb_skipped_decorator(function): | |
42 | ... |
|
42 | ... | |
43 | ... |
|
43 | ... | |
44 | ... def wrapped_fn(*args, **kwargs): |
|
44 | ... def wrapped_fn(*args, **kwargs): | |
45 | ... __debuggerskip__ = True |
|
45 | ... __debuggerskip__ = True | |
46 | ... helper_1() |
|
46 | ... helper_1() | |
47 | ... __debuggerskip__ = False |
|
47 | ... __debuggerskip__ = False | |
48 | ... result = function(*args, **kwargs) |
|
48 | ... result = function(*args, **kwargs) | |
49 | ... __debuggerskip__ = True |
|
49 | ... __debuggerskip__ = True | |
50 | ... helper_2() |
|
50 | ... helper_2() | |
51 | ... # setting __debuggerskip__ to False again is not necessary |
|
51 | ... # setting __debuggerskip__ to False again is not necessary | |
52 | ... return result |
|
52 | ... return result | |
53 | ... |
|
53 | ... | |
54 | ... return wrapped_fn |
|
54 | ... return wrapped_fn | |
55 |
|
55 | |||
56 | When decorating a function, ipdb will directly step into ``bar()`` by |
|
56 | When decorating a function, ipdb will directly step into ``bar()`` by | |
57 | default: |
|
57 | default: | |
58 |
|
58 | |||
59 | >>> @foo_decorator |
|
59 | >>> @foo_decorator | |
60 | ... def bar(x, y): |
|
60 | ... def bar(x, y): | |
61 | ... return x * y |
|
61 | ... return x * y | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | You can toggle the behavior with |
|
64 | You can toggle the behavior with | |
65 |
|
65 | |||
66 | ipdb> skip_predicates debuggerskip false |
|
66 | ipdb> skip_predicates debuggerskip false | |
67 |
|
67 | |||
68 | or configure it in your ``.pdbrc`` |
|
68 | or configure it in your ``.pdbrc`` | |
69 |
|
69 | |||
70 |
|
70 | |||
71 |
|
71 | |||
72 | License |
|
72 | License | |
73 | ------- |
|
73 | ------- | |
74 |
|
74 | |||
75 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
75 | Modified from the standard pdb.Pdb class to avoid including readline, so that | |
76 | the command line completion of other programs which include this isn't |
|
76 | the command line completion of other programs which include this isn't | |
77 | damaged. |
|
77 | damaged. | |
78 |
|
78 | |||
79 | In the future, this class will be expanded with improvements over the standard |
|
79 | In the future, this class will be expanded with improvements over the standard | |
80 | pdb. |
|
80 | pdb. | |
81 |
|
81 | |||
82 | The original code in this file is mainly lifted out of cmd.py in Python 2.2, |
|
82 | The original code in this file is mainly lifted out of cmd.py in Python 2.2, | |
83 | with minor changes. Licensing should therefore be under the standard Python |
|
83 | with minor changes. Licensing should therefore be under the standard Python | |
84 | terms. For details on the PSF (Python Software Foundation) standard license, |
|
84 | terms. For details on the PSF (Python Software Foundation) standard license, | |
85 | see: |
|
85 | see: | |
86 |
|
86 | |||
87 | https://docs.python.org/2/license.html |
|
87 | https://docs.python.org/2/license.html | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | All the changes since then are under the same license as IPython. |
|
90 | All the changes since then are under the same license as IPython. | |
91 |
|
91 | |||
92 | """ |
|
92 | """ | |
93 |
|
93 | |||
94 | #***************************************************************************** |
|
94 | #***************************************************************************** | |
95 | # |
|
95 | # | |
96 | # This file is licensed under the PSF license. |
|
96 | # This file is licensed under the PSF license. | |
97 | # |
|
97 | # | |
98 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
98 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
99 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> |
|
99 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> | |
100 | # |
|
100 | # | |
101 | # |
|
101 | # | |
102 | #***************************************************************************** |
|
102 | #***************************************************************************** | |
103 |
|
103 | |||
104 | import bdb |
|
104 | import bdb | |
105 | import inspect |
|
105 | import inspect | |
106 | import linecache |
|
106 | import linecache | |
107 | import sys |
|
107 | import sys | |
108 | import warnings |
|
108 | import warnings | |
109 | import re |
|
109 | import re | |
110 | import os |
|
110 | import os | |
111 |
|
111 | |||
112 | from IPython import get_ipython |
|
112 | from IPython import get_ipython | |
113 | from IPython.utils import PyColorize |
|
113 | from IPython.utils import PyColorize | |
114 | from IPython.utils import coloransi, py3compat |
|
114 | from IPython.utils import coloransi, py3compat | |
115 | from IPython.core.excolors import exception_colors |
|
115 | from IPython.core.excolors import exception_colors | |
116 |
|
116 | |||
117 | # skip module docstests |
|
117 | # skip module docstests | |
118 | __skip_doctest__ = True |
|
118 | __skip_doctest__ = True | |
119 |
|
119 | |||
120 | prompt = 'ipdb> ' |
|
120 | prompt = 'ipdb> ' | |
121 |
|
121 | |||
122 | # We have to check this directly from sys.argv, config struct not yet available |
|
122 | # We have to check this directly from sys.argv, config struct not yet available | |
123 | from pdb import Pdb as OldPdb |
|
123 | from pdb import Pdb as OldPdb | |
124 |
|
124 | |||
125 | # Allow the set_trace code to operate outside of an ipython instance, even if |
|
125 | # Allow the set_trace code to operate outside of an ipython instance, even if | |
126 | # it does so with some limitations. The rest of this support is implemented in |
|
126 | # it does so with some limitations. The rest of this support is implemented in | |
127 | # the Tracer constructor. |
|
127 | # the Tracer constructor. | |
128 |
|
128 | |||
129 | DEBUGGERSKIP = "__debuggerskip__" |
|
129 | DEBUGGERSKIP = "__debuggerskip__" | |
130 |
|
130 | |||
131 |
|
131 | |||
132 | def make_arrow(pad): |
|
132 | def make_arrow(pad): | |
133 | """generate the leading arrow in front of traceback or debugger""" |
|
133 | """generate the leading arrow in front of traceback or debugger""" | |
134 | if pad >= 2: |
|
134 | if pad >= 2: | |
135 | return '-'*(pad-2) + '> ' |
|
135 | return '-'*(pad-2) + '> ' | |
136 | elif pad == 1: |
|
136 | elif pad == 1: | |
137 | return '>' |
|
137 | return '>' | |
138 | return '' |
|
138 | return '' | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | def BdbQuit_excepthook(et, ev, tb, excepthook=None): |
|
141 | def BdbQuit_excepthook(et, ev, tb, excepthook=None): | |
142 | """Exception hook which handles `BdbQuit` exceptions. |
|
142 | """Exception hook which handles `BdbQuit` exceptions. | |
143 |
|
143 | |||
144 | All other exceptions are processed using the `excepthook` |
|
144 | All other exceptions are processed using the `excepthook` | |
145 | parameter. |
|
145 | parameter. | |
146 | """ |
|
146 | """ | |
147 | warnings.warn("`BdbQuit_excepthook` is deprecated since version 5.1", |
|
147 | raise ValueError( | |
148 | DeprecationWarning, stacklevel=2) |
|
148 | "`BdbQuit_excepthook` is deprecated since version 5.1", | |
149 | if et == bdb.BdbQuit: |
|
149 | ) | |
150 | print('Exiting Debugger.') |
|
|||
151 | elif excepthook is not None: |
|
|||
152 | excepthook(et, ev, tb) |
|
|||
153 | else: |
|
|||
154 | # Backwards compatibility. Raise deprecation warning? |
|
|||
155 | BdbQuit_excepthook.excepthook_ori(et, ev, tb) |
|
|||
156 |
|
150 | |||
157 |
|
151 | |||
158 | def BdbQuit_IPython_excepthook(self, et, ev, tb, tb_offset=None): |
|
152 | def BdbQuit_IPython_excepthook(self, et, ev, tb, tb_offset=None): | |
159 | warnings.warn( |
|
153 | raise ValueError( | |
160 | "`BdbQuit_IPython_excepthook` is deprecated since version 5.1", |
|
154 | "`BdbQuit_IPython_excepthook` is deprecated since version 5.1", | |
161 | DeprecationWarning, stacklevel=2) |
|
155 | DeprecationWarning, stacklevel=2) | |
162 | print('Exiting Debugger.') |
|
|||
163 |
|
156 | |||
164 |
|
157 | |||
165 | RGX_EXTRA_INDENT = re.compile(r'(?<=\n)\s+') |
|
158 | RGX_EXTRA_INDENT = re.compile(r'(?<=\n)\s+') | |
166 |
|
159 | |||
167 |
|
160 | |||
168 | def strip_indentation(multiline_string): |
|
161 | def strip_indentation(multiline_string): | |
169 | return RGX_EXTRA_INDENT.sub('', multiline_string) |
|
162 | return RGX_EXTRA_INDENT.sub('', multiline_string) | |
170 |
|
163 | |||
171 |
|
164 | |||
172 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): |
|
165 | def decorate_fn_with_doc(new_fn, old_fn, additional_text=""): | |
173 | """Make new_fn have old_fn's doc string. This is particularly useful |
|
166 | """Make new_fn have old_fn's doc string. This is particularly useful | |
174 | for the ``do_...`` commands that hook into the help system. |
|
167 | for the ``do_...`` commands that hook into the help system. | |
175 | Adapted from from a comp.lang.python posting |
|
168 | Adapted from from a comp.lang.python posting | |
176 | by Duncan Booth.""" |
|
169 | by Duncan Booth.""" | |
177 | def wrapper(*args, **kw): |
|
170 | def wrapper(*args, **kw): | |
178 | return new_fn(*args, **kw) |
|
171 | return new_fn(*args, **kw) | |
179 | if old_fn.__doc__: |
|
172 | if old_fn.__doc__: | |
180 | wrapper.__doc__ = strip_indentation(old_fn.__doc__) + additional_text |
|
173 | wrapper.__doc__ = strip_indentation(old_fn.__doc__) + additional_text | |
181 | return wrapper |
|
174 | return wrapper | |
182 |
|
175 | |||
183 |
|
176 | |||
184 | class Pdb(OldPdb): |
|
177 | class Pdb(OldPdb): | |
185 | """Modified Pdb class, does not load readline. |
|
178 | """Modified Pdb class, does not load readline. | |
186 |
|
179 | |||
187 | for a standalone version that uses prompt_toolkit, see |
|
180 | for a standalone version that uses prompt_toolkit, see | |
188 | `IPython.terminal.debugger.TerminalPdb` and |
|
181 | `IPython.terminal.debugger.TerminalPdb` and | |
189 | `IPython.terminal.debugger.set_trace()` |
|
182 | `IPython.terminal.debugger.set_trace()` | |
190 |
|
183 | |||
191 |
|
184 | |||
192 | This debugger can hide and skip frames that are tagged according to some predicates. |
|
185 | This debugger can hide and skip frames that are tagged according to some predicates. | |
193 | See the `skip_predicates` commands. |
|
186 | See the `skip_predicates` commands. | |
194 |
|
187 | |||
195 | """ |
|
188 | """ | |
196 |
|
189 | |||
197 | default_predicates = { |
|
190 | default_predicates = { | |
198 | "tbhide": True, |
|
191 | "tbhide": True, | |
199 | "readonly": False, |
|
192 | "readonly": False, | |
200 | "ipython_internal": True, |
|
193 | "ipython_internal": True, | |
201 | "debuggerskip": True, |
|
194 | "debuggerskip": True, | |
202 | } |
|
195 | } | |
203 |
|
196 | |||
204 | def __init__(self, completekey=None, stdin=None, stdout=None, context=5, **kwargs): |
|
197 | def __init__(self, completekey=None, stdin=None, stdout=None, context=5, **kwargs): | |
205 | """Create a new IPython debugger. |
|
198 | """Create a new IPython debugger. | |
206 |
|
199 | |||
207 | Parameters |
|
200 | Parameters | |
208 | ---------- |
|
201 | ---------- | |
209 | completekey : default None |
|
202 | completekey : default None | |
210 | Passed to pdb.Pdb. |
|
203 | Passed to pdb.Pdb. | |
211 | stdin : default None |
|
204 | stdin : default None | |
212 | Passed to pdb.Pdb. |
|
205 | Passed to pdb.Pdb. | |
213 | stdout : default None |
|
206 | stdout : default None | |
214 | Passed to pdb.Pdb. |
|
207 | Passed to pdb.Pdb. | |
215 | context : int |
|
208 | context : int | |
216 | Number of lines of source code context to show when |
|
209 | Number of lines of source code context to show when | |
217 | displaying stacktrace information. |
|
210 | displaying stacktrace information. | |
218 | **kwargs |
|
211 | **kwargs | |
219 | Passed to pdb.Pdb. |
|
212 | Passed to pdb.Pdb. | |
220 |
|
213 | |||
221 | Notes |
|
214 | Notes | |
222 | ----- |
|
215 | ----- | |
223 | The possibilities are python version dependent, see the python |
|
216 | The possibilities are python version dependent, see the python | |
224 | docs for more info. |
|
217 | docs for more info. | |
225 | """ |
|
218 | """ | |
226 |
|
219 | |||
227 | # Parent constructor: |
|
220 | # Parent constructor: | |
228 | try: |
|
221 | try: | |
229 | self.context = int(context) |
|
222 | self.context = int(context) | |
230 | if self.context <= 0: |
|
223 | if self.context <= 0: | |
231 | raise ValueError("Context must be a positive integer") |
|
224 | raise ValueError("Context must be a positive integer") | |
232 | except (TypeError, ValueError) as e: |
|
225 | except (TypeError, ValueError) as e: | |
233 | raise ValueError("Context must be a positive integer") from e |
|
226 | raise ValueError("Context must be a positive integer") from e | |
234 |
|
227 | |||
235 | # `kwargs` ensures full compatibility with stdlib's `pdb.Pdb`. |
|
228 | # `kwargs` ensures full compatibility with stdlib's `pdb.Pdb`. | |
236 | OldPdb.__init__(self, completekey, stdin, stdout, **kwargs) |
|
229 | OldPdb.__init__(self, completekey, stdin, stdout, **kwargs) | |
237 |
|
230 | |||
238 | # IPython changes... |
|
231 | # IPython changes... | |
239 | self.shell = get_ipython() |
|
232 | self.shell = get_ipython() | |
240 |
|
233 | |||
241 | if self.shell is None: |
|
234 | if self.shell is None: | |
242 | save_main = sys.modules['__main__'] |
|
235 | save_main = sys.modules['__main__'] | |
243 | # No IPython instance running, we must create one |
|
236 | # No IPython instance running, we must create one | |
244 | from IPython.terminal.interactiveshell import \ |
|
237 | from IPython.terminal.interactiveshell import \ | |
245 | TerminalInteractiveShell |
|
238 | TerminalInteractiveShell | |
246 | self.shell = TerminalInteractiveShell.instance() |
|
239 | self.shell = TerminalInteractiveShell.instance() | |
247 | # needed by any code which calls __import__("__main__") after |
|
240 | # needed by any code which calls __import__("__main__") after | |
248 | # the debugger was entered. See also #9941. |
|
241 | # the debugger was entered. See also #9941. | |
249 | sys.modules["__main__"] = save_main |
|
242 | sys.modules["__main__"] = save_main | |
250 |
|
243 | |||
251 |
|
244 | |||
252 | color_scheme = self.shell.colors |
|
245 | color_scheme = self.shell.colors | |
253 |
|
246 | |||
254 | self.aliases = {} |
|
247 | self.aliases = {} | |
255 |
|
248 | |||
256 | # Create color table: we copy the default one from the traceback |
|
249 | # Create color table: we copy the default one from the traceback | |
257 | # module and add a few attributes needed for debugging |
|
250 | # module and add a few attributes needed for debugging | |
258 | self.color_scheme_table = exception_colors() |
|
251 | self.color_scheme_table = exception_colors() | |
259 |
|
252 | |||
260 | # shorthands |
|
253 | # shorthands | |
261 | C = coloransi.TermColors |
|
254 | C = coloransi.TermColors | |
262 | cst = self.color_scheme_table |
|
255 | cst = self.color_scheme_table | |
263 |
|
256 | |||
264 | cst['NoColor'].colors.prompt = C.NoColor |
|
257 | cst['NoColor'].colors.prompt = C.NoColor | |
265 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor |
|
258 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor | |
266 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor |
|
259 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor | |
267 |
|
260 | |||
268 | cst['Linux'].colors.prompt = C.Green |
|
261 | cst['Linux'].colors.prompt = C.Green | |
269 | cst['Linux'].colors.breakpoint_enabled = C.LightRed |
|
262 | cst['Linux'].colors.breakpoint_enabled = C.LightRed | |
270 | cst['Linux'].colors.breakpoint_disabled = C.Red |
|
263 | cst['Linux'].colors.breakpoint_disabled = C.Red | |
271 |
|
264 | |||
272 | cst['LightBG'].colors.prompt = C.Blue |
|
265 | cst['LightBG'].colors.prompt = C.Blue | |
273 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed |
|
266 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed | |
274 | cst['LightBG'].colors.breakpoint_disabled = C.Red |
|
267 | cst['LightBG'].colors.breakpoint_disabled = C.Red | |
275 |
|
268 | |||
276 | cst['Neutral'].colors.prompt = C.Blue |
|
269 | cst['Neutral'].colors.prompt = C.Blue | |
277 | cst['Neutral'].colors.breakpoint_enabled = C.LightRed |
|
270 | cst['Neutral'].colors.breakpoint_enabled = C.LightRed | |
278 | cst['Neutral'].colors.breakpoint_disabled = C.Red |
|
271 | cst['Neutral'].colors.breakpoint_disabled = C.Red | |
279 |
|
272 | |||
280 | # Add a python parser so we can syntax highlight source while |
|
273 | # Add a python parser so we can syntax highlight source while | |
281 | # debugging. |
|
274 | # debugging. | |
282 | self.parser = PyColorize.Parser(style=color_scheme) |
|
275 | self.parser = PyColorize.Parser(style=color_scheme) | |
283 | self.set_colors(color_scheme) |
|
276 | self.set_colors(color_scheme) | |
284 |
|
277 | |||
285 | # Set the prompt - the default prompt is '(Pdb)' |
|
278 | # Set the prompt - the default prompt is '(Pdb)' | |
286 | self.prompt = prompt |
|
279 | self.prompt = prompt | |
287 | self.skip_hidden = True |
|
280 | self.skip_hidden = True | |
288 | self.report_skipped = True |
|
281 | self.report_skipped = True | |
289 |
|
282 | |||
290 | # list of predicates we use to skip frames |
|
283 | # list of predicates we use to skip frames | |
291 | self._predicates = self.default_predicates |
|
284 | self._predicates = self.default_predicates | |
292 |
|
285 | |||
293 | # |
|
286 | # | |
294 | def set_colors(self, scheme): |
|
287 | def set_colors(self, scheme): | |
295 | """Shorthand access to the color table scheme selector method.""" |
|
288 | """Shorthand access to the color table scheme selector method.""" | |
296 | self.color_scheme_table.set_active_scheme(scheme) |
|
289 | self.color_scheme_table.set_active_scheme(scheme) | |
297 | self.parser.style = scheme |
|
290 | self.parser.style = scheme | |
298 |
|
291 | |||
299 | def set_trace(self, frame=None): |
|
292 | def set_trace(self, frame=None): | |
300 | if frame is None: |
|
293 | if frame is None: | |
301 | frame = sys._getframe().f_back |
|
294 | frame = sys._getframe().f_back | |
302 | self.initial_frame = frame |
|
295 | self.initial_frame = frame | |
303 | return super().set_trace(frame) |
|
296 | return super().set_trace(frame) | |
304 |
|
297 | |||
305 | def _hidden_predicate(self, frame): |
|
298 | def _hidden_predicate(self, frame): | |
306 | """ |
|
299 | """ | |
307 | Given a frame return whether it it should be hidden or not by IPython. |
|
300 | Given a frame return whether it it should be hidden or not by IPython. | |
308 | """ |
|
301 | """ | |
309 |
|
302 | |||
310 | if self._predicates["readonly"]: |
|
303 | if self._predicates["readonly"]: | |
311 | fname = frame.f_code.co_filename |
|
304 | fname = frame.f_code.co_filename | |
312 | # we need to check for file existence and interactively define |
|
305 | # we need to check for file existence and interactively define | |
313 | # function would otherwise appear as RO. |
|
306 | # function would otherwise appear as RO. | |
314 | if os.path.isfile(fname) and not os.access(fname, os.W_OK): |
|
307 | if os.path.isfile(fname) and not os.access(fname, os.W_OK): | |
315 | return True |
|
308 | return True | |
316 |
|
309 | |||
317 | if self._predicates["tbhide"]: |
|
310 | if self._predicates["tbhide"]: | |
318 | if frame in (self.curframe, getattr(self, "initial_frame", None)): |
|
311 | if frame in (self.curframe, getattr(self, "initial_frame", None)): | |
319 | return False |
|
312 | return False | |
320 | frame_locals = self._get_frame_locals(frame) |
|
313 | frame_locals = self._get_frame_locals(frame) | |
321 | if "__tracebackhide__" not in frame_locals: |
|
314 | if "__tracebackhide__" not in frame_locals: | |
322 | return False |
|
315 | return False | |
323 | return frame_locals["__tracebackhide__"] |
|
316 | return frame_locals["__tracebackhide__"] | |
324 | return False |
|
317 | return False | |
325 |
|
318 | |||
326 | def hidden_frames(self, stack): |
|
319 | def hidden_frames(self, stack): | |
327 | """ |
|
320 | """ | |
328 | Given an index in the stack return whether it should be skipped. |
|
321 | Given an index in the stack return whether it should be skipped. | |
329 |
|
322 | |||
330 | This is used in up/down and where to skip frames. |
|
323 | This is used in up/down and where to skip frames. | |
331 | """ |
|
324 | """ | |
332 | # The f_locals dictionary is updated from the actual frame |
|
325 | # The f_locals dictionary is updated from the actual frame | |
333 | # locals whenever the .f_locals accessor is called, so we |
|
326 | # locals whenever the .f_locals accessor is called, so we | |
334 | # avoid calling it here to preserve self.curframe_locals. |
|
327 | # avoid calling it here to preserve self.curframe_locals. | |
335 | # Furthermore, there is no good reason to hide the current frame. |
|
328 | # Furthermore, there is no good reason to hide the current frame. | |
336 | ip_hide = [self._hidden_predicate(s[0]) for s in stack] |
|
329 | ip_hide = [self._hidden_predicate(s[0]) for s in stack] | |
337 | ip_start = [i for i, s in enumerate(ip_hide) if s == "__ipython_bottom__"] |
|
330 | ip_start = [i for i, s in enumerate(ip_hide) if s == "__ipython_bottom__"] | |
338 | if ip_start and self._predicates["ipython_internal"]: |
|
331 | if ip_start and self._predicates["ipython_internal"]: | |
339 | ip_hide = [h if i > ip_start[0] else True for (i, h) in enumerate(ip_hide)] |
|
332 | ip_hide = [h if i > ip_start[0] else True for (i, h) in enumerate(ip_hide)] | |
340 | return ip_hide |
|
333 | return ip_hide | |
341 |
|
334 | |||
342 | def interaction(self, frame, traceback): |
|
335 | def interaction(self, frame, traceback): | |
343 | try: |
|
336 | try: | |
344 | OldPdb.interaction(self, frame, traceback) |
|
337 | OldPdb.interaction(self, frame, traceback) | |
345 | except KeyboardInterrupt: |
|
338 | except KeyboardInterrupt: | |
346 | self.stdout.write("\n" + self.shell.get_exception_only()) |
|
339 | self.stdout.write("\n" + self.shell.get_exception_only()) | |
347 |
|
340 | |||
348 | def precmd(self, line): |
|
341 | def precmd(self, line): | |
349 | """Perform useful escapes on the command before it is executed.""" |
|
342 | """Perform useful escapes on the command before it is executed.""" | |
350 |
|
343 | |||
351 | if line.endswith("??"): |
|
344 | if line.endswith("??"): | |
352 | line = "pinfo2 " + line[:-2] |
|
345 | line = "pinfo2 " + line[:-2] | |
353 | elif line.endswith("?"): |
|
346 | elif line.endswith("?"): | |
354 | line = "pinfo " + line[:-1] |
|
347 | line = "pinfo " + line[:-1] | |
355 |
|
348 | |||
356 | line = super().precmd(line) |
|
349 | line = super().precmd(line) | |
357 |
|
350 | |||
358 | return line |
|
351 | return line | |
359 |
|
352 | |||
360 | def new_do_frame(self, arg): |
|
353 | def new_do_frame(self, arg): | |
361 | OldPdb.do_frame(self, arg) |
|
354 | OldPdb.do_frame(self, arg) | |
362 |
|
355 | |||
363 | def new_do_quit(self, arg): |
|
356 | def new_do_quit(self, arg): | |
364 |
|
357 | |||
365 | if hasattr(self, 'old_all_completions'): |
|
358 | if hasattr(self, 'old_all_completions'): | |
366 | self.shell.Completer.all_completions = self.old_all_completions |
|
359 | self.shell.Completer.all_completions = self.old_all_completions | |
367 |
|
360 | |||
368 | return OldPdb.do_quit(self, arg) |
|
361 | return OldPdb.do_quit(self, arg) | |
369 |
|
362 | |||
370 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) |
|
363 | do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit) | |
371 |
|
364 | |||
372 | def new_do_restart(self, arg): |
|
365 | def new_do_restart(self, arg): | |
373 | """Restart command. In the context of ipython this is exactly the same |
|
366 | """Restart command. In the context of ipython this is exactly the same | |
374 | thing as 'quit'.""" |
|
367 | thing as 'quit'.""" | |
375 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") |
|
368 | self.msg("Restart doesn't make sense here. Using 'quit' instead.") | |
376 | return self.do_quit(arg) |
|
369 | return self.do_quit(arg) | |
377 |
|
370 | |||
378 | def print_stack_trace(self, context=None): |
|
371 | def print_stack_trace(self, context=None): | |
379 | Colors = self.color_scheme_table.active_colors |
|
372 | Colors = self.color_scheme_table.active_colors | |
380 | ColorsNormal = Colors.Normal |
|
373 | ColorsNormal = Colors.Normal | |
381 | if context is None: |
|
374 | if context is None: | |
382 | context = self.context |
|
375 | context = self.context | |
383 | try: |
|
376 | try: | |
384 | context = int(context) |
|
377 | context = int(context) | |
385 | if context <= 0: |
|
378 | if context <= 0: | |
386 | raise ValueError("Context must be a positive integer") |
|
379 | raise ValueError("Context must be a positive integer") | |
387 | except (TypeError, ValueError) as e: |
|
380 | except (TypeError, ValueError) as e: | |
388 | raise ValueError("Context must be a positive integer") from e |
|
381 | raise ValueError("Context must be a positive integer") from e | |
389 | try: |
|
382 | try: | |
390 | skipped = 0 |
|
383 | skipped = 0 | |
391 | for hidden, frame_lineno in zip(self.hidden_frames(self.stack), self.stack): |
|
384 | for hidden, frame_lineno in zip(self.hidden_frames(self.stack), self.stack): | |
392 | if hidden and self.skip_hidden: |
|
385 | if hidden and self.skip_hidden: | |
393 | skipped += 1 |
|
386 | skipped += 1 | |
394 | continue |
|
387 | continue | |
395 | if skipped: |
|
388 | if skipped: | |
396 | print( |
|
389 | print( | |
397 | f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n" |
|
390 | f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n" | |
398 | ) |
|
391 | ) | |
399 | skipped = 0 |
|
392 | skipped = 0 | |
400 | self.print_stack_entry(frame_lineno, context=context) |
|
393 | self.print_stack_entry(frame_lineno, context=context) | |
401 | if skipped: |
|
394 | if skipped: | |
402 | print( |
|
395 | print( | |
403 | f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n" |
|
396 | f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n" | |
404 | ) |
|
397 | ) | |
405 | except KeyboardInterrupt: |
|
398 | except KeyboardInterrupt: | |
406 | pass |
|
399 | pass | |
407 |
|
400 | |||
408 | def print_stack_entry(self, frame_lineno, prompt_prefix='\n-> ', |
|
401 | def print_stack_entry(self, frame_lineno, prompt_prefix='\n-> ', | |
409 | context=None): |
|
402 | context=None): | |
410 | if context is None: |
|
403 | if context is None: | |
411 | context = self.context |
|
404 | context = self.context | |
412 | try: |
|
405 | try: | |
413 | context = int(context) |
|
406 | context = int(context) | |
414 | if context <= 0: |
|
407 | if context <= 0: | |
415 | raise ValueError("Context must be a positive integer") |
|
408 | raise ValueError("Context must be a positive integer") | |
416 | except (TypeError, ValueError) as e: |
|
409 | except (TypeError, ValueError) as e: | |
417 | raise ValueError("Context must be a positive integer") from e |
|
410 | raise ValueError("Context must be a positive integer") from e | |
418 | print(self.format_stack_entry(frame_lineno, '', context), file=self.stdout) |
|
411 | print(self.format_stack_entry(frame_lineno, '', context), file=self.stdout) | |
419 |
|
412 | |||
420 | # vds: >> |
|
413 | # vds: >> | |
421 | frame, lineno = frame_lineno |
|
414 | frame, lineno = frame_lineno | |
422 | filename = frame.f_code.co_filename |
|
415 | filename = frame.f_code.co_filename | |
423 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
416 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) | |
424 | # vds: << |
|
417 | # vds: << | |
425 |
|
418 | |||
426 | def _get_frame_locals(self, frame): |
|
419 | def _get_frame_locals(self, frame): | |
427 | """ " |
|
420 | """ " | |
428 | Accessing f_local of current frame reset the namespace, so we want to avoid |
|
421 | Accessing f_local of current frame reset the namespace, so we want to avoid | |
429 | that or the following can happen |
|
422 | that or the following can happen | |
430 |
|
423 | |||
431 | ipdb> foo |
|
424 | ipdb> foo | |
432 | "old" |
|
425 | "old" | |
433 | ipdb> foo = "new" |
|
426 | ipdb> foo = "new" | |
434 | ipdb> foo |
|
427 | ipdb> foo | |
435 | "new" |
|
428 | "new" | |
436 | ipdb> where |
|
429 | ipdb> where | |
437 | ipdb> foo |
|
430 | ipdb> foo | |
438 | "old" |
|
431 | "old" | |
439 |
|
432 | |||
440 | So if frame is self.current_frame we instead return self.curframe_locals |
|
433 | So if frame is self.current_frame we instead return self.curframe_locals | |
441 |
|
434 | |||
442 | """ |
|
435 | """ | |
443 | if frame is self.curframe: |
|
436 | if frame is self.curframe: | |
444 | return self.curframe_locals |
|
437 | return self.curframe_locals | |
445 | else: |
|
438 | else: | |
446 | return frame.f_locals |
|
439 | return frame.f_locals | |
447 |
|
440 | |||
448 | def format_stack_entry(self, frame_lineno, lprefix=': ', context=None): |
|
441 | def format_stack_entry(self, frame_lineno, lprefix=': ', context=None): | |
449 | if context is None: |
|
442 | if context is None: | |
450 | context = self.context |
|
443 | context = self.context | |
451 | try: |
|
444 | try: | |
452 | context = int(context) |
|
445 | context = int(context) | |
453 | if context <= 0: |
|
446 | if context <= 0: | |
454 | print("Context must be a positive integer", file=self.stdout) |
|
447 | print("Context must be a positive integer", file=self.stdout) | |
455 | except (TypeError, ValueError): |
|
448 | except (TypeError, ValueError): | |
456 | print("Context must be a positive integer", file=self.stdout) |
|
449 | print("Context must be a positive integer", file=self.stdout) | |
457 |
|
450 | |||
458 | import reprlib |
|
451 | import reprlib | |
459 |
|
452 | |||
460 | ret = [] |
|
453 | ret = [] | |
461 |
|
454 | |||
462 | Colors = self.color_scheme_table.active_colors |
|
455 | Colors = self.color_scheme_table.active_colors | |
463 | ColorsNormal = Colors.Normal |
|
456 | ColorsNormal = Colors.Normal | |
464 | tpl_link = "%s%%s%s" % (Colors.filenameEm, ColorsNormal) |
|
457 | tpl_link = "%s%%s%s" % (Colors.filenameEm, ColorsNormal) | |
465 | tpl_call = "%s%%s%s%%s%s" % (Colors.vName, Colors.valEm, ColorsNormal) |
|
458 | tpl_call = "%s%%s%s%%s%s" % (Colors.vName, Colors.valEm, ColorsNormal) | |
466 | tpl_line = "%%s%s%%s %s%%s" % (Colors.lineno, ColorsNormal) |
|
459 | tpl_line = "%%s%s%%s %s%%s" % (Colors.lineno, ColorsNormal) | |
467 | tpl_line_em = "%%s%s%%s %s%%s%s" % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
460 | tpl_line_em = "%%s%s%%s %s%%s%s" % (Colors.linenoEm, Colors.line, ColorsNormal) | |
468 |
|
461 | |||
469 | frame, lineno = frame_lineno |
|
462 | frame, lineno = frame_lineno | |
470 |
|
463 | |||
471 | return_value = '' |
|
464 | return_value = '' | |
472 | loc_frame = self._get_frame_locals(frame) |
|
465 | loc_frame = self._get_frame_locals(frame) | |
473 | if "__return__" in loc_frame: |
|
466 | if "__return__" in loc_frame: | |
474 | rv = loc_frame["__return__"] |
|
467 | rv = loc_frame["__return__"] | |
475 | # return_value += '->' |
|
468 | # return_value += '->' | |
476 | return_value += reprlib.repr(rv) + "\n" |
|
469 | return_value += reprlib.repr(rv) + "\n" | |
477 | ret.append(return_value) |
|
470 | ret.append(return_value) | |
478 |
|
471 | |||
479 | #s = filename + '(' + `lineno` + ')' |
|
472 | #s = filename + '(' + `lineno` + ')' | |
480 | filename = self.canonic(frame.f_code.co_filename) |
|
473 | filename = self.canonic(frame.f_code.co_filename) | |
481 | link = tpl_link % py3compat.cast_unicode(filename) |
|
474 | link = tpl_link % py3compat.cast_unicode(filename) | |
482 |
|
475 | |||
483 | if frame.f_code.co_name: |
|
476 | if frame.f_code.co_name: | |
484 | func = frame.f_code.co_name |
|
477 | func = frame.f_code.co_name | |
485 | else: |
|
478 | else: | |
486 | func = "<lambda>" |
|
479 | func = "<lambda>" | |
487 |
|
480 | |||
488 | call = "" |
|
481 | call = "" | |
489 | if func != "?": |
|
482 | if func != "?": | |
490 | if "__args__" in loc_frame: |
|
483 | if "__args__" in loc_frame: | |
491 | args = reprlib.repr(loc_frame["__args__"]) |
|
484 | args = reprlib.repr(loc_frame["__args__"]) | |
492 | else: |
|
485 | else: | |
493 | args = '()' |
|
486 | args = '()' | |
494 | call = tpl_call % (func, args) |
|
487 | call = tpl_call % (func, args) | |
495 |
|
488 | |||
496 | # The level info should be generated in the same format pdb uses, to |
|
489 | # The level info should be generated in the same format pdb uses, to | |
497 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. |
|
490 | # avoid breaking the pdbtrack functionality of python-mode in *emacs. | |
498 | if frame is self.curframe: |
|
491 | if frame is self.curframe: | |
499 | ret.append('> ') |
|
492 | ret.append('> ') | |
500 | else: |
|
493 | else: | |
501 | ret.append(" ") |
|
494 | ret.append(" ") | |
502 | ret.append("%s(%s)%s\n" % (link, lineno, call)) |
|
495 | ret.append("%s(%s)%s\n" % (link, lineno, call)) | |
503 |
|
496 | |||
504 | start = lineno - 1 - context//2 |
|
497 | start = lineno - 1 - context//2 | |
505 | lines = linecache.getlines(filename) |
|
498 | lines = linecache.getlines(filename) | |
506 | start = min(start, len(lines) - context) |
|
499 | start = min(start, len(lines) - context) | |
507 | start = max(start, 0) |
|
500 | start = max(start, 0) | |
508 | lines = lines[start : start + context] |
|
501 | lines = lines[start : start + context] | |
509 |
|
502 | |||
510 | for i, line in enumerate(lines): |
|
503 | for i, line in enumerate(lines): | |
511 | show_arrow = start + 1 + i == lineno |
|
504 | show_arrow = start + 1 + i == lineno | |
512 | linetpl = (frame is self.curframe or show_arrow) and tpl_line_em or tpl_line |
|
505 | linetpl = (frame is self.curframe or show_arrow) and tpl_line_em or tpl_line | |
513 | ret.append( |
|
506 | ret.append( | |
514 | self.__format_line( |
|
507 | self.__format_line( | |
515 | linetpl, filename, start + 1 + i, line, arrow=show_arrow |
|
508 | linetpl, filename, start + 1 + i, line, arrow=show_arrow | |
516 | ) |
|
509 | ) | |
517 | ) |
|
510 | ) | |
518 | return "".join(ret) |
|
511 | return "".join(ret) | |
519 |
|
512 | |||
520 | def __format_line(self, tpl_line, filename, lineno, line, arrow=False): |
|
513 | def __format_line(self, tpl_line, filename, lineno, line, arrow=False): | |
521 | bp_mark = "" |
|
514 | bp_mark = "" | |
522 | bp_mark_color = "" |
|
515 | bp_mark_color = "" | |
523 |
|
516 | |||
524 | new_line, err = self.parser.format2(line, 'str') |
|
517 | new_line, err = self.parser.format2(line, 'str') | |
525 | if not err: |
|
518 | if not err: | |
526 | line = new_line |
|
519 | line = new_line | |
527 |
|
520 | |||
528 | bp = None |
|
521 | bp = None | |
529 | if lineno in self.get_file_breaks(filename): |
|
522 | if lineno in self.get_file_breaks(filename): | |
530 | bps = self.get_breaks(filename, lineno) |
|
523 | bps = self.get_breaks(filename, lineno) | |
531 | bp = bps[-1] |
|
524 | bp = bps[-1] | |
532 |
|
525 | |||
533 | if bp: |
|
526 | if bp: | |
534 | Colors = self.color_scheme_table.active_colors |
|
527 | Colors = self.color_scheme_table.active_colors | |
535 | bp_mark = str(bp.number) |
|
528 | bp_mark = str(bp.number) | |
536 | bp_mark_color = Colors.breakpoint_enabled |
|
529 | bp_mark_color = Colors.breakpoint_enabled | |
537 | if not bp.enabled: |
|
530 | if not bp.enabled: | |
538 | bp_mark_color = Colors.breakpoint_disabled |
|
531 | bp_mark_color = Colors.breakpoint_disabled | |
539 |
|
532 | |||
540 | numbers_width = 7 |
|
533 | numbers_width = 7 | |
541 | if arrow: |
|
534 | if arrow: | |
542 | # This is the line with the error |
|
535 | # This is the line with the error | |
543 | pad = numbers_width - len(str(lineno)) - len(bp_mark) |
|
536 | pad = numbers_width - len(str(lineno)) - len(bp_mark) | |
544 | num = '%s%s' % (make_arrow(pad), str(lineno)) |
|
537 | num = '%s%s' % (make_arrow(pad), str(lineno)) | |
545 | else: |
|
538 | else: | |
546 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) |
|
539 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) | |
547 |
|
540 | |||
548 | return tpl_line % (bp_mark_color + bp_mark, num, line) |
|
541 | return tpl_line % (bp_mark_color + bp_mark, num, line) | |
549 |
|
542 | |||
550 | def print_list_lines(self, filename, first, last): |
|
543 | def print_list_lines(self, filename, first, last): | |
551 | """The printing (as opposed to the parsing part of a 'list' |
|
544 | """The printing (as opposed to the parsing part of a 'list' | |
552 | command.""" |
|
545 | command.""" | |
553 | try: |
|
546 | try: | |
554 | Colors = self.color_scheme_table.active_colors |
|
547 | Colors = self.color_scheme_table.active_colors | |
555 | ColorsNormal = Colors.Normal |
|
548 | ColorsNormal = Colors.Normal | |
556 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
549 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) | |
557 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
550 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) | |
558 | src = [] |
|
551 | src = [] | |
559 | if filename == "<string>" and hasattr(self, "_exec_filename"): |
|
552 | if filename == "<string>" and hasattr(self, "_exec_filename"): | |
560 | filename = self._exec_filename |
|
553 | filename = self._exec_filename | |
561 |
|
554 | |||
562 | for lineno in range(first, last+1): |
|
555 | for lineno in range(first, last+1): | |
563 | line = linecache.getline(filename, lineno) |
|
556 | line = linecache.getline(filename, lineno) | |
564 | if not line: |
|
557 | if not line: | |
565 | break |
|
558 | break | |
566 |
|
559 | |||
567 | if lineno == self.curframe.f_lineno: |
|
560 | if lineno == self.curframe.f_lineno: | |
568 | line = self.__format_line( |
|
561 | line = self.__format_line( | |
569 | tpl_line_em, filename, lineno, line, arrow=True |
|
562 | tpl_line_em, filename, lineno, line, arrow=True | |
570 | ) |
|
563 | ) | |
571 | else: |
|
564 | else: | |
572 | line = self.__format_line( |
|
565 | line = self.__format_line( | |
573 | tpl_line, filename, lineno, line, arrow=False |
|
566 | tpl_line, filename, lineno, line, arrow=False | |
574 | ) |
|
567 | ) | |
575 |
|
568 | |||
576 | src.append(line) |
|
569 | src.append(line) | |
577 | self.lineno = lineno |
|
570 | self.lineno = lineno | |
578 |
|
571 | |||
579 | print(''.join(src), file=self.stdout) |
|
572 | print(''.join(src), file=self.stdout) | |
580 |
|
573 | |||
581 | except KeyboardInterrupt: |
|
574 | except KeyboardInterrupt: | |
582 | pass |
|
575 | pass | |
583 |
|
576 | |||
584 | def do_skip_predicates(self, args): |
|
577 | def do_skip_predicates(self, args): | |
585 | """ |
|
578 | """ | |
586 | Turn on/off individual predicates as to whether a frame should be hidden/skip. |
|
579 | Turn on/off individual predicates as to whether a frame should be hidden/skip. | |
587 |
|
580 | |||
588 | The global option to skip (or not) hidden frames is set with skip_hidden |
|
581 | The global option to skip (or not) hidden frames is set with skip_hidden | |
589 |
|
582 | |||
590 | To change the value of a predicate |
|
583 | To change the value of a predicate | |
591 |
|
584 | |||
592 | skip_predicates key [true|false] |
|
585 | skip_predicates key [true|false] | |
593 |
|
586 | |||
594 | Call without arguments to see the current values. |
|
587 | Call without arguments to see the current values. | |
595 |
|
588 | |||
596 | To permanently change the value of an option add the corresponding |
|
589 | To permanently change the value of an option add the corresponding | |
597 | command to your ``~/.pdbrc`` file. If you are programmatically using the |
|
590 | command to your ``~/.pdbrc`` file. If you are programmatically using the | |
598 | Pdb instance you can also change the ``default_predicates`` class |
|
591 | Pdb instance you can also change the ``default_predicates`` class | |
599 | attribute. |
|
592 | attribute. | |
600 | """ |
|
593 | """ | |
601 | if not args.strip(): |
|
594 | if not args.strip(): | |
602 | print("current predicates:") |
|
595 | print("current predicates:") | |
603 | for (p, v) in self._predicates.items(): |
|
596 | for (p, v) in self._predicates.items(): | |
604 | print(" ", p, ":", v) |
|
597 | print(" ", p, ":", v) | |
605 | return |
|
598 | return | |
606 | type_value = args.strip().split(" ") |
|
599 | type_value = args.strip().split(" ") | |
607 | if len(type_value) != 2: |
|
600 | if len(type_value) != 2: | |
608 | print( |
|
601 | print( | |
609 | f"Usage: skip_predicates <type> <value>, with <type> one of {set(self._predicates.keys())}" |
|
602 | f"Usage: skip_predicates <type> <value>, with <type> one of {set(self._predicates.keys())}" | |
610 | ) |
|
603 | ) | |
611 | return |
|
604 | return | |
612 |
|
605 | |||
613 | type_, value = type_value |
|
606 | type_, value = type_value | |
614 | if type_ not in self._predicates: |
|
607 | if type_ not in self._predicates: | |
615 | print(f"{type_!r} not in {set(self._predicates.keys())}") |
|
608 | print(f"{type_!r} not in {set(self._predicates.keys())}") | |
616 | return |
|
609 | return | |
617 | if value.lower() not in ("true", "yes", "1", "no", "false", "0"): |
|
610 | if value.lower() not in ("true", "yes", "1", "no", "false", "0"): | |
618 | print( |
|
611 | print( | |
619 | f"{value!r} is invalid - use one of ('true', 'yes', '1', 'no', 'false', '0')" |
|
612 | f"{value!r} is invalid - use one of ('true', 'yes', '1', 'no', 'false', '0')" | |
620 | ) |
|
613 | ) | |
621 | return |
|
614 | return | |
622 |
|
615 | |||
623 | self._predicates[type_] = value.lower() in ("true", "yes", "1") |
|
616 | self._predicates[type_] = value.lower() in ("true", "yes", "1") | |
624 | if not any(self._predicates.values()): |
|
617 | if not any(self._predicates.values()): | |
625 | print( |
|
618 | print( | |
626 | "Warning, all predicates set to False, skip_hidden may not have any effects." |
|
619 | "Warning, all predicates set to False, skip_hidden may not have any effects." | |
627 | ) |
|
620 | ) | |
628 |
|
621 | |||
629 | def do_skip_hidden(self, arg): |
|
622 | def do_skip_hidden(self, arg): | |
630 | """ |
|
623 | """ | |
631 | Change whether or not we should skip frames with the |
|
624 | Change whether or not we should skip frames with the | |
632 | __tracebackhide__ attribute. |
|
625 | __tracebackhide__ attribute. | |
633 | """ |
|
626 | """ | |
634 | if not arg.strip(): |
|
627 | if not arg.strip(): | |
635 | print( |
|
628 | print( | |
636 | f"skip_hidden = {self.skip_hidden}, use 'yes','no', 'true', or 'false' to change." |
|
629 | f"skip_hidden = {self.skip_hidden}, use 'yes','no', 'true', or 'false' to change." | |
637 | ) |
|
630 | ) | |
638 | elif arg.strip().lower() in ("true", "yes"): |
|
631 | elif arg.strip().lower() in ("true", "yes"): | |
639 | self.skip_hidden = True |
|
632 | self.skip_hidden = True | |
640 | elif arg.strip().lower() in ("false", "no"): |
|
633 | elif arg.strip().lower() in ("false", "no"): | |
641 | self.skip_hidden = False |
|
634 | self.skip_hidden = False | |
642 | if not any(self._predicates.values()): |
|
635 | if not any(self._predicates.values()): | |
643 | print( |
|
636 | print( | |
644 | "Warning, all predicates set to False, skip_hidden may not have any effects." |
|
637 | "Warning, all predicates set to False, skip_hidden may not have any effects." | |
645 | ) |
|
638 | ) | |
646 |
|
639 | |||
647 | def do_list(self, arg): |
|
640 | def do_list(self, arg): | |
648 | """Print lines of code from the current stack frame |
|
641 | """Print lines of code from the current stack frame | |
649 | """ |
|
642 | """ | |
650 | self.lastcmd = 'list' |
|
643 | self.lastcmd = 'list' | |
651 | last = None |
|
644 | last = None | |
652 | if arg: |
|
645 | if arg: | |
653 | try: |
|
646 | try: | |
654 | x = eval(arg, {}, {}) |
|
647 | x = eval(arg, {}, {}) | |
655 | if type(x) == type(()): |
|
648 | if type(x) == type(()): | |
656 | first, last = x |
|
649 | first, last = x | |
657 | first = int(first) |
|
650 | first = int(first) | |
658 | last = int(last) |
|
651 | last = int(last) | |
659 | if last < first: |
|
652 | if last < first: | |
660 | # Assume it's a count |
|
653 | # Assume it's a count | |
661 | last = first + last |
|
654 | last = first + last | |
662 | else: |
|
655 | else: | |
663 | first = max(1, int(x) - 5) |
|
656 | first = max(1, int(x) - 5) | |
664 | except: |
|
657 | except: | |
665 | print('*** Error in argument:', repr(arg), file=self.stdout) |
|
658 | print('*** Error in argument:', repr(arg), file=self.stdout) | |
666 | return |
|
659 | return | |
667 | elif self.lineno is None: |
|
660 | elif self.lineno is None: | |
668 | first = max(1, self.curframe.f_lineno - 5) |
|
661 | first = max(1, self.curframe.f_lineno - 5) | |
669 | else: |
|
662 | else: | |
670 | first = self.lineno + 1 |
|
663 | first = self.lineno + 1 | |
671 | if last is None: |
|
664 | if last is None: | |
672 | last = first + 10 |
|
665 | last = first + 10 | |
673 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) |
|
666 | self.print_list_lines(self.curframe.f_code.co_filename, first, last) | |
674 |
|
667 | |||
675 | # vds: >> |
|
668 | # vds: >> | |
676 | lineno = first |
|
669 | lineno = first | |
677 | filename = self.curframe.f_code.co_filename |
|
670 | filename = self.curframe.f_code.co_filename | |
678 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) |
|
671 | self.shell.hooks.synchronize_with_editor(filename, lineno, 0) | |
679 | # vds: << |
|
672 | # vds: << | |
680 |
|
673 | |||
681 | do_l = do_list |
|
674 | do_l = do_list | |
682 |
|
675 | |||
683 | def getsourcelines(self, obj): |
|
676 | def getsourcelines(self, obj): | |
684 | lines, lineno = inspect.findsource(obj) |
|
677 | lines, lineno = inspect.findsource(obj) | |
685 | if inspect.isframe(obj) and obj.f_globals is self._get_frame_locals(obj): |
|
678 | if inspect.isframe(obj) and obj.f_globals is self._get_frame_locals(obj): | |
686 | # must be a module frame: do not try to cut a block out of it |
|
679 | # must be a module frame: do not try to cut a block out of it | |
687 | return lines, 1 |
|
680 | return lines, 1 | |
688 | elif inspect.ismodule(obj): |
|
681 | elif inspect.ismodule(obj): | |
689 | return lines, 1 |
|
682 | return lines, 1 | |
690 | return inspect.getblock(lines[lineno:]), lineno+1 |
|
683 | return inspect.getblock(lines[lineno:]), lineno+1 | |
691 |
|
684 | |||
692 | def do_longlist(self, arg): |
|
685 | def do_longlist(self, arg): | |
693 | """Print lines of code from the current stack frame. |
|
686 | """Print lines of code from the current stack frame. | |
694 |
|
687 | |||
695 | Shows more lines than 'list' does. |
|
688 | Shows more lines than 'list' does. | |
696 | """ |
|
689 | """ | |
697 | self.lastcmd = 'longlist' |
|
690 | self.lastcmd = 'longlist' | |
698 | try: |
|
691 | try: | |
699 | lines, lineno = self.getsourcelines(self.curframe) |
|
692 | lines, lineno = self.getsourcelines(self.curframe) | |
700 | except OSError as err: |
|
693 | except OSError as err: | |
701 | self.error(err) |
|
694 | self.error(err) | |
702 | return |
|
695 | return | |
703 | last = lineno + len(lines) |
|
696 | last = lineno + len(lines) | |
704 | self.print_list_lines(self.curframe.f_code.co_filename, lineno, last) |
|
697 | self.print_list_lines(self.curframe.f_code.co_filename, lineno, last) | |
705 | do_ll = do_longlist |
|
698 | do_ll = do_longlist | |
706 |
|
699 | |||
707 | def do_debug(self, arg): |
|
700 | def do_debug(self, arg): | |
708 | """debug code |
|
701 | """debug code | |
709 | Enter a recursive debugger that steps through the code |
|
702 | Enter a recursive debugger that steps through the code | |
710 | argument (which is an arbitrary expression or statement to be |
|
703 | argument (which is an arbitrary expression or statement to be | |
711 | executed in the current environment). |
|
704 | executed in the current environment). | |
712 | """ |
|
705 | """ | |
713 | trace_function = sys.gettrace() |
|
706 | trace_function = sys.gettrace() | |
714 | sys.settrace(None) |
|
707 | sys.settrace(None) | |
715 | globals = self.curframe.f_globals |
|
708 | globals = self.curframe.f_globals | |
716 | locals = self.curframe_locals |
|
709 | locals = self.curframe_locals | |
717 | p = self.__class__(completekey=self.completekey, |
|
710 | p = self.__class__(completekey=self.completekey, | |
718 | stdin=self.stdin, stdout=self.stdout) |
|
711 | stdin=self.stdin, stdout=self.stdout) | |
719 | p.use_rawinput = self.use_rawinput |
|
712 | p.use_rawinput = self.use_rawinput | |
720 | p.prompt = "(%s) " % self.prompt.strip() |
|
713 | p.prompt = "(%s) " % self.prompt.strip() | |
721 | self.message("ENTERING RECURSIVE DEBUGGER") |
|
714 | self.message("ENTERING RECURSIVE DEBUGGER") | |
722 | sys.call_tracing(p.run, (arg, globals, locals)) |
|
715 | sys.call_tracing(p.run, (arg, globals, locals)) | |
723 | self.message("LEAVING RECURSIVE DEBUGGER") |
|
716 | self.message("LEAVING RECURSIVE DEBUGGER") | |
724 | sys.settrace(trace_function) |
|
717 | sys.settrace(trace_function) | |
725 | self.lastcmd = p.lastcmd |
|
718 | self.lastcmd = p.lastcmd | |
726 |
|
719 | |||
727 | def do_pdef(self, arg): |
|
720 | def do_pdef(self, arg): | |
728 | """Print the call signature for any callable object. |
|
721 | """Print the call signature for any callable object. | |
729 |
|
722 | |||
730 | The debugger interface to %pdef""" |
|
723 | The debugger interface to %pdef""" | |
731 | namespaces = [ |
|
724 | namespaces = [ | |
732 | ("Locals", self.curframe_locals), |
|
725 | ("Locals", self.curframe_locals), | |
733 | ("Globals", self.curframe.f_globals), |
|
726 | ("Globals", self.curframe.f_globals), | |
734 | ] |
|
727 | ] | |
735 | self.shell.find_line_magic("pdef")(arg, namespaces=namespaces) |
|
728 | self.shell.find_line_magic("pdef")(arg, namespaces=namespaces) | |
736 |
|
729 | |||
737 | def do_pdoc(self, arg): |
|
730 | def do_pdoc(self, arg): | |
738 | """Print the docstring for an object. |
|
731 | """Print the docstring for an object. | |
739 |
|
732 | |||
740 | The debugger interface to %pdoc.""" |
|
733 | The debugger interface to %pdoc.""" | |
741 | namespaces = [ |
|
734 | namespaces = [ | |
742 | ("Locals", self.curframe_locals), |
|
735 | ("Locals", self.curframe_locals), | |
743 | ("Globals", self.curframe.f_globals), |
|
736 | ("Globals", self.curframe.f_globals), | |
744 | ] |
|
737 | ] | |
745 | self.shell.find_line_magic("pdoc")(arg, namespaces=namespaces) |
|
738 | self.shell.find_line_magic("pdoc")(arg, namespaces=namespaces) | |
746 |
|
739 | |||
747 | def do_pfile(self, arg): |
|
740 | def do_pfile(self, arg): | |
748 | """Print (or run through pager) the file where an object is defined. |
|
741 | """Print (or run through pager) the file where an object is defined. | |
749 |
|
742 | |||
750 | The debugger interface to %pfile. |
|
743 | The debugger interface to %pfile. | |
751 | """ |
|
744 | """ | |
752 | namespaces = [ |
|
745 | namespaces = [ | |
753 | ("Locals", self.curframe_locals), |
|
746 | ("Locals", self.curframe_locals), | |
754 | ("Globals", self.curframe.f_globals), |
|
747 | ("Globals", self.curframe.f_globals), | |
755 | ] |
|
748 | ] | |
756 | self.shell.find_line_magic("pfile")(arg, namespaces=namespaces) |
|
749 | self.shell.find_line_magic("pfile")(arg, namespaces=namespaces) | |
757 |
|
750 | |||
758 | def do_pinfo(self, arg): |
|
751 | def do_pinfo(self, arg): | |
759 | """Provide detailed information about an object. |
|
752 | """Provide detailed information about an object. | |
760 |
|
753 | |||
761 | The debugger interface to %pinfo, i.e., obj?.""" |
|
754 | The debugger interface to %pinfo, i.e., obj?.""" | |
762 | namespaces = [ |
|
755 | namespaces = [ | |
763 | ("Locals", self.curframe_locals), |
|
756 | ("Locals", self.curframe_locals), | |
764 | ("Globals", self.curframe.f_globals), |
|
757 | ("Globals", self.curframe.f_globals), | |
765 | ] |
|
758 | ] | |
766 | self.shell.find_line_magic("pinfo")(arg, namespaces=namespaces) |
|
759 | self.shell.find_line_magic("pinfo")(arg, namespaces=namespaces) | |
767 |
|
760 | |||
768 | def do_pinfo2(self, arg): |
|
761 | def do_pinfo2(self, arg): | |
769 | """Provide extra detailed information about an object. |
|
762 | """Provide extra detailed information about an object. | |
770 |
|
763 | |||
771 | The debugger interface to %pinfo2, i.e., obj??.""" |
|
764 | The debugger interface to %pinfo2, i.e., obj??.""" | |
772 | namespaces = [ |
|
765 | namespaces = [ | |
773 | ("Locals", self.curframe_locals), |
|
766 | ("Locals", self.curframe_locals), | |
774 | ("Globals", self.curframe.f_globals), |
|
767 | ("Globals", self.curframe.f_globals), | |
775 | ] |
|
768 | ] | |
776 | self.shell.find_line_magic("pinfo2")(arg, namespaces=namespaces) |
|
769 | self.shell.find_line_magic("pinfo2")(arg, namespaces=namespaces) | |
777 |
|
770 | |||
778 | def do_psource(self, arg): |
|
771 | def do_psource(self, arg): | |
779 | """Print (or run through pager) the source code for an object.""" |
|
772 | """Print (or run through pager) the source code for an object.""" | |
780 | namespaces = [ |
|
773 | namespaces = [ | |
781 | ("Locals", self.curframe_locals), |
|
774 | ("Locals", self.curframe_locals), | |
782 | ("Globals", self.curframe.f_globals), |
|
775 | ("Globals", self.curframe.f_globals), | |
783 | ] |
|
776 | ] | |
784 | self.shell.find_line_magic("psource")(arg, namespaces=namespaces) |
|
777 | self.shell.find_line_magic("psource")(arg, namespaces=namespaces) | |
785 |
|
778 | |||
786 | def do_where(self, arg): |
|
779 | def do_where(self, arg): | |
787 | """w(here) |
|
780 | """w(here) | |
788 | Print a stack trace, with the most recent frame at the bottom. |
|
781 | Print a stack trace, with the most recent frame at the bottom. | |
789 | An arrow indicates the "current frame", which determines the |
|
782 | An arrow indicates the "current frame", which determines the | |
790 | context of most commands. 'bt' is an alias for this command. |
|
783 | context of most commands. 'bt' is an alias for this command. | |
791 |
|
784 | |||
792 | Take a number as argument as an (optional) number of context line to |
|
785 | Take a number as argument as an (optional) number of context line to | |
793 | print""" |
|
786 | print""" | |
794 | if arg: |
|
787 | if arg: | |
795 | try: |
|
788 | try: | |
796 | context = int(arg) |
|
789 | context = int(arg) | |
797 | except ValueError as err: |
|
790 | except ValueError as err: | |
798 | self.error(err) |
|
791 | self.error(err) | |
799 | return |
|
792 | return | |
800 | self.print_stack_trace(context) |
|
793 | self.print_stack_trace(context) | |
801 | else: |
|
794 | else: | |
802 | self.print_stack_trace() |
|
795 | self.print_stack_trace() | |
803 |
|
796 | |||
804 | do_w = do_where |
|
797 | do_w = do_where | |
805 |
|
798 | |||
806 | def break_anywhere(self, frame): |
|
799 | def break_anywhere(self, frame): | |
807 | """ |
|
800 | """ | |
808 |
|
801 | |||
809 | _stop_in_decorator_internals is overly restrictive, as we may still want |
|
802 | _stop_in_decorator_internals is overly restrictive, as we may still want | |
810 | to trace function calls, so we need to also update break_anywhere so |
|
803 | to trace function calls, so we need to also update break_anywhere so | |
811 | that is we don't `stop_here`, because of debugger skip, we may still |
|
804 | that is we don't `stop_here`, because of debugger skip, we may still | |
812 | stop at any point inside the function |
|
805 | stop at any point inside the function | |
813 |
|
806 | |||
814 | """ |
|
807 | """ | |
815 |
|
808 | |||
816 | sup = super().break_anywhere(frame) |
|
809 | sup = super().break_anywhere(frame) | |
817 | if sup: |
|
810 | if sup: | |
818 | return sup |
|
811 | return sup | |
819 | if self._predicates["debuggerskip"]: |
|
812 | if self._predicates["debuggerskip"]: | |
820 | if DEBUGGERSKIP in frame.f_code.co_varnames: |
|
813 | if DEBUGGERSKIP in frame.f_code.co_varnames: | |
821 | return True |
|
814 | return True | |
822 | if frame.f_back and self._get_frame_locals(frame.f_back).get(DEBUGGERSKIP): |
|
815 | if frame.f_back and self._get_frame_locals(frame.f_back).get(DEBUGGERSKIP): | |
823 | return True |
|
816 | return True | |
824 | return False |
|
817 | return False | |
825 |
|
818 | |||
826 | def _is_in_decorator_internal_and_should_skip(self, frame): |
|
819 | def _is_in_decorator_internal_and_should_skip(self, frame): | |
827 | """ |
|
820 | """ | |
828 | Utility to tell us whether we are in a decorator internal and should stop. |
|
821 | Utility to tell us whether we are in a decorator internal and should stop. | |
829 |
|
822 | |||
830 |
|
823 | |||
831 |
|
824 | |||
832 | """ |
|
825 | """ | |
833 |
|
826 | |||
834 | # if we are disabled don't skip |
|
827 | # if we are disabled don't skip | |
835 | if not self._predicates["debuggerskip"]: |
|
828 | if not self._predicates["debuggerskip"]: | |
836 | return False |
|
829 | return False | |
837 |
|
830 | |||
838 | # if frame is tagged, skip by default. |
|
831 | # if frame is tagged, skip by default. | |
839 | if DEBUGGERSKIP in frame.f_code.co_varnames: |
|
832 | if DEBUGGERSKIP in frame.f_code.co_varnames: | |
840 | return True |
|
833 | return True | |
841 |
|
834 | |||
842 | # if one of the parent frame value set to True skip as well. |
|
835 | # if one of the parent frame value set to True skip as well. | |
843 |
|
836 | |||
844 | cframe = frame |
|
837 | cframe = frame | |
845 | while getattr(cframe, "f_back", None): |
|
838 | while getattr(cframe, "f_back", None): | |
846 | cframe = cframe.f_back |
|
839 | cframe = cframe.f_back | |
847 | if self._get_frame_locals(cframe).get(DEBUGGERSKIP): |
|
840 | if self._get_frame_locals(cframe).get(DEBUGGERSKIP): | |
848 | return True |
|
841 | return True | |
849 |
|
842 | |||
850 | return False |
|
843 | return False | |
851 |
|
844 | |||
852 | def stop_here(self, frame): |
|
845 | def stop_here(self, frame): | |
853 |
|
846 | |||
854 | if self._is_in_decorator_internal_and_should_skip(frame) is True: |
|
847 | if self._is_in_decorator_internal_and_should_skip(frame) is True: | |
855 | return False |
|
848 | return False | |
856 |
|
849 | |||
857 | hidden = False |
|
850 | hidden = False | |
858 | if self.skip_hidden: |
|
851 | if self.skip_hidden: | |
859 | hidden = self._hidden_predicate(frame) |
|
852 | hidden = self._hidden_predicate(frame) | |
860 | if hidden: |
|
853 | if hidden: | |
861 | if self.report_skipped: |
|
854 | if self.report_skipped: | |
862 | Colors = self.color_scheme_table.active_colors |
|
855 | Colors = self.color_scheme_table.active_colors | |
863 | ColorsNormal = Colors.Normal |
|
856 | ColorsNormal = Colors.Normal | |
864 | print( |
|
857 | print( | |
865 | f"{Colors.excName} [... skipped 1 hidden frame]{ColorsNormal}\n" |
|
858 | f"{Colors.excName} [... skipped 1 hidden frame]{ColorsNormal}\n" | |
866 | ) |
|
859 | ) | |
867 | return super().stop_here(frame) |
|
860 | return super().stop_here(frame) | |
868 |
|
861 | |||
869 | def do_up(self, arg): |
|
862 | def do_up(self, arg): | |
870 | """u(p) [count] |
|
863 | """u(p) [count] | |
871 | Move the current frame count (default one) levels up in the |
|
864 | Move the current frame count (default one) levels up in the | |
872 | stack trace (to an older frame). |
|
865 | stack trace (to an older frame). | |
873 |
|
866 | |||
874 | Will skip hidden frames. |
|
867 | Will skip hidden frames. | |
875 | """ |
|
868 | """ | |
876 | # modified version of upstream that skips |
|
869 | # modified version of upstream that skips | |
877 | # frames with __tracebackhide__ |
|
870 | # frames with __tracebackhide__ | |
878 | if self.curindex == 0: |
|
871 | if self.curindex == 0: | |
879 | self.error("Oldest frame") |
|
872 | self.error("Oldest frame") | |
880 | return |
|
873 | return | |
881 | try: |
|
874 | try: | |
882 | count = int(arg or 1) |
|
875 | count = int(arg or 1) | |
883 | except ValueError: |
|
876 | except ValueError: | |
884 | self.error("Invalid frame count (%s)" % arg) |
|
877 | self.error("Invalid frame count (%s)" % arg) | |
885 | return |
|
878 | return | |
886 | skipped = 0 |
|
879 | skipped = 0 | |
887 | if count < 0: |
|
880 | if count < 0: | |
888 | _newframe = 0 |
|
881 | _newframe = 0 | |
889 | else: |
|
882 | else: | |
890 | counter = 0 |
|
883 | counter = 0 | |
891 | hidden_frames = self.hidden_frames(self.stack) |
|
884 | hidden_frames = self.hidden_frames(self.stack) | |
892 | for i in range(self.curindex - 1, -1, -1): |
|
885 | for i in range(self.curindex - 1, -1, -1): | |
893 | if hidden_frames[i] and self.skip_hidden: |
|
886 | if hidden_frames[i] and self.skip_hidden: | |
894 | skipped += 1 |
|
887 | skipped += 1 | |
895 | continue |
|
888 | continue | |
896 | counter += 1 |
|
889 | counter += 1 | |
897 | if counter >= count: |
|
890 | if counter >= count: | |
898 | break |
|
891 | break | |
899 | else: |
|
892 | else: | |
900 | # if no break occurred. |
|
893 | # if no break occurred. | |
901 | self.error( |
|
894 | self.error( | |
902 | "all frames above hidden, use `skip_hidden False` to get get into those." |
|
895 | "all frames above hidden, use `skip_hidden False` to get get into those." | |
903 | ) |
|
896 | ) | |
904 | return |
|
897 | return | |
905 |
|
898 | |||
906 | Colors = self.color_scheme_table.active_colors |
|
899 | Colors = self.color_scheme_table.active_colors | |
907 | ColorsNormal = Colors.Normal |
|
900 | ColorsNormal = Colors.Normal | |
908 | _newframe = i |
|
901 | _newframe = i | |
909 | self._select_frame(_newframe) |
|
902 | self._select_frame(_newframe) | |
910 | if skipped: |
|
903 | if skipped: | |
911 | print( |
|
904 | print( | |
912 | f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n" |
|
905 | f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n" | |
913 | ) |
|
906 | ) | |
914 |
|
907 | |||
915 | def do_down(self, arg): |
|
908 | def do_down(self, arg): | |
916 | """d(own) [count] |
|
909 | """d(own) [count] | |
917 | Move the current frame count (default one) levels down in the |
|
910 | Move the current frame count (default one) levels down in the | |
918 | stack trace (to a newer frame). |
|
911 | stack trace (to a newer frame). | |
919 |
|
912 | |||
920 | Will skip hidden frames. |
|
913 | Will skip hidden frames. | |
921 | """ |
|
914 | """ | |
922 | if self.curindex + 1 == len(self.stack): |
|
915 | if self.curindex + 1 == len(self.stack): | |
923 | self.error("Newest frame") |
|
916 | self.error("Newest frame") | |
924 | return |
|
917 | return | |
925 | try: |
|
918 | try: | |
926 | count = int(arg or 1) |
|
919 | count = int(arg or 1) | |
927 | except ValueError: |
|
920 | except ValueError: | |
928 | self.error("Invalid frame count (%s)" % arg) |
|
921 | self.error("Invalid frame count (%s)" % arg) | |
929 | return |
|
922 | return | |
930 | if count < 0: |
|
923 | if count < 0: | |
931 | _newframe = len(self.stack) - 1 |
|
924 | _newframe = len(self.stack) - 1 | |
932 | else: |
|
925 | else: | |
933 | counter = 0 |
|
926 | counter = 0 | |
934 | skipped = 0 |
|
927 | skipped = 0 | |
935 | hidden_frames = self.hidden_frames(self.stack) |
|
928 | hidden_frames = self.hidden_frames(self.stack) | |
936 | for i in range(self.curindex + 1, len(self.stack)): |
|
929 | for i in range(self.curindex + 1, len(self.stack)): | |
937 | if hidden_frames[i] and self.skip_hidden: |
|
930 | if hidden_frames[i] and self.skip_hidden: | |
938 | skipped += 1 |
|
931 | skipped += 1 | |
939 | continue |
|
932 | continue | |
940 | counter += 1 |
|
933 | counter += 1 | |
941 | if counter >= count: |
|
934 | if counter >= count: | |
942 | break |
|
935 | break | |
943 | else: |
|
936 | else: | |
944 | self.error( |
|
937 | self.error( | |
945 | "all frames below hidden, use `skip_hidden False` to get get into those." |
|
938 | "all frames below hidden, use `skip_hidden False` to get get into those." | |
946 | ) |
|
939 | ) | |
947 | return |
|
940 | return | |
948 |
|
941 | |||
949 | Colors = self.color_scheme_table.active_colors |
|
942 | Colors = self.color_scheme_table.active_colors | |
950 | ColorsNormal = Colors.Normal |
|
943 | ColorsNormal = Colors.Normal | |
951 | if skipped: |
|
944 | if skipped: | |
952 | print( |
|
945 | print( | |
953 | f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n" |
|
946 | f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n" | |
954 | ) |
|
947 | ) | |
955 | _newframe = i |
|
948 | _newframe = i | |
956 |
|
949 | |||
957 | self._select_frame(_newframe) |
|
950 | self._select_frame(_newframe) | |
958 |
|
951 | |||
959 | do_d = do_down |
|
952 | do_d = do_down | |
960 | do_u = do_up |
|
953 | do_u = do_up | |
961 |
|
954 | |||
962 | def do_context(self, context): |
|
955 | def do_context(self, context): | |
963 | """context number_of_lines |
|
956 | """context number_of_lines | |
964 | Set the number of lines of source code to show when displaying |
|
957 | Set the number of lines of source code to show when displaying | |
965 | stacktrace information. |
|
958 | stacktrace information. | |
966 | """ |
|
959 | """ | |
967 | try: |
|
960 | try: | |
968 | new_context = int(context) |
|
961 | new_context = int(context) | |
969 | if new_context <= 0: |
|
962 | if new_context <= 0: | |
970 | raise ValueError() |
|
963 | raise ValueError() | |
971 | self.context = new_context |
|
964 | self.context = new_context | |
972 | except ValueError: |
|
965 | except ValueError: | |
973 | self.error("The 'context' command requires a positive integer argument.") |
|
966 | self.error("The 'context' command requires a positive integer argument.") | |
974 |
|
967 | |||
975 |
|
968 | |||
976 | class InterruptiblePdb(Pdb): |
|
969 | class InterruptiblePdb(Pdb): | |
977 | """Version of debugger where KeyboardInterrupt exits the debugger altogether.""" |
|
970 | """Version of debugger where KeyboardInterrupt exits the debugger altogether.""" | |
978 |
|
971 | |||
979 | def cmdloop(self, intro=None): |
|
972 | def cmdloop(self, intro=None): | |
980 | """Wrap cmdloop() such that KeyboardInterrupt stops the debugger.""" |
|
973 | """Wrap cmdloop() such that KeyboardInterrupt stops the debugger.""" | |
981 | try: |
|
974 | try: | |
982 | return OldPdb.cmdloop(self, intro=intro) |
|
975 | return OldPdb.cmdloop(self, intro=intro) | |
983 | except KeyboardInterrupt: |
|
976 | except KeyboardInterrupt: | |
984 | self.stop_here = lambda frame: False |
|
977 | self.stop_here = lambda frame: False | |
985 | self.do_quit("") |
|
978 | self.do_quit("") | |
986 | sys.settrace(None) |
|
979 | sys.settrace(None) | |
987 | self.quitting = False |
|
980 | self.quitting = False | |
988 | raise |
|
981 | raise | |
989 |
|
982 | |||
990 | def _cmdloop(self): |
|
983 | def _cmdloop(self): | |
991 | while True: |
|
984 | while True: | |
992 | try: |
|
985 | try: | |
993 | # keyboard interrupts allow for an easy way to cancel |
|
986 | # keyboard interrupts allow for an easy way to cancel | |
994 | # the current command, so allow them during interactive input |
|
987 | # the current command, so allow them during interactive input | |
995 | self.allow_kbdint = True |
|
988 | self.allow_kbdint = True | |
996 | self.cmdloop() |
|
989 | self.cmdloop() | |
997 | self.allow_kbdint = False |
|
990 | self.allow_kbdint = False | |
998 | break |
|
991 | break | |
999 | except KeyboardInterrupt: |
|
992 | except KeyboardInterrupt: | |
1000 | self.message('--KeyboardInterrupt--') |
|
993 | self.message('--KeyboardInterrupt--') | |
1001 | raise |
|
994 | raise | |
1002 |
|
995 | |||
1003 |
|
996 | |||
1004 | def set_trace(frame=None): |
|
997 | def set_trace(frame=None): | |
1005 | """ |
|
998 | """ | |
1006 | Start debugging from `frame`. |
|
999 | Start debugging from `frame`. | |
1007 |
|
1000 | |||
1008 | If frame is not specified, debugging starts from caller's frame. |
|
1001 | If frame is not specified, debugging starts from caller's frame. | |
1009 | """ |
|
1002 | """ | |
1010 | Pdb().set_trace(frame or sys._getframe().f_back) |
|
1003 | Pdb().set_trace(frame or sys._getframe().f_back) |
@@ -1,1026 +1,1024 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Display formatters. |
|
2 | """Display formatters. | |
3 |
|
3 | |||
4 | Inheritance diagram: |
|
4 | Inheritance diagram: | |
5 |
|
5 | |||
6 | .. inheritance-diagram:: IPython.core.formatters |
|
6 | .. inheritance-diagram:: IPython.core.formatters | |
7 | :parts: 3 |
|
7 | :parts: 3 | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | # Copyright (c) IPython Development Team. |
|
10 | # Copyright (c) IPython Development Team. | |
11 | # Distributed under the terms of the Modified BSD License. |
|
11 | # Distributed under the terms of the Modified BSD License. | |
12 |
|
12 | |||
13 | import abc |
|
13 | import abc | |
14 | import json |
|
14 | import json | |
15 | import sys |
|
15 | import sys | |
16 | import traceback |
|
16 | import traceback | |
17 | import warnings |
|
17 | import warnings | |
18 | from io import StringIO |
|
18 | from io import StringIO | |
19 |
|
19 | |||
20 | from decorator import decorator |
|
20 | from decorator import decorator | |
21 |
|
21 | |||
22 | from traitlets.config.configurable import Configurable |
|
22 | from traitlets.config.configurable import Configurable | |
23 | from .getipython import get_ipython |
|
23 | from .getipython import get_ipython | |
24 | from ..utils.sentinel import Sentinel |
|
24 | from ..utils.sentinel import Sentinel | |
25 | from ..utils.dir2 import get_real_method |
|
25 | from ..utils.dir2 import get_real_method | |
26 | from ..lib import pretty |
|
26 | from ..lib import pretty | |
27 | from traitlets import ( |
|
27 | from traitlets import ( | |
28 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, |
|
28 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, | |
29 | ForwardDeclaredInstance, |
|
29 | ForwardDeclaredInstance, | |
30 | default, observe, |
|
30 | default, observe, | |
31 | ) |
|
31 | ) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class DisplayFormatter(Configurable): |
|
34 | class DisplayFormatter(Configurable): | |
35 |
|
35 | |||
36 | active_types = List(Unicode(), |
|
36 | active_types = List(Unicode(), | |
37 | help="""List of currently active mime-types to display. |
|
37 | help="""List of currently active mime-types to display. | |
38 | You can use this to set a white-list for formats to display. |
|
38 | You can use this to set a white-list for formats to display. | |
39 |
|
39 | |||
40 | Most users will not need to change this value. |
|
40 | Most users will not need to change this value. | |
41 | """).tag(config=True) |
|
41 | """).tag(config=True) | |
42 |
|
42 | |||
43 | @default('active_types') |
|
43 | @default('active_types') | |
44 | def _active_types_default(self): |
|
44 | def _active_types_default(self): | |
45 | return self.format_types |
|
45 | return self.format_types | |
46 |
|
46 | |||
47 | @observe('active_types') |
|
47 | @observe('active_types') | |
48 | def _active_types_changed(self, change): |
|
48 | def _active_types_changed(self, change): | |
49 | for key, formatter in self.formatters.items(): |
|
49 | for key, formatter in self.formatters.items(): | |
50 | if key in change['new']: |
|
50 | if key in change['new']: | |
51 | formatter.enabled = True |
|
51 | formatter.enabled = True | |
52 | else: |
|
52 | else: | |
53 | formatter.enabled = False |
|
53 | formatter.enabled = False | |
54 |
|
54 | |||
55 | ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') |
|
55 | ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') | |
56 | @default('ipython_display_formatter') |
|
56 | @default('ipython_display_formatter') | |
57 | def _default_formatter(self): |
|
57 | def _default_formatter(self): | |
58 | return IPythonDisplayFormatter(parent=self) |
|
58 | return IPythonDisplayFormatter(parent=self) | |
59 |
|
59 | |||
60 | mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') |
|
60 | mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') | |
61 | @default('mimebundle_formatter') |
|
61 | @default('mimebundle_formatter') | |
62 | def _default_mime_formatter(self): |
|
62 | def _default_mime_formatter(self): | |
63 | return MimeBundleFormatter(parent=self) |
|
63 | return MimeBundleFormatter(parent=self) | |
64 |
|
64 | |||
65 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
65 | # A dict of formatter whose keys are format types (MIME types) and whose | |
66 | # values are subclasses of BaseFormatter. |
|
66 | # values are subclasses of BaseFormatter. | |
67 | formatters = Dict() |
|
67 | formatters = Dict() | |
68 | @default('formatters') |
|
68 | @default('formatters') | |
69 | def _formatters_default(self): |
|
69 | def _formatters_default(self): | |
70 | """Activate the default formatters.""" |
|
70 | """Activate the default formatters.""" | |
71 | formatter_classes = [ |
|
71 | formatter_classes = [ | |
72 | PlainTextFormatter, |
|
72 | PlainTextFormatter, | |
73 | HTMLFormatter, |
|
73 | HTMLFormatter, | |
74 | MarkdownFormatter, |
|
74 | MarkdownFormatter, | |
75 | SVGFormatter, |
|
75 | SVGFormatter, | |
76 | PNGFormatter, |
|
76 | PNGFormatter, | |
77 | PDFFormatter, |
|
77 | PDFFormatter, | |
78 | JPEGFormatter, |
|
78 | JPEGFormatter, | |
79 | LatexFormatter, |
|
79 | LatexFormatter, | |
80 | JSONFormatter, |
|
80 | JSONFormatter, | |
81 | JavascriptFormatter |
|
81 | JavascriptFormatter | |
82 | ] |
|
82 | ] | |
83 | d = {} |
|
83 | d = {} | |
84 | for cls in formatter_classes: |
|
84 | for cls in formatter_classes: | |
85 | f = cls(parent=self) |
|
85 | f = cls(parent=self) | |
86 | d[f.format_type] = f |
|
86 | d[f.format_type] = f | |
87 | return d |
|
87 | return d | |
88 |
|
88 | |||
89 | def format(self, obj, include=None, exclude=None): |
|
89 | def format(self, obj, include=None, exclude=None): | |
90 | """Return a format data dict for an object. |
|
90 | """Return a format data dict for an object. | |
91 |
|
91 | |||
92 | By default all format types will be computed. |
|
92 | By default all format types will be computed. | |
93 |
|
93 | |||
94 | The following MIME types are usually implemented: |
|
94 | The following MIME types are usually implemented: | |
95 |
|
95 | |||
96 | * text/plain |
|
96 | * text/plain | |
97 | * text/html |
|
97 | * text/html | |
98 | * text/markdown |
|
98 | * text/markdown | |
99 | * text/latex |
|
99 | * text/latex | |
100 | * application/json |
|
100 | * application/json | |
101 | * application/javascript |
|
101 | * application/javascript | |
102 | * application/pdf |
|
102 | * application/pdf | |
103 | * image/png |
|
103 | * image/png | |
104 | * image/jpeg |
|
104 | * image/jpeg | |
105 | * image/svg+xml |
|
105 | * image/svg+xml | |
106 |
|
106 | |||
107 | Parameters |
|
107 | Parameters | |
108 | ---------- |
|
108 | ---------- | |
109 | obj : object |
|
109 | obj : object | |
110 | The Python object whose format data will be computed. |
|
110 | The Python object whose format data will be computed. | |
111 | include : list, tuple or set; optional |
|
111 | include : list, tuple or set; optional | |
112 | A list of format type strings (MIME types) to include in the |
|
112 | A list of format type strings (MIME types) to include in the | |
113 | format data dict. If this is set *only* the format types included |
|
113 | format data dict. If this is set *only* the format types included | |
114 | in this list will be computed. |
|
114 | in this list will be computed. | |
115 | exclude : list, tuple or set; optional |
|
115 | exclude : list, tuple or set; optional | |
116 | A list of format type string (MIME types) to exclude in the format |
|
116 | A list of format type string (MIME types) to exclude in the format | |
117 | data dict. If this is set all format types will be computed, |
|
117 | data dict. If this is set all format types will be computed, | |
118 | except for those included in this argument. |
|
118 | except for those included in this argument. | |
119 | Mimetypes present in exclude will take precedence over the ones in include |
|
119 | Mimetypes present in exclude will take precedence over the ones in include | |
120 |
|
120 | |||
121 | Returns |
|
121 | Returns | |
122 | ------- |
|
122 | ------- | |
123 | (format_dict, metadata_dict) : tuple of two dicts |
|
123 | (format_dict, metadata_dict) : tuple of two dicts | |
124 | format_dict is a dictionary of key/value pairs, one of each format that was |
|
124 | format_dict is a dictionary of key/value pairs, one of each format that was | |
125 | generated for the object. The keys are the format types, which |
|
125 | generated for the object. The keys are the format types, which | |
126 | will usually be MIME type strings and the values and JSON'able |
|
126 | will usually be MIME type strings and the values and JSON'able | |
127 | data structure containing the raw data for the representation in |
|
127 | data structure containing the raw data for the representation in | |
128 | that format. |
|
128 | that format. | |
129 |
|
129 | |||
130 | metadata_dict is a dictionary of metadata about each mime-type output. |
|
130 | metadata_dict is a dictionary of metadata about each mime-type output. | |
131 | Its keys will be a strict subset of the keys in format_dict. |
|
131 | Its keys will be a strict subset of the keys in format_dict. | |
132 |
|
132 | |||
133 | Notes |
|
133 | Notes | |
134 | ----- |
|
134 | ----- | |
135 | If an object implement `_repr_mimebundle_` as well as various |
|
135 | If an object implement `_repr_mimebundle_` as well as various | |
136 | `_repr_*_`, the data returned by `_repr_mimebundle_` will take |
|
136 | `_repr_*_`, the data returned by `_repr_mimebundle_` will take | |
137 | precedence and the corresponding `_repr_*_` for this mimetype will |
|
137 | precedence and the corresponding `_repr_*_` for this mimetype will | |
138 | not be called. |
|
138 | not be called. | |
139 |
|
139 | |||
140 | """ |
|
140 | """ | |
141 | format_dict = {} |
|
141 | format_dict = {} | |
142 | md_dict = {} |
|
142 | md_dict = {} | |
143 |
|
143 | |||
144 | if self.ipython_display_formatter(obj): |
|
144 | if self.ipython_display_formatter(obj): | |
145 | # object handled itself, don't proceed |
|
145 | # object handled itself, don't proceed | |
146 | return {}, {} |
|
146 | return {}, {} | |
147 |
|
147 | |||
148 | format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude) |
|
148 | format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude) | |
149 |
|
149 | |||
150 | if format_dict or md_dict: |
|
150 | if format_dict or md_dict: | |
151 | if include: |
|
151 | if include: | |
152 | format_dict = {k:v for k,v in format_dict.items() if k in include} |
|
152 | format_dict = {k:v for k,v in format_dict.items() if k in include} | |
153 | md_dict = {k:v for k,v in md_dict.items() if k in include} |
|
153 | md_dict = {k:v for k,v in md_dict.items() if k in include} | |
154 | if exclude: |
|
154 | if exclude: | |
155 | format_dict = {k:v for k,v in format_dict.items() if k not in exclude} |
|
155 | format_dict = {k:v for k,v in format_dict.items() if k not in exclude} | |
156 | md_dict = {k:v for k,v in md_dict.items() if k not in exclude} |
|
156 | md_dict = {k:v for k,v in md_dict.items() if k not in exclude} | |
157 |
|
157 | |||
158 | for format_type, formatter in self.formatters.items(): |
|
158 | for format_type, formatter in self.formatters.items(): | |
159 | if format_type in format_dict: |
|
159 | if format_type in format_dict: | |
160 | # already got it from mimebundle, maybe don't render again. |
|
160 | # already got it from mimebundle, maybe don't render again. | |
161 | # exception: manually registered per-mime renderer |
|
161 | # exception: manually registered per-mime renderer | |
162 | # check priority: |
|
162 | # check priority: | |
163 | # 1. user-registered per-mime formatter |
|
163 | # 1. user-registered per-mime formatter | |
164 | # 2. mime-bundle (user-registered or repr method) |
|
164 | # 2. mime-bundle (user-registered or repr method) | |
165 | # 3. default per-mime formatter (e.g. repr method) |
|
165 | # 3. default per-mime formatter (e.g. repr method) | |
166 | try: |
|
166 | try: | |
167 | formatter.lookup(obj) |
|
167 | formatter.lookup(obj) | |
168 | except KeyError: |
|
168 | except KeyError: | |
169 | # no special formatter, use mime-bundle-provided value |
|
169 | # no special formatter, use mime-bundle-provided value | |
170 | continue |
|
170 | continue | |
171 | if include and format_type not in include: |
|
171 | if include and format_type not in include: | |
172 | continue |
|
172 | continue | |
173 | if exclude and format_type in exclude: |
|
173 | if exclude and format_type in exclude: | |
174 | continue |
|
174 | continue | |
175 |
|
175 | |||
176 | md = None |
|
176 | md = None | |
177 | try: |
|
177 | try: | |
178 | data = formatter(obj) |
|
178 | data = formatter(obj) | |
179 | except: |
|
179 | except: | |
180 | # FIXME: log the exception |
|
180 | # FIXME: log the exception | |
181 | raise |
|
181 | raise | |
182 |
|
182 | |||
183 | # formatters can return raw data or (data, metadata) |
|
183 | # formatters can return raw data or (data, metadata) | |
184 | if isinstance(data, tuple) and len(data) == 2: |
|
184 | if isinstance(data, tuple) and len(data) == 2: | |
185 | data, md = data |
|
185 | data, md = data | |
186 |
|
186 | |||
187 | if data is not None: |
|
187 | if data is not None: | |
188 | format_dict[format_type] = data |
|
188 | format_dict[format_type] = data | |
189 | if md is not None: |
|
189 | if md is not None: | |
190 | md_dict[format_type] = md |
|
190 | md_dict[format_type] = md | |
191 | return format_dict, md_dict |
|
191 | return format_dict, md_dict | |
192 |
|
192 | |||
193 | @property |
|
193 | @property | |
194 | def format_types(self): |
|
194 | def format_types(self): | |
195 | """Return the format types (MIME types) of the active formatters.""" |
|
195 | """Return the format types (MIME types) of the active formatters.""" | |
196 | return list(self.formatters.keys()) |
|
196 | return list(self.formatters.keys()) | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | #----------------------------------------------------------------------------- |
|
199 | #----------------------------------------------------------------------------- | |
200 | # Formatters for specific format types (text, html, svg, etc.) |
|
200 | # Formatters for specific format types (text, html, svg, etc.) | |
201 | #----------------------------------------------------------------------------- |
|
201 | #----------------------------------------------------------------------------- | |
202 |
|
202 | |||
203 |
|
203 | |||
204 | def _safe_repr(obj): |
|
204 | def _safe_repr(obj): | |
205 | """Try to return a repr of an object |
|
205 | """Try to return a repr of an object | |
206 |
|
206 | |||
207 | always returns a string, at least. |
|
207 | always returns a string, at least. | |
208 | """ |
|
208 | """ | |
209 | try: |
|
209 | try: | |
210 | return repr(obj) |
|
210 | return repr(obj) | |
211 | except Exception as e: |
|
211 | except Exception as e: | |
212 | return "un-repr-able object (%r)" % e |
|
212 | return "un-repr-able object (%r)" % e | |
213 |
|
213 | |||
214 |
|
214 | |||
215 | class FormatterWarning(UserWarning): |
|
215 | class FormatterWarning(UserWarning): | |
216 | """Warning class for errors in formatters""" |
|
216 | """Warning class for errors in formatters""" | |
217 |
|
217 | |||
218 | @decorator |
|
218 | @decorator | |
219 | def catch_format_error(method, self, *args, **kwargs): |
|
219 | def catch_format_error(method, self, *args, **kwargs): | |
220 | """show traceback on failed format call""" |
|
220 | """show traceback on failed format call""" | |
221 | try: |
|
221 | try: | |
222 | r = method(self, *args, **kwargs) |
|
222 | r = method(self, *args, **kwargs) | |
223 | except NotImplementedError: |
|
223 | except NotImplementedError: | |
224 | # don't warn on NotImplementedErrors |
|
224 | # don't warn on NotImplementedErrors | |
225 | return self._check_return(None, args[0]) |
|
225 | return self._check_return(None, args[0]) | |
226 | except Exception: |
|
226 | except Exception: | |
227 | exc_info = sys.exc_info() |
|
227 | exc_info = sys.exc_info() | |
228 | ip = get_ipython() |
|
228 | ip = get_ipython() | |
229 | if ip is not None: |
|
229 | if ip is not None: | |
230 | ip.showtraceback(exc_info) |
|
230 | ip.showtraceback(exc_info) | |
231 | else: |
|
231 | else: | |
232 | traceback.print_exception(*exc_info) |
|
232 | traceback.print_exception(*exc_info) | |
233 | return self._check_return(None, args[0]) |
|
233 | return self._check_return(None, args[0]) | |
234 | return self._check_return(r, args[0]) |
|
234 | return self._check_return(r, args[0]) | |
235 |
|
235 | |||
236 |
|
236 | |||
237 | class FormatterABC(metaclass=abc.ABCMeta): |
|
237 | class FormatterABC(metaclass=abc.ABCMeta): | |
238 | """ Abstract base class for Formatters. |
|
238 | """ Abstract base class for Formatters. | |
239 |
|
239 | |||
240 | A formatter is a callable class that is responsible for computing the |
|
240 | A formatter is a callable class that is responsible for computing the | |
241 | raw format data for a particular format type (MIME type). For example, |
|
241 | raw format data for a particular format type (MIME type). For example, | |
242 | an HTML formatter would have a format type of `text/html` and would return |
|
242 | an HTML formatter would have a format type of `text/html` and would return | |
243 | the HTML representation of the object when called. |
|
243 | the HTML representation of the object when called. | |
244 | """ |
|
244 | """ | |
245 |
|
245 | |||
246 | # The format type of the data returned, usually a MIME type. |
|
246 | # The format type of the data returned, usually a MIME type. | |
247 | format_type = 'text/plain' |
|
247 | format_type = 'text/plain' | |
248 |
|
248 | |||
249 | # Is the formatter enabled... |
|
249 | # Is the formatter enabled... | |
250 | enabled = True |
|
250 | enabled = True | |
251 |
|
251 | |||
252 | @abc.abstractmethod |
|
252 | @abc.abstractmethod | |
253 | def __call__(self, obj): |
|
253 | def __call__(self, obj): | |
254 | """Return a JSON'able representation of the object. |
|
254 | """Return a JSON'able representation of the object. | |
255 |
|
255 | |||
256 | If the object cannot be formatted by this formatter, |
|
256 | If the object cannot be formatted by this formatter, | |
257 | warn and return None. |
|
257 | warn and return None. | |
258 | """ |
|
258 | """ | |
259 | return repr(obj) |
|
259 | return repr(obj) | |
260 |
|
260 | |||
261 |
|
261 | |||
262 | def _mod_name_key(typ): |
|
262 | def _mod_name_key(typ): | |
263 | """Return a (__module__, __name__) tuple for a type. |
|
263 | """Return a (__module__, __name__) tuple for a type. | |
264 |
|
264 | |||
265 | Used as key in Formatter.deferred_printers. |
|
265 | Used as key in Formatter.deferred_printers. | |
266 | """ |
|
266 | """ | |
267 | module = getattr(typ, '__module__', None) |
|
267 | module = getattr(typ, '__module__', None) | |
268 | name = getattr(typ, '__name__', None) |
|
268 | name = getattr(typ, '__name__', None) | |
269 | return (module, name) |
|
269 | return (module, name) | |
270 |
|
270 | |||
271 |
|
271 | |||
272 | def _get_type(obj): |
|
272 | def _get_type(obj): | |
273 | """Return the type of an instance (old and new-style)""" |
|
273 | """Return the type of an instance (old and new-style)""" | |
274 | return getattr(obj, '__class__', None) or type(obj) |
|
274 | return getattr(obj, '__class__', None) or type(obj) | |
275 |
|
275 | |||
276 |
|
276 | |||
277 | _raise_key_error = Sentinel('_raise_key_error', __name__, |
|
277 | _raise_key_error = Sentinel('_raise_key_error', __name__, | |
278 | """ |
|
278 | """ | |
279 | Special value to raise a KeyError |
|
279 | Special value to raise a KeyError | |
280 |
|
280 | |||
281 | Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop` |
|
281 | Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop` | |
282 | """) |
|
282 | """) | |
283 |
|
283 | |||
284 |
|
284 | |||
285 | class BaseFormatter(Configurable): |
|
285 | class BaseFormatter(Configurable): | |
286 | """A base formatter class that is configurable. |
|
286 | """A base formatter class that is configurable. | |
287 |
|
287 | |||
288 | This formatter should usually be used as the base class of all formatters. |
|
288 | This formatter should usually be used as the base class of all formatters. | |
289 | It is a traited :class:`Configurable` class and includes an extensible |
|
289 | It is a traited :class:`Configurable` class and includes an extensible | |
290 | API for users to determine how their objects are formatted. The following |
|
290 | API for users to determine how their objects are formatted. The following | |
291 | logic is used to find a function to format an given object. |
|
291 | logic is used to find a function to format an given object. | |
292 |
|
292 | |||
293 | 1. The object is introspected to see if it has a method with the name |
|
293 | 1. The object is introspected to see if it has a method with the name | |
294 | :attr:`print_method`. If is does, that object is passed to that method |
|
294 | :attr:`print_method`. If is does, that object is passed to that method | |
295 | for formatting. |
|
295 | for formatting. | |
296 | 2. If no print method is found, three internal dictionaries are consulted |
|
296 | 2. If no print method is found, three internal dictionaries are consulted | |
297 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
297 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` | |
298 | and :attr:`deferred_printers`. |
|
298 | and :attr:`deferred_printers`. | |
299 |
|
299 | |||
300 | Users should use these dictionaries to register functions that will be |
|
300 | Users should use these dictionaries to register functions that will be | |
301 | used to compute the format data for their objects (if those objects don't |
|
301 | used to compute the format data for their objects (if those objects don't | |
302 | have the special print methods). The easiest way of using these |
|
302 | have the special print methods). The easiest way of using these | |
303 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
303 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` | |
304 | methods. |
|
304 | methods. | |
305 |
|
305 | |||
306 | If no function/callable is found to compute the format data, ``None`` is |
|
306 | If no function/callable is found to compute the format data, ``None`` is | |
307 | returned and this format type is not used. |
|
307 | returned and this format type is not used. | |
308 | """ |
|
308 | """ | |
309 |
|
309 | |||
310 | format_type = Unicode('text/plain') |
|
310 | format_type = Unicode('text/plain') | |
311 | _return_type = str |
|
311 | _return_type = str | |
312 |
|
312 | |||
313 | enabled = Bool(True).tag(config=True) |
|
313 | enabled = Bool(True).tag(config=True) | |
314 |
|
314 | |||
315 | print_method = ObjectName('__repr__') |
|
315 | print_method = ObjectName('__repr__') | |
316 |
|
316 | |||
317 | # The singleton printers. |
|
317 | # The singleton printers. | |
318 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
318 | # Maps the IDs of the builtin singleton objects to the format functions. | |
319 | singleton_printers = Dict().tag(config=True) |
|
319 | singleton_printers = Dict().tag(config=True) | |
320 |
|
320 | |||
321 | # The type-specific printers. |
|
321 | # The type-specific printers. | |
322 | # Map type objects to the format functions. |
|
322 | # Map type objects to the format functions. | |
323 | type_printers = Dict().tag(config=True) |
|
323 | type_printers = Dict().tag(config=True) | |
324 |
|
324 | |||
325 | # The deferred-import type-specific printers. |
|
325 | # The deferred-import type-specific printers. | |
326 | # Map (modulename, classname) pairs to the format functions. |
|
326 | # Map (modulename, classname) pairs to the format functions. | |
327 | deferred_printers = Dict().tag(config=True) |
|
327 | deferred_printers = Dict().tag(config=True) | |
328 |
|
328 | |||
329 | @catch_format_error |
|
329 | @catch_format_error | |
330 | def __call__(self, obj): |
|
330 | def __call__(self, obj): | |
331 | """Compute the format for an object.""" |
|
331 | """Compute the format for an object.""" | |
332 | if self.enabled: |
|
332 | if self.enabled: | |
333 | # lookup registered printer |
|
333 | # lookup registered printer | |
334 | try: |
|
334 | try: | |
335 | printer = self.lookup(obj) |
|
335 | printer = self.lookup(obj) | |
336 | except KeyError: |
|
336 | except KeyError: | |
337 | pass |
|
337 | pass | |
338 | else: |
|
338 | else: | |
339 | return printer(obj) |
|
339 | return printer(obj) | |
340 | # Finally look for special method names |
|
340 | # Finally look for special method names | |
341 | method = get_real_method(obj, self.print_method) |
|
341 | method = get_real_method(obj, self.print_method) | |
342 | if method is not None: |
|
342 | if method is not None: | |
343 | return method() |
|
343 | return method() | |
344 | return None |
|
344 | return None | |
345 | else: |
|
345 | else: | |
346 | return None |
|
346 | return None | |
347 |
|
347 | |||
348 | def __contains__(self, typ): |
|
348 | def __contains__(self, typ): | |
349 | """map in to lookup_by_type""" |
|
349 | """map in to lookup_by_type""" | |
350 | try: |
|
350 | try: | |
351 | self.lookup_by_type(typ) |
|
351 | self.lookup_by_type(typ) | |
352 | except KeyError: |
|
352 | except KeyError: | |
353 | return False |
|
353 | return False | |
354 | else: |
|
354 | else: | |
355 | return True |
|
355 | return True | |
356 |
|
356 | |||
357 | def _check_return(self, r, obj): |
|
357 | def _check_return(self, r, obj): | |
358 | """Check that a return value is appropriate |
|
358 | """Check that a return value is appropriate | |
359 |
|
359 | |||
360 | Return the value if so, None otherwise, warning if invalid. |
|
360 | Return the value if so, None otherwise, warning if invalid. | |
361 | """ |
|
361 | """ | |
362 | if r is None or isinstance(r, self._return_type) or \ |
|
362 | if r is None or isinstance(r, self._return_type) or \ | |
363 | (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)): |
|
363 | (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)): | |
364 | return r |
|
364 | return r | |
365 | else: |
|
365 | else: | |
366 | warnings.warn( |
|
366 | warnings.warn( | |
367 | "%s formatter returned invalid type %s (expected %s) for object: %s" % \ |
|
367 | "%s formatter returned invalid type %s (expected %s) for object: %s" % \ | |
368 | (self.format_type, type(r), self._return_type, _safe_repr(obj)), |
|
368 | (self.format_type, type(r), self._return_type, _safe_repr(obj)), | |
369 | FormatterWarning |
|
369 | FormatterWarning | |
370 | ) |
|
370 | ) | |
371 |
|
371 | |||
372 | def lookup(self, obj): |
|
372 | def lookup(self, obj): | |
373 | """Look up the formatter for a given instance. |
|
373 | """Look up the formatter for a given instance. | |
374 |
|
374 | |||
375 | Parameters |
|
375 | Parameters | |
376 | ---------- |
|
376 | ---------- | |
377 | obj : object instance |
|
377 | obj : object instance | |
378 |
|
378 | |||
379 | Returns |
|
379 | Returns | |
380 | ------- |
|
380 | ------- | |
381 | f : callable |
|
381 | f : callable | |
382 | The registered formatting callable for the type. |
|
382 | The registered formatting callable for the type. | |
383 |
|
383 | |||
384 | Raises |
|
384 | Raises | |
385 | ------ |
|
385 | ------ | |
386 | KeyError if the type has not been registered. |
|
386 | KeyError if the type has not been registered. | |
387 | """ |
|
387 | """ | |
388 | # look for singleton first |
|
388 | # look for singleton first | |
389 | obj_id = id(obj) |
|
389 | obj_id = id(obj) | |
390 | if obj_id in self.singleton_printers: |
|
390 | if obj_id in self.singleton_printers: | |
391 | return self.singleton_printers[obj_id] |
|
391 | return self.singleton_printers[obj_id] | |
392 | # then lookup by type |
|
392 | # then lookup by type | |
393 | return self.lookup_by_type(_get_type(obj)) |
|
393 | return self.lookup_by_type(_get_type(obj)) | |
394 |
|
394 | |||
395 | def lookup_by_type(self, typ): |
|
395 | def lookup_by_type(self, typ): | |
396 | """Look up the registered formatter for a type. |
|
396 | """Look up the registered formatter for a type. | |
397 |
|
397 | |||
398 | Parameters |
|
398 | Parameters | |
399 | ---------- |
|
399 | ---------- | |
400 | typ : type or '__module__.__name__' string for a type |
|
400 | typ : type or '__module__.__name__' string for a type | |
401 |
|
401 | |||
402 | Returns |
|
402 | Returns | |
403 | ------- |
|
403 | ------- | |
404 | f : callable |
|
404 | f : callable | |
405 | The registered formatting callable for the type. |
|
405 | The registered formatting callable for the type. | |
406 |
|
406 | |||
407 | Raises |
|
407 | Raises | |
408 | ------ |
|
408 | ------ | |
409 | KeyError if the type has not been registered. |
|
409 | KeyError if the type has not been registered. | |
410 | """ |
|
410 | """ | |
411 | if isinstance(typ, str): |
|
411 | if isinstance(typ, str): | |
412 | typ_key = tuple(typ.rsplit('.',1)) |
|
412 | typ_key = tuple(typ.rsplit('.',1)) | |
413 | if typ_key not in self.deferred_printers: |
|
413 | if typ_key not in self.deferred_printers: | |
414 | # We may have it cached in the type map. We will have to |
|
414 | # We may have it cached in the type map. We will have to | |
415 | # iterate over all of the types to check. |
|
415 | # iterate over all of the types to check. | |
416 | for cls in self.type_printers: |
|
416 | for cls in self.type_printers: | |
417 | if _mod_name_key(cls) == typ_key: |
|
417 | if _mod_name_key(cls) == typ_key: | |
418 | return self.type_printers[cls] |
|
418 | return self.type_printers[cls] | |
419 | else: |
|
419 | else: | |
420 | return self.deferred_printers[typ_key] |
|
420 | return self.deferred_printers[typ_key] | |
421 | else: |
|
421 | else: | |
422 | for cls in pretty._get_mro(typ): |
|
422 | for cls in pretty._get_mro(typ): | |
423 | if cls in self.type_printers or self._in_deferred_types(cls): |
|
423 | if cls in self.type_printers or self._in_deferred_types(cls): | |
424 | return self.type_printers[cls] |
|
424 | return self.type_printers[cls] | |
425 |
|
425 | |||
426 | # If we have reached here, the lookup failed. |
|
426 | # If we have reached here, the lookup failed. | |
427 | raise KeyError("No registered printer for {0!r}".format(typ)) |
|
427 | raise KeyError("No registered printer for {0!r}".format(typ)) | |
428 |
|
428 | |||
429 | def for_type(self, typ, func=None): |
|
429 | def for_type(self, typ, func=None): | |
430 | """Add a format function for a given type. |
|
430 | """Add a format function for a given type. | |
431 |
|
431 | |||
432 | Parameters |
|
432 | Parameters | |
433 | ---------- |
|
433 | ---------- | |
434 | typ : type or '__module__.__name__' string for a type |
|
434 | typ : type or '__module__.__name__' string for a type | |
435 | The class of the object that will be formatted using `func`. |
|
435 | The class of the object that will be formatted using `func`. | |
436 | func : callable |
|
436 | func : callable | |
437 | A callable for computing the format data. |
|
437 | A callable for computing the format data. | |
438 | `func` will be called with the object to be formatted, |
|
438 | `func` will be called with the object to be formatted, | |
439 | and will return the raw data in this formatter's format. |
|
439 | and will return the raw data in this formatter's format. | |
440 | Subclasses may use a different call signature for the |
|
440 | Subclasses may use a different call signature for the | |
441 | `func` argument. |
|
441 | `func` argument. | |
442 |
|
442 | |||
443 | If `func` is None or not specified, there will be no change, |
|
443 | If `func` is None or not specified, there will be no change, | |
444 | only returning the current value. |
|
444 | only returning the current value. | |
445 |
|
445 | |||
446 | Returns |
|
446 | Returns | |
447 | ------- |
|
447 | ------- | |
448 | oldfunc : callable |
|
448 | oldfunc : callable | |
449 | The currently registered callable. |
|
449 | The currently registered callable. | |
450 | If you are registering a new formatter, |
|
450 | If you are registering a new formatter, | |
451 | this will be the previous value (to enable restoring later). |
|
451 | this will be the previous value (to enable restoring later). | |
452 | """ |
|
452 | """ | |
453 | # if string given, interpret as 'pkg.module.class_name' |
|
453 | # if string given, interpret as 'pkg.module.class_name' | |
454 | if isinstance(typ, str): |
|
454 | if isinstance(typ, str): | |
455 | type_module, type_name = typ.rsplit('.', 1) |
|
455 | type_module, type_name = typ.rsplit('.', 1) | |
456 | return self.for_type_by_name(type_module, type_name, func) |
|
456 | return self.for_type_by_name(type_module, type_name, func) | |
457 |
|
457 | |||
458 | try: |
|
458 | try: | |
459 | oldfunc = self.lookup_by_type(typ) |
|
459 | oldfunc = self.lookup_by_type(typ) | |
460 | except KeyError: |
|
460 | except KeyError: | |
461 | oldfunc = None |
|
461 | oldfunc = None | |
462 |
|
462 | |||
463 | if func is not None: |
|
463 | if func is not None: | |
464 | self.type_printers[typ] = func |
|
464 | self.type_printers[typ] = func | |
465 |
|
465 | |||
466 | return oldfunc |
|
466 | return oldfunc | |
467 |
|
467 | |||
468 | def for_type_by_name(self, type_module, type_name, func=None): |
|
468 | def for_type_by_name(self, type_module, type_name, func=None): | |
469 | """Add a format function for a type specified by the full dotted |
|
469 | """Add a format function for a type specified by the full dotted | |
470 | module and name of the type, rather than the type of the object. |
|
470 | module and name of the type, rather than the type of the object. | |
471 |
|
471 | |||
472 | Parameters |
|
472 | Parameters | |
473 | ---------- |
|
473 | ---------- | |
474 | type_module : str |
|
474 | type_module : str | |
475 | The full dotted name of the module the type is defined in, like |
|
475 | The full dotted name of the module the type is defined in, like | |
476 | ``numpy``. |
|
476 | ``numpy``. | |
477 | type_name : str |
|
477 | type_name : str | |
478 | The name of the type (the class name), like ``dtype`` |
|
478 | The name of the type (the class name), like ``dtype`` | |
479 | func : callable |
|
479 | func : callable | |
480 | A callable for computing the format data. |
|
480 | A callable for computing the format data. | |
481 | `func` will be called with the object to be formatted, |
|
481 | `func` will be called with the object to be formatted, | |
482 | and will return the raw data in this formatter's format. |
|
482 | and will return the raw data in this formatter's format. | |
483 | Subclasses may use a different call signature for the |
|
483 | Subclasses may use a different call signature for the | |
484 | `func` argument. |
|
484 | `func` argument. | |
485 |
|
485 | |||
486 | If `func` is None or unspecified, there will be no change, |
|
486 | If `func` is None or unspecified, there will be no change, | |
487 | only returning the current value. |
|
487 | only returning the current value. | |
488 |
|
488 | |||
489 | Returns |
|
489 | Returns | |
490 | ------- |
|
490 | ------- | |
491 | oldfunc : callable |
|
491 | oldfunc : callable | |
492 | The currently registered callable. |
|
492 | The currently registered callable. | |
493 | If you are registering a new formatter, |
|
493 | If you are registering a new formatter, | |
494 | this will be the previous value (to enable restoring later). |
|
494 | this will be the previous value (to enable restoring later). | |
495 | """ |
|
495 | """ | |
496 | key = (type_module, type_name) |
|
496 | key = (type_module, type_name) | |
497 |
|
497 | |||
498 | try: |
|
498 | try: | |
499 | oldfunc = self.lookup_by_type("%s.%s" % key) |
|
499 | oldfunc = self.lookup_by_type("%s.%s" % key) | |
500 | except KeyError: |
|
500 | except KeyError: | |
501 | oldfunc = None |
|
501 | oldfunc = None | |
502 |
|
502 | |||
503 | if func is not None: |
|
503 | if func is not None: | |
504 | self.deferred_printers[key] = func |
|
504 | self.deferred_printers[key] = func | |
505 | return oldfunc |
|
505 | return oldfunc | |
506 |
|
506 | |||
507 | def pop(self, typ, default=_raise_key_error): |
|
507 | def pop(self, typ, default=_raise_key_error): | |
508 | """Pop a formatter for the given type. |
|
508 | """Pop a formatter for the given type. | |
509 |
|
509 | |||
510 | Parameters |
|
510 | Parameters | |
511 | ---------- |
|
511 | ---------- | |
512 | typ : type or '__module__.__name__' string for a type |
|
512 | typ : type or '__module__.__name__' string for a type | |
513 | default : object |
|
513 | default : object | |
514 | value to be returned if no formatter is registered for typ. |
|
514 | value to be returned if no formatter is registered for typ. | |
515 |
|
515 | |||
516 | Returns |
|
516 | Returns | |
517 | ------- |
|
517 | ------- | |
518 | obj : object |
|
518 | obj : object | |
519 | The last registered object for the type. |
|
519 | The last registered object for the type. | |
520 |
|
520 | |||
521 | Raises |
|
521 | Raises | |
522 | ------ |
|
522 | ------ | |
523 | KeyError if the type is not registered and default is not specified. |
|
523 | KeyError if the type is not registered and default is not specified. | |
524 | """ |
|
524 | """ | |
525 |
|
525 | |||
526 | if isinstance(typ, str): |
|
526 | if isinstance(typ, str): | |
527 | typ_key = tuple(typ.rsplit('.',1)) |
|
527 | typ_key = tuple(typ.rsplit('.',1)) | |
528 | if typ_key not in self.deferred_printers: |
|
528 | if typ_key not in self.deferred_printers: | |
529 | # We may have it cached in the type map. We will have to |
|
529 | # We may have it cached in the type map. We will have to | |
530 | # iterate over all of the types to check. |
|
530 | # iterate over all of the types to check. | |
531 | for cls in self.type_printers: |
|
531 | for cls in self.type_printers: | |
532 | if _mod_name_key(cls) == typ_key: |
|
532 | if _mod_name_key(cls) == typ_key: | |
533 | old = self.type_printers.pop(cls) |
|
533 | old = self.type_printers.pop(cls) | |
534 | break |
|
534 | break | |
535 | else: |
|
535 | else: | |
536 | old = default |
|
536 | old = default | |
537 | else: |
|
537 | else: | |
538 | old = self.deferred_printers.pop(typ_key) |
|
538 | old = self.deferred_printers.pop(typ_key) | |
539 | else: |
|
539 | else: | |
540 | if typ in self.type_printers: |
|
540 | if typ in self.type_printers: | |
541 | old = self.type_printers.pop(typ) |
|
541 | old = self.type_printers.pop(typ) | |
542 | else: |
|
542 | else: | |
543 | old = self.deferred_printers.pop(_mod_name_key(typ), default) |
|
543 | old = self.deferred_printers.pop(_mod_name_key(typ), default) | |
544 | if old is _raise_key_error: |
|
544 | if old is _raise_key_error: | |
545 | raise KeyError("No registered value for {0!r}".format(typ)) |
|
545 | raise KeyError("No registered value for {0!r}".format(typ)) | |
546 | return old |
|
546 | return old | |
547 |
|
547 | |||
548 | def _in_deferred_types(self, cls): |
|
548 | def _in_deferred_types(self, cls): | |
549 | """ |
|
549 | """ | |
550 | Check if the given class is specified in the deferred type registry. |
|
550 | Check if the given class is specified in the deferred type registry. | |
551 |
|
551 | |||
552 | Successful matches will be moved to the regular type registry for future use. |
|
552 | Successful matches will be moved to the regular type registry for future use. | |
553 | """ |
|
553 | """ | |
554 | mod = getattr(cls, '__module__', None) |
|
554 | mod = getattr(cls, '__module__', None) | |
555 | name = getattr(cls, '__name__', None) |
|
555 | name = getattr(cls, '__name__', None) | |
556 | key = (mod, name) |
|
556 | key = (mod, name) | |
557 | if key in self.deferred_printers: |
|
557 | if key in self.deferred_printers: | |
558 | # Move the printer over to the regular registry. |
|
558 | # Move the printer over to the regular registry. | |
559 | printer = self.deferred_printers.pop(key) |
|
559 | printer = self.deferred_printers.pop(key) | |
560 | self.type_printers[cls] = printer |
|
560 | self.type_printers[cls] = printer | |
561 | return True |
|
561 | return True | |
562 | return False |
|
562 | return False | |
563 |
|
563 | |||
564 |
|
564 | |||
565 | class PlainTextFormatter(BaseFormatter): |
|
565 | class PlainTextFormatter(BaseFormatter): | |
566 | """The default pretty-printer. |
|
566 | """The default pretty-printer. | |
567 |
|
567 | |||
568 | This uses :mod:`IPython.lib.pretty` to compute the format data of |
|
568 | This uses :mod:`IPython.lib.pretty` to compute the format data of | |
569 | the object. If the object cannot be pretty printed, :func:`repr` is used. |
|
569 | the object. If the object cannot be pretty printed, :func:`repr` is used. | |
570 | See the documentation of :mod:`IPython.lib.pretty` for details on |
|
570 | See the documentation of :mod:`IPython.lib.pretty` for details on | |
571 | how to write pretty printers. Here is a simple example:: |
|
571 | how to write pretty printers. Here is a simple example:: | |
572 |
|
572 | |||
573 | def dtype_pprinter(obj, p, cycle): |
|
573 | def dtype_pprinter(obj, p, cycle): | |
574 | if cycle: |
|
574 | if cycle: | |
575 | return p.text('dtype(...)') |
|
575 | return p.text('dtype(...)') | |
576 | if hasattr(obj, 'fields'): |
|
576 | if hasattr(obj, 'fields'): | |
577 | if obj.fields is None: |
|
577 | if obj.fields is None: | |
578 | p.text(repr(obj)) |
|
578 | p.text(repr(obj)) | |
579 | else: |
|
579 | else: | |
580 | p.begin_group(7, 'dtype([') |
|
580 | p.begin_group(7, 'dtype([') | |
581 | for i, field in enumerate(obj.descr): |
|
581 | for i, field in enumerate(obj.descr): | |
582 | if i > 0: |
|
582 | if i > 0: | |
583 | p.text(',') |
|
583 | p.text(',') | |
584 | p.breakable() |
|
584 | p.breakable() | |
585 | p.pretty(field) |
|
585 | p.pretty(field) | |
586 | p.end_group(7, '])') |
|
586 | p.end_group(7, '])') | |
587 | """ |
|
587 | """ | |
588 |
|
588 | |||
589 | # The format type of data returned. |
|
589 | # The format type of data returned. | |
590 | format_type = Unicode('text/plain') |
|
590 | format_type = Unicode('text/plain') | |
591 |
|
591 | |||
592 | # This subclass ignores this attribute as it always need to return |
|
592 | # This subclass ignores this attribute as it always need to return | |
593 | # something. |
|
593 | # something. | |
594 | enabled = Bool(True).tag(config=False) |
|
594 | enabled = Bool(True).tag(config=False) | |
595 |
|
595 | |||
596 | max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, |
|
596 | max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, | |
597 | help="""Truncate large collections (lists, dicts, tuples, sets) to this size. |
|
597 | help="""Truncate large collections (lists, dicts, tuples, sets) to this size. | |
598 |
|
598 | |||
599 | Set to 0 to disable truncation. |
|
599 | Set to 0 to disable truncation. | |
600 | """ |
|
600 | """ | |
601 | ).tag(config=True) |
|
601 | ).tag(config=True) | |
602 |
|
602 | |||
603 | # Look for a _repr_pretty_ methods to use for pretty printing. |
|
603 | # Look for a _repr_pretty_ methods to use for pretty printing. | |
604 | print_method = ObjectName('_repr_pretty_') |
|
604 | print_method = ObjectName('_repr_pretty_') | |
605 |
|
605 | |||
606 | # Whether to pretty-print or not. |
|
606 | # Whether to pretty-print or not. | |
607 | pprint = Bool(True).tag(config=True) |
|
607 | pprint = Bool(True).tag(config=True) | |
608 |
|
608 | |||
609 | # Whether to be verbose or not. |
|
609 | # Whether to be verbose or not. | |
610 | verbose = Bool(False).tag(config=True) |
|
610 | verbose = Bool(False).tag(config=True) | |
611 |
|
611 | |||
612 | # The maximum width. |
|
612 | # The maximum width. | |
613 | max_width = Integer(79).tag(config=True) |
|
613 | max_width = Integer(79).tag(config=True) | |
614 |
|
614 | |||
615 | # The newline character. |
|
615 | # The newline character. | |
616 | newline = Unicode('\n').tag(config=True) |
|
616 | newline = Unicode('\n').tag(config=True) | |
617 |
|
617 | |||
618 | # format-string for pprinting floats |
|
618 | # format-string for pprinting floats | |
619 | float_format = Unicode('%r') |
|
619 | float_format = Unicode('%r') | |
620 | # setter for float precision, either int or direct format-string |
|
620 | # setter for float precision, either int or direct format-string | |
621 | float_precision = CUnicode('').tag(config=True) |
|
621 | float_precision = CUnicode('').tag(config=True) | |
622 |
|
622 | |||
623 | @observe('float_precision') |
|
623 | @observe('float_precision') | |
624 | def _float_precision_changed(self, change): |
|
624 | def _float_precision_changed(self, change): | |
625 | """float_precision changed, set float_format accordingly. |
|
625 | """float_precision changed, set float_format accordingly. | |
626 |
|
626 | |||
627 | float_precision can be set by int or str. |
|
627 | float_precision can be set by int or str. | |
628 | This will set float_format, after interpreting input. |
|
628 | This will set float_format, after interpreting input. | |
629 | If numpy has been imported, numpy print precision will also be set. |
|
629 | If numpy has been imported, numpy print precision will also be set. | |
630 |
|
630 | |||
631 | integer `n` sets format to '%.nf', otherwise, format set directly. |
|
631 | integer `n` sets format to '%.nf', otherwise, format set directly. | |
632 |
|
632 | |||
633 | An empty string returns to defaults (repr for float, 8 for numpy). |
|
633 | An empty string returns to defaults (repr for float, 8 for numpy). | |
634 |
|
634 | |||
635 | This parameter can be set via the '%precision' magic. |
|
635 | This parameter can be set via the '%precision' magic. | |
636 | """ |
|
636 | """ | |
637 | new = change['new'] |
|
637 | new = change['new'] | |
638 | if '%' in new: |
|
638 | if '%' in new: | |
639 | # got explicit format string |
|
639 | # got explicit format string | |
640 | fmt = new |
|
640 | fmt = new | |
641 | try: |
|
641 | try: | |
642 | fmt%3.14159 |
|
642 | fmt%3.14159 | |
643 | except Exception as e: |
|
643 | except Exception as e: | |
644 | raise ValueError("Precision must be int or format string, not %r"%new) from e |
|
644 | raise ValueError("Precision must be int or format string, not %r"%new) from e | |
645 | elif new: |
|
645 | elif new: | |
646 | # otherwise, should be an int |
|
646 | # otherwise, should be an int | |
647 | try: |
|
647 | try: | |
648 | i = int(new) |
|
648 | i = int(new) | |
649 | assert i >= 0 |
|
649 | assert i >= 0 | |
650 | except ValueError as e: |
|
650 | except ValueError as e: | |
651 | raise ValueError("Precision must be int or format string, not %r"%new) from e |
|
651 | raise ValueError("Precision must be int or format string, not %r"%new) from e | |
652 | except AssertionError as e: |
|
652 | except AssertionError as e: | |
653 | raise ValueError("int precision must be non-negative, not %r"%i) from e |
|
653 | raise ValueError("int precision must be non-negative, not %r"%i) from e | |
654 |
|
654 | |||
655 | fmt = '%%.%if'%i |
|
655 | fmt = '%%.%if'%i | |
656 | if 'numpy' in sys.modules: |
|
656 | if 'numpy' in sys.modules: | |
657 | # set numpy precision if it has been imported |
|
657 | # set numpy precision if it has been imported | |
658 | import numpy |
|
658 | import numpy | |
659 | numpy.set_printoptions(precision=i) |
|
659 | numpy.set_printoptions(precision=i) | |
660 | else: |
|
660 | else: | |
661 | # default back to repr |
|
661 | # default back to repr | |
662 | fmt = '%r' |
|
662 | fmt = '%r' | |
663 | if 'numpy' in sys.modules: |
|
663 | if 'numpy' in sys.modules: | |
664 | import numpy |
|
664 | import numpy | |
665 | # numpy default is 8 |
|
665 | # numpy default is 8 | |
666 | numpy.set_printoptions(precision=8) |
|
666 | numpy.set_printoptions(precision=8) | |
667 | self.float_format = fmt |
|
667 | self.float_format = fmt | |
668 |
|
668 | |||
669 | # Use the default pretty printers from IPython.lib.pretty. |
|
669 | # Use the default pretty printers from IPython.lib.pretty. | |
670 | @default('singleton_printers') |
|
670 | @default('singleton_printers') | |
671 | def _singleton_printers_default(self): |
|
671 | def _singleton_printers_default(self): | |
672 | return pretty._singleton_pprinters.copy() |
|
672 | return pretty._singleton_pprinters.copy() | |
673 |
|
673 | |||
674 | @default('type_printers') |
|
674 | @default('type_printers') | |
675 | def _type_printers_default(self): |
|
675 | def _type_printers_default(self): | |
676 | d = pretty._type_pprinters.copy() |
|
676 | d = pretty._type_pprinters.copy() | |
677 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) |
|
677 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) | |
678 | # if NumPy is used, set precision for its float64 type |
|
678 | # if NumPy is used, set precision for its float64 type | |
679 | if "numpy" in sys.modules: |
|
679 | if "numpy" in sys.modules: | |
680 | import numpy |
|
680 | import numpy | |
681 |
|
681 | |||
682 | d[numpy.float64] = lambda obj, p, cycle: p.text(self.float_format % obj) |
|
682 | d[numpy.float64] = lambda obj, p, cycle: p.text(self.float_format % obj) | |
683 | return d |
|
683 | return d | |
684 |
|
684 | |||
685 | @default('deferred_printers') |
|
685 | @default('deferred_printers') | |
686 | def _deferred_printers_default(self): |
|
686 | def _deferred_printers_default(self): | |
687 | return pretty._deferred_type_pprinters.copy() |
|
687 | return pretty._deferred_type_pprinters.copy() | |
688 |
|
688 | |||
689 | #### FormatterABC interface #### |
|
689 | #### FormatterABC interface #### | |
690 |
|
690 | |||
691 | @catch_format_error |
|
691 | @catch_format_error | |
692 | def __call__(self, obj): |
|
692 | def __call__(self, obj): | |
693 | """Compute the pretty representation of the object.""" |
|
693 | """Compute the pretty representation of the object.""" | |
694 | if not self.pprint: |
|
694 | if not self.pprint: | |
695 | return repr(obj) |
|
695 | return repr(obj) | |
696 | else: |
|
696 | else: | |
697 | stream = StringIO() |
|
697 | stream = StringIO() | |
698 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
698 | printer = pretty.RepresentationPrinter(stream, self.verbose, | |
699 | self.max_width, self.newline, |
|
699 | self.max_width, self.newline, | |
700 | max_seq_length=self.max_seq_length, |
|
700 | max_seq_length=self.max_seq_length, | |
701 | singleton_pprinters=self.singleton_printers, |
|
701 | singleton_pprinters=self.singleton_printers, | |
702 | type_pprinters=self.type_printers, |
|
702 | type_pprinters=self.type_printers, | |
703 | deferred_pprinters=self.deferred_printers) |
|
703 | deferred_pprinters=self.deferred_printers) | |
704 | printer.pretty(obj) |
|
704 | printer.pretty(obj) | |
705 | printer.flush() |
|
705 | printer.flush() | |
706 | return stream.getvalue() |
|
706 | return stream.getvalue() | |
707 |
|
707 | |||
708 |
|
708 | |||
709 | class HTMLFormatter(BaseFormatter): |
|
709 | class HTMLFormatter(BaseFormatter): | |
710 | """An HTML formatter. |
|
710 | """An HTML formatter. | |
711 |
|
711 | |||
712 | To define the callables that compute the HTML representation of your |
|
712 | To define the callables that compute the HTML representation of your | |
713 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` |
|
713 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` | |
714 | or :meth:`for_type_by_name` methods to register functions that handle |
|
714 | or :meth:`for_type_by_name` methods to register functions that handle | |
715 | this. |
|
715 | this. | |
716 |
|
716 | |||
717 | The return value of this formatter should be a valid HTML snippet that |
|
717 | The return value of this formatter should be a valid HTML snippet that | |
718 | could be injected into an existing DOM. It should *not* include the |
|
718 | could be injected into an existing DOM. It should *not* include the | |
719 | ```<html>`` or ```<body>`` tags. |
|
719 | ```<html>`` or ```<body>`` tags. | |
720 | """ |
|
720 | """ | |
721 | format_type = Unicode('text/html') |
|
721 | format_type = Unicode('text/html') | |
722 |
|
722 | |||
723 | print_method = ObjectName('_repr_html_') |
|
723 | print_method = ObjectName('_repr_html_') | |
724 |
|
724 | |||
725 |
|
725 | |||
726 | class MarkdownFormatter(BaseFormatter): |
|
726 | class MarkdownFormatter(BaseFormatter): | |
727 | """A Markdown formatter. |
|
727 | """A Markdown formatter. | |
728 |
|
728 | |||
729 | To define the callables that compute the Markdown representation of your |
|
729 | To define the callables that compute the Markdown representation of your | |
730 | objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type` |
|
730 | objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type` | |
731 | or :meth:`for_type_by_name` methods to register functions that handle |
|
731 | or :meth:`for_type_by_name` methods to register functions that handle | |
732 | this. |
|
732 | this. | |
733 |
|
733 | |||
734 | The return value of this formatter should be a valid Markdown. |
|
734 | The return value of this formatter should be a valid Markdown. | |
735 | """ |
|
735 | """ | |
736 | format_type = Unicode('text/markdown') |
|
736 | format_type = Unicode('text/markdown') | |
737 |
|
737 | |||
738 | print_method = ObjectName('_repr_markdown_') |
|
738 | print_method = ObjectName('_repr_markdown_') | |
739 |
|
739 | |||
740 | class SVGFormatter(BaseFormatter): |
|
740 | class SVGFormatter(BaseFormatter): | |
741 | """An SVG formatter. |
|
741 | """An SVG formatter. | |
742 |
|
742 | |||
743 | To define the callables that compute the SVG representation of your |
|
743 | To define the callables that compute the SVG representation of your | |
744 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` |
|
744 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` | |
745 | or :meth:`for_type_by_name` methods to register functions that handle |
|
745 | or :meth:`for_type_by_name` methods to register functions that handle | |
746 | this. |
|
746 | this. | |
747 |
|
747 | |||
748 | The return value of this formatter should be valid SVG enclosed in |
|
748 | The return value of this formatter should be valid SVG enclosed in | |
749 | ```<svg>``` tags, that could be injected into an existing DOM. It should |
|
749 | ```<svg>``` tags, that could be injected into an existing DOM. It should | |
750 | *not* include the ```<html>`` or ```<body>`` tags. |
|
750 | *not* include the ```<html>`` or ```<body>`` tags. | |
751 | """ |
|
751 | """ | |
752 | format_type = Unicode('image/svg+xml') |
|
752 | format_type = Unicode('image/svg+xml') | |
753 |
|
753 | |||
754 | print_method = ObjectName('_repr_svg_') |
|
754 | print_method = ObjectName('_repr_svg_') | |
755 |
|
755 | |||
756 |
|
756 | |||
757 | class PNGFormatter(BaseFormatter): |
|
757 | class PNGFormatter(BaseFormatter): | |
758 | """A PNG formatter. |
|
758 | """A PNG formatter. | |
759 |
|
759 | |||
760 | To define the callables that compute the PNG representation of your |
|
760 | To define the callables that compute the PNG representation of your | |
761 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` |
|
761 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` | |
762 | or :meth:`for_type_by_name` methods to register functions that handle |
|
762 | or :meth:`for_type_by_name` methods to register functions that handle | |
763 | this. |
|
763 | this. | |
764 |
|
764 | |||
765 | The return value of this formatter should be raw PNG data, *not* |
|
765 | The return value of this formatter should be raw PNG data, *not* | |
766 | base64 encoded. |
|
766 | base64 encoded. | |
767 | """ |
|
767 | """ | |
768 | format_type = Unicode('image/png') |
|
768 | format_type = Unicode('image/png') | |
769 |
|
769 | |||
770 | print_method = ObjectName('_repr_png_') |
|
770 | print_method = ObjectName('_repr_png_') | |
771 |
|
771 | |||
772 | _return_type = (bytes, str) |
|
772 | _return_type = (bytes, str) | |
773 |
|
773 | |||
774 |
|
774 | |||
775 | class JPEGFormatter(BaseFormatter): |
|
775 | class JPEGFormatter(BaseFormatter): | |
776 | """A JPEG formatter. |
|
776 | """A JPEG formatter. | |
777 |
|
777 | |||
778 | To define the callables that compute the JPEG representation of your |
|
778 | To define the callables that compute the JPEG representation of your | |
779 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` |
|
779 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` | |
780 | or :meth:`for_type_by_name` methods to register functions that handle |
|
780 | or :meth:`for_type_by_name` methods to register functions that handle | |
781 | this. |
|
781 | this. | |
782 |
|
782 | |||
783 | The return value of this formatter should be raw JPEG data, *not* |
|
783 | The return value of this formatter should be raw JPEG data, *not* | |
784 | base64 encoded. |
|
784 | base64 encoded. | |
785 | """ |
|
785 | """ | |
786 | format_type = Unicode('image/jpeg') |
|
786 | format_type = Unicode('image/jpeg') | |
787 |
|
787 | |||
788 | print_method = ObjectName('_repr_jpeg_') |
|
788 | print_method = ObjectName('_repr_jpeg_') | |
789 |
|
789 | |||
790 | _return_type = (bytes, str) |
|
790 | _return_type = (bytes, str) | |
791 |
|
791 | |||
792 |
|
792 | |||
793 | class LatexFormatter(BaseFormatter): |
|
793 | class LatexFormatter(BaseFormatter): | |
794 | """A LaTeX formatter. |
|
794 | """A LaTeX formatter. | |
795 |
|
795 | |||
796 | To define the callables that compute the LaTeX representation of your |
|
796 | To define the callables that compute the LaTeX representation of your | |
797 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` |
|
797 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` | |
798 | or :meth:`for_type_by_name` methods to register functions that handle |
|
798 | or :meth:`for_type_by_name` methods to register functions that handle | |
799 | this. |
|
799 | this. | |
800 |
|
800 | |||
801 | The return value of this formatter should be a valid LaTeX equation, |
|
801 | The return value of this formatter should be a valid LaTeX equation, | |
802 | enclosed in either ```$```, ```$$``` or another LaTeX equation |
|
802 | enclosed in either ```$```, ```$$``` or another LaTeX equation | |
803 | environment. |
|
803 | environment. | |
804 | """ |
|
804 | """ | |
805 | format_type = Unicode('text/latex') |
|
805 | format_type = Unicode('text/latex') | |
806 |
|
806 | |||
807 | print_method = ObjectName('_repr_latex_') |
|
807 | print_method = ObjectName('_repr_latex_') | |
808 |
|
808 | |||
809 |
|
809 | |||
810 | class JSONFormatter(BaseFormatter): |
|
810 | class JSONFormatter(BaseFormatter): | |
811 | """A JSON string formatter. |
|
811 | """A JSON string formatter. | |
812 |
|
812 | |||
813 | To define the callables that compute the JSONable representation of |
|
813 | To define the callables that compute the JSONable representation of | |
814 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` |
|
814 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` | |
815 | or :meth:`for_type_by_name` methods to register functions that handle |
|
815 | or :meth:`for_type_by_name` methods to register functions that handle | |
816 | this. |
|
816 | this. | |
817 |
|
817 | |||
818 | The return value of this formatter should be a JSONable list or dict. |
|
818 | The return value of this formatter should be a JSONable list or dict. | |
819 | JSON scalars (None, number, string) are not allowed, only dict or list containers. |
|
819 | JSON scalars (None, number, string) are not allowed, only dict or list containers. | |
820 | """ |
|
820 | """ | |
821 | format_type = Unicode('application/json') |
|
821 | format_type = Unicode('application/json') | |
822 | _return_type = (list, dict) |
|
822 | _return_type = (list, dict) | |
823 |
|
823 | |||
824 | print_method = ObjectName('_repr_json_') |
|
824 | print_method = ObjectName('_repr_json_') | |
825 |
|
825 | |||
826 | def _check_return(self, r, obj): |
|
826 | def _check_return(self, r, obj): | |
827 | """Check that a return value is appropriate |
|
827 | """Check that a return value is appropriate | |
828 |
|
828 | |||
829 | Return the value if so, None otherwise, warning if invalid. |
|
829 | Return the value if so, None otherwise, warning if invalid. | |
830 | """ |
|
830 | """ | |
831 | if r is None: |
|
831 | if r is None: | |
832 | return |
|
832 | return | |
833 | md = None |
|
833 | md = None | |
834 | if isinstance(r, tuple): |
|
834 | if isinstance(r, tuple): | |
835 | # unpack data, metadata tuple for type checking on first element |
|
835 | # unpack data, metadata tuple for type checking on first element | |
836 | r, md = r |
|
836 | r, md = r | |
837 |
|
837 | |||
838 | # handle deprecated JSON-as-string form from IPython < 3 |
|
838 | assert not isinstance( | |
839 |
|
|
839 | r, str | |
840 | warnings.warn("JSON expects JSONable list/dict containers, not JSON strings", |
|
840 | ), "JSON-as-string has been deprecated since IPython < 3" | |
841 | FormatterWarning) |
|
841 | ||
842 | r = json.loads(r) |
|
|||
843 |
|
||||
844 | if md is not None: |
|
842 | if md is not None: | |
845 | # put the tuple back together |
|
843 | # put the tuple back together | |
846 | r = (r, md) |
|
844 | r = (r, md) | |
847 | return super(JSONFormatter, self)._check_return(r, obj) |
|
845 | return super(JSONFormatter, self)._check_return(r, obj) | |
848 |
|
846 | |||
849 |
|
847 | |||
850 | class JavascriptFormatter(BaseFormatter): |
|
848 | class JavascriptFormatter(BaseFormatter): | |
851 | """A Javascript formatter. |
|
849 | """A Javascript formatter. | |
852 |
|
850 | |||
853 | To define the callables that compute the Javascript representation of |
|
851 | To define the callables that compute the Javascript representation of | |
854 | your objects, define a :meth:`_repr_javascript_` method or use the |
|
852 | your objects, define a :meth:`_repr_javascript_` method or use the | |
855 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions |
|
853 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions | |
856 | that handle this. |
|
854 | that handle this. | |
857 |
|
855 | |||
858 | The return value of this formatter should be valid Javascript code and |
|
856 | The return value of this formatter should be valid Javascript code and | |
859 | should *not* be enclosed in ```<script>``` tags. |
|
857 | should *not* be enclosed in ```<script>``` tags. | |
860 | """ |
|
858 | """ | |
861 | format_type = Unicode('application/javascript') |
|
859 | format_type = Unicode('application/javascript') | |
862 |
|
860 | |||
863 | print_method = ObjectName('_repr_javascript_') |
|
861 | print_method = ObjectName('_repr_javascript_') | |
864 |
|
862 | |||
865 |
|
863 | |||
866 | class PDFFormatter(BaseFormatter): |
|
864 | class PDFFormatter(BaseFormatter): | |
867 | """A PDF formatter. |
|
865 | """A PDF formatter. | |
868 |
|
866 | |||
869 | To define the callables that compute the PDF representation of your |
|
867 | To define the callables that compute the PDF representation of your | |
870 | objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type` |
|
868 | objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type` | |
871 | or :meth:`for_type_by_name` methods to register functions that handle |
|
869 | or :meth:`for_type_by_name` methods to register functions that handle | |
872 | this. |
|
870 | this. | |
873 |
|
871 | |||
874 | The return value of this formatter should be raw PDF data, *not* |
|
872 | The return value of this formatter should be raw PDF data, *not* | |
875 | base64 encoded. |
|
873 | base64 encoded. | |
876 | """ |
|
874 | """ | |
877 | format_type = Unicode('application/pdf') |
|
875 | format_type = Unicode('application/pdf') | |
878 |
|
876 | |||
879 | print_method = ObjectName('_repr_pdf_') |
|
877 | print_method = ObjectName('_repr_pdf_') | |
880 |
|
878 | |||
881 | _return_type = (bytes, str) |
|
879 | _return_type = (bytes, str) | |
882 |
|
880 | |||
883 | class IPythonDisplayFormatter(BaseFormatter): |
|
881 | class IPythonDisplayFormatter(BaseFormatter): | |
884 | """An escape-hatch Formatter for objects that know how to display themselves. |
|
882 | """An escape-hatch Formatter for objects that know how to display themselves. | |
885 |
|
883 | |||
886 | To define the callables that compute the representation of your |
|
884 | To define the callables that compute the representation of your | |
887 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` |
|
885 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` | |
888 | or :meth:`for_type_by_name` methods to register functions that handle |
|
886 | or :meth:`for_type_by_name` methods to register functions that handle | |
889 | this. Unlike mime-type displays, this method should not return anything, |
|
887 | this. Unlike mime-type displays, this method should not return anything, | |
890 | instead calling any appropriate display methods itself. |
|
888 | instead calling any appropriate display methods itself. | |
891 |
|
889 | |||
892 | This display formatter has highest priority. |
|
890 | This display formatter has highest priority. | |
893 | If it fires, no other display formatter will be called. |
|
891 | If it fires, no other display formatter will be called. | |
894 |
|
892 | |||
895 | Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types |
|
893 | Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types | |
896 | without registering a new Formatter. |
|
894 | without registering a new Formatter. | |
897 |
|
895 | |||
898 | IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types, |
|
896 | IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types, | |
899 | so `_ipython_display_` should only be used for objects that require unusual |
|
897 | so `_ipython_display_` should only be used for objects that require unusual | |
900 | display patterns, such as multiple display calls. |
|
898 | display patterns, such as multiple display calls. | |
901 | """ |
|
899 | """ | |
902 | print_method = ObjectName('_ipython_display_') |
|
900 | print_method = ObjectName('_ipython_display_') | |
903 | _return_type = (type(None), bool) |
|
901 | _return_type = (type(None), bool) | |
904 |
|
902 | |||
905 | @catch_format_error |
|
903 | @catch_format_error | |
906 | def __call__(self, obj): |
|
904 | def __call__(self, obj): | |
907 | """Compute the format for an object.""" |
|
905 | """Compute the format for an object.""" | |
908 | if self.enabled: |
|
906 | if self.enabled: | |
909 | # lookup registered printer |
|
907 | # lookup registered printer | |
910 | try: |
|
908 | try: | |
911 | printer = self.lookup(obj) |
|
909 | printer = self.lookup(obj) | |
912 | except KeyError: |
|
910 | except KeyError: | |
913 | pass |
|
911 | pass | |
914 | else: |
|
912 | else: | |
915 | printer(obj) |
|
913 | printer(obj) | |
916 | return True |
|
914 | return True | |
917 | # Finally look for special method names |
|
915 | # Finally look for special method names | |
918 | method = get_real_method(obj, self.print_method) |
|
916 | method = get_real_method(obj, self.print_method) | |
919 | if method is not None: |
|
917 | if method is not None: | |
920 | method() |
|
918 | method() | |
921 | return True |
|
919 | return True | |
922 |
|
920 | |||
923 |
|
921 | |||
924 | class MimeBundleFormatter(BaseFormatter): |
|
922 | class MimeBundleFormatter(BaseFormatter): | |
925 | """A Formatter for arbitrary mime-types. |
|
923 | """A Formatter for arbitrary mime-types. | |
926 |
|
924 | |||
927 | Unlike other `_repr_<mimetype>_` methods, |
|
925 | Unlike other `_repr_<mimetype>_` methods, | |
928 | `_repr_mimebundle_` should return mime-bundle data, |
|
926 | `_repr_mimebundle_` should return mime-bundle data, | |
929 | either the mime-keyed `data` dictionary or the tuple `(data, metadata)`. |
|
927 | either the mime-keyed `data` dictionary or the tuple `(data, metadata)`. | |
930 | Any mime-type is valid. |
|
928 | Any mime-type is valid. | |
931 |
|
929 | |||
932 | To define the callables that compute the mime-bundle representation of your |
|
930 | To define the callables that compute the mime-bundle representation of your | |
933 | objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type` |
|
931 | objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type` | |
934 | or :meth:`for_type_by_name` methods to register functions that handle |
|
932 | or :meth:`for_type_by_name` methods to register functions that handle | |
935 | this. |
|
933 | this. | |
936 |
|
934 | |||
937 | .. versionadded:: 6.1 |
|
935 | .. versionadded:: 6.1 | |
938 | """ |
|
936 | """ | |
939 | print_method = ObjectName('_repr_mimebundle_') |
|
937 | print_method = ObjectName('_repr_mimebundle_') | |
940 | _return_type = dict |
|
938 | _return_type = dict | |
941 |
|
939 | |||
942 | def _check_return(self, r, obj): |
|
940 | def _check_return(self, r, obj): | |
943 | r = super(MimeBundleFormatter, self)._check_return(r, obj) |
|
941 | r = super(MimeBundleFormatter, self)._check_return(r, obj) | |
944 | # always return (data, metadata): |
|
942 | # always return (data, metadata): | |
945 | if r is None: |
|
943 | if r is None: | |
946 | return {}, {} |
|
944 | return {}, {} | |
947 | if not isinstance(r, tuple): |
|
945 | if not isinstance(r, tuple): | |
948 | return r, {} |
|
946 | return r, {} | |
949 | return r |
|
947 | return r | |
950 |
|
948 | |||
951 | @catch_format_error |
|
949 | @catch_format_error | |
952 | def __call__(self, obj, include=None, exclude=None): |
|
950 | def __call__(self, obj, include=None, exclude=None): | |
953 | """Compute the format for an object. |
|
951 | """Compute the format for an object. | |
954 |
|
952 | |||
955 | Identical to parent's method but we pass extra parameters to the method. |
|
953 | Identical to parent's method but we pass extra parameters to the method. | |
956 |
|
954 | |||
957 | Unlike other _repr_*_ `_repr_mimebundle_` should allow extra kwargs, in |
|
955 | Unlike other _repr_*_ `_repr_mimebundle_` should allow extra kwargs, in | |
958 | particular `include` and `exclude`. |
|
956 | particular `include` and `exclude`. | |
959 | """ |
|
957 | """ | |
960 | if self.enabled: |
|
958 | if self.enabled: | |
961 | # lookup registered printer |
|
959 | # lookup registered printer | |
962 | try: |
|
960 | try: | |
963 | printer = self.lookup(obj) |
|
961 | printer = self.lookup(obj) | |
964 | except KeyError: |
|
962 | except KeyError: | |
965 | pass |
|
963 | pass | |
966 | else: |
|
964 | else: | |
967 | return printer(obj) |
|
965 | return printer(obj) | |
968 | # Finally look for special method names |
|
966 | # Finally look for special method names | |
969 | method = get_real_method(obj, self.print_method) |
|
967 | method = get_real_method(obj, self.print_method) | |
970 |
|
968 | |||
971 | if method is not None: |
|
969 | if method is not None: | |
972 | return method(include=include, exclude=exclude) |
|
970 | return method(include=include, exclude=exclude) | |
973 | return None |
|
971 | return None | |
974 | else: |
|
972 | else: | |
975 | return None |
|
973 | return None | |
976 |
|
974 | |||
977 |
|
975 | |||
978 | FormatterABC.register(BaseFormatter) |
|
976 | FormatterABC.register(BaseFormatter) | |
979 | FormatterABC.register(PlainTextFormatter) |
|
977 | FormatterABC.register(PlainTextFormatter) | |
980 | FormatterABC.register(HTMLFormatter) |
|
978 | FormatterABC.register(HTMLFormatter) | |
981 | FormatterABC.register(MarkdownFormatter) |
|
979 | FormatterABC.register(MarkdownFormatter) | |
982 | FormatterABC.register(SVGFormatter) |
|
980 | FormatterABC.register(SVGFormatter) | |
983 | FormatterABC.register(PNGFormatter) |
|
981 | FormatterABC.register(PNGFormatter) | |
984 | FormatterABC.register(PDFFormatter) |
|
982 | FormatterABC.register(PDFFormatter) | |
985 | FormatterABC.register(JPEGFormatter) |
|
983 | FormatterABC.register(JPEGFormatter) | |
986 | FormatterABC.register(LatexFormatter) |
|
984 | FormatterABC.register(LatexFormatter) | |
987 | FormatterABC.register(JSONFormatter) |
|
985 | FormatterABC.register(JSONFormatter) | |
988 | FormatterABC.register(JavascriptFormatter) |
|
986 | FormatterABC.register(JavascriptFormatter) | |
989 | FormatterABC.register(IPythonDisplayFormatter) |
|
987 | FormatterABC.register(IPythonDisplayFormatter) | |
990 | FormatterABC.register(MimeBundleFormatter) |
|
988 | FormatterABC.register(MimeBundleFormatter) | |
991 |
|
989 | |||
992 |
|
990 | |||
993 | def format_display_data(obj, include=None, exclude=None): |
|
991 | def format_display_data(obj, include=None, exclude=None): | |
994 | """Return a format data dict for an object. |
|
992 | """Return a format data dict for an object. | |
995 |
|
993 | |||
996 | By default all format types will be computed. |
|
994 | By default all format types will be computed. | |
997 |
|
995 | |||
998 | Parameters |
|
996 | Parameters | |
999 | ---------- |
|
997 | ---------- | |
1000 | obj : object |
|
998 | obj : object | |
1001 | The Python object whose format data will be computed. |
|
999 | The Python object whose format data will be computed. | |
1002 |
|
1000 | |||
1003 | Returns |
|
1001 | Returns | |
1004 | ------- |
|
1002 | ------- | |
1005 | format_dict : dict |
|
1003 | format_dict : dict | |
1006 | A dictionary of key/value pairs, one or each format that was |
|
1004 | A dictionary of key/value pairs, one or each format that was | |
1007 | generated for the object. The keys are the format types, which |
|
1005 | generated for the object. The keys are the format types, which | |
1008 | will usually be MIME type strings and the values and JSON'able |
|
1006 | will usually be MIME type strings and the values and JSON'able | |
1009 | data structure containing the raw data for the representation in |
|
1007 | data structure containing the raw data for the representation in | |
1010 | that format. |
|
1008 | that format. | |
1011 | include : list or tuple, optional |
|
1009 | include : list or tuple, optional | |
1012 | A list of format type strings (MIME types) to include in the |
|
1010 | A list of format type strings (MIME types) to include in the | |
1013 | format data dict. If this is set *only* the format types included |
|
1011 | format data dict. If this is set *only* the format types included | |
1014 | in this list will be computed. |
|
1012 | in this list will be computed. | |
1015 | exclude : list or tuple, optional |
|
1013 | exclude : list or tuple, optional | |
1016 | A list of format type string (MIME types) to exclude in the format |
|
1014 | A list of format type string (MIME types) to exclude in the format | |
1017 | data dict. If this is set all format types will be computed, |
|
1015 | data dict. If this is set all format types will be computed, | |
1018 | except for those included in this argument. |
|
1016 | except for those included in this argument. | |
1019 | """ |
|
1017 | """ | |
1020 | from .interactiveshell import InteractiveShell |
|
1018 | from .interactiveshell import InteractiveShell | |
1021 |
|
1019 | |||
1022 | return InteractiveShell.instance().display_formatter.format( |
|
1020 | return InteractiveShell.instance().display_formatter.format( | |
1023 | obj, |
|
1021 | obj, | |
1024 | include, |
|
1022 | include, | |
1025 | exclude |
|
1023 | exclude | |
1026 | ) |
|
1024 | ) |
@@ -1,3655 +1,3660 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 |
|
13 | |||
14 | import abc |
|
14 | import abc | |
15 | import ast |
|
15 | import ast | |
16 | import atexit |
|
16 | import atexit | |
17 | import builtins as builtin_mod |
|
17 | import builtins as builtin_mod | |
18 | import functools |
|
18 | import functools | |
19 | import inspect |
|
19 | import inspect | |
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 pathlib import Path |
|
31 | from pathlib import Path | |
32 | from pickleshare import PickleShareDB |
|
32 | from pickleshare import PickleShareDB | |
33 |
|
33 | |||
34 | from traitlets.config.configurable import SingletonConfigurable |
|
34 | from traitlets.config.configurable import SingletonConfigurable | |
35 | from traitlets.utils.importstring import import_item |
|
35 | from traitlets.utils.importstring import import_item | |
36 | from IPython.core import oinspect |
|
36 | from IPython.core import oinspect | |
37 | from IPython.core import magic |
|
37 | from IPython.core import magic | |
38 | from IPython.core import page |
|
38 | from IPython.core import page | |
39 | from IPython.core import prefilter |
|
39 | from IPython.core import prefilter | |
40 | from IPython.core import ultratb |
|
40 | from IPython.core import ultratb | |
41 | from IPython.core.alias import Alias, AliasManager |
|
41 | from IPython.core.alias import Alias, AliasManager | |
42 | from IPython.core.autocall import ExitAutocall |
|
42 | from IPython.core.autocall import ExitAutocall | |
43 | from IPython.core.builtin_trap import BuiltinTrap |
|
43 | from IPython.core.builtin_trap import BuiltinTrap | |
44 | from IPython.core.events import EventManager, available_events |
|
44 | from IPython.core.events import EventManager, available_events | |
45 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
45 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython | |
46 | from IPython.core.debugger import InterruptiblePdb |
|
46 | from IPython.core.debugger import InterruptiblePdb | |
47 | from IPython.core.display_trap import DisplayTrap |
|
47 | from IPython.core.display_trap import DisplayTrap | |
48 | from IPython.core.displayhook import DisplayHook |
|
48 | from IPython.core.displayhook import DisplayHook | |
49 | from IPython.core.displaypub import DisplayPublisher |
|
49 | from IPython.core.displaypub import DisplayPublisher | |
50 | from IPython.core.error import InputRejected, UsageError |
|
50 | from IPython.core.error import InputRejected, UsageError | |
51 | from IPython.core.extensions import ExtensionManager |
|
51 | from IPython.core.extensions import ExtensionManager | |
52 | from IPython.core.formatters import DisplayFormatter |
|
52 | from IPython.core.formatters import DisplayFormatter | |
53 | from IPython.core.history import HistoryManager |
|
53 | from IPython.core.history import HistoryManager | |
54 | from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2 |
|
54 | from IPython.core.inputtransformer2 import ESC_MAGIC, ESC_MAGIC2 | |
55 | from IPython.core.logger import Logger |
|
55 | from IPython.core.logger import Logger | |
56 | from IPython.core.macro import Macro |
|
56 | from IPython.core.macro import Macro | |
57 | from IPython.core.payload import PayloadManager |
|
57 | from IPython.core.payload import PayloadManager | |
58 | from IPython.core.prefilter import PrefilterManager |
|
58 | from IPython.core.prefilter import PrefilterManager | |
59 | from IPython.core.profiledir import ProfileDir |
|
59 | from IPython.core.profiledir import ProfileDir | |
60 | from IPython.core.usage import default_banner |
|
60 | from IPython.core.usage import default_banner | |
61 | from IPython.display import display |
|
61 | from IPython.display import display | |
62 | from IPython.testing.skipdoctest import skip_doctest |
|
62 | from IPython.testing.skipdoctest import skip_doctest | |
63 | from IPython.utils import PyColorize |
|
63 | from IPython.utils import PyColorize | |
64 | from IPython.utils import io |
|
64 | from IPython.utils import io | |
65 | from IPython.utils import py3compat |
|
65 | from IPython.utils import py3compat | |
66 | from IPython.utils import openpy |
|
66 | from IPython.utils import openpy | |
67 | from IPython.utils.decorators import undoc |
|
67 | from IPython.utils.decorators import undoc | |
68 | from IPython.utils.io import ask_yes_no |
|
68 | from IPython.utils.io import ask_yes_no | |
69 | from IPython.utils.ipstruct import Struct |
|
69 | from IPython.utils.ipstruct import Struct | |
70 | from IPython.paths import get_ipython_dir |
|
70 | from IPython.paths import get_ipython_dir | |
71 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists |
|
71 | from IPython.utils.path import get_home_dir, get_py_filename, ensure_dir_exists | |
72 | from IPython.utils.process import system, getoutput |
|
72 | from IPython.utils.process import system, getoutput | |
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, validate, Any |
|
79 | observe, default, validate, Any | |
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 | from typing import List as ListType, Tuple, Optional, Callable |
|
85 | from typing import List as ListType, Tuple, Optional, Callable | |
86 | from ast import stmt |
|
86 | from ast import stmt | |
87 |
|
87 | |||
88 | sphinxify: Optional[Callable] |
|
88 | sphinxify: Optional[Callable] | |
89 |
|
89 | |||
90 | try: |
|
90 | try: | |
91 | import docrepr.sphinxify as sphx |
|
91 | import docrepr.sphinxify as sphx | |
92 |
|
92 | |||
93 | def sphinxify(doc): |
|
93 | def sphinxify(doc): | |
94 | with TemporaryDirectory() as dirname: |
|
94 | with TemporaryDirectory() as dirname: | |
95 | return { |
|
95 | return { | |
96 | 'text/html': sphx.sphinxify(doc, dirname), |
|
96 | 'text/html': sphx.sphinxify(doc, dirname), | |
97 | 'text/plain': doc |
|
97 | 'text/plain': doc | |
98 | } |
|
98 | } | |
99 | except ImportError: |
|
99 | except ImportError: | |
100 | sphinxify = None |
|
100 | sphinxify = None | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | class ProvisionalWarning(DeprecationWarning): |
|
103 | class ProvisionalWarning(DeprecationWarning): | |
104 | """ |
|
104 | """ | |
105 | Warning class for unstable features |
|
105 | Warning class for unstable features | |
106 | """ |
|
106 | """ | |
107 | pass |
|
107 | pass | |
108 |
|
108 | |||
109 | from ast import Module |
|
109 | from ast import Module | |
110 |
|
110 | |||
111 | _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) |
|
111 | _assign_nodes = (ast.AugAssign, ast.AnnAssign, ast.Assign) | |
112 | _single_targets_nodes = (ast.AugAssign, ast.AnnAssign) |
|
112 | _single_targets_nodes = (ast.AugAssign, ast.AnnAssign) | |
113 |
|
113 | |||
114 | #----------------------------------------------------------------------------- |
|
114 | #----------------------------------------------------------------------------- | |
115 | # Await Helpers |
|
115 | # Await Helpers | |
116 | #----------------------------------------------------------------------------- |
|
116 | #----------------------------------------------------------------------------- | |
117 |
|
117 | |||
118 | # we still need to run things using the asyncio eventloop, but there is no |
|
118 | # we still need to run things using the asyncio eventloop, but there is no | |
119 | # async integration |
|
119 | # async integration | |
120 | from .async_helpers import _asyncio_runner, _pseudo_sync_runner |
|
120 | from .async_helpers import _asyncio_runner, _pseudo_sync_runner | |
121 | from .async_helpers import _curio_runner, _trio_runner, _should_be_async |
|
121 | from .async_helpers import _curio_runner, _trio_runner, _should_be_async | |
122 |
|
122 | |||
123 | #----------------------------------------------------------------------------- |
|
123 | #----------------------------------------------------------------------------- | |
124 | # Globals |
|
124 | # Globals | |
125 | #----------------------------------------------------------------------------- |
|
125 | #----------------------------------------------------------------------------- | |
126 |
|
126 | |||
127 | # compiled regexps for autoindent management |
|
127 | # compiled regexps for autoindent management | |
128 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
128 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
129 |
|
129 | |||
130 | #----------------------------------------------------------------------------- |
|
130 | #----------------------------------------------------------------------------- | |
131 | # Utilities |
|
131 | # Utilities | |
132 | #----------------------------------------------------------------------------- |
|
132 | #----------------------------------------------------------------------------- | |
133 |
|
133 | |||
134 | @undoc |
|
134 | @undoc | |
135 | def softspace(file, newvalue): |
|
135 | def softspace(file, newvalue): | |
136 | """Copied from code.py, to remove the dependency""" |
|
136 | """Copied from code.py, to remove the dependency""" | |
137 |
|
137 | |||
138 | oldvalue = 0 |
|
138 | oldvalue = 0 | |
139 | try: |
|
139 | try: | |
140 | oldvalue = file.softspace |
|
140 | oldvalue = file.softspace | |
141 | except AttributeError: |
|
141 | except AttributeError: | |
142 | pass |
|
142 | pass | |
143 | try: |
|
143 | try: | |
144 | file.softspace = newvalue |
|
144 | file.softspace = newvalue | |
145 | except (AttributeError, TypeError): |
|
145 | except (AttributeError, TypeError): | |
146 | # "attribute-less object" or "read-only attributes" |
|
146 | # "attribute-less object" or "read-only attributes" | |
147 | pass |
|
147 | pass | |
148 | return oldvalue |
|
148 | return oldvalue | |
149 |
|
149 | |||
150 | @undoc |
|
150 | @undoc | |
151 | def no_op(*a, **kw): |
|
151 | def no_op(*a, **kw): | |
152 | pass |
|
152 | pass | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | class SpaceInInput(Exception): pass |
|
155 | class SpaceInInput(Exception): pass | |
156 |
|
156 | |||
157 |
|
157 | |||
158 | class SeparateUnicode(Unicode): |
|
158 | class SeparateUnicode(Unicode): | |
159 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
159 | r"""A Unicode subclass to validate separate_in, separate_out, etc. | |
160 |
|
160 | |||
161 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
161 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. | |
162 | """ |
|
162 | """ | |
163 |
|
163 | |||
164 | def validate(self, obj, value): |
|
164 | def validate(self, obj, value): | |
165 | if value == '0': value = '' |
|
165 | if value == '0': value = '' | |
166 | value = value.replace('\\n','\n') |
|
166 | value = value.replace('\\n','\n') | |
167 | return super(SeparateUnicode, self).validate(obj, value) |
|
167 | return super(SeparateUnicode, self).validate(obj, value) | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | @undoc |
|
170 | @undoc | |
171 | class DummyMod(object): |
|
171 | class DummyMod(object): | |
172 | """A dummy module used for IPython's interactive module when |
|
172 | """A dummy module used for IPython's interactive module when | |
173 | a namespace must be assigned to the module's __dict__.""" |
|
173 | a namespace must be assigned to the module's __dict__.""" | |
174 | __spec__ = None |
|
174 | __spec__ = None | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | class ExecutionInfo(object): |
|
177 | class ExecutionInfo(object): | |
178 | """The arguments used for a call to :meth:`InteractiveShell.run_cell` |
|
178 | """The arguments used for a call to :meth:`InteractiveShell.run_cell` | |
179 |
|
179 | |||
180 | Stores information about what is going to happen. |
|
180 | Stores information about what is going to happen. | |
181 | """ |
|
181 | """ | |
182 | raw_cell = None |
|
182 | raw_cell = None | |
183 | store_history = False |
|
183 | store_history = False | |
184 | silent = False |
|
184 | silent = False | |
185 | shell_futures = True |
|
185 | shell_futures = True | |
186 |
|
186 | |||
187 | def __init__(self, raw_cell, store_history, silent, shell_futures): |
|
187 | def __init__(self, raw_cell, store_history, silent, shell_futures): | |
188 | self.raw_cell = raw_cell |
|
188 | self.raw_cell = raw_cell | |
189 | self.store_history = store_history |
|
189 | self.store_history = store_history | |
190 | self.silent = silent |
|
190 | self.silent = silent | |
191 | self.shell_futures = shell_futures |
|
191 | self.shell_futures = shell_futures | |
192 |
|
192 | |||
193 | def __repr__(self): |
|
193 | def __repr__(self): | |
194 | name = self.__class__.__qualname__ |
|
194 | name = self.__class__.__qualname__ | |
195 | raw_cell = ((self.raw_cell[:50] + '..') |
|
195 | raw_cell = ((self.raw_cell[:50] + '..') | |
196 | if len(self.raw_cell) > 50 else self.raw_cell) |
|
196 | if len(self.raw_cell) > 50 else self.raw_cell) | |
197 | return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s>' %\ |
|
197 | return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s>' %\ | |
198 | (name, id(self), raw_cell, self.store_history, self.silent, self.shell_futures) |
|
198 | (name, id(self), raw_cell, self.store_history, self.silent, self.shell_futures) | |
199 |
|
199 | |||
200 |
|
200 | |||
201 | class ExecutionResult(object): |
|
201 | class ExecutionResult(object): | |
202 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
202 | """The result of a call to :meth:`InteractiveShell.run_cell` | |
203 |
|
203 | |||
204 | Stores information about what took place. |
|
204 | Stores information about what took place. | |
205 | """ |
|
205 | """ | |
206 | execution_count = None |
|
206 | execution_count = None | |
207 | error_before_exec = None |
|
207 | error_before_exec = None | |
208 | error_in_exec: Optional[BaseException] = None |
|
208 | error_in_exec: Optional[BaseException] = None | |
209 | info = None |
|
209 | info = None | |
210 | result = None |
|
210 | result = None | |
211 |
|
211 | |||
212 | def __init__(self, info): |
|
212 | def __init__(self, info): | |
213 | self.info = info |
|
213 | self.info = info | |
214 |
|
214 | |||
215 | @property |
|
215 | @property | |
216 | def success(self): |
|
216 | def success(self): | |
217 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
217 | return (self.error_before_exec is None) and (self.error_in_exec is None) | |
218 |
|
218 | |||
219 | def raise_error(self): |
|
219 | def raise_error(self): | |
220 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
220 | """Reraises error if `success` is `False`, otherwise does nothing""" | |
221 | if self.error_before_exec is not None: |
|
221 | if self.error_before_exec is not None: | |
222 | raise self.error_before_exec |
|
222 | raise self.error_before_exec | |
223 | if self.error_in_exec is not None: |
|
223 | if self.error_in_exec is not None: | |
224 | raise self.error_in_exec |
|
224 | raise self.error_in_exec | |
225 |
|
225 | |||
226 | def __repr__(self): |
|
226 | def __repr__(self): | |
227 | name = self.__class__.__qualname__ |
|
227 | name = self.__class__.__qualname__ | |
228 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\ |
|
228 | return '<%s object at %x, execution_count=%s error_before_exec=%s error_in_exec=%s info=%s result=%s>' %\ | |
229 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result)) |
|
229 | (name, id(self), self.execution_count, self.error_before_exec, self.error_in_exec, repr(self.info), repr(self.result)) | |
230 |
|
230 | |||
231 |
|
231 | |||
232 | class InteractiveShell(SingletonConfigurable): |
|
232 | class InteractiveShell(SingletonConfigurable): | |
233 | """An enhanced, interactive shell for Python.""" |
|
233 | """An enhanced, interactive shell for Python.""" | |
234 |
|
234 | |||
235 | _instance = None |
|
235 | _instance = None | |
236 |
|
236 | |||
237 | ast_transformers = List([], help= |
|
237 | ast_transformers = List([], help= | |
238 | """ |
|
238 | """ | |
239 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
239 | A list of ast.NodeTransformer subclass instances, which will be applied | |
240 | to user input before code is run. |
|
240 | to user input before code is run. | |
241 | """ |
|
241 | """ | |
242 | ).tag(config=True) |
|
242 | ).tag(config=True) | |
243 |
|
243 | |||
244 | autocall = Enum((0,1,2), default_value=0, help= |
|
244 | autocall = Enum((0,1,2), default_value=0, help= | |
245 | """ |
|
245 | """ | |
246 | Make IPython automatically call any callable object even if you didn't |
|
246 | Make IPython automatically call any callable object even if you didn't | |
247 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
247 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' | |
248 | automatically. The value can be '0' to disable the feature, '1' for |
|
248 | automatically. The value can be '0' to disable the feature, '1' for | |
249 | 'smart' autocall, where it is not applied if there are no more |
|
249 | 'smart' autocall, where it is not applied if there are no more | |
250 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
250 | arguments on the line, and '2' for 'full' autocall, where all callable | |
251 | objects are automatically called (even if no arguments are present). |
|
251 | objects are automatically called (even if no arguments are present). | |
252 | """ |
|
252 | """ | |
253 | ).tag(config=True) |
|
253 | ).tag(config=True) | |
254 |
|
254 | |||
255 | autoindent = Bool(True, help= |
|
255 | autoindent = Bool(True, help= | |
256 | """ |
|
256 | """ | |
257 | Autoindent IPython code entered interactively. |
|
257 | Autoindent IPython code entered interactively. | |
258 | """ |
|
258 | """ | |
259 | ).tag(config=True) |
|
259 | ).tag(config=True) | |
260 |
|
260 | |||
261 | autoawait = Bool(True, help= |
|
261 | autoawait = Bool(True, help= | |
262 | """ |
|
262 | """ | |
263 | Automatically run await statement in the top level repl. |
|
263 | Automatically run await statement in the top level repl. | |
264 | """ |
|
264 | """ | |
265 | ).tag(config=True) |
|
265 | ).tag(config=True) | |
266 |
|
266 | |||
267 | loop_runner_map ={ |
|
267 | loop_runner_map ={ | |
268 | 'asyncio':(_asyncio_runner, True), |
|
268 | 'asyncio':(_asyncio_runner, True), | |
269 | 'curio':(_curio_runner, True), |
|
269 | 'curio':(_curio_runner, True), | |
270 | 'trio':(_trio_runner, True), |
|
270 | 'trio':(_trio_runner, True), | |
271 | 'sync': (_pseudo_sync_runner, False) |
|
271 | 'sync': (_pseudo_sync_runner, False) | |
272 | } |
|
272 | } | |
273 |
|
273 | |||
274 | loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner", |
|
274 | loop_runner = Any(default_value="IPython.core.interactiveshell._asyncio_runner", | |
275 | allow_none=True, |
|
275 | allow_none=True, | |
276 | help="""Select the loop runner that will be used to execute top-level asynchronous code""" |
|
276 | help="""Select the loop runner that will be used to execute top-level asynchronous code""" | |
277 | ).tag(config=True) |
|
277 | ).tag(config=True) | |
278 |
|
278 | |||
279 | @default('loop_runner') |
|
279 | @default('loop_runner') | |
280 | def _default_loop_runner(self): |
|
280 | def _default_loop_runner(self): | |
281 | return import_item("IPython.core.interactiveshell._asyncio_runner") |
|
281 | return import_item("IPython.core.interactiveshell._asyncio_runner") | |
282 |
|
282 | |||
283 | @validate('loop_runner') |
|
283 | @validate('loop_runner') | |
284 | def _import_runner(self, proposal): |
|
284 | def _import_runner(self, proposal): | |
285 | if isinstance(proposal.value, str): |
|
285 | if isinstance(proposal.value, str): | |
286 | if proposal.value in self.loop_runner_map: |
|
286 | if proposal.value in self.loop_runner_map: | |
287 | runner, autoawait = self.loop_runner_map[proposal.value] |
|
287 | runner, autoawait = self.loop_runner_map[proposal.value] | |
288 | self.autoawait = autoawait |
|
288 | self.autoawait = autoawait | |
289 | return runner |
|
289 | return runner | |
290 | runner = import_item(proposal.value) |
|
290 | runner = import_item(proposal.value) | |
291 | if not callable(runner): |
|
291 | if not callable(runner): | |
292 | raise ValueError('loop_runner must be callable') |
|
292 | raise ValueError('loop_runner must be callable') | |
293 | return runner |
|
293 | return runner | |
294 | if not callable(proposal.value): |
|
294 | if not callable(proposal.value): | |
295 | raise ValueError('loop_runner must be callable') |
|
295 | raise ValueError('loop_runner must be callable') | |
296 | return proposal.value |
|
296 | return proposal.value | |
297 |
|
297 | |||
298 | automagic = Bool(True, help= |
|
298 | automagic = Bool(True, help= | |
299 | """ |
|
299 | """ | |
300 | Enable magic commands to be called without the leading %. |
|
300 | Enable magic commands to be called without the leading %. | |
301 | """ |
|
301 | """ | |
302 | ).tag(config=True) |
|
302 | ).tag(config=True) | |
303 |
|
303 | |||
304 | banner1 = Unicode(default_banner, |
|
304 | banner1 = Unicode(default_banner, | |
305 | help="""The part of the banner to be printed before the profile""" |
|
305 | help="""The part of the banner to be printed before the profile""" | |
306 | ).tag(config=True) |
|
306 | ).tag(config=True) | |
307 | banner2 = Unicode('', |
|
307 | banner2 = Unicode('', | |
308 | help="""The part of the banner to be printed after the profile""" |
|
308 | help="""The part of the banner to be printed after the profile""" | |
309 | ).tag(config=True) |
|
309 | ).tag(config=True) | |
310 |
|
310 | |||
311 | cache_size = Integer(1000, help= |
|
311 | cache_size = Integer(1000, help= | |
312 | """ |
|
312 | """ | |
313 | Set the size of the output cache. The default is 1000, you can |
|
313 | Set the size of the output cache. The default is 1000, you can | |
314 | change it permanently in your config file. Setting it to 0 completely |
|
314 | change it permanently in your config file. Setting it to 0 completely | |
315 | disables the caching system, and the minimum value accepted is 3 (if |
|
315 | disables the caching system, and the minimum value accepted is 3 (if | |
316 | you provide a value less than 3, it is reset to 0 and a warning is |
|
316 | you provide a value less than 3, it is reset to 0 and a warning is | |
317 | issued). This limit is defined because otherwise you'll spend more |
|
317 | issued). This limit is defined because otherwise you'll spend more | |
318 | time re-flushing a too small cache than working |
|
318 | time re-flushing a too small cache than working | |
319 | """ |
|
319 | """ | |
320 | ).tag(config=True) |
|
320 | ).tag(config=True) | |
321 | color_info = Bool(True, help= |
|
321 | color_info = Bool(True, help= | |
322 | """ |
|
322 | """ | |
323 | Use colors for displaying information about objects. Because this |
|
323 | Use colors for displaying information about objects. Because this | |
324 | information is passed through a pager (like 'less'), and some pagers |
|
324 | information is passed through a pager (like 'less'), and some pagers | |
325 | get confused with color codes, this capability can be turned off. |
|
325 | get confused with color codes, this capability can be turned off. | |
326 | """ |
|
326 | """ | |
327 | ).tag(config=True) |
|
327 | ).tag(config=True) | |
328 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), |
|
328 | colors = CaselessStrEnum(('Neutral', 'NoColor','LightBG','Linux'), | |
329 | default_value='Neutral', |
|
329 | default_value='Neutral', | |
330 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." |
|
330 | help="Set the color scheme (NoColor, Neutral, Linux, or LightBG)." | |
331 | ).tag(config=True) |
|
331 | ).tag(config=True) | |
332 | debug = Bool(False).tag(config=True) |
|
332 | debug = Bool(False).tag(config=True) | |
333 | disable_failing_post_execute = Bool(False, |
|
333 | disable_failing_post_execute = Bool(False, | |
334 | help="Don't call post-execute functions that have failed in the past." |
|
334 | help="Don't call post-execute functions that have failed in the past." | |
335 | ).tag(config=True) |
|
335 | ).tag(config=True) | |
336 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
336 | display_formatter = Instance(DisplayFormatter, allow_none=True) | |
337 | displayhook_class = Type(DisplayHook) |
|
337 | displayhook_class = Type(DisplayHook) | |
338 | display_pub_class = Type(DisplayPublisher) |
|
338 | display_pub_class = Type(DisplayPublisher) | |
339 | compiler_class = Type(CachingCompiler) |
|
339 | compiler_class = Type(CachingCompiler) | |
340 |
|
340 | |||
341 | sphinxify_docstring = Bool(False, help= |
|
341 | sphinxify_docstring = Bool(False, help= | |
342 | """ |
|
342 | """ | |
343 | Enables rich html representation of docstrings. (This requires the |
|
343 | Enables rich html representation of docstrings. (This requires the | |
344 | docrepr module). |
|
344 | docrepr module). | |
345 | """).tag(config=True) |
|
345 | """).tag(config=True) | |
346 |
|
346 | |||
347 | @observe("sphinxify_docstring") |
|
347 | @observe("sphinxify_docstring") | |
348 | def _sphinxify_docstring_changed(self, change): |
|
348 | def _sphinxify_docstring_changed(self, change): | |
349 | if change['new']: |
|
349 | if change['new']: | |
350 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) |
|
350 | warn("`sphinxify_docstring` is provisional since IPython 5.0 and might change in future versions." , ProvisionalWarning) | |
351 |
|
351 | |||
352 | enable_html_pager = Bool(False, help= |
|
352 | enable_html_pager = Bool(False, help= | |
353 | """ |
|
353 | """ | |
354 | (Provisional API) enables html representation in mime bundles sent |
|
354 | (Provisional API) enables html representation in mime bundles sent | |
355 | to pagers. |
|
355 | to pagers. | |
356 | """).tag(config=True) |
|
356 | """).tag(config=True) | |
357 |
|
357 | |||
358 | @observe("enable_html_pager") |
|
358 | @observe("enable_html_pager") | |
359 | def _enable_html_pager_changed(self, change): |
|
359 | def _enable_html_pager_changed(self, change): | |
360 | if change['new']: |
|
360 | if change['new']: | |
361 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) |
|
361 | warn("`enable_html_pager` is provisional since IPython 5.0 and might change in future versions.", ProvisionalWarning) | |
362 |
|
362 | |||
363 | data_pub_class = None |
|
363 | data_pub_class = None | |
364 |
|
364 | |||
365 | exit_now = Bool(False) |
|
365 | exit_now = Bool(False) | |
366 | exiter = Instance(ExitAutocall) |
|
366 | exiter = Instance(ExitAutocall) | |
367 | @default('exiter') |
|
367 | @default('exiter') | |
368 | def _exiter_default(self): |
|
368 | def _exiter_default(self): | |
369 | return ExitAutocall(self) |
|
369 | return ExitAutocall(self) | |
370 | # Monotonically increasing execution counter |
|
370 | # Monotonically increasing execution counter | |
371 | execution_count = Integer(1) |
|
371 | execution_count = Integer(1) | |
372 | filename = Unicode("<ipython console>") |
|
372 | filename = Unicode("<ipython console>") | |
373 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ |
|
373 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
374 |
|
374 | |||
375 | # Used to transform cells before running them, and check whether code is complete |
|
375 | # Used to transform cells before running them, and check whether code is complete | |
376 | input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager', |
|
376 | input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager', | |
377 | ()) |
|
377 | ()) | |
378 |
|
378 | |||
379 | @property |
|
379 | @property | |
380 | def input_transformers_cleanup(self): |
|
380 | def input_transformers_cleanup(self): | |
381 | return self.input_transformer_manager.cleanup_transforms |
|
381 | return self.input_transformer_manager.cleanup_transforms | |
382 |
|
382 | |||
383 | input_transformers_post = List([], |
|
383 | input_transformers_post = List([], | |
384 | help="A list of string input transformers, to be applied after IPython's " |
|
384 | help="A list of string input transformers, to be applied after IPython's " | |
385 | "own input transformations." |
|
385 | "own input transformations." | |
386 | ) |
|
386 | ) | |
387 |
|
387 | |||
388 | @property |
|
388 | @property | |
389 | def input_splitter(self): |
|
389 | def input_splitter(self): | |
390 | """Make this available for backward compatibility (pre-7.0 release) with existing code. |
|
390 | """Make this available for backward compatibility (pre-7.0 release) with existing code. | |
391 |
|
391 | |||
392 | For example, ipykernel ipykernel currently uses |
|
392 | For example, ipykernel ipykernel currently uses | |
393 | `shell.input_splitter.check_complete` |
|
393 | `shell.input_splitter.check_complete` | |
394 | """ |
|
394 | """ | |
395 | from warnings import warn |
|
395 | from warnings import warn | |
396 | warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.", |
|
396 | warn("`input_splitter` is deprecated since IPython 7.0, prefer `input_transformer_manager`.", | |
397 | DeprecationWarning, stacklevel=2 |
|
397 | DeprecationWarning, stacklevel=2 | |
398 | ) |
|
398 | ) | |
399 | return self.input_transformer_manager |
|
399 | return self.input_transformer_manager | |
400 |
|
400 | |||
401 | logstart = Bool(False, help= |
|
401 | logstart = Bool(False, help= | |
402 | """ |
|
402 | """ | |
403 | Start logging to the default log file in overwrite mode. |
|
403 | Start logging to the default log file in overwrite mode. | |
404 | Use `logappend` to specify a log file to **append** logs to. |
|
404 | Use `logappend` to specify a log file to **append** logs to. | |
405 | """ |
|
405 | """ | |
406 | ).tag(config=True) |
|
406 | ).tag(config=True) | |
407 | logfile = Unicode('', help= |
|
407 | logfile = Unicode('', help= | |
408 | """ |
|
408 | """ | |
409 | The name of the logfile to use. |
|
409 | The name of the logfile to use. | |
410 | """ |
|
410 | """ | |
411 | ).tag(config=True) |
|
411 | ).tag(config=True) | |
412 | logappend = Unicode('', help= |
|
412 | logappend = Unicode('', help= | |
413 | """ |
|
413 | """ | |
414 | Start logging to the given file in append mode. |
|
414 | Start logging to the given file in append mode. | |
415 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
415 | Use `logfile` to specify a log file to **overwrite** logs to. | |
416 | """ |
|
416 | """ | |
417 | ).tag(config=True) |
|
417 | ).tag(config=True) | |
418 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
418 | object_info_string_level = Enum((0,1,2), default_value=0, | |
419 | ).tag(config=True) |
|
419 | ).tag(config=True) | |
420 | pdb = Bool(False, help= |
|
420 | pdb = Bool(False, help= | |
421 | """ |
|
421 | """ | |
422 | Automatically call the pdb debugger after every exception. |
|
422 | Automatically call the pdb debugger after every exception. | |
423 | """ |
|
423 | """ | |
424 | ).tag(config=True) |
|
424 | ).tag(config=True) | |
425 | display_page = Bool(False, |
|
425 | display_page = Bool(False, | |
426 | help="""If True, anything that would be passed to the pager |
|
426 | help="""If True, anything that would be passed to the pager | |
427 | will be displayed as regular output instead.""" |
|
427 | will be displayed as regular output instead.""" | |
428 | ).tag(config=True) |
|
428 | ).tag(config=True) | |
429 |
|
429 | |||
430 |
|
430 | |||
431 | show_rewritten_input = Bool(True, |
|
431 | show_rewritten_input = Bool(True, | |
432 | help="Show rewritten input, e.g. for autocall." |
|
432 | help="Show rewritten input, e.g. for autocall." | |
433 | ).tag(config=True) |
|
433 | ).tag(config=True) | |
434 |
|
434 | |||
435 | quiet = Bool(False).tag(config=True) |
|
435 | quiet = Bool(False).tag(config=True) | |
436 |
|
436 | |||
437 | history_length = Integer(10000, |
|
437 | history_length = Integer(10000, | |
438 | help='Total length of command history' |
|
438 | help='Total length of command history' | |
439 | ).tag(config=True) |
|
439 | ).tag(config=True) | |
440 |
|
440 | |||
441 | history_load_length = Integer(1000, help= |
|
441 | history_load_length = Integer(1000, help= | |
442 | """ |
|
442 | """ | |
443 | The number of saved history entries to be loaded |
|
443 | The number of saved history entries to be loaded | |
444 | into the history buffer at startup. |
|
444 | into the history buffer at startup. | |
445 | """ |
|
445 | """ | |
446 | ).tag(config=True) |
|
446 | ).tag(config=True) | |
447 |
|
447 | |||
448 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'], |
|
448 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'], | |
449 | default_value='last_expr', |
|
449 | default_value='last_expr', | |
450 | help=""" |
|
450 | help=""" | |
451 | 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying |
|
451 | 'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying | |
452 | which nodes should be run interactively (displaying output from expressions). |
|
452 | which nodes should be run interactively (displaying output from expressions). | |
453 | """ |
|
453 | """ | |
454 | ).tag(config=True) |
|
454 | ).tag(config=True) | |
455 |
|
455 | |||
456 | # TODO: this part of prompt management should be moved to the frontends. |
|
456 | # TODO: this part of prompt management should be moved to the frontends. | |
457 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
457 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' | |
458 | separate_in = SeparateUnicode('\n').tag(config=True) |
|
458 | separate_in = SeparateUnicode('\n').tag(config=True) | |
459 | separate_out = SeparateUnicode('').tag(config=True) |
|
459 | separate_out = SeparateUnicode('').tag(config=True) | |
460 | separate_out2 = SeparateUnicode('').tag(config=True) |
|
460 | separate_out2 = SeparateUnicode('').tag(config=True) | |
461 | wildcards_case_sensitive = Bool(True).tag(config=True) |
|
461 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
462 | xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'), |
|
462 | xmode = CaselessStrEnum(('Context', 'Plain', 'Verbose', 'Minimal'), | |
463 | default_value='Context', |
|
463 | default_value='Context', | |
464 | help="Switch modes for the IPython exception handlers." |
|
464 | help="Switch modes for the IPython exception handlers." | |
465 | ).tag(config=True) |
|
465 | ).tag(config=True) | |
466 |
|
466 | |||
467 | # Subcomponents of InteractiveShell |
|
467 | # Subcomponents of InteractiveShell | |
468 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
468 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) | |
469 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
469 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) | |
470 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
470 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) | |
471 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
471 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) | |
472 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
472 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) | |
473 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
473 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) | |
474 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
474 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) | |
475 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
475 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) | |
476 |
|
476 | |||
477 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
477 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) | |
478 | @property |
|
478 | @property | |
479 | def profile(self): |
|
479 | def profile(self): | |
480 | if self.profile_dir is not None: |
|
480 | if self.profile_dir is not None: | |
481 | name = os.path.basename(self.profile_dir.location) |
|
481 | name = os.path.basename(self.profile_dir.location) | |
482 | return name.replace('profile_','') |
|
482 | return name.replace('profile_','') | |
483 |
|
483 | |||
484 |
|
484 | |||
485 | # Private interface |
|
485 | # Private interface | |
486 | _post_execute = Dict() |
|
486 | _post_execute = Dict() | |
487 |
|
487 | |||
488 | # Tracks any GUI loop loaded for pylab |
|
488 | # Tracks any GUI loop loaded for pylab | |
489 | pylab_gui_select = None |
|
489 | pylab_gui_select = None | |
490 |
|
490 | |||
491 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') |
|
491 | last_execution_succeeded = Bool(True, help='Did last executed command succeeded') | |
492 |
|
492 | |||
493 | last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True) |
|
493 | last_execution_result = Instance('IPython.core.interactiveshell.ExecutionResult', help='Result of executing the last command', allow_none=True) | |
494 |
|
494 | |||
495 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
495 | def __init__(self, ipython_dir=None, profile_dir=None, | |
496 | user_module=None, user_ns=None, |
|
496 | user_module=None, user_ns=None, | |
497 | custom_exceptions=((), None), **kwargs): |
|
497 | custom_exceptions=((), None), **kwargs): | |
498 |
|
498 | |||
499 | # This is where traits with a config_key argument are updated |
|
499 | # This is where traits with a config_key argument are updated | |
500 | # from the values on config. |
|
500 | # from the values on config. | |
501 | super(InteractiveShell, self).__init__(**kwargs) |
|
501 | super(InteractiveShell, self).__init__(**kwargs) | |
502 | if 'PromptManager' in self.config: |
|
502 | if 'PromptManager' in self.config: | |
503 | warn('As of IPython 5.0 `PromptManager` config will have no effect' |
|
503 | warn('As of IPython 5.0 `PromptManager` config will have no effect' | |
504 | ' and has been replaced by TerminalInteractiveShell.prompts_class') |
|
504 | ' and has been replaced by TerminalInteractiveShell.prompts_class') | |
505 | self.configurables = [self] |
|
505 | self.configurables = [self] | |
506 |
|
506 | |||
507 | # These are relatively independent and stateless |
|
507 | # These are relatively independent and stateless | |
508 | self.init_ipython_dir(ipython_dir) |
|
508 | self.init_ipython_dir(ipython_dir) | |
509 | self.init_profile_dir(profile_dir) |
|
509 | self.init_profile_dir(profile_dir) | |
510 | self.init_instance_attrs() |
|
510 | self.init_instance_attrs() | |
511 | self.init_environment() |
|
511 | self.init_environment() | |
512 |
|
512 | |||
513 | # Check if we're in a virtualenv, and set up sys.path. |
|
513 | # Check if we're in a virtualenv, and set up sys.path. | |
514 | self.init_virtualenv() |
|
514 | self.init_virtualenv() | |
515 |
|
515 | |||
516 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
516 | # Create namespaces (user_ns, user_global_ns, etc.) | |
517 | self.init_create_namespaces(user_module, user_ns) |
|
517 | self.init_create_namespaces(user_module, user_ns) | |
518 | # This has to be done after init_create_namespaces because it uses |
|
518 | # This has to be done after init_create_namespaces because it uses | |
519 | # something in self.user_ns, but before init_sys_modules, which |
|
519 | # something in self.user_ns, but before init_sys_modules, which | |
520 | # is the first thing to modify sys. |
|
520 | # is the first thing to modify sys. | |
521 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
521 | # TODO: When we override sys.stdout and sys.stderr before this class | |
522 | # is created, we are saving the overridden ones here. Not sure if this |
|
522 | # is created, we are saving the overridden ones here. Not sure if this | |
523 | # is what we want to do. |
|
523 | # is what we want to do. | |
524 | self.save_sys_module_state() |
|
524 | self.save_sys_module_state() | |
525 | self.init_sys_modules() |
|
525 | self.init_sys_modules() | |
526 |
|
526 | |||
527 | # While we're trying to have each part of the code directly access what |
|
527 | # While we're trying to have each part of the code directly access what | |
528 | # it needs without keeping redundant references to objects, we have too |
|
528 | # it needs without keeping redundant references to objects, we have too | |
529 | # much legacy code that expects ip.db to exist. |
|
529 | # much legacy code that expects ip.db to exist. | |
530 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
530 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) | |
531 |
|
531 | |||
532 | self.init_history() |
|
532 | self.init_history() | |
533 | self.init_encoding() |
|
533 | self.init_encoding() | |
534 | self.init_prefilter() |
|
534 | self.init_prefilter() | |
535 |
|
535 | |||
536 | self.init_syntax_highlighting() |
|
536 | self.init_syntax_highlighting() | |
537 | self.init_hooks() |
|
537 | self.init_hooks() | |
538 | self.init_events() |
|
538 | self.init_events() | |
539 | self.init_pushd_popd_magic() |
|
539 | self.init_pushd_popd_magic() | |
540 | self.init_user_ns() |
|
540 | self.init_user_ns() | |
541 | self.init_logger() |
|
541 | self.init_logger() | |
542 | self.init_builtins() |
|
542 | self.init_builtins() | |
543 |
|
543 | |||
544 | # The following was in post_config_initialization |
|
544 | # The following was in post_config_initialization | |
545 | self.init_inspector() |
|
545 | self.init_inspector() | |
546 | self.raw_input_original = input |
|
546 | self.raw_input_original = input | |
547 | self.init_completer() |
|
547 | self.init_completer() | |
548 | # TODO: init_io() needs to happen before init_traceback handlers |
|
548 | # TODO: init_io() needs to happen before init_traceback handlers | |
549 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
549 | # because the traceback handlers hardcode the stdout/stderr streams. | |
550 | # This logic in in debugger.Pdb and should eventually be changed. |
|
550 | # This logic in in debugger.Pdb and should eventually be changed. | |
551 | self.init_io() |
|
551 | self.init_io() | |
552 | self.init_traceback_handlers(custom_exceptions) |
|
552 | self.init_traceback_handlers(custom_exceptions) | |
553 | self.init_prompts() |
|
553 | self.init_prompts() | |
554 | self.init_display_formatter() |
|
554 | self.init_display_formatter() | |
555 | self.init_display_pub() |
|
555 | self.init_display_pub() | |
556 | self.init_data_pub() |
|
556 | self.init_data_pub() | |
557 | self.init_displayhook() |
|
557 | self.init_displayhook() | |
558 | self.init_magics() |
|
558 | self.init_magics() | |
559 | self.init_alias() |
|
559 | self.init_alias() | |
560 | self.init_logstart() |
|
560 | self.init_logstart() | |
561 | self.init_pdb() |
|
561 | self.init_pdb() | |
562 | self.init_extension_manager() |
|
562 | self.init_extension_manager() | |
563 | self.init_payload() |
|
563 | self.init_payload() | |
564 | self.hooks.late_startup_hook() |
|
564 | self.hooks.late_startup_hook() | |
565 | self.events.trigger('shell_initialized', self) |
|
565 | self.events.trigger('shell_initialized', self) | |
566 | atexit.register(self.atexit_operations) |
|
566 | atexit.register(self.atexit_operations) | |
567 |
|
567 | |||
568 | # The trio runner is used for running Trio in the foreground thread. It |
|
568 | # The trio runner is used for running Trio in the foreground thread. It | |
569 | # is different from `_trio_runner(async_fn)` in `async_helpers.py` |
|
569 | # is different from `_trio_runner(async_fn)` in `async_helpers.py` | |
570 | # which calls `trio.run()` for every cell. This runner runs all cells |
|
570 | # which calls `trio.run()` for every cell. This runner runs all cells | |
571 | # inside a single Trio event loop. If used, it is set from |
|
571 | # inside a single Trio event loop. If used, it is set from | |
572 | # `ipykernel.kernelapp`. |
|
572 | # `ipykernel.kernelapp`. | |
573 | self.trio_runner = None |
|
573 | self.trio_runner = None | |
574 |
|
574 | |||
575 | def get_ipython(self): |
|
575 | def get_ipython(self): | |
576 | """Return the currently running IPython instance.""" |
|
576 | """Return the currently running IPython instance.""" | |
577 | return self |
|
577 | return self | |
578 |
|
578 | |||
579 | #------------------------------------------------------------------------- |
|
579 | #------------------------------------------------------------------------- | |
580 | # Trait changed handlers |
|
580 | # Trait changed handlers | |
581 | #------------------------------------------------------------------------- |
|
581 | #------------------------------------------------------------------------- | |
582 | @observe('ipython_dir') |
|
582 | @observe('ipython_dir') | |
583 | def _ipython_dir_changed(self, change): |
|
583 | def _ipython_dir_changed(self, change): | |
584 | ensure_dir_exists(change['new']) |
|
584 | ensure_dir_exists(change['new']) | |
585 |
|
585 | |||
586 | def set_autoindent(self,value=None): |
|
586 | def set_autoindent(self,value=None): | |
587 | """Set the autoindent flag. |
|
587 | """Set the autoindent flag. | |
588 |
|
588 | |||
589 | If called with no arguments, it acts as a toggle.""" |
|
589 | If called with no arguments, it acts as a toggle.""" | |
590 | if value is None: |
|
590 | if value is None: | |
591 | self.autoindent = not self.autoindent |
|
591 | self.autoindent = not self.autoindent | |
592 | else: |
|
592 | else: | |
593 | self.autoindent = value |
|
593 | self.autoindent = value | |
594 |
|
594 | |||
595 | def set_trio_runner(self, tr): |
|
595 | def set_trio_runner(self, tr): | |
596 | self.trio_runner = tr |
|
596 | self.trio_runner = tr | |
597 |
|
597 | |||
598 | #------------------------------------------------------------------------- |
|
598 | #------------------------------------------------------------------------- | |
599 | # init_* methods called by __init__ |
|
599 | # init_* methods called by __init__ | |
600 | #------------------------------------------------------------------------- |
|
600 | #------------------------------------------------------------------------- | |
601 |
|
601 | |||
602 | def init_ipython_dir(self, ipython_dir): |
|
602 | def init_ipython_dir(self, ipython_dir): | |
603 | if ipython_dir is not None: |
|
603 | if ipython_dir is not None: | |
604 | self.ipython_dir = ipython_dir |
|
604 | self.ipython_dir = ipython_dir | |
605 | return |
|
605 | return | |
606 |
|
606 | |||
607 | self.ipython_dir = get_ipython_dir() |
|
607 | self.ipython_dir = get_ipython_dir() | |
608 |
|
608 | |||
609 | def init_profile_dir(self, profile_dir): |
|
609 | def init_profile_dir(self, profile_dir): | |
610 | if profile_dir is not None: |
|
610 | if profile_dir is not None: | |
611 | self.profile_dir = profile_dir |
|
611 | self.profile_dir = profile_dir | |
612 | return |
|
612 | return | |
613 | self.profile_dir = ProfileDir.create_profile_dir_by_name( |
|
613 | self.profile_dir = ProfileDir.create_profile_dir_by_name( | |
614 | self.ipython_dir, "default" |
|
614 | self.ipython_dir, "default" | |
615 | ) |
|
615 | ) | |
616 |
|
616 | |||
617 | def init_instance_attrs(self): |
|
617 | def init_instance_attrs(self): | |
618 | self.more = False |
|
618 | self.more = False | |
619 |
|
619 | |||
620 | # command compiler |
|
620 | # command compiler | |
621 | self.compile = self.compiler_class() |
|
621 | self.compile = self.compiler_class() | |
622 |
|
622 | |||
623 | # Make an empty namespace, which extension writers can rely on both |
|
623 | # Make an empty namespace, which extension writers can rely on both | |
624 | # existing and NEVER being used by ipython itself. This gives them a |
|
624 | # existing and NEVER being used by ipython itself. This gives them a | |
625 | # convenient location for storing additional information and state |
|
625 | # convenient location for storing additional information and state | |
626 | # their extensions may require, without fear of collisions with other |
|
626 | # their extensions may require, without fear of collisions with other | |
627 | # ipython names that may develop later. |
|
627 | # ipython names that may develop later. | |
628 | self.meta = Struct() |
|
628 | self.meta = Struct() | |
629 |
|
629 | |||
630 | # Temporary files used for various purposes. Deleted at exit. |
|
630 | # Temporary files used for various purposes. Deleted at exit. | |
631 | # The files here are stored with Path from Pathlib |
|
631 | # The files here are stored with Path from Pathlib | |
632 | self.tempfiles = [] |
|
632 | self.tempfiles = [] | |
633 | self.tempdirs = [] |
|
633 | self.tempdirs = [] | |
634 |
|
634 | |||
635 | # keep track of where we started running (mainly for crash post-mortem) |
|
635 | # keep track of where we started running (mainly for crash post-mortem) | |
636 | # This is not being used anywhere currently. |
|
636 | # This is not being used anywhere currently. | |
637 | self.starting_dir = os.getcwd() |
|
637 | self.starting_dir = os.getcwd() | |
638 |
|
638 | |||
639 | # Indentation management |
|
639 | # Indentation management | |
640 | self.indent_current_nsp = 0 |
|
640 | self.indent_current_nsp = 0 | |
641 |
|
641 | |||
642 | # Dict to track post-execution functions that have been registered |
|
642 | # Dict to track post-execution functions that have been registered | |
643 | self._post_execute = {} |
|
643 | self._post_execute = {} | |
644 |
|
644 | |||
645 | def init_environment(self): |
|
645 | def init_environment(self): | |
646 | """Any changes we need to make to the user's environment.""" |
|
646 | """Any changes we need to make to the user's environment.""" | |
647 | pass |
|
647 | pass | |
648 |
|
648 | |||
649 | def init_encoding(self): |
|
649 | def init_encoding(self): | |
650 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
650 | # Get system encoding at startup time. Certain terminals (like Emacs | |
651 | # under Win32 have it set to None, and we need to have a known valid |
|
651 | # under Win32 have it set to None, and we need to have a known valid | |
652 | # encoding to use in the raw_input() method |
|
652 | # encoding to use in the raw_input() method | |
653 | try: |
|
653 | try: | |
654 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
654 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
655 | except AttributeError: |
|
655 | except AttributeError: | |
656 | self.stdin_encoding = 'ascii' |
|
656 | self.stdin_encoding = 'ascii' | |
657 |
|
657 | |||
658 |
|
658 | |||
659 | @observe('colors') |
|
659 | @observe('colors') | |
660 | def init_syntax_highlighting(self, changes=None): |
|
660 | def init_syntax_highlighting(self, changes=None): | |
661 | # Python source parser/formatter for syntax highlighting |
|
661 | # Python source parser/formatter for syntax highlighting | |
662 | pyformat = PyColorize.Parser(style=self.colors, parent=self).format |
|
662 | pyformat = PyColorize.Parser(style=self.colors, parent=self).format | |
663 | self.pycolorize = lambda src: pyformat(src,'str') |
|
663 | self.pycolorize = lambda src: pyformat(src,'str') | |
664 |
|
664 | |||
665 | def refresh_style(self): |
|
665 | def refresh_style(self): | |
666 | # No-op here, used in subclass |
|
666 | # No-op here, used in subclass | |
667 | pass |
|
667 | pass | |
668 |
|
668 | |||
669 | def init_pushd_popd_magic(self): |
|
669 | def init_pushd_popd_magic(self): | |
670 | # for pushd/popd management |
|
670 | # for pushd/popd management | |
671 | self.home_dir = get_home_dir() |
|
671 | self.home_dir = get_home_dir() | |
672 |
|
672 | |||
673 | self.dir_stack = [] |
|
673 | self.dir_stack = [] | |
674 |
|
674 | |||
675 | def init_logger(self): |
|
675 | def init_logger(self): | |
676 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
676 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', | |
677 | logmode='rotate') |
|
677 | logmode='rotate') | |
678 |
|
678 | |||
679 | def init_logstart(self): |
|
679 | def init_logstart(self): | |
680 | """Initialize logging in case it was requested at the command line. |
|
680 | """Initialize logging in case it was requested at the command line. | |
681 | """ |
|
681 | """ | |
682 | if self.logappend: |
|
682 | if self.logappend: | |
683 | self.magic('logstart %s append' % self.logappend) |
|
683 | self.magic('logstart %s append' % self.logappend) | |
684 | elif self.logfile: |
|
684 | elif self.logfile: | |
685 | self.magic('logstart %s' % self.logfile) |
|
685 | self.magic('logstart %s' % self.logfile) | |
686 | elif self.logstart: |
|
686 | elif self.logstart: | |
687 | self.magic('logstart') |
|
687 | self.magic('logstart') | |
688 |
|
688 | |||
689 |
|
689 | |||
690 | def init_builtins(self): |
|
690 | def init_builtins(self): | |
691 | # A single, static flag that we set to True. Its presence indicates |
|
691 | # A single, static flag that we set to True. Its presence indicates | |
692 | # that an IPython shell has been created, and we make no attempts at |
|
692 | # that an IPython shell has been created, and we make no attempts at | |
693 | # removing on exit or representing the existence of more than one |
|
693 | # removing on exit or representing the existence of more than one | |
694 | # IPython at a time. |
|
694 | # IPython at a time. | |
695 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
695 | builtin_mod.__dict__['__IPYTHON__'] = True | |
696 | builtin_mod.__dict__['display'] = display |
|
696 | builtin_mod.__dict__['display'] = display | |
697 |
|
697 | |||
698 | self.builtin_trap = BuiltinTrap(shell=self) |
|
698 | self.builtin_trap = BuiltinTrap(shell=self) | |
699 |
|
699 | |||
700 | @observe('colors') |
|
700 | @observe('colors') | |
701 | def init_inspector(self, changes=None): |
|
701 | def init_inspector(self, changes=None): | |
702 | # Object inspector |
|
702 | # Object inspector | |
703 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
703 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
704 | PyColorize.ANSICodeColors, |
|
704 | PyColorize.ANSICodeColors, | |
705 | self.colors, |
|
705 | self.colors, | |
706 | self.object_info_string_level) |
|
706 | self.object_info_string_level) | |
707 |
|
707 | |||
708 | def init_io(self): |
|
708 | def init_io(self): | |
709 | # implemented in subclasses, TerminalInteractiveShell does call |
|
709 | # implemented in subclasses, TerminalInteractiveShell does call | |
710 | # colorama.init(). |
|
710 | # colorama.init(). | |
711 | pass |
|
711 | pass | |
712 |
|
712 | |||
713 | def init_prompts(self): |
|
713 | def init_prompts(self): | |
714 | # Set system prompts, so that scripts can decide if they are running |
|
714 | # Set system prompts, so that scripts can decide if they are running | |
715 | # interactively. |
|
715 | # interactively. | |
716 | sys.ps1 = 'In : ' |
|
716 | sys.ps1 = 'In : ' | |
717 | sys.ps2 = '...: ' |
|
717 | sys.ps2 = '...: ' | |
718 | sys.ps3 = 'Out: ' |
|
718 | sys.ps3 = 'Out: ' | |
719 |
|
719 | |||
720 | def init_display_formatter(self): |
|
720 | def init_display_formatter(self): | |
721 | self.display_formatter = DisplayFormatter(parent=self) |
|
721 | self.display_formatter = DisplayFormatter(parent=self) | |
722 | self.configurables.append(self.display_formatter) |
|
722 | self.configurables.append(self.display_formatter) | |
723 |
|
723 | |||
724 | def init_display_pub(self): |
|
724 | def init_display_pub(self): | |
725 | self.display_pub = self.display_pub_class(parent=self, shell=self) |
|
725 | self.display_pub = self.display_pub_class(parent=self, shell=self) | |
726 | self.configurables.append(self.display_pub) |
|
726 | self.configurables.append(self.display_pub) | |
727 |
|
727 | |||
728 | def init_data_pub(self): |
|
728 | def init_data_pub(self): | |
729 | if not self.data_pub_class: |
|
729 | if not self.data_pub_class: | |
730 | self.data_pub = None |
|
730 | self.data_pub = None | |
731 | return |
|
731 | return | |
732 | self.data_pub = self.data_pub_class(parent=self) |
|
732 | self.data_pub = self.data_pub_class(parent=self) | |
733 | self.configurables.append(self.data_pub) |
|
733 | self.configurables.append(self.data_pub) | |
734 |
|
734 | |||
735 | def init_displayhook(self): |
|
735 | def init_displayhook(self): | |
736 | # Initialize displayhook, set in/out prompts and printing system |
|
736 | # Initialize displayhook, set in/out prompts and printing system | |
737 | self.displayhook = self.displayhook_class( |
|
737 | self.displayhook = self.displayhook_class( | |
738 | parent=self, |
|
738 | parent=self, | |
739 | shell=self, |
|
739 | shell=self, | |
740 | cache_size=self.cache_size, |
|
740 | cache_size=self.cache_size, | |
741 | ) |
|
741 | ) | |
742 | self.configurables.append(self.displayhook) |
|
742 | self.configurables.append(self.displayhook) | |
743 | # This is a context manager that installs/revmoes the displayhook at |
|
743 | # This is a context manager that installs/revmoes the displayhook at | |
744 | # the appropriate time. |
|
744 | # the appropriate time. | |
745 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
745 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
746 |
|
746 | |||
747 | def init_virtualenv(self): |
|
747 | def init_virtualenv(self): | |
748 | """Add the current virtualenv to sys.path so the user can import modules from it. |
|
748 | """Add the current virtualenv to sys.path so the user can import modules from it. | |
749 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
749 | This isn't perfect: it doesn't use the Python interpreter with which the | |
750 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
750 | virtualenv was built, and it ignores the --no-site-packages option. A | |
751 | warning will appear suggesting the user installs IPython in the |
|
751 | warning will appear suggesting the user installs IPython in the | |
752 | virtualenv, but for many cases, it probably works well enough. |
|
752 | virtualenv, but for many cases, it probably works well enough. | |
753 |
|
753 | |||
754 | Adapted from code snippets online. |
|
754 | Adapted from code snippets online. | |
755 |
|
755 | |||
756 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
756 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv | |
757 | """ |
|
757 | """ | |
758 | if 'VIRTUAL_ENV' not in os.environ: |
|
758 | if 'VIRTUAL_ENV' not in os.environ: | |
759 | # Not in a virtualenv |
|
759 | # Not in a virtualenv | |
760 | return |
|
760 | return | |
761 | elif os.environ["VIRTUAL_ENV"] == "": |
|
761 | elif os.environ["VIRTUAL_ENV"] == "": | |
762 | warn("Virtual env path set to '', please check if this is intended.") |
|
762 | warn("Virtual env path set to '', please check if this is intended.") | |
763 | return |
|
763 | return | |
764 |
|
764 | |||
765 | p = Path(sys.executable) |
|
765 | p = Path(sys.executable) | |
766 | p_venv = Path(os.environ["VIRTUAL_ENV"]) |
|
766 | p_venv = Path(os.environ["VIRTUAL_ENV"]) | |
767 |
|
767 | |||
768 | # fallback venv detection: |
|
768 | # fallback venv detection: | |
769 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
769 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
770 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
770 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
771 | # So we just check every item in the symlink tree (generally <= 3) |
|
771 | # So we just check every item in the symlink tree (generally <= 3) | |
772 | paths = [p] |
|
772 | paths = [p] | |
773 | while p.is_symlink(): |
|
773 | while p.is_symlink(): | |
774 | p = Path(os.readlink(p)) |
|
774 | p = Path(os.readlink(p)) | |
775 | paths.append(p.resolve()) |
|
775 | paths.append(p.resolve()) | |
776 |
|
776 | |||
777 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible |
|
777 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible | |
778 | if p_venv.parts[1] == "cygdrive": |
|
778 | if p_venv.parts[1] == "cygdrive": | |
779 | drive_name = p_venv.parts[2] |
|
779 | drive_name = p_venv.parts[2] | |
780 | p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:]) |
|
780 | p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:]) | |
781 |
|
781 | |||
782 | if any(p_venv == p.parents[1] for p in paths): |
|
782 | if any(p_venv == p.parents[1] for p in paths): | |
783 | # Our exe is inside or has access to the virtualenv, don't need to do anything. |
|
783 | # Our exe is inside or has access to the virtualenv, don't need to do anything. | |
784 | return |
|
784 | return | |
785 |
|
785 | |||
786 | if sys.platform == "win32": |
|
786 | if sys.platform == "win32": | |
787 | virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages")) |
|
787 | virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages")) | |
788 | else: |
|
788 | else: | |
789 | virtual_env_path = Path( |
|
789 | virtual_env_path = Path( | |
790 | os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages" |
|
790 | os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages" | |
791 | ) |
|
791 | ) | |
792 | p_ver = sys.version_info[:2] |
|
792 | p_ver = sys.version_info[:2] | |
793 |
|
793 | |||
794 | # Predict version from py[thon]-x.x in the $VIRTUAL_ENV |
|
794 | # Predict version from py[thon]-x.x in the $VIRTUAL_ENV | |
795 | re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"]) |
|
795 | re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"]) | |
796 | if re_m: |
|
796 | if re_m: | |
797 | predicted_path = Path(str(virtual_env_path).format(*re_m.groups())) |
|
797 | predicted_path = Path(str(virtual_env_path).format(*re_m.groups())) | |
798 | if predicted_path.exists(): |
|
798 | if predicted_path.exists(): | |
799 | p_ver = re_m.groups() |
|
799 | p_ver = re_m.groups() | |
800 |
|
800 | |||
801 | virtual_env = str(virtual_env_path).format(*p_ver) |
|
801 | virtual_env = str(virtual_env_path).format(*p_ver) | |
802 |
|
802 | |||
803 | warn( |
|
803 | warn( | |
804 | "Attempting to work in a virtualenv. If you encounter problems, " |
|
804 | "Attempting to work in a virtualenv. If you encounter problems, " | |
805 | "please install IPython inside the virtualenv." |
|
805 | "please install IPython inside the virtualenv." | |
806 | ) |
|
806 | ) | |
807 | import site |
|
807 | import site | |
808 | sys.path.insert(0, virtual_env) |
|
808 | sys.path.insert(0, virtual_env) | |
809 | site.addsitedir(virtual_env) |
|
809 | site.addsitedir(virtual_env) | |
810 |
|
810 | |||
811 | #------------------------------------------------------------------------- |
|
811 | #------------------------------------------------------------------------- | |
812 | # Things related to injections into the sys module |
|
812 | # Things related to injections into the sys module | |
813 | #------------------------------------------------------------------------- |
|
813 | #------------------------------------------------------------------------- | |
814 |
|
814 | |||
815 | def save_sys_module_state(self): |
|
815 | def save_sys_module_state(self): | |
816 | """Save the state of hooks in the sys module. |
|
816 | """Save the state of hooks in the sys module. | |
817 |
|
817 | |||
818 | This has to be called after self.user_module is created. |
|
818 | This has to be called after self.user_module is created. | |
819 | """ |
|
819 | """ | |
820 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
820 | self._orig_sys_module_state = {'stdin': sys.stdin, | |
821 | 'stdout': sys.stdout, |
|
821 | 'stdout': sys.stdout, | |
822 | 'stderr': sys.stderr, |
|
822 | 'stderr': sys.stderr, | |
823 | 'excepthook': sys.excepthook} |
|
823 | 'excepthook': sys.excepthook} | |
824 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
824 | self._orig_sys_modules_main_name = self.user_module.__name__ | |
825 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
825 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) | |
826 |
|
826 | |||
827 | def restore_sys_module_state(self): |
|
827 | def restore_sys_module_state(self): | |
828 | """Restore the state of the sys module.""" |
|
828 | """Restore the state of the sys module.""" | |
829 | try: |
|
829 | try: | |
830 | for k, v in self._orig_sys_module_state.items(): |
|
830 | for k, v in self._orig_sys_module_state.items(): | |
831 | setattr(sys, k, v) |
|
831 | setattr(sys, k, v) | |
832 | except AttributeError: |
|
832 | except AttributeError: | |
833 | pass |
|
833 | pass | |
834 | # Reset what what done in self.init_sys_modules |
|
834 | # Reset what what done in self.init_sys_modules | |
835 | if self._orig_sys_modules_main_mod is not None: |
|
835 | if self._orig_sys_modules_main_mod is not None: | |
836 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
836 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod | |
837 |
|
837 | |||
838 | #------------------------------------------------------------------------- |
|
838 | #------------------------------------------------------------------------- | |
839 | # Things related to the banner |
|
839 | # Things related to the banner | |
840 | #------------------------------------------------------------------------- |
|
840 | #------------------------------------------------------------------------- | |
841 |
|
841 | |||
842 | @property |
|
842 | @property | |
843 | def banner(self): |
|
843 | def banner(self): | |
844 | banner = self.banner1 |
|
844 | banner = self.banner1 | |
845 | if self.profile and self.profile != 'default': |
|
845 | if self.profile and self.profile != 'default': | |
846 | banner += '\nIPython profile: %s\n' % self.profile |
|
846 | banner += '\nIPython profile: %s\n' % self.profile | |
847 | if self.banner2: |
|
847 | if self.banner2: | |
848 | banner += '\n' + self.banner2 |
|
848 | banner += '\n' + self.banner2 | |
849 | return banner |
|
849 | return banner | |
850 |
|
850 | |||
851 | def show_banner(self, banner=None): |
|
851 | def show_banner(self, banner=None): | |
852 | if banner is None: |
|
852 | if banner is None: | |
853 | banner = self.banner |
|
853 | banner = self.banner | |
854 | sys.stdout.write(banner) |
|
854 | sys.stdout.write(banner) | |
855 |
|
855 | |||
856 | #------------------------------------------------------------------------- |
|
856 | #------------------------------------------------------------------------- | |
857 | # Things related to hooks |
|
857 | # Things related to hooks | |
858 | #------------------------------------------------------------------------- |
|
858 | #------------------------------------------------------------------------- | |
859 |
|
859 | |||
860 | def init_hooks(self): |
|
860 | def init_hooks(self): | |
861 | # hooks holds pointers used for user-side customizations |
|
861 | # hooks holds pointers used for user-side customizations | |
862 | self.hooks = Struct() |
|
862 | self.hooks = Struct() | |
863 |
|
863 | |||
864 | self.strdispatchers = {} |
|
864 | self.strdispatchers = {} | |
865 |
|
865 | |||
866 | # Set all default hooks, defined in the IPython.hooks module. |
|
866 | # Set all default hooks, defined in the IPython.hooks module. | |
867 | hooks = IPython.core.hooks |
|
867 | hooks = IPython.core.hooks | |
868 | for hook_name in hooks.__all__: |
|
868 | for hook_name in hooks.__all__: | |
869 | # default hooks have priority 100, i.e. low; user hooks should have |
|
869 | # default hooks have priority 100, i.e. low; user hooks should have | |
870 | # 0-100 priority |
|
870 | # 0-100 priority | |
871 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
871 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) | |
872 |
|
872 | |||
873 | if self.display_page: |
|
873 | if self.display_page: | |
874 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
874 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) | |
875 |
|
875 | |||
876 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
876 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, | |
877 | _warn_deprecated=True): |
|
877 | _warn_deprecated=True): | |
878 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
878 | """set_hook(name,hook) -> sets an internal IPython hook. | |
879 |
|
879 | |||
880 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
880 | IPython exposes some of its internal API as user-modifiable hooks. By | |
881 | adding your function to one of these hooks, you can modify IPython's |
|
881 | adding your function to one of these hooks, you can modify IPython's | |
882 | behavior to call at runtime your own routines.""" |
|
882 | behavior to call at runtime your own routines.""" | |
883 |
|
883 | |||
884 | # At some point in the future, this should validate the hook before it |
|
884 | # At some point in the future, this should validate the hook before it | |
885 | # accepts it. Probably at least check that the hook takes the number |
|
885 | # accepts it. Probably at least check that the hook takes the number | |
886 | # of args it's supposed to. |
|
886 | # of args it's supposed to. | |
887 |
|
887 | |||
888 | f = types.MethodType(hook,self) |
|
888 | f = types.MethodType(hook,self) | |
889 |
|
889 | |||
890 | # check if the hook is for strdispatcher first |
|
890 | # check if the hook is for strdispatcher first | |
891 | if str_key is not None: |
|
891 | if str_key is not None: | |
892 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
892 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
893 | sdp.add_s(str_key, f, priority ) |
|
893 | sdp.add_s(str_key, f, priority ) | |
894 | self.strdispatchers[name] = sdp |
|
894 | self.strdispatchers[name] = sdp | |
895 | return |
|
895 | return | |
896 | if re_key is not None: |
|
896 | if re_key is not None: | |
897 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
897 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
898 | sdp.add_re(re.compile(re_key), f, priority ) |
|
898 | sdp.add_re(re.compile(re_key), f, priority ) | |
899 | self.strdispatchers[name] = sdp |
|
899 | self.strdispatchers[name] = sdp | |
900 | return |
|
900 | return | |
901 |
|
901 | |||
902 | dp = getattr(self.hooks, name, None) |
|
902 | dp = getattr(self.hooks, name, None) | |
903 | if name not in IPython.core.hooks.__all__: |
|
903 | if name not in IPython.core.hooks.__all__: | |
904 | print("Warning! Hook '%s' is not one of %s" % \ |
|
904 | print("Warning! Hook '%s' is not one of %s" % \ | |
905 | (name, IPython.core.hooks.__all__ )) |
|
905 | (name, IPython.core.hooks.__all__ )) | |
906 |
|
906 | |||
907 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
907 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): | |
908 | alternative = IPython.core.hooks.deprecated[name] |
|
908 | alternative = IPython.core.hooks.deprecated[name] | |
909 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative), stacklevel=2) |
|
909 | raise ValueError( | |
|
910 | "Hook {} has been deprecated since IPython 5.0. Use {} instead.".format( | |||
|
911 | name, alternative | |||
|
912 | ) | |||
|
913 | ) | |||
910 |
|
914 | |||
911 | if not dp: |
|
915 | if not dp: | |
912 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
916 | dp = IPython.core.hooks.CommandChainDispatcher() | |
913 |
|
917 | |||
914 | try: |
|
918 | try: | |
915 | dp.add(f,priority) |
|
919 | dp.add(f,priority) | |
916 | except AttributeError: |
|
920 | except AttributeError: | |
917 | # it was not commandchain, plain old func - replace |
|
921 | # it was not commandchain, plain old func - replace | |
918 | dp = f |
|
922 | dp = f | |
919 |
|
923 | |||
920 | setattr(self.hooks,name, dp) |
|
924 | setattr(self.hooks,name, dp) | |
921 |
|
925 | |||
922 | #------------------------------------------------------------------------- |
|
926 | #------------------------------------------------------------------------- | |
923 | # Things related to events |
|
927 | # Things related to events | |
924 | #------------------------------------------------------------------------- |
|
928 | #------------------------------------------------------------------------- | |
925 |
|
929 | |||
926 | def init_events(self): |
|
930 | def init_events(self): | |
927 | self.events = EventManager(self, available_events) |
|
931 | self.events = EventManager(self, available_events) | |
928 |
|
932 | |||
929 | self.events.register("pre_execute", self._clear_warning_registry) |
|
933 | self.events.register("pre_execute", self._clear_warning_registry) | |
930 |
|
934 | |||
931 | def register_post_execute(self, func): |
|
935 | def register_post_execute(self, func): | |
932 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
936 | """DEPRECATED: Use ip.events.register('post_run_cell', func) | |
933 |
|
937 | |||
934 | Register a function for calling after code execution. |
|
938 | Register a function for calling after code execution. | |
935 | """ |
|
939 | """ | |
936 | warn("ip.register_post_execute is deprecated, use " |
|
940 | raise ValueError( | |
937 | "ip.events.register('post_run_cell', func) instead.", stacklevel=2) |
|
941 | "ip.register_post_execute is deprecated since IPython 1.0, use " | |
938 |
|
|
942 | "ip.events.register('post_run_cell', func) instead." | |
|
943 | ) | |||
939 |
|
944 | |||
940 | def _clear_warning_registry(self): |
|
945 | def _clear_warning_registry(self): | |
941 | # clear the warning registry, so that different code blocks with |
|
946 | # clear the warning registry, so that different code blocks with | |
942 | # overlapping line number ranges don't cause spurious suppression of |
|
947 | # overlapping line number ranges don't cause spurious suppression of | |
943 | # warnings (see gh-6611 for details) |
|
948 | # warnings (see gh-6611 for details) | |
944 | if "__warningregistry__" in self.user_global_ns: |
|
949 | if "__warningregistry__" in self.user_global_ns: | |
945 | del self.user_global_ns["__warningregistry__"] |
|
950 | del self.user_global_ns["__warningregistry__"] | |
946 |
|
951 | |||
947 | #------------------------------------------------------------------------- |
|
952 | #------------------------------------------------------------------------- | |
948 | # Things related to the "main" module |
|
953 | # Things related to the "main" module | |
949 | #------------------------------------------------------------------------- |
|
954 | #------------------------------------------------------------------------- | |
950 |
|
955 | |||
951 | def new_main_mod(self, filename, modname): |
|
956 | def new_main_mod(self, filename, modname): | |
952 | """Return a new 'main' module object for user code execution. |
|
957 | """Return a new 'main' module object for user code execution. | |
953 |
|
958 | |||
954 | ``filename`` should be the path of the script which will be run in the |
|
959 | ``filename`` should be the path of the script which will be run in the | |
955 | module. Requests with the same filename will get the same module, with |
|
960 | module. Requests with the same filename will get the same module, with | |
956 | its namespace cleared. |
|
961 | its namespace cleared. | |
957 |
|
962 | |||
958 | ``modname`` should be the module name - normally either '__main__' or |
|
963 | ``modname`` should be the module name - normally either '__main__' or | |
959 | the basename of the file without the extension. |
|
964 | the basename of the file without the extension. | |
960 |
|
965 | |||
961 | When scripts are executed via %run, we must keep a reference to their |
|
966 | When scripts are executed via %run, we must keep a reference to their | |
962 | __main__ module around so that Python doesn't |
|
967 | __main__ module around so that Python doesn't | |
963 | clear it, rendering references to module globals useless. |
|
968 | clear it, rendering references to module globals useless. | |
964 |
|
969 | |||
965 | This method keeps said reference in a private dict, keyed by the |
|
970 | This method keeps said reference in a private dict, keyed by the | |
966 | absolute path of the script. This way, for multiple executions of the |
|
971 | absolute path of the script. This way, for multiple executions of the | |
967 | same script we only keep one copy of the namespace (the last one), |
|
972 | same script we only keep one copy of the namespace (the last one), | |
968 | thus preventing memory leaks from old references while allowing the |
|
973 | thus preventing memory leaks from old references while allowing the | |
969 | objects from the last execution to be accessible. |
|
974 | objects from the last execution to be accessible. | |
970 | """ |
|
975 | """ | |
971 | filename = os.path.abspath(filename) |
|
976 | filename = os.path.abspath(filename) | |
972 | try: |
|
977 | try: | |
973 | main_mod = self._main_mod_cache[filename] |
|
978 | main_mod = self._main_mod_cache[filename] | |
974 | except KeyError: |
|
979 | except KeyError: | |
975 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
980 | main_mod = self._main_mod_cache[filename] = types.ModuleType( | |
976 | modname, |
|
981 | modname, | |
977 | doc="Module created for script run in IPython") |
|
982 | doc="Module created for script run in IPython") | |
978 | else: |
|
983 | else: | |
979 | main_mod.__dict__.clear() |
|
984 | main_mod.__dict__.clear() | |
980 | main_mod.__name__ = modname |
|
985 | main_mod.__name__ = modname | |
981 |
|
986 | |||
982 | main_mod.__file__ = filename |
|
987 | main_mod.__file__ = filename | |
983 | # It seems pydoc (and perhaps others) needs any module instance to |
|
988 | # It seems pydoc (and perhaps others) needs any module instance to | |
984 | # implement a __nonzero__ method |
|
989 | # implement a __nonzero__ method | |
985 | main_mod.__nonzero__ = lambda : True |
|
990 | main_mod.__nonzero__ = lambda : True | |
986 |
|
991 | |||
987 | return main_mod |
|
992 | return main_mod | |
988 |
|
993 | |||
989 | def clear_main_mod_cache(self): |
|
994 | def clear_main_mod_cache(self): | |
990 | """Clear the cache of main modules. |
|
995 | """Clear the cache of main modules. | |
991 |
|
996 | |||
992 | Mainly for use by utilities like %reset. |
|
997 | Mainly for use by utilities like %reset. | |
993 |
|
998 | |||
994 | Examples |
|
999 | Examples | |
995 | -------- |
|
1000 | -------- | |
996 | In [15]: import IPython |
|
1001 | In [15]: import IPython | |
997 |
|
1002 | |||
998 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
1003 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
999 |
|
1004 | |||
1000 | In [17]: len(_ip._main_mod_cache) > 0 |
|
1005 | In [17]: len(_ip._main_mod_cache) > 0 | |
1001 | Out[17]: True |
|
1006 | Out[17]: True | |
1002 |
|
1007 | |||
1003 | In [18]: _ip.clear_main_mod_cache() |
|
1008 | In [18]: _ip.clear_main_mod_cache() | |
1004 |
|
1009 | |||
1005 | In [19]: len(_ip._main_mod_cache) == 0 |
|
1010 | In [19]: len(_ip._main_mod_cache) == 0 | |
1006 | Out[19]: True |
|
1011 | Out[19]: True | |
1007 | """ |
|
1012 | """ | |
1008 | self._main_mod_cache.clear() |
|
1013 | self._main_mod_cache.clear() | |
1009 |
|
1014 | |||
1010 | #------------------------------------------------------------------------- |
|
1015 | #------------------------------------------------------------------------- | |
1011 | # Things related to debugging |
|
1016 | # Things related to debugging | |
1012 | #------------------------------------------------------------------------- |
|
1017 | #------------------------------------------------------------------------- | |
1013 |
|
1018 | |||
1014 | def init_pdb(self): |
|
1019 | def init_pdb(self): | |
1015 | # Set calling of pdb on exceptions |
|
1020 | # Set calling of pdb on exceptions | |
1016 | # self.call_pdb is a property |
|
1021 | # self.call_pdb is a property | |
1017 | self.call_pdb = self.pdb |
|
1022 | self.call_pdb = self.pdb | |
1018 |
|
1023 | |||
1019 | def _get_call_pdb(self): |
|
1024 | def _get_call_pdb(self): | |
1020 | return self._call_pdb |
|
1025 | return self._call_pdb | |
1021 |
|
1026 | |||
1022 | def _set_call_pdb(self,val): |
|
1027 | def _set_call_pdb(self,val): | |
1023 |
|
1028 | |||
1024 | if val not in (0,1,False,True): |
|
1029 | if val not in (0,1,False,True): | |
1025 | raise ValueError('new call_pdb value must be boolean') |
|
1030 | raise ValueError('new call_pdb value must be boolean') | |
1026 |
|
1031 | |||
1027 | # store value in instance |
|
1032 | # store value in instance | |
1028 | self._call_pdb = val |
|
1033 | self._call_pdb = val | |
1029 |
|
1034 | |||
1030 | # notify the actual exception handlers |
|
1035 | # notify the actual exception handlers | |
1031 | self.InteractiveTB.call_pdb = val |
|
1036 | self.InteractiveTB.call_pdb = val | |
1032 |
|
1037 | |||
1033 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
1038 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
1034 | 'Control auto-activation of pdb at exceptions') |
|
1039 | 'Control auto-activation of pdb at exceptions') | |
1035 |
|
1040 | |||
1036 | def debugger(self,force=False): |
|
1041 | def debugger(self,force=False): | |
1037 | """Call the pdb debugger. |
|
1042 | """Call the pdb debugger. | |
1038 |
|
1043 | |||
1039 | Keywords: |
|
1044 | Keywords: | |
1040 |
|
1045 | |||
1041 | - force(False): by default, this routine checks the instance call_pdb |
|
1046 | - force(False): by default, this routine checks the instance call_pdb | |
1042 | flag and does not actually invoke the debugger if the flag is false. |
|
1047 | flag and does not actually invoke the debugger if the flag is false. | |
1043 | The 'force' option forces the debugger to activate even if the flag |
|
1048 | The 'force' option forces the debugger to activate even if the flag | |
1044 | is false. |
|
1049 | is false. | |
1045 | """ |
|
1050 | """ | |
1046 |
|
1051 | |||
1047 | if not (force or self.call_pdb): |
|
1052 | if not (force or self.call_pdb): | |
1048 | return |
|
1053 | return | |
1049 |
|
1054 | |||
1050 | if not hasattr(sys,'last_traceback'): |
|
1055 | if not hasattr(sys,'last_traceback'): | |
1051 | error('No traceback has been produced, nothing to debug.') |
|
1056 | error('No traceback has been produced, nothing to debug.') | |
1052 | return |
|
1057 | return | |
1053 |
|
1058 | |||
1054 | self.InteractiveTB.debugger(force=True) |
|
1059 | self.InteractiveTB.debugger(force=True) | |
1055 |
|
1060 | |||
1056 | #------------------------------------------------------------------------- |
|
1061 | #------------------------------------------------------------------------- | |
1057 | # Things related to IPython's various namespaces |
|
1062 | # Things related to IPython's various namespaces | |
1058 | #------------------------------------------------------------------------- |
|
1063 | #------------------------------------------------------------------------- | |
1059 | default_user_namespaces = True |
|
1064 | default_user_namespaces = True | |
1060 |
|
1065 | |||
1061 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
1066 | def init_create_namespaces(self, user_module=None, user_ns=None): | |
1062 | # Create the namespace where the user will operate. user_ns is |
|
1067 | # Create the namespace where the user will operate. user_ns is | |
1063 | # normally the only one used, and it is passed to the exec calls as |
|
1068 | # normally the only one used, and it is passed to the exec calls as | |
1064 | # the locals argument. But we do carry a user_global_ns namespace |
|
1069 | # the locals argument. But we do carry a user_global_ns namespace | |
1065 | # given as the exec 'globals' argument, This is useful in embedding |
|
1070 | # given as the exec 'globals' argument, This is useful in embedding | |
1066 | # situations where the ipython shell opens in a context where the |
|
1071 | # situations where the ipython shell opens in a context where the | |
1067 | # distinction between locals and globals is meaningful. For |
|
1072 | # distinction between locals and globals is meaningful. For | |
1068 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1073 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
1069 |
|
1074 | |||
1070 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1075 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
1071 | # level as a dict instead of a module. This is a manual fix, but I |
|
1076 | # level as a dict instead of a module. This is a manual fix, but I | |
1072 | # should really track down where the problem is coming from. Alex |
|
1077 | # should really track down where the problem is coming from. Alex | |
1073 | # Schmolck reported this problem first. |
|
1078 | # Schmolck reported this problem first. | |
1074 |
|
1079 | |||
1075 | # A useful post by Alex Martelli on this topic: |
|
1080 | # A useful post by Alex Martelli on this topic: | |
1076 | # Re: inconsistent value from __builtins__ |
|
1081 | # Re: inconsistent value from __builtins__ | |
1077 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1082 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
1078 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1083 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
1079 | # Gruppen: comp.lang.python |
|
1084 | # Gruppen: comp.lang.python | |
1080 |
|
1085 | |||
1081 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1086 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
1082 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1087 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
1083 | # > <type 'dict'> |
|
1088 | # > <type 'dict'> | |
1084 | # > >>> print type(__builtins__) |
|
1089 | # > >>> print type(__builtins__) | |
1085 | # > <type 'module'> |
|
1090 | # > <type 'module'> | |
1086 | # > Is this difference in return value intentional? |
|
1091 | # > Is this difference in return value intentional? | |
1087 |
|
1092 | |||
1088 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1093 | # Well, it's documented that '__builtins__' can be either a dictionary | |
1089 | # or a module, and it's been that way for a long time. Whether it's |
|
1094 | # or a module, and it's been that way for a long time. Whether it's | |
1090 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1095 | # intentional (or sensible), I don't know. In any case, the idea is | |
1091 | # that if you need to access the built-in namespace directly, you |
|
1096 | # that if you need to access the built-in namespace directly, you | |
1092 | # should start with "import __builtin__" (note, no 's') which will |
|
1097 | # should start with "import __builtin__" (note, no 's') which will | |
1093 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1098 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
1094 |
|
1099 | |||
1095 | # These routines return a properly built module and dict as needed by |
|
1100 | # These routines return a properly built module and dict as needed by | |
1096 | # the rest of the code, and can also be used by extension writers to |
|
1101 | # the rest of the code, and can also be used by extension writers to | |
1097 | # generate properly initialized namespaces. |
|
1102 | # generate properly initialized namespaces. | |
1098 | if (user_ns is not None) or (user_module is not None): |
|
1103 | if (user_ns is not None) or (user_module is not None): | |
1099 | self.default_user_namespaces = False |
|
1104 | self.default_user_namespaces = False | |
1100 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1105 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) | |
1101 |
|
1106 | |||
1102 | # A record of hidden variables we have added to the user namespace, so |
|
1107 | # A record of hidden variables we have added to the user namespace, so | |
1103 | # we can list later only variables defined in actual interactive use. |
|
1108 | # we can list later only variables defined in actual interactive use. | |
1104 | self.user_ns_hidden = {} |
|
1109 | self.user_ns_hidden = {} | |
1105 |
|
1110 | |||
1106 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1111 | # Now that FakeModule produces a real module, we've run into a nasty | |
1107 | # problem: after script execution (via %run), the module where the user |
|
1112 | # problem: after script execution (via %run), the module where the user | |
1108 | # code ran is deleted. Now that this object is a true module (needed |
|
1113 | # code ran is deleted. Now that this object is a true module (needed | |
1109 | # so doctest and other tools work correctly), the Python module |
|
1114 | # so doctest and other tools work correctly), the Python module | |
1110 | # teardown mechanism runs over it, and sets to None every variable |
|
1115 | # teardown mechanism runs over it, and sets to None every variable | |
1111 | # present in that module. Top-level references to objects from the |
|
1116 | # present in that module. Top-level references to objects from the | |
1112 | # script survive, because the user_ns is updated with them. However, |
|
1117 | # script survive, because the user_ns is updated with them. However, | |
1113 | # calling functions defined in the script that use other things from |
|
1118 | # calling functions defined in the script that use other things from | |
1114 | # the script will fail, because the function's closure had references |
|
1119 | # the script will fail, because the function's closure had references | |
1115 | # to the original objects, which are now all None. So we must protect |
|
1120 | # to the original objects, which are now all None. So we must protect | |
1116 | # these modules from deletion by keeping a cache. |
|
1121 | # these modules from deletion by keeping a cache. | |
1117 | # |
|
1122 | # | |
1118 | # To avoid keeping stale modules around (we only need the one from the |
|
1123 | # To avoid keeping stale modules around (we only need the one from the | |
1119 | # last run), we use a dict keyed with the full path to the script, so |
|
1124 | # last run), we use a dict keyed with the full path to the script, so | |
1120 | # only the last version of the module is held in the cache. Note, |
|
1125 | # only the last version of the module is held in the cache. Note, | |
1121 | # however, that we must cache the module *namespace contents* (their |
|
1126 | # however, that we must cache the module *namespace contents* (their | |
1122 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1127 | # __dict__). Because if we try to cache the actual modules, old ones | |
1123 | # (uncached) could be destroyed while still holding references (such as |
|
1128 | # (uncached) could be destroyed while still holding references (such as | |
1124 | # those held by GUI objects that tend to be long-lived)> |
|
1129 | # those held by GUI objects that tend to be long-lived)> | |
1125 | # |
|
1130 | # | |
1126 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1131 | # The %reset command will flush this cache. See the cache_main_mod() | |
1127 | # and clear_main_mod_cache() methods for details on use. |
|
1132 | # and clear_main_mod_cache() methods for details on use. | |
1128 |
|
1133 | |||
1129 | # This is the cache used for 'main' namespaces |
|
1134 | # This is the cache used for 'main' namespaces | |
1130 | self._main_mod_cache = {} |
|
1135 | self._main_mod_cache = {} | |
1131 |
|
1136 | |||
1132 | # A table holding all the namespaces IPython deals with, so that |
|
1137 | # A table holding all the namespaces IPython deals with, so that | |
1133 | # introspection facilities can search easily. |
|
1138 | # introspection facilities can search easily. | |
1134 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1139 | self.ns_table = {'user_global':self.user_module.__dict__, | |
1135 | 'user_local':self.user_ns, |
|
1140 | 'user_local':self.user_ns, | |
1136 | 'builtin':builtin_mod.__dict__ |
|
1141 | 'builtin':builtin_mod.__dict__ | |
1137 | } |
|
1142 | } | |
1138 |
|
1143 | |||
1139 | @property |
|
1144 | @property | |
1140 | def user_global_ns(self): |
|
1145 | def user_global_ns(self): | |
1141 | return self.user_module.__dict__ |
|
1146 | return self.user_module.__dict__ | |
1142 |
|
1147 | |||
1143 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1148 | def prepare_user_module(self, user_module=None, user_ns=None): | |
1144 | """Prepare the module and namespace in which user code will be run. |
|
1149 | """Prepare the module and namespace in which user code will be run. | |
1145 |
|
1150 | |||
1146 | When IPython is started normally, both parameters are None: a new module |
|
1151 | When IPython is started normally, both parameters are None: a new module | |
1147 | is created automatically, and its __dict__ used as the namespace. |
|
1152 | is created automatically, and its __dict__ used as the namespace. | |
1148 |
|
1153 | |||
1149 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1154 | If only user_module is provided, its __dict__ is used as the namespace. | |
1150 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1155 | If only user_ns is provided, a dummy module is created, and user_ns | |
1151 | becomes the global namespace. If both are provided (as they may be |
|
1156 | becomes the global namespace. If both are provided (as they may be | |
1152 | when embedding), user_ns is the local namespace, and user_module |
|
1157 | when embedding), user_ns is the local namespace, and user_module | |
1153 | provides the global namespace. |
|
1158 | provides the global namespace. | |
1154 |
|
1159 | |||
1155 | Parameters |
|
1160 | Parameters | |
1156 | ---------- |
|
1161 | ---------- | |
1157 | user_module : module, optional |
|
1162 | user_module : module, optional | |
1158 | The current user module in which IPython is being run. If None, |
|
1163 | The current user module in which IPython is being run. If None, | |
1159 | a clean module will be created. |
|
1164 | a clean module will be created. | |
1160 | user_ns : dict, optional |
|
1165 | user_ns : dict, optional | |
1161 | A namespace in which to run interactive commands. |
|
1166 | A namespace in which to run interactive commands. | |
1162 |
|
1167 | |||
1163 | Returns |
|
1168 | Returns | |
1164 | ------- |
|
1169 | ------- | |
1165 | A tuple of user_module and user_ns, each properly initialised. |
|
1170 | A tuple of user_module and user_ns, each properly initialised. | |
1166 | """ |
|
1171 | """ | |
1167 | if user_module is None and user_ns is not None: |
|
1172 | if user_module is None and user_ns is not None: | |
1168 | user_ns.setdefault("__name__", "__main__") |
|
1173 | user_ns.setdefault("__name__", "__main__") | |
1169 | user_module = DummyMod() |
|
1174 | user_module = DummyMod() | |
1170 | user_module.__dict__ = user_ns |
|
1175 | user_module.__dict__ = user_ns | |
1171 |
|
1176 | |||
1172 | if user_module is None: |
|
1177 | if user_module is None: | |
1173 | user_module = types.ModuleType("__main__", |
|
1178 | user_module = types.ModuleType("__main__", | |
1174 | doc="Automatically created module for IPython interactive environment") |
|
1179 | doc="Automatically created module for IPython interactive environment") | |
1175 |
|
1180 | |||
1176 | # We must ensure that __builtin__ (without the final 's') is always |
|
1181 | # We must ensure that __builtin__ (without the final 's') is always | |
1177 | # available and pointing to the __builtin__ *module*. For more details: |
|
1182 | # available and pointing to the __builtin__ *module*. For more details: | |
1178 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1183 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1179 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1184 | user_module.__dict__.setdefault('__builtin__', builtin_mod) | |
1180 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1185 | user_module.__dict__.setdefault('__builtins__', builtin_mod) | |
1181 |
|
1186 | |||
1182 | if user_ns is None: |
|
1187 | if user_ns is None: | |
1183 | user_ns = user_module.__dict__ |
|
1188 | user_ns = user_module.__dict__ | |
1184 |
|
1189 | |||
1185 | return user_module, user_ns |
|
1190 | return user_module, user_ns | |
1186 |
|
1191 | |||
1187 | def init_sys_modules(self): |
|
1192 | def init_sys_modules(self): | |
1188 | # We need to insert into sys.modules something that looks like a |
|
1193 | # We need to insert into sys.modules something that looks like a | |
1189 | # module but which accesses the IPython namespace, for shelve and |
|
1194 | # module but which accesses the IPython namespace, for shelve and | |
1190 | # pickle to work interactively. Normally they rely on getting |
|
1195 | # pickle to work interactively. Normally they rely on getting | |
1191 | # everything out of __main__, but for embedding purposes each IPython |
|
1196 | # everything out of __main__, but for embedding purposes each IPython | |
1192 | # instance has its own private namespace, so we can't go shoving |
|
1197 | # instance has its own private namespace, so we can't go shoving | |
1193 | # everything into __main__. |
|
1198 | # everything into __main__. | |
1194 |
|
1199 | |||
1195 | # note, however, that we should only do this for non-embedded |
|
1200 | # note, however, that we should only do this for non-embedded | |
1196 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1201 | # ipythons, which really mimic the __main__.__dict__ with their own | |
1197 | # namespace. Embedded instances, on the other hand, should not do |
|
1202 | # namespace. Embedded instances, on the other hand, should not do | |
1198 | # this because they need to manage the user local/global namespaces |
|
1203 | # this because they need to manage the user local/global namespaces | |
1199 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1204 | # only, but they live within a 'normal' __main__ (meaning, they | |
1200 | # shouldn't overtake the execution environment of the script they're |
|
1205 | # shouldn't overtake the execution environment of the script they're | |
1201 | # embedded in). |
|
1206 | # embedded in). | |
1202 |
|
1207 | |||
1203 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1208 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
1204 | main_name = self.user_module.__name__ |
|
1209 | main_name = self.user_module.__name__ | |
1205 | sys.modules[main_name] = self.user_module |
|
1210 | sys.modules[main_name] = self.user_module | |
1206 |
|
1211 | |||
1207 | def init_user_ns(self): |
|
1212 | def init_user_ns(self): | |
1208 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1213 | """Initialize all user-visible namespaces to their minimum defaults. | |
1209 |
|
1214 | |||
1210 | Certain history lists are also initialized here, as they effectively |
|
1215 | Certain history lists are also initialized here, as they effectively | |
1211 | act as user namespaces. |
|
1216 | act as user namespaces. | |
1212 |
|
1217 | |||
1213 | Notes |
|
1218 | Notes | |
1214 | ----- |
|
1219 | ----- | |
1215 | All data structures here are only filled in, they are NOT reset by this |
|
1220 | All data structures here are only filled in, they are NOT reset by this | |
1216 | method. If they were not empty before, data will simply be added to |
|
1221 | method. If they were not empty before, data will simply be added to | |
1217 | them. |
|
1222 | them. | |
1218 | """ |
|
1223 | """ | |
1219 | # This function works in two parts: first we put a few things in |
|
1224 | # This function works in two parts: first we put a few things in | |
1220 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1225 | # user_ns, and we sync that contents into user_ns_hidden so that these | |
1221 | # initial variables aren't shown by %who. After the sync, we add the |
|
1226 | # initial variables aren't shown by %who. After the sync, we add the | |
1222 | # rest of what we *do* want the user to see with %who even on a new |
|
1227 | # rest of what we *do* want the user to see with %who even on a new | |
1223 | # session (probably nothing, so they really only see their own stuff) |
|
1228 | # session (probably nothing, so they really only see their own stuff) | |
1224 |
|
1229 | |||
1225 | # The user dict must *always* have a __builtin__ reference to the |
|
1230 | # The user dict must *always* have a __builtin__ reference to the | |
1226 | # Python standard __builtin__ namespace, which must be imported. |
|
1231 | # Python standard __builtin__ namespace, which must be imported. | |
1227 | # This is so that certain operations in prompt evaluation can be |
|
1232 | # This is so that certain operations in prompt evaluation can be | |
1228 | # reliably executed with builtins. Note that we can NOT use |
|
1233 | # reliably executed with builtins. Note that we can NOT use | |
1229 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1234 | # __builtins__ (note the 's'), because that can either be a dict or a | |
1230 | # module, and can even mutate at runtime, depending on the context |
|
1235 | # module, and can even mutate at runtime, depending on the context | |
1231 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1236 | # (Python makes no guarantees on it). In contrast, __builtin__ is | |
1232 | # always a module object, though it must be explicitly imported. |
|
1237 | # always a module object, though it must be explicitly imported. | |
1233 |
|
1238 | |||
1234 | # For more details: |
|
1239 | # For more details: | |
1235 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1240 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html | |
1236 | ns = {} |
|
1241 | ns = {} | |
1237 |
|
1242 | |||
1238 | # make global variables for user access to the histories |
|
1243 | # make global variables for user access to the histories | |
1239 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1244 | ns['_ih'] = self.history_manager.input_hist_parsed | |
1240 | ns['_oh'] = self.history_manager.output_hist |
|
1245 | ns['_oh'] = self.history_manager.output_hist | |
1241 | ns['_dh'] = self.history_manager.dir_hist |
|
1246 | ns['_dh'] = self.history_manager.dir_hist | |
1242 |
|
1247 | |||
1243 | # user aliases to input and output histories. These shouldn't show up |
|
1248 | # user aliases to input and output histories. These shouldn't show up | |
1244 | # in %who, as they can have very large reprs. |
|
1249 | # in %who, as they can have very large reprs. | |
1245 | ns['In'] = self.history_manager.input_hist_parsed |
|
1250 | ns['In'] = self.history_manager.input_hist_parsed | |
1246 | ns['Out'] = self.history_manager.output_hist |
|
1251 | ns['Out'] = self.history_manager.output_hist | |
1247 |
|
1252 | |||
1248 | # Store myself as the public api!!! |
|
1253 | # Store myself as the public api!!! | |
1249 | ns['get_ipython'] = self.get_ipython |
|
1254 | ns['get_ipython'] = self.get_ipython | |
1250 |
|
1255 | |||
1251 | ns['exit'] = self.exiter |
|
1256 | ns['exit'] = self.exiter | |
1252 | ns['quit'] = self.exiter |
|
1257 | ns['quit'] = self.exiter | |
1253 |
|
1258 | |||
1254 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1259 | # Sync what we've added so far to user_ns_hidden so these aren't seen | |
1255 | # by %who |
|
1260 | # by %who | |
1256 | self.user_ns_hidden.update(ns) |
|
1261 | self.user_ns_hidden.update(ns) | |
1257 |
|
1262 | |||
1258 | # Anything put into ns now would show up in %who. Think twice before |
|
1263 | # Anything put into ns now would show up in %who. Think twice before | |
1259 | # putting anything here, as we really want %who to show the user their |
|
1264 | # putting anything here, as we really want %who to show the user their | |
1260 | # stuff, not our variables. |
|
1265 | # stuff, not our variables. | |
1261 |
|
1266 | |||
1262 | # Finally, update the real user's namespace |
|
1267 | # Finally, update the real user's namespace | |
1263 | self.user_ns.update(ns) |
|
1268 | self.user_ns.update(ns) | |
1264 |
|
1269 | |||
1265 | @property |
|
1270 | @property | |
1266 | def all_ns_refs(self): |
|
1271 | def all_ns_refs(self): | |
1267 | """Get a list of references to all the namespace dictionaries in which |
|
1272 | """Get a list of references to all the namespace dictionaries in which | |
1268 | IPython might store a user-created object. |
|
1273 | IPython might store a user-created object. | |
1269 |
|
1274 | |||
1270 | Note that this does not include the displayhook, which also caches |
|
1275 | Note that this does not include the displayhook, which also caches | |
1271 | objects from the output.""" |
|
1276 | objects from the output.""" | |
1272 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1277 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ | |
1273 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1278 | [m.__dict__ for m in self._main_mod_cache.values()] | |
1274 |
|
1279 | |||
1275 | def reset(self, new_session=True, aggressive=False): |
|
1280 | def reset(self, new_session=True, aggressive=False): | |
1276 | """Clear all internal namespaces, and attempt to release references to |
|
1281 | """Clear all internal namespaces, and attempt to release references to | |
1277 | user objects. |
|
1282 | user objects. | |
1278 |
|
1283 | |||
1279 | If new_session is True, a new history session will be opened. |
|
1284 | If new_session is True, a new history session will be opened. | |
1280 | """ |
|
1285 | """ | |
1281 | # Clear histories |
|
1286 | # Clear histories | |
1282 | self.history_manager.reset(new_session) |
|
1287 | self.history_manager.reset(new_session) | |
1283 | # Reset counter used to index all histories |
|
1288 | # Reset counter used to index all histories | |
1284 | if new_session: |
|
1289 | if new_session: | |
1285 | self.execution_count = 1 |
|
1290 | self.execution_count = 1 | |
1286 |
|
1291 | |||
1287 | # Reset last execution result |
|
1292 | # Reset last execution result | |
1288 | self.last_execution_succeeded = True |
|
1293 | self.last_execution_succeeded = True | |
1289 | self.last_execution_result = None |
|
1294 | self.last_execution_result = None | |
1290 |
|
1295 | |||
1291 | # Flush cached output items |
|
1296 | # Flush cached output items | |
1292 | if self.displayhook.do_full_cache: |
|
1297 | if self.displayhook.do_full_cache: | |
1293 | self.displayhook.flush() |
|
1298 | self.displayhook.flush() | |
1294 |
|
1299 | |||
1295 | # The main execution namespaces must be cleared very carefully, |
|
1300 | # The main execution namespaces must be cleared very carefully, | |
1296 | # skipping the deletion of the builtin-related keys, because doing so |
|
1301 | # skipping the deletion of the builtin-related keys, because doing so | |
1297 | # would cause errors in many object's __del__ methods. |
|
1302 | # would cause errors in many object's __del__ methods. | |
1298 | if self.user_ns is not self.user_global_ns: |
|
1303 | if self.user_ns is not self.user_global_ns: | |
1299 | self.user_ns.clear() |
|
1304 | self.user_ns.clear() | |
1300 | ns = self.user_global_ns |
|
1305 | ns = self.user_global_ns | |
1301 | drop_keys = set(ns.keys()) |
|
1306 | drop_keys = set(ns.keys()) | |
1302 | drop_keys.discard('__builtin__') |
|
1307 | drop_keys.discard('__builtin__') | |
1303 | drop_keys.discard('__builtins__') |
|
1308 | drop_keys.discard('__builtins__') | |
1304 | drop_keys.discard('__name__') |
|
1309 | drop_keys.discard('__name__') | |
1305 | for k in drop_keys: |
|
1310 | for k in drop_keys: | |
1306 | del ns[k] |
|
1311 | del ns[k] | |
1307 |
|
1312 | |||
1308 | self.user_ns_hidden.clear() |
|
1313 | self.user_ns_hidden.clear() | |
1309 |
|
1314 | |||
1310 | # Restore the user namespaces to minimal usability |
|
1315 | # Restore the user namespaces to minimal usability | |
1311 | self.init_user_ns() |
|
1316 | self.init_user_ns() | |
1312 | if aggressive and not hasattr(self, "_sys_modules_keys"): |
|
1317 | if aggressive and not hasattr(self, "_sys_modules_keys"): | |
1313 | print("Cannot restore sys.module, no snapshot") |
|
1318 | print("Cannot restore sys.module, no snapshot") | |
1314 | elif aggressive: |
|
1319 | elif aggressive: | |
1315 | print("culling sys module...") |
|
1320 | print("culling sys module...") | |
1316 | current_keys = set(sys.modules.keys()) |
|
1321 | current_keys = set(sys.modules.keys()) | |
1317 | for k in current_keys - self._sys_modules_keys: |
|
1322 | for k in current_keys - self._sys_modules_keys: | |
1318 | if k.startswith("multiprocessing"): |
|
1323 | if k.startswith("multiprocessing"): | |
1319 | continue |
|
1324 | continue | |
1320 | del sys.modules[k] |
|
1325 | del sys.modules[k] | |
1321 |
|
1326 | |||
1322 | # Restore the default and user aliases |
|
1327 | # Restore the default and user aliases | |
1323 | self.alias_manager.clear_aliases() |
|
1328 | self.alias_manager.clear_aliases() | |
1324 | self.alias_manager.init_aliases() |
|
1329 | self.alias_manager.init_aliases() | |
1325 |
|
1330 | |||
1326 | # Now define aliases that only make sense on the terminal, because they |
|
1331 | # Now define aliases that only make sense on the terminal, because they | |
1327 | # need direct access to the console in a way that we can't emulate in |
|
1332 | # need direct access to the console in a way that we can't emulate in | |
1328 | # GUI or web frontend |
|
1333 | # GUI or web frontend | |
1329 | if os.name == 'posix': |
|
1334 | if os.name == 'posix': | |
1330 | for cmd in ('clear', 'more', 'less', 'man'): |
|
1335 | for cmd in ('clear', 'more', 'less', 'man'): | |
1331 | if cmd not in self.magics_manager.magics['line']: |
|
1336 | if cmd not in self.magics_manager.magics['line']: | |
1332 | self.alias_manager.soft_define_alias(cmd, cmd) |
|
1337 | self.alias_manager.soft_define_alias(cmd, cmd) | |
1333 |
|
1338 | |||
1334 | # Flush the private list of module references kept for script |
|
1339 | # Flush the private list of module references kept for script | |
1335 | # execution protection |
|
1340 | # execution protection | |
1336 | self.clear_main_mod_cache() |
|
1341 | self.clear_main_mod_cache() | |
1337 |
|
1342 | |||
1338 | def del_var(self, varname, by_name=False): |
|
1343 | def del_var(self, varname, by_name=False): | |
1339 | """Delete a variable from the various namespaces, so that, as |
|
1344 | """Delete a variable from the various namespaces, so that, as | |
1340 | far as possible, we're not keeping any hidden references to it. |
|
1345 | far as possible, we're not keeping any hidden references to it. | |
1341 |
|
1346 | |||
1342 | Parameters |
|
1347 | Parameters | |
1343 | ---------- |
|
1348 | ---------- | |
1344 | varname : str |
|
1349 | varname : str | |
1345 | The name of the variable to delete. |
|
1350 | The name of the variable to delete. | |
1346 | by_name : bool |
|
1351 | by_name : bool | |
1347 | If True, delete variables with the given name in each |
|
1352 | If True, delete variables with the given name in each | |
1348 | namespace. If False (default), find the variable in the user |
|
1353 | namespace. If False (default), find the variable in the user | |
1349 | namespace, and delete references to it. |
|
1354 | namespace, and delete references to it. | |
1350 | """ |
|
1355 | """ | |
1351 | if varname in ('__builtin__', '__builtins__'): |
|
1356 | if varname in ('__builtin__', '__builtins__'): | |
1352 | raise ValueError("Refusing to delete %s" % varname) |
|
1357 | raise ValueError("Refusing to delete %s" % varname) | |
1353 |
|
1358 | |||
1354 | ns_refs = self.all_ns_refs |
|
1359 | ns_refs = self.all_ns_refs | |
1355 |
|
1360 | |||
1356 | if by_name: # Delete by name |
|
1361 | if by_name: # Delete by name | |
1357 | for ns in ns_refs: |
|
1362 | for ns in ns_refs: | |
1358 | try: |
|
1363 | try: | |
1359 | del ns[varname] |
|
1364 | del ns[varname] | |
1360 | except KeyError: |
|
1365 | except KeyError: | |
1361 | pass |
|
1366 | pass | |
1362 | else: # Delete by object |
|
1367 | else: # Delete by object | |
1363 | try: |
|
1368 | try: | |
1364 | obj = self.user_ns[varname] |
|
1369 | obj = self.user_ns[varname] | |
1365 | except KeyError as e: |
|
1370 | except KeyError as e: | |
1366 | raise NameError("name '%s' is not defined" % varname) from e |
|
1371 | raise NameError("name '%s' is not defined" % varname) from e | |
1367 | # Also check in output history |
|
1372 | # Also check in output history | |
1368 | ns_refs.append(self.history_manager.output_hist) |
|
1373 | ns_refs.append(self.history_manager.output_hist) | |
1369 | for ns in ns_refs: |
|
1374 | for ns in ns_refs: | |
1370 | to_delete = [n for n, o in ns.items() if o is obj] |
|
1375 | to_delete = [n for n, o in ns.items() if o is obj] | |
1371 | for name in to_delete: |
|
1376 | for name in to_delete: | |
1372 | del ns[name] |
|
1377 | del ns[name] | |
1373 |
|
1378 | |||
1374 | # Ensure it is removed from the last execution result |
|
1379 | # Ensure it is removed from the last execution result | |
1375 | if self.last_execution_result.result is obj: |
|
1380 | if self.last_execution_result.result is obj: | |
1376 | self.last_execution_result = None |
|
1381 | self.last_execution_result = None | |
1377 |
|
1382 | |||
1378 | # displayhook keeps extra references, but not in a dictionary |
|
1383 | # displayhook keeps extra references, but not in a dictionary | |
1379 | for name in ('_', '__', '___'): |
|
1384 | for name in ('_', '__', '___'): | |
1380 | if getattr(self.displayhook, name) is obj: |
|
1385 | if getattr(self.displayhook, name) is obj: | |
1381 | setattr(self.displayhook, name, None) |
|
1386 | setattr(self.displayhook, name, None) | |
1382 |
|
1387 | |||
1383 | def reset_selective(self, regex=None): |
|
1388 | def reset_selective(self, regex=None): | |
1384 | """Clear selective variables from internal namespaces based on a |
|
1389 | """Clear selective variables from internal namespaces based on a | |
1385 | specified regular expression. |
|
1390 | specified regular expression. | |
1386 |
|
1391 | |||
1387 | Parameters |
|
1392 | Parameters | |
1388 | ---------- |
|
1393 | ---------- | |
1389 | regex : string or compiled pattern, optional |
|
1394 | regex : string or compiled pattern, optional | |
1390 | A regular expression pattern that will be used in searching |
|
1395 | A regular expression pattern that will be used in searching | |
1391 | variable names in the users namespaces. |
|
1396 | variable names in the users namespaces. | |
1392 | """ |
|
1397 | """ | |
1393 | if regex is not None: |
|
1398 | if regex is not None: | |
1394 | try: |
|
1399 | try: | |
1395 | m = re.compile(regex) |
|
1400 | m = re.compile(regex) | |
1396 | except TypeError as e: |
|
1401 | except TypeError as e: | |
1397 | raise TypeError('regex must be a string or compiled pattern') from e |
|
1402 | raise TypeError('regex must be a string or compiled pattern') from e | |
1398 | # Search for keys in each namespace that match the given regex |
|
1403 | # Search for keys in each namespace that match the given regex | |
1399 | # If a match is found, delete the key/value pair. |
|
1404 | # If a match is found, delete the key/value pair. | |
1400 | for ns in self.all_ns_refs: |
|
1405 | for ns in self.all_ns_refs: | |
1401 | for var in ns: |
|
1406 | for var in ns: | |
1402 | if m.search(var): |
|
1407 | if m.search(var): | |
1403 | del ns[var] |
|
1408 | del ns[var] | |
1404 |
|
1409 | |||
1405 | def push(self, variables, interactive=True): |
|
1410 | def push(self, variables, interactive=True): | |
1406 | """Inject a group of variables into the IPython user namespace. |
|
1411 | """Inject a group of variables into the IPython user namespace. | |
1407 |
|
1412 | |||
1408 | Parameters |
|
1413 | Parameters | |
1409 | ---------- |
|
1414 | ---------- | |
1410 | variables : dict, str or list/tuple of str |
|
1415 | variables : dict, str or list/tuple of str | |
1411 | The variables to inject into the user's namespace. If a dict, a |
|
1416 | The variables to inject into the user's namespace. If a dict, a | |
1412 | simple update is done. If a str, the string is assumed to have |
|
1417 | simple update is done. If a str, the string is assumed to have | |
1413 | variable names separated by spaces. A list/tuple of str can also |
|
1418 | variable names separated by spaces. A list/tuple of str can also | |
1414 | be used to give the variable names. If just the variable names are |
|
1419 | be used to give the variable names. If just the variable names are | |
1415 | give (list/tuple/str) then the variable values looked up in the |
|
1420 | give (list/tuple/str) then the variable values looked up in the | |
1416 | callers frame. |
|
1421 | callers frame. | |
1417 | interactive : bool |
|
1422 | interactive : bool | |
1418 | If True (default), the variables will be listed with the ``who`` |
|
1423 | If True (default), the variables will be listed with the ``who`` | |
1419 | magic. |
|
1424 | magic. | |
1420 | """ |
|
1425 | """ | |
1421 | vdict = None |
|
1426 | vdict = None | |
1422 |
|
1427 | |||
1423 | # We need a dict of name/value pairs to do namespace updates. |
|
1428 | # We need a dict of name/value pairs to do namespace updates. | |
1424 | if isinstance(variables, dict): |
|
1429 | if isinstance(variables, dict): | |
1425 | vdict = variables |
|
1430 | vdict = variables | |
1426 | elif isinstance(variables, (str, list, tuple)): |
|
1431 | elif isinstance(variables, (str, list, tuple)): | |
1427 | if isinstance(variables, str): |
|
1432 | if isinstance(variables, str): | |
1428 | vlist = variables.split() |
|
1433 | vlist = variables.split() | |
1429 | else: |
|
1434 | else: | |
1430 | vlist = variables |
|
1435 | vlist = variables | |
1431 | vdict = {} |
|
1436 | vdict = {} | |
1432 | cf = sys._getframe(1) |
|
1437 | cf = sys._getframe(1) | |
1433 | for name in vlist: |
|
1438 | for name in vlist: | |
1434 | try: |
|
1439 | try: | |
1435 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1440 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
1436 | except: |
|
1441 | except: | |
1437 | print('Could not get variable %s from %s' % |
|
1442 | print('Could not get variable %s from %s' % | |
1438 | (name,cf.f_code.co_name)) |
|
1443 | (name,cf.f_code.co_name)) | |
1439 | else: |
|
1444 | else: | |
1440 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1445 | raise ValueError('variables must be a dict/str/list/tuple') | |
1441 |
|
1446 | |||
1442 | # Propagate variables to user namespace |
|
1447 | # Propagate variables to user namespace | |
1443 | self.user_ns.update(vdict) |
|
1448 | self.user_ns.update(vdict) | |
1444 |
|
1449 | |||
1445 | # And configure interactive visibility |
|
1450 | # And configure interactive visibility | |
1446 | user_ns_hidden = self.user_ns_hidden |
|
1451 | user_ns_hidden = self.user_ns_hidden | |
1447 | if interactive: |
|
1452 | if interactive: | |
1448 | for name in vdict: |
|
1453 | for name in vdict: | |
1449 | user_ns_hidden.pop(name, None) |
|
1454 | user_ns_hidden.pop(name, None) | |
1450 | else: |
|
1455 | else: | |
1451 | user_ns_hidden.update(vdict) |
|
1456 | user_ns_hidden.update(vdict) | |
1452 |
|
1457 | |||
1453 | def drop_by_id(self, variables): |
|
1458 | def drop_by_id(self, variables): | |
1454 | """Remove a dict of variables from the user namespace, if they are the |
|
1459 | """Remove a dict of variables from the user namespace, if they are the | |
1455 | same as the values in the dictionary. |
|
1460 | same as the values in the dictionary. | |
1456 |
|
1461 | |||
1457 | This is intended for use by extensions: variables that they've added can |
|
1462 | This is intended for use by extensions: variables that they've added can | |
1458 | be taken back out if they are unloaded, without removing any that the |
|
1463 | be taken back out if they are unloaded, without removing any that the | |
1459 | user has overwritten. |
|
1464 | user has overwritten. | |
1460 |
|
1465 | |||
1461 | Parameters |
|
1466 | Parameters | |
1462 | ---------- |
|
1467 | ---------- | |
1463 | variables : dict |
|
1468 | variables : dict | |
1464 | A dictionary mapping object names (as strings) to the objects. |
|
1469 | A dictionary mapping object names (as strings) to the objects. | |
1465 | """ |
|
1470 | """ | |
1466 | for name, obj in variables.items(): |
|
1471 | for name, obj in variables.items(): | |
1467 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1472 | if name in self.user_ns and self.user_ns[name] is obj: | |
1468 | del self.user_ns[name] |
|
1473 | del self.user_ns[name] | |
1469 | self.user_ns_hidden.pop(name, None) |
|
1474 | self.user_ns_hidden.pop(name, None) | |
1470 |
|
1475 | |||
1471 | #------------------------------------------------------------------------- |
|
1476 | #------------------------------------------------------------------------- | |
1472 | # Things related to object introspection |
|
1477 | # Things related to object introspection | |
1473 | #------------------------------------------------------------------------- |
|
1478 | #------------------------------------------------------------------------- | |
1474 |
|
1479 | |||
1475 | def _ofind(self, oname, namespaces=None): |
|
1480 | def _ofind(self, oname, namespaces=None): | |
1476 | """Find an object in the available namespaces. |
|
1481 | """Find an object in the available namespaces. | |
1477 |
|
1482 | |||
1478 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1483 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic | |
1479 |
|
1484 | |||
1480 | Has special code to detect magic functions. |
|
1485 | Has special code to detect magic functions. | |
1481 | """ |
|
1486 | """ | |
1482 | oname = oname.strip() |
|
1487 | oname = oname.strip() | |
1483 | if not oname.startswith(ESC_MAGIC) and \ |
|
1488 | if not oname.startswith(ESC_MAGIC) and \ | |
1484 | not oname.startswith(ESC_MAGIC2) and \ |
|
1489 | not oname.startswith(ESC_MAGIC2) and \ | |
1485 | not all(a.isidentifier() for a in oname.split(".")): |
|
1490 | not all(a.isidentifier() for a in oname.split(".")): | |
1486 | return {'found': False} |
|
1491 | return {'found': False} | |
1487 |
|
1492 | |||
1488 | if namespaces is None: |
|
1493 | if namespaces is None: | |
1489 | # Namespaces to search in: |
|
1494 | # Namespaces to search in: | |
1490 | # Put them in a list. The order is important so that we |
|
1495 | # Put them in a list. The order is important so that we | |
1491 | # find things in the same order that Python finds them. |
|
1496 | # find things in the same order that Python finds them. | |
1492 | namespaces = [ ('Interactive', self.user_ns), |
|
1497 | namespaces = [ ('Interactive', self.user_ns), | |
1493 | ('Interactive (global)', self.user_global_ns), |
|
1498 | ('Interactive (global)', self.user_global_ns), | |
1494 | ('Python builtin', builtin_mod.__dict__), |
|
1499 | ('Python builtin', builtin_mod.__dict__), | |
1495 | ] |
|
1500 | ] | |
1496 |
|
1501 | |||
1497 | ismagic = False |
|
1502 | ismagic = False | |
1498 | isalias = False |
|
1503 | isalias = False | |
1499 | found = False |
|
1504 | found = False | |
1500 | ospace = None |
|
1505 | ospace = None | |
1501 | parent = None |
|
1506 | parent = None | |
1502 | obj = None |
|
1507 | obj = None | |
1503 |
|
1508 | |||
1504 |
|
1509 | |||
1505 | # Look for the given name by splitting it in parts. If the head is |
|
1510 | # Look for the given name by splitting it in parts. If the head is | |
1506 | # found, then we look for all the remaining parts as members, and only |
|
1511 | # found, then we look for all the remaining parts as members, and only | |
1507 | # declare success if we can find them all. |
|
1512 | # declare success if we can find them all. | |
1508 | oname_parts = oname.split('.') |
|
1513 | oname_parts = oname.split('.') | |
1509 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1514 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] | |
1510 | for nsname,ns in namespaces: |
|
1515 | for nsname,ns in namespaces: | |
1511 | try: |
|
1516 | try: | |
1512 | obj = ns[oname_head] |
|
1517 | obj = ns[oname_head] | |
1513 | except KeyError: |
|
1518 | except KeyError: | |
1514 | continue |
|
1519 | continue | |
1515 | else: |
|
1520 | else: | |
1516 | for idx, part in enumerate(oname_rest): |
|
1521 | for idx, part in enumerate(oname_rest): | |
1517 | try: |
|
1522 | try: | |
1518 | parent = obj |
|
1523 | parent = obj | |
1519 | # The last part is looked up in a special way to avoid |
|
1524 | # The last part is looked up in a special way to avoid | |
1520 | # descriptor invocation as it may raise or have side |
|
1525 | # descriptor invocation as it may raise or have side | |
1521 | # effects. |
|
1526 | # effects. | |
1522 | if idx == len(oname_rest) - 1: |
|
1527 | if idx == len(oname_rest) - 1: | |
1523 | obj = self._getattr_property(obj, part) |
|
1528 | obj = self._getattr_property(obj, part) | |
1524 | else: |
|
1529 | else: | |
1525 | obj = getattr(obj, part) |
|
1530 | obj = getattr(obj, part) | |
1526 | except: |
|
1531 | except: | |
1527 | # Blanket except b/c some badly implemented objects |
|
1532 | # Blanket except b/c some badly implemented objects | |
1528 | # allow __getattr__ to raise exceptions other than |
|
1533 | # allow __getattr__ to raise exceptions other than | |
1529 | # AttributeError, which then crashes IPython. |
|
1534 | # AttributeError, which then crashes IPython. | |
1530 | break |
|
1535 | break | |
1531 | else: |
|
1536 | else: | |
1532 | # If we finish the for loop (no break), we got all members |
|
1537 | # If we finish the for loop (no break), we got all members | |
1533 | found = True |
|
1538 | found = True | |
1534 | ospace = nsname |
|
1539 | ospace = nsname | |
1535 | break # namespace loop |
|
1540 | break # namespace loop | |
1536 |
|
1541 | |||
1537 | # Try to see if it's magic |
|
1542 | # Try to see if it's magic | |
1538 | if not found: |
|
1543 | if not found: | |
1539 | obj = None |
|
1544 | obj = None | |
1540 | if oname.startswith(ESC_MAGIC2): |
|
1545 | if oname.startswith(ESC_MAGIC2): | |
1541 | oname = oname.lstrip(ESC_MAGIC2) |
|
1546 | oname = oname.lstrip(ESC_MAGIC2) | |
1542 | obj = self.find_cell_magic(oname) |
|
1547 | obj = self.find_cell_magic(oname) | |
1543 | elif oname.startswith(ESC_MAGIC): |
|
1548 | elif oname.startswith(ESC_MAGIC): | |
1544 | oname = oname.lstrip(ESC_MAGIC) |
|
1549 | oname = oname.lstrip(ESC_MAGIC) | |
1545 | obj = self.find_line_magic(oname) |
|
1550 | obj = self.find_line_magic(oname) | |
1546 | else: |
|
1551 | else: | |
1547 | # search without prefix, so run? will find %run? |
|
1552 | # search without prefix, so run? will find %run? | |
1548 | obj = self.find_line_magic(oname) |
|
1553 | obj = self.find_line_magic(oname) | |
1549 | if obj is None: |
|
1554 | if obj is None: | |
1550 | obj = self.find_cell_magic(oname) |
|
1555 | obj = self.find_cell_magic(oname) | |
1551 | if obj is not None: |
|
1556 | if obj is not None: | |
1552 | found = True |
|
1557 | found = True | |
1553 | ospace = 'IPython internal' |
|
1558 | ospace = 'IPython internal' | |
1554 | ismagic = True |
|
1559 | ismagic = True | |
1555 | isalias = isinstance(obj, Alias) |
|
1560 | isalias = isinstance(obj, Alias) | |
1556 |
|
1561 | |||
1557 | # Last try: special-case some literals like '', [], {}, etc: |
|
1562 | # Last try: special-case some literals like '', [], {}, etc: | |
1558 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1563 | if not found and oname_head in ["''",'""','[]','{}','()']: | |
1559 | obj = eval(oname_head) |
|
1564 | obj = eval(oname_head) | |
1560 | found = True |
|
1565 | found = True | |
1561 | ospace = 'Interactive' |
|
1566 | ospace = 'Interactive' | |
1562 |
|
1567 | |||
1563 | return { |
|
1568 | return { | |
1564 | 'obj':obj, |
|
1569 | 'obj':obj, | |
1565 | 'found':found, |
|
1570 | 'found':found, | |
1566 | 'parent':parent, |
|
1571 | 'parent':parent, | |
1567 | 'ismagic':ismagic, |
|
1572 | 'ismagic':ismagic, | |
1568 | 'isalias':isalias, |
|
1573 | 'isalias':isalias, | |
1569 | 'namespace':ospace |
|
1574 | 'namespace':ospace | |
1570 | } |
|
1575 | } | |
1571 |
|
1576 | |||
1572 | @staticmethod |
|
1577 | @staticmethod | |
1573 | def _getattr_property(obj, attrname): |
|
1578 | def _getattr_property(obj, attrname): | |
1574 | """Property-aware getattr to use in object finding. |
|
1579 | """Property-aware getattr to use in object finding. | |
1575 |
|
1580 | |||
1576 | If attrname represents a property, return it unevaluated (in case it has |
|
1581 | If attrname represents a property, return it unevaluated (in case it has | |
1577 | side effects or raises an error. |
|
1582 | side effects or raises an error. | |
1578 |
|
1583 | |||
1579 | """ |
|
1584 | """ | |
1580 | if not isinstance(obj, type): |
|
1585 | if not isinstance(obj, type): | |
1581 | try: |
|
1586 | try: | |
1582 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1587 | # `getattr(type(obj), attrname)` is not guaranteed to return | |
1583 | # `obj`, but does so for property: |
|
1588 | # `obj`, but does so for property: | |
1584 | # |
|
1589 | # | |
1585 | # property.__get__(self, None, cls) -> self |
|
1590 | # property.__get__(self, None, cls) -> self | |
1586 | # |
|
1591 | # | |
1587 | # The universal alternative is to traverse the mro manually |
|
1592 | # The universal alternative is to traverse the mro manually | |
1588 | # searching for attrname in class dicts. |
|
1593 | # searching for attrname in class dicts. | |
1589 | attr = getattr(type(obj), attrname) |
|
1594 | attr = getattr(type(obj), attrname) | |
1590 | except AttributeError: |
|
1595 | except AttributeError: | |
1591 | pass |
|
1596 | pass | |
1592 | else: |
|
1597 | else: | |
1593 | # This relies on the fact that data descriptors (with both |
|
1598 | # This relies on the fact that data descriptors (with both | |
1594 | # __get__ & __set__ magic methods) take precedence over |
|
1599 | # __get__ & __set__ magic methods) take precedence over | |
1595 | # instance-level attributes: |
|
1600 | # instance-level attributes: | |
1596 | # |
|
1601 | # | |
1597 | # class A(object): |
|
1602 | # class A(object): | |
1598 | # @property |
|
1603 | # @property | |
1599 | # def foobar(self): return 123 |
|
1604 | # def foobar(self): return 123 | |
1600 | # a = A() |
|
1605 | # a = A() | |
1601 | # a.__dict__['foobar'] = 345 |
|
1606 | # a.__dict__['foobar'] = 345 | |
1602 | # a.foobar # == 123 |
|
1607 | # a.foobar # == 123 | |
1603 | # |
|
1608 | # | |
1604 | # So, a property may be returned right away. |
|
1609 | # So, a property may be returned right away. | |
1605 | if isinstance(attr, property): |
|
1610 | if isinstance(attr, property): | |
1606 | return attr |
|
1611 | return attr | |
1607 |
|
1612 | |||
1608 | # Nothing helped, fall back. |
|
1613 | # Nothing helped, fall back. | |
1609 | return getattr(obj, attrname) |
|
1614 | return getattr(obj, attrname) | |
1610 |
|
1615 | |||
1611 | def _object_find(self, oname, namespaces=None): |
|
1616 | def _object_find(self, oname, namespaces=None): | |
1612 | """Find an object and return a struct with info about it.""" |
|
1617 | """Find an object and return a struct with info about it.""" | |
1613 | return Struct(self._ofind(oname, namespaces)) |
|
1618 | return Struct(self._ofind(oname, namespaces)) | |
1614 |
|
1619 | |||
1615 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1620 | def _inspect(self, meth, oname, namespaces=None, **kw): | |
1616 | """Generic interface to the inspector system. |
|
1621 | """Generic interface to the inspector system. | |
1617 |
|
1622 | |||
1618 | This function is meant to be called by pdef, pdoc & friends. |
|
1623 | This function is meant to be called by pdef, pdoc & friends. | |
1619 | """ |
|
1624 | """ | |
1620 | info = self._object_find(oname, namespaces) |
|
1625 | info = self._object_find(oname, namespaces) | |
1621 | docformat = sphinxify if self.sphinxify_docstring else None |
|
1626 | docformat = sphinxify if self.sphinxify_docstring else None | |
1622 | if info.found: |
|
1627 | if info.found: | |
1623 | pmethod = getattr(self.inspector, meth) |
|
1628 | pmethod = getattr(self.inspector, meth) | |
1624 | # TODO: only apply format_screen to the plain/text repr of the mime |
|
1629 | # TODO: only apply format_screen to the plain/text repr of the mime | |
1625 | # bundle. |
|
1630 | # bundle. | |
1626 | formatter = format_screen if info.ismagic else docformat |
|
1631 | formatter = format_screen if info.ismagic else docformat | |
1627 | if meth == 'pdoc': |
|
1632 | if meth == 'pdoc': | |
1628 | pmethod(info.obj, oname, formatter) |
|
1633 | pmethod(info.obj, oname, formatter) | |
1629 | elif meth == 'pinfo': |
|
1634 | elif meth == 'pinfo': | |
1630 | pmethod( |
|
1635 | pmethod( | |
1631 | info.obj, |
|
1636 | info.obj, | |
1632 | oname, |
|
1637 | oname, | |
1633 | formatter, |
|
1638 | formatter, | |
1634 | info, |
|
1639 | info, | |
1635 | enable_html_pager=self.enable_html_pager, |
|
1640 | enable_html_pager=self.enable_html_pager, | |
1636 | **kw |
|
1641 | **kw | |
1637 | ) |
|
1642 | ) | |
1638 | else: |
|
1643 | else: | |
1639 | pmethod(info.obj, oname) |
|
1644 | pmethod(info.obj, oname) | |
1640 | else: |
|
1645 | else: | |
1641 | print('Object `%s` not found.' % oname) |
|
1646 | print('Object `%s` not found.' % oname) | |
1642 | return 'not found' # so callers can take other action |
|
1647 | return 'not found' # so callers can take other action | |
1643 |
|
1648 | |||
1644 | def object_inspect(self, oname, detail_level=0): |
|
1649 | def object_inspect(self, oname, detail_level=0): | |
1645 | """Get object info about oname""" |
|
1650 | """Get object info about oname""" | |
1646 | with self.builtin_trap: |
|
1651 | with self.builtin_trap: | |
1647 | info = self._object_find(oname) |
|
1652 | info = self._object_find(oname) | |
1648 | if info.found: |
|
1653 | if info.found: | |
1649 | return self.inspector.info(info.obj, oname, info=info, |
|
1654 | return self.inspector.info(info.obj, oname, info=info, | |
1650 | detail_level=detail_level |
|
1655 | detail_level=detail_level | |
1651 | ) |
|
1656 | ) | |
1652 | else: |
|
1657 | else: | |
1653 | return oinspect.object_info(name=oname, found=False) |
|
1658 | return oinspect.object_info(name=oname, found=False) | |
1654 |
|
1659 | |||
1655 | def object_inspect_text(self, oname, detail_level=0): |
|
1660 | def object_inspect_text(self, oname, detail_level=0): | |
1656 | """Get object info as formatted text""" |
|
1661 | """Get object info as formatted text""" | |
1657 | return self.object_inspect_mime(oname, detail_level)['text/plain'] |
|
1662 | return self.object_inspect_mime(oname, detail_level)['text/plain'] | |
1658 |
|
1663 | |||
1659 | def object_inspect_mime(self, oname, detail_level=0, omit_sections=()): |
|
1664 | def object_inspect_mime(self, oname, detail_level=0, omit_sections=()): | |
1660 | """Get object info as a mimebundle of formatted representations. |
|
1665 | """Get object info as a mimebundle of formatted representations. | |
1661 |
|
1666 | |||
1662 | A mimebundle is a dictionary, keyed by mime-type. |
|
1667 | A mimebundle is a dictionary, keyed by mime-type. | |
1663 | It must always have the key `'text/plain'`. |
|
1668 | It must always have the key `'text/plain'`. | |
1664 | """ |
|
1669 | """ | |
1665 | with self.builtin_trap: |
|
1670 | with self.builtin_trap: | |
1666 | info = self._object_find(oname) |
|
1671 | info = self._object_find(oname) | |
1667 | if info.found: |
|
1672 | if info.found: | |
1668 | docformat = sphinxify if self.sphinxify_docstring else None |
|
1673 | docformat = sphinxify if self.sphinxify_docstring else None | |
1669 | return self.inspector._get_info( |
|
1674 | return self.inspector._get_info( | |
1670 | info.obj, |
|
1675 | info.obj, | |
1671 | oname, |
|
1676 | oname, | |
1672 | info=info, |
|
1677 | info=info, | |
1673 | detail_level=detail_level, |
|
1678 | detail_level=detail_level, | |
1674 | formatter=docformat, |
|
1679 | formatter=docformat, | |
1675 | omit_sections=omit_sections, |
|
1680 | omit_sections=omit_sections, | |
1676 | ) |
|
1681 | ) | |
1677 | else: |
|
1682 | else: | |
1678 | raise KeyError(oname) |
|
1683 | raise KeyError(oname) | |
1679 |
|
1684 | |||
1680 | #------------------------------------------------------------------------- |
|
1685 | #------------------------------------------------------------------------- | |
1681 | # Things related to history management |
|
1686 | # Things related to history management | |
1682 | #------------------------------------------------------------------------- |
|
1687 | #------------------------------------------------------------------------- | |
1683 |
|
1688 | |||
1684 | def init_history(self): |
|
1689 | def init_history(self): | |
1685 | """Sets up the command history, and starts regular autosaves.""" |
|
1690 | """Sets up the command history, and starts regular autosaves.""" | |
1686 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1691 | self.history_manager = HistoryManager(shell=self, parent=self) | |
1687 | self.configurables.append(self.history_manager) |
|
1692 | self.configurables.append(self.history_manager) | |
1688 |
|
1693 | |||
1689 | #------------------------------------------------------------------------- |
|
1694 | #------------------------------------------------------------------------- | |
1690 | # Things related to exception handling and tracebacks (not debugging) |
|
1695 | # Things related to exception handling and tracebacks (not debugging) | |
1691 | #------------------------------------------------------------------------- |
|
1696 | #------------------------------------------------------------------------- | |
1692 |
|
1697 | |||
1693 | debugger_cls = InterruptiblePdb |
|
1698 | debugger_cls = InterruptiblePdb | |
1694 |
|
1699 | |||
1695 | def init_traceback_handlers(self, custom_exceptions): |
|
1700 | def init_traceback_handlers(self, custom_exceptions): | |
1696 | # Syntax error handler. |
|
1701 | # Syntax error handler. | |
1697 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self) |
|
1702 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor', parent=self) | |
1698 |
|
1703 | |||
1699 | # The interactive one is initialized with an offset, meaning we always |
|
1704 | # The interactive one is initialized with an offset, meaning we always | |
1700 | # want to remove the topmost item in the traceback, which is our own |
|
1705 | # want to remove the topmost item in the traceback, which is our own | |
1701 | # internal code. Valid modes: ['Plain','Context','Verbose','Minimal'] |
|
1706 | # internal code. Valid modes: ['Plain','Context','Verbose','Minimal'] | |
1702 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1707 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
1703 | color_scheme='NoColor', |
|
1708 | color_scheme='NoColor', | |
1704 | tb_offset = 1, |
|
1709 | tb_offset = 1, | |
1705 | check_cache=check_linecache_ipython, |
|
1710 | check_cache=check_linecache_ipython, | |
1706 | debugger_cls=self.debugger_cls, parent=self) |
|
1711 | debugger_cls=self.debugger_cls, parent=self) | |
1707 |
|
1712 | |||
1708 | # The instance will store a pointer to the system-wide exception hook, |
|
1713 | # The instance will store a pointer to the system-wide exception hook, | |
1709 | # so that runtime code (such as magics) can access it. This is because |
|
1714 | # so that runtime code (such as magics) can access it. This is because | |
1710 | # during the read-eval loop, it may get temporarily overwritten. |
|
1715 | # during the read-eval loop, it may get temporarily overwritten. | |
1711 | self.sys_excepthook = sys.excepthook |
|
1716 | self.sys_excepthook = sys.excepthook | |
1712 |
|
1717 | |||
1713 | # and add any custom exception handlers the user may have specified |
|
1718 | # and add any custom exception handlers the user may have specified | |
1714 | self.set_custom_exc(*custom_exceptions) |
|
1719 | self.set_custom_exc(*custom_exceptions) | |
1715 |
|
1720 | |||
1716 | # Set the exception mode |
|
1721 | # Set the exception mode | |
1717 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1722 | self.InteractiveTB.set_mode(mode=self.xmode) | |
1718 |
|
1723 | |||
1719 | def set_custom_exc(self, exc_tuple, handler): |
|
1724 | def set_custom_exc(self, exc_tuple, handler): | |
1720 | """set_custom_exc(exc_tuple, handler) |
|
1725 | """set_custom_exc(exc_tuple, handler) | |
1721 |
|
1726 | |||
1722 | Set a custom exception handler, which will be called if any of the |
|
1727 | Set a custom exception handler, which will be called if any of the | |
1723 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1728 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1724 | run_code() method). |
|
1729 | run_code() method). | |
1725 |
|
1730 | |||
1726 | Parameters |
|
1731 | Parameters | |
1727 | ---------- |
|
1732 | ---------- | |
1728 |
|
1733 | |||
1729 | exc_tuple : tuple of exception classes |
|
1734 | exc_tuple : tuple of exception classes | |
1730 | A *tuple* of exception classes, for which to call the defined |
|
1735 | A *tuple* of exception classes, for which to call the defined | |
1731 | handler. It is very important that you use a tuple, and NOT A |
|
1736 | handler. It is very important that you use a tuple, and NOT A | |
1732 | LIST here, because of the way Python's except statement works. If |
|
1737 | LIST here, because of the way Python's except statement works. If | |
1733 | you only want to trap a single exception, use a singleton tuple:: |
|
1738 | you only want to trap a single exception, use a singleton tuple:: | |
1734 |
|
1739 | |||
1735 | exc_tuple == (MyCustomException,) |
|
1740 | exc_tuple == (MyCustomException,) | |
1736 |
|
1741 | |||
1737 | handler : callable |
|
1742 | handler : callable | |
1738 | handler must have the following signature:: |
|
1743 | handler must have the following signature:: | |
1739 |
|
1744 | |||
1740 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1745 | def my_handler(self, etype, value, tb, tb_offset=None): | |
1741 | ... |
|
1746 | ... | |
1742 | return structured_traceback |
|
1747 | return structured_traceback | |
1743 |
|
1748 | |||
1744 | Your handler must return a structured traceback (a list of strings), |
|
1749 | Your handler must return a structured traceback (a list of strings), | |
1745 | or None. |
|
1750 | or None. | |
1746 |
|
1751 | |||
1747 | This will be made into an instance method (via types.MethodType) |
|
1752 | This will be made into an instance method (via types.MethodType) | |
1748 | of IPython itself, and it will be called if any of the exceptions |
|
1753 | of IPython itself, and it will be called if any of the exceptions | |
1749 | listed in the exc_tuple are caught. If the handler is None, an |
|
1754 | listed in the exc_tuple are caught. If the handler is None, an | |
1750 | internal basic one is used, which just prints basic info. |
|
1755 | internal basic one is used, which just prints basic info. | |
1751 |
|
1756 | |||
1752 | To protect IPython from crashes, if your handler ever raises an |
|
1757 | To protect IPython from crashes, if your handler ever raises an | |
1753 | exception or returns an invalid result, it will be immediately |
|
1758 | exception or returns an invalid result, it will be immediately | |
1754 | disabled. |
|
1759 | disabled. | |
1755 |
|
1760 | |||
1756 | Notes |
|
1761 | Notes | |
1757 | ----- |
|
1762 | ----- | |
1758 |
|
1763 | |||
1759 | WARNING: by putting in your own exception handler into IPython's main |
|
1764 | WARNING: by putting in your own exception handler into IPython's main | |
1760 | execution loop, you run a very good chance of nasty crashes. This |
|
1765 | execution loop, you run a very good chance of nasty crashes. This | |
1761 | facility should only be used if you really know what you are doing. |
|
1766 | facility should only be used if you really know what you are doing. | |
1762 | """ |
|
1767 | """ | |
1763 |
|
1768 | |||
1764 | if not isinstance(exc_tuple, tuple): |
|
1769 | if not isinstance(exc_tuple, tuple): | |
1765 | raise TypeError("The custom exceptions must be given as a tuple.") |
|
1770 | raise TypeError("The custom exceptions must be given as a tuple.") | |
1766 |
|
1771 | |||
1767 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
|
1772 | def dummy_handler(self, etype, value, tb, tb_offset=None): | |
1768 | print('*** Simple custom exception handler ***') |
|
1773 | print('*** Simple custom exception handler ***') | |
1769 | print('Exception type :', etype) |
|
1774 | print('Exception type :', etype) | |
1770 | print('Exception value:', value) |
|
1775 | print('Exception value:', value) | |
1771 | print('Traceback :', tb) |
|
1776 | print('Traceback :', tb) | |
1772 |
|
1777 | |||
1773 | def validate_stb(stb): |
|
1778 | def validate_stb(stb): | |
1774 | """validate structured traceback return type |
|
1779 | """validate structured traceback return type | |
1775 |
|
1780 | |||
1776 | return type of CustomTB *should* be a list of strings, but allow |
|
1781 | return type of CustomTB *should* be a list of strings, but allow | |
1777 | single strings or None, which are harmless. |
|
1782 | single strings or None, which are harmless. | |
1778 |
|
1783 | |||
1779 | This function will *always* return a list of strings, |
|
1784 | This function will *always* return a list of strings, | |
1780 | and will raise a TypeError if stb is inappropriate. |
|
1785 | and will raise a TypeError if stb is inappropriate. | |
1781 | """ |
|
1786 | """ | |
1782 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1787 | msg = "CustomTB must return list of strings, not %r" % stb | |
1783 | if stb is None: |
|
1788 | if stb is None: | |
1784 | return [] |
|
1789 | return [] | |
1785 | elif isinstance(stb, str): |
|
1790 | elif isinstance(stb, str): | |
1786 | return [stb] |
|
1791 | return [stb] | |
1787 | elif not isinstance(stb, list): |
|
1792 | elif not isinstance(stb, list): | |
1788 | raise TypeError(msg) |
|
1793 | raise TypeError(msg) | |
1789 | # it's a list |
|
1794 | # it's a list | |
1790 | for line in stb: |
|
1795 | for line in stb: | |
1791 | # check every element |
|
1796 | # check every element | |
1792 | if not isinstance(line, str): |
|
1797 | if not isinstance(line, str): | |
1793 | raise TypeError(msg) |
|
1798 | raise TypeError(msg) | |
1794 | return stb |
|
1799 | return stb | |
1795 |
|
1800 | |||
1796 | if handler is None: |
|
1801 | if handler is None: | |
1797 | wrapped = dummy_handler |
|
1802 | wrapped = dummy_handler | |
1798 | else: |
|
1803 | else: | |
1799 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1804 | def wrapped(self,etype,value,tb,tb_offset=None): | |
1800 | """wrap CustomTB handler, to protect IPython from user code |
|
1805 | """wrap CustomTB handler, to protect IPython from user code | |
1801 |
|
1806 | |||
1802 | This makes it harder (but not impossible) for custom exception |
|
1807 | This makes it harder (but not impossible) for custom exception | |
1803 | handlers to crash IPython. |
|
1808 | handlers to crash IPython. | |
1804 | """ |
|
1809 | """ | |
1805 | try: |
|
1810 | try: | |
1806 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1811 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) | |
1807 | return validate_stb(stb) |
|
1812 | return validate_stb(stb) | |
1808 | except: |
|
1813 | except: | |
1809 | # clear custom handler immediately |
|
1814 | # clear custom handler immediately | |
1810 | self.set_custom_exc((), None) |
|
1815 | self.set_custom_exc((), None) | |
1811 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1816 | print("Custom TB Handler failed, unregistering", file=sys.stderr) | |
1812 | # show the exception in handler first |
|
1817 | # show the exception in handler first | |
1813 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1818 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) | |
1814 | print(self.InteractiveTB.stb2text(stb)) |
|
1819 | print(self.InteractiveTB.stb2text(stb)) | |
1815 | print("The original exception:") |
|
1820 | print("The original exception:") | |
1816 | stb = self.InteractiveTB.structured_traceback( |
|
1821 | stb = self.InteractiveTB.structured_traceback( | |
1817 | (etype,value,tb), tb_offset=tb_offset |
|
1822 | (etype,value,tb), tb_offset=tb_offset | |
1818 | ) |
|
1823 | ) | |
1819 | return stb |
|
1824 | return stb | |
1820 |
|
1825 | |||
1821 | self.CustomTB = types.MethodType(wrapped,self) |
|
1826 | self.CustomTB = types.MethodType(wrapped,self) | |
1822 | self.custom_exceptions = exc_tuple |
|
1827 | self.custom_exceptions = exc_tuple | |
1823 |
|
1828 | |||
1824 | def excepthook(self, etype, value, tb): |
|
1829 | def excepthook(self, etype, value, tb): | |
1825 | """One more defense for GUI apps that call sys.excepthook. |
|
1830 | """One more defense for GUI apps that call sys.excepthook. | |
1826 |
|
1831 | |||
1827 | GUI frameworks like wxPython trap exceptions and call |
|
1832 | GUI frameworks like wxPython trap exceptions and call | |
1828 | sys.excepthook themselves. I guess this is a feature that |
|
1833 | sys.excepthook themselves. I guess this is a feature that | |
1829 | enables them to keep running after exceptions that would |
|
1834 | enables them to keep running after exceptions that would | |
1830 | otherwise kill their mainloop. This is a bother for IPython |
|
1835 | otherwise kill their mainloop. This is a bother for IPython | |
1831 | which expects to catch all of the program exceptions with a try: |
|
1836 | which expects to catch all of the program exceptions with a try: | |
1832 | except: statement. |
|
1837 | except: statement. | |
1833 |
|
1838 | |||
1834 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1839 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1835 | any app directly invokes sys.excepthook, it will look to the user like |
|
1840 | any app directly invokes sys.excepthook, it will look to the user like | |
1836 | IPython crashed. In order to work around this, we can disable the |
|
1841 | IPython crashed. In order to work around this, we can disable the | |
1837 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1842 | CrashHandler and replace it with this excepthook instead, which prints a | |
1838 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1843 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1839 | call sys.excepthook will generate a regular-looking exception from |
|
1844 | call sys.excepthook will generate a regular-looking exception from | |
1840 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1845 | IPython, and the CrashHandler will only be triggered by real IPython | |
1841 | crashes. |
|
1846 | crashes. | |
1842 |
|
1847 | |||
1843 | This hook should be used sparingly, only in places which are not likely |
|
1848 | This hook should be used sparingly, only in places which are not likely | |
1844 | to be true IPython errors. |
|
1849 | to be true IPython errors. | |
1845 | """ |
|
1850 | """ | |
1846 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1851 | self.showtraceback((etype, value, tb), tb_offset=0) | |
1847 |
|
1852 | |||
1848 | def _get_exc_info(self, exc_tuple=None): |
|
1853 | def _get_exc_info(self, exc_tuple=None): | |
1849 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1854 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. | |
1850 |
|
1855 | |||
1851 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1856 | Ensures sys.last_type,value,traceback hold the exc_info we found, | |
1852 | from whichever source. |
|
1857 | from whichever source. | |
1853 |
|
1858 | |||
1854 | raises ValueError if none of these contain any information |
|
1859 | raises ValueError if none of these contain any information | |
1855 | """ |
|
1860 | """ | |
1856 | if exc_tuple is None: |
|
1861 | if exc_tuple is None: | |
1857 | etype, value, tb = sys.exc_info() |
|
1862 | etype, value, tb = sys.exc_info() | |
1858 | else: |
|
1863 | else: | |
1859 | etype, value, tb = exc_tuple |
|
1864 | etype, value, tb = exc_tuple | |
1860 |
|
1865 | |||
1861 | if etype is None: |
|
1866 | if etype is None: | |
1862 | if hasattr(sys, 'last_type'): |
|
1867 | if hasattr(sys, 'last_type'): | |
1863 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1868 | etype, value, tb = sys.last_type, sys.last_value, \ | |
1864 | sys.last_traceback |
|
1869 | sys.last_traceback | |
1865 |
|
1870 | |||
1866 | if etype is None: |
|
1871 | if etype is None: | |
1867 | raise ValueError("No exception to find") |
|
1872 | raise ValueError("No exception to find") | |
1868 |
|
1873 | |||
1869 | # Now store the exception info in sys.last_type etc. |
|
1874 | # Now store the exception info in sys.last_type etc. | |
1870 | # WARNING: these variables are somewhat deprecated and not |
|
1875 | # WARNING: these variables are somewhat deprecated and not | |
1871 | # necessarily safe to use in a threaded environment, but tools |
|
1876 | # necessarily safe to use in a threaded environment, but tools | |
1872 | # like pdb depend on their existence, so let's set them. If we |
|
1877 | # like pdb depend on their existence, so let's set them. If we | |
1873 | # find problems in the field, we'll need to revisit their use. |
|
1878 | # find problems in the field, we'll need to revisit their use. | |
1874 | sys.last_type = etype |
|
1879 | sys.last_type = etype | |
1875 | sys.last_value = value |
|
1880 | sys.last_value = value | |
1876 | sys.last_traceback = tb |
|
1881 | sys.last_traceback = tb | |
1877 |
|
1882 | |||
1878 | return etype, value, tb |
|
1883 | return etype, value, tb | |
1879 |
|
1884 | |||
1880 | def show_usage_error(self, exc): |
|
1885 | def show_usage_error(self, exc): | |
1881 | """Show a short message for UsageErrors |
|
1886 | """Show a short message for UsageErrors | |
1882 |
|
1887 | |||
1883 | These are special exceptions that shouldn't show a traceback. |
|
1888 | These are special exceptions that shouldn't show a traceback. | |
1884 | """ |
|
1889 | """ | |
1885 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1890 | print("UsageError: %s" % exc, file=sys.stderr) | |
1886 |
|
1891 | |||
1887 | def get_exception_only(self, exc_tuple=None): |
|
1892 | def get_exception_only(self, exc_tuple=None): | |
1888 | """ |
|
1893 | """ | |
1889 | Return as a string (ending with a newline) the exception that |
|
1894 | Return as a string (ending with a newline) the exception that | |
1890 | just occurred, without any traceback. |
|
1895 | just occurred, without any traceback. | |
1891 | """ |
|
1896 | """ | |
1892 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1897 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1893 | msg = traceback.format_exception_only(etype, value) |
|
1898 | msg = traceback.format_exception_only(etype, value) | |
1894 | return ''.join(msg) |
|
1899 | return ''.join(msg) | |
1895 |
|
1900 | |||
1896 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1901 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, | |
1897 | exception_only=False, running_compiled_code=False): |
|
1902 | exception_only=False, running_compiled_code=False): | |
1898 | """Display the exception that just occurred. |
|
1903 | """Display the exception that just occurred. | |
1899 |
|
1904 | |||
1900 | If nothing is known about the exception, this is the method which |
|
1905 | If nothing is known about the exception, this is the method which | |
1901 | should be used throughout the code for presenting user tracebacks, |
|
1906 | should be used throughout the code for presenting user tracebacks, | |
1902 | rather than directly invoking the InteractiveTB object. |
|
1907 | rather than directly invoking the InteractiveTB object. | |
1903 |
|
1908 | |||
1904 | A specific showsyntaxerror() also exists, but this method can take |
|
1909 | A specific showsyntaxerror() also exists, but this method can take | |
1905 | care of calling it if needed, so unless you are explicitly catching a |
|
1910 | care of calling it if needed, so unless you are explicitly catching a | |
1906 | SyntaxError exception, don't try to analyze the stack manually and |
|
1911 | SyntaxError exception, don't try to analyze the stack manually and | |
1907 | simply call this method.""" |
|
1912 | simply call this method.""" | |
1908 |
|
1913 | |||
1909 | try: |
|
1914 | try: | |
1910 | try: |
|
1915 | try: | |
1911 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1916 | etype, value, tb = self._get_exc_info(exc_tuple) | |
1912 | except ValueError: |
|
1917 | except ValueError: | |
1913 | print('No traceback available to show.', file=sys.stderr) |
|
1918 | print('No traceback available to show.', file=sys.stderr) | |
1914 | return |
|
1919 | return | |
1915 |
|
1920 | |||
1916 | if issubclass(etype, SyntaxError): |
|
1921 | if issubclass(etype, SyntaxError): | |
1917 | # Though this won't be called by syntax errors in the input |
|
1922 | # Though this won't be called by syntax errors in the input | |
1918 | # line, there may be SyntaxError cases with imported code. |
|
1923 | # line, there may be SyntaxError cases with imported code. | |
1919 | self.showsyntaxerror(filename, running_compiled_code) |
|
1924 | self.showsyntaxerror(filename, running_compiled_code) | |
1920 | elif etype is UsageError: |
|
1925 | elif etype is UsageError: | |
1921 | self.show_usage_error(value) |
|
1926 | self.show_usage_error(value) | |
1922 | else: |
|
1927 | else: | |
1923 | if exception_only: |
|
1928 | if exception_only: | |
1924 | stb = ['An exception has occurred, use %tb to see ' |
|
1929 | stb = ['An exception has occurred, use %tb to see ' | |
1925 | 'the full traceback.\n'] |
|
1930 | 'the full traceback.\n'] | |
1926 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1931 | stb.extend(self.InteractiveTB.get_exception_only(etype, | |
1927 | value)) |
|
1932 | value)) | |
1928 | else: |
|
1933 | else: | |
1929 | try: |
|
1934 | try: | |
1930 | # Exception classes can customise their traceback - we |
|
1935 | # Exception classes can customise their traceback - we | |
1931 | # use this in IPython.parallel for exceptions occurring |
|
1936 | # use this in IPython.parallel for exceptions occurring | |
1932 | # in the engines. This should return a list of strings. |
|
1937 | # in the engines. This should return a list of strings. | |
1933 | stb = value._render_traceback_() |
|
1938 | stb = value._render_traceback_() | |
1934 | except Exception: |
|
1939 | except Exception: | |
1935 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1940 | stb = self.InteractiveTB.structured_traceback(etype, | |
1936 | value, tb, tb_offset=tb_offset) |
|
1941 | value, tb, tb_offset=tb_offset) | |
1937 |
|
1942 | |||
1938 | self._showtraceback(etype, value, stb) |
|
1943 | self._showtraceback(etype, value, stb) | |
1939 | if self.call_pdb: |
|
1944 | if self.call_pdb: | |
1940 | # drop into debugger |
|
1945 | # drop into debugger | |
1941 | self.debugger(force=True) |
|
1946 | self.debugger(force=True) | |
1942 | return |
|
1947 | return | |
1943 |
|
1948 | |||
1944 | # Actually show the traceback |
|
1949 | # Actually show the traceback | |
1945 | self._showtraceback(etype, value, stb) |
|
1950 | self._showtraceback(etype, value, stb) | |
1946 |
|
1951 | |||
1947 | except KeyboardInterrupt: |
|
1952 | except KeyboardInterrupt: | |
1948 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1953 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
1949 |
|
1954 | |||
1950 | def _showtraceback(self, etype, evalue, stb: str): |
|
1955 | def _showtraceback(self, etype, evalue, stb: str): | |
1951 | """Actually show a traceback. |
|
1956 | """Actually show a traceback. | |
1952 |
|
1957 | |||
1953 | Subclasses may override this method to put the traceback on a different |
|
1958 | Subclasses may override this method to put the traceback on a different | |
1954 | place, like a side channel. |
|
1959 | place, like a side channel. | |
1955 | """ |
|
1960 | """ | |
1956 | val = self.InteractiveTB.stb2text(stb) |
|
1961 | val = self.InteractiveTB.stb2text(stb) | |
1957 | try: |
|
1962 | try: | |
1958 | print(val) |
|
1963 | print(val) | |
1959 | except UnicodeEncodeError: |
|
1964 | except UnicodeEncodeError: | |
1960 | print(val.encode("utf-8", "backslashreplace").decode()) |
|
1965 | print(val.encode("utf-8", "backslashreplace").decode()) | |
1961 |
|
1966 | |||
1962 | def showsyntaxerror(self, filename=None, running_compiled_code=False): |
|
1967 | def showsyntaxerror(self, filename=None, running_compiled_code=False): | |
1963 | """Display the syntax error that just occurred. |
|
1968 | """Display the syntax error that just occurred. | |
1964 |
|
1969 | |||
1965 | This doesn't display a stack trace because there isn't one. |
|
1970 | This doesn't display a stack trace because there isn't one. | |
1966 |
|
1971 | |||
1967 | If a filename is given, it is stuffed in the exception instead |
|
1972 | If a filename is given, it is stuffed in the exception instead | |
1968 | of what was there before (because Python's parser always uses |
|
1973 | of what was there before (because Python's parser always uses | |
1969 | "<string>" when reading from a string). |
|
1974 | "<string>" when reading from a string). | |
1970 |
|
1975 | |||
1971 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), |
|
1976 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), | |
1972 | longer stack trace will be displayed. |
|
1977 | longer stack trace will be displayed. | |
1973 | """ |
|
1978 | """ | |
1974 | etype, value, last_traceback = self._get_exc_info() |
|
1979 | etype, value, last_traceback = self._get_exc_info() | |
1975 |
|
1980 | |||
1976 | if filename and issubclass(etype, SyntaxError): |
|
1981 | if filename and issubclass(etype, SyntaxError): | |
1977 | try: |
|
1982 | try: | |
1978 | value.filename = filename |
|
1983 | value.filename = filename | |
1979 | except: |
|
1984 | except: | |
1980 | # Not the format we expect; leave it alone |
|
1985 | # Not the format we expect; leave it alone | |
1981 | pass |
|
1986 | pass | |
1982 |
|
1987 | |||
1983 | # If the error occurred when executing compiled code, we should provide full stacktrace. |
|
1988 | # If the error occurred when executing compiled code, we should provide full stacktrace. | |
1984 | elist = traceback.extract_tb(last_traceback) if running_compiled_code else [] |
|
1989 | elist = traceback.extract_tb(last_traceback) if running_compiled_code else [] | |
1985 | stb = self.SyntaxTB.structured_traceback(etype, value, elist) |
|
1990 | stb = self.SyntaxTB.structured_traceback(etype, value, elist) | |
1986 | self._showtraceback(etype, value, stb) |
|
1991 | self._showtraceback(etype, value, stb) | |
1987 |
|
1992 | |||
1988 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1993 | # This is overridden in TerminalInteractiveShell to show a message about | |
1989 | # the %paste magic. |
|
1994 | # the %paste magic. | |
1990 | def showindentationerror(self): |
|
1995 | def showindentationerror(self): | |
1991 | """Called by _run_cell when there's an IndentationError in code entered |
|
1996 | """Called by _run_cell when there's an IndentationError in code entered | |
1992 | at the prompt. |
|
1997 | at the prompt. | |
1993 |
|
1998 | |||
1994 | This is overridden in TerminalInteractiveShell to show a message about |
|
1999 | This is overridden in TerminalInteractiveShell to show a message about | |
1995 | the %paste magic.""" |
|
2000 | the %paste magic.""" | |
1996 | self.showsyntaxerror() |
|
2001 | self.showsyntaxerror() | |
1997 |
|
2002 | |||
1998 | @skip_doctest |
|
2003 | @skip_doctest | |
1999 | def set_next_input(self, s, replace=False): |
|
2004 | def set_next_input(self, s, replace=False): | |
2000 | """ Sets the 'default' input string for the next command line. |
|
2005 | """ Sets the 'default' input string for the next command line. | |
2001 |
|
2006 | |||
2002 | Example:: |
|
2007 | Example:: | |
2003 |
|
2008 | |||
2004 | In [1]: _ip.set_next_input("Hello Word") |
|
2009 | In [1]: _ip.set_next_input("Hello Word") | |
2005 | In [2]: Hello Word_ # cursor is here |
|
2010 | In [2]: Hello Word_ # cursor is here | |
2006 | """ |
|
2011 | """ | |
2007 | self.rl_next_input = s |
|
2012 | self.rl_next_input = s | |
2008 |
|
2013 | |||
2009 | def _indent_current_str(self): |
|
2014 | def _indent_current_str(self): | |
2010 | """return the current level of indentation as a string""" |
|
2015 | """return the current level of indentation as a string""" | |
2011 | return self.input_splitter.get_indent_spaces() * ' ' |
|
2016 | return self.input_splitter.get_indent_spaces() * ' ' | |
2012 |
|
2017 | |||
2013 | #------------------------------------------------------------------------- |
|
2018 | #------------------------------------------------------------------------- | |
2014 | # Things related to text completion |
|
2019 | # Things related to text completion | |
2015 | #------------------------------------------------------------------------- |
|
2020 | #------------------------------------------------------------------------- | |
2016 |
|
2021 | |||
2017 | def init_completer(self): |
|
2022 | def init_completer(self): | |
2018 | """Initialize the completion machinery. |
|
2023 | """Initialize the completion machinery. | |
2019 |
|
2024 | |||
2020 | This creates completion machinery that can be used by client code, |
|
2025 | This creates completion machinery that can be used by client code, | |
2021 | either interactively in-process (typically triggered by the readline |
|
2026 | either interactively in-process (typically triggered by the readline | |
2022 | library), programmatically (such as in test suites) or out-of-process |
|
2027 | library), programmatically (such as in test suites) or out-of-process | |
2023 | (typically over the network by remote frontends). |
|
2028 | (typically over the network by remote frontends). | |
2024 | """ |
|
2029 | """ | |
2025 | from IPython.core.completer import IPCompleter |
|
2030 | from IPython.core.completer import IPCompleter | |
2026 | from IPython.core.completerlib import (module_completer, |
|
2031 | from IPython.core.completerlib import (module_completer, | |
2027 | magic_run_completer, cd_completer, reset_completer) |
|
2032 | magic_run_completer, cd_completer, reset_completer) | |
2028 |
|
2033 | |||
2029 | self.Completer = IPCompleter(shell=self, |
|
2034 | self.Completer = IPCompleter(shell=self, | |
2030 | namespace=self.user_ns, |
|
2035 | namespace=self.user_ns, | |
2031 | global_namespace=self.user_global_ns, |
|
2036 | global_namespace=self.user_global_ns, | |
2032 | parent=self, |
|
2037 | parent=self, | |
2033 | ) |
|
2038 | ) | |
2034 | self.configurables.append(self.Completer) |
|
2039 | self.configurables.append(self.Completer) | |
2035 |
|
2040 | |||
2036 | # Add custom completers to the basic ones built into IPCompleter |
|
2041 | # Add custom completers to the basic ones built into IPCompleter | |
2037 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
2042 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
2038 | self.strdispatchers['complete_command'] = sdisp |
|
2043 | self.strdispatchers['complete_command'] = sdisp | |
2039 | self.Completer.custom_completers = sdisp |
|
2044 | self.Completer.custom_completers = sdisp | |
2040 |
|
2045 | |||
2041 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
2046 | self.set_hook('complete_command', module_completer, str_key = 'import') | |
2042 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
2047 | self.set_hook('complete_command', module_completer, str_key = 'from') | |
2043 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
2048 | self.set_hook('complete_command', module_completer, str_key = '%aimport') | |
2044 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
2049 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') | |
2045 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
2050 | self.set_hook('complete_command', cd_completer, str_key = '%cd') | |
2046 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
2051 | self.set_hook('complete_command', reset_completer, str_key = '%reset') | |
2047 |
|
2052 | |||
2048 | @skip_doctest |
|
2053 | @skip_doctest | |
2049 | def complete(self, text, line=None, cursor_pos=None): |
|
2054 | def complete(self, text, line=None, cursor_pos=None): | |
2050 | """Return the completed text and a list of completions. |
|
2055 | """Return the completed text and a list of completions. | |
2051 |
|
2056 | |||
2052 | Parameters |
|
2057 | Parameters | |
2053 | ---------- |
|
2058 | ---------- | |
2054 |
|
2059 | |||
2055 | text : string |
|
2060 | text : string | |
2056 | A string of text to be completed on. It can be given as empty and |
|
2061 | A string of text to be completed on. It can be given as empty and | |
2057 | instead a line/position pair are given. In this case, the |
|
2062 | instead a line/position pair are given. In this case, the | |
2058 | completer itself will split the line like readline does. |
|
2063 | completer itself will split the line like readline does. | |
2059 |
|
2064 | |||
2060 | line : string, optional |
|
2065 | line : string, optional | |
2061 | The complete line that text is part of. |
|
2066 | The complete line that text is part of. | |
2062 |
|
2067 | |||
2063 | cursor_pos : int, optional |
|
2068 | cursor_pos : int, optional | |
2064 | The position of the cursor on the input line. |
|
2069 | The position of the cursor on the input line. | |
2065 |
|
2070 | |||
2066 | Returns |
|
2071 | Returns | |
2067 | ------- |
|
2072 | ------- | |
2068 | text : string |
|
2073 | text : string | |
2069 | The actual text that was completed. |
|
2074 | The actual text that was completed. | |
2070 |
|
2075 | |||
2071 | matches : list |
|
2076 | matches : list | |
2072 | A sorted list with all possible completions. |
|
2077 | A sorted list with all possible completions. | |
2073 |
|
2078 | |||
2074 |
|
2079 | |||
2075 | Notes |
|
2080 | Notes | |
2076 | ----- |
|
2081 | ----- | |
2077 | The optional arguments allow the completion to take more context into |
|
2082 | The optional arguments allow the completion to take more context into | |
2078 | account, and are part of the low-level completion API. |
|
2083 | account, and are part of the low-level completion API. | |
2079 |
|
2084 | |||
2080 | This is a wrapper around the completion mechanism, similar to what |
|
2085 | This is a wrapper around the completion mechanism, similar to what | |
2081 | readline does at the command line when the TAB key is hit. By |
|
2086 | readline does at the command line when the TAB key is hit. By | |
2082 | exposing it as a method, it can be used by other non-readline |
|
2087 | exposing it as a method, it can be used by other non-readline | |
2083 | environments (such as GUIs) for text completion. |
|
2088 | environments (such as GUIs) for text completion. | |
2084 |
|
2089 | |||
2085 | Examples |
|
2090 | Examples | |
2086 | -------- |
|
2091 | -------- | |
2087 |
|
2092 | |||
2088 | In [1]: x = 'hello' |
|
2093 | In [1]: x = 'hello' | |
2089 |
|
2094 | |||
2090 | In [2]: _ip.complete('x.l') |
|
2095 | In [2]: _ip.complete('x.l') | |
2091 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
2096 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |
2092 | """ |
|
2097 | """ | |
2093 |
|
2098 | |||
2094 | # Inject names into __builtin__ so we can complete on the added names. |
|
2099 | # Inject names into __builtin__ so we can complete on the added names. | |
2095 | with self.builtin_trap: |
|
2100 | with self.builtin_trap: | |
2096 | return self.Completer.complete(text, line, cursor_pos) |
|
2101 | return self.Completer.complete(text, line, cursor_pos) | |
2097 |
|
2102 | |||
2098 | def set_custom_completer(self, completer, pos=0) -> None: |
|
2103 | def set_custom_completer(self, completer, pos=0) -> None: | |
2099 | """Adds a new custom completer function. |
|
2104 | """Adds a new custom completer function. | |
2100 |
|
2105 | |||
2101 | The position argument (defaults to 0) is the index in the completers |
|
2106 | The position argument (defaults to 0) is the index in the completers | |
2102 | list where you want the completer to be inserted. |
|
2107 | list where you want the completer to be inserted. | |
2103 |
|
2108 | |||
2104 | `completer` should have the following signature:: |
|
2109 | `completer` should have the following signature:: | |
2105 |
|
2110 | |||
2106 | def completion(self: Completer, text: string) -> List[str]: |
|
2111 | def completion(self: Completer, text: string) -> List[str]: | |
2107 | raise NotImplementedError |
|
2112 | raise NotImplementedError | |
2108 |
|
2113 | |||
2109 | It will be bound to the current Completer instance and pass some text |
|
2114 | It will be bound to the current Completer instance and pass some text | |
2110 | and return a list with current completions to suggest to the user. |
|
2115 | and return a list with current completions to suggest to the user. | |
2111 | """ |
|
2116 | """ | |
2112 |
|
2117 | |||
2113 | newcomp = types.MethodType(completer, self.Completer) |
|
2118 | newcomp = types.MethodType(completer, self.Completer) | |
2114 | self.Completer.custom_matchers.insert(pos,newcomp) |
|
2119 | self.Completer.custom_matchers.insert(pos,newcomp) | |
2115 |
|
2120 | |||
2116 | def set_completer_frame(self, frame=None): |
|
2121 | def set_completer_frame(self, frame=None): | |
2117 | """Set the frame of the completer.""" |
|
2122 | """Set the frame of the completer.""" | |
2118 | if frame: |
|
2123 | if frame: | |
2119 | self.Completer.namespace = frame.f_locals |
|
2124 | self.Completer.namespace = frame.f_locals | |
2120 | self.Completer.global_namespace = frame.f_globals |
|
2125 | self.Completer.global_namespace = frame.f_globals | |
2121 | else: |
|
2126 | else: | |
2122 | self.Completer.namespace = self.user_ns |
|
2127 | self.Completer.namespace = self.user_ns | |
2123 | self.Completer.global_namespace = self.user_global_ns |
|
2128 | self.Completer.global_namespace = self.user_global_ns | |
2124 |
|
2129 | |||
2125 | #------------------------------------------------------------------------- |
|
2130 | #------------------------------------------------------------------------- | |
2126 | # Things related to magics |
|
2131 | # Things related to magics | |
2127 | #------------------------------------------------------------------------- |
|
2132 | #------------------------------------------------------------------------- | |
2128 |
|
2133 | |||
2129 | def init_magics(self): |
|
2134 | def init_magics(self): | |
2130 | from IPython.core import magics as m |
|
2135 | from IPython.core import magics as m | |
2131 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2136 | self.magics_manager = magic.MagicsManager(shell=self, | |
2132 | parent=self, |
|
2137 | parent=self, | |
2133 | user_magics=m.UserMagics(self)) |
|
2138 | user_magics=m.UserMagics(self)) | |
2134 | self.configurables.append(self.magics_manager) |
|
2139 | self.configurables.append(self.magics_manager) | |
2135 |
|
2140 | |||
2136 | # Expose as public API from the magics manager |
|
2141 | # Expose as public API from the magics manager | |
2137 | self.register_magics = self.magics_manager.register |
|
2142 | self.register_magics = self.magics_manager.register | |
2138 |
|
2143 | |||
2139 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2144 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |
2140 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2145 | m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics, | |
2141 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2146 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |
2142 | m.NamespaceMagics, m.OSMagics, m.PackagingMagics, |
|
2147 | m.NamespaceMagics, m.OSMagics, m.PackagingMagics, | |
2143 | m.PylabMagics, m.ScriptMagics, |
|
2148 | m.PylabMagics, m.ScriptMagics, | |
2144 | ) |
|
2149 | ) | |
2145 | self.register_magics(m.AsyncMagics) |
|
2150 | self.register_magics(m.AsyncMagics) | |
2146 |
|
2151 | |||
2147 | # Register Magic Aliases |
|
2152 | # Register Magic Aliases | |
2148 | mman = self.magics_manager |
|
2153 | mman = self.magics_manager | |
2149 | # FIXME: magic aliases should be defined by the Magics classes |
|
2154 | # FIXME: magic aliases should be defined by the Magics classes | |
2150 | # or in MagicsManager, not here |
|
2155 | # or in MagicsManager, not here | |
2151 | mman.register_alias('ed', 'edit') |
|
2156 | mman.register_alias('ed', 'edit') | |
2152 | mman.register_alias('hist', 'history') |
|
2157 | mman.register_alias('hist', 'history') | |
2153 | mman.register_alias('rep', 'recall') |
|
2158 | mman.register_alias('rep', 'recall') | |
2154 | mman.register_alias('SVG', 'svg', 'cell') |
|
2159 | mman.register_alias('SVG', 'svg', 'cell') | |
2155 | mman.register_alias('HTML', 'html', 'cell') |
|
2160 | mman.register_alias('HTML', 'html', 'cell') | |
2156 | mman.register_alias('file', 'writefile', 'cell') |
|
2161 | mman.register_alias('file', 'writefile', 'cell') | |
2157 |
|
2162 | |||
2158 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2163 | # FIXME: Move the color initialization to the DisplayHook, which | |
2159 | # should be split into a prompt manager and displayhook. We probably |
|
2164 | # should be split into a prompt manager and displayhook. We probably | |
2160 | # even need a centralize colors management object. |
|
2165 | # even need a centralize colors management object. | |
2161 | self.run_line_magic('colors', self.colors) |
|
2166 | self.run_line_magic('colors', self.colors) | |
2162 |
|
2167 | |||
2163 | # Defined here so that it's included in the documentation |
|
2168 | # Defined here so that it's included in the documentation | |
2164 | @functools.wraps(magic.MagicsManager.register_function) |
|
2169 | @functools.wraps(magic.MagicsManager.register_function) | |
2165 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2170 | def register_magic_function(self, func, magic_kind='line', magic_name=None): | |
2166 | self.magics_manager.register_function( |
|
2171 | self.magics_manager.register_function( | |
2167 | func, magic_kind=magic_kind, magic_name=magic_name |
|
2172 | func, magic_kind=magic_kind, magic_name=magic_name | |
2168 | ) |
|
2173 | ) | |
2169 |
|
2174 | |||
2170 | def run_line_magic(self, magic_name, line, _stack_depth=1): |
|
2175 | def run_line_magic(self, magic_name, line, _stack_depth=1): | |
2171 | """Execute the given line magic. |
|
2176 | """Execute the given line magic. | |
2172 |
|
2177 | |||
2173 | Parameters |
|
2178 | Parameters | |
2174 | ---------- |
|
2179 | ---------- | |
2175 | magic_name : str |
|
2180 | magic_name : str | |
2176 | Name of the desired magic function, without '%' prefix. |
|
2181 | Name of the desired magic function, without '%' prefix. | |
2177 | line : str |
|
2182 | line : str | |
2178 | The rest of the input line as a single string. |
|
2183 | The rest of the input line as a single string. | |
2179 | _stack_depth : int |
|
2184 | _stack_depth : int | |
2180 | If run_line_magic() is called from magic() then _stack_depth=2. |
|
2185 | If run_line_magic() is called from magic() then _stack_depth=2. | |
2181 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' |
|
2186 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' | |
2182 | """ |
|
2187 | """ | |
2183 | fn = self.find_line_magic(magic_name) |
|
2188 | fn = self.find_line_magic(magic_name) | |
2184 | if fn is None: |
|
2189 | if fn is None: | |
2185 | cm = self.find_cell_magic(magic_name) |
|
2190 | cm = self.find_cell_magic(magic_name) | |
2186 | etpl = "Line magic function `%%%s` not found%s." |
|
2191 | etpl = "Line magic function `%%%s` not found%s." | |
2187 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2192 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |
2188 | 'did you mean that instead?)' % magic_name ) |
|
2193 | 'did you mean that instead?)' % magic_name ) | |
2189 | raise UsageError(etpl % (magic_name, extra)) |
|
2194 | raise UsageError(etpl % (magic_name, extra)) | |
2190 | else: |
|
2195 | else: | |
2191 | # Note: this is the distance in the stack to the user's frame. |
|
2196 | # Note: this is the distance in the stack to the user's frame. | |
2192 | # This will need to be updated if the internal calling logic gets |
|
2197 | # This will need to be updated if the internal calling logic gets | |
2193 | # refactored, or else we'll be expanding the wrong variables. |
|
2198 | # refactored, or else we'll be expanding the wrong variables. | |
2194 |
|
2199 | |||
2195 | # Determine stack_depth depending on where run_line_magic() has been called |
|
2200 | # Determine stack_depth depending on where run_line_magic() has been called | |
2196 | stack_depth = _stack_depth |
|
2201 | stack_depth = _stack_depth | |
2197 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): |
|
2202 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): | |
2198 | # magic has opted out of var_expand |
|
2203 | # magic has opted out of var_expand | |
2199 | magic_arg_s = line |
|
2204 | magic_arg_s = line | |
2200 | else: |
|
2205 | else: | |
2201 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2206 | magic_arg_s = self.var_expand(line, stack_depth) | |
2202 | # Put magic args in a list so we can call with f(*a) syntax |
|
2207 | # Put magic args in a list so we can call with f(*a) syntax | |
2203 | args = [magic_arg_s] |
|
2208 | args = [magic_arg_s] | |
2204 | kwargs = {} |
|
2209 | kwargs = {} | |
2205 | # Grab local namespace if we need it: |
|
2210 | # Grab local namespace if we need it: | |
2206 | if getattr(fn, "needs_local_scope", False): |
|
2211 | if getattr(fn, "needs_local_scope", False): | |
2207 | kwargs['local_ns'] = self.get_local_scope(stack_depth) |
|
2212 | kwargs['local_ns'] = self.get_local_scope(stack_depth) | |
2208 | with self.builtin_trap: |
|
2213 | with self.builtin_trap: | |
2209 | result = fn(*args, **kwargs) |
|
2214 | result = fn(*args, **kwargs) | |
2210 | return result |
|
2215 | return result | |
2211 |
|
2216 | |||
2212 | def get_local_scope(self, stack_depth): |
|
2217 | def get_local_scope(self, stack_depth): | |
2213 | """Get local scope at given stack depth. |
|
2218 | """Get local scope at given stack depth. | |
2214 |
|
2219 | |||
2215 | Parameters |
|
2220 | Parameters | |
2216 | ---------- |
|
2221 | ---------- | |
2217 | stack_depth : int |
|
2222 | stack_depth : int | |
2218 | Depth relative to calling frame |
|
2223 | Depth relative to calling frame | |
2219 | """ |
|
2224 | """ | |
2220 | return sys._getframe(stack_depth + 1).f_locals |
|
2225 | return sys._getframe(stack_depth + 1).f_locals | |
2221 |
|
2226 | |||
2222 | def run_cell_magic(self, magic_name, line, cell): |
|
2227 | def run_cell_magic(self, magic_name, line, cell): | |
2223 | """Execute the given cell magic. |
|
2228 | """Execute the given cell magic. | |
2224 |
|
2229 | |||
2225 | Parameters |
|
2230 | Parameters | |
2226 | ---------- |
|
2231 | ---------- | |
2227 | magic_name : str |
|
2232 | magic_name : str | |
2228 | Name of the desired magic function, without '%' prefix. |
|
2233 | Name of the desired magic function, without '%' prefix. | |
2229 | line : str |
|
2234 | line : str | |
2230 | The rest of the first input line as a single string. |
|
2235 | The rest of the first input line as a single string. | |
2231 | cell : str |
|
2236 | cell : str | |
2232 | The body of the cell as a (possibly multiline) string. |
|
2237 | The body of the cell as a (possibly multiline) string. | |
2233 | """ |
|
2238 | """ | |
2234 | fn = self.find_cell_magic(magic_name) |
|
2239 | fn = self.find_cell_magic(magic_name) | |
2235 | if fn is None: |
|
2240 | if fn is None: | |
2236 | lm = self.find_line_magic(magic_name) |
|
2241 | lm = self.find_line_magic(magic_name) | |
2237 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2242 | etpl = "Cell magic `%%{0}` not found{1}." | |
2238 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2243 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' | |
2239 | 'did you mean that instead?)'.format(magic_name)) |
|
2244 | 'did you mean that instead?)'.format(magic_name)) | |
2240 | raise UsageError(etpl.format(magic_name, extra)) |
|
2245 | raise UsageError(etpl.format(magic_name, extra)) | |
2241 | elif cell == '': |
|
2246 | elif cell == '': | |
2242 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2247 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) | |
2243 | if self.find_line_magic(magic_name) is not None: |
|
2248 | if self.find_line_magic(magic_name) is not None: | |
2244 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2249 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) | |
2245 | raise UsageError(message) |
|
2250 | raise UsageError(message) | |
2246 | else: |
|
2251 | else: | |
2247 | # Note: this is the distance in the stack to the user's frame. |
|
2252 | # Note: this is the distance in the stack to the user's frame. | |
2248 | # This will need to be updated if the internal calling logic gets |
|
2253 | # This will need to be updated if the internal calling logic gets | |
2249 | # refactored, or else we'll be expanding the wrong variables. |
|
2254 | # refactored, or else we'll be expanding the wrong variables. | |
2250 | stack_depth = 2 |
|
2255 | stack_depth = 2 | |
2251 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): |
|
2256 | if getattr(fn, magic.MAGIC_NO_VAR_EXPAND_ATTR, False): | |
2252 | # magic has opted out of var_expand |
|
2257 | # magic has opted out of var_expand | |
2253 | magic_arg_s = line |
|
2258 | magic_arg_s = line | |
2254 | else: |
|
2259 | else: | |
2255 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2260 | magic_arg_s = self.var_expand(line, stack_depth) | |
2256 | kwargs = {} |
|
2261 | kwargs = {} | |
2257 | if getattr(fn, "needs_local_scope", False): |
|
2262 | if getattr(fn, "needs_local_scope", False): | |
2258 | kwargs['local_ns'] = self.user_ns |
|
2263 | kwargs['local_ns'] = self.user_ns | |
2259 |
|
2264 | |||
2260 | with self.builtin_trap: |
|
2265 | with self.builtin_trap: | |
2261 | args = (magic_arg_s, cell) |
|
2266 | args = (magic_arg_s, cell) | |
2262 | result = fn(*args, **kwargs) |
|
2267 | result = fn(*args, **kwargs) | |
2263 | return result |
|
2268 | return result | |
2264 |
|
2269 | |||
2265 | def find_line_magic(self, magic_name): |
|
2270 | def find_line_magic(self, magic_name): | |
2266 | """Find and return a line magic by name. |
|
2271 | """Find and return a line magic by name. | |
2267 |
|
2272 | |||
2268 | Returns None if the magic isn't found.""" |
|
2273 | Returns None if the magic isn't found.""" | |
2269 | return self.magics_manager.magics['line'].get(magic_name) |
|
2274 | return self.magics_manager.magics['line'].get(magic_name) | |
2270 |
|
2275 | |||
2271 | def find_cell_magic(self, magic_name): |
|
2276 | def find_cell_magic(self, magic_name): | |
2272 | """Find and return a cell magic by name. |
|
2277 | """Find and return a cell magic by name. | |
2273 |
|
2278 | |||
2274 | Returns None if the magic isn't found.""" |
|
2279 | Returns None if the magic isn't found.""" | |
2275 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2280 | return self.magics_manager.magics['cell'].get(magic_name) | |
2276 |
|
2281 | |||
2277 | def find_magic(self, magic_name, magic_kind='line'): |
|
2282 | def find_magic(self, magic_name, magic_kind='line'): | |
2278 | """Find and return a magic of the given type by name. |
|
2283 | """Find and return a magic of the given type by name. | |
2279 |
|
2284 | |||
2280 | Returns None if the magic isn't found.""" |
|
2285 | Returns None if the magic isn't found.""" | |
2281 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2286 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
2282 |
|
2287 | |||
2283 | def magic(self, arg_s): |
|
2288 | def magic(self, arg_s): | |
2284 | """DEPRECATED. Use run_line_magic() instead. |
|
2289 | """DEPRECATED. Use run_line_magic() instead. | |
2285 |
|
2290 | |||
2286 | Call a magic function by name. |
|
2291 | Call a magic function by name. | |
2287 |
|
2292 | |||
2288 | Input: a string containing the name of the magic function to call and |
|
2293 | Input: a string containing the name of the magic function to call and | |
2289 | any additional arguments to be passed to the magic. |
|
2294 | any additional arguments to be passed to the magic. | |
2290 |
|
2295 | |||
2291 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2296 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
2292 | prompt: |
|
2297 | prompt: | |
2293 |
|
2298 | |||
2294 | In[1]: %name -opt foo bar |
|
2299 | In[1]: %name -opt foo bar | |
2295 |
|
2300 | |||
2296 | To call a magic without arguments, simply use magic('name'). |
|
2301 | To call a magic without arguments, simply use magic('name'). | |
2297 |
|
2302 | |||
2298 | This provides a proper Python function to call IPython's magics in any |
|
2303 | This provides a proper Python function to call IPython's magics in any | |
2299 | valid Python code you can type at the interpreter, including loops and |
|
2304 | valid Python code you can type at the interpreter, including loops and | |
2300 | compound statements. |
|
2305 | compound statements. | |
2301 | """ |
|
2306 | """ | |
2302 | # TODO: should we issue a loud deprecation warning here? |
|
2307 | # TODO: should we issue a loud deprecation warning here? | |
2303 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2308 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2304 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2309 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2305 | return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) |
|
2310 | return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) | |
2306 |
|
2311 | |||
2307 | #------------------------------------------------------------------------- |
|
2312 | #------------------------------------------------------------------------- | |
2308 | # Things related to macros |
|
2313 | # Things related to macros | |
2309 | #------------------------------------------------------------------------- |
|
2314 | #------------------------------------------------------------------------- | |
2310 |
|
2315 | |||
2311 | def define_macro(self, name, themacro): |
|
2316 | def define_macro(self, name, themacro): | |
2312 | """Define a new macro |
|
2317 | """Define a new macro | |
2313 |
|
2318 | |||
2314 | Parameters |
|
2319 | Parameters | |
2315 | ---------- |
|
2320 | ---------- | |
2316 | name : str |
|
2321 | name : str | |
2317 | The name of the macro. |
|
2322 | The name of the macro. | |
2318 | themacro : str or Macro |
|
2323 | themacro : str or Macro | |
2319 | The action to do upon invoking the macro. If a string, a new |
|
2324 | The action to do upon invoking the macro. If a string, a new | |
2320 | Macro object is created by passing the string to it. |
|
2325 | Macro object is created by passing the string to it. | |
2321 | """ |
|
2326 | """ | |
2322 |
|
2327 | |||
2323 | from IPython.core import macro |
|
2328 | from IPython.core import macro | |
2324 |
|
2329 | |||
2325 | if isinstance(themacro, str): |
|
2330 | if isinstance(themacro, str): | |
2326 | themacro = macro.Macro(themacro) |
|
2331 | themacro = macro.Macro(themacro) | |
2327 | if not isinstance(themacro, macro.Macro): |
|
2332 | if not isinstance(themacro, macro.Macro): | |
2328 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2333 | raise ValueError('A macro must be a string or a Macro instance.') | |
2329 | self.user_ns[name] = themacro |
|
2334 | self.user_ns[name] = themacro | |
2330 |
|
2335 | |||
2331 | #------------------------------------------------------------------------- |
|
2336 | #------------------------------------------------------------------------- | |
2332 | # Things related to the running of system commands |
|
2337 | # Things related to the running of system commands | |
2333 | #------------------------------------------------------------------------- |
|
2338 | #------------------------------------------------------------------------- | |
2334 |
|
2339 | |||
2335 | def system_piped(self, cmd): |
|
2340 | def system_piped(self, cmd): | |
2336 | """Call the given cmd in a subprocess, piping stdout/err |
|
2341 | """Call the given cmd in a subprocess, piping stdout/err | |
2337 |
|
2342 | |||
2338 | Parameters |
|
2343 | Parameters | |
2339 | ---------- |
|
2344 | ---------- | |
2340 | cmd : str |
|
2345 | cmd : str | |
2341 | Command to execute (can not end in '&', as background processes are |
|
2346 | Command to execute (can not end in '&', as background processes are | |
2342 | not supported. Should not be a command that expects input |
|
2347 | not supported. Should not be a command that expects input | |
2343 | other than simple text. |
|
2348 | other than simple text. | |
2344 | """ |
|
2349 | """ | |
2345 | if cmd.rstrip().endswith('&'): |
|
2350 | if cmd.rstrip().endswith('&'): | |
2346 | # this is *far* from a rigorous test |
|
2351 | # this is *far* from a rigorous test | |
2347 | # We do not support backgrounding processes because we either use |
|
2352 | # We do not support backgrounding processes because we either use | |
2348 | # pexpect or pipes to read from. Users can always just call |
|
2353 | # pexpect or pipes to read from. Users can always just call | |
2349 | # os.system() or use ip.system=ip.system_raw |
|
2354 | # os.system() or use ip.system=ip.system_raw | |
2350 | # if they really want a background process. |
|
2355 | # if they really want a background process. | |
2351 | raise OSError("Background processes not supported.") |
|
2356 | raise OSError("Background processes not supported.") | |
2352 |
|
2357 | |||
2353 | # we explicitly do NOT return the subprocess status code, because |
|
2358 | # we explicitly do NOT return the subprocess status code, because | |
2354 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2359 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2355 | # Instead, we store the exit_code in user_ns. |
|
2360 | # Instead, we store the exit_code in user_ns. | |
2356 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2361 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) | |
2357 |
|
2362 | |||
2358 | def system_raw(self, cmd): |
|
2363 | def system_raw(self, cmd): | |
2359 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2364 | """Call the given cmd in a subprocess using os.system on Windows or | |
2360 | subprocess.call using the system shell on other platforms. |
|
2365 | subprocess.call using the system shell on other platforms. | |
2361 |
|
2366 | |||
2362 | Parameters |
|
2367 | Parameters | |
2363 | ---------- |
|
2368 | ---------- | |
2364 | cmd : str |
|
2369 | cmd : str | |
2365 | Command to execute. |
|
2370 | Command to execute. | |
2366 | """ |
|
2371 | """ | |
2367 | cmd = self.var_expand(cmd, depth=1) |
|
2372 | cmd = self.var_expand(cmd, depth=1) | |
2368 | # warn if there is an IPython magic alternative. |
|
2373 | # warn if there is an IPython magic alternative. | |
2369 | main_cmd = cmd.split()[0] |
|
2374 | main_cmd = cmd.split()[0] | |
2370 | has_magic_alternatives = ("pip", "conda", "cd", "ls") |
|
2375 | has_magic_alternatives = ("pip", "conda", "cd", "ls") | |
2371 |
|
2376 | |||
2372 | # had to check if the command was an alias expanded because of `ls` |
|
2377 | # had to check if the command was an alias expanded because of `ls` | |
2373 | is_alias_expanded = self.alias_manager.is_alias(main_cmd) and ( |
|
2378 | is_alias_expanded = self.alias_manager.is_alias(main_cmd) and ( | |
2374 | self.alias_manager.retrieve_alias(main_cmd).strip() == cmd.strip() |
|
2379 | self.alias_manager.retrieve_alias(main_cmd).strip() == cmd.strip() | |
2375 | ) |
|
2380 | ) | |
2376 |
|
2381 | |||
2377 | if main_cmd in has_magic_alternatives and not is_alias_expanded: |
|
2382 | if main_cmd in has_magic_alternatives and not is_alias_expanded: | |
2378 | warnings.warn( |
|
2383 | warnings.warn( | |
2379 | ( |
|
2384 | ( | |
2380 | "You executed the system command !{0} which may not work " |
|
2385 | "You executed the system command !{0} which may not work " | |
2381 | "as expected. Try the IPython magic %{0} instead." |
|
2386 | "as expected. Try the IPython magic %{0} instead." | |
2382 | ).format(main_cmd) |
|
2387 | ).format(main_cmd) | |
2383 | ) |
|
2388 | ) | |
2384 |
|
2389 | |||
2385 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2390 | # protect os.system from UNC paths on Windows, which it can't handle: | |
2386 | if sys.platform == 'win32': |
|
2391 | if sys.platform == 'win32': | |
2387 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2392 | from IPython.utils._process_win32 import AvoidUNCPath | |
2388 | with AvoidUNCPath() as path: |
|
2393 | with AvoidUNCPath() as path: | |
2389 | if path is not None: |
|
2394 | if path is not None: | |
2390 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2395 | cmd = '"pushd %s &&"%s' % (path, cmd) | |
2391 | try: |
|
2396 | try: | |
2392 | ec = os.system(cmd) |
|
2397 | ec = os.system(cmd) | |
2393 | except KeyboardInterrupt: |
|
2398 | except KeyboardInterrupt: | |
2394 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2399 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2395 | ec = -2 |
|
2400 | ec = -2 | |
2396 | else: |
|
2401 | else: | |
2397 | # For posix the result of the subprocess.call() below is an exit |
|
2402 | # For posix the result of the subprocess.call() below is an exit | |
2398 | # code, which by convention is zero for success, positive for |
|
2403 | # code, which by convention is zero for success, positive for | |
2399 | # program failure. Exit codes above 128 are reserved for signals, |
|
2404 | # program failure. Exit codes above 128 are reserved for signals, | |
2400 | # and the formula for converting a signal to an exit code is usually |
|
2405 | # and the formula for converting a signal to an exit code is usually | |
2401 | # signal_number+128. To more easily differentiate between exit |
|
2406 | # signal_number+128. To more easily differentiate between exit | |
2402 | # codes and signals, ipython uses negative numbers. For instance |
|
2407 | # codes and signals, ipython uses negative numbers. For instance | |
2403 | # since control-c is signal 2 but exit code 130, ipython's |
|
2408 | # since control-c is signal 2 but exit code 130, ipython's | |
2404 | # _exit_code variable will read -2. Note that some shells like |
|
2409 | # _exit_code variable will read -2. Note that some shells like | |
2405 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2410 | # csh and fish don't follow sh/bash conventions for exit codes. | |
2406 | executable = os.environ.get('SHELL', None) |
|
2411 | executable = os.environ.get('SHELL', None) | |
2407 | try: |
|
2412 | try: | |
2408 | # Use env shell instead of default /bin/sh |
|
2413 | # Use env shell instead of default /bin/sh | |
2409 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2414 | ec = subprocess.call(cmd, shell=True, executable=executable) | |
2410 | except KeyboardInterrupt: |
|
2415 | except KeyboardInterrupt: | |
2411 | # intercept control-C; a long traceback is not useful here |
|
2416 | # intercept control-C; a long traceback is not useful here | |
2412 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2417 | print('\n' + self.get_exception_only(), file=sys.stderr) | |
2413 | ec = 130 |
|
2418 | ec = 130 | |
2414 | if ec > 128: |
|
2419 | if ec > 128: | |
2415 | ec = -(ec - 128) |
|
2420 | ec = -(ec - 128) | |
2416 |
|
2421 | |||
2417 | # We explicitly do NOT return the subprocess status code, because |
|
2422 | # We explicitly do NOT return the subprocess status code, because | |
2418 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2423 | # a non-None value would trigger :func:`sys.displayhook` calls. | |
2419 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2424 | # Instead, we store the exit_code in user_ns. Note the semantics | |
2420 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2425 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, | |
2421 | # but raising SystemExit(_exit_code) will give status 254! |
|
2426 | # but raising SystemExit(_exit_code) will give status 254! | |
2422 | self.user_ns['_exit_code'] = ec |
|
2427 | self.user_ns['_exit_code'] = ec | |
2423 |
|
2428 | |||
2424 | # use piped system by default, because it is better behaved |
|
2429 | # use piped system by default, because it is better behaved | |
2425 | system = system_piped |
|
2430 | system = system_piped | |
2426 |
|
2431 | |||
2427 | def getoutput(self, cmd, split=True, depth=0): |
|
2432 | def getoutput(self, cmd, split=True, depth=0): | |
2428 | """Get output (possibly including stderr) from a subprocess. |
|
2433 | """Get output (possibly including stderr) from a subprocess. | |
2429 |
|
2434 | |||
2430 | Parameters |
|
2435 | Parameters | |
2431 | ---------- |
|
2436 | ---------- | |
2432 | cmd : str |
|
2437 | cmd : str | |
2433 | Command to execute (can not end in '&', as background processes are |
|
2438 | Command to execute (can not end in '&', as background processes are | |
2434 | not supported. |
|
2439 | not supported. | |
2435 | split : bool, optional |
|
2440 | split : bool, optional | |
2436 | If True, split the output into an IPython SList. Otherwise, an |
|
2441 | If True, split the output into an IPython SList. Otherwise, an | |
2437 | IPython LSString is returned. These are objects similar to normal |
|
2442 | IPython LSString is returned. These are objects similar to normal | |
2438 | lists and strings, with a few convenience attributes for easier |
|
2443 | lists and strings, with a few convenience attributes for easier | |
2439 | manipulation of line-based output. You can use '?' on them for |
|
2444 | manipulation of line-based output. You can use '?' on them for | |
2440 | details. |
|
2445 | details. | |
2441 | depth : int, optional |
|
2446 | depth : int, optional | |
2442 | How many frames above the caller are the local variables which should |
|
2447 | How many frames above the caller are the local variables which should | |
2443 | be expanded in the command string? The default (0) assumes that the |
|
2448 | be expanded in the command string? The default (0) assumes that the | |
2444 | expansion variables are in the stack frame calling this function. |
|
2449 | expansion variables are in the stack frame calling this function. | |
2445 | """ |
|
2450 | """ | |
2446 | if cmd.rstrip().endswith('&'): |
|
2451 | if cmd.rstrip().endswith('&'): | |
2447 | # this is *far* from a rigorous test |
|
2452 | # this is *far* from a rigorous test | |
2448 | raise OSError("Background processes not supported.") |
|
2453 | raise OSError("Background processes not supported.") | |
2449 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2454 | out = getoutput(self.var_expand(cmd, depth=depth+1)) | |
2450 | if split: |
|
2455 | if split: | |
2451 | out = SList(out.splitlines()) |
|
2456 | out = SList(out.splitlines()) | |
2452 | else: |
|
2457 | else: | |
2453 | out = LSString(out) |
|
2458 | out = LSString(out) | |
2454 | return out |
|
2459 | return out | |
2455 |
|
2460 | |||
2456 | #------------------------------------------------------------------------- |
|
2461 | #------------------------------------------------------------------------- | |
2457 | # Things related to aliases |
|
2462 | # Things related to aliases | |
2458 | #------------------------------------------------------------------------- |
|
2463 | #------------------------------------------------------------------------- | |
2459 |
|
2464 | |||
2460 | def init_alias(self): |
|
2465 | def init_alias(self): | |
2461 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2466 | self.alias_manager = AliasManager(shell=self, parent=self) | |
2462 | self.configurables.append(self.alias_manager) |
|
2467 | self.configurables.append(self.alias_manager) | |
2463 |
|
2468 | |||
2464 | #------------------------------------------------------------------------- |
|
2469 | #------------------------------------------------------------------------- | |
2465 | # Things related to extensions |
|
2470 | # Things related to extensions | |
2466 | #------------------------------------------------------------------------- |
|
2471 | #------------------------------------------------------------------------- | |
2467 |
|
2472 | |||
2468 | def init_extension_manager(self): |
|
2473 | def init_extension_manager(self): | |
2469 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2474 | self.extension_manager = ExtensionManager(shell=self, parent=self) | |
2470 | self.configurables.append(self.extension_manager) |
|
2475 | self.configurables.append(self.extension_manager) | |
2471 |
|
2476 | |||
2472 | #------------------------------------------------------------------------- |
|
2477 | #------------------------------------------------------------------------- | |
2473 | # Things related to payloads |
|
2478 | # Things related to payloads | |
2474 | #------------------------------------------------------------------------- |
|
2479 | #------------------------------------------------------------------------- | |
2475 |
|
2480 | |||
2476 | def init_payload(self): |
|
2481 | def init_payload(self): | |
2477 | self.payload_manager = PayloadManager(parent=self) |
|
2482 | self.payload_manager = PayloadManager(parent=self) | |
2478 | self.configurables.append(self.payload_manager) |
|
2483 | self.configurables.append(self.payload_manager) | |
2479 |
|
2484 | |||
2480 | #------------------------------------------------------------------------- |
|
2485 | #------------------------------------------------------------------------- | |
2481 | # Things related to the prefilter |
|
2486 | # Things related to the prefilter | |
2482 | #------------------------------------------------------------------------- |
|
2487 | #------------------------------------------------------------------------- | |
2483 |
|
2488 | |||
2484 | def init_prefilter(self): |
|
2489 | def init_prefilter(self): | |
2485 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2490 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) | |
2486 | self.configurables.append(self.prefilter_manager) |
|
2491 | self.configurables.append(self.prefilter_manager) | |
2487 | # Ultimately this will be refactored in the new interpreter code, but |
|
2492 | # Ultimately this will be refactored in the new interpreter code, but | |
2488 | # for now, we should expose the main prefilter method (there's legacy |
|
2493 | # for now, we should expose the main prefilter method (there's legacy | |
2489 | # code out there that may rely on this). |
|
2494 | # code out there that may rely on this). | |
2490 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2495 | self.prefilter = self.prefilter_manager.prefilter_lines | |
2491 |
|
2496 | |||
2492 | def auto_rewrite_input(self, cmd): |
|
2497 | def auto_rewrite_input(self, cmd): | |
2493 | """Print to the screen the rewritten form of the user's command. |
|
2498 | """Print to the screen the rewritten form of the user's command. | |
2494 |
|
2499 | |||
2495 | This shows visual feedback by rewriting input lines that cause |
|
2500 | This shows visual feedback by rewriting input lines that cause | |
2496 | automatic calling to kick in, like:: |
|
2501 | automatic calling to kick in, like:: | |
2497 |
|
2502 | |||
2498 | /f x |
|
2503 | /f x | |
2499 |
|
2504 | |||
2500 | into:: |
|
2505 | into:: | |
2501 |
|
2506 | |||
2502 | ------> f(x) |
|
2507 | ------> f(x) | |
2503 |
|
2508 | |||
2504 | after the user's input prompt. This helps the user understand that the |
|
2509 | after the user's input prompt. This helps the user understand that the | |
2505 | input line was transformed automatically by IPython. |
|
2510 | input line was transformed automatically by IPython. | |
2506 | """ |
|
2511 | """ | |
2507 | if not self.show_rewritten_input: |
|
2512 | if not self.show_rewritten_input: | |
2508 | return |
|
2513 | return | |
2509 |
|
2514 | |||
2510 | # This is overridden in TerminalInteractiveShell to use fancy prompts |
|
2515 | # This is overridden in TerminalInteractiveShell to use fancy prompts | |
2511 | print("------> " + cmd) |
|
2516 | print("------> " + cmd) | |
2512 |
|
2517 | |||
2513 | #------------------------------------------------------------------------- |
|
2518 | #------------------------------------------------------------------------- | |
2514 | # Things related to extracting values/expressions from kernel and user_ns |
|
2519 | # Things related to extracting values/expressions from kernel and user_ns | |
2515 | #------------------------------------------------------------------------- |
|
2520 | #------------------------------------------------------------------------- | |
2516 |
|
2521 | |||
2517 | def _user_obj_error(self): |
|
2522 | def _user_obj_error(self): | |
2518 | """return simple exception dict |
|
2523 | """return simple exception dict | |
2519 |
|
2524 | |||
2520 | for use in user_expressions |
|
2525 | for use in user_expressions | |
2521 | """ |
|
2526 | """ | |
2522 |
|
2527 | |||
2523 | etype, evalue, tb = self._get_exc_info() |
|
2528 | etype, evalue, tb = self._get_exc_info() | |
2524 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2529 | stb = self.InteractiveTB.get_exception_only(etype, evalue) | |
2525 |
|
2530 | |||
2526 | exc_info = { |
|
2531 | exc_info = { | |
2527 | "status": "error", |
|
2532 | "status": "error", | |
2528 | "traceback": stb, |
|
2533 | "traceback": stb, | |
2529 | "ename": etype.__name__, |
|
2534 | "ename": etype.__name__, | |
2530 | "evalue": py3compat.safe_unicode(evalue), |
|
2535 | "evalue": py3compat.safe_unicode(evalue), | |
2531 | } |
|
2536 | } | |
2532 |
|
2537 | |||
2533 | return exc_info |
|
2538 | return exc_info | |
2534 |
|
2539 | |||
2535 | def _format_user_obj(self, obj): |
|
2540 | def _format_user_obj(self, obj): | |
2536 | """format a user object to display dict |
|
2541 | """format a user object to display dict | |
2537 |
|
2542 | |||
2538 | for use in user_expressions |
|
2543 | for use in user_expressions | |
2539 | """ |
|
2544 | """ | |
2540 |
|
2545 | |||
2541 | data, md = self.display_formatter.format(obj) |
|
2546 | data, md = self.display_formatter.format(obj) | |
2542 | value = { |
|
2547 | value = { | |
2543 | 'status' : 'ok', |
|
2548 | 'status' : 'ok', | |
2544 | 'data' : data, |
|
2549 | 'data' : data, | |
2545 | 'metadata' : md, |
|
2550 | 'metadata' : md, | |
2546 | } |
|
2551 | } | |
2547 | return value |
|
2552 | return value | |
2548 |
|
2553 | |||
2549 | def user_expressions(self, expressions): |
|
2554 | def user_expressions(self, expressions): | |
2550 | """Evaluate a dict of expressions in the user's namespace. |
|
2555 | """Evaluate a dict of expressions in the user's namespace. | |
2551 |
|
2556 | |||
2552 | Parameters |
|
2557 | Parameters | |
2553 | ---------- |
|
2558 | ---------- | |
2554 | expressions : dict |
|
2559 | expressions : dict | |
2555 | A dict with string keys and string values. The expression values |
|
2560 | A dict with string keys and string values. The expression values | |
2556 | should be valid Python expressions, each of which will be evaluated |
|
2561 | should be valid Python expressions, each of which will be evaluated | |
2557 | in the user namespace. |
|
2562 | in the user namespace. | |
2558 |
|
2563 | |||
2559 | Returns |
|
2564 | Returns | |
2560 | ------- |
|
2565 | ------- | |
2561 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2566 | A dict, keyed like the input expressions dict, with the rich mime-typed | |
2562 | display_data of each value. |
|
2567 | display_data of each value. | |
2563 | """ |
|
2568 | """ | |
2564 | out = {} |
|
2569 | out = {} | |
2565 | user_ns = self.user_ns |
|
2570 | user_ns = self.user_ns | |
2566 | global_ns = self.user_global_ns |
|
2571 | global_ns = self.user_global_ns | |
2567 |
|
2572 | |||
2568 | for key, expr in expressions.items(): |
|
2573 | for key, expr in expressions.items(): | |
2569 | try: |
|
2574 | try: | |
2570 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2575 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) | |
2571 | except: |
|
2576 | except: | |
2572 | value = self._user_obj_error() |
|
2577 | value = self._user_obj_error() | |
2573 | out[key] = value |
|
2578 | out[key] = value | |
2574 | return out |
|
2579 | return out | |
2575 |
|
2580 | |||
2576 | #------------------------------------------------------------------------- |
|
2581 | #------------------------------------------------------------------------- | |
2577 | # Things related to the running of code |
|
2582 | # Things related to the running of code | |
2578 | #------------------------------------------------------------------------- |
|
2583 | #------------------------------------------------------------------------- | |
2579 |
|
2584 | |||
2580 | def ex(self, cmd): |
|
2585 | def ex(self, cmd): | |
2581 | """Execute a normal python statement in user namespace.""" |
|
2586 | """Execute a normal python statement in user namespace.""" | |
2582 | with self.builtin_trap: |
|
2587 | with self.builtin_trap: | |
2583 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2588 | exec(cmd, self.user_global_ns, self.user_ns) | |
2584 |
|
2589 | |||
2585 | def ev(self, expr): |
|
2590 | def ev(self, expr): | |
2586 | """Evaluate python expression expr in user namespace. |
|
2591 | """Evaluate python expression expr in user namespace. | |
2587 |
|
2592 | |||
2588 | Returns the result of evaluation |
|
2593 | Returns the result of evaluation | |
2589 | """ |
|
2594 | """ | |
2590 | with self.builtin_trap: |
|
2595 | with self.builtin_trap: | |
2591 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2596 | return eval(expr, self.user_global_ns, self.user_ns) | |
2592 |
|
2597 | |||
2593 | def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False): |
|
2598 | def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False, shell_futures=False): | |
2594 | """A safe version of the builtin execfile(). |
|
2599 | """A safe version of the builtin execfile(). | |
2595 |
|
2600 | |||
2596 | This version will never throw an exception, but instead print |
|
2601 | This version will never throw an exception, but instead print | |
2597 | helpful error messages to the screen. This only works on pure |
|
2602 | helpful error messages to the screen. This only works on pure | |
2598 | Python files with the .py extension. |
|
2603 | Python files with the .py extension. | |
2599 |
|
2604 | |||
2600 | Parameters |
|
2605 | Parameters | |
2601 | ---------- |
|
2606 | ---------- | |
2602 | fname : string |
|
2607 | fname : string | |
2603 | The name of the file to be executed. |
|
2608 | The name of the file to be executed. | |
2604 | where : tuple |
|
2609 | where : tuple | |
2605 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2610 | One or two namespaces, passed to execfile() as (globals,locals). | |
2606 | If only one is given, it is passed as both. |
|
2611 | If only one is given, it is passed as both. | |
2607 | exit_ignore : bool (False) |
|
2612 | exit_ignore : bool (False) | |
2608 | If True, then silence SystemExit for non-zero status (it is always |
|
2613 | If True, then silence SystemExit for non-zero status (it is always | |
2609 | silenced for zero status, as it is so common). |
|
2614 | silenced for zero status, as it is so common). | |
2610 | raise_exceptions : bool (False) |
|
2615 | raise_exceptions : bool (False) | |
2611 | If True raise exceptions everywhere. Meant for testing. |
|
2616 | If True raise exceptions everywhere. Meant for testing. | |
2612 | shell_futures : bool (False) |
|
2617 | shell_futures : bool (False) | |
2613 | If True, the code will share future statements with the interactive |
|
2618 | If True, the code will share future statements with the interactive | |
2614 | shell. It will both be affected by previous __future__ imports, and |
|
2619 | shell. It will both be affected by previous __future__ imports, and | |
2615 | any __future__ imports in the code will affect the shell. If False, |
|
2620 | any __future__ imports in the code will affect the shell. If False, | |
2616 | __future__ imports are not shared in either direction. |
|
2621 | __future__ imports are not shared in either direction. | |
2617 |
|
2622 | |||
2618 | """ |
|
2623 | """ | |
2619 | fname = Path(fname).expanduser().resolve() |
|
2624 | fname = Path(fname).expanduser().resolve() | |
2620 |
|
2625 | |||
2621 | # Make sure we can open the file |
|
2626 | # Make sure we can open the file | |
2622 | try: |
|
2627 | try: | |
2623 | with fname.open(): |
|
2628 | with fname.open(): | |
2624 | pass |
|
2629 | pass | |
2625 | except: |
|
2630 | except: | |
2626 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2631 | warn('Could not open file <%s> for safe execution.' % fname) | |
2627 | return |
|
2632 | return | |
2628 |
|
2633 | |||
2629 | # Find things also in current directory. This is needed to mimic the |
|
2634 | # Find things also in current directory. This is needed to mimic the | |
2630 | # behavior of running a script from the system command line, where |
|
2635 | # behavior of running a script from the system command line, where | |
2631 | # Python inserts the script's directory into sys.path |
|
2636 | # Python inserts the script's directory into sys.path | |
2632 | dname = str(fname.parent) |
|
2637 | dname = str(fname.parent) | |
2633 |
|
2638 | |||
2634 | with prepended_to_syspath(dname), self.builtin_trap: |
|
2639 | with prepended_to_syspath(dname), self.builtin_trap: | |
2635 | try: |
|
2640 | try: | |
2636 | glob, loc = (where + (None, ))[:2] |
|
2641 | glob, loc = (where + (None, ))[:2] | |
2637 | py3compat.execfile( |
|
2642 | py3compat.execfile( | |
2638 | fname, glob, loc, |
|
2643 | fname, glob, loc, | |
2639 | self.compile if shell_futures else None) |
|
2644 | self.compile if shell_futures else None) | |
2640 | except SystemExit as status: |
|
2645 | except SystemExit as status: | |
2641 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2646 | # If the call was made with 0 or None exit status (sys.exit(0) | |
2642 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2647 | # or sys.exit() ), don't bother showing a traceback, as both of | |
2643 | # these are considered normal by the OS: |
|
2648 | # these are considered normal by the OS: | |
2644 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2649 | # > python -c'import sys;sys.exit(0)'; echo $? | |
2645 | # 0 |
|
2650 | # 0 | |
2646 | # > python -c'import sys;sys.exit()'; echo $? |
|
2651 | # > python -c'import sys;sys.exit()'; echo $? | |
2647 | # 0 |
|
2652 | # 0 | |
2648 | # For other exit status, we show the exception unless |
|
2653 | # For other exit status, we show the exception unless | |
2649 | # explicitly silenced, but only in short form. |
|
2654 | # explicitly silenced, but only in short form. | |
2650 | if status.code: |
|
2655 | if status.code: | |
2651 | if raise_exceptions: |
|
2656 | if raise_exceptions: | |
2652 | raise |
|
2657 | raise | |
2653 | if not exit_ignore: |
|
2658 | if not exit_ignore: | |
2654 | self.showtraceback(exception_only=True) |
|
2659 | self.showtraceback(exception_only=True) | |
2655 | except: |
|
2660 | except: | |
2656 | if raise_exceptions: |
|
2661 | if raise_exceptions: | |
2657 | raise |
|
2662 | raise | |
2658 | # tb offset is 2 because we wrap execfile |
|
2663 | # tb offset is 2 because we wrap execfile | |
2659 | self.showtraceback(tb_offset=2) |
|
2664 | self.showtraceback(tb_offset=2) | |
2660 |
|
2665 | |||
2661 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2666 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): | |
2662 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2667 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. | |
2663 |
|
2668 | |||
2664 | Parameters |
|
2669 | Parameters | |
2665 | ---------- |
|
2670 | ---------- | |
2666 | fname : str |
|
2671 | fname : str | |
2667 | The name of the file to execute. The filename must have a |
|
2672 | The name of the file to execute. The filename must have a | |
2668 | .ipy or .ipynb extension. |
|
2673 | .ipy or .ipynb extension. | |
2669 | shell_futures : bool (False) |
|
2674 | shell_futures : bool (False) | |
2670 | If True, the code will share future statements with the interactive |
|
2675 | If True, the code will share future statements with the interactive | |
2671 | shell. It will both be affected by previous __future__ imports, and |
|
2676 | shell. It will both be affected by previous __future__ imports, and | |
2672 | any __future__ imports in the code will affect the shell. If False, |
|
2677 | any __future__ imports in the code will affect the shell. If False, | |
2673 | __future__ imports are not shared in either direction. |
|
2678 | __future__ imports are not shared in either direction. | |
2674 | raise_exceptions : bool (False) |
|
2679 | raise_exceptions : bool (False) | |
2675 | If True raise exceptions everywhere. Meant for testing. |
|
2680 | If True raise exceptions everywhere. Meant for testing. | |
2676 | """ |
|
2681 | """ | |
2677 | fname = Path(fname).expanduser().resolve() |
|
2682 | fname = Path(fname).expanduser().resolve() | |
2678 |
|
2683 | |||
2679 | # Make sure we can open the file |
|
2684 | # Make sure we can open the file | |
2680 | try: |
|
2685 | try: | |
2681 | with fname.open(): |
|
2686 | with fname.open(): | |
2682 | pass |
|
2687 | pass | |
2683 | except: |
|
2688 | except: | |
2684 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2689 | warn('Could not open file <%s> for safe execution.' % fname) | |
2685 | return |
|
2690 | return | |
2686 |
|
2691 | |||
2687 | # Find things also in current directory. This is needed to mimic the |
|
2692 | # Find things also in current directory. This is needed to mimic the | |
2688 | # behavior of running a script from the system command line, where |
|
2693 | # behavior of running a script from the system command line, where | |
2689 | # Python inserts the script's directory into sys.path |
|
2694 | # Python inserts the script's directory into sys.path | |
2690 | dname = str(fname.parent) |
|
2695 | dname = str(fname.parent) | |
2691 |
|
2696 | |||
2692 | def get_cells(): |
|
2697 | def get_cells(): | |
2693 | """generator for sequence of code blocks to run""" |
|
2698 | """generator for sequence of code blocks to run""" | |
2694 | if fname.suffix == ".ipynb": |
|
2699 | if fname.suffix == ".ipynb": | |
2695 | from nbformat import read |
|
2700 | from nbformat import read | |
2696 | nb = read(fname, as_version=4) |
|
2701 | nb = read(fname, as_version=4) | |
2697 | if not nb.cells: |
|
2702 | if not nb.cells: | |
2698 | return |
|
2703 | return | |
2699 | for cell in nb.cells: |
|
2704 | for cell in nb.cells: | |
2700 | if cell.cell_type == 'code': |
|
2705 | if cell.cell_type == 'code': | |
2701 | yield cell.source |
|
2706 | yield cell.source | |
2702 | else: |
|
2707 | else: | |
2703 | yield fname.read_text() |
|
2708 | yield fname.read_text() | |
2704 |
|
2709 | |||
2705 | with prepended_to_syspath(dname): |
|
2710 | with prepended_to_syspath(dname): | |
2706 | try: |
|
2711 | try: | |
2707 | for cell in get_cells(): |
|
2712 | for cell in get_cells(): | |
2708 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2713 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) | |
2709 | if raise_exceptions: |
|
2714 | if raise_exceptions: | |
2710 | result.raise_error() |
|
2715 | result.raise_error() | |
2711 | elif not result.success: |
|
2716 | elif not result.success: | |
2712 | break |
|
2717 | break | |
2713 | except: |
|
2718 | except: | |
2714 | if raise_exceptions: |
|
2719 | if raise_exceptions: | |
2715 | raise |
|
2720 | raise | |
2716 | self.showtraceback() |
|
2721 | self.showtraceback() | |
2717 | warn('Unknown failure executing file: <%s>' % fname) |
|
2722 | warn('Unknown failure executing file: <%s>' % fname) | |
2718 |
|
2723 | |||
2719 | def safe_run_module(self, mod_name, where): |
|
2724 | def safe_run_module(self, mod_name, where): | |
2720 | """A safe version of runpy.run_module(). |
|
2725 | """A safe version of runpy.run_module(). | |
2721 |
|
2726 | |||
2722 | This version will never throw an exception, but instead print |
|
2727 | This version will never throw an exception, but instead print | |
2723 | helpful error messages to the screen. |
|
2728 | helpful error messages to the screen. | |
2724 |
|
2729 | |||
2725 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2730 | `SystemExit` exceptions with status code 0 or None are ignored. | |
2726 |
|
2731 | |||
2727 | Parameters |
|
2732 | Parameters | |
2728 | ---------- |
|
2733 | ---------- | |
2729 | mod_name : string |
|
2734 | mod_name : string | |
2730 | The name of the module to be executed. |
|
2735 | The name of the module to be executed. | |
2731 | where : dict |
|
2736 | where : dict | |
2732 | The globals namespace. |
|
2737 | The globals namespace. | |
2733 | """ |
|
2738 | """ | |
2734 | try: |
|
2739 | try: | |
2735 | try: |
|
2740 | try: | |
2736 | where.update( |
|
2741 | where.update( | |
2737 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2742 | runpy.run_module(str(mod_name), run_name="__main__", | |
2738 | alter_sys=True) |
|
2743 | alter_sys=True) | |
2739 | ) |
|
2744 | ) | |
2740 | except SystemExit as status: |
|
2745 | except SystemExit as status: | |
2741 | if status.code: |
|
2746 | if status.code: | |
2742 | raise |
|
2747 | raise | |
2743 | except: |
|
2748 | except: | |
2744 | self.showtraceback() |
|
2749 | self.showtraceback() | |
2745 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2750 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2746 |
|
2751 | |||
2747 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2752 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): | |
2748 | """Run a complete IPython cell. |
|
2753 | """Run a complete IPython cell. | |
2749 |
|
2754 | |||
2750 | Parameters |
|
2755 | Parameters | |
2751 | ---------- |
|
2756 | ---------- | |
2752 | raw_cell : str |
|
2757 | raw_cell : str | |
2753 | The code (including IPython code such as %magic functions) to run. |
|
2758 | The code (including IPython code such as %magic functions) to run. | |
2754 | store_history : bool |
|
2759 | store_history : bool | |
2755 | If True, the raw and translated cell will be stored in IPython's |
|
2760 | If True, the raw and translated cell will be stored in IPython's | |
2756 | history. For user code calling back into IPython's machinery, this |
|
2761 | history. For user code calling back into IPython's machinery, this | |
2757 | should be set to False. |
|
2762 | should be set to False. | |
2758 | silent : bool |
|
2763 | silent : bool | |
2759 | If True, avoid side-effects, such as implicit displayhooks and |
|
2764 | If True, avoid side-effects, such as implicit displayhooks and | |
2760 | and logging. silent=True forces store_history=False. |
|
2765 | and logging. silent=True forces store_history=False. | |
2761 | shell_futures : bool |
|
2766 | shell_futures : bool | |
2762 | If True, the code will share future statements with the interactive |
|
2767 | If True, the code will share future statements with the interactive | |
2763 | shell. It will both be affected by previous __future__ imports, and |
|
2768 | shell. It will both be affected by previous __future__ imports, and | |
2764 | any __future__ imports in the code will affect the shell. If False, |
|
2769 | any __future__ imports in the code will affect the shell. If False, | |
2765 | __future__ imports are not shared in either direction. |
|
2770 | __future__ imports are not shared in either direction. | |
2766 |
|
2771 | |||
2767 | Returns |
|
2772 | Returns | |
2768 | ------- |
|
2773 | ------- | |
2769 | result : :class:`ExecutionResult` |
|
2774 | result : :class:`ExecutionResult` | |
2770 | """ |
|
2775 | """ | |
2771 | result = None |
|
2776 | result = None | |
2772 | try: |
|
2777 | try: | |
2773 | result = self._run_cell( |
|
2778 | result = self._run_cell( | |
2774 | raw_cell, store_history, silent, shell_futures) |
|
2779 | raw_cell, store_history, silent, shell_futures) | |
2775 | finally: |
|
2780 | finally: | |
2776 | self.events.trigger('post_execute') |
|
2781 | self.events.trigger('post_execute') | |
2777 | if not silent: |
|
2782 | if not silent: | |
2778 | self.events.trigger('post_run_cell', result) |
|
2783 | self.events.trigger('post_run_cell', result) | |
2779 | return result |
|
2784 | return result | |
2780 |
|
2785 | |||
2781 | def _run_cell(self, raw_cell:str, store_history:bool, silent:bool, shell_futures:bool) -> ExecutionResult: |
|
2786 | def _run_cell(self, raw_cell:str, store_history:bool, silent:bool, shell_futures:bool) -> ExecutionResult: | |
2782 | """Internal method to run a complete IPython cell.""" |
|
2787 | """Internal method to run a complete IPython cell.""" | |
2783 |
|
2788 | |||
2784 | # we need to avoid calling self.transform_cell multiple time on the same thing |
|
2789 | # we need to avoid calling self.transform_cell multiple time on the same thing | |
2785 | # so we need to store some results: |
|
2790 | # so we need to store some results: | |
2786 | preprocessing_exc_tuple = None |
|
2791 | preprocessing_exc_tuple = None | |
2787 | try: |
|
2792 | try: | |
2788 | transformed_cell = self.transform_cell(raw_cell) |
|
2793 | transformed_cell = self.transform_cell(raw_cell) | |
2789 | except Exception: |
|
2794 | except Exception: | |
2790 | transformed_cell = raw_cell |
|
2795 | transformed_cell = raw_cell | |
2791 | preprocessing_exc_tuple = sys.exc_info() |
|
2796 | preprocessing_exc_tuple = sys.exc_info() | |
2792 |
|
2797 | |||
2793 | assert transformed_cell is not None |
|
2798 | assert transformed_cell is not None | |
2794 | coro = self.run_cell_async( |
|
2799 | coro = self.run_cell_async( | |
2795 | raw_cell, |
|
2800 | raw_cell, | |
2796 | store_history=store_history, |
|
2801 | store_history=store_history, | |
2797 | silent=silent, |
|
2802 | silent=silent, | |
2798 | shell_futures=shell_futures, |
|
2803 | shell_futures=shell_futures, | |
2799 | transformed_cell=transformed_cell, |
|
2804 | transformed_cell=transformed_cell, | |
2800 | preprocessing_exc_tuple=preprocessing_exc_tuple, |
|
2805 | preprocessing_exc_tuple=preprocessing_exc_tuple, | |
2801 | ) |
|
2806 | ) | |
2802 |
|
2807 | |||
2803 | # run_cell_async is async, but may not actually need an eventloop. |
|
2808 | # run_cell_async is async, but may not actually need an eventloop. | |
2804 | # when this is the case, we want to run it using the pseudo_sync_runner |
|
2809 | # when this is the case, we want to run it using the pseudo_sync_runner | |
2805 | # so that code can invoke eventloops (for example via the %run , and |
|
2810 | # so that code can invoke eventloops (for example via the %run , and | |
2806 | # `%paste` magic. |
|
2811 | # `%paste` magic. | |
2807 | if self.trio_runner: |
|
2812 | if self.trio_runner: | |
2808 | runner = self.trio_runner |
|
2813 | runner = self.trio_runner | |
2809 | elif self.should_run_async( |
|
2814 | elif self.should_run_async( | |
2810 | raw_cell, |
|
2815 | raw_cell, | |
2811 | transformed_cell=transformed_cell, |
|
2816 | transformed_cell=transformed_cell, | |
2812 | preprocessing_exc_tuple=preprocessing_exc_tuple, |
|
2817 | preprocessing_exc_tuple=preprocessing_exc_tuple, | |
2813 | ): |
|
2818 | ): | |
2814 | runner = self.loop_runner |
|
2819 | runner = self.loop_runner | |
2815 | else: |
|
2820 | else: | |
2816 | runner = _pseudo_sync_runner |
|
2821 | runner = _pseudo_sync_runner | |
2817 |
|
2822 | |||
2818 | try: |
|
2823 | try: | |
2819 | return runner(coro) |
|
2824 | return runner(coro) | |
2820 | except BaseException as e: |
|
2825 | except BaseException as e: | |
2821 | info = ExecutionInfo(raw_cell, store_history, silent, shell_futures) |
|
2826 | info = ExecutionInfo(raw_cell, store_history, silent, shell_futures) | |
2822 | result = ExecutionResult(info) |
|
2827 | result = ExecutionResult(info) | |
2823 | result.error_in_exec = e |
|
2828 | result.error_in_exec = e | |
2824 | self.showtraceback(running_compiled_code=True) |
|
2829 | self.showtraceback(running_compiled_code=True) | |
2825 | return result |
|
2830 | return result | |
2826 |
|
2831 | |||
2827 | def should_run_async( |
|
2832 | def should_run_async( | |
2828 | self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None |
|
2833 | self, raw_cell: str, *, transformed_cell=None, preprocessing_exc_tuple=None | |
2829 | ) -> bool: |
|
2834 | ) -> bool: | |
2830 | """Return whether a cell should be run asynchronously via a coroutine runner |
|
2835 | """Return whether a cell should be run asynchronously via a coroutine runner | |
2831 |
|
2836 | |||
2832 | Parameters |
|
2837 | Parameters | |
2833 | ---------- |
|
2838 | ---------- | |
2834 | raw_cell: str |
|
2839 | raw_cell: str | |
2835 | The code to be executed |
|
2840 | The code to be executed | |
2836 |
|
2841 | |||
2837 | Returns |
|
2842 | Returns | |
2838 | ------- |
|
2843 | ------- | |
2839 | result: bool |
|
2844 | result: bool | |
2840 | Whether the code needs to be run with a coroutine runner or not |
|
2845 | Whether the code needs to be run with a coroutine runner or not | |
2841 |
|
2846 | |||
2842 | .. versionadded:: 7.0 |
|
2847 | .. versionadded:: 7.0 | |
2843 | """ |
|
2848 | """ | |
2844 | if not self.autoawait: |
|
2849 | if not self.autoawait: | |
2845 | return False |
|
2850 | return False | |
2846 | if preprocessing_exc_tuple is not None: |
|
2851 | if preprocessing_exc_tuple is not None: | |
2847 | return False |
|
2852 | return False | |
2848 | assert preprocessing_exc_tuple is None |
|
2853 | assert preprocessing_exc_tuple is None | |
2849 | if transformed_cell is None: |
|
2854 | if transformed_cell is None: | |
2850 | warnings.warn( |
|
2855 | warnings.warn( | |
2851 | "`should_run_async` will not call `transform_cell`" |
|
2856 | "`should_run_async` will not call `transform_cell`" | |
2852 | " automatically in the future. Please pass the result to" |
|
2857 | " automatically in the future. Please pass the result to" | |
2853 | " `transformed_cell` argument and any exception that happen" |
|
2858 | " `transformed_cell` argument and any exception that happen" | |
2854 | " during the" |
|
2859 | " during the" | |
2855 | "transform in `preprocessing_exc_tuple` in" |
|
2860 | "transform in `preprocessing_exc_tuple` in" | |
2856 | " IPython 7.17 and above.", |
|
2861 | " IPython 7.17 and above.", | |
2857 | DeprecationWarning, |
|
2862 | DeprecationWarning, | |
2858 | stacklevel=2, |
|
2863 | stacklevel=2, | |
2859 | ) |
|
2864 | ) | |
2860 | try: |
|
2865 | try: | |
2861 | cell = self.transform_cell(raw_cell) |
|
2866 | cell = self.transform_cell(raw_cell) | |
2862 | except Exception: |
|
2867 | except Exception: | |
2863 | # any exception during transform will be raised |
|
2868 | # any exception during transform will be raised | |
2864 | # prior to execution |
|
2869 | # prior to execution | |
2865 | return False |
|
2870 | return False | |
2866 | else: |
|
2871 | else: | |
2867 | cell = transformed_cell |
|
2872 | cell = transformed_cell | |
2868 | return _should_be_async(cell) |
|
2873 | return _should_be_async(cell) | |
2869 |
|
2874 | |||
2870 | async def run_cell_async( |
|
2875 | async def run_cell_async( | |
2871 | self, |
|
2876 | self, | |
2872 | raw_cell: str, |
|
2877 | raw_cell: str, | |
2873 | store_history=False, |
|
2878 | store_history=False, | |
2874 | silent=False, |
|
2879 | silent=False, | |
2875 | shell_futures=True, |
|
2880 | shell_futures=True, | |
2876 | *, |
|
2881 | *, | |
2877 | transformed_cell: Optional[str] = None, |
|
2882 | transformed_cell: Optional[str] = None, | |
2878 | preprocessing_exc_tuple: Optional[Any] = None |
|
2883 | preprocessing_exc_tuple: Optional[Any] = None | |
2879 | ) -> ExecutionResult: |
|
2884 | ) -> ExecutionResult: | |
2880 | """Run a complete IPython cell asynchronously. |
|
2885 | """Run a complete IPython cell asynchronously. | |
2881 |
|
2886 | |||
2882 | Parameters |
|
2887 | Parameters | |
2883 | ---------- |
|
2888 | ---------- | |
2884 | raw_cell : str |
|
2889 | raw_cell : str | |
2885 | The code (including IPython code such as %magic functions) to run. |
|
2890 | The code (including IPython code such as %magic functions) to run. | |
2886 | store_history : bool |
|
2891 | store_history : bool | |
2887 | If True, the raw and translated cell will be stored in IPython's |
|
2892 | If True, the raw and translated cell will be stored in IPython's | |
2888 | history. For user code calling back into IPython's machinery, this |
|
2893 | history. For user code calling back into IPython's machinery, this | |
2889 | should be set to False. |
|
2894 | should be set to False. | |
2890 | silent : bool |
|
2895 | silent : bool | |
2891 | If True, avoid side-effects, such as implicit displayhooks and |
|
2896 | If True, avoid side-effects, such as implicit displayhooks and | |
2892 | and logging. silent=True forces store_history=False. |
|
2897 | and logging. silent=True forces store_history=False. | |
2893 | shell_futures : bool |
|
2898 | shell_futures : bool | |
2894 | If True, the code will share future statements with the interactive |
|
2899 | If True, the code will share future statements with the interactive | |
2895 | shell. It will both be affected by previous __future__ imports, and |
|
2900 | shell. It will both be affected by previous __future__ imports, and | |
2896 | any __future__ imports in the code will affect the shell. If False, |
|
2901 | any __future__ imports in the code will affect the shell. If False, | |
2897 | __future__ imports are not shared in either direction. |
|
2902 | __future__ imports are not shared in either direction. | |
2898 | transformed_cell: str |
|
2903 | transformed_cell: str | |
2899 | cell that was passed through transformers |
|
2904 | cell that was passed through transformers | |
2900 | preprocessing_exc_tuple: |
|
2905 | preprocessing_exc_tuple: | |
2901 | trace if the transformation failed. |
|
2906 | trace if the transformation failed. | |
2902 |
|
2907 | |||
2903 | Returns |
|
2908 | Returns | |
2904 | ------- |
|
2909 | ------- | |
2905 | result : :class:`ExecutionResult` |
|
2910 | result : :class:`ExecutionResult` | |
2906 |
|
2911 | |||
2907 | .. versionadded:: 7.0 |
|
2912 | .. versionadded:: 7.0 | |
2908 | """ |
|
2913 | """ | |
2909 | info = ExecutionInfo( |
|
2914 | info = ExecutionInfo( | |
2910 | raw_cell, store_history, silent, shell_futures) |
|
2915 | raw_cell, store_history, silent, shell_futures) | |
2911 | result = ExecutionResult(info) |
|
2916 | result = ExecutionResult(info) | |
2912 |
|
2917 | |||
2913 | if (not raw_cell) or raw_cell.isspace(): |
|
2918 | if (not raw_cell) or raw_cell.isspace(): | |
2914 | self.last_execution_succeeded = True |
|
2919 | self.last_execution_succeeded = True | |
2915 | self.last_execution_result = result |
|
2920 | self.last_execution_result = result | |
2916 | return result |
|
2921 | return result | |
2917 |
|
2922 | |||
2918 | if silent: |
|
2923 | if silent: | |
2919 | store_history = False |
|
2924 | store_history = False | |
2920 |
|
2925 | |||
2921 | if store_history: |
|
2926 | if store_history: | |
2922 | result.execution_count = self.execution_count |
|
2927 | result.execution_count = self.execution_count | |
2923 |
|
2928 | |||
2924 | def error_before_exec(value): |
|
2929 | def error_before_exec(value): | |
2925 | if store_history: |
|
2930 | if store_history: | |
2926 | self.execution_count += 1 |
|
2931 | self.execution_count += 1 | |
2927 | result.error_before_exec = value |
|
2932 | result.error_before_exec = value | |
2928 | self.last_execution_succeeded = False |
|
2933 | self.last_execution_succeeded = False | |
2929 | self.last_execution_result = result |
|
2934 | self.last_execution_result = result | |
2930 | return result |
|
2935 | return result | |
2931 |
|
2936 | |||
2932 | self.events.trigger('pre_execute') |
|
2937 | self.events.trigger('pre_execute') | |
2933 | if not silent: |
|
2938 | if not silent: | |
2934 | self.events.trigger('pre_run_cell', info) |
|
2939 | self.events.trigger('pre_run_cell', info) | |
2935 |
|
2940 | |||
2936 | if transformed_cell is None: |
|
2941 | if transformed_cell is None: | |
2937 | warnings.warn( |
|
2942 | warnings.warn( | |
2938 | "`run_cell_async` will not call `transform_cell`" |
|
2943 | "`run_cell_async` will not call `transform_cell`" | |
2939 | " automatically in the future. Please pass the result to" |
|
2944 | " automatically in the future. Please pass the result to" | |
2940 | " `transformed_cell` argument and any exception that happen" |
|
2945 | " `transformed_cell` argument and any exception that happen" | |
2941 | " during the" |
|
2946 | " during the" | |
2942 | "transform in `preprocessing_exc_tuple` in" |
|
2947 | "transform in `preprocessing_exc_tuple` in" | |
2943 | " IPython 7.17 and above.", |
|
2948 | " IPython 7.17 and above.", | |
2944 | DeprecationWarning, |
|
2949 | DeprecationWarning, | |
2945 | stacklevel=2, |
|
2950 | stacklevel=2, | |
2946 | ) |
|
2951 | ) | |
2947 | # If any of our input transformation (input_transformer_manager or |
|
2952 | # If any of our input transformation (input_transformer_manager or | |
2948 | # prefilter_manager) raises an exception, we store it in this variable |
|
2953 | # prefilter_manager) raises an exception, we store it in this variable | |
2949 | # so that we can display the error after logging the input and storing |
|
2954 | # so that we can display the error after logging the input and storing | |
2950 | # it in the history. |
|
2955 | # it in the history. | |
2951 | try: |
|
2956 | try: | |
2952 | cell = self.transform_cell(raw_cell) |
|
2957 | cell = self.transform_cell(raw_cell) | |
2953 | except Exception: |
|
2958 | except Exception: | |
2954 | preprocessing_exc_tuple = sys.exc_info() |
|
2959 | preprocessing_exc_tuple = sys.exc_info() | |
2955 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2960 | cell = raw_cell # cell has to exist so it can be stored/logged | |
2956 | else: |
|
2961 | else: | |
2957 | preprocessing_exc_tuple = None |
|
2962 | preprocessing_exc_tuple = None | |
2958 | else: |
|
2963 | else: | |
2959 | if preprocessing_exc_tuple is None: |
|
2964 | if preprocessing_exc_tuple is None: | |
2960 | cell = transformed_cell |
|
2965 | cell = transformed_cell | |
2961 | else: |
|
2966 | else: | |
2962 | cell = raw_cell |
|
2967 | cell = raw_cell | |
2963 |
|
2968 | |||
2964 | # Store raw and processed history |
|
2969 | # Store raw and processed history | |
2965 | if store_history: |
|
2970 | if store_history: | |
2966 | self.history_manager.store_inputs(self.execution_count, |
|
2971 | self.history_manager.store_inputs(self.execution_count, | |
2967 | cell, raw_cell) |
|
2972 | cell, raw_cell) | |
2968 | if not silent: |
|
2973 | if not silent: | |
2969 | self.logger.log(cell, raw_cell) |
|
2974 | self.logger.log(cell, raw_cell) | |
2970 |
|
2975 | |||
2971 | # Display the exception if input processing failed. |
|
2976 | # Display the exception if input processing failed. | |
2972 | if preprocessing_exc_tuple is not None: |
|
2977 | if preprocessing_exc_tuple is not None: | |
2973 | self.showtraceback(preprocessing_exc_tuple) |
|
2978 | self.showtraceback(preprocessing_exc_tuple) | |
2974 | if store_history: |
|
2979 | if store_history: | |
2975 | self.execution_count += 1 |
|
2980 | self.execution_count += 1 | |
2976 | return error_before_exec(preprocessing_exc_tuple[1]) |
|
2981 | return error_before_exec(preprocessing_exc_tuple[1]) | |
2977 |
|
2982 | |||
2978 | # Our own compiler remembers the __future__ environment. If we want to |
|
2983 | # Our own compiler remembers the __future__ environment. If we want to | |
2979 | # run code with a separate __future__ environment, use the default |
|
2984 | # run code with a separate __future__ environment, use the default | |
2980 | # compiler |
|
2985 | # compiler | |
2981 | compiler = self.compile if shell_futures else self.compiler_class() |
|
2986 | compiler = self.compile if shell_futures else self.compiler_class() | |
2982 |
|
2987 | |||
2983 | _run_async = False |
|
2988 | _run_async = False | |
2984 |
|
2989 | |||
2985 | with self.builtin_trap: |
|
2990 | with self.builtin_trap: | |
2986 | cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell) |
|
2991 | cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell) | |
2987 |
|
2992 | |||
2988 | with self.display_trap: |
|
2993 | with self.display_trap: | |
2989 | # Compile to bytecode |
|
2994 | # Compile to bytecode | |
2990 | try: |
|
2995 | try: | |
2991 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2996 | code_ast = compiler.ast_parse(cell, filename=cell_name) | |
2992 | except self.custom_exceptions as e: |
|
2997 | except self.custom_exceptions as e: | |
2993 | etype, value, tb = sys.exc_info() |
|
2998 | etype, value, tb = sys.exc_info() | |
2994 | self.CustomTB(etype, value, tb) |
|
2999 | self.CustomTB(etype, value, tb) | |
2995 | return error_before_exec(e) |
|
3000 | return error_before_exec(e) | |
2996 | except IndentationError as e: |
|
3001 | except IndentationError as e: | |
2997 | self.showindentationerror() |
|
3002 | self.showindentationerror() | |
2998 | return error_before_exec(e) |
|
3003 | return error_before_exec(e) | |
2999 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
3004 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
3000 | MemoryError) as e: |
|
3005 | MemoryError) as e: | |
3001 | self.showsyntaxerror() |
|
3006 | self.showsyntaxerror() | |
3002 | return error_before_exec(e) |
|
3007 | return error_before_exec(e) | |
3003 |
|
3008 | |||
3004 | # Apply AST transformations |
|
3009 | # Apply AST transformations | |
3005 | try: |
|
3010 | try: | |
3006 | code_ast = self.transform_ast(code_ast) |
|
3011 | code_ast = self.transform_ast(code_ast) | |
3007 | except InputRejected as e: |
|
3012 | except InputRejected as e: | |
3008 | self.showtraceback() |
|
3013 | self.showtraceback() | |
3009 | return error_before_exec(e) |
|
3014 | return error_before_exec(e) | |
3010 |
|
3015 | |||
3011 | # Give the displayhook a reference to our ExecutionResult so it |
|
3016 | # Give the displayhook a reference to our ExecutionResult so it | |
3012 | # can fill in the output value. |
|
3017 | # can fill in the output value. | |
3013 | self.displayhook.exec_result = result |
|
3018 | self.displayhook.exec_result = result | |
3014 |
|
3019 | |||
3015 | # Execute the user code |
|
3020 | # Execute the user code | |
3016 | interactivity = "none" if silent else self.ast_node_interactivity |
|
3021 | interactivity = "none" if silent else self.ast_node_interactivity | |
3017 |
|
3022 | |||
3018 | has_raised = await self.run_ast_nodes(code_ast.body, cell_name, |
|
3023 | has_raised = await self.run_ast_nodes(code_ast.body, cell_name, | |
3019 | interactivity=interactivity, compiler=compiler, result=result) |
|
3024 | interactivity=interactivity, compiler=compiler, result=result) | |
3020 |
|
3025 | |||
3021 | self.last_execution_succeeded = not has_raised |
|
3026 | self.last_execution_succeeded = not has_raised | |
3022 | self.last_execution_result = result |
|
3027 | self.last_execution_result = result | |
3023 |
|
3028 | |||
3024 | # Reset this so later displayed values do not modify the |
|
3029 | # Reset this so later displayed values do not modify the | |
3025 | # ExecutionResult |
|
3030 | # ExecutionResult | |
3026 | self.displayhook.exec_result = None |
|
3031 | self.displayhook.exec_result = None | |
3027 |
|
3032 | |||
3028 | if store_history: |
|
3033 | if store_history: | |
3029 | # Write output to the database. Does nothing unless |
|
3034 | # Write output to the database. Does nothing unless | |
3030 | # history output logging is enabled. |
|
3035 | # history output logging is enabled. | |
3031 | self.history_manager.store_output(self.execution_count) |
|
3036 | self.history_manager.store_output(self.execution_count) | |
3032 | # Each cell is a *single* input, regardless of how many lines it has |
|
3037 | # Each cell is a *single* input, regardless of how many lines it has | |
3033 | self.execution_count += 1 |
|
3038 | self.execution_count += 1 | |
3034 |
|
3039 | |||
3035 | return result |
|
3040 | return result | |
3036 |
|
3041 | |||
3037 | def transform_cell(self, raw_cell): |
|
3042 | def transform_cell(self, raw_cell): | |
3038 | """Transform an input cell before parsing it. |
|
3043 | """Transform an input cell before parsing it. | |
3039 |
|
3044 | |||
3040 | Static transformations, implemented in IPython.core.inputtransformer2, |
|
3045 | Static transformations, implemented in IPython.core.inputtransformer2, | |
3041 | deal with things like ``%magic`` and ``!system`` commands. |
|
3046 | deal with things like ``%magic`` and ``!system`` commands. | |
3042 | These run on all input. |
|
3047 | These run on all input. | |
3043 | Dynamic transformations, for things like unescaped magics and the exit |
|
3048 | Dynamic transformations, for things like unescaped magics and the exit | |
3044 | autocall, depend on the state of the interpreter. |
|
3049 | autocall, depend on the state of the interpreter. | |
3045 | These only apply to single line inputs. |
|
3050 | These only apply to single line inputs. | |
3046 |
|
3051 | |||
3047 | These string-based transformations are followed by AST transformations; |
|
3052 | These string-based transformations are followed by AST transformations; | |
3048 | see :meth:`transform_ast`. |
|
3053 | see :meth:`transform_ast`. | |
3049 | """ |
|
3054 | """ | |
3050 | # Static input transformations |
|
3055 | # Static input transformations | |
3051 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
3056 | cell = self.input_transformer_manager.transform_cell(raw_cell) | |
3052 |
|
3057 | |||
3053 | if len(cell.splitlines()) == 1: |
|
3058 | if len(cell.splitlines()) == 1: | |
3054 | # Dynamic transformations - only applied for single line commands |
|
3059 | # Dynamic transformations - only applied for single line commands | |
3055 | with self.builtin_trap: |
|
3060 | with self.builtin_trap: | |
3056 | # use prefilter_lines to handle trailing newlines |
|
3061 | # use prefilter_lines to handle trailing newlines | |
3057 | # restore trailing newline for ast.parse |
|
3062 | # restore trailing newline for ast.parse | |
3058 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
3063 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' | |
3059 |
|
3064 | |||
3060 | lines = cell.splitlines(keepends=True) |
|
3065 | lines = cell.splitlines(keepends=True) | |
3061 | for transform in self.input_transformers_post: |
|
3066 | for transform in self.input_transformers_post: | |
3062 | lines = transform(lines) |
|
3067 | lines = transform(lines) | |
3063 | cell = ''.join(lines) |
|
3068 | cell = ''.join(lines) | |
3064 |
|
3069 | |||
3065 | return cell |
|
3070 | return cell | |
3066 |
|
3071 | |||
3067 | def transform_ast(self, node): |
|
3072 | def transform_ast(self, node): | |
3068 | """Apply the AST transformations from self.ast_transformers |
|
3073 | """Apply the AST transformations from self.ast_transformers | |
3069 |
|
3074 | |||
3070 | Parameters |
|
3075 | Parameters | |
3071 | ---------- |
|
3076 | ---------- | |
3072 | node : ast.Node |
|
3077 | node : ast.Node | |
3073 | The root node to be transformed. Typically called with the ast.Module |
|
3078 | The root node to be transformed. Typically called with the ast.Module | |
3074 | produced by parsing user input. |
|
3079 | produced by parsing user input. | |
3075 |
|
3080 | |||
3076 | Returns |
|
3081 | Returns | |
3077 | ------- |
|
3082 | ------- | |
3078 | An ast.Node corresponding to the node it was called with. Note that it |
|
3083 | An ast.Node corresponding to the node it was called with. Note that it | |
3079 | may also modify the passed object, so don't rely on references to the |
|
3084 | may also modify the passed object, so don't rely on references to the | |
3080 | original AST. |
|
3085 | original AST. | |
3081 | """ |
|
3086 | """ | |
3082 | for transformer in self.ast_transformers: |
|
3087 | for transformer in self.ast_transformers: | |
3083 | try: |
|
3088 | try: | |
3084 | node = transformer.visit(node) |
|
3089 | node = transformer.visit(node) | |
3085 | except InputRejected: |
|
3090 | except InputRejected: | |
3086 | # User-supplied AST transformers can reject an input by raising |
|
3091 | # User-supplied AST transformers can reject an input by raising | |
3087 | # an InputRejected. Short-circuit in this case so that we |
|
3092 | # an InputRejected. Short-circuit in this case so that we | |
3088 | # don't unregister the transform. |
|
3093 | # don't unregister the transform. | |
3089 | raise |
|
3094 | raise | |
3090 | except Exception: |
|
3095 | except Exception: | |
3091 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
3096 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) | |
3092 | self.ast_transformers.remove(transformer) |
|
3097 | self.ast_transformers.remove(transformer) | |
3093 |
|
3098 | |||
3094 | if self.ast_transformers: |
|
3099 | if self.ast_transformers: | |
3095 | ast.fix_missing_locations(node) |
|
3100 | ast.fix_missing_locations(node) | |
3096 | return node |
|
3101 | return node | |
3097 |
|
3102 | |||
3098 | async def run_ast_nodes( |
|
3103 | async def run_ast_nodes( | |
3099 | self, |
|
3104 | self, | |
3100 | nodelist: ListType[stmt], |
|
3105 | nodelist: ListType[stmt], | |
3101 | cell_name: str, |
|
3106 | cell_name: str, | |
3102 | interactivity="last_expr", |
|
3107 | interactivity="last_expr", | |
3103 | compiler=compile, |
|
3108 | compiler=compile, | |
3104 | result=None, |
|
3109 | result=None, | |
3105 | ): |
|
3110 | ): | |
3106 | """Run a sequence of AST nodes. The execution mode depends on the |
|
3111 | """Run a sequence of AST nodes. The execution mode depends on the | |
3107 | interactivity parameter. |
|
3112 | interactivity parameter. | |
3108 |
|
3113 | |||
3109 | Parameters |
|
3114 | Parameters | |
3110 | ---------- |
|
3115 | ---------- | |
3111 | nodelist : list |
|
3116 | nodelist : list | |
3112 | A sequence of AST nodes to run. |
|
3117 | A sequence of AST nodes to run. | |
3113 | cell_name : str |
|
3118 | cell_name : str | |
3114 | Will be passed to the compiler as the filename of the cell. Typically |
|
3119 | Will be passed to the compiler as the filename of the cell. Typically | |
3115 | the value returned by ip.compile.cache(cell). |
|
3120 | the value returned by ip.compile.cache(cell). | |
3116 | interactivity : str |
|
3121 | interactivity : str | |
3117 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', |
|
3122 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', | |
3118 | specifying which nodes should be run interactively (displaying output |
|
3123 | specifying which nodes should be run interactively (displaying output | |
3119 | from expressions). 'last_expr' will run the last node interactively |
|
3124 | from expressions). 'last_expr' will run the last node interactively | |
3120 | only if it is an expression (i.e. expressions in loops or other blocks |
|
3125 | only if it is an expression (i.e. expressions in loops or other blocks | |
3121 | are not displayed) 'last_expr_or_assign' will run the last expression |
|
3126 | are not displayed) 'last_expr_or_assign' will run the last expression | |
3122 | or the last assignment. Other values for this parameter will raise a |
|
3127 | or the last assignment. Other values for this parameter will raise a | |
3123 | ValueError. |
|
3128 | ValueError. | |
3124 |
|
3129 | |||
3125 | compiler : callable |
|
3130 | compiler : callable | |
3126 | A function with the same interface as the built-in compile(), to turn |
|
3131 | A function with the same interface as the built-in compile(), to turn | |
3127 | the AST nodes into code objects. Default is the built-in compile(). |
|
3132 | the AST nodes into code objects. Default is the built-in compile(). | |
3128 | result : ExecutionResult, optional |
|
3133 | result : ExecutionResult, optional | |
3129 | An object to store exceptions that occur during execution. |
|
3134 | An object to store exceptions that occur during execution. | |
3130 |
|
3135 | |||
3131 | Returns |
|
3136 | Returns | |
3132 | ------- |
|
3137 | ------- | |
3133 | True if an exception occurred while running code, False if it finished |
|
3138 | True if an exception occurred while running code, False if it finished | |
3134 | running. |
|
3139 | running. | |
3135 | """ |
|
3140 | """ | |
3136 | if not nodelist: |
|
3141 | if not nodelist: | |
3137 | return |
|
3142 | return | |
3138 |
|
3143 | |||
3139 |
|
3144 | |||
3140 | if interactivity == 'last_expr_or_assign': |
|
3145 | if interactivity == 'last_expr_or_assign': | |
3141 | if isinstance(nodelist[-1], _assign_nodes): |
|
3146 | if isinstance(nodelist[-1], _assign_nodes): | |
3142 | asg = nodelist[-1] |
|
3147 | asg = nodelist[-1] | |
3143 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: |
|
3148 | if isinstance(asg, ast.Assign) and len(asg.targets) == 1: | |
3144 | target = asg.targets[0] |
|
3149 | target = asg.targets[0] | |
3145 | elif isinstance(asg, _single_targets_nodes): |
|
3150 | elif isinstance(asg, _single_targets_nodes): | |
3146 | target = asg.target |
|
3151 | target = asg.target | |
3147 | else: |
|
3152 | else: | |
3148 | target = None |
|
3153 | target = None | |
3149 | if isinstance(target, ast.Name): |
|
3154 | if isinstance(target, ast.Name): | |
3150 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) |
|
3155 | nnode = ast.Expr(ast.Name(target.id, ast.Load())) | |
3151 | ast.fix_missing_locations(nnode) |
|
3156 | ast.fix_missing_locations(nnode) | |
3152 | nodelist.append(nnode) |
|
3157 | nodelist.append(nnode) | |
3153 | interactivity = 'last_expr' |
|
3158 | interactivity = 'last_expr' | |
3154 |
|
3159 | |||
3155 | _async = False |
|
3160 | _async = False | |
3156 | if interactivity == 'last_expr': |
|
3161 | if interactivity == 'last_expr': | |
3157 | if isinstance(nodelist[-1], ast.Expr): |
|
3162 | if isinstance(nodelist[-1], ast.Expr): | |
3158 | interactivity = "last" |
|
3163 | interactivity = "last" | |
3159 | else: |
|
3164 | else: | |
3160 | interactivity = "none" |
|
3165 | interactivity = "none" | |
3161 |
|
3166 | |||
3162 | if interactivity == 'none': |
|
3167 | if interactivity == 'none': | |
3163 | to_run_exec, to_run_interactive = nodelist, [] |
|
3168 | to_run_exec, to_run_interactive = nodelist, [] | |
3164 | elif interactivity == 'last': |
|
3169 | elif interactivity == 'last': | |
3165 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
3170 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] | |
3166 | elif interactivity == 'all': |
|
3171 | elif interactivity == 'all': | |
3167 | to_run_exec, to_run_interactive = [], nodelist |
|
3172 | to_run_exec, to_run_interactive = [], nodelist | |
3168 | else: |
|
3173 | else: | |
3169 | raise ValueError("Interactivity was %r" % interactivity) |
|
3174 | raise ValueError("Interactivity was %r" % interactivity) | |
3170 |
|
3175 | |||
3171 | try: |
|
3176 | try: | |
3172 |
|
3177 | |||
3173 | def compare(code): |
|
3178 | def compare(code): | |
3174 | is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE |
|
3179 | is_async = inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE | |
3175 | return is_async |
|
3180 | return is_async | |
3176 |
|
3181 | |||
3177 | # refactor that to just change the mod constructor. |
|
3182 | # refactor that to just change the mod constructor. | |
3178 | to_run = [] |
|
3183 | to_run = [] | |
3179 | for node in to_run_exec: |
|
3184 | for node in to_run_exec: | |
3180 | to_run.append((node, "exec")) |
|
3185 | to_run.append((node, "exec")) | |
3181 |
|
3186 | |||
3182 | for node in to_run_interactive: |
|
3187 | for node in to_run_interactive: | |
3183 | to_run.append((node, "single")) |
|
3188 | to_run.append((node, "single")) | |
3184 |
|
3189 | |||
3185 | for node, mode in to_run: |
|
3190 | for node, mode in to_run: | |
3186 | if mode == "exec": |
|
3191 | if mode == "exec": | |
3187 | mod = Module([node], []) |
|
3192 | mod = Module([node], []) | |
3188 | elif mode == "single": |
|
3193 | elif mode == "single": | |
3189 | mod = ast.Interactive([node]) |
|
3194 | mod = ast.Interactive([node]) | |
3190 | with compiler.extra_flags( |
|
3195 | with compiler.extra_flags( | |
3191 | getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) |
|
3196 | getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) | |
3192 | if self.autoawait |
|
3197 | if self.autoawait | |
3193 | else 0x0 |
|
3198 | else 0x0 | |
3194 | ): |
|
3199 | ): | |
3195 | code = compiler(mod, cell_name, mode) |
|
3200 | code = compiler(mod, cell_name, mode) | |
3196 | asy = compare(code) |
|
3201 | asy = compare(code) | |
3197 | if await self.run_code(code, result, async_=asy): |
|
3202 | if await self.run_code(code, result, async_=asy): | |
3198 | return True |
|
3203 | return True | |
3199 |
|
3204 | |||
3200 | # Flush softspace |
|
3205 | # Flush softspace | |
3201 | if softspace(sys.stdout, 0): |
|
3206 | if softspace(sys.stdout, 0): | |
3202 | print() |
|
3207 | print() | |
3203 |
|
3208 | |||
3204 | except: |
|
3209 | except: | |
3205 | # It's possible to have exceptions raised here, typically by |
|
3210 | # It's possible to have exceptions raised here, typically by | |
3206 | # compilation of odd code (such as a naked 'return' outside a |
|
3211 | # compilation of odd code (such as a naked 'return' outside a | |
3207 | # function) that did parse but isn't valid. Typically the exception |
|
3212 | # function) that did parse but isn't valid. Typically the exception | |
3208 | # is a SyntaxError, but it's safest just to catch anything and show |
|
3213 | # is a SyntaxError, but it's safest just to catch anything and show | |
3209 | # the user a traceback. |
|
3214 | # the user a traceback. | |
3210 |
|
3215 | |||
3211 | # We do only one try/except outside the loop to minimize the impact |
|
3216 | # We do only one try/except outside the loop to minimize the impact | |
3212 | # on runtime, and also because if any node in the node list is |
|
3217 | # on runtime, and also because if any node in the node list is | |
3213 | # broken, we should stop execution completely. |
|
3218 | # broken, we should stop execution completely. | |
3214 | if result: |
|
3219 | if result: | |
3215 | result.error_before_exec = sys.exc_info()[1] |
|
3220 | result.error_before_exec = sys.exc_info()[1] | |
3216 | self.showtraceback() |
|
3221 | self.showtraceback() | |
3217 | return True |
|
3222 | return True | |
3218 |
|
3223 | |||
3219 | return False |
|
3224 | return False | |
3220 |
|
3225 | |||
3221 | async def run_code(self, code_obj, result=None, *, async_=False): |
|
3226 | async def run_code(self, code_obj, result=None, *, async_=False): | |
3222 | """Execute a code object. |
|
3227 | """Execute a code object. | |
3223 |
|
3228 | |||
3224 | When an exception occurs, self.showtraceback() is called to display a |
|
3229 | When an exception occurs, self.showtraceback() is called to display a | |
3225 | traceback. |
|
3230 | traceback. | |
3226 |
|
3231 | |||
3227 | Parameters |
|
3232 | Parameters | |
3228 | ---------- |
|
3233 | ---------- | |
3229 | code_obj : code object |
|
3234 | code_obj : code object | |
3230 | A compiled code object, to be executed |
|
3235 | A compiled code object, to be executed | |
3231 | result : ExecutionResult, optional |
|
3236 | result : ExecutionResult, optional | |
3232 | An object to store exceptions that occur during execution. |
|
3237 | An object to store exceptions that occur during execution. | |
3233 | async_ : Bool (Experimental) |
|
3238 | async_ : Bool (Experimental) | |
3234 | Attempt to run top-level asynchronous code in a default loop. |
|
3239 | Attempt to run top-level asynchronous code in a default loop. | |
3235 |
|
3240 | |||
3236 | Returns |
|
3241 | Returns | |
3237 | ------- |
|
3242 | ------- | |
3238 | False : successful execution. |
|
3243 | False : successful execution. | |
3239 | True : an error occurred. |
|
3244 | True : an error occurred. | |
3240 | """ |
|
3245 | """ | |
3241 | # special value to say that anything above is IPython and should be |
|
3246 | # special value to say that anything above is IPython and should be | |
3242 | # hidden. |
|
3247 | # hidden. | |
3243 | __tracebackhide__ = "__ipython_bottom__" |
|
3248 | __tracebackhide__ = "__ipython_bottom__" | |
3244 | # Set our own excepthook in case the user code tries to call it |
|
3249 | # Set our own excepthook in case the user code tries to call it | |
3245 | # directly, so that the IPython crash handler doesn't get triggered |
|
3250 | # directly, so that the IPython crash handler doesn't get triggered | |
3246 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
3251 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook | |
3247 |
|
3252 | |||
3248 | # we save the original sys.excepthook in the instance, in case config |
|
3253 | # we save the original sys.excepthook in the instance, in case config | |
3249 | # code (such as magics) needs access to it. |
|
3254 | # code (such as magics) needs access to it. | |
3250 | self.sys_excepthook = old_excepthook |
|
3255 | self.sys_excepthook = old_excepthook | |
3251 | outflag = True # happens in more places, so it's easier as default |
|
3256 | outflag = True # happens in more places, so it's easier as default | |
3252 | try: |
|
3257 | try: | |
3253 | try: |
|
3258 | try: | |
3254 | self.hooks.pre_run_code_hook() |
|
3259 | self.hooks.pre_run_code_hook() | |
3255 | if async_: |
|
3260 | if async_: | |
3256 | await eval(code_obj, self.user_global_ns, self.user_ns) |
|
3261 | await eval(code_obj, self.user_global_ns, self.user_ns) | |
3257 | else: |
|
3262 | else: | |
3258 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
3263 | exec(code_obj, self.user_global_ns, self.user_ns) | |
3259 | finally: |
|
3264 | finally: | |
3260 | # Reset our crash handler in place |
|
3265 | # Reset our crash handler in place | |
3261 | sys.excepthook = old_excepthook |
|
3266 | sys.excepthook = old_excepthook | |
3262 | except SystemExit as e: |
|
3267 | except SystemExit as e: | |
3263 | if result is not None: |
|
3268 | if result is not None: | |
3264 | result.error_in_exec = e |
|
3269 | result.error_in_exec = e | |
3265 | self.showtraceback(exception_only=True) |
|
3270 | self.showtraceback(exception_only=True) | |
3266 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) |
|
3271 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) | |
3267 | except self.custom_exceptions: |
|
3272 | except self.custom_exceptions: | |
3268 | etype, value, tb = sys.exc_info() |
|
3273 | etype, value, tb = sys.exc_info() | |
3269 | if result is not None: |
|
3274 | if result is not None: | |
3270 | result.error_in_exec = value |
|
3275 | result.error_in_exec = value | |
3271 | self.CustomTB(etype, value, tb) |
|
3276 | self.CustomTB(etype, value, tb) | |
3272 | except: |
|
3277 | except: | |
3273 | if result is not None: |
|
3278 | if result is not None: | |
3274 | result.error_in_exec = sys.exc_info()[1] |
|
3279 | result.error_in_exec = sys.exc_info()[1] | |
3275 | self.showtraceback(running_compiled_code=True) |
|
3280 | self.showtraceback(running_compiled_code=True) | |
3276 | else: |
|
3281 | else: | |
3277 | outflag = False |
|
3282 | outflag = False | |
3278 | return outflag |
|
3283 | return outflag | |
3279 |
|
3284 | |||
3280 | # For backwards compatibility |
|
3285 | # For backwards compatibility | |
3281 | runcode = run_code |
|
3286 | runcode = run_code | |
3282 |
|
3287 | |||
3283 | def check_complete(self, code: str) -> Tuple[str, str]: |
|
3288 | def check_complete(self, code: str) -> Tuple[str, str]: | |
3284 | """Return whether a block of code is ready to execute, or should be continued |
|
3289 | """Return whether a block of code is ready to execute, or should be continued | |
3285 |
|
3290 | |||
3286 | Parameters |
|
3291 | Parameters | |
3287 | ---------- |
|
3292 | ---------- | |
3288 | source : string |
|
3293 | source : string | |
3289 | Python input code, which can be multiline. |
|
3294 | Python input code, which can be multiline. | |
3290 |
|
3295 | |||
3291 | Returns |
|
3296 | Returns | |
3292 | ------- |
|
3297 | ------- | |
3293 | status : str |
|
3298 | status : str | |
3294 | One of 'complete', 'incomplete', or 'invalid' if source is not a |
|
3299 | One of 'complete', 'incomplete', or 'invalid' if source is not a | |
3295 | prefix of valid code. |
|
3300 | prefix of valid code. | |
3296 | indent : str |
|
3301 | indent : str | |
3297 | When status is 'incomplete', this is some whitespace to insert on |
|
3302 | When status is 'incomplete', this is some whitespace to insert on | |
3298 | the next line of the prompt. |
|
3303 | the next line of the prompt. | |
3299 | """ |
|
3304 | """ | |
3300 | status, nspaces = self.input_transformer_manager.check_complete(code) |
|
3305 | status, nspaces = self.input_transformer_manager.check_complete(code) | |
3301 | return status, ' ' * (nspaces or 0) |
|
3306 | return status, ' ' * (nspaces or 0) | |
3302 |
|
3307 | |||
3303 | #------------------------------------------------------------------------- |
|
3308 | #------------------------------------------------------------------------- | |
3304 | # Things related to GUI support and pylab |
|
3309 | # Things related to GUI support and pylab | |
3305 | #------------------------------------------------------------------------- |
|
3310 | #------------------------------------------------------------------------- | |
3306 |
|
3311 | |||
3307 | active_eventloop = None |
|
3312 | active_eventloop = None | |
3308 |
|
3313 | |||
3309 | def enable_gui(self, gui=None): |
|
3314 | def enable_gui(self, gui=None): | |
3310 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
3315 | raise NotImplementedError('Implement enable_gui in a subclass') | |
3311 |
|
3316 | |||
3312 | def enable_matplotlib(self, gui=None): |
|
3317 | def enable_matplotlib(self, gui=None): | |
3313 | """Enable interactive matplotlib and inline figure support. |
|
3318 | """Enable interactive matplotlib and inline figure support. | |
3314 |
|
3319 | |||
3315 | This takes the following steps: |
|
3320 | This takes the following steps: | |
3316 |
|
3321 | |||
3317 | 1. select the appropriate eventloop and matplotlib backend |
|
3322 | 1. select the appropriate eventloop and matplotlib backend | |
3318 | 2. set up matplotlib for interactive use with that backend |
|
3323 | 2. set up matplotlib for interactive use with that backend | |
3319 | 3. configure formatters for inline figure display |
|
3324 | 3. configure formatters for inline figure display | |
3320 | 4. enable the selected gui eventloop |
|
3325 | 4. enable the selected gui eventloop | |
3321 |
|
3326 | |||
3322 | Parameters |
|
3327 | Parameters | |
3323 | ---------- |
|
3328 | ---------- | |
3324 | gui : optional, string |
|
3329 | gui : optional, string | |
3325 | If given, dictates the choice of matplotlib GUI backend to use |
|
3330 | If given, dictates the choice of matplotlib GUI backend to use | |
3326 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3331 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
3327 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3332 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
3328 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3333 | matplotlib (as dictated by the matplotlib build-time options plus the | |
3329 | user's matplotlibrc configuration file). Note that not all backends |
|
3334 | user's matplotlibrc configuration file). Note that not all backends | |
3330 | make sense in all contexts, for example a terminal ipython can't |
|
3335 | make sense in all contexts, for example a terminal ipython can't | |
3331 | display figures inline. |
|
3336 | display figures inline. | |
3332 | """ |
|
3337 | """ | |
3333 | from IPython.core import pylabtools as pt |
|
3338 | from IPython.core import pylabtools as pt | |
3334 | from matplotlib_inline.backend_inline import configure_inline_support |
|
3339 | from matplotlib_inline.backend_inline import configure_inline_support | |
3335 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
3340 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) | |
3336 |
|
3341 | |||
3337 | if gui != 'inline': |
|
3342 | if gui != 'inline': | |
3338 | # If we have our first gui selection, store it |
|
3343 | # If we have our first gui selection, store it | |
3339 | if self.pylab_gui_select is None: |
|
3344 | if self.pylab_gui_select is None: | |
3340 | self.pylab_gui_select = gui |
|
3345 | self.pylab_gui_select = gui | |
3341 | # Otherwise if they are different |
|
3346 | # Otherwise if they are different | |
3342 | elif gui != self.pylab_gui_select: |
|
3347 | elif gui != self.pylab_gui_select: | |
3343 | print('Warning: Cannot change to a different GUI toolkit: %s.' |
|
3348 | print('Warning: Cannot change to a different GUI toolkit: %s.' | |
3344 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
3349 | ' Using %s instead.' % (gui, self.pylab_gui_select)) | |
3345 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
3350 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) | |
3346 |
|
3351 | |||
3347 | pt.activate_matplotlib(backend) |
|
3352 | pt.activate_matplotlib(backend) | |
3348 | configure_inline_support(self, backend) |
|
3353 | configure_inline_support(self, backend) | |
3349 |
|
3354 | |||
3350 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
3355 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
3351 | # plot updates into account |
|
3356 | # plot updates into account | |
3352 | self.enable_gui(gui) |
|
3357 | self.enable_gui(gui) | |
3353 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
3358 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
3354 | pt.mpl_runner(self.safe_execfile) |
|
3359 | pt.mpl_runner(self.safe_execfile) | |
3355 |
|
3360 | |||
3356 | return gui, backend |
|
3361 | return gui, backend | |
3357 |
|
3362 | |||
3358 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
3363 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): | |
3359 | """Activate pylab support at runtime. |
|
3364 | """Activate pylab support at runtime. | |
3360 |
|
3365 | |||
3361 | This turns on support for matplotlib, preloads into the interactive |
|
3366 | This turns on support for matplotlib, preloads into the interactive | |
3362 | namespace all of numpy and pylab, and configures IPython to correctly |
|
3367 | namespace all of numpy and pylab, and configures IPython to correctly | |
3363 | interact with the GUI event loop. The GUI backend to be used can be |
|
3368 | interact with the GUI event loop. The GUI backend to be used can be | |
3364 | optionally selected with the optional ``gui`` argument. |
|
3369 | optionally selected with the optional ``gui`` argument. | |
3365 |
|
3370 | |||
3366 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
3371 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. | |
3367 |
|
3372 | |||
3368 | Parameters |
|
3373 | Parameters | |
3369 | ---------- |
|
3374 | ---------- | |
3370 | gui : optional, string |
|
3375 | gui : optional, string | |
3371 | If given, dictates the choice of matplotlib GUI backend to use |
|
3376 | If given, dictates the choice of matplotlib GUI backend to use | |
3372 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3377 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', | |
3373 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3378 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by | |
3374 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3379 | matplotlib (as dictated by the matplotlib build-time options plus the | |
3375 | user's matplotlibrc configuration file). Note that not all backends |
|
3380 | user's matplotlibrc configuration file). Note that not all backends | |
3376 | make sense in all contexts, for example a terminal ipython can't |
|
3381 | make sense in all contexts, for example a terminal ipython can't | |
3377 | display figures inline. |
|
3382 | display figures inline. | |
3378 | import_all : optional, bool, default: True |
|
3383 | import_all : optional, bool, default: True | |
3379 | Whether to do `from numpy import *` and `from pylab import *` |
|
3384 | Whether to do `from numpy import *` and `from pylab import *` | |
3380 | in addition to module imports. |
|
3385 | in addition to module imports. | |
3381 | welcome_message : deprecated |
|
3386 | welcome_message : deprecated | |
3382 | This argument is ignored, no welcome message will be displayed. |
|
3387 | This argument is ignored, no welcome message will be displayed. | |
3383 | """ |
|
3388 | """ | |
3384 | from IPython.core.pylabtools import import_pylab |
|
3389 | from IPython.core.pylabtools import import_pylab | |
3385 |
|
3390 | |||
3386 | gui, backend = self.enable_matplotlib(gui) |
|
3391 | gui, backend = self.enable_matplotlib(gui) | |
3387 |
|
3392 | |||
3388 | # We want to prevent the loading of pylab to pollute the user's |
|
3393 | # We want to prevent the loading of pylab to pollute the user's | |
3389 | # namespace as shown by the %who* magics, so we execute the activation |
|
3394 | # namespace as shown by the %who* magics, so we execute the activation | |
3390 | # code in an empty namespace, and we update *both* user_ns and |
|
3395 | # code in an empty namespace, and we update *both* user_ns and | |
3391 | # user_ns_hidden with this information. |
|
3396 | # user_ns_hidden with this information. | |
3392 | ns = {} |
|
3397 | ns = {} | |
3393 | import_pylab(ns, import_all) |
|
3398 | import_pylab(ns, import_all) | |
3394 | # warn about clobbered names |
|
3399 | # warn about clobbered names | |
3395 | ignored = {"__builtins__"} |
|
3400 | ignored = {"__builtins__"} | |
3396 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
3401 | both = set(ns).intersection(self.user_ns).difference(ignored) | |
3397 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
3402 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] | |
3398 | self.user_ns.update(ns) |
|
3403 | self.user_ns.update(ns) | |
3399 | self.user_ns_hidden.update(ns) |
|
3404 | self.user_ns_hidden.update(ns) | |
3400 | return gui, backend, clobbered |
|
3405 | return gui, backend, clobbered | |
3401 |
|
3406 | |||
3402 | #------------------------------------------------------------------------- |
|
3407 | #------------------------------------------------------------------------- | |
3403 | # Utilities |
|
3408 | # Utilities | |
3404 | #------------------------------------------------------------------------- |
|
3409 | #------------------------------------------------------------------------- | |
3405 |
|
3410 | |||
3406 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
3411 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): | |
3407 | """Expand python variables in a string. |
|
3412 | """Expand python variables in a string. | |
3408 |
|
3413 | |||
3409 | The depth argument indicates how many frames above the caller should |
|
3414 | The depth argument indicates how many frames above the caller should | |
3410 | be walked to look for the local namespace where to expand variables. |
|
3415 | be walked to look for the local namespace where to expand variables. | |
3411 |
|
3416 | |||
3412 | The global namespace for expansion is always the user's interactive |
|
3417 | The global namespace for expansion is always the user's interactive | |
3413 | namespace. |
|
3418 | namespace. | |
3414 | """ |
|
3419 | """ | |
3415 | ns = self.user_ns.copy() |
|
3420 | ns = self.user_ns.copy() | |
3416 | try: |
|
3421 | try: | |
3417 | frame = sys._getframe(depth+1) |
|
3422 | frame = sys._getframe(depth+1) | |
3418 | except ValueError: |
|
3423 | except ValueError: | |
3419 | # This is thrown if there aren't that many frames on the stack, |
|
3424 | # This is thrown if there aren't that many frames on the stack, | |
3420 | # e.g. if a script called run_line_magic() directly. |
|
3425 | # e.g. if a script called run_line_magic() directly. | |
3421 | pass |
|
3426 | pass | |
3422 | else: |
|
3427 | else: | |
3423 | ns.update(frame.f_locals) |
|
3428 | ns.update(frame.f_locals) | |
3424 |
|
3429 | |||
3425 | try: |
|
3430 | try: | |
3426 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3431 | # We have to use .vformat() here, because 'self' is a valid and common | |
3427 | # name, and expanding **ns for .format() would make it collide with |
|
3432 | # name, and expanding **ns for .format() would make it collide with | |
3428 | # the 'self' argument of the method. |
|
3433 | # the 'self' argument of the method. | |
3429 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3434 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) | |
3430 | except Exception: |
|
3435 | except Exception: | |
3431 | # if formatter couldn't format, just let it go untransformed |
|
3436 | # if formatter couldn't format, just let it go untransformed | |
3432 | pass |
|
3437 | pass | |
3433 | return cmd |
|
3438 | return cmd | |
3434 |
|
3439 | |||
3435 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3440 | def mktempfile(self, data=None, prefix='ipython_edit_'): | |
3436 | """Make a new tempfile and return its filename. |
|
3441 | """Make a new tempfile and return its filename. | |
3437 |
|
3442 | |||
3438 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3443 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), | |
3439 | but it registers the created filename internally so ipython cleans it up |
|
3444 | but it registers the created filename internally so ipython cleans it up | |
3440 | at exit time. |
|
3445 | at exit time. | |
3441 |
|
3446 | |||
3442 | Optional inputs: |
|
3447 | Optional inputs: | |
3443 |
|
3448 | |||
3444 | - data(None): if data is given, it gets written out to the temp file |
|
3449 | - data(None): if data is given, it gets written out to the temp file | |
3445 | immediately, and the file is closed again.""" |
|
3450 | immediately, and the file is closed again.""" | |
3446 |
|
3451 | |||
3447 | dir_path = Path(tempfile.mkdtemp(prefix=prefix)) |
|
3452 | dir_path = Path(tempfile.mkdtemp(prefix=prefix)) | |
3448 | self.tempdirs.append(dir_path) |
|
3453 | self.tempdirs.append(dir_path) | |
3449 |
|
3454 | |||
3450 | handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path)) |
|
3455 | handle, filename = tempfile.mkstemp(".py", prefix, dir=str(dir_path)) | |
3451 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3456 | os.close(handle) # On Windows, there can only be one open handle on a file | |
3452 |
|
3457 | |||
3453 | file_path = Path(filename) |
|
3458 | file_path = Path(filename) | |
3454 | self.tempfiles.append(file_path) |
|
3459 | self.tempfiles.append(file_path) | |
3455 |
|
3460 | |||
3456 | if data: |
|
3461 | if data: | |
3457 | file_path.write_text(data) |
|
3462 | file_path.write_text(data) | |
3458 | return filename |
|
3463 | return filename | |
3459 |
|
3464 | |||
3460 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3465 | def ask_yes_no(self, prompt, default=None, interrupt=None): | |
3461 | if self.quiet: |
|
3466 | if self.quiet: | |
3462 | return True |
|
3467 | return True | |
3463 | return ask_yes_no(prompt,default,interrupt) |
|
3468 | return ask_yes_no(prompt,default,interrupt) | |
3464 |
|
3469 | |||
3465 | def show_usage(self): |
|
3470 | def show_usage(self): | |
3466 | """Show a usage message""" |
|
3471 | """Show a usage message""" | |
3467 | page.page(IPython.core.usage.interactive_usage) |
|
3472 | page.page(IPython.core.usage.interactive_usage) | |
3468 |
|
3473 | |||
3469 | def extract_input_lines(self, range_str, raw=False): |
|
3474 | def extract_input_lines(self, range_str, raw=False): | |
3470 | """Return as a string a set of input history slices. |
|
3475 | """Return as a string a set of input history slices. | |
3471 |
|
3476 | |||
3472 | Parameters |
|
3477 | Parameters | |
3473 | ---------- |
|
3478 | ---------- | |
3474 | range_str : str |
|
3479 | range_str : str | |
3475 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3480 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |
3476 | since this function is for use by magic functions which get their |
|
3481 | since this function is for use by magic functions which get their | |
3477 | arguments as strings. The number before the / is the session |
|
3482 | arguments as strings. The number before the / is the session | |
3478 | number: ~n goes n back from the current session. |
|
3483 | number: ~n goes n back from the current session. | |
3479 |
|
3484 | |||
3480 | If empty string is given, returns history of current session |
|
3485 | If empty string is given, returns history of current session | |
3481 | without the last input. |
|
3486 | without the last input. | |
3482 |
|
3487 | |||
3483 | raw : bool, optional |
|
3488 | raw : bool, optional | |
3484 | By default, the processed input is used. If this is true, the raw |
|
3489 | By default, the processed input is used. If this is true, the raw | |
3485 | input history is used instead. |
|
3490 | input history is used instead. | |
3486 |
|
3491 | |||
3487 | Notes |
|
3492 | Notes | |
3488 | ----- |
|
3493 | ----- | |
3489 |
|
3494 | |||
3490 | Slices can be described with two notations: |
|
3495 | Slices can be described with two notations: | |
3491 |
|
3496 | |||
3492 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3497 | * ``N:M`` -> standard python form, means including items N...(M-1). | |
3493 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3498 | * ``N-M`` -> include items N..M (closed endpoint). | |
3494 | """ |
|
3499 | """ | |
3495 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3500 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3496 | text = "\n".join(x for _, _, x in lines) |
|
3501 | text = "\n".join(x for _, _, x in lines) | |
3497 |
|
3502 | |||
3498 | # Skip the last line, as it's probably the magic that called this |
|
3503 | # Skip the last line, as it's probably the magic that called this | |
3499 | if not range_str: |
|
3504 | if not range_str: | |
3500 | if "\n" not in text: |
|
3505 | if "\n" not in text: | |
3501 | text = "" |
|
3506 | text = "" | |
3502 | else: |
|
3507 | else: | |
3503 | text = text[: text.rfind("\n")] |
|
3508 | text = text[: text.rfind("\n")] | |
3504 |
|
3509 | |||
3505 | return text |
|
3510 | return text | |
3506 |
|
3511 | |||
3507 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3512 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3508 | """Get a code string from history, file, url, or a string or macro. |
|
3513 | """Get a code string from history, file, url, or a string or macro. | |
3509 |
|
3514 | |||
3510 | This is mainly used by magic functions. |
|
3515 | This is mainly used by magic functions. | |
3511 |
|
3516 | |||
3512 | Parameters |
|
3517 | Parameters | |
3513 | ---------- |
|
3518 | ---------- | |
3514 | target : str |
|
3519 | target : str | |
3515 | A string specifying code to retrieve. This will be tried respectively |
|
3520 | A string specifying code to retrieve. This will be tried respectively | |
3516 | as: ranges of input history (see %history for syntax), url, |
|
3521 | as: ranges of input history (see %history for syntax), url, | |
3517 | corresponding .py file, filename, or an expression evaluating to a |
|
3522 | corresponding .py file, filename, or an expression evaluating to a | |
3518 | string or Macro in the user namespace. |
|
3523 | string or Macro in the user namespace. | |
3519 |
|
3524 | |||
3520 | If empty string is given, returns complete history of current |
|
3525 | If empty string is given, returns complete history of current | |
3521 | session, without the last line. |
|
3526 | session, without the last line. | |
3522 |
|
3527 | |||
3523 | raw : bool |
|
3528 | raw : bool | |
3524 | If true (default), retrieve raw history. Has no effect on the other |
|
3529 | If true (default), retrieve raw history. Has no effect on the other | |
3525 | retrieval mechanisms. |
|
3530 | retrieval mechanisms. | |
3526 |
|
3531 | |||
3527 | py_only : bool (default False) |
|
3532 | py_only : bool (default False) | |
3528 | Only try to fetch python code, do not try alternative methods to decode file |
|
3533 | Only try to fetch python code, do not try alternative methods to decode file | |
3529 | if unicode fails. |
|
3534 | if unicode fails. | |
3530 |
|
3535 | |||
3531 | Returns |
|
3536 | Returns | |
3532 | ------- |
|
3537 | ------- | |
3533 | A string of code. |
|
3538 | A string of code. | |
3534 |
|
3539 | |||
3535 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3540 | ValueError is raised if nothing is found, and TypeError if it evaluates | |
3536 | to an object of another type. In each case, .args[0] is a printable |
|
3541 | to an object of another type. In each case, .args[0] is a printable | |
3537 | message. |
|
3542 | message. | |
3538 | """ |
|
3543 | """ | |
3539 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3544 | code = self.extract_input_lines(target, raw=raw) # Grab history | |
3540 | if code: |
|
3545 | if code: | |
3541 | return code |
|
3546 | return code | |
3542 | try: |
|
3547 | try: | |
3543 | if target.startswith(('http://', 'https://')): |
|
3548 | if target.startswith(('http://', 'https://')): | |
3544 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) |
|
3549 | return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) | |
3545 | except UnicodeDecodeError as e: |
|
3550 | except UnicodeDecodeError as e: | |
3546 | if not py_only : |
|
3551 | if not py_only : | |
3547 | # Deferred import |
|
3552 | # Deferred import | |
3548 | from urllib.request import urlopen |
|
3553 | from urllib.request import urlopen | |
3549 | response = urlopen(target) |
|
3554 | response = urlopen(target) | |
3550 | return response.read().decode('latin1') |
|
3555 | return response.read().decode('latin1') | |
3551 | raise ValueError(("'%s' seem to be unreadable.") % target) from e |
|
3556 | raise ValueError(("'%s' seem to be unreadable.") % target) from e | |
3552 |
|
3557 | |||
3553 | potential_target = [target] |
|
3558 | potential_target = [target] | |
3554 | try : |
|
3559 | try : | |
3555 | potential_target.insert(0,get_py_filename(target)) |
|
3560 | potential_target.insert(0,get_py_filename(target)) | |
3556 | except IOError: |
|
3561 | except IOError: | |
3557 | pass |
|
3562 | pass | |
3558 |
|
3563 | |||
3559 | for tgt in potential_target : |
|
3564 | for tgt in potential_target : | |
3560 | if os.path.isfile(tgt): # Read file |
|
3565 | if os.path.isfile(tgt): # Read file | |
3561 | try : |
|
3566 | try : | |
3562 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3567 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) | |
3563 | except UnicodeDecodeError as e: |
|
3568 | except UnicodeDecodeError as e: | |
3564 | if not py_only : |
|
3569 | if not py_only : | |
3565 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3570 | with io_open(tgt,'r', encoding='latin1') as f : | |
3566 | return f.read() |
|
3571 | return f.read() | |
3567 | raise ValueError(("'%s' seem to be unreadable.") % target) from e |
|
3572 | raise ValueError(("'%s' seem to be unreadable.") % target) from e | |
3568 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3573 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3569 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3574 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3570 |
|
3575 | |||
3571 | if search_ns: |
|
3576 | if search_ns: | |
3572 | # Inspect namespace to load object source |
|
3577 | # Inspect namespace to load object source | |
3573 | object_info = self.object_inspect(target, detail_level=1) |
|
3578 | object_info = self.object_inspect(target, detail_level=1) | |
3574 | if object_info['found'] and object_info['source']: |
|
3579 | if object_info['found'] and object_info['source']: | |
3575 | return object_info['source'] |
|
3580 | return object_info['source'] | |
3576 |
|
3581 | |||
3577 | try: # User namespace |
|
3582 | try: # User namespace | |
3578 | codeobj = eval(target, self.user_ns) |
|
3583 | codeobj = eval(target, self.user_ns) | |
3579 | except Exception as e: |
|
3584 | except Exception as e: | |
3580 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3585 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3581 | "nor in the user namespace.") % target) from e |
|
3586 | "nor in the user namespace.") % target) from e | |
3582 |
|
3587 | |||
3583 | if isinstance(codeobj, str): |
|
3588 | if isinstance(codeobj, str): | |
3584 | return codeobj |
|
3589 | return codeobj | |
3585 | elif isinstance(codeobj, Macro): |
|
3590 | elif isinstance(codeobj, Macro): | |
3586 | return codeobj.value |
|
3591 | return codeobj.value | |
3587 |
|
3592 | |||
3588 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3593 | raise TypeError("%s is neither a string nor a macro." % target, | |
3589 | codeobj) |
|
3594 | codeobj) | |
3590 |
|
3595 | |||
3591 | def _atexit_once(self): |
|
3596 | def _atexit_once(self): | |
3592 | """ |
|
3597 | """ | |
3593 | At exist operation that need to be called at most once. |
|
3598 | At exist operation that need to be called at most once. | |
3594 | Second call to this function per instance will do nothing. |
|
3599 | Second call to this function per instance will do nothing. | |
3595 | """ |
|
3600 | """ | |
3596 |
|
3601 | |||
3597 | if not getattr(self, "_atexit_once_called", False): |
|
3602 | if not getattr(self, "_atexit_once_called", False): | |
3598 | self._atexit_once_called = True |
|
3603 | self._atexit_once_called = True | |
3599 | # Clear all user namespaces to release all references cleanly. |
|
3604 | # Clear all user namespaces to release all references cleanly. | |
3600 | self.reset(new_session=False) |
|
3605 | self.reset(new_session=False) | |
3601 | # Close the history session (this stores the end time and line count) |
|
3606 | # Close the history session (this stores the end time and line count) | |
3602 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3607 | # this must be *before* the tempfile cleanup, in case of temporary | |
3603 | # history db |
|
3608 | # history db | |
3604 | self.history_manager.end_session() |
|
3609 | self.history_manager.end_session() | |
3605 | self.history_manager = None |
|
3610 | self.history_manager = None | |
3606 |
|
3611 | |||
3607 | #------------------------------------------------------------------------- |
|
3612 | #------------------------------------------------------------------------- | |
3608 | # Things related to IPython exiting |
|
3613 | # Things related to IPython exiting | |
3609 | #------------------------------------------------------------------------- |
|
3614 | #------------------------------------------------------------------------- | |
3610 | def atexit_operations(self): |
|
3615 | def atexit_operations(self): | |
3611 | """This will be executed at the time of exit. |
|
3616 | """This will be executed at the time of exit. | |
3612 |
|
3617 | |||
3613 | Cleanup operations and saving of persistent data that is done |
|
3618 | Cleanup operations and saving of persistent data that is done | |
3614 | unconditionally by IPython should be performed here. |
|
3619 | unconditionally by IPython should be performed here. | |
3615 |
|
3620 | |||
3616 | For things that may depend on startup flags or platform specifics (such |
|
3621 | For things that may depend on startup flags or platform specifics (such | |
3617 | as having readline or not), register a separate atexit function in the |
|
3622 | as having readline or not), register a separate atexit function in the | |
3618 | code that has the appropriate information, rather than trying to |
|
3623 | code that has the appropriate information, rather than trying to | |
3619 | clutter |
|
3624 | clutter | |
3620 | """ |
|
3625 | """ | |
3621 | self._atexit_once() |
|
3626 | self._atexit_once() | |
3622 |
|
3627 | |||
3623 | # Cleanup all tempfiles and folders left around |
|
3628 | # Cleanup all tempfiles and folders left around | |
3624 | for tfile in self.tempfiles: |
|
3629 | for tfile in self.tempfiles: | |
3625 | try: |
|
3630 | try: | |
3626 | tfile.unlink() |
|
3631 | tfile.unlink() | |
3627 | self.tempfiles.remove(tfile) |
|
3632 | self.tempfiles.remove(tfile) | |
3628 | except FileNotFoundError: |
|
3633 | except FileNotFoundError: | |
3629 | pass |
|
3634 | pass | |
3630 | del self.tempfiles |
|
3635 | del self.tempfiles | |
3631 | for tdir in self.tempdirs: |
|
3636 | for tdir in self.tempdirs: | |
3632 | try: |
|
3637 | try: | |
3633 | tdir.rmdir() |
|
3638 | tdir.rmdir() | |
3634 | self.tempdirs.remove(tdir) |
|
3639 | self.tempdirs.remove(tdir) | |
3635 | except FileNotFoundError: |
|
3640 | except FileNotFoundError: | |
3636 | pass |
|
3641 | pass | |
3637 | del self.tempdirs |
|
3642 | del self.tempdirs | |
3638 |
|
3643 | |||
3639 |
|
3644 | |||
3640 | # Run user hooks |
|
3645 | # Run user hooks | |
3641 | self.hooks.shutdown_hook() |
|
3646 | self.hooks.shutdown_hook() | |
3642 |
|
3647 | |||
3643 | def cleanup(self): |
|
3648 | def cleanup(self): | |
3644 | self.restore_sys_module_state() |
|
3649 | self.restore_sys_module_state() | |
3645 |
|
3650 | |||
3646 |
|
3651 | |||
3647 | # Overridden in terminal subclass to change prompts |
|
3652 | # Overridden in terminal subclass to change prompts | |
3648 | def switch_doctest_mode(self, mode): |
|
3653 | def switch_doctest_mode(self, mode): | |
3649 | pass |
|
3654 | pass | |
3650 |
|
3655 | |||
3651 |
|
3656 | |||
3652 | class InteractiveShellABC(metaclass=abc.ABCMeta): |
|
3657 | class InteractiveShellABC(metaclass=abc.ABCMeta): | |
3653 | """An abstract base class for InteractiveShell.""" |
|
3658 | """An abstract base class for InteractiveShell.""" | |
3654 |
|
3659 | |||
3655 | InteractiveShellABC.register(InteractiveShell) |
|
3660 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,470 +1,452 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | A mixin for :class:`~IPython.core.application.Application` classes that |
|
3 | A mixin for :class:`~IPython.core.application.Application` classes that | |
4 | launch InteractiveShell instances, load extensions, etc. |
|
4 | launch InteractiveShell instances, load extensions, etc. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | # Copyright (c) IPython Development Team. |
|
7 | # Copyright (c) IPython Development Team. | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 |
|
9 | |||
10 | import glob |
|
10 | import glob | |
11 | from itertools import chain |
|
11 | from itertools import chain | |
12 | import os |
|
12 | import os | |
13 | import sys |
|
13 | import sys | |
14 |
|
14 | |||
15 | from traitlets.config.application import boolean_flag |
|
15 | from traitlets.config.application import boolean_flag | |
16 | from traitlets.config.configurable import Configurable |
|
16 | from traitlets.config.configurable import Configurable | |
17 | from traitlets.config.loader import Config |
|
17 | from traitlets.config.loader import Config | |
18 | from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS |
|
18 | from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS | |
19 | from IPython.core import pylabtools |
|
19 | from IPython.core import pylabtools | |
20 | from IPython.utils.contexts import preserve_keys |
|
20 | from IPython.utils.contexts import preserve_keys | |
21 | from IPython.utils.path import filefind |
|
21 | from IPython.utils.path import filefind | |
22 | import traitlets |
|
22 | import traitlets | |
23 | from traitlets import ( |
|
23 | from traitlets import ( | |
24 | Unicode, Instance, List, Bool, CaselessStrEnum, observe, |
|
24 | Unicode, Instance, List, Bool, CaselessStrEnum, observe, | |
25 | DottedObjectName, |
|
25 | DottedObjectName, | |
26 | ) |
|
26 | ) | |
27 | from IPython.terminal import pt_inputhooks |
|
27 | from IPython.terminal import pt_inputhooks | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Aliases and Flags |
|
30 | # Aliases and Flags | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases)) |
|
33 | gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases)) | |
34 |
|
34 | |||
35 | backend_keys = sorted(pylabtools.backends.keys()) |
|
35 | backend_keys = sorted(pylabtools.backends.keys()) | |
36 | backend_keys.insert(0, 'auto') |
|
36 | backend_keys.insert(0, 'auto') | |
37 |
|
37 | |||
38 | shell_flags = {} |
|
38 | shell_flags = {} | |
39 |
|
39 | |||
40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) |
|
40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) | |
41 | addflag('autoindent', 'InteractiveShell.autoindent', |
|
41 | addflag('autoindent', 'InteractiveShell.autoindent', | |
42 | 'Turn on autoindenting.', 'Turn off autoindenting.' |
|
42 | 'Turn on autoindenting.', 'Turn off autoindenting.' | |
43 | ) |
|
43 | ) | |
44 | addflag('automagic', 'InteractiveShell.automagic', |
|
44 | addflag('automagic', 'InteractiveShell.automagic', | |
45 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
45 | """Turn on the auto calling of magic commands. Type %%magic at the | |
46 | IPython prompt for more information.""", |
|
46 | IPython prompt for more information.""", | |
47 | 'Turn off the auto calling of magic commands.' |
|
47 | 'Turn off the auto calling of magic commands.' | |
48 | ) |
|
48 | ) | |
49 | addflag('pdb', 'InteractiveShell.pdb', |
|
49 | addflag('pdb', 'InteractiveShell.pdb', | |
50 | "Enable auto calling the pdb debugger after every exception.", |
|
50 | "Enable auto calling the pdb debugger after every exception.", | |
51 | "Disable auto calling the pdb debugger after every exception." |
|
51 | "Disable auto calling the pdb debugger after every exception." | |
52 | ) |
|
52 | ) | |
53 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
53 | addflag('pprint', 'PlainTextFormatter.pprint', | |
54 | "Enable auto pretty printing of results.", |
|
54 | "Enable auto pretty printing of results.", | |
55 | "Disable auto pretty printing of results." |
|
55 | "Disable auto pretty printing of results." | |
56 | ) |
|
56 | ) | |
57 | addflag('color-info', 'InteractiveShell.color_info', |
|
57 | addflag('color-info', 'InteractiveShell.color_info', | |
58 | """IPython can display information about objects via a set of functions, |
|
58 | """IPython can display information about objects via a set of functions, | |
59 | and optionally can use colors for this, syntax highlighting |
|
59 | and optionally can use colors for this, syntax highlighting | |
60 | source code and various other elements. This is on by default, but can cause |
|
60 | source code and various other elements. This is on by default, but can cause | |
61 | problems with some pagers. If you see such problems, you can disable the |
|
61 | problems with some pagers. If you see such problems, you can disable the | |
62 | colours.""", |
|
62 | colours.""", | |
63 | "Disable using colors for info related things." |
|
63 | "Disable using colors for info related things." | |
64 | ) |
|
64 | ) | |
65 | addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd', |
|
65 | addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd', | |
66 | "Exclude the current working directory from sys.path", |
|
66 | "Exclude the current working directory from sys.path", | |
67 | "Include the current working directory in sys.path", |
|
67 | "Include the current working directory in sys.path", | |
68 | ) |
|
68 | ) | |
69 | nosep_config = Config() |
|
69 | nosep_config = Config() | |
70 | nosep_config.InteractiveShell.separate_in = '' |
|
70 | nosep_config.InteractiveShell.separate_in = '' | |
71 | nosep_config.InteractiveShell.separate_out = '' |
|
71 | nosep_config.InteractiveShell.separate_out = '' | |
72 | nosep_config.InteractiveShell.separate_out2 = '' |
|
72 | nosep_config.InteractiveShell.separate_out2 = '' | |
73 |
|
73 | |||
74 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
74 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") | |
75 | shell_flags['pylab'] = ( |
|
75 | shell_flags['pylab'] = ( | |
76 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, |
|
76 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, | |
77 | """Pre-load matplotlib and numpy for interactive use with |
|
77 | """Pre-load matplotlib and numpy for interactive use with | |
78 | the default matplotlib backend.""" |
|
78 | the default matplotlib backend.""" | |
79 | ) |
|
79 | ) | |
80 | shell_flags['matplotlib'] = ( |
|
80 | shell_flags['matplotlib'] = ( | |
81 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, |
|
81 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, | |
82 | """Configure matplotlib for interactive use with |
|
82 | """Configure matplotlib for interactive use with | |
83 | the default matplotlib backend.""" |
|
83 | the default matplotlib backend.""" | |
84 | ) |
|
84 | ) | |
85 |
|
85 | |||
86 | # it's possible we don't want short aliases for *all* of these: |
|
86 | # it's possible we don't want short aliases for *all* of these: | |
87 | shell_aliases = dict( |
|
87 | shell_aliases = dict( | |
88 | autocall='InteractiveShell.autocall', |
|
88 | autocall='InteractiveShell.autocall', | |
89 | colors='InteractiveShell.colors', |
|
89 | colors='InteractiveShell.colors', | |
90 | logfile='InteractiveShell.logfile', |
|
90 | logfile='InteractiveShell.logfile', | |
91 | logappend='InteractiveShell.logappend', |
|
91 | logappend='InteractiveShell.logappend', | |
92 | c='InteractiveShellApp.code_to_run', |
|
92 | c='InteractiveShellApp.code_to_run', | |
93 | m='InteractiveShellApp.module_to_run', |
|
93 | m='InteractiveShellApp.module_to_run', | |
94 | ext="InteractiveShellApp.extra_extensions", |
|
94 | ext="InteractiveShellApp.extra_extensions", | |
95 | gui='InteractiveShellApp.gui', |
|
95 | gui='InteractiveShellApp.gui', | |
96 | pylab='InteractiveShellApp.pylab', |
|
96 | pylab='InteractiveShellApp.pylab', | |
97 | matplotlib='InteractiveShellApp.matplotlib', |
|
97 | matplotlib='InteractiveShellApp.matplotlib', | |
98 | ) |
|
98 | ) | |
99 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' |
|
99 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' | |
100 |
|
100 | |||
101 | if traitlets.version_info < (5, 0): |
|
|||
102 | # traitlets 4 doesn't handle lists on CLI |
|
|||
103 | shell_aliases["ext"] = "InteractiveShellApp.extra_extension" |
|
|||
104 |
|
||||
105 |
|
||||
106 | #----------------------------------------------------------------------------- |
|
101 | #----------------------------------------------------------------------------- | |
107 | # Main classes and functions |
|
102 | # Main classes and functions | |
108 | #----------------------------------------------------------------------------- |
|
103 | #----------------------------------------------------------------------------- | |
109 |
|
104 | |||
110 | class InteractiveShellApp(Configurable): |
|
105 | class InteractiveShellApp(Configurable): | |
111 | """A Mixin for applications that start InteractiveShell instances. |
|
106 | """A Mixin for applications that start InteractiveShell instances. | |
112 |
|
107 | |||
113 | Provides configurables for loading extensions and executing files |
|
108 | Provides configurables for loading extensions and executing files | |
114 | as part of configuring a Shell environment. |
|
109 | as part of configuring a Shell environment. | |
115 |
|
110 | |||
116 | The following methods should be called by the :meth:`initialize` method |
|
111 | The following methods should be called by the :meth:`initialize` method | |
117 | of the subclass: |
|
112 | of the subclass: | |
118 |
|
113 | |||
119 | - :meth:`init_path` |
|
114 | - :meth:`init_path` | |
120 | - :meth:`init_shell` (to be implemented by the subclass) |
|
115 | - :meth:`init_shell` (to be implemented by the subclass) | |
121 | - :meth:`init_gui_pylab` |
|
116 | - :meth:`init_gui_pylab` | |
122 | - :meth:`init_extensions` |
|
117 | - :meth:`init_extensions` | |
123 | - :meth:`init_code` |
|
118 | - :meth:`init_code` | |
124 | """ |
|
119 | """ | |
125 | extensions = List(Unicode(), |
|
120 | extensions = List(Unicode(), | |
126 | help="A list of dotted module names of IPython extensions to load." |
|
121 | help="A list of dotted module names of IPython extensions to load." | |
127 | ).tag(config=True) |
|
122 | ).tag(config=True) | |
128 |
|
123 | |||
129 | extra_extension = Unicode( |
|
|||
130 | "", |
|
|||
131 | help=""" |
|
|||
132 | DEPRECATED. Dotted module name of a single extra IPython extension to load. |
|
|||
133 |
|
||||
134 | Only one extension can be added this way. |
|
|||
135 |
|
||||
136 | Only used with traitlets < 5.0, plural extra_extensions list is used in traitlets 5. |
|
|||
137 | """, |
|
|||
138 | ).tag(config=True) |
|
|||
139 |
|
||||
140 | extra_extensions = List( |
|
124 | extra_extensions = List( | |
141 | DottedObjectName(), |
|
125 | DottedObjectName(), | |
142 | help=""" |
|
126 | help=""" | |
143 | Dotted module name(s) of one or more IPython extensions to load. |
|
127 | Dotted module name(s) of one or more IPython extensions to load. | |
144 |
|
128 | |||
145 | For specifying extra extensions to load on the command-line. |
|
129 | For specifying extra extensions to load on the command-line. | |
146 |
|
130 | |||
147 | .. versionadded:: 7.10 |
|
131 | .. versionadded:: 7.10 | |
148 | """, |
|
132 | """, | |
149 | ).tag(config=True) |
|
133 | ).tag(config=True) | |
150 |
|
134 | |||
151 | reraise_ipython_extension_failures = Bool(False, |
|
135 | reraise_ipython_extension_failures = Bool(False, | |
152 | help="Reraise exceptions encountered loading IPython extensions?", |
|
136 | help="Reraise exceptions encountered loading IPython extensions?", | |
153 | ).tag(config=True) |
|
137 | ).tag(config=True) | |
154 |
|
138 | |||
155 | # Extensions that are always loaded (not configurable) |
|
139 | # Extensions that are always loaded (not configurable) | |
156 | default_extensions = List(Unicode(), [u'storemagic']).tag(config=False) |
|
140 | default_extensions = List(Unicode(), [u'storemagic']).tag(config=False) | |
157 |
|
141 | |||
158 | hide_initial_ns = Bool(True, |
|
142 | hide_initial_ns = Bool(True, | |
159 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) |
|
143 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) | |
160 | be hidden from tools like %who?""" |
|
144 | be hidden from tools like %who?""" | |
161 | ).tag(config=True) |
|
145 | ).tag(config=True) | |
162 |
|
146 | |||
163 | exec_files = List(Unicode(), |
|
147 | exec_files = List(Unicode(), | |
164 | help="""List of files to run at IPython startup.""" |
|
148 | help="""List of files to run at IPython startup.""" | |
165 | ).tag(config=True) |
|
149 | ).tag(config=True) | |
166 | exec_PYTHONSTARTUP = Bool(True, |
|
150 | exec_PYTHONSTARTUP = Bool(True, | |
167 | help="""Run the file referenced by the PYTHONSTARTUP environment |
|
151 | help="""Run the file referenced by the PYTHONSTARTUP environment | |
168 | variable at IPython startup.""" |
|
152 | variable at IPython startup.""" | |
169 | ).tag(config=True) |
|
153 | ).tag(config=True) | |
170 | file_to_run = Unicode('', |
|
154 | file_to_run = Unicode('', | |
171 | help="""A file to be run""").tag(config=True) |
|
155 | help="""A file to be run""").tag(config=True) | |
172 |
|
156 | |||
173 | exec_lines = List(Unicode(), |
|
157 | exec_lines = List(Unicode(), | |
174 | help="""lines of code to run at IPython startup.""" |
|
158 | help="""lines of code to run at IPython startup.""" | |
175 | ).tag(config=True) |
|
159 | ).tag(config=True) | |
176 | code_to_run = Unicode('', |
|
160 | code_to_run = Unicode('', | |
177 | help="Execute the given command string." |
|
161 | help="Execute the given command string." | |
178 | ).tag(config=True) |
|
162 | ).tag(config=True) | |
179 | module_to_run = Unicode('', |
|
163 | module_to_run = Unicode('', | |
180 | help="Run the module as a script." |
|
164 | help="Run the module as a script." | |
181 | ).tag(config=True) |
|
165 | ).tag(config=True) | |
182 | gui = CaselessStrEnum(gui_keys, allow_none=True, |
|
166 | gui = CaselessStrEnum(gui_keys, allow_none=True, | |
183 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) |
|
167 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) | |
184 | ).tag(config=True) |
|
168 | ).tag(config=True) | |
185 | matplotlib = CaselessStrEnum(backend_keys, allow_none=True, |
|
169 | matplotlib = CaselessStrEnum(backend_keys, allow_none=True, | |
186 | help="""Configure matplotlib for interactive use with |
|
170 | help="""Configure matplotlib for interactive use with | |
187 | the default matplotlib backend.""" |
|
171 | the default matplotlib backend.""" | |
188 | ).tag(config=True) |
|
172 | ).tag(config=True) | |
189 | pylab = CaselessStrEnum(backend_keys, allow_none=True, |
|
173 | pylab = CaselessStrEnum(backend_keys, allow_none=True, | |
190 | help="""Pre-load matplotlib and numpy for interactive use, |
|
174 | help="""Pre-load matplotlib and numpy for interactive use, | |
191 | selecting a particular matplotlib backend and loop integration. |
|
175 | selecting a particular matplotlib backend and loop integration. | |
192 | """ |
|
176 | """ | |
193 | ).tag(config=True) |
|
177 | ).tag(config=True) | |
194 | pylab_import_all = Bool(True, |
|
178 | pylab_import_all = Bool(True, | |
195 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. |
|
179 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. | |
196 | and an ``import *`` is done from numpy and pylab, when using pylab mode. |
|
180 | and an ``import *`` is done from numpy and pylab, when using pylab mode. | |
197 |
|
181 | |||
198 | When False, pylab mode should not import any names into the user namespace. |
|
182 | When False, pylab mode should not import any names into the user namespace. | |
199 | """ |
|
183 | """ | |
200 | ).tag(config=True) |
|
184 | ).tag(config=True) | |
201 | ignore_cwd = Bool( |
|
185 | ignore_cwd = Bool( | |
202 | False, |
|
186 | False, | |
203 | help="""If True, IPython will not add the current working directory to sys.path. |
|
187 | help="""If True, IPython will not add the current working directory to sys.path. | |
204 | When False, the current working directory is added to sys.path, allowing imports |
|
188 | When False, the current working directory is added to sys.path, allowing imports | |
205 | of modules defined in the current directory.""" |
|
189 | of modules defined in the current directory.""" | |
206 | ).tag(config=True) |
|
190 | ).tag(config=True) | |
207 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
191 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', | |
208 | allow_none=True) |
|
192 | allow_none=True) | |
209 | # whether interact-loop should start |
|
193 | # whether interact-loop should start | |
210 | interact = Bool(True) |
|
194 | interact = Bool(True) | |
211 |
|
195 | |||
212 | user_ns = Instance(dict, args=None, allow_none=True) |
|
196 | user_ns = Instance(dict, args=None, allow_none=True) | |
213 | @observe('user_ns') |
|
197 | @observe('user_ns') | |
214 | def _user_ns_changed(self, change): |
|
198 | def _user_ns_changed(self, change): | |
215 | if self.shell is not None: |
|
199 | if self.shell is not None: | |
216 | self.shell.user_ns = change['new'] |
|
200 | self.shell.user_ns = change['new'] | |
217 | self.shell.init_user_ns() |
|
201 | self.shell.init_user_ns() | |
218 |
|
202 | |||
219 | def init_path(self): |
|
203 | def init_path(self): | |
220 | """Add current working directory, '', to sys.path |
|
204 | """Add current working directory, '', to sys.path | |
221 |
|
205 | |||
222 | Unlike Python's default, we insert before the first `site-packages` |
|
206 | Unlike Python's default, we insert before the first `site-packages` | |
223 | or `dist-packages` directory, |
|
207 | or `dist-packages` directory, | |
224 | so that it is after the standard library. |
|
208 | so that it is after the standard library. | |
225 |
|
209 | |||
226 | .. versionchanged:: 7.2 |
|
210 | .. versionchanged:: 7.2 | |
227 | Try to insert after the standard library, instead of first. |
|
211 | Try to insert after the standard library, instead of first. | |
228 | .. versionchanged:: 8.0 |
|
212 | .. versionchanged:: 8.0 | |
229 | Allow optionally not including the current directory in sys.path |
|
213 | Allow optionally not including the current directory in sys.path | |
230 | """ |
|
214 | """ | |
231 | if '' in sys.path or self.ignore_cwd: |
|
215 | if '' in sys.path or self.ignore_cwd: | |
232 | return |
|
216 | return | |
233 | for idx, path in enumerate(sys.path): |
|
217 | for idx, path in enumerate(sys.path): | |
234 | parent, last_part = os.path.split(path) |
|
218 | parent, last_part = os.path.split(path) | |
235 | if last_part in {'site-packages', 'dist-packages'}: |
|
219 | if last_part in {'site-packages', 'dist-packages'}: | |
236 | break |
|
220 | break | |
237 | else: |
|
221 | else: | |
238 | # no site-packages or dist-packages found (?!) |
|
222 | # no site-packages or dist-packages found (?!) | |
239 | # back to original behavior of inserting at the front |
|
223 | # back to original behavior of inserting at the front | |
240 | idx = 0 |
|
224 | idx = 0 | |
241 | sys.path.insert(idx, '') |
|
225 | sys.path.insert(idx, '') | |
242 |
|
226 | |||
243 | def init_shell(self): |
|
227 | def init_shell(self): | |
244 | raise NotImplementedError("Override in subclasses") |
|
228 | raise NotImplementedError("Override in subclasses") | |
245 |
|
229 | |||
246 | def init_gui_pylab(self): |
|
230 | def init_gui_pylab(self): | |
247 | """Enable GUI event loop integration, taking pylab into account.""" |
|
231 | """Enable GUI event loop integration, taking pylab into account.""" | |
248 | enable = False |
|
232 | enable = False | |
249 | shell = self.shell |
|
233 | shell = self.shell | |
250 | if self.pylab: |
|
234 | if self.pylab: | |
251 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) |
|
235 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) | |
252 | key = self.pylab |
|
236 | key = self.pylab | |
253 | elif self.matplotlib: |
|
237 | elif self.matplotlib: | |
254 | enable = shell.enable_matplotlib |
|
238 | enable = shell.enable_matplotlib | |
255 | key = self.matplotlib |
|
239 | key = self.matplotlib | |
256 | elif self.gui: |
|
240 | elif self.gui: | |
257 | enable = shell.enable_gui |
|
241 | enable = shell.enable_gui | |
258 | key = self.gui |
|
242 | key = self.gui | |
259 |
|
243 | |||
260 | if not enable: |
|
244 | if not enable: | |
261 | return |
|
245 | return | |
262 |
|
246 | |||
263 | try: |
|
247 | try: | |
264 | r = enable(key) |
|
248 | r = enable(key) | |
265 | except ImportError: |
|
249 | except ImportError: | |
266 | self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?") |
|
250 | self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?") | |
267 | self.shell.showtraceback() |
|
251 | self.shell.showtraceback() | |
268 | return |
|
252 | return | |
269 | except Exception: |
|
253 | except Exception: | |
270 | self.log.warning("GUI event loop or pylab initialization failed") |
|
254 | self.log.warning("GUI event loop or pylab initialization failed") | |
271 | self.shell.showtraceback() |
|
255 | self.shell.showtraceback() | |
272 | return |
|
256 | return | |
273 |
|
257 | |||
274 | if isinstance(r, tuple): |
|
258 | if isinstance(r, tuple): | |
275 | gui, backend = r[:2] |
|
259 | gui, backend = r[:2] | |
276 | self.log.info("Enabling GUI event loop integration, " |
|
260 | self.log.info("Enabling GUI event loop integration, " | |
277 | "eventloop=%s, matplotlib=%s", gui, backend) |
|
261 | "eventloop=%s, matplotlib=%s", gui, backend) | |
278 | if key == "auto": |
|
262 | if key == "auto": | |
279 | print("Using matplotlib backend: %s" % backend) |
|
263 | print("Using matplotlib backend: %s" % backend) | |
280 | else: |
|
264 | else: | |
281 | gui = r |
|
265 | gui = r | |
282 | self.log.info("Enabling GUI event loop integration, " |
|
266 | self.log.info("Enabling GUI event loop integration, " | |
283 | "eventloop=%s", gui) |
|
267 | "eventloop=%s", gui) | |
284 |
|
268 | |||
285 | def init_extensions(self): |
|
269 | def init_extensions(self): | |
286 | """Load all IPython extensions in IPythonApp.extensions. |
|
270 | """Load all IPython extensions in IPythonApp.extensions. | |
287 |
|
271 | |||
288 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
272 | This uses the :meth:`ExtensionManager.load_extensions` to load all | |
289 | the extensions listed in ``self.extensions``. |
|
273 | the extensions listed in ``self.extensions``. | |
290 | """ |
|
274 | """ | |
291 | try: |
|
275 | try: | |
292 | self.log.debug("Loading IPython extensions...") |
|
276 | self.log.debug("Loading IPython extensions...") | |
293 | extensions = ( |
|
277 | extensions = ( | |
294 | self.default_extensions + self.extensions + self.extra_extensions |
|
278 | self.default_extensions + self.extensions + self.extra_extensions | |
295 | ) |
|
279 | ) | |
296 | if self.extra_extension: |
|
|||
297 | extensions.append(self.extra_extension) |
|
|||
298 | for ext in extensions: |
|
280 | for ext in extensions: | |
299 | try: |
|
281 | try: | |
300 | self.log.info("Loading IPython extension: %s" % ext) |
|
282 | self.log.info("Loading IPython extension: %s" % ext) | |
301 | self.shell.extension_manager.load_extension(ext) |
|
283 | self.shell.extension_manager.load_extension(ext) | |
302 | except: |
|
284 | except: | |
303 | if self.reraise_ipython_extension_failures: |
|
285 | if self.reraise_ipython_extension_failures: | |
304 | raise |
|
286 | raise | |
305 | msg = ("Error in loading extension: {ext}\n" |
|
287 | msg = ("Error in loading extension: {ext}\n" | |
306 | "Check your config files in {location}".format( |
|
288 | "Check your config files in {location}".format( | |
307 | ext=ext, |
|
289 | ext=ext, | |
308 | location=self.profile_dir.location |
|
290 | location=self.profile_dir.location | |
309 | )) |
|
291 | )) | |
310 | self.log.warning(msg, exc_info=True) |
|
292 | self.log.warning(msg, exc_info=True) | |
311 | except: |
|
293 | except: | |
312 | if self.reraise_ipython_extension_failures: |
|
294 | if self.reraise_ipython_extension_failures: | |
313 | raise |
|
295 | raise | |
314 | self.log.warning("Unknown error in loading extensions:", exc_info=True) |
|
296 | self.log.warning("Unknown error in loading extensions:", exc_info=True) | |
315 |
|
297 | |||
316 | def init_code(self): |
|
298 | def init_code(self): | |
317 | """run the pre-flight code, specified via exec_lines""" |
|
299 | """run the pre-flight code, specified via exec_lines""" | |
318 | self._run_startup_files() |
|
300 | self._run_startup_files() | |
319 | self._run_exec_lines() |
|
301 | self._run_exec_lines() | |
320 | self._run_exec_files() |
|
302 | self._run_exec_files() | |
321 |
|
303 | |||
322 | # Hide variables defined here from %who etc. |
|
304 | # Hide variables defined here from %who etc. | |
323 | if self.hide_initial_ns: |
|
305 | if self.hide_initial_ns: | |
324 | self.shell.user_ns_hidden.update(self.shell.user_ns) |
|
306 | self.shell.user_ns_hidden.update(self.shell.user_ns) | |
325 |
|
307 | |||
326 | # command-line execution (ipython -i script.py, ipython -m module) |
|
308 | # command-line execution (ipython -i script.py, ipython -m module) | |
327 | # should *not* be excluded from %whos |
|
309 | # should *not* be excluded from %whos | |
328 | self._run_cmd_line_code() |
|
310 | self._run_cmd_line_code() | |
329 | self._run_module() |
|
311 | self._run_module() | |
330 |
|
312 | |||
331 | # flush output, so itwon't be attached to the first cell |
|
313 | # flush output, so itwon't be attached to the first cell | |
332 | sys.stdout.flush() |
|
314 | sys.stdout.flush() | |
333 | sys.stderr.flush() |
|
315 | sys.stderr.flush() | |
334 | self.shell._sys_modules_keys = set(sys.modules.keys()) |
|
316 | self.shell._sys_modules_keys = set(sys.modules.keys()) | |
335 |
|
317 | |||
336 | def _run_exec_lines(self): |
|
318 | def _run_exec_lines(self): | |
337 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
319 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" | |
338 | if not self.exec_lines: |
|
320 | if not self.exec_lines: | |
339 | return |
|
321 | return | |
340 | try: |
|
322 | try: | |
341 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
323 | self.log.debug("Running code from IPythonApp.exec_lines...") | |
342 | for line in self.exec_lines: |
|
324 | for line in self.exec_lines: | |
343 | try: |
|
325 | try: | |
344 | self.log.info("Running code in user namespace: %s" % |
|
326 | self.log.info("Running code in user namespace: %s" % | |
345 | line) |
|
327 | line) | |
346 | self.shell.run_cell(line, store_history=False) |
|
328 | self.shell.run_cell(line, store_history=False) | |
347 | except: |
|
329 | except: | |
348 | self.log.warning("Error in executing line in user " |
|
330 | self.log.warning("Error in executing line in user " | |
349 | "namespace: %s" % line) |
|
331 | "namespace: %s" % line) | |
350 | self.shell.showtraceback() |
|
332 | self.shell.showtraceback() | |
351 | except: |
|
333 | except: | |
352 | self.log.warning("Unknown error in handling IPythonApp.exec_lines:") |
|
334 | self.log.warning("Unknown error in handling IPythonApp.exec_lines:") | |
353 | self.shell.showtraceback() |
|
335 | self.shell.showtraceback() | |
354 |
|
336 | |||
355 | def _exec_file(self, fname, shell_futures=False): |
|
337 | def _exec_file(self, fname, shell_futures=False): | |
356 | try: |
|
338 | try: | |
357 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
339 | full_filename = filefind(fname, [u'.', self.ipython_dir]) | |
358 | except IOError: |
|
340 | except IOError: | |
359 | self.log.warning("File not found: %r"%fname) |
|
341 | self.log.warning("File not found: %r"%fname) | |
360 | return |
|
342 | return | |
361 | # Make sure that the running script gets a proper sys.argv as if it |
|
343 | # Make sure that the running script gets a proper sys.argv as if it | |
362 | # were run from a system shell. |
|
344 | # were run from a system shell. | |
363 | save_argv = sys.argv |
|
345 | save_argv = sys.argv | |
364 | sys.argv = [full_filename] + self.extra_args[1:] |
|
346 | sys.argv = [full_filename] + self.extra_args[1:] | |
365 | try: |
|
347 | try: | |
366 | if os.path.isfile(full_filename): |
|
348 | if os.path.isfile(full_filename): | |
367 | self.log.info("Running file in user namespace: %s" % |
|
349 | self.log.info("Running file in user namespace: %s" % | |
368 | full_filename) |
|
350 | full_filename) | |
369 | # Ensure that __file__ is always defined to match Python |
|
351 | # Ensure that __file__ is always defined to match Python | |
370 | # behavior. |
|
352 | # behavior. | |
371 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
353 | with preserve_keys(self.shell.user_ns, '__file__'): | |
372 | self.shell.user_ns['__file__'] = fname |
|
354 | self.shell.user_ns['__file__'] = fname | |
373 | if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'): |
|
355 | if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'): | |
374 | self.shell.safe_execfile_ipy(full_filename, |
|
356 | self.shell.safe_execfile_ipy(full_filename, | |
375 | shell_futures=shell_futures) |
|
357 | shell_futures=shell_futures) | |
376 | else: |
|
358 | else: | |
377 | # default to python, even without extension |
|
359 | # default to python, even without extension | |
378 | self.shell.safe_execfile(full_filename, |
|
360 | self.shell.safe_execfile(full_filename, | |
379 | self.shell.user_ns, |
|
361 | self.shell.user_ns, | |
380 | shell_futures=shell_futures, |
|
362 | shell_futures=shell_futures, | |
381 | raise_exceptions=True) |
|
363 | raise_exceptions=True) | |
382 | finally: |
|
364 | finally: | |
383 | sys.argv = save_argv |
|
365 | sys.argv = save_argv | |
384 |
|
366 | |||
385 | def _run_startup_files(self): |
|
367 | def _run_startup_files(self): | |
386 | """Run files from profile startup directory""" |
|
368 | """Run files from profile startup directory""" | |
387 | startup_dirs = [self.profile_dir.startup_dir] + [ |
|
369 | startup_dirs = [self.profile_dir.startup_dir] + [ | |
388 | os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS) |
|
370 | os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS) | |
389 | ] |
|
371 | ] | |
390 | startup_files = [] |
|
372 | startup_files = [] | |
391 |
|
373 | |||
392 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ |
|
374 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ | |
393 | not (self.file_to_run or self.code_to_run or self.module_to_run): |
|
375 | not (self.file_to_run or self.code_to_run or self.module_to_run): | |
394 | python_startup = os.environ['PYTHONSTARTUP'] |
|
376 | python_startup = os.environ['PYTHONSTARTUP'] | |
395 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) |
|
377 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) | |
396 | try: |
|
378 | try: | |
397 | self._exec_file(python_startup) |
|
379 | self._exec_file(python_startup) | |
398 | except: |
|
380 | except: | |
399 | self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) |
|
381 | self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) | |
400 | self.shell.showtraceback() |
|
382 | self.shell.showtraceback() | |
401 | for startup_dir in startup_dirs[::-1]: |
|
383 | for startup_dir in startup_dirs[::-1]: | |
402 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) |
|
384 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) | |
403 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) |
|
385 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) | |
404 | if not startup_files: |
|
386 | if not startup_files: | |
405 | return |
|
387 | return | |
406 |
|
388 | |||
407 | self.log.debug("Running startup files from %s...", startup_dir) |
|
389 | self.log.debug("Running startup files from %s...", startup_dir) | |
408 | try: |
|
390 | try: | |
409 | for fname in sorted(startup_files): |
|
391 | for fname in sorted(startup_files): | |
410 | self._exec_file(fname) |
|
392 | self._exec_file(fname) | |
411 | except: |
|
393 | except: | |
412 | self.log.warning("Unknown error in handling startup files:") |
|
394 | self.log.warning("Unknown error in handling startup files:") | |
413 | self.shell.showtraceback() |
|
395 | self.shell.showtraceback() | |
414 |
|
396 | |||
415 | def _run_exec_files(self): |
|
397 | def _run_exec_files(self): | |
416 | """Run files from IPythonApp.exec_files""" |
|
398 | """Run files from IPythonApp.exec_files""" | |
417 | if not self.exec_files: |
|
399 | if not self.exec_files: | |
418 | return |
|
400 | return | |
419 |
|
401 | |||
420 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
402 | self.log.debug("Running files in IPythonApp.exec_files...") | |
421 | try: |
|
403 | try: | |
422 | for fname in self.exec_files: |
|
404 | for fname in self.exec_files: | |
423 | self._exec_file(fname) |
|
405 | self._exec_file(fname) | |
424 | except: |
|
406 | except: | |
425 | self.log.warning("Unknown error in handling IPythonApp.exec_files:") |
|
407 | self.log.warning("Unknown error in handling IPythonApp.exec_files:") | |
426 | self.shell.showtraceback() |
|
408 | self.shell.showtraceback() | |
427 |
|
409 | |||
428 | def _run_cmd_line_code(self): |
|
410 | def _run_cmd_line_code(self): | |
429 | """Run code or file specified at the command-line""" |
|
411 | """Run code or file specified at the command-line""" | |
430 | if self.code_to_run: |
|
412 | if self.code_to_run: | |
431 | line = self.code_to_run |
|
413 | line = self.code_to_run | |
432 | try: |
|
414 | try: | |
433 | self.log.info("Running code given at command line (c=): %s" % |
|
415 | self.log.info("Running code given at command line (c=): %s" % | |
434 | line) |
|
416 | line) | |
435 | self.shell.run_cell(line, store_history=False) |
|
417 | self.shell.run_cell(line, store_history=False) | |
436 | except: |
|
418 | except: | |
437 | self.log.warning("Error in executing line in user namespace: %s" % |
|
419 | self.log.warning("Error in executing line in user namespace: %s" % | |
438 | line) |
|
420 | line) | |
439 | self.shell.showtraceback() |
|
421 | self.shell.showtraceback() | |
440 | if not self.interact: |
|
422 | if not self.interact: | |
441 | self.exit(1) |
|
423 | self.exit(1) | |
442 |
|
424 | |||
443 | # Like Python itself, ignore the second if the first of these is present |
|
425 | # Like Python itself, ignore the second if the first of these is present | |
444 | elif self.file_to_run: |
|
426 | elif self.file_to_run: | |
445 | fname = self.file_to_run |
|
427 | fname = self.file_to_run | |
446 | if os.path.isdir(fname): |
|
428 | if os.path.isdir(fname): | |
447 | fname = os.path.join(fname, "__main__.py") |
|
429 | fname = os.path.join(fname, "__main__.py") | |
448 | if not os.path.exists(fname): |
|
430 | if not os.path.exists(fname): | |
449 | self.log.warning("File '%s' doesn't exist", fname) |
|
431 | self.log.warning("File '%s' doesn't exist", fname) | |
450 | if not self.interact: |
|
432 | if not self.interact: | |
451 | self.exit(2) |
|
433 | self.exit(2) | |
452 | try: |
|
434 | try: | |
453 | self._exec_file(fname, shell_futures=True) |
|
435 | self._exec_file(fname, shell_futures=True) | |
454 | except: |
|
436 | except: | |
455 | self.shell.showtraceback(tb_offset=4) |
|
437 | self.shell.showtraceback(tb_offset=4) | |
456 | if not self.interact: |
|
438 | if not self.interact: | |
457 | self.exit(1) |
|
439 | self.exit(1) | |
458 |
|
440 | |||
459 | def _run_module(self): |
|
441 | def _run_module(self): | |
460 | """Run module specified at the command-line.""" |
|
442 | """Run module specified at the command-line.""" | |
461 | if self.module_to_run: |
|
443 | if self.module_to_run: | |
462 | # Make sure that the module gets a proper sys.argv as if it were |
|
444 | # Make sure that the module gets a proper sys.argv as if it were | |
463 | # run using `python -m`. |
|
445 | # run using `python -m`. | |
464 | save_argv = sys.argv |
|
446 | save_argv = sys.argv | |
465 | sys.argv = [sys.executable] + self.extra_args |
|
447 | sys.argv = [sys.executable] + self.extra_args | |
466 | try: |
|
448 | try: | |
467 | self.shell.safe_run_module(self.module_to_run, |
|
449 | self.shell.safe_run_module(self.module_to_run, | |
468 | self.shell.user_ns) |
|
450 | self.shell.user_ns) | |
469 | finally: |
|
451 | finally: | |
470 | sys.argv = save_argv |
|
452 | sys.argv = save_argv |
@@ -1,544 +1,532 b'' | |||||
1 | """Tests for the Formatters.""" |
|
1 | """Tests for the Formatters.""" | |
2 |
|
2 | |||
3 | import warnings |
|
3 | import warnings | |
4 | from math import pi |
|
4 | from math import pi | |
5 |
|
5 | |||
6 | try: |
|
6 | try: | |
7 | import numpy |
|
7 | import numpy | |
8 | except: |
|
8 | except: | |
9 | numpy = None |
|
9 | numpy = None | |
10 | import pytest |
|
10 | import pytest | |
11 |
|
11 | |||
12 | from IPython import get_ipython |
|
12 | from IPython import get_ipython | |
13 | from traitlets.config import Config |
|
13 | from traitlets.config import Config | |
14 | from IPython.core.formatters import ( |
|
14 | from IPython.core.formatters import ( | |
15 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key, |
|
15 | PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key, | |
16 | DisplayFormatter, JSONFormatter, |
|
16 | DisplayFormatter, JSONFormatter, | |
17 | ) |
|
17 | ) | |
18 | from IPython.utils.io import capture_output |
|
18 | from IPython.utils.io import capture_output | |
19 |
|
19 | |||
20 | class A(object): |
|
20 | class A(object): | |
21 | def __repr__(self): |
|
21 | def __repr__(self): | |
22 | return 'A()' |
|
22 | return 'A()' | |
23 |
|
23 | |||
24 | class B(A): |
|
24 | class B(A): | |
25 | def __repr__(self): |
|
25 | def __repr__(self): | |
26 | return 'B()' |
|
26 | return 'B()' | |
27 |
|
27 | |||
28 | class C: |
|
28 | class C: | |
29 | pass |
|
29 | pass | |
30 |
|
30 | |||
31 | class BadRepr(object): |
|
31 | class BadRepr(object): | |
32 | def __repr__(self): |
|
32 | def __repr__(self): | |
33 | raise ValueError("bad repr") |
|
33 | raise ValueError("bad repr") | |
34 |
|
34 | |||
35 | class BadPretty(object): |
|
35 | class BadPretty(object): | |
36 | _repr_pretty_ = None |
|
36 | _repr_pretty_ = None | |
37 |
|
37 | |||
38 | class GoodPretty(object): |
|
38 | class GoodPretty(object): | |
39 | def _repr_pretty_(self, pp, cycle): |
|
39 | def _repr_pretty_(self, pp, cycle): | |
40 | pp.text('foo') |
|
40 | pp.text('foo') | |
41 |
|
41 | |||
42 | def __repr__(self): |
|
42 | def __repr__(self): | |
43 | return 'GoodPretty()' |
|
43 | return 'GoodPretty()' | |
44 |
|
44 | |||
45 | def foo_printer(obj, pp, cycle): |
|
45 | def foo_printer(obj, pp, cycle): | |
46 | pp.text('foo') |
|
46 | pp.text('foo') | |
47 |
|
47 | |||
48 | def test_pretty(): |
|
48 | def test_pretty(): | |
49 | f = PlainTextFormatter() |
|
49 | f = PlainTextFormatter() | |
50 | f.for_type(A, foo_printer) |
|
50 | f.for_type(A, foo_printer) | |
51 | assert f(A()) == "foo" |
|
51 | assert f(A()) == "foo" | |
52 | assert f(B()) == "B()" |
|
52 | assert f(B()) == "B()" | |
53 | assert f(GoodPretty()) == "foo" |
|
53 | assert f(GoodPretty()) == "foo" | |
54 | # Just don't raise an exception for the following: |
|
54 | # Just don't raise an exception for the following: | |
55 | f(BadPretty()) |
|
55 | f(BadPretty()) | |
56 |
|
56 | |||
57 | f.pprint = False |
|
57 | f.pprint = False | |
58 | assert f(A()) == "A()" |
|
58 | assert f(A()) == "A()" | |
59 | assert f(B()) == "B()" |
|
59 | assert f(B()) == "B()" | |
60 | assert f(GoodPretty()) == "GoodPretty()" |
|
60 | assert f(GoodPretty()) == "GoodPretty()" | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | def test_deferred(): |
|
63 | def test_deferred(): | |
64 | f = PlainTextFormatter() |
|
64 | f = PlainTextFormatter() | |
65 |
|
65 | |||
66 | def test_precision(): |
|
66 | def test_precision(): | |
67 | """test various values for float_precision.""" |
|
67 | """test various values for float_precision.""" | |
68 | f = PlainTextFormatter() |
|
68 | f = PlainTextFormatter() | |
69 | assert f(pi) == repr(pi) |
|
69 | assert f(pi) == repr(pi) | |
70 | f.float_precision = 0 |
|
70 | f.float_precision = 0 | |
71 | if numpy: |
|
71 | if numpy: | |
72 | po = numpy.get_printoptions() |
|
72 | po = numpy.get_printoptions() | |
73 | assert po["precision"] == 0 |
|
73 | assert po["precision"] == 0 | |
74 | assert f(pi) == "3" |
|
74 | assert f(pi) == "3" | |
75 | f.float_precision = 2 |
|
75 | f.float_precision = 2 | |
76 | if numpy: |
|
76 | if numpy: | |
77 | po = numpy.get_printoptions() |
|
77 | po = numpy.get_printoptions() | |
78 | assert po["precision"] == 2 |
|
78 | assert po["precision"] == 2 | |
79 | assert f(pi) == "3.14" |
|
79 | assert f(pi) == "3.14" | |
80 | f.float_precision = "%g" |
|
80 | f.float_precision = "%g" | |
81 | if numpy: |
|
81 | if numpy: | |
82 | po = numpy.get_printoptions() |
|
82 | po = numpy.get_printoptions() | |
83 | assert po["precision"] == 2 |
|
83 | assert po["precision"] == 2 | |
84 | assert f(pi) == "3.14159" |
|
84 | assert f(pi) == "3.14159" | |
85 | f.float_precision = "%e" |
|
85 | f.float_precision = "%e" | |
86 | assert f(pi) == "3.141593e+00" |
|
86 | assert f(pi) == "3.141593e+00" | |
87 | f.float_precision = "" |
|
87 | f.float_precision = "" | |
88 | if numpy: |
|
88 | if numpy: | |
89 | po = numpy.get_printoptions() |
|
89 | po = numpy.get_printoptions() | |
90 | assert po["precision"] == 8 |
|
90 | assert po["precision"] == 8 | |
91 | assert f(pi) == repr(pi) |
|
91 | assert f(pi) == repr(pi) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def test_bad_precision(): |
|
94 | def test_bad_precision(): | |
95 | """test various invalid values for float_precision.""" |
|
95 | """test various invalid values for float_precision.""" | |
96 | f = PlainTextFormatter() |
|
96 | f = PlainTextFormatter() | |
97 | def set_fp(p): |
|
97 | def set_fp(p): | |
98 | f.float_precision = p |
|
98 | f.float_precision = p | |
99 |
|
99 | |||
100 | pytest.raises(ValueError, set_fp, "%") |
|
100 | pytest.raises(ValueError, set_fp, "%") | |
101 | pytest.raises(ValueError, set_fp, "%.3f%i") |
|
101 | pytest.raises(ValueError, set_fp, "%.3f%i") | |
102 | pytest.raises(ValueError, set_fp, "foo") |
|
102 | pytest.raises(ValueError, set_fp, "foo") | |
103 | pytest.raises(ValueError, set_fp, -1) |
|
103 | pytest.raises(ValueError, set_fp, -1) | |
104 |
|
104 | |||
105 | def test_for_type(): |
|
105 | def test_for_type(): | |
106 | f = PlainTextFormatter() |
|
106 | f = PlainTextFormatter() | |
107 |
|
107 | |||
108 | # initial return, None |
|
108 | # initial return, None | |
109 | assert f.for_type(C, foo_printer) is None |
|
109 | assert f.for_type(C, foo_printer) is None | |
110 | # no func queries |
|
110 | # no func queries | |
111 | assert f.for_type(C) is foo_printer |
|
111 | assert f.for_type(C) is foo_printer | |
112 | # shouldn't change anything |
|
112 | # shouldn't change anything | |
113 | assert f.for_type(C) is foo_printer |
|
113 | assert f.for_type(C) is foo_printer | |
114 | # None should do the same |
|
114 | # None should do the same | |
115 | assert f.for_type(C, None) is foo_printer |
|
115 | assert f.for_type(C, None) is foo_printer | |
116 | assert f.for_type(C, None) is foo_printer |
|
116 | assert f.for_type(C, None) is foo_printer | |
117 |
|
117 | |||
118 | def test_for_type_string(): |
|
118 | def test_for_type_string(): | |
119 | f = PlainTextFormatter() |
|
119 | f = PlainTextFormatter() | |
120 |
|
120 | |||
121 | type_str = '%s.%s' % (C.__module__, 'C') |
|
121 | type_str = '%s.%s' % (C.__module__, 'C') | |
122 |
|
122 | |||
123 | # initial return, None |
|
123 | # initial return, None | |
124 | assert f.for_type(type_str, foo_printer) is None |
|
124 | assert f.for_type(type_str, foo_printer) is None | |
125 | # no func queries |
|
125 | # no func queries | |
126 | assert f.for_type(type_str) is foo_printer |
|
126 | assert f.for_type(type_str) is foo_printer | |
127 | assert _mod_name_key(C) in f.deferred_printers |
|
127 | assert _mod_name_key(C) in f.deferred_printers | |
128 | assert f.for_type(C) is foo_printer |
|
128 | assert f.for_type(C) is foo_printer | |
129 | assert _mod_name_key(C) not in f.deferred_printers |
|
129 | assert _mod_name_key(C) not in f.deferred_printers | |
130 | assert C in f.type_printers |
|
130 | assert C in f.type_printers | |
131 |
|
131 | |||
132 | def test_for_type_by_name(): |
|
132 | def test_for_type_by_name(): | |
133 | f = PlainTextFormatter() |
|
133 | f = PlainTextFormatter() | |
134 |
|
134 | |||
135 | mod = C.__module__ |
|
135 | mod = C.__module__ | |
136 |
|
136 | |||
137 | # initial return, None |
|
137 | # initial return, None | |
138 | assert f.for_type_by_name(mod, "C", foo_printer) is None |
|
138 | assert f.for_type_by_name(mod, "C", foo_printer) is None | |
139 | # no func queries |
|
139 | # no func queries | |
140 | assert f.for_type_by_name(mod, "C") is foo_printer |
|
140 | assert f.for_type_by_name(mod, "C") is foo_printer | |
141 | # shouldn't change anything |
|
141 | # shouldn't change anything | |
142 | assert f.for_type_by_name(mod, "C") is foo_printer |
|
142 | assert f.for_type_by_name(mod, "C") is foo_printer | |
143 | # None should do the same |
|
143 | # None should do the same | |
144 | assert f.for_type_by_name(mod, "C", None) is foo_printer |
|
144 | assert f.for_type_by_name(mod, "C", None) is foo_printer | |
145 | assert f.for_type_by_name(mod, "C", None) is foo_printer |
|
145 | assert f.for_type_by_name(mod, "C", None) is foo_printer | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | def test_lookup(): |
|
148 | def test_lookup(): | |
149 | f = PlainTextFormatter() |
|
149 | f = PlainTextFormatter() | |
150 |
|
150 | |||
151 | f.for_type(C, foo_printer) |
|
151 | f.for_type(C, foo_printer) | |
152 | assert f.lookup(C()) is foo_printer |
|
152 | assert f.lookup(C()) is foo_printer | |
153 | with pytest.raises(KeyError): |
|
153 | with pytest.raises(KeyError): | |
154 | f.lookup(A()) |
|
154 | f.lookup(A()) | |
155 |
|
155 | |||
156 | def test_lookup_string(): |
|
156 | def test_lookup_string(): | |
157 | f = PlainTextFormatter() |
|
157 | f = PlainTextFormatter() | |
158 | type_str = '%s.%s' % (C.__module__, 'C') |
|
158 | type_str = '%s.%s' % (C.__module__, 'C') | |
159 |
|
159 | |||
160 | f.for_type(type_str, foo_printer) |
|
160 | f.for_type(type_str, foo_printer) | |
161 | assert f.lookup(C()) is foo_printer |
|
161 | assert f.lookup(C()) is foo_printer | |
162 | # should move from deferred to imported dict |
|
162 | # should move from deferred to imported dict | |
163 | assert _mod_name_key(C) not in f.deferred_printers |
|
163 | assert _mod_name_key(C) not in f.deferred_printers | |
164 | assert C in f.type_printers |
|
164 | assert C in f.type_printers | |
165 |
|
165 | |||
166 | def test_lookup_by_type(): |
|
166 | def test_lookup_by_type(): | |
167 | f = PlainTextFormatter() |
|
167 | f = PlainTextFormatter() | |
168 | f.for_type(C, foo_printer) |
|
168 | f.for_type(C, foo_printer) | |
169 | assert f.lookup_by_type(C) is foo_printer |
|
169 | assert f.lookup_by_type(C) is foo_printer | |
170 | with pytest.raises(KeyError): |
|
170 | with pytest.raises(KeyError): | |
171 | f.lookup_by_type(A) |
|
171 | f.lookup_by_type(A) | |
172 |
|
172 | |||
173 | def test_lookup_by_type_string(): |
|
173 | def test_lookup_by_type_string(): | |
174 | f = PlainTextFormatter() |
|
174 | f = PlainTextFormatter() | |
175 | type_str = '%s.%s' % (C.__module__, 'C') |
|
175 | type_str = '%s.%s' % (C.__module__, 'C') | |
176 | f.for_type(type_str, foo_printer) |
|
176 | f.for_type(type_str, foo_printer) | |
177 |
|
177 | |||
178 | # verify insertion |
|
178 | # verify insertion | |
179 | assert _mod_name_key(C) in f.deferred_printers |
|
179 | assert _mod_name_key(C) in f.deferred_printers | |
180 | assert C not in f.type_printers |
|
180 | assert C not in f.type_printers | |
181 |
|
181 | |||
182 | assert f.lookup_by_type(type_str) is foo_printer |
|
182 | assert f.lookup_by_type(type_str) is foo_printer | |
183 | # lookup by string doesn't cause import |
|
183 | # lookup by string doesn't cause import | |
184 | assert _mod_name_key(C) in f.deferred_printers |
|
184 | assert _mod_name_key(C) in f.deferred_printers | |
185 | assert C not in f.type_printers |
|
185 | assert C not in f.type_printers | |
186 |
|
186 | |||
187 | assert f.lookup_by_type(C) is foo_printer |
|
187 | assert f.lookup_by_type(C) is foo_printer | |
188 | # should move from deferred to imported dict |
|
188 | # should move from deferred to imported dict | |
189 | assert _mod_name_key(C) not in f.deferred_printers |
|
189 | assert _mod_name_key(C) not in f.deferred_printers | |
190 | assert C in f.type_printers |
|
190 | assert C in f.type_printers | |
191 |
|
191 | |||
192 | def test_in_formatter(): |
|
192 | def test_in_formatter(): | |
193 | f = PlainTextFormatter() |
|
193 | f = PlainTextFormatter() | |
194 | f.for_type(C, foo_printer) |
|
194 | f.for_type(C, foo_printer) | |
195 | type_str = '%s.%s' % (C.__module__, 'C') |
|
195 | type_str = '%s.%s' % (C.__module__, 'C') | |
196 | assert C in f |
|
196 | assert C in f | |
197 | assert type_str in f |
|
197 | assert type_str in f | |
198 |
|
198 | |||
199 | def test_string_in_formatter(): |
|
199 | def test_string_in_formatter(): | |
200 | f = PlainTextFormatter() |
|
200 | f = PlainTextFormatter() | |
201 | type_str = '%s.%s' % (C.__module__, 'C') |
|
201 | type_str = '%s.%s' % (C.__module__, 'C') | |
202 | f.for_type(type_str, foo_printer) |
|
202 | f.for_type(type_str, foo_printer) | |
203 | assert type_str in f |
|
203 | assert type_str in f | |
204 | assert C in f |
|
204 | assert C in f | |
205 |
|
205 | |||
206 | def test_pop(): |
|
206 | def test_pop(): | |
207 | f = PlainTextFormatter() |
|
207 | f = PlainTextFormatter() | |
208 | f.for_type(C, foo_printer) |
|
208 | f.for_type(C, foo_printer) | |
209 | assert f.lookup_by_type(C) is foo_printer |
|
209 | assert f.lookup_by_type(C) is foo_printer | |
210 | assert f.pop(C, None) is foo_printer |
|
210 | assert f.pop(C, None) is foo_printer | |
211 | f.for_type(C, foo_printer) |
|
211 | f.for_type(C, foo_printer) | |
212 | assert f.pop(C) is foo_printer |
|
212 | assert f.pop(C) is foo_printer | |
213 | with pytest.raises(KeyError): |
|
213 | with pytest.raises(KeyError): | |
214 | f.lookup_by_type(C) |
|
214 | f.lookup_by_type(C) | |
215 | with pytest.raises(KeyError): |
|
215 | with pytest.raises(KeyError): | |
216 | f.pop(C) |
|
216 | f.pop(C) | |
217 | with pytest.raises(KeyError): |
|
217 | with pytest.raises(KeyError): | |
218 | f.pop(A) |
|
218 | f.pop(A) | |
219 | assert f.pop(A, None) is None |
|
219 | assert f.pop(A, None) is None | |
220 |
|
220 | |||
221 | def test_pop_string(): |
|
221 | def test_pop_string(): | |
222 | f = PlainTextFormatter() |
|
222 | f = PlainTextFormatter() | |
223 | type_str = '%s.%s' % (C.__module__, 'C') |
|
223 | type_str = '%s.%s' % (C.__module__, 'C') | |
224 |
|
224 | |||
225 | with pytest.raises(KeyError): |
|
225 | with pytest.raises(KeyError): | |
226 | f.pop(type_str) |
|
226 | f.pop(type_str) | |
227 |
|
227 | |||
228 | f.for_type(type_str, foo_printer) |
|
228 | f.for_type(type_str, foo_printer) | |
229 | f.pop(type_str) |
|
229 | f.pop(type_str) | |
230 | with pytest.raises(KeyError): |
|
230 | with pytest.raises(KeyError): | |
231 | f.lookup_by_type(C) |
|
231 | f.lookup_by_type(C) | |
232 | with pytest.raises(KeyError): |
|
232 | with pytest.raises(KeyError): | |
233 | f.pop(type_str) |
|
233 | f.pop(type_str) | |
234 |
|
234 | |||
235 | f.for_type(C, foo_printer) |
|
235 | f.for_type(C, foo_printer) | |
236 | assert f.pop(type_str, None) is foo_printer |
|
236 | assert f.pop(type_str, None) is foo_printer | |
237 | with pytest.raises(KeyError): |
|
237 | with pytest.raises(KeyError): | |
238 | f.lookup_by_type(C) |
|
238 | f.lookup_by_type(C) | |
239 | with pytest.raises(KeyError): |
|
239 | with pytest.raises(KeyError): | |
240 | f.pop(type_str) |
|
240 | f.pop(type_str) | |
241 | assert f.pop(type_str, None) is None |
|
241 | assert f.pop(type_str, None) is None | |
242 |
|
242 | |||
243 |
|
243 | |||
244 | def test_error_method(): |
|
244 | def test_error_method(): | |
245 | f = HTMLFormatter() |
|
245 | f = HTMLFormatter() | |
246 | class BadHTML(object): |
|
246 | class BadHTML(object): | |
247 | def _repr_html_(self): |
|
247 | def _repr_html_(self): | |
248 | raise ValueError("Bad HTML") |
|
248 | raise ValueError("Bad HTML") | |
249 | bad = BadHTML() |
|
249 | bad = BadHTML() | |
250 | with capture_output() as captured: |
|
250 | with capture_output() as captured: | |
251 | result = f(bad) |
|
251 | result = f(bad) | |
252 | assert result is None |
|
252 | assert result is None | |
253 | assert "Traceback" in captured.stdout |
|
253 | assert "Traceback" in captured.stdout | |
254 | assert "Bad HTML" in captured.stdout |
|
254 | assert "Bad HTML" in captured.stdout | |
255 | assert "_repr_html_" in captured.stdout |
|
255 | assert "_repr_html_" in captured.stdout | |
256 |
|
256 | |||
257 | def test_nowarn_notimplemented(): |
|
257 | def test_nowarn_notimplemented(): | |
258 | f = HTMLFormatter() |
|
258 | f = HTMLFormatter() | |
259 | class HTMLNotImplemented(object): |
|
259 | class HTMLNotImplemented(object): | |
260 | def _repr_html_(self): |
|
260 | def _repr_html_(self): | |
261 | raise NotImplementedError |
|
261 | raise NotImplementedError | |
262 | h = HTMLNotImplemented() |
|
262 | h = HTMLNotImplemented() | |
263 | with capture_output() as captured: |
|
263 | with capture_output() as captured: | |
264 | result = f(h) |
|
264 | result = f(h) | |
265 | assert result is None |
|
265 | assert result is None | |
266 | assert "" == captured.stderr |
|
266 | assert "" == captured.stderr | |
267 | assert "" == captured.stdout |
|
267 | assert "" == captured.stdout | |
268 |
|
268 | |||
269 |
|
269 | |||
270 | def test_warn_error_for_type(): |
|
270 | def test_warn_error_for_type(): | |
271 | f = HTMLFormatter() |
|
271 | f = HTMLFormatter() | |
272 | f.for_type(int, lambda i: name_error) |
|
272 | f.for_type(int, lambda i: name_error) | |
273 | with capture_output() as captured: |
|
273 | with capture_output() as captured: | |
274 | result = f(5) |
|
274 | result = f(5) | |
275 | assert result is None |
|
275 | assert result is None | |
276 | assert "Traceback" in captured.stdout |
|
276 | assert "Traceback" in captured.stdout | |
277 | assert "NameError" in captured.stdout |
|
277 | assert "NameError" in captured.stdout | |
278 | assert "name_error" in captured.stdout |
|
278 | assert "name_error" in captured.stdout | |
279 |
|
279 | |||
280 | def test_error_pretty_method(): |
|
280 | def test_error_pretty_method(): | |
281 | f = PlainTextFormatter() |
|
281 | f = PlainTextFormatter() | |
282 | class BadPretty(object): |
|
282 | class BadPretty(object): | |
283 | def _repr_pretty_(self): |
|
283 | def _repr_pretty_(self): | |
284 | return "hello" |
|
284 | return "hello" | |
285 | bad = BadPretty() |
|
285 | bad = BadPretty() | |
286 | with capture_output() as captured: |
|
286 | with capture_output() as captured: | |
287 | result = f(bad) |
|
287 | result = f(bad) | |
288 | assert result is None |
|
288 | assert result is None | |
289 | assert "Traceback" in captured.stdout |
|
289 | assert "Traceback" in captured.stdout | |
290 | assert "_repr_pretty_" in captured.stdout |
|
290 | assert "_repr_pretty_" in captured.stdout | |
291 | assert "given" in captured.stdout |
|
291 | assert "given" in captured.stdout | |
292 | assert "argument" in captured.stdout |
|
292 | assert "argument" in captured.stdout | |
293 |
|
293 | |||
294 |
|
294 | |||
295 | def test_bad_repr_traceback(): |
|
295 | def test_bad_repr_traceback(): | |
296 | f = PlainTextFormatter() |
|
296 | f = PlainTextFormatter() | |
297 | bad = BadRepr() |
|
297 | bad = BadRepr() | |
298 | with capture_output() as captured: |
|
298 | with capture_output() as captured: | |
299 | result = f(bad) |
|
299 | result = f(bad) | |
300 | # catches error, returns None |
|
300 | # catches error, returns None | |
301 | assert result is None |
|
301 | assert result is None | |
302 | assert "Traceback" in captured.stdout |
|
302 | assert "Traceback" in captured.stdout | |
303 | assert "__repr__" in captured.stdout |
|
303 | assert "__repr__" in captured.stdout | |
304 | assert "ValueError" in captured.stdout |
|
304 | assert "ValueError" in captured.stdout | |
305 |
|
305 | |||
306 |
|
306 | |||
307 | class MakePDF(object): |
|
307 | class MakePDF(object): | |
308 | def _repr_pdf_(self): |
|
308 | def _repr_pdf_(self): | |
309 | return 'PDF' |
|
309 | return 'PDF' | |
310 |
|
310 | |||
311 | def test_pdf_formatter(): |
|
311 | def test_pdf_formatter(): | |
312 | pdf = MakePDF() |
|
312 | pdf = MakePDF() | |
313 | f = PDFFormatter() |
|
313 | f = PDFFormatter() | |
314 | assert f(pdf) == "PDF" |
|
314 | assert f(pdf) == "PDF" | |
315 |
|
315 | |||
316 |
|
316 | |||
317 | def test_print_method_bound(): |
|
317 | def test_print_method_bound(): | |
318 | f = HTMLFormatter() |
|
318 | f = HTMLFormatter() | |
319 | class MyHTML(object): |
|
319 | class MyHTML(object): | |
320 | def _repr_html_(self): |
|
320 | def _repr_html_(self): | |
321 | return "hello" |
|
321 | return "hello" | |
322 | with capture_output() as captured: |
|
322 | with capture_output() as captured: | |
323 | result = f(MyHTML) |
|
323 | result = f(MyHTML) | |
324 | assert result is None |
|
324 | assert result is None | |
325 | assert "FormatterWarning" not in captured.stderr |
|
325 | assert "FormatterWarning" not in captured.stderr | |
326 |
|
326 | |||
327 | with capture_output() as captured: |
|
327 | with capture_output() as captured: | |
328 | result = f(MyHTML()) |
|
328 | result = f(MyHTML()) | |
329 | assert result == "hello" |
|
329 | assert result == "hello" | |
330 | assert captured.stderr == "" |
|
330 | assert captured.stderr == "" | |
331 |
|
331 | |||
332 |
|
332 | |||
333 | def test_print_method_weird(): |
|
333 | def test_print_method_weird(): | |
334 |
|
334 | |||
335 | class TextMagicHat(object): |
|
335 | class TextMagicHat(object): | |
336 | def __getattr__(self, key): |
|
336 | def __getattr__(self, key): | |
337 | return key |
|
337 | return key | |
338 |
|
338 | |||
339 | f = HTMLFormatter() |
|
339 | f = HTMLFormatter() | |
340 |
|
340 | |||
341 | text_hat = TextMagicHat() |
|
341 | text_hat = TextMagicHat() | |
342 | assert text_hat._repr_html_ == "_repr_html_" |
|
342 | assert text_hat._repr_html_ == "_repr_html_" | |
343 | with capture_output() as captured: |
|
343 | with capture_output() as captured: | |
344 | result = f(text_hat) |
|
344 | result = f(text_hat) | |
345 |
|
345 | |||
346 | assert result is None |
|
346 | assert result is None | |
347 | assert "FormatterWarning" not in captured.stderr |
|
347 | assert "FormatterWarning" not in captured.stderr | |
348 |
|
348 | |||
349 | class CallableMagicHat(object): |
|
349 | class CallableMagicHat(object): | |
350 | def __getattr__(self, key): |
|
350 | def __getattr__(self, key): | |
351 | return lambda : key |
|
351 | return lambda : key | |
352 |
|
352 | |||
353 | call_hat = CallableMagicHat() |
|
353 | call_hat = CallableMagicHat() | |
354 | with capture_output() as captured: |
|
354 | with capture_output() as captured: | |
355 | result = f(call_hat) |
|
355 | result = f(call_hat) | |
356 |
|
356 | |||
357 | assert result is None |
|
357 | assert result is None | |
358 |
|
358 | |||
359 | class BadReprArgs(object): |
|
359 | class BadReprArgs(object): | |
360 | def _repr_html_(self, extra, args): |
|
360 | def _repr_html_(self, extra, args): | |
361 | return "html" |
|
361 | return "html" | |
362 |
|
362 | |||
363 | bad = BadReprArgs() |
|
363 | bad = BadReprArgs() | |
364 | with capture_output() as captured: |
|
364 | with capture_output() as captured: | |
365 | result = f(bad) |
|
365 | result = f(bad) | |
366 |
|
366 | |||
367 | assert result is None |
|
367 | assert result is None | |
368 | assert "FormatterWarning" not in captured.stderr |
|
368 | assert "FormatterWarning" not in captured.stderr | |
369 |
|
369 | |||
370 |
|
370 | |||
371 | def test_format_config(): |
|
371 | def test_format_config(): | |
372 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
372 | """config objects don't pretend to support fancy reprs with lazy attrs""" | |
373 | f = HTMLFormatter() |
|
373 | f = HTMLFormatter() | |
374 | cfg = Config() |
|
374 | cfg = Config() | |
375 | with capture_output() as captured: |
|
375 | with capture_output() as captured: | |
376 | result = f(cfg) |
|
376 | result = f(cfg) | |
377 | assert result is None |
|
377 | assert result is None | |
378 | assert captured.stderr == "" |
|
378 | assert captured.stderr == "" | |
379 |
|
379 | |||
380 | with capture_output() as captured: |
|
380 | with capture_output() as captured: | |
381 | result = f(Config) |
|
381 | result = f(Config) | |
382 | assert result is None |
|
382 | assert result is None | |
383 | assert captured.stderr == "" |
|
383 | assert captured.stderr == "" | |
384 |
|
384 | |||
385 |
|
385 | |||
386 | def test_pretty_max_seq_length(): |
|
386 | def test_pretty_max_seq_length(): | |
387 | f = PlainTextFormatter(max_seq_length=1) |
|
387 | f = PlainTextFormatter(max_seq_length=1) | |
388 | lis = list(range(3)) |
|
388 | lis = list(range(3)) | |
389 | text = f(lis) |
|
389 | text = f(lis) | |
390 | assert text == "[0, ...]" |
|
390 | assert text == "[0, ...]" | |
391 | f.max_seq_length = 0 |
|
391 | f.max_seq_length = 0 | |
392 | text = f(lis) |
|
392 | text = f(lis) | |
393 | assert text == "[0, 1, 2]" |
|
393 | assert text == "[0, 1, 2]" | |
394 | text = f(list(range(1024))) |
|
394 | text = f(list(range(1024))) | |
395 | lines = text.splitlines() |
|
395 | lines = text.splitlines() | |
396 | assert len(lines) == 1024 |
|
396 | assert len(lines) == 1024 | |
397 |
|
397 | |||
398 |
|
398 | |||
399 | def test_ipython_display_formatter(): |
|
399 | def test_ipython_display_formatter(): | |
400 | """Objects with _ipython_display_ defined bypass other formatters""" |
|
400 | """Objects with _ipython_display_ defined bypass other formatters""" | |
401 | f = get_ipython().display_formatter |
|
401 | f = get_ipython().display_formatter | |
402 | catcher = [] |
|
402 | catcher = [] | |
403 | class SelfDisplaying(object): |
|
403 | class SelfDisplaying(object): | |
404 | def _ipython_display_(self): |
|
404 | def _ipython_display_(self): | |
405 | catcher.append(self) |
|
405 | catcher.append(self) | |
406 |
|
406 | |||
407 | class NotSelfDisplaying(object): |
|
407 | class NotSelfDisplaying(object): | |
408 | def __repr__(self): |
|
408 | def __repr__(self): | |
409 | return "NotSelfDisplaying" |
|
409 | return "NotSelfDisplaying" | |
410 |
|
410 | |||
411 | def _ipython_display_(self): |
|
411 | def _ipython_display_(self): | |
412 | raise NotImplementedError |
|
412 | raise NotImplementedError | |
413 |
|
413 | |||
414 | save_enabled = f.ipython_display_formatter.enabled |
|
414 | save_enabled = f.ipython_display_formatter.enabled | |
415 | f.ipython_display_formatter.enabled = True |
|
415 | f.ipython_display_formatter.enabled = True | |
416 |
|
416 | |||
417 | yes = SelfDisplaying() |
|
417 | yes = SelfDisplaying() | |
418 | no = NotSelfDisplaying() |
|
418 | no = NotSelfDisplaying() | |
419 |
|
419 | |||
420 | d, md = f.format(no) |
|
420 | d, md = f.format(no) | |
421 | assert d == {"text/plain": repr(no)} |
|
421 | assert d == {"text/plain": repr(no)} | |
422 | assert md == {} |
|
422 | assert md == {} | |
423 | assert catcher == [] |
|
423 | assert catcher == [] | |
424 |
|
424 | |||
425 | d, md = f.format(yes) |
|
425 | d, md = f.format(yes) | |
426 | assert d == {} |
|
426 | assert d == {} | |
427 | assert md == {} |
|
427 | assert md == {} | |
428 | assert catcher == [yes] |
|
428 | assert catcher == [yes] | |
429 |
|
429 | |||
430 | f.ipython_display_formatter.enabled = save_enabled |
|
430 | f.ipython_display_formatter.enabled = save_enabled | |
431 |
|
431 | |||
432 |
|
432 | |||
433 | def test_json_as_string_deprecated(): |
|
|||
434 | class JSONString(object): |
|
|||
435 | def _repr_json_(self): |
|
|||
436 | return '{}' |
|
|||
437 |
|
||||
438 | f = JSONFormatter() |
|
|||
439 | with warnings.catch_warnings(record=True) as w: |
|
|||
440 | d = f(JSONString()) |
|
|||
441 | assert d == {} |
|
|||
442 | assert len(w) == 1 |
|
|||
443 |
|
||||
444 |
|
||||
445 | def test_repr_mime(): |
|
433 | def test_repr_mime(): | |
446 | class HasReprMime(object): |
|
434 | class HasReprMime(object): | |
447 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
435 | def _repr_mimebundle_(self, include=None, exclude=None): | |
448 | return { |
|
436 | return { | |
449 | 'application/json+test.v2': { |
|
437 | 'application/json+test.v2': { | |
450 | 'x': 'y' |
|
438 | 'x': 'y' | |
451 | }, |
|
439 | }, | |
452 | 'plain/text' : '<HasReprMime>', |
|
440 | 'plain/text' : '<HasReprMime>', | |
453 | 'image/png' : 'i-overwrite' |
|
441 | 'image/png' : 'i-overwrite' | |
454 | } |
|
442 | } | |
455 |
|
443 | |||
456 | def _repr_png_(self): |
|
444 | def _repr_png_(self): | |
457 | return 'should-be-overwritten' |
|
445 | return 'should-be-overwritten' | |
458 | def _repr_html_(self): |
|
446 | def _repr_html_(self): | |
459 | return '<b>hi!</b>' |
|
447 | return '<b>hi!</b>' | |
460 |
|
448 | |||
461 | f = get_ipython().display_formatter |
|
449 | f = get_ipython().display_formatter | |
462 | html_f = f.formatters['text/html'] |
|
450 | html_f = f.formatters['text/html'] | |
463 | save_enabled = html_f.enabled |
|
451 | save_enabled = html_f.enabled | |
464 | html_f.enabled = True |
|
452 | html_f.enabled = True | |
465 | obj = HasReprMime() |
|
453 | obj = HasReprMime() | |
466 | d, md = f.format(obj) |
|
454 | d, md = f.format(obj) | |
467 | html_f.enabled = save_enabled |
|
455 | html_f.enabled = save_enabled | |
468 |
|
456 | |||
469 | assert sorted(d) == [ |
|
457 | assert sorted(d) == [ | |
470 | "application/json+test.v2", |
|
458 | "application/json+test.v2", | |
471 | "image/png", |
|
459 | "image/png", | |
472 | "plain/text", |
|
460 | "plain/text", | |
473 | "text/html", |
|
461 | "text/html", | |
474 | "text/plain", |
|
462 | "text/plain", | |
475 | ] |
|
463 | ] | |
476 | assert md == {} |
|
464 | assert md == {} | |
477 |
|
465 | |||
478 | d, md = f.format(obj, include={"image/png"}) |
|
466 | d, md = f.format(obj, include={"image/png"}) | |
479 | assert list(d.keys()) == [ |
|
467 | assert list(d.keys()) == [ | |
480 | "image/png" |
|
468 | "image/png" | |
481 | ], "Include should filter out even things from repr_mimebundle" |
|
469 | ], "Include should filter out even things from repr_mimebundle" | |
482 |
|
470 | |||
483 | assert d["image/png"] == "i-overwrite", "_repr_mimebundle_ take precedence" |
|
471 | assert d["image/png"] == "i-overwrite", "_repr_mimebundle_ take precedence" | |
484 |
|
472 | |||
485 |
|
473 | |||
486 | def test_pass_correct_include_exclude(): |
|
474 | def test_pass_correct_include_exclude(): | |
487 | class Tester(object): |
|
475 | class Tester(object): | |
488 |
|
476 | |||
489 | def __init__(self, include=None, exclude=None): |
|
477 | def __init__(self, include=None, exclude=None): | |
490 | self.include = include |
|
478 | self.include = include | |
491 | self.exclude = exclude |
|
479 | self.exclude = exclude | |
492 |
|
480 | |||
493 | def _repr_mimebundle_(self, include, exclude, **kwargs): |
|
481 | def _repr_mimebundle_(self, include, exclude, **kwargs): | |
494 | if include and (include != self.include): |
|
482 | if include and (include != self.include): | |
495 | raise ValueError('include got modified: display() may be broken.') |
|
483 | raise ValueError('include got modified: display() may be broken.') | |
496 | if exclude and (exclude != self.exclude): |
|
484 | if exclude and (exclude != self.exclude): | |
497 | raise ValueError('exclude got modified: display() may be broken.') |
|
485 | raise ValueError('exclude got modified: display() may be broken.') | |
498 |
|
486 | |||
499 | return None |
|
487 | return None | |
500 |
|
488 | |||
501 | include = {'a', 'b', 'c'} |
|
489 | include = {'a', 'b', 'c'} | |
502 | exclude = {'c', 'e' , 'f'} |
|
490 | exclude = {'c', 'e' , 'f'} | |
503 |
|
491 | |||
504 | f = get_ipython().display_formatter |
|
492 | f = get_ipython().display_formatter | |
505 | f.format(Tester(include=include, exclude=exclude), include=include, exclude=exclude) |
|
493 | f.format(Tester(include=include, exclude=exclude), include=include, exclude=exclude) | |
506 | f.format(Tester(exclude=exclude), exclude=exclude) |
|
494 | f.format(Tester(exclude=exclude), exclude=exclude) | |
507 | f.format(Tester(include=include), include=include) |
|
495 | f.format(Tester(include=include), include=include) | |
508 |
|
496 | |||
509 |
|
497 | |||
510 | def test_repr_mime_meta(): |
|
498 | def test_repr_mime_meta(): | |
511 | class HasReprMimeMeta(object): |
|
499 | class HasReprMimeMeta(object): | |
512 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
500 | def _repr_mimebundle_(self, include=None, exclude=None): | |
513 | data = { |
|
501 | data = { | |
514 | 'image/png': 'base64-image-data', |
|
502 | 'image/png': 'base64-image-data', | |
515 | } |
|
503 | } | |
516 | metadata = { |
|
504 | metadata = { | |
517 | 'image/png': { |
|
505 | 'image/png': { | |
518 | 'width': 5, |
|
506 | 'width': 5, | |
519 | 'height': 10, |
|
507 | 'height': 10, | |
520 | } |
|
508 | } | |
521 | } |
|
509 | } | |
522 | return (data, metadata) |
|
510 | return (data, metadata) | |
523 |
|
511 | |||
524 | f = get_ipython().display_formatter |
|
512 | f = get_ipython().display_formatter | |
525 | obj = HasReprMimeMeta() |
|
513 | obj = HasReprMimeMeta() | |
526 | d, md = f.format(obj) |
|
514 | d, md = f.format(obj) | |
527 | assert sorted(d) == ["image/png", "text/plain"] |
|
515 | assert sorted(d) == ["image/png", "text/plain"] | |
528 | assert md == { |
|
516 | assert md == { | |
529 | "image/png": { |
|
517 | "image/png": { | |
530 | "width": 5, |
|
518 | "width": 5, | |
531 | "height": 10, |
|
519 | "height": 10, | |
532 | } |
|
520 | } | |
533 | } |
|
521 | } | |
534 |
|
522 | |||
535 |
|
523 | |||
536 | def test_repr_mime_failure(): |
|
524 | def test_repr_mime_failure(): | |
537 | class BadReprMime(object): |
|
525 | class BadReprMime(object): | |
538 | def _repr_mimebundle_(self, include=None, exclude=None): |
|
526 | def _repr_mimebundle_(self, include=None, exclude=None): | |
539 | raise RuntimeError |
|
527 | raise RuntimeError | |
540 |
|
528 | |||
541 | f = get_ipython().display_formatter |
|
529 | f = get_ipython().display_formatter | |
542 | obj = BadReprMime() |
|
530 | obj = BadReprMime() | |
543 | d, md = f.format(obj) |
|
531 | d, md = f.format(obj) | |
544 | assert "text/plain" in d |
|
532 | assert "text/plain" in d |
@@ -1,356 +1,340 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | The :class:`~IPython.core.application.Application` object for the command |
|
4 | The :class:`~IPython.core.application.Application` object for the command | |
5 | line :command:`ipython` program. |
|
5 | line :command:`ipython` program. | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | # Copyright (c) IPython Development Team. |
|
8 | # Copyright (c) IPython Development Team. | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | import logging |
|
12 | import logging | |
13 | import os |
|
13 | import os | |
14 | import sys |
|
14 | import sys | |
15 | import warnings |
|
15 | import warnings | |
16 |
|
16 | |||
17 | from traitlets.config.loader import Config |
|
17 | from traitlets.config.loader import Config | |
18 | from traitlets.config.application import boolean_flag, catch_config_error |
|
18 | from traitlets.config.application import boolean_flag, catch_config_error | |
19 | from IPython.core import release |
|
19 | from IPython.core import release | |
20 | from IPython.core import usage |
|
20 | from IPython.core import usage | |
21 | from IPython.core.completer import IPCompleter |
|
21 | from IPython.core.completer import IPCompleter | |
22 | from IPython.core.crashhandler import CrashHandler |
|
22 | from IPython.core.crashhandler import CrashHandler | |
23 | from IPython.core.formatters import PlainTextFormatter |
|
23 | from IPython.core.formatters import PlainTextFormatter | |
24 | from IPython.core.history import HistoryManager |
|
24 | from IPython.core.history import HistoryManager | |
25 | from IPython.core.application import ( |
|
25 | from IPython.core.application import ( | |
26 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
26 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases | |
27 | ) |
|
27 | ) | |
28 | from IPython.core.magics import ( |
|
28 | from IPython.core.magics import ( | |
29 | ScriptMagics, LoggingMagics |
|
29 | ScriptMagics, LoggingMagics | |
30 | ) |
|
30 | ) | |
31 | from IPython.core.shellapp import ( |
|
31 | from IPython.core.shellapp import ( | |
32 | InteractiveShellApp, shell_flags, shell_aliases |
|
32 | InteractiveShellApp, shell_flags, shell_aliases | |
33 | ) |
|
33 | ) | |
34 | from IPython.extensions.storemagic import StoreMagics |
|
34 | from IPython.extensions.storemagic import StoreMagics | |
35 | from .interactiveshell import TerminalInteractiveShell |
|
35 | from .interactiveshell import TerminalInteractiveShell | |
36 | from IPython.paths import get_ipython_dir |
|
36 | from IPython.paths import get_ipython_dir | |
37 | from traitlets import ( |
|
37 | from traitlets import ( | |
38 | Bool, List, default, observe, Type |
|
38 | Bool, List, default, observe, Type | |
39 | ) |
|
39 | ) | |
40 |
|
40 | |||
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 | # Globals, utilities and helpers |
|
42 | # Globals, utilities and helpers | |
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 | _examples = """ |
|
45 | _examples = """ | |
46 | ipython --matplotlib # enable matplotlib integration |
|
46 | ipython --matplotlib # enable matplotlib integration | |
47 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend |
|
47 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend | |
48 |
|
48 | |||
49 | ipython --log-level=DEBUG # set logging to DEBUG |
|
49 | ipython --log-level=DEBUG # set logging to DEBUG | |
50 | ipython --profile=foo # start with profile foo |
|
50 | ipython --profile=foo # start with profile foo | |
51 |
|
51 | |||
52 | ipython profile create foo # create profile foo w/ default config files |
|
52 | ipython profile create foo # create profile foo w/ default config files | |
53 | ipython help profile # show the help for the profile subcmd |
|
53 | ipython help profile # show the help for the profile subcmd | |
54 |
|
54 | |||
55 | ipython locate # print the path to the IPython directory |
|
55 | ipython locate # print the path to the IPython directory | |
56 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
56 | ipython locate profile foo # print the path to the directory for profile `foo` | |
57 | """ |
|
57 | """ | |
58 |
|
58 | |||
59 | #----------------------------------------------------------------------------- |
|
59 | #----------------------------------------------------------------------------- | |
60 | # Crash handler for this application |
|
60 | # Crash handler for this application | |
61 | #----------------------------------------------------------------------------- |
|
61 | #----------------------------------------------------------------------------- | |
62 |
|
62 | |||
63 | class IPAppCrashHandler(CrashHandler): |
|
63 | class IPAppCrashHandler(CrashHandler): | |
64 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
64 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" | |
65 |
|
65 | |||
66 | def __init__(self, app): |
|
66 | def __init__(self, app): | |
67 | contact_name = release.author |
|
67 | contact_name = release.author | |
68 | contact_email = release.author_email |
|
68 | contact_email = release.author_email | |
69 | bug_tracker = 'https://github.com/ipython/ipython/issues' |
|
69 | bug_tracker = 'https://github.com/ipython/ipython/issues' | |
70 | super(IPAppCrashHandler,self).__init__( |
|
70 | super(IPAppCrashHandler,self).__init__( | |
71 | app, contact_name, contact_email, bug_tracker |
|
71 | app, contact_name, contact_email, bug_tracker | |
72 | ) |
|
72 | ) | |
73 |
|
73 | |||
74 | def make_report(self,traceback): |
|
74 | def make_report(self,traceback): | |
75 | """Return a string containing a crash report.""" |
|
75 | """Return a string containing a crash report.""" | |
76 |
|
76 | |||
77 | sec_sep = self.section_sep |
|
77 | sec_sep = self.section_sep | |
78 | # Start with parent report |
|
78 | # Start with parent report | |
79 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
79 | report = [super(IPAppCrashHandler, self).make_report(traceback)] | |
80 | # Add interactive-specific info we may have |
|
80 | # Add interactive-specific info we may have | |
81 | rpt_add = report.append |
|
81 | rpt_add = report.append | |
82 | try: |
|
82 | try: | |
83 | rpt_add(sec_sep+"History of session input:") |
|
83 | rpt_add(sec_sep+"History of session input:") | |
84 | for line in self.app.shell.user_ns['_ih']: |
|
84 | for line in self.app.shell.user_ns['_ih']: | |
85 | rpt_add(line) |
|
85 | rpt_add(line) | |
86 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
86 | rpt_add('\n*** Last line of input (may not be in above history):\n') | |
87 | rpt_add(self.app.shell._last_input_line+'\n') |
|
87 | rpt_add(self.app.shell._last_input_line+'\n') | |
88 | except: |
|
88 | except: | |
89 | pass |
|
89 | pass | |
90 |
|
90 | |||
91 | return ''.join(report) |
|
91 | return ''.join(report) | |
92 |
|
92 | |||
93 | #----------------------------------------------------------------------------- |
|
93 | #----------------------------------------------------------------------------- | |
94 | # Aliases and Flags |
|
94 | # Aliases and Flags | |
95 | #----------------------------------------------------------------------------- |
|
95 | #----------------------------------------------------------------------------- | |
96 | flags = dict(base_flags) |
|
96 | flags = dict(base_flags) | |
97 | flags.update(shell_flags) |
|
97 | flags.update(shell_flags) | |
98 | frontend_flags = {} |
|
98 | frontend_flags = {} | |
99 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) |
|
99 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) | |
100 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
100 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', | |
101 | 'Turn on auto editing of files with syntax errors.', |
|
101 | 'Turn on auto editing of files with syntax errors.', | |
102 | 'Turn off auto editing of files with syntax errors.' |
|
102 | 'Turn off auto editing of files with syntax errors.' | |
103 | ) |
|
103 | ) | |
104 | addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt', |
|
104 | addflag('simple-prompt', 'TerminalInteractiveShell.simple_prompt', | |
105 | "Force simple minimal prompt using `raw_input`", |
|
105 | "Force simple minimal prompt using `raw_input`", | |
106 | "Use a rich interactive prompt with prompt_toolkit", |
|
106 | "Use a rich interactive prompt with prompt_toolkit", | |
107 | ) |
|
107 | ) | |
108 |
|
108 | |||
109 | addflag('banner', 'TerminalIPythonApp.display_banner', |
|
109 | addflag('banner', 'TerminalIPythonApp.display_banner', | |
110 | "Display a banner upon starting IPython.", |
|
110 | "Display a banner upon starting IPython.", | |
111 | "Don't display a banner upon starting IPython." |
|
111 | "Don't display a banner upon starting IPython." | |
112 | ) |
|
112 | ) | |
113 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
113 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', | |
114 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
114 | """Set to confirm when you try to exit IPython with an EOF (Control-D | |
115 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
115 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
116 | you can force a direct exit without any confirmation.""", |
|
116 | you can force a direct exit without any confirmation.""", | |
117 | "Don't prompt the user when exiting." |
|
117 | "Don't prompt the user when exiting." | |
118 | ) |
|
118 | ) | |
119 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
119 | addflag('term-title', 'TerminalInteractiveShell.term_title', | |
120 | "Enable auto setting the terminal title.", |
|
120 | "Enable auto setting the terminal title.", | |
121 | "Disable auto setting the terminal title." |
|
121 | "Disable auto setting the terminal title." | |
122 | ) |
|
122 | ) | |
123 | classic_config = Config() |
|
123 | classic_config = Config() | |
124 | classic_config.InteractiveShell.cache_size = 0 |
|
124 | classic_config.InteractiveShell.cache_size = 0 | |
125 | classic_config.PlainTextFormatter.pprint = False |
|
125 | classic_config.PlainTextFormatter.pprint = False | |
126 | classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts' |
|
126 | classic_config.TerminalInteractiveShell.prompts_class='IPython.terminal.prompts.ClassicPrompts' | |
127 | classic_config.InteractiveShell.separate_in = '' |
|
127 | classic_config.InteractiveShell.separate_in = '' | |
128 | classic_config.InteractiveShell.separate_out = '' |
|
128 | classic_config.InteractiveShell.separate_out = '' | |
129 | classic_config.InteractiveShell.separate_out2 = '' |
|
129 | classic_config.InteractiveShell.separate_out2 = '' | |
130 | classic_config.InteractiveShell.colors = 'NoColor' |
|
130 | classic_config.InteractiveShell.colors = 'NoColor' | |
131 | classic_config.InteractiveShell.xmode = 'Plain' |
|
131 | classic_config.InteractiveShell.xmode = 'Plain' | |
132 |
|
132 | |||
133 | frontend_flags['classic']=( |
|
133 | frontend_flags['classic']=( | |
134 | classic_config, |
|
134 | classic_config, | |
135 | "Gives IPython a similar feel to the classic Python prompt." |
|
135 | "Gives IPython a similar feel to the classic Python prompt." | |
136 | ) |
|
136 | ) | |
137 | # # log doesn't make so much sense this way anymore |
|
137 | # # log doesn't make so much sense this way anymore | |
138 | # paa('--log','-l', |
|
138 | # paa('--log','-l', | |
139 | # action='store_true', dest='InteractiveShell.logstart', |
|
139 | # action='store_true', dest='InteractiveShell.logstart', | |
140 | # help="Start logging to the default log file (./ipython_log.py).") |
|
140 | # help="Start logging to the default log file (./ipython_log.py).") | |
141 | # |
|
141 | # | |
142 | # # quick is harder to implement |
|
142 | # # quick is harder to implement | |
143 | frontend_flags['quick']=( |
|
143 | frontend_flags['quick']=( | |
144 | {'TerminalIPythonApp' : {'quick' : True}}, |
|
144 | {'TerminalIPythonApp' : {'quick' : True}}, | |
145 | "Enable quick startup with no config files." |
|
145 | "Enable quick startup with no config files." | |
146 | ) |
|
146 | ) | |
147 |
|
147 | |||
148 | frontend_flags['i'] = ( |
|
148 | frontend_flags['i'] = ( | |
149 | {'TerminalIPythonApp' : {'force_interact' : True}}, |
|
149 | {'TerminalIPythonApp' : {'force_interact' : True}}, | |
150 | """If running code from the command line, become interactive afterwards. |
|
150 | """If running code from the command line, become interactive afterwards. | |
151 | It is often useful to follow this with `--` to treat remaining flags as |
|
151 | It is often useful to follow this with `--` to treat remaining flags as | |
152 | script arguments. |
|
152 | script arguments. | |
153 | """ |
|
153 | """ | |
154 | ) |
|
154 | ) | |
155 | flags.update(frontend_flags) |
|
155 | flags.update(frontend_flags) | |
156 |
|
156 | |||
157 | aliases = dict(base_aliases) |
|
157 | aliases = dict(base_aliases) | |
158 | aliases.update(shell_aliases) |
|
158 | aliases.update(shell_aliases) | |
159 |
|
159 | |||
160 | #----------------------------------------------------------------------------- |
|
160 | #----------------------------------------------------------------------------- | |
161 | # Main classes and functions |
|
161 | # Main classes and functions | |
162 | #----------------------------------------------------------------------------- |
|
162 | #----------------------------------------------------------------------------- | |
163 |
|
163 | |||
164 |
|
164 | |||
165 | class LocateIPythonApp(BaseIPythonApplication): |
|
165 | class LocateIPythonApp(BaseIPythonApplication): | |
166 | description = """print the path to the IPython dir""" |
|
166 | description = """print the path to the IPython dir""" | |
167 | subcommands = dict( |
|
167 | subcommands = dict( | |
168 | profile=('IPython.core.profileapp.ProfileLocate', |
|
168 | profile=('IPython.core.profileapp.ProfileLocate', | |
169 | "print the path to an IPython profile directory", |
|
169 | "print the path to an IPython profile directory", | |
170 | ), |
|
170 | ), | |
171 | ) |
|
171 | ) | |
172 | def start(self): |
|
172 | def start(self): | |
173 | if self.subapp is not None: |
|
173 | if self.subapp is not None: | |
174 | return self.subapp.start() |
|
174 | return self.subapp.start() | |
175 | else: |
|
175 | else: | |
176 | print(self.ipython_dir) |
|
176 | print(self.ipython_dir) | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): |
|
179 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): | |
180 | name = u'ipython' |
|
180 | name = u'ipython' | |
181 | description = usage.cl_usage |
|
181 | description = usage.cl_usage | |
182 | crash_handler_class = IPAppCrashHandler |
|
182 | crash_handler_class = IPAppCrashHandler | |
183 | examples = _examples |
|
183 | examples = _examples | |
184 |
|
184 | |||
185 | flags = flags |
|
185 | flags = flags | |
186 | aliases = aliases |
|
186 | aliases = aliases | |
187 | classes = List() |
|
187 | classes = List() | |
188 |
|
188 | |||
189 | interactive_shell_class = Type( |
|
189 | interactive_shell_class = Type( | |
190 | klass=object, # use default_value otherwise which only allow subclasses. |
|
190 | klass=object, # use default_value otherwise which only allow subclasses. | |
191 | default_value=TerminalInteractiveShell, |
|
191 | default_value=TerminalInteractiveShell, | |
192 | help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends" |
|
192 | help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends" | |
193 | ).tag(config=True) |
|
193 | ).tag(config=True) | |
194 |
|
194 | |||
195 | @default('classes') |
|
195 | @default('classes') | |
196 | def _classes_default(self): |
|
196 | def _classes_default(self): | |
197 | """This has to be in a method, for TerminalIPythonApp to be available.""" |
|
197 | """This has to be in a method, for TerminalIPythonApp to be available.""" | |
198 | return [ |
|
198 | return [ | |
199 | InteractiveShellApp, # ShellApp comes before TerminalApp, because |
|
199 | InteractiveShellApp, # ShellApp comes before TerminalApp, because | |
200 | self.__class__, # it will also affect subclasses (e.g. QtConsole) |
|
200 | self.__class__, # it will also affect subclasses (e.g. QtConsole) | |
201 | TerminalInteractiveShell, |
|
201 | TerminalInteractiveShell, | |
202 | HistoryManager, |
|
202 | HistoryManager, | |
203 | ProfileDir, |
|
203 | ProfileDir, | |
204 | PlainTextFormatter, |
|
204 | PlainTextFormatter, | |
205 | IPCompleter, |
|
205 | IPCompleter, | |
206 | ScriptMagics, |
|
206 | ScriptMagics, | |
207 | LoggingMagics, |
|
207 | LoggingMagics, | |
208 | StoreMagics, |
|
208 | StoreMagics, | |
209 | ] |
|
209 | ] | |
210 |
|
210 | |||
211 | subcommands = dict( |
|
211 | subcommands = dict( | |
212 | profile = ("IPython.core.profileapp.ProfileApp", |
|
212 | profile = ("IPython.core.profileapp.ProfileApp", | |
213 | "Create and manage IPython profiles." |
|
213 | "Create and manage IPython profiles." | |
214 | ), |
|
214 | ), | |
215 | kernel = ("ipykernel.kernelapp.IPKernelApp", |
|
215 | kernel = ("ipykernel.kernelapp.IPKernelApp", | |
216 | "Start a kernel without an attached frontend." |
|
216 | "Start a kernel without an attached frontend." | |
217 | ), |
|
217 | ), | |
218 | locate=('IPython.terminal.ipapp.LocateIPythonApp', |
|
218 | locate=('IPython.terminal.ipapp.LocateIPythonApp', | |
219 | LocateIPythonApp.description |
|
219 | LocateIPythonApp.description | |
220 | ), |
|
220 | ), | |
221 | history=('IPython.core.historyapp.HistoryApp', |
|
221 | history=('IPython.core.historyapp.HistoryApp', | |
222 | "Manage the IPython history database." |
|
222 | "Manage the IPython history database." | |
223 | ), |
|
223 | ), | |
224 | ) |
|
224 | ) | |
225 |
|
225 | |||
226 |
|
226 | |||
227 | # *do* autocreate requested profile, but don't create the config file. |
|
227 | # *do* autocreate requested profile, but don't create the config file. | |
228 | auto_create=Bool(True) |
|
228 | auto_create=Bool(True) | |
229 | # configurables |
|
229 | # configurables | |
230 | quick = Bool(False, |
|
230 | quick = Bool(False, | |
231 | help="""Start IPython quickly by skipping the loading of config files.""" |
|
231 | help="""Start IPython quickly by skipping the loading of config files.""" | |
232 | ).tag(config=True) |
|
232 | ).tag(config=True) | |
233 | @observe('quick') |
|
233 | @observe('quick') | |
234 | def _quick_changed(self, change): |
|
234 | def _quick_changed(self, change): | |
235 | if change['new']: |
|
235 | if change['new']: | |
236 | self.load_config_file = lambda *a, **kw: None |
|
236 | self.load_config_file = lambda *a, **kw: None | |
237 |
|
237 | |||
238 | display_banner = Bool(True, |
|
238 | display_banner = Bool(True, | |
239 | help="Whether to display a banner upon starting IPython." |
|
239 | help="Whether to display a banner upon starting IPython." | |
240 | ).tag(config=True) |
|
240 | ).tag(config=True) | |
241 |
|
241 | |||
242 | # if there is code of files to run from the cmd line, don't interact |
|
242 | # if there is code of files to run from the cmd line, don't interact | |
243 | # unless the --i flag (App.force_interact) is true. |
|
243 | # unless the --i flag (App.force_interact) is true. | |
244 | force_interact = Bool(False, |
|
244 | force_interact = Bool(False, | |
245 | help="""If a command or file is given via the command-line, |
|
245 | help="""If a command or file is given via the command-line, | |
246 | e.g. 'ipython foo.py', start an interactive shell after executing the |
|
246 | e.g. 'ipython foo.py', start an interactive shell after executing the | |
247 | file or command.""" |
|
247 | file or command.""" | |
248 | ).tag(config=True) |
|
248 | ).tag(config=True) | |
249 | @observe('force_interact') |
|
249 | @observe('force_interact') | |
250 | def _force_interact_changed(self, change): |
|
250 | def _force_interact_changed(self, change): | |
251 | if change['new']: |
|
251 | if change['new']: | |
252 | self.interact = True |
|
252 | self.interact = True | |
253 |
|
253 | |||
254 | @observe('file_to_run', 'code_to_run', 'module_to_run') |
|
254 | @observe('file_to_run', 'code_to_run', 'module_to_run') | |
255 | def _file_to_run_changed(self, change): |
|
255 | def _file_to_run_changed(self, change): | |
256 | new = change['new'] |
|
256 | new = change['new'] | |
257 | if new: |
|
257 | if new: | |
258 | self.something_to_run = True |
|
258 | self.something_to_run = True | |
259 | if new and not self.force_interact: |
|
259 | if new and not self.force_interact: | |
260 | self.interact = False |
|
260 | self.interact = False | |
261 |
|
261 | |||
262 | # internal, not-configurable |
|
262 | # internal, not-configurable | |
263 | something_to_run=Bool(False) |
|
263 | something_to_run=Bool(False) | |
264 |
|
264 | |||
265 | def parse_command_line(self, argv=None): |
|
|||
266 | """override to allow old '-pylab' flag with deprecation warning""" |
|
|||
267 |
|
||||
268 | argv = sys.argv[1:] if argv is None else argv |
|
|||
269 |
|
||||
270 | if '-pylab' in argv: |
|
|||
271 | # deprecated `-pylab` given, |
|
|||
272 | # warn and transform into current syntax |
|
|||
273 | argv = argv[:] # copy, don't clobber |
|
|||
274 | idx = argv.index('-pylab') |
|
|||
275 | warnings.warn("`-pylab` flag has been deprecated.\n" |
|
|||
276 | " Use `--matplotlib <backend>` and import pylab manually.") |
|
|||
277 | argv[idx] = '--pylab' |
|
|||
278 |
|
||||
279 | return super(TerminalIPythonApp, self).parse_command_line(argv) |
|
|||
280 |
|
||||
281 | @catch_config_error |
|
265 | @catch_config_error | |
282 | def initialize(self, argv=None): |
|
266 | def initialize(self, argv=None): | |
283 | """Do actions after construct, but before starting the app.""" |
|
267 | """Do actions after construct, but before starting the app.""" | |
284 | super(TerminalIPythonApp, self).initialize(argv) |
|
268 | super(TerminalIPythonApp, self).initialize(argv) | |
285 | if self.subapp is not None: |
|
269 | if self.subapp is not None: | |
286 | # don't bother initializing further, starting subapp |
|
270 | # don't bother initializing further, starting subapp | |
287 | return |
|
271 | return | |
288 | # print self.extra_args |
|
272 | # print self.extra_args | |
289 | if self.extra_args and not self.something_to_run: |
|
273 | if self.extra_args and not self.something_to_run: | |
290 | self.file_to_run = self.extra_args[0] |
|
274 | self.file_to_run = self.extra_args[0] | |
291 | self.init_path() |
|
275 | self.init_path() | |
292 | # create the shell |
|
276 | # create the shell | |
293 | self.init_shell() |
|
277 | self.init_shell() | |
294 | # and draw the banner |
|
278 | # and draw the banner | |
295 | self.init_banner() |
|
279 | self.init_banner() | |
296 | # Now a variety of things that happen after the banner is printed. |
|
280 | # Now a variety of things that happen after the banner is printed. | |
297 | self.init_gui_pylab() |
|
281 | self.init_gui_pylab() | |
298 | self.init_extensions() |
|
282 | self.init_extensions() | |
299 | self.init_code() |
|
283 | self.init_code() | |
300 |
|
284 | |||
301 | def init_shell(self): |
|
285 | def init_shell(self): | |
302 | """initialize the InteractiveShell instance""" |
|
286 | """initialize the InteractiveShell instance""" | |
303 | # Create an InteractiveShell instance. |
|
287 | # Create an InteractiveShell instance. | |
304 | # shell.display_banner should always be False for the terminal |
|
288 | # shell.display_banner should always be False for the terminal | |
305 | # based app, because we call shell.show_banner() by hand below |
|
289 | # based app, because we call shell.show_banner() by hand below | |
306 | # so the banner shows *before* all extension loading stuff. |
|
290 | # so the banner shows *before* all extension loading stuff. | |
307 | self.shell = self.interactive_shell_class.instance(parent=self, |
|
291 | self.shell = self.interactive_shell_class.instance(parent=self, | |
308 | profile_dir=self.profile_dir, |
|
292 | profile_dir=self.profile_dir, | |
309 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) |
|
293 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) | |
310 | self.shell.configurables.append(self) |
|
294 | self.shell.configurables.append(self) | |
311 |
|
295 | |||
312 | def init_banner(self): |
|
296 | def init_banner(self): | |
313 | """optionally display the banner""" |
|
297 | """optionally display the banner""" | |
314 | if self.display_banner and self.interact: |
|
298 | if self.display_banner and self.interact: | |
315 | self.shell.show_banner() |
|
299 | self.shell.show_banner() | |
316 | # Make sure there is a space below the banner. |
|
300 | # Make sure there is a space below the banner. | |
317 | if self.log_level <= logging.INFO: print() |
|
301 | if self.log_level <= logging.INFO: print() | |
318 |
|
302 | |||
319 | def _pylab_changed(self, name, old, new): |
|
303 | def _pylab_changed(self, name, old, new): | |
320 | """Replace --pylab='inline' with --pylab='auto'""" |
|
304 | """Replace --pylab='inline' with --pylab='auto'""" | |
321 | if new == 'inline': |
|
305 | if new == 'inline': | |
322 | warnings.warn("'inline' not available as pylab backend, " |
|
306 | warnings.warn("'inline' not available as pylab backend, " | |
323 | "using 'auto' instead.") |
|
307 | "using 'auto' instead.") | |
324 | self.pylab = 'auto' |
|
308 | self.pylab = 'auto' | |
325 |
|
309 | |||
326 | def start(self): |
|
310 | def start(self): | |
327 | if self.subapp is not None: |
|
311 | if self.subapp is not None: | |
328 | return self.subapp.start() |
|
312 | return self.subapp.start() | |
329 | # perform any prexec steps: |
|
313 | # perform any prexec steps: | |
330 | if self.interact: |
|
314 | if self.interact: | |
331 | self.log.debug("Starting IPython's mainloop...") |
|
315 | self.log.debug("Starting IPython's mainloop...") | |
332 | self.shell.mainloop() |
|
316 | self.shell.mainloop() | |
333 | else: |
|
317 | else: | |
334 | self.log.debug("IPython not interactive...") |
|
318 | self.log.debug("IPython not interactive...") | |
335 | if not self.shell.last_execution_succeeded: |
|
319 | if not self.shell.last_execution_succeeded: | |
336 | sys.exit(1) |
|
320 | sys.exit(1) | |
337 |
|
321 | |||
338 | def load_default_config(ipython_dir=None): |
|
322 | def load_default_config(ipython_dir=None): | |
339 | """Load the default config file from the default ipython_dir. |
|
323 | """Load the default config file from the default ipython_dir. | |
340 |
|
324 | |||
341 | This is useful for embedded shells. |
|
325 | This is useful for embedded shells. | |
342 | """ |
|
326 | """ | |
343 | if ipython_dir is None: |
|
327 | if ipython_dir is None: | |
344 | ipython_dir = get_ipython_dir() |
|
328 | ipython_dir = get_ipython_dir() | |
345 |
|
329 | |||
346 | profile_dir = os.path.join(ipython_dir, 'profile_default') |
|
330 | profile_dir = os.path.join(ipython_dir, 'profile_default') | |
347 | app = TerminalIPythonApp() |
|
331 | app = TerminalIPythonApp() | |
348 | app.config_file_paths.append(profile_dir) |
|
332 | app.config_file_paths.append(profile_dir) | |
349 | app.load_config_file() |
|
333 | app.load_config_file() | |
350 | return app.config |
|
334 | return app.config | |
351 |
|
335 | |||
352 | launch_new_instance = TerminalIPythonApp.launch_instance |
|
336 | launch_new_instance = TerminalIPythonApp.launch_instance | |
353 |
|
337 | |||
354 |
|
338 | |||
355 | if __name__ == '__main__': |
|
339 | if __name__ == '__main__': | |
356 | launch_new_instance() |
|
340 | launch_new_instance() |
@@ -1,440 +1,392 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for path handling. |
|
3 | Utilities for path handling. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | # Copyright (c) IPython Development Team. |
|
6 | # Copyright (c) IPython Development Team. | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 |
|
8 | |||
9 | import os |
|
9 | import os | |
10 | import sys |
|
10 | import sys | |
11 | import errno |
|
11 | import errno | |
12 | import shutil |
|
12 | import shutil | |
13 | import random |
|
13 | import random | |
14 | import glob |
|
14 | import glob | |
15 | from warnings import warn |
|
15 | from warnings import warn | |
16 |
|
16 | |||
17 | from IPython.utils.process import system |
|
17 | from IPython.utils.process import system | |
18 | from IPython.utils.decorators import undoc |
|
18 | from IPython.utils.decorators import undoc | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Code |
|
21 | # Code | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | fs_encoding = sys.getfilesystemencoding() |
|
23 | fs_encoding = sys.getfilesystemencoding() | |
24 |
|
24 | |||
25 | def _writable_dir(path): |
|
25 | def _writable_dir(path): | |
26 | """Whether `path` is a directory, to which the user has write access.""" |
|
26 | """Whether `path` is a directory, to which the user has write access.""" | |
27 | return os.path.isdir(path) and os.access(path, os.W_OK) |
|
27 | return os.path.isdir(path) and os.access(path, os.W_OK) | |
28 |
|
28 | |||
29 | if sys.platform == 'win32': |
|
29 | if sys.platform == 'win32': | |
30 | def _get_long_path_name(path): |
|
30 | def _get_long_path_name(path): | |
31 | """Get a long path name (expand ~) on Windows using ctypes. |
|
31 | """Get a long path name (expand ~) on Windows using ctypes. | |
32 |
|
32 | |||
33 | Examples |
|
33 | Examples | |
34 | -------- |
|
34 | -------- | |
35 |
|
35 | |||
36 | >>> get_long_path_name('c:\\\\docume~1') |
|
36 | >>> get_long_path_name('c:\\\\docume~1') | |
37 | 'c:\\\\Documents and Settings' |
|
37 | 'c:\\\\Documents and Settings' | |
38 |
|
38 | |||
39 | """ |
|
39 | """ | |
40 | try: |
|
40 | try: | |
41 | import ctypes |
|
41 | import ctypes | |
42 | except ImportError as e: |
|
42 | except ImportError as e: | |
43 | raise ImportError('you need to have ctypes installed for this to work') from e |
|
43 | raise ImportError('you need to have ctypes installed for this to work') from e | |
44 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW |
|
44 | _GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW | |
45 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, |
|
45 | _GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, | |
46 | ctypes.c_uint ] |
|
46 | ctypes.c_uint ] | |
47 |
|
47 | |||
48 | buf = ctypes.create_unicode_buffer(260) |
|
48 | buf = ctypes.create_unicode_buffer(260) | |
49 | rv = _GetLongPathName(path, buf, 260) |
|
49 | rv = _GetLongPathName(path, buf, 260) | |
50 | if rv == 0 or rv > 260: |
|
50 | if rv == 0 or rv > 260: | |
51 | return path |
|
51 | return path | |
52 | else: |
|
52 | else: | |
53 | return buf.value |
|
53 | return buf.value | |
54 | else: |
|
54 | else: | |
55 | def _get_long_path_name(path): |
|
55 | def _get_long_path_name(path): | |
56 | """Dummy no-op.""" |
|
56 | """Dummy no-op.""" | |
57 | return path |
|
57 | return path | |
58 |
|
58 | |||
59 |
|
59 | |||
60 |
|
60 | |||
61 | def get_long_path_name(path): |
|
61 | def get_long_path_name(path): | |
62 | """Expand a path into its long form. |
|
62 | """Expand a path into its long form. | |
63 |
|
63 | |||
64 | On Windows this expands any ~ in the paths. On other platforms, it is |
|
64 | On Windows this expands any ~ in the paths. On other platforms, it is | |
65 | a null operation. |
|
65 | a null operation. | |
66 | """ |
|
66 | """ | |
67 | return _get_long_path_name(path) |
|
67 | return _get_long_path_name(path) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | def unquote_filename(name, win32=(sys.platform=='win32')): |
|
|||
71 | """ On Windows, remove leading and trailing quotes from filenames. |
|
|||
72 |
|
||||
73 | This function has been deprecated and should not be used any more: |
|
|||
74 | unquoting is now taken care of by :func:`IPython.utils.process.arg_split`. |
|
|||
75 | """ |
|
|||
76 | warn("'unquote_filename' is deprecated since IPython 5.0 and should not " |
|
|||
77 | "be used anymore", DeprecationWarning, stacklevel=2) |
|
|||
78 | if win32: |
|
|||
79 | if name.startswith(("'", '"')) and name.endswith(("'", '"')): |
|
|||
80 | name = name[1:-1] |
|
|||
81 | return name |
|
|||
82 |
|
||||
83 |
|
||||
84 | def compress_user(path): |
|
70 | def compress_user(path): | |
85 | """Reverse of :func:`os.path.expanduser` |
|
71 | """Reverse of :func:`os.path.expanduser` | |
86 | """ |
|
72 | """ | |
87 | home = os.path.expanduser('~') |
|
73 | home = os.path.expanduser('~') | |
88 | if path.startswith(home): |
|
74 | if path.startswith(home): | |
89 | path = "~" + path[len(home):] |
|
75 | path = "~" + path[len(home):] | |
90 | return path |
|
76 | return path | |
91 |
|
77 | |||
92 |
def get_py_filename(name |
|
78 | def get_py_filename(name): | |
93 | """Return a valid python filename in the current directory. |
|
79 | """Return a valid python filename in the current directory. | |
94 |
|
80 | |||
95 | If the given name is not a file, it adds '.py' and searches again. |
|
81 | If the given name is not a file, it adds '.py' and searches again. | |
96 | Raises IOError with an informative message if the file isn't found. |
|
82 | Raises IOError with an informative message if the file isn't found. | |
97 | """ |
|
83 | """ | |
98 |
|
84 | |||
99 | name = os.path.expanduser(name) |
|
85 | name = os.path.expanduser(name) | |
100 | if force_win32 is not None: |
|
|||
101 | warn("The 'force_win32' argument to 'get_py_filename' is deprecated " |
|
|||
102 | "since IPython 5.0 and should not be used anymore", |
|
|||
103 | DeprecationWarning, stacklevel=2) |
|
|||
104 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
86 | if not os.path.isfile(name) and not name.endswith('.py'): | |
105 | name += '.py' |
|
87 | name += '.py' | |
106 | if os.path.isfile(name): |
|
88 | if os.path.isfile(name): | |
107 | return name |
|
89 | return name | |
108 | else: |
|
90 | else: | |
109 | raise IOError('File `%r` not found.' % name) |
|
91 | raise IOError('File `%r` not found.' % name) | |
110 |
|
92 | |||
111 |
|
93 | |||
112 | def filefind(filename: str, path_dirs=None) -> str: |
|
94 | def filefind(filename: str, path_dirs=None) -> str: | |
113 | """Find a file by looking through a sequence of paths. |
|
95 | """Find a file by looking through a sequence of paths. | |
114 |
|
96 | |||
115 | This iterates through a sequence of paths looking for a file and returns |
|
97 | This iterates through a sequence of paths looking for a file and returns | |
116 | the full, absolute path of the first occurrence of the file. If no set of |
|
98 | the full, absolute path of the first occurrence of the file. If no set of | |
117 | path dirs is given, the filename is tested as is, after running through |
|
99 | path dirs is given, the filename is tested as is, after running through | |
118 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: |
|
100 | :func:`expandvars` and :func:`expanduser`. Thus a simple call:: | |
119 |
|
101 | |||
120 | filefind('myfile.txt') |
|
102 | filefind('myfile.txt') | |
121 |
|
103 | |||
122 | will find the file in the current working dir, but:: |
|
104 | will find the file in the current working dir, but:: | |
123 |
|
105 | |||
124 | filefind('~/myfile.txt') |
|
106 | filefind('~/myfile.txt') | |
125 |
|
107 | |||
126 | Will find the file in the users home directory. This function does not |
|
108 | Will find the file in the users home directory. This function does not | |
127 | automatically try any paths, such as the cwd or the user's home directory. |
|
109 | automatically try any paths, such as the cwd or the user's home directory. | |
128 |
|
110 | |||
129 | Parameters |
|
111 | Parameters | |
130 | ---------- |
|
112 | ---------- | |
131 | filename : str |
|
113 | filename : str | |
132 | The filename to look for. |
|
114 | The filename to look for. | |
133 | path_dirs : str, None or sequence of str |
|
115 | path_dirs : str, None or sequence of str | |
134 | The sequence of paths to look for the file in. If None, the filename |
|
116 | The sequence of paths to look for the file in. If None, the filename | |
135 | need to be absolute or be in the cwd. If a string, the string is |
|
117 | need to be absolute or be in the cwd. If a string, the string is | |
136 | put into a sequence and the searched. If a sequence, walk through |
|
118 | put into a sequence and the searched. If a sequence, walk through | |
137 | each element and join with ``filename``, calling :func:`expandvars` |
|
119 | each element and join with ``filename``, calling :func:`expandvars` | |
138 | and :func:`expanduser` before testing for existence. |
|
120 | and :func:`expanduser` before testing for existence. | |
139 |
|
121 | |||
140 | Returns |
|
122 | Returns | |
141 | ------- |
|
123 | ------- | |
142 | path : str |
|
124 | path : str | |
143 | returns absolute path to file. |
|
125 | returns absolute path to file. | |
144 |
|
126 | |||
145 | Raises |
|
127 | Raises | |
146 | ------ |
|
128 | ------ | |
147 | IOError |
|
129 | IOError | |
148 | """ |
|
130 | """ | |
149 |
|
131 | |||
150 | # If paths are quoted, abspath gets confused, strip them... |
|
132 | # If paths are quoted, abspath gets confused, strip them... | |
151 | filename = filename.strip('"').strip("'") |
|
133 | filename = filename.strip('"').strip("'") | |
152 | # If the input is an absolute path, just check it exists |
|
134 | # If the input is an absolute path, just check it exists | |
153 | if os.path.isabs(filename) and os.path.isfile(filename): |
|
135 | if os.path.isabs(filename) and os.path.isfile(filename): | |
154 | return filename |
|
136 | return filename | |
155 |
|
137 | |||
156 | if path_dirs is None: |
|
138 | if path_dirs is None: | |
157 | path_dirs = ("",) |
|
139 | path_dirs = ("",) | |
158 | elif isinstance(path_dirs, str): |
|
140 | elif isinstance(path_dirs, str): | |
159 | path_dirs = (path_dirs,) |
|
141 | path_dirs = (path_dirs,) | |
160 |
|
142 | |||
161 | for path in path_dirs: |
|
143 | for path in path_dirs: | |
162 | if path == '.': path = os.getcwd() |
|
144 | if path == '.': path = os.getcwd() | |
163 | testname = expand_path(os.path.join(path, filename)) |
|
145 | testname = expand_path(os.path.join(path, filename)) | |
164 | if os.path.isfile(testname): |
|
146 | if os.path.isfile(testname): | |
165 | return os.path.abspath(testname) |
|
147 | return os.path.abspath(testname) | |
166 |
|
148 | |||
167 | raise IOError("File %r does not exist in any of the search paths: %r" % |
|
149 | raise IOError("File %r does not exist in any of the search paths: %r" % | |
168 | (filename, path_dirs) ) |
|
150 | (filename, path_dirs) ) | |
169 |
|
151 | |||
170 |
|
152 | |||
171 | class HomeDirError(Exception): |
|
153 | class HomeDirError(Exception): | |
172 | pass |
|
154 | pass | |
173 |
|
155 | |||
174 |
|
156 | |||
175 | def get_home_dir(require_writable=False) -> str: |
|
157 | def get_home_dir(require_writable=False) -> str: | |
176 | """Return the 'home' directory, as a unicode string. |
|
158 | """Return the 'home' directory, as a unicode string. | |
177 |
|
159 | |||
178 | Uses os.path.expanduser('~'), and checks for writability. |
|
160 | Uses os.path.expanduser('~'), and checks for writability. | |
179 |
|
161 | |||
180 | See stdlib docs for how this is determined. |
|
162 | See stdlib docs for how this is determined. | |
181 | For Python <3.8, $HOME is first priority on *ALL* platforms. |
|
163 | For Python <3.8, $HOME is first priority on *ALL* platforms. | |
182 | For Python >=3.8 on Windows, %HOME% is no longer considered. |
|
164 | For Python >=3.8 on Windows, %HOME% is no longer considered. | |
183 |
|
165 | |||
184 | Parameters |
|
166 | Parameters | |
185 | ---------- |
|
167 | ---------- | |
186 | require_writable : bool [default: False] |
|
168 | require_writable : bool [default: False] | |
187 | if True: |
|
169 | if True: | |
188 | guarantees the return value is a writable directory, otherwise |
|
170 | guarantees the return value is a writable directory, otherwise | |
189 | raises HomeDirError |
|
171 | raises HomeDirError | |
190 | if False: |
|
172 | if False: | |
191 | The path is resolved, but it is not guaranteed to exist or be writable. |
|
173 | The path is resolved, but it is not guaranteed to exist or be writable. | |
192 | """ |
|
174 | """ | |
193 |
|
175 | |||
194 | homedir = os.path.expanduser('~') |
|
176 | homedir = os.path.expanduser('~') | |
195 | # Next line will make things work even when /home/ is a symlink to |
|
177 | # Next line will make things work even when /home/ is a symlink to | |
196 | # /usr/home as it is on FreeBSD, for example |
|
178 | # /usr/home as it is on FreeBSD, for example | |
197 | homedir = os.path.realpath(homedir) |
|
179 | homedir = os.path.realpath(homedir) | |
198 |
|
180 | |||
199 | if not _writable_dir(homedir) and os.name == 'nt': |
|
181 | if not _writable_dir(homedir) and os.name == 'nt': | |
200 | # expanduser failed, use the registry to get the 'My Documents' folder. |
|
182 | # expanduser failed, use the registry to get the 'My Documents' folder. | |
201 | try: |
|
183 | try: | |
202 | import winreg as wreg |
|
184 | import winreg as wreg | |
203 | with wreg.OpenKey( |
|
185 | with wreg.OpenKey( | |
204 | wreg.HKEY_CURRENT_USER, |
|
186 | wreg.HKEY_CURRENT_USER, | |
205 | r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |
|
187 | r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" | |
206 | ) as key: |
|
188 | ) as key: | |
207 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
189 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
208 | except: |
|
190 | except: | |
209 | pass |
|
191 | pass | |
210 |
|
192 | |||
211 | if (not require_writable) or _writable_dir(homedir): |
|
193 | if (not require_writable) or _writable_dir(homedir): | |
212 | assert isinstance(homedir, str), "Homedir should be unicode not bytes" |
|
194 | assert isinstance(homedir, str), "Homedir should be unicode not bytes" | |
213 | return homedir |
|
195 | return homedir | |
214 | else: |
|
196 | else: | |
215 | raise HomeDirError('%s is not a writable dir, ' |
|
197 | raise HomeDirError('%s is not a writable dir, ' | |
216 | 'set $HOME environment variable to override' % homedir) |
|
198 | 'set $HOME environment variable to override' % homedir) | |
217 |
|
199 | |||
218 | def get_xdg_dir(): |
|
200 | def get_xdg_dir(): | |
219 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. |
|
201 | """Return the XDG_CONFIG_HOME, if it is defined and exists, else None. | |
220 |
|
202 | |||
221 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
203 | This is only for non-OS X posix (Linux,Unix,etc.) systems. | |
222 | """ |
|
204 | """ | |
223 |
|
205 | |||
224 | env = os.environ |
|
206 | env = os.environ | |
225 |
|
207 | |||
226 | if os.name == 'posix' and sys.platform != 'darwin': |
|
208 | if os.name == 'posix' and sys.platform != 'darwin': | |
227 | # Linux, Unix, AIX, etc. |
|
209 | # Linux, Unix, AIX, etc. | |
228 | # use ~/.config if empty OR not set |
|
210 | # use ~/.config if empty OR not set | |
229 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') |
|
211 | xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config') | |
230 | if xdg and _writable_dir(xdg): |
|
212 | if xdg and _writable_dir(xdg): | |
231 | assert isinstance(xdg, str) |
|
213 | assert isinstance(xdg, str) | |
232 | return xdg |
|
214 | return xdg | |
233 |
|
215 | |||
234 | return None |
|
216 | return None | |
235 |
|
217 | |||
236 |
|
218 | |||
237 | def get_xdg_cache_dir(): |
|
219 | def get_xdg_cache_dir(): | |
238 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. |
|
220 | """Return the XDG_CACHE_HOME, if it is defined and exists, else None. | |
239 |
|
221 | |||
240 | This is only for non-OS X posix (Linux,Unix,etc.) systems. |
|
222 | This is only for non-OS X posix (Linux,Unix,etc.) systems. | |
241 | """ |
|
223 | """ | |
242 |
|
224 | |||
243 | env = os.environ |
|
225 | env = os.environ | |
244 |
|
226 | |||
245 | if os.name == 'posix' and sys.platform != 'darwin': |
|
227 | if os.name == 'posix' and sys.platform != 'darwin': | |
246 | # Linux, Unix, AIX, etc. |
|
228 | # Linux, Unix, AIX, etc. | |
247 | # use ~/.cache if empty OR not set |
|
229 | # use ~/.cache if empty OR not set | |
248 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') |
|
230 | xdg = env.get("XDG_CACHE_HOME", None) or os.path.join(get_home_dir(), '.cache') | |
249 | if xdg and _writable_dir(xdg): |
|
231 | if xdg and _writable_dir(xdg): | |
250 | assert isinstance(xdg, str) |
|
232 | assert isinstance(xdg, str) | |
251 | return xdg |
|
233 | return xdg | |
252 |
|
234 | |||
253 | return None |
|
235 | return None | |
254 |
|
236 | |||
255 |
|
237 | |||
256 | @undoc |
|
|||
257 | def get_ipython_dir(): |
|
|||
258 | warn("get_ipython_dir has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) |
|
|||
259 | from IPython.paths import get_ipython_dir |
|
|||
260 | return get_ipython_dir() |
|
|||
261 |
|
||||
262 | @undoc |
|
|||
263 | def get_ipython_cache_dir(): |
|
|||
264 | warn("get_ipython_cache_dir has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) |
|
|||
265 | from IPython.paths import get_ipython_cache_dir |
|
|||
266 | return get_ipython_cache_dir() |
|
|||
267 |
|
||||
268 | @undoc |
|
|||
269 | def get_ipython_package_dir(): |
|
|||
270 | warn("get_ipython_package_dir has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) |
|
|||
271 | from IPython.paths import get_ipython_package_dir |
|
|||
272 | return get_ipython_package_dir() |
|
|||
273 |
|
||||
274 | @undoc |
|
|||
275 | def get_ipython_module_path(module_str): |
|
|||
276 | warn("get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) |
|
|||
277 | from IPython.paths import get_ipython_module_path |
|
|||
278 | return get_ipython_module_path(module_str) |
|
|||
279 |
|
||||
280 | @undoc |
|
|||
281 | def locate_profile(profile='default'): |
|
|||
282 | warn("locate_profile has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) |
|
|||
283 | from IPython.paths import locate_profile |
|
|||
284 | return locate_profile(profile=profile) |
|
|||
285 |
|
||||
286 | def expand_path(s): |
|
238 | def expand_path(s): | |
287 | """Expand $VARS and ~names in a string, like a shell |
|
239 | """Expand $VARS and ~names in a string, like a shell | |
288 |
|
240 | |||
289 | :Examples: |
|
241 | :Examples: | |
290 |
|
242 | |||
291 | In [2]: os.environ['FOO']='test' |
|
243 | In [2]: os.environ['FOO']='test' | |
292 |
|
244 | |||
293 | In [3]: expand_path('variable FOO is $FOO') |
|
245 | In [3]: expand_path('variable FOO is $FOO') | |
294 | Out[3]: 'variable FOO is test' |
|
246 | Out[3]: 'variable FOO is test' | |
295 | """ |
|
247 | """ | |
296 | # This is a pretty subtle hack. When expand user is given a UNC path |
|
248 | # This is a pretty subtle hack. When expand user is given a UNC path | |
297 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes |
|
249 | # on Windows (\\server\share$\%username%), os.path.expandvars, removes | |
298 | # the $ to get (\\server\share\%username%). I think it considered $ |
|
250 | # the $ to get (\\server\share\%username%). I think it considered $ | |
299 | # alone an empty var. But, we need the $ to remains there (it indicates |
|
251 | # alone an empty var. But, we need the $ to remains there (it indicates | |
300 | # a hidden share). |
|
252 | # a hidden share). | |
301 | if os.name=='nt': |
|
253 | if os.name=='nt': | |
302 | s = s.replace('$\\', 'IPYTHON_TEMP') |
|
254 | s = s.replace('$\\', 'IPYTHON_TEMP') | |
303 | s = os.path.expandvars(os.path.expanduser(s)) |
|
255 | s = os.path.expandvars(os.path.expanduser(s)) | |
304 | if os.name=='nt': |
|
256 | if os.name=='nt': | |
305 | s = s.replace('IPYTHON_TEMP', '$\\') |
|
257 | s = s.replace('IPYTHON_TEMP', '$\\') | |
306 | return s |
|
258 | return s | |
307 |
|
259 | |||
308 |
|
260 | |||
309 | def unescape_glob(string): |
|
261 | def unescape_glob(string): | |
310 | """Unescape glob pattern in `string`.""" |
|
262 | """Unescape glob pattern in `string`.""" | |
311 | def unescape(s): |
|
263 | def unescape(s): | |
312 | for pattern in '*[]!?': |
|
264 | for pattern in '*[]!?': | |
313 | s = s.replace(r'\{0}'.format(pattern), pattern) |
|
265 | s = s.replace(r'\{0}'.format(pattern), pattern) | |
314 | return s |
|
266 | return s | |
315 | return '\\'.join(map(unescape, string.split('\\\\'))) |
|
267 | return '\\'.join(map(unescape, string.split('\\\\'))) | |
316 |
|
268 | |||
317 |
|
269 | |||
318 | def shellglob(args): |
|
270 | def shellglob(args): | |
319 | """ |
|
271 | """ | |
320 | Do glob expansion for each element in `args` and return a flattened list. |
|
272 | Do glob expansion for each element in `args` and return a flattened list. | |
321 |
|
273 | |||
322 | Unmatched glob pattern will remain as-is in the returned list. |
|
274 | Unmatched glob pattern will remain as-is in the returned list. | |
323 |
|
275 | |||
324 | """ |
|
276 | """ | |
325 | expanded = [] |
|
277 | expanded = [] | |
326 | # Do not unescape backslash in Windows as it is interpreted as |
|
278 | # Do not unescape backslash in Windows as it is interpreted as | |
327 | # path separator: |
|
279 | # path separator: | |
328 | unescape = unescape_glob if sys.platform != 'win32' else lambda x: x |
|
280 | unescape = unescape_glob if sys.platform != 'win32' else lambda x: x | |
329 | for a in args: |
|
281 | for a in args: | |
330 | expanded.extend(glob.glob(a) or [unescape(a)]) |
|
282 | expanded.extend(glob.glob(a) or [unescape(a)]) | |
331 | return expanded |
|
283 | return expanded | |
332 |
|
284 | |||
333 |
|
285 | |||
334 | def target_outdated(target,deps): |
|
286 | def target_outdated(target,deps): | |
335 | """Determine whether a target is out of date. |
|
287 | """Determine whether a target is out of date. | |
336 |
|
288 | |||
337 | target_outdated(target,deps) -> 1/0 |
|
289 | target_outdated(target,deps) -> 1/0 | |
338 |
|
290 | |||
339 | deps: list of filenames which MUST exist. |
|
291 | deps: list of filenames which MUST exist. | |
340 | target: single filename which may or may not exist. |
|
292 | target: single filename which may or may not exist. | |
341 |
|
293 | |||
342 | If target doesn't exist or is older than any file listed in deps, return |
|
294 | If target doesn't exist or is older than any file listed in deps, return | |
343 | true, otherwise return false. |
|
295 | true, otherwise return false. | |
344 | """ |
|
296 | """ | |
345 | try: |
|
297 | try: | |
346 | target_time = os.path.getmtime(target) |
|
298 | target_time = os.path.getmtime(target) | |
347 | except os.error: |
|
299 | except os.error: | |
348 | return 1 |
|
300 | return 1 | |
349 | for dep in deps: |
|
301 | for dep in deps: | |
350 | dep_time = os.path.getmtime(dep) |
|
302 | dep_time = os.path.getmtime(dep) | |
351 | if dep_time > target_time: |
|
303 | if dep_time > target_time: | |
352 | #print "For target",target,"Dep failed:",dep # dbg |
|
304 | #print "For target",target,"Dep failed:",dep # dbg | |
353 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
305 | #print "times (dep,tar):",dep_time,target_time # dbg | |
354 | return 1 |
|
306 | return 1 | |
355 | return 0 |
|
307 | return 0 | |
356 |
|
308 | |||
357 |
|
309 | |||
358 | def target_update(target,deps,cmd): |
|
310 | def target_update(target,deps,cmd): | |
359 | """Update a target with a given command given a list of dependencies. |
|
311 | """Update a target with a given command given a list of dependencies. | |
360 |
|
312 | |||
361 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
313 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
362 |
|
314 | |||
363 | This is just a wrapper around target_outdated() which calls the given |
|
315 | This is just a wrapper around target_outdated() which calls the given | |
364 | command if target is outdated.""" |
|
316 | command if target is outdated.""" | |
365 |
|
317 | |||
366 | if target_outdated(target,deps): |
|
318 | if target_outdated(target,deps): | |
367 | system(cmd) |
|
319 | system(cmd) | |
368 |
|
320 | |||
369 |
|
321 | |||
370 | ENOLINK = 1998 |
|
322 | ENOLINK = 1998 | |
371 |
|
323 | |||
372 | def link(src, dst): |
|
324 | def link(src, dst): | |
373 | """Hard links ``src`` to ``dst``, returning 0 or errno. |
|
325 | """Hard links ``src`` to ``dst``, returning 0 or errno. | |
374 |
|
326 | |||
375 | Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't |
|
327 | Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't | |
376 | supported by the operating system. |
|
328 | supported by the operating system. | |
377 | """ |
|
329 | """ | |
378 |
|
330 | |||
379 | if not hasattr(os, "link"): |
|
331 | if not hasattr(os, "link"): | |
380 | return ENOLINK |
|
332 | return ENOLINK | |
381 | link_errno = 0 |
|
333 | link_errno = 0 | |
382 | try: |
|
334 | try: | |
383 | os.link(src, dst) |
|
335 | os.link(src, dst) | |
384 | except OSError as e: |
|
336 | except OSError as e: | |
385 | link_errno = e.errno |
|
337 | link_errno = e.errno | |
386 | return link_errno |
|
338 | return link_errno | |
387 |
|
339 | |||
388 |
|
340 | |||
389 | def link_or_copy(src, dst): |
|
341 | def link_or_copy(src, dst): | |
390 | """Attempts to hardlink ``src`` to ``dst``, copying if the link fails. |
|
342 | """Attempts to hardlink ``src`` to ``dst``, copying if the link fails. | |
391 |
|
343 | |||
392 | Attempts to maintain the semantics of ``shutil.copy``. |
|
344 | Attempts to maintain the semantics of ``shutil.copy``. | |
393 |
|
345 | |||
394 | Because ``os.link`` does not overwrite files, a unique temporary file |
|
346 | Because ``os.link`` does not overwrite files, a unique temporary file | |
395 | will be used if the target already exists, then that file will be moved |
|
347 | will be used if the target already exists, then that file will be moved | |
396 | into place. |
|
348 | into place. | |
397 | """ |
|
349 | """ | |
398 |
|
350 | |||
399 | if os.path.isdir(dst): |
|
351 | if os.path.isdir(dst): | |
400 | dst = os.path.join(dst, os.path.basename(src)) |
|
352 | dst = os.path.join(dst, os.path.basename(src)) | |
401 |
|
353 | |||
402 | link_errno = link(src, dst) |
|
354 | link_errno = link(src, dst) | |
403 | if link_errno == errno.EEXIST: |
|
355 | if link_errno == errno.EEXIST: | |
404 | if os.stat(src).st_ino == os.stat(dst).st_ino: |
|
356 | if os.stat(src).st_ino == os.stat(dst).st_ino: | |
405 | # dst is already a hard link to the correct file, so we don't need |
|
357 | # dst is already a hard link to the correct file, so we don't need | |
406 | # to do anything else. If we try to link and rename the file |
|
358 | # to do anything else. If we try to link and rename the file | |
407 | # anyway, we get duplicate files - see http://bugs.python.org/issue21876 |
|
359 | # anyway, we get duplicate files - see http://bugs.python.org/issue21876 | |
408 | return |
|
360 | return | |
409 |
|
361 | |||
410 | new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), ) |
|
362 | new_dst = dst + "-temp-%04X" %(random.randint(1, 16**4), ) | |
411 | try: |
|
363 | try: | |
412 | link_or_copy(src, new_dst) |
|
364 | link_or_copy(src, new_dst) | |
413 | except: |
|
365 | except: | |
414 | try: |
|
366 | try: | |
415 | os.remove(new_dst) |
|
367 | os.remove(new_dst) | |
416 | except OSError: |
|
368 | except OSError: | |
417 | pass |
|
369 | pass | |
418 | raise |
|
370 | raise | |
419 | os.rename(new_dst, dst) |
|
371 | os.rename(new_dst, dst) | |
420 | elif link_errno != 0: |
|
372 | elif link_errno != 0: | |
421 | # Either link isn't supported, or the filesystem doesn't support |
|
373 | # Either link isn't supported, or the filesystem doesn't support | |
422 | # linking, or 'src' and 'dst' are on different filesystems. |
|
374 | # linking, or 'src' and 'dst' are on different filesystems. | |
423 | shutil.copy(src, dst) |
|
375 | shutil.copy(src, dst) | |
424 |
|
376 | |||
425 | def ensure_dir_exists(path, mode=0o755): |
|
377 | def ensure_dir_exists(path, mode=0o755): | |
426 | """ensure that a directory exists |
|
378 | """ensure that a directory exists | |
427 |
|
379 | |||
428 | If it doesn't exist, try to create it and protect against a race condition |
|
380 | If it doesn't exist, try to create it and protect against a race condition | |
429 | if another process is doing the same. |
|
381 | if another process is doing the same. | |
430 |
|
382 | |||
431 | The default permissions are 755, which differ from os.makedirs default of 777. |
|
383 | The default permissions are 755, which differ from os.makedirs default of 777. | |
432 | """ |
|
384 | """ | |
433 | if not os.path.exists(path): |
|
385 | if not os.path.exists(path): | |
434 | try: |
|
386 | try: | |
435 | os.makedirs(path, mode=mode) |
|
387 | os.makedirs(path, mode=mode) | |
436 | except OSError as e: |
|
388 | except OSError as e: | |
437 | if e.errno != errno.EEXIST: |
|
389 | if e.errno != errno.EEXIST: | |
438 | raise |
|
390 | raise | |
439 | elif not os.path.isdir(path): |
|
391 | elif not os.path.isdir(path): | |
440 | raise IOError("%r exists but is not a directory" % path) |
|
392 | raise IOError("%r exists but is not a directory" % path) |
@@ -1,122 +1,123 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for timing code execution. |
|
3 | Utilities for timing code execution. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
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 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import time |
|
17 | import time | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Code |
|
20 | # Code | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | # If possible (Unix), use the resource module instead of time.clock() |
|
23 | # If possible (Unix), use the resource module instead of time.clock() | |
24 | try: |
|
24 | try: | |
25 | import resource |
|
25 | import resource | |
26 | except ImportError: |
|
26 | except ImportError: | |
27 | resource = None |
|
27 | resource = None | |
28 |
|
28 | |||
29 | # Some implementations (like jyputerlite) don't have getrusage |
|
29 | # Some implementations (like jyputerlite) don't have getrusage | |
30 | if resource is not None and hasattr(resource, "getrusage"): |
|
30 | if resource is not None and hasattr(resource, "getrusage"): | |
31 | def clocku(): |
|
31 | def clocku(): | |
32 | """clocku() -> floating point number |
|
32 | """clocku() -> floating point number | |
33 |
|
33 | |||
34 | Return the *USER* CPU time in seconds since the start of the process. |
|
34 | Return the *USER* CPU time in seconds since the start of the process. | |
35 | This is done via a call to resource.getrusage, so it avoids the |
|
35 | This is done via a call to resource.getrusage, so it avoids the | |
36 | wraparound problems in time.clock().""" |
|
36 | wraparound problems in time.clock().""" | |
37 |
|
37 | |||
38 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
38 | return resource.getrusage(resource.RUSAGE_SELF)[0] | |
39 |
|
39 | |||
40 | def clocks(): |
|
40 | def clocks(): | |
41 | """clocks() -> floating point number |
|
41 | """clocks() -> floating point number | |
42 |
|
42 | |||
43 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
43 | Return the *SYSTEM* CPU time in seconds since the start of the process. | |
44 | This is done via a call to resource.getrusage, so it avoids the |
|
44 | This is done via a call to resource.getrusage, so it avoids the | |
45 | wraparound problems in time.clock().""" |
|
45 | wraparound problems in time.clock().""" | |
46 |
|
46 | |||
47 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
47 | return resource.getrusage(resource.RUSAGE_SELF)[1] | |
48 |
|
48 | |||
49 | def clock(): |
|
49 | def clock(): | |
50 | """clock() -> floating point number |
|
50 | """clock() -> floating point number | |
51 |
|
51 | |||
52 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
52 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of | |
53 | the process. This is done via a call to resource.getrusage, so it |
|
53 | the process. This is done via a call to resource.getrusage, so it | |
54 | avoids the wraparound problems in time.clock().""" |
|
54 | avoids the wraparound problems in time.clock().""" | |
55 |
|
55 | |||
56 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
56 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] | |
57 | return u+s |
|
57 | return u+s | |
58 |
|
58 | |||
59 | def clock2(): |
|
59 | def clock2(): | |
60 | """clock2() -> (t_user,t_system) |
|
60 | """clock2() -> (t_user,t_system) | |
61 |
|
61 | |||
62 | Similar to clock(), but return a tuple of user/system times.""" |
|
62 | Similar to clock(), but return a tuple of user/system times.""" | |
63 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
63 | return resource.getrusage(resource.RUSAGE_SELF)[:2] | |
64 |
|
64 | |||
|
65 | ||||
65 | else: |
|
66 | else: | |
66 | # There is no distinction of user/system time under windows, so we just use |
|
67 | # There is no distinction of user/system time under windows, so we just use | |
67 | # time.perff_counter() for everything... |
|
68 | # time.perff_counter() for everything... | |
68 | clocku = clocks = clock = time.perf_counter |
|
69 | clocku = clocks = clock = time.perf_counter | |
69 | def clock2(): |
|
70 | def clock2(): | |
70 | """Under windows, system CPU time can't be measured. |
|
71 | """Under windows, system CPU time can't be measured. | |
71 |
|
72 | |||
72 | This just returns perf_counter() and zero.""" |
|
73 | This just returns perf_counter() and zero.""" | |
73 | return time.perf_counter(),0.0 |
|
74 | return time.perf_counter(),0.0 | |
74 |
|
75 | |||
75 |
|
76 | |||
76 | def timings_out(reps,func,*args,**kw): |
|
77 | def timings_out(reps,func,*args,**kw): | |
77 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
78 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) | |
78 |
|
79 | |||
79 | Execute a function reps times, return a tuple with the elapsed total |
|
80 | Execute a function reps times, return a tuple with the elapsed total | |
80 | CPU time in seconds, the time per call and the function's output. |
|
81 | CPU time in seconds, the time per call and the function's output. | |
81 |
|
82 | |||
82 | Under Unix, the return value is the sum of user+system time consumed by |
|
83 | Under Unix, the return value is the sum of user+system time consumed by | |
83 | the process, computed via the resource module. This prevents problems |
|
84 | the process, computed via the resource module. This prevents problems | |
84 | related to the wraparound effect which the time.clock() function has. |
|
85 | related to the wraparound effect which the time.clock() function has. | |
85 |
|
86 | |||
86 | Under Windows the return value is in wall clock seconds. See the |
|
87 | Under Windows the return value is in wall clock seconds. See the | |
87 | documentation for the time module for more details.""" |
|
88 | documentation for the time module for more details.""" | |
88 |
|
89 | |||
89 | reps = int(reps) |
|
90 | reps = int(reps) | |
90 | assert reps >=1, 'reps must be >= 1' |
|
91 | assert reps >=1, 'reps must be >= 1' | |
91 | if reps==1: |
|
92 | if reps==1: | |
92 | start = clock() |
|
93 | start = clock() | |
93 | out = func(*args,**kw) |
|
94 | out = func(*args,**kw) | |
94 | tot_time = clock()-start |
|
95 | tot_time = clock()-start | |
95 | else: |
|
96 | else: | |
96 | rng = range(reps-1) # the last time is executed separately to store output |
|
97 | rng = range(reps-1) # the last time is executed separately to store output | |
97 | start = clock() |
|
98 | start = clock() | |
98 | for dummy in rng: func(*args,**kw) |
|
99 | for dummy in rng: func(*args,**kw) | |
99 | out = func(*args,**kw) # one last time |
|
100 | out = func(*args,**kw) # one last time | |
100 | tot_time = clock()-start |
|
101 | tot_time = clock()-start | |
101 | av_time = tot_time / reps |
|
102 | av_time = tot_time / reps | |
102 | return tot_time,av_time,out |
|
103 | return tot_time,av_time,out | |
103 |
|
104 | |||
104 |
|
105 | |||
105 | def timings(reps,func,*args,**kw): |
|
106 | def timings(reps,func,*args,**kw): | |
106 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
107 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) | |
107 |
|
108 | |||
108 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
109 | Execute a function reps times, return a tuple with the elapsed total CPU | |
109 | time in seconds and the time per call. These are just the first two values |
|
110 | time in seconds and the time per call. These are just the first two values | |
110 | in timings_out().""" |
|
111 | in timings_out().""" | |
111 |
|
112 | |||
112 | return timings_out(reps,func,*args,**kw)[0:2] |
|
113 | return timings_out(reps,func,*args,**kw)[0:2] | |
113 |
|
114 | |||
114 |
|
115 | |||
115 | def timing(func,*args,**kw): |
|
116 | def timing(func,*args,**kw): | |
116 | """timing(func,*args,**kw) -> t_total |
|
117 | """timing(func,*args,**kw) -> t_total | |
117 |
|
118 | |||
118 | Execute a function once, return the elapsed total CPU time in |
|
119 | Execute a function once, return the elapsed total CPU time in | |
119 | seconds. This is just the first value in timings_out().""" |
|
120 | seconds. This is just the first value in timings_out().""" | |
120 |
|
121 | |||
121 | return timings_out(1,func,*args,**kw)[0] |
|
122 | return timings_out(1,func,*args,**kw)[0] | |
122 |
|
123 |
@@ -1,185 +1,184 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Setup script for IPython. |
|
2 | """Setup script for IPython. | |
3 |
|
3 | |||
4 | Under Posix environments it works like a typical setup.py script. |
|
4 | Under Posix environments it works like a typical setup.py script. | |
5 | Under Windows, the command sdist is not supported, since IPython |
|
5 | Under Windows, the command sdist is not supported, since IPython | |
6 | requires utilities which are not available under Windows.""" |
|
6 | requires utilities which are not available under Windows.""" | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (c) 2008-2011, IPython Development Team. |
|
9 | # Copyright (c) 2008-2011, IPython Development Team. | |
10 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> |
|
10 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> | |
11 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> |
|
11 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> | |
12 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> |
|
12 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the Modified BSD License. |
|
14 | # Distributed under the terms of the Modified BSD License. | |
15 | # |
|
15 | # | |
16 | # The full license is in the file COPYING.rst, distributed with this software. |
|
16 | # The full license is in the file COPYING.rst, distributed with this software. | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import sys |
|
20 | import sys | |
21 | from itertools import chain |
|
21 | from itertools import chain | |
22 |
|
22 | |||
23 | # **Python version check** |
|
23 | # **Python version check** | |
24 | # |
|
24 | # | |
25 | # This check is also made in IPython/__init__, don't forget to update both when |
|
25 | # This check is also made in IPython/__init__, don't forget to update both when | |
26 | # changing Python version requirements. |
|
26 | # changing Python version requirements. | |
27 | if sys.version_info < (3, 8): |
|
27 | if sys.version_info < (3, 8): | |
28 | pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.' |
|
28 | pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.' | |
29 | try: |
|
29 | try: | |
30 | import pip |
|
30 | import pip | |
31 | pip_version = tuple([int(x) for x in pip.__version__.split('.')[:3]]) |
|
31 | pip_version = tuple([int(x) for x in pip.__version__.split('.')[:3]]) | |
32 | if pip_version < (9, 0, 1) : |
|
32 | if pip_version < (9, 0, 1) : | |
33 | pip_message = 'Your pip version is out of date, please install pip >= 9.0.1. '\ |
|
33 | pip_message = 'Your pip version is out of date, please install pip >= 9.0.1. '\ | |
34 | 'pip {} detected.'.format(pip.__version__) |
|
34 | 'pip {} detected.'.format(pip.__version__) | |
35 | else: |
|
35 | else: | |
36 | # pip is new enough - it must be something else |
|
36 | # pip is new enough - it must be something else | |
37 | pip_message = '' |
|
37 | pip_message = '' | |
38 | except Exception: |
|
38 | except Exception: | |
39 | pass |
|
39 | pass | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | error = """ |
|
42 | error = """ | |
43 | IPython 8+ supports Python 3.8 and above, following NEP 29. |
|
43 | IPython 8+ supports Python 3.8 and above, following NEP 29. | |
44 | When using Python 2.7, please install IPython 5.x LTS Long Term Support version. |
|
44 | When using Python 2.7, please install IPython 5.x LTS Long Term Support version. | |
45 | Python 3.3 and 3.4 were supported up to IPython 6.x. |
|
45 | Python 3.3 and 3.4 were supported up to IPython 6.x. | |
46 | Python 3.5 was supported with IPython 7.0 to 7.9. |
|
46 | Python 3.5 was supported with IPython 7.0 to 7.9. | |
47 | Python 3.6 was supported with IPython up to 7.16. |
|
47 | Python 3.6 was supported with IPython up to 7.16. | |
48 | Python 3.7 was still supported with the 7.x branch. |
|
48 | Python 3.7 was still supported with the 7.x branch. | |
49 |
|
49 | |||
50 | See IPython `README.rst` file for more information: |
|
50 | See IPython `README.rst` file for more information: | |
51 |
|
51 | |||
52 | https://github.com/ipython/ipython/blob/master/README.rst |
|
52 | https://github.com/ipython/ipython/blob/master/README.rst | |
53 |
|
53 | |||
54 | Python {py} detected. |
|
54 | Python {py} detected. | |
55 | {pip} |
|
55 | {pip} | |
56 | """.format(py=sys.version_info, pip=pip_message ) |
|
56 | """.format(py=sys.version_info, pip=pip_message ) | |
57 |
|
57 | |||
58 | print(error, file=sys.stderr) |
|
58 | print(error, file=sys.stderr) | |
59 | sys.exit(1) |
|
59 | sys.exit(1) | |
60 |
|
60 | |||
61 | # At least we're on the python version we need, move on. |
|
61 | # At least we're on the python version we need, move on. | |
62 |
|
62 | |||
63 | from setuptools import setup |
|
63 | from setuptools import setup | |
64 |
|
64 | |||
65 | # Our own imports |
|
65 | # Our own imports | |
66 | from setupbase import target_update |
|
66 | from setupbase import target_update | |
67 |
|
67 | |||
68 | from setupbase import ( |
|
68 | from setupbase import ( | |
69 | setup_args, |
|
69 | setup_args, | |
70 | check_package_data_first, |
|
70 | check_package_data_first, | |
71 | find_data_files, |
|
71 | find_data_files, | |
72 | git_prebuild, |
|
72 | git_prebuild, | |
73 | install_symlinked, |
|
73 | install_symlinked, | |
74 | install_lib_symlink, |
|
74 | install_lib_symlink, | |
75 | install_scripts_for_symlink, |
|
75 | install_scripts_for_symlink, | |
76 | unsymlink, |
|
76 | unsymlink, | |
77 | ) |
|
77 | ) | |
78 |
|
78 | |||
79 | #------------------------------------------------------------------------------- |
|
79 | #------------------------------------------------------------------------------- | |
80 | # Handle OS specific things |
|
80 | # Handle OS specific things | |
81 | #------------------------------------------------------------------------------- |
|
81 | #------------------------------------------------------------------------------- | |
82 |
|
82 | |||
83 | if os.name in ('nt','dos'): |
|
83 | if os.name in ('nt','dos'): | |
84 | os_name = 'windows' |
|
84 | os_name = 'windows' | |
85 | else: |
|
85 | else: | |
86 | os_name = os.name |
|
86 | os_name = os.name | |
87 |
|
87 | |||
88 | # Under Windows, 'sdist' has not been supported. Now that the docs build with |
|
88 | # Under Windows, 'sdist' has not been supported. Now that the docs build with | |
89 | # Sphinx it might work, but let's not turn it on until someone confirms that it |
|
89 | # Sphinx it might work, but let's not turn it on until someone confirms that it | |
90 | # actually works. |
|
90 | # actually works. | |
91 | if os_name == 'windows' and 'sdist' in sys.argv: |
|
91 | if os_name == 'windows' and 'sdist' in sys.argv: | |
92 | print('The sdist command is not available under Windows. Exiting.') |
|
92 | print('The sdist command is not available under Windows. Exiting.') | |
93 | sys.exit(1) |
|
93 | sys.exit(1) | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | #------------------------------------------------------------------------------- |
|
96 | #------------------------------------------------------------------------------- | |
97 | # Things related to the IPython documentation |
|
97 | # Things related to the IPython documentation | |
98 | #------------------------------------------------------------------------------- |
|
98 | #------------------------------------------------------------------------------- | |
99 |
|
99 | |||
100 | # update the manuals when building a source dist |
|
100 | # update the manuals when building a source dist | |
101 | if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'): |
|
101 | if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'): | |
102 |
|
102 | |||
103 | # List of things to be updated. Each entry is a triplet of args for |
|
103 | # List of things to be updated. Each entry is a triplet of args for | |
104 | # target_update() |
|
104 | # target_update() | |
105 | to_update = [ |
|
105 | to_update = [ | |
106 | ( |
|
106 | ( | |
107 | "docs/man/ipython.1.gz", |
|
107 | "docs/man/ipython.1.gz", | |
108 | ["docs/man/ipython.1"], |
|
108 | ["docs/man/ipython.1"], | |
109 | "cd docs/man && python -m gzip --best ipython.1", |
|
109 | "cd docs/man && python -m gzip --best ipython.1", | |
110 | ), |
|
110 | ), | |
111 | ] |
|
111 | ] | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | [ target_update(*t) for t in to_update ] |
|
114 | [ target_update(*t) for t in to_update ] | |
115 |
|
115 | |||
116 | #--------------------------------------------------------------------------- |
|
116 | #--------------------------------------------------------------------------- | |
117 | # Find all the packages, package data, and data_files |
|
117 | # Find all the packages, package data, and data_files | |
118 | #--------------------------------------------------------------------------- |
|
118 | #--------------------------------------------------------------------------- | |
119 |
|
119 | |||
120 | data_files = find_data_files() |
|
120 | data_files = find_data_files() | |
121 |
|
121 | |||
122 | setup_args['data_files'] = data_files |
|
122 | setup_args['data_files'] = data_files | |
123 |
|
123 | |||
124 | #--------------------------------------------------------------------------- |
|
124 | #--------------------------------------------------------------------------- | |
125 | # custom distutils commands |
|
125 | # custom distutils commands | |
126 | #--------------------------------------------------------------------------- |
|
126 | #--------------------------------------------------------------------------- | |
127 | # imports here, so they are after setuptools import if there was one |
|
127 | # imports here, so they are after setuptools import if there was one | |
128 | from setuptools.command.sdist import sdist |
|
128 | from setuptools.command.sdist import sdist | |
129 |
|
129 | |||
130 | setup_args['cmdclass'] = { |
|
130 | setup_args['cmdclass'] = { | |
131 | 'build_py': \ |
|
131 | 'build_py': \ | |
132 | check_package_data_first(git_prebuild('IPython')), |
|
132 | check_package_data_first(git_prebuild('IPython')), | |
133 | 'sdist' : git_prebuild('IPython', sdist), |
|
133 | 'sdist' : git_prebuild('IPython', sdist), | |
134 | 'symlink': install_symlinked, |
|
134 | 'symlink': install_symlinked, | |
135 | 'install_lib_symlink': install_lib_symlink, |
|
135 | 'install_lib_symlink': install_lib_symlink, | |
136 | 'install_scripts_sym': install_scripts_for_symlink, |
|
136 | 'install_scripts_sym': install_scripts_for_symlink, | |
137 | 'unsymlink': unsymlink, |
|
137 | 'unsymlink': unsymlink, | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | #--------------------------------------------------------------------------- |
|
141 | #--------------------------------------------------------------------------- | |
142 | # Handle scripts, dependencies, and setuptools specific things |
|
142 | # Handle scripts, dependencies, and setuptools specific things | |
143 | #--------------------------------------------------------------------------- |
|
143 | #--------------------------------------------------------------------------- | |
144 |
|
144 | |||
145 | # setuptools requirements |
|
145 | # setuptools requirements | |
146 |
|
146 | |||
147 | extras_require = dict( |
|
147 | extras_require = dict( | |
148 | parallel=["ipyparallel"], |
|
148 | parallel=["ipyparallel"], | |
149 | qtconsole=["qtconsole"], |
|
149 | qtconsole=["qtconsole"], | |
150 | doc=["Sphinx>=1.3"], |
|
150 | doc=["Sphinx>=1.3"], | |
151 | test=[ |
|
151 | test=[ | |
152 | "pytest", |
|
152 | "pytest", | |
153 | "testpath", |
|
153 | "testpath", | |
154 | "pygments", |
|
154 | "pygments", | |
155 | ], |
|
155 | ], | |
156 | test_extra=[ |
|
156 | test_extra=[ | |
157 | "pytest", |
|
157 | "pytest", | |
158 | "testpath", |
|
158 | "testpath", | |
159 | "curio", |
|
159 | "curio", | |
160 | "matplotlib!=3.2.0", |
|
160 | "matplotlib!=3.2.0", | |
161 | "nbformat", |
|
161 | "nbformat", | |
162 | "numpy>=1.17", |
|
162 | "numpy>=1.17", | |
163 | "pandas", |
|
163 | "pandas", | |
164 | "pygments", |
|
164 | "pygments", | |
165 | "trio", |
|
165 | "trio", | |
166 | ], |
|
166 | ], | |
167 | terminal=[], |
|
167 | terminal=[], | |
168 | kernel=["ipykernel"], |
|
168 | kernel=["ipykernel"], | |
169 | nbformat=["nbformat"], |
|
169 | nbformat=["nbformat"], | |
170 | notebook=["notebook", "ipywidgets"], |
|
170 | notebook=["notebook", "ipywidgets"], | |
171 | nbconvert=["nbconvert"], |
|
171 | nbconvert=["nbconvert"], | |
172 | ) |
|
172 | ) | |
173 |
|
173 | |||
174 |
|
||||
175 | everything = set(chain.from_iterable(extras_require.values())) |
|
174 | everything = set(chain.from_iterable(extras_require.values())) | |
176 | extras_require['all'] = list(sorted(everything)) |
|
175 | extras_require['all'] = list(sorted(everything)) | |
177 |
|
176 | |||
178 | setup_args["extras_require"] = extras_require |
|
177 | setup_args["extras_require"] = extras_require | |
179 |
|
178 | |||
180 | #--------------------------------------------------------------------------- |
|
179 | #--------------------------------------------------------------------------- | |
181 | # Do the actual setup now |
|
180 | # Do the actual setup now | |
182 | #--------------------------------------------------------------------------- |
|
181 | #--------------------------------------------------------------------------- | |
183 |
|
182 | |||
184 | if __name__ == "__main__": |
|
183 | if __name__ == "__main__": | |
185 | setup(**setup_args) |
|
184 | setup(**setup_args) |
General Comments 0
You need to be logged in to leave comments.
Login now