Show More
@@ -2,7 +2,7 b'' | |||||
2 | """ |
|
2 | """ | |
3 | Logger class for IPython's logging facilities. |
|
3 | Logger class for IPython's logging facilities. | |
4 |
|
4 | |||
5 |
$Id: Logger.py 98 |
|
5 | $Id: Logger.py 988 2006-01-02 21:21:47Z fperez $ | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #***************************************************************************** |
|
8 | #***************************************************************************** | |
@@ -172,7 +172,12 b' which already exists. But you must first start the logging process with' | |||||
172 | # update the auto _i tables |
|
172 | # update the auto _i tables | |
173 | #print '***logging line',line # dbg |
|
173 | #print '***logging line',line # dbg | |
174 | #print '***cache_count', self.shell.outputcache.prompt_count # dbg |
|
174 | #print '***cache_count', self.shell.outputcache.prompt_count # dbg | |
|
175 | try: | |||
175 | input_hist = self.shell.user_ns['_ih'] |
|
176 | input_hist = self.shell.user_ns['_ih'] | |
|
177 | except: | |||
|
178 | print 'userns:',self.shell.user_ns.keys() | |||
|
179 | return | |||
|
180 | ||||
176 | if not continuation and line: |
|
181 | if not continuation and line: | |
177 | self._iii = self._ii |
|
182 | self._iii = self._ii | |
178 | self._ii = self._i |
|
183 | self._ii = self._i |
@@ -1,7 +1,7 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Magic functions for InteractiveShell. |
|
2 | """Magic functions for InteractiveShell. | |
3 |
|
3 | |||
4 |
$Id: Magic.py 98 |
|
4 | $Id: Magic.py 988 2006-01-02 21:21:47Z fperez $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
@@ -742,12 +742,14 b' Currently the magic system has the following functions:\\n"""' | |||||
742 | arguments are returned.""" |
|
742 | arguments are returned.""" | |
743 |
|
743 | |||
744 | user_ns = self.shell.user_ns |
|
744 | user_ns = self.shell.user_ns | |
|
745 | internal_ns = self.shell.internal_ns | |||
|
746 | user_config_ns = self.shell.user_config_ns | |||
745 | out = [] |
|
747 | out = [] | |
746 | typelist = parameter_s.split() |
|
748 | typelist = parameter_s.split() | |
747 | for i in self.shell.user_ns.keys(): |
|
749 | ||
|
750 | for i in user_ns: | |||
748 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
751 | if not (i.startswith('_') or i.startswith('_i')) \ | |
749 |
and not ( |
|
752 | and not (i in internal_ns or i in user_config_ns): | |
750 | self.shell.user_config_ns.has_key(i)): |
|
|||
751 | if typelist: |
|
753 | if typelist: | |
752 | if type(user_ns[i]).__name__ in typelist: |
|
754 | if type(user_ns[i]).__name__ in typelist: | |
753 | out.append(i) |
|
755 | out.append(i) | |
@@ -1638,6 +1640,17 b' Currently the magic system has the following functions:\\n"""' | |||||
1638 | print 'The following commands were written to file `%s`:' % fname |
|
1640 | print 'The following commands were written to file `%s`:' % fname | |
1639 | print cmds |
|
1641 | print cmds | |
1640 |
|
1642 | |||
|
1643 | def _edit_macro(self,mname,macro): | |||
|
1644 | """open an editor with the macro data in a file""" | |||
|
1645 | filename = self.shell.mktempfile(macro.value) | |||
|
1646 | self.shell.hooks.editor(filename) | |||
|
1647 | ||||
|
1648 | # and make a new macro object, to replace the old one | |||
|
1649 | mfile = open(filename) | |||
|
1650 | mvalue = mfile.read() | |||
|
1651 | mfile.close() | |||
|
1652 | self.shell.user_ns[mname] = Macro(mvalue) | |||
|
1653 | ||||
1641 |
def magic_ed(self,parameter_s |
|
1654 | def magic_ed(self,parameter_s=''): | |
1642 | """Alias to %edit.""" |
|
1655 | """Alias to %edit.""" | |
1643 | return self.magic_edit(parameter_s) |
|
1656 | return self.magic_edit(parameter_s) | |
@@ -1695,6 +1708,10 b' Currently the magic system has the following functions:\\n"""' | |||||
1695 | to load an editor exactly at the point where 'function' is defined, |
|
1708 | to load an editor exactly at the point where 'function' is defined, | |
1696 | edit it and have the file be executed automatically. |
|
1709 | edit it and have the file be executed automatically. | |
1697 |
|
1710 | |||
|
1711 | If the object is a macro (see %macro for details), this opens up your | |||
|
1712 | specified editor with a temporary file containing the macro's data. | |||
|
1713 | Upon exit, the macro is reloaded with the contents of the file. | |||
|
1714 | ||||
1698 | Note: opening at an exact line is only supported under Unix, and some |
|
1715 | Note: opening at an exact line is only supported under Unix, and some | |
1699 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1716 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |
1700 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1717 | '+NUMBER' parameter necessary for this feature. Good editors like | |
@@ -1826,6 +1843,7 b' Currently the magic system has the following functions:\\n"""' | |||||
1826 | data = eval(args,self.shell.user_ns) |
|
1843 | data = eval(args,self.shell.user_ns) | |
1827 | if not type(data) in StringTypes: |
|
1844 | if not type(data) in StringTypes: | |
1828 | raise DataIsObject |
|
1845 | raise DataIsObject | |
|
1846 | ||||
1829 | except (NameError,SyntaxError): |
|
1847 | except (NameError,SyntaxError): | |
1830 | # given argument is not a variable, try as a filename |
|
1848 | # given argument is not a variable, try as a filename | |
1831 | filename = make_filename(args) |
|
1849 | filename = make_filename(args) | |
@@ -1833,9 +1851,16 b' Currently the magic system has the following functions:\\n"""' | |||||
1833 | warn("Argument given (%s) can't be found as a variable " |
|
1851 | warn("Argument given (%s) can't be found as a variable " | |
1834 | "or as a filename." % args) |
|
1852 | "or as a filename." % args) | |
1835 | return |
|
1853 | return | |
|
1854 | ||||
1836 | data = '' |
|
1855 | data = '' | |
1837 | use_temp = 0 |
|
1856 | use_temp = 0 | |
1838 | except DataIsObject: |
|
1857 | except DataIsObject: | |
|
1858 | ||||
|
1859 | # macros have a special edit function | |||
|
1860 | if isinstance(data,Macro): | |||
|
1861 | self._edit_macro(args,data) | |||
|
1862 | return | |||
|
1863 | ||||
1839 | # For objects, try to edit the file where they are defined |
|
1864 | # For objects, try to edit the file where they are defined | |
1840 | try: |
|
1865 | try: | |
1841 | filename = inspect.getabsfile(data) |
|
1866 | filename = inspect.getabsfile(data) | |
@@ -1861,13 +1886,7 b' Currently the magic system has the following functions:\\n"""' | |||||
1861 | data = '' |
|
1886 | data = '' | |
1862 |
|
1887 | |||
1863 | if use_temp: |
|
1888 | if use_temp: | |
1864 |
filename = |
|
1889 | filename = self.shell.mktempfile(data) | |
1865 | self.shell.tempfiles.append(filename) |
|
|||
1866 |
|
||||
1867 | if data and use_temp: |
|
|||
1868 | tmp_file = open(filename,'w') |
|
|||
1869 | tmp_file.write(data) |
|
|||
1870 | tmp_file.close() |
|
|||
1871 |
|
1890 | |||
1872 | # do actual editing here |
|
1891 | # do actual editing here | |
1873 | print 'Editing...', |
|
1892 | print 'Editing...', | |
@@ -1887,9 +1906,6 b' Currently the magic system has the following functions:\\n"""' | |||||
1887 | self.shell.showtraceback() |
|
1906 | self.shell.showtraceback() | |
1888 | except: |
|
1907 | except: | |
1889 | self.shell.showtraceback() |
|
1908 | self.shell.showtraceback() | |
1890 | if use_temp: |
|
|||
1891 | contents = open(filename).read() |
|
|||
1892 | return contents |
|
|||
1893 |
|
1909 | |||
1894 | def magic_xmode(self,parameter_s = ''): |
|
1910 | def magic_xmode(self,parameter_s = ''): | |
1895 | """Switch modes for the exception handlers. |
|
1911 | """Switch modes for the exception handlers. | |
@@ -2570,7 +2586,7 b' Defaulting color scheme to \'NoColor\'"""' | |||||
2570 |
|
2586 | |||
2571 | %store - Show list of all variables and their current values\\ |
|
2587 | %store - Show list of all variables and their current values\\ | |
2572 | %store <var> - Store the *current* value of the variable to disk\\ |
|
2588 | %store <var> - Store the *current* value of the variable to disk\\ | |
2573 |
%store -d |
|
2589 | %store -d <var> - Remove the variable and its value from storage\\ | |
2574 | %store -r - Remove all variables from storage |
|
2590 | %store -r - Remove all variables from storage | |
2575 |
|
2591 | |||
2576 | It should be noted that if you change the value of a variable, you |
|
2592 | It should be noted that if you change the value of a variable, you |
@@ -1,7 +1,7 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Release data for the IPython project. |
|
2 | """Release data for the IPython project. | |
3 |
|
3 | |||
4 |
$Id: Release.py 98 |
|
4 | $Id: Release.py 988 2006-01-02 21:21:47Z fperez $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu> |
|
7 | # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu> | |
@@ -22,9 +22,9 b" name = 'ipython'" | |||||
22 | # because bdist_rpm does not accept dashes (an RPM) convention, and |
|
22 | # because bdist_rpm does not accept dashes (an RPM) convention, and | |
23 | # bdist_deb does not accept underscores (a Debian convention). |
|
23 | # bdist_deb does not accept underscores (a Debian convention). | |
24 |
|
24 | |||
25 |
version = '0.7.0.rc |
|
25 | version = '0.7.0.rc7' | |
26 |
|
26 | |||
27 |
revision = '$Revision: 98 |
|
27 | revision = '$Revision: 988 $' | |
28 |
|
28 | |||
29 | description = "An enhanced interactive Python shell." |
|
29 | description = "An enhanced interactive Python shell." | |
30 |
|
30 |
@@ -192,10 +192,6 b' class Completer:' | |||||
192 | except: |
|
192 | except: | |
193 | object = eval(expr, self.global_namespace) |
|
193 | object = eval(expr, self.global_namespace) | |
194 |
|
194 | |||
195 | # for modules which define __all__, complete only on those. |
|
|||
196 | if type(object) == types.ModuleType and hasattr(object, '__all__'): |
|
|||
197 | words = getattr(object, '__all__') |
|
|||
198 | else: |
|
|||
199 |
|
|
195 | words = dir(object) | |
200 |
|
|
196 | if hasattr(object,'__class__'): | |
201 |
|
|
197 | words.append('__class__') |
@@ -32,7 +32,7 b" ip_set_hook('editor',myiphooks.calljed)" | |||||
32 | The ip_set_hook function is put by IPython into the builtin namespace, so it |
|
32 | The ip_set_hook function is put by IPython into the builtin namespace, so it | |
33 | is always available from all running code. |
|
33 | is always available from all running code. | |
34 |
|
34 | |||
35 |
$Id: hooks.py 9 |
|
35 | $Id: hooks.py 988 2006-01-02 21:21:47Z fperez $""" | |
36 |
|
36 | |||
37 | #***************************************************************************** |
|
37 | #***************************************************************************** | |
38 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> |
|
38 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> | |
@@ -52,7 +52,7 b' import os' | |||||
52 | # but over time we'll move here all the public API for user-accessible things. |
|
52 | # but over time we'll move here all the public API for user-accessible things. | |
53 | __all__ = ['editor', 'fix_error_editor'] |
|
53 | __all__ = ['editor', 'fix_error_editor'] | |
54 |
|
54 | |||
55 | def editor(self,filename, linenum): |
|
55 | def editor(self,filename, linenum=None): | |
56 | """Open the default editor at the given filename and linenumber. |
|
56 | """Open the default editor at the given filename and linenumber. | |
57 |
|
57 | |||
58 | This is IPython's default editor hook, you can use it as an example to |
|
58 | This is IPython's default editor hook, you can use it as an example to |
@@ -6,7 +6,7 b' Requires Python 2.1 or newer.' | |||||
6 |
|
6 | |||
7 | This file contains all the classes and helper functions specific to IPython. |
|
7 | This file contains all the classes and helper functions specific to IPython. | |
8 |
|
8 | |||
9 |
$Id: iplib.py 98 |
|
9 | $Id: iplib.py 988 2006-01-02 21:21:47Z fperez $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
@@ -55,6 +55,7 b' import re' | |||||
55 | import shutil |
|
55 | import shutil | |
56 | import string |
|
56 | import string | |
57 | import sys |
|
57 | import sys | |
|
58 | import tempfile | |||
58 | import traceback |
|
59 | import traceback | |
59 | import types |
|
60 | import types | |
60 |
|
61 | |||
@@ -100,80 +101,6 b' def softspace(file, newvalue):' | |||||
100 | return oldvalue |
|
101 | return oldvalue | |
101 |
|
102 | |||
102 | #**************************************************************************** |
|
103 | #**************************************************************************** | |
103 | # These special functions get installed in the builtin namespace, to provide |
|
|||
104 | # programmatic (pure python) access to magics, aliases and system calls. This |
|
|||
105 | # is important for logging, user scripting, and more. |
|
|||
106 |
|
||||
107 | # We are basically exposing, via normal python functions, the three mechanisms |
|
|||
108 | # in which ipython offers special call modes (magics for internal control, |
|
|||
109 | # aliases for direct system access via pre-selected names, and !cmd for |
|
|||
110 | # calling arbitrary system commands). |
|
|||
111 |
|
||||
112 | def ipmagic(arg_s): |
|
|||
113 | """Call a magic function by name. |
|
|||
114 |
|
||||
115 | Input: a string containing the name of the magic function to call and any |
|
|||
116 | additional arguments to be passed to the magic. |
|
|||
117 |
|
||||
118 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
|||
119 | prompt: |
|
|||
120 |
|
||||
121 | In[1]: %name -opt foo bar |
|
|||
122 |
|
||||
123 | To call a magic without arguments, simply use ipmagic('name'). |
|
|||
124 |
|
||||
125 | This provides a proper Python function to call IPython's magics in any |
|
|||
126 | valid Python code you can type at the interpreter, including loops and |
|
|||
127 | compound statements. It is added by IPython to the Python builtin |
|
|||
128 | namespace upon initialization.""" |
|
|||
129 |
|
||||
130 | args = arg_s.split(' ',1) |
|
|||
131 | magic_name = args[0] |
|
|||
132 | if magic_name.startswith(__IPYTHON__.ESC_MAGIC): |
|
|||
133 | magic_name = magic_name[1:] |
|
|||
134 | try: |
|
|||
135 | magic_args = args[1] |
|
|||
136 | except IndexError: |
|
|||
137 | magic_args = '' |
|
|||
138 | fn = getattr(__IPYTHON__,'magic_'+magic_name,None) |
|
|||
139 | if fn is None: |
|
|||
140 | error("Magic function `%s` not found." % magic_name) |
|
|||
141 | else: |
|
|||
142 | magic_args = __IPYTHON__.var_expand(magic_args) |
|
|||
143 | return fn(magic_args) |
|
|||
144 |
|
||||
145 | def ipalias(arg_s): |
|
|||
146 | """Call an alias by name. |
|
|||
147 |
|
||||
148 | Input: a string containing the name of the alias to call and any |
|
|||
149 | additional arguments to be passed to the magic. |
|
|||
150 |
|
||||
151 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
|||
152 | prompt: |
|
|||
153 |
|
||||
154 | In[1]: name -opt foo bar |
|
|||
155 |
|
||||
156 | To call an alias without arguments, simply use ipalias('name'). |
|
|||
157 |
|
||||
158 | This provides a proper Python function to call IPython's aliases in any |
|
|||
159 | valid Python code you can type at the interpreter, including loops and |
|
|||
160 | compound statements. It is added by IPython to the Python builtin |
|
|||
161 | namespace upon initialization.""" |
|
|||
162 |
|
||||
163 | args = arg_s.split(' ',1) |
|
|||
164 | alias_name = args[0] |
|
|||
165 | try: |
|
|||
166 | alias_args = args[1] |
|
|||
167 | except IndexError: |
|
|||
168 | alias_args = '' |
|
|||
169 | if alias_name in __IPYTHON__.alias_table: |
|
|||
170 | __IPYTHON__.call_alias(alias_name,alias_args) |
|
|||
171 | else: |
|
|||
172 | error("Alias `%s` not found." % alias_name) |
|
|||
173 |
|
||||
174 | def ipsystem(arg_s): |
|
|||
175 | """Make a system call, using IPython.""" |
|
|||
176 | __IPYTHON__.system(arg_s) |
|
|||
177 |
|
104 | |||
178 |
|
105 | |||
179 | #**************************************************************************** |
|
106 | #**************************************************************************** | |
@@ -184,6 +111,8 b' class SpaceInInput(exceptions.Exception): pass' | |||||
184 | # Local use classes |
|
111 | # Local use classes | |
185 | class Bunch: pass |
|
112 | class Bunch: pass | |
186 |
|
113 | |||
|
114 | class Undefined: pass | |||
|
115 | ||||
187 | class InputList(list): |
|
116 | class InputList(list): | |
188 | """Class to store user input. |
|
117 | """Class to store user input. | |
189 |
|
118 | |||
@@ -255,27 +184,17 b' class InteractiveShell(object,Magic):' | |||||
255 | if ns is not None and type(ns) != types.DictType: |
|
184 | if ns is not None and type(ns) != types.DictType: | |
256 | raise TypeError,'namespace must be a dictionary' |
|
185 | raise TypeError,'namespace must be a dictionary' | |
257 |
|
186 | |||
258 | # Put a reference to self in builtins so that any form of embedded or |
|
187 | # Job manager (for jobs run as background threads) | |
259 | # imported code can test for being inside IPython. |
|
188 | self.jobs = BackgroundJobManager() | |
260 | __builtin__.__IPYTHON__ = self |
|
|||
261 |
|
||||
262 | # And load into builtins ipmagic/ipalias/ipsystem as well |
|
|||
263 | __builtin__.ipmagic = ipmagic |
|
|||
264 | __builtin__.ipalias = ipalias |
|
|||
265 | __builtin__.ipsystem = ipsystem |
|
|||
266 |
|
||||
267 | # Add to __builtin__ other parts of IPython's public API |
|
|||
268 | __builtin__.ip_set_hook = self.set_hook |
|
|||
269 |
|
189 | |||
270 | # Keep in the builtins a flag for when IPython is active. We set it |
|
190 | # track which builtins we add, so we can clean up later | |
271 | # with setdefault so that multiple nested IPythons don't clobber one |
|
191 | self.builtins_added = {} | |
272 | # another. Each will increase its value by one upon being activated, |
|
192 | # This method will add the necessary builtins for operation, but | |
273 | # which also gives us a way to determine the nesting level. |
|
193 | # tracking what it did via the builtins_added dict. | |
274 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
194 | self.add_builtins() | |
275 |
|
195 | |||
276 | # Do the intuitively correct thing for quit/exit: we remove the |
|
196 | # Do the intuitively correct thing for quit/exit: we remove the | |
277 |
# builtins if they exist, and our own |
|
197 | # builtins if they exist, and our own magics will deal with this | |
278 | # these special cases |
|
|||
279 | try: |
|
198 | try: | |
280 | del __builtin__.exit, __builtin__.quit |
|
199 | del __builtin__.exit, __builtin__.quit | |
281 | except AttributeError: |
|
200 | except AttributeError: | |
@@ -434,11 +353,6 b' class InteractiveShell(object,Magic):' | |||||
434 | # item which gets cleared once run. |
|
353 | # item which gets cleared once run. | |
435 | self.code_to_run = None |
|
354 | self.code_to_run = None | |
436 |
|
355 | |||
437 | # Job manager (for jobs run as background threads) |
|
|||
438 | self.jobs = BackgroundJobManager() |
|
|||
439 | # Put the job manager into builtins so it's always there. |
|
|||
440 | __builtin__.jobs = self.jobs |
|
|||
441 |
|
||||
442 | # escapes for automatic behavior on the command line |
|
356 | # escapes for automatic behavior on the command line | |
443 | self.ESC_SHELL = '!' |
|
357 | self.ESC_SHELL = '!' | |
444 | self.ESC_HELP = '?' |
|
358 | self.ESC_HELP = '?' | |
@@ -726,6 +640,45 b' class InteractiveShell(object,Magic):' | |||||
726 |
|
640 | |||
727 | self.user_ns[key] = obj |
|
641 | self.user_ns[key] = obj | |
728 |
|
642 | |||
|
643 | def add_builtins(self): | |||
|
644 | """Store ipython references into the builtin namespace. | |||
|
645 | ||||
|
646 | Some parts of ipython operate via builtins injected here, which hold a | |||
|
647 | reference to IPython itself.""" | |||
|
648 | ||||
|
649 | builtins_new = dict(__IPYTHON__ = self, | |||
|
650 | ip_set_hook = self.set_hook, | |||
|
651 | jobs = self.jobs, | |||
|
652 | ipmagic = self.ipmagic, | |||
|
653 | ipalias = self.ipalias, | |||
|
654 | ipsystem = self.ipsystem, | |||
|
655 | ) | |||
|
656 | for biname,bival in builtins_new.items(): | |||
|
657 | try: | |||
|
658 | # store the orignal value so we can restore it | |||
|
659 | self.builtins_added[biname] = __builtin__.__dict__[biname] | |||
|
660 | except KeyError: | |||
|
661 | # or mark that it wasn't defined, and we'll just delete it at | |||
|
662 | # cleanup | |||
|
663 | self.builtins_added[biname] = Undefined | |||
|
664 | __builtin__.__dict__[biname] = bival | |||
|
665 | ||||
|
666 | # Keep in the builtins a flag for when IPython is active. We set it | |||
|
667 | # with setdefault so that multiple nested IPythons don't clobber one | |||
|
668 | # another. Each will increase its value by one upon being activated, | |||
|
669 | # which also gives us a way to determine the nesting level. | |||
|
670 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |||
|
671 | ||||
|
672 | def clean_builtins(self): | |||
|
673 | """Remove any builtins which might have been added by add_builtins, or | |||
|
674 | restore overwritten ones to their previous values.""" | |||
|
675 | for biname,bival in self.builtins_added.items(): | |||
|
676 | if bival is Undefined: | |||
|
677 | del __builtin__.__dict__[biname] | |||
|
678 | else: | |||
|
679 | __builtin__.__dict__[biname] = bival | |||
|
680 | self.builtins_added.clear() | |||
|
681 | ||||
729 | def set_hook(self,name,hook): |
|
682 | def set_hook(self,name,hook): | |
730 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
683 | """set_hook(name,hook) -> sets an internal IPython hook. | |
731 |
|
684 | |||
@@ -815,6 +768,82 b' class InteractiveShell(object,Magic):' | |||||
815 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
768 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
816 | 'Control auto-activation of pdb at exceptions') |
|
769 | 'Control auto-activation of pdb at exceptions') | |
817 |
|
770 | |||
|
771 | ||||
|
772 | # These special functions get installed in the builtin namespace, to | |||
|
773 | # provide programmatic (pure python) access to magics, aliases and system | |||
|
774 | # calls. This is important for logging, user scripting, and more. | |||
|
775 | ||||
|
776 | # We are basically exposing, via normal python functions, the three | |||
|
777 | # mechanisms in which ipython offers special call modes (magics for | |||
|
778 | # internal control, aliases for direct system access via pre-selected | |||
|
779 | # names, and !cmd for calling arbitrary system commands). | |||
|
780 | ||||
|
781 | def ipmagic(self,arg_s): | |||
|
782 | """Call a magic function by name. | |||
|
783 | ||||
|
784 | Input: a string containing the name of the magic function to call and any | |||
|
785 | additional arguments to be passed to the magic. | |||
|
786 | ||||
|
787 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython | |||
|
788 | prompt: | |||
|
789 | ||||
|
790 | In[1]: %name -opt foo bar | |||
|
791 | ||||
|
792 | To call a magic without arguments, simply use ipmagic('name'). | |||
|
793 | ||||
|
794 | This provides a proper Python function to call IPython's magics in any | |||
|
795 | valid Python code you can type at the interpreter, including loops and | |||
|
796 | compound statements. It is added by IPython to the Python builtin | |||
|
797 | namespace upon initialization.""" | |||
|
798 | ||||
|
799 | args = arg_s.split(' ',1) | |||
|
800 | magic_name = args[0] | |||
|
801 | if magic_name.startswith(self.ESC_MAGIC): | |||
|
802 | magic_name = magic_name[1:] | |||
|
803 | try: | |||
|
804 | magic_args = args[1] | |||
|
805 | except IndexError: | |||
|
806 | magic_args = '' | |||
|
807 | fn = getattr(self,'magic_'+magic_name,None) | |||
|
808 | if fn is None: | |||
|
809 | error("Magic function `%s` not found." % magic_name) | |||
|
810 | else: | |||
|
811 | magic_args = self.var_expand(magic_args) | |||
|
812 | return fn(magic_args) | |||
|
813 | ||||
|
814 | def ipalias(self,arg_s): | |||
|
815 | """Call an alias by name. | |||
|
816 | ||||
|
817 | Input: a string containing the name of the alias to call and any | |||
|
818 | additional arguments to be passed to the magic. | |||
|
819 | ||||
|
820 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |||
|
821 | prompt: | |||
|
822 | ||||
|
823 | In[1]: name -opt foo bar | |||
|
824 | ||||
|
825 | To call an alias without arguments, simply use ipalias('name'). | |||
|
826 | ||||
|
827 | This provides a proper Python function to call IPython's aliases in any | |||
|
828 | valid Python code you can type at the interpreter, including loops and | |||
|
829 | compound statements. It is added by IPython to the Python builtin | |||
|
830 | namespace upon initialization.""" | |||
|
831 | ||||
|
832 | args = arg_s.split(' ',1) | |||
|
833 | alias_name = args[0] | |||
|
834 | try: | |||
|
835 | alias_args = args[1] | |||
|
836 | except IndexError: | |||
|
837 | alias_args = '' | |||
|
838 | if alias_name in self.alias_table: | |||
|
839 | self.call_alias(alias_name,alias_args) | |||
|
840 | else: | |||
|
841 | error("Alias `%s` not found." % alias_name) | |||
|
842 | ||||
|
843 | def ipsystem(self,arg_s): | |||
|
844 | """Make a system call, using IPython.""" | |||
|
845 | self.system(arg_s) | |||
|
846 | ||||
818 | def complete(self,text): |
|
847 | def complete(self,text): | |
819 | """Return a sorted list of all possible completions on text. |
|
848 | """Return a sorted list of all possible completions on text. | |
820 |
|
849 | |||
@@ -1287,9 +1316,18 b' want to merge them back into the new files.""" % locals()' | |||||
1287 | global_ns = call_frame.f_globals |
|
1316 | global_ns = call_frame.f_globals | |
1288 |
|
1317 | |||
1289 | # Update namespaces and fire up interpreter |
|
1318 | # Update namespaces and fire up interpreter | |
1290 | self.user_ns = local_ns |
|
1319 | ||
|
1320 | # The global one is easy, we can just throw it in | |||
1291 | self.user_global_ns = global_ns |
|
1321 | self.user_global_ns = global_ns | |
1292 |
|
1322 | |||
|
1323 | # but the user/local one is tricky: ipython needs it to store internal | |||
|
1324 | # data, but we also need the locals. We'll copy locals in the user | |||
|
1325 | # one, but will track what got copied so we can delete them at exit. | |||
|
1326 | # This is so that a later embedded call doesn't see locals from a | |||
|
1327 | # previous call (which most likely existed in a separate scope). | |||
|
1328 | local_varnames = local_ns.keys() | |||
|
1329 | self.user_ns.update(local_ns) | |||
|
1330 | ||||
1293 | # Patch for global embedding to make sure that things don't overwrite |
|
1331 | # Patch for global embedding to make sure that things don't overwrite | |
1294 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1332 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
1295 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1333 | # FIXME. Test this a bit more carefully (the if.. is new) | |
@@ -1300,8 +1338,21 b' want to merge them back into the new files.""" % locals()' | |||||
1300 | # actually completes using the frame's locals/globals |
|
1338 | # actually completes using the frame's locals/globals | |
1301 | self.set_completer_frame(call_frame) |
|
1339 | self.set_completer_frame(call_frame) | |
1302 |
|
1340 | |||
|
1341 | # before activating the interactive mode, we need to make sure that | |||
|
1342 | # all names in the builtin namespace needed by ipython point to | |||
|
1343 | # ourselves, and not to other instances. | |||
|
1344 | self.add_builtins() | |||
|
1345 | ||||
1303 | self.interact(header) |
|
1346 | self.interact(header) | |
1304 |
|
1347 | |||
|
1348 | # now, purge out the user namespace from anything we might have added | |||
|
1349 | # from the caller's local namespace | |||
|
1350 | delvar = self.user_ns.pop | |||
|
1351 | for var in local_varnames: | |||
|
1352 | delvar(var,None) | |||
|
1353 | # and clean builtins we may have overridden | |||
|
1354 | self.clean_builtins() | |||
|
1355 | ||||
1305 | def interact(self, banner=None): |
|
1356 | def interact(self, banner=None): | |
1306 | """Closely emulate the interactive Python console. |
|
1357 | """Closely emulate the interactive Python console. | |
1307 |
|
1358 | |||
@@ -1327,7 +1378,9 b' want to merge them back into the new files.""" % locals()' | |||||
1327 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1378 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1328 |
|
1379 | |||
1329 | # exit_now is set by a call to %Exit or %Quit |
|
1380 | # exit_now is set by a call to %Exit or %Quit | |
|
1381 | self.exit_now = False | |||
1330 | while not self.exit_now: |
|
1382 | while not self.exit_now: | |
|
1383 | ||||
1331 | try: |
|
1384 | try: | |
1332 | if more: |
|
1385 | if more: | |
1333 | prompt = self.outputcache.prompt2 |
|
1386 | prompt = self.outputcache.prompt2 | |
@@ -1942,6 +1995,26 b' want to merge them back into the new files.""" % locals()' | |||||
1942 |
|
1995 | |||
1943 | return line |
|
1996 | return line | |
1944 |
|
1997 | |||
|
1998 | def mktempfile(self,data=None): | |||
|
1999 | """Make a new tempfile and return its filename. | |||
|
2000 | ||||
|
2001 | This makes a call to tempfile.mktemp, but it registers the created | |||
|
2002 | filename internally so ipython cleans it up at exit time. | |||
|
2003 | ||||
|
2004 | Optional inputs: | |||
|
2005 | ||||
|
2006 | - data(None): if data is given, it gets written out to the temp file | |||
|
2007 | immediately, and the file is closed again.""" | |||
|
2008 | ||||
|
2009 | filename = tempfile.mktemp('.py') | |||
|
2010 | self.tempfiles.append(filename) | |||
|
2011 | ||||
|
2012 | if data: | |||
|
2013 | tmp_file = open(filename,'w') | |||
|
2014 | tmp_file.write(data) | |||
|
2015 | tmp_file.close() | |||
|
2016 | return filename | |||
|
2017 | ||||
1945 | def write(self,data): |
|
2018 | def write(self,data): | |
1946 | """Write a string to the default output""" |
|
2019 | """Write a string to the default output""" | |
1947 | Term.cout.write(data) |
|
2020 | Term.cout.write(data) |
@@ -60,7 +60,7 b' You can implement other color schemes easily, the syntax is fairly' | |||||
60 | self-explanatory. Please send back new schemes you develop to the author for |
|
60 | self-explanatory. Please send back new schemes you develop to the author for | |
61 | possible inclusion in future releases. |
|
61 | possible inclusion in future releases. | |
62 |
|
62 | |||
63 |
$Id: ultraTB.py 9 |
|
63 | $Id: ultraTB.py 988 2006-01-02 21:21:47Z fperez $""" | |
64 |
|
64 | |||
65 | #***************************************************************************** |
|
65 | #***************************************************************************** | |
66 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
66 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
@@ -95,9 +95,14 b' from IPython.Struct import Struct' | |||||
95 | from IPython.excolors import ExceptionColors |
|
95 | from IPython.excolors import ExceptionColors | |
96 | from IPython.genutils import Term,uniq_stable,error,info |
|
96 | from IPython.genutils import Term,uniq_stable,error,info | |
97 |
|
97 | |||
|
98 | # Globals | |||
|
99 | # amount of space to put line numbers before verbose tracebacks | |||
|
100 | INDENT_SIZE = 8 | |||
|
101 | ||||
98 | #--------------------------------------------------------------------------- |
|
102 | #--------------------------------------------------------------------------- | |
99 | # Code begins |
|
103 | # Code begins | |
100 |
|
104 | |||
|
105 | # Utility functions | |||
101 | def inspect_error(): |
|
106 | def inspect_error(): | |
102 | """Print a message about internal inspect errors. |
|
107 | """Print a message about internal inspect errors. | |
103 |
|
108 | |||
@@ -106,6 +111,76 b' def inspect_error():' | |||||
106 | error('Internal Python error in the inspect module.\n' |
|
111 | error('Internal Python error in the inspect module.\n' | |
107 | 'Below is the traceback from this internal error.\n') |
|
112 | 'Below is the traceback from this internal error.\n') | |
108 |
|
113 | |||
|
114 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): | |||
|
115 | import linecache | |||
|
116 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |||
|
117 | ||||
|
118 | records = inspect.getinnerframes(etb, context) | |||
|
119 | ||||
|
120 | # If the error is at the console, don't build any context, since it would | |||
|
121 | # otherwise produce 5 blank lines printed out (there is no file at the | |||
|
122 | # console) | |||
|
123 | rec_check = records[tb_offset:] | |||
|
124 | rname = rec_check[0][1] | |||
|
125 | if rname == '<ipython console>' or rname.endswith('<string>'): | |||
|
126 | return rec_check | |||
|
127 | ||||
|
128 | aux = traceback.extract_tb(etb) | |||
|
129 | assert len(records) == len(aux) | |||
|
130 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |||
|
131 | maybeStart = lnum-1 - context//2 | |||
|
132 | start = max(maybeStart, 0) | |||
|
133 | end = start + context | |||
|
134 | lines = linecache.getlines(file)[start:end] | |||
|
135 | # pad with empty lines if necessary | |||
|
136 | if maybeStart < 0: | |||
|
137 | lines = (['\n'] * -maybeStart) + lines | |||
|
138 | if len(lines) < context: | |||
|
139 | lines += ['\n'] * (context - len(lines)) | |||
|
140 | assert len(lines) == context | |||
|
141 | buf = list(records[i]) | |||
|
142 | buf[LNUM_POS] = lnum | |||
|
143 | buf[INDEX_POS] = lnum - 1 - start | |||
|
144 | buf[LINES_POS] = lines | |||
|
145 | records[i] = tuple(buf) | |||
|
146 | return records[tb_offset:] | |||
|
147 | ||||
|
148 | # Helper function -- largely belongs to VerboseTB, but we need the same | |||
|
149 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |||
|
150 | # can be recognized properly by ipython.el's py-traceback-line-re | |||
|
151 | # (SyntaxErrors have to be treated specially because they have no traceback) | |||
|
152 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None): | |||
|
153 | numbers_width = INDENT_SIZE - 1 | |||
|
154 | res = [] | |||
|
155 | i = lnum - index | |||
|
156 | for line in lines: | |||
|
157 | if i == lnum: | |||
|
158 | # This is the line with the error | |||
|
159 | pad = numbers_width - len(str(i)) | |||
|
160 | if pad >= 3: | |||
|
161 | marker = '-'*(pad-3) + '-> ' | |||
|
162 | elif pad == 2: | |||
|
163 | marker = '> ' | |||
|
164 | elif pad == 1: | |||
|
165 | marker = '>' | |||
|
166 | else: | |||
|
167 | marker = '' | |||
|
168 | num = marker + str(i) | |||
|
169 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, | |||
|
170 | Colors.line, line, Colors.Normal) | |||
|
171 | else: | |||
|
172 | num = '%*s' % (numbers_width,i) | |||
|
173 | line = '%s%s%s %s' %(Colors.lineno, num, | |||
|
174 | Colors.Normal, line) | |||
|
175 | ||||
|
176 | res.append(line) | |||
|
177 | if lvals and i == lnum: | |||
|
178 | res.append(lvals + '\n') | |||
|
179 | i = i + 1 | |||
|
180 | return res | |||
|
181 | ||||
|
182 | #--------------------------------------------------------------------------- | |||
|
183 | # Module classes | |||
109 | class TBTools: |
|
184 | class TBTools: | |
110 | """Basic tools used by all traceback printer classes.""" |
|
185 | """Basic tools used by all traceback printer classes.""" | |
111 |
|
186 | |||
@@ -316,9 +391,7 b' class VerboseTB(TBTools):' | |||||
316 | # some locals |
|
391 | # some locals | |
317 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
392 | Colors = self.Colors # just a shorthand + quicker name lookup | |
318 | ColorsNormal = Colors.Normal # used a lot |
|
393 | ColorsNormal = Colors.Normal # used a lot | |
319 | indent_size = 8 # we need some space to put line numbers before |
|
394 | indent = ' '*INDENT_SIZE | |
320 | indent = ' '*indent_size |
|
|||
321 | numbers_width = indent_size - 1 # leave space between numbers & code |
|
|||
322 | text_repr = pydoc.text.repr |
|
395 | text_repr = pydoc.text.repr | |
323 | exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal) |
|
396 | exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal) | |
324 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
397 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) | |
@@ -353,7 +426,13 b' class VerboseTB(TBTools):' | |||||
353 | linecache.checkcache() |
|
426 | linecache.checkcache() | |
354 | # Drop topmost frames if requested |
|
427 | # Drop topmost frames if requested | |
355 | try: |
|
428 | try: | |
356 | records = inspect.getinnerframes(etb, context)[self.tb_offset:] |
|
429 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
|
430 | # problems, but it generates empty tracebacks for console errors | |||
|
431 | # (5 blanks lines) where none should be returned. | |||
|
432 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] | |||
|
433 | #print 'python records:', records # dbg | |||
|
434 | records = _fixed_getinnerframes(etb, context,self.tb_offset) | |||
|
435 | #print 'alex records:', records # dbg | |||
357 | except: |
|
436 | except: | |
358 |
|
437 | |||
359 | # FIXME: I've been getting many crash reports from python 2.3 |
|
438 | # FIXME: I've been getting many crash reports from python 2.3 | |
@@ -519,32 +598,12 b' class VerboseTB(TBTools):' | |||||
519 | lvals = '' |
|
598 | lvals = '' | |
520 |
|
599 | |||
521 | level = '%s %s\n' % (link,call) |
|
600 | level = '%s %s\n' % (link,call) | |
522 | excerpt = [] |
|
|||
523 | if index is not None: |
|
|||
524 | i = lnum - index |
|
|||
525 | for line in lines: |
|
|||
526 | if i == lnum: |
|
|||
527 | # This is the line with the error |
|
|||
528 | pad = numbers_width - len(str(i)) |
|
|||
529 | if pad >= 3: |
|
|||
530 | marker = '-'*(pad-3) + '-> ' |
|
|||
531 | elif pad == 2: |
|
|||
532 | marker = '> ' |
|
|||
533 | elif pad == 1: |
|
|||
534 | marker = '>' |
|
|||
535 | else: |
|
|||
536 | marker = '' |
|
|||
537 | num = '%s%s' % (marker,i) |
|
|||
538 | line = tpl_line_em % (num,line) |
|
|||
539 | else: |
|
|||
540 | num = '%*s' % (numbers_width,i) |
|
|||
541 | line = tpl_line % (num,line) |
|
|||
542 |
|
601 | |||
543 | excerpt.append(line) |
|
602 | if index is None: | |
544 | if self.include_vars and i == lnum: |
|
603 | frames.append(level) | |
545 | excerpt.append('%s\n' % lvals) |
|
604 | else: | |
546 | i += 1 |
|
605 | frames.append('%s%s' % (level,''.join( | |
547 | frames.append('%s%s' % (level,''.join(excerpt)) ) |
|
606 | _formatTracebackLines(lnum,index,lines,self.Colors,lvals)))) | |
548 |
|
607 | |||
549 | # Get (safely) a string form of the exception info |
|
608 | # Get (safely) a string form of the exception info | |
550 | try: |
|
609 | try: |
@@ -1,3 +1,26 b'' | |||||
|
1 | 2006-01-02 Fernando Perez <Fernando.Perez@colorado.edu> | |||
|
2 | ||||
|
3 | * IPython/ultraTB.py (_fixed_getinnerframes): added Alex | |||
|
4 | Schmolck's patch to fix inspect.getinnerframes(). | |||
|
5 | ||||
|
6 | * IPython/iplib.py (InteractiveShell.__init__): significant fixes | |||
|
7 | for embedded instances, regarding handling of namespaces and items | |||
|
8 | added to the __builtin__ one. Multiple embedded instances and | |||
|
9 | recursive embeddings should work better now (though I'm not sure | |||
|
10 | I've got all the corner cases fixed, that code is a bit of a brain | |||
|
11 | twister). | |||
|
12 | ||||
|
13 | * IPython/Magic.py (magic_edit): added support to edit in-memory | |||
|
14 | macros (automatically creates the necessary temp files). %edit | |||
|
15 | also doesn't return the file contents anymore, it's just noise. | |||
|
16 | ||||
|
17 | * IPython/completer.py (Completer.attr_matches): revert change to | |||
|
18 | complete only on attributes listed in __all__. I realized it | |||
|
19 | cripples the tab-completion system as a tool for exploring the | |||
|
20 | internals of unknown libraries (it renders any non-__all__ | |||
|
21 | attribute off-limits). I got bit by this when trying to see | |||
|
22 | something inside the dis module. | |||
|
23 | ||||
1 | 2005-12-31 Fernando Perez <Fernando.Perez@colorado.edu> |
|
24 | 2005-12-31 Fernando Perez <Fernando.Perez@colorado.edu> | |
2 |
|
25 | |||
3 | * IPython/iplib.py (InteractiveShell.__init__): add .meta |
|
26 | * IPython/iplib.py (InteractiveShell.__init__): add .meta |
@@ -27,7 +27,8 b' else:' | |||||
27 | print "The prompts for the nested copy have been modified" |
|
27 | print "The prompts for the nested copy have been modified" | |
28 | nested = 1 |
|
28 | nested = 1 | |
29 | # what the embedded instance will see as sys.argv: |
|
29 | # what the embedded instance will see as sys.argv: | |
30 |
args = ['-pi1','In <\\#>:','-pi2',' .\\D.:', |
|
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
|
31 | '-po','Out<\\#>: ','-nosep'] | |||
31 |
|
32 | |||
32 | # First import the embeddable shell class |
|
33 | # First import the embeddable shell class | |
33 | from IPython.Shell import IPShellEmbed |
|
34 | from IPython.Shell import IPShellEmbed | |
@@ -44,7 +45,8 b' ipshell = IPShellEmbed(args,' | |||||
44 | if nested: |
|
45 | if nested: | |
45 | args[1] = 'In2<\\#>' |
|
46 | args[1] = 'In2<\\#>' | |
46 | else: |
|
47 | else: | |
47 |
args = ['-pi1','In2<\\#>:','-pi2',' .\\D.:', |
|
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
|
49 | '-po','Out<\\#>: ','-nosep'] | |||
48 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
49 |
|
51 | |||
50 | print '\nHello. This is printed from the main controller program.\n' |
|
52 | print '\nHello. This is printed from the main controller program.\n' |
General Comments 0
You need to be logged in to leave comments.
Login now