blob: 5c853e778c4b967d3f7c94b98cb51a4eb8e64e72 [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 Rossum16d6e711994-08-08 12:30:22 +000056\begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start
57\optional{\, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000058 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000059 referring to. The format is explained
60 below in a table. The \var{len} argument specifies the length of the
61 section that should be locked. The default is \code{0}. \var{start}
62 specifies the starting offset of the section, where the default is
63 \code{0}. The \var{whence} argument specifies where the offset is
Guido van Rossum7f61b351994-05-19 09:09:50 +000064 relative to. It accepts one of the constants \code{SEEK_SET},
65 \code{SEEK_CUR} or \code{SEEK_END}. The default is \code{SEEK_SET}.
66 For more information about the arguments refer to the fcntl
67 manual page on your system.
68\end{funcdesc}
69
Guido van Rossum96628a91995-04-10 11:34:00 +000070\begin{funcdesc}{flags}{\optional{flags}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000071 Set the specified flags for the file that the file object is referring
72 to. The new flags are ORed with the old flags, unless specified
73 otherwise. The format is explained below in a table. Without
Guido van Rossum96628a91995-04-10 11:34:00 +000074 the \var{flags} argument
75 a string indicating the current flags is returned (this is
Guido van Rossum16d6e711994-08-08 12:30:22 +000076 the same as the '?' modifier). For more information about the flags
Guido van Rossum7f61b351994-05-19 09:09:50 +000077 refer to the fcntl manual page on your system.
78\end{funcdesc}
79
80\begin{funcdesc}{dup}{}
81 Duplicate the file object and the underlying file pointer and file
82 descriptor. The resulting object behaves as if it were newly
83 opened.
84\end{funcdesc}
85
86\begin{funcdesc}{dup2}{fd}
87 Duplicate the file object and the underlying file pointer and file
88 descriptor. The new object will have the given file descriptor.
89 Otherwise the resulting object behaves as if it were newly opened.
90\end{funcdesc}
91
92\begin{funcdesc}{file}{}
93 Return the standard file object that the posixfile object is based
94 on. This is sometimes necessary for functions that insist on a
95 standard file object.
96\end{funcdesc}
97
98All methods return \code{IOError} when the request fails.
99
100Format characters for the \code{lock()} method have the following meaning:
101
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000102\begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{}
103 \lineiii{u}{unlock the specified region}{}
104 \lineiii{r}{request a read lock for the specified section}{}
105 \lineiii{w}{request a write lock for the specified section}{}
106\end{tableiii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000107
108In addition the following modifiers can be added to the format:
109
110\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
111 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum470be141995-03-17 16:07:09 +0000112 \lineiii{?}{return the first lock conflicting with the requested lock, or
113 \code{None} if there is no conflict.}{(1)}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000114\end{tableiii}
115
116Note:
117
118(1) The lock returned is in the format \code{(mode, len, start,
119whence, pid)} where mode is a character representing the type of lock
120('r' or 'w'). This modifier prevents a request from being granted; it
121is for query purposes only.
122
123Format character for the \code{flags()} method have the following meaning:
124
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000125\begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{}
126 \lineiii{a}{append only flag}{}
127 \lineiii{c}{close on exec flag}{}
128 \lineiii{n}{no delay flag (also called non-blocking flag)}{}
129 \lineiii{s}{synchronization flag}{}
130\end{tableiii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000131
132In addition the following modifiers can be added to the format:
133
134\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
135 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
136 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
137 \lineiii{?}{return a string in which the characters represent the flags that
138 are set.}{(2)}
139\end{tableiii}
140
141Note:
142
143(1) The \code{!} and \code{=} modifiers are mutually exclusive.
144
Guido van Rossum16d6e711994-08-08 12:30:22 +0000145(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000146by the same call.
147
148Examples:
149
150\bcode\begin{verbatim}
151from posixfile import *
152
153file = open('/tmp/test', 'w')
154file.lock('w|')
155...
156file.lock('u')
157file.close()
158\end{verbatim}\ecode