Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 752ba39

Browse files
committed
Clarify a number of issues about the file-like object API based on
discussion on python-dev.
1 parent 7158126 commit 752ba39

1 file changed

Lines changed: 36 additions & 20 deletions

File tree

Doc/lib/libstdtypes.tex

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -995,23 +995,33 @@ \subsubsection{File Objects\obindex{file}
995995

996996
\begin{methoddesc}[file]{close}{}
997997
Close the file. A closed file cannot be read or written anymore.
998+
Any operation which requires that the file be open will raise an
999+
\exception{IOError} after the file has been closed. Calling
1000+
\method{close()} more than once is allowed.
9981001
\end{methoddesc}
9991002

10001003
\begin{methoddesc}[file]{flush}{}
1001-
Flush the internal buffer, like \code{stdio}'s \cfunction{fflush()}.
1004+
Flush the internal buffer, like \code{stdio}'s
1005+
\cfunction{fflush()}. This may be a no-op on some file-like
1006+
objects.
10021007
\end{methoddesc}
10031008

10041009
\begin{methoddesc}[file]{isatty}{}
1005-
Return \code{1} if the file is connected to a tty(-like) device, else
1006-
\code{0}.
1010+
Return true if the file is connected to a tty(-like) device, else
1011+
false. \strong{Note:} If a file-like object is not associated
1012+
with a real file, this method should \emph{not} be implemented.
10071013
\end{methoddesc}
10081014

10091015
\begin{methoddesc}[file]{fileno}{}
1010-
Return the integer ``file descriptor'' that is used by the underlying
1011-
implementation to request I/O operations from the operating system.
1012-
This can be useful for other, lower level interfaces that use file
1013-
descriptors, e.g. module \module{fcntl} or \function{os.read()} and friends.
1014-
\refbimodindex{fcntl}
1016+
\index{file descriptor}
1017+
\index{descriptor, file}
1018+
Return the integer ``file descriptor'' that is used by the
1019+
underlying implementation to request I/O operations from the
1020+
operating system. This can be useful for other, lower level
1021+
interfaces that use file descriptors, e.g.\ module
1022+
\refmodule{fcntl}\refbimodindex{fcntl} or \function{os.read()} and
1023+
friends. \strong{Note:} File-like objects which do not have a real
1024+
file descriptor should \emph{not} provide this method!
10151025
\end{methoddesc}
10161026

10171027
\begin{methoddesc}[file]{read}{\optional{size}}
@@ -1040,17 +1050,19 @@ \subsubsection{File Objects\obindex{file}
10401050
non-negative, it is a maximum byte count (including the trailing
10411051
newline) and an incomplete line may be returned.
10421052
An empty string is returned when \EOF{} is hit
1043-
immediately. Note: Unlike \code{stdio}'s \cfunction{fgets()}, the returned
1044-
string contains null characters (\code{'\e 0'}) if they occurred in the
1045-
input.
1053+
immediately. Note: Unlike \code{stdio}'s \cfunction{fgets()}, the
1054+
returned string contains null characters (\code{'\e 0'}) if they
1055+
occurred in the input.
10461056
\end{methoddesc}
10471057

10481058
\begin{methoddesc}[file]{readlines}{\optional{sizehint}}
10491059
Read until \EOF{} using \method{readline()} and return a list containing
10501060
the lines thus read. If the optional \var{sizehint} argument is
10511061
present, instead of reading up to \EOF{}, whole lines totalling
10521062
approximately \var{sizehint} bytes (possibly after rounding up to an
1053-
internal buffer size) are read.
1063+
internal buffer size) are read. Objects implementing a file-like
1064+
interface may choose to ignore \var{sizehint} if it cannot be
1065+
implemented, or cannot be implemented efficiently.
10541066
\end{methoddesc}
10551067

10561068
\begin{methoddesc}[file]{seek}{offset\optional{, whence}}
@@ -1067,11 +1079,11 @@ \subsubsection{File Objects\obindex{file}
10671079
\end{methoddesc}
10681080

10691081
\begin{methoddesc}[file]{truncate}{\optional{size}}
1070-
Truncate the file's size. If the optional size argument present, the
1071-
file is truncated to (at most) that size. The size defaults to the
1072-
current position. Availability of this function depends on the
1073-
operating system version (for example, not all \UNIX{} versions support this
1074-
operation).
1082+
Truncate the file's size. If the optional \var{size} argument
1083+
present, the file is truncated to (at most) that size. The size
1084+
defaults to the current position. Availability of this function
1085+
depends on the operating system version (for example, not all
1086+
\UNIX{} versions support this operation).
10751087
\end{methoddesc}
10761088

10771089
\begin{methoddesc}[file]{write}{str}
@@ -1087,24 +1099,28 @@ \subsubsection{File Objects\obindex{file}
10871099
\end{methoddesc}
10881100

10891101

1090-
File objects also offer the following attributes:
1102+
File objects also offer a number of other interesting attributes.
1103+
These are not required for file-like objects, but should be
1104+
implemented if they make sense for the particular object.
10911105

10921106
\begin{memberdesc}[file]{closed}
10931107
Boolean indicating the current state of the file object. This is a
10941108
read-only attribute; the \method{close()} method changes the value.
1109+
It may not be available on all file-like objects.
10951110
\end{memberdesc}
10961111

10971112
\begin{memberdesc}[file]{mode}
10981113
The I/O mode for the file. If the file was created using the
10991114
\function{open()} built-in function, this will be the value of the
1100-
\var{mode} parameter. This is a read-only attribute.
1115+
\var{mode} parameter. This is a read-only attribute and may not be
1116+
present on all file-like objects.
11011117
\end{memberdesc}
11021118

11031119
\begin{memberdesc}[file]{name}
11041120
If the file object was created using \function{open()}, the name of
11051121
the file. Otherwise, some string that indicates the source of the
11061122
file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only
1107-
attribute.
1123+
attribute and may not be present on all file-like objects.
11081124
\end{memberdesc}
11091125

11101126
\begin{memberdesc}[file]{softspace}

0 commit comments

Comments
 (0)