Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 1 | % Manual text and implementation by Jaap Vermeulen |
| 2 | \section{Standard Module \sectcode{posixfile}} |
| 3 | \bimodindex{posixfile} |
| 4 | \indexii{posix}{file object} |
| 5 | |
| 6 | This module implements some additional functionality over the built-in |
| 7 | file objects. In particular, it implements file locking, control over |
| 8 | the file flags, and an easy interface to duplicate the file object. |
| 9 | The module defines a new file object, the posixfile object. It |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 10 | has all the standard file object methods and adds the methods |
| 11 | described below. This module only works for certain flavors of |
| 12 | \UNIX{}, since it uses \code{fcntl()} for file locking. |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 13 | |
| 14 | To instantiate a posixfile object, use the \code{open()} function in |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 15 | the posixfile module. The resulting object looks and feels roughly |
| 16 | the same as a standard file object. |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 17 | |
| 18 | The posixfile module defines the following constants: |
| 19 | |
| 20 | \renewcommand{\indexsubitem}{(in module posixfile)} |
| 21 | \begin{datadesc}{SEEK_SET} |
| 22 | offset is calculated from the start of the file |
| 23 | \end{datadesc} |
| 24 | |
| 25 | \begin{datadesc}{SEEK_CUR} |
| 26 | offset is calculated from the current position in the file |
| 27 | \end{datadesc} |
| 28 | |
| 29 | \begin{datadesc}{SEEK_END} |
| 30 | offset is calculated from the end of the file |
| 31 | \end{datadesc} |
| 32 | |
| 33 | The posixfile module defines the following functions: |
| 34 | |
| 35 | \renewcommand{\indexsubitem}{(in module posixfile)} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 36 | |
| 37 | \begin{funcdesc}{open}{filename\optional{\, mode\optional{\, bufsize}}} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 38 | Create a new posixfile object with the given filename and mode. The |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 39 | \var{filename}, \var{mode} and \var{bufsize} arguments are |
Guido van Rossum | f23e0fe | 1995-03-18 11:04:29 +0000 | [diff] [blame] | 40 | interpreted the same way as by the built-in \code{open()} function. |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 41 | \end{funcdesc} |
| 42 | |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 43 | \begin{funcdesc}{fileopen}{fileobject} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 44 | Create a new posixfile object with the given standard file object. |
| 45 | The resulting object has the same filename and mode as the original |
| 46 | file object. |
| 47 | \end{funcdesc} |
| 48 | |
| 49 | The posixfile object defines the following additional methods: |
| 50 | |
| 51 | \renewcommand{\indexsubitem}{(posixfile method)} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 52 | \begin{funcdesc}{lock}{fmt\, \optional{len\optional{\, start |
| 53 | \optional{\, whence}}}} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 54 | Lock the specified section of the file that the file object is |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 55 | referring to. The format is explained |
| 56 | below in a table. The \var{len} argument specifies the length of the |
| 57 | section that should be locked. The default is \code{0}. \var{start} |
| 58 | specifies the starting offset of the section, where the default is |
| 59 | \code{0}. The \var{whence} argument specifies where the offset is |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 60 | relative to. It accepts one of the constants \code{SEEK_SET}, |
| 61 | \code{SEEK_CUR} or \code{SEEK_END}. The default is \code{SEEK_SET}. |
| 62 | For more information about the arguments refer to the fcntl |
| 63 | manual page on your system. |
| 64 | \end{funcdesc} |
| 65 | |
| 66 | \begin{funcdesc}{flags}{fmt} |
| 67 | Set the specified flags for the file that the file object is referring |
| 68 | to. The new flags are ORed with the old flags, unless specified |
| 69 | otherwise. The format is explained below in a table. Without |
| 70 | arguments a string indicating the current flags is returned (this is |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 71 | the same as the '?' modifier). For more information about the flags |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 72 | refer to the fcntl manual page on your system. |
| 73 | \end{funcdesc} |
| 74 | |
| 75 | \begin{funcdesc}{dup}{} |
| 76 | Duplicate the file object and the underlying file pointer and file |
| 77 | descriptor. The resulting object behaves as if it were newly |
| 78 | opened. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{dup2}{fd} |
| 82 | Duplicate the file object and the underlying file pointer and file |
| 83 | descriptor. The new object will have the given file descriptor. |
| 84 | Otherwise the resulting object behaves as if it were newly opened. |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | \begin{funcdesc}{file}{} |
| 88 | Return the standard file object that the posixfile object is based |
| 89 | on. This is sometimes necessary for functions that insist on a |
| 90 | standard file object. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | All methods return \code{IOError} when the request fails. |
| 94 | |
| 95 | Format characters for the \code{lock()} method have the following meaning: |
| 96 | |
Guido van Rossum | b8b264b | 1994-08-12 13:13:50 +0000 | [diff] [blame] | 97 | \begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{} |
| 98 | \lineiii{u}{unlock the specified region}{} |
| 99 | \lineiii{r}{request a read lock for the specified section}{} |
| 100 | \lineiii{w}{request a write lock for the specified section}{} |
| 101 | \end{tableiii} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 102 | |
| 103 | In addition the following modifiers can be added to the format: |
| 104 | |
| 105 | \begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes} |
| 106 | \lineiii{|}{wait until the lock has been granted}{} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 107 | \lineiii{?}{return the first lock conflicting with the requested lock, or |
| 108 | \code{None} if there is no conflict.}{(1)} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 109 | \end{tableiii} |
| 110 | |
| 111 | Note: |
| 112 | |
| 113 | (1) The lock returned is in the format \code{(mode, len, start, |
| 114 | whence, pid)} where mode is a character representing the type of lock |
| 115 | ('r' or 'w'). This modifier prevents a request from being granted; it |
| 116 | is for query purposes only. |
| 117 | |
| 118 | Format character for the \code{flags()} method have the following meaning: |
| 119 | |
Guido van Rossum | b8b264b | 1994-08-12 13:13:50 +0000 | [diff] [blame] | 120 | \begin{tableiii}{|c|l|c|}{samp}{Format}{Meaning}{} |
| 121 | \lineiii{a}{append only flag}{} |
| 122 | \lineiii{c}{close on exec flag}{} |
| 123 | \lineiii{n}{no delay flag (also called non-blocking flag)}{} |
| 124 | \lineiii{s}{synchronization flag}{} |
| 125 | \end{tableiii} |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 126 | |
| 127 | In addition the following modifiers can be added to the format: |
| 128 | |
| 129 | \begin{tableiii}{|c|l|c|}{samp}{Modifier}{Meaning}{Notes} |
| 130 | \lineiii{!}{turn the specified flags 'off', instead of the default 'on'}{(1)} |
| 131 | \lineiii{=}{replace the flags, instead of the default 'OR' operation}{(1)} |
| 132 | \lineiii{?}{return a string in which the characters represent the flags that |
| 133 | are set.}{(2)} |
| 134 | \end{tableiii} |
| 135 | |
| 136 | Note: |
| 137 | |
| 138 | (1) The \code{!} and \code{=} modifiers are mutually exclusive. |
| 139 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 140 | (2) This string represents the flags after they may have been altered |
Guido van Rossum | 7f61b35 | 1994-05-19 09:09:50 +0000 | [diff] [blame] | 141 | by the same call. |
| 142 | |
| 143 | Examples: |
| 144 | |
| 145 | \bcode\begin{verbatim} |
| 146 | from posixfile import * |
| 147 | |
| 148 | file = open('/tmp/test', 'w') |
| 149 | file.lock('w|') |
| 150 | ... |
| 151 | file.lock('u') |
| 152 | file.close() |
| 153 | \end{verbatim}\ecode |