Show More
@@ -1,5 +1,50 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Module containing custom doctests. |
|
2 | Module containing additional handlers for the IPython directive's @doctest. | |
|
3 | ||||
|
4 | The Sphinx extension that provides support for embedded IPython code provides | |||
|
5 | a pseudo-decorator @doctest, which treats the input/output block as a | |||
|
6 | doctest, raising a RuntimeError during doc generation if the actual output | |||
|
7 | (after running the input) does not match the expected output. | |||
|
8 | ||||
|
9 | An example usage is: | |||
|
10 | ||||
|
11 | .. code-block:: rst | |||
|
12 | ||||
|
13 | .. ipython:: | |||
|
14 | ||||
|
15 | In [1]: x = 1 | |||
|
16 | ||||
|
17 | @doctest | |||
|
18 | In [2]: x + 2 | |||
|
19 | Out[3]: 3 | |||
|
20 | ||||
|
21 | One can also provide arguments to the decorator. The first argument should be | |||
|
22 | the name of a custom handler. The specification of any other arguments is | |||
|
23 | determined by the handler. For example, | |||
|
24 | ||||
|
25 | .. code-block:: rst | |||
|
26 | ||||
|
27 | .. ipython:: | |||
|
28 | ||||
|
29 | @doctest float | |||
|
30 | In [154]: 0.1 + 0.2 | |||
|
31 | Out[154]: 0.3 | |||
|
32 | ||||
|
33 | allows the actual output ``0.30000000000000004`` to match the expected output | |||
|
34 | due to a comparison with `np.allclose`. | |||
|
35 | ||||
|
36 | This module contains handlers for the @doctest pseudo-decorator. Handlers | |||
|
37 | should have the following function signature:: | |||
|
38 | ||||
|
39 | handler(sphinx_shell, args, input_lines, found, submitted) | |||
|
40 | ||||
|
41 | where `sphinx_shell` is the embedded Sphinx shell, `args` contains the list | |||
|
42 | of arguments that follow: '@doctest handler_name', `input_lines` contains | |||
|
43 | a list of the lines relevant to the current doctest, `found` is a string | |||
|
44 | containing the output from the IPython shell, and `submitted` is a string | |||
|
45 | containing the expected output from the IPython shell. | |||
|
46 | ||||
|
47 | Handlers must be registered in the `doctests` dict at the end of this module. | |||
3 |
|
48 | |||
4 | """ |
|
49 | """ | |
5 |
|
50 | |||
@@ -103,6 +148,8 b' def float_doctest(sphinx_shell, args, input_lines, found, submitted):' | |||||
103 | repr(submitted), TAB=TAB) |
|
148 | repr(submitted), TAB=TAB) | |
104 | raise RuntimeError(e) |
|
149 | raise RuntimeError(e) | |
105 |
|
150 | |||
|
151 | # dict of allowable doctest handlers. The key represents the first argument | |||
|
152 | # that must be given to @doctest in order to activate the handler. | |||
106 | doctests = { |
|
153 | doctests = { | |
107 | 'float': float_doctest, |
|
154 | 'float': float_doctest, | |
108 | } |
|
155 | } |
@@ -258,7 +258,7 b' class EmbeddedSphinxShell(object):' | |||||
258 | # This will persist across different EmbededSphinxShell instances. |
|
258 | # This will persist across different EmbededSphinxShell instances. | |
259 | IP = InteractiveShell.instance(config=config, profile_dir=profile) |
|
259 | IP = InteractiveShell.instance(config=config, profile_dir=profile) | |
260 |
|
260 | |||
261 |
# io.stdout redirect must be done |
|
261 | # io.stdout redirect must be done after instantiating InteractiveShell | |
262 | io.stdout = self.cout |
|
262 | io.stdout = self.cout | |
263 | io.stderr = self.cout |
|
263 | io.stderr = self.cout | |
264 |
|
264 |
General Comments 0
You need to be logged in to leave comments.
Login now