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 | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 37 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 39 | #endif /* DONT_HAVE_SYS_TYPES_H */ |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | eceebb8 | 2000-06-28 20:57:07 +0000 | [diff] [blame] | 41 | /* We expect that fstat exists on most systems. |
| 42 | It's confirmed on Unix, Mac and Windows. |
| 43 | If you don't have it, add #define DONT_HAVE_FSTAT to your config.h. */ |
| 44 | #ifndef DONT_HAVE_FSTAT |
| 45 | #define HAVE_FSTAT |
| 46 | |
| 47 | #ifndef DONT_HAVE_SYS_TYPES_H |
| 48 | #include <sys/types.h> |
| 49 | #endif |
| 50 | |
| 51 | #ifndef DONT_HAVE_SYS_STAT_H |
| 52 | #include <sys/stat.h> |
| 53 | #else |
| 54 | #ifdef HAVE_STAT_H |
| 55 | #include <stat.h> |
| 56 | #endif |
| 57 | #endif |
| 58 | |
| 59 | #endif /* DONT_HAVE_FSTAT */ |
| 60 | |
Guido van Rossum | 685a38e | 1996-12-05 21:54:17 +0000 | [diff] [blame] | 61 | #ifdef HAVE_UNISTD_H |
| 62 | #include <unistd.h> |
| 63 | #endif |
| 64 | |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 65 | #ifdef MS_WIN32 |
| 66 | #define ftruncate _chsize |
| 67 | #define fileno _fileno |
| 68 | #define HAVE_FTRUNCATE |
| 69 | #endif |
| 70 | |
Guido van Rossum | f2044e1 | 1998-04-28 16:05:59 +0000 | [diff] [blame] | 71 | #ifdef macintosh |
| 72 | #ifdef USE_GUSI |
| 73 | #define HAVE_FTRUNCATE |
| 74 | #endif |
| 75 | #endif |
| 76 | |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 77 | #ifdef __MWERKS__ |
| 78 | /* Mwerks fopen() doesn't always set errno */ |
| 79 | #define NO_FOPEN_ERRNO |
| 80 | #endif |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 84 | #ifndef DONT_HAVE_ERRNO_H |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 85 | #include <errno.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | |
| 88 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 89 | PyObject_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | FILE *f_fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | PyObject *f_name; |
| 92 | PyObject *f_mode; |
| 93 | int (*f_close) Py_PROTO((FILE *)); |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 94 | int f_softspace; /* Flag used by 'print' command */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 95 | int f_binary; /* Flag which indicates whether the file is open |
| 96 | open in binary (1) or test (0) mode */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | } PyFileObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | |
| 99 | FILE * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | PyFile_AsFile(f) |
| 101 | PyObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 103 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | return NULL; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 105 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | return ((PyFileObject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | PyObject * |
| 110 | PyFile_Name(f) |
| 111 | PyObject *f; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 116 | return ((PyFileObject *)f)->f_name; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 119 | PyObject * |
| 120 | PyFile_FromFile(fp, name, mode, close) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | FILE *fp; |
| 122 | char *name; |
| 123 | char *mode; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 124 | int (*close) Py_FPROTO((FILE *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | if (f == NULL) |
| 128 | return NULL; |
| 129 | f->f_fp = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 130 | f->f_name = PyString_FromString(name); |
| 131 | f->f_mode = PyString_FromString(mode); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 132 | f->f_close = close; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 133 | f->f_softspace = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 134 | if (strchr(mode,'b') != NULL) |
| 135 | f->f_binary = 1; |
| 136 | else |
| 137 | f->f_binary = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | if (f->f_name == NULL || f->f_mode == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 139 | Py_DECREF(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | } |
| 142 | f->f_fp = fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | return (PyObject *) f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | PyObject * |
| 147 | PyFile_FromString(name, mode) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | char *name, *mode; |
| 149 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | extern int fclose Py_PROTO((FILE *)); |
| 151 | PyFileObject *f; |
| 152 | f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | if (f == NULL) |
| 154 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 155 | #ifdef HAVE_FOPENRF |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 156 | if (*mode == '*') { |
| 157 | FILE *fopenRF(); |
| 158 | f->f_fp = fopenRF(name, mode+1); |
| 159 | } |
| 160 | else |
| 161 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 162 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 164 | f->f_fp = fopen(name, mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 166 | } |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 167 | if (f->f_fp == NULL) { |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 168 | #ifdef NO_FOPEN_ERRNO |
Barry Warsaw | 52ddc0e | 1998-07-23 16:07:02 +0000 | [diff] [blame] | 169 | /* Metroworks only, not testable, so unchanged */ |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 170 | if ( errno == 0 ) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | PyErr_SetString(PyExc_IOError, "Cannot open file"); |
| 172 | Py_DECREF(f); |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | #endif |
Barry Warsaw | 52ddc0e | 1998-07-23 16:07:02 +0000 | [diff] [blame] | 176 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 177 | Py_DECREF(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 180 | return (PyObject *)f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | PyFile_SetBufSize(f, bufsize) |
| 185 | PyObject *f; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | int bufsize; |
| 187 | { |
| 188 | if (bufsize >= 0) { |
| 189 | #ifdef HAVE_SETVBUF |
| 190 | int type; |
| 191 | switch (bufsize) { |
| 192 | case 0: |
| 193 | type = _IONBF; |
| 194 | break; |
| 195 | case 1: |
| 196 | type = _IOLBF; |
| 197 | bufsize = BUFSIZ; |
| 198 | break; |
| 199 | default: |
| 200 | type = _IOFBF; |
| 201 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 202 | setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
| 203 | type, bufsize); |
Guido van Rossum | f8b4de0 | 1998-03-06 15:32:40 +0000 | [diff] [blame] | 204 | #else /* !HAVE_SETVBUF */ |
| 205 | if (bufsize <= 1) |
| 206 | setbuf(((PyFileObject *)f)->f_fp, (char *)NULL); |
| 207 | #endif /* !HAVE_SETVBUF */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 211 | static PyObject * |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 212 | err_closed() |
| 213 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 215 | return NULL; |
| 216 | } |
| 217 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 218 | /* Methods */ |
| 219 | |
| 220 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 221 | file_dealloc(f) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 222 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 224 | if (f->f_fp != NULL && f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 225 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 226 | (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 227 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 228 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 229 | if (f->f_name != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | Py_DECREF(f->f_name); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 231 | } |
| 232 | if (f->f_mode != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 233 | Py_DECREF(f->f_mode); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 234 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 235 | PyObject_DEL(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 239 | file_repr(f) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | { |
| 242 | char buf[300]; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 243 | sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | f->f_fp == NULL ? "closed" : "open", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | PyString_AsString(f->f_name), |
| 246 | PyString_AsString(f->f_mode), |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 247 | f); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 252 | file_close(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 253 | PyFileObject *f; |
| 254 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 255 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 256 | int sts = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 257 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | if (f->f_fp != NULL) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 260 | if (f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 262 | errno = 0; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 263 | sts = (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 264 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 265 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 266 | f->f_fp = NULL; |
| 267 | } |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 268 | if (sts == EOF) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 269 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 270 | if (sts != 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | return PyInt_FromLong((long)sts); |
| 272 | Py_INCREF(Py_None); |
| 273 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 276 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 277 | file_seek(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 278 | PyFileObject *f; |
| 279 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 281 | int whence; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 282 | int ret; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 283 | off_t offset; |
| 284 | PyObject *offobj; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 286 | if (f->f_fp == NULL) |
| 287 | return err_closed(); |
| 288 | whence = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 289 | if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 292 | offset = PyInt_AsLong(offobj); |
| 293 | #else |
| 294 | offset = PyLong_Check(offobj) ? |
| 295 | PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj); |
| 296 | #endif |
| 297 | if (PyErr_Occurred()) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 298 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 300 | errno = 0; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 301 | #if defined(HAVE_FSEEKO) |
| 302 | ret = fseeko(f->f_fp, offset, whence); |
| 303 | #elif defined(HAVE_FSEEK64) |
| 304 | ret = fseek64(f->f_fp, offset, whence); |
| 305 | #else |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 306 | ret = fseek(f->f_fp, offset, whence); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 307 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 309 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 311 | clearerr(f->f_fp); |
| 312 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 313 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | Py_INCREF(Py_None); |
| 315 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 318 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 319 | static PyObject * |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 320 | file_truncate(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | PyFileObject *f; |
| 322 | PyObject *args; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 323 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 324 | int ret; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 325 | off_t newsize; |
| 326 | PyObject *newsizeobj; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 327 | |
| 328 | if (f->f_fp == NULL) |
| 329 | return err_closed(); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 330 | newsizeobj = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 331 | if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj)) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 332 | return NULL; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 333 | if (newsizeobj != NULL) { |
| 334 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 335 | newsize = PyInt_AsLong(newsizeobj); |
| 336 | #else |
| 337 | newsize = PyLong_Check(newsizeobj) ? |
| 338 | PyLong_AsLongLong(newsizeobj) : |
| 339 | PyInt_AsLong(newsizeobj); |
| 340 | #endif |
| 341 | if (PyErr_Occurred()) |
| 342 | return NULL; |
| 343 | } else { |
| 344 | /* Default to current position*/ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 345 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 346 | errno = 0; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 347 | #if defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT) |
| 348 | newsize = ftello(f->f_fp); |
| 349 | #elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT) |
| 350 | newsize = ftell64(f->f_fp); |
| 351 | #else |
| 352 | newsize = ftell(f->f_fp); |
| 353 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 355 | if (newsize == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 356 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 357 | clearerr(f->f_fp); |
| 358 | return NULL; |
| 359 | } |
| 360 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 362 | errno = 0; |
| 363 | ret = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | Py_END_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 365 | if (ret == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 367 | errno = 0; |
| 368 | ret = ftruncate(fileno(f->f_fp), newsize); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 369 | Py_END_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 370 | } |
| 371 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 373 | clearerr(f->f_fp); |
| 374 | return NULL; |
| 375 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 376 | Py_INCREF(Py_None); |
| 377 | return Py_None; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 378 | } |
| 379 | #endif /* HAVE_FTRUNCATE */ |
| 380 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 381 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 382 | file_tell(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 383 | PyFileObject *f; |
| 384 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 385 | { |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 386 | off_t offset; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 387 | if (f->f_fp == NULL) |
| 388 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 389 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 391 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 392 | errno = 0; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 393 | #if defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT) |
| 394 | offset = ftello(f->f_fp); |
| 395 | #elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT) |
| 396 | offset = ftell64(f->f_fp); |
| 397 | #else |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 398 | offset = ftell(f->f_fp); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 399 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 401 | if (offset == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 402 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 403 | clearerr(f->f_fp); |
| 404 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 405 | } |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 406 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | return PyInt_FromLong(offset); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 408 | #else |
| 409 | return PyLong_FromLongLong(offset); |
| 410 | #endif |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | static PyObject * |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 414 | file_fileno(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 415 | PyFileObject *f; |
| 416 | PyObject *args; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 417 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 418 | if (f->f_fp == NULL) |
| 419 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 420 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 421 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 422 | return PyInt_FromLong((long) fileno(f->f_fp)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 425 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 426 | file_flush(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | PyFileObject *f; |
| 428 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 429 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 430 | int res; |
| 431 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 432 | if (f->f_fp == NULL) |
| 433 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 436 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 437 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 438 | res = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 439 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 440 | if (res != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 441 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 442 | clearerr(f->f_fp); |
| 443 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 444 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 445 | Py_INCREF(Py_None); |
| 446 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 449 | static PyObject * |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 450 | file_isatty(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | PyFileObject *f; |
| 452 | PyObject *args; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 453 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 454 | long res; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 455 | if (f->f_fp == NULL) |
| 456 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 458 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 459 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 460 | res = isatty((int)fileno(f->f_fp)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 461 | Py_END_ALLOW_THREADS |
| 462 | return PyInt_FromLong(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 466 | #if BUFSIZ < 8192 |
| 467 | #define SMALLCHUNK 8192 |
| 468 | #else |
| 469 | #define SMALLCHUNK BUFSIZ |
| 470 | #endif |
| 471 | |
Guido van Rossum | 3c25904 | 1999-01-14 19:00:14 +0000 | [diff] [blame] | 472 | #if SIZEOF_INT < 4 |
| 473 | #define BIGCHUNK (512 * 32) |
| 474 | #else |
| 475 | #define BIGCHUNK (512 * 1024) |
| 476 | #endif |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 477 | |
| 478 | static size_t |
| 479 | new_buffersize(f, currentsize) |
| 480 | PyFileObject *f; |
| 481 | size_t currentsize; |
| 482 | { |
| 483 | #ifdef HAVE_FSTAT |
| 484 | long pos, end; |
| 485 | struct stat st; |
| 486 | if (fstat(fileno(f->f_fp), &st) == 0) { |
| 487 | end = st.st_size; |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 488 | /* The following is not a bug: we really need to call lseek() |
| 489 | *and* ftell(). The reason is that some stdio libraries |
| 490 | mistakenly flush their buffer when ftell() is called and |
| 491 | the lseek() call it makes fails, thereby throwing away |
| 492 | data that cannot be recovered in any way. To avoid this, |
| 493 | we first test lseek(), and only call ftell() if lseek() |
| 494 | works. We can't use the lseek() value either, because we |
| 495 | need to take the amount of buffered data into account. |
| 496 | (Yet another reason why stdio stinks. :-) */ |
Guido van Rossum | 91aaa92 | 1998-05-05 22:21:35 +0000 | [diff] [blame] | 497 | pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
| 498 | if (pos >= 0) |
| 499 | pos = ftell(f->f_fp); |
Guido van Rossum | d30dc0a | 1998-04-27 19:01:08 +0000 | [diff] [blame] | 500 | if (pos < 0) |
| 501 | clearerr(f->f_fp); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 502 | if (end > pos && pos >= 0) |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 503 | return currentsize + end - pos + 1; |
Guido van Rossum | dcb5e7f | 1998-03-03 22:36:10 +0000 | [diff] [blame] | 504 | /* Add 1 so if the file were to grow we'd notice. */ |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 505 | } |
| 506 | #endif |
| 507 | if (currentsize > SMALLCHUNK) { |
| 508 | /* Keep doubling until we reach BIGCHUNK; |
| 509 | then keep adding BIGCHUNK. */ |
| 510 | if (currentsize <= BIGCHUNK) |
| 511 | return currentsize + currentsize; |
| 512 | else |
| 513 | return currentsize + BIGCHUNK; |
| 514 | } |
| 515 | return currentsize + SMALLCHUNK; |
| 516 | } |
| 517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 519 | file_read(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 520 | PyFileObject *f; |
| 521 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 522 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 523 | long bytesrequested = -1; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 524 | size_t bytesread, buffersize, chunksize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 525 | PyObject *v; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 527 | if (f->f_fp == NULL) |
| 528 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 529 | if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 530 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 531 | if (bytesrequested < 0) |
Guido van Rossum | ff1ccbf | 1999-04-10 15:48:23 +0000 | [diff] [blame] | 532 | buffersize = new_buffersize(f, (size_t)0); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 533 | else |
| 534 | buffersize = bytesrequested; |
| 535 | v = PyString_FromStringAndSize((char *)NULL, buffersize); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 536 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 538 | bytesread = 0; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 539 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | errno = 0; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 542 | chunksize = fread(BUF(v) + bytesread, 1, |
| 543 | buffersize - bytesread, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 544 | Py_END_ALLOW_THREADS |
| 545 | if (chunksize == 0) { |
| 546 | if (!ferror(f->f_fp)) |
| 547 | break; |
| 548 | PyErr_SetFromErrno(PyExc_IOError); |
| 549 | clearerr(f->f_fp); |
| 550 | Py_DECREF(v); |
| 551 | return NULL; |
| 552 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 553 | bytesread += chunksize; |
| 554 | if (bytesread < buffersize) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 555 | break; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 556 | if (bytesrequested < 0) { |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 557 | buffersize = new_buffersize(f, buffersize); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 558 | if (_PyString_Resize(&v, buffersize) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 559 | return NULL; |
| 560 | } |
| 561 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 562 | if (bytesread != buffersize) |
| 563 | _PyString_Resize(&v, bytesread); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | return v; |
| 565 | } |
| 566 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 567 | static PyObject * |
| 568 | file_readinto(f, args) |
| 569 | PyFileObject *f; |
| 570 | PyObject *args; |
| 571 | { |
| 572 | char *ptr; |
| 573 | int ntodo, ndone, nnow; |
| 574 | |
| 575 | if (f->f_fp == NULL) |
| 576 | return err_closed(); |
| 577 | if (!PyArg_Parse(args, "w#", &ptr, &ntodo)) |
| 578 | return NULL; |
| 579 | ndone = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 580 | while (ntodo > 0) { |
| 581 | Py_BEGIN_ALLOW_THREADS |
| 582 | errno = 0; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 583 | nnow = fread(ptr+ndone, 1, ntodo, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 584 | Py_END_ALLOW_THREADS |
| 585 | if (nnow == 0) { |
| 586 | if (!ferror(f->f_fp)) |
| 587 | break; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 588 | PyErr_SetFromErrno(PyExc_IOError); |
| 589 | clearerr(f->f_fp); |
| 590 | return NULL; |
| 591 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 592 | ndone += nnow; |
| 593 | ntodo -= nnow; |
| 594 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 595 | return PyInt_FromLong(ndone); |
| 596 | } |
| 597 | |
| 598 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 599 | /* Internal routine to get a line. |
| 600 | Size argument interpretation: |
| 601 | > 0: max length; |
| 602 | = 0: read arbitrary line; |
| 603 | < 0: strip trailing '\n', raise EOFError if EOF reached immediately |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 604 | */ |
| 605 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | static PyObject * |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 607 | getline(f, n) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | PyFileObject *f; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 609 | int n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 610 | { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 611 | register FILE *fp; |
| 612 | register int c; |
| 613 | register char *buf, *end; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 614 | int n1, n2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | PyObject *v; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | c10aa77 | 1992-07-31 12:42:38 +0000 | [diff] [blame] | 617 | fp = f->f_fp; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 618 | n2 = n > 0 ? n : 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 619 | v = PyString_FromStringAndSize((char *)NULL, n2); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 620 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 621 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 622 | buf = BUF(v); |
| 623 | end = buf + n2; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 624 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 626 | for (;;) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 627 | if ((c = getc(fp)) == EOF) { |
Guido van Rossum | 76ad8ed | 1991-06-03 10:54:55 +0000 | [diff] [blame] | 628 | clearerr(fp); |
Guido van Rossum | f518154 | 1997-11-07 19:20:34 +0000 | [diff] [blame] | 629 | Py_BLOCK_THREADS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 630 | if (PyErr_CheckSignals()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 631 | Py_DECREF(v); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
| 634 | if (n < 0 && buf == BUF(v)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | Py_DECREF(v); |
| 636 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 201be05 | 1991-12-24 13:26:41 +0000 | [diff] [blame] | 637 | "EOF when reading a line"); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 638 | return NULL; |
| 639 | } |
Guido van Rossum | f518154 | 1997-11-07 19:20:34 +0000 | [diff] [blame] | 640 | Py_UNBLOCK_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 641 | break; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 642 | } |
| 643 | if ((*buf++ = c) == '\n') { |
| 644 | if (n < 0) |
| 645 | buf--; |
| 646 | break; |
| 647 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 648 | if (buf == end) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 649 | if (n > 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 650 | break; |
| 651 | n1 = n2; |
| 652 | n2 += 1000; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 653 | Py_BLOCK_THREADS |
| 654 | if (_PyString_Resize(&v, n2) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 655 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 656 | Py_UNBLOCK_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 657 | buf = BUF(v) + n1; |
| 658 | end = BUF(v) + n2; |
| 659 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 660 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 662 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 663 | n1 = buf - BUF(v); |
| 664 | if (n1 != n2) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | _PyString_Resize(&v, n1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 666 | return v; |
| 667 | } |
| 668 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 669 | /* External C interface */ |
| 670 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 671 | PyObject * |
| 672 | PyFile_GetLine(f, n) |
| 673 | PyObject *f; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 674 | int n; |
| 675 | { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 676 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 677 | PyErr_BadInternalCall(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 680 | if (!PyFile_Check(f)) { |
| 681 | PyObject *reader; |
| 682 | PyObject *args; |
| 683 | PyObject *result; |
| 684 | reader = PyObject_GetAttrString(f, "readline"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 685 | if (reader == NULL) |
| 686 | return NULL; |
| 687 | if (n <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 688 | args = Py_BuildValue("()"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 689 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 690 | args = Py_BuildValue("(i)", n); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 691 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 692 | Py_DECREF(reader); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 693 | return NULL; |
| 694 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | result = PyEval_CallObject(reader, args); |
| 696 | Py_DECREF(reader); |
| 697 | Py_DECREF(args); |
| 698 | if (result != NULL && !PyString_Check(result)) { |
| 699 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 700 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 701 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 702 | "object.readline() returned non-string"); |
| 703 | } |
| 704 | if (n < 0 && result != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 705 | char *s = PyString_AsString(result); |
| 706 | int len = PyString_Size(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 707 | if (len == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 708 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 709 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 710 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 711 | "EOF when reading a line"); |
| 712 | } |
| 713 | else if (s[len-1] == '\n') { |
| 714 | if (result->ob_refcnt == 1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 715 | _PyString_Resize(&result, len-1); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 716 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 717 | PyObject *v; |
| 718 | v = PyString_FromStringAndSize(s, |
| 719 | len-1); |
| 720 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 721 | result = v; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | return result; |
| 726 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 727 | if (((PyFileObject*)f)->f_fp == NULL) |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 728 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 729 | return getline((PyFileObject *)f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* Python method */ |
| 733 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 734 | static PyObject * |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 735 | file_readline(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 736 | PyFileObject *f; |
| 737 | PyObject *args; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 738 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 739 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 741 | if (f->f_fp == NULL) |
| 742 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 743 | if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 744 | return NULL; |
| 745 | if (n == 0) |
| 746 | return PyString_FromString(""); |
| 747 | if (n < 0) |
| 748 | n = 0; |
Guido van Rossum | 51415a7 | 1992-03-27 17:23:38 +0000 | [diff] [blame] | 749 | return getline(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 752 | static PyObject * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 753 | file_readlines(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 754 | PyFileObject *f; |
| 755 | PyObject *args; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 756 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 757 | long sizehint = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 758 | PyObject *list; |
| 759 | PyObject *line; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 760 | char small_buffer[SMALLCHUNK]; |
| 761 | char *buffer = small_buffer; |
| 762 | size_t buffersize = SMALLCHUNK; |
| 763 | PyObject *big_buffer = NULL; |
| 764 | size_t nfilled = 0; |
| 765 | size_t nread; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 766 | size_t totalread = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 767 | char *p, *q, *end; |
| 768 | int err; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 770 | if (f->f_fp == NULL) |
| 771 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 772 | if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 773 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 774 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 775 | return NULL; |
| 776 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 777 | Py_BEGIN_ALLOW_THREADS |
| 778 | errno = 0; |
| 779 | nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp); |
| 780 | Py_END_ALLOW_THREADS |
| 781 | if (nread == 0) { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 782 | sizehint = 0; |
Guido van Rossum | 3da3fce | 1998-02-19 20:46:48 +0000 | [diff] [blame] | 783 | if (!ferror(f->f_fp)) |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 784 | break; |
| 785 | PyErr_SetFromErrno(PyExc_IOError); |
| 786 | clearerr(f->f_fp); |
| 787 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 788 | Py_DECREF(list); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 789 | list = NULL; |
| 790 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 791 | } |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 792 | totalread += nread; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 793 | p = memchr(buffer+nfilled, '\n', nread); |
| 794 | if (p == NULL) { |
| 795 | /* Need a larger buffer to fit this line */ |
| 796 | nfilled += nread; |
| 797 | buffersize *= 2; |
| 798 | if (big_buffer == NULL) { |
| 799 | /* Create the big buffer */ |
| 800 | big_buffer = PyString_FromStringAndSize( |
| 801 | NULL, buffersize); |
| 802 | if (big_buffer == NULL) |
| 803 | goto error; |
| 804 | buffer = PyString_AS_STRING(big_buffer); |
| 805 | memcpy(buffer, small_buffer, nfilled); |
| 806 | } |
| 807 | else { |
| 808 | /* Grow the big buffer */ |
| 809 | _PyString_Resize(&big_buffer, buffersize); |
| 810 | buffer = PyString_AS_STRING(big_buffer); |
| 811 | } |
| 812 | continue; |
| 813 | } |
| 814 | end = buffer+nfilled+nread; |
| 815 | q = buffer; |
| 816 | do { |
| 817 | /* Process complete lines */ |
| 818 | p++; |
| 819 | line = PyString_FromStringAndSize(q, p-q); |
| 820 | if (line == NULL) |
| 821 | goto error; |
| 822 | err = PyList_Append(list, line); |
| 823 | Py_DECREF(line); |
| 824 | if (err != 0) |
| 825 | goto error; |
| 826 | q = p; |
| 827 | p = memchr(q, '\n', end-q); |
| 828 | } while (p != NULL); |
| 829 | /* Move the remaining incomplete line to the start */ |
| 830 | nfilled = end-q; |
| 831 | memmove(buffer, q, nfilled); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 832 | if (sizehint > 0) |
| 833 | if (totalread >= (size_t)sizehint) |
| 834 | break; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 835 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 836 | if (nfilled != 0) { |
| 837 | /* Partial last line */ |
| 838 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 839 | if (line == NULL) |
| 840 | goto error; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 841 | if (sizehint > 0) { |
| 842 | /* Need to complete the last line */ |
| 843 | PyObject *rest = getline(f, 0); |
| 844 | if (rest == NULL) { |
| 845 | Py_DECREF(line); |
| 846 | goto error; |
| 847 | } |
| 848 | PyString_Concat(&line, rest); |
| 849 | Py_DECREF(rest); |
| 850 | if (line == NULL) |
| 851 | goto error; |
| 852 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 853 | err = PyList_Append(list, line); |
| 854 | Py_DECREF(line); |
| 855 | if (err != 0) |
| 856 | goto error; |
| 857 | } |
| 858 | cleanup: |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 859 | if (big_buffer) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 860 | Py_DECREF(big_buffer); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 861 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 862 | return list; |
| 863 | } |
| 864 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 865 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 866 | file_write(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 867 | PyFileObject *f; |
| 868 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 870 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 871 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 872 | if (f->f_fp == NULL) |
| 873 | return err_closed(); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 874 | if (!PyArg_Parse(args, f->f_binary ? "s#" : "t#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 875 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 876 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 877 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 878 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 879 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 880 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 881 | if (n2 != n) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 883 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 884 | return NULL; |
| 885 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 886 | Py_INCREF(Py_None); |
| 887 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 890 | static PyObject * |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 891 | file_writelines(f, args) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 892 | PyFileObject *f; |
| 893 | PyObject *args; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 894 | { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 895 | #define CHUNKSIZE 1000 |
| 896 | PyObject *list, *line; |
| 897 | PyObject *result; |
| 898 | int i, j, index, len, nwritten, islist; |
| 899 | |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 900 | if (f->f_fp == NULL) |
| 901 | return err_closed(); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 902 | if (args == NULL || !PySequence_Check(args)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 903 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 904 | "writelines() requires sequence of strings"); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 905 | return NULL; |
| 906 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 907 | islist = PyList_Check(args); |
| 908 | |
| 909 | /* Strategy: slurp CHUNKSIZE lines into a private list, |
| 910 | checking that they are all strings, then write that list |
| 911 | without holding the interpreter lock, then come back for more. */ |
| 912 | index = 0; |
| 913 | if (islist) |
| 914 | list = NULL; |
| 915 | else { |
| 916 | list = PyList_New(CHUNKSIZE); |
| 917 | if (list == NULL) |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 918 | return NULL; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 919 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 920 | result = NULL; |
| 921 | |
| 922 | for (;;) { |
| 923 | if (islist) { |
| 924 | Py_XDECREF(list); |
| 925 | list = PyList_GetSlice(args, index, index+CHUNKSIZE); |
| 926 | if (list == NULL) |
| 927 | return NULL; |
| 928 | j = PyList_GET_SIZE(list); |
| 929 | } |
| 930 | else { |
| 931 | for (j = 0; j < CHUNKSIZE; j++) { |
| 932 | line = PySequence_GetItem(args, index+j); |
| 933 | if (line == NULL) { |
| 934 | if (PyErr_ExceptionMatches( |
| 935 | PyExc_IndexError)) |
| 936 | { |
| 937 | PyErr_Clear(); |
| 938 | break; |
| 939 | } |
| 940 | /* Some other error occurred. |
| 941 | XXX We may lose some output. */ |
| 942 | goto error; |
| 943 | } |
| 944 | if (!PyString_Check(line)) { |
| 945 | Py_DECREF(line); |
| 946 | PyErr_SetString(PyExc_TypeError, |
| 947 | "writelines() requires sequences of strings"); |
| 948 | goto error; |
| 949 | } |
| 950 | PyList_SetItem(list, j, line); |
| 951 | } |
| 952 | } |
| 953 | if (j == 0) |
| 954 | break; |
| 955 | |
| 956 | Py_BEGIN_ALLOW_THREADS |
| 957 | f->f_softspace = 0; |
| 958 | errno = 0; |
| 959 | for (i = 0; i < j; i++) { |
| 960 | line = PyList_GET_ITEM(list, i); |
| 961 | len = PyString_GET_SIZE(line); |
| 962 | nwritten = fwrite(PyString_AS_STRING(line), |
| 963 | 1, len, f->f_fp); |
| 964 | if (nwritten != len) { |
| 965 | Py_BLOCK_THREADS |
| 966 | PyErr_SetFromErrno(PyExc_IOError); |
| 967 | clearerr(f->f_fp); |
| 968 | goto error; |
| 969 | } |
| 970 | } |
| 971 | Py_END_ALLOW_THREADS |
| 972 | |
| 973 | if (j < CHUNKSIZE) |
| 974 | break; |
| 975 | index += CHUNKSIZE; |
| 976 | } |
| 977 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 978 | Py_INCREF(Py_None); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 979 | result = Py_None; |
| 980 | error: |
| 981 | Py_XDECREF(list); |
| 982 | return result; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | static PyMethodDef file_methods[] = { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 986 | {"readline", (PyCFunction)file_readline, 1}, |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 987 | {"read", (PyCFunction)file_read, 1}, |
| 988 | {"write", (PyCFunction)file_write, 0}, |
| 989 | {"fileno", (PyCFunction)file_fileno, 0}, |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 990 | {"seek", (PyCFunction)file_seek, 1}, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 991 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 992 | {"truncate", (PyCFunction)file_truncate, 1}, |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 993 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | {"tell", (PyCFunction)file_tell, 0}, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 995 | {"readinto", (PyCFunction)file_readinto, 0}, |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 996 | {"readlines", (PyCFunction)file_readlines, 1}, |
| 997 | {"writelines", (PyCFunction)file_writelines, 0}, |
| 998 | {"flush", (PyCFunction)file_flush, 0}, |
| 999 | {"close", (PyCFunction)file_close, 0}, |
| 1000 | {"isatty", (PyCFunction)file_isatty, 0}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1001 | {NULL, NULL} /* sentinel */ |
| 1002 | }; |
| 1003 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1004 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1005 | |
| 1006 | static struct memberlist file_memberlist[] = { |
| 1007 | {"softspace", T_INT, OFF(f_softspace)}, |
| 1008 | {"mode", T_OBJECT, OFF(f_mode), RO}, |
| 1009 | {"name", T_OBJECT, OFF(f_name), RO}, |
| 1010 | /* getattr(f, "closed") is implemented without this table */ |
| 1011 | {"closed", T_INT, 0, RO}, |
| 1012 | {NULL} /* Sentinel */ |
| 1013 | }; |
| 1014 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1015 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1016 | file_getattr(f, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1017 | PyFileObject *f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1018 | char *name; |
| 1019 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1020 | PyObject *res; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1021 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1022 | res = Py_FindMethod(file_methods, (PyObject *)f, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1023 | if (res != NULL) |
| 1024 | return res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1025 | PyErr_Clear(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1026 | if (strcmp(name, "closed") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1027 | return PyInt_FromLong((long)(f->f_fp == 0)); |
| 1028 | return PyMember_Get((char *)f, file_memberlist, name); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | static int |
| 1032 | file_setattr(f, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1033 | PyFileObject *f; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1034 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1035 | PyObject *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1036 | { |
| 1037 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1038 | PyErr_SetString(PyExc_AttributeError, |
| 1039 | "can't delete file attributes"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1040 | return -1; |
| 1041 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1042 | return PyMember_Set((char *)f, file_memberlist, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1045 | PyTypeObject PyFile_Type = { |
| 1046 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1047 | 0, |
| 1048 | "file", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1049 | sizeof(PyFileObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1050 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1051 | (destructor)file_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1052 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1053 | (getattrfunc)file_getattr, /*tp_getattr*/ |
| 1054 | (setattrfunc)file_setattr, /*tp_setattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1055 | 0, /*tp_compare*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1056 | (reprfunc)file_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1057 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* Interface for the 'soft space' between print items. */ |
| 1060 | |
| 1061 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1062 | PyFile_SoftSpace(f, newflag) |
| 1063 | PyObject *f; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1064 | int newflag; |
| 1065 | { |
| 1066 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1067 | if (f == NULL) { |
| 1068 | /* Do nothing */ |
| 1069 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1070 | else if (PyFile_Check(f)) { |
| 1071 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 1072 | ((PyFileObject *)f)->f_softspace = newflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1073 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1074 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | PyObject *v; |
| 1076 | v = PyObject_GetAttrString(f, "softspace"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1077 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1079 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1080 | if (PyInt_Check(v)) |
| 1081 | oldflag = PyInt_AsLong(v); |
| 1082 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1083 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1084 | v = PyInt_FromLong((long)newflag); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1085 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1086 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1087 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1088 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 1089 | PyErr_Clear(); |
| 1090 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1093 | return oldflag; |
| 1094 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1095 | |
| 1096 | /* Interfaces to write objects/strings to file-like objects */ |
| 1097 | |
| 1098 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1099 | PyFile_WriteObject(v, f, flags) |
| 1100 | PyObject *v; |
| 1101 | PyObject *f; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1102 | int flags; |
| 1103 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1104 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1105 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1106 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1107 | return -1; |
| 1108 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1109 | else if (PyFile_Check(f)) { |
| 1110 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1111 | if (fp == NULL) { |
| 1112 | err_closed(); |
| 1113 | return -1; |
| 1114 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1115 | return PyObject_Print(v, fp, flags); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1116 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1117 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1118 | if (writer == NULL) |
| 1119 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1120 | if (flags & Py_PRINT_RAW) |
| 1121 | value = PyObject_Str(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1122 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1123 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1124 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1125 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1126 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1127 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1128 | args = Py_BuildValue("(O)", value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 1129 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1130 | Py_DECREF(value); |
| 1131 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1134 | result = PyEval_CallObject(writer, args); |
| 1135 | Py_DECREF(args); |
| 1136 | Py_DECREF(value); |
| 1137 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1138 | if (result == NULL) |
| 1139 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1140 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1144 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1145 | PyFile_WriteString(s, f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1146 | char *s; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1147 | PyObject *f; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1148 | { |
| 1149 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1150 | /* Should be caused by a pre-existing error */ |
| 1151 | if(!PyErr_Occurred()) |
| 1152 | PyErr_SetString(PyExc_SystemError, |
| 1153 | "null file for PyFile_WriteString"); |
| 1154 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1155 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1156 | else if (PyFile_Check(f)) { |
| 1157 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1158 | if (fp == NULL) { |
| 1159 | err_closed(); |
| 1160 | return -1; |
| 1161 | } |
| 1162 | fputs(s, fp); |
| 1163 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1164 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1165 | else if (!PyErr_Occurred()) { |
| 1166 | PyObject *v = PyString_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1167 | int err; |
| 1168 | if (v == NULL) |
| 1169 | return -1; |
| 1170 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 1171 | Py_DECREF(v); |
| 1172 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1173 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 1174 | else |
| 1175 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1176 | } |