Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* File object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 7 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 9 | #endif /* DONT_HAVE_SYS_TYPES_H */ |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 685a38e | 1996-12-05 21:54:17 +0000 | [diff] [blame] | 11 | #ifdef HAVE_UNISTD_H |
| 12 | #include <unistd.h> |
| 13 | #endif |
| 14 | |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 15 | #ifdef MS_WIN32 |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 16 | #define fileno _fileno |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 17 | /* can (almost fully) duplicate with _chsize, see file_truncate */ |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 18 | #define HAVE_FTRUNCATE |
| 19 | #endif |
| 20 | |
Guido van Rossum | f2044e1 | 1998-04-28 16:05:59 +0000 | [diff] [blame] | 21 | #ifdef macintosh |
| 22 | #ifdef USE_GUSI |
| 23 | #define HAVE_FTRUNCATE |
| 24 | #endif |
| 25 | #endif |
| 26 | |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 27 | #ifdef __MWERKS__ |
| 28 | /* Mwerks fopen() doesn't always set errno */ |
| 29 | #define NO_FOPEN_ERRNO |
| 30 | #endif |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 34 | #ifndef DONT_HAVE_ERRNO_H |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 35 | #include <errno.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 36 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 40 | PyObject_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | FILE *f_fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | PyObject *f_name; |
| 43 | PyObject *f_mode; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 44 | int (*f_close)(FILE *); |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 45 | int f_softspace; /* Flag used by 'print' command */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 46 | int f_binary; /* Flag which indicates whether the file is open |
| 47 | open in binary (1) or test (0) mode */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 48 | } PyFileObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
| 50 | FILE * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 51 | PyFile_AsFile(PyObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | return NULL; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 55 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 56 | return ((PyFileObject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 60 | PyFile_Name(PyObject *f) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 65 | return ((PyFileObject *)f)->f_name; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 68 | |
| 69 | static PyObject * |
| 70 | fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode, |
| 71 | int (*close)(FILE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | { |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 73 | assert(f != NULL); |
| 74 | assert(PyFile_Check(f)); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 75 | assert(f->f_fp == NULL); |
| 76 | |
| 77 | Py_DECREF(f->f_name); |
| 78 | Py_DECREF(f->f_mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | f->f_name = PyString_FromString(name); |
| 80 | f->f_mode = PyString_FromString(mode); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 82 | f->f_close = close; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 83 | f->f_softspace = 0; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 84 | f->f_binary = strchr(mode,'b') != NULL; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 85 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 86 | if (f->f_name == NULL || f->f_mode == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | f->f_fp = fp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 89 | return (PyObject *) f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 92 | static PyObject * |
| 93 | open_the_file(PyFileObject *f, char *name, char *mode) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | { |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 95 | assert(f != NULL); |
| 96 | assert(PyFile_Check(f)); |
| 97 | assert(name != NULL); |
| 98 | assert(mode != NULL); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 99 | assert(f->f_fp == NULL); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 100 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 101 | /* rexec.py can't stop a user from getting the file() constructor -- |
| 102 | all they have to do is get *any* file object f, and then do |
| 103 | type(f). Here we prevent them from doing damage with it. */ |
| 104 | if (PyEval_GetRestricted()) { |
| 105 | PyErr_SetString(PyExc_IOError, |
| 106 | "file() constructor not accessible in restricted mode"); |
| 107 | return NULL; |
| 108 | } |
Tim Peters | a27a150 | 2001-11-09 20:59:14 +0000 | [diff] [blame] | 109 | errno = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 110 | #ifdef HAVE_FOPENRF |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 111 | if (*mode == '*') { |
| 112 | FILE *fopenRF(); |
| 113 | f->f_fp = fopenRF(name, mode+1); |
| 114 | } |
| 115 | else |
| 116 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 117 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 119 | f->f_fp = fopen(name, mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 121 | } |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 122 | if (f->f_fp == NULL) { |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 123 | #ifdef NO_FOPEN_ERRNO |
Jack Jansen | b3be216 | 2001-11-30 14:16:36 +0000 | [diff] [blame] | 124 | /* Metroworks only, wich does not always sets errno */ |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 125 | if (errno == 0) { |
Jack Jansen | b3be216 | 2001-11-30 14:16:36 +0000 | [diff] [blame] | 126 | PyObject *v; |
| 127 | v = Py_BuildValue("(is)", 0, "Cannot open file"); |
| 128 | if (v != NULL) { |
| 129 | PyErr_SetObject(PyExc_IOError, v); |
| 130 | Py_DECREF(v); |
| 131 | } |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | #endif |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 135 | if (errno == EINVAL) |
| 136 | PyErr_Format(PyExc_IOError, "invalid argument: %s", |
| 137 | mode); |
| 138 | else |
| 139 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 140 | f = NULL; |
| 141 | } |
| 142 | return (PyObject *)f; |
| 143 | } |
| 144 | |
| 145 | PyObject * |
| 146 | PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) |
| 147 | { |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 148 | PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, |
| 149 | NULL, NULL); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 150 | if (f != NULL) { |
| 151 | if (fill_file_fields(f, fp, name, mode, close) == NULL) { |
| 152 | Py_DECREF(f); |
| 153 | f = NULL; |
| 154 | } |
| 155 | } |
| 156 | return (PyObject *) f; |
| 157 | } |
| 158 | |
| 159 | PyObject * |
| 160 | PyFile_FromString(char *name, char *mode) |
| 161 | { |
| 162 | extern int fclose(FILE *); |
| 163 | PyFileObject *f; |
| 164 | |
| 165 | f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
| 166 | if (f != NULL) { |
| 167 | if (open_the_file(f, name, mode) == NULL) { |
| 168 | Py_DECREF(f); |
| 169 | f = NULL; |
| 170 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | return (PyObject *)f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 176 | PyFile_SetBufSize(PyObject *f, int bufsize) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | { |
| 178 | if (bufsize >= 0) { |
| 179 | #ifdef HAVE_SETVBUF |
| 180 | int type; |
| 181 | switch (bufsize) { |
| 182 | case 0: |
| 183 | type = _IONBF; |
| 184 | break; |
| 185 | case 1: |
| 186 | type = _IOLBF; |
| 187 | bufsize = BUFSIZ; |
| 188 | break; |
| 189 | default: |
| 190 | type = _IOFBF; |
| 191 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
| 193 | type, bufsize); |
Guido van Rossum | f8b4de0 | 1998-03-06 15:32:40 +0000 | [diff] [blame] | 194 | #else /* !HAVE_SETVBUF */ |
| 195 | if (bufsize <= 1) |
| 196 | setbuf(((PyFileObject *)f)->f_fp, (char *)NULL); |
| 197 | #endif /* !HAVE_SETVBUF */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 201 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 202 | err_closed(void) |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | /* Methods */ |
| 209 | |
| 210 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 211 | file_dealloc(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 213 | if (f->f_fp != NULL && f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 215 | (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 217 | } |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 218 | Py_XDECREF(f->f_name); |
| 219 | Py_XDECREF(f->f_mode); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 220 | f->ob_type->tp_free((PyObject *)f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 224 | file_repr(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 226 | return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", |
| 227 | f->f_fp == NULL ? "closed" : "open", |
| 228 | PyString_AsString(f->f_name), |
| 229 | PyString_AsString(f->f_mode), |
| 230 | f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 233 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 234 | file_close(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 236 | int sts = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | if (f->f_fp != NULL) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 238 | if (f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 239 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 240 | errno = 0; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 241 | sts = (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 242 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 243 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | f->f_fp = NULL; |
| 245 | } |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 246 | if (sts == EOF) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 248 | if (sts != 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 249 | return PyInt_FromLong((long)sts); |
| 250 | Py_INCREF(Py_None); |
| 251 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 255 | /* Our very own off_t-like type, 64-bit if possible */ |
| 256 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 257 | typedef off_t Py_off_t; |
| 258 | #elif SIZEOF_OFF_T >= 8 |
| 259 | typedef off_t Py_off_t; |
| 260 | #elif SIZEOF_FPOS_T >= 8 |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 261 | typedef fpos_t Py_off_t; |
| 262 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 263 | #error "Large file support, but neither off_t nor fpos_t is large enough." |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 264 | #endif |
| 265 | |
| 266 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 267 | /* a portable fseek() function |
| 268 | return 0 on success, non-zero on failure (with errno set) */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 269 | static int |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 270 | _portable_fseek(FILE *fp, Py_off_t offset, int whence) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 272 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 273 | return fseek(fp, offset, whence); |
| 274 | #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8 |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 275 | return fseeko(fp, offset, whence); |
| 276 | #elif defined(HAVE_FSEEK64) |
| 277 | return fseek64(fp, offset, whence); |
Fred Drake | db810ac | 2000-10-06 20:42:33 +0000 | [diff] [blame] | 278 | #elif defined(__BEOS__) |
| 279 | return _fseek(fp, offset, whence); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 280 | #elif SIZEOF_FPOS_T >= 8 |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 281 | /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos() |
| 282 | and fgetpos() to implement fseek()*/ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 283 | fpos_t pos; |
| 284 | switch (whence) { |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 285 | case SEEK_END: |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 286 | #ifdef MS_WINDOWS |
| 287 | fflush(fp); |
| 288 | if (_lseeki64(fileno(fp), 0, 2) == -1) |
| 289 | return -1; |
| 290 | #else |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 291 | if (fseek(fp, 0, SEEK_END) != 0) |
| 292 | return -1; |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 293 | #endif |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 294 | /* fall through */ |
| 295 | case SEEK_CUR: |
| 296 | if (fgetpos(fp, &pos) != 0) |
| 297 | return -1; |
| 298 | offset += pos; |
| 299 | break; |
| 300 | /* case SEEK_SET: break; */ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 301 | } |
| 302 | return fsetpos(fp, &offset); |
| 303 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 304 | #error "Large file support, but no way to fseek." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 305 | #endif |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* a portable ftell() function |
| 310 | Return -1 on failure with errno set appropriately, current file |
| 311 | position on success */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 312 | static Py_off_t |
Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 313 | _portable_ftell(FILE* fp) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 314 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 315 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 316 | return ftell(fp); |
| 317 | #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8 |
| 318 | return ftello(fp); |
| 319 | #elif defined(HAVE_FTELL64) |
| 320 | return ftell64(fp); |
| 321 | #elif SIZEOF_FPOS_T >= 8 |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 322 | fpos_t pos; |
| 323 | if (fgetpos(fp, &pos) != 0) |
| 324 | return -1; |
| 325 | return pos; |
| 326 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 327 | #error "Large file support, but no way to ftell." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 328 | #endif |
| 329 | } |
| 330 | |
| 331 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 333 | file_seek(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 335 | int whence; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 336 | int ret; |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 337 | Py_off_t offset; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 338 | PyObject *offobj; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 340 | if (f->f_fp == NULL) |
| 341 | return err_closed(); |
| 342 | whence = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 343 | if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 344 | return NULL; |
| 345 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 346 | offset = PyInt_AsLong(offobj); |
| 347 | #else |
| 348 | offset = PyLong_Check(offobj) ? |
| 349 | PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj); |
| 350 | #endif |
| 351 | if (PyErr_Occurred()) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 352 | return NULL; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 353 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 355 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 356 | ret = _portable_fseek(f->f_fp, offset, whence); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | Py_END_ALLOW_THREADS |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 359 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 360 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 361 | clearerr(f->f_fp); |
| 362 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 363 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | Py_INCREF(Py_None); |
| 365 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 369 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 370 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 371 | file_truncate(PyFileObject *f, PyObject *args) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 372 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 373 | int ret; |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 374 | Py_off_t newsize; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 375 | PyObject *newsizeobj; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 377 | if (f->f_fp == NULL) |
| 378 | return err_closed(); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 379 | newsizeobj = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 380 | if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj)) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 381 | return NULL; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 382 | if (newsizeobj != NULL) { |
| 383 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 384 | newsize = PyInt_AsLong(newsizeobj); |
| 385 | #else |
| 386 | newsize = PyLong_Check(newsizeobj) ? |
| 387 | PyLong_AsLongLong(newsizeobj) : |
| 388 | PyInt_AsLong(newsizeobj); |
| 389 | #endif |
| 390 | if (PyErr_Occurred()) |
| 391 | return NULL; |
| 392 | } else { |
| 393 | /* Default to current position*/ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 394 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 395 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 396 | newsize = _portable_ftell(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 397 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 398 | if (newsize == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 400 | clearerr(f->f_fp); |
| 401 | return NULL; |
| 402 | } |
| 403 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 404 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 405 | errno = 0; |
| 406 | ret = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | Py_END_ALLOW_THREADS |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 408 | if (ret != 0) goto onioerror; |
| 409 | |
| 410 | #ifdef MS_WIN32 |
| 411 | /* can use _chsize; if, however, the newsize overflows 32-bits then |
| 412 | _chsize is *not* adequate; in this case, an OverflowError is raised */ |
| 413 | if (newsize > LONG_MAX) { |
| 414 | PyErr_SetString(PyExc_OverflowError, |
| 415 | "the new size is too long for _chsize (it is limited to 32-bit values)"); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 416 | return NULL; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 417 | } else { |
| 418 | Py_BEGIN_ALLOW_THREADS |
| 419 | errno = 0; |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 420 | ret = _chsize(fileno(f->f_fp), (long)newsize); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 421 | Py_END_ALLOW_THREADS |
| 422 | if (ret != 0) goto onioerror; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 423 | } |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 424 | #else |
| 425 | Py_BEGIN_ALLOW_THREADS |
| 426 | errno = 0; |
| 427 | ret = ftruncate(fileno(f->f_fp), newsize); |
| 428 | Py_END_ALLOW_THREADS |
| 429 | if (ret != 0) goto onioerror; |
| 430 | #endif /* !MS_WIN32 */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 431 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 432 | Py_INCREF(Py_None); |
| 433 | return Py_None; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 434 | |
| 435 | onioerror: |
| 436 | PyErr_SetFromErrno(PyExc_IOError); |
| 437 | clearerr(f->f_fp); |
| 438 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 439 | } |
| 440 | #endif /* HAVE_FTRUNCATE */ |
| 441 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 442 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 443 | file_tell(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 444 | { |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 445 | Py_off_t pos; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 447 | if (f->f_fp == NULL) |
| 448 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 449 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 450 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 451 | pos = _portable_ftell(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 452 | Py_END_ALLOW_THREADS |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 453 | if (pos == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 454 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 455 | clearerr(f->f_fp); |
| 456 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 457 | } |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 458 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 459 | return PyInt_FromLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 460 | #else |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 461 | return PyLong_FromLongLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 462 | #endif |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 465 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 466 | file_fileno(PyFileObject *f) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 467 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 468 | if (f->f_fp == NULL) |
| 469 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | return PyInt_FromLong((long) fileno(f->f_fp)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 473 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 474 | file_flush(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 475 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 476 | int res; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 478 | if (f->f_fp == NULL) |
| 479 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 481 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 482 | res = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 483 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 484 | if (res != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 486 | clearerr(f->f_fp); |
| 487 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 488 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 489 | Py_INCREF(Py_None); |
| 490 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 494 | file_isatty(PyFileObject *f) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 495 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 496 | long res; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 497 | if (f->f_fp == NULL) |
| 498 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 499 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 500 | res = isatty((int)fileno(f->f_fp)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 501 | Py_END_ALLOW_THREADS |
| 502 | return PyInt_FromLong(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 506 | #if BUFSIZ < 8192 |
| 507 | #define SMALLCHUNK 8192 |
| 508 | #else |
| 509 | #define SMALLCHUNK BUFSIZ |
| 510 | #endif |
| 511 | |
Guido van Rossum | 3c25904 | 1999-01-14 19:00:14 +0000 | [diff] [blame] | 512 | #if SIZEOF_INT < 4 |
| 513 | #define BIGCHUNK (512 * 32) |
| 514 | #else |
| 515 | #define BIGCHUNK (512 * 1024) |
| 516 | #endif |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 517 | |
| 518 | static size_t |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 519 | new_buffersize(PyFileObject *f, size_t currentsize) |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 520 | { |
| 521 | #ifdef HAVE_FSTAT |
Fred Drake | 1bc8fab | 2001-07-19 21:49:38 +0000 | [diff] [blame] | 522 | off_t pos, end; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 523 | struct stat st; |
| 524 | if (fstat(fileno(f->f_fp), &st) == 0) { |
| 525 | end = st.st_size; |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 526 | /* The following is not a bug: we really need to call lseek() |
| 527 | *and* ftell(). The reason is that some stdio libraries |
| 528 | mistakenly flush their buffer when ftell() is called and |
| 529 | the lseek() call it makes fails, thereby throwing away |
| 530 | data that cannot be recovered in any way. To avoid this, |
| 531 | we first test lseek(), and only call ftell() if lseek() |
| 532 | works. We can't use the lseek() value either, because we |
| 533 | need to take the amount of buffered data into account. |
| 534 | (Yet another reason why stdio stinks. :-) */ |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 535 | #ifdef USE_GUSI2 |
| 536 | pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR); |
| 537 | pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR); |
| 538 | #else |
Guido van Rossum | 91aaa92 | 1998-05-05 22:21:35 +0000 | [diff] [blame] | 539 | pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 540 | #endif |
| 541 | if (pos >= 0) { |
Guido van Rossum | 91aaa92 | 1998-05-05 22:21:35 +0000 | [diff] [blame] | 542 | pos = ftell(f->f_fp); |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 543 | } |
Guido van Rossum | d30dc0a | 1998-04-27 19:01:08 +0000 | [diff] [blame] | 544 | if (pos < 0) |
| 545 | clearerr(f->f_fp); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 546 | if (end > pos && pos >= 0) |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 547 | return currentsize + end - pos + 1; |
Guido van Rossum | dcb5e7f | 1998-03-03 22:36:10 +0000 | [diff] [blame] | 548 | /* 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] | 549 | } |
| 550 | #endif |
| 551 | if (currentsize > SMALLCHUNK) { |
| 552 | /* Keep doubling until we reach BIGCHUNK; |
| 553 | then keep adding BIGCHUNK. */ |
| 554 | if (currentsize <= BIGCHUNK) |
| 555 | return currentsize + currentsize; |
| 556 | else |
| 557 | return currentsize + BIGCHUNK; |
| 558 | } |
| 559 | return currentsize + SMALLCHUNK; |
| 560 | } |
| 561 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 562 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 563 | file_read(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 564 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 565 | long bytesrequested = -1; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 566 | size_t bytesread, buffersize, chunksize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 567 | PyObject *v; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 568 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 569 | if (f->f_fp == NULL) |
| 570 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 571 | if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 572 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 573 | if (bytesrequested < 0) |
Guido van Rossum | ff1ccbf | 1999-04-10 15:48:23 +0000 | [diff] [blame] | 574 | buffersize = new_buffersize(f, (size_t)0); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 575 | else |
| 576 | buffersize = bytesrequested; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 577 | if (buffersize > INT_MAX) { |
| 578 | PyErr_SetString(PyExc_OverflowError, |
| 579 | "requested number of bytes is more than a Python string can hold"); |
| 580 | return NULL; |
| 581 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 582 | v = PyString_FromStringAndSize((char *)NULL, buffersize); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 583 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 584 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 585 | bytesread = 0; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 586 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 587 | Py_BEGIN_ALLOW_THREADS |
| 588 | errno = 0; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 589 | chunksize = fread(BUF(v) + bytesread, 1, |
| 590 | buffersize - bytesread, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 591 | Py_END_ALLOW_THREADS |
| 592 | if (chunksize == 0) { |
| 593 | if (!ferror(f->f_fp)) |
| 594 | break; |
| 595 | PyErr_SetFromErrno(PyExc_IOError); |
| 596 | clearerr(f->f_fp); |
| 597 | Py_DECREF(v); |
| 598 | return NULL; |
| 599 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 600 | bytesread += chunksize; |
| 601 | if (bytesread < buffersize) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 602 | break; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 603 | if (bytesrequested < 0) { |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 604 | buffersize = new_buffersize(f, buffersize); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 605 | if (_PyString_Resize(&v, buffersize) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | } |
| 608 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 609 | if (bytesread != buffersize) |
| 610 | _PyString_Resize(&v, bytesread); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 611 | return v; |
| 612 | } |
| 613 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 614 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 615 | file_readinto(PyFileObject *f, PyObject *args) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 616 | { |
| 617 | char *ptr; |
Guido van Rossum | 00ebd46 | 2001-10-23 21:25:24 +0000 | [diff] [blame] | 618 | int ntodo; |
| 619 | size_t ndone, nnow; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 621 | if (f->f_fp == NULL) |
| 622 | return err_closed(); |
| 623 | if (!PyArg_Parse(args, "w#", &ptr, &ntodo)) |
| 624 | return NULL; |
| 625 | ndone = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 626 | while (ntodo > 0) { |
| 627 | Py_BEGIN_ALLOW_THREADS |
| 628 | errno = 0; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 629 | nnow = fread(ptr+ndone, 1, ntodo, f->f_fp); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 630 | Py_END_ALLOW_THREADS |
| 631 | if (nnow == 0) { |
| 632 | if (!ferror(f->f_fp)) |
| 633 | break; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 634 | PyErr_SetFromErrno(PyExc_IOError); |
| 635 | clearerr(f->f_fp); |
| 636 | return NULL; |
| 637 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 638 | ndone += nnow; |
| 639 | ntodo -= nnow; |
| 640 | } |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 641 | return PyInt_FromLong((long)ndone); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 644 | /************************************************************************** |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 645 | Routine to get next line using platform fgets(). |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 646 | |
| 647 | Under MSVC 6: |
| 648 | |
Tim Peters | 1c73323 | 2001-01-08 04:02:07 +0000 | [diff] [blame] | 649 | + MS threadsafe getc is very slow (multiple layers of function calls before+ |
| 650 | after each character, to lock+unlock the stream). |
| 651 | + The stream-locking functions are MS-internal -- can't access them from user |
| 652 | code. |
| 653 | + There's nothing Tim could find in the MS C or platform SDK libraries that |
| 654 | can worm around this. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 655 | + MS fgets locks/unlocks only once per line; it's the only hook we have. |
| 656 | |
| 657 | So we use fgets for speed(!), despite that it's painful. |
| 658 | |
| 659 | MS realloc is also slow. |
| 660 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 661 | Reports from other platforms on this method vs getc_unlocked (which MS doesn't |
| 662 | have): |
| 663 | Linux a wash |
| 664 | Solaris a wash |
| 665 | Tru64 Unix getline_via_fgets significantly faster |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 666 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 667 | CAUTION: The C std isn't clear about this: in those cases where fgets |
| 668 | writes something into the buffer, can it write into any position beyond the |
| 669 | required trailing null byte? MSVC 6 fgets does not, and no platform is (yet) |
| 670 | known on which it does; and it would be a strange way to code fgets. Still, |
| 671 | getline_via_fgets may not work correctly if it does. The std test |
| 672 | test_bufio.py should fail if platform fgets() routinely writes beyond the |
| 673 | trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 674 | **************************************************************************/ |
| 675 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 676 | /* Use this routine if told to, or by default on non-get_unlocked() |
| 677 | * platforms unless told not to. Yikes! Let's spell that out: |
| 678 | * On a platform with getc_unlocked(): |
| 679 | * By default, use getc_unlocked(). |
| 680 | * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE. |
| 681 | * On a platform without getc_unlocked(): |
| 682 | * By default, use fgets(). |
| 683 | * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE. |
| 684 | */ |
| 685 | #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED) |
| 686 | #define USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 687 | #endif |
| 688 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 689 | #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE) |
| 690 | #undef USE_FGETS_IN_GETLINE |
| 691 | #endif |
| 692 | |
| 693 | #ifdef USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 694 | static PyObject* |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 695 | getline_via_fgets(FILE *fp) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 696 | { |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 697 | /* INITBUFSIZE is the maximum line length that lets us get away with the fast |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 698 | * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have |
| 699 | * to fill this much of the buffer with a known value in order to figure out |
| 700 | * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger |
| 701 | * than "most" lines, we waste time filling unused buffer slots. 100 is |
| 702 | * surely adequate for most peoples' email archives, chewing over source code, |
| 703 | * etc -- "regular old text files". |
| 704 | * MAXBUFSIZE is the maximum line length that lets us get away with the less |
| 705 | * fast (but still zippy) no-realloc, two-fgets()-call path. See above for |
| 706 | * cautions about boosting that. 300 was chosen because the worst real-life |
| 707 | * text-crunching job reported on Python-Dev was a mail-log crawler where over |
| 708 | * half the lines were 254 chars. |
| 709 | * INCBUFSIZE is the amount by which we grow the buffer, if MAXBUFSIZE isn't |
| 710 | * enough. It doesn't much matter what this is set to: we only get here for |
| 711 | * absurdly long lines anyway. |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 712 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 713 | #define INITBUFSIZE 100 |
| 714 | #define MAXBUFSIZE 300 |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 715 | #define INCBUFSIZE 1000 |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 716 | char* p; /* temp */ |
| 717 | char buf[MAXBUFSIZE]; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 718 | PyObject* v; /* the string object result */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 719 | char* pvfree; /* address of next free slot */ |
| 720 | char* pvend; /* address one beyond last free slot */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 721 | size_t nfree; /* # of free buffer slots; pvend-pvfree */ |
| 722 | size_t total_v_size; /* total # of slots in buffer */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 723 | |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 724 | /* Optimize for normal case: avoid _PyString_Resize if at all |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 725 | * possible via first reading into stack buffer "buf". |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 726 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 727 | total_v_size = INITBUFSIZE; /* start small and pray */ |
| 728 | pvfree = buf; |
| 729 | for (;;) { |
| 730 | Py_BEGIN_ALLOW_THREADS |
| 731 | pvend = buf + total_v_size; |
| 732 | nfree = pvend - pvfree; |
| 733 | memset(pvfree, '\n', nfree); |
| 734 | p = fgets(pvfree, nfree, fp); |
| 735 | Py_END_ALLOW_THREADS |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 736 | |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 737 | if (p == NULL) { |
| 738 | clearerr(fp); |
| 739 | if (PyErr_CheckSignals()) |
| 740 | return NULL; |
| 741 | v = PyString_FromStringAndSize(buf, pvfree - buf); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 742 | return v; |
| 743 | } |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 744 | /* fgets read *something* */ |
| 745 | p = memchr(pvfree, '\n', nfree); |
| 746 | if (p != NULL) { |
| 747 | /* Did the \n come from fgets or from us? |
| 748 | * Since fgets stops at the first \n, and then writes |
| 749 | * \0, if it's from fgets a \0 must be next. But if |
| 750 | * that's so, it could not have come from us, since |
| 751 | * the \n's we filled the buffer with have only more |
| 752 | * \n's to the right. |
| 753 | */ |
| 754 | if (p+1 < pvend && *(p+1) == '\0') { |
| 755 | /* It's from fgets: we win! In particular, |
| 756 | * we haven't done any mallocs yet, and can |
| 757 | * build the final result on the first try. |
| 758 | */ |
| 759 | ++p; /* include \n from fgets */ |
| 760 | } |
| 761 | else { |
| 762 | /* Must be from us: fgets didn't fill the |
| 763 | * buffer and didn't find a newline, so it |
| 764 | * must be the last and newline-free line of |
| 765 | * the file. |
| 766 | */ |
| 767 | assert(p > pvfree && *(p-1) == '\0'); |
| 768 | --p; /* don't include \0 from fgets */ |
| 769 | } |
| 770 | v = PyString_FromStringAndSize(buf, p - buf); |
| 771 | return v; |
| 772 | } |
| 773 | /* yuck: fgets overwrote all the newlines, i.e. the entire |
| 774 | * buffer. So this line isn't over yet, or maybe it is but |
| 775 | * we're exactly at EOF. If we haven't already, try using the |
| 776 | * rest of the stack buffer. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 777 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 778 | assert(*(pvend-1) == '\0'); |
| 779 | if (pvfree == buf) { |
| 780 | pvfree = pvend - 1; /* overwrite trailing null */ |
| 781 | total_v_size = MAXBUFSIZE; |
| 782 | } |
| 783 | else |
| 784 | break; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 785 | } |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 786 | |
| 787 | /* The stack buffer isn't big enough; malloc a string object and read |
| 788 | * into its buffer. |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 789 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 790 | total_v_size = MAXBUFSIZE + INCBUFSIZE; |
Tim Peters | 1c73323 | 2001-01-08 04:02:07 +0000 | [diff] [blame] | 791 | v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size); |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 792 | if (v == NULL) |
| 793 | return v; |
| 794 | /* copy over everything except the last null byte */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 795 | memcpy(BUF(v), buf, MAXBUFSIZE-1); |
| 796 | pvfree = BUF(v) + MAXBUFSIZE - 1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 797 | |
| 798 | /* Keep reading stuff into v; if it ever ends successfully, break |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 799 | * after setting p one beyond the end of the line. The code here is |
| 800 | * very much like the code above, except reads into v's buffer; see |
| 801 | * the code above for detailed comments about the logic. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 802 | */ |
| 803 | for (;;) { |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 804 | Py_BEGIN_ALLOW_THREADS |
| 805 | pvend = BUF(v) + total_v_size; |
| 806 | nfree = pvend - pvfree; |
| 807 | memset(pvfree, '\n', nfree); |
| 808 | p = fgets(pvfree, nfree, fp); |
| 809 | Py_END_ALLOW_THREADS |
| 810 | |
| 811 | if (p == NULL) { |
| 812 | clearerr(fp); |
| 813 | if (PyErr_CheckSignals()) { |
| 814 | Py_DECREF(v); |
| 815 | return NULL; |
| 816 | } |
| 817 | p = pvfree; |
| 818 | break; |
| 819 | } |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 820 | p = memchr(pvfree, '\n', nfree); |
| 821 | if (p != NULL) { |
| 822 | if (p+1 < pvend && *(p+1) == '\0') { |
| 823 | /* \n came from fgets */ |
| 824 | ++p; |
| 825 | break; |
| 826 | } |
| 827 | /* \n came from us; last line of file, no newline */ |
| 828 | assert(p > pvfree && *(p-1) == '\0'); |
| 829 | --p; |
| 830 | break; |
| 831 | } |
| 832 | /* expand buffer and try again */ |
| 833 | assert(*(pvend-1) == '\0'); |
| 834 | total_v_size += INCBUFSIZE; |
| 835 | if (total_v_size > INT_MAX) { |
| 836 | PyErr_SetString(PyExc_OverflowError, |
| 837 | "line is longer than a Python string can hold"); |
| 838 | Py_DECREF(v); |
| 839 | return NULL; |
| 840 | } |
| 841 | if (_PyString_Resize(&v, (int)total_v_size) < 0) |
| 842 | return NULL; |
| 843 | /* overwrite the trailing null byte */ |
| 844 | pvfree = BUF(v) + (total_v_size - INCBUFSIZE - 1); |
| 845 | } |
| 846 | if (BUF(v) + total_v_size != p) |
| 847 | _PyString_Resize(&v, p - BUF(v)); |
| 848 | return v; |
| 849 | #undef INITBUFSIZE |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 850 | #undef MAXBUFSIZE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 851 | #undef INCBUFSIZE |
| 852 | } |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 853 | #endif /* ifdef USE_FGETS_IN_GETLINE */ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 854 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 855 | /* Internal routine to get a line. |
| 856 | Size argument interpretation: |
| 857 | > 0: max length; |
Guido van Rossum | 8628206 | 2001-01-08 01:26:47 +0000 | [diff] [blame] | 858 | <= 0: read arbitrary line |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 859 | */ |
| 860 | |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 861 | #ifdef HAVE_GETC_UNLOCKED |
| 862 | #define GETC(f) getc_unlocked(f) |
| 863 | #define FLOCKFILE(f) flockfile(f) |
| 864 | #define FUNLOCKFILE(f) funlockfile(f) |
| 865 | #else |
| 866 | #define GETC(f) getc(f) |
| 867 | #define FLOCKFILE(f) |
| 868 | #define FUNLOCKFILE(f) |
| 869 | #endif |
| 870 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 871 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 872 | get_line(PyFileObject *f, int n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 873 | { |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 874 | FILE *fp = f->f_fp; |
| 875 | int c; |
Andrew M. Kuchling | 4b2b445 | 2000-11-29 02:53:22 +0000 | [diff] [blame] | 876 | char *buf, *end; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 877 | size_t n1, n2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 878 | PyObject *v; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 879 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 880 | #ifdef USE_FGETS_IN_GETLINE |
Guido van Rossum | 8628206 | 2001-01-08 01:26:47 +0000 | [diff] [blame] | 881 | if (n <= 0) |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 882 | return getline_via_fgets(fp); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 883 | #endif |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 884 | n2 = n > 0 ? n : 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | v = PyString_FromStringAndSize((char *)NULL, n2); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 886 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 887 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 888 | buf = BUF(v); |
| 889 | end = buf + n2; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 890 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 891 | for (;;) { |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 892 | Py_BEGIN_ALLOW_THREADS |
| 893 | FLOCKFILE(fp); |
| 894 | while ((c = GETC(fp)) != EOF && |
| 895 | (*buf++ = c) != '\n' && |
| 896 | buf != end) |
| 897 | ; |
| 898 | FUNLOCKFILE(fp); |
| 899 | Py_END_ALLOW_THREADS |
| 900 | if (c == '\n') |
| 901 | break; |
| 902 | if (c == EOF) { |
Guido van Rossum | 29206bc | 2001-08-09 18:14:59 +0000 | [diff] [blame] | 903 | if (ferror(fp)) { |
| 904 | PyErr_SetFromErrno(PyExc_IOError); |
| 905 | clearerr(fp); |
| 906 | Py_DECREF(v); |
| 907 | return NULL; |
| 908 | } |
Guido van Rossum | 76ad8ed | 1991-06-03 10:54:55 +0000 | [diff] [blame] | 909 | clearerr(fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 910 | if (PyErr_CheckSignals()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 911 | Py_DECREF(v); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 912 | return NULL; |
| 913 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 914 | break; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 915 | } |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 916 | /* Must be because buf == end */ |
| 917 | if (n > 0) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 918 | break; |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 919 | n1 = n2; |
| 920 | n2 += 1000; |
| 921 | if (n2 > INT_MAX) { |
| 922 | PyErr_SetString(PyExc_OverflowError, |
| 923 | "line is longer than a Python string can hold"); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 924 | Py_DECREF(v); |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 925 | return NULL; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 926 | } |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 927 | if (_PyString_Resize(&v, n2) < 0) |
| 928 | return NULL; |
| 929 | buf = BUF(v) + n1; |
| 930 | end = BUF(v) + n2; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 931 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 932 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 933 | n1 = buf - BUF(v); |
| 934 | if (n1 != n2) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 935 | _PyString_Resize(&v, n1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 936 | return v; |
| 937 | } |
| 938 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 939 | /* External C interface */ |
| 940 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 941 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 942 | PyFile_GetLine(PyObject *f, int n) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 943 | { |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 944 | PyObject *result; |
| 945 | |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 946 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | PyErr_BadInternalCall(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 948 | return NULL; |
| 949 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 950 | |
| 951 | if (PyFile_Check(f)) { |
| 952 | if (((PyFileObject*)f)->f_fp == NULL) |
| 953 | return err_closed(); |
| 954 | result = get_line((PyFileObject *)f, n); |
| 955 | } |
| 956 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 957 | PyObject *reader; |
| 958 | PyObject *args; |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 960 | reader = PyObject_GetAttrString(f, "readline"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 961 | if (reader == NULL) |
| 962 | return NULL; |
| 963 | if (n <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 964 | args = Py_BuildValue("()"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 965 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 966 | args = Py_BuildValue("(i)", n); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 967 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 968 | Py_DECREF(reader); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 969 | return NULL; |
| 970 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 971 | result = PyEval_CallObject(reader, args); |
| 972 | Py_DECREF(reader); |
| 973 | Py_DECREF(args); |
| 974 | if (result != NULL && !PyString_Check(result)) { |
| 975 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 976 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 978 | "object.readline() returned non-string"); |
| 979 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | if (n < 0 && result != NULL && PyString_Check(result)) { |
| 983 | char *s = PyString_AS_STRING(result); |
| 984 | int len = PyString_GET_SIZE(result); |
| 985 | if (len == 0) { |
| 986 | Py_DECREF(result); |
| 987 | result = NULL; |
| 988 | PyErr_SetString(PyExc_EOFError, |
| 989 | "EOF when reading a line"); |
| 990 | } |
| 991 | else if (s[len-1] == '\n') { |
| 992 | if (result->ob_refcnt == 1) |
| 993 | _PyString_Resize(&result, len-1); |
| 994 | else { |
| 995 | PyObject *v; |
| 996 | v = PyString_FromStringAndSize(s, len-1); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 997 | Py_DECREF(result); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 998 | result = v; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1001 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1002 | return result; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* Python method */ |
| 1006 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1007 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1008 | file_readline(PyFileObject *f, PyObject *args) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1009 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1010 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1011 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1012 | if (f->f_fp == NULL) |
| 1013 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1014 | if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1015 | return NULL; |
| 1016 | if (n == 0) |
| 1017 | return PyString_FromString(""); |
| 1018 | if (n < 0) |
| 1019 | n = 0; |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1020 | return get_line(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1023 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1024 | file_xreadlines(PyFileObject *f) |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1025 | { |
| 1026 | static PyObject* xreadlines_function = NULL; |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1027 | |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1028 | if (!xreadlines_function) { |
| 1029 | PyObject *xreadlines_module = |
| 1030 | PyImport_ImportModule("xreadlines"); |
| 1031 | if(!xreadlines_module) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | xreadlines_function = PyObject_GetAttrString(xreadlines_module, |
| 1035 | "xreadlines"); |
| 1036 | Py_DECREF(xreadlines_module); |
| 1037 | if(!xreadlines_function) |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | return PyObject_CallFunction(xreadlines_function, "(O)", f); |
| 1041 | } |
| 1042 | |
| 1043 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1044 | file_readlines(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1045 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1046 | long sizehint = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1047 | PyObject *list; |
| 1048 | PyObject *line; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1049 | char small_buffer[SMALLCHUNK]; |
| 1050 | char *buffer = small_buffer; |
| 1051 | size_t buffersize = SMALLCHUNK; |
| 1052 | PyObject *big_buffer = NULL; |
| 1053 | size_t nfilled = 0; |
| 1054 | size_t nread; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1055 | size_t totalread = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1056 | char *p, *q, *end; |
| 1057 | int err; |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1058 | int shortread = 0; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1059 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1060 | if (f->f_fp == NULL) |
| 1061 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1062 | if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1063 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1064 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1065 | return NULL; |
| 1066 | for (;;) { |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1067 | if (shortread) |
| 1068 | nread = 0; |
| 1069 | else { |
| 1070 | Py_BEGIN_ALLOW_THREADS |
| 1071 | errno = 0; |
| 1072 | nread = fread(buffer+nfilled, 1, |
| 1073 | buffersize-nfilled, f->f_fp); |
| 1074 | Py_END_ALLOW_THREADS |
| 1075 | shortread = (nread < buffersize-nfilled); |
| 1076 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1077 | if (nread == 0) { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1078 | sizehint = 0; |
Guido van Rossum | 3da3fce | 1998-02-19 20:46:48 +0000 | [diff] [blame] | 1079 | if (!ferror(f->f_fp)) |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | PyErr_SetFromErrno(PyExc_IOError); |
| 1082 | clearerr(f->f_fp); |
| 1083 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1084 | Py_DECREF(list); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1085 | list = NULL; |
| 1086 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1087 | } |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1088 | totalread += nread; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1089 | p = memchr(buffer+nfilled, '\n', nread); |
| 1090 | if (p == NULL) { |
| 1091 | /* Need a larger buffer to fit this line */ |
| 1092 | nfilled += nread; |
| 1093 | buffersize *= 2; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1094 | if (buffersize > INT_MAX) { |
| 1095 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1096 | "line is longer than a Python string can hold"); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1097 | goto error; |
| 1098 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1099 | if (big_buffer == NULL) { |
| 1100 | /* Create the big buffer */ |
| 1101 | big_buffer = PyString_FromStringAndSize( |
| 1102 | NULL, buffersize); |
| 1103 | if (big_buffer == NULL) |
| 1104 | goto error; |
| 1105 | buffer = PyString_AS_STRING(big_buffer); |
| 1106 | memcpy(buffer, small_buffer, nfilled); |
| 1107 | } |
| 1108 | else { |
| 1109 | /* Grow the big buffer */ |
| 1110 | _PyString_Resize(&big_buffer, buffersize); |
| 1111 | buffer = PyString_AS_STRING(big_buffer); |
| 1112 | } |
| 1113 | continue; |
| 1114 | } |
| 1115 | end = buffer+nfilled+nread; |
| 1116 | q = buffer; |
| 1117 | do { |
| 1118 | /* Process complete lines */ |
| 1119 | p++; |
| 1120 | line = PyString_FromStringAndSize(q, p-q); |
| 1121 | if (line == NULL) |
| 1122 | goto error; |
| 1123 | err = PyList_Append(list, line); |
| 1124 | Py_DECREF(line); |
| 1125 | if (err != 0) |
| 1126 | goto error; |
| 1127 | q = p; |
| 1128 | p = memchr(q, '\n', end-q); |
| 1129 | } while (p != NULL); |
| 1130 | /* Move the remaining incomplete line to the start */ |
| 1131 | nfilled = end-q; |
| 1132 | memmove(buffer, q, nfilled); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1133 | if (sizehint > 0) |
| 1134 | if (totalread >= (size_t)sizehint) |
| 1135 | break; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1136 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1137 | if (nfilled != 0) { |
| 1138 | /* Partial last line */ |
| 1139 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 1140 | if (line == NULL) |
| 1141 | goto error; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1142 | if (sizehint > 0) { |
| 1143 | /* Need to complete the last line */ |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1144 | PyObject *rest = get_line(f, 0); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1145 | if (rest == NULL) { |
| 1146 | Py_DECREF(line); |
| 1147 | goto error; |
| 1148 | } |
| 1149 | PyString_Concat(&line, rest); |
| 1150 | Py_DECREF(rest); |
| 1151 | if (line == NULL) |
| 1152 | goto error; |
| 1153 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1154 | err = PyList_Append(list, line); |
| 1155 | Py_DECREF(line); |
| 1156 | if (err != 0) |
| 1157 | goto error; |
| 1158 | } |
| 1159 | cleanup: |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 1160 | if (big_buffer) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1161 | Py_DECREF(big_buffer); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 1162 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1163 | return list; |
| 1164 | } |
| 1165 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1166 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1167 | file_write(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1168 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1169 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1170 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1171 | if (f->f_fp == NULL) |
| 1172 | return err_closed(); |
Michael W. Hudson | e2ec3eb | 2001-10-31 18:51:01 +0000 | [diff] [blame] | 1173 | if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1174 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1175 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1176 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1177 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1178 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1179 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1180 | if (n2 != n) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1181 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 1182 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | return NULL; |
| 1184 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1185 | Py_INCREF(Py_None); |
| 1186 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1189 | static PyObject * |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1190 | file_writelines(PyFileObject *f, PyObject *seq) |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1191 | { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1192 | #define CHUNKSIZE 1000 |
| 1193 | PyObject *list, *line; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1194 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1195 | PyObject *result; |
| 1196 | int i, j, index, len, nwritten, islist; |
| 1197 | |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1198 | assert(seq != NULL); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1199 | if (f->f_fp == NULL) |
| 1200 | return err_closed(); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1201 | |
| 1202 | result = NULL; |
| 1203 | list = NULL; |
| 1204 | islist = PyList_Check(seq); |
| 1205 | if (islist) |
| 1206 | it = NULL; |
| 1207 | else { |
| 1208 | it = PyObject_GetIter(seq); |
| 1209 | if (it == NULL) { |
| 1210 | PyErr_SetString(PyExc_TypeError, |
| 1211 | "writelines() requires an iterable argument"); |
| 1212 | return NULL; |
| 1213 | } |
| 1214 | /* From here on, fail by going to error, to reclaim "it". */ |
| 1215 | list = PyList_New(CHUNKSIZE); |
| 1216 | if (list == NULL) |
| 1217 | goto error; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1218 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* Strategy: slurp CHUNKSIZE lines into a private list, |
| 1221 | checking that they are all strings, then write that list |
| 1222 | without holding the interpreter lock, then come back for more. */ |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1223 | for (index = 0; ; index += CHUNKSIZE) { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1224 | if (islist) { |
| 1225 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1226 | list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1227 | if (list == NULL) |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1228 | goto error; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1229 | j = PyList_GET_SIZE(list); |
| 1230 | } |
| 1231 | else { |
| 1232 | for (j = 0; j < CHUNKSIZE; j++) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1233 | line = PyIter_Next(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1234 | if (line == NULL) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1235 | if (PyErr_Occurred()) |
| 1236 | goto error; |
| 1237 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1238 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1239 | PyList_SetItem(list, j, line); |
| 1240 | } |
| 1241 | } |
| 1242 | if (j == 0) |
| 1243 | break; |
| 1244 | |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1245 | /* Check that all entries are indeed strings. If not, |
| 1246 | apply the same rules as for file.write() and |
| 1247 | convert the results to strings. This is slow, but |
| 1248 | seems to be the only way since all conversion APIs |
| 1249 | could potentially execute Python code. */ |
| 1250 | for (i = 0; i < j; i++) { |
| 1251 | PyObject *v = PyList_GET_ITEM(list, i); |
| 1252 | if (!PyString_Check(v)) { |
| 1253 | const char *buffer; |
| 1254 | int len; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1255 | if (((f->f_binary && |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1256 | PyObject_AsReadBuffer(v, |
| 1257 | (const void**)&buffer, |
| 1258 | &len)) || |
| 1259 | PyObject_AsCharBuffer(v, |
| 1260 | &buffer, |
| 1261 | &len))) { |
| 1262 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1263 | "writelines() argument must be a sequence of strings"); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1264 | goto error; |
| 1265 | } |
| 1266 | line = PyString_FromStringAndSize(buffer, |
| 1267 | len); |
| 1268 | if (line == NULL) |
| 1269 | goto error; |
| 1270 | Py_DECREF(v); |
Marc-André Lemburg | f5e96fa | 2000-08-25 22:49:05 +0000 | [diff] [blame] | 1271 | PyList_SET_ITEM(list, i, line); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* Since we are releasing the global lock, the |
| 1276 | following code may *not* execute Python code. */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1277 | Py_BEGIN_ALLOW_THREADS |
| 1278 | f->f_softspace = 0; |
| 1279 | errno = 0; |
| 1280 | for (i = 0; i < j; i++) { |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1281 | line = PyList_GET_ITEM(list, i); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1282 | len = PyString_GET_SIZE(line); |
| 1283 | nwritten = fwrite(PyString_AS_STRING(line), |
| 1284 | 1, len, f->f_fp); |
| 1285 | if (nwritten != len) { |
| 1286 | Py_BLOCK_THREADS |
| 1287 | PyErr_SetFromErrno(PyExc_IOError); |
| 1288 | clearerr(f->f_fp); |
| 1289 | goto error; |
| 1290 | } |
| 1291 | } |
| 1292 | Py_END_ALLOW_THREADS |
| 1293 | |
| 1294 | if (j < CHUNKSIZE) |
| 1295 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1298 | Py_INCREF(Py_None); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1299 | result = Py_None; |
| 1300 | error: |
| 1301 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1302 | Py_XDECREF(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1303 | return result; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1304 | #undef CHUNKSIZE |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1307 | static char readline_doc[] = |
| 1308 | "readline([size]) -> next line from the file, as a string.\n" |
| 1309 | "\n" |
| 1310 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 1311 | "number of bytes to return (an incomplete line may be returned then).\n" |
| 1312 | "Return an empty string at EOF."; |
| 1313 | |
| 1314 | static char read_doc[] = |
| 1315 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 1316 | "\n" |
| 1317 | "If the size argument is negative or omitted, read until EOF is reached."; |
| 1318 | |
| 1319 | static char write_doc[] = |
| 1320 | "write(str) -> None. Write string str to file.\n" |
| 1321 | "\n" |
| 1322 | "Note that due to buffering, flush() or close() may be needed before\n" |
| 1323 | "the file on disk reflects the data written."; |
| 1324 | |
| 1325 | static char fileno_doc[] = |
| 1326 | "fileno() -> integer \"file descriptor\".\n" |
| 1327 | "\n" |
| 1328 | "This is needed for lower-level file interfaces, such os.read()."; |
| 1329 | |
| 1330 | static char seek_doc[] = |
| 1331 | "seek(offset[, whence]) -> None. Move to new file position.\n" |
| 1332 | "\n" |
| 1333 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 1334 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 1335 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 1336 | "relative to end of file, usually negative, although many platforms allow\n" |
| 1337 | "seeking beyond the end of a file).\n" |
| 1338 | "\n" |
| 1339 | "Note that not all file objects are seekable."; |
| 1340 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1341 | #ifdef HAVE_FTRUNCATE |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1342 | static char truncate_doc[] = |
| 1343 | "truncate([size]) -> None. Truncate the file to at most size bytes.\n" |
| 1344 | "\n" |
| 1345 | "Size defaults to the current file position, as returned by tell()."; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1346 | #endif |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1347 | |
| 1348 | static char tell_doc[] = |
| 1349 | "tell() -> current file position, an integer (may be a long integer)."; |
| 1350 | |
| 1351 | static char readinto_doc[] = |
| 1352 | "readinto() -> Undocumented. Don't use this; it may go away."; |
| 1353 | |
| 1354 | static char readlines_doc[] = |
| 1355 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 1356 | "\n" |
| 1357 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 1358 | "The optional size argument, if given, is an approximate bound on the\n" |
| 1359 | "total number of bytes in the lines returned."; |
| 1360 | |
| 1361 | static char xreadlines_doc[] = |
| 1362 | "xreadlines() -> next line from the file, as a string.\n" |
| 1363 | "\n" |
| 1364 | "Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n" |
| 1365 | "often quicker, due to reading ahead internally."; |
| 1366 | |
| 1367 | static char writelines_doc[] = |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1368 | "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1369 | "\n" |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1370 | "Note that newlines are not added. The sequence can be any iterable object\n" |
| 1371 | "producing strings. This is equivalent to calling write() for each string."; |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1372 | |
| 1373 | static char flush_doc[] = |
| 1374 | "flush() -> None. Flush the internal I/O buffer."; |
| 1375 | |
| 1376 | static char close_doc[] = |
| 1377 | "close() -> None or (perhaps) an integer. Close the file.\n" |
| 1378 | "\n" |
| 1379 | "Sets data attribute .closed to true. A closed file cannot be used for\n" |
| 1380 | "further I/O operations. close() may be called more than once without\n" |
| 1381 | "error. Some kinds of file objects (for example, opened by popen())\n" |
| 1382 | "may return an exit status upon closing."; |
| 1383 | |
| 1384 | static char isatty_doc[] = |
| 1385 | "isatty() -> true or false. True if the file is connected to a tty device."; |
| 1386 | |
| 1387 | static PyMethodDef file_methods[] = { |
| 1388 | {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
| 1389 | {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, |
Michael W. Hudson | e2ec3eb | 2001-10-31 18:51:01 +0000 | [diff] [blame] | 1390 | {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1391 | {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, |
| 1392 | {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, |
| 1393 | #ifdef HAVE_FTRUNCATE |
| 1394 | {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, |
| 1395 | #endif |
| 1396 | {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
| 1397 | {"readinto", (PyCFunction)file_readinto, METH_OLDARGS, readinto_doc}, |
| 1398 | {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc}, |
| 1399 | {"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc}, |
| 1400 | {"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc}, |
| 1401 | {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, |
| 1402 | {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, |
| 1403 | {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1404 | {NULL, NULL} /* sentinel */ |
| 1405 | }; |
| 1406 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1407 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1408 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1409 | static PyMemberDef file_memberlist[] = { |
| 1410 | {"softspace", T_INT, OFF(f_softspace), 0, |
| 1411 | "flag indicating that a space needs to be printed; used by print"}, |
| 1412 | {"mode", T_OBJECT, OFF(f_mode), RO, |
| 1413 | "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"}, |
| 1414 | {"name", T_OBJECT, OFF(f_name), RO, |
| 1415 | "file name"}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1416 | /* getattr(f, "closed") is implemented without this table */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1417 | {NULL} /* Sentinel */ |
| 1418 | }; |
| 1419 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1420 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1421 | get_closed(PyFileObject *f, void *closure) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1422 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1423 | return PyInt_FromLong((long)(f->f_fp == 0)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 1426 | static PyGetSetDef file_getsetlist[] = { |
| 1427 | {"closed", (getter)get_closed, NULL, "flag set if the file is closed"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1428 | {0}, |
| 1429 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1430 | |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1431 | static PyObject * |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1432 | file_getiter(PyObject *f) |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1433 | { |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1434 | return PyObject_CallMethod(f, "xreadlines", ""); |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1437 | static PyObject * |
| 1438 | file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1439 | { |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1440 | PyObject *self; |
| 1441 | static PyObject *not_yet_string; |
| 1442 | |
| 1443 | assert(type != NULL && type->tp_alloc != NULL); |
| 1444 | |
| 1445 | if (not_yet_string == NULL) { |
| 1446 | not_yet_string = PyString_FromString("<uninitialized file>"); |
| 1447 | if (not_yet_string == NULL) |
| 1448 | return NULL; |
| 1449 | } |
| 1450 | |
| 1451 | self = type->tp_alloc(type, 0); |
| 1452 | if (self != NULL) { |
| 1453 | /* Always fill in the name and mode, so that nobody else |
| 1454 | needs to special-case NULLs there. */ |
| 1455 | Py_INCREF(not_yet_string); |
| 1456 | ((PyFileObject *)self)->f_name = not_yet_string; |
| 1457 | Py_INCREF(not_yet_string); |
| 1458 | ((PyFileObject *)self)->f_mode = not_yet_string; |
| 1459 | } |
| 1460 | return self; |
| 1461 | } |
| 1462 | |
| 1463 | static int |
| 1464 | file_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1465 | { |
| 1466 | PyFileObject *foself = (PyFileObject *)self; |
| 1467 | int ret = 0; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1468 | static char *kwlist[] = {"name", "mode", "buffering", 0}; |
| 1469 | char *name = NULL; |
| 1470 | char *mode = "r"; |
| 1471 | int bufsize = -1; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1472 | |
| 1473 | assert(PyFile_Check(self)); |
| 1474 | if (foself->f_fp != NULL) { |
| 1475 | /* Have to close the existing file first. */ |
| 1476 | PyObject *closeresult = file_close(foself); |
| 1477 | if (closeresult == NULL) |
| 1478 | return -1; |
| 1479 | Py_DECREF(closeresult); |
| 1480 | } |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
| 1483 | Py_FileSystemDefaultEncoding, &name, |
| 1484 | &mode, &bufsize)) |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1485 | return -1; |
| 1486 | if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL) |
| 1487 | goto Error; |
| 1488 | if (open_the_file(foself, name, mode) == NULL) |
| 1489 | goto Error; |
| 1490 | PyFile_SetBufSize(self, bufsize); |
| 1491 | goto Done; |
| 1492 | |
| 1493 | Error: |
| 1494 | ret = -1; |
| 1495 | /* fall through */ |
| 1496 | Done: |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1497 | PyMem_Free(name); /* free the encoded string */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1498 | return ret; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1501 | static char file_doc[] = |
| 1502 | "file(name[, mode[, buffering]]) -> file object\n" |
| 1503 | "\n" |
| 1504 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 1505 | "writing or appending. The file will be created if it doesn't exist\n" |
| 1506 | "when opened for writing or appending; it will be truncated when\n" |
| 1507 | "opened for writing. Add a 'b' to the mode for binary files.\n" |
| 1508 | "Add a '+' to the mode to allow simultaneous reading and writing.\n" |
| 1509 | "If the buffering argument is given, 0 means unbuffered, 1 means line\n" |
Tim Peters | 742dfd6 | 2001-09-13 21:49:44 +0000 | [diff] [blame] | 1510 | "buffered, and larger numbers specify the buffer size.\n" |
| 1511 | "Note: open() is an alias for file().\n"; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1512 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1513 | PyTypeObject PyFile_Type = { |
| 1514 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1515 | 0, |
| 1516 | "file", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1517 | sizeof(PyFileObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1518 | 0, |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1519 | (destructor)file_dealloc, /* tp_dealloc */ |
| 1520 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1521 | 0, /* tp_getattr */ |
| 1522 | 0, /* tp_setattr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1523 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1524 | (reprfunc)file_repr, /* tp_repr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1525 | 0, /* tp_as_number */ |
| 1526 | 0, /* tp_as_sequence */ |
| 1527 | 0, /* tp_as_mapping */ |
| 1528 | 0, /* tp_hash */ |
| 1529 | 0, /* tp_call */ |
| 1530 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1531 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1532 | 0, /* tp_setattro */ |
| 1533 | 0, /* tp_as_buffer */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1534 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1535 | file_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1536 | 0, /* tp_traverse */ |
| 1537 | 0, /* tp_clear */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1538 | 0, /* tp_richcompare */ |
| 1539 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1540 | file_getiter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1541 | 0, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1542 | file_methods, /* tp_methods */ |
| 1543 | file_memberlist, /* tp_members */ |
| 1544 | file_getsetlist, /* tp_getset */ |
| 1545 | 0, /* tp_base */ |
| 1546 | 0, /* tp_dict */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1547 | 0, /* tp_descr_get */ |
| 1548 | 0, /* tp_descr_set */ |
| 1549 | 0, /* tp_dictoffset */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1550 | (initproc)file_init, /* tp_init */ |
| 1551 | PyType_GenericAlloc, /* tp_alloc */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1552 | file_new, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1553 | _PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1554 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1555 | |
| 1556 | /* Interface for the 'soft space' between print items. */ |
| 1557 | |
| 1558 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1559 | PyFile_SoftSpace(PyObject *f, int newflag) |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1560 | { |
| 1561 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1562 | if (f == NULL) { |
| 1563 | /* Do nothing */ |
| 1564 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1565 | else if (PyFile_Check(f)) { |
| 1566 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 1567 | ((PyFileObject *)f)->f_softspace = newflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1568 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1569 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1570 | PyObject *v; |
| 1571 | v = PyObject_GetAttrString(f, "softspace"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1572 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1573 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1574 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1575 | if (PyInt_Check(v)) |
| 1576 | oldflag = PyInt_AsLong(v); |
| 1577 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1578 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1579 | v = PyInt_FromLong((long)newflag); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1580 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1581 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1582 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1583 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 1584 | PyErr_Clear(); |
| 1585 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1586 | } |
| 1587 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1588 | return oldflag; |
| 1589 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* Interfaces to write objects/strings to file-like objects */ |
| 1592 | |
| 1593 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1594 | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1595 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1596 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1597 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1598 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1599 | return -1; |
| 1600 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1601 | else if (PyFile_Check(f)) { |
| 1602 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1603 | if (fp == NULL) { |
| 1604 | err_closed(); |
| 1605 | return -1; |
| 1606 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1607 | return PyObject_Print(v, fp, flags); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1608 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1609 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1610 | if (writer == NULL) |
| 1611 | return -1; |
Martin v. Löwis | 2777c02 | 2001-09-19 13:47:32 +0000 | [diff] [blame] | 1612 | if (flags & Py_PRINT_RAW) { |
| 1613 | if (PyUnicode_Check(v)) { |
| 1614 | value = v; |
| 1615 | Py_INCREF(value); |
| 1616 | } else |
| 1617 | value = PyObject_Str(v); |
| 1618 | } |
| 1619 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1620 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1621 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1622 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1623 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1624 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1625 | args = Py_BuildValue("(O)", value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 1626 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1627 | Py_DECREF(value); |
| 1628 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 1629 | return -1; |
| 1630 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1631 | result = PyEval_CallObject(writer, args); |
| 1632 | Py_DECREF(args); |
| 1633 | Py_DECREF(value); |
| 1634 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1635 | if (result == NULL) |
| 1636 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1637 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1638 | return 0; |
| 1639 | } |
| 1640 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1641 | int |
Tim Peters | c1bbcb8 | 2001-11-28 22:13:25 +0000 | [diff] [blame] | 1642 | PyFile_WriteString(const char *s, PyObject *f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1643 | { |
| 1644 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1645 | /* Should be caused by a pre-existing error */ |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1646 | if (!PyErr_Occurred()) |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1647 | PyErr_SetString(PyExc_SystemError, |
| 1648 | "null file for PyFile_WriteString"); |
| 1649 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1650 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1651 | else if (PyFile_Check(f)) { |
| 1652 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1653 | if (fp == NULL) { |
| 1654 | err_closed(); |
| 1655 | return -1; |
| 1656 | } |
| 1657 | fputs(s, fp); |
| 1658 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1659 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1660 | else if (!PyErr_Occurred()) { |
| 1661 | PyObject *v = PyString_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1662 | int err; |
| 1663 | if (v == NULL) |
| 1664 | return -1; |
| 1665 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 1666 | Py_DECREF(v); |
| 1667 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1668 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 1669 | else |
| 1670 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1671 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 1672 | |
| 1673 | /* Try to get a file-descriptor from a Python object. If the object |
| 1674 | is an integer or long integer, its value is returned. If not, the |
| 1675 | object's fileno() method is called if it exists; the method must return |
| 1676 | an integer or long integer, which is returned as the file descriptor value. |
| 1677 | -1 is returned on failure. |
| 1678 | */ |
| 1679 | |
| 1680 | int PyObject_AsFileDescriptor(PyObject *o) |
| 1681 | { |
| 1682 | int fd; |
| 1683 | PyObject *meth; |
| 1684 | |
| 1685 | if (PyInt_Check(o)) { |
| 1686 | fd = PyInt_AsLong(o); |
| 1687 | } |
| 1688 | else if (PyLong_Check(o)) { |
| 1689 | fd = PyLong_AsLong(o); |
| 1690 | } |
| 1691 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 1692 | { |
| 1693 | PyObject *fno = PyEval_CallObject(meth, NULL); |
| 1694 | Py_DECREF(meth); |
| 1695 | if (fno == NULL) |
| 1696 | return -1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1697 | |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 1698 | if (PyInt_Check(fno)) { |
| 1699 | fd = PyInt_AsLong(fno); |
| 1700 | Py_DECREF(fno); |
| 1701 | } |
| 1702 | else if (PyLong_Check(fno)) { |
| 1703 | fd = PyLong_AsLong(fno); |
| 1704 | Py_DECREF(fno); |
| 1705 | } |
| 1706 | else { |
| 1707 | PyErr_SetString(PyExc_TypeError, |
| 1708 | "fileno() returned a non-integer"); |
| 1709 | Py_DECREF(fno); |
| 1710 | return -1; |
| 1711 | } |
| 1712 | } |
| 1713 | else { |
| 1714 | PyErr_SetString(PyExc_TypeError, |
| 1715 | "argument must be an int, or have a fileno() method."); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | |
| 1719 | if (fd < 0) { |
| 1720 | PyErr_Format(PyExc_ValueError, |
| 1721 | "file descriptor cannot be a negative integer (%i)", |
| 1722 | fd); |
| 1723 | return -1; |
| 1724 | } |
| 1725 | return fd; |
| 1726 | } |