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