Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* File object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | fa3da8a | 1992-01-27 16:53:23 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 31 | #define BUF(v) GETSTRINGVALUE((stringobject *)v) |
| 32 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | #include "errno.h" |
| 34 | #ifndef errno |
| 35 | extern int errno; |
| 36 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | |
| 38 | typedef struct { |
| 39 | OB_HEAD |
| 40 | FILE *f_fp; |
| 41 | object *f_name; |
| 42 | object *f_mode; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 43 | int (*f_close) PROTO((FILE *)); |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 44 | int f_softspace; /* Flag used by 'print' command */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | } fileobject; |
| 46 | |
| 47 | FILE * |
| 48 | getfilefile(f) |
| 49 | object *f; |
| 50 | { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 51 | if (f == NULL || !is_fileobject(f)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | return NULL; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 53 | else |
| 54 | return ((fileobject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | object * |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 58 | newopenfileobject(fp, name, mode, close) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | FILE *fp; |
| 60 | char *name; |
| 61 | char *mode; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 62 | int (*close) FPROTO((FILE *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | { |
| 64 | fileobject *f = NEWOBJ(fileobject, &Filetype); |
| 65 | if (f == NULL) |
| 66 | return NULL; |
| 67 | f->f_fp = NULL; |
| 68 | f->f_name = newstringobject(name); |
| 69 | f->f_mode = newstringobject(mode); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 70 | f->f_close = close; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 71 | f->f_softspace = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | if (f->f_name == NULL || f->f_mode == NULL) { |
| 73 | DECREF(f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | f->f_fp = fp; |
| 77 | return (object *) f; |
| 78 | } |
| 79 | |
| 80 | object * |
| 81 | newfileobject(name, mode) |
| 82 | char *name, *mode; |
| 83 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 84 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | fileobject *f; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 86 | f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | if (f == NULL) |
| 88 | return NULL; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 89 | #ifdef USE_FOPENRF |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 90 | if (*mode == '*') { |
| 91 | FILE *fopenRF(); |
| 92 | f->f_fp = fopenRF(name, mode+1); |
| 93 | } |
| 94 | else |
| 95 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 96 | { |
| 97 | BGN_SAVE |
| 98 | f->f_fp = fopen(name, mode); |
| 99 | END_SAVE |
| 100 | } |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 101 | if (f->f_fp == NULL) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 102 | err_errno(IOError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | DECREF(f); |
| 104 | return NULL; |
| 105 | } |
| 106 | return (object *)f; |
| 107 | } |
| 108 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 109 | static object * |
| 110 | err_closed() |
| 111 | { |
| 112 | err_setstr(ValueError, "I/O operation on closed file"); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | /* Methods */ |
| 117 | |
| 118 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 119 | file_dealloc(f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | fileobject *f; |
| 121 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 122 | if (f->f_fp != NULL && f->f_close != NULL) { |
| 123 | BGN_SAVE |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 124 | (*f->f_close)(f->f_fp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 125 | END_SAVE |
| 126 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | if (f->f_name != NULL) |
| 128 | DECREF(f->f_name); |
| 129 | if (f->f_mode != NULL) |
| 130 | DECREF(f->f_mode); |
| 131 | free((char *)f); |
| 132 | } |
| 133 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 135 | file_repr(f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | fileobject *f; |
| 137 | { |
| 138 | char buf[300]; |
Guido van Rossum | e35399e | 1993-01-10 18:33:56 +0000 | [diff] [blame] | 139 | sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | f->f_fp == NULL ? "closed" : "open", |
| 141 | getstringvalue(f->f_name), |
Guido van Rossum | e35399e | 1993-01-10 18:33:56 +0000 | [diff] [blame] | 142 | getstringvalue(f->f_mode), |
| 143 | (long)f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | return newstringobject(buf); |
| 145 | } |
| 146 | |
| 147 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 148 | file_close(f, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | fileobject *f; |
| 150 | object *args; |
| 151 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 152 | int sts = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 153 | if (!getnoarg(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | if (f->f_fp != NULL) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 156 | if (f->f_close != NULL) { |
| 157 | BGN_SAVE |
| 158 | errno = 0; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 159 | sts = (*f->f_close)(f->f_fp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 160 | END_SAVE |
| 161 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | f->f_fp = NULL; |
| 163 | } |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 164 | if (sts == EOF) |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 165 | return err_errno(IOError); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 166 | if (sts != 0) |
| 167 | return newintobject((long)sts); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | INCREF(None); |
| 169 | return None; |
| 170 | } |
| 171 | |
| 172 | static object * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 173 | file_seek(f, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | fileobject *f; |
| 175 | object *args; |
| 176 | { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 177 | long offset; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 178 | int whence; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 179 | int ret; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 181 | if (f->f_fp == NULL) |
| 182 | return err_closed(); |
| 183 | whence = 0; |
| 184 | if (!getargs(args, "l", &offset)) { |
| 185 | err_clear(); |
| 186 | if (!getargs(args, "(li)", &offset, &whence)) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 189 | BGN_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 190 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 191 | ret = fseek(f->f_fp, offset, whence); |
| 192 | END_SAVE |
| 193 | if (ret != 0) { |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 194 | err_errno(IOError); |
| 195 | clearerr(f->f_fp); |
| 196 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 197 | } |
| 198 | INCREF(None); |
| 199 | return None; |
| 200 | } |
| 201 | |
| 202 | static object * |
| 203 | file_tell(f, args) |
| 204 | fileobject *f; |
| 205 | object *args; |
| 206 | { |
| 207 | long offset; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 208 | if (f->f_fp == NULL) |
| 209 | return err_closed(); |
| 210 | if (!getnoarg(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 212 | BGN_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 213 | errno = 0; |
| 214 | offset = ftell(f->f_fp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 215 | END_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 216 | if (offset == -1L) { |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 217 | err_errno(IOError); |
| 218 | clearerr(f->f_fp); |
| 219 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 220 | } |
| 221 | return newintobject(offset); |
| 222 | } |
| 223 | |
| 224 | static object * |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 225 | file_fileno(f, args) |
| 226 | fileobject *f; |
| 227 | object *args; |
| 228 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 229 | if (f->f_fp == NULL) |
| 230 | return err_closed(); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 231 | if (!getnoarg(args)) |
| 232 | return NULL; |
| 233 | return newintobject((long) fileno(f->f_fp)); |
| 234 | } |
| 235 | |
| 236 | static object * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 237 | file_flush(f, args) |
| 238 | fileobject *f; |
| 239 | object *args; |
| 240 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 241 | int res; |
| 242 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 243 | if (f->f_fp == NULL) |
| 244 | return err_closed(); |
| 245 | if (!getnoarg(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 247 | BGN_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 248 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 249 | res = fflush(f->f_fp); |
| 250 | END_SAVE |
| 251 | if (res != 0) { |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 252 | err_errno(IOError); |
| 253 | clearerr(f->f_fp); |
| 254 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 255 | } |
| 256 | INCREF(None); |
| 257 | return None; |
| 258 | } |
| 259 | |
| 260 | static object * |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 261 | file_isatty(f, args) |
| 262 | fileobject *f; |
| 263 | object *args; |
| 264 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 265 | long res; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 266 | if (f->f_fp == NULL) |
| 267 | return err_closed(); |
| 268 | if (!getnoarg(args)) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 269 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 270 | BGN_SAVE |
| 271 | res = isatty((int)fileno(f->f_fp)); |
| 272 | END_SAVE |
| 273 | return newintobject(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static object * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 277 | file_read(f, args) |
| 278 | fileobject *f; |
| 279 | object *args; |
| 280 | { |
| 281 | int n, n1, n2, n3; |
| 282 | object *v; |
| 283 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 284 | if (f->f_fp == NULL) |
| 285 | return err_closed(); |
| 286 | if (args == NULL) { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 287 | n = 0; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 288 | if (n < 0) { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 289 | err_setstr(ValueError, "negative read count"); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
| 292 | } |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 293 | else if (!getargs(args, "i", &n)) |
| 294 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 295 | |
| 296 | n2 = n != 0 ? n : BUFSIZ; |
| 297 | v = newsizedstringobject((char *)NULL, n2); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 298 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 300 | n1 = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 301 | BGN_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 302 | for (;;) { |
| 303 | n3 = fread(BUF(v)+n1, 1, n2-n1, f->f_fp); |
| 304 | /* XXX Error check? */ |
| 305 | if (n3 == 0) |
| 306 | break; |
| 307 | n1 += n3; |
| 308 | if (n1 == n) |
| 309 | break; |
| 310 | if (n == 0) { |
| 311 | n2 = n1 + BUFSIZ; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 312 | RET_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 313 | if (resizestring(&v, n2) < 0) |
| 314 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 315 | RES_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 318 | END_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 319 | if (n1 != n2) |
| 320 | resizestring(&v, n1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | return v; |
| 322 | } |
| 323 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 324 | /* Internal routine to get a line. |
| 325 | Size argument interpretation: |
| 326 | > 0: max length; |
| 327 | = 0: read arbitrary line; |
| 328 | < 0: strip trailing '\n', raise EOFError if EOF reached immediately |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 329 | */ |
| 330 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame^] | 331 | static object * |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 332 | getline(f, n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | fileobject *f; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 334 | int n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 336 | register FILE *fp; |
| 337 | register int c; |
| 338 | register char *buf, *end; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 339 | int n1, n2; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 340 | object *v; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | c10aa77 | 1992-07-31 12:42:38 +0000 | [diff] [blame] | 342 | fp = f->f_fp; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 343 | n2 = n > 0 ? n : 100; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 344 | v = newsizedstringobject((char *)NULL, n2); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 345 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 347 | buf = BUF(v); |
| 348 | end = buf + n2; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 350 | BGN_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 351 | for (;;) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 352 | if ((c = getc(fp)) == EOF) { |
Guido van Rossum | 76ad8ed | 1991-06-03 10:54:55 +0000 | [diff] [blame] | 353 | clearerr(fp); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 354 | if (intrcheck()) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 355 | RET_SAVE |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 356 | DECREF(v); |
| 357 | err_set(KeyboardInterrupt); |
| 358 | return NULL; |
| 359 | } |
| 360 | if (n < 0 && buf == BUF(v)) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 361 | RET_SAVE |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 362 | DECREF(v); |
Guido van Rossum | 201be05 | 1991-12-24 13:26:41 +0000 | [diff] [blame] | 363 | err_setstr(EOFError, |
| 364 | "EOF when reading a line"); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 367 | break; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 368 | } |
| 369 | if ((*buf++ = c) == '\n') { |
| 370 | if (n < 0) |
| 371 | buf--; |
| 372 | break; |
| 373 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 374 | if (buf == end) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 375 | if (n > 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 376 | break; |
| 377 | n1 = n2; |
| 378 | n2 += 1000; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 379 | RET_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 380 | if (resizestring(&v, n2) < 0) |
| 381 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 382 | RES_SAVE |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 383 | buf = BUF(v) + n1; |
| 384 | end = BUF(v) + n2; |
| 385 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 386 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 387 | END_SAVE |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 389 | n1 = buf - BUF(v); |
| 390 | if (n1 != n2) |
| 391 | resizestring(&v, n1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 392 | return v; |
| 393 | } |
| 394 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 395 | /* External C interface */ |
| 396 | |
| 397 | object * |
| 398 | filegetline(f, n) |
| 399 | object *f; |
| 400 | int n; |
| 401 | { |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 402 | if (f == NULL) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 403 | err_badcall(); |
| 404 | return NULL; |
| 405 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 406 | if (!is_fileobject(f)) { |
| 407 | object *reader; |
| 408 | object *args; |
| 409 | object *result; |
| 410 | reader = getattr(f, "readline"); |
| 411 | if (reader == NULL) |
| 412 | return NULL; |
| 413 | if (n <= 0) |
| 414 | args = mkvalue("()"); |
| 415 | else |
| 416 | args = mkvalue("(i)", n); |
| 417 | if (args == NULL) { |
| 418 | DECREF(reader); |
| 419 | return NULL; |
| 420 | } |
| 421 | result = call_object(reader, args); |
| 422 | DECREF(reader); |
| 423 | DECREF(args); |
| 424 | if (result != NULL && !is_stringobject(result)) { |
| 425 | DECREF(result); |
| 426 | result = NULL; |
| 427 | err_setstr(TypeError, |
| 428 | "object.readline() returned non-string"); |
| 429 | } |
| 430 | if (n < 0 && result != NULL) { |
| 431 | char *s = getstringvalue(result); |
| 432 | int len = getstringsize(result); |
| 433 | if (len == 0) { |
| 434 | DECREF(result); |
| 435 | result = NULL; |
| 436 | err_setstr(EOFError, |
| 437 | "EOF when reading a line"); |
| 438 | } |
| 439 | else if (s[len-1] == '\n') { |
| 440 | if (result->ob_refcnt == 1) |
| 441 | resizestring(&result, len-1); |
| 442 | else { |
| 443 | object *v; |
Guido van Rossum | de788b8 | 1992-12-22 14:24:04 +0000 | [diff] [blame] | 444 | v = newsizedstringobject(s, len-1); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 445 | DECREF(result); |
| 446 | result = v; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | return result; |
| 451 | } |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 452 | if (((fileobject*)f)->f_fp == NULL) |
| 453 | return err_closed(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 454 | return getline((fileobject *)f, n); |
| 455 | } |
| 456 | |
| 457 | /* Python method */ |
| 458 | |
| 459 | static object * |
| 460 | file_readline(f, args) |
| 461 | fileobject *f; |
| 462 | object *args; |
| 463 | { |
| 464 | int n; |
| 465 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 466 | if (f->f_fp == NULL) |
| 467 | return err_closed(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 468 | if (args == NULL) |
| 469 | n = 0; /* Unlimited */ |
| 470 | else { |
| 471 | if (!getintarg(args, &n)) |
| 472 | return NULL; |
| 473 | if (n < 0) { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 474 | err_setstr(ValueError, "negative readline count"); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 475 | return NULL; |
| 476 | } |
| 477 | } |
| 478 | |
Guido van Rossum | 51415a7 | 1992-03-27 17:23:38 +0000 | [diff] [blame] | 479 | return getline(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 482 | static object * |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 483 | file_readlines(f, args) |
| 484 | fileobject *f; |
| 485 | object *args; |
| 486 | { |
| 487 | object *list; |
| 488 | object *line; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 490 | if (f->f_fp == NULL) |
| 491 | return err_closed(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 492 | if (!getnoarg(args)) |
| 493 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 494 | if ((list = newlistobject(0)) == NULL) |
| 495 | return NULL; |
| 496 | for (;;) { |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 497 | line = getline(f, 0); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 498 | if (line != NULL && getstringsize(line) == 0) { |
| 499 | DECREF(line); |
| 500 | break; |
| 501 | } |
| 502 | if (line == NULL || addlistitem(list, line) != 0) { |
| 503 | DECREF(list); |
| 504 | XDECREF(line); |
| 505 | return NULL; |
| 506 | } |
| 507 | DECREF(line); |
| 508 | } |
| 509 | return list; |
| 510 | } |
| 511 | |
| 512 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 513 | file_write(f, args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 514 | fileobject *f; |
| 515 | object *args; |
| 516 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 517 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 519 | if (f->f_fp == NULL) |
| 520 | return err_closed(); |
| 521 | if (!getargs(args, "s#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 522 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 523 | f->f_softspace = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 524 | BGN_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 526 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 527 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | if (n2 != n) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 529 | err_errno(IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 530 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
| 533 | INCREF(None); |
| 534 | return None; |
| 535 | } |
| 536 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 537 | static struct methodlist file_methods[] = { |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 538 | {"close", file_close}, |
| 539 | {"flush", file_flush}, |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 540 | {"fileno", file_fileno}, |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 541 | {"isatty", file_isatty}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 542 | {"read", file_read}, |
| 543 | {"readline", file_readline}, |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 544 | {"readlines", file_readlines}, |
| 545 | {"seek", file_seek}, |
| 546 | {"tell", file_tell}, |
| 547 | {"write", file_write}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 548 | {NULL, NULL} /* sentinel */ |
| 549 | }; |
| 550 | |
| 551 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 552 | file_getattr(f, name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | fileobject *f; |
| 554 | char *name; |
| 555 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 556 | return findmethod(file_methods, (object *)f, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | typeobject Filetype = { |
| 560 | OB_HEAD_INIT(&Typetype) |
| 561 | 0, |
| 562 | "file", |
| 563 | sizeof(fileobject), |
| 564 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 565 | file_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 566 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 567 | file_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | 0, /*tp_setattr*/ |
| 569 | 0, /*tp_compare*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 570 | file_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 572 | |
| 573 | /* Interface for the 'soft space' between print items. */ |
| 574 | |
| 575 | int |
| 576 | softspace(f, newflag) |
| 577 | object *f; |
| 578 | int newflag; |
| 579 | { |
| 580 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 581 | if (f == NULL) { |
| 582 | /* Do nothing */ |
| 583 | } |
| 584 | if (is_fileobject(f)) { |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 585 | oldflag = ((fileobject *)f)->f_softspace; |
| 586 | ((fileobject *)f)->f_softspace = newflag; |
| 587 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 588 | else { |
| 589 | object *v; |
| 590 | v = getattr(f, "softspace"); |
| 591 | if (v == NULL) |
| 592 | err_clear(); |
| 593 | else { |
| 594 | if (is_intobject(v)) |
| 595 | oldflag = getintvalue(v); |
| 596 | DECREF(v); |
| 597 | } |
| 598 | v = newintobject((long)newflag); |
| 599 | if (v == NULL) |
| 600 | err_clear(); |
| 601 | else { |
| 602 | if (setattr(f, "softspace", v) != 0) |
| 603 | err_clear(); |
| 604 | DECREF(v); |
| 605 | } |
| 606 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 607 | return oldflag; |
| 608 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 609 | |
| 610 | /* Interfaces to write objects/strings to file-like objects */ |
| 611 | |
| 612 | int |
| 613 | writeobject(v, f, flags) |
| 614 | object *v; |
| 615 | object *f; |
| 616 | int flags; |
| 617 | { |
| 618 | object *writer, *value, *result; |
| 619 | if (f == NULL) { |
| 620 | err_setstr(TypeError, "writeobject with NULL file"); |
| 621 | return -1; |
| 622 | } |
| 623 | else if (is_fileobject(f)) { |
| 624 | FILE *fp = getfilefile(f); |
| 625 | if (fp == NULL) { |
| 626 | err_closed(); |
| 627 | return -1; |
| 628 | } |
| 629 | return printobject(v, fp, flags); |
| 630 | } |
| 631 | writer = getattr(f, "write"); |
| 632 | if (writer == NULL) |
| 633 | return -1; |
| 634 | if ((flags & PRINT_RAW) && is_stringobject(v)) { |
| 635 | value = v; |
| 636 | INCREF(value); |
| 637 | } |
| 638 | else { |
| 639 | value = reprobject(v); |
| 640 | if (value == NULL) { |
| 641 | DECREF(writer); |
| 642 | return -1; |
| 643 | } |
| 644 | } |
| 645 | result = call_object(writer, value); |
| 646 | DECREF(writer); |
| 647 | DECREF(value); |
| 648 | if (result == NULL) |
| 649 | return -1; |
| 650 | DECREF(result); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | void |
| 655 | writestring(s, f) |
| 656 | char *s; |
| 657 | object *f; |
| 658 | { |
| 659 | if (f == NULL) { |
| 660 | /* Do nothing */ |
| 661 | } |
| 662 | else if (is_fileobject(f)) { |
| 663 | FILE *fp = getfilefile(f); |
| 664 | if (fp != NULL) |
| 665 | fputs(s, fp); |
| 666 | } |
| 667 | else { |
| 668 | object *v = newstringobject(s); |
| 669 | if (v == NULL) { |
| 670 | err_clear(); |
| 671 | } |
| 672 | else { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 673 | if (writeobject(v, f, PRINT_RAW) != 0) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 674 | err_clear(); |
| 675 | DECREF(v); |
| 676 | } |
| 677 | } |
| 678 | } |