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