mirror of
https://github.com/python/cpython.git
synced 2025-10-29 01:22:59 +00:00
Work around the broken formatting of sys.ps1 prompts in running text.
Move sample sessions to the left margin of the file for consistency; formatting can adjust the margin if needed. This closes SF bug #133213.
This commit is contained in:
parent
2f55b11bf7
commit
19f3c52347
1 changed files with 48 additions and 46 deletions
|
|
@ -226,11 +226,11 @@ No problem, as long as the only output generated by the example is the
|
||||||
traceback itself. For example:
|
traceback itself. For example:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> [1, 2, 3].remove(42)
|
>>> [1, 2, 3].remove(42)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "<stdin>", line 1, in ?
|
File "<stdin>", line 1, in ?
|
||||||
ValueError: list.remove(x): x not in list
|
ValueError: list.remove(x): x not in list
|
||||||
>>>
|
>>>
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Note that only the exception type and value are compared (specifically,
|
Note that only the exception type and value are compared (specifically,
|
||||||
|
|
@ -257,26 +257,27 @@ tabs and spaces if you're too lazy to do it right, but doctest is not in
|
||||||
the business of guessing what you think a tab means).
|
the business of guessing what you think a tab means).
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> # comments are ignored
|
>>> # comments are ignored
|
||||||
>>> x = 12
|
>>> x = 12
|
||||||
>>> x
|
>>> x
|
||||||
12
|
12
|
||||||
>>> if x == 13:
|
>>> if x == 13:
|
||||||
... print "yes"
|
... print "yes"
|
||||||
... else:
|
... else:
|
||||||
... print "no"
|
... print "no"
|
||||||
... print "NO"
|
... print "NO"
|
||||||
... print "NO!!!"
|
... print "NO!!!"
|
||||||
...
|
...
|
||||||
no
|
no
|
||||||
NO
|
NO
|
||||||
NO!!!
|
NO!!!
|
||||||
>>>
|
>>>
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Any expected output must immediately follow the final \code{">>>"} or
|
Any expected output must immediately follow the final
|
||||||
\code{"..."} line containing the code, and the expected output (if any)
|
\code{'>\code{>}>~'} or \code{'...~'} line containing the code, and
|
||||||
extends to the next \code{">>>"} or all-whitespace line.
|
the expected output (if any) extends to the next \code{'>\code{>}>~'}
|
||||||
|
or all-whitespace line.
|
||||||
|
|
||||||
The fine print:
|
The fine print:
|
||||||
|
|
||||||
|
|
@ -310,8 +311,9 @@ yes
|
||||||
1.0
|
1.0
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
and as many leading whitespace characters are stripped from the expected
|
and as many leading whitespace characters are stripped from the
|
||||||
output as appeared in the initial ">>>" line that triggered it.
|
expected output as appeared in the initial \code{'>\code{>}>~'} line
|
||||||
|
that triggered it.
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
|
||||||
\subsection{Warnings}
|
\subsection{Warnings}
|
||||||
|
|
@ -349,26 +351,26 @@ testmod skips it by default. Other approaches are described in
|
||||||
% Hey! What happened to Monty Python examples?
|
% Hey! What happened to Monty Python examples?
|
||||||
% Tim: ask Guido -- it's his example!
|
% Tim: ask Guido -- it's his example!
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> foo()
|
>>> foo()
|
||||||
{"Hermione": "hippogryph", "Harry": "broomstick"}
|
{"Hermione": "hippogryph", "Harry": "broomstick"}
|
||||||
>>>
|
>>>
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
is vulnerable! One workaround is to do
|
is vulnerable! One workaround is to do
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
|
>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
|
||||||
1
|
1
|
||||||
>>>
|
>>>
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
instead. Another is to do
|
instead. Another is to do
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> d = foo().items()
|
>>> d = foo().items()
|
||||||
>>> d.sort()
|
>>> d.sort()
|
||||||
>>> d
|
>>> d
|
||||||
[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]
|
[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
There are others, but you get the idea.
|
There are others, but you get the idea.
|
||||||
|
|
@ -376,9 +378,9 @@ There are others, but you get the idea.
|
||||||
Another bad idea is to print things that embed an object address, like
|
Another bad idea is to print things that embed an object address, like
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> id(1.0) # certain to fail some of the time
|
>>> id(1.0) # certain to fail some of the time
|
||||||
7948648
|
7948648
|
||||||
>>>
|
>>>
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Floating-point numbers are also subject to small output variations across
|
Floating-point numbers are also subject to small output variations across
|
||||||
|
|
@ -386,20 +388,20 @@ platforms, because Python defers to the platform C library for float
|
||||||
formatting, and C libraries vary widely in quality here.
|
formatting, and C libraries vary widely in quality here.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> 1./7 # risky
|
>>> 1./7 # risky
|
||||||
0.14285714285714285
|
0.14285714285714285
|
||||||
>>> print 1./7 # safer
|
>>> print 1./7 # safer
|
||||||
0.142857142857
|
0.142857142857
|
||||||
>>> print round(1./7, 6) # much safer
|
>>> print round(1./7, 6) # much safer
|
||||||
0.142857
|
0.142857
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Numbers of the form \code{I/2.**J} are safe across all platforms, and I
|
Numbers of the form \code{I/2.**J} are safe across all platforms, and I
|
||||||
often contrive doctest examples to produce numbers of that form:
|
often contrive doctest examples to produce numbers of that form:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> 3./4 # utterly safe
|
>>> 3./4 # utterly safe
|
||||||
0.75
|
0.75
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Simple fractions are also easier for people to understand, and that makes
|
Simple fractions are also easier for people to understand, and that makes
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue