Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{posix} --- |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 2 | The most common \POSIX{} system calls} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{posix} |
Fred Drake | a54a887 | 1999-03-02 17:03:42 +0000 | [diff] [blame] | 5 | \platform{Unix} |
Fred Drake | c116b82 | 2001-05-09 15:50:17 +0000 | [diff] [blame] | 6 | \modulesynopsis{The most common \POSIX\ system calls (normally used |
Fred Drake | b216060 | 1999-07-01 13:53:32 +0000 | [diff] [blame] | 7 | via module \refmodule{os}).} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 | |
| 10 | This module provides access to operating system functionality that is |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 11 | standardized by the C Standard and the \POSIX{} standard (a thinly |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 12 | disguised \UNIX{} interface). |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 13 | |
| 14 | \strong{Do not import this module directly.} Instead, import the |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 15 | module \refmodule{os}, which provides a \emph{portable} version of this |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 16 | interface. On \UNIX, the \refmodule{os} module provides a superset of |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 17 | the \module{posix} interface. On non-\UNIX{} operating systems the |
| 18 | \module{posix} module is not available, but a subset is always |
Fred Drake | 2799f9d | 1999-04-21 18:33:47 +0000 | [diff] [blame] | 19 | available through the \refmodule{os} interface. Once \refmodule{os} is |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 20 | imported, there is \emph{no} performance penalty in using it instead |
Fred Drake | 2799f9d | 1999-04-21 18:33:47 +0000 | [diff] [blame] | 21 | of \module{posix}. In addition, \refmodule{os}\refstmodindex{os} |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 22 | provides some additional functionality, such as automatically calling |
| 23 | \function{putenv()} when an entry in \code{os.environ} is changed. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 282290f | 1997-08-27 14:54:25 +0000 | [diff] [blame] | 25 | The descriptions below are very terse; refer to the corresponding |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 26 | \UNIX{} manual (or \POSIX{} documentation) entry for more information. |
Guido van Rossum | 282290f | 1997-08-27 14:54:25 +0000 | [diff] [blame] | 27 | Arguments called \var{path} refer to a pathname given as a string. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | |
Barry Warsaw | eef2cd1 | 1998-07-23 19:50:09 +0000 | [diff] [blame] | 29 | Errors are reported as exceptions; the usual exceptions are given for |
| 30 | type errors, while errors reported by the system calls raise |
| 31 | \exception{error} (a synonym for the standard exception |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 32 | \exception{OSError}), described below. |
| 33 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 34 | |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 35 | \subsection{Large File Support \label{posix-large-files}} |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 36 | \sectionauthor{Steve Clift}{clift@mail.anacapa.net} |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 37 | \index{large files} |
| 38 | \index{file!large files} |
| 39 | |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 40 | |
| 41 | Several operating systems (including AIX, HPUX, Irix and Solaris) |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 42 | provide support for files that are larger than 2 Gb from a C |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 43 | programming model where \ctype{int} and \ctype{long} are 32-bit |
| 44 | values. This is typically accomplished by defining the relevant size |
| 45 | and offset types as 64-bit values. Such files are sometimes referred |
| 46 | to as \dfn{large files}. |
| 47 | |
| 48 | Large file support is enabled in Python when the size of an |
| 49 | \ctype{off_t} is larger than a \ctype{long} and the \ctype{long long} |
| 50 | type is available and is at least as large as an \ctype{off_t}. Python |
| 51 | longs are then used to represent file sizes, offsets and other values |
| 52 | that can exceed the range of a Python int. It may be necessary to |
| 53 | configure and compile Python with certain compiler flags to enable |
| 54 | this mode. For example, it is enabled by default with recent versions |
| 55 | of Irix, but with Solaris 2.6 and 2.7 you need to do something like: |
| 56 | |
| 57 | \begin{verbatim} |
Fred Drake | 82f355a | 1999-05-26 13:03:34 +0000 | [diff] [blame] | 58 | CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \ |
Guido van Rossum | f89ad5e | 2001-03-01 18:29:57 +0000 | [diff] [blame] | 59 | ./configure |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 60 | \end{verbatim} % $ <-- bow to font-lock |
| 61 | |
Guido van Rossum | f89ad5e | 2001-03-01 18:29:57 +0000 | [diff] [blame] | 62 | On large-file-capable Linux systems, this might work: |
| 63 | |
| 64 | \begin{verbatim} |
Fred Drake | 93438bf | 2001-06-22 16:01:20 +0000 | [diff] [blame] | 65 | CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \ |
| 66 | ./configure |
| 67 | \end{verbatim} % $ <-- bow to font-lock |
Guido van Rossum | f89ad5e | 2001-03-01 18:29:57 +0000 | [diff] [blame] | 68 | |
Fred Drake | bb3b002 | 1999-01-11 18:36:23 +0000 | [diff] [blame] | 69 | |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 70 | \subsection{Module Contents \label{posix-contents}} |
| 71 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 72 | |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 73 | Module \module{posix} defines the following data item: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | \begin{datadesc}{environ} |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 76 | A dictionary representing the string environment at the time the |
| 77 | interpreter was started. For example, \code{environ['HOME']} is the |
Fred Drake | 2799f9d | 1999-04-21 18:33:47 +0000 | [diff] [blame] | 78 | pathname of your home directory, equivalent to |
| 79 | \code{getenv("HOME")} in C. |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 81 | Modifying this dictionary does not affect the string environment |
Fred Drake | 75aae9a | 1998-03-11 05:29:58 +0000 | [diff] [blame] | 82 | passed on by \function{execv()}, \function{popen()} or |
| 83 | \function{system()}; if you need to change the environment, pass |
| 84 | \code{environ} to \function{execve()} or add variable assignments and |
| 85 | export statements to the command string for \function{system()} or |
| 86 | \function{popen()}. |
Guido van Rossum | 9c43c59 | 1997-08-08 21:05:09 +0000 | [diff] [blame] | 87 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 88 | \note{The \refmodule{os} module provides an alternate |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 89 | implementation of \code{environ} which updates the environment on |
| 90 | modification. Note also that updating \code{os.environ} will render |
Fred Drake | 0559d95 | 2001-10-24 21:10:52 +0000 | [diff] [blame] | 91 | this dictionary obsolete. Use of the \refmodule{os} module version of |
| 92 | this is recommended over direct access to the \module{posix} module.} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 93 | \end{datadesc} |
| 94 | |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 95 | Additional contents of this module should only be accessed via the |
Fred Drake | 215fe2f | 1999-02-02 19:02:35 +0000 | [diff] [blame] | 96 | \refmodule{os} module; refer to the documentation for that module for |
Fred Drake | 68a8c69 | 1999-02-01 20:23:02 +0000 | [diff] [blame] | 97 | further information. |