Get rid of a bunch more raw_input references

This commit is contained in:
Neal Norwitz 2006-03-17 06:49:51 +00:00
parent 9e2b9665ae
commit ce96f69d69
42 changed files with 222 additions and 144 deletions

View file

@ -186,13 +186,3 @@ The character used to draw separator lines under the help-message
headers. If empty, no ruler line is drawn. It defaults to
\character{=}.
\end{memberdesc}
\begin{memberdesc}{use_rawinput}
A flag, defaulting to true. If true, \method{cmdloop()} uses
\function{raw_input()} to display a prompt and read the next command;
if false, \method{sys.stdout.write()} and
\method{sys.stdin.readline()} are used. (This means that by
importing \refmodule{readline}, on systems that support it, the
interpreter will automatically support \program{Emacs}-like line editing
and command-history keystrokes.)
\end{memberdesc}

View file

@ -167,7 +167,7 @@ Remove any unhandled source text from the input buffer.
\begin{methoddesc}{raw_input}{\optional{prompt}}
Write a prompt and read a line. The returned line does not include
the trailing newline. When the user enters the \EOF{} key sequence,
\exception{EOFError} is raised. The base implementation uses the
built-in function \function{raw_input()}; a subclass may replace this
\exception{EOFError} is raised. The base implementation reads from
\code{sys.stdin}; a subclass may replace this
with a different implementation.
\end{methoddesc}

View file

@ -41,6 +41,12 @@ A simple example illustrating typical use:
\begin{verbatim}
import crypt, getpass, pwd
def raw_input(prompt):
import sys
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
def login():
username = raw_input('Python login:')
cryptedpasswd = pwd.getpwnam(username)[1]

View file

@ -153,9 +153,7 @@ Raised when an \keyword{assert} statement fails.
\begin{excdesc}{EOFError}
% XXXJH xrefs here
Raised when one of the built-in functions (\function{input()} or
\function{raw_input()}) hits an end-of-file condition (\EOF) without
reading any data.
Raised when attempting to read beyond the end of a file.
% XXXJH xrefs here
(N.B.: the \method{read()} and \method{readline()} methods of file
objects return an empty string when they hit \EOF.)
@ -213,9 +211,6 @@ Raised when an \keyword{assert} statement fails.
\kbd{Control-C} or \kbd{Delete}). During execution, a check for
interrupts is made regularly.
% XXX(hylton) xrefs here
Interrupts typed when a built-in function \function{input()} or
\function{raw_input()} is waiting for input also raise this
exception.
The exception inherits from \exception{BaseException} so as to not be
accidentally caught by code that catches \exception{Exception} and thus
prevent the interpreter from exiting.

View file

@ -551,23 +551,6 @@ class C:
note: this is the address of the object.)
\end{funcdesc}
\begin{funcdesc}{input}{\optional{prompt}}
Equivalent to \code{eval(raw_input(\var{prompt}))}.
\warning{This function is not safe from user errors! It
expects a valid Python expression as input; if the input is not
syntactically valid, a \exception{SyntaxError} will be raised.
Other exceptions may be raised if there is an error during
evaluation. (On the other hand, sometimes this is exactly what you
need when writing a quick script for expert use.)}
If the \refmodule{readline} module was loaded, then
\function{input()} will use it to provide elaborate line editing and
history features.
Consider using the \function{raw_input()} function for general input
from users.
\end{funcdesc}
\begin{funcdesc}{int}{\optional{x\optional{, radix}}}
Convert a string or number to a plain integer. If the argument is a
string, it must contain a possibly signed decimal number
@ -811,24 +794,6 @@ class C(object):
\end{verbatim}
\end{funcdesc}
\begin{funcdesc}{raw_input}{\optional{prompt}}
If the \var{prompt} argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns that.
When \EOF{} is read, \exception{EOFError} is raised. Example:
\begin{verbatim}
>>> s = raw_input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
\end{verbatim}
If the \refmodule{readline} module was loaded, then
\function{raw_input()} will use it to provide elaborate
line editing and history features.
\end{funcdesc}
\begin{funcdesc}{reduce}{function, sequence\optional{, initializer}}
Apply \var{function} of two arguments cumulatively to the items of
\var{sequence}, from left to right, so as to reduce the sequence to

View file

@ -267,6 +267,12 @@ processing of the \rfc{822} headers. In particular, the `To' and
\begin{verbatim}
import smtplib
def raw_input(prompt):
import sys
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
def prompt(prompt):
return raw_input(prompt).strip()

View file

@ -511,11 +511,8 @@ else:
\dataline{stderr}
File objects corresponding to the interpreter's standard input,
output and error streams. \code{stdin} is used for all interpreter
input except for scripts but including calls to
\function{input()}\bifuncindex{input} and
\function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is
used for the output of \keyword{print} and expression statements and
for the prompts of \function{input()} and \function{raw_input()}.
input except for scripts. \code{stdout} is
used for the output of \keyword{print} and expression statements.
The interpreter's own prompts and (almost all of) its error messages
go to \code{stderr}. \code{stdout} and \code{stderr} needn't be
built-in file objects: any object is acceptable as long as it has a

View file

@ -196,6 +196,11 @@ import getpass
import sys
import telnetlib
def raw_input(prompt):
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

View file

@ -91,6 +91,12 @@ and a \keyword{try} ... \keyword{finally} statement to ensure that the
old tty attributes are restored exactly no matter what happens:
\begin{verbatim}
def raw_input(prompt):
import sys
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
def getpass(prompt = "Password: "):
import termios, sys
fd = sys.stdin.fileno()

View file

@ -103,10 +103,7 @@ The input line read by \function{input()} must have the following form:
\end{productionlist}
Note: to read `raw' input line without interpretation, you can use the
built-in function \function{raw_input()} or the \method{readline()} method
of file objects.
the \method{readline()} method of file objects, including \code{sys.stdin}.
\obindex{file}
\index{input!raw}
\index{raw input}
\bifuncindex{raw_input}
\withsubitem{(file method)}{\ttindex{readline()}}

View file

@ -2,6 +2,12 @@
# This Python program sorts and reformats the table of keywords in ref2.tex
def raw_input(prompt):
import sys
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
l = []
try:
while 1:

View file

@ -231,7 +231,7 @@ full name on the command line.
Note that there is a difference between \samp{python file} and
\samp{python <file}. In the latter case, input requests from the
program, such as calls to \function{input()} and \function{raw_input()}, are
program, such as calling \code{sys.stdin.read()}, are
satisfied from \emph{file}. Since this file has already been read
until the end by the parser before the program starts executing, the
program will encounter end-of-file immediately. In the former case
@ -1161,6 +1161,12 @@ Perhaps the most well-known statement type is the
\keyword{if} statement. For example:
\begin{verbatim}
>>> def raw_input(prompt):
... import sys
... sys.stdout.write(prompt)
... sys.stdout.flush()
... return sys.stdin.readline()
...
>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
... x = 0
@ -1453,6 +1459,12 @@ arguments. This creates a function that can be called with fewer
arguments than it is defined to allow. For example:
\begin{verbatim}
def raw_input(prompt):
import sys
sys.stdout.write(prompt)
sys.stdout.flush()
return sys.stdin.readline()
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = raw_input(prompt)
@ -2711,15 +2723,15 @@ standard module \module{__builtin__}\refbimodindex{__builtin__}:
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UserWarning', 'ValueError', 'Warning', 'WindowsError',
'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__',
'__name__', 'abs', 'apply', 'basestring', 'bool', 'buffer',
'__name__', 'abs', 'basestring', 'bool', 'buffer',
'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile',
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float',
'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex',
'id', 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter',
'id', 'int', 'intern', 'isinstance', 'issubclass', 'iter',
'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min',
'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range',
'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set',
'reduce', 'reload', 'repr', 'reversed', 'round', 'set',
'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',
'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
\end{verbatim}
@ -3412,6 +3424,12 @@ supports); note that a user-generated interruption is signalled by
raising the \exception{KeyboardInterrupt} exception.
\begin{verbatim}
>>> def raw_input(prompt):
... import sys
... sys.stdout.write(prompt)
... sys.stdout.flush()
... return sys.stdin.readline()
...
>>> while True:
... try:
... x = int(raw_input("Please enter a number: "))
@ -4983,7 +5001,12 @@ renaming utility for a photo browser may elect to use percent signs for
placeholders such as the current date, image sequence number, or file format:
\begin{verbatim}
>>> import time, os.path
>>> import time, os.path, sys
>>> def raw_input(prompt):
... sys.stdout.write(prompt)
... sys.stdout.flush()
... return sys.stdin.readline()
...
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
... delimiter = '%'