blob: 5c86f3e38795c806e5cce2195df919be8a40f217 [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
Guido van Rossumd8faa362007-04-27 19:54:29 +000065\begin{methoddesc}[posixfile]{lock}{fmt, \optional{len\optional{, start\optional{, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000066 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000067 referring to. The format is explained
68 below in a table. The \var{len} argument specifies the length of the
69 section that should be locked. The default is \code{0}. \var{start}
70 specifies the starting offset of the section, where the default is
71 \code{0}. The \var{whence} argument specifies where the offset is
Fred Drake295eaa81998-03-11 06:11:37 +000072 relative to. It accepts one of the constants \constant{SEEK_SET},
73 \constant{SEEK_CUR} or \constant{SEEK_END}. The default is
74 \constant{SEEK_SET}. For more information about the arguments refer
75 to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossumd8faa362007-04-27 19:54:29 +000076\end{methoddesc}
Guido van Rossum7f61b351994-05-19 09:09:50 +000077
Guido van Rossumd8faa362007-04-27 19:54:29 +000078\begin{methoddesc}[posixfile]{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000079 Set the specified flags for the file that the file object is referring
80 to. The new flags are ORed with the old flags, unless specified
81 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000082 the \var{flags} argument
83 a string indicating the current flags is returned (this is
Fred Drake295eaa81998-03-11 06:11:37 +000084 the same as the \samp{?} modifier). For more information about the
85 flags refer to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossumd8faa362007-04-27 19:54:29 +000086\end{methoddesc}
Guido van Rossum7f61b351994-05-19 09:09:50 +000087
Guido van Rossumd8faa362007-04-27 19:54:29 +000088\begin{methoddesc}[posixfile]{dup}{}
Guido van Rossum7f61b351994-05-19 09:09:50 +000089 Duplicate the file object and the underlying file pointer and file
90 descriptor. The resulting object behaves as if it were newly
91 opened.
Guido van Rossumd8faa362007-04-27 19:54:29 +000092\end{methoddesc}
Guido van Rossum7f61b351994-05-19 09:09:50 +000093
Guido van Rossumd8faa362007-04-27 19:54:29 +000094\begin{methoddesc}[posixfile]{dup2}{fd}
Guido van Rossum7f61b351994-05-19 09:09:50 +000095 Duplicate the file object and the underlying file pointer and file
96 descriptor. The new object will have the given file descriptor.
97 Otherwise the resulting object behaves as if it were newly opened.
Guido van Rossumd8faa362007-04-27 19:54:29 +000098\end{methoddesc}
Guido van Rossum7f61b351994-05-19 09:09:50 +000099
Guido van Rossumd8faa362007-04-27 19:54:29 +0000100\begin{methoddesc}[posixfile]{file}{}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000101 Return the standard file object that the posixfile object is based
102 on. This is sometimes necessary for functions that insist on a
103 standard file object.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000104\end{methoddesc}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000105
Fred Drake295eaa81998-03-11 06:11:37 +0000106All methods raise \exception{IOError} when the request fails.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000107
Fred Drake295eaa81998-03-11 06:11:37 +0000108Format characters for the \method{lock()} method have the following
109meaning:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000110
Fred Drakeee601911998-04-11 20:53:03 +0000111\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000112 \lineii{u}{unlock the specified region}
113 \lineii{r}{request a read lock for the specified section}
114 \lineii{w}{request a write lock for the specified section}
115\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000116
117In addition the following modifiers can be added to the format:
118
Fred Drakeee601911998-04-11 20:53:03 +0000119\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000120 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000121 \lineiii{?}{return the first lock conflicting with the requested lock, or
122 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000123\end{tableiii}
124
Fred Draked146edf1999-04-21 21:15:35 +0000125\noindent
Guido van Rossum7f61b351994-05-19 09:09:50 +0000126Note:
127
Fred Draked146edf1999-04-21 21:15:35 +0000128\begin{description}
129\item[(1)] The lock returned is in the format \code{(\var{mode}, \var{len},
Fred Drake295eaa81998-03-11 06:11:37 +0000130\var{start}, \var{whence}, \var{pid})} where \var{mode} is a character
131representing the type of lock ('r' or 'w'). This modifier prevents a
132request from being granted; it is for query purposes only.
Fred Draked146edf1999-04-21 21:15:35 +0000133\end{description}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000134
Fred Drake295eaa81998-03-11 06:11:37 +0000135Format characters for the \method{flags()} method have the following
136meanings:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000137
Fred Drakeee601911998-04-11 20:53:03 +0000138\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000139 \lineii{a}{append only flag}
140 \lineii{c}{close on exec flag}
141 \lineii{n}{no delay flag (also called non-blocking flag)}
142 \lineii{s}{synchronization flag}
143\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000144
145In addition the following modifiers can be added to the format:
146
Fred Drakeee601911998-04-11 20:53:03 +0000147\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000148 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
149 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
150 \lineiii{?}{return a string in which the characters represent the flags that
151 are set.}{(2)}
152\end{tableiii}
153
Fred Draked146edf1999-04-21 21:15:35 +0000154\noindent
155Notes:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000156
Fred Draked146edf1999-04-21 21:15:35 +0000157\begin{description}
158\item[(1)] The \samp{!} and \samp{=} modifiers are mutually exclusive.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000159
Fred Draked146edf1999-04-21 21:15:35 +0000160\item[(2)] This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000161by the same call.
Fred Draked146edf1999-04-21 21:15:35 +0000162\end{description}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000163
164Examples:
165
Fred Drake19479911998-02-13 06:58:54 +0000166\begin{verbatim}
Fred Drake295eaa81998-03-11 06:11:37 +0000167import posixfile
Guido van Rossum7f61b351994-05-19 09:09:50 +0000168
Fred Drake295eaa81998-03-11 06:11:37 +0000169file = posixfile.open('/tmp/test', 'w')
Guido van Rossum7f61b351994-05-19 09:09:50 +0000170file.lock('w|')
171...
172file.lock('u')
173file.close()
Fred Drake19479911998-02-13 06:58:54 +0000174\end{verbatim}