blob: 27612f491edc78efbcd992939729885d2573c450 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* File object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossumff7e83d1999-08-27 20:39:37 +00007#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossum41498431999-01-07 22:09:51 +00008#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +00009#endif /* DONT_HAVE_SYS_TYPES_H */
Guido van Rossum41498431999-01-07 22:09:51 +000010
Guido van Rossum685a38e1996-12-05 21:54:17 +000011#ifdef HAVE_UNISTD_H
12#include <unistd.h>
13#endif
14
Guido van Rossumb8199141997-05-06 15:23:24 +000015#ifdef MS_WIN32
Guido van Rossumb8199141997-05-06 15:23:24 +000016#define fileno _fileno
Trent Mickf29f47b2000-08-11 19:02:59 +000017/* can (almost fully) duplicate with _chsize, see file_truncate */
Guido van Rossumb8199141997-05-06 15:23:24 +000018#define HAVE_FTRUNCATE
19#endif
20
Guido van Rossumf2044e11998-04-28 16:05:59 +000021#ifdef macintosh
22#ifdef USE_GUSI
23#define HAVE_FTRUNCATE
24#endif
25#endif
26
Jack Jansene08dea191995-04-23 22:12:47 +000027#ifdef __MWERKS__
28/* Mwerks fopen() doesn't always set errno */
29#define NO_FOPEN_ERRNO
30#endif
Guido van Rossum295d1711995-02-19 15:55:19 +000031
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Guido van Rossumce5ba841991-03-06 13:06:18 +000033
Guido van Rossumff7e83d1999-08-27 20:39:37 +000034#ifndef DONT_HAVE_ERRNO_H
Guido van Rossumf1dc5661993-07-05 10:31:29 +000035#include <errno.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000036#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037
Trent Mickf29f47b2000-08-11 19:02:59 +000038
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040 PyObject_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 FILE *f_fp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042 PyObject *f_name;
43 PyObject *f_mode;
Tim Petersdbd9ba62000-07-09 03:09:57 +000044 int (*f_close)(FILE *);
Guido van Rossumeb183da1991-04-04 10:44:06 +000045 int f_softspace; /* Flag used by 'print' command */
Guido van Rossum4c08d552000-03-10 22:55:18 +000046 int f_binary; /* Flag which indicates whether the file is open
47 open in binary (1) or test (0) mode */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048} PyFileObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049
50FILE *
Fred Drakefd99de62000-07-09 05:02:18 +000051PyFile_AsFile(PyObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053 if (f == NULL || !PyFile_Check(f))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 return NULL;
Guido van Rossum3165fe61992-09-25 21:59:05 +000055 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 return ((PyFileObject *)f)->f_fp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000060PyFile_Name(PyObject *f)
Guido van Rossumdb3165e1993-10-18 17:06:59 +000061{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 if (f == NULL || !PyFile_Check(f))
Guido van Rossumdb3165e1993-10-18 17:06:59 +000063 return NULL;
64 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 return ((PyFileObject *)f)->f_name;
Guido van Rossumdb3165e1993-10-18 17:06:59 +000066}
67
Tim Peters59c9a642001-09-13 05:38:56 +000068
69static PyObject *
70fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
71 int (*close)(FILE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072{
Tim Peters59c9a642001-09-13 05:38:56 +000073 assert(f != NULL);
74 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +000075 assert(f->f_fp == NULL);
76
77 Py_DECREF(f->f_name);
78 Py_DECREF(f->f_mode);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079 f->f_name = PyString_FromString(name);
80 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +000081
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +000082 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +000083 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +000084 f->f_binary = strchr(mode,'b') != NULL;
Tim Peters44410012001-09-14 03:26:08 +000085
Tim Peters59c9a642001-09-13 05:38:56 +000086 if (f->f_name == NULL || f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 f->f_fp = fp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Tim Peters59c9a642001-09-13 05:38:56 +000092static PyObject *
93open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Tim Peters59c9a642001-09-13 05:38:56 +000095 assert(f != NULL);
96 assert(PyFile_Check(f));
97 assert(name != NULL);
98 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +000099 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000100
Tim Peters8fa45672001-09-13 21:01:29 +0000101 /* rexec.py can't stop a user from getting the file() constructor --
102 all they have to do is get *any* file object f, and then do
103 type(f). Here we prevent them from doing damage with it. */
104 if (PyEval_GetRestricted()) {
105 PyErr_SetString(PyExc_IOError,
106 "file() constructor not accessible in restricted mode");
107 return NULL;
108 }
Tim Petersa27a1502001-11-09 20:59:14 +0000109 errno = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000110#ifdef HAVE_FOPENRF
Guido van Rossuma08095a1991-02-13 23:25:27 +0000111 if (*mode == '*') {
112 FILE *fopenRF();
113 f->f_fp = fopenRF(name, mode+1);
114 }
115 else
116#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000117 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000119 f->f_fp = fopen(name, mode);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000121 }
Guido van Rossuma08095a1991-02-13 23:25:27 +0000122 if (f->f_fp == NULL) {
Jack Jansene08dea191995-04-23 22:12:47 +0000123#ifdef NO_FOPEN_ERRNO
Jack Jansenb3be2162001-11-30 14:16:36 +0000124 /* Metroworks only, wich does not always sets errno */
Jeremy Hylton41c83212001-11-09 16:17:24 +0000125 if (errno == 0) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000126 PyObject *v;
127 v = Py_BuildValue("(is)", 0, "Cannot open file");
128 if (v != NULL) {
129 PyErr_SetObject(PyExc_IOError, v);
130 Py_DECREF(v);
131 }
Jack Jansene08dea191995-04-23 22:12:47 +0000132 return NULL;
133 }
134#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000135 if (errno == EINVAL)
136 PyErr_Format(PyExc_IOError, "invalid argument: %s",
137 mode);
138 else
139 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Tim Peters59c9a642001-09-13 05:38:56 +0000140 f = NULL;
141 }
142 return (PyObject *)f;
143}
144
145PyObject *
146PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
147{
Tim Peters44410012001-09-14 03:26:08 +0000148 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
149 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000150 if (f != NULL) {
151 if (fill_file_fields(f, fp, name, mode, close) == NULL) {
152 Py_DECREF(f);
153 f = NULL;
154 }
155 }
156 return (PyObject *) f;
157}
158
159PyObject *
160PyFile_FromString(char *name, char *mode)
161{
162 extern int fclose(FILE *);
163 PyFileObject *f;
164
165 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
166 if (f != NULL) {
167 if (open_the_file(f, name, mode) == NULL) {
168 Py_DECREF(f);
169 f = NULL;
170 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000172 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173}
174
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175void
Fred Drakefd99de62000-07-09 05:02:18 +0000176PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177{
178 if (bufsize >= 0) {
179#ifdef HAVE_SETVBUF
180 int type;
181 switch (bufsize) {
182 case 0:
183 type = _IONBF;
184 break;
185 case 1:
186 type = _IOLBF;
187 bufsize = BUFSIZ;
188 break;
189 default:
190 type = _IOFBF;
191 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
193 type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000194#else /* !HAVE_SETVBUF */
195 if (bufsize <= 1)
196 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
197#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198 }
199}
200
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000201static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000202err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000203{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000205 return NULL;
206}
207
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208/* Methods */
209
210static void
Fred Drakefd99de62000-07-09 05:02:18 +0000211file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000213 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000215 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000217 }
Tim Peters44410012001-09-14 03:26:08 +0000218 Py_XDECREF(f->f_name);
219 Py_XDECREF(f->f_mode);
Guido van Rossum9475a232001-10-05 20:51:39 +0000220 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000224file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000226 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
227 f->f_fp == NULL ? "closed" : "open",
228 PyString_AsString(f->f_name),
229 PyString_AsString(f->f_mode),
230 f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231}
232
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000233static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000234file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000236 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000238 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000240 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000241 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000243 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 f->f_fp = NULL;
245 }
Guido van Rossumfebd5511992-03-04 16:39:24 +0000246 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000248 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 return PyInt_FromLong((long)sts);
250 Py_INCREF(Py_None);
251 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Trent Mickf29f47b2000-08-11 19:02:59 +0000254
Guido van Rossumb8552162001-09-05 14:58:11 +0000255/* Our very own off_t-like type, 64-bit if possible */
256#if !defined(HAVE_LARGEFILE_SUPPORT)
257typedef off_t Py_off_t;
258#elif SIZEOF_OFF_T >= 8
259typedef off_t Py_off_t;
260#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000261typedef fpos_t Py_off_t;
262#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000263#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000264#endif
265
266
Trent Mickf29f47b2000-08-11 19:02:59 +0000267/* a portable fseek() function
268 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000269static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000270_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000271{
Guido van Rossumb8552162001-09-05 14:58:11 +0000272#if !defined(HAVE_LARGEFILE_SUPPORT)
273 return fseek(fp, offset, whence);
274#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000275 return fseeko(fp, offset, whence);
276#elif defined(HAVE_FSEEK64)
277 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000278#elif defined(__BEOS__)
279 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000280#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000281 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
282 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000283 fpos_t pos;
284 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000285 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000286#ifdef MS_WINDOWS
287 fflush(fp);
288 if (_lseeki64(fileno(fp), 0, 2) == -1)
289 return -1;
290#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000291 if (fseek(fp, 0, SEEK_END) != 0)
292 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000293#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000294 /* fall through */
295 case SEEK_CUR:
296 if (fgetpos(fp, &pos) != 0)
297 return -1;
298 offset += pos;
299 break;
300 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000301 }
302 return fsetpos(fp, &offset);
303#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000304#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000305#endif
306}
307
308
309/* a portable ftell() function
310 Return -1 on failure with errno set appropriately, current file
311 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000312static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000313_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000314{
Guido van Rossumb8552162001-09-05 14:58:11 +0000315#if !defined(HAVE_LARGEFILE_SUPPORT)
316 return ftell(fp);
317#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
318 return ftello(fp);
319#elif defined(HAVE_FTELL64)
320 return ftell64(fp);
321#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000322 fpos_t pos;
323 if (fgetpos(fp, &pos) != 0)
324 return -1;
325 return pos;
326#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000327#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000328#endif
329}
330
331
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000332static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000333file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000335 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000336 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000337 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000338 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000339
Guido van Rossumd7297e61992-07-06 14:19:26 +0000340 if (f->f_fp == NULL)
341 return err_closed();
342 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000343 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000344 return NULL;
345#if !defined(HAVE_LARGEFILE_SUPPORT)
346 offset = PyInt_AsLong(offobj);
347#else
348 offset = PyLong_Check(offobj) ?
349 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
350#endif
351 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000352 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000355 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000356 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000358
Guido van Rossumff4949e1992-08-05 19:58:53 +0000359 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000360 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000361 clearerr(f->f_fp);
362 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000363 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 Py_INCREF(Py_None);
365 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000366}
367
Trent Mickf29f47b2000-08-11 19:02:59 +0000368
Guido van Rossumd7047b31995-01-02 19:07:15 +0000369#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000370static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000371file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000372{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000373 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000374 Py_off_t newsize;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000375 PyObject *newsizeobj;
Tim Peters86821b22001-01-07 21:19:34 +0000376
Guido van Rossumd7047b31995-01-02 19:07:15 +0000377 if (f->f_fp == NULL)
378 return err_closed();
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000379 newsizeobj = NULL;
Guido van Rossum43713e52000-02-29 13:59:29 +0000380 if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000381 return NULL;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000382 if (newsizeobj != NULL) {
383#if !defined(HAVE_LARGEFILE_SUPPORT)
384 newsize = PyInt_AsLong(newsizeobj);
385#else
386 newsize = PyLong_Check(newsizeobj) ?
387 PyLong_AsLongLong(newsizeobj) :
388 PyInt_AsLong(newsizeobj);
389#endif
390 if (PyErr_Occurred())
391 return NULL;
392 } else {
393 /* Default to current position*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000395 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000396 newsize = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 Py_END_ALLOW_THREADS
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000398 if (newsize == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumd7047b31995-01-02 19:07:15 +0000400 clearerr(f->f_fp);
401 return NULL;
402 }
403 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000405 errno = 0;
406 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000408 if (ret != 0) goto onioerror;
409
410#ifdef MS_WIN32
411 /* can use _chsize; if, however, the newsize overflows 32-bits then
412 _chsize is *not* adequate; in this case, an OverflowError is raised */
413 if (newsize > LONG_MAX) {
414 PyErr_SetString(PyExc_OverflowError,
415 "the new size is too long for _chsize (it is limited to 32-bit values)");
Guido van Rossumd7047b31995-01-02 19:07:15 +0000416 return NULL;
Trent Mickf29f47b2000-08-11 19:02:59 +0000417 } else {
418 Py_BEGIN_ALLOW_THREADS
419 errno = 0;
Tim Peters6e13a562001-09-06 00:32:15 +0000420 ret = _chsize(fileno(f->f_fp), (long)newsize);
Trent Mickf29f47b2000-08-11 19:02:59 +0000421 Py_END_ALLOW_THREADS
422 if (ret != 0) goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000423 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000424#else
425 Py_BEGIN_ALLOW_THREADS
426 errno = 0;
427 ret = ftruncate(fileno(f->f_fp), newsize);
428 Py_END_ALLOW_THREADS
429 if (ret != 0) goto onioerror;
430#endif /* !MS_WIN32 */
Tim Peters86821b22001-01-07 21:19:34 +0000431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 Py_INCREF(Py_None);
433 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000434
435onioerror:
436 PyErr_SetFromErrno(PyExc_IOError);
437 clearerr(f->f_fp);
438 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000439}
440#endif /* HAVE_FTRUNCATE */
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000443file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000444{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000445 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000446
Guido van Rossumd7297e61992-07-06 14:19:26 +0000447 if (f->f_fp == NULL)
448 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000450 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000451 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000453 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000455 clearerr(f->f_fp);
456 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000457 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000458#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000459 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000460#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000461 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000462#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000463}
464
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000466file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000467{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000468 if (f->f_fp == NULL)
469 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000471}
472
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000474file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000475{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000476 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000477
Guido van Rossumd7297e61992-07-06 14:19:26 +0000478 if (f->f_fp == NULL)
479 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000481 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000482 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000484 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000486 clearerr(f->f_fp);
487 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000488 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489 Py_INCREF(Py_None);
490 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000491}
492
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000494file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000495{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000496 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000497 if (f->f_fp == NULL)
498 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000500 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 Py_END_ALLOW_THREADS
502 return PyInt_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000503}
504
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000505
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000506#if BUFSIZ < 8192
507#define SMALLCHUNK 8192
508#else
509#define SMALLCHUNK BUFSIZ
510#endif
511
Guido van Rossum3c259041999-01-14 19:00:14 +0000512#if SIZEOF_INT < 4
513#define BIGCHUNK (512 * 32)
514#else
515#define BIGCHUNK (512 * 1024)
516#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000517
518static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000519new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000520{
521#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000522 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000523 struct stat st;
524 if (fstat(fileno(f->f_fp), &st) == 0) {
525 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000526 /* The following is not a bug: we really need to call lseek()
527 *and* ftell(). The reason is that some stdio libraries
528 mistakenly flush their buffer when ftell() is called and
529 the lseek() call it makes fails, thereby throwing away
530 data that cannot be recovered in any way. To avoid this,
531 we first test lseek(), and only call ftell() if lseek()
532 works. We can't use the lseek() value either, because we
533 need to take the amount of buffered data into account.
534 (Yet another reason why stdio stinks. :-) */
Jack Jansen2771b5b2001-10-10 22:03:27 +0000535#ifdef USE_GUSI2
536 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
537 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
538#else
Guido van Rossum91aaa921998-05-05 22:21:35 +0000539 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000540#endif
541 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000542 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000543 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000544 if (pos < 0)
545 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000546 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000547 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000548 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000549 }
550#endif
551 if (currentsize > SMALLCHUNK) {
552 /* Keep doubling until we reach BIGCHUNK;
553 then keep adding BIGCHUNK. */
554 if (currentsize <= BIGCHUNK)
555 return currentsize + currentsize;
556 else
557 return currentsize + BIGCHUNK;
558 }
559 return currentsize + SMALLCHUNK;
560}
561
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000563file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000564{
Guido van Rossum789a1611997-05-10 22:33:55 +0000565 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000566 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000568
Guido van Rossumd7297e61992-07-06 14:19:26 +0000569 if (f->f_fp == NULL)
570 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000571 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000572 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000573 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000574 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000575 else
576 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000577 if (buffersize > INT_MAX) {
578 PyErr_SetString(PyExc_OverflowError,
579 "requested number of bytes is more than a Python string can hold");
580 return NULL;
581 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000582 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000583 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000585 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000586 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000587 Py_BEGIN_ALLOW_THREADS
588 errno = 0;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000589 chunksize = fread(BUF(v) + bytesread, 1,
590 buffersize - bytesread, f->f_fp);
Guido van Rossum6263d541997-05-10 22:07:25 +0000591 Py_END_ALLOW_THREADS
592 if (chunksize == 0) {
593 if (!ferror(f->f_fp))
594 break;
595 PyErr_SetFromErrno(PyExc_IOError);
596 clearerr(f->f_fp);
597 Py_DECREF(v);
598 return NULL;
599 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000600 bytesread += chunksize;
601 if (bytesread < buffersize)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000602 break;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000603 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000604 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000605 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000606 return NULL;
607 }
608 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000609 if (bytesread != buffersize)
610 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611 return v;
612}
613
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000614static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000615file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000616{
617 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000618 int ntodo;
619 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000620
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000621 if (f->f_fp == NULL)
622 return err_closed();
623 if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
624 return NULL;
625 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000626 while (ntodo > 0) {
627 Py_BEGIN_ALLOW_THREADS
628 errno = 0;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000629 nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
Guido van Rossum6263d541997-05-10 22:07:25 +0000630 Py_END_ALLOW_THREADS
631 if (nnow == 0) {
632 if (!ferror(f->f_fp))
633 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000634 PyErr_SetFromErrno(PyExc_IOError);
635 clearerr(f->f_fp);
636 return NULL;
637 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000638 ndone += nnow;
639 ntodo -= nnow;
640 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000641 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000642}
643
Tim Peters86821b22001-01-07 21:19:34 +0000644/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000645Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000646
647Under MSVC 6:
648
Tim Peters1c733232001-01-08 04:02:07 +0000649+ MS threadsafe getc is very slow (multiple layers of function calls before+
650 after each character, to lock+unlock the stream).
651+ The stream-locking functions are MS-internal -- can't access them from user
652 code.
653+ There's nothing Tim could find in the MS C or platform SDK libraries that
654 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000655+ MS fgets locks/unlocks only once per line; it's the only hook we have.
656
657So we use fgets for speed(!), despite that it's painful.
658
659MS realloc is also slow.
660
Tim Petersf29b64d2001-01-15 06:33:19 +0000661Reports from other platforms on this method vs getc_unlocked (which MS doesn't
662have):
663 Linux a wash
664 Solaris a wash
665 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000666
Tim Petersf29b64d2001-01-15 06:33:19 +0000667CAUTION: The C std isn't clear about this: in those cases where fgets
668writes something into the buffer, can it write into any position beyond the
669required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
670known on which it does; and it would be a strange way to code fgets. Still,
671getline_via_fgets may not work correctly if it does. The std test
672test_bufio.py should fail if platform fgets() routinely writes beyond the
673trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000674**************************************************************************/
675
Tim Petersf29b64d2001-01-15 06:33:19 +0000676/* Use this routine if told to, or by default on non-get_unlocked()
677 * platforms unless told not to. Yikes! Let's spell that out:
678 * On a platform with getc_unlocked():
679 * By default, use getc_unlocked().
680 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
681 * On a platform without getc_unlocked():
682 * By default, use fgets().
683 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
684 */
685#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
686#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000687#endif
688
Tim Petersf29b64d2001-01-15 06:33:19 +0000689#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
690#undef USE_FGETS_IN_GETLINE
691#endif
692
693#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000694static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000695getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000696{
Tim Peters15b83852001-01-08 00:53:12 +0000697/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000698 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
699 * to fill this much of the buffer with a known value in order to figure out
700 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
701 * than "most" lines, we waste time filling unused buffer slots. 100 is
702 * surely adequate for most peoples' email archives, chewing over source code,
703 * etc -- "regular old text files".
704 * MAXBUFSIZE is the maximum line length that lets us get away with the less
705 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
706 * cautions about boosting that. 300 was chosen because the worst real-life
707 * text-crunching job reported on Python-Dev was a mail-log crawler where over
708 * half the lines were 254 chars.
709 * INCBUFSIZE is the amount by which we grow the buffer, if MAXBUFSIZE isn't
710 * enough. It doesn't much matter what this is set to: we only get here for
711 * absurdly long lines anyway.
Tim Peters15b83852001-01-08 00:53:12 +0000712 */
Tim Peters142297a2001-01-15 10:36:56 +0000713#define INITBUFSIZE 100
714#define MAXBUFSIZE 300
Tim Peters86821b22001-01-07 21:19:34 +0000715#define INCBUFSIZE 1000
Tim Peters142297a2001-01-15 10:36:56 +0000716 char* p; /* temp */
717 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000718 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000719 char* pvfree; /* address of next free slot */
720 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000721 size_t nfree; /* # of free buffer slots; pvend-pvfree */
722 size_t total_v_size; /* total # of slots in buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000723
Tim Peters15b83852001-01-08 00:53:12 +0000724 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000725 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000726 */
Tim Peters142297a2001-01-15 10:36:56 +0000727 total_v_size = INITBUFSIZE; /* start small and pray */
728 pvfree = buf;
729 for (;;) {
730 Py_BEGIN_ALLOW_THREADS
731 pvend = buf + total_v_size;
732 nfree = pvend - pvfree;
733 memset(pvfree, '\n', nfree);
734 p = fgets(pvfree, nfree, fp);
735 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000736
Tim Peters142297a2001-01-15 10:36:56 +0000737 if (p == NULL) {
738 clearerr(fp);
739 if (PyErr_CheckSignals())
740 return NULL;
741 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000742 return v;
743 }
Tim Peters142297a2001-01-15 10:36:56 +0000744 /* fgets read *something* */
745 p = memchr(pvfree, '\n', nfree);
746 if (p != NULL) {
747 /* Did the \n come from fgets or from us?
748 * Since fgets stops at the first \n, and then writes
749 * \0, if it's from fgets a \0 must be next. But if
750 * that's so, it could not have come from us, since
751 * the \n's we filled the buffer with have only more
752 * \n's to the right.
753 */
754 if (p+1 < pvend && *(p+1) == '\0') {
755 /* It's from fgets: we win! In particular,
756 * we haven't done any mallocs yet, and can
757 * build the final result on the first try.
758 */
759 ++p; /* include \n from fgets */
760 }
761 else {
762 /* Must be from us: fgets didn't fill the
763 * buffer and didn't find a newline, so it
764 * must be the last and newline-free line of
765 * the file.
766 */
767 assert(p > pvfree && *(p-1) == '\0');
768 --p; /* don't include \0 from fgets */
769 }
770 v = PyString_FromStringAndSize(buf, p - buf);
771 return v;
772 }
773 /* yuck: fgets overwrote all the newlines, i.e. the entire
774 * buffer. So this line isn't over yet, or maybe it is but
775 * we're exactly at EOF. If we haven't already, try using the
776 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000777 */
Tim Peters142297a2001-01-15 10:36:56 +0000778 assert(*(pvend-1) == '\0');
779 if (pvfree == buf) {
780 pvfree = pvend - 1; /* overwrite trailing null */
781 total_v_size = MAXBUFSIZE;
782 }
783 else
784 break;
Tim Peters86821b22001-01-07 21:19:34 +0000785 }
Tim Peters142297a2001-01-15 10:36:56 +0000786
787 /* The stack buffer isn't big enough; malloc a string object and read
788 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000789 */
Tim Peters142297a2001-01-15 10:36:56 +0000790 total_v_size = MAXBUFSIZE + INCBUFSIZE;
Tim Peters1c733232001-01-08 04:02:07 +0000791 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000792 if (v == NULL)
793 return v;
794 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +0000795 memcpy(BUF(v), buf, MAXBUFSIZE-1);
796 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +0000797
798 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +0000799 * after setting p one beyond the end of the line. The code here is
800 * very much like the code above, except reads into v's buffer; see
801 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +0000802 */
803 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +0000804 Py_BEGIN_ALLOW_THREADS
805 pvend = BUF(v) + total_v_size;
806 nfree = pvend - pvfree;
807 memset(pvfree, '\n', nfree);
808 p = fgets(pvfree, nfree, fp);
809 Py_END_ALLOW_THREADS
810
811 if (p == NULL) {
812 clearerr(fp);
813 if (PyErr_CheckSignals()) {
814 Py_DECREF(v);
815 return NULL;
816 }
817 p = pvfree;
818 break;
819 }
Tim Peters86821b22001-01-07 21:19:34 +0000820 p = memchr(pvfree, '\n', nfree);
821 if (p != NULL) {
822 if (p+1 < pvend && *(p+1) == '\0') {
823 /* \n came from fgets */
824 ++p;
825 break;
826 }
827 /* \n came from us; last line of file, no newline */
828 assert(p > pvfree && *(p-1) == '\0');
829 --p;
830 break;
831 }
832 /* expand buffer and try again */
833 assert(*(pvend-1) == '\0');
834 total_v_size += INCBUFSIZE;
835 if (total_v_size > INT_MAX) {
836 PyErr_SetString(PyExc_OverflowError,
837 "line is longer than a Python string can hold");
838 Py_DECREF(v);
839 return NULL;
840 }
841 if (_PyString_Resize(&v, (int)total_v_size) < 0)
842 return NULL;
843 /* overwrite the trailing null byte */
844 pvfree = BUF(v) + (total_v_size - INCBUFSIZE - 1);
845 }
846 if (BUF(v) + total_v_size != p)
847 _PyString_Resize(&v, p - BUF(v));
848 return v;
849#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +0000850#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +0000851#undef INCBUFSIZE
852}
Tim Petersf29b64d2001-01-15 06:33:19 +0000853#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000854
Guido van Rossum0bd24411991-04-04 15:21:57 +0000855/* Internal routine to get a line.
856 Size argument interpretation:
857 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +0000858 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +0000859*/
860
Guido van Rossum1187aa42001-01-05 14:43:05 +0000861#ifdef HAVE_GETC_UNLOCKED
862#define GETC(f) getc_unlocked(f)
863#define FLOCKFILE(f) flockfile(f)
864#define FUNLOCKFILE(f) funlockfile(f)
865#else
866#define GETC(f) getc(f)
867#define FLOCKFILE(f)
868#define FUNLOCKFILE(f)
869#endif
870
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000872get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873{
Guido van Rossum1187aa42001-01-05 14:43:05 +0000874 FILE *fp = f->f_fp;
875 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +0000876 char *buf, *end;
Trent Mickf29f47b2000-08-11 19:02:59 +0000877 size_t n1, n2;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 PyObject *v;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000879
Tim Petersf29b64d2001-01-15 06:33:19 +0000880#ifdef USE_FGETS_IN_GETLINE
Guido van Rossum86282062001-01-08 01:26:47 +0000881 if (n <= 0)
Tim Petersf29b64d2001-01-15 06:33:19 +0000882 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +0000883#endif
Guido van Rossum0bd24411991-04-04 15:21:57 +0000884 n2 = n > 0 ? n : 100;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885 v = PyString_FromStringAndSize((char *)NULL, n2);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000886 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000888 buf = BUF(v);
889 end = buf + n2;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000890
Guido van Rossumce5ba841991-03-06 13:06:18 +0000891 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +0000892 Py_BEGIN_ALLOW_THREADS
893 FLOCKFILE(fp);
894 while ((c = GETC(fp)) != EOF &&
895 (*buf++ = c) != '\n' &&
896 buf != end)
897 ;
898 FUNLOCKFILE(fp);
899 Py_END_ALLOW_THREADS
900 if (c == '\n')
901 break;
902 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +0000903 if (ferror(fp)) {
904 PyErr_SetFromErrno(PyExc_IOError);
905 clearerr(fp);
906 Py_DECREF(v);
907 return NULL;
908 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +0000909 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +0000912 return NULL;
913 }
Guido van Rossumce5ba841991-03-06 13:06:18 +0000914 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000915 }
Guido van Rossum1187aa42001-01-05 14:43:05 +0000916 /* Must be because buf == end */
917 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +0000918 break;
Guido van Rossum1187aa42001-01-05 14:43:05 +0000919 n1 = n2;
920 n2 += 1000;
921 if (n2 > INT_MAX) {
922 PyErr_SetString(PyExc_OverflowError,
923 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +0000924 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +0000925 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000926 }
Guido van Rossum1187aa42001-01-05 14:43:05 +0000927 if (_PyString_Resize(&v, n2) < 0)
928 return NULL;
929 buf = BUF(v) + n1;
930 end = BUF(v) + n2;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000931 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000932
Guido van Rossumce5ba841991-03-06 13:06:18 +0000933 n1 = buf - BUF(v);
934 if (n1 != n2)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 _PyString_Resize(&v, n1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936 return v;
937}
938
Guido van Rossum0bd24411991-04-04 15:21:57 +0000939/* External C interface */
940
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000941PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000942PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +0000943{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +0000944 PyObject *result;
945
Guido van Rossum3165fe61992-09-25 21:59:05 +0000946 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +0000948 return NULL;
949 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +0000950
951 if (PyFile_Check(f)) {
952 if (((PyFileObject*)f)->f_fp == NULL)
953 return err_closed();
954 result = get_line((PyFileObject *)f, n);
955 }
956 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957 PyObject *reader;
958 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +0000959
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000960 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000961 if (reader == NULL)
962 return NULL;
963 if (n <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 args = Py_BuildValue("()");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000965 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000967 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000968 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000969 return NULL;
970 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 result = PyEval_CallObject(reader, args);
972 Py_DECREF(reader);
973 Py_DECREF(args);
974 if (result != NULL && !PyString_Check(result)) {
975 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +0000976 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +0000978 "object.readline() returned non-string");
979 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +0000980 }
981
982 if (n < 0 && result != NULL && PyString_Check(result)) {
983 char *s = PyString_AS_STRING(result);
984 int len = PyString_GET_SIZE(result);
985 if (len == 0) {
986 Py_DECREF(result);
987 result = NULL;
988 PyErr_SetString(PyExc_EOFError,
989 "EOF when reading a line");
990 }
991 else if (s[len-1] == '\n') {
992 if (result->ob_refcnt == 1)
993 _PyString_Resize(&result, len-1);
994 else {
995 PyObject *v;
996 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +0000998 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +0000999 }
1000 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001001 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001002 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001003}
1004
1005/* Python method */
1006
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001008file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001009{
Guido van Rossum789a1611997-05-10 22:33:55 +00001010 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001011
Guido van Rossumd7297e61992-07-06 14:19:26 +00001012 if (f->f_fp == NULL)
1013 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001014 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001015 return NULL;
1016 if (n == 0)
1017 return PyString_FromString("");
1018 if (n < 0)
1019 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001020 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001021}
1022
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001024file_xreadlines(PyFileObject *f)
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001025{
1026 static PyObject* xreadlines_function = NULL;
Tim Petersf29b64d2001-01-15 06:33:19 +00001027
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001028 if (!xreadlines_function) {
1029 PyObject *xreadlines_module =
1030 PyImport_ImportModule("xreadlines");
1031 if(!xreadlines_module)
1032 return NULL;
1033
1034 xreadlines_function = PyObject_GetAttrString(xreadlines_module,
1035 "xreadlines");
1036 Py_DECREF(xreadlines_module);
1037 if(!xreadlines_function)
1038 return NULL;
1039 }
1040 return PyObject_CallFunction(xreadlines_function, "(O)", f);
1041}
1042
1043static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001044file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001045{
Guido van Rossum789a1611997-05-10 22:33:55 +00001046 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 PyObject *list;
1048 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001049 char small_buffer[SMALLCHUNK];
1050 char *buffer = small_buffer;
1051 size_t buffersize = SMALLCHUNK;
1052 PyObject *big_buffer = NULL;
1053 size_t nfilled = 0;
1054 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001055 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001056 char *p, *q, *end;
1057 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001058 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001059
Guido van Rossumd7297e61992-07-06 14:19:26 +00001060 if (f->f_fp == NULL)
1061 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001062 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001063 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001064 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001065 return NULL;
1066 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001067 if (shortread)
1068 nread = 0;
1069 else {
1070 Py_BEGIN_ALLOW_THREADS
1071 errno = 0;
1072 nread = fread(buffer+nfilled, 1,
1073 buffersize-nfilled, f->f_fp);
1074 Py_END_ALLOW_THREADS
1075 shortread = (nread < buffersize-nfilled);
1076 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001077 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001078 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001079 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001080 break;
1081 PyErr_SetFromErrno(PyExc_IOError);
1082 clearerr(f->f_fp);
1083 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001085 list = NULL;
1086 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001087 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001088 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001089 p = memchr(buffer+nfilled, '\n', nread);
1090 if (p == NULL) {
1091 /* Need a larger buffer to fit this line */
1092 nfilled += nread;
1093 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001094 if (buffersize > INT_MAX) {
1095 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001096 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001097 goto error;
1098 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001099 if (big_buffer == NULL) {
1100 /* Create the big buffer */
1101 big_buffer = PyString_FromStringAndSize(
1102 NULL, buffersize);
1103 if (big_buffer == NULL)
1104 goto error;
1105 buffer = PyString_AS_STRING(big_buffer);
1106 memcpy(buffer, small_buffer, nfilled);
1107 }
1108 else {
1109 /* Grow the big buffer */
1110 _PyString_Resize(&big_buffer, buffersize);
1111 buffer = PyString_AS_STRING(big_buffer);
1112 }
1113 continue;
1114 }
1115 end = buffer+nfilled+nread;
1116 q = buffer;
1117 do {
1118 /* Process complete lines */
1119 p++;
1120 line = PyString_FromStringAndSize(q, p-q);
1121 if (line == NULL)
1122 goto error;
1123 err = PyList_Append(list, line);
1124 Py_DECREF(line);
1125 if (err != 0)
1126 goto error;
1127 q = p;
1128 p = memchr(q, '\n', end-q);
1129 } while (p != NULL);
1130 /* Move the remaining incomplete line to the start */
1131 nfilled = end-q;
1132 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001133 if (sizehint > 0)
1134 if (totalread >= (size_t)sizehint)
1135 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001136 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001137 if (nfilled != 0) {
1138 /* Partial last line */
1139 line = PyString_FromStringAndSize(buffer, nfilled);
1140 if (line == NULL)
1141 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001142 if (sizehint > 0) {
1143 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001144 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001145 if (rest == NULL) {
1146 Py_DECREF(line);
1147 goto error;
1148 }
1149 PyString_Concat(&line, rest);
1150 Py_DECREF(rest);
1151 if (line == NULL)
1152 goto error;
1153 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001154 err = PyList_Append(list, line);
1155 Py_DECREF(line);
1156 if (err != 0)
1157 goto error;
1158 }
1159 cleanup:
Guido van Rossum1109fbc1998-04-10 22:16:39 +00001160 if (big_buffer) {
Guido van Rossum6263d541997-05-10 22:07:25 +00001161 Py_DECREF(big_buffer);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00001162 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001163 return list;
1164}
1165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001167file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001168{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001169 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001171 if (f->f_fp == NULL)
1172 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001173 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001175 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001178 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001180 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001182 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001183 return NULL;
1184 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 Py_INCREF(Py_None);
1186 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187}
1188
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001190file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001191{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001192#define CHUNKSIZE 1000
1193 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001194 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001195 PyObject *result;
1196 int i, j, index, len, nwritten, islist;
1197
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001198 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001199 if (f->f_fp == NULL)
1200 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001201
1202 result = NULL;
1203 list = NULL;
1204 islist = PyList_Check(seq);
1205 if (islist)
1206 it = NULL;
1207 else {
1208 it = PyObject_GetIter(seq);
1209 if (it == NULL) {
1210 PyErr_SetString(PyExc_TypeError,
1211 "writelines() requires an iterable argument");
1212 return NULL;
1213 }
1214 /* From here on, fail by going to error, to reclaim "it". */
1215 list = PyList_New(CHUNKSIZE);
1216 if (list == NULL)
1217 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001218 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001219
1220 /* Strategy: slurp CHUNKSIZE lines into a private list,
1221 checking that they are all strings, then write that list
1222 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001223 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001224 if (islist) {
1225 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001226 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001227 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001228 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001229 j = PyList_GET_SIZE(list);
1230 }
1231 else {
1232 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001233 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001234 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001235 if (PyErr_Occurred())
1236 goto error;
1237 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001238 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001239 PyList_SetItem(list, j, line);
1240 }
1241 }
1242 if (j == 0)
1243 break;
1244
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001245 /* Check that all entries are indeed strings. If not,
1246 apply the same rules as for file.write() and
1247 convert the results to strings. This is slow, but
1248 seems to be the only way since all conversion APIs
1249 could potentially execute Python code. */
1250 for (i = 0; i < j; i++) {
1251 PyObject *v = PyList_GET_ITEM(list, i);
1252 if (!PyString_Check(v)) {
1253 const char *buffer;
1254 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001255 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001256 PyObject_AsReadBuffer(v,
1257 (const void**)&buffer,
1258 &len)) ||
1259 PyObject_AsCharBuffer(v,
1260 &buffer,
1261 &len))) {
1262 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00001263 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001264 goto error;
1265 }
1266 line = PyString_FromStringAndSize(buffer,
1267 len);
1268 if (line == NULL)
1269 goto error;
1270 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001271 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001272 }
1273 }
1274
1275 /* Since we are releasing the global lock, the
1276 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001277 Py_BEGIN_ALLOW_THREADS
1278 f->f_softspace = 0;
1279 errno = 0;
1280 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001281 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001282 len = PyString_GET_SIZE(line);
1283 nwritten = fwrite(PyString_AS_STRING(line),
1284 1, len, f->f_fp);
1285 if (nwritten != len) {
1286 Py_BLOCK_THREADS
1287 PyErr_SetFromErrno(PyExc_IOError);
1288 clearerr(f->f_fp);
1289 goto error;
1290 }
1291 }
1292 Py_END_ALLOW_THREADS
1293
1294 if (j < CHUNKSIZE)
1295 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001296 }
1297
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001299 result = Py_None;
1300 error:
1301 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001302 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001303 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001304#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001305}
1306
Tim Petersefc3a3a2001-09-20 07:55:22 +00001307static char readline_doc[] =
1308"readline([size]) -> next line from the file, as a string.\n"
1309"\n"
1310"Retain newline. A non-negative size argument limits the maximum\n"
1311"number of bytes to return (an incomplete line may be returned then).\n"
1312"Return an empty string at EOF.";
1313
1314static char read_doc[] =
1315"read([size]) -> read at most size bytes, returned as a string.\n"
1316"\n"
1317"If the size argument is negative or omitted, read until EOF is reached.";
1318
1319static char write_doc[] =
1320"write(str) -> None. Write string str to file.\n"
1321"\n"
1322"Note that due to buffering, flush() or close() may be needed before\n"
1323"the file on disk reflects the data written.";
1324
1325static char fileno_doc[] =
1326"fileno() -> integer \"file descriptor\".\n"
1327"\n"
1328"This is needed for lower-level file interfaces, such os.read().";
1329
1330static char seek_doc[] =
1331"seek(offset[, whence]) -> None. Move to new file position.\n"
1332"\n"
1333"Argument offset is a byte count. Optional argument whence defaults to\n"
1334"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1335"(move relative to current position, positive or negative), and 2 (move\n"
1336"relative to end of file, usually negative, although many platforms allow\n"
1337"seeking beyond the end of a file).\n"
1338"\n"
1339"Note that not all file objects are seekable.";
1340
Guido van Rossumd7047b31995-01-02 19:07:15 +00001341#ifdef HAVE_FTRUNCATE
Tim Petersefc3a3a2001-09-20 07:55:22 +00001342static char truncate_doc[] =
1343"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1344"\n"
1345"Size defaults to the current file position, as returned by tell().";
Guido van Rossumd7047b31995-01-02 19:07:15 +00001346#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001347
1348static char tell_doc[] =
1349"tell() -> current file position, an integer (may be a long integer).";
1350
1351static char readinto_doc[] =
1352"readinto() -> Undocumented. Don't use this; it may go away.";
1353
1354static char readlines_doc[] =
1355"readlines([size]) -> list of strings, each a line from the file.\n"
1356"\n"
1357"Call readline() repeatedly and return a list of the lines so read.\n"
1358"The optional size argument, if given, is an approximate bound on the\n"
1359"total number of bytes in the lines returned.";
1360
1361static char xreadlines_doc[] =
1362"xreadlines() -> next line from the file, as a string.\n"
1363"\n"
1364"Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n"
1365"often quicker, due to reading ahead internally.";
1366
1367static char writelines_doc[] =
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001368"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001369"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001370"Note that newlines are not added. The sequence can be any iterable object\n"
1371"producing strings. This is equivalent to calling write() for each string.";
Tim Petersefc3a3a2001-09-20 07:55:22 +00001372
1373static char flush_doc[] =
1374"flush() -> None. Flush the internal I/O buffer.";
1375
1376static char close_doc[] =
1377"close() -> None or (perhaps) an integer. Close the file.\n"
1378"\n"
1379"Sets data attribute .closed to true. A closed file cannot be used for\n"
1380"further I/O operations. close() may be called more than once without\n"
1381"error. Some kinds of file objects (for example, opened by popen())\n"
1382"may return an exit status upon closing.";
1383
1384static char isatty_doc[] =
1385"isatty() -> true or false. True if the file is connected to a tty device.";
1386
1387static PyMethodDef file_methods[] = {
1388 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1389 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001390 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001391 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1392 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
1393#ifdef HAVE_FTRUNCATE
1394 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
1395#endif
1396 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1397 {"readinto", (PyCFunction)file_readinto, METH_OLDARGS, readinto_doc},
1398 {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
1399 {"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
1400 {"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc},
1401 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1402 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1403 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001404 {NULL, NULL} /* sentinel */
1405};
1406
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001408
Guido van Rossum6f799372001-09-20 20:46:19 +00001409static PyMemberDef file_memberlist[] = {
1410 {"softspace", T_INT, OFF(f_softspace), 0,
1411 "flag indicating that a space needs to be printed; used by print"},
1412 {"mode", T_OBJECT, OFF(f_mode), RO,
1413 "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"},
1414 {"name", T_OBJECT, OFF(f_name), RO,
1415 "file name"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001416 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001417 {NULL} /* Sentinel */
1418};
1419
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001420static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001422{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001423 return PyInt_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001424}
1425
Guido van Rossum32d34c82001-09-20 21:45:26 +00001426static PyGetSetDef file_getsetlist[] = {
1427 {"closed", (getter)get_closed, NULL, "flag set if the file is closed"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428 {0},
1429};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001430
Guido van Rossum65967252001-04-21 13:20:18 +00001431static PyObject *
Guido van Rossum5b021842001-05-22 16:48:37 +00001432file_getiter(PyObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001433{
Guido van Rossum5b021842001-05-22 16:48:37 +00001434 return PyObject_CallMethod(f, "xreadlines", "");
Guido van Rossum65967252001-04-21 13:20:18 +00001435}
1436
Tim Peters59c9a642001-09-13 05:38:56 +00001437static PyObject *
1438file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1439{
Tim Peters44410012001-09-14 03:26:08 +00001440 PyObject *self;
1441 static PyObject *not_yet_string;
1442
1443 assert(type != NULL && type->tp_alloc != NULL);
1444
1445 if (not_yet_string == NULL) {
1446 not_yet_string = PyString_FromString("<uninitialized file>");
1447 if (not_yet_string == NULL)
1448 return NULL;
1449 }
1450
1451 self = type->tp_alloc(type, 0);
1452 if (self != NULL) {
1453 /* Always fill in the name and mode, so that nobody else
1454 needs to special-case NULLs there. */
1455 Py_INCREF(not_yet_string);
1456 ((PyFileObject *)self)->f_name = not_yet_string;
1457 Py_INCREF(not_yet_string);
1458 ((PyFileObject *)self)->f_mode = not_yet_string;
1459 }
1460 return self;
1461}
1462
1463static int
1464file_init(PyObject *self, PyObject *args, PyObject *kwds)
1465{
1466 PyFileObject *foself = (PyFileObject *)self;
1467 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001468 static char *kwlist[] = {"name", "mode", "buffering", 0};
1469 char *name = NULL;
1470 char *mode = "r";
1471 int bufsize = -1;
Tim Peters44410012001-09-14 03:26:08 +00001472
1473 assert(PyFile_Check(self));
1474 if (foself->f_fp != NULL) {
1475 /* Have to close the existing file first. */
1476 PyObject *closeresult = file_close(foself);
1477 if (closeresult == NULL)
1478 return -1;
1479 Py_DECREF(closeresult);
1480 }
Tim Peters59c9a642001-09-13 05:38:56 +00001481
1482 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1483 Py_FileSystemDefaultEncoding, &name,
1484 &mode, &bufsize))
Tim Peters44410012001-09-14 03:26:08 +00001485 return -1;
1486 if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL)
1487 goto Error;
1488 if (open_the_file(foself, name, mode) == NULL)
1489 goto Error;
1490 PyFile_SetBufSize(self, bufsize);
1491 goto Done;
1492
1493Error:
1494 ret = -1;
1495 /* fall through */
1496Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001497 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001498 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001499}
1500
Tim Peters59c9a642001-09-13 05:38:56 +00001501static char file_doc[] =
1502"file(name[, mode[, buffering]]) -> file object\n"
1503"\n"
1504"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1505"writing or appending. The file will be created if it doesn't exist\n"
1506"when opened for writing or appending; it will be truncated when\n"
1507"opened for writing. Add a 'b' to the mode for binary files.\n"
1508"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1509"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001510"buffered, and larger numbers specify the buffer size.\n"
1511"Note: open() is an alias for file().\n";
Tim Peters59c9a642001-09-13 05:38:56 +00001512
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001513PyTypeObject PyFile_Type = {
1514 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515 0,
1516 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001518 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001519 (destructor)file_dealloc, /* tp_dealloc */
1520 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001521 0, /* tp_getattr */
1522 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001523 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001524 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001525 0, /* tp_as_number */
1526 0, /* tp_as_sequence */
1527 0, /* tp_as_mapping */
1528 0, /* tp_hash */
1529 0, /* tp_call */
1530 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001531 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001532 0, /* tp_setattro */
1533 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001534 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001535 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001536 0, /* tp_traverse */
1537 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001538 0, /* tp_richcompare */
1539 0, /* tp_weaklistoffset */
Guido van Rossum5b021842001-05-22 16:48:37 +00001540 file_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001541 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001542 file_methods, /* tp_methods */
1543 file_memberlist, /* tp_members */
1544 file_getsetlist, /* tp_getset */
1545 0, /* tp_base */
1546 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001547 0, /* tp_descr_get */
1548 0, /* tp_descr_set */
1549 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001550 (initproc)file_init, /* tp_init */
1551 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001552 file_new, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001553 _PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001554};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001555
1556/* Interface for the 'soft space' between print items. */
1557
1558int
Fred Drakefd99de62000-07-09 05:02:18 +00001559PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001560{
1561 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001562 if (f == NULL) {
1563 /* Do nothing */
1564 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565 else if (PyFile_Check(f)) {
1566 oldflag = ((PyFileObject *)f)->f_softspace;
1567 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001568 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001569 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001570 PyObject *v;
1571 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001572 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001573 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001574 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001575 if (PyInt_Check(v))
1576 oldflag = PyInt_AsLong(v);
1577 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001578 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001579 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001580 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001581 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001582 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001583 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1584 PyErr_Clear();
1585 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001586 }
1587 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00001588 return oldflag;
1589}
Guido van Rossum3165fe61992-09-25 21:59:05 +00001590
1591/* Interfaces to write objects/strings to file-like objects */
1592
1593int
Fred Drakefd99de62000-07-09 05:02:18 +00001594PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00001595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001596 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001597 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001598 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001599 return -1;
1600 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001601 else if (PyFile_Check(f)) {
1602 FILE *fp = PyFile_AsFile(f);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001603 if (fp == NULL) {
1604 err_closed();
1605 return -1;
1606 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001607 return PyObject_Print(v, fp, flags);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001608 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001610 if (writer == NULL)
1611 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00001612 if (flags & Py_PRINT_RAW) {
1613 if (PyUnicode_Check(v)) {
1614 value = v;
1615 Py_INCREF(value);
1616 } else
1617 value = PyObject_Str(v);
1618 }
1619 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001620 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00001621 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001622 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00001623 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001624 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625 args = Py_BuildValue("(O)", value);
Guido van Rossume9eec541997-05-22 14:02:25 +00001626 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627 Py_DECREF(value);
1628 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00001629 return -1;
1630 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001631 result = PyEval_CallObject(writer, args);
1632 Py_DECREF(args);
1633 Py_DECREF(value);
1634 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001635 if (result == NULL)
1636 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001637 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001638 return 0;
1639}
1640
Guido van Rossum27a60b11997-05-22 22:25:11 +00001641int
Tim Petersc1bbcb82001-11-28 22:13:25 +00001642PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00001643{
1644 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00001645 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00001646 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00001647 PyErr_SetString(PyExc_SystemError,
1648 "null file for PyFile_WriteString");
1649 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001650 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001651 else if (PyFile_Check(f)) {
1652 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00001653 if (fp == NULL) {
1654 err_closed();
1655 return -1;
1656 }
1657 fputs(s, fp);
1658 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001659 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001660 else if (!PyErr_Occurred()) {
1661 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00001662 int err;
1663 if (v == NULL)
1664 return -1;
1665 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
1666 Py_DECREF(v);
1667 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001668 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00001669 else
1670 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001671}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00001672
1673/* Try to get a file-descriptor from a Python object. If the object
1674 is an integer or long integer, its value is returned. If not, the
1675 object's fileno() method is called if it exists; the method must return
1676 an integer or long integer, which is returned as the file descriptor value.
1677 -1 is returned on failure.
1678*/
1679
1680int PyObject_AsFileDescriptor(PyObject *o)
1681{
1682 int fd;
1683 PyObject *meth;
1684
1685 if (PyInt_Check(o)) {
1686 fd = PyInt_AsLong(o);
1687 }
1688 else if (PyLong_Check(o)) {
1689 fd = PyLong_AsLong(o);
1690 }
1691 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
1692 {
1693 PyObject *fno = PyEval_CallObject(meth, NULL);
1694 Py_DECREF(meth);
1695 if (fno == NULL)
1696 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00001697
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00001698 if (PyInt_Check(fno)) {
1699 fd = PyInt_AsLong(fno);
1700 Py_DECREF(fno);
1701 }
1702 else if (PyLong_Check(fno)) {
1703 fd = PyLong_AsLong(fno);
1704 Py_DECREF(fno);
1705 }
1706 else {
1707 PyErr_SetString(PyExc_TypeError,
1708 "fileno() returned a non-integer");
1709 Py_DECREF(fno);
1710 return -1;
1711 }
1712 }
1713 else {
1714 PyErr_SetString(PyExc_TypeError,
1715 "argument must be an int, or have a fileno() method.");
1716 return -1;
1717 }
1718
1719 if (fd < 0) {
1720 PyErr_Format(PyExc_ValueError,
1721 "file descriptor cannot be a negative integer (%i)",
1722 fd);
1723 return -1;
1724 }
1725 return fd;
1726}