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