##// END OF EJS Templates
- New profile with doctest support (two way: for generating doctests and for...
fperez -
Show More
@@ -0,0 +1,40 b''
1 """Config file for 'doctest' profile.
2
3 This profile modifies the prompts to be the standard Python ones, so that you
4 can generate easily doctests from an IPython session.
5
6 But more importantly, it enables pasting of code with '>>>' prompts and
7 arbitrary initial whitespace, as is typical of doctests in reST files and
8 docstrings. This allows you to easily re-run existing doctests and iteratively
9 work on them as part of your development workflow.
10
11 The exception mode is also set to 'plain' so the generated exceptions are as
12 similar as possible to the default Python ones, for inclusion in doctests."""
13
14 # get various stuff that are there for historical / familiarity reasons
15 import ipy_legacy
16
17 from IPython import ipapi
18
19 from IPython.Extensions import InterpreterPasteInput
20
21 def main():
22 ip = ipapi.get()
23 o = ip.options
24
25 # Set the prompts similar to the defaults
26 o.prompt_in1 = '>>> '
27 o.prompt_in2 = '... '
28 o.prompt_out = ''
29
30 # No separation between successive inputs
31 o.separate_in = ''
32
33 # Disable pprint, so that outputs are printed as similarly to standard
34 # python as possible
35 o.pprint = 0
36
37 # Use plain exceptions, to also resemble normal pyhton.
38 o.xmode = 'plain'
39
40 main()
@@ -31,7 +31,25 b' The >>> and ... are stripped from the input so that the python interpreter'
31 only sees the real part of the code.
31 only sees the real part of the code.
32
32
33 All other input is processed normally.
33 All other input is processed normally.
34
35 Notes
36 =====
37
38 * You can even paste code that has extra initial spaces, such as is common in
39 doctests:
40
41 In [3]: >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
42
43 In [4]: >>> for i in range(len(a)):
44 ...: ... print i, a[i]
45 ...: ...
46 0 Mary
47 1 had
48 2 a
49 3 little
50 4 lamb
34 """
51 """
52
35 #*****************************************************************************
53 #*****************************************************************************
36 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
54 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
37 #
55 #
@@ -60,23 +78,32 b' __license__ = Release.license'
60 # The prototype of any alternate prefilter must be like this one (the name
78 # The prototype of any alternate prefilter must be like this one (the name
61 # doesn't matter):
79 # doesn't matter):
62 # - line is a string containing the user input line.
80 # - line is a string containing the user input line.
63 # - continuation is a parameter which tells us if we are processing a first line of
81 # - continuation is a parameter which tells us if we are processing a first
64 # user input or the second or higher of a multi-line statement.
82 # line of user input or the second or higher of a multi-line statement.
83
84 import re
85
86 PROMPT_RE = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
65
87
66 def prefilter_paste(self,line,continuation):
88 def prefilter_paste(self,line,continuation):
67 """Alternate prefilter for input of pasted code from an interpreter.
89 """Alternate prefilter for input of pasted code from an interpreter.
68 """
90 """
69
91 if not line:
70 from re import match
92 return ''
71
93 m = PROMPT_RE.match(line)
72 if match(r'^>>> |^\.\.\. ',line):
94 if m:
73 # In the end, always call the default IPython _prefilter() function.
95 # In the end, always call the default IPython _prefilter() function.
74 # Note that self must be passed explicitly, b/c we're calling the
96 # Note that self must be passed explicitly, b/c we're calling the
75 # unbound class method (since this method will overwrite the instance
97 # unbound class method (since this method will overwrite the instance
76 # prefilter())
98 # prefilter())
77 return self._prefilter(line[4:],continuation)
99 return self._prefilter(line[len(m.group(0)):],continuation)
78 elif line.strip() == '...':
100 elif line.strip() == '...':
79 return self._prefilter('',continuation)
101 return self._prefilter('',continuation)
102 elif line.isspace():
103 # This allows us to recognize multiple input prompts separated by blank
104 # lines and pasted in a single chunk, very common when pasting doctests
105 # or long tutorial passages.
106 return ''
80 else:
107 else:
81 return self._prefilter(line,continuation)
108 return self._prefilter(line,continuation)
82
109
@@ -6,7 +6,7 b' Requires Python 2.3 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 2577 2007-08-02 23:50:02Z fperez $
9 $Id: iplib.py 2581 2007-08-04 20:52:05Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -2059,6 +2059,17 b' want to merge them back into the new files.""" % locals()'
2059
2059
2060 #print '***line: <%s>' % line # dbg
2060 #print '***line: <%s>' % line # dbg
2061
2061
2062 if not line:
2063 # Return immediately on purely empty lines, so that if the user
2064 # previously typed some whitespace that started a continuation
2065 # prompt, he can break out of that loop with just an empty line.
2066 # This is how the default python prompt works.
2067
2068 # Only return if the accumulated input buffer was just whitespace!
2069 if ''.join(self.buffer).isspace():
2070 self.buffer[:] = []
2071 return ''
2072
2062 line_info = prefilter.LineInfo(line, continue_prompt)
2073 line_info = prefilter.LineInfo(line, continue_prompt)
2063
2074
2064 # the input history needs to track even empty lines
2075 # the input history needs to track even empty lines
@@ -1,3 +1,24 b''
1 2007-08-04 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Extensions/ipy_profile_doctest.py: New profile for
4 doctest support. It sets prompts/exceptions as similar to
5 standard Python as possible, so that ipython sessions in this
6 profile can be easily pasted as doctests with minimal
7 modifications. It also enables pasting of doctests from external
8 sources (even if they have leading whitespace), so that you can
9 rerun doctests from existing sources.
10
11 * IPython/iplib.py (_prefilter): fix a buglet where after entering
12 some whitespace, the prompt would become a continuation prompt
13 with no way of exiting it other than Ctrl-C. This fix brings us
14 into conformity with how the default python prompt works.
15
16 * IPython/Extensions/InterpreterPasteInput.py (prefilter_paste):
17 Add support for pasting not only lines that start with '>>>', but
18 also with ' >>>'. That is, arbitrary whitespace can now precede
19 the prompts. This makes the system useful for pasting doctests
20 from docstrings back into a normal session.
21
1 2007-08-02 Fernando Perez <Fernando.Perez@colorado.edu>
22 2007-08-02 Fernando Perez <Fernando.Perez@colorado.edu>
2
23
3 * IPython/Shell.py (IPShellEmbed.__call__): fix bug introduced in
24 * IPython/Shell.py (IPShellEmbed.__call__): fix bug introduced in
General Comments 0
You need to be logged in to leave comments. Login now