blob: 6b7e01b91c8583c6946c888e83d802cd40c909e1 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* File object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00004#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossumff7e83d1999-08-27 20:39:37 +00006#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossum41498431999-01-07 22:09:51 +00007#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +00008#endif /* DONT_HAVE_SYS_TYPES_H */
Guido van Rossum41498431999-01-07 22:09:51 +00009
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010#ifdef MS_WINDOWS
Guido van Rossumb8199141997-05-06 15:23:24 +000011#define fileno _fileno
Tim Petersfb05db22002-03-11 00:24:00 +000012/* can simulate truncate with Win32 API functions; see file_truncate */
Guido van Rossumb8199141997-05-06 15:23:24 +000013#define HAVE_FTRUNCATE
Tim Peters7a1f9172002-07-14 22:14:19 +000014#define WIN32_LEAN_AND_MEAN
Tim Petersfb05db22002-03-11 00:24:00 +000015#include <windows.h>
Guido van Rossumb8199141997-05-06 15:23:24 +000016#endif
17
Mark Hammondc2e85bd2002-10-03 05:10:39 +000018#ifdef _MSC_VER
19/* Need GetVersion to see if on NT so safe to use _wfopen */
20#define WIN32_LEAN_AND_MEAN
21#include <windows.h>
22#endif /* _MSC_VER */
23
Andrew MacIntyrec4874392002-02-26 11:36:35 +000024#if defined(PYOS_OS2) && defined(PYCC_GCC)
25#include <io.h>
26#endif
27
Guido van Rossumc0b618a1997-05-02 03:12:38 +000028#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Guido van Rossumce5ba841991-03-06 13:06:18 +000029
Guido van Rossumff7e83d1999-08-27 20:39:37 +000030#ifndef DONT_HAVE_ERRNO_H
Guido van Rossumf1dc5661993-07-05 10:31:29 +000031#include <errno.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000032#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Jack Jansen7b8c7542002-04-14 20:12:41 +000034#ifdef HAVE_GETC_UNLOCKED
35#define GETC(f) getc_unlocked(f)
36#define FLOCKFILE(f) flockfile(f)
37#define FUNLOCKFILE(f) funlockfile(f)
38#else
39#define GETC(f) getc(f)
40#define FLOCKFILE(f)
41#define FUNLOCKFILE(f)
42#endif
43
Jack Jansen7b8c7542002-04-14 20:12:41 +000044/* Bits in f_newlinetypes */
45#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
46#define NEWLINE_CR 1 /* \r newline seen */
47#define NEWLINE_LF 2 /* \n newline seen */
48#define NEWLINE_CRLF 4 /* \r\n newline seen */
Trent Mickf29f47b2000-08-11 19:02:59 +000049
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050FILE *
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
Neil Schemenauered19b882002-03-23 02:06:50 +000068/* On Unix, fopen will succeed for directories.
69 In Python, there should be no file objects referring to
70 directories, so we need a check. */
71
72static PyFileObject*
73dircheck(PyFileObject* f)
74{
75#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
76 struct stat buf;
77 if (f->f_fp == NULL)
78 return f;
79 if (fstat(fileno(f->f_fp), &buf) == 0 &&
80 S_ISDIR(buf.st_mode)) {
81#ifdef HAVE_STRERROR
82 char *msg = strerror(EISDIR);
83#else
84 char *msg = "Is a directory";
85#endif
Tim Petersf1827cf2003-09-07 03:30:18 +000086 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
Jeremy Hylton8b735422002-08-14 21:01:41 +000087 EISDIR, msg);
Neil Schemenauered19b882002-03-23 02:06:50 +000088 PyErr_SetObject(PyExc_IOError, exc);
Neal Norwitz98cad482003-08-15 20:05:45 +000089 Py_XDECREF(exc);
Neil Schemenauered19b882002-03-23 02:06:50 +000090 return NULL;
91 }
92#endif
93 return f;
94}
95
Tim Peters59c9a642001-09-13 05:38:56 +000096
97static PyObject *
Nicholas Bastinabce8a62004-03-21 20:24:07 +000098fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
99 int (*close)(FILE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Tim Peters59c9a642001-09-13 05:38:56 +0000101 assert(f != NULL);
102 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +0000103 assert(f->f_fp == NULL);
104
105 Py_DECREF(f->f_name);
106 Py_DECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000107 Py_DECREF(f->f_encoding);
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000108
109 Py_INCREF (name);
110 f->f_name = name;
111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +0000113
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000114 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +0000115 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +0000116 f->f_binary = strchr(mode,'b') != NULL;
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000117 f->f_buf = NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000118 f->f_univ_newline = (strchr(mode, 'U') != NULL);
119 f->f_newlinetypes = NEWLINE_UNKNOWN;
120 f->f_skipnextlf = 0;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000121 Py_INCREF(Py_None);
122 f->f_encoding = Py_None;
Tim Petersf1827cf2003-09-07 03:30:18 +0000123
Tim Peters59c9a642001-09-13 05:38:56 +0000124 if (f->f_name == NULL || f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 f->f_fp = fp;
Neil Schemenauered19b882002-03-23 02:06:50 +0000127 f = dircheck(f);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000129}
130
Tim Peters59c9a642001-09-13 05:38:56 +0000131static PyObject *
132open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133{
Tim Peters59c9a642001-09-13 05:38:56 +0000134 assert(f != NULL);
135 assert(PyFile_Check(f));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000136#ifdef MS_WINDOWS
137 /* windows ignores the passed name in order to support Unicode */
138 assert(f->f_name != NULL);
139#else
Tim Peters59c9a642001-09-13 05:38:56 +0000140 assert(name != NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000141#endif
Tim Peters59c9a642001-09-13 05:38:56 +0000142 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +0000143 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000144
Tim Peters8fa45672001-09-13 21:01:29 +0000145 /* rexec.py can't stop a user from getting the file() constructor --
146 all they have to do is get *any* file object f, and then do
147 type(f). Here we prevent them from doing damage with it. */
148 if (PyEval_GetRestricted()) {
149 PyErr_SetString(PyExc_IOError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000150 "file() constructor not accessible in restricted mode");
Tim Peters8fa45672001-09-13 21:01:29 +0000151 return NULL;
152 }
Tim Petersa27a1502001-11-09 20:59:14 +0000153 errno = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000154#ifdef HAVE_FOPENRF
Guido van Rossuma08095a1991-02-13 23:25:27 +0000155 if (*mode == '*') {
156 FILE *fopenRF();
157 f->f_fp = fopenRF(name, mode+1);
158 }
159 else
160#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000161 {
Jack Jansen7b8c7542002-04-14 20:12:41 +0000162 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
163 mode = "rb";
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000164#ifdef MS_WINDOWS
165 if (PyUnicode_Check(f->f_name)) {
Tim Petersf1827cf2003-09-07 03:30:18 +0000166 PyObject *wmode;
167 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000168 if (f->f_name && wmode) {
169 Py_BEGIN_ALLOW_THREADS
170 /* PyUnicode_AS_UNICODE OK without thread
171 lock as it is a simple dereference. */
172 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
173 PyUnicode_AS_UNICODE(wmode));
174 Py_END_ALLOW_THREADS
175 }
176 Py_XDECREF(wmode);
177 }
178#endif
179 if (NULL == f->f_fp && NULL != name) {
180 Py_BEGIN_ALLOW_THREADS
181 f->f_fp = fopen(name, mode);
182 Py_END_ALLOW_THREADS
183 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000184 }
Guido van Rossuma08095a1991-02-13 23:25:27 +0000185 if (f->f_fp == NULL) {
Tim Peters2ea91112002-04-08 04:13:12 +0000186#ifdef _MSC_VER
187 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
188 * across all Windows flavors. When it sets EINVAL varies
189 * across Windows flavors, the exact conditions aren't
190 * documented, and the answer lies in the OS's implementation
191 * of Win32's CreateFile function (whose source is secret).
192 * Seems the best we can do is map EINVAL to ENOENT.
193 */
194 if (errno == 0) /* bad mode string */
195 errno = EINVAL;
196 else if (errno == EINVAL) /* unknown, but not a mode string */
197 errno = ENOENT;
198#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000199 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000200 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000201 mode);
202 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000203 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000204 f = NULL;
205 }
Tim Peters2ea91112002-04-08 04:13:12 +0000206 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000207 f = dircheck(f);
Tim Peters59c9a642001-09-13 05:38:56 +0000208 return (PyObject *)f;
209}
210
211PyObject *
212PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
213{
Tim Peters44410012001-09-14 03:26:08 +0000214 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
215 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000216 if (f != NULL) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000217 PyObject *o_name = PyString_FromString(name);
218 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000219 Py_DECREF(f);
220 f = NULL;
221 }
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000222 Py_DECREF(o_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000223 }
224 return (PyObject *) f;
225}
226
227PyObject *
228PyFile_FromString(char *name, char *mode)
229{
230 extern int fclose(FILE *);
231 PyFileObject *f;
232
233 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
234 if (f != NULL) {
235 if (open_the_file(f, name, mode) == NULL) {
236 Py_DECREF(f);
237 f = NULL;
238 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241}
242
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243void
Fred Drakefd99de62000-07-09 05:02:18 +0000244PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245{
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000246 PyFileObject *file = (PyFileObject *)f;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247 if (bufsize >= 0) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248 int type;
249 switch (bufsize) {
250 case 0:
251 type = _IONBF;
252 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000253#ifdef HAVE_SETVBUF
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254 case 1:
255 type = _IOLBF;
256 bufsize = BUFSIZ;
257 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000258#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259 default:
260 type = _IOFBF;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000261#ifndef HAVE_SETVBUF
262 bufsize = BUFSIZ;
263#endif
264 break;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265 }
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000266 fflush(file->f_fp);
267 if (type == _IONBF) {
268 PyMem_Free(file->f_setbuf);
269 file->f_setbuf = NULL;
270 } else {
271 file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
272 }
273#ifdef HAVE_SETVBUF
274 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000275#else /* !HAVE_SETVBUF */
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000276 setbuf(file->f_fp, file->f_setbuf);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000277#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278 }
279}
280
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000281/* Set the encoding used to output Unicode strings.
282 Returh 1 on success, 0 on failure. */
283
284int
285PyFile_SetEncoding(PyObject *f, const char *enc)
286{
287 PyFileObject *file = (PyFileObject*)f;
288 PyObject *str = PyString_FromString(enc);
289 if (!str)
290 return 0;
291 Py_DECREF(file->f_encoding);
292 file->f_encoding = str;
293 return 1;
294}
295
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000297err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000298{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000300 return NULL;
301}
302
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000303static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000304
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305/* Methods */
306
307static void
Fred Drakefd99de62000-07-09 05:02:18 +0000308file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000310 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000312 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314 }
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000315 PyMem_Free(f->f_setbuf);
Tim Peters44410012001-09-14 03:26:08 +0000316 Py_XDECREF(f->f_name);
317 Py_XDECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000318 Py_XDECREF(f->f_encoding);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000319 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000320 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321}
322
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000324file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000326 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000327#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000328 PyObject *ret = NULL;
329 PyObject *name;
330 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
331 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
332 f->f_fp == NULL ? "closed" : "open",
333 PyString_AsString(name),
334 PyString_AsString(f->f_mode),
335 f);
336 Py_XDECREF(name);
337 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000338#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000339 } else {
340 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000341 f->f_fp == NULL ? "closed" : "open",
342 PyString_AsString(f->f_name),
343 PyString_AsString(f->f_mode),
344 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000345 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000349file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000350{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000351 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000353 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000355 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000356 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000358 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 f->f_fp = NULL;
360 }
Martin v. Löwis7bbcde72003-09-07 20:42:29 +0000361 PyMem_Free(f->f_setbuf);
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000362 f->f_setbuf = NULL;
Guido van Rossumfebd5511992-03-04 16:39:24 +0000363 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000365 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366 return PyInt_FromLong((long)sts);
367 Py_INCREF(Py_None);
368 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000369}
370
Trent Mickf29f47b2000-08-11 19:02:59 +0000371
Guido van Rossumb8552162001-09-05 14:58:11 +0000372/* Our very own off_t-like type, 64-bit if possible */
373#if !defined(HAVE_LARGEFILE_SUPPORT)
374typedef off_t Py_off_t;
375#elif SIZEOF_OFF_T >= 8
376typedef off_t Py_off_t;
377#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000378typedef fpos_t Py_off_t;
379#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000380#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000381#endif
382
383
Trent Mickf29f47b2000-08-11 19:02:59 +0000384/* a portable fseek() function
385 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000386static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000387_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000388{
Guido van Rossumb8552162001-09-05 14:58:11 +0000389#if !defined(HAVE_LARGEFILE_SUPPORT)
390 return fseek(fp, offset, whence);
391#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000392 return fseeko(fp, offset, whence);
393#elif defined(HAVE_FSEEK64)
394 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000395#elif defined(__BEOS__)
396 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000397#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000398 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
399 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000400 fpos_t pos;
401 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000402 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000403#ifdef MS_WINDOWS
404 fflush(fp);
405 if (_lseeki64(fileno(fp), 0, 2) == -1)
406 return -1;
407#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000408 if (fseek(fp, 0, SEEK_END) != 0)
409 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000410#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000411 /* fall through */
412 case SEEK_CUR:
413 if (fgetpos(fp, &pos) != 0)
414 return -1;
415 offset += pos;
416 break;
417 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000418 }
419 return fsetpos(fp, &offset);
420#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000421#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000422#endif
423}
424
425
426/* a portable ftell() function
427 Return -1 on failure with errno set appropriately, current file
428 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000429static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000430_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000431{
Guido van Rossumb8552162001-09-05 14:58:11 +0000432#if !defined(HAVE_LARGEFILE_SUPPORT)
433 return ftell(fp);
434#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
435 return ftello(fp);
436#elif defined(HAVE_FTELL64)
437 return ftell64(fp);
438#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000439 fpos_t pos;
440 if (fgetpos(fp, &pos) != 0)
441 return -1;
442 return pos;
443#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000444#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000445#endif
446}
447
448
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000450file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000452 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000453 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000454 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000455 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000456
Guido van Rossumd7297e61992-07-06 14:19:26 +0000457 if (f->f_fp == NULL)
458 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000459 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000460 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000461 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000462 return NULL;
463#if !defined(HAVE_LARGEFILE_SUPPORT)
464 offset = PyInt_AsLong(offobj);
465#else
466 offset = PyLong_Check(offobj) ?
467 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
468#endif
469 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000470 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000473 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000474 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000476
Guido van Rossumff4949e1992-08-05 19:58:53 +0000477 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000479 clearerr(f->f_fp);
480 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000481 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000482 f->f_skipnextlf = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 Py_INCREF(Py_None);
484 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000485}
486
Trent Mickf29f47b2000-08-11 19:02:59 +0000487
Guido van Rossumd7047b31995-01-02 19:07:15 +0000488#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000490file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000491{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000492 Py_off_t newsize;
Tim Petersf1827cf2003-09-07 03:30:18 +0000493 PyObject *newsizeobj = NULL;
494 Py_off_t initialpos;
495 int ret;
Tim Peters86821b22001-01-07 21:19:34 +0000496
Guido van Rossumd7047b31995-01-02 19:07:15 +0000497 if (f->f_fp == NULL)
498 return err_closed();
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000499 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000500 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000501
Tim Petersf1827cf2003-09-07 03:30:18 +0000502 /* Get current file position. If the file happens to be open for
503 * update and the last operation was an input operation, C doesn't
504 * define what the later fflush() will do, but we promise truncate()
505 * won't change the current position (and fflush() *does* change it
506 * then at least on Windows). The easiest thing is to capture
507 * current pos now and seek back to it at the end.
508 */
509 Py_BEGIN_ALLOW_THREADS
510 errno = 0;
511 initialpos = _portable_ftell(f->f_fp);
512 Py_END_ALLOW_THREADS
513 if (initialpos == -1)
514 goto onioerror;
515
Tim Petersfb05db22002-03-11 00:24:00 +0000516 /* Set newsize to current postion if newsizeobj NULL, else to the
Tim Petersf1827cf2003-09-07 03:30:18 +0000517 * specified value.
518 */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000519 if (newsizeobj != NULL) {
520#if !defined(HAVE_LARGEFILE_SUPPORT)
521 newsize = PyInt_AsLong(newsizeobj);
522#else
523 newsize = PyLong_Check(newsizeobj) ?
524 PyLong_AsLongLong(newsizeobj) :
525 PyInt_AsLong(newsizeobj);
526#endif
527 if (PyErr_Occurred())
528 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000529 }
Tim Petersf1827cf2003-09-07 03:30:18 +0000530 else /* default to current position */
531 newsize = initialpos;
Tim Petersfb05db22002-03-11 00:24:00 +0000532
Tim Petersf1827cf2003-09-07 03:30:18 +0000533 /* Flush the stream. We're mixing stream-level I/O with lower-level
534 * I/O, and a flush may be necessary to synch both platform views
535 * of the current file state.
536 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000538 errno = 0;
539 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000541 if (ret != 0)
542 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000543
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000544#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000545 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000546 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000547 {
Tim Petersfb05db22002-03-11 00:24:00 +0000548 HANDLE hFile;
Tim Petersfb05db22002-03-11 00:24:00 +0000549
Tim Petersf1827cf2003-09-07 03:30:18 +0000550 /* Have to move current pos to desired endpoint on Windows. */
551 Py_BEGIN_ALLOW_THREADS
552 errno = 0;
553 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
554 Py_END_ALLOW_THREADS
555 if (ret)
556 goto onioerror;
Tim Petersfb05db22002-03-11 00:24:00 +0000557
Tim Peters8f01b682002-03-12 03:04:44 +0000558 /* Truncate. Note that this may grow the file! */
559 Py_BEGIN_ALLOW_THREADS
560 errno = 0;
561 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
Tim Petersf1827cf2003-09-07 03:30:18 +0000562 ret = hFile == (HANDLE)-1;
563 if (ret == 0) {
564 ret = SetEndOfFile(hFile) == 0;
565 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000566 errno = EACCES;
567 }
568 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000569 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000570 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000571 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000572#else
573 Py_BEGIN_ALLOW_THREADS
574 errno = 0;
575 ret = ftruncate(fileno(f->f_fp), newsize);
576 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000577 if (ret != 0)
578 goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000579#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000580
Tim Petersf1827cf2003-09-07 03:30:18 +0000581 /* Restore original file position. */
582 Py_BEGIN_ALLOW_THREADS
583 errno = 0;
584 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
585 Py_END_ALLOW_THREADS
586 if (ret)
587 goto onioerror;
588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 Py_INCREF(Py_None);
590 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000591
592onioerror:
593 PyErr_SetFromErrno(PyExc_IOError);
594 clearerr(f->f_fp);
595 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000596}
597#endif /* HAVE_FTRUNCATE */
598
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000600file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000601{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000602 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000603
Guido van Rossumd7297e61992-07-06 14:19:26 +0000604 if (f->f_fp == NULL)
605 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000607 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000608 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000610 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000612 clearerr(f->f_fp);
613 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000614 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000615 if (f->f_skipnextlf) {
616 int c;
617 c = GETC(f->f_fp);
618 if (c == '\n') {
619 pos++;
620 f->f_skipnextlf = 0;
621 } else if (c != EOF) ungetc(c, f->f_fp);
622 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000623#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000624 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000625#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000626 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000627#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000628}
629
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000631file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000632{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000633 if (f->f_fp == NULL)
634 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000636}
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000639file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000640{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000641 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000642
Guido van Rossumd7297e61992-07-06 14:19:26 +0000643 if (f->f_fp == NULL)
644 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000646 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000647 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000649 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000651 clearerr(f->f_fp);
652 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000653 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 Py_INCREF(Py_None);
655 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000656}
657
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000659file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000660{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000661 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000662 if (f->f_fp == NULL)
663 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000665 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000667 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000668}
669
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000670
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000671#if BUFSIZ < 8192
672#define SMALLCHUNK 8192
673#else
674#define SMALLCHUNK BUFSIZ
675#endif
676
Guido van Rossum3c259041999-01-14 19:00:14 +0000677#if SIZEOF_INT < 4
678#define BIGCHUNK (512 * 32)
679#else
680#define BIGCHUNK (512 * 1024)
681#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000682
683static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000684new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000685{
686#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000687 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000688 struct stat st;
689 if (fstat(fileno(f->f_fp), &st) == 0) {
690 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000691 /* The following is not a bug: we really need to call lseek()
692 *and* ftell(). The reason is that some stdio libraries
693 mistakenly flush their buffer when ftell() is called and
694 the lseek() call it makes fails, thereby throwing away
695 data that cannot be recovered in any way. To avoid this,
696 we first test lseek(), and only call ftell() if lseek()
697 works. We can't use the lseek() value either, because we
698 need to take the amount of buffered data into account.
699 (Yet another reason why stdio stinks. :-) */
Guido van Rossum91aaa921998-05-05 22:21:35 +0000700 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000701 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000702 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000703 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000704 if (pos < 0)
705 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000706 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000707 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000708 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000709 }
710#endif
711 if (currentsize > SMALLCHUNK) {
712 /* Keep doubling until we reach BIGCHUNK;
713 then keep adding BIGCHUNK. */
714 if (currentsize <= BIGCHUNK)
715 return currentsize + currentsize;
716 else
717 return currentsize + BIGCHUNK;
718 }
719 return currentsize + SMALLCHUNK;
720}
721
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000722#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
723#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
724#else
725#ifdef EWOULDBLOCK
726#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
727#else
728#ifdef EAGAIN
729#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
730#else
731#define BLOCKED_ERRNO(x) 0
732#endif
733#endif
734#endif
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000737file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000738{
Guido van Rossum789a1611997-05-10 22:33:55 +0000739 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000740 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000742
Guido van Rossumd7297e61992-07-06 14:19:26 +0000743 if (f->f_fp == NULL)
744 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000745 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000746 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000747 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000748 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000749 else
750 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000751 if (buffersize > INT_MAX) {
752 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000753 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000754 return NULL;
755 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000756 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000757 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000759 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000760 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000761 Py_BEGIN_ALLOW_THREADS
762 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000763 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000764 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000765 Py_END_ALLOW_THREADS
766 if (chunksize == 0) {
767 if (!ferror(f->f_fp))
768 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000769 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000770 /* When in non-blocking mode, data shouldn't
771 * be discarded if a blocking signal was
772 * received. That will also happen if
773 * chunksize != 0, but bytesread < buffersize. */
774 if (bytesread > 0 && BLOCKED_ERRNO(errno))
775 break;
776 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000777 Py_DECREF(v);
778 return NULL;
779 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000780 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000781 if (bytesread < buffersize) {
782 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000783 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000784 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000785 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000786 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000787 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000788 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000789 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000790 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000791 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000792 }
793 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000794 if (bytesread != buffersize)
795 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796 return v;
797}
798
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000799static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000800file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000801{
802 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000803 int ntodo;
804 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000805
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000806 if (f->f_fp == NULL)
807 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000808 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000809 return NULL;
810 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000811 while (ntodo > 0) {
812 Py_BEGIN_ALLOW_THREADS
813 errno = 0;
Tim Petersf1827cf2003-09-07 03:30:18 +0000814 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000815 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000816 Py_END_ALLOW_THREADS
817 if (nnow == 0) {
818 if (!ferror(f->f_fp))
819 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000820 PyErr_SetFromErrno(PyExc_IOError);
821 clearerr(f->f_fp);
822 return NULL;
823 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000824 ndone += nnow;
825 ntodo -= nnow;
826 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000827 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000828}
829
Tim Peters86821b22001-01-07 21:19:34 +0000830/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000831Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000832
833Under MSVC 6:
834
Tim Peters1c733232001-01-08 04:02:07 +0000835+ MS threadsafe getc is very slow (multiple layers of function calls before+
836 after each character, to lock+unlock the stream).
837+ The stream-locking functions are MS-internal -- can't access them from user
838 code.
839+ There's nothing Tim could find in the MS C or platform SDK libraries that
840 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000841+ MS fgets locks/unlocks only once per line; it's the only hook we have.
842
843So we use fgets for speed(!), despite that it's painful.
844
845MS realloc is also slow.
846
Tim Petersf29b64d2001-01-15 06:33:19 +0000847Reports from other platforms on this method vs getc_unlocked (which MS doesn't
848have):
849 Linux a wash
850 Solaris a wash
851 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000852
Tim Petersf29b64d2001-01-15 06:33:19 +0000853CAUTION: The C std isn't clear about this: in those cases where fgets
854writes something into the buffer, can it write into any position beyond the
855required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
856known on which it does; and it would be a strange way to code fgets. Still,
857getline_via_fgets may not work correctly if it does. The std test
858test_bufio.py should fail if platform fgets() routinely writes beyond the
859trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000860**************************************************************************/
861
Tim Petersf29b64d2001-01-15 06:33:19 +0000862/* Use this routine if told to, or by default on non-get_unlocked()
863 * platforms unless told not to. Yikes! Let's spell that out:
864 * On a platform with getc_unlocked():
865 * By default, use getc_unlocked().
866 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
867 * On a platform without getc_unlocked():
868 * By default, use fgets().
869 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
870 */
871#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
872#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000873#endif
874
Tim Petersf29b64d2001-01-15 06:33:19 +0000875#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
876#undef USE_FGETS_IN_GETLINE
877#endif
878
879#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000880static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000881getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000882{
Tim Peters15b83852001-01-08 00:53:12 +0000883/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000884 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
885 * to fill this much of the buffer with a known value in order to figure out
886 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
887 * than "most" lines, we waste time filling unused buffer slots. 100 is
888 * surely adequate for most peoples' email archives, chewing over source code,
889 * etc -- "regular old text files".
890 * MAXBUFSIZE is the maximum line length that lets us get away with the less
891 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
892 * cautions about boosting that. 300 was chosen because the worst real-life
893 * text-crunching job reported on Python-Dev was a mail-log crawler where over
894 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000895 */
Tim Peters142297a2001-01-15 10:36:56 +0000896#define INITBUFSIZE 100
897#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000898 char* p; /* temp */
899 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000900 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000901 char* pvfree; /* address of next free slot */
902 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000903 size_t nfree; /* # of free buffer slots; pvend-pvfree */
904 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000905 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000906
Tim Peters15b83852001-01-08 00:53:12 +0000907 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000908 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000909 */
Tim Peters142297a2001-01-15 10:36:56 +0000910 total_v_size = INITBUFSIZE; /* start small and pray */
911 pvfree = buf;
912 for (;;) {
913 Py_BEGIN_ALLOW_THREADS
914 pvend = buf + total_v_size;
915 nfree = pvend - pvfree;
916 memset(pvfree, '\n', nfree);
917 p = fgets(pvfree, nfree, fp);
918 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000919
Tim Peters142297a2001-01-15 10:36:56 +0000920 if (p == NULL) {
921 clearerr(fp);
922 if (PyErr_CheckSignals())
923 return NULL;
924 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000925 return v;
926 }
Tim Peters142297a2001-01-15 10:36:56 +0000927 /* fgets read *something* */
928 p = memchr(pvfree, '\n', nfree);
929 if (p != NULL) {
930 /* Did the \n come from fgets or from us?
931 * Since fgets stops at the first \n, and then writes
932 * \0, if it's from fgets a \0 must be next. But if
933 * that's so, it could not have come from us, since
934 * the \n's we filled the buffer with have only more
935 * \n's to the right.
936 */
937 if (p+1 < pvend && *(p+1) == '\0') {
938 /* It's from fgets: we win! In particular,
939 * we haven't done any mallocs yet, and can
940 * build the final result on the first try.
941 */
942 ++p; /* include \n from fgets */
943 }
944 else {
945 /* Must be from us: fgets didn't fill the
946 * buffer and didn't find a newline, so it
947 * must be the last and newline-free line of
948 * the file.
949 */
950 assert(p > pvfree && *(p-1) == '\0');
951 --p; /* don't include \0 from fgets */
952 }
953 v = PyString_FromStringAndSize(buf, p - buf);
954 return v;
955 }
956 /* yuck: fgets overwrote all the newlines, i.e. the entire
957 * buffer. So this line isn't over yet, or maybe it is but
958 * we're exactly at EOF. If we haven't already, try using the
959 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000960 */
Tim Peters142297a2001-01-15 10:36:56 +0000961 assert(*(pvend-1) == '\0');
962 if (pvfree == buf) {
963 pvfree = pvend - 1; /* overwrite trailing null */
964 total_v_size = MAXBUFSIZE;
965 }
966 else
967 break;
Tim Peters86821b22001-01-07 21:19:34 +0000968 }
Tim Peters142297a2001-01-15 10:36:56 +0000969
970 /* The stack buffer isn't big enough; malloc a string object and read
971 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000972 */
Tim Petersddea2082002-03-23 10:03:50 +0000973 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +0000974 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000975 if (v == NULL)
976 return v;
977 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +0000978 memcpy(BUF(v), buf, MAXBUFSIZE-1);
979 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +0000980
981 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +0000982 * after setting p one beyond the end of the line. The code here is
983 * very much like the code above, except reads into v's buffer; see
984 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +0000985 */
986 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +0000987 Py_BEGIN_ALLOW_THREADS
988 pvend = BUF(v) + total_v_size;
989 nfree = pvend - pvfree;
990 memset(pvfree, '\n', nfree);
991 p = fgets(pvfree, nfree, fp);
992 Py_END_ALLOW_THREADS
993
994 if (p == NULL) {
995 clearerr(fp);
996 if (PyErr_CheckSignals()) {
997 Py_DECREF(v);
998 return NULL;
999 }
1000 p = pvfree;
1001 break;
1002 }
Tim Peters86821b22001-01-07 21:19:34 +00001003 p = memchr(pvfree, '\n', nfree);
1004 if (p != NULL) {
1005 if (p+1 < pvend && *(p+1) == '\0') {
1006 /* \n came from fgets */
1007 ++p;
1008 break;
1009 }
1010 /* \n came from us; last line of file, no newline */
1011 assert(p > pvfree && *(p-1) == '\0');
1012 --p;
1013 break;
1014 }
1015 /* expand buffer and try again */
1016 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001017 increment = total_v_size >> 2; /* mild exponential growth */
1018 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001019 if (total_v_size > INT_MAX) {
1020 PyErr_SetString(PyExc_OverflowError,
1021 "line is longer than a Python string can hold");
1022 Py_DECREF(v);
1023 return NULL;
1024 }
1025 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1026 return NULL;
1027 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001028 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001029 }
1030 if (BUF(v) + total_v_size != p)
1031 _PyString_Resize(&v, p - BUF(v));
1032 return v;
1033#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001034#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001035}
Tim Petersf29b64d2001-01-15 06:33:19 +00001036#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001037
Guido van Rossum0bd24411991-04-04 15:21:57 +00001038/* Internal routine to get a line.
1039 Size argument interpretation:
1040 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001041 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001042*/
1043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001045get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001047 FILE *fp = f->f_fp;
1048 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001049 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001050 size_t total_v_size; /* total # of slots in buffer */
1051 size_t used_v_size; /* # used slots in buffer */
1052 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001054 int newlinetypes = f->f_newlinetypes;
1055 int skipnextlf = f->f_skipnextlf;
1056 int univ_newline = f->f_univ_newline;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001057
Jack Jansen7b8c7542002-04-14 20:12:41 +00001058#if defined(USE_FGETS_IN_GETLINE)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001059 if (n <= 0 && !univ_newline )
Tim Petersf29b64d2001-01-15 06:33:19 +00001060 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001061#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001062 total_v_size = n > 0 ? n : 100;
1063 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001064 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001065 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001066 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001067 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001068
Guido van Rossumce5ba841991-03-06 13:06:18 +00001069 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001070 Py_BEGIN_ALLOW_THREADS
1071 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001072 if (univ_newline) {
1073 c = 'x'; /* Shut up gcc warning */
1074 while ( buf != end && (c = GETC(fp)) != EOF ) {
1075 if (skipnextlf ) {
1076 skipnextlf = 0;
1077 if (c == '\n') {
Tim Petersf1827cf2003-09-07 03:30:18 +00001078 /* Seeing a \n here with
1079 * skipnextlf true means we
Jeremy Hylton8b735422002-08-14 21:01:41 +00001080 * saw a \r before.
1081 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001082 newlinetypes |= NEWLINE_CRLF;
1083 c = GETC(fp);
1084 if (c == EOF) break;
1085 } else {
1086 newlinetypes |= NEWLINE_CR;
1087 }
1088 }
1089 if (c == '\r') {
1090 skipnextlf = 1;
1091 c = '\n';
1092 } else if ( c == '\n')
1093 newlinetypes |= NEWLINE_LF;
1094 *buf++ = c;
1095 if (c == '\n') break;
1096 }
1097 if ( c == EOF && skipnextlf )
1098 newlinetypes |= NEWLINE_CR;
1099 } else /* If not universal newlines use the normal loop */
Guido van Rossum1187aa42001-01-05 14:43:05 +00001100 while ((c = GETC(fp)) != EOF &&
1101 (*buf++ = c) != '\n' &&
1102 buf != end)
1103 ;
1104 FUNLOCKFILE(fp);
1105 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001106 f->f_newlinetypes = newlinetypes;
1107 f->f_skipnextlf = skipnextlf;
Guido van Rossum1187aa42001-01-05 14:43:05 +00001108 if (c == '\n')
1109 break;
1110 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001111 if (ferror(fp)) {
1112 PyErr_SetFromErrno(PyExc_IOError);
1113 clearerr(fp);
1114 Py_DECREF(v);
1115 return NULL;
1116 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001117 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001119 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001120 return NULL;
1121 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001122 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001123 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001124 /* Must be because buf == end */
1125 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001126 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001127 used_v_size = total_v_size;
1128 increment = total_v_size >> 2; /* mild exponential growth */
1129 total_v_size += increment;
1130 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001131 PyErr_SetString(PyExc_OverflowError,
1132 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001133 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001134 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001135 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001136 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001137 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001138 buf = BUF(v) + used_v_size;
1139 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001141
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001142 used_v_size = buf - BUF(v);
1143 if (used_v_size != total_v_size)
1144 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145 return v;
1146}
1147
Guido van Rossum0bd24411991-04-04 15:21:57 +00001148/* External C interface */
1149
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001151PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001152{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001153 PyObject *result;
1154
Guido van Rossum3165fe61992-09-25 21:59:05 +00001155 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001156 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001157 return NULL;
1158 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001159
1160 if (PyFile_Check(f)) {
1161 if (((PyFileObject*)f)->f_fp == NULL)
1162 return err_closed();
1163 result = get_line((PyFileObject *)f, n);
1164 }
1165 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 PyObject *reader;
1167 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001168
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001169 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001170 if (reader == NULL)
1171 return NULL;
1172 if (n <= 0)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001173 args = PyTuple_New(0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001174 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001176 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001177 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001178 return NULL;
1179 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180 result = PyEval_CallObject(reader, args);
1181 Py_DECREF(reader);
1182 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001183 if (result != NULL && !PyString_Check(result) &&
1184 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001186 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001188 "object.readline() returned non-string");
1189 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001190 }
1191
1192 if (n < 0 && result != NULL && PyString_Check(result)) {
1193 char *s = PyString_AS_STRING(result);
1194 int len = PyString_GET_SIZE(result);
1195 if (len == 0) {
1196 Py_DECREF(result);
1197 result = NULL;
1198 PyErr_SetString(PyExc_EOFError,
1199 "EOF when reading a line");
1200 }
1201 else if (s[len-1] == '\n') {
1202 if (result->ob_refcnt == 1)
1203 _PyString_Resize(&result, len-1);
1204 else {
1205 PyObject *v;
1206 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001208 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001209 }
1210 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001211 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001212#ifdef Py_USING_UNICODE
1213 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1214 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1215 int len = PyUnicode_GET_SIZE(result);
1216 if (len == 0) {
1217 Py_DECREF(result);
1218 result = NULL;
1219 PyErr_SetString(PyExc_EOFError,
1220 "EOF when reading a line");
1221 }
1222 else if (s[len-1] == '\n') {
1223 if (result->ob_refcnt == 1)
1224 PyUnicode_Resize(&result, len-1);
1225 else {
1226 PyObject *v;
1227 v = PyUnicode_FromUnicode(s, len-1);
1228 Py_DECREF(result);
1229 result = v;
1230 }
1231 }
1232 }
1233#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001234 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001235}
1236
1237/* Python method */
1238
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001240file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001241{
Guido van Rossum789a1611997-05-10 22:33:55 +00001242 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001243
Guido van Rossumd7297e61992-07-06 14:19:26 +00001244 if (f->f_fp == NULL)
1245 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001246 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001247 return NULL;
1248 if (n == 0)
1249 return PyString_FromString("");
1250 if (n < 0)
1251 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001252 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001253}
1254
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001256file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001257{
Guido van Rossum789a1611997-05-10 22:33:55 +00001258 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001259 PyObject *list;
1260 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001261 char small_buffer[SMALLCHUNK];
1262 char *buffer = small_buffer;
1263 size_t buffersize = SMALLCHUNK;
1264 PyObject *big_buffer = NULL;
1265 size_t nfilled = 0;
1266 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001267 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001268 char *p, *q, *end;
1269 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001270 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001271
Guido van Rossumd7297e61992-07-06 14:19:26 +00001272 if (f->f_fp == NULL)
1273 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001274 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001275 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001277 return NULL;
1278 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001279 if (shortread)
1280 nread = 0;
1281 else {
1282 Py_BEGIN_ALLOW_THREADS
1283 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001284 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001285 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001286 Py_END_ALLOW_THREADS
1287 shortread = (nread < buffersize-nfilled);
1288 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001289 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001290 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001291 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001292 break;
1293 PyErr_SetFromErrno(PyExc_IOError);
1294 clearerr(f->f_fp);
1295 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001297 list = NULL;
1298 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001299 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001300 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001301 p = memchr(buffer+nfilled, '\n', nread);
1302 if (p == NULL) {
1303 /* Need a larger buffer to fit this line */
1304 nfilled += nread;
1305 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001306 if (buffersize > INT_MAX) {
1307 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001308 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001309 goto error;
1310 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001311 if (big_buffer == NULL) {
1312 /* Create the big buffer */
1313 big_buffer = PyString_FromStringAndSize(
1314 NULL, buffersize);
1315 if (big_buffer == NULL)
1316 goto error;
1317 buffer = PyString_AS_STRING(big_buffer);
1318 memcpy(buffer, small_buffer, nfilled);
1319 }
1320 else {
1321 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001322 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1323 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001324 buffer = PyString_AS_STRING(big_buffer);
1325 }
1326 continue;
1327 }
1328 end = buffer+nfilled+nread;
1329 q = buffer;
1330 do {
1331 /* Process complete lines */
1332 p++;
1333 line = PyString_FromStringAndSize(q, p-q);
1334 if (line == NULL)
1335 goto error;
1336 err = PyList_Append(list, line);
1337 Py_DECREF(line);
1338 if (err != 0)
1339 goto error;
1340 q = p;
1341 p = memchr(q, '\n', end-q);
1342 } while (p != NULL);
1343 /* Move the remaining incomplete line to the start */
1344 nfilled = end-q;
1345 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001346 if (sizehint > 0)
1347 if (totalread >= (size_t)sizehint)
1348 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001349 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001350 if (nfilled != 0) {
1351 /* Partial last line */
1352 line = PyString_FromStringAndSize(buffer, nfilled);
1353 if (line == NULL)
1354 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001355 if (sizehint > 0) {
1356 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001357 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001358 if (rest == NULL) {
1359 Py_DECREF(line);
1360 goto error;
1361 }
1362 PyString_Concat(&line, rest);
1363 Py_DECREF(rest);
1364 if (line == NULL)
1365 goto error;
1366 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001367 err = PyList_Append(list, line);
1368 Py_DECREF(line);
1369 if (err != 0)
1370 goto error;
1371 }
1372 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001373 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001374 return list;
1375}
1376
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001378file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001380 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001382 if (f->f_fp == NULL)
1383 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001384 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001385 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001386 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001388 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001389 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001391 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001393 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394 return NULL;
1395 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396 Py_INCREF(Py_None);
1397 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398}
1399
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001400static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001401file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001402{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001403#define CHUNKSIZE 1000
1404 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001405 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001406 PyObject *result;
1407 int i, j, index, len, nwritten, islist;
1408
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001409 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001410 if (f->f_fp == NULL)
1411 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001412
1413 result = NULL;
1414 list = NULL;
1415 islist = PyList_Check(seq);
1416 if (islist)
1417 it = NULL;
1418 else {
1419 it = PyObject_GetIter(seq);
1420 if (it == NULL) {
1421 PyErr_SetString(PyExc_TypeError,
1422 "writelines() requires an iterable argument");
1423 return NULL;
1424 }
1425 /* From here on, fail by going to error, to reclaim "it". */
1426 list = PyList_New(CHUNKSIZE);
1427 if (list == NULL)
1428 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001429 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001430
1431 /* Strategy: slurp CHUNKSIZE lines into a private list,
1432 checking that they are all strings, then write that list
1433 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001434 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001435 if (islist) {
1436 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001437 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001438 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001439 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001440 j = PyList_GET_SIZE(list);
1441 }
1442 else {
1443 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001444 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001445 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001446 if (PyErr_Occurred())
1447 goto error;
1448 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001449 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001450 PyList_SetItem(list, j, line);
1451 }
1452 }
1453 if (j == 0)
1454 break;
1455
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001456 /* Check that all entries are indeed strings. If not,
1457 apply the same rules as for file.write() and
1458 convert the results to strings. This is slow, but
1459 seems to be the only way since all conversion APIs
1460 could potentially execute Python code. */
1461 for (i = 0; i < j; i++) {
1462 PyObject *v = PyList_GET_ITEM(list, i);
1463 if (!PyString_Check(v)) {
1464 const char *buffer;
1465 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001466 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001467 PyObject_AsReadBuffer(v,
1468 (const void**)&buffer,
1469 &len)) ||
1470 PyObject_AsCharBuffer(v,
1471 &buffer,
1472 &len))) {
1473 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001474 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001475 goto error;
1476 }
1477 line = PyString_FromStringAndSize(buffer,
1478 len);
1479 if (line == NULL)
1480 goto error;
1481 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001482 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001483 }
1484 }
1485
1486 /* Since we are releasing the global lock, the
1487 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001488 Py_BEGIN_ALLOW_THREADS
1489 f->f_softspace = 0;
1490 errno = 0;
1491 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001492 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001493 len = PyString_GET_SIZE(line);
1494 nwritten = fwrite(PyString_AS_STRING(line),
1495 1, len, f->f_fp);
1496 if (nwritten != len) {
1497 Py_BLOCK_THREADS
1498 PyErr_SetFromErrno(PyExc_IOError);
1499 clearerr(f->f_fp);
1500 goto error;
1501 }
1502 }
1503 Py_END_ALLOW_THREADS
1504
1505 if (j < CHUNKSIZE)
1506 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001507 }
1508
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001509 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001510 result = Py_None;
1511 error:
1512 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001513 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001514 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001515#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001516}
1517
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001518static PyObject *
1519file_getiter(PyFileObject *f)
1520{
1521 if (f->f_fp == NULL)
1522 return err_closed();
1523 Py_INCREF(f);
1524 return (PyObject *)f;
1525}
1526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001528"readline([size]) -> next line from the file, as a string.\n"
1529"\n"
1530"Retain newline. A non-negative size argument limits the maximum\n"
1531"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001535"read([size]) -> read at most size bytes, returned as a string.\n"
1536"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001537"If the size argument is negative or omitted, read until EOF is reached.\n"
1538"Notice that when in non-blocking mode, less data than what was requested\n"
1539"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001541PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001542"write(str) -> None. Write string str to file.\n"
1543"\n"
1544"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001548"fileno() -> integer \"file descriptor\".\n"
1549"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001553"seek(offset[, whence]) -> None. Move to new file position.\n"
1554"\n"
1555"Argument offset is a byte count. Optional argument whence defaults to\n"
1556"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1557"(move relative to current position, positive or negative), and 2 (move\n"
1558"relative to end of file, usually negative, although many platforms allow\n"
Martin v. Löwis849a9722003-10-18 09:38:01 +00001559"seeking beyond the end of a file). If the file is opened in text mode,\n"
1560"only offsets returned by tell() are legal. Use of other offsets causes\n"
1561"undefined behavior."
Tim Petersefc3a3a2001-09-20 07:55:22 +00001562"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001564
Guido van Rossumd7047b31995-01-02 19:07:15 +00001565#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001567"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1568"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001569"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001570#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001571
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572PyDoc_STRVAR(tell_doc,
1573"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001575PyDoc_STRVAR(readinto_doc,
1576"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001578PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001579"readlines([size]) -> list of strings, each a line from the file.\n"
1580"\n"
1581"Call readline() repeatedly and return a list of the lines so read.\n"
1582"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001583"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001585PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001586"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001587"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001588"For backward compatibility. File objects now include the performance\n"
1589"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001592"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001593"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001594"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(flush_doc,
1598"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001601"close() -> None or (perhaps) an integer. Close the file.\n"
1602"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001603"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001604"further I/O operations. close() may be called more than once without\n"
1605"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(isatty_doc,
1609"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001610
1611static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001612 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1613 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1614 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1615 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1616 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001617#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001618 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001619#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001620 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1621 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1622 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1623 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1624 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1625 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1626 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1627 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1628 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001629};
1630
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001631#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001632
Guido van Rossum6f799372001-09-20 20:46:19 +00001633static PyMemberDef file_memberlist[] = {
1634 {"softspace", T_INT, OFF(f_softspace), 0,
1635 "flag indicating that a space needs to be printed; used by print"},
1636 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001637 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001638 {"name", T_OBJECT, OFF(f_name), RO,
1639 "file name"},
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001640 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1641 "file encoding"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001642 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001643 {NULL} /* Sentinel */
1644};
1645
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001647get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001649 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001650}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001651static PyObject *
1652get_newlines(PyFileObject *f, void *closure)
1653{
1654 switch (f->f_newlinetypes) {
1655 case NEWLINE_UNKNOWN:
1656 Py_INCREF(Py_None);
1657 return Py_None;
1658 case NEWLINE_CR:
1659 return PyString_FromString("\r");
1660 case NEWLINE_LF:
1661 return PyString_FromString("\n");
1662 case NEWLINE_CR|NEWLINE_LF:
1663 return Py_BuildValue("(ss)", "\r", "\n");
1664 case NEWLINE_CRLF:
1665 return PyString_FromString("\r\n");
1666 case NEWLINE_CR|NEWLINE_CRLF:
1667 return Py_BuildValue("(ss)", "\r", "\r\n");
1668 case NEWLINE_LF|NEWLINE_CRLF:
1669 return Py_BuildValue("(ss)", "\n", "\r\n");
1670 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1671 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1672 default:
Tim Petersf1827cf2003-09-07 03:30:18 +00001673 PyErr_Format(PyExc_SystemError,
1674 "Unknown newlines value 0x%x\n",
Jeremy Hylton8b735422002-08-14 21:01:41 +00001675 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001676 return NULL;
1677 }
1678}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001679
Guido van Rossum32d34c82001-09-20 21:45:26 +00001680static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001681 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Tim Petersf1827cf2003-09-07 03:30:18 +00001682 {"newlines", (getter)get_newlines, NULL,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001683 "end-of-line convention used in this file"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001684 {0},
1685};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001686
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001687static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001688drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001689{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001690 if (f->f_buf != NULL) {
1691 PyMem_Free(f->f_buf);
1692 f->f_buf = NULL;
1693 }
Guido van Rossum65967252001-04-21 13:20:18 +00001694}
1695
Tim Petersf1827cf2003-09-07 03:30:18 +00001696/* Make sure that file has a readahead buffer with at least one byte
1697 (unless at EOF) and no more than bufsize. Returns negative value on
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001698 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001699static int
1700readahead(PyFileObject *f, int bufsize)
1701{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001702 int chunksize;
1703
1704 if (f->f_buf != NULL) {
Tim Petersf1827cf2003-09-07 03:30:18 +00001705 if( (f->f_bufend - f->f_bufptr) >= 1)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001706 return 0;
1707 else
1708 drop_readahead(f);
1709 }
1710 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1711 return -1;
1712 }
1713 Py_BEGIN_ALLOW_THREADS
1714 errno = 0;
1715 chunksize = Py_UniversalNewlineFread(
1716 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1717 Py_END_ALLOW_THREADS
1718 if (chunksize == 0) {
1719 if (ferror(f->f_fp)) {
1720 PyErr_SetFromErrno(PyExc_IOError);
1721 clearerr(f->f_fp);
1722 drop_readahead(f);
1723 return -1;
1724 }
1725 }
1726 f->f_bufptr = f->f_buf;
1727 f->f_bufend = f->f_buf + chunksize;
1728 return 0;
1729}
1730
1731/* Used by file_iternext. The returned string will start with 'skip'
Tim Petersf1827cf2003-09-07 03:30:18 +00001732 uninitialized bytes followed by the remainder of the line. Don't be
1733 horrified by the recursive call: maximum recursion depth is limited by
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001734 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1735
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001736static PyStringObject *
1737readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1738{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001739 PyStringObject* s;
1740 char *bufptr;
1741 char *buf;
1742 int len;
1743
1744 if (f->f_buf == NULL)
Tim Petersf1827cf2003-09-07 03:30:18 +00001745 if (readahead(f, bufsize) < 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001746 return NULL;
1747
1748 len = f->f_bufend - f->f_bufptr;
Tim Petersf1827cf2003-09-07 03:30:18 +00001749 if (len == 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001750 return (PyStringObject *)
1751 PyString_FromStringAndSize(NULL, skip);
1752 bufptr = memchr(f->f_bufptr, '\n', len);
1753 if (bufptr != NULL) {
1754 bufptr++; /* Count the '\n' */
1755 len = bufptr - f->f_bufptr;
1756 s = (PyStringObject *)
1757 PyString_FromStringAndSize(NULL, skip+len);
Tim Petersf1827cf2003-09-07 03:30:18 +00001758 if (s == NULL)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001759 return NULL;
1760 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1761 f->f_bufptr = bufptr;
1762 if (bufptr == f->f_bufend)
1763 drop_readahead(f);
1764 } else {
1765 bufptr = f->f_bufptr;
1766 buf = f->f_buf;
1767 f->f_buf = NULL; /* Force new readahead buffer */
1768 s = readahead_get_line_skip(
1769 f, skip+len, bufsize + (bufsize>>2) );
1770 if (s == NULL) {
1771 PyMem_Free(buf);
1772 return NULL;
1773 }
1774 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1775 PyMem_Free(buf);
1776 }
1777 return s;
1778}
1779
1780/* A larger buffer size may actually decrease performance. */
1781#define READAHEAD_BUFSIZE 8192
1782
1783static PyObject *
1784file_iternext(PyFileObject *f)
1785{
1786 PyStringObject* l;
1787
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001788 if (f->f_fp == NULL)
1789 return err_closed();
1790
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001791 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1792 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1793 Py_XDECREF(l);
1794 return NULL;
1795 }
1796 return (PyObject *)l;
1797}
1798
1799
Tim Peters59c9a642001-09-13 05:38:56 +00001800static PyObject *
1801file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1802{
Tim Peters44410012001-09-14 03:26:08 +00001803 PyObject *self;
1804 static PyObject *not_yet_string;
1805
1806 assert(type != NULL && type->tp_alloc != NULL);
1807
1808 if (not_yet_string == NULL) {
1809 not_yet_string = PyString_FromString("<uninitialized file>");
1810 if (not_yet_string == NULL)
1811 return NULL;
1812 }
1813
1814 self = type->tp_alloc(type, 0);
1815 if (self != NULL) {
1816 /* Always fill in the name and mode, so that nobody else
1817 needs to special-case NULLs there. */
1818 Py_INCREF(not_yet_string);
1819 ((PyFileObject *)self)->f_name = not_yet_string;
1820 Py_INCREF(not_yet_string);
1821 ((PyFileObject *)self)->f_mode = not_yet_string;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001822 Py_INCREF(Py_None);
1823 ((PyFileObject *)self)->f_encoding = Py_None;
Tim Peters44410012001-09-14 03:26:08 +00001824 }
1825 return self;
1826}
1827
1828static int
1829file_init(PyObject *self, PyObject *args, PyObject *kwds)
1830{
1831 PyFileObject *foself = (PyFileObject *)self;
1832 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001833 static char *kwlist[] = {"name", "mode", "buffering", 0};
1834 char *name = NULL;
1835 char *mode = "r";
1836 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001837 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001838
1839 assert(PyFile_Check(self));
1840 if (foself->f_fp != NULL) {
1841 /* Have to close the existing file first. */
1842 PyObject *closeresult = file_close(foself);
1843 if (closeresult == NULL)
1844 return -1;
1845 Py_DECREF(closeresult);
1846 }
Tim Peters59c9a642001-09-13 05:38:56 +00001847
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#ifdef Py_WIN_WIDE_FILENAMES
1849 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1850 PyObject *po;
1851 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1852 kwlist, &po, &mode, &bufsize)) {
1853 wideargument = 1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001854 if (fill_file_fields(foself, NULL, po, mode,
1855 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001856 goto Error;
1857 } else {
1858 /* Drop the argument parsing error as narrow
1859 strings are also valid. */
1860 PyErr_Clear();
1861 }
1862 }
1863#endif
1864
1865 if (!wideargument) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001866 PyObject *o_name;
1867
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001868 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1869 Py_FileSystemDefaultEncoding,
1870 &name,
1871 &mode, &bufsize))
1872 return -1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001873
1874 /* We parse again to get the name as a PyObject */
1875 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", kwlist,
1876 &o_name, &mode, &bufsize))
1877 return -1;
1878
1879 if (fill_file_fields(foself, NULL, o_name, mode,
1880 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001881 goto Error;
1882 }
Tim Peters44410012001-09-14 03:26:08 +00001883 if (open_the_file(foself, name, mode) == NULL)
1884 goto Error;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +00001885 foself->f_setbuf = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001886 PyFile_SetBufSize(self, bufsize);
1887 goto Done;
1888
1889Error:
1890 ret = -1;
1891 /* fall through */
1892Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001893 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001894 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001895}
1896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_VAR(file_doc) =
1898PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001899"file(name[, mode[, buffering]]) -> file object\n"
1900"\n"
1901"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1902"writing or appending. The file will be created if it doesn't exist\n"
1903"when opened for writing or appending; it will be truncated when\n"
1904"opened for writing. Add a 'b' to the mode for binary files.\n"
1905"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1906"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001907"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001910"Add a 'U' to mode to open the file for input with universal newline\n"
1911"support. Any line ending in the input file will be seen as a '\\n'\n"
1912"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1913"the value for this attribute is one of None (no newline read yet),\n"
1914"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1915"\n"
1916"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001919"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920"Note: open() is an alias for file()."
1921);
Tim Peters59c9a642001-09-13 05:38:56 +00001922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001923PyTypeObject PyFile_Type = {
1924 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 0,
1926 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001927 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001928 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001929 (destructor)file_dealloc, /* tp_dealloc */
1930 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931 0, /* tp_getattr */
1932 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001933 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001935 0, /* tp_as_number */
1936 0, /* tp_as_sequence */
1937 0, /* tp_as_mapping */
1938 0, /* tp_hash */
1939 0, /* tp_call */
1940 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001941 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00001942 /* softspace is writable: we must supply tp_setattro */
1943 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001944 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001945 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001946 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001947 0, /* tp_traverse */
1948 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001949 0, /* tp_richcompare */
1950 0, /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001951 (getiterfunc)file_getiter, /* tp_iter */
1952 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953 file_methods, /* tp_methods */
1954 file_memberlist, /* tp_members */
1955 file_getsetlist, /* tp_getset */
1956 0, /* tp_base */
1957 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001958 0, /* tp_descr_get */
1959 0, /* tp_descr_set */
1960 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001961 (initproc)file_init, /* tp_init */
1962 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001963 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001964 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001965};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001966
1967/* Interface for the 'soft space' between print items. */
1968
1969int
Fred Drakefd99de62000-07-09 05:02:18 +00001970PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001971{
1972 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001973 if (f == NULL) {
1974 /* Do nothing */
1975 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001976 else if (PyFile_Check(f)) {
1977 oldflag = ((PyFileObject *)f)->f_softspace;
1978 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001979 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001980 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001981 PyObject *v;
1982 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001983 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001984 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001985 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001986 if (PyInt_Check(v))
1987 oldflag = PyInt_AsLong(v);
1988 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001989 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001990 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001991 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001992 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001993 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001994 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1995 PyErr_Clear();
1996 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001997 }
1998 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00001999 return oldflag;
2000}
Guido van Rossum3165fe61992-09-25 21:59:05 +00002001
2002/* Interfaces to write objects/strings to file-like objects */
2003
2004int
Fred Drakefd99de62000-07-09 05:02:18 +00002005PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002006{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002007 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002008 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002009 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002010 return -1;
2011 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002012 else if (PyFile_Check(f)) {
2013 FILE *fp = PyFile_AsFile(f);
Fred Drake086a0f72004-03-19 15:22:36 +00002014#ifdef Py_USING_UNICODE
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002015 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2016 int result;
Fred Drake086a0f72004-03-19 15:22:36 +00002017#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002018 if (fp == NULL) {
2019 err_closed();
2020 return -1;
2021 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002022#ifdef Py_USING_UNICODE
Tim Petersf1827cf2003-09-07 03:30:18 +00002023 if ((flags & Py_PRINT_RAW) &&
Martin v. Löwis415da6e2003-05-18 12:56:25 +00002024 PyUnicode_Check(v) && enc != Py_None) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002025 char *cenc = PyString_AS_STRING(enc);
2026 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2027 if (value == NULL)
2028 return -1;
2029 } else {
2030 value = v;
2031 Py_INCREF(value);
2032 }
2033 result = PyObject_Print(value, fp, flags);
2034 Py_DECREF(value);
2035 return result;
2036#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002037 return PyObject_Print(v, fp, flags);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002038#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002039 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002040 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002041 if (writer == NULL)
2042 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002043 if (flags & Py_PRINT_RAW) {
2044 if (PyUnicode_Check(v)) {
2045 value = v;
2046 Py_INCREF(value);
2047 } else
2048 value = PyObject_Str(v);
2049 }
2050 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002051 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002052 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002053 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002054 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002055 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002056 args = PyTuple_Pack(1, value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002057 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002058 Py_DECREF(value);
2059 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002060 return -1;
2061 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002062 result = PyEval_CallObject(writer, args);
2063 Py_DECREF(args);
2064 Py_DECREF(value);
2065 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002066 if (result == NULL)
2067 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002068 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002069 return 0;
2070}
2071
Guido van Rossum27a60b11997-05-22 22:25:11 +00002072int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002073PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002074{
2075 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002076 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002077 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002078 PyErr_SetString(PyExc_SystemError,
2079 "null file for PyFile_WriteString");
2080 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002081 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002082 else if (PyFile_Check(f)) {
2083 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002084 if (fp == NULL) {
2085 err_closed();
2086 return -1;
2087 }
2088 fputs(s, fp);
2089 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002090 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002091 else if (!PyErr_Occurred()) {
2092 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002093 int err;
2094 if (v == NULL)
2095 return -1;
2096 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2097 Py_DECREF(v);
2098 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002099 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002100 else
2101 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002102}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002103
2104/* Try to get a file-descriptor from a Python object. If the object
2105 is an integer or long integer, its value is returned. If not, the
2106 object's fileno() method is called if it exists; the method must return
2107 an integer or long integer, which is returned as the file descriptor value.
2108 -1 is returned on failure.
2109*/
2110
2111int PyObject_AsFileDescriptor(PyObject *o)
2112{
2113 int fd;
2114 PyObject *meth;
2115
2116 if (PyInt_Check(o)) {
2117 fd = PyInt_AsLong(o);
2118 }
2119 else if (PyLong_Check(o)) {
2120 fd = PyLong_AsLong(o);
2121 }
2122 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2123 {
2124 PyObject *fno = PyEval_CallObject(meth, NULL);
2125 Py_DECREF(meth);
2126 if (fno == NULL)
2127 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002128
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002129 if (PyInt_Check(fno)) {
2130 fd = PyInt_AsLong(fno);
2131 Py_DECREF(fno);
2132 }
2133 else if (PyLong_Check(fno)) {
2134 fd = PyLong_AsLong(fno);
2135 Py_DECREF(fno);
2136 }
2137 else {
2138 PyErr_SetString(PyExc_TypeError,
2139 "fileno() returned a non-integer");
2140 Py_DECREF(fno);
2141 return -1;
2142 }
2143 }
2144 else {
2145 PyErr_SetString(PyExc_TypeError,
2146 "argument must be an int, or have a fileno() method.");
2147 return -1;
2148 }
2149
2150 if (fd < 0) {
2151 PyErr_Format(PyExc_ValueError,
2152 "file descriptor cannot be a negative integer (%i)",
2153 fd);
2154 return -1;
2155 }
2156 return fd;
2157}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002158
Jack Jansen7b8c7542002-04-14 20:12:41 +00002159/* From here on we need access to the real fgets and fread */
2160#undef fgets
2161#undef fread
2162
2163/*
2164** Py_UniversalNewlineFgets is an fgets variation that understands
2165** all of \r, \n and \r\n conventions.
2166** The stream should be opened in binary mode.
2167** If fobj is NULL the routine always does newline conversion, and
2168** it may peek one char ahead to gobble the second char in \r\n.
2169** If fobj is non-NULL it must be a PyFileObject. In this case there
2170** is no readahead but in stead a flag is used to skip a following
2171** \n on the next read. Also, if the file is open in binary mode
2172** the whole conversion is skipped. Finally, the routine keeps track of
2173** the different types of newlines seen.
2174** Note that we need no error handling: fgets() treats error and eof
2175** identically.
2176*/
2177char *
2178Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2179{
2180 char *p = buf;
2181 int c;
2182 int newlinetypes = 0;
2183 int skipnextlf = 0;
2184 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002185
Jack Jansen7b8c7542002-04-14 20:12:41 +00002186 if (fobj) {
2187 if (!PyFile_Check(fobj)) {
2188 errno = ENXIO; /* What can you do... */
2189 return NULL;
2190 }
2191 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2192 if ( !univ_newline )
2193 return fgets(buf, n, stream);
2194 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2195 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2196 }
2197 FLOCKFILE(stream);
2198 c = 'x'; /* Shut up gcc warning */
2199 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2200 if (skipnextlf ) {
2201 skipnextlf = 0;
2202 if (c == '\n') {
2203 /* Seeing a \n here with skipnextlf true
2204 ** means we saw a \r before.
2205 */
2206 newlinetypes |= NEWLINE_CRLF;
2207 c = GETC(stream);
2208 if (c == EOF) break;
2209 } else {
2210 /*
2211 ** Note that c == EOF also brings us here,
2212 ** so we're okay if the last char in the file
2213 ** is a CR.
2214 */
2215 newlinetypes |= NEWLINE_CR;
2216 }
2217 }
2218 if (c == '\r') {
2219 /* A \r is translated into a \n, and we skip
2220 ** an adjacent \n, if any. We don't set the
2221 ** newlinetypes flag until we've seen the next char.
2222 */
2223 skipnextlf = 1;
2224 c = '\n';
2225 } else if ( c == '\n') {
2226 newlinetypes |= NEWLINE_LF;
2227 }
2228 *p++ = c;
2229 if (c == '\n') break;
2230 }
2231 if ( c == EOF && skipnextlf )
2232 newlinetypes |= NEWLINE_CR;
2233 FUNLOCKFILE(stream);
2234 *p = '\0';
2235 if (fobj) {
2236 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2237 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2238 } else if ( skipnextlf ) {
2239 /* If we have no file object we cannot save the
2240 ** skipnextlf flag. We have to readahead, which
2241 ** will cause a pause if we're reading from an
2242 ** interactive stream, but that is very unlikely
2243 ** unless we're doing something silly like
2244 ** execfile("/dev/tty").
2245 */
2246 c = GETC(stream);
2247 if ( c != '\n' )
2248 ungetc(c, stream);
2249 }
2250 if (p == buf)
2251 return NULL;
2252 return buf;
2253}
2254
2255/*
2256** Py_UniversalNewlineFread is an fread variation that understands
2257** all of \r, \n and \r\n conventions.
2258** The stream should be opened in binary mode.
2259** fobj must be a PyFileObject. In this case there
2260** is no readahead but in stead a flag is used to skip a following
2261** \n on the next read. Also, if the file is open in binary mode
2262** the whole conversion is skipped. Finally, the routine keeps track of
2263** the different types of newlines seen.
2264*/
2265size_t
Tim Peters058b1412002-04-21 07:29:14 +00002266Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002267 FILE *stream, PyObject *fobj)
2268{
Tim Peters058b1412002-04-21 07:29:14 +00002269 char *dst = buf;
2270 PyFileObject *f = (PyFileObject *)fobj;
2271 int newlinetypes, skipnextlf;
2272
2273 assert(buf != NULL);
2274 assert(stream != NULL);
2275
Jack Jansen7b8c7542002-04-14 20:12:41 +00002276 if (!fobj || !PyFile_Check(fobj)) {
2277 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002278 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002279 }
Tim Peters058b1412002-04-21 07:29:14 +00002280 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002281 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002282 newlinetypes = f->f_newlinetypes;
2283 skipnextlf = f->f_skipnextlf;
2284 /* Invariant: n is the number of bytes remaining to be filled
2285 * in the buffer.
2286 */
2287 while (n) {
2288 size_t nread;
2289 int shortread;
2290 char *src = dst;
2291
2292 nread = fread(dst, 1, n, stream);
2293 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002294 if (nread == 0)
2295 break;
2296
Tim Peterse1682a82002-04-21 18:15:20 +00002297 n -= nread; /* assuming 1 byte out for each in; will adjust */
2298 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002299 while (nread--) {
2300 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002301 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002302 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002303 *dst++ = '\n';
2304 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002305 }
2306 else if (skipnextlf && c == '\n') {
2307 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002308 skipnextlf = 0;
2309 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002310 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002311 }
2312 else {
2313 /* Normal char to be stored in buffer. Also
2314 * update the newlinetypes flag if either this
2315 * is an LF or the previous char was a CR.
2316 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002317 if (c == '\n')
2318 newlinetypes |= NEWLINE_LF;
2319 else if (skipnextlf)
2320 newlinetypes |= NEWLINE_CR;
2321 *dst++ = c;
2322 skipnextlf = 0;
2323 }
2324 }
Tim Peters058b1412002-04-21 07:29:14 +00002325 if (shortread) {
2326 /* If this is EOF, update type flags. */
2327 if (skipnextlf && feof(stream))
2328 newlinetypes |= NEWLINE_CR;
2329 break;
2330 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002331 }
Tim Peters058b1412002-04-21 07:29:14 +00002332 f->f_newlinetypes = newlinetypes;
2333 f->f_skipnextlf = skipnextlf;
2334 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002335}