| 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-options.html" /> |
| 13 | <link rel="prev" href="doctest-execution-context.html" /> |
| 14 | <link rel="parent" href="doctest-how-it-works.html" /> |
| 15 | <link rel="next" href="doctest-options.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.2.3.4 What About Exceptions?</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.3 What's the Execution" |
| 25 | href="doctest-execution-context.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.5 Option Flags and" |
| 31 | href="doctest-options.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-execution-context.html">5.2.3.3 What's the Execution</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-options.html">5.2.3.5 Option Flags and</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION007234000000000000000"></A><A NAME="doctest-exceptions"></A> |
| 56 | <BR> |
| 57 | 5.2.3.4 What About Exceptions? |
| 58 | </H3> |
| 59 | |
| 60 | <P> |
| 61 | No problem, provided that the traceback is the only output produced by |
| 62 | the example: just paste in the traceback. Since tracebacks contain |
| 63 | details that are likely to change rapidly (for example, exact file paths |
| 64 | and line numbers), this is one case where doctest works hard to be |
| 65 | flexible in what it accepts. |
| 66 | |
| 67 | <P> |
| 68 | Simple example: |
| 69 | |
| 70 | <P> |
| 71 | <div class="verbatim"><pre> |
| 72 | >>> [1, 2, 3].remove(42) |
| 73 | Traceback (most recent call last): |
| 74 | File "<stdin>", line 1, in ? |
| 75 | ValueError: list.remove(x): x not in list |
| 76 | </pre></div> |
| 77 | |
| 78 | <P> |
| 79 | That doctest succeeds if <tt class="exception">ValueError</tt> is raised, with the |
| 80 | "<tt class="samp">list.remove(x): x not in list</tt>" detail as shown. |
| 81 | |
| 82 | <P> |
| 83 | The expected output for an exception must start with a traceback |
| 84 | header, which may be either of the following two lines, indented the |
| 85 | same as the first line of the example: |
| 86 | |
| 87 | <P> |
| 88 | <div class="verbatim"><pre> |
| 89 | Traceback (most recent call last): |
| 90 | Traceback (innermost last): |
| 91 | </pre></div> |
| 92 | |
| 93 | <P> |
| 94 | The traceback header is followed by an optional traceback stack, whose |
| 95 | contents are ignored by doctest. The traceback stack is typically |
| 96 | omitted, or copied verbatim from an interactive session. |
| 97 | |
| 98 | <P> |
| 99 | The traceback stack is followed by the most interesting part: the |
| 100 | line(s) containing the exception type and detail. This is usually the |
| 101 | last line of a traceback, but can extend across multiple lines if the |
| 102 | exception has a multi-line detail: |
| 103 | |
| 104 | <P> |
| 105 | <div class="verbatim"><pre> |
| 106 | >>> raise ValueError('multi\n line\ndetail') |
| 107 | Traceback (most recent call last): |
| 108 | File "<stdin>", line 1, in ? |
| 109 | ValueError: multi |
| 110 | line |
| 111 | detail |
| 112 | </pre></div> |
| 113 | |
| 114 | <P> |
| 115 | The last three lines (starting with <tt class="exception">ValueError</tt>) are |
| 116 | compared against the exception's type and detail, and the rest are |
| 117 | ignored. |
| 118 | |
| 119 | <P> |
| 120 | Best practice is to omit the traceback stack, unless it adds |
| 121 | significant documentation value to the example. So the last example |
| 122 | is probably better as: |
| 123 | |
| 124 | <P> |
| 125 | <div class="verbatim"><pre> |
| 126 | >>> raise ValueError('multi\n line\ndetail') |
| 127 | Traceback (most recent call last): |
| 128 | ... |
| 129 | ValueError: multi |
| 130 | line |
| 131 | detail |
| 132 | </pre></div> |
| 133 | |
| 134 | <P> |
| 135 | Note that tracebacks are treated very specially. In particular, in the |
| 136 | rewritten example, the use of "<tt class="samp">...</tt>" is independent of doctest's |
| 137 | <tt class="constant">ELLIPSIS</tt> option. The ellipsis in that example could be left |
| 138 | out, or could just as well be three (or three hundred) commas or digits, |
| 139 | or an indented transcript of a Monty Python skit. |
| 140 | |
| 141 | <P> |
| 142 | Some details you should read once, but won't need to remember: |
| 143 | |
| 144 | <P> |
| 145 | |
| 146 | <UL> |
| 147 | <LI>Doctest can't guess whether your expected output came from an |
| 148 | exception traceback or from ordinary printing. So, e.g., an example |
| 149 | that expects "<tt class="samp">ValueError: 42 is prime</tt>" will pass whether |
| 150 | <tt class="exception">ValueError</tt> is actually raised or if the example merely |
| 151 | prints that traceback text. In practice, ordinary output rarely begins |
| 152 | with a traceback header line, so this doesn't create real problems. |
| 153 | |
| 154 | <P> |
| 155 | </LI> |
| 156 | <LI>Each line of the traceback stack (if present) must be indented |
| 157 | further than the first line of the example, <em>or</em> start with a |
| 158 | non-alphanumeric character. The first line following the traceback |
| 159 | header indented the same and starting with an alphanumeric is taken |
| 160 | to be the start of the exception detail. Of course this does the |
| 161 | right thing for genuine tracebacks. |
| 162 | |
| 163 | <P> |
| 164 | </LI> |
| 165 | <LI>When the <tt class="constant">IGNORE_EXCEPTION_DETAIL</tt> doctest option is |
| 166 | is specified, everything following the leftmost colon is ignored. |
| 167 | |
| 168 | <P> |
| 169 | </LI> |
| 170 | <LI>The interactive shell omits the traceback header line for some |
| 171 | <tt class="exception">SyntaxError</tt>s. But doctest uses the traceback header |
| 172 | line to distinguish exceptions from non-exceptions. So in the rare |
| 173 | case where you need to test a <tt class="exception">SyntaxError</tt> that omits the |
| 174 | traceback header, you will need to manually add the traceback header |
| 175 | line to your test example. |
| 176 | |
| 177 | <P> |
| 178 | </LI> |
| 179 | <LI>For some <tt class="exception">SyntaxError</tt>s, Python displays the character |
| 180 | position of the syntax error, using a <code>^</code> marker: |
| 181 | |
| 182 | <P> |
| 183 | <div class="verbatim"><pre> |
| 184 | >>> 1 1 |
| 185 | File "<stdin>", line 1 |
| 186 | 1 1 |
| 187 | ^ |
| 188 | SyntaxError: invalid syntax |
| 189 | </pre></div> |
| 190 | |
| 191 | <P> |
| 192 | Since the lines showing the position of the error come before the |
| 193 | exception type and detail, they are not checked by doctest. For |
| 194 | example, the following test would pass, even though it puts the |
| 195 | <code>^</code> marker in the wrong location: |
| 196 | |
| 197 | <P> |
| 198 | <div class="verbatim"><pre> |
| 199 | >>> 1 1 |
| 200 | Traceback (most recent call last): |
| 201 | File "<stdin>", line 1 |
| 202 | 1 1 |
| 203 | ^ |
| 204 | SyntaxError: invalid syntax |
| 205 | </pre></div> |
| 206 | |
| 207 | <P> |
| 208 | </LI> |
| 209 | </UL> |
| 210 | |
| 211 | <P> |
| 212 | |
| 213 | <span class="versionnote">Changed in version 2.4: |
| 214 | The ability to handle a multi-line exception detail, |
| 215 | and the <tt class="constant">IGNORE_EXCEPTION_DETAIL</tt> doctest option, |
| 216 | were added.</span> |
| 217 | |
| 218 | <P> |
| 219 | |
| 220 | <DIV CLASS="navigation"> |
| 221 | <div class='online-navigation'> |
| 222 | <p></p><hr /> |
| 223 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 224 | <tr> |
| 225 | <td class='online-navigation'><a rel="prev" title="5.2.3.3 What's the Execution" |
| 226 | href="doctest-execution-context.html"><img src='../icons/previous.png' |
| 227 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 228 | <td class='online-navigation'><a rel="parent" title="5.2.3 How It Works" |
| 229 | href="doctest-how-it-works.html"><img src='../icons/up.png' |
| 230 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 231 | <td class='online-navigation'><a rel="next" title="5.2.3.5 Option Flags and" |
| 232 | href="doctest-options.html"><img src='../icons/next.png' |
| 233 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 234 | <td align="center" width="100%">Python Library Reference</td> |
| 235 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 236 | href="contents.html"><img src='../icons/contents.png' |
| 237 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 238 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 239 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 240 | <td class='online-navigation'><a rel="index" title="Index" |
| 241 | href="genindex.html"><img src='../icons/index.png' |
| 242 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 243 | </tr></table> |
| 244 | <div class='online-navigation'> |
| 245 | <b class="navlabel">Previous:</b> |
| 246 | <a class="sectref" rel="prev" href="doctest-execution-context.html">5.2.3.3 What's the Execution</A> |
| 247 | <b class="navlabel">Up:</b> |
| 248 | <a class="sectref" rel="parent" href="doctest-how-it-works.html">5.2.3 How It Works</A> |
| 249 | <b class="navlabel">Next:</b> |
| 250 | <a class="sectref" rel="next" href="doctest-options.html">5.2.3.5 Option Flags and</A> |
| 251 | </div> |
| 252 | </div> |
| 253 | <hr /> |
| 254 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 255 | </DIV> |
| 256 | <!--End of Navigation Panel--> |
| 257 | <ADDRESS> |
| 258 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 259 | </ADDRESS> |
| 260 | </BODY> |
| 261 | </HTML> |