##// END OF EJS Templates
- New dtutils module for running doctests interactively with more...
fperez -
Show More
@@ -0,0 +1,137 b''
1 """Doctest-related utilities for IPython.
2
3 For most common uses, all you should need to run is::
4
5 from IPython.dtutils import idoctest
6
7 See the idoctest docstring below for usage details.
8 """
9
10 import doctest
11 import sys
12
13 import IPython.ipapi
14 ip = IPython.ipapi.get()
15
16 def rundoctest(text,ns=None,eraise=False):
17 """Run a the input source as a doctest, in the caller's namespace.
18
19 :Parameters:
20 text : str
21 Source to execute.
22
23 :Keywords:
24 ns : dict (None)
25 Namespace where the code should be executed. If not given, the
26 caller's locals and globals are used.
27 eraise : bool (False)
28 If true, immediately raise any exceptions instead of reporting them at
29 the end. This allows you to then do interactive debugging via
30 IPython's facilities (use %debug after the fact, or with %pdb for
31 automatic activation).
32 """
33
34 name = 'interactive doctest'
35 filename = '<IPython console>'
36
37 if eraise:
38 runner = doctest.DebugRunner()
39 else:
40 runner = doctest.DocTestRunner()
41
42 parser = doctest.DocTestParser()
43 if ns is None:
44 f = sys._getframe(1)
45 ns = f.f_globals.copy()
46 ns.update(f.f_locals)
47
48 test = parser.get_doctest(text,ns,name,filename,0)
49 runner.run(test)
50 runner.summarize(True)
51
52
53 def idoctest(ns=None,eraise=False):
54 """Interactively prompt for input and run it as a doctest.
55
56 To finish entering input, enter two blank lines or Ctrl-D (EOF). If you
57 use Ctrl-C, the example is aborted and all input discarded.
58
59 :Keywords:
60 ns : dict (None)
61 Namespace where the code should be executed. If not given, the IPython
62 interactive namespace is used.
63 eraise : bool (False)
64 If true, immediately raise any exceptions instead of reporting them at
65 the end. This allows you to then do interactive debugging via
66 IPython's facilities (use %debug after the fact, or with %pdb for
67 automatic activation).
68 end_mark : str ('--')
69 String to explicitly indicate the end of input.
70
71 """
72
73 inlines = []
74 empty_lines = 0 # count consecutive empty lines
75 run_test = True
76
77 if ns is None:
78 ns = ip.user_ns
79
80 ip.IP.savehist()
81 try:
82 while True:
83 line = raw_input()
84 if not line or line.isspace():
85 empty_lines += 1
86 else:
87 empty_lines = 0
88
89 if empty_lines>=2:
90 break
91
92 inlines.append(line)
93 except EOFError:
94 pass
95 except KeyboardInterrupt:
96 print "KeyboardInterrupt - Discarding input."
97 run_test = False
98
99 ip.IP.reloadhist()
100
101 if run_test:
102 # Extra blank line at the end to ensure that the final docstring has a
103 # closing newline
104 inlines.append('')
105 rundoctest('\n'.join(inlines),ns,eraise)
106
107
108 # For debugging of this module itself.
109 if __name__ == "__main__":
110 t = """
111 >>> for i in range(10):
112 ... print i,
113 ...
114 0 1 2 3 4 5 6 7 8 9
115 """
116
117 t2 = """
118 A simple example::
119
120 >>> for i in range(10):
121 ... print i,
122 ...
123 0 1 2 3 4 5 6 7 8 9
124
125 Some more details::
126
127 >>> print "hello"
128 hello
129 """
130
131 t3 = """
132 A failing example::
133
134 >>> x=1
135 >>> x+1
136 3
137 """
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 2874 2007-11-26 06:50:42Z fperez $"""
4 $Id: Magic.py 2899 2007-12-28 08:32:59Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1566,8 +1566,7 b' Currently the magic system has the following functions:\\n"""'
1566 1566
1567 1567 stats = None
1568 1568 try:
1569 if self.shell.has_readline:
1570 self.shell.savehist()
1569 self.shell.savehist()
1571 1570
1572 1571 if opts.has_key('p'):
1573 1572 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
@@ -2,7 +2,7 b''
2 2 """
3 3 Classes for handling input/output prompts.
4 4
5 $Id: Prompts.py 2855 2007-11-06 06:53:49Z vivainio $"""
5 $Id: Prompts.py 2899 2007-12-28 08:32:59Z fperez $"""
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -323,6 +323,13 b' class BasePrompt(object):'
323 323 else:
324 324 return os.sep
325 325
326 def __nonzero__(self):
327 """Implement boolean behavior.
328
329 Checks whether the p_str attribute is non-empty"""
330
331 return bool(self.p_template)
332
326 333 class Prompt1(BasePrompt):
327 334 """Input interactive prompt similar to Mathematica's."""
328 335
@@ -32,7 +32,7 b" ip.set_hook('editor', calljed)"
32 32 You can then enable the functionality by doing 'import myiphooks'
33 33 somewhere in your configuration files or ipython command line.
34 34
35 $Id: hooks.py 1854 2006-10-30 19:54:25Z vivainio $"""
35 $Id: hooks.py 2899 2007-12-28 08:32:59Z fperez $"""
36 36
37 37 #*****************************************************************************
38 38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
@@ -213,5 +213,3 b' def generate_prompt(self, is_continuation):'
213 213 def generate_output_prompt(self):
214 214 ip = self.api
215 215 return str(ip.IP.outputcache.prompt_out)
216
217 No newline at end of file
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 2894 2007-12-13 20:34:23Z vivainio $
9 $Id: iplib.py 2899 2007-12-28 08:32:59Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -1247,6 +1247,10 b' want to merge them back into the new files.""" % locals()'
1247 1247
1248 1248 def savehist(self):
1249 1249 """Save input history to a file (via readline library)."""
1250
1251 if not self.has_readline:
1252 return
1253
1250 1254 try:
1251 1255 self.readline.write_history_file(self.histfile)
1252 1256 except:
@@ -6,7 +6,7 b' Requires Python 2.1 or better.'
6 6
7 7 This file contains the main make_IPython() starter function.
8 8
9 $Id: ipmaker.py 2887 2007-12-12 08:28:43Z fperez $"""
9 $Id: ipmaker.py 2899 2007-12-28 08:32:59Z fperez $"""
10 10
11 11 #*****************************************************************************
12 12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -190,25 +190,26 b" object? -> Details about 'object'. ?object also works, ?? prints more."
190 190 autoindent = 0,
191 191 automagic = 1,
192 192 banner = 1,
193 cache_size = 1000,
194 193 c = '',
194 cache_size = 1000,
195 195 classic = 0,
196 colors = 'NoColor',
197 196 color_info = 0,
197 colors = 'NoColor',
198 198 confirm_exit = 1,
199 199 debug = 0,
200 200 deep_reload = 0,
201 201 editor = '0',
202 gthread = 0,
202 203 help = 0,
203 204 interact = 1,
204 205 ipythondir = ipythondir_def,
205 206 log = 0,
206 207 logfile = '',
207 208 logplay = '',
208 multi_line_specials = 1,
209 209 messages = 1,
210 object_info_string_level = 0,
210 multi_line_specials = 1,
211 211 nosep = 0,
212 object_info_string_level = 0,
212 213 pdb = 0,
213 214 pprint = 0,
214 215 profile = '',
@@ -216,31 +217,30 b" object? -> Details about 'object'. ?object also works, ?? prints more."
216 217 prompt_in2 = ' .\\D.: ',
217 218 prompt_out = 'Out[\\#]: ',
218 219 prompts_pad_left = 1,
220 pylab = 0,
219 221 pylab_import_all = 1,
220 quiet = 0,
222 q4thread = 0,
223 qthread = 0,
221 224 quick = 0,
225 quiet = 0,
226 rcfile = 'ipythonrc' + rc_suffix,
222 227 readline = 1,
223 228 readline_merge_completions = 1,
224 229 readline_omit__names = 0,
225 rcfile = 'ipythonrc' + rc_suffix,
226 230 screen_length = 0,
227 231 separate_in = '\n',
228 232 separate_out = '\n',
229 233 separate_out2 = '',
230 234 system_header = 'IPython system call: ',
231 235 system_verbose = 0,
232 gthread = 0,
233 qthread = 0,
234 q4thread = 0,
235 wthread = 0,
236 pylab = 0,
237 236 term_title = 1,
238 237 tk = 0,
239 238 upgrade = 0,
240 239 Version = 0,
241 xmode = 'Verbose',
242 240 wildcards_case_sensitive = 1,
241 wthread = 0,
243 242 wxversion = '0',
243 xmode = 'Context',
244 244 magic_docstrings = 0, # undocumented, for doc generation
245 245 )
246 246
@@ -1,9 +1,21 b''
1 2007-12-28 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/dtutils.py: Add utilities for interactively running
4 doctests. Still needs work to more easily handle the namespace of
5 the package one may be working on, but the basics are in place.
6
1 7 2007-12-20 Ville Vainio <vivainio@gmail.com>
2 8
3 9 * completer.py, generics.py(complete_object): Allow
4 10 custom complers based on python objects via simplegeneric.
5 11 See generics.py / my_demo_complete_object
6 12
13 2007-12-13 Fernando Perez <Fernando.Perez@colorado.edu>
14
15 * IPython/Prompts.py (BasePrompt.__nonzero__): add proper boolean
16 behavior to prompt objects, useful for display hooks to adjust
17 themselves depending on whether prompts will be there or not.
18
7 19 2007-12-13 Ville Vainio <vivainio@gmail.com>
8 20
9 21 * iplib.py(raw_input): unix readline does not allow unicode in
General Comments 0
You need to be logged in to leave comments. Login now