blob: 9c0ffd8186b73d3ec99800a16b348a3bf3eb08f5 [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} ---
3 A file-like object with support for locking.}
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{builtin}{posixfile}
5
6\modulesynopsis{A file-like object with support for locking.}
7
Fred Drake1f3ab1c1998-02-18 15:22:08 +00008\indexii{\POSIX{}}{file object}
Guido van Rossum7f61b351994-05-19 09:09:50 +00009
Guido van Rossum9b058111996-10-11 17:43:34 +000010\emph{Note:} This module will become obsolete in a future release.
11The locking operation that it provides is done better and more
Fred Drake295eaa81998-03-11 06:11:37 +000012portably by the \function{fcntl.lockf()} call.%
Fred Drake4e7e11a1998-03-12 06:46:16 +000013\withsubitem{(in module fcntl)}{\ttindex{lockf()}}
Guido van Rossum9b058111996-10-11 17:43:34 +000014
Guido van Rossum7f61b351994-05-19 09:09:50 +000015This module implements some additional functionality over the built-in
16file objects. In particular, it implements file locking, control over
17the file flags, and an easy interface to duplicate the file object.
18The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000019has all the standard file object methods and adds the methods
20described below. This module only works for certain flavors of
Fred Drake4e7e11a1998-03-12 06:46:16 +000021\UNIX{}, since it uses \function{fcntl.fcntl()} for file locking.%
22\withsubitem{(in module fcntl)}{\ttindex{fcntl()}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000023
Fred Drake295eaa81998-03-11 06:11:37 +000024To instantiate a posixfile object, use the \function{open()} function
25in the \module{posixfile} module. The resulting object looks and
26feels roughly the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000027
Fred Drake295eaa81998-03-11 06:11:37 +000028The \module{posixfile} module defines the following constants:
Guido van Rossum7f61b351994-05-19 09:09:50 +000029
Fred Drake295eaa81998-03-11 06:11:37 +000030
Guido van Rossum7f61b351994-05-19 09:09:50 +000031\begin{datadesc}{SEEK_SET}
Fred Drakea19bb9b1998-02-13 21:57:33 +000032Offset is calculated from the start of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000033\end{datadesc}
34
35\begin{datadesc}{SEEK_CUR}
Fred Drakea19bb9b1998-02-13 21:57:33 +000036Offset is calculated from the current position in the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000037\end{datadesc}
38
39\begin{datadesc}{SEEK_END}
Fred Drakea19bb9b1998-02-13 21:57:33 +000040Offset is calculated from the end of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000041\end{datadesc}
42
Fred Drake295eaa81998-03-11 06:11:37 +000043The \module{posixfile} module defines the following functions:
Guido van Rossum7f61b351994-05-19 09:09:50 +000044
Guido van Rossum470be141995-03-17 16:07:09 +000045
Fred Drake295eaa81998-03-11 06:11:37 +000046\begin{funcdesc}{open}{filename\optional{, mode\optional{, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000047 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000048 \var{filename}, \var{mode} and \var{bufsize} arguments are
Fred Drake295eaa81998-03-11 06:11:37 +000049 interpreted the same way as by the built-in \function{open()}
50 function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000051\end{funcdesc}
52
Guido van Rossum31cce971995-01-04 19:17:34 +000053\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000054 Create a new posixfile object with the given standard file object.
55 The resulting object has the same filename and mode as the original
56 file object.
57\end{funcdesc}
58
59The posixfile object defines the following additional methods:
60
Fred Drake19479911998-02-13 06:58:54 +000061\setindexsubitem{(posixfile method)}
Fred Drake295eaa81998-03-11 06:11:37 +000062\begin{funcdesc}{lock}{fmt, \optional{len\optional{, start\optional{, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000063 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000064 referring to. The format is explained
65 below in a table. The \var{len} argument specifies the length of the
66 section that should be locked. The default is \code{0}. \var{start}
67 specifies the starting offset of the section, where the default is
68 \code{0}. The \var{whence} argument specifies where the offset is
Fred Drake295eaa81998-03-11 06:11:37 +000069 relative to. It accepts one of the constants \constant{SEEK_SET},
70 \constant{SEEK_CUR} or \constant{SEEK_END}. The default is
71 \constant{SEEK_SET}. For more information about the arguments refer
72 to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000073\end{funcdesc}
74
Guido van Rossum96628a91995-04-10 11:34:00 +000075\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000076 Set the specified flags for the file that the file object is referring
77 to. The new flags are ORed with the old flags, unless specified
78 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000079 the \var{flags} argument
80 a string indicating the current flags is returned (this is
Fred Drake295eaa81998-03-11 06:11:37 +000081 the same as the \samp{?} modifier). For more information about the
82 flags refer to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000083\end{funcdesc}
84
85\begin{funcdesc}{dup}{}
86 Duplicate the file object and the underlying file pointer and file
87 descriptor. The resulting object behaves as if it were newly
88 opened.
89\end{funcdesc}
90
91\begin{funcdesc}{dup2}{fd}
92 Duplicate the file object and the underlying file pointer and file
93 descriptor. The new object will have the given file descriptor.
94 Otherwise the resulting object behaves as if it were newly opened.
95\end{funcdesc}
96
97\begin{funcdesc}{file}{}
98 Return the standard file object that the posixfile object is based
99 on. This is sometimes necessary for functions that insist on a
100 standard file object.
101\end{funcdesc}
102
Fred Drake295eaa81998-03-11 06:11:37 +0000103All methods raise \exception{IOError} when the request fails.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000104
Fred Drake295eaa81998-03-11 06:11:37 +0000105Format characters for the \method{lock()} method have the following
106meaning:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000107
Fred Drakeee601911998-04-11 20:53:03 +0000108\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000109 \lineii{u}{unlock the specified region}
110 \lineii{r}{request a read lock for the specified section}
111 \lineii{w}{request a write lock for the specified section}
112\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000113
114In addition the following modifiers can be added to the format:
115
Fred Drakeee601911998-04-11 20:53:03 +0000116\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000117 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000118 \lineiii{?}{return the first lock conflicting with the requested lock, or
119 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000120\end{tableiii}
121
122Note:
123
Fred Drake295eaa81998-03-11 06:11:37 +0000124(1) The lock returned is in the format \code{(\var{mode}, \var{len},
125\var{start}, \var{whence}, \var{pid})} where \var{mode} is a character
126representing the type of lock ('r' or 'w'). This modifier prevents a
127request from being granted; it is for query purposes only.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000128
Fred Drake295eaa81998-03-11 06:11:37 +0000129Format characters for the \method{flags()} method have the following
130meanings:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000131
Fred Drakeee601911998-04-11 20:53:03 +0000132\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000133 \lineii{a}{append only flag}
134 \lineii{c}{close on exec flag}
135 \lineii{n}{no delay flag (also called non-blocking flag)}
136 \lineii{s}{synchronization flag}
137\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000138
139In addition the following modifiers can be added to the format:
140
Fred Drakeee601911998-04-11 20:53:03 +0000141\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000142 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
143 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
144 \lineiii{?}{return a string in which the characters represent the flags that
145 are set.}{(2)}
146\end{tableiii}
147
148Note:
149
Fred Drake396b8271998-04-07 19:17:27 +0000150(1) The \samp{!} and \samp{=} modifiers are mutually exclusive.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000151
Guido van Rossum16d6e711994-08-08 12:30:22 +0000152(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000153by the same call.
154
155Examples:
156
Fred Drake19479911998-02-13 06:58:54 +0000157\begin{verbatim}
Fred Drake295eaa81998-03-11 06:11:37 +0000158import posixfile
Guido van Rossum7f61b351994-05-19 09:09:50 +0000159
Fred Drake295eaa81998-03-11 06:11:37 +0000160file = posixfile.open('/tmp/test', 'w')
Guido van Rossum7f61b351994-05-19 09:09:50 +0000161file.lock('w|')
162...
163file.lock('u')
164file.close()
Fred Drake19479911998-02-13 06:58:54 +0000165\end{verbatim}