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