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