blob: 62861f02d60bdd3926db65cd48949ac1bca4b02d [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text and implementation by Jaap Vermeulen
Fred Drake295da241998-08-10 19:42:37 +00002\section{\module{posixfile} ---
Fred Drakef6863c11999-03-02 16:37:17 +00003 File-like objects with locking support}
Fred Drakeb91e9341998-07-23 17:59:49 +00004
Fred Drakef6863c11999-03-02 16:37:17 +00005\declaremodule{builtin}{posixfile}
Fred Drakea54a8871999-03-02 17:03:42 +00006 \platform{Unix}
Fred Drakeb91e9341998-07-23 17:59:49 +00007\modulesynopsis{A file-like object with support for locking.}
Fred Drakef6863c11999-03-02 16:37:17 +00008\moduleauthor{Jaap Vermeulen}{}
9\sectionauthor{Jaap Vermeulen}{}
10
Fred Drakeb91e9341998-07-23 17:59:49 +000011
Fred Drakec37b65e2001-11-28 07:26:15 +000012\indexii{\POSIX}{file object}
Guido van Rossum7f61b351994-05-19 09:09:50 +000013
Fred Drakebc006af2001-10-24 21:56:59 +000014\deprecated{1.5}{The locking operation that this module provides is
15done better and more portably by the
16\function{\refmodule{fcntl}.lockf()} call.
Fred Drake0aa811c2001-10-20 04:24:09 +000017\withsubitem{(in module fcntl)}{\ttindex{lockf()}}}
Guido van Rossum9b058111996-10-11 17:43:34 +000018
Guido van Rossum7f61b351994-05-19 09:09:50 +000019This module implements some additional functionality over the built-in
20file objects. In particular, it implements file locking, control over
21the file flags, and an easy interface to duplicate the file object.
22The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000023has all the standard file object methods and adds the methods
24described below. This module only works for certain flavors of
Fred Drakec37b65e2001-11-28 07:26:15 +000025\UNIX, since it uses \function{fcntl.fcntl()} for file locking.%
Fred Drake4e7e11a1998-03-12 06:46:16 +000026\withsubitem{(in module fcntl)}{\ttindex{fcntl()}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000027
Fred Drake295eaa81998-03-11 06:11:37 +000028To instantiate a posixfile object, use the \function{open()} function
29in the \module{posixfile} module. The resulting object looks and
30feels roughly the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000031
Fred Drake295eaa81998-03-11 06:11:37 +000032The \module{posixfile} module defines the following constants:
Guido van Rossum7f61b351994-05-19 09:09:50 +000033
Fred Drake295eaa81998-03-11 06:11:37 +000034
Guido van Rossum7f61b351994-05-19 09:09:50 +000035\begin{datadesc}{SEEK_SET}
Fred Drakea19bb9b1998-02-13 21:57:33 +000036Offset is calculated from the start of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000037\end{datadesc}
38
39\begin{datadesc}{SEEK_CUR}
Fred Drakea19bb9b1998-02-13 21:57:33 +000040Offset is calculated from the current position in the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000041\end{datadesc}
42
43\begin{datadesc}{SEEK_END}
Fred Drakea19bb9b1998-02-13 21:57:33 +000044Offset is calculated from the end of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000045\end{datadesc}
46
Fred Drake295eaa81998-03-11 06:11:37 +000047The \module{posixfile} module defines the following functions:
Guido van Rossum7f61b351994-05-19 09:09:50 +000048
Guido van Rossum470be141995-03-17 16:07:09 +000049
Fred Drake295eaa81998-03-11 06:11:37 +000050\begin{funcdesc}{open}{filename\optional{, mode\optional{, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000051 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000052 \var{filename}, \var{mode} and \var{bufsize} arguments are
Fred Drake295eaa81998-03-11 06:11:37 +000053 interpreted the same way as by the built-in \function{open()}
54 function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000055\end{funcdesc}
56
Guido van Rossum31cce971995-01-04 19:17:34 +000057\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000058 Create a new posixfile object with the given standard file object.
59 The resulting object has the same filename and mode as the original
60 file object.
61\end{funcdesc}
62
63The posixfile object defines the following additional methods:
64
Fred Drake19479911998-02-13 06:58:54 +000065\setindexsubitem{(posixfile method)}
Fred Drake295eaa81998-03-11 06:11:37 +000066\begin{funcdesc}{lock}{fmt, \optional{len\optional{, start\optional{, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000067 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000068 referring to. The format is explained
69 below in a table. The \var{len} argument specifies the length of the
70 section that should be locked. The default is \code{0}. \var{start}
71 specifies the starting offset of the section, where the default is
72 \code{0}. The \var{whence} argument specifies where the offset is
Fred Drake295eaa81998-03-11 06:11:37 +000073 relative to. It accepts one of the constants \constant{SEEK_SET},
74 \constant{SEEK_CUR} or \constant{SEEK_END}. The default is
75 \constant{SEEK_SET}. For more information about the arguments refer
76 to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000077\end{funcdesc}
78
Guido van Rossum96628a91995-04-10 11:34:00 +000079\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000080 Set the specified flags for the file that the file object is referring
81 to. The new flags are ORed with the old flags, unless specified
82 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000083 the \var{flags} argument
84 a string indicating the current flags is returned (this is
Fred Drake295eaa81998-03-11 06:11:37 +000085 the same as the \samp{?} modifier). For more information about the
86 flags refer to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000087\end{funcdesc}
88
89\begin{funcdesc}{dup}{}
90 Duplicate the file object and the underlying file pointer and file
91 descriptor. The resulting object behaves as if it were newly
92 opened.
93\end{funcdesc}
94
95\begin{funcdesc}{dup2}{fd}
96 Duplicate the file object and the underlying file pointer and file
97 descriptor. The new object will have the given file descriptor.
98 Otherwise the resulting object behaves as if it were newly opened.
99\end{funcdesc}
100
101\begin{funcdesc}{file}{}
102 Return the standard file object that the posixfile object is based
103 on. This is sometimes necessary for functions that insist on a
104 standard file object.
105\end{funcdesc}
106
Fred Drake295eaa81998-03-11 06:11:37 +0000107All methods raise \exception{IOError} when the request fails.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000108
Fred Drake295eaa81998-03-11 06:11:37 +0000109Format characters for the \method{lock()} method have the following
110meaning:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000111
Fred Drakeee601911998-04-11 20:53:03 +0000112\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000113 \lineii{u}{unlock the specified region}
114 \lineii{r}{request a read lock for the specified section}
115 \lineii{w}{request a write lock for the specified section}
116\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000117
118In addition the following modifiers can be added to the format:
119
Fred Drakeee601911998-04-11 20:53:03 +0000120\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000121 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000122 \lineiii{?}{return the first lock conflicting with the requested lock, or
123 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000124\end{tableiii}
125
Fred Draked146edf1999-04-21 21:15:35 +0000126\noindent
Guido van Rossum7f61b351994-05-19 09:09:50 +0000127Note:
128
Fred Draked146edf1999-04-21 21:15:35 +0000129\begin{description}
130\item[(1)] The lock returned is in the format \code{(\var{mode}, \var{len},
Fred Drake295eaa81998-03-11 06:11:37 +0000131\var{start}, \var{whence}, \var{pid})} where \var{mode} is a character
132representing the type of lock ('r' or 'w'). This modifier prevents a
133request from being granted; it is for query purposes only.
Fred Draked146edf1999-04-21 21:15:35 +0000134\end{description}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000135
Fred Drake295eaa81998-03-11 06:11:37 +0000136Format characters for the \method{flags()} method have the following
137meanings:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000138
Fred Drakeee601911998-04-11 20:53:03 +0000139\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000140 \lineii{a}{append only flag}
141 \lineii{c}{close on exec flag}
142 \lineii{n}{no delay flag (also called non-blocking flag)}
143 \lineii{s}{synchronization flag}
144\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000145
146In addition the following modifiers can be added to the format:
147
Fred Drakeee601911998-04-11 20:53:03 +0000148\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000149 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
150 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
151 \lineiii{?}{return a string in which the characters represent the flags that
152 are set.}{(2)}
153\end{tableiii}
154
Fred Draked146edf1999-04-21 21:15:35 +0000155\noindent
156Notes:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000157
Fred Draked146edf1999-04-21 21:15:35 +0000158\begin{description}
159\item[(1)] The \samp{!} and \samp{=} modifiers are mutually exclusive.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000160
Fred Draked146edf1999-04-21 21:15:35 +0000161\item[(2)] This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000162by the same call.
Fred Draked146edf1999-04-21 21:15:35 +0000163\end{description}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000164
165Examples:
166
Fred Drake19479911998-02-13 06:58:54 +0000167\begin{verbatim}
Fred Drake295eaa81998-03-11 06:11:37 +0000168import posixfile
Guido van Rossum7f61b351994-05-19 09:09:50 +0000169
Fred Drake295eaa81998-03-11 06:11:37 +0000170file = posixfile.open('/tmp/test', 'w')
Guido van Rossum7f61b351994-05-19 09:09:50 +0000171file.lock('w|')
172...
173file.lock('u')
174file.close()
Fred Drake19479911998-02-13 06:58:54 +0000175\end{verbatim}