Show More
@@ -1,1351 +1,1351 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Implementation of execution-related magic functions.""" |
|
2 | """Implementation of execution-related magic functions.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | from __future__ import print_function |
|
7 | from __future__ import print_function | |
8 |
|
8 | |||
9 | import ast |
|
9 | import ast | |
10 | import bdb |
|
10 | import bdb | |
11 | import gc |
|
11 | import gc | |
12 | import itertools |
|
12 | import itertools | |
13 | import os |
|
13 | import os | |
14 | import sys |
|
14 | import sys | |
15 | import time |
|
15 | import time | |
16 | import timeit |
|
16 | import timeit | |
17 | from pdb import Restart |
|
17 | from pdb import Restart | |
18 |
|
18 | |||
19 | # cProfile was added in Python2.5 |
|
19 | # cProfile was added in Python2.5 | |
20 | try: |
|
20 | try: | |
21 | import cProfile as profile |
|
21 | import cProfile as profile | |
22 | import pstats |
|
22 | import pstats | |
23 | except ImportError: |
|
23 | except ImportError: | |
24 | # profile isn't bundled by default in Debian for license reasons |
|
24 | # profile isn't bundled by default in Debian for license reasons | |
25 | try: |
|
25 | try: | |
26 | import profile, pstats |
|
26 | import profile, pstats | |
27 | except ImportError: |
|
27 | except ImportError: | |
28 | profile = pstats = None |
|
28 | profile = pstats = None | |
29 |
|
29 | |||
30 | from IPython.core import debugger, oinspect |
|
30 | from IPython.core import debugger, oinspect | |
31 | from IPython.core import magic_arguments |
|
31 | from IPython.core import magic_arguments | |
32 | from IPython.core import page |
|
32 | from IPython.core import page | |
33 | from IPython.core.error import UsageError |
|
33 | from IPython.core.error import UsageError | |
34 | from IPython.core.macro import Macro |
|
34 | from IPython.core.macro import Macro | |
35 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, |
|
35 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, | |
36 | line_cell_magic, on_off, needs_local_scope) |
|
36 | line_cell_magic, on_off, needs_local_scope) | |
37 | from IPython.testing.skipdoctest import skip_doctest |
|
37 | from IPython.testing.skipdoctest import skip_doctest | |
38 | from IPython.utils import py3compat |
|
38 | from IPython.utils import py3compat | |
39 | from IPython.utils.py3compat import builtin_mod, iteritems, PY3 |
|
39 | from IPython.utils.py3compat import builtin_mod, iteritems, PY3 | |
40 | from IPython.utils.contexts import preserve_keys |
|
40 | from IPython.utils.contexts import preserve_keys | |
41 |
from IPython.utils. |
|
41 | from IPython.utils.capture import capture_output | |
42 | from IPython.utils.ipstruct import Struct |
|
42 | from IPython.utils.ipstruct import Struct | |
43 | from IPython.utils.module_paths import find_mod |
|
43 | from IPython.utils.module_paths import find_mod | |
44 | from IPython.utils.path import get_py_filename, unquote_filename, shellglob |
|
44 | from IPython.utils.path import get_py_filename, unquote_filename, shellglob | |
45 | from IPython.utils.timing import clock, clock2 |
|
45 | from IPython.utils.timing import clock, clock2 | |
46 | from IPython.utils.warn import warn, error |
|
46 | from IPython.utils.warn import warn, error | |
47 |
|
47 | |||
48 | if PY3: |
|
48 | if PY3: | |
49 | from io import StringIO |
|
49 | from io import StringIO | |
50 | else: |
|
50 | else: | |
51 | from StringIO import StringIO |
|
51 | from StringIO import StringIO | |
52 |
|
52 | |||
53 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
54 | # Magic implementation classes |
|
54 | # Magic implementation classes | |
55 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class TimeitResult(object): |
|
58 | class TimeitResult(object): | |
59 | """ |
|
59 | """ | |
60 | Object returned by the timeit magic with info about the run. |
|
60 | Object returned by the timeit magic with info about the run. | |
61 |
|
61 | |||
62 | Contain the following attributes : |
|
62 | Contain the following attributes : | |
63 |
|
63 | |||
64 | loops: (int) number of loop done per measurement |
|
64 | loops: (int) number of loop done per measurement | |
65 | repeat: (int) number of time the mesurement has been repeated |
|
65 | repeat: (int) number of time the mesurement has been repeated | |
66 | best: (float) best execusion time / number |
|
66 | best: (float) best execusion time / number | |
67 | all_runs: (list of float) execusion time of each run (in s) |
|
67 | all_runs: (list of float) execusion time of each run (in s) | |
68 | compile_time: (float) time of statement compilation (s) |
|
68 | compile_time: (float) time of statement compilation (s) | |
69 |
|
69 | |||
70 | """ |
|
70 | """ | |
71 |
|
71 | |||
72 | def __init__(self, loops, repeat, best, all_runs, compile_time, precision): |
|
72 | def __init__(self, loops, repeat, best, all_runs, compile_time, precision): | |
73 | self.loops = loops |
|
73 | self.loops = loops | |
74 | self.repeat = repeat |
|
74 | self.repeat = repeat | |
75 | self.best = best |
|
75 | self.best = best | |
76 | self.all_runs = all_runs |
|
76 | self.all_runs = all_runs | |
77 | self.compile_time = compile_time |
|
77 | self.compile_time = compile_time | |
78 | self._precision = precision |
|
78 | self._precision = precision | |
79 |
|
79 | |||
80 | def _repr_pretty_(self, p , cycle): |
|
80 | def _repr_pretty_(self, p , cycle): | |
81 | unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat, |
|
81 | unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat, | |
82 | _format_time(self.best, self._precision)) |
|
82 | _format_time(self.best, self._precision)) | |
83 | p.text(u'<TimeitResult : '+unic+u'>') |
|
83 | p.text(u'<TimeitResult : '+unic+u'>') | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | class TimeitTemplateFiller(ast.NodeTransformer): |
|
86 | class TimeitTemplateFiller(ast.NodeTransformer): | |
87 | """Fill in the AST template for timing execution. |
|
87 | """Fill in the AST template for timing execution. | |
88 |
|
88 | |||
89 | This is quite closely tied to the template definition, which is in |
|
89 | This is quite closely tied to the template definition, which is in | |
90 | :meth:`ExecutionMagics.timeit`. |
|
90 | :meth:`ExecutionMagics.timeit`. | |
91 | """ |
|
91 | """ | |
92 | def __init__(self, ast_setup, ast_stmt): |
|
92 | def __init__(self, ast_setup, ast_stmt): | |
93 | self.ast_setup = ast_setup |
|
93 | self.ast_setup = ast_setup | |
94 | self.ast_stmt = ast_stmt |
|
94 | self.ast_stmt = ast_stmt | |
95 |
|
95 | |||
96 | def visit_FunctionDef(self, node): |
|
96 | def visit_FunctionDef(self, node): | |
97 | "Fill in the setup statement" |
|
97 | "Fill in the setup statement" | |
98 | self.generic_visit(node) |
|
98 | self.generic_visit(node) | |
99 | if node.name == "inner": |
|
99 | if node.name == "inner": | |
100 | node.body[:1] = self.ast_setup.body |
|
100 | node.body[:1] = self.ast_setup.body | |
101 |
|
101 | |||
102 | return node |
|
102 | return node | |
103 |
|
103 | |||
104 | def visit_For(self, node): |
|
104 | def visit_For(self, node): | |
105 | "Fill in the statement to be timed" |
|
105 | "Fill in the statement to be timed" | |
106 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': |
|
106 | if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt': | |
107 | node.body = self.ast_stmt.body |
|
107 | node.body = self.ast_stmt.body | |
108 | return node |
|
108 | return node | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | class Timer(timeit.Timer): |
|
111 | class Timer(timeit.Timer): | |
112 | """Timer class that explicitly uses self.inner |
|
112 | """Timer class that explicitly uses self.inner | |
113 |
|
113 | |||
114 | which is an undocumented implementation detail of CPython, |
|
114 | which is an undocumented implementation detail of CPython, | |
115 | not shared by PyPy. |
|
115 | not shared by PyPy. | |
116 | """ |
|
116 | """ | |
117 | # Timer.timeit copied from CPython 3.4.2 |
|
117 | # Timer.timeit copied from CPython 3.4.2 | |
118 | def timeit(self, number=timeit.default_number): |
|
118 | def timeit(self, number=timeit.default_number): | |
119 | """Time 'number' executions of the main statement. |
|
119 | """Time 'number' executions of the main statement. | |
120 |
|
120 | |||
121 | To be precise, this executes the setup statement once, and |
|
121 | To be precise, this executes the setup statement once, and | |
122 | then returns the time it takes to execute the main statement |
|
122 | then returns the time it takes to execute the main statement | |
123 | a number of times, as a float measured in seconds. The |
|
123 | a number of times, as a float measured in seconds. The | |
124 | argument is the number of times through the loop, defaulting |
|
124 | argument is the number of times through the loop, defaulting | |
125 | to one million. The main statement, the setup statement and |
|
125 | to one million. The main statement, the setup statement and | |
126 | the timer function to be used are passed to the constructor. |
|
126 | the timer function to be used are passed to the constructor. | |
127 | """ |
|
127 | """ | |
128 | it = itertools.repeat(None, number) |
|
128 | it = itertools.repeat(None, number) | |
129 | gcold = gc.isenabled() |
|
129 | gcold = gc.isenabled() | |
130 | gc.disable() |
|
130 | gc.disable() | |
131 | try: |
|
131 | try: | |
132 | timing = self.inner(it, self.timer) |
|
132 | timing = self.inner(it, self.timer) | |
133 | finally: |
|
133 | finally: | |
134 | if gcold: |
|
134 | if gcold: | |
135 | gc.enable() |
|
135 | gc.enable() | |
136 | return timing |
|
136 | return timing | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | @magics_class |
|
139 | @magics_class | |
140 | class ExecutionMagics(Magics): |
|
140 | class ExecutionMagics(Magics): | |
141 | """Magics related to code execution, debugging, profiling, etc. |
|
141 | """Magics related to code execution, debugging, profiling, etc. | |
142 |
|
142 | |||
143 | """ |
|
143 | """ | |
144 |
|
144 | |||
145 | def __init__(self, shell): |
|
145 | def __init__(self, shell): | |
146 | super(ExecutionMagics, self).__init__(shell) |
|
146 | super(ExecutionMagics, self).__init__(shell) | |
147 | if profile is None: |
|
147 | if profile is None: | |
148 | self.prun = self.profile_missing_notice |
|
148 | self.prun = self.profile_missing_notice | |
149 | # Default execution function used to actually run user code. |
|
149 | # Default execution function used to actually run user code. | |
150 | self.default_runner = None |
|
150 | self.default_runner = None | |
151 |
|
151 | |||
152 | def profile_missing_notice(self, *args, **kwargs): |
|
152 | def profile_missing_notice(self, *args, **kwargs): | |
153 | error("""\ |
|
153 | error("""\ | |
154 | The profile module could not be found. It has been removed from the standard |
|
154 | The profile module could not be found. It has been removed from the standard | |
155 | python packages because of its non-free license. To use profiling, install the |
|
155 | python packages because of its non-free license. To use profiling, install the | |
156 | python-profiler package from non-free.""") |
|
156 | python-profiler package from non-free.""") | |
157 |
|
157 | |||
158 | @skip_doctest |
|
158 | @skip_doctest | |
159 | @line_cell_magic |
|
159 | @line_cell_magic | |
160 | def prun(self, parameter_s='', cell=None): |
|
160 | def prun(self, parameter_s='', cell=None): | |
161 |
|
161 | |||
162 | """Run a statement through the python code profiler. |
|
162 | """Run a statement through the python code profiler. | |
163 |
|
163 | |||
164 | Usage, in line mode: |
|
164 | Usage, in line mode: | |
165 | %prun [options] statement |
|
165 | %prun [options] statement | |
166 |
|
166 | |||
167 | Usage, in cell mode: |
|
167 | Usage, in cell mode: | |
168 | %%prun [options] [statement] |
|
168 | %%prun [options] [statement] | |
169 | code... |
|
169 | code... | |
170 | code... |
|
170 | code... | |
171 |
|
171 | |||
172 | In cell mode, the additional code lines are appended to the (possibly |
|
172 | In cell mode, the additional code lines are appended to the (possibly | |
173 | empty) statement in the first line. Cell mode allows you to easily |
|
173 | empty) statement in the first line. Cell mode allows you to easily | |
174 | profile multiline blocks without having to put them in a separate |
|
174 | profile multiline blocks without having to put them in a separate | |
175 | function. |
|
175 | function. | |
176 |
|
176 | |||
177 | The given statement (which doesn't require quote marks) is run via the |
|
177 | The given statement (which doesn't require quote marks) is run via the | |
178 | python profiler in a manner similar to the profile.run() function. |
|
178 | python profiler in a manner similar to the profile.run() function. | |
179 | Namespaces are internally managed to work correctly; profile.run |
|
179 | Namespaces are internally managed to work correctly; profile.run | |
180 | cannot be used in IPython because it makes certain assumptions about |
|
180 | cannot be used in IPython because it makes certain assumptions about | |
181 | namespaces which do not hold under IPython. |
|
181 | namespaces which do not hold under IPython. | |
182 |
|
182 | |||
183 | Options: |
|
183 | Options: | |
184 |
|
184 | |||
185 | -l <limit> |
|
185 | -l <limit> | |
186 | you can place restrictions on what or how much of the |
|
186 | you can place restrictions on what or how much of the | |
187 | profile gets printed. The limit value can be: |
|
187 | profile gets printed. The limit value can be: | |
188 |
|
188 | |||
189 | * A string: only information for function names containing this string |
|
189 | * A string: only information for function names containing this string | |
190 | is printed. |
|
190 | is printed. | |
191 |
|
191 | |||
192 | * An integer: only these many lines are printed. |
|
192 | * An integer: only these many lines are printed. | |
193 |
|
193 | |||
194 | * A float (between 0 and 1): this fraction of the report is printed |
|
194 | * A float (between 0 and 1): this fraction of the report is printed | |
195 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
195 | (for example, use a limit of 0.4 to see the topmost 40% only). | |
196 |
|
196 | |||
197 | You can combine several limits with repeated use of the option. For |
|
197 | You can combine several limits with repeated use of the option. For | |
198 | example, ``-l __init__ -l 5`` will print only the topmost 5 lines of |
|
198 | example, ``-l __init__ -l 5`` will print only the topmost 5 lines of | |
199 | information about class constructors. |
|
199 | information about class constructors. | |
200 |
|
200 | |||
201 | -r |
|
201 | -r | |
202 | return the pstats.Stats object generated by the profiling. This |
|
202 | return the pstats.Stats object generated by the profiling. This | |
203 | object has all the information about the profile in it, and you can |
|
203 | object has all the information about the profile in it, and you can | |
204 | later use it for further analysis or in other functions. |
|
204 | later use it for further analysis or in other functions. | |
205 |
|
205 | |||
206 | -s <key> |
|
206 | -s <key> | |
207 | sort profile by given key. You can provide more than one key |
|
207 | sort profile by given key. You can provide more than one key | |
208 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
208 | by using the option several times: '-s key1 -s key2 -s key3...'. The | |
209 | default sorting key is 'time'. |
|
209 | default sorting key is 'time'. | |
210 |
|
210 | |||
211 | The following is copied verbatim from the profile documentation |
|
211 | The following is copied verbatim from the profile documentation | |
212 | referenced below: |
|
212 | referenced below: | |
213 |
|
213 | |||
214 | When more than one key is provided, additional keys are used as |
|
214 | When more than one key is provided, additional keys are used as | |
215 | secondary criteria when the there is equality in all keys selected |
|
215 | secondary criteria when the there is equality in all keys selected | |
216 | before them. |
|
216 | before them. | |
217 |
|
217 | |||
218 | Abbreviations can be used for any key names, as long as the |
|
218 | Abbreviations can be used for any key names, as long as the | |
219 | abbreviation is unambiguous. The following are the keys currently |
|
219 | abbreviation is unambiguous. The following are the keys currently | |
220 | defined: |
|
220 | defined: | |
221 |
|
221 | |||
222 | ============ ===================== |
|
222 | ============ ===================== | |
223 | Valid Arg Meaning |
|
223 | Valid Arg Meaning | |
224 | ============ ===================== |
|
224 | ============ ===================== | |
225 | "calls" call count |
|
225 | "calls" call count | |
226 | "cumulative" cumulative time |
|
226 | "cumulative" cumulative time | |
227 | "file" file name |
|
227 | "file" file name | |
228 | "module" file name |
|
228 | "module" file name | |
229 | "pcalls" primitive call count |
|
229 | "pcalls" primitive call count | |
230 | "line" line number |
|
230 | "line" line number | |
231 | "name" function name |
|
231 | "name" function name | |
232 | "nfl" name/file/line |
|
232 | "nfl" name/file/line | |
233 | "stdname" standard name |
|
233 | "stdname" standard name | |
234 | "time" internal time |
|
234 | "time" internal time | |
235 | ============ ===================== |
|
235 | ============ ===================== | |
236 |
|
236 | |||
237 | Note that all sorts on statistics are in descending order (placing |
|
237 | Note that all sorts on statistics are in descending order (placing | |
238 | most time consuming items first), where as name, file, and line number |
|
238 | most time consuming items first), where as name, file, and line number | |
239 | searches are in ascending order (i.e., alphabetical). The subtle |
|
239 | searches are in ascending order (i.e., alphabetical). The subtle | |
240 | distinction between "nfl" and "stdname" is that the standard name is a |
|
240 | distinction between "nfl" and "stdname" is that the standard name is a | |
241 | sort of the name as printed, which means that the embedded line |
|
241 | sort of the name as printed, which means that the embedded line | |
242 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
242 | numbers get compared in an odd way. For example, lines 3, 20, and 40 | |
243 | would (if the file names were the same) appear in the string order |
|
243 | would (if the file names were the same) appear in the string order | |
244 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
244 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the | |
245 | line numbers. In fact, sort_stats("nfl") is the same as |
|
245 | line numbers. In fact, sort_stats("nfl") is the same as | |
246 | sort_stats("name", "file", "line"). |
|
246 | sort_stats("name", "file", "line"). | |
247 |
|
247 | |||
248 | -T <filename> |
|
248 | -T <filename> | |
249 | save profile results as shown on screen to a text |
|
249 | save profile results as shown on screen to a text | |
250 | file. The profile is still shown on screen. |
|
250 | file. The profile is still shown on screen. | |
251 |
|
251 | |||
252 | -D <filename> |
|
252 | -D <filename> | |
253 | save (via dump_stats) profile statistics to given |
|
253 | save (via dump_stats) profile statistics to given | |
254 | filename. This data is in a format understood by the pstats module, and |
|
254 | filename. This data is in a format understood by the pstats module, and | |
255 | is generated by a call to the dump_stats() method of profile |
|
255 | is generated by a call to the dump_stats() method of profile | |
256 | objects. The profile is still shown on screen. |
|
256 | objects. The profile is still shown on screen. | |
257 |
|
257 | |||
258 | -q |
|
258 | -q | |
259 | suppress output to the pager. Best used with -T and/or -D above. |
|
259 | suppress output to the pager. Best used with -T and/or -D above. | |
260 |
|
260 | |||
261 | If you want to run complete programs under the profiler's control, use |
|
261 | If you want to run complete programs under the profiler's control, use | |
262 | ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts |
|
262 | ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts | |
263 | contains profiler specific options as described here. |
|
263 | contains profiler specific options as described here. | |
264 |
|
264 | |||
265 | You can read the complete documentation for the profile module with:: |
|
265 | You can read the complete documentation for the profile module with:: | |
266 |
|
266 | |||
267 | In [1]: import profile; profile.help() |
|
267 | In [1]: import profile; profile.help() | |
268 | """ |
|
268 | """ | |
269 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', |
|
269 | opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q', | |
270 | list_all=True, posix=False) |
|
270 | list_all=True, posix=False) | |
271 | if cell is not None: |
|
271 | if cell is not None: | |
272 | arg_str += '\n' + cell |
|
272 | arg_str += '\n' + cell | |
273 | arg_str = self.shell.input_splitter.transform_cell(arg_str) |
|
273 | arg_str = self.shell.input_splitter.transform_cell(arg_str) | |
274 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) |
|
274 | return self._run_with_profiler(arg_str, opts, self.shell.user_ns) | |
275 |
|
275 | |||
276 | def _run_with_profiler(self, code, opts, namespace): |
|
276 | def _run_with_profiler(self, code, opts, namespace): | |
277 | """ |
|
277 | """ | |
278 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. |
|
278 | Run `code` with profiler. Used by ``%prun`` and ``%run -p``. | |
279 |
|
279 | |||
280 | Parameters |
|
280 | Parameters | |
281 | ---------- |
|
281 | ---------- | |
282 | code : str |
|
282 | code : str | |
283 | Code to be executed. |
|
283 | Code to be executed. | |
284 | opts : Struct |
|
284 | opts : Struct | |
285 | Options parsed by `self.parse_options`. |
|
285 | Options parsed by `self.parse_options`. | |
286 | namespace : dict |
|
286 | namespace : dict | |
287 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). |
|
287 | A dictionary for Python namespace (e.g., `self.shell.user_ns`). | |
288 |
|
288 | |||
289 | """ |
|
289 | """ | |
290 |
|
290 | |||
291 | # Fill default values for unspecified options: |
|
291 | # Fill default values for unspecified options: | |
292 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) |
|
292 | opts.merge(Struct(D=[''], l=[], s=['time'], T=[''])) | |
293 |
|
293 | |||
294 | prof = profile.Profile() |
|
294 | prof = profile.Profile() | |
295 | try: |
|
295 | try: | |
296 | prof = prof.runctx(code, namespace, namespace) |
|
296 | prof = prof.runctx(code, namespace, namespace) | |
297 | sys_exit = '' |
|
297 | sys_exit = '' | |
298 | except SystemExit: |
|
298 | except SystemExit: | |
299 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
299 | sys_exit = """*** SystemExit exception caught in code being profiled.""" | |
300 |
|
300 | |||
301 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
301 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) | |
302 |
|
302 | |||
303 | lims = opts.l |
|
303 | lims = opts.l | |
304 | if lims: |
|
304 | if lims: | |
305 | lims = [] # rebuild lims with ints/floats/strings |
|
305 | lims = [] # rebuild lims with ints/floats/strings | |
306 | for lim in opts.l: |
|
306 | for lim in opts.l: | |
307 | try: |
|
307 | try: | |
308 | lims.append(int(lim)) |
|
308 | lims.append(int(lim)) | |
309 | except ValueError: |
|
309 | except ValueError: | |
310 | try: |
|
310 | try: | |
311 | lims.append(float(lim)) |
|
311 | lims.append(float(lim)) | |
312 | except ValueError: |
|
312 | except ValueError: | |
313 | lims.append(lim) |
|
313 | lims.append(lim) | |
314 |
|
314 | |||
315 | # Trap output. |
|
315 | # Trap output. | |
316 | stdout_trap = StringIO() |
|
316 | stdout_trap = StringIO() | |
317 | stats_stream = stats.stream |
|
317 | stats_stream = stats.stream | |
318 | try: |
|
318 | try: | |
319 | stats.stream = stdout_trap |
|
319 | stats.stream = stdout_trap | |
320 | stats.print_stats(*lims) |
|
320 | stats.print_stats(*lims) | |
321 | finally: |
|
321 | finally: | |
322 | stats.stream = stats_stream |
|
322 | stats.stream = stats_stream | |
323 |
|
323 | |||
324 | output = stdout_trap.getvalue() |
|
324 | output = stdout_trap.getvalue() | |
325 | output = output.rstrip() |
|
325 | output = output.rstrip() | |
326 |
|
326 | |||
327 | if 'q' not in opts: |
|
327 | if 'q' not in opts: | |
328 | page.page(output) |
|
328 | page.page(output) | |
329 | print(sys_exit, end=' ') |
|
329 | print(sys_exit, end=' ') | |
330 |
|
330 | |||
331 | dump_file = opts.D[0] |
|
331 | dump_file = opts.D[0] | |
332 | text_file = opts.T[0] |
|
332 | text_file = opts.T[0] | |
333 | if dump_file: |
|
333 | if dump_file: | |
334 | dump_file = unquote_filename(dump_file) |
|
334 | dump_file = unquote_filename(dump_file) | |
335 | prof.dump_stats(dump_file) |
|
335 | prof.dump_stats(dump_file) | |
336 | print('\n*** Profile stats marshalled to file',\ |
|
336 | print('\n*** Profile stats marshalled to file',\ | |
337 | repr(dump_file)+'.',sys_exit) |
|
337 | repr(dump_file)+'.',sys_exit) | |
338 | if text_file: |
|
338 | if text_file: | |
339 | text_file = unquote_filename(text_file) |
|
339 | text_file = unquote_filename(text_file) | |
340 | pfile = open(text_file,'w') |
|
340 | pfile = open(text_file,'w') | |
341 | pfile.write(output) |
|
341 | pfile.write(output) | |
342 | pfile.close() |
|
342 | pfile.close() | |
343 | print('\n*** Profile printout saved to text file',\ |
|
343 | print('\n*** Profile printout saved to text file',\ | |
344 | repr(text_file)+'.',sys_exit) |
|
344 | repr(text_file)+'.',sys_exit) | |
345 |
|
345 | |||
346 | if 'r' in opts: |
|
346 | if 'r' in opts: | |
347 | return stats |
|
347 | return stats | |
348 | else: |
|
348 | else: | |
349 | return None |
|
349 | return None | |
350 |
|
350 | |||
351 | @line_magic |
|
351 | @line_magic | |
352 | def pdb(self, parameter_s=''): |
|
352 | def pdb(self, parameter_s=''): | |
353 | """Control the automatic calling of the pdb interactive debugger. |
|
353 | """Control the automatic calling of the pdb interactive debugger. | |
354 |
|
354 | |||
355 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
355 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without | |
356 | argument it works as a toggle. |
|
356 | argument it works as a toggle. | |
357 |
|
357 | |||
358 | When an exception is triggered, IPython can optionally call the |
|
358 | When an exception is triggered, IPython can optionally call the | |
359 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
359 | interactive pdb debugger after the traceback printout. %pdb toggles | |
360 | this feature on and off. |
|
360 | this feature on and off. | |
361 |
|
361 | |||
362 | The initial state of this feature is set in your configuration |
|
362 | The initial state of this feature is set in your configuration | |
363 | file (the option is ``InteractiveShell.pdb``). |
|
363 | file (the option is ``InteractiveShell.pdb``). | |
364 |
|
364 | |||
365 | If you want to just activate the debugger AFTER an exception has fired, |
|
365 | If you want to just activate the debugger AFTER an exception has fired, | |
366 | without having to type '%pdb on' and rerunning your code, you can use |
|
366 | without having to type '%pdb on' and rerunning your code, you can use | |
367 | the %debug magic.""" |
|
367 | the %debug magic.""" | |
368 |
|
368 | |||
369 | par = parameter_s.strip().lower() |
|
369 | par = parameter_s.strip().lower() | |
370 |
|
370 | |||
371 | if par: |
|
371 | if par: | |
372 | try: |
|
372 | try: | |
373 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
373 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] | |
374 | except KeyError: |
|
374 | except KeyError: | |
375 | print ('Incorrect argument. Use on/1, off/0, ' |
|
375 | print ('Incorrect argument. Use on/1, off/0, ' | |
376 | 'or nothing for a toggle.') |
|
376 | 'or nothing for a toggle.') | |
377 | return |
|
377 | return | |
378 | else: |
|
378 | else: | |
379 | # toggle |
|
379 | # toggle | |
380 | new_pdb = not self.shell.call_pdb |
|
380 | new_pdb = not self.shell.call_pdb | |
381 |
|
381 | |||
382 | # set on the shell |
|
382 | # set on the shell | |
383 | self.shell.call_pdb = new_pdb |
|
383 | self.shell.call_pdb = new_pdb | |
384 | print('Automatic pdb calling has been turned',on_off(new_pdb)) |
|
384 | print('Automatic pdb calling has been turned',on_off(new_pdb)) | |
385 |
|
385 | |||
386 | @skip_doctest |
|
386 | @skip_doctest | |
387 | @magic_arguments.magic_arguments() |
|
387 | @magic_arguments.magic_arguments() | |
388 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', |
|
388 | @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE', | |
389 | help=""" |
|
389 | help=""" | |
390 | Set break point at LINE in FILE. |
|
390 | Set break point at LINE in FILE. | |
391 | """ |
|
391 | """ | |
392 | ) |
|
392 | ) | |
393 | @magic_arguments.argument('statement', nargs='*', |
|
393 | @magic_arguments.argument('statement', nargs='*', | |
394 | help=""" |
|
394 | help=""" | |
395 | Code to run in debugger. |
|
395 | Code to run in debugger. | |
396 | You can omit this in cell magic mode. |
|
396 | You can omit this in cell magic mode. | |
397 | """ |
|
397 | """ | |
398 | ) |
|
398 | ) | |
399 | @line_cell_magic |
|
399 | @line_cell_magic | |
400 | def debug(self, line='', cell=None): |
|
400 | def debug(self, line='', cell=None): | |
401 | """Activate the interactive debugger. |
|
401 | """Activate the interactive debugger. | |
402 |
|
402 | |||
403 | This magic command support two ways of activating debugger. |
|
403 | This magic command support two ways of activating debugger. | |
404 | One is to activate debugger before executing code. This way, you |
|
404 | One is to activate debugger before executing code. This way, you | |
405 | can set a break point, to step through the code from the point. |
|
405 | can set a break point, to step through the code from the point. | |
406 | You can use this mode by giving statements to execute and optionally |
|
406 | You can use this mode by giving statements to execute and optionally | |
407 | a breakpoint. |
|
407 | a breakpoint. | |
408 |
|
408 | |||
409 | The other one is to activate debugger in post-mortem mode. You can |
|
409 | The other one is to activate debugger in post-mortem mode. You can | |
410 | activate this mode simply running %debug without any argument. |
|
410 | activate this mode simply running %debug without any argument. | |
411 | If an exception has just occurred, this lets you inspect its stack |
|
411 | If an exception has just occurred, this lets you inspect its stack | |
412 | frames interactively. Note that this will always work only on the last |
|
412 | frames interactively. Note that this will always work only on the last | |
413 | traceback that occurred, so you must call this quickly after an |
|
413 | traceback that occurred, so you must call this quickly after an | |
414 | exception that you wish to inspect has fired, because if another one |
|
414 | exception that you wish to inspect has fired, because if another one | |
415 | occurs, it clobbers the previous one. |
|
415 | occurs, it clobbers the previous one. | |
416 |
|
416 | |||
417 | If you want IPython to automatically do this on every exception, see |
|
417 | If you want IPython to automatically do this on every exception, see | |
418 | the %pdb magic for more details. |
|
418 | the %pdb magic for more details. | |
419 | """ |
|
419 | """ | |
420 | args = magic_arguments.parse_argstring(self.debug, line) |
|
420 | args = magic_arguments.parse_argstring(self.debug, line) | |
421 |
|
421 | |||
422 | if not (args.breakpoint or args.statement or cell): |
|
422 | if not (args.breakpoint or args.statement or cell): | |
423 | self._debug_post_mortem() |
|
423 | self._debug_post_mortem() | |
424 | else: |
|
424 | else: | |
425 | code = "\n".join(args.statement) |
|
425 | code = "\n".join(args.statement) | |
426 | if cell: |
|
426 | if cell: | |
427 | code += "\n" + cell |
|
427 | code += "\n" + cell | |
428 | self._debug_exec(code, args.breakpoint) |
|
428 | self._debug_exec(code, args.breakpoint) | |
429 |
|
429 | |||
430 | def _debug_post_mortem(self): |
|
430 | def _debug_post_mortem(self): | |
431 | self.shell.debugger(force=True) |
|
431 | self.shell.debugger(force=True) | |
432 |
|
432 | |||
433 | def _debug_exec(self, code, breakpoint): |
|
433 | def _debug_exec(self, code, breakpoint): | |
434 | if breakpoint: |
|
434 | if breakpoint: | |
435 | (filename, bp_line) = breakpoint.split(':', 1) |
|
435 | (filename, bp_line) = breakpoint.split(':', 1) | |
436 | bp_line = int(bp_line) |
|
436 | bp_line = int(bp_line) | |
437 | else: |
|
437 | else: | |
438 | (filename, bp_line) = (None, None) |
|
438 | (filename, bp_line) = (None, None) | |
439 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) |
|
439 | self._run_with_debugger(code, self.shell.user_ns, filename, bp_line) | |
440 |
|
440 | |||
441 | @line_magic |
|
441 | @line_magic | |
442 | def tb(self, s): |
|
442 | def tb(self, s): | |
443 | """Print the last traceback with the currently active exception mode. |
|
443 | """Print the last traceback with the currently active exception mode. | |
444 |
|
444 | |||
445 | See %xmode for changing exception reporting modes.""" |
|
445 | See %xmode for changing exception reporting modes.""" | |
446 | self.shell.showtraceback() |
|
446 | self.shell.showtraceback() | |
447 |
|
447 | |||
448 | @skip_doctest |
|
448 | @skip_doctest | |
449 | @line_magic |
|
449 | @line_magic | |
450 | def run(self, parameter_s='', runner=None, |
|
450 | def run(self, parameter_s='', runner=None, | |
451 | file_finder=get_py_filename): |
|
451 | file_finder=get_py_filename): | |
452 | """Run the named file inside IPython as a program. |
|
452 | """Run the named file inside IPython as a program. | |
453 |
|
453 | |||
454 | Usage:: |
|
454 | Usage:: | |
455 |
|
455 | |||
456 | %run [-n -i -e -G] |
|
456 | %run [-n -i -e -G] | |
457 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] |
|
457 | [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )] | |
458 | ( -m mod | file ) [args] |
|
458 | ( -m mod | file ) [args] | |
459 |
|
459 | |||
460 | Parameters after the filename are passed as command-line arguments to |
|
460 | Parameters after the filename are passed as command-line arguments to | |
461 | the program (put in sys.argv). Then, control returns to IPython's |
|
461 | the program (put in sys.argv). Then, control returns to IPython's | |
462 | prompt. |
|
462 | prompt. | |
463 |
|
463 | |||
464 | This is similar to running at a system prompt ``python file args``, |
|
464 | This is similar to running at a system prompt ``python file args``, | |
465 | but with the advantage of giving you IPython's tracebacks, and of |
|
465 | but with the advantage of giving you IPython's tracebacks, and of | |
466 | loading all variables into your interactive namespace for further use |
|
466 | loading all variables into your interactive namespace for further use | |
467 | (unless -p is used, see below). |
|
467 | (unless -p is used, see below). | |
468 |
|
468 | |||
469 | The file is executed in a namespace initially consisting only of |
|
469 | The file is executed in a namespace initially consisting only of | |
470 | ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus |
|
470 | ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus | |
471 | sees its environment as if it were being run as a stand-alone program |
|
471 | sees its environment as if it were being run as a stand-alone program | |
472 | (except for sharing global objects such as previously imported |
|
472 | (except for sharing global objects such as previously imported | |
473 | modules). But after execution, the IPython interactive namespace gets |
|
473 | modules). But after execution, the IPython interactive namespace gets | |
474 | updated with all variables defined in the program (except for __name__ |
|
474 | updated with all variables defined in the program (except for __name__ | |
475 | and sys.argv). This allows for very convenient loading of code for |
|
475 | and sys.argv). This allows for very convenient loading of code for | |
476 | interactive work, while giving each program a 'clean sheet' to run in. |
|
476 | interactive work, while giving each program a 'clean sheet' to run in. | |
477 |
|
477 | |||
478 | Arguments are expanded using shell-like glob match. Patterns |
|
478 | Arguments are expanded using shell-like glob match. Patterns | |
479 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, |
|
479 | '*', '?', '[seq]' and '[!seq]' can be used. Additionally, | |
480 | tilde '~' will be expanded into user's home directory. Unlike |
|
480 | tilde '~' will be expanded into user's home directory. Unlike | |
481 | real shells, quotation does not suppress expansions. Use |
|
481 | real shells, quotation does not suppress expansions. Use | |
482 | *two* back slashes (e.g. ``\\\\*``) to suppress expansions. |
|
482 | *two* back slashes (e.g. ``\\\\*``) to suppress expansions. | |
483 | To completely disable these expansions, you can use -G flag. |
|
483 | To completely disable these expansions, you can use -G flag. | |
484 |
|
484 | |||
485 | Options: |
|
485 | Options: | |
486 |
|
486 | |||
487 | -n |
|
487 | -n | |
488 | __name__ is NOT set to '__main__', but to the running file's name |
|
488 | __name__ is NOT set to '__main__', but to the running file's name | |
489 | without extension (as python does under import). This allows running |
|
489 | without extension (as python does under import). This allows running | |
490 | scripts and reloading the definitions in them without calling code |
|
490 | scripts and reloading the definitions in them without calling code | |
491 | protected by an ``if __name__ == "__main__"`` clause. |
|
491 | protected by an ``if __name__ == "__main__"`` clause. | |
492 |
|
492 | |||
493 | -i |
|
493 | -i | |
494 | run the file in IPython's namespace instead of an empty one. This |
|
494 | run the file in IPython's namespace instead of an empty one. This | |
495 | is useful if you are experimenting with code written in a text editor |
|
495 | is useful if you are experimenting with code written in a text editor | |
496 | which depends on variables defined interactively. |
|
496 | which depends on variables defined interactively. | |
497 |
|
497 | |||
498 | -e |
|
498 | -e | |
499 | ignore sys.exit() calls or SystemExit exceptions in the script |
|
499 | ignore sys.exit() calls or SystemExit exceptions in the script | |
500 | being run. This is particularly useful if IPython is being used to |
|
500 | being run. This is particularly useful if IPython is being used to | |
501 | run unittests, which always exit with a sys.exit() call. In such |
|
501 | run unittests, which always exit with a sys.exit() call. In such | |
502 | cases you are interested in the output of the test results, not in |
|
502 | cases you are interested in the output of the test results, not in | |
503 | seeing a traceback of the unittest module. |
|
503 | seeing a traceback of the unittest module. | |
504 |
|
504 | |||
505 | -t |
|
505 | -t | |
506 | print timing information at the end of the run. IPython will give |
|
506 | print timing information at the end of the run. IPython will give | |
507 | you an estimated CPU time consumption for your script, which under |
|
507 | you an estimated CPU time consumption for your script, which under | |
508 | Unix uses the resource module to avoid the wraparound problems of |
|
508 | Unix uses the resource module to avoid the wraparound problems of | |
509 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
509 | time.clock(). Under Unix, an estimate of time spent on system tasks | |
510 | is also given (for Windows platforms this is reported as 0.0). |
|
510 | is also given (for Windows platforms this is reported as 0.0). | |
511 |
|
511 | |||
512 | If -t is given, an additional ``-N<N>`` option can be given, where <N> |
|
512 | If -t is given, an additional ``-N<N>`` option can be given, where <N> | |
513 | must be an integer indicating how many times you want the script to |
|
513 | must be an integer indicating how many times you want the script to | |
514 | run. The final timing report will include total and per run results. |
|
514 | run. The final timing report will include total and per run results. | |
515 |
|
515 | |||
516 | For example (testing the script uniq_stable.py):: |
|
516 | For example (testing the script uniq_stable.py):: | |
517 |
|
517 | |||
518 | In [1]: run -t uniq_stable |
|
518 | In [1]: run -t uniq_stable | |
519 |
|
519 | |||
520 | IPython CPU timings (estimated): |
|
520 | IPython CPU timings (estimated): | |
521 | User : 0.19597 s. |
|
521 | User : 0.19597 s. | |
522 | System: 0.0 s. |
|
522 | System: 0.0 s. | |
523 |
|
523 | |||
524 | In [2]: run -t -N5 uniq_stable |
|
524 | In [2]: run -t -N5 uniq_stable | |
525 |
|
525 | |||
526 | IPython CPU timings (estimated): |
|
526 | IPython CPU timings (estimated): | |
527 | Total runs performed: 5 |
|
527 | Total runs performed: 5 | |
528 | Times : Total Per run |
|
528 | Times : Total Per run | |
529 | User : 0.910862 s, 0.1821724 s. |
|
529 | User : 0.910862 s, 0.1821724 s. | |
530 | System: 0.0 s, 0.0 s. |
|
530 | System: 0.0 s, 0.0 s. | |
531 |
|
531 | |||
532 | -d |
|
532 | -d | |
533 | run your program under the control of pdb, the Python debugger. |
|
533 | run your program under the control of pdb, the Python debugger. | |
534 | This allows you to execute your program step by step, watch variables, |
|
534 | This allows you to execute your program step by step, watch variables, | |
535 | etc. Internally, what IPython does is similar to calling:: |
|
535 | etc. Internally, what IPython does is similar to calling:: | |
536 |
|
536 | |||
537 | pdb.run('execfile("YOURFILENAME")') |
|
537 | pdb.run('execfile("YOURFILENAME")') | |
538 |
|
538 | |||
539 | with a breakpoint set on line 1 of your file. You can change the line |
|
539 | with a breakpoint set on line 1 of your file. You can change the line | |
540 | number for this automatic breakpoint to be <N> by using the -bN option |
|
540 | number for this automatic breakpoint to be <N> by using the -bN option | |
541 | (where N must be an integer). For example:: |
|
541 | (where N must be an integer). For example:: | |
542 |
|
542 | |||
543 | %run -d -b40 myscript |
|
543 | %run -d -b40 myscript | |
544 |
|
544 | |||
545 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
545 | will set the first breakpoint at line 40 in myscript.py. Note that | |
546 | the first breakpoint must be set on a line which actually does |
|
546 | the first breakpoint must be set on a line which actually does | |
547 | something (not a comment or docstring) for it to stop execution. |
|
547 | something (not a comment or docstring) for it to stop execution. | |
548 |
|
548 | |||
549 | Or you can specify a breakpoint in a different file:: |
|
549 | Or you can specify a breakpoint in a different file:: | |
550 |
|
550 | |||
551 | %run -d -b myotherfile.py:20 myscript |
|
551 | %run -d -b myotherfile.py:20 myscript | |
552 |
|
552 | |||
553 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
553 | When the pdb debugger starts, you will see a (Pdb) prompt. You must | |
554 | first enter 'c' (without quotes) to start execution up to the first |
|
554 | first enter 'c' (without quotes) to start execution up to the first | |
555 | breakpoint. |
|
555 | breakpoint. | |
556 |
|
556 | |||
557 | Entering 'help' gives information about the use of the debugger. You |
|
557 | Entering 'help' gives information about the use of the debugger. You | |
558 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
558 | can easily see pdb's full documentation with "import pdb;pdb.help()" | |
559 | at a prompt. |
|
559 | at a prompt. | |
560 |
|
560 | |||
561 | -p |
|
561 | -p | |
562 | run program under the control of the Python profiler module (which |
|
562 | run program under the control of the Python profiler module (which | |
563 | prints a detailed report of execution times, function calls, etc). |
|
563 | prints a detailed report of execution times, function calls, etc). | |
564 |
|
564 | |||
565 | You can pass other options after -p which affect the behavior of the |
|
565 | You can pass other options after -p which affect the behavior of the | |
566 | profiler itself. See the docs for %prun for details. |
|
566 | profiler itself. See the docs for %prun for details. | |
567 |
|
567 | |||
568 | In this mode, the program's variables do NOT propagate back to the |
|
568 | In this mode, the program's variables do NOT propagate back to the | |
569 | IPython interactive namespace (because they remain in the namespace |
|
569 | IPython interactive namespace (because they remain in the namespace | |
570 | where the profiler executes them). |
|
570 | where the profiler executes them). | |
571 |
|
571 | |||
572 | Internally this triggers a call to %prun, see its documentation for |
|
572 | Internally this triggers a call to %prun, see its documentation for | |
573 | details on the options available specifically for profiling. |
|
573 | details on the options available specifically for profiling. | |
574 |
|
574 | |||
575 | There is one special usage for which the text above doesn't apply: |
|
575 | There is one special usage for which the text above doesn't apply: | |
576 | if the filename ends with .ipy[nb], the file is run as ipython script, |
|
576 | if the filename ends with .ipy[nb], the file is run as ipython script, | |
577 | just as if the commands were written on IPython prompt. |
|
577 | just as if the commands were written on IPython prompt. | |
578 |
|
578 | |||
579 | -m |
|
579 | -m | |
580 | specify module name to load instead of script path. Similar to |
|
580 | specify module name to load instead of script path. Similar to | |
581 | the -m option for the python interpreter. Use this option last if you |
|
581 | the -m option for the python interpreter. Use this option last if you | |
582 | want to combine with other %run options. Unlike the python interpreter |
|
582 | want to combine with other %run options. Unlike the python interpreter | |
583 | only source modules are allowed no .pyc or .pyo files. |
|
583 | only source modules are allowed no .pyc or .pyo files. | |
584 | For example:: |
|
584 | For example:: | |
585 |
|
585 | |||
586 | %run -m example |
|
586 | %run -m example | |
587 |
|
587 | |||
588 | will run the example module. |
|
588 | will run the example module. | |
589 |
|
589 | |||
590 | -G |
|
590 | -G | |
591 | disable shell-like glob expansion of arguments. |
|
591 | disable shell-like glob expansion of arguments. | |
592 |
|
592 | |||
593 | """ |
|
593 | """ | |
594 |
|
594 | |||
595 | # get arguments and set sys.argv for program to be run. |
|
595 | # get arguments and set sys.argv for program to be run. | |
596 | opts, arg_lst = self.parse_options(parameter_s, |
|
596 | opts, arg_lst = self.parse_options(parameter_s, | |
597 | 'nidtN:b:pD:l:rs:T:em:G', |
|
597 | 'nidtN:b:pD:l:rs:T:em:G', | |
598 | mode='list', list_all=1) |
|
598 | mode='list', list_all=1) | |
599 | if "m" in opts: |
|
599 | if "m" in opts: | |
600 | modulename = opts["m"][0] |
|
600 | modulename = opts["m"][0] | |
601 | modpath = find_mod(modulename) |
|
601 | modpath = find_mod(modulename) | |
602 | if modpath is None: |
|
602 | if modpath is None: | |
603 | warn('%r is not a valid modulename on sys.path'%modulename) |
|
603 | warn('%r is not a valid modulename on sys.path'%modulename) | |
604 | return |
|
604 | return | |
605 | arg_lst = [modpath] + arg_lst |
|
605 | arg_lst = [modpath] + arg_lst | |
606 | try: |
|
606 | try: | |
607 | filename = file_finder(arg_lst[0]) |
|
607 | filename = file_finder(arg_lst[0]) | |
608 | except IndexError: |
|
608 | except IndexError: | |
609 | warn('you must provide at least a filename.') |
|
609 | warn('you must provide at least a filename.') | |
610 | print('\n%run:\n', oinspect.getdoc(self.run)) |
|
610 | print('\n%run:\n', oinspect.getdoc(self.run)) | |
611 | return |
|
611 | return | |
612 | except IOError as e: |
|
612 | except IOError as e: | |
613 | try: |
|
613 | try: | |
614 | msg = str(e) |
|
614 | msg = str(e) | |
615 | except UnicodeError: |
|
615 | except UnicodeError: | |
616 | msg = e.message |
|
616 | msg = e.message | |
617 | error(msg) |
|
617 | error(msg) | |
618 | return |
|
618 | return | |
619 |
|
619 | |||
620 | if filename.lower().endswith(('.ipy', '.ipynb')): |
|
620 | if filename.lower().endswith(('.ipy', '.ipynb')): | |
621 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
621 | with preserve_keys(self.shell.user_ns, '__file__'): | |
622 | self.shell.user_ns['__file__'] = filename |
|
622 | self.shell.user_ns['__file__'] = filename | |
623 | self.shell.safe_execfile_ipy(filename) |
|
623 | self.shell.safe_execfile_ipy(filename) | |
624 | return |
|
624 | return | |
625 |
|
625 | |||
626 | # Control the response to exit() calls made by the script being run |
|
626 | # Control the response to exit() calls made by the script being run | |
627 | exit_ignore = 'e' in opts |
|
627 | exit_ignore = 'e' in opts | |
628 |
|
628 | |||
629 | # Make sure that the running script gets a proper sys.argv as if it |
|
629 | # Make sure that the running script gets a proper sys.argv as if it | |
630 | # were run from a system shell. |
|
630 | # were run from a system shell. | |
631 | save_argv = sys.argv # save it for later restoring |
|
631 | save_argv = sys.argv # save it for later restoring | |
632 |
|
632 | |||
633 | if 'G' in opts: |
|
633 | if 'G' in opts: | |
634 | args = arg_lst[1:] |
|
634 | args = arg_lst[1:] | |
635 | else: |
|
635 | else: | |
636 | # tilde and glob expansion |
|
636 | # tilde and glob expansion | |
637 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) |
|
637 | args = shellglob(map(os.path.expanduser, arg_lst[1:])) | |
638 |
|
638 | |||
639 | sys.argv = [filename] + args # put in the proper filename |
|
639 | sys.argv = [filename] + args # put in the proper filename | |
640 | # protect sys.argv from potential unicode strings on Python 2: |
|
640 | # protect sys.argv from potential unicode strings on Python 2: | |
641 | if not py3compat.PY3: |
|
641 | if not py3compat.PY3: | |
642 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
642 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |
643 |
|
643 | |||
644 | if 'i' in opts: |
|
644 | if 'i' in opts: | |
645 | # Run in user's interactive namespace |
|
645 | # Run in user's interactive namespace | |
646 | prog_ns = self.shell.user_ns |
|
646 | prog_ns = self.shell.user_ns | |
647 | __name__save = self.shell.user_ns['__name__'] |
|
647 | __name__save = self.shell.user_ns['__name__'] | |
648 | prog_ns['__name__'] = '__main__' |
|
648 | prog_ns['__name__'] = '__main__' | |
649 | main_mod = self.shell.user_module |
|
649 | main_mod = self.shell.user_module | |
650 |
|
650 | |||
651 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
651 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |
652 | # set the __file__ global in the script's namespace |
|
652 | # set the __file__ global in the script's namespace | |
653 | # TK: Is this necessary in interactive mode? |
|
653 | # TK: Is this necessary in interactive mode? | |
654 | prog_ns['__file__'] = filename |
|
654 | prog_ns['__file__'] = filename | |
655 | else: |
|
655 | else: | |
656 | # Run in a fresh, empty namespace |
|
656 | # Run in a fresh, empty namespace | |
657 | if 'n' in opts: |
|
657 | if 'n' in opts: | |
658 | name = os.path.splitext(os.path.basename(filename))[0] |
|
658 | name = os.path.splitext(os.path.basename(filename))[0] | |
659 | else: |
|
659 | else: | |
660 | name = '__main__' |
|
660 | name = '__main__' | |
661 |
|
661 | |||
662 | # The shell MUST hold a reference to prog_ns so after %run |
|
662 | # The shell MUST hold a reference to prog_ns so after %run | |
663 | # exits, the python deletion mechanism doesn't zero it out |
|
663 | # exits, the python deletion mechanism doesn't zero it out | |
664 | # (leaving dangling references). See interactiveshell for details |
|
664 | # (leaving dangling references). See interactiveshell for details | |
665 | main_mod = self.shell.new_main_mod(filename, name) |
|
665 | main_mod = self.shell.new_main_mod(filename, name) | |
666 | prog_ns = main_mod.__dict__ |
|
666 | prog_ns = main_mod.__dict__ | |
667 |
|
667 | |||
668 | # pickle fix. See interactiveshell for an explanation. But we need to |
|
668 | # pickle fix. See interactiveshell for an explanation. But we need to | |
669 | # make sure that, if we overwrite __main__, we replace it at the end |
|
669 | # make sure that, if we overwrite __main__, we replace it at the end | |
670 | main_mod_name = prog_ns['__name__'] |
|
670 | main_mod_name = prog_ns['__name__'] | |
671 |
|
671 | |||
672 | if main_mod_name == '__main__': |
|
672 | if main_mod_name == '__main__': | |
673 | restore_main = sys.modules['__main__'] |
|
673 | restore_main = sys.modules['__main__'] | |
674 | else: |
|
674 | else: | |
675 | restore_main = False |
|
675 | restore_main = False | |
676 |
|
676 | |||
677 | # This needs to be undone at the end to prevent holding references to |
|
677 | # This needs to be undone at the end to prevent holding references to | |
678 | # every single object ever created. |
|
678 | # every single object ever created. | |
679 | sys.modules[main_mod_name] = main_mod |
|
679 | sys.modules[main_mod_name] = main_mod | |
680 |
|
680 | |||
681 | if 'p' in opts or 'd' in opts: |
|
681 | if 'p' in opts or 'd' in opts: | |
682 | if 'm' in opts: |
|
682 | if 'm' in opts: | |
683 | code = 'run_module(modulename, prog_ns)' |
|
683 | code = 'run_module(modulename, prog_ns)' | |
684 | code_ns = { |
|
684 | code_ns = { | |
685 | 'run_module': self.shell.safe_run_module, |
|
685 | 'run_module': self.shell.safe_run_module, | |
686 | 'prog_ns': prog_ns, |
|
686 | 'prog_ns': prog_ns, | |
687 | 'modulename': modulename, |
|
687 | 'modulename': modulename, | |
688 | } |
|
688 | } | |
689 | else: |
|
689 | else: | |
690 | if 'd' in opts: |
|
690 | if 'd' in opts: | |
691 | # allow exceptions to raise in debug mode |
|
691 | # allow exceptions to raise in debug mode | |
692 | code = 'execfile(filename, prog_ns, raise_exceptions=True)' |
|
692 | code = 'execfile(filename, prog_ns, raise_exceptions=True)' | |
693 | else: |
|
693 | else: | |
694 | code = 'execfile(filename, prog_ns)' |
|
694 | code = 'execfile(filename, prog_ns)' | |
695 | code_ns = { |
|
695 | code_ns = { | |
696 | 'execfile': self.shell.safe_execfile, |
|
696 | 'execfile': self.shell.safe_execfile, | |
697 | 'prog_ns': prog_ns, |
|
697 | 'prog_ns': prog_ns, | |
698 | 'filename': get_py_filename(filename), |
|
698 | 'filename': get_py_filename(filename), | |
699 | } |
|
699 | } | |
700 |
|
700 | |||
701 | try: |
|
701 | try: | |
702 | stats = None |
|
702 | stats = None | |
703 | with self.shell.readline_no_record: |
|
703 | with self.shell.readline_no_record: | |
704 | if 'p' in opts: |
|
704 | if 'p' in opts: | |
705 | stats = self._run_with_profiler(code, opts, code_ns) |
|
705 | stats = self._run_with_profiler(code, opts, code_ns) | |
706 | else: |
|
706 | else: | |
707 | if 'd' in opts: |
|
707 | if 'd' in opts: | |
708 | bp_file, bp_line = parse_breakpoint( |
|
708 | bp_file, bp_line = parse_breakpoint( | |
709 | opts.get('b', ['1'])[0], filename) |
|
709 | opts.get('b', ['1'])[0], filename) | |
710 | self._run_with_debugger( |
|
710 | self._run_with_debugger( | |
711 | code, code_ns, filename, bp_line, bp_file) |
|
711 | code, code_ns, filename, bp_line, bp_file) | |
712 | else: |
|
712 | else: | |
713 | if 'm' in opts: |
|
713 | if 'm' in opts: | |
714 | def run(): |
|
714 | def run(): | |
715 | self.shell.safe_run_module(modulename, prog_ns) |
|
715 | self.shell.safe_run_module(modulename, prog_ns) | |
716 | else: |
|
716 | else: | |
717 | if runner is None: |
|
717 | if runner is None: | |
718 | runner = self.default_runner |
|
718 | runner = self.default_runner | |
719 | if runner is None: |
|
719 | if runner is None: | |
720 | runner = self.shell.safe_execfile |
|
720 | runner = self.shell.safe_execfile | |
721 |
|
721 | |||
722 | def run(): |
|
722 | def run(): | |
723 | runner(filename, prog_ns, prog_ns, |
|
723 | runner(filename, prog_ns, prog_ns, | |
724 | exit_ignore=exit_ignore) |
|
724 | exit_ignore=exit_ignore) | |
725 |
|
725 | |||
726 | if 't' in opts: |
|
726 | if 't' in opts: | |
727 | # timed execution |
|
727 | # timed execution | |
728 | try: |
|
728 | try: | |
729 | nruns = int(opts['N'][0]) |
|
729 | nruns = int(opts['N'][0]) | |
730 | if nruns < 1: |
|
730 | if nruns < 1: | |
731 | error('Number of runs must be >=1') |
|
731 | error('Number of runs must be >=1') | |
732 | return |
|
732 | return | |
733 | except (KeyError): |
|
733 | except (KeyError): | |
734 | nruns = 1 |
|
734 | nruns = 1 | |
735 | self._run_with_timing(run, nruns) |
|
735 | self._run_with_timing(run, nruns) | |
736 | else: |
|
736 | else: | |
737 | # regular execution |
|
737 | # regular execution | |
738 | run() |
|
738 | run() | |
739 |
|
739 | |||
740 | if 'i' in opts: |
|
740 | if 'i' in opts: | |
741 | self.shell.user_ns['__name__'] = __name__save |
|
741 | self.shell.user_ns['__name__'] = __name__save | |
742 | else: |
|
742 | else: | |
743 | # update IPython interactive namespace |
|
743 | # update IPython interactive namespace | |
744 |
|
744 | |||
745 | # Some forms of read errors on the file may mean the |
|
745 | # Some forms of read errors on the file may mean the | |
746 | # __name__ key was never set; using pop we don't have to |
|
746 | # __name__ key was never set; using pop we don't have to | |
747 | # worry about a possible KeyError. |
|
747 | # worry about a possible KeyError. | |
748 | prog_ns.pop('__name__', None) |
|
748 | prog_ns.pop('__name__', None) | |
749 |
|
749 | |||
750 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
750 | with preserve_keys(self.shell.user_ns, '__file__'): | |
751 | self.shell.user_ns.update(prog_ns) |
|
751 | self.shell.user_ns.update(prog_ns) | |
752 | finally: |
|
752 | finally: | |
753 | # It's a bit of a mystery why, but __builtins__ can change from |
|
753 | # It's a bit of a mystery why, but __builtins__ can change from | |
754 | # being a module to becoming a dict missing some key data after |
|
754 | # being a module to becoming a dict missing some key data after | |
755 | # %run. As best I can see, this is NOT something IPython is doing |
|
755 | # %run. As best I can see, this is NOT something IPython is doing | |
756 | # at all, and similar problems have been reported before: |
|
756 | # at all, and similar problems have been reported before: | |
757 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html |
|
757 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html | |
758 | # Since this seems to be done by the interpreter itself, the best |
|
758 | # Since this seems to be done by the interpreter itself, the best | |
759 | # we can do is to at least restore __builtins__ for the user on |
|
759 | # we can do is to at least restore __builtins__ for the user on | |
760 | # exit. |
|
760 | # exit. | |
761 | self.shell.user_ns['__builtins__'] = builtin_mod |
|
761 | self.shell.user_ns['__builtins__'] = builtin_mod | |
762 |
|
762 | |||
763 | # Ensure key global structures are restored |
|
763 | # Ensure key global structures are restored | |
764 | sys.argv = save_argv |
|
764 | sys.argv = save_argv | |
765 | if restore_main: |
|
765 | if restore_main: | |
766 | sys.modules['__main__'] = restore_main |
|
766 | sys.modules['__main__'] = restore_main | |
767 | else: |
|
767 | else: | |
768 | # Remove from sys.modules the reference to main_mod we'd |
|
768 | # Remove from sys.modules the reference to main_mod we'd | |
769 | # added. Otherwise it will trap references to objects |
|
769 | # added. Otherwise it will trap references to objects | |
770 | # contained therein. |
|
770 | # contained therein. | |
771 | del sys.modules[main_mod_name] |
|
771 | del sys.modules[main_mod_name] | |
772 |
|
772 | |||
773 | return stats |
|
773 | return stats | |
774 |
|
774 | |||
775 | def _run_with_debugger(self, code, code_ns, filename=None, |
|
775 | def _run_with_debugger(self, code, code_ns, filename=None, | |
776 | bp_line=None, bp_file=None): |
|
776 | bp_line=None, bp_file=None): | |
777 | """ |
|
777 | """ | |
778 | Run `code` in debugger with a break point. |
|
778 | Run `code` in debugger with a break point. | |
779 |
|
779 | |||
780 | Parameters |
|
780 | Parameters | |
781 | ---------- |
|
781 | ---------- | |
782 | code : str |
|
782 | code : str | |
783 | Code to execute. |
|
783 | Code to execute. | |
784 | code_ns : dict |
|
784 | code_ns : dict | |
785 | A namespace in which `code` is executed. |
|
785 | A namespace in which `code` is executed. | |
786 | filename : str |
|
786 | filename : str | |
787 | `code` is ran as if it is in `filename`. |
|
787 | `code` is ran as if it is in `filename`. | |
788 | bp_line : int, optional |
|
788 | bp_line : int, optional | |
789 | Line number of the break point. |
|
789 | Line number of the break point. | |
790 | bp_file : str, optional |
|
790 | bp_file : str, optional | |
791 | Path to the file in which break point is specified. |
|
791 | Path to the file in which break point is specified. | |
792 | `filename` is used if not given. |
|
792 | `filename` is used if not given. | |
793 |
|
793 | |||
794 | Raises |
|
794 | Raises | |
795 | ------ |
|
795 | ------ | |
796 | UsageError |
|
796 | UsageError | |
797 | If the break point given by `bp_line` is not valid. |
|
797 | If the break point given by `bp_line` is not valid. | |
798 |
|
798 | |||
799 | """ |
|
799 | """ | |
800 | deb = debugger.Pdb(self.shell.colors) |
|
800 | deb = debugger.Pdb(self.shell.colors) | |
801 | # reset Breakpoint state, which is moronically kept |
|
801 | # reset Breakpoint state, which is moronically kept | |
802 | # in a class |
|
802 | # in a class | |
803 | bdb.Breakpoint.next = 1 |
|
803 | bdb.Breakpoint.next = 1 | |
804 | bdb.Breakpoint.bplist = {} |
|
804 | bdb.Breakpoint.bplist = {} | |
805 | bdb.Breakpoint.bpbynumber = [None] |
|
805 | bdb.Breakpoint.bpbynumber = [None] | |
806 | if bp_line is not None: |
|
806 | if bp_line is not None: | |
807 | # Set an initial breakpoint to stop execution |
|
807 | # Set an initial breakpoint to stop execution | |
808 | maxtries = 10 |
|
808 | maxtries = 10 | |
809 | bp_file = bp_file or filename |
|
809 | bp_file = bp_file or filename | |
810 | checkline = deb.checkline(bp_file, bp_line) |
|
810 | checkline = deb.checkline(bp_file, bp_line) | |
811 | if not checkline: |
|
811 | if not checkline: | |
812 | for bp in range(bp_line + 1, bp_line + maxtries + 1): |
|
812 | for bp in range(bp_line + 1, bp_line + maxtries + 1): | |
813 | if deb.checkline(bp_file, bp): |
|
813 | if deb.checkline(bp_file, bp): | |
814 | break |
|
814 | break | |
815 | else: |
|
815 | else: | |
816 | msg = ("\nI failed to find a valid line to set " |
|
816 | msg = ("\nI failed to find a valid line to set " | |
817 | "a breakpoint\n" |
|
817 | "a breakpoint\n" | |
818 | "after trying up to line: %s.\n" |
|
818 | "after trying up to line: %s.\n" | |
819 | "Please set a valid breakpoint manually " |
|
819 | "Please set a valid breakpoint manually " | |
820 | "with the -b option." % bp) |
|
820 | "with the -b option." % bp) | |
821 | raise UsageError(msg) |
|
821 | raise UsageError(msg) | |
822 | # if we find a good linenumber, set the breakpoint |
|
822 | # if we find a good linenumber, set the breakpoint | |
823 | deb.do_break('%s:%s' % (bp_file, bp_line)) |
|
823 | deb.do_break('%s:%s' % (bp_file, bp_line)) | |
824 |
|
824 | |||
825 | if filename: |
|
825 | if filename: | |
826 | # Mimic Pdb._runscript(...) |
|
826 | # Mimic Pdb._runscript(...) | |
827 | deb._wait_for_mainpyfile = True |
|
827 | deb._wait_for_mainpyfile = True | |
828 | deb.mainpyfile = deb.canonic(filename) |
|
828 | deb.mainpyfile = deb.canonic(filename) | |
829 |
|
829 | |||
830 | # Start file run |
|
830 | # Start file run | |
831 | print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt) |
|
831 | print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt) | |
832 | try: |
|
832 | try: | |
833 | if filename: |
|
833 | if filename: | |
834 | # save filename so it can be used by methods on the deb object |
|
834 | # save filename so it can be used by methods on the deb object | |
835 | deb._exec_filename = filename |
|
835 | deb._exec_filename = filename | |
836 | while True: |
|
836 | while True: | |
837 | try: |
|
837 | try: | |
838 | deb.run(code, code_ns) |
|
838 | deb.run(code, code_ns) | |
839 | except Restart: |
|
839 | except Restart: | |
840 | print("Restarting") |
|
840 | print("Restarting") | |
841 | if filename: |
|
841 | if filename: | |
842 | deb._wait_for_mainpyfile = True |
|
842 | deb._wait_for_mainpyfile = True | |
843 | deb.mainpyfile = deb.canonic(filename) |
|
843 | deb.mainpyfile = deb.canonic(filename) | |
844 | continue |
|
844 | continue | |
845 | else: |
|
845 | else: | |
846 | break |
|
846 | break | |
847 |
|
847 | |||
848 |
|
848 | |||
849 | except: |
|
849 | except: | |
850 | etype, value, tb = sys.exc_info() |
|
850 | etype, value, tb = sys.exc_info() | |
851 | # Skip three frames in the traceback: the %run one, |
|
851 | # Skip three frames in the traceback: the %run one, | |
852 | # one inside bdb.py, and the command-line typed by the |
|
852 | # one inside bdb.py, and the command-line typed by the | |
853 | # user (run by exec in pdb itself). |
|
853 | # user (run by exec in pdb itself). | |
854 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) |
|
854 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) | |
855 |
|
855 | |||
856 | @staticmethod |
|
856 | @staticmethod | |
857 | def _run_with_timing(run, nruns): |
|
857 | def _run_with_timing(run, nruns): | |
858 | """ |
|
858 | """ | |
859 | Run function `run` and print timing information. |
|
859 | Run function `run` and print timing information. | |
860 |
|
860 | |||
861 | Parameters |
|
861 | Parameters | |
862 | ---------- |
|
862 | ---------- | |
863 | run : callable |
|
863 | run : callable | |
864 | Any callable object which takes no argument. |
|
864 | Any callable object which takes no argument. | |
865 | nruns : int |
|
865 | nruns : int | |
866 | Number of times to execute `run`. |
|
866 | Number of times to execute `run`. | |
867 |
|
867 | |||
868 | """ |
|
868 | """ | |
869 | twall0 = time.time() |
|
869 | twall0 = time.time() | |
870 | if nruns == 1: |
|
870 | if nruns == 1: | |
871 | t0 = clock2() |
|
871 | t0 = clock2() | |
872 | run() |
|
872 | run() | |
873 | t1 = clock2() |
|
873 | t1 = clock2() | |
874 | t_usr = t1[0] - t0[0] |
|
874 | t_usr = t1[0] - t0[0] | |
875 | t_sys = t1[1] - t0[1] |
|
875 | t_sys = t1[1] - t0[1] | |
876 | print("\nIPython CPU timings (estimated):") |
|
876 | print("\nIPython CPU timings (estimated):") | |
877 | print(" User : %10.2f s." % t_usr) |
|
877 | print(" User : %10.2f s." % t_usr) | |
878 | print(" System : %10.2f s." % t_sys) |
|
878 | print(" System : %10.2f s." % t_sys) | |
879 | else: |
|
879 | else: | |
880 | runs = range(nruns) |
|
880 | runs = range(nruns) | |
881 | t0 = clock2() |
|
881 | t0 = clock2() | |
882 | for nr in runs: |
|
882 | for nr in runs: | |
883 | run() |
|
883 | run() | |
884 | t1 = clock2() |
|
884 | t1 = clock2() | |
885 | t_usr = t1[0] - t0[0] |
|
885 | t_usr = t1[0] - t0[0] | |
886 | t_sys = t1[1] - t0[1] |
|
886 | t_sys = t1[1] - t0[1] | |
887 | print("\nIPython CPU timings (estimated):") |
|
887 | print("\nIPython CPU timings (estimated):") | |
888 | print("Total runs performed:", nruns) |
|
888 | print("Total runs performed:", nruns) | |
889 | print(" Times : %10s %10s" % ('Total', 'Per run')) |
|
889 | print(" Times : %10s %10s" % ('Total', 'Per run')) | |
890 | print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)) |
|
890 | print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)) | |
891 | print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)) |
|
891 | print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)) | |
892 | twall1 = time.time() |
|
892 | twall1 = time.time() | |
893 | print("Wall time: %10.2f s." % (twall1 - twall0)) |
|
893 | print("Wall time: %10.2f s." % (twall1 - twall0)) | |
894 |
|
894 | |||
895 | @skip_doctest |
|
895 | @skip_doctest | |
896 | @line_cell_magic |
|
896 | @line_cell_magic | |
897 | def timeit(self, line='', cell=None): |
|
897 | def timeit(self, line='', cell=None): | |
898 | """Time execution of a Python statement or expression |
|
898 | """Time execution of a Python statement or expression | |
899 |
|
899 | |||
900 | Usage, in line mode: |
|
900 | Usage, in line mode: | |
901 | %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement |
|
901 | %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement | |
902 | or in cell mode: |
|
902 | or in cell mode: | |
903 | %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code |
|
903 | %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code | |
904 | code |
|
904 | code | |
905 | code... |
|
905 | code... | |
906 |
|
906 | |||
907 | Time execution of a Python statement or expression using the timeit |
|
907 | Time execution of a Python statement or expression using the timeit | |
908 | module. This function can be used both as a line and cell magic: |
|
908 | module. This function can be used both as a line and cell magic: | |
909 |
|
909 | |||
910 | - In line mode you can time a single-line statement (though multiple |
|
910 | - In line mode you can time a single-line statement (though multiple | |
911 | ones can be chained with using semicolons). |
|
911 | ones can be chained with using semicolons). | |
912 |
|
912 | |||
913 | - In cell mode, the statement in the first line is used as setup code |
|
913 | - In cell mode, the statement in the first line is used as setup code | |
914 | (executed but not timed) and the body of the cell is timed. The cell |
|
914 | (executed but not timed) and the body of the cell is timed. The cell | |
915 | body has access to any variables created in the setup code. |
|
915 | body has access to any variables created in the setup code. | |
916 |
|
916 | |||
917 | Options: |
|
917 | Options: | |
918 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
918 | -n<N>: execute the given statement <N> times in a loop. If this value | |
919 | is not given, a fitting value is chosen. |
|
919 | is not given, a fitting value is chosen. | |
920 |
|
920 | |||
921 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
921 | -r<R>: repeat the loop iteration <R> times and take the best result. | |
922 | Default: 3 |
|
922 | Default: 3 | |
923 |
|
923 | |||
924 | -t: use time.time to measure the time, which is the default on Unix. |
|
924 | -t: use time.time to measure the time, which is the default on Unix. | |
925 | This function measures wall time. |
|
925 | This function measures wall time. | |
926 |
|
926 | |||
927 | -c: use time.clock to measure the time, which is the default on |
|
927 | -c: use time.clock to measure the time, which is the default on | |
928 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
928 | Windows and measures wall time. On Unix, resource.getrusage is used | |
929 | instead and returns the CPU user time. |
|
929 | instead and returns the CPU user time. | |
930 |
|
930 | |||
931 | -p<P>: use a precision of <P> digits to display the timing result. |
|
931 | -p<P>: use a precision of <P> digits to display the timing result. | |
932 | Default: 3 |
|
932 | Default: 3 | |
933 |
|
933 | |||
934 | -q: Quiet, do not print result. |
|
934 | -q: Quiet, do not print result. | |
935 |
|
935 | |||
936 | -o: return a TimeitResult that can be stored in a variable to inspect |
|
936 | -o: return a TimeitResult that can be stored in a variable to inspect | |
937 | the result in more details. |
|
937 | the result in more details. | |
938 |
|
938 | |||
939 |
|
939 | |||
940 | Examples |
|
940 | Examples | |
941 | -------- |
|
941 | -------- | |
942 | :: |
|
942 | :: | |
943 |
|
943 | |||
944 | In [1]: %timeit pass |
|
944 | In [1]: %timeit pass | |
945 | 10000000 loops, best of 3: 53.3 ns per loop |
|
945 | 10000000 loops, best of 3: 53.3 ns per loop | |
946 |
|
946 | |||
947 | In [2]: u = None |
|
947 | In [2]: u = None | |
948 |
|
948 | |||
949 | In [3]: %timeit u is None |
|
949 | In [3]: %timeit u is None | |
950 | 10000000 loops, best of 3: 184 ns per loop |
|
950 | 10000000 loops, best of 3: 184 ns per loop | |
951 |
|
951 | |||
952 | In [4]: %timeit -r 4 u == None |
|
952 | In [4]: %timeit -r 4 u == None | |
953 | 1000000 loops, best of 4: 242 ns per loop |
|
953 | 1000000 loops, best of 4: 242 ns per loop | |
954 |
|
954 | |||
955 | In [5]: import time |
|
955 | In [5]: import time | |
956 |
|
956 | |||
957 | In [6]: %timeit -n1 time.sleep(2) |
|
957 | In [6]: %timeit -n1 time.sleep(2) | |
958 | 1 loops, best of 3: 2 s per loop |
|
958 | 1 loops, best of 3: 2 s per loop | |
959 |
|
959 | |||
960 |
|
960 | |||
961 | The times reported by %timeit will be slightly higher than those |
|
961 | The times reported by %timeit will be slightly higher than those | |
962 | reported by the timeit.py script when variables are accessed. This is |
|
962 | reported by the timeit.py script when variables are accessed. This is | |
963 | due to the fact that %timeit executes the statement in the namespace |
|
963 | due to the fact that %timeit executes the statement in the namespace | |
964 | of the shell, compared with timeit.py, which uses a single setup |
|
964 | of the shell, compared with timeit.py, which uses a single setup | |
965 | statement to import function or create variables. Generally, the bias |
|
965 | statement to import function or create variables. Generally, the bias | |
966 | does not matter as long as results from timeit.py are not mixed with |
|
966 | does not matter as long as results from timeit.py are not mixed with | |
967 | those from %timeit.""" |
|
967 | those from %timeit.""" | |
968 |
|
968 | |||
969 | opts, stmt = self.parse_options(line,'n:r:tcp:qo', |
|
969 | opts, stmt = self.parse_options(line,'n:r:tcp:qo', | |
970 | posix=False, strict=False) |
|
970 | posix=False, strict=False) | |
971 | if stmt == "" and cell is None: |
|
971 | if stmt == "" and cell is None: | |
972 | return |
|
972 | return | |
973 |
|
973 | |||
974 | timefunc = timeit.default_timer |
|
974 | timefunc = timeit.default_timer | |
975 | number = int(getattr(opts, "n", 0)) |
|
975 | number = int(getattr(opts, "n", 0)) | |
976 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
976 | repeat = int(getattr(opts, "r", timeit.default_repeat)) | |
977 | precision = int(getattr(opts, "p", 3)) |
|
977 | precision = int(getattr(opts, "p", 3)) | |
978 | quiet = 'q' in opts |
|
978 | quiet = 'q' in opts | |
979 | return_result = 'o' in opts |
|
979 | return_result = 'o' in opts | |
980 | if hasattr(opts, "t"): |
|
980 | if hasattr(opts, "t"): | |
981 | timefunc = time.time |
|
981 | timefunc = time.time | |
982 | if hasattr(opts, "c"): |
|
982 | if hasattr(opts, "c"): | |
983 | timefunc = clock |
|
983 | timefunc = clock | |
984 |
|
984 | |||
985 | timer = Timer(timer=timefunc) |
|
985 | timer = Timer(timer=timefunc) | |
986 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
986 | # this code has tight coupling to the inner workings of timeit.Timer, | |
987 | # but is there a better way to achieve that the code stmt has access |
|
987 | # but is there a better way to achieve that the code stmt has access | |
988 | # to the shell namespace? |
|
988 | # to the shell namespace? | |
989 | transform = self.shell.input_splitter.transform_cell |
|
989 | transform = self.shell.input_splitter.transform_cell | |
990 |
|
990 | |||
991 | if cell is None: |
|
991 | if cell is None: | |
992 | # called as line magic |
|
992 | # called as line magic | |
993 | ast_setup = self.shell.compile.ast_parse("pass") |
|
993 | ast_setup = self.shell.compile.ast_parse("pass") | |
994 | ast_stmt = self.shell.compile.ast_parse(transform(stmt)) |
|
994 | ast_stmt = self.shell.compile.ast_parse(transform(stmt)) | |
995 | else: |
|
995 | else: | |
996 | ast_setup = self.shell.compile.ast_parse(transform(stmt)) |
|
996 | ast_setup = self.shell.compile.ast_parse(transform(stmt)) | |
997 | ast_stmt = self.shell.compile.ast_parse(transform(cell)) |
|
997 | ast_stmt = self.shell.compile.ast_parse(transform(cell)) | |
998 |
|
998 | |||
999 | ast_setup = self.shell.transform_ast(ast_setup) |
|
999 | ast_setup = self.shell.transform_ast(ast_setup) | |
1000 | ast_stmt = self.shell.transform_ast(ast_stmt) |
|
1000 | ast_stmt = self.shell.transform_ast(ast_stmt) | |
1001 |
|
1001 | |||
1002 | # This codestring is taken from timeit.template - we fill it in as an |
|
1002 | # This codestring is taken from timeit.template - we fill it in as an | |
1003 | # AST, so that we can apply our AST transformations to the user code |
|
1003 | # AST, so that we can apply our AST transformations to the user code | |
1004 | # without affecting the timing code. |
|
1004 | # without affecting the timing code. | |
1005 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' |
|
1005 | timeit_ast_template = ast.parse('def inner(_it, _timer):\n' | |
1006 | ' setup\n' |
|
1006 | ' setup\n' | |
1007 | ' _t0 = _timer()\n' |
|
1007 | ' _t0 = _timer()\n' | |
1008 | ' for _i in _it:\n' |
|
1008 | ' for _i in _it:\n' | |
1009 | ' stmt\n' |
|
1009 | ' stmt\n' | |
1010 | ' _t1 = _timer()\n' |
|
1010 | ' _t1 = _timer()\n' | |
1011 | ' return _t1 - _t0\n') |
|
1011 | ' return _t1 - _t0\n') | |
1012 |
|
1012 | |||
1013 | timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template) |
|
1013 | timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template) | |
1014 | timeit_ast = ast.fix_missing_locations(timeit_ast) |
|
1014 | timeit_ast = ast.fix_missing_locations(timeit_ast) | |
1015 |
|
1015 | |||
1016 | # Track compilation time so it can be reported if too long |
|
1016 | # Track compilation time so it can be reported if too long | |
1017 | # Minimum time above which compilation time will be reported |
|
1017 | # Minimum time above which compilation time will be reported | |
1018 | tc_min = 0.1 |
|
1018 | tc_min = 0.1 | |
1019 |
|
1019 | |||
1020 | t0 = clock() |
|
1020 | t0 = clock() | |
1021 | code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec") |
|
1021 | code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec") | |
1022 | tc = clock()-t0 |
|
1022 | tc = clock()-t0 | |
1023 |
|
1023 | |||
1024 | ns = {} |
|
1024 | ns = {} | |
1025 | exec(code, self.shell.user_ns, ns) |
|
1025 | exec(code, self.shell.user_ns, ns) | |
1026 | timer.inner = ns["inner"] |
|
1026 | timer.inner = ns["inner"] | |
1027 |
|
1027 | |||
1028 | # This is used to check if there is a huge difference between the |
|
1028 | # This is used to check if there is a huge difference between the | |
1029 | # best and worst timings. |
|
1029 | # best and worst timings. | |
1030 | # Issue: https://github.com/ipython/ipython/issues/6471 |
|
1030 | # Issue: https://github.com/ipython/ipython/issues/6471 | |
1031 | worst_tuning = 0 |
|
1031 | worst_tuning = 0 | |
1032 | if number == 0: |
|
1032 | if number == 0: | |
1033 | # determine number so that 0.2 <= total time < 2.0 |
|
1033 | # determine number so that 0.2 <= total time < 2.0 | |
1034 | number = 1 |
|
1034 | number = 1 | |
1035 | for _ in range(1, 10): |
|
1035 | for _ in range(1, 10): | |
1036 | time_number = timer.timeit(number) |
|
1036 | time_number = timer.timeit(number) | |
1037 | worst_tuning = max(worst_tuning, time_number / number) |
|
1037 | worst_tuning = max(worst_tuning, time_number / number) | |
1038 | if time_number >= 0.2: |
|
1038 | if time_number >= 0.2: | |
1039 | break |
|
1039 | break | |
1040 | number *= 10 |
|
1040 | number *= 10 | |
1041 | all_runs = timer.repeat(repeat, number) |
|
1041 | all_runs = timer.repeat(repeat, number) | |
1042 | best = min(all_runs) / number |
|
1042 | best = min(all_runs) / number | |
1043 | if not quiet : |
|
1043 | if not quiet : | |
1044 | worst = max(all_runs) / number |
|
1044 | worst = max(all_runs) / number | |
1045 | if worst_tuning: |
|
1045 | if worst_tuning: | |
1046 | worst = max(worst, worst_tuning) |
|
1046 | worst = max(worst, worst_tuning) | |
1047 | # Check best timing is greater than zero to avoid a |
|
1047 | # Check best timing is greater than zero to avoid a | |
1048 | # ZeroDivisionError. |
|
1048 | # ZeroDivisionError. | |
1049 | # In cases where the slowest timing is lesser than a micosecond |
|
1049 | # In cases where the slowest timing is lesser than a micosecond | |
1050 | # we assume that it does not really matter if the fastest |
|
1050 | # we assume that it does not really matter if the fastest | |
1051 | # timing is 4 times faster than the slowest timing or not. |
|
1051 | # timing is 4 times faster than the slowest timing or not. | |
1052 | if worst > 4 * best and best > 0 and worst > 1e-6: |
|
1052 | if worst > 4 * best and best > 0 and worst > 1e-6: | |
1053 | print("The slowest run took %0.2f times longer than the " |
|
1053 | print("The slowest run took %0.2f times longer than the " | |
1054 | "fastest. This could mean that an intermediate result " |
|
1054 | "fastest. This could mean that an intermediate result " | |
1055 | "is being cached " % (worst / best)) |
|
1055 | "is being cached " % (worst / best)) | |
1056 | print(u"%d loops, best of %d: %s per loop" % (number, repeat, |
|
1056 | print(u"%d loops, best of %d: %s per loop" % (number, repeat, | |
1057 | _format_time(best, precision))) |
|
1057 | _format_time(best, precision))) | |
1058 | if tc > tc_min: |
|
1058 | if tc > tc_min: | |
1059 | print("Compiler time: %.2f s" % tc) |
|
1059 | print("Compiler time: %.2f s" % tc) | |
1060 | if return_result: |
|
1060 | if return_result: | |
1061 | return TimeitResult(number, repeat, best, all_runs, tc, precision) |
|
1061 | return TimeitResult(number, repeat, best, all_runs, tc, precision) | |
1062 |
|
1062 | |||
1063 | @skip_doctest |
|
1063 | @skip_doctest | |
1064 | @needs_local_scope |
|
1064 | @needs_local_scope | |
1065 | @line_cell_magic |
|
1065 | @line_cell_magic | |
1066 | def time(self,line='', cell=None, local_ns=None): |
|
1066 | def time(self,line='', cell=None, local_ns=None): | |
1067 | """Time execution of a Python statement or expression. |
|
1067 | """Time execution of a Python statement or expression. | |
1068 |
|
1068 | |||
1069 | The CPU and wall clock times are printed, and the value of the |
|
1069 | The CPU and wall clock times are printed, and the value of the | |
1070 | expression (if any) is returned. Note that under Win32, system time |
|
1070 | expression (if any) is returned. Note that under Win32, system time | |
1071 | is always reported as 0, since it can not be measured. |
|
1071 | is always reported as 0, since it can not be measured. | |
1072 |
|
1072 | |||
1073 | This function can be used both as a line and cell magic: |
|
1073 | This function can be used both as a line and cell magic: | |
1074 |
|
1074 | |||
1075 | - In line mode you can time a single-line statement (though multiple |
|
1075 | - In line mode you can time a single-line statement (though multiple | |
1076 | ones can be chained with using semicolons). |
|
1076 | ones can be chained with using semicolons). | |
1077 |
|
1077 | |||
1078 | - In cell mode, you can time the cell body (a directly |
|
1078 | - In cell mode, you can time the cell body (a directly | |
1079 | following statement raises an error). |
|
1079 | following statement raises an error). | |
1080 |
|
1080 | |||
1081 | This function provides very basic timing functionality. Use the timeit |
|
1081 | This function provides very basic timing functionality. Use the timeit | |
1082 | magic for more control over the measurement. |
|
1082 | magic for more control over the measurement. | |
1083 |
|
1083 | |||
1084 | Examples |
|
1084 | Examples | |
1085 | -------- |
|
1085 | -------- | |
1086 | :: |
|
1086 | :: | |
1087 |
|
1087 | |||
1088 | In [1]: %time 2**128 |
|
1088 | In [1]: %time 2**128 | |
1089 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1089 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1090 | Wall time: 0.00 |
|
1090 | Wall time: 0.00 | |
1091 | Out[1]: 340282366920938463463374607431768211456L |
|
1091 | Out[1]: 340282366920938463463374607431768211456L | |
1092 |
|
1092 | |||
1093 | In [2]: n = 1000000 |
|
1093 | In [2]: n = 1000000 | |
1094 |
|
1094 | |||
1095 | In [3]: %time sum(range(n)) |
|
1095 | In [3]: %time sum(range(n)) | |
1096 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1096 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s | |
1097 | Wall time: 1.37 |
|
1097 | Wall time: 1.37 | |
1098 | Out[3]: 499999500000L |
|
1098 | Out[3]: 499999500000L | |
1099 |
|
1099 | |||
1100 | In [4]: %time print 'hello world' |
|
1100 | In [4]: %time print 'hello world' | |
1101 | hello world |
|
1101 | hello world | |
1102 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1102 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1103 | Wall time: 0.00 |
|
1103 | Wall time: 0.00 | |
1104 |
|
1104 | |||
1105 | Note that the time needed by Python to compile the given expression |
|
1105 | Note that the time needed by Python to compile the given expression | |
1106 | will be reported if it is more than 0.1s. In this example, the |
|
1106 | will be reported if it is more than 0.1s. In this example, the | |
1107 | actual exponentiation is done by Python at compilation time, so while |
|
1107 | actual exponentiation is done by Python at compilation time, so while | |
1108 | the expression can take a noticeable amount of time to compute, that |
|
1108 | the expression can take a noticeable amount of time to compute, that | |
1109 | time is purely due to the compilation: |
|
1109 | time is purely due to the compilation: | |
1110 |
|
1110 | |||
1111 | In [5]: %time 3**9999; |
|
1111 | In [5]: %time 3**9999; | |
1112 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1112 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1113 | Wall time: 0.00 s |
|
1113 | Wall time: 0.00 s | |
1114 |
|
1114 | |||
1115 | In [6]: %time 3**999999; |
|
1115 | In [6]: %time 3**999999; | |
1116 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1116 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
1117 | Wall time: 0.00 s |
|
1117 | Wall time: 0.00 s | |
1118 | Compiler : 0.78 s |
|
1118 | Compiler : 0.78 s | |
1119 | """ |
|
1119 | """ | |
1120 |
|
1120 | |||
1121 | # fail immediately if the given expression can't be compiled |
|
1121 | # fail immediately if the given expression can't be compiled | |
1122 |
|
1122 | |||
1123 | if line and cell: |
|
1123 | if line and cell: | |
1124 | raise UsageError("Can't use statement directly after '%%time'!") |
|
1124 | raise UsageError("Can't use statement directly after '%%time'!") | |
1125 |
|
1125 | |||
1126 | if cell: |
|
1126 | if cell: | |
1127 | expr = self.shell.input_transformer_manager.transform_cell(cell) |
|
1127 | expr = self.shell.input_transformer_manager.transform_cell(cell) | |
1128 | else: |
|
1128 | else: | |
1129 | expr = self.shell.input_transformer_manager.transform_cell(line) |
|
1129 | expr = self.shell.input_transformer_manager.transform_cell(line) | |
1130 |
|
1130 | |||
1131 | # Minimum time above which parse time will be reported |
|
1131 | # Minimum time above which parse time will be reported | |
1132 | tp_min = 0.1 |
|
1132 | tp_min = 0.1 | |
1133 |
|
1133 | |||
1134 | t0 = clock() |
|
1134 | t0 = clock() | |
1135 | expr_ast = self.shell.compile.ast_parse(expr) |
|
1135 | expr_ast = self.shell.compile.ast_parse(expr) | |
1136 | tp = clock()-t0 |
|
1136 | tp = clock()-t0 | |
1137 |
|
1137 | |||
1138 | # Apply AST transformations |
|
1138 | # Apply AST transformations | |
1139 | expr_ast = self.shell.transform_ast(expr_ast) |
|
1139 | expr_ast = self.shell.transform_ast(expr_ast) | |
1140 |
|
1140 | |||
1141 | # Minimum time above which compilation time will be reported |
|
1141 | # Minimum time above which compilation time will be reported | |
1142 | tc_min = 0.1 |
|
1142 | tc_min = 0.1 | |
1143 |
|
1143 | |||
1144 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): |
|
1144 | if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr): | |
1145 | mode = 'eval' |
|
1145 | mode = 'eval' | |
1146 | source = '<timed eval>' |
|
1146 | source = '<timed eval>' | |
1147 | expr_ast = ast.Expression(expr_ast.body[0].value) |
|
1147 | expr_ast = ast.Expression(expr_ast.body[0].value) | |
1148 | else: |
|
1148 | else: | |
1149 | mode = 'exec' |
|
1149 | mode = 'exec' | |
1150 | source = '<timed exec>' |
|
1150 | source = '<timed exec>' | |
1151 | t0 = clock() |
|
1151 | t0 = clock() | |
1152 | code = self.shell.compile(expr_ast, source, mode) |
|
1152 | code = self.shell.compile(expr_ast, source, mode) | |
1153 | tc = clock()-t0 |
|
1153 | tc = clock()-t0 | |
1154 |
|
1154 | |||
1155 | # skew measurement as little as possible |
|
1155 | # skew measurement as little as possible | |
1156 | glob = self.shell.user_ns |
|
1156 | glob = self.shell.user_ns | |
1157 | wtime = time.time |
|
1157 | wtime = time.time | |
1158 | # time execution |
|
1158 | # time execution | |
1159 | wall_st = wtime() |
|
1159 | wall_st = wtime() | |
1160 | if mode=='eval': |
|
1160 | if mode=='eval': | |
1161 | st = clock2() |
|
1161 | st = clock2() | |
1162 | out = eval(code, glob, local_ns) |
|
1162 | out = eval(code, glob, local_ns) | |
1163 | end = clock2() |
|
1163 | end = clock2() | |
1164 | else: |
|
1164 | else: | |
1165 | st = clock2() |
|
1165 | st = clock2() | |
1166 | exec(code, glob, local_ns) |
|
1166 | exec(code, glob, local_ns) | |
1167 | end = clock2() |
|
1167 | end = clock2() | |
1168 | out = None |
|
1168 | out = None | |
1169 | wall_end = wtime() |
|
1169 | wall_end = wtime() | |
1170 | # Compute actual times and report |
|
1170 | # Compute actual times and report | |
1171 | wall_time = wall_end-wall_st |
|
1171 | wall_time = wall_end-wall_st | |
1172 | cpu_user = end[0]-st[0] |
|
1172 | cpu_user = end[0]-st[0] | |
1173 | cpu_sys = end[1]-st[1] |
|
1173 | cpu_sys = end[1]-st[1] | |
1174 | cpu_tot = cpu_user+cpu_sys |
|
1174 | cpu_tot = cpu_user+cpu_sys | |
1175 | # On windows cpu_sys is always zero, so no new information to the next print |
|
1175 | # On windows cpu_sys is always zero, so no new information to the next print | |
1176 | if sys.platform != 'win32': |
|
1176 | if sys.platform != 'win32': | |
1177 | print("CPU times: user %s, sys: %s, total: %s" % \ |
|
1177 | print("CPU times: user %s, sys: %s, total: %s" % \ | |
1178 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))) |
|
1178 | (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))) | |
1179 | print("Wall time: %s" % _format_time(wall_time)) |
|
1179 | print("Wall time: %s" % _format_time(wall_time)) | |
1180 | if tc > tc_min: |
|
1180 | if tc > tc_min: | |
1181 | print("Compiler : %s" % _format_time(tc)) |
|
1181 | print("Compiler : %s" % _format_time(tc)) | |
1182 | if tp > tp_min: |
|
1182 | if tp > tp_min: | |
1183 | print("Parser : %s" % _format_time(tp)) |
|
1183 | print("Parser : %s" % _format_time(tp)) | |
1184 | return out |
|
1184 | return out | |
1185 |
|
1185 | |||
1186 | @skip_doctest |
|
1186 | @skip_doctest | |
1187 | @line_magic |
|
1187 | @line_magic | |
1188 | def macro(self, parameter_s=''): |
|
1188 | def macro(self, parameter_s=''): | |
1189 | """Define a macro for future re-execution. It accepts ranges of history, |
|
1189 | """Define a macro for future re-execution. It accepts ranges of history, | |
1190 | filenames or string objects. |
|
1190 | filenames or string objects. | |
1191 |
|
1191 | |||
1192 | Usage:\\ |
|
1192 | Usage:\\ | |
1193 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1193 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... | |
1194 |
|
1194 | |||
1195 | Options: |
|
1195 | Options: | |
1196 |
|
1196 | |||
1197 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
1197 | -r: use 'raw' input. By default, the 'processed' history is used, | |
1198 | so that magics are loaded in their transformed version to valid |
|
1198 | so that magics are loaded in their transformed version to valid | |
1199 | Python. If this option is given, the raw input as typed at the |
|
1199 | Python. If this option is given, the raw input as typed at the | |
1200 | command line is used instead. |
|
1200 | command line is used instead. | |
1201 |
|
1201 | |||
1202 | -q: quiet macro definition. By default, a tag line is printed |
|
1202 | -q: quiet macro definition. By default, a tag line is printed | |
1203 | to indicate the macro has been created, and then the contents of |
|
1203 | to indicate the macro has been created, and then the contents of | |
1204 | the macro are printed. If this option is given, then no printout |
|
1204 | the macro are printed. If this option is given, then no printout | |
1205 | is produced once the macro is created. |
|
1205 | is produced once the macro is created. | |
1206 |
|
1206 | |||
1207 | This will define a global variable called `name` which is a string |
|
1207 | This will define a global variable called `name` which is a string | |
1208 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1208 | made of joining the slices and lines you specify (n1,n2,... numbers | |
1209 | above) from your input history into a single string. This variable |
|
1209 | above) from your input history into a single string. This variable | |
1210 | acts like an automatic function which re-executes those lines as if |
|
1210 | acts like an automatic function which re-executes those lines as if | |
1211 | you had typed them. You just type 'name' at the prompt and the code |
|
1211 | you had typed them. You just type 'name' at the prompt and the code | |
1212 | executes. |
|
1212 | executes. | |
1213 |
|
1213 | |||
1214 | The syntax for indicating input ranges is described in %history. |
|
1214 | The syntax for indicating input ranges is described in %history. | |
1215 |
|
1215 | |||
1216 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1216 | Note: as a 'hidden' feature, you can also use traditional python slice | |
1217 | notation, where N:M means numbers N through M-1. |
|
1217 | notation, where N:M means numbers N through M-1. | |
1218 |
|
1218 | |||
1219 | For example, if your history contains (print using %hist -n ):: |
|
1219 | For example, if your history contains (print using %hist -n ):: | |
1220 |
|
1220 | |||
1221 | 44: x=1 |
|
1221 | 44: x=1 | |
1222 | 45: y=3 |
|
1222 | 45: y=3 | |
1223 | 46: z=x+y |
|
1223 | 46: z=x+y | |
1224 | 47: print x |
|
1224 | 47: print x | |
1225 | 48: a=5 |
|
1225 | 48: a=5 | |
1226 | 49: print 'x',x,'y',y |
|
1226 | 49: print 'x',x,'y',y | |
1227 |
|
1227 | |||
1228 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1228 | you can create a macro with lines 44 through 47 (included) and line 49 | |
1229 | called my_macro with:: |
|
1229 | called my_macro with:: | |
1230 |
|
1230 | |||
1231 | In [55]: %macro my_macro 44-47 49 |
|
1231 | In [55]: %macro my_macro 44-47 49 | |
1232 |
|
1232 | |||
1233 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1233 | Now, typing `my_macro` (without quotes) will re-execute all this code | |
1234 | in one pass. |
|
1234 | in one pass. | |
1235 |
|
1235 | |||
1236 | You don't need to give the line-numbers in order, and any given line |
|
1236 | You don't need to give the line-numbers in order, and any given line | |
1237 | number can appear multiple times. You can assemble macros with any |
|
1237 | number can appear multiple times. You can assemble macros with any | |
1238 | lines from your input history in any order. |
|
1238 | lines from your input history in any order. | |
1239 |
|
1239 | |||
1240 | The macro is a simple object which holds its value in an attribute, |
|
1240 | The macro is a simple object which holds its value in an attribute, | |
1241 | but IPython's display system checks for macros and executes them as |
|
1241 | but IPython's display system checks for macros and executes them as | |
1242 | code instead of printing them when you type their name. |
|
1242 | code instead of printing them when you type their name. | |
1243 |
|
1243 | |||
1244 | You can view a macro's contents by explicitly printing it with:: |
|
1244 | You can view a macro's contents by explicitly printing it with:: | |
1245 |
|
1245 | |||
1246 | print macro_name |
|
1246 | print macro_name | |
1247 |
|
1247 | |||
1248 | """ |
|
1248 | """ | |
1249 | opts,args = self.parse_options(parameter_s,'rq',mode='list') |
|
1249 | opts,args = self.parse_options(parameter_s,'rq',mode='list') | |
1250 | if not args: # List existing macros |
|
1250 | if not args: # List existing macros | |
1251 | return sorted(k for k,v in iteritems(self.shell.user_ns) if\ |
|
1251 | return sorted(k for k,v in iteritems(self.shell.user_ns) if\ | |
1252 | isinstance(v, Macro)) |
|
1252 | isinstance(v, Macro)) | |
1253 | if len(args) == 1: |
|
1253 | if len(args) == 1: | |
1254 | raise UsageError( |
|
1254 | raise UsageError( | |
1255 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") |
|
1255 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") | |
1256 | name, codefrom = args[0], " ".join(args[1:]) |
|
1256 | name, codefrom = args[0], " ".join(args[1:]) | |
1257 |
|
1257 | |||
1258 | #print 'rng',ranges # dbg |
|
1258 | #print 'rng',ranges # dbg | |
1259 | try: |
|
1259 | try: | |
1260 | lines = self.shell.find_user_code(codefrom, 'r' in opts) |
|
1260 | lines = self.shell.find_user_code(codefrom, 'r' in opts) | |
1261 | except (ValueError, TypeError) as e: |
|
1261 | except (ValueError, TypeError) as e: | |
1262 | print(e.args[0]) |
|
1262 | print(e.args[0]) | |
1263 | return |
|
1263 | return | |
1264 | macro = Macro(lines) |
|
1264 | macro = Macro(lines) | |
1265 | self.shell.define_macro(name, macro) |
|
1265 | self.shell.define_macro(name, macro) | |
1266 | if not ( 'q' in opts) : |
|
1266 | if not ( 'q' in opts) : | |
1267 | print('Macro `%s` created. To execute, type its name (without quotes).' % name) |
|
1267 | print('Macro `%s` created. To execute, type its name (without quotes).' % name) | |
1268 | print('=== Macro contents: ===') |
|
1268 | print('=== Macro contents: ===') | |
1269 | print(macro, end=' ') |
|
1269 | print(macro, end=' ') | |
1270 |
|
1270 | |||
1271 | @magic_arguments.magic_arguments() |
|
1271 | @magic_arguments.magic_arguments() | |
1272 | @magic_arguments.argument('output', type=str, default='', nargs='?', |
|
1272 | @magic_arguments.argument('output', type=str, default='', nargs='?', | |
1273 | help="""The name of the variable in which to store output. |
|
1273 | help="""The name of the variable in which to store output. | |
1274 | This is a utils.io.CapturedIO object with stdout/err attributes |
|
1274 | This is a utils.io.CapturedIO object with stdout/err attributes | |
1275 | for the text of the captured output. |
|
1275 | for the text of the captured output. | |
1276 |
|
1276 | |||
1277 | CapturedOutput also has a show() method for displaying the output, |
|
1277 | CapturedOutput also has a show() method for displaying the output, | |
1278 | and __call__ as well, so you can use that to quickly display the |
|
1278 | and __call__ as well, so you can use that to quickly display the | |
1279 | output. |
|
1279 | output. | |
1280 |
|
1280 | |||
1281 | If unspecified, captured output is discarded. |
|
1281 | If unspecified, captured output is discarded. | |
1282 | """ |
|
1282 | """ | |
1283 | ) |
|
1283 | ) | |
1284 | @magic_arguments.argument('--no-stderr', action="store_true", |
|
1284 | @magic_arguments.argument('--no-stderr', action="store_true", | |
1285 | help="""Don't capture stderr.""" |
|
1285 | help="""Don't capture stderr.""" | |
1286 | ) |
|
1286 | ) | |
1287 | @magic_arguments.argument('--no-stdout', action="store_true", |
|
1287 | @magic_arguments.argument('--no-stdout', action="store_true", | |
1288 | help="""Don't capture stdout.""" |
|
1288 | help="""Don't capture stdout.""" | |
1289 | ) |
|
1289 | ) | |
1290 | @magic_arguments.argument('--no-display', action="store_true", |
|
1290 | @magic_arguments.argument('--no-display', action="store_true", | |
1291 | help="""Don't capture IPython's rich display.""" |
|
1291 | help="""Don't capture IPython's rich display.""" | |
1292 | ) |
|
1292 | ) | |
1293 | @cell_magic |
|
1293 | @cell_magic | |
1294 | def capture(self, line, cell): |
|
1294 | def capture(self, line, cell): | |
1295 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" |
|
1295 | """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" | |
1296 | args = magic_arguments.parse_argstring(self.capture, line) |
|
1296 | args = magic_arguments.parse_argstring(self.capture, line) | |
1297 | out = not args.no_stdout |
|
1297 | out = not args.no_stdout | |
1298 | err = not args.no_stderr |
|
1298 | err = not args.no_stderr | |
1299 | disp = not args.no_display |
|
1299 | disp = not args.no_display | |
1300 | with capture_output(out, err, disp) as io: |
|
1300 | with capture_output(out, err, disp) as io: | |
1301 | self.shell.run_cell(cell) |
|
1301 | self.shell.run_cell(cell) | |
1302 | if args.output: |
|
1302 | if args.output: | |
1303 | self.shell.user_ns[args.output] = io |
|
1303 | self.shell.user_ns[args.output] = io | |
1304 |
|
1304 | |||
1305 | def parse_breakpoint(text, current_file): |
|
1305 | def parse_breakpoint(text, current_file): | |
1306 | '''Returns (file, line) for file:line and (current_file, line) for line''' |
|
1306 | '''Returns (file, line) for file:line and (current_file, line) for line''' | |
1307 | colon = text.find(':') |
|
1307 | colon = text.find(':') | |
1308 | if colon == -1: |
|
1308 | if colon == -1: | |
1309 | return current_file, int(text) |
|
1309 | return current_file, int(text) | |
1310 | else: |
|
1310 | else: | |
1311 | return text[:colon], int(text[colon+1:]) |
|
1311 | return text[:colon], int(text[colon+1:]) | |
1312 |
|
1312 | |||
1313 | def _format_time(timespan, precision=3): |
|
1313 | def _format_time(timespan, precision=3): | |
1314 | """Formats the timespan in a human readable form""" |
|
1314 | """Formats the timespan in a human readable form""" | |
1315 | import math |
|
1315 | import math | |
1316 |
|
1316 | |||
1317 | if timespan >= 60.0: |
|
1317 | if timespan >= 60.0: | |
1318 | # we have more than a minute, format that in a human readable form |
|
1318 | # we have more than a minute, format that in a human readable form | |
1319 | # Idea from http://snipplr.com/view/5713/ |
|
1319 | # Idea from http://snipplr.com/view/5713/ | |
1320 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] |
|
1320 | parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)] | |
1321 | time = [] |
|
1321 | time = [] | |
1322 | leftover = timespan |
|
1322 | leftover = timespan | |
1323 | for suffix, length in parts: |
|
1323 | for suffix, length in parts: | |
1324 | value = int(leftover / length) |
|
1324 | value = int(leftover / length) | |
1325 | if value > 0: |
|
1325 | if value > 0: | |
1326 | leftover = leftover % length |
|
1326 | leftover = leftover % length | |
1327 | time.append(u'%s%s' % (str(value), suffix)) |
|
1327 | time.append(u'%s%s' % (str(value), suffix)) | |
1328 | if leftover < 1: |
|
1328 | if leftover < 1: | |
1329 | break |
|
1329 | break | |
1330 | return " ".join(time) |
|
1330 | return " ".join(time) | |
1331 |
|
1331 | |||
1332 |
|
1332 | |||
1333 | # Unfortunately the unicode 'micro' symbol can cause problems in |
|
1333 | # Unfortunately the unicode 'micro' symbol can cause problems in | |
1334 | # certain terminals. |
|
1334 | # certain terminals. | |
1335 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 |
|
1335 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 | |
1336 | # Try to prevent crashes by being more secure than it needs to |
|
1336 | # Try to prevent crashes by being more secure than it needs to | |
1337 | # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set. |
|
1337 | # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set. | |
1338 | units = [u"s", u"ms",u'us',"ns"] # the save value |
|
1338 | units = [u"s", u"ms",u'us',"ns"] # the save value | |
1339 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: |
|
1339 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: | |
1340 | try: |
|
1340 | try: | |
1341 | u'\xb5'.encode(sys.stdout.encoding) |
|
1341 | u'\xb5'.encode(sys.stdout.encoding) | |
1342 | units = [u"s", u"ms",u'\xb5s',"ns"] |
|
1342 | units = [u"s", u"ms",u'\xb5s',"ns"] | |
1343 | except: |
|
1343 | except: | |
1344 | pass |
|
1344 | pass | |
1345 | scaling = [1, 1e3, 1e6, 1e9] |
|
1345 | scaling = [1, 1e3, 1e6, 1e9] | |
1346 |
|
1346 | |||
1347 | if timespan > 0.0: |
|
1347 | if timespan > 0.0: | |
1348 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) |
|
1348 | order = min(-int(math.floor(math.log10(timespan)) // 3), 3) | |
1349 | else: |
|
1349 | else: | |
1350 | order = 3 |
|
1350 | order = 3 | |
1351 | return u"%.*g %s" % (precision, timespan * scaling[order], units[order]) |
|
1351 | return u"%.*g %s" % (precision, timespan * scaling[order], units[order]) |
@@ -1,235 +1,234 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """An object for managing IPython profile directories.""" |
|
2 | """An object for managing IPython profile directories.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | import os |
|
7 | import os | |
8 | import shutil |
|
8 | import shutil | |
9 | import errno |
|
9 | import errno | |
10 |
|
10 | |||
11 | from traitlets.config.configurable import LoggingConfigurable |
|
11 | from traitlets.config.configurable import LoggingConfigurable | |
12 | from IPython.paths import get_ipython_package_dir |
|
12 | from IPython.paths import get_ipython_package_dir | |
13 | from IPython.utils.path import expand_path, ensure_dir_exists |
|
13 | from IPython.utils.path import expand_path, ensure_dir_exists | |
14 | from IPython.utils import py3compat |
|
14 | from IPython.utils import py3compat | |
15 | from traitlets import Unicode, Bool |
|
15 | from traitlets import Unicode, Bool | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Module errors |
|
18 | # Module errors | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | class ProfileDirError(Exception): |
|
21 | class ProfileDirError(Exception): | |
22 | pass |
|
22 | pass | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Class for managing profile directories |
|
26 | # Class for managing profile directories | |
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 |
|
28 | |||
29 | class ProfileDir(LoggingConfigurable): |
|
29 | class ProfileDir(LoggingConfigurable): | |
30 | """An object to manage the profile directory and its resources. |
|
30 | """An object to manage the profile directory and its resources. | |
31 |
|
31 | |||
32 | The profile directory is used by all IPython applications, to manage |
|
32 | The profile directory is used by all IPython applications, to manage | |
33 | configuration, logging and security. |
|
33 | configuration, logging and security. | |
34 |
|
34 | |||
35 | This object knows how to find, create and manage these directories. This |
|
35 | This object knows how to find, create and manage these directories. This | |
36 | should be used by any code that wants to handle profiles. |
|
36 | should be used by any code that wants to handle profiles. | |
37 | """ |
|
37 | """ | |
38 |
|
38 | |||
39 | security_dir_name = Unicode('security') |
|
39 | security_dir_name = Unicode('security') | |
40 | log_dir_name = Unicode('log') |
|
40 | log_dir_name = Unicode('log') | |
41 | startup_dir_name = Unicode('startup') |
|
41 | startup_dir_name = Unicode('startup') | |
42 | pid_dir_name = Unicode('pid') |
|
42 | pid_dir_name = Unicode('pid') | |
43 | static_dir_name = Unicode('static') |
|
43 | static_dir_name = Unicode('static') | |
44 | security_dir = Unicode(u'') |
|
44 | security_dir = Unicode(u'') | |
45 | log_dir = Unicode(u'') |
|
45 | log_dir = Unicode(u'') | |
46 | startup_dir = Unicode(u'') |
|
46 | startup_dir = Unicode(u'') | |
47 | pid_dir = Unicode(u'') |
|
47 | pid_dir = Unicode(u'') | |
48 | static_dir = Unicode(u'') |
|
48 | static_dir = Unicode(u'') | |
49 |
|
49 | |||
50 | location = Unicode(u'', config=True, |
|
50 | location = Unicode(u'', config=True, | |
51 | help="""Set the profile location directly. This overrides the logic used by the |
|
51 | help="""Set the profile location directly. This overrides the logic used by the | |
52 | `profile` option.""", |
|
52 | `profile` option.""", | |
53 | ) |
|
53 | ) | |
54 |
|
54 | |||
55 | _location_isset = Bool(False) # flag for detecting multiply set location |
|
55 | _location_isset = Bool(False) # flag for detecting multiply set location | |
56 |
|
56 | |||
57 | def _location_changed(self, name, old, new): |
|
57 | def _location_changed(self, name, old, new): | |
58 | if self._location_isset: |
|
58 | if self._location_isset: | |
59 | raise RuntimeError("Cannot set profile location more than once.") |
|
59 | raise RuntimeError("Cannot set profile location more than once.") | |
60 | self._location_isset = True |
|
60 | self._location_isset = True | |
61 | ensure_dir_exists(new) |
|
61 | ensure_dir_exists(new) | |
62 |
|
62 | |||
63 | # ensure config files exist: |
|
63 | # ensure config files exist: | |
64 | self.security_dir = os.path.join(new, self.security_dir_name) |
|
64 | self.security_dir = os.path.join(new, self.security_dir_name) | |
65 | self.log_dir = os.path.join(new, self.log_dir_name) |
|
65 | self.log_dir = os.path.join(new, self.log_dir_name) | |
66 | self.startup_dir = os.path.join(new, self.startup_dir_name) |
|
66 | self.startup_dir = os.path.join(new, self.startup_dir_name) | |
67 | self.pid_dir = os.path.join(new, self.pid_dir_name) |
|
67 | self.pid_dir = os.path.join(new, self.pid_dir_name) | |
68 | self.static_dir = os.path.join(new, self.static_dir_name) |
|
68 | self.static_dir = os.path.join(new, self.static_dir_name) | |
69 | self.check_dirs() |
|
69 | self.check_dirs() | |
70 |
|
70 | |||
71 | def _log_dir_changed(self, name, old, new): |
|
71 | def _log_dir_changed(self, name, old, new): | |
72 | self.check_log_dir() |
|
72 | self.check_log_dir() | |
73 |
|
73 | |||
74 | def _mkdir(self, path, mode=None): |
|
74 | def _mkdir(self, path, mode=None): | |
75 | """ensure a directory exists at a given path |
|
75 | """ensure a directory exists at a given path | |
76 |
|
76 | |||
77 | This is a version of os.mkdir, with the following differences: |
|
77 | This is a version of os.mkdir, with the following differences: | |
78 |
|
78 | |||
79 | - returns True if it created the directory, False otherwise |
|
79 | - returns True if it created the directory, False otherwise | |
80 | - ignores EEXIST, protecting against race conditions where |
|
80 | - ignores EEXIST, protecting against race conditions where | |
81 | the dir may have been created in between the check and |
|
81 | the dir may have been created in between the check and | |
82 | the creation |
|
82 | the creation | |
83 | - sets permissions if requested and the dir already exists |
|
83 | - sets permissions if requested and the dir already exists | |
84 | """ |
|
84 | """ | |
85 | if os.path.exists(path): |
|
85 | if os.path.exists(path): | |
86 | if mode and os.stat(path).st_mode != mode: |
|
86 | if mode and os.stat(path).st_mode != mode: | |
87 | try: |
|
87 | try: | |
88 | os.chmod(path, mode) |
|
88 | os.chmod(path, mode) | |
89 | except OSError: |
|
89 | except OSError: | |
90 | self.log.warn( |
|
90 | self.log.warn( | |
91 | "Could not set permissions on %s", |
|
91 | "Could not set permissions on %s", | |
92 | path |
|
92 | path | |
93 | ) |
|
93 | ) | |
94 | return False |
|
94 | return False | |
95 | try: |
|
95 | try: | |
96 | if mode: |
|
96 | if mode: | |
97 | os.mkdir(path, mode) |
|
97 | os.mkdir(path, mode) | |
98 | else: |
|
98 | else: | |
99 | os.mkdir(path) |
|
99 | os.mkdir(path) | |
100 | except OSError as e: |
|
100 | except OSError as e: | |
101 | if e.errno == errno.EEXIST: |
|
101 | if e.errno == errno.EEXIST: | |
102 | return False |
|
102 | return False | |
103 | else: |
|
103 | else: | |
104 | raise |
|
104 | raise | |
105 |
|
105 | |||
106 | return True |
|
106 | return True | |
107 |
|
107 | |||
108 | def check_log_dir(self): |
|
108 | def check_log_dir(self): | |
109 | self._mkdir(self.log_dir) |
|
109 | self._mkdir(self.log_dir) | |
110 |
|
110 | |||
111 | def _startup_dir_changed(self, name, old, new): |
|
111 | def _startup_dir_changed(self, name, old, new): | |
112 | self.check_startup_dir() |
|
112 | self.check_startup_dir() | |
113 |
|
113 | |||
114 | def check_startup_dir(self): |
|
114 | def check_startup_dir(self): | |
115 | self._mkdir(self.startup_dir) |
|
115 | self._mkdir(self.startup_dir) | |
116 |
|
116 | |||
117 | readme = os.path.join(self.startup_dir, 'README') |
|
117 | readme = os.path.join(self.startup_dir, 'README') | |
118 | src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') |
|
118 | src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') | |
119 |
|
119 | |||
120 | if not os.path.exists(src): |
|
120 | if not os.path.exists(src): | |
121 | self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) |
|
121 | self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) | |
122 |
|
122 | |||
123 | if os.path.exists(src) and not os.path.exists(readme): |
|
123 | if os.path.exists(src) and not os.path.exists(readme): | |
124 | shutil.copy(src, readme) |
|
124 | shutil.copy(src, readme) | |
125 |
|
125 | |||
126 | def _security_dir_changed(self, name, old, new): |
|
126 | def _security_dir_changed(self, name, old, new): | |
127 | self.check_security_dir() |
|
127 | self.check_security_dir() | |
128 |
|
128 | |||
129 | def check_security_dir(self): |
|
129 | def check_security_dir(self): | |
130 | self._mkdir(self.security_dir, 0o40700) |
|
130 | self._mkdir(self.security_dir, 0o40700) | |
131 |
|
131 | |||
132 | def _pid_dir_changed(self, name, old, new): |
|
132 | def _pid_dir_changed(self, name, old, new): | |
133 | self.check_pid_dir() |
|
133 | self.check_pid_dir() | |
134 |
|
134 | |||
135 | def check_pid_dir(self): |
|
135 | def check_pid_dir(self): | |
136 | self._mkdir(self.pid_dir, 0o40700) |
|
136 | self._mkdir(self.pid_dir, 0o40700) | |
137 |
|
137 | |||
138 | def _static_dir_changed(self, name, old, new): |
|
138 | def _static_dir_changed(self, name, old, new): | |
139 | self.check_startup_dir() |
|
139 | self.check_startup_dir() | |
140 |
|
140 | |||
141 | def check_dirs(self): |
|
141 | def check_dirs(self): | |
142 | self.check_security_dir() |
|
142 | self.check_security_dir() | |
143 | self.check_log_dir() |
|
143 | self.check_log_dir() | |
144 | self.check_pid_dir() |
|
144 | self.check_pid_dir() | |
145 | self.check_startup_dir() |
|
145 | self.check_startup_dir() | |
146 |
|
146 | |||
147 | def copy_config_file(self, config_file, path=None, overwrite=False): |
|
147 | def copy_config_file(self, config_file, path=None, overwrite=False): | |
148 | """Copy a default config file into the active profile directory. |
|
148 | """Copy a default config file into the active profile directory. | |
149 |
|
149 | |||
150 | Default configuration files are kept in :mod:`IPython.core.profile`. |
|
150 | Default configuration files are kept in :mod:`IPython.core.profile`. | |
151 | This function moves these from that location to the working profile |
|
151 | This function moves these from that location to the working profile | |
152 | directory. |
|
152 | directory. | |
153 | """ |
|
153 | """ | |
154 | dst = os.path.join(self.location, config_file) |
|
154 | dst = os.path.join(self.location, config_file) | |
155 | if os.path.isfile(dst) and not overwrite: |
|
155 | if os.path.isfile(dst) and not overwrite: | |
156 | return False |
|
156 | return False | |
157 | if path is None: |
|
157 | if path is None: | |
158 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') |
|
158 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') | |
159 | src = os.path.join(path, config_file) |
|
159 | src = os.path.join(path, config_file) | |
160 | shutil.copy(src, dst) |
|
160 | shutil.copy(src, dst) | |
161 | return True |
|
161 | return True | |
162 |
|
162 | |||
163 | @classmethod |
|
163 | @classmethod | |
164 | def create_profile_dir(cls, profile_dir, config=None): |
|
164 | def create_profile_dir(cls, profile_dir, config=None): | |
165 | """Create a new profile directory given a full path. |
|
165 | """Create a new profile directory given a full path. | |
166 |
|
166 | |||
167 | Parameters |
|
167 | Parameters | |
168 | ---------- |
|
168 | ---------- | |
169 | profile_dir : str |
|
169 | profile_dir : str | |
170 | The full path to the profile directory. If it does exist, it will |
|
170 | The full path to the profile directory. If it does exist, it will | |
171 | be used. If not, it will be created. |
|
171 | be used. If not, it will be created. | |
172 | """ |
|
172 | """ | |
173 | return cls(location=profile_dir, config=config) |
|
173 | return cls(location=profile_dir, config=config) | |
174 |
|
174 | |||
175 | @classmethod |
|
175 | @classmethod | |
176 | def create_profile_dir_by_name(cls, path, name=u'default', config=None): |
|
176 | def create_profile_dir_by_name(cls, path, name=u'default', config=None): | |
177 | """Create a profile dir by profile name and path. |
|
177 | """Create a profile dir by profile name and path. | |
178 |
|
178 | |||
179 | Parameters |
|
179 | Parameters | |
180 | ---------- |
|
180 | ---------- | |
181 | path : unicode |
|
181 | path : unicode | |
182 | The path (directory) to put the profile directory in. |
|
182 | The path (directory) to put the profile directory in. | |
183 | name : unicode |
|
183 | name : unicode | |
184 | The name of the profile. The name of the profile directory will |
|
184 | The name of the profile. The name of the profile directory will | |
185 | be "profile_<profile>". |
|
185 | be "profile_<profile>". | |
186 | """ |
|
186 | """ | |
187 | if not os.path.isdir(path): |
|
187 | if not os.path.isdir(path): | |
188 | raise ProfileDirError('Directory not found: %s' % path) |
|
188 | raise ProfileDirError('Directory not found: %s' % path) | |
189 | profile_dir = os.path.join(path, u'profile_' + name) |
|
189 | profile_dir = os.path.join(path, u'profile_' + name) | |
190 | return cls(location=profile_dir, config=config) |
|
190 | return cls(location=profile_dir, config=config) | |
191 |
|
191 | |||
192 | @classmethod |
|
192 | @classmethod | |
193 | def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): |
|
193 | def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): | |
194 | """Find an existing profile dir by profile name, return its ProfileDir. |
|
194 | """Find an existing profile dir by profile name, return its ProfileDir. | |
195 |
|
195 | |||
196 | This searches through a sequence of paths for a profile dir. If it |
|
196 | This searches through a sequence of paths for a profile dir. If it | |
197 | is not found, a :class:`ProfileDirError` exception will be raised. |
|
197 | is not found, a :class:`ProfileDirError` exception will be raised. | |
198 |
|
198 | |||
199 | The search path algorithm is: |
|
199 | The search path algorithm is: | |
200 | 1. ``py3compat.getcwd()`` |
|
200 | 1. ``py3compat.getcwd()`` | |
201 | 2. ``ipython_dir`` |
|
201 | 2. ``ipython_dir`` | |
202 |
|
202 | |||
203 | Parameters |
|
203 | Parameters | |
204 | ---------- |
|
204 | ---------- | |
205 | ipython_dir : unicode or str |
|
205 | ipython_dir : unicode or str | |
206 | The IPython directory to use. |
|
206 | The IPython directory to use. | |
207 | name : unicode or str |
|
207 | name : unicode or str | |
208 | The name of the profile. The name of the profile directory |
|
208 | The name of the profile. The name of the profile directory | |
209 | will be "profile_<profile>". |
|
209 | will be "profile_<profile>". | |
210 | """ |
|
210 | """ | |
211 | dirname = u'profile_' + name |
|
211 | dirname = u'profile_' + name | |
212 | paths = [py3compat.getcwd(), ipython_dir] |
|
212 | paths = [py3compat.getcwd(), ipython_dir] | |
213 | for p in paths: |
|
213 | for p in paths: | |
214 | profile_dir = os.path.join(p, dirname) |
|
214 | profile_dir = os.path.join(p, dirname) | |
215 | if os.path.isdir(profile_dir): |
|
215 | if os.path.isdir(profile_dir): | |
216 | return cls(location=profile_dir, config=config) |
|
216 | return cls(location=profile_dir, config=config) | |
217 | else: |
|
217 | else: | |
218 | raise ProfileDirError('Profile directory not found in paths: %s' % dirname) |
|
218 | raise ProfileDirError('Profile directory not found in paths: %s' % dirname) | |
219 |
|
219 | |||
220 | @classmethod |
|
220 | @classmethod | |
221 | def find_profile_dir(cls, profile_dir, config=None): |
|
221 | def find_profile_dir(cls, profile_dir, config=None): | |
222 | """Find/create a profile dir and return its ProfileDir. |
|
222 | """Find/create a profile dir and return its ProfileDir. | |
223 |
|
223 | |||
224 | This will create the profile directory if it doesn't exist. |
|
224 | This will create the profile directory if it doesn't exist. | |
225 |
|
225 | |||
226 | Parameters |
|
226 | Parameters | |
227 | ---------- |
|
227 | ---------- | |
228 | profile_dir : unicode or str |
|
228 | profile_dir : unicode or str | |
229 |
The path of the profile directory. |
|
229 | The path of the profile directory. | |
230 | :func:`IPython.utils.genutils.expand_path`. |
|
|||
231 | """ |
|
230 | """ | |
232 | profile_dir = expand_path(profile_dir) |
|
231 | profile_dir = expand_path(profile_dir) | |
233 | if not os.path.isdir(profile_dir): |
|
232 | if not os.path.isdir(profile_dir): | |
234 | raise ProfileDirError('Profile directory not found: %s' % profile_dir) |
|
233 | raise ProfileDirError('Profile directory not found: %s' % profile_dir) | |
235 | return cls(location=profile_dir, config=config) |
|
234 | return cls(location=profile_dir, config=config) |
@@ -1,247 +1,225 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | IO related utilities. |
|
3 | IO related utilities. | |
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 | from __future__ import print_function |
|
9 | from __future__ import print_function | |
10 | from __future__ import absolute_import |
|
10 | from __future__ import absolute_import | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | import codecs |
|
|||
14 | from contextlib import contextmanager |
|
|||
15 | import io |
|
|||
16 | import os |
|
13 | import os | |
17 | import shutil |
|
|||
18 | import sys |
|
14 | import sys | |
19 | import tempfile |
|
15 | import tempfile | |
20 | import warnings |
|
|||
21 | from warnings import warn |
|
16 | from warnings import warn | |
22 | from .capture import CapturedIO, capture_output |
|
17 | from .capture import CapturedIO, capture_output | |
23 | from .py3compat import string_types, input, PY3 |
|
18 | from .py3compat import string_types, input, PY3 | |
24 |
|
19 | |||
25 |
|
20 | |||
26 | class IOStream: |
|
21 | class IOStream: | |
27 |
|
22 | |||
28 | def __init__(self,stream, fallback=None): |
|
23 | def __init__(self,stream, fallback=None): | |
29 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
24 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): | |
30 | if fallback is not None: |
|
25 | if fallback is not None: | |
31 | stream = fallback |
|
26 | stream = fallback | |
32 | else: |
|
27 | else: | |
33 | raise ValueError("fallback required, but not specified") |
|
28 | raise ValueError("fallback required, but not specified") | |
34 | self.stream = stream |
|
29 | self.stream = stream | |
35 | self._swrite = stream.write |
|
30 | self._swrite = stream.write | |
36 |
|
31 | |||
37 | # clone all methods not overridden: |
|
32 | # clone all methods not overridden: | |
38 | def clone(meth): |
|
33 | def clone(meth): | |
39 | return not hasattr(self, meth) and not meth.startswith('_') |
|
34 | return not hasattr(self, meth) and not meth.startswith('_') | |
40 | for meth in filter(clone, dir(stream)): |
|
35 | for meth in filter(clone, dir(stream)): | |
41 | setattr(self, meth, getattr(stream, meth)) |
|
36 | setattr(self, meth, getattr(stream, meth)) | |
42 |
|
37 | |||
43 | def __repr__(self): |
|
38 | def __repr__(self): | |
44 | cls = self.__class__ |
|
39 | cls = self.__class__ | |
45 | tpl = '{mod}.{cls}({args})' |
|
40 | tpl = '{mod}.{cls}({args})' | |
46 | return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream) |
|
41 | return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream) | |
47 |
|
42 | |||
48 | def write(self,data): |
|
43 | def write(self,data): | |
49 | try: |
|
44 | try: | |
50 | self._swrite(data) |
|
45 | self._swrite(data) | |
51 | except: |
|
46 | except: | |
52 | try: |
|
47 | try: | |
53 | # print handles some unicode issues which may trip a plain |
|
48 | # print handles some unicode issues which may trip a plain | |
54 | # write() call. Emulate write() by using an empty end |
|
49 | # write() call. Emulate write() by using an empty end | |
55 | # argument. |
|
50 | # argument. | |
56 | print(data, end='', file=self.stream) |
|
51 | print(data, end='', file=self.stream) | |
57 | except: |
|
52 | except: | |
58 | # if we get here, something is seriously broken. |
|
53 | # if we get here, something is seriously broken. | |
59 | print('ERROR - failed to write data to stream:', self.stream, |
|
54 | print('ERROR - failed to write data to stream:', self.stream, | |
60 | file=sys.stderr) |
|
55 | file=sys.stderr) | |
61 |
|
56 | |||
62 | def writelines(self, lines): |
|
57 | def writelines(self, lines): | |
63 | if isinstance(lines, string_types): |
|
58 | if isinstance(lines, string_types): | |
64 | lines = [lines] |
|
59 | lines = [lines] | |
65 | for line in lines: |
|
60 | for line in lines: | |
66 | self.write(line) |
|
61 | self.write(line) | |
67 |
|
62 | |||
68 | # This class used to have a writeln method, but regular files and streams |
|
63 | # This class used to have a writeln method, but regular files and streams | |
69 | # in Python don't have this method. We need to keep this completely |
|
64 | # in Python don't have this method. We need to keep this completely | |
70 | # compatible so we removed it. |
|
65 | # compatible so we removed it. | |
71 |
|
66 | |||
72 | @property |
|
67 | @property | |
73 | def closed(self): |
|
68 | def closed(self): | |
74 | return self.stream.closed |
|
69 | return self.stream.closed | |
75 |
|
70 | |||
76 | def close(self): |
|
71 | def close(self): | |
77 | pass |
|
72 | pass | |
78 |
|
73 | |||
79 | # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr |
|
74 | # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr | |
80 | devnull = open(os.devnull, 'w') |
|
75 | devnull = open(os.devnull, 'w') | |
81 | stdin = IOStream(sys.stdin, fallback=devnull) |
|
76 | stdin = IOStream(sys.stdin, fallback=devnull) | |
82 | stdout = IOStream(sys.stdout, fallback=devnull) |
|
77 | stdout = IOStream(sys.stdout, fallback=devnull) | |
83 | stderr = IOStream(sys.stderr, fallback=devnull) |
|
78 | stderr = IOStream(sys.stderr, fallback=devnull) | |
84 |
|
79 | |||
85 | class IOTerm: |
|
|||
86 | """ Term holds the file or file-like objects for handling I/O operations. |
|
|||
87 |
|
||||
88 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
|||
89 | Windows they can can replaced to allow editing the strings before they are |
|
|||
90 | displayed.""" |
|
|||
91 |
|
||||
92 | # In the future, having IPython channel all its I/O operations through |
|
|||
93 | # this class will make it easier to embed it into other environments which |
|
|||
94 | # are not a normal terminal (such as a GUI-based shell) |
|
|||
95 | def __init__(self, stdin=None, stdout=None, stderr=None): |
|
|||
96 | mymodule = sys.modules[__name__] |
|
|||
97 | self.stdin = IOStream(stdin, mymodule.stdin) |
|
|||
98 | self.stdout = IOStream(stdout, mymodule.stdout) |
|
|||
99 | self.stderr = IOStream(stderr, mymodule.stderr) |
|
|||
100 |
|
||||
101 |
|
||||
102 | class Tee(object): |
|
80 | class Tee(object): | |
103 | """A class to duplicate an output stream to stdout/err. |
|
81 | """A class to duplicate an output stream to stdout/err. | |
104 |
|
82 | |||
105 | This works in a manner very similar to the Unix 'tee' command. |
|
83 | This works in a manner very similar to the Unix 'tee' command. | |
106 |
|
84 | |||
107 | When the object is closed or deleted, it closes the original file given to |
|
85 | When the object is closed or deleted, it closes the original file given to | |
108 | it for duplication. |
|
86 | it for duplication. | |
109 | """ |
|
87 | """ | |
110 | # Inspired by: |
|
88 | # Inspired by: | |
111 | # http://mail.python.org/pipermail/python-list/2007-May/442737.html |
|
89 | # http://mail.python.org/pipermail/python-list/2007-May/442737.html | |
112 |
|
90 | |||
113 | def __init__(self, file_or_name, mode="w", channel='stdout'): |
|
91 | def __init__(self, file_or_name, mode="w", channel='stdout'): | |
114 | """Construct a new Tee object. |
|
92 | """Construct a new Tee object. | |
115 |
|
93 | |||
116 | Parameters |
|
94 | Parameters | |
117 | ---------- |
|
95 | ---------- | |
118 | file_or_name : filename or open filehandle (writable) |
|
96 | file_or_name : filename or open filehandle (writable) | |
119 | File that will be duplicated |
|
97 | File that will be duplicated | |
120 |
|
98 | |||
121 | mode : optional, valid mode for open(). |
|
99 | mode : optional, valid mode for open(). | |
122 | If a filename was give, open with this mode. |
|
100 | If a filename was give, open with this mode. | |
123 |
|
101 | |||
124 | channel : str, one of ['stdout', 'stderr'] |
|
102 | channel : str, one of ['stdout', 'stderr'] | |
125 | """ |
|
103 | """ | |
126 | if channel not in ['stdout', 'stderr']: |
|
104 | if channel not in ['stdout', 'stderr']: | |
127 | raise ValueError('Invalid channel spec %s' % channel) |
|
105 | raise ValueError('Invalid channel spec %s' % channel) | |
128 |
|
106 | |||
129 | if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'): |
|
107 | if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'): | |
130 | self.file = file_or_name |
|
108 | self.file = file_or_name | |
131 | else: |
|
109 | else: | |
132 | self.file = open(file_or_name, mode) |
|
110 | self.file = open(file_or_name, mode) | |
133 | self.channel = channel |
|
111 | self.channel = channel | |
134 | self.ostream = getattr(sys, channel) |
|
112 | self.ostream = getattr(sys, channel) | |
135 | setattr(sys, channel, self) |
|
113 | setattr(sys, channel, self) | |
136 | self._closed = False |
|
114 | self._closed = False | |
137 |
|
115 | |||
138 | def close(self): |
|
116 | def close(self): | |
139 | """Close the file and restore the channel.""" |
|
117 | """Close the file and restore the channel.""" | |
140 | self.flush() |
|
118 | self.flush() | |
141 | setattr(sys, self.channel, self.ostream) |
|
119 | setattr(sys, self.channel, self.ostream) | |
142 | self.file.close() |
|
120 | self.file.close() | |
143 | self._closed = True |
|
121 | self._closed = True | |
144 |
|
122 | |||
145 | def write(self, data): |
|
123 | def write(self, data): | |
146 | """Write data to both channels.""" |
|
124 | """Write data to both channels.""" | |
147 | self.file.write(data) |
|
125 | self.file.write(data) | |
148 | self.ostream.write(data) |
|
126 | self.ostream.write(data) | |
149 | self.ostream.flush() |
|
127 | self.ostream.flush() | |
150 |
|
128 | |||
151 | def flush(self): |
|
129 | def flush(self): | |
152 | """Flush both channels.""" |
|
130 | """Flush both channels.""" | |
153 | self.file.flush() |
|
131 | self.file.flush() | |
154 | self.ostream.flush() |
|
132 | self.ostream.flush() | |
155 |
|
133 | |||
156 | def __del__(self): |
|
134 | def __del__(self): | |
157 | if not self._closed: |
|
135 | if not self._closed: | |
158 | self.close() |
|
136 | self.close() | |
159 |
|
137 | |||
160 |
|
138 | |||
161 | def ask_yes_no(prompt, default=None, interrupt=None): |
|
139 | def ask_yes_no(prompt, default=None, interrupt=None): | |
162 | """Asks a question and returns a boolean (y/n) answer. |
|
140 | """Asks a question and returns a boolean (y/n) answer. | |
163 |
|
141 | |||
164 | If default is given (one of 'y','n'), it is used if the user input is |
|
142 | If default is given (one of 'y','n'), it is used if the user input is | |
165 | empty. If interrupt is given (one of 'y','n'), it is used if the user |
|
143 | empty. If interrupt is given (one of 'y','n'), it is used if the user | |
166 | presses Ctrl-C. Otherwise the question is repeated until an answer is |
|
144 | presses Ctrl-C. Otherwise the question is repeated until an answer is | |
167 | given. |
|
145 | given. | |
168 |
|
146 | |||
169 | An EOF is treated as the default answer. If there is no default, an |
|
147 | An EOF is treated as the default answer. If there is no default, an | |
170 | exception is raised to prevent infinite loops. |
|
148 | exception is raised to prevent infinite loops. | |
171 |
|
149 | |||
172 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
150 | Valid answers are: y/yes/n/no (match is not case sensitive).""" | |
173 |
|
151 | |||
174 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
152 | answers = {'y':True,'n':False,'yes':True,'no':False} | |
175 | ans = None |
|
153 | ans = None | |
176 | while ans not in answers.keys(): |
|
154 | while ans not in answers.keys(): | |
177 | try: |
|
155 | try: | |
178 | ans = input(prompt+' ').lower() |
|
156 | ans = input(prompt+' ').lower() | |
179 | if not ans: # response was an empty string |
|
157 | if not ans: # response was an empty string | |
180 | ans = default |
|
158 | ans = default | |
181 | except KeyboardInterrupt: |
|
159 | except KeyboardInterrupt: | |
182 | if interrupt: |
|
160 | if interrupt: | |
183 | ans = interrupt |
|
161 | ans = interrupt | |
184 | except EOFError: |
|
162 | except EOFError: | |
185 | if default in answers.keys(): |
|
163 | if default in answers.keys(): | |
186 | ans = default |
|
164 | ans = default | |
187 | print() |
|
165 | print() | |
188 | else: |
|
166 | else: | |
189 | raise |
|
167 | raise | |
190 |
|
168 | |||
191 | return answers[ans] |
|
169 | return answers[ans] | |
192 |
|
170 | |||
193 |
|
171 | |||
194 | def temp_pyfile(src, ext='.py'): |
|
172 | def temp_pyfile(src, ext='.py'): | |
195 | """Make a temporary python file, return filename and filehandle. |
|
173 | """Make a temporary python file, return filename and filehandle. | |
196 |
|
174 | |||
197 | Parameters |
|
175 | Parameters | |
198 | ---------- |
|
176 | ---------- | |
199 | src : string or list of strings (no need for ending newlines if list) |
|
177 | src : string or list of strings (no need for ending newlines if list) | |
200 | Source code to be written to the file. |
|
178 | Source code to be written to the file. | |
201 |
|
179 | |||
202 | ext : optional, string |
|
180 | ext : optional, string | |
203 | Extension for the generated file. |
|
181 | Extension for the generated file. | |
204 |
|
182 | |||
205 | Returns |
|
183 | Returns | |
206 | ------- |
|
184 | ------- | |
207 | (filename, open filehandle) |
|
185 | (filename, open filehandle) | |
208 | It is the caller's responsibility to close the open file and unlink it. |
|
186 | It is the caller's responsibility to close the open file and unlink it. | |
209 | """ |
|
187 | """ | |
210 | fname = tempfile.mkstemp(ext)[1] |
|
188 | fname = tempfile.mkstemp(ext)[1] | |
211 | f = open(fname,'w') |
|
189 | f = open(fname,'w') | |
212 | f.write(src) |
|
190 | f.write(src) | |
213 | f.flush() |
|
191 | f.flush() | |
214 | return fname, f |
|
192 | return fname, f | |
215 |
|
193 | |||
216 | def atomic_writing(*args, **kwargs): |
|
194 | def atomic_writing(*args, **kwargs): | |
217 | """DEPRECATED: moved to notebook.services.contents.fileio""" |
|
195 | """DEPRECATED: moved to notebook.services.contents.fileio""" | |
218 | warn("IPython.utils.io.atomic_writing has moved to notebook.services.contents.fileio") |
|
196 | warn("IPython.utils.io.atomic_writing has moved to notebook.services.contents.fileio") | |
219 | from notebook.services.contents.fileio import atomic_writing |
|
197 | from notebook.services.contents.fileio import atomic_writing | |
220 | return atomic_writing(*args, **kwargs) |
|
198 | return atomic_writing(*args, **kwargs) | |
221 |
|
199 | |||
222 | def raw_print(*args, **kw): |
|
200 | def raw_print(*args, **kw): | |
223 | """Raw print to sys.__stdout__, otherwise identical interface to print().""" |
|
201 | """Raw print to sys.__stdout__, otherwise identical interface to print().""" | |
224 |
|
202 | |||
225 | print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'), |
|
203 | print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'), | |
226 | file=sys.__stdout__) |
|
204 | file=sys.__stdout__) | |
227 | sys.__stdout__.flush() |
|
205 | sys.__stdout__.flush() | |
228 |
|
206 | |||
229 |
|
207 | |||
230 | def raw_print_err(*args, **kw): |
|
208 | def raw_print_err(*args, **kw): | |
231 | """Raw print to sys.__stderr__, otherwise identical interface to print().""" |
|
209 | """Raw print to sys.__stderr__, otherwise identical interface to print().""" | |
232 |
|
210 | |||
233 | print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'), |
|
211 | print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'), | |
234 | file=sys.__stderr__) |
|
212 | file=sys.__stderr__) | |
235 | sys.__stderr__.flush() |
|
213 | sys.__stderr__.flush() | |
236 |
|
214 | |||
237 |
|
215 | |||
238 | # Short aliases for quick debugging, do NOT use these in production code. |
|
216 | # Short aliases for quick debugging, do NOT use these in production code. | |
239 | rprint = raw_print |
|
217 | rprint = raw_print | |
240 | rprinte = raw_print_err |
|
218 | rprinte = raw_print_err | |
241 |
|
219 | |||
242 |
|
220 | |||
243 | def unicode_std_stream(stream='stdout'): |
|
221 | def unicode_std_stream(stream='stdout'): | |
244 | """DEPRECATED, moved to nbconvert.utils.io""" |
|
222 | """DEPRECATED, moved to nbconvert.utils.io""" | |
245 | warn("IPython.utils.io.unicode_std_stream has moved to nbconvert.utils.io") |
|
223 | warn("IPython.utils.io.unicode_std_stream has moved to nbconvert.utils.io") | |
246 | from nbconvert.utils.io import unicode_std_stream |
|
224 | from nbconvert.utils.io import unicode_std_stream | |
247 | return unicode_std_stream(stream) |
|
225 | return unicode_std_stream(stream) |
General Comments 0
You need to be logged in to leave comments.
Login now