blob: 787a84ee85bda12f213cd4900cf5e5d636e47aaa [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}}
Fred Drake1f3ab1c1998-02-18 15:22:08 +00003\label{module-posixfile}
Guido van Rossum7f61b351994-05-19 09:09:50 +00004\bimodindex{posixfile}
Fred Drake1f3ab1c1998-02-18 15:22:08 +00005\indexii{\POSIX{}}{file object}
Guido van Rossum7f61b351994-05-19 09:09:50 +00006
Guido van Rossum9b058111996-10-11 17:43:34 +00007\emph{Note:} This module will become obsolete in a future release.
8The locking operation that it provides is done better and more
9portably by the \code{fcntl.lockf()} call.
10
Guido van Rossum7f61b351994-05-19 09:09:50 +000011This module implements some additional functionality over the built-in
12file objects. In particular, it implements file locking, control over
13the file flags, and an easy interface to duplicate the file object.
14The module defines a new file object, the posixfile object. It
Guido van Rossum470be141995-03-17 16:07:09 +000015has all the standard file object methods and adds the methods
16described below. This module only works for certain flavors of
17\UNIX{}, since it uses \code{fcntl()} for file locking.
Guido van Rossum7f61b351994-05-19 09:09:50 +000018
19To instantiate a posixfile object, use the \code{open()} function in
Guido van Rossum470be141995-03-17 16:07:09 +000020the posixfile module. The resulting object looks and feels roughly
21the same as a standard file object.
Guido van Rossum7f61b351994-05-19 09:09:50 +000022
23The posixfile module defines the following constants:
24
Fred Drake19479911998-02-13 06:58:54 +000025\setindexsubitem{(in module posixfile)}
Guido van Rossum7f61b351994-05-19 09:09:50 +000026\begin{datadesc}{SEEK_SET}
Fred Drakea19bb9b1998-02-13 21:57:33 +000027Offset is calculated from the start of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000028\end{datadesc}
29
30\begin{datadesc}{SEEK_CUR}
Fred Drakea19bb9b1998-02-13 21:57:33 +000031Offset is calculated from the current position in the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000032\end{datadesc}
33
34\begin{datadesc}{SEEK_END}
Fred Drakea19bb9b1998-02-13 21:57:33 +000035Offset is calculated from the end of the file.
Guido van Rossum7f61b351994-05-19 09:09:50 +000036\end{datadesc}
37
38The posixfile module defines the following functions:
39
Fred Drake19479911998-02-13 06:58:54 +000040\setindexsubitem{(in module posixfile)}
Guido van Rossum470be141995-03-17 16:07:09 +000041
42\begin{funcdesc}{open}{filename\optional{\, mode\optional{\, bufsize}}}
Guido van Rossum7f61b351994-05-19 09:09:50 +000043 Create a new posixfile object with the given filename and mode. The
Guido van Rossum470be141995-03-17 16:07:09 +000044 \var{filename}, \var{mode} and \var{bufsize} arguments are
Guido van Rossumf23e0fe1995-03-18 11:04:29 +000045 interpreted the same way as by the built-in \code{open()} function.
Guido van Rossum7f61b351994-05-19 09:09:50 +000046\end{funcdesc}
47
Guido van Rossum31cce971995-01-04 19:17:34 +000048\begin{funcdesc}{fileopen}{fileobject}
Guido van Rossum7f61b351994-05-19 09:09:50 +000049 Create a new posixfile object with the given standard file object.
50 The resulting object has the same filename and mode as the original
51 file object.
52\end{funcdesc}
53
54The posixfile object defines the following additional methods:
55
Fred Drake19479911998-02-13 06:58:54 +000056\setindexsubitem{(posixfile method)}
Guido van Rossumee833cc1998-02-11 22:34:51 +000057\begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start\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
Fred Drake7f962911997-12-23 04:21:20 +0000102\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
103 \lineii{u}{unlock the specified region}
104 \lineii{r}{request a read lock for the specified section}
105 \lineii{w}{request a write lock for the specified section}
106\end{tableii}
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
Fred Drake7f962911997-12-23 04:21:20 +0000125\begin{tableii}{|c|l|}{samp}{Format}{Meaning}
126 \lineii{a}{append only flag}
127 \lineii{c}{close on exec flag}
128 \lineii{n}{no delay flag (also called non-blocking flag)}
129 \lineii{s}{synchronization flag}
130\end{tableii}
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
Fred Drake19479911998-02-13 06:58:54 +0000150\begin{verbatim}
Guido van Rossum7f61b351994-05-19 09:09:50 +0000151from posixfile import *
152
153file = open('/tmp/test', 'w')
154file.lock('w|')
155...
156file.lock('u')
157file.close()
Fred Drake19479911998-02-13 06:58:54 +0000158\end{verbatim}