1 :mod:`posixfile` --- File-like objects with locking support
2 ===========================================================
6 :synopsis: A file-like object with support for locking.
8 .. moduleauthor:: Jaap Vermeulen
9 .. sectionauthor:: Jaap Vermeulen
12 .. index:: pair: POSIX; file object
15 The locking operation that this module provides is done better and more portably
16 by the :func:`fcntl.lockf` call.
18 .. index:: single: fcntl() (in module fcntl)
20 This module implements some additional functionality over the built-in file
21 objects. In particular, it implements file locking, control over the file
22 flags, and an easy interface to duplicate the file object. The module defines a
23 new file object, the posixfile object. It has all the standard file object
24 methods and adds the methods described below. This module only works for
25 certain flavors of Unix, since it uses :func:`fcntl.fcntl` for file locking.
27 To instantiate a posixfile object, use the :func:`posixfile.open` function. The
28 resulting object looks and feels roughly the same as a standard file object.
30 The :mod:`posixfile` module defines the following constants:
35 Offset is calculated from the start of the file.
40 Offset is calculated from the current position in the file.
45 Offset is calculated from the end of the file.
47 The :mod:`posixfile` module defines the following functions:
50 .. function:: open(filename[, mode[, bufsize]])
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.
57 .. function:: fileopen(fileobject)
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.
62 The posixfile object defines the following additional methods:
65 .. method:: posixfile.lock(fmt, [len[, start[, whence]]])
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.
77 .. method:: posixfile.flags([flags])
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
87 .. method:: posixfile.dup()
89 Duplicate the file object and the underlying file pointer and file descriptor.
90 The resulting object behaves as if it were newly opened.
93 .. method:: posixfile.dup2(fd)
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.
100 .. method:: posixfile.file()
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.
105 All methods raise :exc:`IOError` when the request fails.
107 Format characters for the :meth:`lock` method have the following meaning:
109 +--------+-----------------------------------------------+
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 |
118 +--------+-----------------------------------------------+
120 In addition the following modifiers can be added to the format:
122 +----------+--------------------------------+-------+
123 | Modifier | Meaning | Notes |
124 +==========+================================+=======+
125 | ``|`` | wait until the lock has been | |
127 +----------+--------------------------------+-------+
128 | ``?`` | return the first lock | \(1) |
129 | | conflicting with the requested | |
130 | | lock, or ``None`` if there is | |
132 +----------+--------------------------------+-------+
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.
141 Format characters for the :meth:`flags` method have the following meanings:
143 +--------+-----------------------------------------------+
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 +--------+-----------------------------------------------+
155 In addition the following modifiers can be added to the format:
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 +----------+---------------------------------+-------+
174 The ``!`` and ``=`` modifiers are mutually exclusive.
177 This string represents the flags after they may have been altered by the same
184 file = posixfile.open('/tmp/test', 'w')