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