Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="doctest-execution-context.html" /> | |
13 | <link rel="prev" href="doctest-which-docstrings.html" /> | |
14 | <link rel="parent" href="doctest-how-it-works.html" /> | |
15 | <link rel="next" href="doctest-execution-context.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.2.3.2 How are Docstring Examples Recognized?</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.2.3.1 Which Docstrings Are" | |
25 | href="doctest-which-docstrings.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5.2.3 How It Works" | |
28 | href="doctest-how-it-works.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.2.3.3 What's the Execution" | |
31 | href="doctest-execution-context.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="doctest-which-docstrings.html">5.2.3.1 Which Docstrings Are</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="doctest-how-it-works.html">5.2.3 How It Works</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="doctest-execution-context.html">5.2.3.3 What's the Execution</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION007232000000000000000"></A><A NAME="doctest-finding-examples"></A> | |
56 | <BR> | |
57 | 5.2.3.2 How are Docstring Examples | |
58 | Recognized? | |
59 | </H3> | |
60 | ||
61 | <P> | |
62 | In most cases a copy-and-paste of an interactive console session works | |
63 | fine, but doctest isn't trying to do an exact emulation of any specific | |
64 | Python shell. All hard tab characters are expanded to spaces, using | |
65 | 8-column tab stops. If you don't believe tabs should mean that, too | |
66 | bad: don't use hard tabs, or write your own <tt class="class">DocTestParser</tt> | |
67 | class. | |
68 | ||
69 | <P> | |
70 | ||
71 | <span class="versionnote">Changed in version 2.4: | |
72 | Expanding tabs to spaces is new; previous versions | |
73 | tried to preserve hard tabs, with confusing results.</span> | |
74 | ||
75 | <P> | |
76 | <div class="verbatim"><pre> | |
77 | >>> # comments are ignored | |
78 | >>> x = 12 | |
79 | >>> x | |
80 | 12 | |
81 | >>> if x == 13: | |
82 | ... print "yes" | |
83 | ... else: | |
84 | ... print "no" | |
85 | ... print "NO" | |
86 | ... print "NO!!!" | |
87 | ... | |
88 | no | |
89 | NO | |
90 | NO!!! | |
91 | >>> | |
92 | </pre></div> | |
93 | ||
94 | <P> | |
95 | Any expected output must immediately follow the final | |
96 | <code>'><code>></code>> '</code> or <code>'... '</code> line containing the code, and | |
97 | the expected output (if any) extends to the next <code>'><code>></code>> '</code> | |
98 | or all-whitespace line. | |
99 | ||
100 | <P> | |
101 | The fine print: | |
102 | ||
103 | <P> | |
104 | ||
105 | <UL> | |
106 | <LI>Expected output cannot contain an all-whitespace line, since such a | |
107 | line is taken to signal the end of expected output. If expected | |
108 | output does contain a blank line, put <code><BLANKLINE></code> in your | |
109 | doctest example each place a blank line is expected. | |
110 | ||
111 | <span class="versionnote">Changed in version 2.4: | |
112 | <code><BLANKLINE></code> was added; there was no way to | |
113 | use expected output containing empty lines in | |
114 | previous versions.</span> | |
115 | ||
116 | <P> | |
117 | </LI> | |
118 | <LI>Output to stdout is captured, but not output to stderr (exception | |
119 | tracebacks are captured via a different means). | |
120 | ||
121 | <P> | |
122 | </LI> | |
123 | <LI>If you continue a line via backslashing in an interactive session, | |
124 | or for any other reason use a backslash, you should use a raw | |
125 | docstring, which will preserve your backslashes exactly as you type | |
126 | them: | |
127 | ||
128 | <P> | |
129 | <div class="verbatim"><pre> | |
130 | >>> def f(x): | |
131 | ... r'''Backslashes in a raw docstring: m\n''' | |
132 | >>> print f.__doc__ | |
133 | Backslashes in a raw docstring: m\n | |
134 | </pre></div> | |
135 | ||
136 | <P> | |
137 | Otherwise, the backslash will be interpreted as part of the string. | |
138 | For example, the "\" above would be interpreted as a | |
139 | newline character. Alternatively, you can double each backslash in the | |
140 | doctest version (and not use a raw string): | |
141 | ||
142 | <P> | |
143 | <div class="verbatim"><pre> | |
144 | >>> def f(x): | |
145 | ... '''Backslashes in a raw docstring: m\\n''' | |
146 | >>> print f.__doc__ | |
147 | Backslashes in a raw docstring: m\n | |
148 | </pre></div> | |
149 | ||
150 | <P> | |
151 | </LI> | |
152 | <LI>The starting column doesn't matter: | |
153 | ||
154 | <P> | |
155 | <div class="verbatim"><pre> | |
156 | >>> assert "Easy!" | |
157 | >>> import math | |
158 | >>> math.floor(1.9) | |
159 | 1.0 | |
160 | </pre></div> | |
161 | ||
162 | <P> | |
163 | and as many leading whitespace characters are stripped from the | |
164 | expected output as appeared in the initial <code>'><code>></code>> '</code> line | |
165 | that started the example. | |
166 | </LI> | |
167 | </UL> | |
168 | ||
169 | <P> | |
170 | ||
171 | <DIV CLASS="navigation"> | |
172 | <div class='online-navigation'> | |
173 | <p></p><hr /> | |
174 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
175 | <tr> | |
176 | <td class='online-navigation'><a rel="prev" title="5.2.3.1 Which Docstrings Are" | |
177 | href="doctest-which-docstrings.html"><img src='../icons/previous.png' | |
178 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
179 | <td class='online-navigation'><a rel="parent" title="5.2.3 How It Works" | |
180 | href="doctest-how-it-works.html"><img src='../icons/up.png' | |
181 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
182 | <td class='online-navigation'><a rel="next" title="5.2.3.3 What's the Execution" | |
183 | href="doctest-execution-context.html"><img src='../icons/next.png' | |
184 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
185 | <td align="center" width="100%">Python Library Reference</td> | |
186 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
187 | href="contents.html"><img src='../icons/contents.png' | |
188 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
189 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
190 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
191 | <td class='online-navigation'><a rel="index" title="Index" | |
192 | href="genindex.html"><img src='../icons/index.png' | |
193 | border='0' height='32' alt='Index' width='32' /></A></td> | |
194 | </tr></table> | |
195 | <div class='online-navigation'> | |
196 | <b class="navlabel">Previous:</b> | |
197 | <a class="sectref" rel="prev" href="doctest-which-docstrings.html">5.2.3.1 Which Docstrings Are</A> | |
198 | <b class="navlabel">Up:</b> | |
199 | <a class="sectref" rel="parent" href="doctest-how-it-works.html">5.2.3 How It Works</A> | |
200 | <b class="navlabel">Next:</b> | |
201 | <a class="sectref" rel="next" href="doctest-execution-context.html">5.2.3.3 What's the Execution</A> | |
202 | </div> | |
203 | </div> | |
204 | <hr /> | |
205 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
206 | </DIV> | |
207 | <!--End of Navigation Panel--> | |
208 | <ADDRESS> | |
209 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
210 | </ADDRESS> | |
211 | </BODY> | |
212 | </HTML> |