Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,76 +1,76 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Release data for the IPython project. |
|
3 | 3 | |
|
4 |
$Id: Release.py 98 |
|
|
4 | $Id: Release.py 982 2005-12-30 23:57:07Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray |
|
10 | 10 | # <n8gray@caltech.edu> |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | # Name of the package for release purposes. This is the name which labels |
|
17 | 17 | # the tarballs and RPMs made by distutils, so it's best to lowercase it. |
|
18 | 18 | name = 'ipython' |
|
19 | 19 | |
|
20 | 20 | # For versions with substrings (like 0.6.16.svn), use an extra . to separate |
|
21 | 21 | # the new substring. We have to avoid using either dashes or underscores, |
|
22 | 22 | # because bdist_rpm does not accept dashes (an RPM) convention, and |
|
23 | 23 | # bdist_deb does not accept underscores (a Debian convention). |
|
24 | 24 | |
|
25 |
version = '0.7.0.rc |
|
|
25 | version = '0.7.0.rc3' | |
|
26 | 26 | |
|
27 |
revision = '$Revision: 98 |
|
|
27 | revision = '$Revision: 982 $' | |
|
28 | 28 | |
|
29 | 29 | description = "An enhanced interactive Python shell." |
|
30 | 30 | |
|
31 | 31 | long_description = \ |
|
32 | 32 | """ |
|
33 | 33 | IPython provides a replacement for the interactive Python interpreter with |
|
34 | 34 | extra functionality. |
|
35 | 35 | |
|
36 | 36 | Main features: |
|
37 | 37 | |
|
38 | 38 | * Comprehensive object introspection. |
|
39 | 39 | |
|
40 | 40 | * Input history, persistent across sessions. |
|
41 | 41 | |
|
42 | 42 | * Caching of output results during a session with automatically generated |
|
43 | 43 | references. |
|
44 | 44 | |
|
45 | 45 | * Readline based name completion. |
|
46 | 46 | |
|
47 | 47 | * Extensible system of 'magic' commands for controlling the environment and |
|
48 | 48 | performing many tasks related either to IPython or the operating system. |
|
49 | 49 | |
|
50 | 50 | * Configuration system with easy switching between different setups (simpler |
|
51 | 51 | than changing $PYTHONSTARTUP environment variables every time). |
|
52 | 52 | |
|
53 | 53 | * Session logging and reloading. |
|
54 | 54 | |
|
55 | 55 | * Extensible syntax processing for special purpose situations. |
|
56 | 56 | |
|
57 | 57 | * Access to the system shell with user-extensible alias system. |
|
58 | 58 | |
|
59 | 59 | * Easily embeddable in other Python programs. |
|
60 | 60 | |
|
61 | 61 | * Integrated access to the pdb debugger and the Python profiler. """ |
|
62 | 62 | |
|
63 | 63 | license = 'BSD' |
|
64 | 64 | |
|
65 | 65 | authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'), |
|
66 | 66 | 'Janko' : ('Janko Hauser','jhauser@zscout.de'), |
|
67 | 67 | 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu') |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | url = 'http://ipython.scipy.org' |
|
71 | 71 | |
|
72 | 72 | download_url = 'http://ipython.scipy.org/dist' |
|
73 | 73 | |
|
74 | 74 | platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME'] |
|
75 | 75 | |
|
76 | 76 | keywords = ['Interactive','Interpreter','Shell'] |
@@ -1,2056 +1,2060 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 9 |
|
|
9 | $Id: iplib.py 982 2005-12-30 23:57:07Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2005 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | 31 | from __future__ import generators # for 2.2 backwards-compatibility |
|
32 | 32 | |
|
33 | 33 | from IPython import Release |
|
34 | 34 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
35 | 35 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
36 | 36 | __license__ = Release.license |
|
37 | 37 | __version__ = Release.version |
|
38 | 38 | |
|
39 | 39 | # Python standard modules |
|
40 | 40 | import __main__ |
|
41 | 41 | import __builtin__ |
|
42 | 42 | import StringIO |
|
43 | 43 | import bdb |
|
44 | 44 | import cPickle as pickle |
|
45 | 45 | import codeop |
|
46 | 46 | import exceptions |
|
47 | 47 | import glob |
|
48 | 48 | import inspect |
|
49 | 49 | import keyword |
|
50 | 50 | import new |
|
51 | 51 | import os |
|
52 | 52 | import pdb |
|
53 | 53 | import pydoc |
|
54 | 54 | import re |
|
55 | 55 | import shutil |
|
56 | 56 | import string |
|
57 | 57 | import sys |
|
58 | 58 | import traceback |
|
59 | 59 | import types |
|
60 | 60 | |
|
61 | 61 | from pprint import pprint, pformat |
|
62 | 62 | |
|
63 | 63 | # IPython's own modules |
|
64 | 64 | import IPython |
|
65 | 65 | from IPython import OInspect,PyColorize,ultraTB |
|
66 | 66 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
67 | 67 | from IPython.FakeModule import FakeModule |
|
68 | 68 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
69 | 69 | from IPython.Logger import Logger |
|
70 | 70 | from IPython.Magic import Magic |
|
71 | 71 | from IPython.Prompts import CachedOutput |
|
72 | 72 | from IPython.Struct import Struct |
|
73 | 73 | from IPython.background_jobs import BackgroundJobManager |
|
74 | 74 | from IPython.usage import cmd_line_usage,interactive_usage |
|
75 | 75 | from IPython.genutils import * |
|
76 | 76 | |
|
77 | 77 | # store the builtin raw_input globally, and use this always, in case user code |
|
78 | 78 | # overwrites it (like wx.py.PyShell does) |
|
79 | 79 | raw_input_original = raw_input |
|
80 | 80 | |
|
81 | 81 | # compiled regexps for autoindent management |
|
82 | 82 | ini_spaces_re = re.compile(r'^(\s+)') |
|
83 | 83 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
84 | 84 | |
|
85 | 85 | #**************************************************************************** |
|
86 | 86 | # Some utility function definitions |
|
87 | 87 | |
|
88 | 88 | def softspace(file, newvalue): |
|
89 | 89 | """Copied from code.py, to remove the dependency""" |
|
90 | 90 | oldvalue = 0 |
|
91 | 91 | try: |
|
92 | 92 | oldvalue = file.softspace |
|
93 | 93 | except AttributeError: |
|
94 | 94 | pass |
|
95 | 95 | try: |
|
96 | 96 | file.softspace = newvalue |
|
97 | 97 | except (AttributeError, TypeError): |
|
98 | 98 | # "attribute-less object" or "read-only attributes" |
|
99 | 99 | pass |
|
100 | 100 | return oldvalue |
|
101 | 101 | |
|
102 | 102 | #**************************************************************************** |
|
103 | 103 | # These special functions get installed in the builtin namespace, to provide |
|
104 | 104 | # programmatic (pure python) access to magics, aliases and system calls. This |
|
105 | 105 | # is important for logging, user scripting, and more. |
|
106 | 106 | |
|
107 | 107 | # We are basically exposing, via normal python functions, the three mechanisms |
|
108 | 108 | # in which ipython offers special call modes (magics for internal control, |
|
109 | 109 | # aliases for direct system access via pre-selected names, and !cmd for |
|
110 | 110 | # calling arbitrary system commands). |
|
111 | 111 | |
|
112 | 112 | def ipmagic(arg_s): |
|
113 | 113 | """Call a magic function by name. |
|
114 | 114 | |
|
115 | 115 | Input: a string containing the name of the magic function to call and any |
|
116 | 116 | additional arguments to be passed to the magic. |
|
117 | 117 | |
|
118 | 118 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
119 | 119 | prompt: |
|
120 | 120 | |
|
121 | 121 | In[1]: %name -opt foo bar |
|
122 | 122 | |
|
123 | 123 | To call a magic without arguments, simply use ipmagic('name'). |
|
124 | 124 | |
|
125 | 125 | This provides a proper Python function to call IPython's magics in any |
|
126 | 126 | valid Python code you can type at the interpreter, including loops and |
|
127 | 127 | compound statements. It is added by IPython to the Python builtin |
|
128 | 128 | namespace upon initialization.""" |
|
129 | 129 | |
|
130 | 130 | args = arg_s.split(' ',1) |
|
131 | 131 | magic_name = args[0] |
|
132 | 132 | if magic_name.startswith(__IPYTHON__.ESC_MAGIC): |
|
133 | 133 | magic_name = magic_name[1:] |
|
134 | 134 | try: |
|
135 | 135 | magic_args = args[1] |
|
136 | 136 | except IndexError: |
|
137 | 137 | magic_args = '' |
|
138 | 138 | fn = getattr(__IPYTHON__,'magic_'+magic_name,None) |
|
139 | 139 | if fn is None: |
|
140 | 140 | error("Magic function `%s` not found." % magic_name) |
|
141 | 141 | else: |
|
142 | 142 | magic_args = __IPYTHON__.var_expand(magic_args) |
|
143 | 143 | return fn(magic_args) |
|
144 | 144 | |
|
145 | 145 | def ipalias(arg_s): |
|
146 | 146 | """Call an alias by name. |
|
147 | 147 | |
|
148 | 148 | Input: a string containing the name of the alias to call and any |
|
149 | 149 | additional arguments to be passed to the magic. |
|
150 | 150 | |
|
151 | 151 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
152 | 152 | prompt: |
|
153 | 153 | |
|
154 | 154 | In[1]: name -opt foo bar |
|
155 | 155 | |
|
156 | 156 | To call an alias without arguments, simply use ipalias('name'). |
|
157 | 157 | |
|
158 | 158 | This provides a proper Python function to call IPython's aliases in any |
|
159 | 159 | valid Python code you can type at the interpreter, including loops and |
|
160 | 160 | compound statements. It is added by IPython to the Python builtin |
|
161 | 161 | namespace upon initialization.""" |
|
162 | 162 | |
|
163 | 163 | args = arg_s.split(' ',1) |
|
164 | 164 | alias_name = args[0] |
|
165 | 165 | try: |
|
166 | 166 | alias_args = args[1] |
|
167 | 167 | except IndexError: |
|
168 | 168 | alias_args = '' |
|
169 | 169 | if alias_name in __IPYTHON__.alias_table: |
|
170 | 170 | __IPYTHON__.call_alias(alias_name,alias_args) |
|
171 | 171 | else: |
|
172 | 172 | error("Alias `%s` not found." % alias_name) |
|
173 | 173 | |
|
174 | 174 | def ipsystem(arg_s): |
|
175 | 175 | """Make a system call, using IPython.""" |
|
176 | 176 | __IPYTHON__.system(arg_s) |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | #**************************************************************************** |
|
180 | 180 | # Local use exceptions |
|
181 | 181 | class SpaceInInput(exceptions.Exception): pass |
|
182 | 182 | |
|
183 | 183 | #**************************************************************************** |
|
184 | 184 | # Local use classes |
|
185 | 185 | class Bunch: pass |
|
186 | 186 | |
|
187 | 187 | class InputList(list): |
|
188 | 188 | """Class to store user input. |
|
189 | 189 | |
|
190 | 190 | It's basically a list, but slices return a string instead of a list, thus |
|
191 | 191 | allowing things like (assuming 'In' is an instance): |
|
192 | 192 | |
|
193 | 193 | exec In[4:7] |
|
194 | 194 | |
|
195 | 195 | or |
|
196 | 196 | |
|
197 | 197 | exec In[5:9] + In[14] + In[21:25]""" |
|
198 | 198 | |
|
199 | 199 | def __getslice__(self,i,j): |
|
200 | 200 | return ''.join(list.__getslice__(self,i,j)) |
|
201 | 201 | |
|
202 | 202 | class SyntaxTB(ultraTB.ListTB): |
|
203 | 203 | """Extension which holds some state: the last exception value""" |
|
204 | 204 | |
|
205 | 205 | def __init__(self,color_scheme = 'NoColor'): |
|
206 | 206 | ultraTB.ListTB.__init__(self,color_scheme) |
|
207 | 207 | self.last_syntax_error = None |
|
208 | 208 | |
|
209 | 209 | def __call__(self, etype, value, elist): |
|
210 | 210 | self.last_syntax_error = value |
|
211 | 211 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
212 | 212 | |
|
213 | 213 | def clear_err_state(self): |
|
214 | 214 | """Return the current error state and clear it""" |
|
215 | 215 | e = self.last_syntax_error |
|
216 | 216 | self.last_syntax_error = None |
|
217 | 217 | return e |
|
218 | 218 | |
|
219 | 219 | #**************************************************************************** |
|
220 | 220 | # Main IPython class |
|
221 | 221 | |
|
222 | 222 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
223 | 223 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
224 | 224 | # attributes and methods, but too much user code out there relies on the |
|
225 | 225 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
226 | 226 | # |
|
227 | 227 | # But at least now, all the pieces have been separated and we could, in |
|
228 | 228 | # principle, stop using the mixin. This will ease the transition to the |
|
229 | 229 | # chainsaw branch. |
|
230 | 230 | |
|
231 | 231 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
232 | 232 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
233 | 233 | # class, to prevent clashes. |
|
234 | 234 | |
|
235 | 235 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
236 | 236 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
237 | 237 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
238 | 238 | # 'self.value'] |
|
239 | 239 | |
|
240 | 240 | class InteractiveShell(Magic): |
|
241 | 241 | """An enhanced console for Python.""" |
|
242 | 242 | |
|
243 | 243 | # class attribute to indicate whether the class supports threads or not. |
|
244 | 244 | # Subclasses with thread support should override this as needed. |
|
245 | 245 | isthreaded = False |
|
246 | 246 | |
|
247 | 247 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
248 | 248 | user_ns = None,user_global_ns=None,banner2='', |
|
249 | 249 | custom_exceptions=((),None),embedded=False): |
|
250 | 250 | |
|
251 | 251 | # some minimal strict typechecks. For some core data structures, I |
|
252 | 252 | # want actual basic python types, not just anything that looks like |
|
253 | 253 | # one. This is especially true for namespaces. |
|
254 | 254 | for ns in (user_ns,user_global_ns): |
|
255 | 255 | if ns is not None and type(ns) != types.DictType: |
|
256 | 256 | raise TypeError,'namespace must be a dictionary' |
|
257 | 257 | |
|
258 | 258 | # Put a reference to self in builtins so that any form of embedded or |
|
259 | 259 | # imported code can test for being inside IPython. |
|
260 | 260 | __builtin__.__IPYTHON__ = self |
|
261 | 261 | |
|
262 | 262 | # And load into builtins ipmagic/ipalias/ipsystem as well |
|
263 | 263 | __builtin__.ipmagic = ipmagic |
|
264 | 264 | __builtin__.ipalias = ipalias |
|
265 | 265 | __builtin__.ipsystem = ipsystem |
|
266 | 266 | |
|
267 | 267 | # Add to __builtin__ other parts of IPython's public API |
|
268 | 268 | __builtin__.ip_set_hook = self.set_hook |
|
269 | 269 | |
|
270 | 270 | # Keep in the builtins a flag for when IPython is active. We set it |
|
271 | 271 | # with setdefault so that multiple nested IPythons don't clobber one |
|
272 | 272 | # another. Each will increase its value by one upon being activated, |
|
273 | 273 | # which also gives us a way to determine the nesting level. |
|
274 | 274 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
275 | 275 | |
|
276 | 276 | # Do the intuitively correct thing for quit/exit: we remove the |
|
277 | 277 | # builtins if they exist, and our own prefilter routine will handle |
|
278 | 278 | # these special cases |
|
279 | 279 | try: |
|
280 | 280 | del __builtin__.exit, __builtin__.quit |
|
281 | 281 | except AttributeError: |
|
282 | 282 | pass |
|
283 | 283 | |
|
284 | 284 | # Store the actual shell's name |
|
285 | 285 | self.name = name |
|
286 | 286 | |
|
287 | 287 | # We need to know whether the instance is meant for embedding, since |
|
288 | 288 | # global/local namespaces need to be handled differently in that case |
|
289 | 289 | self.embedded = embedded |
|
290 | 290 | |
|
291 | 291 | # command compiler |
|
292 | 292 | self.compile = codeop.CommandCompiler() |
|
293 | 293 | |
|
294 | 294 | # User input buffer |
|
295 | 295 | self.buffer = [] |
|
296 | 296 | |
|
297 | 297 | # Default name given in compilation of code |
|
298 | 298 | self.filename = '<ipython console>' |
|
299 | 299 | |
|
300 | 300 | # Create the namespace where the user will operate. user_ns is |
|
301 | 301 | # normally the only one used, and it is passed to the exec calls as |
|
302 | 302 | # the locals argument. But we do carry a user_global_ns namespace |
|
303 | 303 | # given as the exec 'globals' argument, This is useful in embedding |
|
304 | 304 | # situations where the ipython shell opens in a context where the |
|
305 | 305 | # distinction between locals and globals is meaningful. |
|
306 | 306 | |
|
307 | 307 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
308 | 308 | # level as a dict instead of a module. This is a manual fix, but I |
|
309 | 309 | # should really track down where the problem is coming from. Alex |
|
310 | 310 | # Schmolck reported this problem first. |
|
311 | 311 | |
|
312 | 312 | # A useful post by Alex Martelli on this topic: |
|
313 | 313 | # Re: inconsistent value from __builtins__ |
|
314 | 314 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
315 | 315 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
316 | 316 | # Gruppen: comp.lang.python |
|
317 | 317 | |
|
318 | 318 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
319 | 319 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
320 | 320 | # > <type 'dict'> |
|
321 | 321 | # > >>> print type(__builtins__) |
|
322 | 322 | # > <type 'module'> |
|
323 | 323 | # > Is this difference in return value intentional? |
|
324 | 324 | |
|
325 | 325 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
326 | 326 | # or a module, and it's been that way for a long time. Whether it's |
|
327 | 327 | # intentional (or sensible), I don't know. In any case, the idea is |
|
328 | 328 | # that if you need to access the built-in namespace directly, you |
|
329 | 329 | # should start with "import __builtin__" (note, no 's') which will |
|
330 | 330 | # definitely give you a module. Yeah, it's somewhatΒ confusing:-(. |
|
331 | 331 | |
|
332 | 332 | if user_ns is None: |
|
333 | 333 | # Set __name__ to __main__ to better match the behavior of the |
|
334 | 334 | # normal interpreter. |
|
335 | 335 | user_ns = {'__name__' :'__main__', |
|
336 | 336 | '__builtins__' : __builtin__, |
|
337 | 337 | } |
|
338 | 338 | |
|
339 | 339 | if user_global_ns is None: |
|
340 | 340 | user_global_ns = {} |
|
341 | 341 | |
|
342 | 342 | # Assign namespaces |
|
343 | 343 | # This is the namespace where all normal user variables live |
|
344 | 344 | self.user_ns = user_ns |
|
345 | 345 | # Embedded instances require a separate namespace for globals. |
|
346 | 346 | # Normally this one is unused by non-embedded instances. |
|
347 | 347 | self.user_global_ns = user_global_ns |
|
348 | 348 | # A namespace to keep track of internal data structures to prevent |
|
349 | 349 | # them from cluttering user-visible stuff. Will be updated later |
|
350 | 350 | self.internal_ns = {} |
|
351 | 351 | |
|
352 | 352 | # Namespace of system aliases. Each entry in the alias |
|
353 | 353 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
354 | 354 | # of positional arguments of the alias. |
|
355 | 355 | self.alias_table = {} |
|
356 | 356 | |
|
357 | 357 | # A table holding all the namespaces IPython deals with, so that |
|
358 | 358 | # introspection facilities can search easily. |
|
359 | 359 | self.ns_table = {'user':user_ns, |
|
360 | 360 | 'user_global':user_global_ns, |
|
361 | 361 | 'alias':self.alias_table, |
|
362 | 362 | 'internal':self.internal_ns, |
|
363 | 363 | 'builtin':__builtin__.__dict__ |
|
364 | 364 | } |
|
365 | 365 | |
|
366 | 366 | # The user namespace MUST have a pointer to the shell itself. |
|
367 | 367 | self.user_ns[name] = self |
|
368 | 368 | |
|
369 | 369 | # We need to insert into sys.modules something that looks like a |
|
370 | 370 | # module but which accesses the IPython namespace, for shelve and |
|
371 | 371 | # pickle to work interactively. Normally they rely on getting |
|
372 | 372 | # everything out of __main__, but for embedding purposes each IPython |
|
373 | 373 | # instance has its own private namespace, so we can't go shoving |
|
374 | 374 | # everything into __main__. |
|
375 | 375 | |
|
376 | 376 | # note, however, that we should only do this for non-embedded |
|
377 | 377 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
378 | 378 | # namespace. Embedded instances, on the other hand, should not do |
|
379 | 379 | # this because they need to manage the user local/global namespaces |
|
380 | 380 | # only, but they live within a 'normal' __main__ (meaning, they |
|
381 | 381 | # shouldn't overtake the execution environment of the script they're |
|
382 | 382 | # embedded in). |
|
383 | 383 | |
|
384 | 384 | if not embedded: |
|
385 | 385 | try: |
|
386 | 386 | main_name = self.user_ns['__name__'] |
|
387 | 387 | except KeyError: |
|
388 | 388 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
389 | 389 | else: |
|
390 | 390 | #print "pickle hack in place" # dbg |
|
391 | 391 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
392 | 392 | |
|
393 | 393 | # List of input with multi-line handling. |
|
394 | 394 | # Fill its zero entry, user counter starts at 1 |
|
395 | 395 | self.input_hist = InputList(['\n']) |
|
396 | 396 | |
|
397 | 397 | # list of visited directories |
|
398 | 398 | try: |
|
399 | 399 | self.dir_hist = [os.getcwd()] |
|
400 | 400 | except IOError, e: |
|
401 | 401 | self.dir_hist = [] |
|
402 | 402 | |
|
403 | 403 | # dict of output history |
|
404 | 404 | self.output_hist = {} |
|
405 | 405 | |
|
406 | 406 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
407 | 407 | no_alias = {} |
|
408 | 408 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
409 | 409 | for key in keyword.kwlist + no_alias_magics: |
|
410 | 410 | no_alias[key] = 1 |
|
411 | 411 | no_alias.update(__builtin__.__dict__) |
|
412 | 412 | self.no_alias = no_alias |
|
413 | 413 | |
|
414 | 414 | # make global variables for user access to these |
|
415 | 415 | self.user_ns['_ih'] = self.input_hist |
|
416 | 416 | self.user_ns['_oh'] = self.output_hist |
|
417 | 417 | self.user_ns['_dh'] = self.dir_hist |
|
418 | 418 | |
|
419 | 419 | # user aliases to input and output histories |
|
420 | 420 | self.user_ns['In'] = self.input_hist |
|
421 | 421 | self.user_ns['Out'] = self.output_hist |
|
422 | 422 | |
|
423 | 423 | # Object variable to store code object waiting execution. This is |
|
424 | 424 | # used mainly by the multithreaded shells, but it can come in handy in |
|
425 | 425 | # other situations. No need to use a Queue here, since it's a single |
|
426 | 426 | # item which gets cleared once run. |
|
427 | 427 | self.code_to_run = None |
|
428 | 428 | |
|
429 | 429 | # Job manager (for jobs run as background threads) |
|
430 | 430 | self.jobs = BackgroundJobManager() |
|
431 | 431 | # Put the job manager into builtins so it's always there. |
|
432 | 432 | __builtin__.jobs = self.jobs |
|
433 | 433 | |
|
434 | 434 | # escapes for automatic behavior on the command line |
|
435 | 435 | self.ESC_SHELL = '!' |
|
436 | 436 | self.ESC_HELP = '?' |
|
437 | 437 | self.ESC_MAGIC = '%' |
|
438 | 438 | self.ESC_QUOTE = ',' |
|
439 | 439 | self.ESC_QUOTE2 = ';' |
|
440 | 440 | self.ESC_PAREN = '/' |
|
441 | 441 | |
|
442 | 442 | # And their associated handlers |
|
443 | 443 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
444 | 444 | self.ESC_QUOTE : self.handle_auto, |
|
445 | 445 | self.ESC_QUOTE2 : self.handle_auto, |
|
446 | 446 | self.ESC_MAGIC : self.handle_magic, |
|
447 | 447 | self.ESC_HELP : self.handle_help, |
|
448 | 448 | self.ESC_SHELL : self.handle_shell_escape, |
|
449 | 449 | } |
|
450 | 450 | |
|
451 | 451 | # class initializations |
|
452 | 452 | Magic.__init__(self,self) |
|
453 | 453 | |
|
454 | 454 | # Python source parser/formatter for syntax highlighting |
|
455 | 455 | pyformat = PyColorize.Parser().format |
|
456 | 456 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
457 | 457 | |
|
458 | 458 | # hooks holds pointers used for user-side customizations |
|
459 | 459 | self.hooks = Struct() |
|
460 | 460 | |
|
461 | 461 | # Set all default hooks, defined in the IPython.hooks module. |
|
462 | 462 | hooks = IPython.hooks |
|
463 | 463 | for hook_name in hooks.__all__: |
|
464 | 464 | self.set_hook(hook_name,getattr(hooks,hook_name)) |
|
465 | 465 | |
|
466 | 466 | # Flag to mark unconditional exit |
|
467 | 467 | self.exit_now = False |
|
468 | 468 | |
|
469 | 469 | self.usage_min = """\ |
|
470 | 470 | An enhanced console for Python. |
|
471 | 471 | Some of its features are: |
|
472 | 472 | - Readline support if the readline library is present. |
|
473 | 473 | - Tab completion in the local namespace. |
|
474 | 474 | - Logging of input, see command-line options. |
|
475 | 475 | - System shell escape via ! , eg !ls. |
|
476 | 476 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
477 | 477 | - Keeps track of locally defined variables via %who, %whos. |
|
478 | 478 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
479 | 479 | """ |
|
480 | 480 | if usage: self.usage = usage |
|
481 | 481 | else: self.usage = self.usage_min |
|
482 | 482 | |
|
483 | 483 | # Storage |
|
484 | 484 | self.rc = rc # This will hold all configuration information |
|
485 | 485 | self.pager = 'less' |
|
486 | 486 | # temporary files used for various purposes. Deleted at exit. |
|
487 | 487 | self.tempfiles = [] |
|
488 | 488 | |
|
489 | 489 | # Keep track of readline usage (later set by init_readline) |
|
490 | 490 | self.has_readline = False |
|
491 | 491 | |
|
492 | 492 | # template for logfile headers. It gets resolved at runtime by the |
|
493 | 493 | # logstart method. |
|
494 | 494 | self.loghead_tpl = \ |
|
495 | 495 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
496 | 496 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
497 | 497 | #log# opts = %s |
|
498 | 498 | #log# args = %s |
|
499 | 499 | #log# It is safe to make manual edits below here. |
|
500 | 500 | #log#----------------------------------------------------------------------- |
|
501 | 501 | """ |
|
502 | 502 | # for pushd/popd management |
|
503 | 503 | try: |
|
504 | 504 | self.home_dir = get_home_dir() |
|
505 | 505 | except HomeDirError,msg: |
|
506 | 506 | fatal(msg) |
|
507 | 507 | |
|
508 | 508 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
509 | 509 | |
|
510 | 510 | # Functions to call the underlying shell. |
|
511 | 511 | |
|
512 | 512 | # utility to expand user variables via Itpl |
|
513 | 513 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
514 | 514 | self.user_ns)) |
|
515 | 515 | # The first is similar to os.system, but it doesn't return a value, |
|
516 | 516 | # and it allows interpolation of variables in the user's namespace. |
|
517 | 517 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
518 | 518 | header='IPython system call: ', |
|
519 | 519 | verbose=self.rc.system_verbose) |
|
520 | 520 | # These are for getoutput and getoutputerror: |
|
521 | 521 | self.getoutput = lambda cmd: \ |
|
522 | 522 | getoutput(self.var_expand(cmd), |
|
523 | 523 | header='IPython system call: ', |
|
524 | 524 | verbose=self.rc.system_verbose) |
|
525 | 525 | self.getoutputerror = lambda cmd: \ |
|
526 | 526 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), |
|
527 | 527 | self.user_ns)), |
|
528 | 528 | header='IPython system call: ', |
|
529 | 529 | verbose=self.rc.system_verbose) |
|
530 | 530 | |
|
531 | 531 | # RegExp for splitting line contents into pre-char//first |
|
532 | 532 | # word-method//rest. For clarity, each group in on one line. |
|
533 | 533 | |
|
534 | 534 | # WARNING: update the regexp if the above escapes are changed, as they |
|
535 | 535 | # are hardwired in. |
|
536 | 536 | |
|
537 | 537 | # Don't get carried away with trying to make the autocalling catch too |
|
538 | 538 | # much: it's better to be conservative rather than to trigger hidden |
|
539 | 539 | # evals() somewhere and end up causing side effects. |
|
540 | 540 | |
|
541 | 541 | self.line_split = re.compile(r'^([\s*,;/])' |
|
542 | 542 | r'([\?\w\.]+\w*\s*)' |
|
543 | 543 | r'(\(?.*$)') |
|
544 | 544 | |
|
545 | 545 | # Original re, keep around for a while in case changes break something |
|
546 | 546 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
547 | 547 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
548 | 548 | # r'(\(?.*$)') |
|
549 | 549 | |
|
550 | 550 | # RegExp to identify potential function names |
|
551 | 551 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
552 | 552 | # RegExp to exclude strings with this start from autocalling |
|
553 | 553 | self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ') |
|
554 | 554 | |
|
555 | 555 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
556 | 556 | # (experimental). For this to work, the line_split regexp would need |
|
557 | 557 | # to be modified so it wouldn't break things at '['. That line is |
|
558 | 558 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
559 | 559 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
560 | 560 | |
|
561 | 561 | # keep track of where we started running (mainly for crash post-mortem) |
|
562 | 562 | self.starting_dir = os.getcwd() |
|
563 | 563 | |
|
564 | 564 | # Various switches which can be set |
|
565 | 565 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
566 | 566 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
567 | 567 | self.banner2 = banner2 |
|
568 | 568 | |
|
569 | 569 | # TraceBack handlers: |
|
570 | 570 | |
|
571 | 571 | # Syntax error handler. |
|
572 | 572 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
573 | 573 | |
|
574 | 574 | # The interactive one is initialized with an offset, meaning we always |
|
575 | 575 | # want to remove the topmost item in the traceback, which is our own |
|
576 | 576 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
577 | 577 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
578 | 578 | color_scheme='NoColor', |
|
579 | 579 | tb_offset = 1) |
|
580 | 580 | |
|
581 | 581 | # IPython itself shouldn't crash. This will produce a detailed |
|
582 | 582 | # post-mortem if it does. But we only install the crash handler for |
|
583 | 583 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
584 | 584 | # and lose the crash handler. This is because exceptions in the main |
|
585 | 585 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
586 | 586 | # and there's no point in printing crash dumps for every user exception. |
|
587 | 587 | if self.isthreaded: |
|
588 | 588 | sys.excepthook = ultraTB.FormattedTB() |
|
589 | 589 | else: |
|
590 | 590 | from IPython import CrashHandler |
|
591 | 591 | sys.excepthook = CrashHandler.CrashHandler(self) |
|
592 | 592 | |
|
593 | 593 | # The instance will store a pointer to this, so that runtime code |
|
594 | 594 | # (such as magics) can access it. This is because during the |
|
595 | 595 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
596 | 596 | # frameworks). |
|
597 | 597 | self.sys_excepthook = sys.excepthook |
|
598 | 598 | |
|
599 | 599 | # and add any custom exception handlers the user may have specified |
|
600 | 600 | self.set_custom_exc(*custom_exceptions) |
|
601 | 601 | |
|
602 | 602 | # Object inspector |
|
603 | 603 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
604 | 604 | PyColorize.ANSICodeColors, |
|
605 | 605 | 'NoColor') |
|
606 | 606 | # indentation management |
|
607 | 607 | self.autoindent = False |
|
608 | 608 | self.indent_current_nsp = 0 |
|
609 | 609 | self.indent_current = '' # actual indent string |
|
610 | 610 | |
|
611 | 611 | # Make some aliases automatically |
|
612 | 612 | # Prepare list of shell aliases to auto-define |
|
613 | 613 | if os.name == 'posix': |
|
614 | 614 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
615 | 615 | 'mv mv -i','rm rm -i','cp cp -i', |
|
616 | 616 | 'cat cat','less less','clear clear', |
|
617 | 617 | # a better ls |
|
618 | 618 | 'ls ls -F', |
|
619 | 619 | # long ls |
|
620 | 620 | 'll ls -lF', |
|
621 | 621 | # color ls |
|
622 | 622 | 'lc ls -F -o --color', |
|
623 | 623 | # ls normal files only |
|
624 | 624 | 'lf ls -F -o --color %l | grep ^-', |
|
625 | 625 | # ls symbolic links |
|
626 | 626 | 'lk ls -F -o --color %l | grep ^l', |
|
627 | 627 | # directories or links to directories, |
|
628 | 628 | 'ldir ls -F -o --color %l | grep /$', |
|
629 | 629 | # things which are executable |
|
630 | 630 | 'lx ls -F -o --color %l | grep ^-..x', |
|
631 | 631 | ) |
|
632 | 632 | elif os.name in ['nt','dos']: |
|
633 | 633 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
634 | 634 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
635 | 635 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
636 | 636 | 'ren ren','cls cls','copy copy') |
|
637 | 637 | else: |
|
638 | 638 | auto_alias = () |
|
639 | 639 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) |
|
640 | 640 | # Call the actual (public) initializer |
|
641 | 641 | self.init_auto_alias() |
|
642 | 642 | # end __init__ |
|
643 | 643 | |
|
644 | 644 | def post_config_initialization(self): |
|
645 | 645 | """Post configuration init method |
|
646 | 646 | |
|
647 | 647 | This is called after the configuration files have been processed to |
|
648 | 648 | 'finalize' the initialization.""" |
|
649 | 649 | |
|
650 | 650 | rc = self.rc |
|
651 | 651 | |
|
652 | 652 | # Load readline proper |
|
653 | 653 | if rc.readline: |
|
654 | 654 | self.init_readline() |
|
655 | 655 | |
|
656 | 656 | # log system |
|
657 | 657 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
658 | 658 | # local shortcut, this is used a LOT |
|
659 | 659 | self.log = self.logger.log |
|
660 | 660 | |
|
661 | 661 | # Initialize cache, set in/out prompts and printing system |
|
662 | 662 | self.outputcache = CachedOutput(self, |
|
663 | 663 | rc.cache_size, |
|
664 | 664 | rc.pprint, |
|
665 | 665 | input_sep = rc.separate_in, |
|
666 | 666 | output_sep = rc.separate_out, |
|
667 | 667 | output_sep2 = rc.separate_out2, |
|
668 | 668 | ps1 = rc.prompt_in1, |
|
669 | 669 | ps2 = rc.prompt_in2, |
|
670 | 670 | ps_out = rc.prompt_out, |
|
671 | 671 | pad_left = rc.prompts_pad_left) |
|
672 | 672 | |
|
673 | 673 | # user may have over-ridden the default print hook: |
|
674 | 674 | try: |
|
675 | 675 | self.outputcache.__class__.display = self.hooks.display |
|
676 | 676 | except AttributeError: |
|
677 | 677 | pass |
|
678 | 678 | |
|
679 | 679 | # I don't like assigning globally to sys, because it means when embedding |
|
680 | 680 | # instances, each embedded instance overrides the previous choice. But |
|
681 | 681 | # sys.displayhook seems to be called internally by exec, so I don't see a |
|
682 | 682 | # way around it. |
|
683 | 683 | sys.displayhook = self.outputcache |
|
684 | 684 | |
|
685 | 685 | # Set user colors (don't do it in the constructor above so that it |
|
686 | 686 | # doesn't crash if colors option is invalid) |
|
687 | 687 | self.magic_colors(rc.colors) |
|
688 | 688 | |
|
689 | 689 | # Set calling of pdb on exceptions |
|
690 | 690 | self.call_pdb = rc.pdb |
|
691 | 691 | |
|
692 | 692 | # Load user aliases |
|
693 | 693 | for alias in rc.alias: |
|
694 | 694 | self.magic_alias(alias) |
|
695 | 695 | |
|
696 | 696 | # dynamic data that survives through sessions |
|
697 | 697 | # XXX make the filename a config option? |
|
698 | 698 | persist_base = 'persist' |
|
699 | 699 | if rc.profile: |
|
700 | 700 | persist_base += '_%s' % rc.profile |
|
701 | 701 | self.persist_fname = os.path.join(rc.ipythondir,persist_base) |
|
702 | 702 | |
|
703 | 703 | try: |
|
704 | 704 | self.persist = pickle.load(file(self.persist_fname)) |
|
705 | 705 | except: |
|
706 | 706 | self.persist = {} |
|
707 | 707 | |
|
708 | 708 | |
|
709 | 709 | for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]: |
|
710 | 710 | try: |
|
711 | 711 | obj = pickle.loads(value) |
|
712 | 712 | except: |
|
713 | 713 | |
|
714 | 714 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key |
|
715 | 715 | print "The error was:",sys.exc_info()[0] |
|
716 | 716 | continue |
|
717 | 717 | |
|
718 | 718 | |
|
719 | 719 | self.user_ns[key] = obj |
|
720 | 720 | |
|
721 | 721 | |
|
722 | 722 | |
|
723 | 723 | |
|
724 | 724 | def set_hook(self,name,hook): |
|
725 | 725 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
726 | 726 | |
|
727 | 727 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
728 | 728 | resetting one of these hooks, you can modify IPython's behavior to |
|
729 | 729 | call at runtime your own routines.""" |
|
730 | 730 | |
|
731 | 731 | # At some point in the future, this should validate the hook before it |
|
732 | 732 | # accepts it. Probably at least check that the hook takes the number |
|
733 | 733 | # of args it's supposed to. |
|
734 | 734 | setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
735 | 735 | |
|
736 | 736 | def set_custom_exc(self,exc_tuple,handler): |
|
737 | 737 | """set_custom_exc(exc_tuple,handler) |
|
738 | 738 | |
|
739 | 739 | Set a custom exception handler, which will be called if any of the |
|
740 | 740 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
741 | 741 | runcode() method. |
|
742 | 742 | |
|
743 | 743 | Inputs: |
|
744 | 744 | |
|
745 | 745 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
746 | 746 | handler for. It is very important that you use a tuple, and NOT A |
|
747 | 747 | LIST here, because of the way Python's except statement works. If |
|
748 | 748 | you only want to trap a single exception, use a singleton tuple: |
|
749 | 749 | |
|
750 | 750 | exc_tuple == (MyCustomException,) |
|
751 | 751 | |
|
752 | 752 | - handler: this must be defined as a function with the following |
|
753 | 753 | basic interface: def my_handler(self,etype,value,tb). |
|
754 | 754 | |
|
755 | 755 | This will be made into an instance method (via new.instancemethod) |
|
756 | 756 | of IPython itself, and it will be called if any of the exceptions |
|
757 | 757 | listed in the exc_tuple are caught. If the handler is None, an |
|
758 | 758 | internal basic one is used, which just prints basic info. |
|
759 | 759 | |
|
760 | 760 | WARNING: by putting in your own exception handler into IPython's main |
|
761 | 761 | execution loop, you run a very good chance of nasty crashes. This |
|
762 | 762 | facility should only be used if you really know what you are doing.""" |
|
763 | 763 | |
|
764 | 764 | assert type(exc_tuple)==type(()) , \ |
|
765 | 765 | "The custom exceptions must be given AS A TUPLE." |
|
766 | 766 | |
|
767 | 767 | def dummy_handler(self,etype,value,tb): |
|
768 | 768 | print '*** Simple custom exception handler ***' |
|
769 | 769 | print 'Exception type :',etype |
|
770 | 770 | print 'Exception value:',value |
|
771 | 771 | print 'Traceback :',tb |
|
772 | 772 | print 'Source code :','\n'.join(self.buffer) |
|
773 | 773 | |
|
774 | 774 | if handler is None: handler = dummy_handler |
|
775 | 775 | |
|
776 | 776 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
777 | 777 | self.custom_exceptions = exc_tuple |
|
778 | 778 | |
|
779 | 779 | def set_custom_completer(self,completer,pos=0): |
|
780 | 780 | """set_custom_completer(completer,pos=0) |
|
781 | 781 | |
|
782 | 782 | Adds a new custom completer function. |
|
783 | 783 | |
|
784 | 784 | The position argument (defaults to 0) is the index in the completers |
|
785 | 785 | list where you want the completer to be inserted.""" |
|
786 | 786 | |
|
787 | 787 | newcomp = new.instancemethod(completer,self.Completer, |
|
788 | 788 | self.Completer.__class__) |
|
789 | 789 | self.Completer.matchers.insert(pos,newcomp) |
|
790 | 790 | |
|
791 | 791 | def _get_call_pdb(self): |
|
792 | 792 | return self._call_pdb |
|
793 | 793 | |
|
794 | 794 | def _set_call_pdb(self,val): |
|
795 | 795 | |
|
796 | 796 | if val not in (0,1,False,True): |
|
797 | 797 | raise ValueError,'new call_pdb value must be boolean' |
|
798 | 798 | |
|
799 | 799 | # store value in instance |
|
800 | 800 | self._call_pdb = val |
|
801 | 801 | |
|
802 | 802 | # notify the actual exception handlers |
|
803 | 803 | self.InteractiveTB.call_pdb = val |
|
804 | 804 | if self.isthreaded: |
|
805 | 805 | try: |
|
806 | 806 | self.sys_excepthook.call_pdb = val |
|
807 | 807 | except: |
|
808 | 808 | warn('Failed to activate pdb for threaded exception handler') |
|
809 | 809 | |
|
810 | 810 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
811 | 811 | 'Control auto-activation of pdb at exceptions') |
|
812 | 812 | |
|
813 | 813 | def complete(self,text): |
|
814 | 814 | """Return a sorted list of all possible completions on text. |
|
815 | 815 | |
|
816 | 816 | Inputs: |
|
817 | 817 | |
|
818 | 818 | - text: a string of text to be completed on. |
|
819 | 819 | |
|
820 | 820 | This is a wrapper around the completion mechanism, similar to what |
|
821 | 821 | readline does at the command line when the TAB key is hit. By |
|
822 | 822 | exposing it as a method, it can be used by other non-readline |
|
823 | 823 | environments (such as GUIs) for text completion. |
|
824 | 824 | |
|
825 | 825 | Simple usage example: |
|
826 | 826 | |
|
827 | 827 | In [1]: x = 'hello' |
|
828 | 828 | |
|
829 | 829 | In [2]: __IP.complete('x.l') |
|
830 | 830 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
831 | 831 | |
|
832 | 832 | complete = self.Completer.complete |
|
833 | 833 | state = 0 |
|
834 | 834 | # use a dict so we get unique keys, since ipyhton's multiple |
|
835 | 835 | # completers can return duplicates. |
|
836 | 836 | comps = {} |
|
837 | 837 | while True: |
|
838 | 838 | newcomp = complete(text,state) |
|
839 | 839 | if newcomp is None: |
|
840 | 840 | break |
|
841 | 841 | comps[newcomp] = 1 |
|
842 | 842 | state += 1 |
|
843 | 843 | outcomps = comps.keys() |
|
844 | 844 | outcomps.sort() |
|
845 | 845 | return outcomps |
|
846 | 846 | |
|
847 | 847 | def set_completer_frame(self, frame): |
|
848 | 848 | if frame: |
|
849 | 849 | self.Completer.namespace = frame.f_locals |
|
850 | 850 | self.Completer.global_namespace = frame.f_globals |
|
851 | 851 | else: |
|
852 | 852 | self.Completer.namespace = self.user_ns |
|
853 | 853 | self.Completer.global_namespace = self.user_global_ns |
|
854 | 854 | |
|
855 | 855 | def init_auto_alias(self): |
|
856 | 856 | """Define some aliases automatically. |
|
857 | 857 | |
|
858 | 858 | These are ALL parameter-less aliases""" |
|
859 | 859 | for alias,cmd in self.auto_alias: |
|
860 | 860 | self.alias_table[alias] = (0,cmd) |
|
861 | 861 | |
|
862 | 862 | def alias_table_validate(self,verbose=0): |
|
863 | 863 | """Update information about the alias table. |
|
864 | 864 | |
|
865 | 865 | In particular, make sure no Python keywords/builtins are in it.""" |
|
866 | 866 | |
|
867 | 867 | no_alias = self.no_alias |
|
868 | 868 | for k in self.alias_table.keys(): |
|
869 | 869 | if k in no_alias: |
|
870 | 870 | del self.alias_table[k] |
|
871 | 871 | if verbose: |
|
872 | 872 | print ("Deleting alias <%s>, it's a Python " |
|
873 | 873 | "keyword or builtin." % k) |
|
874 | 874 | |
|
875 | 875 | def set_autoindent(self,value=None): |
|
876 | 876 | """Set the autoindent flag, checking for readline support. |
|
877 | 877 | |
|
878 | 878 | If called with no arguments, it acts as a toggle.""" |
|
879 | 879 | |
|
880 | 880 | if not self.has_readline: |
|
881 | 881 | if os.name == 'posix': |
|
882 | 882 | warn("The auto-indent feature requires the readline library") |
|
883 | 883 | self.autoindent = 0 |
|
884 | 884 | return |
|
885 | 885 | if value is None: |
|
886 | 886 | self.autoindent = not self.autoindent |
|
887 | 887 | else: |
|
888 | 888 | self.autoindent = value |
|
889 | 889 | |
|
890 | 890 | def rc_set_toggle(self,rc_field,value=None): |
|
891 | 891 | """Set or toggle a field in IPython's rc config. structure. |
|
892 | 892 | |
|
893 | 893 | If called with no arguments, it acts as a toggle. |
|
894 | 894 | |
|
895 | 895 | If called with a non-existent field, the resulting AttributeError |
|
896 | 896 | exception will propagate out.""" |
|
897 | 897 | |
|
898 | 898 | rc_val = getattr(self.rc,rc_field) |
|
899 | 899 | if value is None: |
|
900 | 900 | value = not rc_val |
|
901 | 901 | setattr(self.rc,rc_field,value) |
|
902 | 902 | |
|
903 | 903 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
904 | 904 | """Install the user configuration directory. |
|
905 | 905 | |
|
906 | 906 | Can be called when running for the first time or to upgrade the user's |
|
907 | 907 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
908 | 908 | and 'upgrade'.""" |
|
909 | 909 | |
|
910 | 910 | def wait(): |
|
911 | 911 | try: |
|
912 | 912 | raw_input("Please press <RETURN> to start IPython.") |
|
913 | 913 | except EOFError: |
|
914 | 914 | print >> Term.cout |
|
915 | 915 | print '*'*70 |
|
916 | 916 | |
|
917 | 917 | cwd = os.getcwd() # remember where we started |
|
918 | 918 | glb = glob.glob |
|
919 | 919 | print '*'*70 |
|
920 | 920 | if mode == 'install': |
|
921 | 921 | print \ |
|
922 | 922 | """Welcome to IPython. I will try to create a personal configuration directory |
|
923 | 923 | where you can customize many aspects of IPython's functionality in:\n""" |
|
924 | 924 | else: |
|
925 | 925 | print 'I am going to upgrade your configuration in:' |
|
926 | 926 | |
|
927 | 927 | print ipythondir |
|
928 | 928 | |
|
929 | 929 | rcdirend = os.path.join('IPython','UserConfig') |
|
930 | 930 | cfg = lambda d: os.path.join(d,rcdirend) |
|
931 | 931 | try: |
|
932 | 932 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
933 | 933 | except IOError: |
|
934 | 934 | warning = """ |
|
935 | 935 | Installation error. IPython's directory was not found. |
|
936 | 936 | |
|
937 | 937 | Check the following: |
|
938 | 938 | |
|
939 | 939 | The ipython/IPython directory should be in a directory belonging to your |
|
940 | 940 | PYTHONPATH environment variable (that is, it should be in a directory |
|
941 | 941 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
942 | 942 | |
|
943 | 943 | IPython will proceed with builtin defaults. |
|
944 | 944 | """ |
|
945 | 945 | warn(warning) |
|
946 | 946 | wait() |
|
947 | 947 | return |
|
948 | 948 | |
|
949 | 949 | if mode == 'install': |
|
950 | 950 | try: |
|
951 | 951 | shutil.copytree(rcdir,ipythondir) |
|
952 | 952 | os.chdir(ipythondir) |
|
953 | 953 | rc_files = glb("ipythonrc*") |
|
954 | 954 | for rc_file in rc_files: |
|
955 | 955 | os.rename(rc_file,rc_file+rc_suffix) |
|
956 | 956 | except: |
|
957 | 957 | warning = """ |
|
958 | 958 | |
|
959 | 959 | There was a problem with the installation: |
|
960 | 960 | %s |
|
961 | 961 | Try to correct it or contact the developers if you think it's a bug. |
|
962 | 962 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
963 | 963 | warn(warning) |
|
964 | 964 | wait() |
|
965 | 965 | return |
|
966 | 966 | |
|
967 | 967 | elif mode == 'upgrade': |
|
968 | 968 | try: |
|
969 | 969 | os.chdir(ipythondir) |
|
970 | 970 | except: |
|
971 | 971 | print """ |
|
972 | 972 | Can not upgrade: changing to directory %s failed. Details: |
|
973 | 973 | %s |
|
974 | 974 | """ % (ipythondir,sys.exc_info()[1]) |
|
975 | 975 | wait() |
|
976 | 976 | return |
|
977 | 977 | else: |
|
978 | 978 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
979 | 979 | for new_full_path in sources: |
|
980 | 980 | new_filename = os.path.basename(new_full_path) |
|
981 | 981 | if new_filename.startswith('ipythonrc'): |
|
982 | 982 | new_filename = new_filename + rc_suffix |
|
983 | 983 | # The config directory should only contain files, skip any |
|
984 | 984 | # directories which may be there (like CVS) |
|
985 | 985 | if os.path.isdir(new_full_path): |
|
986 | 986 | continue |
|
987 | 987 | if os.path.exists(new_filename): |
|
988 | 988 | old_file = new_filename+'.old' |
|
989 | 989 | if os.path.exists(old_file): |
|
990 | 990 | os.remove(old_file) |
|
991 | 991 | os.rename(new_filename,old_file) |
|
992 | 992 | shutil.copy(new_full_path,new_filename) |
|
993 | 993 | else: |
|
994 | 994 | raise ValueError,'unrecognized mode for install:',`mode` |
|
995 | 995 | |
|
996 | 996 | # Fix line-endings to those native to each platform in the config |
|
997 | 997 | # directory. |
|
998 | 998 | try: |
|
999 | 999 | os.chdir(ipythondir) |
|
1000 | 1000 | except: |
|
1001 | 1001 | print """ |
|
1002 | 1002 | Problem: changing to directory %s failed. |
|
1003 | 1003 | Details: |
|
1004 | 1004 | %s |
|
1005 | 1005 | |
|
1006 | 1006 | Some configuration files may have incorrect line endings. This should not |
|
1007 | 1007 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1008 | 1008 | wait() |
|
1009 | 1009 | else: |
|
1010 | 1010 | for fname in glb('ipythonrc*'): |
|
1011 | 1011 | try: |
|
1012 | 1012 | native_line_ends(fname,backup=0) |
|
1013 | 1013 | except IOError: |
|
1014 | 1014 | pass |
|
1015 | 1015 | |
|
1016 | 1016 | if mode == 'install': |
|
1017 | 1017 | print """ |
|
1018 | 1018 | Successful installation! |
|
1019 | 1019 | |
|
1020 | 1020 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1021 | 1021 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1022 | 1022 | distribution) to make sure that your system environment is properly configured |
|
1023 | 1023 | to take advantage of IPython's features.""" |
|
1024 | 1024 | else: |
|
1025 | 1025 | print """ |
|
1026 | 1026 | Successful upgrade! |
|
1027 | 1027 | |
|
1028 | 1028 | All files in your directory: |
|
1029 | 1029 | %(ipythondir)s |
|
1030 | 1030 | which would have been overwritten by the upgrade were backed up with a .old |
|
1031 | 1031 | extension. If you had made particular customizations in those files you may |
|
1032 | 1032 | want to merge them back into the new files.""" % locals() |
|
1033 | 1033 | wait() |
|
1034 | 1034 | os.chdir(cwd) |
|
1035 | 1035 | # end user_setup() |
|
1036 | 1036 | |
|
1037 | 1037 | def atexit_operations(self): |
|
1038 | 1038 | """This will be executed at the time of exit. |
|
1039 | 1039 | |
|
1040 | 1040 | Saving of persistent data should be performed here. """ |
|
1041 | 1041 | |
|
1042 | 1042 | # input history |
|
1043 | 1043 | self.savehist() |
|
1044 | 1044 | |
|
1045 | 1045 | # Cleanup all tempfiles left around |
|
1046 | 1046 | for tfile in self.tempfiles: |
|
1047 | 1047 | try: |
|
1048 | 1048 | os.unlink(tfile) |
|
1049 | 1049 | except OSError: |
|
1050 | 1050 | pass |
|
1051 | 1051 | |
|
1052 | 1052 | # save the "persistent data" catch-all dictionary |
|
1053 | 1053 | try: |
|
1054 | 1054 | pickle.dump(self.persist, open(self.persist_fname,"w")) |
|
1055 | 1055 | except: |
|
1056 | 1056 | print "*** ERROR *** persistent data saving failed." |
|
1057 | 1057 | |
|
1058 | 1058 | def savehist(self): |
|
1059 | 1059 | """Save input history to a file (via readline library).""" |
|
1060 | 1060 | try: |
|
1061 | 1061 | self.readline.write_history_file(self.histfile) |
|
1062 | 1062 | except: |
|
1063 | 1063 | print 'Unable to save IPython command history to file: ' + \ |
|
1064 | 1064 | `self.histfile` |
|
1065 | 1065 | |
|
1066 | 1066 | def pre_readline(self): |
|
1067 | 1067 | """readline hook to be used at the start of each line. |
|
1068 | 1068 | |
|
1069 | 1069 | Currently it handles auto-indent only.""" |
|
1070 | 1070 | |
|
1071 | 1071 | self.readline.insert_text(self.indent_current) |
|
1072 | 1072 | |
|
1073 | 1073 | def init_readline(self): |
|
1074 | 1074 | """Command history completion/saving/reloading.""" |
|
1075 | 1075 | try: |
|
1076 | 1076 | import readline |
|
1077 | 1077 | except ImportError: |
|
1078 | 1078 | self.has_readline = 0 |
|
1079 | 1079 | self.readline = None |
|
1080 | 1080 | # no point in bugging windows users with this every time: |
|
1081 | 1081 | if os.name == 'posix': |
|
1082 | 1082 | warn('Readline services not available on this platform.') |
|
1083 | 1083 | else: |
|
1084 | 1084 | import atexit |
|
1085 | 1085 | from IPython.completer import IPCompleter |
|
1086 | 1086 | self.Completer = IPCompleter(self, |
|
1087 | 1087 | self.user_ns, |
|
1088 | 1088 | self.user_global_ns, |
|
1089 | 1089 | self.rc.readline_omit__names, |
|
1090 | 1090 | self.alias_table) |
|
1091 | 1091 | |
|
1092 | 1092 | # Platform-specific configuration |
|
1093 | 1093 | if os.name == 'nt': |
|
1094 | 1094 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1095 | 1095 | else: |
|
1096 | 1096 | self.readline_startup_hook = readline.set_startup_hook |
|
1097 | 1097 | |
|
1098 | 1098 | # Load user's initrc file (readline config) |
|
1099 | 1099 | inputrc_name = os.environ.get('INPUTRC') |
|
1100 | 1100 | if inputrc_name is None: |
|
1101 | 1101 | home_dir = get_home_dir() |
|
1102 | 1102 | if home_dir is not None: |
|
1103 | 1103 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1104 | 1104 | if os.path.isfile(inputrc_name): |
|
1105 | 1105 | try: |
|
1106 | 1106 | readline.read_init_file(inputrc_name) |
|
1107 | 1107 | except: |
|
1108 | 1108 | warn('Problems reading readline initialization file <%s>' |
|
1109 | 1109 | % inputrc_name) |
|
1110 | 1110 | |
|
1111 | 1111 | self.has_readline = 1 |
|
1112 | 1112 | self.readline = readline |
|
1113 | 1113 | # save this in sys so embedded copies can restore it properly |
|
1114 | 1114 | sys.ipcompleter = self.Completer.complete |
|
1115 | 1115 | readline.set_completer(self.Completer.complete) |
|
1116 | 1116 | |
|
1117 | 1117 | # Configure readline according to user's prefs |
|
1118 | 1118 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1119 | 1119 | readline.parse_and_bind(rlcommand) |
|
1120 | 1120 | |
|
1121 | 1121 | # remove some chars from the delimiters list |
|
1122 | 1122 | delims = readline.get_completer_delims() |
|
1123 | 1123 | delims = delims.translate(string._idmap, |
|
1124 | 1124 | self.rc.readline_remove_delims) |
|
1125 | 1125 | readline.set_completer_delims(delims) |
|
1126 | 1126 | # otherwise we end up with a monster history after a while: |
|
1127 | 1127 | readline.set_history_length(1000) |
|
1128 | 1128 | try: |
|
1129 | 1129 | #print '*** Reading readline history' # dbg |
|
1130 | 1130 | readline.read_history_file(self.histfile) |
|
1131 | 1131 | except IOError: |
|
1132 | 1132 | pass # It doesn't exist yet. |
|
1133 | 1133 | |
|
1134 | 1134 | atexit.register(self.atexit_operations) |
|
1135 | 1135 | del atexit |
|
1136 | 1136 | |
|
1137 | 1137 | # Configure auto-indent for all platforms |
|
1138 | 1138 | self.set_autoindent(self.rc.autoindent) |
|
1139 | 1139 | |
|
1140 | 1140 | def _should_recompile(self,e): |
|
1141 | 1141 | """Utility routine for edit_syntax_error""" |
|
1142 | 1142 | |
|
1143 | 1143 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1144 | 1144 | '<console>'): |
|
1145 | 1145 | return False |
|
1146 | 1146 | try: |
|
1147 | 1147 | if not ask_yes_no('Return to editor to correct syntax error? ' |
|
1148 | 1148 | '[Y/n] ','y'): |
|
1149 | 1149 | return False |
|
1150 | 1150 | except EOFError: |
|
1151 | 1151 | return False |
|
1152 | 1152 | self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg) |
|
1153 | 1153 | return True |
|
1154 | 1154 | |
|
1155 | 1155 | def edit_syntax_error(self): |
|
1156 | 1156 | """The bottom half of the syntax error handler called in the main loop. |
|
1157 | 1157 | |
|
1158 | 1158 | Loop until syntax error is fixed or user cancels. |
|
1159 | 1159 | """ |
|
1160 | 1160 | |
|
1161 | 1161 | while self.SyntaxTB.last_syntax_error: |
|
1162 | 1162 | # copy and clear last_syntax_error |
|
1163 | 1163 | err = self.SyntaxTB.clear_err_state() |
|
1164 | 1164 | if not self._should_recompile(err): |
|
1165 | 1165 | return |
|
1166 | 1166 | try: |
|
1167 | 1167 | # may set last_syntax_error again if a SyntaxError is raised |
|
1168 | 1168 | self.safe_execfile(err.filename,self.shell.user_ns) |
|
1169 | 1169 | except: |
|
1170 | 1170 | self.showtraceback() |
|
1171 | 1171 | else: |
|
1172 | 1172 | f = file(err.filename) |
|
1173 | 1173 | try: |
|
1174 | 1174 | sys.displayhook(f.read()) |
|
1175 | 1175 | finally: |
|
1176 | 1176 | f.close() |
|
1177 | 1177 | |
|
1178 | 1178 | def showsyntaxerror(self, filename=None): |
|
1179 | 1179 | """Display the syntax error that just occurred. |
|
1180 | 1180 | |
|
1181 | 1181 | This doesn't display a stack trace because there isn't one. |
|
1182 | 1182 | |
|
1183 | 1183 | If a filename is given, it is stuffed in the exception instead |
|
1184 | 1184 | of what was there before (because Python's parser always uses |
|
1185 | 1185 | "<string>" when reading from a string). |
|
1186 | 1186 | """ |
|
1187 | 1187 | etype, value, last_traceback = sys.exc_info() |
|
1188 | 1188 | if filename and etype is SyntaxError: |
|
1189 | 1189 | # Work hard to stuff the correct filename in the exception |
|
1190 | 1190 | try: |
|
1191 | 1191 | msg, (dummy_filename, lineno, offset, line) = value |
|
1192 | 1192 | except: |
|
1193 | 1193 | # Not the format we expect; leave it alone |
|
1194 | 1194 | pass |
|
1195 | 1195 | else: |
|
1196 | 1196 | # Stuff in the right filename |
|
1197 | 1197 | try: |
|
1198 | 1198 | # Assume SyntaxError is a class exception |
|
1199 | 1199 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1200 | 1200 | except: |
|
1201 | 1201 | # If that failed, assume SyntaxError is a string |
|
1202 | 1202 | value = msg, (filename, lineno, offset, line) |
|
1203 | 1203 | self.SyntaxTB(etype,value,[]) |
|
1204 | 1204 | |
|
1205 | 1205 | def debugger(self): |
|
1206 | 1206 | """Call the pdb debugger.""" |
|
1207 | 1207 | |
|
1208 | 1208 | if not self.rc.pdb: |
|
1209 | 1209 | return |
|
1210 | 1210 | pdb.pm() |
|
1211 | 1211 | |
|
1212 | 1212 | def showtraceback(self,exc_tuple = None,filename=None): |
|
1213 | 1213 | """Display the exception that just occurred.""" |
|
1214 | 1214 | |
|
1215 | 1215 | # Though this won't be called by syntax errors in the input line, |
|
1216 | 1216 | # there may be SyntaxError cases whith imported code. |
|
1217 | 1217 | if exc_tuple is None: |
|
1218 | 1218 | type, value, tb = sys.exc_info() |
|
1219 | 1219 | else: |
|
1220 | 1220 | type, value, tb = exc_tuple |
|
1221 | 1221 | if type is SyntaxError: |
|
1222 | 1222 | self.showsyntaxerror(filename) |
|
1223 | 1223 | else: |
|
1224 | 1224 | self.InteractiveTB() |
|
1225 | 1225 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1226 | 1226 | # pdb mucks up readline, fix it back |
|
1227 | 1227 | self.readline.set_completer(self.Completer.complete) |
|
1228 | 1228 | |
|
1229 | 1229 | def mainloop(self,banner=None): |
|
1230 | 1230 | """Creates the local namespace and starts the mainloop. |
|
1231 | 1231 | |
|
1232 | 1232 | If an optional banner argument is given, it will override the |
|
1233 | 1233 | internally created default banner.""" |
|
1234 | 1234 | |
|
1235 | 1235 | if self.rc.c: # Emulate Python's -c option |
|
1236 | 1236 | self.exec_init_cmd() |
|
1237 | 1237 | if banner is None: |
|
1238 | 1238 | if self.rc.banner: |
|
1239 | 1239 | banner = self.BANNER+self.banner2 |
|
1240 | 1240 | else: |
|
1241 | 1241 | banner = '' |
|
1242 | 1242 | self.interact(banner) |
|
1243 | 1243 | |
|
1244 | 1244 | def exec_init_cmd(self): |
|
1245 | 1245 | """Execute a command given at the command line. |
|
1246 | 1246 | |
|
1247 | 1247 | This emulates Python's -c option.""" |
|
1248 | 1248 | |
|
1249 | 1249 | sys.argv = ['-c'] |
|
1250 | 1250 | self.push(self.rc.c) |
|
1251 | 1251 | |
|
1252 | 1252 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1253 | 1253 | """Embeds IPython into a running python program. |
|
1254 | 1254 | |
|
1255 | 1255 | Input: |
|
1256 | 1256 | |
|
1257 | 1257 | - header: An optional header message can be specified. |
|
1258 | 1258 | |
|
1259 | 1259 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1260 | 1260 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1261 | 1261 | program variables become visible but user-specific configuration |
|
1262 | 1262 | remains possible. |
|
1263 | 1263 | |
|
1264 | 1264 | - stack_depth: specifies how many levels in the stack to go to |
|
1265 | 1265 | looking for namespaces (when local_ns and global_ns are None). This |
|
1266 | 1266 | allows an intermediate caller to make sure that this function gets |
|
1267 | 1267 | the namespace from the intended level in the stack. By default (0) |
|
1268 | 1268 | it will get its locals and globals from the immediate caller. |
|
1269 | 1269 | |
|
1270 | 1270 | Warning: it's possible to use this in a program which is being run by |
|
1271 | 1271 | IPython itself (via %run), but some funny things will happen (a few |
|
1272 | 1272 | globals get overwritten). In the future this will be cleaned up, as |
|
1273 | 1273 | there is no fundamental reason why it can't work perfectly.""" |
|
1274 | 1274 | |
|
1275 | 1275 | # Get locals and globals from caller |
|
1276 | 1276 | if local_ns is None or global_ns is None: |
|
1277 | 1277 | call_frame = sys._getframe(stack_depth).f_back |
|
1278 | 1278 | |
|
1279 | 1279 | if local_ns is None: |
|
1280 | 1280 | local_ns = call_frame.f_locals |
|
1281 | 1281 | if global_ns is None: |
|
1282 | 1282 | global_ns = call_frame.f_globals |
|
1283 | 1283 | |
|
1284 | 1284 | # Update namespaces and fire up interpreter |
|
1285 | 1285 | self.user_ns = local_ns |
|
1286 | 1286 | self.user_global_ns = global_ns |
|
1287 | 1287 | |
|
1288 | 1288 | # Patch for global embedding to make sure that things don't overwrite |
|
1289 | 1289 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1290 | 1290 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1291 | 1291 | if local_ns is None and global_ns is None: |
|
1292 | 1292 | self.user_global_ns.update(__main__.__dict__) |
|
1293 | 1293 | |
|
1294 | 1294 | # make sure the tab-completer has the correct frame information, so it |
|
1295 | 1295 | # actually completes using the frame's locals/globals |
|
1296 | 1296 | self.set_completer_frame(call_frame) |
|
1297 | 1297 | |
|
1298 | 1298 | self.interact(header) |
|
1299 | 1299 | |
|
1300 | 1300 | def interact(self, banner=None): |
|
1301 | 1301 | """Closely emulate the interactive Python console. |
|
1302 | 1302 | |
|
1303 | 1303 | The optional banner argument specify the banner to print |
|
1304 | 1304 | before the first interaction; by default it prints a banner |
|
1305 | 1305 | similar to the one printed by the real Python interpreter, |
|
1306 | 1306 | followed by the current class name in parentheses (so as not |
|
1307 | 1307 | to confuse this with the real interpreter -- since it's so |
|
1308 | 1308 | close!). |
|
1309 | 1309 | |
|
1310 | 1310 | """ |
|
1311 | 1311 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1312 | 1312 | if banner is None: |
|
1313 | 1313 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1314 | 1314 | (sys.version, sys.platform, cprt, |
|
1315 | 1315 | self.__class__.__name__)) |
|
1316 | 1316 | else: |
|
1317 | 1317 | self.write(banner) |
|
1318 | 1318 | |
|
1319 | 1319 | more = 0 |
|
1320 | 1320 | |
|
1321 | 1321 | # Mark activity in the builtins |
|
1322 | 1322 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1323 | 1323 | |
|
1324 | 1324 | # exit_now is set by a call to %Exit or %Quit |
|
1325 | 1325 | while not self.exit_now: |
|
1326 | 1326 | try: |
|
1327 | 1327 | if more: |
|
1328 | 1328 | prompt = self.outputcache.prompt2 |
|
1329 | 1329 | if self.autoindent: |
|
1330 | 1330 | self.readline_startup_hook(self.pre_readline) |
|
1331 | 1331 | else: |
|
1332 | 1332 | prompt = self.outputcache.prompt1 |
|
1333 | 1333 | try: |
|
1334 | 1334 | line = self.raw_input(prompt,more) |
|
1335 | 1335 | if self.autoindent: |
|
1336 | 1336 | self.readline_startup_hook(None) |
|
1337 | 1337 | except EOFError: |
|
1338 | 1338 | if self.autoindent: |
|
1339 | 1339 | self.readline_startup_hook(None) |
|
1340 | 1340 | self.write("\n") |
|
1341 | 1341 | self.exit() |
|
1342 | 1342 | else: |
|
1343 | 1343 | more = self.push(line) |
|
1344 | 1344 | |
|
1345 | 1345 | if (self.SyntaxTB.last_syntax_error and |
|
1346 | 1346 | self.rc.autoedit_syntax): |
|
1347 | 1347 | self.edit_syntax_error() |
|
1348 | 1348 | |
|
1349 | 1349 | except KeyboardInterrupt: |
|
1350 | 1350 | self.write("\nKeyboardInterrupt\n") |
|
1351 | 1351 | self.resetbuffer() |
|
1352 | 1352 | more = 0 |
|
1353 | 1353 | # keep cache in sync with the prompt counter: |
|
1354 | 1354 | self.outputcache.prompt_count -= 1 |
|
1355 | 1355 | |
|
1356 | 1356 | if self.autoindent: |
|
1357 | 1357 | self.indent_current_nsp = 0 |
|
1358 | 1358 | self.indent_current = ' '* self.indent_current_nsp |
|
1359 | 1359 | |
|
1360 | 1360 | except bdb.BdbQuit: |
|
1361 | 1361 | warn("The Python debugger has exited with a BdbQuit exception.\n" |
|
1362 | 1362 | "Because of how pdb handles the stack, it is impossible\n" |
|
1363 | 1363 | "for IPython to properly format this particular exception.\n" |
|
1364 | 1364 | "IPython will resume normal operation.") |
|
1365 | 1365 | |
|
1366 | 1366 | # We are off again... |
|
1367 | 1367 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1368 | 1368 | |
|
1369 | 1369 | def excepthook(self, type, value, tb): |
|
1370 | 1370 | """One more defense for GUI apps that call sys.excepthook. |
|
1371 | 1371 | |
|
1372 | 1372 | GUI frameworks like wxPython trap exceptions and call |
|
1373 | 1373 | sys.excepthook themselves. I guess this is a feature that |
|
1374 | 1374 | enables them to keep running after exceptions that would |
|
1375 | 1375 | otherwise kill their mainloop. This is a bother for IPython |
|
1376 | 1376 | which excepts to catch all of the program exceptions with a try: |
|
1377 | 1377 | except: statement. |
|
1378 | 1378 | |
|
1379 | 1379 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1380 | 1380 | any app directly invokes sys.excepthook, it will look to the user like |
|
1381 | 1381 | IPython crashed. In order to work around this, we can disable the |
|
1382 | 1382 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1383 | 1383 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1384 | 1384 | call sys.excepthook will generate a regular-looking exception from |
|
1385 | 1385 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1386 | 1386 | crashes. |
|
1387 | 1387 | |
|
1388 | 1388 | This hook should be used sparingly, only in places which are not likely |
|
1389 | 1389 | to be true IPython errors. |
|
1390 | 1390 | """ |
|
1391 | 1391 | |
|
1392 | 1392 | self.InteractiveTB(type, value, tb, tb_offset=0) |
|
1393 | 1393 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1394 | 1394 | self.readline.set_completer(self.Completer.complete) |
|
1395 | 1395 | |
|
1396 | 1396 | def call_alias(self,alias,rest=''): |
|
1397 | 1397 | """Call an alias given its name and the rest of the line. |
|
1398 | 1398 | |
|
1399 | 1399 | This function MUST be given a proper alias, because it doesn't make |
|
1400 | 1400 | any checks when looking up into the alias table. The caller is |
|
1401 | 1401 | responsible for invoking it only with a valid alias.""" |
|
1402 | 1402 | |
|
1403 | 1403 | #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg |
|
1404 | 1404 | nargs,cmd = self.alias_table[alias] |
|
1405 | 1405 | # Expand the %l special to be the user's input line |
|
1406 | 1406 | if cmd.find('%l') >= 0: |
|
1407 | 1407 | cmd = cmd.replace('%l',rest) |
|
1408 | 1408 | rest = '' |
|
1409 | 1409 | if nargs==0: |
|
1410 | 1410 | # Simple, argument-less aliases |
|
1411 | 1411 | cmd = '%s %s' % (cmd,rest) |
|
1412 | 1412 | else: |
|
1413 | 1413 | # Handle aliases with positional arguments |
|
1414 | 1414 | args = rest.split(None,nargs) |
|
1415 | 1415 | if len(args)< nargs: |
|
1416 | 1416 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1417 | 1417 | (alias,nargs,len(args))) |
|
1418 | 1418 | return |
|
1419 | 1419 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1420 | 1420 | # Now call the macro, evaluating in the user's namespace |
|
1421 | 1421 | try: |
|
1422 | 1422 | self.system(cmd) |
|
1423 | 1423 | except: |
|
1424 | 1424 | self.showtraceback() |
|
1425 | 1425 | |
|
1426 | 1426 | def autoindent_update(self,line): |
|
1427 | 1427 | """Keep track of the indent level.""" |
|
1428 | 1428 | if self.autoindent: |
|
1429 | 1429 | if line: |
|
1430 | 1430 | ini_spaces = ini_spaces_re.match(line) |
|
1431 | 1431 | if ini_spaces: |
|
1432 | 1432 | nspaces = ini_spaces.end() |
|
1433 | 1433 | else: |
|
1434 | 1434 | nspaces = 0 |
|
1435 | 1435 | self.indent_current_nsp = nspaces |
|
1436 | 1436 | |
|
1437 | 1437 | if line[-1] == ':': |
|
1438 | 1438 | self.indent_current_nsp += 4 |
|
1439 | 1439 | elif dedent_re.match(line): |
|
1440 | 1440 | self.indent_current_nsp -= 4 |
|
1441 | 1441 | else: |
|
1442 | 1442 | self.indent_current_nsp = 0 |
|
1443 | 1443 | |
|
1444 | 1444 | # indent_current is the actual string to be inserted |
|
1445 | 1445 | # by the readline hooks for indentation |
|
1446 | 1446 | self.indent_current = ' '* self.indent_current_nsp |
|
1447 | 1447 | |
|
1448 | 1448 | def runlines(self,lines): |
|
1449 | 1449 | """Run a string of one or more lines of source. |
|
1450 | 1450 | |
|
1451 | 1451 | This method is capable of running a string containing multiple source |
|
1452 | 1452 | lines, as if they had been entered at the IPython prompt. Since it |
|
1453 | 1453 | exposes IPython's processing machinery, the given strings can contain |
|
1454 | 1454 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1455 | 1455 | |
|
1456 | 1456 | # We must start with a clean buffer, in case this is run from an |
|
1457 | 1457 | # interactive IPython session (via a magic, for example). |
|
1458 | 1458 | self.resetbuffer() |
|
1459 | 1459 | lines = lines.split('\n') |
|
1460 | 1460 | more = 0 |
|
1461 | 1461 | for line in lines: |
|
1462 | 1462 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1463 | 1463 | # NOT skip even a blank line if we are in a code block (more is |
|
1464 | 1464 | # true) |
|
1465 | 1465 | if line or more: |
|
1466 | 1466 | more = self.push(self.prefilter(line,more)) |
|
1467 | 1467 | # IPython's runsource returns None if there was an error |
|
1468 | 1468 | # compiling the code. This allows us to stop processing right |
|
1469 | 1469 | # away, so the user gets the error message at the right place. |
|
1470 | 1470 | if more is None: |
|
1471 | 1471 | break |
|
1472 | 1472 | # final newline in case the input didn't have it, so that the code |
|
1473 | 1473 | # actually does get executed |
|
1474 | 1474 | if more: |
|
1475 | 1475 | self.push('\n') |
|
1476 | 1476 | |
|
1477 | 1477 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1478 | 1478 | """Compile and run some source in the interpreter. |
|
1479 | 1479 | |
|
1480 | 1480 | Arguments are as for compile_command(). |
|
1481 | 1481 | |
|
1482 | 1482 | One several things can happen: |
|
1483 | 1483 | |
|
1484 | 1484 | 1) The input is incorrect; compile_command() raised an |
|
1485 | 1485 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1486 | 1486 | will be printed by calling the showsyntaxerror() method. |
|
1487 | 1487 | |
|
1488 | 1488 | 2) The input is incomplete, and more input is required; |
|
1489 | 1489 | compile_command() returned None. Nothing happens. |
|
1490 | 1490 | |
|
1491 | 1491 | 3) The input is complete; compile_command() returned a code |
|
1492 | 1492 | object. The code is executed by calling self.runcode() (which |
|
1493 | 1493 | also handles run-time exceptions, except for SystemExit). |
|
1494 | 1494 | |
|
1495 | 1495 | The return value is: |
|
1496 | 1496 | |
|
1497 | 1497 | - True in case 2 |
|
1498 | 1498 | |
|
1499 | 1499 | - False in the other cases, unless an exception is raised, where |
|
1500 | 1500 | None is returned instead. This can be used by external callers to |
|
1501 | 1501 | know whether to continue feeding input or not. |
|
1502 | 1502 | |
|
1503 | 1503 | The return value can be used to decide whether to use sys.ps1 or |
|
1504 | 1504 | sys.ps2 to prompt the next line.""" |
|
1505 | 1505 | |
|
1506 | 1506 | try: |
|
1507 | 1507 | code = self.compile(source,filename,symbol) |
|
1508 | 1508 | except (OverflowError, SyntaxError, ValueError): |
|
1509 | 1509 | # Case 1 |
|
1510 | 1510 | self.showsyntaxerror(filename) |
|
1511 | 1511 | return None |
|
1512 | 1512 | |
|
1513 | 1513 | if code is None: |
|
1514 | 1514 | # Case 2 |
|
1515 | 1515 | return True |
|
1516 | 1516 | |
|
1517 | 1517 | # Case 3 |
|
1518 | 1518 | # We store the code object so that threaded shells and |
|
1519 | 1519 | # custom exception handlers can access all this info if needed. |
|
1520 | 1520 | # The source corresponding to this can be obtained from the |
|
1521 | 1521 | # buffer attribute as '\n'.join(self.buffer). |
|
1522 | 1522 | self.code_to_run = code |
|
1523 | 1523 | # now actually execute the code object |
|
1524 | 1524 | if self.runcode(code) == 0: |
|
1525 | 1525 | return False |
|
1526 | 1526 | else: |
|
1527 | 1527 | return None |
|
1528 | 1528 | |
|
1529 | 1529 | def runcode(self,code_obj): |
|
1530 | 1530 | """Execute a code object. |
|
1531 | 1531 | |
|
1532 | 1532 | When an exception occurs, self.showtraceback() is called to display a |
|
1533 | 1533 | traceback. |
|
1534 | 1534 | |
|
1535 | 1535 | Return value: a flag indicating whether the code to be run completed |
|
1536 | 1536 | successfully: |
|
1537 | 1537 | |
|
1538 | 1538 | - 0: successful execution. |
|
1539 | 1539 | - 1: an error occurred. |
|
1540 | 1540 | """ |
|
1541 | 1541 | |
|
1542 | 1542 | # Set our own excepthook in case the user code tries to call it |
|
1543 | 1543 | # directly, so that the IPython crash handler doesn't get triggered |
|
1544 | 1544 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1545 | 1545 | |
|
1546 | 1546 | # we save the original sys.excepthook in the instance, in case config |
|
1547 | 1547 | # code (such as magics) needs access to it. |
|
1548 | 1548 | self.sys_excepthook = old_excepthook |
|
1549 | 1549 | outflag = 1 # happens in more places, so it's easier as default |
|
1550 | 1550 | try: |
|
1551 | 1551 | try: |
|
1552 | 1552 | # Embedded instances require separate global/local namespaces |
|
1553 | 1553 | # so they can see both the surrounding (local) namespace and |
|
1554 | 1554 | # the module-level globals when called inside another function. |
|
1555 | 1555 | if self.embedded: |
|
1556 | 1556 | exec code_obj in self.user_global_ns, self.user_ns |
|
1557 | 1557 | # Normal (non-embedded) instances should only have a single |
|
1558 | 1558 | # namespace for user code execution, otherwise functions won't |
|
1559 | 1559 | # see interactive top-level globals. |
|
1560 | 1560 | else: |
|
1561 | 1561 | exec code_obj in self.user_ns |
|
1562 | 1562 | finally: |
|
1563 | 1563 | # Reset our crash handler in place |
|
1564 | 1564 | sys.excepthook = old_excepthook |
|
1565 | 1565 | except SystemExit: |
|
1566 | 1566 | self.resetbuffer() |
|
1567 | 1567 | self.showtraceback() |
|
1568 | 1568 | warn("Type exit or quit to exit IPython " |
|
1569 | 1569 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1570 | 1570 | except self.custom_exceptions: |
|
1571 | 1571 | etype,value,tb = sys.exc_info() |
|
1572 | 1572 | self.CustomTB(etype,value,tb) |
|
1573 | 1573 | except: |
|
1574 | 1574 | self.showtraceback() |
|
1575 | 1575 | else: |
|
1576 | 1576 | outflag = 0 |
|
1577 | 1577 | if softspace(sys.stdout, 0): |
|
1578 | 1578 | |
|
1579 | 1579 | # Flush out code object which has been run (and source) |
|
1580 | 1580 | self.code_to_run = None |
|
1581 | 1581 | return outflag |
|
1582 | 1582 | |
|
1583 | 1583 | def push(self, line): |
|
1584 | 1584 | """Push a line to the interpreter. |
|
1585 | 1585 | |
|
1586 | 1586 | The line should not have a trailing newline; it may have |
|
1587 | 1587 | internal newlines. The line is appended to a buffer and the |
|
1588 | 1588 | interpreter's runsource() method is called with the |
|
1589 | 1589 | concatenated contents of the buffer as source. If this |
|
1590 | 1590 | indicates that the command was executed or invalid, the buffer |
|
1591 | 1591 | is reset; otherwise, the command is incomplete, and the buffer |
|
1592 | 1592 | is left as it was after the line was appended. The return |
|
1593 | 1593 | value is 1 if more input is required, 0 if the line was dealt |
|
1594 | 1594 | with in some way (this is the same as runsource()). |
|
1595 | 1595 | """ |
|
1596 | 1596 | |
|
1597 | 1597 | # autoindent management should be done here, and not in the |
|
1598 | 1598 | # interactive loop, since that one is only seen by keyboard input. We |
|
1599 | 1599 | # need this done correctly even for code run via runlines (which uses |
|
1600 | 1600 | # push). |
|
1601 | ||
|
1602 | print 'push line: <%s>' % line # dbg | |
|
1601 | 1603 | self.autoindent_update(line) |
|
1602 | 1604 | |
|
1603 | 1605 | self.buffer.append(line) |
|
1604 | 1606 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1605 | 1607 | if not more: |
|
1606 | 1608 | self.resetbuffer() |
|
1607 | 1609 | return more |
|
1608 | 1610 | |
|
1609 | 1611 | def resetbuffer(self): |
|
1610 | 1612 | """Reset the input buffer.""" |
|
1611 | 1613 | self.buffer[:] = [] |
|
1612 | 1614 | |
|
1613 | 1615 | def raw_input(self,prompt='',continue_prompt=False): |
|
1614 | 1616 | """Write a prompt and read a line. |
|
1615 | 1617 | |
|
1616 | 1618 | The returned line does not include the trailing newline. |
|
1617 | 1619 | When the user enters the EOF key sequence, EOFError is raised. |
|
1618 | 1620 | |
|
1619 | 1621 | Optional inputs: |
|
1620 | 1622 | |
|
1621 | 1623 | - prompt(''): a string to be printed to prompt the user. |
|
1622 | 1624 | |
|
1623 | 1625 | - continue_prompt(False): whether this line is the first one or a |
|
1624 | 1626 | continuation in a sequence of inputs. |
|
1625 | 1627 | """ |
|
1626 | 1628 | |
|
1627 | 1629 | line = raw_input_original(prompt) |
|
1628 | 1630 | # Try to be reasonably smart about not re-indenting pasted input more |
|
1629 | 1631 | # than necessary. We do this by trimming out the auto-indent initial |
|
1630 | 1632 | # spaces, if the user's actual input started itself with whitespace. |
|
1631 | 1633 | if self.autoindent: |
|
1632 | 1634 | line2 = line[self.indent_current_nsp:] |
|
1633 | 1635 | if line2[0:1] in (' ','\t'): |
|
1634 | 1636 | line = line2 |
|
1635 | 1637 | return self.prefilter(line,continue_prompt) |
|
1636 | 1638 | |
|
1637 | 1639 | def split_user_input(self,line): |
|
1638 | 1640 | """Split user input into pre-char, function part and rest.""" |
|
1639 | 1641 | |
|
1640 | 1642 | lsplit = self.line_split.match(line) |
|
1641 | 1643 | if lsplit is None: # no regexp match returns None |
|
1642 | 1644 | try: |
|
1643 | 1645 | iFun,theRest = line.split(None,1) |
|
1644 | 1646 | except ValueError: |
|
1645 | 1647 | iFun,theRest = line,'' |
|
1646 | 1648 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1647 | 1649 | else: |
|
1648 | 1650 | pre,iFun,theRest = lsplit.groups() |
|
1649 | 1651 | |
|
1650 | 1652 | #print 'line:<%s>' % line # dbg |
|
1651 | 1653 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1652 | 1654 | return pre,iFun.strip(),theRest |
|
1653 | 1655 | |
|
1654 | 1656 | def _prefilter(self, line, continue_prompt): |
|
1655 | 1657 | """Calls different preprocessors, depending on the form of line.""" |
|
1656 | 1658 | |
|
1657 | 1659 | # All handlers *must* return a value, even if it's blank (''). |
|
1658 | 1660 | |
|
1659 | 1661 | # Lines are NOT logged here. Handlers should process the line as |
|
1660 | 1662 | # needed, update the cache AND log it (so that the input cache array |
|
1661 | 1663 | # stays synced). |
|
1662 | 1664 | |
|
1663 | 1665 | # This function is _very_ delicate, and since it's also the one which |
|
1664 | 1666 | # determines IPython's response to user input, it must be as efficient |
|
1665 | 1667 | # as possible. For this reason it has _many_ returns in it, trying |
|
1666 | 1668 | # always to exit as quickly as it can figure out what it needs to do. |
|
1667 | 1669 | |
|
1668 | 1670 | # This function is the main responsible for maintaining IPython's |
|
1669 | 1671 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1670 | 1672 | # making changes to anything here. |
|
1671 | 1673 | |
|
1672 | 1674 | #..................................................................... |
|
1673 | 1675 | # Code begins |
|
1674 | 1676 | |
|
1675 | 1677 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1676 | 1678 | |
|
1677 | 1679 | # save the line away in case we crash, so the post-mortem handler can |
|
1678 | 1680 | # record it |
|
1679 | 1681 | self._last_input_line = line |
|
1680 | 1682 | |
|
1681 | 1683 | #print '***line: <%s>' % line # dbg |
|
1682 | 1684 | |
|
1683 | 1685 | # the input history needs to track even empty lines |
|
1684 | 1686 | if not line.strip(): |
|
1685 | 1687 | if not continue_prompt: |
|
1686 | 1688 | self.outputcache.prompt_count -= 1 |
|
1687 | 1689 | return self.handle_normal(line,continue_prompt) |
|
1688 | 1690 | #return self.handle_normal('',continue_prompt) |
|
1689 | 1691 | |
|
1690 | 1692 | # print '***cont',continue_prompt # dbg |
|
1691 | 1693 | # special handlers are only allowed for single line statements |
|
1692 | 1694 | if continue_prompt and not self.rc.multi_line_specials: |
|
1693 | 1695 | return self.handle_normal(line,continue_prompt) |
|
1694 | 1696 | |
|
1695 | 1697 | # For the rest, we need the structure of the input |
|
1696 | 1698 | pre,iFun,theRest = self.split_user_input(line) |
|
1697 | 1699 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1698 | 1700 | |
|
1699 | 1701 | # First check for explicit escapes in the last/first character |
|
1700 | 1702 | handler = None |
|
1701 | 1703 | if line[-1] == self.ESC_HELP: |
|
1702 | 1704 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
1703 | 1705 | if handler is None: |
|
1704 | 1706 | # look at the first character of iFun, NOT of line, so we skip |
|
1705 | 1707 | # leading whitespace in multiline input |
|
1706 | 1708 | handler = self.esc_handlers.get(iFun[0:1]) |
|
1707 | 1709 | if handler is not None: |
|
1708 | 1710 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
1709 | 1711 | # Emacs ipython-mode tags certain input lines |
|
1710 | 1712 | if line.endswith('# PYTHON-MODE'): |
|
1711 | 1713 | return self.handle_emacs(line,continue_prompt) |
|
1712 | 1714 | |
|
1713 | 1715 | # Next, check if we can automatically execute this thing |
|
1714 | 1716 | |
|
1715 | 1717 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
1716 | 1718 | if continue_prompt and self.rc.multi_line_specials and \ |
|
1717 | 1719 | iFun.startswith(self.ESC_SHELL): |
|
1718 | 1720 | return self.handle_shell_escape(line,continue_prompt, |
|
1719 | 1721 | pre=pre,iFun=iFun, |
|
1720 | 1722 | theRest=theRest) |
|
1721 | 1723 | |
|
1722 | 1724 | # Let's try to find if the input line is a magic fn |
|
1723 | 1725 | oinfo = None |
|
1724 | 1726 | if hasattr(self,'magic_'+iFun): |
|
1725 | 1727 | # WARNING: _ofind uses getattr(), so it can consume generators and |
|
1726 | 1728 | # cause other side effects. |
|
1727 | 1729 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1728 | 1730 | if oinfo['ismagic']: |
|
1729 | 1731 | # Be careful not to call magics when a variable assignment is |
|
1730 | 1732 | # being made (ls='hi', for example) |
|
1731 | 1733 | if self.rc.automagic and \ |
|
1732 | 1734 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
1733 | 1735 | (self.rc.multi_line_specials or not continue_prompt): |
|
1734 | 1736 | return self.handle_magic(line,continue_prompt, |
|
1735 | 1737 | pre,iFun,theRest) |
|
1736 | 1738 | else: |
|
1737 | 1739 | return self.handle_normal(line,continue_prompt) |
|
1738 | 1740 | |
|
1739 | 1741 | # If the rest of the line begins with an (in)equality, assginment or |
|
1740 | 1742 | # function call, we should not call _ofind but simply execute it. |
|
1741 | 1743 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
1742 | 1744 | # |
|
1743 | 1745 | # It also allows users to assign to either alias or magic names true |
|
1744 | 1746 | # python variables (the magic/alias systems always take second seat to |
|
1745 | 1747 | # true python code). |
|
1746 | 1748 | if theRest and theRest[0] in '!=()': |
|
1747 | 1749 | return self.handle_normal(line,continue_prompt) |
|
1748 | 1750 | |
|
1749 | 1751 | if oinfo is None: |
|
1750 | 1752 | # let's try to ensure that _oinfo is ONLY called when autocall is |
|
1751 | 1753 | # on. Since it has inevitable potential side effects, at least |
|
1752 | 1754 | # having autocall off should be a guarantee to the user that no |
|
1753 | 1755 | # weird things will happen. |
|
1754 | 1756 | |
|
1755 | 1757 | if self.rc.autocall: |
|
1756 | 1758 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1757 | 1759 | else: |
|
1758 | 1760 | # in this case, all that's left is either an alias or |
|
1759 | 1761 | # processing the line normally. |
|
1760 | 1762 | if iFun in self.alias_table: |
|
1761 | 1763 | return self.handle_alias(line,continue_prompt, |
|
1762 | 1764 | pre,iFun,theRest) |
|
1763 | 1765 | else: |
|
1764 | 1766 | return self.handle_normal(line,continue_prompt) |
|
1765 | 1767 | |
|
1766 | 1768 | if not oinfo['found']: |
|
1767 | 1769 | return self.handle_normal(line,continue_prompt) |
|
1768 | 1770 | else: |
|
1769 | 1771 | #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg |
|
1770 | 1772 | if oinfo['isalias']: |
|
1771 | 1773 | return self.handle_alias(line,continue_prompt, |
|
1772 | 1774 | pre,iFun,theRest) |
|
1773 | 1775 | |
|
1774 | 1776 | if self.rc.autocall and \ |
|
1775 | 1777 | not self.re_exclude_auto.match(theRest) and \ |
|
1776 | 1778 | self.re_fun_name.match(iFun) and \ |
|
1777 | 1779 | callable(oinfo['obj']) : |
|
1778 | 1780 | #print 'going auto' # dbg |
|
1779 | 1781 | return self.handle_auto(line,continue_prompt,pre,iFun,theRest) |
|
1780 | 1782 | else: |
|
1781 | 1783 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
1782 | 1784 | return self.handle_normal(line,continue_prompt) |
|
1783 | 1785 | |
|
1784 | 1786 | # If we get here, we have a normal Python line. Log and return. |
|
1785 | 1787 | return self.handle_normal(line,continue_prompt) |
|
1786 | 1788 | |
|
1787 | 1789 | def _prefilter_dumb(self, line, continue_prompt): |
|
1788 | 1790 | """simple prefilter function, for debugging""" |
|
1789 | 1791 | return self.handle_normal(line,continue_prompt) |
|
1790 | 1792 | |
|
1791 | 1793 | # Set the default prefilter() function (this can be user-overridden) |
|
1792 | 1794 | prefilter = _prefilter |
|
1793 | 1795 | |
|
1794 | 1796 | def handle_normal(self,line,continue_prompt=None, |
|
1795 | 1797 | pre=None,iFun=None,theRest=None): |
|
1796 | 1798 | """Handle normal input lines. Use as a template for handlers.""" |
|
1797 | 1799 | |
|
1798 | 1800 | # With autoindent on, we need some way to exit the input loop, and I |
|
1799 | 1801 | # don't want to force the user to have to backspace all the way to |
|
1800 | 1802 | # clear the line. The rule will be in this case, that either two |
|
1801 | 1803 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
1802 | 1804 | # of a size different to the indent level, will exit the input loop. |
|
1803 | 1805 | |
|
1804 | 1806 | if (continue_prompt and self.autoindent and isspace(line) and |
|
1805 | 1807 | (line != self.indent_current or isspace(self.buffer[-1]))): |
|
1806 | 1808 | line = '' |
|
1807 | 1809 | |
|
1808 | 1810 | self.log(line,continue_prompt) |
|
1809 | 1811 | return line |
|
1810 | 1812 | |
|
1811 | 1813 | def handle_alias(self,line,continue_prompt=None, |
|
1812 | 1814 | pre=None,iFun=None,theRest=None): |
|
1813 | 1815 | """Handle alias input lines. """ |
|
1814 | 1816 | |
|
1815 | line_out = 'ipalias("%s %s")' % (iFun,esc_quotes(theRest)) | |
|
1817 | # pre is needed, because it carries the leading whitespace. Otherwise | |
|
1818 | # aliases won't work in indented sections. | |
|
1819 | line_out = '%sipalias("%s %s")' % (pre,iFun,esc_quotes(theRest)) | |
|
1816 | 1820 | self.log(line_out,continue_prompt) |
|
1817 | 1821 | return line_out |
|
1818 | 1822 | |
|
1819 | 1823 | def handle_shell_escape(self, line, continue_prompt=None, |
|
1820 | 1824 | pre=None,iFun=None,theRest=None): |
|
1821 | 1825 | """Execute the line in a shell, empty return value""" |
|
1822 | 1826 | |
|
1823 | 1827 | #print 'line in :', `line` # dbg |
|
1824 | 1828 | # Example of a special handler. Others follow a similar pattern. |
|
1825 | 1829 | if continue_prompt: # multi-line statements |
|
1826 | 1830 | if iFun.startswith('!!'): |
|
1827 | 1831 | print 'SyntaxError: !! is not allowed in multiline statements' |
|
1828 | 1832 | return pre |
|
1829 | 1833 | else: |
|
1830 | 1834 | cmd = ("%s %s" % (iFun[1:],theRest)) |
|
1831 | line_out = 'ipsystem(r"""%s"""[:-1])' % (cmd + "_") | |
|
1835 | line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd + "_") | |
|
1832 | 1836 | else: # single-line input |
|
1833 | 1837 | if line.startswith('!!'): |
|
1834 | 1838 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
1835 | 1839 | # the actual command to be executed, so handle_magic can work |
|
1836 | 1840 | # correctly |
|
1837 | 1841 | theRest = '%s %s' % (iFun[2:],theRest) |
|
1838 | 1842 | iFun = 'sx' |
|
1839 | 1843 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), |
|
1840 | 1844 | continue_prompt,pre,iFun,theRest) |
|
1841 | 1845 | else: |
|
1842 | 1846 | cmd=line[1:] |
|
1843 | line_out = 'ipsystem(r"""%s"""[:-1])' % (cmd +"_") | |
|
1847 | line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd +"_") | |
|
1844 | 1848 | # update cache/log and return |
|
1845 | 1849 | self.log(line_out,continue_prompt) |
|
1846 | 1850 | return line_out |
|
1847 | 1851 | |
|
1848 | 1852 | def handle_magic(self, line, continue_prompt=None, |
|
1849 | 1853 | pre=None,iFun=None,theRest=None): |
|
1850 | 1854 | """Execute magic functions. |
|
1851 | 1855 | |
|
1852 | 1856 | Also log them with a prepended # so the log is clean Python.""" |
|
1853 | 1857 | |
|
1854 | 1858 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) |
|
1855 | 1859 | self.log(cmd,continue_prompt) |
|
1856 | 1860 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
1857 | 1861 | return cmd |
|
1858 | 1862 | |
|
1859 | 1863 | def handle_auto(self, line, continue_prompt=None, |
|
1860 | 1864 | pre=None,iFun=None,theRest=None): |
|
1861 | 1865 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
1862 | 1866 | |
|
1863 | 1867 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1864 | 1868 | |
|
1865 | 1869 | # This should only be active for single-line input! |
|
1866 | 1870 | if continue_prompt: |
|
1867 | 1871 | return line |
|
1868 | 1872 | |
|
1869 | 1873 | if pre == self.ESC_QUOTE: |
|
1870 | 1874 | # Auto-quote splitting on whitespace |
|
1871 | 1875 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
1872 | 1876 | elif pre == self.ESC_QUOTE2: |
|
1873 | 1877 | # Auto-quote whole string |
|
1874 | 1878 | newcmd = '%s("%s")' % (iFun,theRest) |
|
1875 | 1879 | else: |
|
1876 | 1880 | # Auto-paren |
|
1877 | 1881 | if theRest[0:1] in ('=','['): |
|
1878 | 1882 | # Don't autocall in these cases. They can be either |
|
1879 | 1883 | # rebindings of an existing callable's name, or item access |
|
1880 | 1884 | # for an object which is BOTH callable and implements |
|
1881 | 1885 | # __getitem__. |
|
1882 | 1886 | return '%s %s' % (iFun,theRest) |
|
1883 | 1887 | if theRest.endswith(';'): |
|
1884 | 1888 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
1885 | 1889 | else: |
|
1886 | 1890 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
1887 | 1891 | |
|
1888 | 1892 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
1889 | 1893 | # log what is now valid Python, not the actual user input (without the |
|
1890 | 1894 | # final newline) |
|
1891 | 1895 | self.log(newcmd,continue_prompt) |
|
1892 | 1896 | return newcmd |
|
1893 | 1897 | |
|
1894 | 1898 | def handle_help(self, line, continue_prompt=None, |
|
1895 | 1899 | pre=None,iFun=None,theRest=None): |
|
1896 | 1900 | """Try to get some help for the object. |
|
1897 | 1901 | |
|
1898 | 1902 | obj? or ?obj -> basic information. |
|
1899 | 1903 | obj?? or ??obj -> more details. |
|
1900 | 1904 | """ |
|
1901 | 1905 | |
|
1902 | 1906 | # We need to make sure that we don't process lines which would be |
|
1903 | 1907 | # otherwise valid python, such as "x=1 # what?" |
|
1904 | 1908 | try: |
|
1905 | 1909 | codeop.compile_command(line) |
|
1906 | 1910 | except SyntaxError: |
|
1907 | 1911 | # We should only handle as help stuff which is NOT valid syntax |
|
1908 | 1912 | if line[0]==self.ESC_HELP: |
|
1909 | 1913 | line = line[1:] |
|
1910 | 1914 | elif line[-1]==self.ESC_HELP: |
|
1911 | 1915 | line = line[:-1] |
|
1912 | 1916 | self.log('#?'+line) |
|
1913 | 1917 | if line: |
|
1914 | 1918 | self.magic_pinfo(line) |
|
1915 | 1919 | else: |
|
1916 | 1920 | page(self.usage,screen_lines=self.rc.screen_length) |
|
1917 | 1921 | return '' # Empty string is needed here! |
|
1918 | 1922 | except: |
|
1919 | 1923 | # Pass any other exceptions through to the normal handler |
|
1920 | 1924 | return self.handle_normal(line,continue_prompt) |
|
1921 | 1925 | else: |
|
1922 | 1926 | # If the code compiles ok, we should handle it normally |
|
1923 | 1927 | return self.handle_normal(line,continue_prompt) |
|
1924 | 1928 | |
|
1925 | 1929 | def handle_emacs(self,line,continue_prompt=None, |
|
1926 | 1930 | pre=None,iFun=None,theRest=None): |
|
1927 | 1931 | """Handle input lines marked by python-mode.""" |
|
1928 | 1932 | |
|
1929 | 1933 | # Currently, nothing is done. Later more functionality can be added |
|
1930 | 1934 | # here if needed. |
|
1931 | 1935 | |
|
1932 | 1936 | # The input cache shouldn't be updated |
|
1933 | 1937 | |
|
1934 | 1938 | return line |
|
1935 | 1939 | |
|
1936 | 1940 | def write(self,data): |
|
1937 | 1941 | """Write a string to the default output""" |
|
1938 | 1942 | Term.cout.write(data) |
|
1939 | 1943 | |
|
1940 | 1944 | def write_err(self,data): |
|
1941 | 1945 | """Write a string to the default error output""" |
|
1942 | 1946 | Term.cerr.write(data) |
|
1943 | 1947 | |
|
1944 | 1948 | def exit(self): |
|
1945 | 1949 | """Handle interactive exit. |
|
1946 | 1950 | |
|
1947 | 1951 | This method sets the exit_now attribute.""" |
|
1948 | 1952 | |
|
1949 | 1953 | if self.rc.confirm_exit: |
|
1950 | 1954 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
1951 | 1955 | self.exit_now = True |
|
1952 | 1956 | else: |
|
1953 | 1957 | self.exit_now = True |
|
1954 | 1958 | return self.exit_now |
|
1955 | 1959 | |
|
1956 | 1960 | def safe_execfile(self,fname,*where,**kw): |
|
1957 | 1961 | fname = os.path.expanduser(fname) |
|
1958 | 1962 | |
|
1959 | 1963 | # find things also in current directory |
|
1960 | 1964 | dname = os.path.dirname(fname) |
|
1961 | 1965 | if not sys.path.count(dname): |
|
1962 | 1966 | sys.path.append(dname) |
|
1963 | 1967 | |
|
1964 | 1968 | try: |
|
1965 | 1969 | xfile = open(fname) |
|
1966 | 1970 | except: |
|
1967 | 1971 | print >> Term.cerr, \ |
|
1968 | 1972 | 'Could not open file <%s> for safe execution.' % fname |
|
1969 | 1973 | return None |
|
1970 | 1974 | |
|
1971 | 1975 | kw.setdefault('islog',0) |
|
1972 | 1976 | kw.setdefault('quiet',1) |
|
1973 | 1977 | kw.setdefault('exit_ignore',0) |
|
1974 | 1978 | first = xfile.readline() |
|
1975 | 1979 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
1976 | 1980 | xfile.close() |
|
1977 | 1981 | # line by line execution |
|
1978 | 1982 | if first.startswith(loghead) or kw['islog']: |
|
1979 | 1983 | print 'Loading log file <%s> one line at a time...' % fname |
|
1980 | 1984 | if kw['quiet']: |
|
1981 | 1985 | stdout_save = sys.stdout |
|
1982 | 1986 | sys.stdout = StringIO.StringIO() |
|
1983 | 1987 | try: |
|
1984 | 1988 | globs,locs = where[0:2] |
|
1985 | 1989 | except: |
|
1986 | 1990 | try: |
|
1987 | 1991 | globs = locs = where[0] |
|
1988 | 1992 | except: |
|
1989 | 1993 | globs = locs = globals() |
|
1990 | 1994 | badblocks = [] |
|
1991 | 1995 | |
|
1992 | 1996 | # we also need to identify indented blocks of code when replaying |
|
1993 | 1997 | # logs and put them together before passing them to an exec |
|
1994 | 1998 | # statement. This takes a bit of regexp and look-ahead work in the |
|
1995 | 1999 | # file. It's easiest if we swallow the whole thing in memory |
|
1996 | 2000 | # first, and manually walk through the lines list moving the |
|
1997 | 2001 | # counter ourselves. |
|
1998 | 2002 | indent_re = re.compile('\s+\S') |
|
1999 | 2003 | xfile = open(fname) |
|
2000 | 2004 | filelines = xfile.readlines() |
|
2001 | 2005 | xfile.close() |
|
2002 | 2006 | nlines = len(filelines) |
|
2003 | 2007 | lnum = 0 |
|
2004 | 2008 | while lnum < nlines: |
|
2005 | 2009 | line = filelines[lnum] |
|
2006 | 2010 | lnum += 1 |
|
2007 | 2011 | # don't re-insert logger status info into cache |
|
2008 | 2012 | if line.startswith('#log#'): |
|
2009 | 2013 | continue |
|
2010 | 2014 | else: |
|
2011 | 2015 | # build a block of code (maybe a single line) for execution |
|
2012 | 2016 | block = line |
|
2013 | 2017 | try: |
|
2014 | 2018 | next = filelines[lnum] # lnum has already incremented |
|
2015 | 2019 | except: |
|
2016 | 2020 | next = None |
|
2017 | 2021 | while next and indent_re.match(next): |
|
2018 | 2022 | block += next |
|
2019 | 2023 | lnum += 1 |
|
2020 | 2024 | try: |
|
2021 | 2025 | next = filelines[lnum] |
|
2022 | 2026 | except: |
|
2023 | 2027 | next = None |
|
2024 | 2028 | # now execute the block of one or more lines |
|
2025 | 2029 | try: |
|
2026 | 2030 | exec block in globs,locs |
|
2027 | 2031 | except SystemExit: |
|
2028 | 2032 | pass |
|
2029 | 2033 | except: |
|
2030 | 2034 | badblocks.append(block.rstrip()) |
|
2031 | 2035 | if kw['quiet']: # restore stdout |
|
2032 | 2036 | sys.stdout.close() |
|
2033 | 2037 | sys.stdout = stdout_save |
|
2034 | 2038 | print 'Finished replaying log file <%s>' % fname |
|
2035 | 2039 | if badblocks: |
|
2036 | 2040 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2037 | 2041 | '<%s> reported errors:' % fname) |
|
2038 | 2042 | |
|
2039 | 2043 | for badline in badblocks: |
|
2040 | 2044 | print >> sys.stderr, badline |
|
2041 | 2045 | else: # regular file execution |
|
2042 | 2046 | try: |
|
2043 | 2047 | execfile(fname,*where) |
|
2044 | 2048 | except SyntaxError: |
|
2045 | 2049 | etype,evalue = sys.exc_info()[:2] |
|
2046 | 2050 | self.SyntaxTB(etype,evalue,[]) |
|
2047 | 2051 | warn('Failure executing file: <%s>' % fname) |
|
2048 | 2052 | except SystemExit,status: |
|
2049 | 2053 | if not kw['exit_ignore']: |
|
2050 | 2054 | self.InteractiveTB() |
|
2051 | 2055 | warn('Failure executing file: <%s>' % fname) |
|
2052 | 2056 | except: |
|
2053 | 2057 | self.InteractiveTB() |
|
2054 | 2058 | warn('Failure executing file: <%s>' % fname) |
|
2055 | 2059 | |
|
2056 | 2060 | #************************* end of file <iplib.py> ***************************** |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,1345 +1,1358 b'' | |||
|
1 | 1 | #LyX 1.3 created this file. For more info see http://www.lyx.org/ |
|
2 | 2 | \lyxformat 221 |
|
3 | 3 | \textclass article |
|
4 | 4 | \begin_preamble |
|
5 | 5 | \usepackage{ae,aecompl} |
|
6 | 6 | \usepackage{hyperref} |
|
7 | 7 | \usepackage{html} |
|
8 | 8 | \end_preamble |
|
9 | 9 | \language english |
|
10 | 10 | \inputencoding auto |
|
11 | 11 | \fontscheme default |
|
12 | 12 | \graphics default |
|
13 | 13 | \paperfontsize default |
|
14 | 14 | \spacing single |
|
15 | 15 | \papersize Default |
|
16 | 16 | \paperpackage a4 |
|
17 | 17 | \use_geometry 1 |
|
18 | 18 | \use_amsmath 0 |
|
19 | 19 | \use_natbib 0 |
|
20 | 20 | \use_numerical_citations 0 |
|
21 | 21 | \paperorientation portrait |
|
22 | 22 | \leftmargin 1.25in |
|
23 | 23 | \topmargin 1in |
|
24 | 24 | \rightmargin 1.25in |
|
25 | 25 | \bottommargin 1in |
|
26 | 26 | \secnumdepth 3 |
|
27 | 27 | \tocdepth 3 |
|
28 | 28 | \paragraph_separation skip |
|
29 | 29 | \defskip medskip |
|
30 | 30 | \quotes_language english |
|
31 | 31 | \quotes_times 2 |
|
32 | 32 | \papercolumns 1 |
|
33 | 33 | \papersides 1 |
|
34 | 34 | \paperpagestyle default |
|
35 | 35 | |
|
36 | 36 | \layout Title |
|
37 | 37 | |
|
38 | 38 | IPython |
|
39 | 39 | \newline |
|
40 | 40 | |
|
41 | 41 | \size larger |
|
42 | 42 | New design notes |
|
43 | 43 | \layout Author |
|
44 | 44 | |
|
45 | 45 | Fernando PοΏ½rez |
|
46 | 46 | \layout Section |
|
47 | 47 | |
|
48 | 48 | Introduction |
|
49 | 49 | \layout Standard |
|
50 | 50 | |
|
51 | 51 | This is a draft document with notes and ideas for the IPython rewrite. |
|
52 | 52 | The section order and structure of this document roughly reflects in which |
|
53 | 53 | order things should be done and what the dependencies are. |
|
54 | 54 | This document is mainly a draft for developers, a pdf version is provided |
|
55 | 55 | with the standard distribution in case regular users are interested and |
|
56 | 56 | wish to contribute ideas. |
|
57 | 57 | \layout Standard |
|
58 | 58 | |
|
59 | 59 | A tentative plan for the future: |
|
60 | 60 | \layout Itemize |
|
61 | 61 | |
|
62 | 62 | 0.6.x series: in practice, enough people are using IPython for real work that |
|
63 | 63 | I think it warrants a higher number. |
|
64 | 64 | This series will continue to evolve with bugfixes and incremental improvements. |
|
65 | 65 | \layout Itemize |
|
66 | 66 | |
|
67 | 67 | 0.7.x series: (maybe) If resources allow, there may be a branch for 'unstable' |
|
68 | 68 | development, where the architectural rewrite may take place. |
|
69 | 69 | \layout Standard |
|
70 | 70 | |
|
71 | 71 | However, I am starting to doubt it is feasible to keep two separate branches. |
|
72 | 72 | I am leaning more towards a |
|
73 | 73 | \begin_inset ERT |
|
74 | 74 | status Collapsed |
|
75 | 75 | |
|
76 | 76 | \layout Standard |
|
77 | 77 | |
|
78 | 78 | \backslash |
|
79 | 79 | LyX |
|
80 | 80 | \end_inset |
|
81 | 81 | |
|
82 | 82 | -like approach, where the main branch slowly transforms and evolves. |
|
83 | 83 | Having CVS support now makes this a reasonable alternative, as I don't |
|
84 | 84 | have to make pre-releases as often. |
|
85 | 85 | The active branch can remain the mainline of development, and users interested |
|
86 | 86 | in the bleeding-edge stuff can always grab the CVS code. |
|
87 | 87 | \layout Standard |
|
88 | 88 | |
|
89 | 89 | Ideally, IPython should have a clean class setup that would allow further |
|
90 | 90 | extensions for special-purpose systems. |
|
91 | 91 | I view IPython as a base system that provides a great interactive environment |
|
92 | 92 | with full access to the Python language, and which could be used in many |
|
93 | 93 | different contexts. |
|
94 | 94 | The basic hooks are there: the magic extension syntax and the flexible |
|
95 | 95 | system of recursive configuration files and profiles. |
|
96 | 96 | But with a code as messy as the current one, nobody is going to touch it. |
|
97 | 97 | \layout Section |
|
98 | 98 | |
|
99 | 99 | Immediate TODO and bug list |
|
100 | 100 | \layout Standard |
|
101 | 101 | |
|
102 | 102 | Things that should be done for the current series, before starting major |
|
103 | 103 | changes. |
|
104 | 104 | \layout Itemize |
|
105 | 105 | |
|
106 | 106 | Fix any bugs reported at the online bug tracker. |
|
107 | 107 | \layout Itemize |
|
108 | 108 | |
|
109 | History bug: I often see that, under certain circumstances, the input history | |
|
110 | is incorrect. | |
|
111 | The problem is that so far, I've failed to find a simple way to reproduce | |
|
112 | it consistently, so I can't easily track it down. | |
|
113 | It seems to me that it happens when output is generated multiple times | |
|
114 | for the same input (for i in range(10): i will do it). | |
|
115 | But even this isn't reliable... | |
|
116 | Ultimately the right solution for this is to cleanly separate the dataflow | |
|
117 | for input/output history management; right now that happens all over the | |
|
118 | place, which makes the code impossible to debug, and almost guaranteed | |
|
119 | to be buggy in the first place. | |
|
120 | \layout Itemize | |
|
121 | ||
|
109 | 122 | |
|
110 | 123 | \series bold |
|
111 | 124 | Redesign the output traps. |
|
112 | 125 | |
|
113 | 126 | \series default |
|
114 | 127 | They cause problems when users try to execute code which relies on sys.stdout |
|
115 | 128 | being the 'true' sys.stdout. |
|
116 | 129 | They also prevent scripts which use raw_input() to work as command-line |
|
117 | 130 | arguments. |
|
118 | 131 | \newline |
|
119 | 132 | The best solution is probably to print the banner first, and then just execute |
|
120 | 133 | all the user code straight with no output traps at all. |
|
121 | 134 | Whatever comes out comes out. |
|
122 | 135 | This makes the ipython code actually simpler, and eliminates the problem |
|
123 | 136 | altogether. |
|
124 | 137 | \newline |
|
125 | 138 | These things need to be ripped out, they cause no end of problems. |
|
126 | 139 | For example, if user code requires acces to stdin during startup, the process |
|
127 | 140 | just hangs indefinitely. |
|
128 | 141 | For now I've just disabled them, and I'll live with the ugly error messages. |
|
129 | 142 | \layout Itemize |
|
130 | 143 | |
|
131 | 144 | The prompt specials dictionary should be turned into a class which does |
|
132 | 145 | proper namespace management, since the prompt specials need to be evaluated |
|
133 | 146 | in a certain namespace. |
|
134 | 147 | Currently it's just globals, which need to be managed manually by code |
|
135 | 148 | below. |
|
136 | 149 | |
|
137 | 150 | \layout Itemize |
|
138 | 151 | |
|
139 | 152 | Fix coloring of prompts: the pysh color strings don't have any effect on |
|
140 | 153 | prompt numbers, b/c these are controlled by the global scheme. |
|
141 | 154 | Make the prompts fully user-definable, colors and all. |
|
142 | 155 | This is what I said to a user: |
|
143 | 156 | \newline |
|
144 | 157 | As far as the green |
|
145 | 158 | \backslash |
|
146 | 159 | #, this is a minor bug of the coloring code due to the vagaries of history. |
|
147 | 160 | While the color strings allow you to control the coloring of most elements, |
|
148 | 161 | there are a few which are still controlled by the old ipython internal |
|
149 | 162 | coloring code, which only accepts a global 'color scheme' choice. |
|
150 | 163 | So basically the input/output numbers are hardwired to the choice in the |
|
151 | 164 | color scheme, and there are only 'Linux', 'LightBG' and 'NoColor' schemes |
|
152 | 165 | to choose from. |
|
153 | 166 | |
|
154 | 167 | \layout Itemize |
|
155 | 168 | |
|
156 | 169 | Clean up FakeModule issues. |
|
157 | 170 | Currently, unittesting with embedded ipython breaks because a FakeModule |
|
158 | 171 | instance overwrites __main__. |
|
159 | 172 | Maybe ipython should revert back to using __main__ directly as the user |
|
160 | 173 | namespace? Handling a separate namespace is proving |
|
161 | 174 | \emph on |
|
162 | 175 | very |
|
163 | 176 | \emph default |
|
164 | 177 | tricky in all corner cases. |
|
165 | 178 | \layout Itemize |
|
166 | 179 | |
|
167 | 180 | Make the output cache depth independent of the input one. |
|
168 | 181 | This way one can have say only the last 10 results stored and still have |
|
169 | 182 | a long input history/cache. |
|
170 | 183 | \layout Itemize |
|
171 | 184 | |
|
172 | 185 | Fix the fact that importing a shell for embedding screws up the command-line |
|
173 | 186 | history. |
|
174 | 187 | This can be done by not importing the history file when the shell is already |
|
175 | 188 | inside ipython. |
|
176 | 189 | \layout Itemize |
|
177 | 190 | |
|
178 | 191 | Lay out the class structure so that embedding into a gtk/wx/qt app is trivial, |
|
179 | 192 | much like the multithreaded gui shells now provide command-line coexistence |
|
180 | 193 | with the gui toolkits. |
|
181 | 194 | See |
|
182 | 195 | \begin_inset LatexCommand \url{http://www.livejournal.com/users/glyf/32396.html} |
|
183 | 196 | |
|
184 | 197 | \end_inset |
|
185 | 198 | |
|
186 | 199 | |
|
187 | 200 | \layout Itemize |
|
188 | 201 | |
|
189 | 202 | Get Holger's completer in, once he adds filename completion. |
|
190 | 203 | \layout Standard |
|
191 | 204 | |
|
192 | 205 | Lower priority stuff: |
|
193 | 206 | \layout Itemize |
|
194 | 207 | |
|
195 | 208 | Add @showopt/@setopt (decide name) for viewing/setting all options. |
|
196 | 209 | The existing option-setting magics should become aliases for setopt calls. |
|
197 | 210 | \layout Itemize |
|
198 | 211 | |
|
199 | 212 | It would be nice to be able to continue with python stuff after an @ command. |
|
200 | 213 | For instance "@run something; test_stuff()" in order to test stuff even |
|
201 | 214 | faster. |
|
202 | 215 | Suggestion by Kasper Souren <Kasper.Souren@ircam.fr> |
|
203 | 216 | \layout Itemize |
|
204 | 217 | |
|
205 | 218 | Run a 'first time wizard' which configures a few things for the user, such |
|
206 | 219 | as color_info, editor and the like. |
|
207 | 220 | \layout Itemize |
|
208 | 221 | |
|
209 | 222 | Logging: @logstart and -log should start logfiles in ~.ipython, but with |
|
210 | 223 | unique names in case of collisions. |
|
211 | 224 | This would prevent ipython.log files all over while also allowing multiple |
|
212 | 225 | sessions. |
|
213 | 226 | Also the -log option should take an optional filename, instead of having |
|
214 | 227 | a separate -logfile option. |
|
215 | 228 | \newline |
|
216 | 229 | In general the logging system needs a serious cleanup. |
|
217 | 230 | Many functions now in Magic should be moved to Logger, and the magic @s |
|
218 | 231 | should be very simple wrappers to the Logger methods. |
|
219 | 232 | \layout Section |
|
220 | 233 | |
|
221 | 234 | Lighten the code |
|
222 | 235 | \layout Standard |
|
223 | 236 | |
|
224 | 237 | If we decide to base future versions of IPython on Python 2.3, which has |
|
225 | 238 | the new Optik module (called optparse), it should be possible to drop DPyGetOpt. |
|
226 | 239 | We should also remove the need for Itpl. |
|
227 | 240 | Another area for trimming is the Gnuplot stuff: much of that could be merged |
|
228 | 241 | into the mainline project. |
|
229 | 242 | \layout Standard |
|
230 | 243 | |
|
231 | 244 | Double check whether we really need FlexCompleter. |
|
232 | 245 | This was written as an enhanced rlcompleter, but my patches went in for |
|
233 | 246 | python 2.2 (or 2.3, can't remember). |
|
234 | 247 | \layout Standard |
|
235 | 248 | |
|
236 | 249 | With these changes we could shed a fair bit of code from the main trunk. |
|
237 | 250 | \layout Section |
|
238 | 251 | |
|
239 | 252 | Unit testing |
|
240 | 253 | \layout Standard |
|
241 | 254 | |
|
242 | 255 | All new code should use a testing framework. |
|
243 | 256 | Python seems to have very good testing facilities, I just need to learn |
|
244 | 257 | how to use them. |
|
245 | 258 | I should also check out QMTest at |
|
246 | 259 | \begin_inset LatexCommand \htmlurl{http://www.codesourcery.com/qm/qmtest} |
|
247 | 260 | |
|
248 | 261 | \end_inset |
|
249 | 262 | |
|
250 | 263 | , it sounds interesting (it's Python-based too). |
|
251 | 264 | \layout Section |
|
252 | 265 | |
|
253 | 266 | Configuration system |
|
254 | 267 | \layout Standard |
|
255 | 268 | |
|
256 | 269 | Move away from the current ipythonrc format to using standard python files |
|
257 | 270 | for configuration. |
|
258 | 271 | This will require users to be slightly more careful in their syntax, but |
|
259 | 272 | reduces code in IPython, is more in line with Python's normal form (using |
|
260 | 273 | the $PYTHONSTARTUP file) and allows much more flexibility. |
|
261 | 274 | I also think it's more 'pythonic', in using a single language for everything. |
|
262 | 275 | \layout Standard |
|
263 | 276 | |
|
264 | 277 | Options can be set up with a function call which takes keywords and updates |
|
265 | 278 | the options Struct. |
|
266 | 279 | \layout Standard |
|
267 | 280 | |
|
268 | 281 | In order to maintain the recursive inclusion system, write an 'include' |
|
269 | 282 | function which is basically a wrapper around safe_execfile(). |
|
270 | 283 | Also for alias definitions an alias() function will do. |
|
271 | 284 | All functionality which we want to have at startup time for the users can |
|
272 | 285 | be wrapped in a small module so that config files look like: |
|
273 | 286 | \layout Standard |
|
274 | 287 | |
|
275 | 288 | |
|
276 | 289 | \family typewriter |
|
277 | 290 | from IPython.Startup import * |
|
278 | 291 | \newline |
|
279 | 292 | ... |
|
280 | 293 | \newline |
|
281 | 294 | set_options(automagic=1,colors='NoColor',...) |
|
282 | 295 | \newline |
|
283 | 296 | ... |
|
284 | 297 | \newline |
|
285 | 298 | include('mysetup.py') |
|
286 | 299 | \newline |
|
287 | 300 | ... |
|
288 | 301 | \newline |
|
289 | 302 | alias('ls ls --color -l') |
|
290 | 303 | \newline |
|
291 | 304 | ... |
|
292 | 305 | etc. |
|
293 | 306 | \layout Standard |
|
294 | 307 | |
|
295 | 308 | Also, put |
|
296 | 309 | \series bold |
|
297 | 310 | all |
|
298 | 311 | \series default |
|
299 | 312 | aliases in here, out of the core code. |
|
300 | 313 | \layout Standard |
|
301 | 314 | |
|
302 | 315 | The new system should allow for more seamless upgrading, so that: |
|
303 | 316 | \layout Itemize |
|
304 | 317 | |
|
305 | 318 | It automatically recognizes when the config files need updating and does |
|
306 | 319 | the upgrade. |
|
307 | 320 | \layout Itemize |
|
308 | 321 | |
|
309 | 322 | It simply adds the new options to the user's config file without overwriting |
|
310 | 323 | it. |
|
311 | 324 | The current system is annoying since users need to manually re-sync their |
|
312 | 325 | configuration after every update. |
|
313 | 326 | \layout Itemize |
|
314 | 327 | |
|
315 | 328 | It detects obsolete options and informs the user to remove them from his |
|
316 | 329 | config file. |
|
317 | 330 | \layout Standard |
|
318 | 331 | |
|
319 | 332 | Here's a copy of Arnd Baecker suggestions on the matter: |
|
320 | 333 | \layout Standard |
|
321 | 334 | |
|
322 | 335 | 1.) upgrade: it might be nice to have an "auto" upgrade procedure: i.e. |
|
323 | 336 | imagine that IPython is installed system-wide and gets upgraded, how does |
|
324 | 337 | a user know, that an upgrade of the stuff in ~/.ipython is necessary ? So |
|
325 | 338 | maybe one has to a keep a version number in ~/.ipython and if there is a |
|
326 | 339 | mismatch with the started ipython, then invoke the upgrade procedure. |
|
327 | 340 | \layout Standard |
|
328 | 341 | |
|
329 | 342 | 2.) upgrade: I find that replacing the old files in ~/.ipython (after copying |
|
330 | 343 | them to .old not optimal (for example, after every update, I have to change |
|
331 | 344 | my color settings (and some others) in ~/.ipython/ipthonrc). |
|
332 | 345 | So somehow keeping the old files and merging the new features would be |
|
333 | 346 | nice. |
|
334 | 347 | (but how to distinguish changes from version to version with changes made |
|
335 | 348 | by the user ?) For, example, I would have to change in GnuplotMagic.py gnuplot_m |
|
336 | 349 | ouse to 1 after every upgrade ... |
|
337 | 350 | \layout Standard |
|
338 | 351 | |
|
339 | 352 | This is surely a minor point - also things will change during the "BIG" |
|
340 | 353 | rewrite, but maybe this is a point to keep in mind for this ? |
|
341 | 354 | \layout Standard |
|
342 | 355 | |
|
343 | 356 | 3.) upgrade: old, sometimes obsolete files stay in the ~/.ipython subdirectory. |
|
344 | 357 | (hmm, maybe one could move all these into some subdirectory, but which |
|
345 | 358 | name for that (via version-number ?) ?) |
|
346 | 359 | \layout Subsection |
|
347 | 360 | |
|
348 | 361 | Command line options |
|
349 | 362 | \layout Standard |
|
350 | 363 | |
|
351 | 364 | It would be great to design the command-line processing system so that it |
|
352 | 365 | can be dynamically modified in some easy way. |
|
353 | 366 | This would allow systems based on IPython to include their own command-line |
|
354 | 367 | processing to either extend or fully replace IPython's. |
|
355 | 368 | Probably moving to the new optparse library (also known as optik) will |
|
356 | 369 | make this a lot easier. |
|
357 | 370 | \layout Section |
|
358 | 371 | |
|
359 | 372 | OS-dependent code |
|
360 | 373 | \layout Standard |
|
361 | 374 | |
|
362 | 375 | Options which are OS-dependent (such as colors and aliases) should be loaded |
|
363 | 376 | via include files. |
|
364 | 377 | That is, the general file will have: |
|
365 | 378 | \layout Standard |
|
366 | 379 | |
|
367 | 380 | |
|
368 | 381 | \family typewriter |
|
369 | 382 | if os.name == 'posix': |
|
370 | 383 | \newline |
|
371 | 384 | include('ipythonrc-posix.py') |
|
372 | 385 | \newline |
|
373 | 386 | elif os.name == 'nt': |
|
374 | 387 | \newline |
|
375 | 388 | include('ipythonrc-nt.py')... |
|
376 | 389 | \layout Standard |
|
377 | 390 | |
|
378 | 391 | In the |
|
379 | 392 | \family typewriter |
|
380 | 393 | -posix |
|
381 | 394 | \family default |
|
382 | 395 | , |
|
383 | 396 | \family typewriter |
|
384 | 397 | -nt |
|
385 | 398 | \family default |
|
386 | 399 | , etc. |
|
387 | 400 | files we'll set all os-specific options. |
|
388 | 401 | \layout Section |
|
389 | 402 | |
|
390 | 403 | Merging with other shell systems |
|
391 | 404 | \layout Standard |
|
392 | 405 | |
|
393 | 406 | This is listed before the big design issues, as it is something which should |
|
394 | 407 | be kept in mind when that design is made. |
|
395 | 408 | \layout Standard |
|
396 | 409 | |
|
397 | 410 | The following shell systems are out there and I think the whole design of |
|
398 | 411 | IPython should try to be modular enough to make it possible to integrate |
|
399 | 412 | its features into these. |
|
400 | 413 | In all cases IPython should exist as a stand-alone, terminal based program. |
|
401 | 414 | But it would be great if users of these other shells (some of them which |
|
402 | 415 | have very nice features of their own, especially the graphical ones) could |
|
403 | 416 | keep their environment but gain IPython's features. |
|
404 | 417 | \layout List |
|
405 | 418 | \labelwidthstring 00.00.0000 |
|
406 | 419 | |
|
407 | 420 | IDLE This is the standard, distributed as part of Python. |
|
408 | 421 | |
|
409 | 422 | \layout List |
|
410 | 423 | \labelwidthstring 00.00.0000 |
|
411 | 424 | |
|
412 | 425 | pyrepl |
|
413 | 426 | \begin_inset LatexCommand \htmlurl{http://starship.python.net/crew/mwh/hacks/pyrepl.html} |
|
414 | 427 | |
|
415 | 428 | \end_inset |
|
416 | 429 | |
|
417 | 430 | . |
|
418 | 431 | This is a text (curses-based) shell-like replacement which doesn't have |
|
419 | 432 | some of IPython's features, but has a crucially useful (and hard to implement) |
|
420 | 433 | one: full multi-line editing. |
|
421 | 434 | This turns the interactive interpreter into a true code testing and development |
|
422 | 435 | environment. |
|
423 | 436 | |
|
424 | 437 | \layout List |
|
425 | 438 | \labelwidthstring 00.00.0000 |
|
426 | 439 | |
|
427 | 440 | PyCrust |
|
428 | 441 | \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/pycrust} |
|
429 | 442 | |
|
430 | 443 | \end_inset |
|
431 | 444 | |
|
432 | 445 | . |
|
433 | 446 | Very nice, wxWindows based system. |
|
434 | 447 | \layout List |
|
435 | 448 | \labelwidthstring 00.00.0000 |
|
436 | 449 | |
|
437 | 450 | PythonWin |
|
438 | 451 | \begin_inset LatexCommand \htmlurl{http://starship.python.net/crew/mhammond} |
|
439 | 452 | |
|
440 | 453 | \end_inset |
|
441 | 454 | |
|
442 | 455 | . |
|
443 | 456 | Similar to PyCrust in some respects, a very good and free Python development |
|
444 | 457 | environment for Windows systems. |
|
445 | 458 | \layout Section |
|
446 | 459 | |
|
447 | 460 | Class design |
|
448 | 461 | \layout Standard |
|
449 | 462 | |
|
450 | 463 | This is the big one. |
|
451 | 464 | Currently classes use each other in a very messy way, poking inside one |
|
452 | 465 | another for data and methods. |
|
453 | 466 | ipmaker() adds tons of stuff to the main __IP instance by hand, and the |
|
454 | 467 | mix-ins used (Logger, Magic, etc) mean the final __IP instance has a million |
|
455 | 468 | things in it. |
|
456 | 469 | All that needs to be cleanly broken down with well defined interfaces amongst |
|
457 | 470 | the different classes, and probably no mix-ins. |
|
458 | 471 | \layout Standard |
|
459 | 472 | |
|
460 | 473 | The best approach is probably to have all the sub-systems which are currently |
|
461 | 474 | mixins be fully independent classes which talk back only to the main instance |
|
462 | 475 | (and |
|
463 | 476 | \series bold |
|
464 | 477 | not |
|
465 | 478 | \series default |
|
466 | 479 | to each other). |
|
467 | 480 | In the main instance there should be an object whose job is to handle communica |
|
468 | 481 | tion with the sub-systems. |
|
469 | 482 | \layout Standard |
|
470 | 483 | |
|
471 | 484 | I should probably learn a little UML and diagram this whole thing before |
|
472 | 485 | I start coding. |
|
473 | 486 | \layout Subsection |
|
474 | 487 | |
|
475 | 488 | Magic |
|
476 | 489 | \layout Standard |
|
477 | 490 | |
|
478 | 491 | Now all methods which will become publicly available are called Magic.magic_name, |
|
479 | 492 | the magic_ should go away. |
|
480 | 493 | Then, Magic instead of being a mix-in should simply be an attribute of |
|
481 | 494 | __IP: |
|
482 | 495 | \layout Standard |
|
483 | 496 | |
|
484 | 497 | __IP.Magic = Magic() |
|
485 | 498 | \layout Standard |
|
486 | 499 | |
|
487 | 500 | This will then give all the magic functions as __IP.Magic.name(), which is |
|
488 | 501 | much cleaner. |
|
489 | 502 | This will also force a better separation so that Magic doesn't poke inside |
|
490 | 503 | __IP so much. |
|
491 | 504 | In the constructor, Magic should get whatever information it needs to know |
|
492 | 505 | about __IP (even if it means a pointer to __IP itself, but at least we'll |
|
493 | 506 | know where it is. |
|
494 | 507 | Right now since it's a mix-in, there's no way to know which variables belong |
|
495 | 508 | to whom). |
|
496 | 509 | \layout Standard |
|
497 | 510 | |
|
498 | 511 | Build a class MagicFunction so that adding new functions is a matter of: |
|
499 | 512 | \layout Standard |
|
500 | 513 | |
|
501 | 514 | |
|
502 | 515 | \family typewriter |
|
503 | 516 | my_magic = MagicFunction(category = 'System utilities') |
|
504 | 517 | \newline |
|
505 | 518 | my_magic.__call__ = ... |
|
506 | 519 | \layout Standard |
|
507 | 520 | |
|
508 | 521 | Features: |
|
509 | 522 | \layout Itemize |
|
510 | 523 | |
|
511 | 524 | The class constructor should automatically register the functions and keep |
|
512 | 525 | a table with category sections for easy sorting/viewing. |
|
513 | 526 | \layout Itemize |
|
514 | 527 | |
|
515 | 528 | The object interface must allow automatic building of a GUI for them. |
|
516 | 529 | This requires registering the options the command takes, the number of |
|
517 | 530 | arguments, etc, in a formal way. |
|
518 | 531 | The advantage of this approach is that it allows not only to add GUIs to |
|
519 | 532 | the magics, but also for a much more intelligent building of docstrings, |
|
520 | 533 | and better parsing of options and arguments. |
|
521 | 534 | \layout Standard |
|
522 | 535 | |
|
523 | 536 | Also think through better an alias system for magics. |
|
524 | 537 | Since the magic system is like a command shell inside ipython, the relation |
|
525 | 538 | between these aliases and system aliases should be cleanly thought out. |
|
526 | 539 | \layout Subsection |
|
527 | 540 | |
|
528 | 541 | Color schemes |
|
529 | 542 | \layout Standard |
|
530 | 543 | |
|
531 | 544 | These should be loaded from some kind of resource file so they are easier |
|
532 | 545 | to modify by the user. |
|
533 | 546 | \layout Section |
|
534 | 547 | |
|
535 | 548 | Hooks |
|
536 | 549 | \layout Standard |
|
537 | 550 | |
|
538 | 551 | IPython should have a modular system where functions can register themselves |
|
539 | 552 | for certain tasks. |
|
540 | 553 | Currently changing functionality requires overriding certain specific methods, |
|
541 | 554 | there should be a clean API for this to be done. |
|
542 | 555 | \layout Subsection |
|
543 | 556 | |
|
544 | 557 | whos hook |
|
545 | 558 | \layout Standard |
|
546 | 559 | |
|
547 | 560 | This was a very nice suggestion from Alexander Schmolck <a.schmolck@gmx.net>: |
|
548 | 561 | \layout Standard |
|
549 | 562 | |
|
550 | 563 | 2. |
|
551 | 564 | I think it would also be very helpful if there where some sort of hook |
|
552 | 565 | for ``whos`` that let one customize display formaters depending on the |
|
553 | 566 | object type. |
|
554 | 567 | \layout Standard |
|
555 | 568 | |
|
556 | 569 | For example I'd rather have a whos that formats an array like: |
|
557 | 570 | \layout Standard |
|
558 | 571 | |
|
559 | 572 | |
|
560 | 573 | \family typewriter |
|
561 | 574 | Variable Type Data/Length |
|
562 | 575 | \newline |
|
563 | 576 | ------------------------------ |
|
564 | 577 | \newline |
|
565 | 578 | a array size: 4x3 type: 'Float' |
|
566 | 579 | \layout Standard |
|
567 | 580 | |
|
568 | 581 | than |
|
569 | 582 | \layout Standard |
|
570 | 583 | |
|
571 | 584 | |
|
572 | 585 | \family typewriter |
|
573 | 586 | Variable Type Data/Length |
|
574 | 587 | \newline |
|
575 | 588 | ------------------------------ |
|
576 | 589 | \newline |
|
577 | 590 | a array [[ 0. |
|
578 | 591 | 1. |
|
579 | 592 | 2. |
|
580 | 593 | 3<...> 8. |
|
581 | 594 | 9. |
|
582 | 595 | 10. |
|
583 | 596 | 11.]] |
|
584 | 597 | \layout Section |
|
585 | 598 | |
|
586 | 599 | Parallel support |
|
587 | 600 | \layout Standard |
|
588 | 601 | |
|
589 | 602 | For integration with graphical shells and other systems, it will be best |
|
590 | 603 | if ipython is split into a kernel/client model, much like Mathematica works. |
|
591 | 604 | This simultaneously opens the door for support of interactive parallel |
|
592 | 605 | computing. |
|
593 | 606 | Currenlty %bg provides a threads-based proof of concept, and Brian Granger's |
|
594 | 607 | XGrid project is a much more realistic such system. |
|
595 | 608 | The new design should integrates ideas as core elements. |
|
596 | 609 | Some notes from Brian on this topic: |
|
597 | 610 | \layout Standard |
|
598 | 611 | |
|
599 | 612 | 1. |
|
600 | 613 | How should the remote python server/kernel be designed? Multithreaded? |
|
601 | 614 | Blocking? Connected/disconnected modes? Load balancing? |
|
602 | 615 | \layout Standard |
|
603 | 616 | |
|
604 | 617 | 2. |
|
605 | 618 | What APi/protocol should the server/kernel expose to clients? |
|
606 | 619 | \layout Standard |
|
607 | 620 | |
|
608 | 621 | 3. |
|
609 | 622 | How should the client classes (which the user uses to interact with the |
|
610 | 623 | cluster) be designed? |
|
611 | 624 | \layout Standard |
|
612 | 625 | |
|
613 | 626 | 4. |
|
614 | 627 | What API should the client classes expose? |
|
615 | 628 | \layout Standard |
|
616 | 629 | |
|
617 | 630 | 5. |
|
618 | 631 | How should the client API be wrapped in a few simple magic functions? |
|
619 | 632 | \layout Standard |
|
620 | 633 | |
|
621 | 634 | 6. |
|
622 | 635 | How should security be handled? |
|
623 | 636 | \layout Standard |
|
624 | 637 | |
|
625 | 638 | 7. |
|
626 | 639 | How to work around the issues of the GIL and threads? |
|
627 | 640 | \layout Standard |
|
628 | 641 | |
|
629 | 642 | I think the most important things to work out are the client API (#4) the |
|
630 | 643 | server/kernel API/protocol (#2) and the magic function API (#5). |
|
631 | 644 | We should let these determine the design and architecture of the components. |
|
632 | 645 | \layout Standard |
|
633 | 646 | |
|
634 | 647 | One other thing. |
|
635 | 648 | What is your impression of twisted? I have been looking at it and it looks |
|
636 | 649 | like a _very_ powerful set of tools for this type of stuff. |
|
637 | 650 | I am wondering if it might make sense to think about using twisted for |
|
638 | 651 | this project. |
|
639 | 652 | \layout Section |
|
640 | 653 | |
|
641 | 654 | Manuals |
|
642 | 655 | \layout Standard |
|
643 | 656 | |
|
644 | 657 | The documentation should be generated from docstrings for the command line |
|
645 | 658 | args and all the magic commands. |
|
646 | 659 | Look into one of the simple text markup systems to see if we can get latex |
|
647 | 660 | (for reLyXing later) out of this. |
|
648 | 661 | Part of the build command would then be to make an update of the docs based |
|
649 | 662 | on this, thus giving more complete manual (and guaranteed to be in sync |
|
650 | 663 | with the code docstrings). |
|
651 | 664 | \layout Standard |
|
652 | 665 | |
|
653 | 666 | [PARTLY DONE] At least now all magics are auto-documented, works farily |
|
654 | 667 | well. |
|
655 | 668 | Limited Latex formatting yet. |
|
656 | 669 | \layout Subsection |
|
657 | 670 | |
|
658 | 671 | Integration with pydoc-help |
|
659 | 672 | \layout Standard |
|
660 | 673 | |
|
661 | 674 | It should be possible to have access to the manual via the pydoc help system |
|
662 | 675 | somehow. |
|
663 | 676 | This might require subclassing the pydoc help, or figuring out how to add |
|
664 | 677 | the IPython docs in the right form so that help() finds them. |
|
665 | 678 | \layout Standard |
|
666 | 679 | |
|
667 | 680 | Some comments from Arnd and my reply on this topic: |
|
668 | 681 | \layout Standard |
|
669 | 682 | |
|
670 | 683 | > ((Generally I would like to have the nice documentation > more easily |
|
671 | 684 | accessable from within ipython ... |
|
672 | 685 | > Many people just don't read documentation, even if it is > as good as |
|
673 | 686 | the one of IPython )) |
|
674 | 687 | \layout Standard |
|
675 | 688 | |
|
676 | 689 | That's an excellent point. |
|
677 | 690 | I've added a note to this effect in new_design. |
|
678 | 691 | Basically I'd like help() to naturally access the IPython docs. |
|
679 | 692 | Since they are already there in html for the user, it's probably a matter |
|
680 | 693 | of playing a bit with pydoc to tell it where to find them. |
|
681 | 694 | It would definitely make for a much cleaner system. |
|
682 | 695 | Right now the information on IPython is: |
|
683 | 696 | \layout Standard |
|
684 | 697 | |
|
685 | 698 | -ipython --help at the command line: info on command line switches |
|
686 | 699 | \layout Standard |
|
687 | 700 | |
|
688 | 701 | -? at the ipython prompt: overview of IPython |
|
689 | 702 | \layout Standard |
|
690 | 703 | |
|
691 | 704 | -magic at the ipython prompt: overview of the magic system |
|
692 | 705 | \layout Standard |
|
693 | 706 | |
|
694 | 707 | -external docs (html/pdf) |
|
695 | 708 | \layout Standard |
|
696 | 709 | |
|
697 | 710 | All that should be better integrated seamlessly in the help() system, so |
|
698 | 711 | that you can simply say: |
|
699 | 712 | \layout Standard |
|
700 | 713 | |
|
701 | 714 | help ipython -> full documentation access |
|
702 | 715 | \layout Standard |
|
703 | 716 | |
|
704 | 717 | help magic -> magic overview |
|
705 | 718 | \layout Standard |
|
706 | 719 | |
|
707 | 720 | help profile -> help on current profile |
|
708 | 721 | \layout Standard |
|
709 | 722 | |
|
710 | 723 | help -> normal python help access. |
|
711 | 724 | \layout Section |
|
712 | 725 | |
|
713 | 726 | Graphical object browsers |
|
714 | 727 | \layout Standard |
|
715 | 728 | |
|
716 | 729 | I'd like a system for graphically browsing through objects. |
|
717 | 730 | |
|
718 | 731 | \family typewriter |
|
719 | 732 | @obrowse |
|
720 | 733 | \family default |
|
721 | 734 | should open a widged with all the things which |
|
722 | 735 | \family typewriter |
|
723 | 736 | @who |
|
724 | 737 | \family default |
|
725 | 738 | lists, but cliking on each object would open a dedicated object viewer |
|
726 | 739 | (also accessible as |
|
727 | 740 | \family typewriter |
|
728 | 741 | @oview <object> |
|
729 | 742 | \family default |
|
730 | 743 | ). |
|
731 | 744 | This object viewer could show a summary of what |
|
732 | 745 | \family typewriter |
|
733 | 746 | <object>? |
|
734 | 747 | \family default |
|
735 | 748 | currently shows, but also colorize source code and show it via an html |
|
736 | 749 | browser, show all attributes and methods of a given object (themselves |
|
737 | 750 | openable in their own viewers, since in Python everything is an object), |
|
738 | 751 | links to the parent classes, etc. |
|
739 | 752 | \layout Standard |
|
740 | 753 | |
|
741 | 754 | The object viewer widget should be extensible, so that one can add methods |
|
742 | 755 | to view certain types of objects in a special way (for example, plotting |
|
743 | 756 | Numeric arrays via grace or gnuplot). |
|
744 | 757 | This would be very useful when using IPython as part of an interactive |
|
745 | 758 | complex system for working with certain types of data. |
|
746 | 759 | \layout Standard |
|
747 | 760 | |
|
748 | 761 | I should look at what PyCrust has to offer along these lines, at least as |
|
749 | 762 | a starting point. |
|
750 | 763 | \layout Section |
|
751 | 764 | |
|
752 | 765 | Miscellaneous small things |
|
753 | 766 | \layout Itemize |
|
754 | 767 | |
|
755 | 768 | Collect whatever variables matter from the environment in some globals for |
|
756 | 769 | __IP, so we're not testing for them constantly (like $HOME, $TERM, etc.) |
|
757 | 770 | \layout Section |
|
758 | 771 | |
|
759 | 772 | Session restoring |
|
760 | 773 | \layout Standard |
|
761 | 774 | |
|
762 | 775 | I've convinced myself that session restore by log replay is too fragile |
|
763 | 776 | and tricky to ever work reliably. |
|
764 | 777 | Plus it can be dog slow. |
|
765 | 778 | I'd rather have a way of saving/restoring the *current* memory state of |
|
766 | 779 | IPython. |
|
767 | 780 | I tried with pickle but failed (can't pickle modules). |
|
768 | 781 | This seems the right way to do it to me, but it will have to wait until |
|
769 | 782 | someone tells me of a robust way of dumping/reloading *all* of the user |
|
770 | 783 | namespace in a file. |
|
771 | 784 | \layout Standard |
|
772 | 785 | |
|
773 | 786 | Probably the best approach will be to pickle as much as possible and record |
|
774 | 787 | what can not be pickled for manual reload (such as modules). |
|
775 | 788 | This is not trivial to get to work reliably, so it's best left for after |
|
776 | 789 | the code restructuring. |
|
777 | 790 | \layout Standard |
|
778 | 791 | |
|
779 | 792 | The following issues exist (old notes, see above paragraph for my current |
|
780 | 793 | take on the issue): |
|
781 | 794 | \layout Itemize |
|
782 | 795 | |
|
783 | 796 | magic lines aren't properly re-executed when a log file is reloaded (and |
|
784 | 797 | some of them, like clear or run, may change the environment). |
|
785 | 798 | So session restore isn't 100% perfect. |
|
786 | 799 | \layout Itemize |
|
787 | 800 | |
|
788 | 801 | auto-quote/parens lines aren't replayed either. |
|
789 | 802 | All this could be done, but it needs some work. |
|
790 | 803 | Basically it requires re-running the log through IPython itself, not through |
|
791 | 804 | python. |
|
792 | 805 | \layout Itemize |
|
793 | 806 | |
|
794 | 807 | _p variables aren't restored with a session. |
|
795 | 808 | Fix: same as above. |
|
796 | 809 | \layout Section |
|
797 | 810 | |
|
798 | 811 | Tips system |
|
799 | 812 | \layout Standard |
|
800 | 813 | |
|
801 | 814 | It would be nice to have a tip() function which gives tips to users in some |
|
802 | 815 | situations, but keeps track of already-given tips so they aren't given |
|
803 | 816 | every time. |
|
804 | 817 | This could be done by pickling a dict of given tips to IPYTHONDIR. |
|
805 | 818 | \layout Section |
|
806 | 819 | |
|
807 | 820 | TAB completer |
|
808 | 821 | \layout Standard |
|
809 | 822 | |
|
810 | 823 | Some suggestions from Arnd Baecker: |
|
811 | 824 | \layout Standard |
|
812 | 825 | |
|
813 | 826 | a) For file related commands (ls, cat, ...) it would be nice to be able to |
|
814 | 827 | TAB complete the files in the current directory. |
|
815 | 828 | (once you started typing something which is uniquely a file, this leads |
|
816 | 829 | to this effect, apart from going through the list of possible completions |
|
817 | 830 | ...). |
|
818 | 831 | (I know that this point is in your documentation.) |
|
819 | 832 | \layout Standard |
|
820 | 833 | |
|
821 | 834 | More general, this might lead to something like command specific completion |
|
822 | 835 | ? |
|
823 | 836 | \layout Standard |
|
824 | 837 | |
|
825 | 838 | Here's John Hunter's suggestion: |
|
826 | 839 | \layout Standard |
|
827 | 840 | |
|
828 | 841 | The *right way to do it* would be to make intelligent or customizable choices |
|
829 | 842 | about which namespace to add to the completion list depending on the string |
|
830 | 843 | match up to the prompt, eg programmed completions. |
|
831 | 844 | In the simplest implementation, one would only complete on files and directorie |
|
832 | 845 | s if the line preceding the tab press matched 'cd ' or 'run ' (eg you don't |
|
833 | 846 | want callable showing up in 'cd ca<TAB>') |
|
834 | 847 | \layout Standard |
|
835 | 848 | |
|
836 | 849 | In a more advanced scenario, you might imaging that functions supplied the |
|
837 | 850 | TAB namespace, and the user could configure a dictionary that mapped regular |
|
838 | 851 | expressions to namespace providing functions (with sensible defaults). |
|
839 | 852 | Something like |
|
840 | 853 | \layout Standard |
|
841 | 854 | |
|
842 | 855 | completed = { |
|
843 | 856 | \newline |
|
844 | 857 | '^cd |
|
845 | 858 | \backslash |
|
846 | 859 | s+(.*)' : complete_files_and_dirs, |
|
847 | 860 | \newline |
|
848 | 861 | '^run |
|
849 | 862 | \backslash |
|
850 | 863 | s+(.*)' : complete_files_and_dirs, |
|
851 | 864 | \newline |
|
852 | 865 | '^run |
|
853 | 866 | \backslash |
|
854 | 867 | s+(-.*)' : complete_run_options, |
|
855 | 868 | \newline |
|
856 | 869 | } |
|
857 | 870 | \layout Standard |
|
858 | 871 | |
|
859 | 872 | I don't know if this is feasible, but I really like programmed completions, |
|
860 | 873 | which I use extensively in tcsh. |
|
861 | 874 | My feeling is that something like this is eminently doable in ipython. |
|
862 | 875 | \layout Standard |
|
863 | 876 | |
|
864 | 877 | /JDH |
|
865 | 878 | \layout Standard |
|
866 | 879 | |
|
867 | 880 | For something like this to work cleanly, the magic command system needs |
|
868 | 881 | also a clean options framework, so all valid options for a given magic |
|
869 | 882 | can be extracted programatically. |
|
870 | 883 | \layout Section |
|
871 | 884 | |
|
872 | 885 | Debugger |
|
873 | 886 | \layout Standard |
|
874 | 887 | |
|
875 | 888 | Current system uses a minimally tweaked pdb. |
|
876 | 889 | Fine-tune it a bit, to provide at least: |
|
877 | 890 | \layout Itemize |
|
878 | 891 | |
|
879 | 892 | Tab-completion in each stack frame. |
|
880 | 893 | See email to Chris Hart for details. |
|
881 | 894 | \layout Itemize |
|
882 | 895 | |
|
883 | 896 | Object information via ? at least. |
|
884 | 897 | Break up magic_oinfo a bit so that pdb can call it without loading all |
|
885 | 898 | of IPython. |
|
886 | 899 | If possible, also have the other magics for object study: doc, source, |
|
887 | 900 | pdef and pfile. |
|
888 | 901 | \layout Itemize |
|
889 | 902 | |
|
890 | 903 | Shell access via ! |
|
891 | 904 | \layout Itemize |
|
892 | 905 | |
|
893 | 906 | Syntax highlighting in listings. |
|
894 | 907 | Use py2html code, implement color schemes. |
|
895 | 908 | \layout Section |
|
896 | 909 | |
|
897 | 910 | A Python-based system shell - pysh? |
|
898 | 911 | \layout Standard |
|
899 | 912 | |
|
900 | 913 | Note: as of IPython 0.6.1, most of this functionality has actually been implemente |
|
901 | 914 | d. |
|
902 | 915 | \layout Standard |
|
903 | 916 | |
|
904 | 917 | This section is meant as a working draft for discussions on the possibility |
|
905 | 918 | of having a python-based system shell. |
|
906 | 919 | It is the result of my own thinking about these issues as much of discussions |
|
907 | 920 | on the ipython lists. |
|
908 | 921 | I apologize in advance for not giving individual credit to the various |
|
909 | 922 | contributions, but right now I don't have the time to track down each message |
|
910 | 923 | from the archives. |
|
911 | 924 | So please consider this as the result of a collective effort by the ipython |
|
912 | 925 | user community. |
|
913 | 926 | \layout Standard |
|
914 | 927 | |
|
915 | 928 | While IPyhton is (and will remain) a python shell first, it does offer a |
|
916 | 929 | fair amount of system access functionality: |
|
917 | 930 | \layout Standard |
|
918 | 931 | |
|
919 | 932 | - ! and !! for direct system access, |
|
920 | 933 | \layout Standard |
|
921 | 934 | |
|
922 | 935 | - magic commands which wrap various system commands, |
|
923 | 936 | \layout Standard |
|
924 | 937 | |
|
925 | 938 | - @sc and @sx, for shell output capture into python variables, |
|
926 | 939 | \layout Standard |
|
927 | 940 | |
|
928 | 941 | - @alias, for aliasing system commands. |
|
929 | 942 | \layout Standard |
|
930 | 943 | |
|
931 | 944 | This has prompted many users, over time, to ask for a way of extending ipython |
|
932 | 945 | to the point where it could be used as a full-time replacement over typical |
|
933 | 946 | user shells like bash, csh or tcsh. |
|
934 | 947 | While my interest in ipython is such that I'll concentrate my personal |
|
935 | 948 | efforts on other fronts (debugging, architecture, improvements for scientific |
|
936 | 949 | use, gui access), I will be happy to do anything which could make such |
|
937 | 950 | a development possible. |
|
938 | 951 | It would be the responsibility of someone else to maintain the code, but |
|
939 | 952 | I would do all necessary architectural changes to ipython for such an extension |
|
940 | 953 | to be feasible. |
|
941 | 954 | \layout Standard |
|
942 | 955 | |
|
943 | 956 | I'll try to outline here what I see as the key issues which need to be taken |
|
944 | 957 | into account. |
|
945 | 958 | This document should be considered an evolving draft. |
|
946 | 959 | Feel free to submit comments/improvements, even in the form of patches. |
|
947 | 960 | \layout Standard |
|
948 | 961 | |
|
949 | 962 | In what follows, I'll represent the hypothetical python-based shell ('pysh' |
|
950 | 963 | for now) prompt with '>>'. |
|
951 | 964 | \layout Subsection |
|
952 | 965 | |
|
953 | 966 | Basic design principles |
|
954 | 967 | \layout Standard |
|
955 | 968 | |
|
956 | 969 | I think the basic design guideline should be the following: a hypothetical |
|
957 | 970 | python system shell should behave, as much as possible, like a normal shell |
|
958 | 971 | that users are familiar with (bash, tcsh, etc). |
|
959 | 972 | This means: |
|
960 | 973 | \layout Standard |
|
961 | 974 | |
|
962 | 975 | 1. |
|
963 | 976 | System commands can be issued directly at the prompt with no special syntax: |
|
964 | 977 | \layout Standard |
|
965 | 978 | |
|
966 | 979 | >> ls |
|
967 | 980 | \layout Standard |
|
968 | 981 | |
|
969 | 982 | >> xemacs |
|
970 | 983 | \layout Standard |
|
971 | 984 | |
|
972 | 985 | should just work like a user expects. |
|
973 | 986 | \layout Standard |
|
974 | 987 | |
|
975 | 988 | 2. |
|
976 | 989 | The facilities of the python language should always be available, like |
|
977 | 990 | they are in ipython: |
|
978 | 991 | \layout Standard |
|
979 | 992 | |
|
980 | 993 | >> 3+4 |
|
981 | 994 | \newline |
|
982 | 995 | 7 |
|
983 | 996 | \layout Standard |
|
984 | 997 | |
|
985 | 998 | 3. |
|
986 | 999 | It should be possible to easily capture shell output into a variable. |
|
987 | 1000 | bash and friends use backquotes, I think using a command (@sc) like ipython |
|
988 | 1001 | currently does is an acceptable compromise. |
|
989 | 1002 | \layout Standard |
|
990 | 1003 | |
|
991 | 1004 | 4. |
|
992 | 1005 | It should also be possible to expand python variables/commands in the middle |
|
993 | 1006 | of system commands. |
|
994 | 1007 | I thihk this will make it necessary to use $var for name expansions: |
|
995 | 1008 | \layout Standard |
|
996 | 1009 | |
|
997 | 1010 | >> var='hello' # var is a Python variable |
|
998 | 1011 | \newline |
|
999 | 1012 | >> print var hello # This is the result of a Python print command |
|
1000 | 1013 | \newline |
|
1001 | 1014 | >> echo $var hello # This calls the echo command, expanding 'var'. |
|
1002 | 1015 | \layout Standard |
|
1003 | 1016 | |
|
1004 | 1017 | 5. |
|
1005 | 1018 | The above capabilities should remain possible for multi-line commands. |
|
1006 | 1019 | One of the most annoying things I find about tcsh, is that I never quite |
|
1007 | 1020 | remember the syntactic details of looping. |
|
1008 | 1021 | I often want to do something at the shell which involves a simple loop, |
|
1009 | 1022 | but I can never remember how to do it in tcsh. |
|
1010 | 1023 | This often means I just write a quick throwaway python script to do it |
|
1011 | 1024 | (Perl is great for this kind of quick things, but I've forgotten most its |
|
1012 | 1025 | syntax as well). |
|
1013 | 1026 | \layout Standard |
|
1014 | 1027 | |
|
1015 | 1028 | It should be possible to write code like: |
|
1016 | 1029 | \layout Standard |
|
1017 | 1030 | |
|
1018 | 1031 | >> for ext in ['.jpg','.gif']: |
|
1019 | 1032 | \newline |
|
1020 | 1033 | .. |
|
1021 | 1034 | ls file$ext |
|
1022 | 1035 | \layout Standard |
|
1023 | 1036 | |
|
1024 | 1037 | And have it work as 'ls file.jpg;ls file.gif'. |
|
1025 | 1038 | \layout Subsection |
|
1026 | 1039 | |
|
1027 | 1040 | Smaller details |
|
1028 | 1041 | \layout Standard |
|
1029 | 1042 | |
|
1030 | 1043 | If the above are considered as valid guiding principles for how such a python |
|
1031 | 1044 | system shell should behave, then some smaller considerations and comments |
|
1032 | 1045 | to keep in mind are listed below. |
|
1033 | 1046 | \layout Standard |
|
1034 | 1047 | |
|
1035 | 1048 | - it's ok for shell builtins (in this case this includes the python language) |
|
1036 | 1049 | to override system commands on the path. |
|
1037 | 1050 | See tcsh's 'time' vs '/usr/bin/time'. |
|
1038 | 1051 | This settles the 'print' issue and related. |
|
1039 | 1052 | \layout Standard |
|
1040 | 1053 | |
|
1041 | 1054 | - pysh should take |
|
1042 | 1055 | \layout Standard |
|
1043 | 1056 | |
|
1044 | 1057 | foo args |
|
1045 | 1058 | \layout Standard |
|
1046 | 1059 | |
|
1047 | 1060 | as a command if (foo args is NOT valid python) and (foo is in $PATH). |
|
1048 | 1061 | \layout Standard |
|
1049 | 1062 | |
|
1050 | 1063 | If the user types |
|
1051 | 1064 | \layout Standard |
|
1052 | 1065 | |
|
1053 | 1066 | >> ./foo args |
|
1054 | 1067 | \layout Standard |
|
1055 | 1068 | |
|
1056 | 1069 | it should be considered a system command always. |
|
1057 | 1070 | \layout Standard |
|
1058 | 1071 | |
|
1059 | 1072 | - _, __ and ___ should automatically remember the previous 3 outputs captured |
|
1060 | 1073 | from stdout. |
|
1061 | 1074 | In parallel, there should be _e, __e and ___e for stderr. |
|
1062 | 1075 | Whether capture is done as a single string or in list mode should be a |
|
1063 | 1076 | user preference. |
|
1064 | 1077 | If users have numbered prompts, ipython's full In/Out cache system should |
|
1065 | 1078 | be available. |
|
1066 | 1079 | \layout Standard |
|
1067 | 1080 | |
|
1068 | 1081 | But regardless of how variables are captured, the printout should be like |
|
1069 | 1082 | that of a plain shell (without quotes or braces to indicate strings/lists). |
|
1070 | 1083 | The everyday 'feel' of pysh should be more that of bash/tcsh than that |
|
1071 | 1084 | of ipython. |
|
1072 | 1085 | \layout Standard |
|
1073 | 1086 | |
|
1074 | 1087 | - filename completion first. |
|
1075 | 1088 | Tab completion could work like in ipython, but with the order of priorities |
|
1076 | 1089 | reversed: first files, then python names. |
|
1077 | 1090 | \layout Standard |
|
1078 | 1091 | |
|
1079 | 1092 | - configuration via standard python files. |
|
1080 | 1093 | Instead of 'setenv' you'd simply write into the os.environ[] dictionary. |
|
1081 | 1094 | This assumes that IPython itself has been fixed to be configured via normal |
|
1082 | 1095 | python files, instead of the current clunky ipythonrc format. |
|
1083 | 1096 | \layout Standard |
|
1084 | 1097 | |
|
1085 | 1098 | - IPython can already configure the prompt in fairly generic ways. |
|
1086 | 1099 | It should be able to generate almost any kind of prompt which bash/tcsh |
|
1087 | 1100 | can (within reason). |
|
1088 | 1101 | \layout Standard |
|
1089 | 1102 | |
|
1090 | 1103 | - Keep the Magics system. |
|
1091 | 1104 | They provide a lightweight syntax for configuring and modifying the state |
|
1092 | 1105 | of the user's session itself. |
|
1093 | 1106 | Plus, they are an extensible system so why not give the users one more |
|
1094 | 1107 | tool which is fairly flexible by nature? Finally, having the @magic optional |
|
1095 | 1108 | syntax allows a user to always be able to access the shell's control system, |
|
1096 | 1109 | regardless of name collisions with defined variables or system commands. |
|
1097 | 1110 | \layout Standard |
|
1098 | 1111 | |
|
1099 | 1112 | But we need to move all magic functionality into a protected namespace, |
|
1100 | 1113 | instead of the current messy name-mangling tricks (which don't scale well). |
|
1101 | 1114 | |
|
1102 | 1115 | \layout Section |
|
1103 | 1116 | |
|
1104 | 1117 | Future improvements |
|
1105 | 1118 | \layout Itemize |
|
1106 | 1119 | |
|
1107 | 1120 | When from <mod> import * is used, first check the existing namespace and |
|
1108 | 1121 | at least issue a warning on screen if names are overwritten. |
|
1109 | 1122 | \layout Itemize |
|
1110 | 1123 | |
|
1111 | 1124 | Auto indent? Done, for users with readline support. |
|
1112 | 1125 | \layout Subsection |
|
1113 | 1126 | |
|
1114 | 1127 | Better completion a la zsh |
|
1115 | 1128 | \layout Standard |
|
1116 | 1129 | |
|
1117 | 1130 | This was suggested by Arnd: |
|
1118 | 1131 | \layout Standard |
|
1119 | 1132 | |
|
1120 | 1133 | > >\SpecialChar ~ |
|
1121 | 1134 | \SpecialChar ~ |
|
1122 | 1135 | \SpecialChar ~ |
|
1123 | 1136 | More general, this might lead to something like |
|
1124 | 1137 | \layout Standard |
|
1125 | 1138 | |
|
1126 | 1139 | > >\SpecialChar ~ |
|
1127 | 1140 | \SpecialChar ~ |
|
1128 | 1141 | \SpecialChar ~ |
|
1129 | 1142 | command specific completion ? |
|
1130 | 1143 | \layout Standard |
|
1131 | 1144 | |
|
1132 | 1145 | > |
|
1133 | 1146 | \layout Standard |
|
1134 | 1147 | |
|
1135 | 1148 | > I'm not sure what you mean here. |
|
1136 | 1149 | \layout Standard |
|
1137 | 1150 | |
|
1138 | 1151 | \SpecialChar ~ |
|
1139 | 1152 | |
|
1140 | 1153 | \layout Standard |
|
1141 | 1154 | |
|
1142 | 1155 | Sorry, that was not understandable, indeed ... |
|
1143 | 1156 | \layout Standard |
|
1144 | 1157 | |
|
1145 | 1158 | I thought of something like |
|
1146 | 1159 | \layout Standard |
|
1147 | 1160 | |
|
1148 | 1161 | \SpecialChar ~ |
|
1149 | 1162 | - cd and then use TAB to go through the list of directories |
|
1150 | 1163 | \layout Standard |
|
1151 | 1164 | |
|
1152 | 1165 | \SpecialChar ~ |
|
1153 | 1166 | - ls and then TAB to consider all files and directories |
|
1154 | 1167 | \layout Standard |
|
1155 | 1168 | |
|
1156 | 1169 | \SpecialChar ~ |
|
1157 | 1170 | - cat and TAB: only files (no directories ...) |
|
1158 | 1171 | \layout Standard |
|
1159 | 1172 | |
|
1160 | 1173 | \SpecialChar ~ |
|
1161 | 1174 | |
|
1162 | 1175 | \layout Standard |
|
1163 | 1176 | |
|
1164 | 1177 | For zsh things like this are established by defining in .zshrc |
|
1165 | 1178 | \layout Standard |
|
1166 | 1179 | |
|
1167 | 1180 | \SpecialChar ~ |
|
1168 | 1181 | |
|
1169 | 1182 | \layout Standard |
|
1170 | 1183 | |
|
1171 | 1184 | compctl -g '*.dvi' xdvi |
|
1172 | 1185 | \layout Standard |
|
1173 | 1186 | |
|
1174 | 1187 | compctl -g '*.dvi' dvips |
|
1175 | 1188 | \layout Standard |
|
1176 | 1189 | |
|
1177 | 1190 | compctl -g '*.tex' latex |
|
1178 | 1191 | \layout Standard |
|
1179 | 1192 | |
|
1180 | 1193 | compctl -g '*.tex' tex |
|
1181 | 1194 | \layout Standard |
|
1182 | 1195 | |
|
1183 | 1196 | ... |
|
1184 | 1197 | \layout Section |
|
1185 | 1198 | |
|
1186 | 1199 | Outline of steps |
|
1187 | 1200 | \layout Standard |
|
1188 | 1201 | |
|
1189 | 1202 | Here's a rough outline of the order in which to start implementing the various |
|
1190 | 1203 | parts of the redesign. |
|
1191 | 1204 | The first 'test of success' should be a clean pychecker run (not the mess |
|
1192 | 1205 | we get right now). |
|
1193 | 1206 | \layout Itemize |
|
1194 | 1207 | |
|
1195 | 1208 | Make Logger and Magic not be mixins but attributes of the main class. |
|
1196 | 1209 | |
|
1197 | 1210 | \begin_deeper |
|
1198 | 1211 | \layout Itemize |
|
1199 | 1212 | |
|
1200 | 1213 | Magic should have a pointer back to the main instance (even if this creates |
|
1201 | 1214 | a recursive structure) so it can control it with minimal message-passing |
|
1202 | 1215 | machinery. |
|
1203 | 1216 | |
|
1204 | 1217 | \layout Itemize |
|
1205 | 1218 | |
|
1206 | 1219 | Logger can be a standalone object, simply with a nice, clean interface. |
|
1207 | 1220 | \layout Itemize |
|
1208 | 1221 | |
|
1209 | 1222 | Logger currently handles part of the prompt caching, but other parts of |
|
1210 | 1223 | that are in the prompts class itself. |
|
1211 | 1224 | Clean up. |
|
1212 | 1225 | \end_deeper |
|
1213 | 1226 | \layout Itemize |
|
1214 | 1227 | |
|
1215 | 1228 | Change to python-based config system. |
|
1216 | 1229 | \layout Itemize |
|
1217 | 1230 | |
|
1218 | 1231 | Move make_IPython() into the main shell class, as part of the constructor. |
|
1219 | 1232 | Do this |
|
1220 | 1233 | \emph on |
|
1221 | 1234 | after |
|
1222 | 1235 | \emph default |
|
1223 | 1236 | the config system has been changed, debugging will be a lot easier then. |
|
1224 | 1237 | \layout Itemize |
|
1225 | 1238 | |
|
1226 | 1239 | Merge the embeddable class and the normal one into one. |
|
1227 | 1240 | After all, the standard ipython script |
|
1228 | 1241 | \emph on |
|
1229 | 1242 | is |
|
1230 | 1243 | \emph default |
|
1231 | 1244 | a python program with IPython embedded in it. |
|
1232 | 1245 | There's no need for two separate classes ( |
|
1233 | 1246 | \emph on |
|
1234 | 1247 | maybe |
|
1235 | 1248 | \emph default |
|
1236 | 1249 | keep the old one around for the sake of backwards compatibility). |
|
1237 | 1250 | \layout Section |
|
1238 | 1251 | |
|
1239 | 1252 | Ville Vainio's suggestions |
|
1240 | 1253 | \layout Standard |
|
1241 | 1254 | |
|
1242 | 1255 | Some notes sent in by Ville Vainio |
|
1243 | 1256 | \family typewriter |
|
1244 | 1257 | <vivainio@kolumbus.fi> |
|
1245 | 1258 | \family default |
|
1246 | 1259 | on Tue, 29 Jun 2004. |
|
1247 | 1260 | Keep here for reference, some of it replicates things already said above. |
|
1248 | 1261 | \layout Standard |
|
1249 | 1262 | |
|
1250 | 1263 | Current ipython seems to "special case" lots of stuff - aliases, magics |
|
1251 | 1264 | etc. |
|
1252 | 1265 | It would seem to yield itself to a simpler and more extensible architecture, |
|
1253 | 1266 | consisting of multple dictionaries, where just the order of search is determine |
|
1254 | 1267 | d by profile/prefix. |
|
1255 | 1268 | All the functionality would just be "pushed" to ipython core, i.e. |
|
1256 | 1269 | the objects that represent the functionality are instantiated on "plugins" |
|
1257 | 1270 | and they are registered with ipython core. |
|
1258 | 1271 | i.e. |
|
1259 | 1272 | \layout Standard |
|
1260 | 1273 | |
|
1261 | 1274 | def magic_f(options, args): pass |
|
1262 | 1275 | \layout Standard |
|
1263 | 1276 | |
|
1264 | 1277 | m = MyMagic(magic_f) m.arghandler = stockhandlers.OptParseArgHandler m.options |
|
1265 | 1278 | = .... |
|
1266 | 1279 | # optparse options, for easy passing to magic_f and help display |
|
1267 | 1280 | \layout Standard |
|
1268 | 1281 | |
|
1269 | 1282 | # note that arghandler takes a peek at the instance, sees options, and proceeds |
|
1270 | 1283 | # accordingly. |
|
1271 | 1284 | Various arg handlers can ask for arbitrary options. |
|
1272 | 1285 | # some handler might optionally glob the filenames, search data folders |
|
1273 | 1286 | for filenames etc. |
|
1274 | 1287 | \layout Standard |
|
1275 | 1288 | |
|
1276 | 1289 | ipythonregistry.register(category = "magic", name = "mymagic", obj = m) |
|
1277 | 1290 | \layout Standard |
|
1278 | 1291 | |
|
1279 | 1292 | I bet most of the current functionality could easily be added to such a |
|
1280 | 1293 | registry by just instantiating e.g. |
|
1281 | 1294 | "Magic" class and registering all the functions with some sensible default |
|
1282 | 1295 | args. |
|
1283 | 1296 | Supporting legacy stuff in general would be easy - just implement new handlers |
|
1284 | 1297 | (arg and otherwise) for new stuff, and have the old handlers around forever |
|
1285 | 1298 | / as long as is deemed appropriate. |
|
1286 | 1299 | The 'python' namespace (locals() + globals()) should be special, of course. |
|
1287 | 1300 | \layout Standard |
|
1288 | 1301 | |
|
1289 | 1302 | It should be easy to have arbitrary number of "categories" (like 'magic', |
|
1290 | 1303 | 'shellcommand','projectspecific_myproject', 'projectspecific_otherproject'). |
|
1291 | 1304 | It would only influence the order in which the completions are suggested, |
|
1292 | 1305 | and in case of name collision which one is selected. |
|
1293 | 1306 | Also, I think all completions should be shown, even the ones in "later" |
|
1294 | 1307 | categories in the case of a match in an "earlier" category. |
|
1295 | 1308 | \layout Standard |
|
1296 | 1309 | |
|
1297 | 1310 | The "functionality object" might also have a callable object 'expandarg', |
|
1298 | 1311 | and ipython would run it (with the arg index) when tab completion is attempted |
|
1299 | 1312 | after typing the function name. |
|
1300 | 1313 | It would return the possible completions for that particular command... |
|
1301 | 1314 | or None to "revert to default file completions". |
|
1302 | 1315 | Such functionality could be useful in making ipython an "operating console" |
|
1303 | 1316 | of a sort. |
|
1304 | 1317 | I'm talking about: |
|
1305 | 1318 | \layout Standard |
|
1306 | 1319 | |
|
1307 | 1320 | >> lscat reactor # list commands in category - reactor is "project specific" |
|
1308 | 1321 | category |
|
1309 | 1322 | \layout Standard |
|
1310 | 1323 | |
|
1311 | 1324 | r_operate |
|
1312 | 1325 | \layout Standard |
|
1313 | 1326 | |
|
1314 | 1327 | >> r_operate <tab> start shutdown notify_meltdown evacuate |
|
1315 | 1328 | \layout Standard |
|
1316 | 1329 | |
|
1317 | 1330 | >> r_operate shutdown <tab> |
|
1318 | 1331 | \layout Standard |
|
1319 | 1332 | |
|
1320 | 1333 | 1 2 5 6 # note that 3 and 4 are already shut down |
|
1321 | 1334 | \layout Standard |
|
1322 | 1335 | |
|
1323 | 1336 | >> r_operate shutdown 2 |
|
1324 | 1337 | \layout Standard |
|
1325 | 1338 | |
|
1326 | 1339 | Shutting down.. |
|
1327 | 1340 | ok. |
|
1328 | 1341 | \layout Standard |
|
1329 | 1342 | |
|
1330 | 1343 | >> r_operate start <tab> |
|
1331 | 1344 | \layout Standard |
|
1332 | 1345 | |
|
1333 | 1346 | 2 3 4 # 2 was shut down, can be started now |
|
1334 | 1347 | \layout Standard |
|
1335 | 1348 | |
|
1336 | 1349 | >> r_operate start 2 |
|
1337 | 1350 | \layout Standard |
|
1338 | 1351 | |
|
1339 | 1352 | Starting.... |
|
1340 | 1353 | ok. |
|
1341 | 1354 | \layout Standard |
|
1342 | 1355 | |
|
1343 | 1356 | I'm talking about having a super-configurable man-machine language here! |
|
1344 | 1357 | Like cmd.Cmd on steroids, as a free addition to ipython! |
|
1345 | 1358 | \the_end |
General Comments 0
You need to be logged in to leave comments.
Login now