blob: f3c5d983449d9e4b33daf5624e251c54e9bcfe39 [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text and implementation by Jaap Vermeulen
Fred Drake3a0351c1998-04-04 07:23:21 +00002\section{Standard Module \module{posixfile}}
Fred Drake1f3ab1c1998-02-18 15:22:08 +00003\label{module-posixfile}
Guido van Rossum7f61b351994-05-19 09:09:50 +00004\bimodindex{posixfile}
Fred Drake1f3ab1c1998-02-18 15:22:08 +00005\indexii{\POSIX{}}{file object}
Guido van Rossum7f61b351994-05-19 09:09:50 +00006
Guido van Rossum9b058111996-10-11 17:43:34 +00007\emph{Note:} This module will become obsolete in a future release.
8The locking operation that it provides is done better and more
Fred Drake295eaa81998-03-11 06:11:37 +00009portably by the \function{fcntl.lockf()} call.%
Fred Drake4e7e11a1998-03-12 06:46:16 +000010\withsubitem{(in module fcntl)}{\ttindex{lockf()}}
Guido van Rossum9b058111996-10-11 17:43:34 +000011
Guido van Rossum7f61b351994-05-19 09:09:50 +000012This module implements some additional functionality over the built-in
13file objects. In particular, it implements file locking, control over
14the file flags, and an easy interface to duplicate the file object.
15The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000016has all the standard file object methods and adds the methods
17described below. This module only works for certain flavors of
Fred Drake4e7e11a1998-03-12 06:46:16 +000018\UNIX{}, since it uses \function{fcntl.fcntl()} for file locking.%
19\withsubitem{(in module fcntl)}{\ttindex{fcntl()}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000020
Fred Drake295eaa81998-03-11 06:11:37 +000021To instantiate a posixfile object, use the \function{open()} function
22in the \module{posixfile} module. The resulting object looks and
23feels roughly the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000024
Fred Drake295eaa81998-03-11 06:11:37 +000025The \module{posixfile} module defines the following constants:
Guido van Rossum7f61b351994-05-19 09:09:50 +000026
Fred Drake295eaa81998-03-11 06:11:37 +000027
Guido van Rossum7f61b351994-05-19 09:09:50 +000028\begin{datadesc}{SEEK_SET}
Fred Drakea19bb9b1998-02-13 21:57:33 +000029Offset is calculated from the start of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000030\end{datadesc}
31
32\begin{datadesc}{SEEK_CUR}
Fred Drakea19bb9b1998-02-13 21:57:33 +000033Offset is calculated from the current position in the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000034\end{datadesc}
35
36\begin{datadesc}{SEEK_END}
Fred Drakea19bb9b1998-02-13 21:57:33 +000037Offset is calculated from the end of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000038\end{datadesc}
39
Fred Drake295eaa81998-03-11 06:11:37 +000040The \module{posixfile} module defines the following functions:
Guido van Rossum7f61b351994-05-19 09:09:50 +000041
Guido van Rossum470be141995-03-17 16:07:09 +000042
Fred Drake295eaa81998-03-11 06:11:37 +000043\begin{funcdesc}{open}{filename\optional{, mode\optional{, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000044 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000045 \var{filename}, \var{mode} and \var{bufsize} arguments are
Fred Drake295eaa81998-03-11 06:11:37 +000046 interpreted the same way as by the built-in \function{open()}
47 function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000048\end{funcdesc}
49
Guido van Rossum31cce971995-01-04 19:17:34 +000050\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000051 Create a new posixfile object with the given standard file object.
52 The resulting object has the same filename and mode as the original
53 file object.
54\end{funcdesc}
55
56The posixfile object defines the following additional methods:
57
Fred Drake19479911998-02-13 06:58:54 +000058\setindexsubitem{(posixfile method)}
Fred Drake295eaa81998-03-11 06:11:37 +000059\begin{funcdesc}{lock}{fmt, \optional{len\optional{, start\optional{, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000060 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000061 referring to. The format is explained
62 below in a table. The \var{len} argument specifies the length of the
63 section that should be locked. The default is \code{0}. \var{start}
64 specifies the starting offset of the section, where the default is
65 \code{0}. The \var{whence} argument specifies where the offset is
Fred Drake295eaa81998-03-11 06:11:37 +000066 relative to. It accepts one of the constants \constant{SEEK_SET},
67 \constant{SEEK_CUR} or \constant{SEEK_END}. The default is
68 \constant{SEEK_SET}. For more information about the arguments refer
69 to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000070\end{funcdesc}
71
Guido van Rossum96628a91995-04-10 11:34:00 +000072\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000073 Set the specified flags for the file that the file object is referring
74 to. The new flags are ORed with the old flags, unless specified
75 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000076 the \var{flags} argument
77 a string indicating the current flags is returned (this is
Fred Drake295eaa81998-03-11 06:11:37 +000078 the same as the \samp{?} modifier). For more information about the
79 flags refer to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000080\end{funcdesc}
81
82\begin{funcdesc}{dup}{}
83 Duplicate the file object and the underlying file pointer and file
84 descriptor. The resulting object behaves as if it were newly
85 opened.
86\end{funcdesc}
87
88\begin{funcdesc}{dup2}{fd}
89 Duplicate the file object and the underlying file pointer and file
90 descriptor. The new object will have the given file descriptor.
91 Otherwise the resulting object behaves as if it were newly opened.
92\end{funcdesc}
93
94\begin{funcdesc}{file}{}
95 Return the standard file object that the posixfile object is based
96 on. This is sometimes necessary for functions that insist on a
97 standard file object.
98\end{funcdesc}
99
Fred Drake295eaa81998-03-11 06:11:37 +0000100All methods raise \exception{IOError} when the request fails.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000101
Fred Drake295eaa81998-03-11 06:11:37 +0000102Format characters for the \method{lock()} method have the following
103meaning:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000104
Fred Drake7f962911997-12-23 04:21:20 +0000105\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
106 \lineii{u}{unlock the specified region}
107 \lineii{r}{request a read lock for the specified section}
108 \lineii{w}{request a write lock for the specified section}
109\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000110
111In addition the following modifiers can be added to the format:
112
113\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
114 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000115 \lineiii{?}{return the first lock conflicting with the requested lock, or
116 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000117\end{tableiii}
118
119Note:
120
Fred Drake295eaa81998-03-11 06:11:37 +0000121(1) The lock returned is in the format \code{(\var{mode}, \var{len},
122\var{start}, \var{whence}, \var{pid})} where \var{mode} is a character
123representing the type of lock ('r' or 'w'). This modifier prevents a
124request from being granted; it is for query purposes only.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000125
Fred Drake295eaa81998-03-11 06:11:37 +0000126Format characters for the \method{flags()} method have the following
127meanings:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000128
Fred Drake7f962911997-12-23 04:21:20 +0000129\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
130 \lineii{a}{append only flag}
131 \lineii{c}{close on exec flag}
132 \lineii{n}{no delay flag (also called non-blocking flag)}
133 \lineii{s}{synchronization flag}
134\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000135
136In addition the following modifiers can be added to the format:
137
138\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
139 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
140 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
141 \lineiii{?}{return a string in which the characters represent the flags that
142 are set.}{(2)}
143\end{tableiii}
144
145Note:
146
147(1) The \code{!} and \code{=} modifiers are mutually exclusive.
148
Guido van Rossum16d6e711994-08-08 12:30:22 +0000149(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000150by the same call.
151
152Examples:
153
Fred Drake19479911998-02-13 06:58:54 +0000154\begin{verbatim}
Fred Drake295eaa81998-03-11 06:11:37 +0000155import posixfile
Guido van Rossum7f61b351994-05-19 09:09:50 +0000156
Fred Drake295eaa81998-03-11 06:11:37 +0000157file = posixfile.open('/tmp/test', 'w')
Guido van Rossum7f61b351994-05-19 09:09:50 +0000158file.lock('w|')
159...
160file.lock('u')
161file.close()
Fred Drake19479911998-02-13 06:58:54 +0000162\end{verbatim}