##// END OF EJS Templates
- thread-safety fixes...
fperez -
Show More
@@ -0,0 +1,23 b''
1 """Support for interactive macros in IPython"""
2
3 #*****************************************************************************
4 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
5 #
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
8 #*****************************************************************************
9
10 class Macro:
11 """Simple class to store the value of macros as strings.
12
13 This allows us to later exec them by checking when something is an
14 instance of this class."""
15
16 def __init__(self,data):
17
18 # store the macro value, as a single string which can be evaluated by
19 # runlines()
20 self.value = ''.join(data).rstrip()+'\n'
21
22 def __str__(self):
23 return self.value
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 974 2005-12-29 19:48:33Z fperez $"""
4 $Id: Magic.py 975 2005-12-29 23:50:22Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -47,6 +47,7 b' from IPython.FakeModule import FakeModule'
47 47 from IPython.Itpl import Itpl, itpl, printpl,itplns
48 48 from IPython.PyColorize import Parser
49 49 from IPython.Struct import Struct
50 from IPython.macro import Macro
50 51 from IPython.genutils import *
51 52
52 53 #***************************************************************************
@@ -56,18 +57,6 b' def on_off(tag):'
56 57 return ['OFF','ON'][tag]
57 58
58 59
59 #****************************************************************************
60 # Utility classes
61 class Macro(list):
62 """Simple class to store the value of macros as strings.
63
64 This allows us to later exec them by checking when something is an
65 instance of this class."""
66
67 def __init__(self,data):
68 list.__init__(self,data)
69 self.value = ''.join(data)
70
71 60 #***************************************************************************
72 61 # Main class implementing Magic functionality
73 62 class Magic:
@@ -224,13 +213,12 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")'
224 213
225 214 return {'found':found, 'obj':obj, 'namespace':ospace,
226 215 'ismagic':ismagic, 'isalias':isalias}
227
216
228 217 def arg_err(self,func):
229 218 """Print docstring if incorrect arguments were passed"""
230 219 print 'Error in arguments:'
231 220 print OInspect.getdoc(func)
232 221
233
234 222 def format_latex(self,strng):
235 223 """Format a string for latex inclusion."""
236 224
@@ -845,7 +833,15 b' Currently the magic system has the following functions:\\n"""'
845 833 get_vars = lambda i: self.shell.user_ns[i]
846 834 type_name = lambda v: type(v).__name__
847 835 varlist = map(get_vars,varnames)
848 typelist = map(type_name,varlist)
836
837 typelist = []
838 for vv in varlist:
839 tt = type_name(vv)
840 if tt=='instance':
841 typelist.append(str(vv.__class__))
842 else:
843 typelist.append(tt)
844
849 845 # column labels and # of spaces as separator
850 846 varlabel = 'Variable'
851 847 typelabel = 'Type'
@@ -881,7 +877,7 b' Currently the magic system has the following functions:\\n"""'
881 877 else:
882 878 print '(%s Mb)' % (vbytes/Mb,)
883 879 else:
884 vstr = str(var)
880 vstr = str(var).replace('\n','\\n')
885 881 if len(vstr) < 50:
886 882 print vstr
887 883 else:
@@ -1593,7 +1589,7 b' Currently the magic system has the following functions:\\n"""'
1593 1589 self.shell.user_ns.update({name:macro})
1594 1590 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1595 1591 print 'Macro contents:'
1596 print macro
1592 print macro,
1597 1593
1598 1594 def magic_save(self,parameter_s = ''):
1599 1595 """Save a set of lines to a given filename.
@@ -2,7 +2,7 b''
2 2 """
3 3 Classes for handling input/output prompts.
4 4
5 $Id: Prompts.py 966 2005-12-29 08:34:07Z fperez $"""
5 $Id: Prompts.py 975 2005-12-29 23:50:22Z fperez $"""
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
@@ -26,11 +26,11 b' import time'
26 26 from pprint import pprint,pformat
27 27
28 28 # IPython's own
29 from IPython.genutils import *
30 from IPython.Struct import Struct
31 from IPython.Magic import Macro
32 from IPython.Itpl import ItplNS
33 29 from IPython import ColorANSI
30 from IPython.Itpl import ItplNS
31 from IPython.Struct import Struct
32 from IPython.macro import Macro
33 from IPython.genutils import *
34 34
35 35 #****************************************************************************
36 36 #Color schemes for Prompts.
@@ -6,7 +6,7 b' 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 968 2005-12-29 17:15:38Z fperez $
9 $Id: iplib.py 975 2005-12-29 23:50:22Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -78,6 +78,10 b' from IPython.genutils import *'
78 78 # overwrites it (like wx.py.PyShell does)
79 79 raw_input_original = raw_input
80 80
81 # compiled regexps for autoindent management
82 ini_spaces_re = re.compile(r'^(\s+)')
83 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
84
81 85 #****************************************************************************
82 86 # Some utility function definitions
83 87
@@ -1171,10 +1175,8 b' want to merge them back into the new files.""" % locals()'
1171 1175 of what was there before (because Python's parser always uses
1172 1176 "<string>" when reading from a string).
1173 1177 """
1174 type, value, sys.last_traceback = sys.exc_info()
1175 sys.last_type = type
1176 sys.last_value = value
1177 if filename and type is SyntaxError:
1178 etype, value, last_traceback = sys.exc_info()
1179 if filename and etype is SyntaxError:
1178 1180 # Work hard to stuff the correct filename in the exception
1179 1181 try:
1180 1182 msg, (dummy_filename, lineno, offset, line) = value
@@ -1189,7 +1191,7 b' want to merge them back into the new files.""" % locals()'
1189 1191 except:
1190 1192 # If that failed, assume SyntaxError is a string
1191 1193 value = msg, (filename, lineno, offset, line)
1192 self.SyntaxTB(type,value,[])
1194 self.SyntaxTB(etype,value,[])
1193 1195
1194 1196 def debugger(self):
1195 1197 """Call the pdb debugger."""
@@ -1210,9 +1212,6 b' want to merge them back into the new files.""" % locals()'
1210 1212 if type is SyntaxError:
1211 1213 self.showsyntaxerror(filename)
1212 1214 else:
1213 sys.last_type = type
1214 sys.last_value = value
1215 sys.last_traceback = tb
1216 1215 self.InteractiveTB()
1217 1216 if self.InteractiveTB.call_pdb and self.has_readline:
1218 1217 # pdb mucks up readline, fix it back
@@ -1220,7 +1219,10 b' want to merge them back into the new files.""" % locals()'
1220 1219
1221 1220 def update_cache(self, line):
1222 1221 """puts line into cache"""
1223 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1222 return # dbg
1223
1224 # This copies the cache every time ... :-(
1225 self.inputcache.insert(0, line)
1224 1226 if len(self.inputcache) >= self.CACHELENGTH:
1225 1227 self.inputcache.pop() # This doesn't :-)
1226 1228
@@ -1319,10 +1321,6 b' want to merge them back into the new files.""" % locals()'
1319 1321 # Mark activity in the builtins
1320 1322 __builtin__.__dict__['__IPYTHON__active'] += 1
1321 1323
1322 # compiled regexps for autoindent management
1323 ini_spaces_re = re.compile(r'^(\s+)')
1324 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
1325
1326 1324 # exit_now is set by a call to %Exit or %Quit
1327 1325 while not self.exit_now:
1328 1326 try:
@@ -1343,26 +1341,6 b' want to merge them back into the new files.""" % locals()'
1343 1341 self.exit()
1344 1342 else:
1345 1343 more = self.push(line)
1346 # Auto-indent management
1347 if self.autoindent:
1348 if line:
1349 ini_spaces = ini_spaces_re.match(line)
1350 if ini_spaces:
1351 nspaces = ini_spaces.end()
1352 else:
1353 nspaces = 0
1354 self.indent_current_nsp = nspaces
1355
1356 if line[-1] == ':':
1357 self.indent_current_nsp += 4
1358 elif dedent_re.match(line):
1359 self.indent_current_nsp -= 4
1360 else:
1361 self.indent_current_nsp = 0
1362
1363 # indent_current is the actual string to be inserted
1364 # by the readline hooks for indentation
1365 self.indent_current = ' '* self.indent_current_nsp
1366 1344
1367 1345 if (self.SyntaxTB.last_syntax_error and
1368 1346 self.rc.autoedit_syntax):
@@ -1445,6 +1423,28 b' want to merge them back into the new files.""" % locals()'
1445 1423 except:
1446 1424 self.showtraceback()
1447 1425
1426 def autoindent_update(self,line):
1427 """Keep track of the indent level."""
1428 if self.autoindent:
1429 if line:
1430 ini_spaces = ini_spaces_re.match(line)
1431 if ini_spaces:
1432 nspaces = ini_spaces.end()
1433 else:
1434 nspaces = 0
1435 self.indent_current_nsp = nspaces
1436
1437 if line[-1] == ':':
1438 self.indent_current_nsp += 4
1439 elif dedent_re.match(line):
1440 self.indent_current_nsp -= 4
1441 else:
1442 self.indent_current_nsp = 0
1443
1444 # indent_current is the actual string to be inserted
1445 # by the readline hooks for indentation
1446 self.indent_current = ' '* self.indent_current_nsp
1447
1448 1448 def runlines(self,lines):
1449 1449 """Run a string of one or more lines of source.
1450 1450
@@ -1462,8 +1462,11 b' want to merge them back into the new files.""" % locals()'
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 #print 'rl line:<%s>' % line # dbg
1465 1466 if line or more:
1466 more = self.push((self.prefilter(line,more)))
1467 #print 'doit' # dbg
1468 newline = self.prefilter(line,more)
1469 more = self.push(newline)
1467 1470 # IPython's runsource returns None if there was an error
1468 1471 # compiling the code. This allows us to stop processing right
1469 1472 # away, so the user gets the error message at the right place.
@@ -1716,6 +1719,8 b' want to merge them back into the new files.""" % locals()'
1716 1719 # Let's try to find if the input line is a magic fn
1717 1720 oinfo = None
1718 1721 if hasattr(self,'magic_'+iFun):
1722 # WARNING: _ofind uses getattr(), so it can consume generators and
1723 # cause other side effects.
1719 1724 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1720 1725 if oinfo['ismagic']:
1721 1726 # Be careful not to call magics when a variable assignment is
@@ -60,7 +60,7 b' You can implement other color schemes easily, the syntax is fairly'
60 60 self-explanatory. Please send back new schemes you develop to the author for
61 61 possible inclusion in future releases.
62 62
63 $Id: ultraTB.py 965 2005-12-28 23:23:09Z fperez $"""
63 $Id: ultraTB.py 975 2005-12-29 23:50:22Z fperez $"""
64 64
65 65 #*****************************************************************************
66 66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
@@ -225,12 +225,12 b' class ListTB(TBTools):'
225 225 """Format the exception part of a traceback.
226 226
227 227 The arguments are the exception type and value such as given by
228 sys.last_type and sys.last_value. The return value is a list of
229 strings, each ending in a newline. Normally, the list contains a
230 single string; however, for SyntaxError exceptions, it contains
231 several lines that (when printed) display detailed information
232 about where the syntax error occurred. The message indicating
233 which exception occurred is the always last string in the list.
228 sys.exc_info()[:2]. The return value is a list of strings, each ending
229 in a newline. Normally, the list contains a single string; however,
230 for SyntaxError exceptions, it contains several lines that (when
231 printed) display detailed information about where the syntax error
232 occurred. The message indicating which exception occurred is the
233 always last string in the list.
234 234
235 235 Also lifted nearly verbatim from traceback.py
236 236 """
@@ -1,8 +1,26 b''
1 1 2005-12-29 Fernando Perez <Fernando.Perez@colorado.edu>
2 2
3 * IPython/iplib.py (showtraceback): remove use of the
4 sys.last_{type/value/traceback} structures, which are non
5 thread-safe.
6
7 * IPython/macro.py (Macro.__init__): moved macros to a standalone
8 file. Now that they'll be more likely to be used with the
9 persistance system (%store), I want to make sure their module path
10 doesn't change in the future, so that we don't break things for
11 users' persisted data.
12
13 * IPython/iplib.py (autoindent_update): move indentation
14 management into the _text_ processing loop, not the keyboard
15 interactive one. This is necessary to correctly process non-typed
16 multiline input (such as macros).
17
3 18 * IPython/Magic.py (Magic.format_latex): patch by Stefan van der
4 19 Walt <stefan-AT-sun.ac.za> to fix latex formatting of docstrings,
5 20 which was producing problems in the resulting manual.
21 (magic_whos): improve reporting of instances (show their class,
22 instead of simply printing 'instance' which isn't terribly
23 informative).
6 24
7 25 * IPython/genutils.py (shell): commit Jorgen Stenarson's patch
8 26 (minor mods) to support network shares under win32.
General Comments 0
You need to be logged in to leave comments. Login now