blob: 050ba5a94ae70133a7da6a65cbea1b0b2342d825 [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
6This module implements some additional functionality over the built-in
7file objects. In particular, it implements file locking, control over
8the file flags, and an easy interface to duplicate the file object.
9The module defines a new file object, the posixfile object. It
10inherits all the standard file object methods and adds the methods
11described below.
12
13To instantiate a posixfile object, use the \code{open()} function in
14the posixfile module. The resulting object looks and feels the same as
15a standard file object.
16
17The posixfile module defines the following constants:
18
19\renewcommand{\indexsubitem}{(in module posixfile)}
20\begin{datadesc}{SEEK_SET}
21offset is calculated from the start of the file
22\end{datadesc}
23
24\begin{datadesc}{SEEK_CUR}
25offset is calculated from the current position in the file
26\end{datadesc}
27
28\begin{datadesc}{SEEK_END}
29offset is calculated from the end of the file
30\end{datadesc}
31
32The posixfile module defines the following functions:
33
34\renewcommand{\indexsubitem}{(in module posixfile)}
35\begin{funcdesc}{open}{filename\, mode}
36 Create a new posixfile object with the given filename and mode. The
37 filename and mode are interpreted the same way as the \code{open()}
38 builtin function.
39\end{funcdesc}
40
Guido van Rossum31cce971995-01-04 19:17:34 +000041\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000042 Create a new posixfile object with the given standard file object.
43 The resulting object has the same filename and mode as the original
44 file object.
45\end{funcdesc}
46
47The posixfile object defines the following additional methods:
48
49\renewcommand{\indexsubitem}{(posixfile method)}
Guido van Rossum16d6e711994-08-08 12:30:22 +000050\begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start
51\optional{\, whence}}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000052 Lock the specified section of the file that the file object is
Guido van Rossum16d6e711994-08-08 12:30:22 +000053 referring to. The format is explained
54 below in a table. The \var{len} argument specifies the length of the
55 section that should be locked. The default is \code{0}. \var{start}
56 specifies the starting offset of the section, where the default is
57 \code{0}. The \var{whence} argument specifies where the offset is
Guido van Rossum7f61b351994-05-19 09:09:50 +000058 relative to. It accepts one of the constants \code{SEEK_SET},
59 \code{SEEK_CUR} or \code{SEEK_END}. The default is \code{SEEK_SET}.
60 For more information about the arguments refer to the fcntl
61 manual page on your system.
62\end{funcdesc}
63
64\begin{funcdesc}{flags}{fmt}
65 Set the specified flags for the file that the file object is referring
66 to. The new flags are ORed with the old flags, unless specified
67 otherwise. The format is explained below in a table. Without
68 arguments a string indicating the current flags is returned (this is
Guido van Rossum16d6e711994-08-08 12:30:22 +000069 the same as the '?' modifier). For more information about the flags
Guido van Rossum7f61b351994-05-19 09:09:50 +000070 refer to the fcntl manual page on your system.
71\end{funcdesc}
72
73\begin{funcdesc}{dup}{}
74 Duplicate the file object and the underlying file pointer and file
75 descriptor. The resulting object behaves as if it were newly
76 opened.
77\end{funcdesc}
78
79\begin{funcdesc}{dup2}{fd}
80 Duplicate the file object and the underlying file pointer and file
81 descriptor. The new object will have the given file descriptor.
82 Otherwise the resulting object behaves as if it were newly opened.
83\end{funcdesc}
84
85\begin{funcdesc}{file}{}
86 Return the standard file object that the posixfile object is based
87 on. This is sometimes necessary for functions that insist on a
88 standard file object.
89\end{funcdesc}
90
91All methods return \code{IOError} when the request fails.
92
93Format characters for the \code{lock()} method have the following meaning:
94
Guido van Rossumb8b264b1994-08-12 13:13:50 +000095\begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{}
96 \lineiii{u}{unlock the specified region}{}
97 \lineiii{r}{request a read lock for the specified section}{}
98 \lineiii{w}{request a write lock for the specified section}{}
99\end{tableiii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000100
101In addition the following modifiers can be added to the format:
102
103\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
104 \lineiii{|}{wait until the lock has been granted}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000105 \lineiii{?}{return the first lock conflicting with the requested lock,}{(1)}
106 {}&{\hskip0.5cm or \code{None} if there is no conflict.}&{}\\
Guido van Rossum7f61b351994-05-19 09:09:50 +0000107\end{tableiii}
108
109Note:
110
111(1) The lock returned is in the format \code{(mode, len, start,
112whence, pid)} where mode is a character representing the type of lock
113('r' or 'w'). This modifier prevents a request from being granted; it
114is for query purposes only.
115
116Format character for the \code{flags()} method have the following meaning:
117
Guido van Rossumb8b264b1994-08-12 13:13:50 +0000118\begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{}
119 \lineiii{a}{append only flag}{}
120 \lineiii{c}{close on exec flag}{}
121 \lineiii{n}{no delay flag (also called non-blocking flag)}{}
122 \lineiii{s}{synchronization flag}{}
123\end{tableiii}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000124
125In addition the following modifiers can be added to the format:
126
127\begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes}
128 \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)}
129 \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)}
130 \lineiii{?}{return a string in which the characters represent the flags that
131 are set.}{(2)}
132\end{tableiii}
133
134Note:
135
136(1) The \code{!} and \code{=} modifiers are mutually exclusive.
137
Guido van Rossum16d6e711994-08-08 12:30:22 +0000138(2) This string represents the flags after they may have been altered
Guido van Rossum7f61b351994-05-19 09:09:50 +0000139by the same call.
140
141Examples:
142
143\bcode\begin{verbatim}
144from posixfile import *
145
146file = open('/tmp/test', 'w')
147file.lock('w|')
148...
149file.lock('u')
150file.close()
151\end{verbatim}\ecode