blob: b1742f12a5086f7b2da6dcb9e638dda8e3a6d086 [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 Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{posixfile}
4
5\modulesynopsis{A file-like object with support for locking.}
6
Fred Drake1f3ab1c1998-02-18 15:22:08 +00007\indexii{\POSIX{}}{file object}
Guido van Rossum7f61b351994-05-19 09:09:50 +00008
Guido van Rossum9b058111996-10-11 17:43:34 +00009\emph{Note:} This module will become obsolete in a future release.
10The locking operation that it provides is done better and more
Fred Drake295eaa81998-03-11 06:11:37 +000011portably by the \function{fcntl.lockf()} call.%
Fred Drake4e7e11a1998-03-12 06:46:16 +000012\withsubitem{(in module fcntl)}{\ttindex{lockf()}}
Guido van Rossum9b058111996-10-11 17:43:34 +000013
Guido van Rossum7f61b351994-05-19 09:09:50 +000014This module implements some additional functionality over the built-in
15file objects. In particular, it implements file locking, control over
16the file flags, and an easy interface to duplicate the file object.
17The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000018has all the standard file object methods and adds the methods
19described below. This module only works for certain flavors of
Fred Drake4e7e11a1998-03-12 06:46:16 +000020\UNIX{}, since it uses \function{fcntl.fcntl()} for file locking.%
21\withsubitem{(in module fcntl)}{\ttindex{fcntl()}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000022
Fred Drake295eaa81998-03-11 06:11:37 +000023To instantiate a posixfile object, use the \function{open()} function
24in the \module{posixfile} module. The resulting object looks and
25feels roughly the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000026
Fred Drake295eaa81998-03-11 06:11:37 +000027The \module{posixfile} module defines the following constants:
Guido van Rossum7f61b351994-05-19 09:09:50 +000028
Fred Drake295eaa81998-03-11 06:11:37 +000029
Guido van Rossum7f61b351994-05-19 09:09:50 +000030\begin{datadesc}{SEEK_SET}
Fred Drakea19bb9b1998-02-13 21:57:33 +000031Offset is calculated from the start of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000032\end{datadesc}
33
34\begin{datadesc}{SEEK_CUR}
Fred Drakea19bb9b1998-02-13 21:57:33 +000035Offset is calculated from the current position in the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000036\end{datadesc}
37
38\begin{datadesc}{SEEK_END}
Fred Drakea19bb9b1998-02-13 21:57:33 +000039Offset is calculated from the end of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000040\end{datadesc}
41
Fred Drake295eaa81998-03-11 06:11:37 +000042The \module{posixfile} module defines the following functions:
Guido van Rossum7f61b351994-05-19 09:09:50 +000043
Guido van Rossum470be141995-03-17 16:07:09 +000044
Fred Drake295eaa81998-03-11 06:11:37 +000045\begin{funcdesc}{open}{filename\optional{, mode\optional{, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000046 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000047 \var{filename}, \var{mode} and \var{bufsize} arguments are
Fred Drake295eaa81998-03-11 06:11:37 +000048 interpreted the same way as by the built-in \function{open()}
49 function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000050\end{funcdesc}
51
Guido van Rossum31cce971995-01-04 19:17:34 +000052\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000053 Create a new posixfile object with the given standard file object.
54 The resulting object has the same filename and mode as the original
55 file object.
56\end{funcdesc}
57
58The posixfile object defines the following additional methods:
59
Fred Drake19479911998-02-13 06:58:54 +000060\setindexsubitem{(posixfile method)}
Fred Drake295eaa81998-03-11 06:11:37 +000061\begin{funcdesc}{lock}{fmt, \optional{len\optional{, start\optional{, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000062 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000063 referring to. The format is explained
64 below in a table. The \var{len} argument specifies the length of the
65 section that should be locked. The default is \code{0}. \var{start}
66 specifies the starting offset of the section, where the default is
67 \code{0}. The \var{whence} argument specifies where the offset is
Fred Drake295eaa81998-03-11 06:11:37 +000068 relative to. It accepts one of the constants \constant{SEEK_SET},
69 \constant{SEEK_CUR} or \constant{SEEK_END}. The default is
70 \constant{SEEK_SET}. For more information about the arguments refer
71 to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000072\end{funcdesc}
73
Guido van Rossum96628a91995-04-10 11:34:00 +000074\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000075 Set the specified flags for the file that the file object is referring
76 to. The new flags are ORed with the old flags, unless specified
77 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000078 the \var{flags} argument
79 a string indicating the current flags is returned (this is
Fred Drake295eaa81998-03-11 06:11:37 +000080 the same as the \samp{?} modifier). For more information about the
81 flags refer to the \manpage{fcntl}{2} manual page on your system.
Guido van Rossum7f61b351994-05-19 09:09:50 +000082\end{funcdesc}
83
84\begin{funcdesc}{dup}{}
85 Duplicate the file object and the underlying file pointer and file
86 descriptor. The resulting object behaves as if it were newly
87 opened.
88\end{funcdesc}
89
90\begin{funcdesc}{dup2}{fd}
91 Duplicate the file object and the underlying file pointer and file
92 descriptor. The new object will have the given file descriptor.
93 Otherwise the resulting object behaves as if it were newly opened.
94\end{funcdesc}
95
96\begin{funcdesc}{file}{}
97 Return the standard file object that the posixfile object is based
98 on. This is sometimes necessary for functions that insist on a
99 standard file object.
100\end{funcdesc}
101
Fred Drake295eaa81998-03-11 06:11:37 +0000102All methods raise \exception{IOError} when the request fails.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000103
Fred Drake295eaa81998-03-11 06:11:37 +0000104Format characters for the \method{lock()} method have the following
105meaning:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000106
Fred Drakeee601911998-04-11 20:53:03 +0000107\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000108 \lineii{u}{unlock the specified region}
109 \lineii{r}{request a read lock for the specified section}
110 \lineii{w}{request a write lock for the specified section}
111\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000112
113In addition the following modifiers can be added to the format:
114
Fred Drakeee601911998-04-11 20:53:03 +0000115\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000116 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000117 \lineiii{?}{return the first lock conflicting with the requested lock, or
118 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000119\end{tableiii}
120
121Note:
122
Fred Drake295eaa81998-03-11 06:11:37 +0000123(1) The lock returned is in the format \code{(\var{mode}, \var{len},
124\var{start}, \var{whence}, \var{pid})} where \var{mode} is a character
125representing the type of lock ('r' or 'w'). This modifier prevents a
126request from being granted; it is for query purposes only.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000127
Fred Drake295eaa81998-03-11 06:11:37 +0000128Format characters for the \method{flags()} method have the following
129meanings:
Guido van Rossum7f61b351994-05-19 09:09:50 +0000130
Fred Drakeee601911998-04-11 20:53:03 +0000131\begin{tableii}{c|l}{samp}{Format}{Meaning}
Fred Drake7f962911997-12-23 04:21:20 +0000132 \lineii{a}{append only flag}
133 \lineii{c}{close on exec flag}
134 \lineii{n}{no delay flag (also called non-blocking flag)}
135 \lineii{s}{synchronization flag}
136\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000137
138In addition the following modifiers can be added to the format:
139
Fred Drakeee601911998-04-11 20:53:03 +0000140\begin{tableiii}{c|l|c}{samp}{Modifier}{Meaning}{Notes}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000141 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
142 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
143 \lineiii{?}{return a string in which the characters represent the flags that
144 are set.}{(2)}
145\end{tableiii}
146
147Note:
148
Fred Drake396b8271998-04-07 19:17:27 +0000149(1) The \samp{!} and \samp{=} modifiers are mutually exclusive.
Guido van Rossum7f61b351994-05-19 09:09:50 +0000150
Guido van Rossum16d6e711994-08-08 12:30:22 +0000151(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000152by the same call.
153
154Examples:
155
Fred Drake19479911998-02-13 06:58:54 +0000156\begin{verbatim}
Fred Drake295eaa81998-03-11 06:11:37 +0000157import posixfile
Guido van Rossum7f61b351994-05-19 09:09:50 +0000158
Fred Drake295eaa81998-03-11 06:11:37 +0000159file = posixfile.open('/tmp/test', 'w')
Guido van Rossum7f61b351994-05-19 09:09:50 +0000160file.lock('w|')
161...
162file.lock('u')
163file.close()
Fred Drake19479911998-02-13 06:58:54 +0000164\end{verbatim}