Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* File object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 35 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 685a38e | 1996-12-05 21:54:17 +0000 | [diff] [blame] | 37 | #ifdef HAVE_UNISTD_H |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 | |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 41 | #ifdef MS_WIN32 |
| 42 | #define ftruncate _chsize |
| 43 | #define fileno _fileno |
| 44 | #define HAVE_FTRUNCATE |
| 45 | #endif |
| 46 | |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 47 | #ifdef THINK_C |
| 48 | #define HAVE_FOPENRF |
| 49 | #endif |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 50 | #ifdef __MWERKS__ |
| 51 | /* Mwerks fopen() doesn't always set errno */ |
| 52 | #define NO_FOPEN_ERRNO |
| 53 | #endif |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 57 | #include <errno.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | |
| 59 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | PyObject_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | FILE *f_fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyObject *f_name; |
| 63 | PyObject *f_mode; |
| 64 | int (*f_close) Py_PROTO((FILE *)); |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 65 | int f_softspace; /* Flag used by 'print' command */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 66 | } PyFileObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | |
| 68 | FILE * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | PyFile_AsFile(f) |
| 70 | PyObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 72 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | return NULL; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 74 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | return ((PyFileObject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | PyObject * |
| 79 | PyFile_Name(f) |
| 80 | PyObject *f; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 83 | return NULL; |
| 84 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | return ((PyFileObject *)f)->f_name; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | PyObject * |
| 89 | PyFile_FromFile(fp, name, mode, close) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | FILE *fp; |
| 91 | char *name; |
| 92 | char *mode; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | int (*close) Py_FPROTO((FILE *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | if (f == NULL) |
| 97 | return NULL; |
| 98 | f->f_fp = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | f->f_name = PyString_FromString(name); |
| 100 | f->f_mode = PyString_FromString(mode); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 101 | f->f_close = close; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 102 | f->f_softspace = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | if (f->f_name == NULL || f->f_mode == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | Py_DECREF(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | f->f_fp = fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | return (PyObject *) f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 111 | PyObject * |
| 112 | PyFile_FromString(name, mode) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | char *name, *mode; |
| 114 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | extern int fclose Py_PROTO((FILE *)); |
| 116 | PyFileObject *f; |
| 117 | f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | if (f == NULL) |
| 119 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 120 | #ifdef HAVE_FOPENRF |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 121 | if (*mode == '*') { |
| 122 | FILE *fopenRF(); |
| 123 | f->f_fp = fopenRF(name, mode+1); |
| 124 | } |
| 125 | else |
| 126 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 127 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 128 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 129 | f->f_fp = fopen(name, mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 130 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 131 | } |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 132 | if (f->f_fp == NULL) { |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 133 | #ifdef NO_FOPEN_ERRNO |
| 134 | if ( errno == 0 ) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 135 | PyErr_SetString(PyExc_IOError, "Cannot open file"); |
| 136 | Py_DECREF(f); |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | PyErr_SetFromErrno(PyExc_IOError); |
| 141 | Py_DECREF(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 144 | return (PyObject *)f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 147 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyFile_SetBufSize(f, bufsize) |
| 149 | PyObject *f; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 150 | int bufsize; |
| 151 | { |
| 152 | if (bufsize >= 0) { |
| 153 | #ifdef HAVE_SETVBUF |
| 154 | int type; |
| 155 | switch (bufsize) { |
| 156 | case 0: |
| 157 | type = _IONBF; |
| 158 | break; |
| 159 | case 1: |
| 160 | type = _IOLBF; |
| 161 | bufsize = BUFSIZ; |
| 162 | break; |
| 163 | default: |
| 164 | type = _IOFBF; |
| 165 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 166 | setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
| 167 | type, bufsize); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | #endif /* HAVE_SETVBUF */ |
| 169 | } |
| 170 | } |
| 171 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | static PyObject * |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 173 | err_closed() |
| 174 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | /* Methods */ |
| 180 | |
| 181 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 182 | file_dealloc(f) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 183 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 185 | if (f->f_fp != NULL && f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 187 | (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | if (f->f_name != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | Py_DECREF(f->f_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | if (f->f_mode != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 193 | Py_DECREF(f->f_mode); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | free((char *)f); |
| 195 | } |
| 196 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 197 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 198 | file_repr(f) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 199 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 200 | { |
| 201 | char buf[300]; |
Guido van Rossum | e35399e | 1993-01-10 18:33:56 +0000 | [diff] [blame] | 202 | sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | f->f_fp == NULL ? "closed" : "open", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyString_AsString(f->f_name), |
| 205 | PyString_AsString(f->f_mode), |
Guido van Rossum | e35399e | 1993-01-10 18:33:56 +0000 | [diff] [blame] | 206 | (long)f); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 207 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 211 | file_close(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | PyFileObject *f; |
| 213 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 215 | int sts = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | if (f->f_fp != NULL) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 219 | if (f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 220 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 221 | errno = 0; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 222 | sts = (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 224 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | f->f_fp = NULL; |
| 226 | } |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 227 | if (sts == EOF) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 229 | if (sts != 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | return PyInt_FromLong((long)sts); |
| 231 | Py_INCREF(Py_None); |
| 232 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 235 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 236 | file_seek(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 237 | PyFileObject *f; |
| 238 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 240 | long offset; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 241 | int whence; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 242 | int ret; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 244 | if (f->f_fp == NULL) |
| 245 | return err_closed(); |
| 246 | whence = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | if (!PyArg_Parse(args, "l", &offset)) { |
| 248 | PyErr_Clear(); |
| 249 | if (!PyArg_Parse(args, "(li)", &offset, &whence)) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 252 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 253 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 254 | ret = fseek(f->f_fp, offset, whence); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 255 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 256 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 257 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 258 | clearerr(f->f_fp); |
| 259 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 260 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | Py_INCREF(Py_None); |
| 262 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 265 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 266 | static PyObject * |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 267 | file_truncate(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 268 | PyFileObject *f; |
| 269 | PyObject *args; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 270 | { |
| 271 | long newsize; |
| 272 | int ret; |
| 273 | |
| 274 | if (f->f_fp == NULL) |
| 275 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | if (!PyArg_Parse(args, "l", &newsize)) { |
| 277 | PyErr_Clear(); |
| 278 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 279 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 280 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 281 | errno = 0; |
| 282 | newsize = ftell(f->f_fp); /* default to current position*/ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | Py_END_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 284 | if (newsize == -1L) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 285 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 286 | clearerr(f->f_fp); |
| 287 | return NULL; |
| 288 | } |
| 289 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 291 | errno = 0; |
| 292 | ret = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 293 | Py_END_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 294 | if (ret == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 295 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 296 | errno = 0; |
| 297 | ret = ftruncate(fileno(f->f_fp), newsize); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 298 | Py_END_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 299 | } |
| 300 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 301 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 302 | clearerr(f->f_fp); |
| 303 | return NULL; |
| 304 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | Py_INCREF(Py_None); |
| 306 | return Py_None; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 307 | } |
| 308 | #endif /* HAVE_FTRUNCATE */ |
| 309 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 311 | file_tell(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 312 | PyFileObject *f; |
| 313 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 314 | { |
| 315 | long offset; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 316 | if (f->f_fp == NULL) |
| 317 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 318 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 319 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 321 | errno = 0; |
| 322 | offset = ftell(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | Py_END_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 324 | if (offset == -1L) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 325 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 326 | clearerr(f->f_fp); |
| 327 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 328 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 329 | return PyInt_FromLong(offset); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | static PyObject * |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 333 | file_fileno(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | PyFileObject *f; |
| 335 | PyObject *args; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 336 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 337 | if (f->f_fp == NULL) |
| 338 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 340 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | return PyInt_FromLong((long) fileno(f->f_fp)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 345 | file_flush(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | PyFileObject *f; |
| 347 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 348 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 349 | int res; |
| 350 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 351 | if (f->f_fp == NULL) |
| 352 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 353 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 356 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 357 | res = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 359 | if (res != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 360 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 361 | clearerr(f->f_fp); |
| 362 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 363 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | Py_INCREF(Py_None); |
| 365 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 368 | static PyObject * |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 369 | file_isatty(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 370 | PyFileObject *f; |
| 371 | PyObject *args; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 372 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 373 | long res; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 374 | if (f->f_fp == NULL) |
| 375 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 376 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 377 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 379 | res = isatty((int)fileno(f->f_fp)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 380 | Py_END_ALLOW_THREADS |
| 381 | return PyInt_FromLong(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 384 | /* We expect that fstat exists on most systems. |
| 385 | It's confirmed on Unix, Mac and Windows. |
| 386 | If you don't have it, add #define DONT_HAVE_FSTAT to your config.h. */ |
| 387 | #ifndef DONT_HAVE_FSTAT |
| 388 | #define HAVE_FSTAT |
| 389 | |
| 390 | #include <sys/types.h> |
| 391 | #include <sys/stat.h> |
| 392 | |
| 393 | #endif |
| 394 | |
| 395 | #if BUFSIZ < 8192 |
| 396 | #define SMALLCHUNK 8192 |
| 397 | #else |
| 398 | #define SMALLCHUNK BUFSIZ |
| 399 | #endif |
| 400 | |
| 401 | #define BIGCHUNK (512*1024) |
| 402 | |
| 403 | static size_t |
| 404 | new_buffersize(f, currentsize) |
| 405 | PyFileObject *f; |
| 406 | size_t currentsize; |
| 407 | { |
| 408 | #ifdef HAVE_FSTAT |
Guido van Rossum | 36f8e2d | 1997-08-21 02:31:25 +0000 | [diff] [blame] | 409 | #ifndef SEEK_CUR |
| 410 | #define SEEK_CUR 1 |
| 411 | #endif |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 412 | long pos, end; |
| 413 | struct stat st; |
| 414 | if (fstat(fileno(f->f_fp), &st) == 0) { |
| 415 | end = st.st_size; |
Guido van Rossum | 36f8e2d | 1997-08-21 02:31:25 +0000 | [diff] [blame] | 416 | pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 417 | if (end > pos && pos >= 0) |
Guido van Rossum | 36f8e2d | 1997-08-21 02:31:25 +0000 | [diff] [blame] | 418 | return end - pos + BUFSIZ; |
| 419 | /* Add BUFSIZ to allow for stdio buffered data */ |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | if (currentsize > SMALLCHUNK) { |
| 423 | /* Keep doubling until we reach BIGCHUNK; |
| 424 | then keep adding BIGCHUNK. */ |
| 425 | if (currentsize <= BIGCHUNK) |
| 426 | return currentsize + currentsize; |
| 427 | else |
| 428 | return currentsize + BIGCHUNK; |
| 429 | } |
| 430 | return currentsize + SMALLCHUNK; |
| 431 | } |
| 432 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 434 | file_read(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 435 | PyFileObject *f; |
| 436 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 437 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 438 | long bytesrequested = -1; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 439 | size_t bytesread, buffersize, chunksize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | PyObject *v; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 442 | if (f->f_fp == NULL) |
| 443 | return err_closed(); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 444 | if (!PyArg_ParseTuple(args, "|l", &bytesrequested)) |
| 445 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 446 | if (bytesrequested < 0) |
| 447 | buffersize = new_buffersize(f, 0); |
| 448 | else |
| 449 | buffersize = bytesrequested; |
| 450 | v = PyString_FromStringAndSize((char *)NULL, buffersize); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 451 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 452 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 453 | bytesread = 0; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 454 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 455 | Py_BEGIN_ALLOW_THREADS |
| 456 | errno = 0; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 457 | chunksize = fread(BUF(v) + bytesread, 1, |
| 458 | buffersize - bytesread, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 459 | Py_END_ALLOW_THREADS |
| 460 | if (chunksize == 0) { |
| 461 | if (!ferror(f->f_fp)) |
| 462 | break; |
| 463 | PyErr_SetFromErrno(PyExc_IOError); |
| 464 | clearerr(f->f_fp); |
| 465 | Py_DECREF(v); |
| 466 | return NULL; |
| 467 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 468 | bytesread += chunksize; |
| 469 | if (bytesread < buffersize) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 470 | break; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 471 | if (bytesrequested < 0) { |
| 472 | buffersize = new_buffersize(f, buffersize); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 473 | if (_PyString_Resize(&v, buffersize) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 474 | return NULL; |
| 475 | } |
| 476 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 477 | if (bytesread != buffersize) |
| 478 | _PyString_Resize(&v, bytesread); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 479 | return v; |
| 480 | } |
| 481 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 482 | static PyObject * |
| 483 | file_readinto(f, args) |
| 484 | PyFileObject *f; |
| 485 | PyObject *args; |
| 486 | { |
| 487 | char *ptr; |
| 488 | int ntodo, ndone, nnow; |
| 489 | |
| 490 | if (f->f_fp == NULL) |
| 491 | return err_closed(); |
| 492 | if (!PyArg_Parse(args, "w#", &ptr, &ntodo)) |
| 493 | return NULL; |
| 494 | ndone = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 495 | while (ntodo > 0) { |
| 496 | Py_BEGIN_ALLOW_THREADS |
| 497 | errno = 0; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 498 | nnow = fread(ptr+ndone, 1, ntodo, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 499 | Py_END_ALLOW_THREADS |
| 500 | if (nnow == 0) { |
| 501 | if (!ferror(f->f_fp)) |
| 502 | break; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 503 | PyErr_SetFromErrno(PyExc_IOError); |
| 504 | clearerr(f->f_fp); |
| 505 | return NULL; |
| 506 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 507 | ndone += nnow; |
| 508 | ntodo -= nnow; |
| 509 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 510 | return PyInt_FromLong(ndone); |
| 511 | } |
| 512 | |
| 513 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 514 | /* Internal routine to get a line. |
| 515 | Size argument interpretation: |
| 516 | > 0: max length; |
| 517 | = 0: read arbitrary line; |
| 518 | < 0: strip trailing '\n', raise EOFError if EOF reached immediately |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 519 | */ |
| 520 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 521 | static PyObject * |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 522 | getline(f, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | PyFileObject *f; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 524 | int n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 526 | register FILE *fp; |
| 527 | register int c; |
| 528 | register char *buf, *end; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 529 | int n1, n2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 530 | PyObject *v; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | c10aa77 | 1992-07-31 12:42:38 +0000 | [diff] [blame] | 532 | fp = f->f_fp; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 533 | n2 = n > 0 ? n : 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 534 | v = PyString_FromStringAndSize((char *)NULL, n2); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 535 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 536 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 537 | buf = BUF(v); |
| 538 | end = buf + n2; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 541 | for (;;) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 542 | if ((c = getc(fp)) == EOF) { |
Guido van Rossum | 76ad8ed | 1991-06-03 10:54:55 +0000 | [diff] [blame] | 543 | clearerr(fp); |
Guido van Rossum | f518154 | 1997-11-07 19:20:34 +0000 | [diff] [blame] | 544 | Py_BLOCK_THREADS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | if (PyErr_CheckSignals()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 546 | Py_DECREF(v); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
| 549 | if (n < 0 && buf == BUF(v)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 550 | Py_DECREF(v); |
| 551 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 201be05 | 1991-12-24 13:26:41 +0000 | [diff] [blame] | 552 | "EOF when reading a line"); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
Guido van Rossum | f518154 | 1997-11-07 19:20:34 +0000 | [diff] [blame] | 555 | Py_UNBLOCK_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 556 | break; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 557 | } |
| 558 | if ((*buf++ = c) == '\n') { |
| 559 | if (n < 0) |
| 560 | buf--; |
| 561 | break; |
| 562 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 563 | if (buf == end) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 564 | if (n > 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 565 | break; |
| 566 | n1 = n2; |
| 567 | n2 += 1000; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | Py_BLOCK_THREADS |
| 569 | if (_PyString_Resize(&v, n2) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 570 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 571 | Py_UNBLOCK_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 572 | buf = BUF(v) + n1; |
| 573 | end = BUF(v) + n2; |
| 574 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 578 | n1 = buf - BUF(v); |
| 579 | if (n1 != n2) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 580 | _PyString_Resize(&v, n1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 581 | return v; |
| 582 | } |
| 583 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 584 | /* External C interface */ |
| 585 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | PyObject * |
| 587 | PyFile_GetLine(f, n) |
| 588 | PyObject *f; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 589 | int n; |
| 590 | { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 591 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | PyErr_BadInternalCall(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 593 | return NULL; |
| 594 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 595 | if (!PyFile_Check(f)) { |
| 596 | PyObject *reader; |
| 597 | PyObject *args; |
| 598 | PyObject *result; |
| 599 | reader = PyObject_GetAttrString(f, "readline"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 600 | if (reader == NULL) |
| 601 | return NULL; |
| 602 | if (n <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | args = Py_BuildValue("()"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 604 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | args = Py_BuildValue("(i)", n); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 606 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 607 | Py_DECREF(reader); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | result = PyEval_CallObject(reader, args); |
| 611 | Py_DECREF(reader); |
| 612 | Py_DECREF(args); |
| 613 | if (result != NULL && !PyString_Check(result)) { |
| 614 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 615 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 616 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 617 | "object.readline() returned non-string"); |
| 618 | } |
| 619 | if (n < 0 && result != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | char *s = PyString_AsString(result); |
| 621 | int len = PyString_Size(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 622 | if (len == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 623 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 624 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 626 | "EOF when reading a line"); |
| 627 | } |
| 628 | else if (s[len-1] == '\n') { |
| 629 | if (result->ob_refcnt == 1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 630 | _PyString_Resize(&result, len-1); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 631 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 632 | PyObject *v; |
| 633 | v = PyString_FromStringAndSize(s, |
| 634 | len-1); |
| 635 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 636 | result = v; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | return result; |
| 641 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | if (((PyFileObject*)f)->f_fp == NULL) |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 643 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 644 | return getline((PyFileObject *)f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* Python method */ |
| 648 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 649 | static PyObject * |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 650 | file_readline(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 651 | PyFileObject *f; |
| 652 | PyObject *args; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 653 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 654 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 656 | if (f->f_fp == NULL) |
| 657 | return err_closed(); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 658 | if (!PyArg_ParseTuple(args, "|i", &n)) |
| 659 | return NULL; |
| 660 | if (n == 0) |
| 661 | return PyString_FromString(""); |
| 662 | if (n < 0) |
| 663 | n = 0; |
Guido van Rossum | 51415a7 | 1992-03-27 17:23:38 +0000 | [diff] [blame] | 664 | return getline(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 667 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 668 | file_readlines(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 669 | PyFileObject *f; |
| 670 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 671 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 672 | long sizehint = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 673 | PyObject *list; |
| 674 | PyObject *line; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 675 | char small_buffer[SMALLCHUNK]; |
| 676 | char *buffer = small_buffer; |
| 677 | size_t buffersize = SMALLCHUNK; |
| 678 | PyObject *big_buffer = NULL; |
| 679 | size_t nfilled = 0; |
| 680 | size_t nread; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 681 | size_t totalread = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 682 | char *p, *q, *end; |
| 683 | int err; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 685 | if (f->f_fp == NULL) |
| 686 | return err_closed(); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 687 | if (!PyArg_ParseTuple(args, "|l", &sizehint)) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 688 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 689 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 690 | return NULL; |
| 691 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 692 | Py_BEGIN_ALLOW_THREADS |
| 693 | errno = 0; |
| 694 | nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp); |
| 695 | Py_END_ALLOW_THREADS |
| 696 | if (nread == 0) { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 697 | sizehint = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 698 | if (nread == 0) |
| 699 | break; |
| 700 | PyErr_SetFromErrno(PyExc_IOError); |
| 701 | clearerr(f->f_fp); |
| 702 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 703 | Py_DECREF(list); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 704 | list = NULL; |
| 705 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 706 | } |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 707 | totalread += nread; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 708 | p = memchr(buffer+nfilled, '\n', nread); |
| 709 | if (p == NULL) { |
| 710 | /* Need a larger buffer to fit this line */ |
| 711 | nfilled += nread; |
| 712 | buffersize *= 2; |
| 713 | if (big_buffer == NULL) { |
| 714 | /* Create the big buffer */ |
| 715 | big_buffer = PyString_FromStringAndSize( |
| 716 | NULL, buffersize); |
| 717 | if (big_buffer == NULL) |
| 718 | goto error; |
| 719 | buffer = PyString_AS_STRING(big_buffer); |
| 720 | memcpy(buffer, small_buffer, nfilled); |
| 721 | } |
| 722 | else { |
| 723 | /* Grow the big buffer */ |
| 724 | _PyString_Resize(&big_buffer, buffersize); |
| 725 | buffer = PyString_AS_STRING(big_buffer); |
| 726 | } |
| 727 | continue; |
| 728 | } |
| 729 | end = buffer+nfilled+nread; |
| 730 | q = buffer; |
| 731 | do { |
| 732 | /* Process complete lines */ |
| 733 | p++; |
| 734 | line = PyString_FromStringAndSize(q, p-q); |
| 735 | if (line == NULL) |
| 736 | goto error; |
| 737 | err = PyList_Append(list, line); |
| 738 | Py_DECREF(line); |
| 739 | if (err != 0) |
| 740 | goto error; |
| 741 | q = p; |
| 742 | p = memchr(q, '\n', end-q); |
| 743 | } while (p != NULL); |
| 744 | /* Move the remaining incomplete line to the start */ |
| 745 | nfilled = end-q; |
| 746 | memmove(buffer, q, nfilled); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 747 | if (sizehint > 0) |
| 748 | if (totalread >= (size_t)sizehint) |
| 749 | break; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 750 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 751 | if (nfilled != 0) { |
| 752 | /* Partial last line */ |
| 753 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 754 | if (line == NULL) |
| 755 | goto error; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 756 | if (sizehint > 0) { |
| 757 | /* Need to complete the last line */ |
| 758 | PyObject *rest = getline(f, 0); |
| 759 | if (rest == NULL) { |
| 760 | Py_DECREF(line); |
| 761 | goto error; |
| 762 | } |
| 763 | PyString_Concat(&line, rest); |
| 764 | Py_DECREF(rest); |
| 765 | if (line == NULL) |
| 766 | goto error; |
| 767 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 768 | err = PyList_Append(list, line); |
| 769 | Py_DECREF(line); |
| 770 | if (err != 0) |
| 771 | goto error; |
| 772 | } |
| 773 | cleanup: |
| 774 | if (big_buffer) |
| 775 | Py_DECREF(big_buffer); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 776 | return list; |
| 777 | } |
| 778 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 779 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 780 | file_write(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 781 | PyFileObject *f; |
| 782 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 783 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 784 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 785 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 786 | if (f->f_fp == NULL) |
| 787 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 788 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 790 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 791 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 793 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 794 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 795 | if (n2 != n) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 796 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 797 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 800 | Py_INCREF(Py_None); |
| 801 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 804 | static PyObject * |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 805 | file_writelines(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 806 | PyFileObject *f; |
| 807 | PyObject *args; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 808 | { |
| 809 | int i, n; |
| 810 | if (f->f_fp == NULL) |
| 811 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 812 | if (args == NULL || !PyList_Check(args)) { |
| 813 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 814 | "writelines() requires list of strings"); |
| 815 | return NULL; |
| 816 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 817 | n = PyList_Size(args); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 818 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 819 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 820 | errno = 0; |
| 821 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 822 | PyObject *line = PyList_GetItem(args, i); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 823 | int len; |
| 824 | int nwritten; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 825 | if (!PyString_Check(line)) { |
| 826 | Py_BLOCK_THREADS |
| 827 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 828 | "writelines() requires list of strings"); |
| 829 | return NULL; |
| 830 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 831 | len = PyString_Size(line); |
| 832 | nwritten = fwrite(PyString_AsString(line), 1, len, f->f_fp); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 833 | if (nwritten != len) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 834 | Py_BLOCK_THREADS |
| 835 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 836 | clearerr(f->f_fp); |
| 837 | return NULL; |
| 838 | } |
| 839 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 840 | Py_END_ALLOW_THREADS |
| 841 | Py_INCREF(Py_None); |
| 842 | return Py_None; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 845 | static PyMethodDef file_methods[] = { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 846 | {"readline", (PyCFunction)file_readline, 1}, |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 847 | {"read", (PyCFunction)file_read, 1}, |
| 848 | {"write", (PyCFunction)file_write, 0}, |
| 849 | {"fileno", (PyCFunction)file_fileno, 0}, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 850 | {"seek", (PyCFunction)file_seek, 0}, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 851 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 852 | {"truncate", (PyCFunction)file_truncate, 0}, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 853 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 854 | {"tell", (PyCFunction)file_tell, 0}, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 855 | {"readinto", (PyCFunction)file_readinto, 0}, |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 856 | {"readlines", (PyCFunction)file_readlines, 1}, |
| 857 | {"writelines", (PyCFunction)file_writelines, 0}, |
| 858 | {"flush", (PyCFunction)file_flush, 0}, |
| 859 | {"close", (PyCFunction)file_close, 0}, |
| 860 | {"isatty", (PyCFunction)file_isatty, 0}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 861 | {NULL, NULL} /* sentinel */ |
| 862 | }; |
| 863 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 864 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 865 | |
| 866 | static struct memberlist file_memberlist[] = { |
| 867 | {"softspace", T_INT, OFF(f_softspace)}, |
| 868 | {"mode", T_OBJECT, OFF(f_mode), RO}, |
| 869 | {"name", T_OBJECT, OFF(f_name), RO}, |
| 870 | /* getattr(f, "closed") is implemented without this table */ |
| 871 | {"closed", T_INT, 0, RO}, |
| 872 | {NULL} /* Sentinel */ |
| 873 | }; |
| 874 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 875 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 876 | file_getattr(f, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 877 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 878 | char *name; |
| 879 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 880 | PyObject *res; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 881 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | res = Py_FindMethod(file_methods, (PyObject *)f, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 883 | if (res != NULL) |
| 884 | return res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | PyErr_Clear(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 886 | if (strcmp(name, "closed") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 887 | return PyInt_FromLong((long)(f->f_fp == 0)); |
| 888 | return PyMember_Get((char *)f, file_memberlist, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static int |
| 892 | file_setattr(f, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 893 | PyFileObject *f; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 894 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 895 | PyObject *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 896 | { |
| 897 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 898 | PyErr_SetString(PyExc_AttributeError, |
| 899 | "can't delete file attributes"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 900 | return -1; |
| 901 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 902 | return PyMember_Set((char *)f, file_memberlist, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 905 | PyTypeObject PyFile_Type = { |
| 906 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 907 | 0, |
| 908 | "file", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 909 | sizeof(PyFileObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 910 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 911 | (destructor)file_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 912 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 913 | (getattrfunc)file_getattr, /*tp_getattr*/ |
| 914 | (setattrfunc)file_setattr, /*tp_setattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 915 | 0, /*tp_compare*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 916 | (reprfunc)file_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 917 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 918 | |
| 919 | /* Interface for the 'soft space' between print items. */ |
| 920 | |
| 921 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 922 | PyFile_SoftSpace(f, newflag) |
| 923 | PyObject *f; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 924 | int newflag; |
| 925 | { |
| 926 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 927 | if (f == NULL) { |
| 928 | /* Do nothing */ |
| 929 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | else if (PyFile_Check(f)) { |
| 931 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 932 | ((PyFileObject *)f)->f_softspace = newflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 933 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 934 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 935 | PyObject *v; |
| 936 | v = PyObject_GetAttrString(f, "softspace"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 937 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 938 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 939 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 940 | if (PyInt_Check(v)) |
| 941 | oldflag = PyInt_AsLong(v); |
| 942 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 943 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 944 | v = PyInt_FromLong((long)newflag); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 945 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 946 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 947 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 948 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 949 | PyErr_Clear(); |
| 950 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 953 | return oldflag; |
| 954 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 955 | |
| 956 | /* Interfaces to write objects/strings to file-like objects */ |
| 957 | |
| 958 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 959 | PyFile_WriteObject(v, f, flags) |
| 960 | PyObject *v; |
| 961 | PyObject *f; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 962 | int flags; |
| 963 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 964 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 965 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 966 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 967 | return -1; |
| 968 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 969 | else if (PyFile_Check(f)) { |
| 970 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 971 | if (fp == NULL) { |
| 972 | err_closed(); |
| 973 | return -1; |
| 974 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 975 | return PyObject_Print(v, fp, flags); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 976 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 978 | if (writer == NULL) |
| 979 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 980 | if (flags & Py_PRINT_RAW) |
| 981 | value = PyObject_Str(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 982 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 984 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 986 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 987 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 988 | args = Py_BuildValue("(O)", value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 989 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 990 | Py_DECREF(value); |
| 991 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 992 | return -1; |
| 993 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | result = PyEval_CallObject(writer, args); |
| 995 | Py_DECREF(args); |
| 996 | Py_DECREF(value); |
| 997 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 998 | if (result == NULL) |
| 999 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1000 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |
| 1003 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1004 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1005 | PyFile_WriteString(s, f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1006 | char *s; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1007 | PyObject *f; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1008 | { |
| 1009 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1010 | /* Should be caused by a pre-existing error */ |
| 1011 | if(!PyErr_Occurred()) |
| 1012 | PyErr_SetString(PyExc_SystemError, |
| 1013 | "null file for PyFile_WriteString"); |
| 1014 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1015 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1016 | else if (PyFile_Check(f)) { |
| 1017 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1018 | if (fp == NULL) { |
| 1019 | err_closed(); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | fputs(s, fp); |
| 1023 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1024 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1025 | else if (!PyErr_Occurred()) { |
| 1026 | PyObject *v = PyString_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1027 | int err; |
| 1028 | if (v == NULL) |
| 1029 | return -1; |
| 1030 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 1031 | Py_DECREF(v); |
| 1032 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1033 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 1034 | else |
| 1035 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1036 | } |