blob: 3d1fad1297db39eb700a657fc6be0988af023757 [file] [log] [blame]
Guido van Rossum7f61b351994-05-19 09:09:50 +00001% Manual text and implementation by Jaap Vermeulen
2\section{Standard Module \sectcode{posixfile}}
3\bimodindex{posixfile}
4\indexii{posix}{file object}
5
Guido van Rossum9b058111996-10-11 17:43:34 +00006\emph{Note:} This module will become obsolete in a future release.
7The locking operation that it provides is done better and more
8portably by the \code{fcntl.lockf()} call.
9
Guido van Rossum7f61b351994-05-19 09:09:50 +000010This module implements some additional functionality over the built-in
11file objects. In particular, it implements file locking, control over
12the file flags, and an easy interface to duplicate the file object.
13The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000014has all the standard file object methods and adds the methods
15described below. This module only works for certain flavors of
16\UNIX{}, since it uses \code{fcntl()} for file locking.
Guido van Rossum7f61b351994-05-19 09:09:50 +000017
18To instantiate a posixfile object, use the \code{open()} function in
Guido van Rossum470be141995-03-17 16:07:09 +000019the posixfile module. The resulting object looks and feels roughly
20the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000021
22The posixfile module defines the following constants:
23
24\renewcommand{\indexsubitem}{(in module posixfile)}
25\begin{datadesc}{SEEK_SET}
26offset is calculated from the start of the file
27\end{datadesc}
28
29\begin{datadesc}{SEEK_CUR}
30offset is calculated from the current position in the file
31\end{datadesc}
32
33\begin{datadesc}{SEEK_END}
34offset is calculated from the end of the file
35\end{datadesc}
36
37The posixfile module defines the following functions:
38
39\renewcommand{\indexsubitem}{(in module posixfile)}
Guido van Rossum470be141995-03-17 16:07:09 +000040
41\begin{funcdesc}{open}{filename\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000042 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000043 \var{filename}, \var{mode} and \var{bufsize} arguments are
Guido van Rossumf23e0fe1995-03-18 11:04:29 +000044 interpreted the same way as by the built-in \code{open()} function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000045\end{funcdesc}
46
Guido van Rossum31cce971995-01-04 19:17:34 +000047\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000048 Create a new posixfile object with the given standard file object.
49 The resulting object has the same filename and mode as the original
50 file object.
51\end{funcdesc}
52
53The posixfile object defines the following additional methods:
54
55\renewcommand{\indexsubitem}{(posixfile method)}
Guido van Rossumee833cc1998-02-11 22:34:51 +000056\begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start\optional{\, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000057 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000058 referring to. The format is explained
59 below in a table. The \var{len} argument specifies the length of the
60 section that should be locked. The default is \code{0}. \var{start}
61 specifies the starting offset of the section, where the default is
62 \code{0}. The \var{whence} argument specifies where the offset is
Guido van Rossum7f61b351994-05-19 09:09:50 +000063 relative to. It accepts one of the constants \code{SEEK_SET},
64 \code{SEEK_CUR} or \code{SEEK_END}. The default is \code{SEEK_SET}.
65 For more information about the arguments refer to the fcntl
66 manual page on your system.
67\end{funcdesc}
68
Guido van Rossum96628a91995-04-10 11:34:00 +000069\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000070 Set the specified flags for the file that the file object is referring
71 to. The new flags are ORed with the old flags, unless specified
72 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000073 the \var{flags} argument
74 a string indicating the current flags is returned (this is
Guido van Rossum16d6e711994-08-08 12:30:22 +000075 the same as the '?' modifier). For more information about the flags
Guido van Rossum7f61b351994-05-19 09:09:50 +000076 refer to the fcntl manual page on your system.
77\end{funcdesc}
78
79\begin{funcdesc}{dup}{}
80 Duplicate the file object and the underlying file pointer and file
81 descriptor. The resulting object behaves as if it were newly
82 opened.
83\end{funcdesc}
84
85\begin{funcdesc}{dup2}{fd}
86 Duplicate the file object and the underlying file pointer and file
87 descriptor. The new object will have the given file descriptor.
88 Otherwise the resulting object behaves as if it were newly opened.
89\end{funcdesc}
90
91\begin{funcdesc}{file}{}
92 Return the standard file object that the posixfile object is based
93 on. This is sometimes necessary for functions that insist on a
94 standard file object.
95\end{funcdesc}
96
97All methods return \code{IOError} when the request fails.
98
99Format characters for the \code{lock()} method have the following meaning:
100
Fred Drake7f962911997-12-23 04:21:20 +0000101\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
102 \lineii{u}{unlock the specified region}
103 \lineii{r}{request a read lock for the specified section}
104 \lineii{w}{request a write lock for the specified section}
105\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000106
107In addition the following modifiers can be added to the format:
108
109\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
110 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000111 \lineiii{?}{return the first lock conflicting with the requested lock, or
112 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000113\end{tableiii}
114
115Note:
116
117(1) The lock returned is in the format \code{(mode, len, start,
118whence, pid)} where mode is a character representing the type of lock
119('r' or 'w'). This modifier prevents a request from being granted; it
120is for query purposes only.
121
122Format character for the \code{flags()} method have the following meaning:
123
Fred Drake7f962911997-12-23 04:21:20 +0000124\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
125 \lineii{a}{append only flag}
126 \lineii{c}{close on exec flag}
127 \lineii{n}{no delay flag (also called non-blocking flag)}
128 \lineii{s}{synchronization flag}
129\end{tableii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000130
131In addition the following modifiers can be added to the format:
132
133\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
134 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
135 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
136 \lineiii{?}{return a string in which the characters represent the flags that
137 are set.}{(2)}
138\end{tableiii}
139
140Note:
141
142(1) The \code{!} and \code{=} modifiers are mutually exclusive.
143
Guido van Rossum16d6e711994-08-08 12:30:22 +0000144(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000145by the same call.
146
147Examples:
148
149\bcode\begin{verbatim}
150from posixfile import *
151
152file = open('/tmp/test', 'w')
153file.lock('w|')
154...
155file.lock('u')
156file.close()
157\end{verbatim}\ecode