blob: ec58b94635c98b6a11f27164becd1142a7e5bca5 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. % Manual text and implementation by Jaap Vermeulen
2
3
4:mod:`posixfile` --- File-like objects with locking support
5===========================================================
6
7.. module:: posixfile
8 :platform: Unix
9 :synopsis: A file-like object with support for locking.
10.. moduleauthor:: Jaap Vermeulen
11.. sectionauthor:: Jaap Vermeulen
12
13
14.. index:: pair: POSIX; file object
15
16.. deprecated:: 1.5
17 .. index:: single: lockf() (in module fcntl)
18
19 The locking operation that this module provides is done better and more portably
20 by the :func:`fcntl.lockf` call.
21
22.. index:: single: fcntl() (in module fcntl)
23
24This module implements some additional functionality over the built-in file
25objects. In particular, it implements file locking, control over the file
26flags, and an easy interface to duplicate the file object. The module defines a
27new file object, the posixfile object. It has all the standard file object
28methods and adds the methods described below. This module only works for
29certain flavors of Unix, since it uses :func:`fcntl.fcntl` for file locking.
30
31.. %
32
33To instantiate a posixfile object, use the :func:`open` function in the
34:mod:`posixfile` module. The resulting object looks and feels roughly the same
35as a standard file object.
36
37The :mod:`posixfile` module defines the following constants:
38
39
40.. data:: SEEK_SET
41
42 Offset is calculated from the start of the file.
43
44
45.. data:: SEEK_CUR
46
47 Offset is calculated from the current position in the file.
48
49
50.. data:: SEEK_END
51
52 Offset is calculated from the end of the file.
53
54The :mod:`posixfile` module defines the following functions:
55
56
57.. function:: open(filename[, mode[, bufsize]])
58
59 Create a new posixfile object with the given filename and mode. The *filename*,
60 *mode* and *bufsize* arguments are interpreted the same way as by the built-in
61 :func:`open` function.
62
63
64.. function:: fileopen(fileobject)
65
66 Create a new posixfile object with the given standard file object. The resulting
67 object has the same filename and mode as the original file object.
68
69The posixfile object defines the following additional methods:
70
71
72.. method:: posixfile.lock(fmt, [len[, start[, whence]]])
73
74 Lock the specified section of the file that the file object is referring to.
75 The format is explained below in a table. The *len* argument specifies the
76 length of the section that should be locked. The default is ``0``. *start*
77 specifies the starting offset of the section, where the default is ``0``. The
78 *whence* argument specifies where the offset is relative to. It accepts one of
79 the constants :const:`SEEK_SET`, :const:`SEEK_CUR` or :const:`SEEK_END`. The
80 default is :const:`SEEK_SET`. For more information about the arguments refer to
81 the :manpage:`fcntl(2)` manual page on your system.
82
83
84.. method:: posixfile.flags([flags])
85
86 Set the specified flags for the file that the file object is referring to. The
87 new flags are ORed with the old flags, unless specified otherwise. The format
88 is explained below in a table. Without the *flags* argument a string indicating
89 the current flags is returned (this is the same as the ``?`` modifier). For
90 more information about the flags refer to the :manpage:`fcntl(2)` manual page on
91 your system.
92
93
94.. method:: posixfile.dup()
95
96 Duplicate the file object and the underlying file pointer and file descriptor.
97 The resulting object behaves as if it were newly opened.
98
99
100.. method:: posixfile.dup2(fd)
101
102 Duplicate the file object and the underlying file pointer and file descriptor.
103 The new object will have the given file descriptor. Otherwise the resulting
104 object behaves as if it were newly opened.
105
106
107.. method:: posixfile.file()
108
109 Return the standard file object that the posixfile object is based on. This is
110 sometimes necessary for functions that insist on a standard file object.
111
112All methods raise :exc:`IOError` when the request fails.
113
114Format characters for the :meth:`lock` method have the following meaning:
115
116+--------+-----------------------------------------------+
117| Format | Meaning |
118+========+===============================================+
119| ``u`` | unlock the specified region |
120+--------+-----------------------------------------------+
121| ``r`` | request a read lock for the specified section |
122+--------+-----------------------------------------------+
123| ``w`` | request a write lock for the specified |
124| | section |
125+--------+-----------------------------------------------+
126
127In addition the following modifiers can be added to the format:
128
129+----------+--------------------------------+-------+
130| Modifier | Meaning | Notes |
131+==========+================================+=======+
132| ``|`` | wait until the lock has been | |
133| | granted | |
134+----------+--------------------------------+-------+
135| ``?`` | return the first lock | \(1) |
136| | conflicting with the requested | |
137| | lock, or ``None`` if there is | |
138| | no conflict. | |
139+----------+--------------------------------+-------+
140
141Note:
142
143(1)
144 The lock returned is in the format ``(mode, len, start, whence, pid)`` where
145 *mode* is a character representing the type of lock ('r' or 'w'). This modifier
146 prevents a request from being granted; it is for query purposes only.
147
148Format characters for the :meth:`flags` method have the following meanings:
149
150+--------+-----------------------------------------------+
151| Format | Meaning |
152+========+===============================================+
153| ``a`` | append only flag |
154+--------+-----------------------------------------------+
155| ``c`` | close on exec flag |
156+--------+-----------------------------------------------+
157| ``n`` | no delay flag (also called non-blocking flag) |
158+--------+-----------------------------------------------+
159| ``s`` | synchronization flag |
160+--------+-----------------------------------------------+
161
162In addition the following modifiers can be added to the format:
163
164+----------+---------------------------------+-------+
165| Modifier | Meaning | Notes |
166+==========+=================================+=======+
167| ``!`` | turn the specified flags 'off', | \(1) |
168| | instead of the default 'on' | |
169+----------+---------------------------------+-------+
170| ``=`` | replace the flags, instead of | \(1) |
171| | the default 'OR' operation | |
172+----------+---------------------------------+-------+
173| ``?`` | return a string in which the | \(2) |
174| | characters represent the flags | |
175| | that are set. | |
176+----------+---------------------------------+-------+
177
178Notes:
179
180(1)
181 The ``!`` and ``=`` modifiers are mutually exclusive.
182
183(2)
184 This string represents the flags after they may have been altered by the same
185 call.
186
187Examples::
188
189 import posixfile
190
191 file = posixfile.open('/tmp/test', 'w')
192 file.lock('w|')
193 ...
194 file.lock('u')
195 file.close()
196