blob: c97336602d1dea1e271adf73d4ae8a7ef3468daf [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 }
Tim Peters44410012001-09-14 03:26:08 +0000315 Py_XDECREF(f->f_name);
316 Py_XDECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000317 Py_XDECREF(f->f_encoding);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000318 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000319 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320}
321
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000322static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000323file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000325 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000326#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000327 PyObject *ret = NULL;
328 PyObject *name;
329 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
330 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
331 f->f_fp == NULL ? "closed" : "open",
332 PyString_AsString(name),
333 PyString_AsString(f->f_mode),
334 f);
335 Py_XDECREF(name);
336 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000337#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000338 } else {
339 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000340 f->f_fp == NULL ? "closed" : "open",
341 PyString_AsString(f->f_name),
342 PyString_AsString(f->f_mode),
343 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000344 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345}
346
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000348file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000350 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000352 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000354 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000355 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000357 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 f->f_fp = NULL;
359 }
Martin v. Löwis7bbcde72003-09-07 20:42:29 +0000360 PyMem_Free(f->f_setbuf);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000361 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000363 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 return PyInt_FromLong((long)sts);
365 Py_INCREF(Py_None);
366 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367}
368
Trent Mickf29f47b2000-08-11 19:02:59 +0000369
Guido van Rossumb8552162001-09-05 14:58:11 +0000370/* Our very own off_t-like type, 64-bit if possible */
371#if !defined(HAVE_LARGEFILE_SUPPORT)
372typedef off_t Py_off_t;
373#elif SIZEOF_OFF_T >= 8
374typedef off_t Py_off_t;
375#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000376typedef fpos_t Py_off_t;
377#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000378#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000379#endif
380
381
Trent Mickf29f47b2000-08-11 19:02:59 +0000382/* a portable fseek() function
383 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000384static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000385_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000386{
Guido van Rossumb8552162001-09-05 14:58:11 +0000387#if !defined(HAVE_LARGEFILE_SUPPORT)
388 return fseek(fp, offset, whence);
389#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000390 return fseeko(fp, offset, whence);
391#elif defined(HAVE_FSEEK64)
392 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000393#elif defined(__BEOS__)
394 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000395#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000396 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
397 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000398 fpos_t pos;
399 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000400 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000401#ifdef MS_WINDOWS
402 fflush(fp);
403 if (_lseeki64(fileno(fp), 0, 2) == -1)
404 return -1;
405#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000406 if (fseek(fp, 0, SEEK_END) != 0)
407 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000408#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000409 /* fall through */
410 case SEEK_CUR:
411 if (fgetpos(fp, &pos) != 0)
412 return -1;
413 offset += pos;
414 break;
415 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000416 }
417 return fsetpos(fp, &offset);
418#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000419#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000420#endif
421}
422
423
424/* a portable ftell() function
425 Return -1 on failure with errno set appropriately, current file
426 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000427static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000428_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000429{
Guido van Rossumb8552162001-09-05 14:58:11 +0000430#if !defined(HAVE_LARGEFILE_SUPPORT)
431 return ftell(fp);
432#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
433 return ftello(fp);
434#elif defined(HAVE_FTELL64)
435 return ftell64(fp);
436#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000437 fpos_t pos;
438 if (fgetpos(fp, &pos) != 0)
439 return -1;
440 return pos;
441#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000442#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000443#endif
444}
445
446
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000448file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000450 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000451 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000452 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000453 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000454
Guido van Rossumd7297e61992-07-06 14:19:26 +0000455 if (f->f_fp == NULL)
456 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000457 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000458 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000459 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000460 return NULL;
461#if !defined(HAVE_LARGEFILE_SUPPORT)
462 offset = PyInt_AsLong(offobj);
463#else
464 offset = PyLong_Check(offobj) ?
465 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
466#endif
467 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000468 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000469
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000471 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000472 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000474
Guido van Rossumff4949e1992-08-05 19:58:53 +0000475 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000477 clearerr(f->f_fp);
478 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000479 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000480 f->f_skipnextlf = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 Py_INCREF(Py_None);
482 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000483}
484
Trent Mickf29f47b2000-08-11 19:02:59 +0000485
Guido van Rossumd7047b31995-01-02 19:07:15 +0000486#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000488file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000489{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000490 Py_off_t newsize;
Tim Petersf1827cf2003-09-07 03:30:18 +0000491 PyObject *newsizeobj = NULL;
492 Py_off_t initialpos;
493 int ret;
Tim Peters86821b22001-01-07 21:19:34 +0000494
Guido van Rossumd7047b31995-01-02 19:07:15 +0000495 if (f->f_fp == NULL)
496 return err_closed();
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000497 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000498 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000499
Tim Petersf1827cf2003-09-07 03:30:18 +0000500 /* Get current file position. If the file happens to be open for
501 * update and the last operation was an input operation, C doesn't
502 * define what the later fflush() will do, but we promise truncate()
503 * won't change the current position (and fflush() *does* change it
504 * then at least on Windows). The easiest thing is to capture
505 * current pos now and seek back to it at the end.
506 */
507 Py_BEGIN_ALLOW_THREADS
508 errno = 0;
509 initialpos = _portable_ftell(f->f_fp);
510 Py_END_ALLOW_THREADS
511 if (initialpos == -1)
512 goto onioerror;
513
Tim Petersfb05db22002-03-11 00:24:00 +0000514 /* Set newsize to current postion if newsizeobj NULL, else to the
Tim Petersf1827cf2003-09-07 03:30:18 +0000515 * specified value.
516 */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000517 if (newsizeobj != NULL) {
518#if !defined(HAVE_LARGEFILE_SUPPORT)
519 newsize = PyInt_AsLong(newsizeobj);
520#else
521 newsize = PyLong_Check(newsizeobj) ?
522 PyLong_AsLongLong(newsizeobj) :
523 PyInt_AsLong(newsizeobj);
524#endif
525 if (PyErr_Occurred())
526 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000527 }
Tim Petersf1827cf2003-09-07 03:30:18 +0000528 else /* default to current position */
529 newsize = initialpos;
Tim Petersfb05db22002-03-11 00:24:00 +0000530
Tim Petersf1827cf2003-09-07 03:30:18 +0000531 /* Flush the stream. We're mixing stream-level I/O with lower-level
532 * I/O, and a flush may be necessary to synch both platform views
533 * of the current file state.
534 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000536 errno = 0;
537 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000539 if (ret != 0)
540 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000541
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000542#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000543 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000544 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000545 {
Tim Petersfb05db22002-03-11 00:24:00 +0000546 HANDLE hFile;
Tim Petersfb05db22002-03-11 00:24:00 +0000547
Tim Petersf1827cf2003-09-07 03:30:18 +0000548 /* Have to move current pos to desired endpoint on Windows. */
549 Py_BEGIN_ALLOW_THREADS
550 errno = 0;
551 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
552 Py_END_ALLOW_THREADS
553 if (ret)
554 goto onioerror;
Tim Petersfb05db22002-03-11 00:24:00 +0000555
Tim Peters8f01b682002-03-12 03:04:44 +0000556 /* Truncate. Note that this may grow the file! */
557 Py_BEGIN_ALLOW_THREADS
558 errno = 0;
559 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
Tim Petersf1827cf2003-09-07 03:30:18 +0000560 ret = hFile == (HANDLE)-1;
561 if (ret == 0) {
562 ret = SetEndOfFile(hFile) == 0;
563 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000564 errno = EACCES;
565 }
566 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000567 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000568 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000569 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000570#else
571 Py_BEGIN_ALLOW_THREADS
572 errno = 0;
573 ret = ftruncate(fileno(f->f_fp), newsize);
574 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000575 if (ret != 0)
576 goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000577#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000578
Tim Petersf1827cf2003-09-07 03:30:18 +0000579 /* Restore original file position. */
580 Py_BEGIN_ALLOW_THREADS
581 errno = 0;
582 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
583 Py_END_ALLOW_THREADS
584 if (ret)
585 goto onioerror;
586
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 Py_INCREF(Py_None);
588 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000589
590onioerror:
591 PyErr_SetFromErrno(PyExc_IOError);
592 clearerr(f->f_fp);
593 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000594}
595#endif /* HAVE_FTRUNCATE */
596
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000598file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000599{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000600 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000601
Guido van Rossumd7297e61992-07-06 14:19:26 +0000602 if (f->f_fp == NULL)
603 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000605 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000606 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000608 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000610 clearerr(f->f_fp);
611 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000612 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000613 if (f->f_skipnextlf) {
614 int c;
615 c = GETC(f->f_fp);
616 if (c == '\n') {
617 pos++;
618 f->f_skipnextlf = 0;
619 } else if (c != EOF) ungetc(c, f->f_fp);
620 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000621#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000622 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000623#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000624 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000625#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000626}
627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000629file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000630{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000631 if (f->f_fp == NULL)
632 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000634}
635
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000637file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000638{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000639 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000640
Guido van Rossumd7297e61992-07-06 14:19:26 +0000641 if (f->f_fp == NULL)
642 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000644 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000645 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000647 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000649 clearerr(f->f_fp);
650 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000651 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 Py_INCREF(Py_None);
653 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000654}
655
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000657file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000658{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000659 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000660 if (f->f_fp == NULL)
661 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000663 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000665 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000666}
667
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000668
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000669#if BUFSIZ < 8192
670#define SMALLCHUNK 8192
671#else
672#define SMALLCHUNK BUFSIZ
673#endif
674
Guido van Rossum3c259041999-01-14 19:00:14 +0000675#if SIZEOF_INT < 4
676#define BIGCHUNK (512 * 32)
677#else
678#define BIGCHUNK (512 * 1024)
679#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000680
681static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000682new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000683{
684#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000685 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000686 struct stat st;
687 if (fstat(fileno(f->f_fp), &st) == 0) {
688 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000689 /* The following is not a bug: we really need to call lseek()
690 *and* ftell(). The reason is that some stdio libraries
691 mistakenly flush their buffer when ftell() is called and
692 the lseek() call it makes fails, thereby throwing away
693 data that cannot be recovered in any way. To avoid this,
694 we first test lseek(), and only call ftell() if lseek()
695 works. We can't use the lseek() value either, because we
696 need to take the amount of buffered data into account.
697 (Yet another reason why stdio stinks. :-) */
Guido van Rossum91aaa921998-05-05 22:21:35 +0000698 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000699 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000700 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000701 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000702 if (pos < 0)
703 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000704 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000705 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000706 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000707 }
708#endif
709 if (currentsize > SMALLCHUNK) {
710 /* Keep doubling until we reach BIGCHUNK;
711 then keep adding BIGCHUNK. */
712 if (currentsize <= BIGCHUNK)
713 return currentsize + currentsize;
714 else
715 return currentsize + BIGCHUNK;
716 }
717 return currentsize + SMALLCHUNK;
718}
719
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000720#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
721#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
722#else
723#ifdef EWOULDBLOCK
724#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
725#else
726#ifdef EAGAIN
727#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
728#else
729#define BLOCKED_ERRNO(x) 0
730#endif
731#endif
732#endif
733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000735file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000736{
Guido van Rossum789a1611997-05-10 22:33:55 +0000737 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000738 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000740
Guido van Rossumd7297e61992-07-06 14:19:26 +0000741 if (f->f_fp == NULL)
742 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000743 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000744 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000745 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000746 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000747 else
748 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000749 if (buffersize > INT_MAX) {
750 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000751 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000752 return NULL;
753 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000754 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000755 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000757 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000758 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000759 Py_BEGIN_ALLOW_THREADS
760 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000761 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000762 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000763 Py_END_ALLOW_THREADS
764 if (chunksize == 0) {
765 if (!ferror(f->f_fp))
766 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000767 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000768 /* When in non-blocking mode, data shouldn't
769 * be discarded if a blocking signal was
770 * received. That will also happen if
771 * chunksize != 0, but bytesread < buffersize. */
772 if (bytesread > 0 && BLOCKED_ERRNO(errno))
773 break;
774 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000775 Py_DECREF(v);
776 return NULL;
777 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000778 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000779 if (bytesread < buffersize) {
780 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000781 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000782 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000783 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000784 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000785 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000786 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000787 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000788 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000789 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000790 }
791 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000792 if (bytesread != buffersize)
793 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794 return v;
795}
796
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000797static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000798file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000799{
800 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000801 int ntodo;
802 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000803
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000804 if (f->f_fp == NULL)
805 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000806 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000807 return NULL;
808 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000809 while (ntodo > 0) {
810 Py_BEGIN_ALLOW_THREADS
811 errno = 0;
Tim Petersf1827cf2003-09-07 03:30:18 +0000812 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000813 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000814 Py_END_ALLOW_THREADS
815 if (nnow == 0) {
816 if (!ferror(f->f_fp))
817 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000818 PyErr_SetFromErrno(PyExc_IOError);
819 clearerr(f->f_fp);
820 return NULL;
821 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000822 ndone += nnow;
823 ntodo -= nnow;
824 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000825 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000826}
827
Tim Peters86821b22001-01-07 21:19:34 +0000828/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000829Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000830
831Under MSVC 6:
832
Tim Peters1c733232001-01-08 04:02:07 +0000833+ MS threadsafe getc is very slow (multiple layers of function calls before+
834 after each character, to lock+unlock the stream).
835+ The stream-locking functions are MS-internal -- can't access them from user
836 code.
837+ There's nothing Tim could find in the MS C or platform SDK libraries that
838 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000839+ MS fgets locks/unlocks only once per line; it's the only hook we have.
840
841So we use fgets for speed(!), despite that it's painful.
842
843MS realloc is also slow.
844
Tim Petersf29b64d2001-01-15 06:33:19 +0000845Reports from other platforms on this method vs getc_unlocked (which MS doesn't
846have):
847 Linux a wash
848 Solaris a wash
849 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000850
Tim Petersf29b64d2001-01-15 06:33:19 +0000851CAUTION: The C std isn't clear about this: in those cases where fgets
852writes something into the buffer, can it write into any position beyond the
853required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
854known on which it does; and it would be a strange way to code fgets. Still,
855getline_via_fgets may not work correctly if it does. The std test
856test_bufio.py should fail if platform fgets() routinely writes beyond the
857trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000858**************************************************************************/
859
Tim Petersf29b64d2001-01-15 06:33:19 +0000860/* Use this routine if told to, or by default on non-get_unlocked()
861 * platforms unless told not to. Yikes! Let's spell that out:
862 * On a platform with getc_unlocked():
863 * By default, use getc_unlocked().
864 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
865 * On a platform without getc_unlocked():
866 * By default, use fgets().
867 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
868 */
869#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
870#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000871#endif
872
Tim Petersf29b64d2001-01-15 06:33:19 +0000873#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
874#undef USE_FGETS_IN_GETLINE
875#endif
876
877#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000878static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000879getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000880{
Tim Peters15b83852001-01-08 00:53:12 +0000881/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000882 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
883 * to fill this much of the buffer with a known value in order to figure out
884 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
885 * than "most" lines, we waste time filling unused buffer slots. 100 is
886 * surely adequate for most peoples' email archives, chewing over source code,
887 * etc -- "regular old text files".
888 * MAXBUFSIZE is the maximum line length that lets us get away with the less
889 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
890 * cautions about boosting that. 300 was chosen because the worst real-life
891 * text-crunching job reported on Python-Dev was a mail-log crawler where over
892 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000893 */
Tim Peters142297a2001-01-15 10:36:56 +0000894#define INITBUFSIZE 100
895#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000896 char* p; /* temp */
897 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000898 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000899 char* pvfree; /* address of next free slot */
900 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000901 size_t nfree; /* # of free buffer slots; pvend-pvfree */
902 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000903 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000904
Tim Peters15b83852001-01-08 00:53:12 +0000905 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000906 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000907 */
Tim Peters142297a2001-01-15 10:36:56 +0000908 total_v_size = INITBUFSIZE; /* start small and pray */
909 pvfree = buf;
910 for (;;) {
911 Py_BEGIN_ALLOW_THREADS
912 pvend = buf + total_v_size;
913 nfree = pvend - pvfree;
914 memset(pvfree, '\n', nfree);
915 p = fgets(pvfree, nfree, fp);
916 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000917
Tim Peters142297a2001-01-15 10:36:56 +0000918 if (p == NULL) {
919 clearerr(fp);
920 if (PyErr_CheckSignals())
921 return NULL;
922 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000923 return v;
924 }
Tim Peters142297a2001-01-15 10:36:56 +0000925 /* fgets read *something* */
926 p = memchr(pvfree, '\n', nfree);
927 if (p != NULL) {
928 /* Did the \n come from fgets or from us?
929 * Since fgets stops at the first \n, and then writes
930 * \0, if it's from fgets a \0 must be next. But if
931 * that's so, it could not have come from us, since
932 * the \n's we filled the buffer with have only more
933 * \n's to the right.
934 */
935 if (p+1 < pvend && *(p+1) == '\0') {
936 /* It's from fgets: we win! In particular,
937 * we haven't done any mallocs yet, and can
938 * build the final result on the first try.
939 */
940 ++p; /* include \n from fgets */
941 }
942 else {
943 /* Must be from us: fgets didn't fill the
944 * buffer and didn't find a newline, so it
945 * must be the last and newline-free line of
946 * the file.
947 */
948 assert(p > pvfree && *(p-1) == '\0');
949 --p; /* don't include \0 from fgets */
950 }
951 v = PyString_FromStringAndSize(buf, p - buf);
952 return v;
953 }
954 /* yuck: fgets overwrote all the newlines, i.e. the entire
955 * buffer. So this line isn't over yet, or maybe it is but
956 * we're exactly at EOF. If we haven't already, try using the
957 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000958 */
Tim Peters142297a2001-01-15 10:36:56 +0000959 assert(*(pvend-1) == '\0');
960 if (pvfree == buf) {
961 pvfree = pvend - 1; /* overwrite trailing null */
962 total_v_size = MAXBUFSIZE;
963 }
964 else
965 break;
Tim Peters86821b22001-01-07 21:19:34 +0000966 }
Tim Peters142297a2001-01-15 10:36:56 +0000967
968 /* The stack buffer isn't big enough; malloc a string object and read
969 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000970 */
Tim Petersddea2082002-03-23 10:03:50 +0000971 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +0000972 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000973 if (v == NULL)
974 return v;
975 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +0000976 memcpy(BUF(v), buf, MAXBUFSIZE-1);
977 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +0000978
979 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +0000980 * after setting p one beyond the end of the line. The code here is
981 * very much like the code above, except reads into v's buffer; see
982 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +0000983 */
984 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +0000985 Py_BEGIN_ALLOW_THREADS
986 pvend = BUF(v) + total_v_size;
987 nfree = pvend - pvfree;
988 memset(pvfree, '\n', nfree);
989 p = fgets(pvfree, nfree, fp);
990 Py_END_ALLOW_THREADS
991
992 if (p == NULL) {
993 clearerr(fp);
994 if (PyErr_CheckSignals()) {
995 Py_DECREF(v);
996 return NULL;
997 }
998 p = pvfree;
999 break;
1000 }
Tim Peters86821b22001-01-07 21:19:34 +00001001 p = memchr(pvfree, '\n', nfree);
1002 if (p != NULL) {
1003 if (p+1 < pvend && *(p+1) == '\0') {
1004 /* \n came from fgets */
1005 ++p;
1006 break;
1007 }
1008 /* \n came from us; last line of file, no newline */
1009 assert(p > pvfree && *(p-1) == '\0');
1010 --p;
1011 break;
1012 }
1013 /* expand buffer and try again */
1014 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001015 increment = total_v_size >> 2; /* mild exponential growth */
1016 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001017 if (total_v_size > INT_MAX) {
1018 PyErr_SetString(PyExc_OverflowError,
1019 "line is longer than a Python string can hold");
1020 Py_DECREF(v);
1021 return NULL;
1022 }
1023 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1024 return NULL;
1025 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001026 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001027 }
1028 if (BUF(v) + total_v_size != p)
1029 _PyString_Resize(&v, p - BUF(v));
1030 return v;
1031#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001032#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001033}
Tim Petersf29b64d2001-01-15 06:33:19 +00001034#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001035
Guido van Rossum0bd24411991-04-04 15:21:57 +00001036/* Internal routine to get a line.
1037 Size argument interpretation:
1038 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001039 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001040*/
1041
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001042static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001043get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001045 FILE *fp = f->f_fp;
1046 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001047 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001048 size_t total_v_size; /* total # of slots in buffer */
1049 size_t used_v_size; /* # used slots in buffer */
1050 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001052 int newlinetypes = f->f_newlinetypes;
1053 int skipnextlf = f->f_skipnextlf;
1054 int univ_newline = f->f_univ_newline;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001055
Jack Jansen7b8c7542002-04-14 20:12:41 +00001056#if defined(USE_FGETS_IN_GETLINE)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001057 if (n <= 0 && !univ_newline )
Tim Petersf29b64d2001-01-15 06:33:19 +00001058 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001059#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001060 total_v_size = n > 0 ? n : 100;
1061 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001062 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001063 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001064 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001065 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001066
Guido van Rossumce5ba841991-03-06 13:06:18 +00001067 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001068 Py_BEGIN_ALLOW_THREADS
1069 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001070 if (univ_newline) {
1071 c = 'x'; /* Shut up gcc warning */
1072 while ( buf != end && (c = GETC(fp)) != EOF ) {
1073 if (skipnextlf ) {
1074 skipnextlf = 0;
1075 if (c == '\n') {
Tim Petersf1827cf2003-09-07 03:30:18 +00001076 /* Seeing a \n here with
1077 * skipnextlf true means we
Jeremy Hylton8b735422002-08-14 21:01:41 +00001078 * saw a \r before.
1079 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001080 newlinetypes |= NEWLINE_CRLF;
1081 c = GETC(fp);
1082 if (c == EOF) break;
1083 } else {
1084 newlinetypes |= NEWLINE_CR;
1085 }
1086 }
1087 if (c == '\r') {
1088 skipnextlf = 1;
1089 c = '\n';
1090 } else if ( c == '\n')
1091 newlinetypes |= NEWLINE_LF;
1092 *buf++ = c;
1093 if (c == '\n') break;
1094 }
1095 if ( c == EOF && skipnextlf )
1096 newlinetypes |= NEWLINE_CR;
1097 } else /* If not universal newlines use the normal loop */
Guido van Rossum1187aa42001-01-05 14:43:05 +00001098 while ((c = GETC(fp)) != EOF &&
1099 (*buf++ = c) != '\n' &&
1100 buf != end)
1101 ;
1102 FUNLOCKFILE(fp);
1103 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001104 f->f_newlinetypes = newlinetypes;
1105 f->f_skipnextlf = skipnextlf;
Guido van Rossum1187aa42001-01-05 14:43:05 +00001106 if (c == '\n')
1107 break;
1108 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001109 if (ferror(fp)) {
1110 PyErr_SetFromErrno(PyExc_IOError);
1111 clearerr(fp);
1112 Py_DECREF(v);
1113 return NULL;
1114 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001115 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001118 return NULL;
1119 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001120 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001121 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001122 /* Must be because buf == end */
1123 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001124 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001125 used_v_size = total_v_size;
1126 increment = total_v_size >> 2; /* mild exponential growth */
1127 total_v_size += increment;
1128 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001129 PyErr_SetString(PyExc_OverflowError,
1130 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001131 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001132 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001133 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001134 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001135 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001136 buf = BUF(v) + used_v_size;
1137 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001139
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001140 used_v_size = buf - BUF(v);
1141 if (used_v_size != total_v_size)
1142 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001143 return v;
1144}
1145
Guido van Rossum0bd24411991-04-04 15:21:57 +00001146/* External C interface */
1147
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001148PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001149PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001150{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001151 PyObject *result;
1152
Guido van Rossum3165fe61992-09-25 21:59:05 +00001153 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001155 return NULL;
1156 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001157
1158 if (PyFile_Check(f)) {
1159 if (((PyFileObject*)f)->f_fp == NULL)
1160 return err_closed();
1161 result = get_line((PyFileObject *)f, n);
1162 }
1163 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 PyObject *reader;
1165 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001168 if (reader == NULL)
1169 return NULL;
1170 if (n <= 0)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001171 args = PyTuple_New(0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001172 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001173 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001174 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001176 return NULL;
1177 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001178 result = PyEval_CallObject(reader, args);
1179 Py_DECREF(reader);
1180 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001181 if (result != NULL && !PyString_Check(result) &&
1182 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001183 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001184 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001186 "object.readline() returned non-string");
1187 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001188 }
1189
1190 if (n < 0 && result != NULL && PyString_Check(result)) {
1191 char *s = PyString_AS_STRING(result);
1192 int len = PyString_GET_SIZE(result);
1193 if (len == 0) {
1194 Py_DECREF(result);
1195 result = NULL;
1196 PyErr_SetString(PyExc_EOFError,
1197 "EOF when reading a line");
1198 }
1199 else if (s[len-1] == '\n') {
1200 if (result->ob_refcnt == 1)
1201 _PyString_Resize(&result, len-1);
1202 else {
1203 PyObject *v;
1204 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001205 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001206 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001207 }
1208 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001209 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001210#ifdef Py_USING_UNICODE
1211 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1212 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1213 int len = PyUnicode_GET_SIZE(result);
1214 if (len == 0) {
1215 Py_DECREF(result);
1216 result = NULL;
1217 PyErr_SetString(PyExc_EOFError,
1218 "EOF when reading a line");
1219 }
1220 else if (s[len-1] == '\n') {
1221 if (result->ob_refcnt == 1)
1222 PyUnicode_Resize(&result, len-1);
1223 else {
1224 PyObject *v;
1225 v = PyUnicode_FromUnicode(s, len-1);
1226 Py_DECREF(result);
1227 result = v;
1228 }
1229 }
1230 }
1231#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001232 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001233}
1234
1235/* Python method */
1236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001238file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001239{
Guido van Rossum789a1611997-05-10 22:33:55 +00001240 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001241
Guido van Rossumd7297e61992-07-06 14:19:26 +00001242 if (f->f_fp == NULL)
1243 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001244 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001245 return NULL;
1246 if (n == 0)
1247 return PyString_FromString("");
1248 if (n < 0)
1249 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001250 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001251}
1252
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001253static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001254file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001255{
Guido van Rossum789a1611997-05-10 22:33:55 +00001256 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001257 PyObject *list;
1258 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001259 char small_buffer[SMALLCHUNK];
1260 char *buffer = small_buffer;
1261 size_t buffersize = SMALLCHUNK;
1262 PyObject *big_buffer = NULL;
1263 size_t nfilled = 0;
1264 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001265 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001266 char *p, *q, *end;
1267 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001268 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001269
Guido van Rossumd7297e61992-07-06 14:19:26 +00001270 if (f->f_fp == NULL)
1271 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001272 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001273 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001275 return NULL;
1276 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001277 if (shortread)
1278 nread = 0;
1279 else {
1280 Py_BEGIN_ALLOW_THREADS
1281 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001282 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001283 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001284 Py_END_ALLOW_THREADS
1285 shortread = (nread < buffersize-nfilled);
1286 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001287 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001288 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001289 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001290 break;
1291 PyErr_SetFromErrno(PyExc_IOError);
1292 clearerr(f->f_fp);
1293 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001295 list = NULL;
1296 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001297 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001298 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001299 p = memchr(buffer+nfilled, '\n', nread);
1300 if (p == NULL) {
1301 /* Need a larger buffer to fit this line */
1302 nfilled += nread;
1303 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001304 if (buffersize > INT_MAX) {
1305 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001306 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001307 goto error;
1308 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001309 if (big_buffer == NULL) {
1310 /* Create the big buffer */
1311 big_buffer = PyString_FromStringAndSize(
1312 NULL, buffersize);
1313 if (big_buffer == NULL)
1314 goto error;
1315 buffer = PyString_AS_STRING(big_buffer);
1316 memcpy(buffer, small_buffer, nfilled);
1317 }
1318 else {
1319 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001320 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1321 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001322 buffer = PyString_AS_STRING(big_buffer);
1323 }
1324 continue;
1325 }
1326 end = buffer+nfilled+nread;
1327 q = buffer;
1328 do {
1329 /* Process complete lines */
1330 p++;
1331 line = PyString_FromStringAndSize(q, p-q);
1332 if (line == NULL)
1333 goto error;
1334 err = PyList_Append(list, line);
1335 Py_DECREF(line);
1336 if (err != 0)
1337 goto error;
1338 q = p;
1339 p = memchr(q, '\n', end-q);
1340 } while (p != NULL);
1341 /* Move the remaining incomplete line to the start */
1342 nfilled = end-q;
1343 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001344 if (sizehint > 0)
1345 if (totalread >= (size_t)sizehint)
1346 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001347 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001348 if (nfilled != 0) {
1349 /* Partial last line */
1350 line = PyString_FromStringAndSize(buffer, nfilled);
1351 if (line == NULL)
1352 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001353 if (sizehint > 0) {
1354 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001355 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001356 if (rest == NULL) {
1357 Py_DECREF(line);
1358 goto error;
1359 }
1360 PyString_Concat(&line, rest);
1361 Py_DECREF(rest);
1362 if (line == NULL)
1363 goto error;
1364 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001365 err = PyList_Append(list, line);
1366 Py_DECREF(line);
1367 if (err != 0)
1368 goto error;
1369 }
1370 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001371 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001372 return list;
1373}
1374
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001376file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001378 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001380 if (f->f_fp == NULL)
1381 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001382 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001384 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001386 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001387 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001389 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001391 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392 return NULL;
1393 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394 Py_INCREF(Py_None);
1395 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001396}
1397
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001399file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001400{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001401#define CHUNKSIZE 1000
1402 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001403 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001404 PyObject *result;
1405 int i, j, index, len, nwritten, islist;
1406
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001407 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001408 if (f->f_fp == NULL)
1409 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001410
1411 result = NULL;
1412 list = NULL;
1413 islist = PyList_Check(seq);
1414 if (islist)
1415 it = NULL;
1416 else {
1417 it = PyObject_GetIter(seq);
1418 if (it == NULL) {
1419 PyErr_SetString(PyExc_TypeError,
1420 "writelines() requires an iterable argument");
1421 return NULL;
1422 }
1423 /* From here on, fail by going to error, to reclaim "it". */
1424 list = PyList_New(CHUNKSIZE);
1425 if (list == NULL)
1426 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001427 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001428
1429 /* Strategy: slurp CHUNKSIZE lines into a private list,
1430 checking that they are all strings, then write that list
1431 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001432 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001433 if (islist) {
1434 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001435 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001436 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001437 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001438 j = PyList_GET_SIZE(list);
1439 }
1440 else {
1441 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001442 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001443 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001444 if (PyErr_Occurred())
1445 goto error;
1446 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001447 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001448 PyList_SetItem(list, j, line);
1449 }
1450 }
1451 if (j == 0)
1452 break;
1453
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001454 /* Check that all entries are indeed strings. If not,
1455 apply the same rules as for file.write() and
1456 convert the results to strings. This is slow, but
1457 seems to be the only way since all conversion APIs
1458 could potentially execute Python code. */
1459 for (i = 0; i < j; i++) {
1460 PyObject *v = PyList_GET_ITEM(list, i);
1461 if (!PyString_Check(v)) {
1462 const char *buffer;
1463 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001464 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001465 PyObject_AsReadBuffer(v,
1466 (const void**)&buffer,
1467 &len)) ||
1468 PyObject_AsCharBuffer(v,
1469 &buffer,
1470 &len))) {
1471 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001472 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001473 goto error;
1474 }
1475 line = PyString_FromStringAndSize(buffer,
1476 len);
1477 if (line == NULL)
1478 goto error;
1479 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001480 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001481 }
1482 }
1483
1484 /* Since we are releasing the global lock, the
1485 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001486 Py_BEGIN_ALLOW_THREADS
1487 f->f_softspace = 0;
1488 errno = 0;
1489 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001490 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001491 len = PyString_GET_SIZE(line);
1492 nwritten = fwrite(PyString_AS_STRING(line),
1493 1, len, f->f_fp);
1494 if (nwritten != len) {
1495 Py_BLOCK_THREADS
1496 PyErr_SetFromErrno(PyExc_IOError);
1497 clearerr(f->f_fp);
1498 goto error;
1499 }
1500 }
1501 Py_END_ALLOW_THREADS
1502
1503 if (j < CHUNKSIZE)
1504 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001505 }
1506
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001507 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001508 result = Py_None;
1509 error:
1510 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001511 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001512 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001513#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001514}
1515
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001516static PyObject *
1517file_getiter(PyFileObject *f)
1518{
1519 if (f->f_fp == NULL)
1520 return err_closed();
1521 Py_INCREF(f);
1522 return (PyObject *)f;
1523}
1524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001526"readline([size]) -> next line from the file, as a string.\n"
1527"\n"
1528"Retain newline. A non-negative size argument limits the maximum\n"
1529"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001530"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001533"read([size]) -> read at most size bytes, returned as a string.\n"
1534"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001535"If the size argument is negative or omitted, read until EOF is reached.\n"
1536"Notice that when in non-blocking mode, less data than what was requested\n"
1537"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001539PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001540"write(str) -> None. Write string str to file.\n"
1541"\n"
1542"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001543"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001546"fileno() -> integer \"file descriptor\".\n"
1547"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001548"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001551"seek(offset[, whence]) -> None. Move to new file position.\n"
1552"\n"
1553"Argument offset is a byte count. Optional argument whence defaults to\n"
1554"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1555"(move relative to current position, positive or negative), and 2 (move\n"
1556"relative to end of file, usually negative, although many platforms allow\n"
Martin v. Löwis849a9722003-10-18 09:38:01 +00001557"seeking beyond the end of a file). If the file is opened in text mode,\n"
1558"only offsets returned by tell() are legal. Use of other offsets causes\n"
1559"undefined behavior."
Tim Petersefc3a3a2001-09-20 07:55:22 +00001560"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001562
Guido van Rossumd7047b31995-01-02 19:07:15 +00001563#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001565"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1566"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001568#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(tell_doc,
1571"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001573PyDoc_STRVAR(readinto_doc,
1574"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001576PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001577"readlines([size]) -> list of strings, each a line from the file.\n"
1578"\n"
1579"Call readline() repeatedly and return a list of the lines so read.\n"
1580"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001583PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001584"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001585"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001586"For backward compatibility. File objects now include the performance\n"
1587"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001590"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001591"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001592"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(flush_doc,
1596"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001599"close() -> None or (perhaps) an integer. Close the file.\n"
1600"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001601"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001602"further I/O operations. close() may be called more than once without\n"
1603"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(isatty_doc,
1607"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001608
1609static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001610 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1611 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1612 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1613 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1614 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001615#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001616 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001617#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001618 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1619 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1620 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1621 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1622 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1623 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1624 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1625 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1626 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627};
1628
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001630
Guido van Rossum6f799372001-09-20 20:46:19 +00001631static PyMemberDef file_memberlist[] = {
1632 {"softspace", T_INT, OFF(f_softspace), 0,
1633 "flag indicating that a space needs to be printed; used by print"},
1634 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001635 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001636 {"name", T_OBJECT, OFF(f_name), RO,
1637 "file name"},
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001638 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1639 "file encoding"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001640 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001641 {NULL} /* Sentinel */
1642};
1643
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001644static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001645get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001646{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001647 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001648}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001649static PyObject *
1650get_newlines(PyFileObject *f, void *closure)
1651{
1652 switch (f->f_newlinetypes) {
1653 case NEWLINE_UNKNOWN:
1654 Py_INCREF(Py_None);
1655 return Py_None;
1656 case NEWLINE_CR:
1657 return PyString_FromString("\r");
1658 case NEWLINE_LF:
1659 return PyString_FromString("\n");
1660 case NEWLINE_CR|NEWLINE_LF:
1661 return Py_BuildValue("(ss)", "\r", "\n");
1662 case NEWLINE_CRLF:
1663 return PyString_FromString("\r\n");
1664 case NEWLINE_CR|NEWLINE_CRLF:
1665 return Py_BuildValue("(ss)", "\r", "\r\n");
1666 case NEWLINE_LF|NEWLINE_CRLF:
1667 return Py_BuildValue("(ss)", "\n", "\r\n");
1668 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1669 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1670 default:
Tim Petersf1827cf2003-09-07 03:30:18 +00001671 PyErr_Format(PyExc_SystemError,
1672 "Unknown newlines value 0x%x\n",
Jeremy Hylton8b735422002-08-14 21:01:41 +00001673 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001674 return NULL;
1675 }
1676}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001677
Guido van Rossum32d34c82001-09-20 21:45:26 +00001678static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001679 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Tim Petersf1827cf2003-09-07 03:30:18 +00001680 {"newlines", (getter)get_newlines, NULL,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001681 "end-of-line convention used in this file"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001682 {0},
1683};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001684
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001685static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001686drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001687{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001688 if (f->f_buf != NULL) {
1689 PyMem_Free(f->f_buf);
1690 f->f_buf = NULL;
1691 }
Guido van Rossum65967252001-04-21 13:20:18 +00001692}
1693
Tim Petersf1827cf2003-09-07 03:30:18 +00001694/* Make sure that file has a readahead buffer with at least one byte
1695 (unless at EOF) and no more than bufsize. Returns negative value on
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001696 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001697static int
1698readahead(PyFileObject *f, int bufsize)
1699{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001700 int chunksize;
1701
1702 if (f->f_buf != NULL) {
Tim Petersf1827cf2003-09-07 03:30:18 +00001703 if( (f->f_bufend - f->f_bufptr) >= 1)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001704 return 0;
1705 else
1706 drop_readahead(f);
1707 }
1708 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1709 return -1;
1710 }
1711 Py_BEGIN_ALLOW_THREADS
1712 errno = 0;
1713 chunksize = Py_UniversalNewlineFread(
1714 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1715 Py_END_ALLOW_THREADS
1716 if (chunksize == 0) {
1717 if (ferror(f->f_fp)) {
1718 PyErr_SetFromErrno(PyExc_IOError);
1719 clearerr(f->f_fp);
1720 drop_readahead(f);
1721 return -1;
1722 }
1723 }
1724 f->f_bufptr = f->f_buf;
1725 f->f_bufend = f->f_buf + chunksize;
1726 return 0;
1727}
1728
1729/* Used by file_iternext. The returned string will start with 'skip'
Tim Petersf1827cf2003-09-07 03:30:18 +00001730 uninitialized bytes followed by the remainder of the line. Don't be
1731 horrified by the recursive call: maximum recursion depth is limited by
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001732 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1733
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001734static PyStringObject *
1735readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1736{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001737 PyStringObject* s;
1738 char *bufptr;
1739 char *buf;
1740 int len;
1741
1742 if (f->f_buf == NULL)
Tim Petersf1827cf2003-09-07 03:30:18 +00001743 if (readahead(f, bufsize) < 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001744 return NULL;
1745
1746 len = f->f_bufend - f->f_bufptr;
Tim Petersf1827cf2003-09-07 03:30:18 +00001747 if (len == 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001748 return (PyStringObject *)
1749 PyString_FromStringAndSize(NULL, skip);
1750 bufptr = memchr(f->f_bufptr, '\n', len);
1751 if (bufptr != NULL) {
1752 bufptr++; /* Count the '\n' */
1753 len = bufptr - f->f_bufptr;
1754 s = (PyStringObject *)
1755 PyString_FromStringAndSize(NULL, skip+len);
Tim Petersf1827cf2003-09-07 03:30:18 +00001756 if (s == NULL)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001757 return NULL;
1758 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1759 f->f_bufptr = bufptr;
1760 if (bufptr == f->f_bufend)
1761 drop_readahead(f);
1762 } else {
1763 bufptr = f->f_bufptr;
1764 buf = f->f_buf;
1765 f->f_buf = NULL; /* Force new readahead buffer */
1766 s = readahead_get_line_skip(
1767 f, skip+len, bufsize + (bufsize>>2) );
1768 if (s == NULL) {
1769 PyMem_Free(buf);
1770 return NULL;
1771 }
1772 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1773 PyMem_Free(buf);
1774 }
1775 return s;
1776}
1777
1778/* A larger buffer size may actually decrease performance. */
1779#define READAHEAD_BUFSIZE 8192
1780
1781static PyObject *
1782file_iternext(PyFileObject *f)
1783{
1784 PyStringObject* l;
1785
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001786 if (f->f_fp == NULL)
1787 return err_closed();
1788
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001789 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1790 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1791 Py_XDECREF(l);
1792 return NULL;
1793 }
1794 return (PyObject *)l;
1795}
1796
1797
Tim Peters59c9a642001-09-13 05:38:56 +00001798static PyObject *
1799file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1800{
Tim Peters44410012001-09-14 03:26:08 +00001801 PyObject *self;
1802 static PyObject *not_yet_string;
1803
1804 assert(type != NULL && type->tp_alloc != NULL);
1805
1806 if (not_yet_string == NULL) {
1807 not_yet_string = PyString_FromString("<uninitialized file>");
1808 if (not_yet_string == NULL)
1809 return NULL;
1810 }
1811
1812 self = type->tp_alloc(type, 0);
1813 if (self != NULL) {
1814 /* Always fill in the name and mode, so that nobody else
1815 needs to special-case NULLs there. */
1816 Py_INCREF(not_yet_string);
1817 ((PyFileObject *)self)->f_name = not_yet_string;
1818 Py_INCREF(not_yet_string);
1819 ((PyFileObject *)self)->f_mode = not_yet_string;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001820 Py_INCREF(Py_None);
1821 ((PyFileObject *)self)->f_encoding = Py_None;
Tim Peters44410012001-09-14 03:26:08 +00001822 }
1823 return self;
1824}
1825
1826static int
1827file_init(PyObject *self, PyObject *args, PyObject *kwds)
1828{
1829 PyFileObject *foself = (PyFileObject *)self;
1830 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001831 static char *kwlist[] = {"name", "mode", "buffering", 0};
1832 char *name = NULL;
1833 char *mode = "r";
1834 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001835 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001836
1837 assert(PyFile_Check(self));
1838 if (foself->f_fp != NULL) {
1839 /* Have to close the existing file first. */
1840 PyObject *closeresult = file_close(foself);
1841 if (closeresult == NULL)
1842 return -1;
1843 Py_DECREF(closeresult);
1844 }
Tim Peters59c9a642001-09-13 05:38:56 +00001845
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef Py_WIN_WIDE_FILENAMES
1847 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1848 PyObject *po;
1849 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1850 kwlist, &po, &mode, &bufsize)) {
1851 wideargument = 1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001852 if (fill_file_fields(foself, NULL, po, mode,
1853 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001854 goto Error;
1855 } else {
1856 /* Drop the argument parsing error as narrow
1857 strings are also valid. */
1858 PyErr_Clear();
1859 }
1860 }
1861#endif
1862
1863 if (!wideargument) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001864 PyObject *o_name;
1865
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001866 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1867 Py_FileSystemDefaultEncoding,
1868 &name,
1869 &mode, &bufsize))
1870 return -1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001871
1872 /* We parse again to get the name as a PyObject */
1873 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", kwlist,
1874 &o_name, &mode, &bufsize))
1875 return -1;
1876
1877 if (fill_file_fields(foself, NULL, o_name, mode,
1878 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001879 goto Error;
1880 }
Tim Peters44410012001-09-14 03:26:08 +00001881 if (open_the_file(foself, name, mode) == NULL)
1882 goto Error;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +00001883 foself->f_setbuf = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001884 PyFile_SetBufSize(self, bufsize);
1885 goto Done;
1886
1887Error:
1888 ret = -1;
1889 /* fall through */
1890Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001891 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001892 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001893}
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_VAR(file_doc) =
1896PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001897"file(name[, mode[, buffering]]) -> file object\n"
1898"\n"
1899"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1900"writing or appending. The file will be created if it doesn't exist\n"
1901"when opened for writing or appending; it will be truncated when\n"
1902"opened for writing. Add a 'b' to the mode for binary files.\n"
1903"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1904"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001905"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001908"Add a 'U' to mode to open the file for input with universal newline\n"
1909"support. Any line ending in the input file will be seen as a '\\n'\n"
1910"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1911"the value for this attribute is one of None (no newline read yet),\n"
1912"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1913"\n"
1914"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001915)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001917"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918"Note: open() is an alias for file()."
1919);
Tim Peters59c9a642001-09-13 05:38:56 +00001920
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001921PyTypeObject PyFile_Type = {
1922 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001923 0,
1924 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001925 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001926 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001927 (destructor)file_dealloc, /* tp_dealloc */
1928 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001929 0, /* tp_getattr */
1930 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001931 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001932 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001933 0, /* tp_as_number */
1934 0, /* tp_as_sequence */
1935 0, /* tp_as_mapping */
1936 0, /* tp_hash */
1937 0, /* tp_call */
1938 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001939 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00001940 /* softspace is writable: we must supply tp_setattro */
1941 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001942 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001943 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001944 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001945 0, /* tp_traverse */
1946 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001947 0, /* tp_richcompare */
1948 0, /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001949 (getiterfunc)file_getiter, /* tp_iter */
1950 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001951 file_methods, /* tp_methods */
1952 file_memberlist, /* tp_members */
1953 file_getsetlist, /* tp_getset */
1954 0, /* tp_base */
1955 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001956 0, /* tp_descr_get */
1957 0, /* tp_descr_set */
1958 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001959 (initproc)file_init, /* tp_init */
1960 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001961 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001962 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001963};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001964
1965/* Interface for the 'soft space' between print items. */
1966
1967int
Fred Drakefd99de62000-07-09 05:02:18 +00001968PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001969{
1970 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001971 if (f == NULL) {
1972 /* Do nothing */
1973 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001974 else if (PyFile_Check(f)) {
1975 oldflag = ((PyFileObject *)f)->f_softspace;
1976 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001977 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001978 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001979 PyObject *v;
1980 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001981 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001982 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001983 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001984 if (PyInt_Check(v))
1985 oldflag = PyInt_AsLong(v);
1986 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001987 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001989 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001990 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001991 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001992 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1993 PyErr_Clear();
1994 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001995 }
1996 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00001997 return oldflag;
1998}
Guido van Rossum3165fe61992-09-25 21:59:05 +00001999
2000/* Interfaces to write objects/strings to file-like objects */
2001
2002int
Fred Drakefd99de62000-07-09 05:02:18 +00002003PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002004{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002006 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002007 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002008 return -1;
2009 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002010 else if (PyFile_Check(f)) {
2011 FILE *fp = PyFile_AsFile(f);
Fred Drake086a0f72004-03-19 15:22:36 +00002012#ifdef Py_USING_UNICODE
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002013 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2014 int result;
Fred Drake086a0f72004-03-19 15:22:36 +00002015#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002016 if (fp == NULL) {
2017 err_closed();
2018 return -1;
2019 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002020#ifdef Py_USING_UNICODE
Tim Petersf1827cf2003-09-07 03:30:18 +00002021 if ((flags & Py_PRINT_RAW) &&
Martin v. Löwis415da6e2003-05-18 12:56:25 +00002022 PyUnicode_Check(v) && enc != Py_None) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002023 char *cenc = PyString_AS_STRING(enc);
2024 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2025 if (value == NULL)
2026 return -1;
2027 } else {
2028 value = v;
2029 Py_INCREF(value);
2030 }
2031 result = PyObject_Print(value, fp, flags);
2032 Py_DECREF(value);
2033 return result;
2034#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002035 return PyObject_Print(v, fp, flags);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002036#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002037 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002038 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002039 if (writer == NULL)
2040 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002041 if (flags & Py_PRINT_RAW) {
2042 if (PyUnicode_Check(v)) {
2043 value = v;
2044 Py_INCREF(value);
2045 } else
2046 value = PyObject_Str(v);
2047 }
2048 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002049 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002050 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002051 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002052 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002053 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002054 args = PyTuple_Pack(1, value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002055 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056 Py_DECREF(value);
2057 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002058 return -1;
2059 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002060 result = PyEval_CallObject(writer, args);
2061 Py_DECREF(args);
2062 Py_DECREF(value);
2063 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002064 if (result == NULL)
2065 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002066 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002067 return 0;
2068}
2069
Guido van Rossum27a60b11997-05-22 22:25:11 +00002070int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002071PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002072{
2073 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002074 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002075 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002076 PyErr_SetString(PyExc_SystemError,
2077 "null file for PyFile_WriteString");
2078 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002079 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002080 else if (PyFile_Check(f)) {
2081 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002082 if (fp == NULL) {
2083 err_closed();
2084 return -1;
2085 }
2086 fputs(s, fp);
2087 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002088 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002089 else if (!PyErr_Occurred()) {
2090 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002091 int err;
2092 if (v == NULL)
2093 return -1;
2094 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2095 Py_DECREF(v);
2096 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002097 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002098 else
2099 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002100}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002101
2102/* Try to get a file-descriptor from a Python object. If the object
2103 is an integer or long integer, its value is returned. If not, the
2104 object's fileno() method is called if it exists; the method must return
2105 an integer or long integer, which is returned as the file descriptor value.
2106 -1 is returned on failure.
2107*/
2108
2109int PyObject_AsFileDescriptor(PyObject *o)
2110{
2111 int fd;
2112 PyObject *meth;
2113
2114 if (PyInt_Check(o)) {
2115 fd = PyInt_AsLong(o);
2116 }
2117 else if (PyLong_Check(o)) {
2118 fd = PyLong_AsLong(o);
2119 }
2120 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2121 {
2122 PyObject *fno = PyEval_CallObject(meth, NULL);
2123 Py_DECREF(meth);
2124 if (fno == NULL)
2125 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002126
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002127 if (PyInt_Check(fno)) {
2128 fd = PyInt_AsLong(fno);
2129 Py_DECREF(fno);
2130 }
2131 else if (PyLong_Check(fno)) {
2132 fd = PyLong_AsLong(fno);
2133 Py_DECREF(fno);
2134 }
2135 else {
2136 PyErr_SetString(PyExc_TypeError,
2137 "fileno() returned a non-integer");
2138 Py_DECREF(fno);
2139 return -1;
2140 }
2141 }
2142 else {
2143 PyErr_SetString(PyExc_TypeError,
2144 "argument must be an int, or have a fileno() method.");
2145 return -1;
2146 }
2147
2148 if (fd < 0) {
2149 PyErr_Format(PyExc_ValueError,
2150 "file descriptor cannot be a negative integer (%i)",
2151 fd);
2152 return -1;
2153 }
2154 return fd;
2155}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002156
Jack Jansen7b8c7542002-04-14 20:12:41 +00002157/* From here on we need access to the real fgets and fread */
2158#undef fgets
2159#undef fread
2160
2161/*
2162** Py_UniversalNewlineFgets is an fgets variation that understands
2163** all of \r, \n and \r\n conventions.
2164** The stream should be opened in binary mode.
2165** If fobj is NULL the routine always does newline conversion, and
2166** it may peek one char ahead to gobble the second char in \r\n.
2167** If fobj is non-NULL it must be a PyFileObject. In this case there
2168** is no readahead but in stead a flag is used to skip a following
2169** \n on the next read. Also, if the file is open in binary mode
2170** the whole conversion is skipped. Finally, the routine keeps track of
2171** the different types of newlines seen.
2172** Note that we need no error handling: fgets() treats error and eof
2173** identically.
2174*/
2175char *
2176Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2177{
2178 char *p = buf;
2179 int c;
2180 int newlinetypes = 0;
2181 int skipnextlf = 0;
2182 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002183
Jack Jansen7b8c7542002-04-14 20:12:41 +00002184 if (fobj) {
2185 if (!PyFile_Check(fobj)) {
2186 errno = ENXIO; /* What can you do... */
2187 return NULL;
2188 }
2189 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2190 if ( !univ_newline )
2191 return fgets(buf, n, stream);
2192 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2193 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2194 }
2195 FLOCKFILE(stream);
2196 c = 'x'; /* Shut up gcc warning */
2197 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2198 if (skipnextlf ) {
2199 skipnextlf = 0;
2200 if (c == '\n') {
2201 /* Seeing a \n here with skipnextlf true
2202 ** means we saw a \r before.
2203 */
2204 newlinetypes |= NEWLINE_CRLF;
2205 c = GETC(stream);
2206 if (c == EOF) break;
2207 } else {
2208 /*
2209 ** Note that c == EOF also brings us here,
2210 ** so we're okay if the last char in the file
2211 ** is a CR.
2212 */
2213 newlinetypes |= NEWLINE_CR;
2214 }
2215 }
2216 if (c == '\r') {
2217 /* A \r is translated into a \n, and we skip
2218 ** an adjacent \n, if any. We don't set the
2219 ** newlinetypes flag until we've seen the next char.
2220 */
2221 skipnextlf = 1;
2222 c = '\n';
2223 } else if ( c == '\n') {
2224 newlinetypes |= NEWLINE_LF;
2225 }
2226 *p++ = c;
2227 if (c == '\n') break;
2228 }
2229 if ( c == EOF && skipnextlf )
2230 newlinetypes |= NEWLINE_CR;
2231 FUNLOCKFILE(stream);
2232 *p = '\0';
2233 if (fobj) {
2234 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2235 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2236 } else if ( skipnextlf ) {
2237 /* If we have no file object we cannot save the
2238 ** skipnextlf flag. We have to readahead, which
2239 ** will cause a pause if we're reading from an
2240 ** interactive stream, but that is very unlikely
2241 ** unless we're doing something silly like
2242 ** execfile("/dev/tty").
2243 */
2244 c = GETC(stream);
2245 if ( c != '\n' )
2246 ungetc(c, stream);
2247 }
2248 if (p == buf)
2249 return NULL;
2250 return buf;
2251}
2252
2253/*
2254** Py_UniversalNewlineFread is an fread variation that understands
2255** all of \r, \n and \r\n conventions.
2256** The stream should be opened in binary mode.
2257** fobj must be a PyFileObject. In this case there
2258** is no readahead but in stead a flag is used to skip a following
2259** \n on the next read. Also, if the file is open in binary mode
2260** the whole conversion is skipped. Finally, the routine keeps track of
2261** the different types of newlines seen.
2262*/
2263size_t
Tim Peters058b1412002-04-21 07:29:14 +00002264Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002265 FILE *stream, PyObject *fobj)
2266{
Tim Peters058b1412002-04-21 07:29:14 +00002267 char *dst = buf;
2268 PyFileObject *f = (PyFileObject *)fobj;
2269 int newlinetypes, skipnextlf;
2270
2271 assert(buf != NULL);
2272 assert(stream != NULL);
2273
Jack Jansen7b8c7542002-04-14 20:12:41 +00002274 if (!fobj || !PyFile_Check(fobj)) {
2275 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002276 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002277 }
Tim Peters058b1412002-04-21 07:29:14 +00002278 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002279 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002280 newlinetypes = f->f_newlinetypes;
2281 skipnextlf = f->f_skipnextlf;
2282 /* Invariant: n is the number of bytes remaining to be filled
2283 * in the buffer.
2284 */
2285 while (n) {
2286 size_t nread;
2287 int shortread;
2288 char *src = dst;
2289
2290 nread = fread(dst, 1, n, stream);
2291 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002292 if (nread == 0)
2293 break;
2294
Tim Peterse1682a82002-04-21 18:15:20 +00002295 n -= nread; /* assuming 1 byte out for each in; will adjust */
2296 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002297 while (nread--) {
2298 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002299 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002300 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002301 *dst++ = '\n';
2302 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002303 }
2304 else if (skipnextlf && c == '\n') {
2305 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002306 skipnextlf = 0;
2307 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002308 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002309 }
2310 else {
2311 /* Normal char to be stored in buffer. Also
2312 * update the newlinetypes flag if either this
2313 * is an LF or the previous char was a CR.
2314 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002315 if (c == '\n')
2316 newlinetypes |= NEWLINE_LF;
2317 else if (skipnextlf)
2318 newlinetypes |= NEWLINE_CR;
2319 *dst++ = c;
2320 skipnextlf = 0;
2321 }
2322 }
Tim Peters058b1412002-04-21 07:29:14 +00002323 if (shortread) {
2324 /* If this is EOF, update type flags. */
2325 if (skipnextlf && feof(stream))
2326 newlinetypes |= NEWLINE_CR;
2327 break;
2328 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002329 }
Tim Peters058b1412002-04-21 07:29:14 +00002330 f->f_newlinetypes = newlinetypes;
2331 f->f_skipnextlf = skipnextlf;
2332 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002333}