blob: 34f28e32ac550a4e937e7bb94667090568eda529 [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;
Skip Montanaro51ffac62004-06-11 04:49:03 +0000154
155 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
156 mode = "rb";
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000157#ifdef MS_WINDOWS
Skip Montanaro51ffac62004-06-11 04:49:03 +0000158 if (PyUnicode_Check(f->f_name)) {
159 PyObject *wmode;
160 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
161 if (f->f_name && wmode) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000162 Py_BEGIN_ALLOW_THREADS
Skip Montanaro51ffac62004-06-11 04:49:03 +0000163 /* PyUnicode_AS_UNICODE OK without thread
164 lock as it is a simple dereference. */
165 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
166 PyUnicode_AS_UNICODE(wmode));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000167 Py_END_ALLOW_THREADS
168 }
Skip Montanaro51ffac62004-06-11 04:49:03 +0000169 Py_XDECREF(wmode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000170 }
Skip Montanaro51ffac62004-06-11 04:49:03 +0000171#endif
172 if (NULL == f->f_fp && NULL != name) {
173 Py_BEGIN_ALLOW_THREADS
174 f->f_fp = fopen(name, mode);
175 Py_END_ALLOW_THREADS
176 }
177
Guido van Rossuma08095a1991-02-13 23:25:27 +0000178 if (f->f_fp == NULL) {
Tim Peters2ea91112002-04-08 04:13:12 +0000179#ifdef _MSC_VER
180 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
181 * across all Windows flavors. When it sets EINVAL varies
182 * across Windows flavors, the exact conditions aren't
183 * documented, and the answer lies in the OS's implementation
184 * of Win32's CreateFile function (whose source is secret).
185 * Seems the best we can do is map EINVAL to ENOENT.
186 */
187 if (errno == 0) /* bad mode string */
188 errno = EINVAL;
189 else if (errno == EINVAL) /* unknown, but not a mode string */
190 errno = ENOENT;
191#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000192 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000193 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000194 mode);
195 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000196 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000197 f = NULL;
198 }
Tim Peters2ea91112002-04-08 04:13:12 +0000199 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000200 f = dircheck(f);
Tim Peters59c9a642001-09-13 05:38:56 +0000201 return (PyObject *)f;
202}
203
204PyObject *
205PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
206{
Tim Peters44410012001-09-14 03:26:08 +0000207 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
208 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000209 if (f != NULL) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000210 PyObject *o_name = PyString_FromString(name);
211 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000212 Py_DECREF(f);
213 f = NULL;
214 }
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000215 Py_DECREF(o_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000216 }
217 return (PyObject *) f;
218}
219
220PyObject *
221PyFile_FromString(char *name, char *mode)
222{
223 extern int fclose(FILE *);
224 PyFileObject *f;
225
226 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
227 if (f != NULL) {
228 if (open_the_file(f, name, mode) == NULL) {
229 Py_DECREF(f);
230 f = NULL;
231 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000233 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234}
235
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236void
Fred Drakefd99de62000-07-09 05:02:18 +0000237PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238{
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000239 PyFileObject *file = (PyFileObject *)f;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240 if (bufsize >= 0) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241 int type;
242 switch (bufsize) {
243 case 0:
244 type = _IONBF;
245 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000246#ifdef HAVE_SETVBUF
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247 case 1:
248 type = _IOLBF;
249 bufsize = BUFSIZ;
250 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000251#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252 default:
253 type = _IOFBF;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000254#ifndef HAVE_SETVBUF
255 bufsize = BUFSIZ;
256#endif
257 break;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258 }
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000259 fflush(file->f_fp);
260 if (type == _IONBF) {
261 PyMem_Free(file->f_setbuf);
262 file->f_setbuf = NULL;
263 } else {
264 file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
265 }
266#ifdef HAVE_SETVBUF
267 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000268#else /* !HAVE_SETVBUF */
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000269 setbuf(file->f_fp, file->f_setbuf);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000270#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271 }
272}
273
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000274/* Set the encoding used to output Unicode strings.
275 Returh 1 on success, 0 on failure. */
276
277int
278PyFile_SetEncoding(PyObject *f, const char *enc)
279{
280 PyFileObject *file = (PyFileObject*)f;
281 PyObject *str = PyString_FromString(enc);
282 if (!str)
283 return 0;
284 Py_DECREF(file->f_encoding);
285 file->f_encoding = str;
286 return 1;
287}
288
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000290err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000291{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000293 return NULL;
294}
295
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000296static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000297
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298/* Methods */
299
300static void
Fred Drakefd99de62000-07-09 05:02:18 +0000301file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302{
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000303 if (f->weakreflist != NULL)
304 PyObject_ClearWeakRefs((PyObject *) f);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000305 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000306 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000307 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000309 }
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000310 PyMem_Free(f->f_setbuf);
Tim Peters44410012001-09-14 03:26:08 +0000311 Py_XDECREF(f->f_name);
312 Py_XDECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000313 Py_XDECREF(f->f_encoding);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000314 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000315 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000319file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000321 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000322#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000323 PyObject *ret = NULL;
324 PyObject *name;
325 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
326 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
327 f->f_fp == NULL ? "closed" : "open",
328 PyString_AsString(name),
329 PyString_AsString(f->f_mode),
330 f);
331 Py_XDECREF(name);
332 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000333#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000334 } else {
335 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000336 f->f_fp == NULL ? "closed" : "open",
337 PyString_AsString(f->f_name),
338 PyString_AsString(f->f_mode),
339 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000340 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341}
342
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000344file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000346 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000348 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000350 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000351 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000352 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000353 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000354 f->f_fp = NULL;
355 }
Martin v. Löwis7bbcde72003-09-07 20:42:29 +0000356 PyMem_Free(f->f_setbuf);
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000357 f->f_setbuf = NULL;
Guido van Rossumfebd5511992-03-04 16:39:24 +0000358 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000360 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 return PyInt_FromLong((long)sts);
362 Py_INCREF(Py_None);
363 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364}
365
Trent Mickf29f47b2000-08-11 19:02:59 +0000366
Guido van Rossumb8552162001-09-05 14:58:11 +0000367/* Our very own off_t-like type, 64-bit if possible */
368#if !defined(HAVE_LARGEFILE_SUPPORT)
369typedef off_t Py_off_t;
370#elif SIZEOF_OFF_T >= 8
371typedef off_t Py_off_t;
372#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000373typedef fpos_t Py_off_t;
374#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000375#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000376#endif
377
378
Trent Mickf29f47b2000-08-11 19:02:59 +0000379/* a portable fseek() function
380 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000381static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000382_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000383{
Guido van Rossumb8552162001-09-05 14:58:11 +0000384#if !defined(HAVE_LARGEFILE_SUPPORT)
385 return fseek(fp, offset, whence);
386#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000387 return fseeko(fp, offset, whence);
388#elif defined(HAVE_FSEEK64)
389 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000390#elif defined(__BEOS__)
391 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000392#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000393 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
394 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000395 fpos_t pos;
396 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000397 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000398#ifdef MS_WINDOWS
399 fflush(fp);
400 if (_lseeki64(fileno(fp), 0, 2) == -1)
401 return -1;
402#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000403 if (fseek(fp, 0, SEEK_END) != 0)
404 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000405#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000406 /* fall through */
407 case SEEK_CUR:
408 if (fgetpos(fp, &pos) != 0)
409 return -1;
410 offset += pos;
411 break;
412 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000413 }
414 return fsetpos(fp, &offset);
415#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000416#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000417#endif
418}
419
420
421/* a portable ftell() function
422 Return -1 on failure with errno set appropriately, current file
423 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000424static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000425_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000426{
Guido van Rossumb8552162001-09-05 14:58:11 +0000427#if !defined(HAVE_LARGEFILE_SUPPORT)
428 return ftell(fp);
429#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
430 return ftello(fp);
431#elif defined(HAVE_FTELL64)
432 return ftell64(fp);
433#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000434 fpos_t pos;
435 if (fgetpos(fp, &pos) != 0)
436 return -1;
437 return pos;
438#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000439#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000440#endif
441}
442
443
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000445file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000447 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000449 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000450 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000451
Guido van Rossumd7297e61992-07-06 14:19:26 +0000452 if (f->f_fp == NULL)
453 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000454 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000455 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000456 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000457 return NULL;
458#if !defined(HAVE_LARGEFILE_SUPPORT)
459 offset = PyInt_AsLong(offobj);
460#else
461 offset = PyLong_Check(offobj) ?
462 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
463#endif
464 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000465 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000468 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000469 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000471
Guido van Rossumff4949e1992-08-05 19:58:53 +0000472 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000474 clearerr(f->f_fp);
475 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000476 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000477 f->f_skipnextlf = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 Py_INCREF(Py_None);
479 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000480}
481
Trent Mickf29f47b2000-08-11 19:02:59 +0000482
Guido van Rossumd7047b31995-01-02 19:07:15 +0000483#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000485file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000486{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000487 Py_off_t newsize;
Tim Petersf1827cf2003-09-07 03:30:18 +0000488 PyObject *newsizeobj = NULL;
489 Py_off_t initialpos;
490 int ret;
Tim Peters86821b22001-01-07 21:19:34 +0000491
Guido van Rossumd7047b31995-01-02 19:07:15 +0000492 if (f->f_fp == NULL)
493 return err_closed();
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000494 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000495 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000496
Tim Petersf1827cf2003-09-07 03:30:18 +0000497 /* Get current file position. If the file happens to be open for
498 * update and the last operation was an input operation, C doesn't
499 * define what the later fflush() will do, but we promise truncate()
500 * won't change the current position (and fflush() *does* change it
501 * then at least on Windows). The easiest thing is to capture
502 * current pos now and seek back to it at the end.
503 */
504 Py_BEGIN_ALLOW_THREADS
505 errno = 0;
506 initialpos = _portable_ftell(f->f_fp);
507 Py_END_ALLOW_THREADS
508 if (initialpos == -1)
509 goto onioerror;
510
Tim Petersfb05db22002-03-11 00:24:00 +0000511 /* Set newsize to current postion if newsizeobj NULL, else to the
Tim Petersf1827cf2003-09-07 03:30:18 +0000512 * specified value.
513 */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000514 if (newsizeobj != NULL) {
515#if !defined(HAVE_LARGEFILE_SUPPORT)
516 newsize = PyInt_AsLong(newsizeobj);
517#else
518 newsize = PyLong_Check(newsizeobj) ?
519 PyLong_AsLongLong(newsizeobj) :
520 PyInt_AsLong(newsizeobj);
521#endif
522 if (PyErr_Occurred())
523 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000524 }
Tim Petersf1827cf2003-09-07 03:30:18 +0000525 else /* default to current position */
526 newsize = initialpos;
Tim Petersfb05db22002-03-11 00:24:00 +0000527
Tim Petersf1827cf2003-09-07 03:30:18 +0000528 /* Flush the stream. We're mixing stream-level I/O with lower-level
529 * I/O, and a flush may be necessary to synch both platform views
530 * of the current file state.
531 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000533 errno = 0;
534 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000536 if (ret != 0)
537 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000538
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000539#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000540 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000541 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000542 {
Tim Petersfb05db22002-03-11 00:24:00 +0000543 HANDLE hFile;
Tim Petersfb05db22002-03-11 00:24:00 +0000544
Tim Petersf1827cf2003-09-07 03:30:18 +0000545 /* Have to move current pos to desired endpoint on Windows. */
546 Py_BEGIN_ALLOW_THREADS
547 errno = 0;
548 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
549 Py_END_ALLOW_THREADS
550 if (ret)
551 goto onioerror;
Tim Petersfb05db22002-03-11 00:24:00 +0000552
Tim Peters8f01b682002-03-12 03:04:44 +0000553 /* Truncate. Note that this may grow the file! */
554 Py_BEGIN_ALLOW_THREADS
555 errno = 0;
556 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
Tim Petersf1827cf2003-09-07 03:30:18 +0000557 ret = hFile == (HANDLE)-1;
558 if (ret == 0) {
559 ret = SetEndOfFile(hFile) == 0;
560 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000561 errno = EACCES;
562 }
563 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000564 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000565 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000566 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000567#else
568 Py_BEGIN_ALLOW_THREADS
569 errno = 0;
570 ret = ftruncate(fileno(f->f_fp), newsize);
571 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000572 if (ret != 0)
573 goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000574#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000575
Tim Petersf1827cf2003-09-07 03:30:18 +0000576 /* Restore original file position. */
577 Py_BEGIN_ALLOW_THREADS
578 errno = 0;
579 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
580 Py_END_ALLOW_THREADS
581 if (ret)
582 goto onioerror;
583
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 Py_INCREF(Py_None);
585 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000586
587onioerror:
588 PyErr_SetFromErrno(PyExc_IOError);
589 clearerr(f->f_fp);
590 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000591}
592#endif /* HAVE_FTRUNCATE */
593
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000595file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000596{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000597 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000598
Guido van Rossumd7297e61992-07-06 14:19:26 +0000599 if (f->f_fp == NULL)
600 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000601 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000602 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000603 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000605 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000607 clearerr(f->f_fp);
608 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000609 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000610 if (f->f_skipnextlf) {
611 int c;
612 c = GETC(f->f_fp);
613 if (c == '\n') {
614 pos++;
615 f->f_skipnextlf = 0;
616 } else if (c != EOF) ungetc(c, f->f_fp);
617 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000618#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000619 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000620#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000621 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000622#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000623}
624
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000626file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000627{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000628 if (f->f_fp == NULL)
629 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000634file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000635{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000636 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000637
Guido van Rossumd7297e61992-07-06 14:19:26 +0000638 if (f->f_fp == NULL)
639 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000641 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000642 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000644 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000646 clearerr(f->f_fp);
647 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000648 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 Py_INCREF(Py_None);
650 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000651}
652
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000654file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000655{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000656 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000657 if (f->f_fp == NULL)
658 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000660 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000662 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000663}
664
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000665
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000666#if BUFSIZ < 8192
667#define SMALLCHUNK 8192
668#else
669#define SMALLCHUNK BUFSIZ
670#endif
671
Guido van Rossum3c259041999-01-14 19:00:14 +0000672#if SIZEOF_INT < 4
673#define BIGCHUNK (512 * 32)
674#else
675#define BIGCHUNK (512 * 1024)
676#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000677
678static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000679new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000680{
681#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000682 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000683 struct stat st;
684 if (fstat(fileno(f->f_fp), &st) == 0) {
685 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000686 /* The following is not a bug: we really need to call lseek()
687 *and* ftell(). The reason is that some stdio libraries
688 mistakenly flush their buffer when ftell() is called and
689 the lseek() call it makes fails, thereby throwing away
690 data that cannot be recovered in any way. To avoid this,
691 we first test lseek(), and only call ftell() if lseek()
692 works. We can't use the lseek() value either, because we
693 need to take the amount of buffered data into account.
694 (Yet another reason why stdio stinks. :-) */
Guido van Rossum91aaa921998-05-05 22:21:35 +0000695 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000696 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000697 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000698 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000699 if (pos < 0)
700 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000701 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000702 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000703 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000704 }
705#endif
706 if (currentsize > SMALLCHUNK) {
707 /* Keep doubling until we reach BIGCHUNK;
708 then keep adding BIGCHUNK. */
709 if (currentsize <= BIGCHUNK)
710 return currentsize + currentsize;
711 else
712 return currentsize + BIGCHUNK;
713 }
714 return currentsize + SMALLCHUNK;
715}
716
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000717#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
718#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
719#else
720#ifdef EWOULDBLOCK
721#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
722#else
723#ifdef EAGAIN
724#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
725#else
726#define BLOCKED_ERRNO(x) 0
727#endif
728#endif
729#endif
730
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000732file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000733{
Guido van Rossum789a1611997-05-10 22:33:55 +0000734 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000735 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000737
Guido van Rossumd7297e61992-07-06 14:19:26 +0000738 if (f->f_fp == NULL)
739 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000740 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000741 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000742 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000743 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000744 else
745 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000746 if (buffersize > INT_MAX) {
747 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000748 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000749 return NULL;
750 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000751 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000752 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000754 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000755 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000756 Py_BEGIN_ALLOW_THREADS
757 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000758 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000759 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000760 Py_END_ALLOW_THREADS
761 if (chunksize == 0) {
762 if (!ferror(f->f_fp))
763 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000764 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000765 /* When in non-blocking mode, data shouldn't
766 * be discarded if a blocking signal was
767 * received. That will also happen if
768 * chunksize != 0, but bytesread < buffersize. */
769 if (bytesread > 0 && BLOCKED_ERRNO(errno))
770 break;
771 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000772 Py_DECREF(v);
773 return NULL;
774 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000775 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000776 if (bytesread < buffersize) {
777 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000778 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000779 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000780 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000781 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000782 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000783 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000784 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000785 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000786 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000787 }
788 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000789 if (bytesread != buffersize)
790 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791 return v;
792}
793
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000794static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000795file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000796{
797 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000798 int ntodo;
799 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000800
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000801 if (f->f_fp == NULL)
802 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000803 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000804 return NULL;
805 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000806 while (ntodo > 0) {
807 Py_BEGIN_ALLOW_THREADS
808 errno = 0;
Tim Petersf1827cf2003-09-07 03:30:18 +0000809 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000810 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000811 Py_END_ALLOW_THREADS
812 if (nnow == 0) {
813 if (!ferror(f->f_fp))
814 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000815 PyErr_SetFromErrno(PyExc_IOError);
816 clearerr(f->f_fp);
817 return NULL;
818 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000819 ndone += nnow;
820 ntodo -= nnow;
821 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000822 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000823}
824
Tim Peters86821b22001-01-07 21:19:34 +0000825/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000826Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000827
828Under MSVC 6:
829
Tim Peters1c733232001-01-08 04:02:07 +0000830+ MS threadsafe getc is very slow (multiple layers of function calls before+
831 after each character, to lock+unlock the stream).
832+ The stream-locking functions are MS-internal -- can't access them from user
833 code.
834+ There's nothing Tim could find in the MS C or platform SDK libraries that
835 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000836+ MS fgets locks/unlocks only once per line; it's the only hook we have.
837
838So we use fgets for speed(!), despite that it's painful.
839
840MS realloc is also slow.
841
Tim Petersf29b64d2001-01-15 06:33:19 +0000842Reports from other platforms on this method vs getc_unlocked (which MS doesn't
843have):
844 Linux a wash
845 Solaris a wash
846 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000847
Tim Petersf29b64d2001-01-15 06:33:19 +0000848CAUTION: The C std isn't clear about this: in those cases where fgets
849writes something into the buffer, can it write into any position beyond the
850required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
851known on which it does; and it would be a strange way to code fgets. Still,
852getline_via_fgets may not work correctly if it does. The std test
853test_bufio.py should fail if platform fgets() routinely writes beyond the
854trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000855**************************************************************************/
856
Tim Petersf29b64d2001-01-15 06:33:19 +0000857/* Use this routine if told to, or by default on non-get_unlocked()
858 * platforms unless told not to. Yikes! Let's spell that out:
859 * On a platform with getc_unlocked():
860 * By default, use getc_unlocked().
861 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
862 * On a platform without getc_unlocked():
863 * By default, use fgets().
864 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
865 */
866#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
867#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000868#endif
869
Tim Petersf29b64d2001-01-15 06:33:19 +0000870#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
871#undef USE_FGETS_IN_GETLINE
872#endif
873
874#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000875static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000876getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000877{
Tim Peters15b83852001-01-08 00:53:12 +0000878/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000879 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
880 * to fill this much of the buffer with a known value in order to figure out
881 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
882 * than "most" lines, we waste time filling unused buffer slots. 100 is
883 * surely adequate for most peoples' email archives, chewing over source code,
884 * etc -- "regular old text files".
885 * MAXBUFSIZE is the maximum line length that lets us get away with the less
886 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
887 * cautions about boosting that. 300 was chosen because the worst real-life
888 * text-crunching job reported on Python-Dev was a mail-log crawler where over
889 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000890 */
Tim Peters142297a2001-01-15 10:36:56 +0000891#define INITBUFSIZE 100
892#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000893 char* p; /* temp */
894 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000895 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000896 char* pvfree; /* address of next free slot */
897 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000898 size_t nfree; /* # of free buffer slots; pvend-pvfree */
899 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000900 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000901
Tim Peters15b83852001-01-08 00:53:12 +0000902 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000903 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000904 */
Tim Peters142297a2001-01-15 10:36:56 +0000905 total_v_size = INITBUFSIZE; /* start small and pray */
906 pvfree = buf;
907 for (;;) {
908 Py_BEGIN_ALLOW_THREADS
909 pvend = buf + total_v_size;
910 nfree = pvend - pvfree;
911 memset(pvfree, '\n', nfree);
912 p = fgets(pvfree, nfree, fp);
913 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000914
Tim Peters142297a2001-01-15 10:36:56 +0000915 if (p == NULL) {
916 clearerr(fp);
917 if (PyErr_CheckSignals())
918 return NULL;
919 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000920 return v;
921 }
Tim Peters142297a2001-01-15 10:36:56 +0000922 /* fgets read *something* */
923 p = memchr(pvfree, '\n', nfree);
924 if (p != NULL) {
925 /* Did the \n come from fgets or from us?
926 * Since fgets stops at the first \n, and then writes
927 * \0, if it's from fgets a \0 must be next. But if
928 * that's so, it could not have come from us, since
929 * the \n's we filled the buffer with have only more
930 * \n's to the right.
931 */
932 if (p+1 < pvend && *(p+1) == '\0') {
933 /* It's from fgets: we win! In particular,
934 * we haven't done any mallocs yet, and can
935 * build the final result on the first try.
936 */
937 ++p; /* include \n from fgets */
938 }
939 else {
940 /* Must be from us: fgets didn't fill the
941 * buffer and didn't find a newline, so it
942 * must be the last and newline-free line of
943 * the file.
944 */
945 assert(p > pvfree && *(p-1) == '\0');
946 --p; /* don't include \0 from fgets */
947 }
948 v = PyString_FromStringAndSize(buf, p - buf);
949 return v;
950 }
951 /* yuck: fgets overwrote all the newlines, i.e. the entire
952 * buffer. So this line isn't over yet, or maybe it is but
953 * we're exactly at EOF. If we haven't already, try using the
954 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000955 */
Tim Peters142297a2001-01-15 10:36:56 +0000956 assert(*(pvend-1) == '\0');
957 if (pvfree == buf) {
958 pvfree = pvend - 1; /* overwrite trailing null */
959 total_v_size = MAXBUFSIZE;
960 }
961 else
962 break;
Tim Peters86821b22001-01-07 21:19:34 +0000963 }
Tim Peters142297a2001-01-15 10:36:56 +0000964
965 /* The stack buffer isn't big enough; malloc a string object and read
966 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000967 */
Tim Petersddea2082002-03-23 10:03:50 +0000968 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +0000969 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000970 if (v == NULL)
971 return v;
972 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +0000973 memcpy(BUF(v), buf, MAXBUFSIZE-1);
974 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +0000975
976 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +0000977 * after setting p one beyond the end of the line. The code here is
978 * very much like the code above, except reads into v's buffer; see
979 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +0000980 */
981 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +0000982 Py_BEGIN_ALLOW_THREADS
983 pvend = BUF(v) + total_v_size;
984 nfree = pvend - pvfree;
985 memset(pvfree, '\n', nfree);
986 p = fgets(pvfree, nfree, fp);
987 Py_END_ALLOW_THREADS
988
989 if (p == NULL) {
990 clearerr(fp);
991 if (PyErr_CheckSignals()) {
992 Py_DECREF(v);
993 return NULL;
994 }
995 p = pvfree;
996 break;
997 }
Tim Peters86821b22001-01-07 21:19:34 +0000998 p = memchr(pvfree, '\n', nfree);
999 if (p != NULL) {
1000 if (p+1 < pvend && *(p+1) == '\0') {
1001 /* \n came from fgets */
1002 ++p;
1003 break;
1004 }
1005 /* \n came from us; last line of file, no newline */
1006 assert(p > pvfree && *(p-1) == '\0');
1007 --p;
1008 break;
1009 }
1010 /* expand buffer and try again */
1011 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001012 increment = total_v_size >> 2; /* mild exponential growth */
1013 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001014 if (total_v_size > INT_MAX) {
1015 PyErr_SetString(PyExc_OverflowError,
1016 "line is longer than a Python string can hold");
1017 Py_DECREF(v);
1018 return NULL;
1019 }
1020 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1021 return NULL;
1022 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001023 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001024 }
1025 if (BUF(v) + total_v_size != p)
1026 _PyString_Resize(&v, p - BUF(v));
1027 return v;
1028#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001029#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001030}
Tim Petersf29b64d2001-01-15 06:33:19 +00001031#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001032
Guido van Rossum0bd24411991-04-04 15:21:57 +00001033/* Internal routine to get a line.
1034 Size argument interpretation:
1035 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001036 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001037*/
1038
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001040get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001041{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001042 FILE *fp = f->f_fp;
1043 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001044 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001045 size_t total_v_size; /* total # of slots in buffer */
1046 size_t used_v_size; /* # used slots in buffer */
1047 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001048 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001049 int newlinetypes = f->f_newlinetypes;
1050 int skipnextlf = f->f_skipnextlf;
1051 int univ_newline = f->f_univ_newline;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001052
Jack Jansen7b8c7542002-04-14 20:12:41 +00001053#if defined(USE_FGETS_IN_GETLINE)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001054 if (n <= 0 && !univ_newline )
Tim Petersf29b64d2001-01-15 06:33:19 +00001055 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001056#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001057 total_v_size = n > 0 ? n : 100;
1058 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001059 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001061 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001062 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001063
Guido van Rossumce5ba841991-03-06 13:06:18 +00001064 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001065 Py_BEGIN_ALLOW_THREADS
1066 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001067 if (univ_newline) {
1068 c = 'x'; /* Shut up gcc warning */
1069 while ( buf != end && (c = GETC(fp)) != EOF ) {
1070 if (skipnextlf ) {
1071 skipnextlf = 0;
1072 if (c == '\n') {
Tim Petersf1827cf2003-09-07 03:30:18 +00001073 /* Seeing a \n here with
1074 * skipnextlf true means we
Jeremy Hylton8b735422002-08-14 21:01:41 +00001075 * saw a \r before.
1076 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001077 newlinetypes |= NEWLINE_CRLF;
1078 c = GETC(fp);
1079 if (c == EOF) break;
1080 } else {
1081 newlinetypes |= NEWLINE_CR;
1082 }
1083 }
1084 if (c == '\r') {
1085 skipnextlf = 1;
1086 c = '\n';
1087 } else if ( c == '\n')
1088 newlinetypes |= NEWLINE_LF;
1089 *buf++ = c;
1090 if (c == '\n') break;
1091 }
1092 if ( c == EOF && skipnextlf )
1093 newlinetypes |= NEWLINE_CR;
1094 } else /* If not universal newlines use the normal loop */
Guido van Rossum1187aa42001-01-05 14:43:05 +00001095 while ((c = GETC(fp)) != EOF &&
1096 (*buf++ = c) != '\n' &&
1097 buf != end)
1098 ;
1099 FUNLOCKFILE(fp);
1100 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001101 f->f_newlinetypes = newlinetypes;
1102 f->f_skipnextlf = skipnextlf;
Guido van Rossum1187aa42001-01-05 14:43:05 +00001103 if (c == '\n')
1104 break;
1105 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001106 if (ferror(fp)) {
1107 PyErr_SetFromErrno(PyExc_IOError);
1108 clearerr(fp);
1109 Py_DECREF(v);
1110 return NULL;
1111 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001112 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001114 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001115 return NULL;
1116 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001117 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001118 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001119 /* Must be because buf == end */
1120 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001121 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001122 used_v_size = total_v_size;
1123 increment = total_v_size >> 2; /* mild exponential growth */
1124 total_v_size += increment;
1125 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001126 PyErr_SetString(PyExc_OverflowError,
1127 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001128 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001129 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001130 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001131 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001132 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001133 buf = BUF(v) + used_v_size;
1134 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001136
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001137 used_v_size = buf - BUF(v);
1138 if (used_v_size != total_v_size)
1139 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 return v;
1141}
1142
Guido van Rossum0bd24411991-04-04 15:21:57 +00001143/* External C interface */
1144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001146PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001147{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001148 PyObject *result;
1149
Guido van Rossum3165fe61992-09-25 21:59:05 +00001150 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001152 return NULL;
1153 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001154
1155 if (PyFile_Check(f)) {
1156 if (((PyFileObject*)f)->f_fp == NULL)
1157 return err_closed();
1158 result = get_line((PyFileObject *)f, n);
1159 }
1160 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 PyObject *reader;
1162 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001163
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001165 if (reader == NULL)
1166 return NULL;
1167 if (n <= 0)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001168 args = PyTuple_New(0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001169 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001171 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001173 return NULL;
1174 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 result = PyEval_CallObject(reader, args);
1176 Py_DECREF(reader);
1177 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001178 if (result != NULL && !PyString_Check(result) &&
1179 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001181 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001183 "object.readline() returned non-string");
1184 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001185 }
1186
1187 if (n < 0 && result != NULL && PyString_Check(result)) {
1188 char *s = PyString_AS_STRING(result);
1189 int len = PyString_GET_SIZE(result);
1190 if (len == 0) {
1191 Py_DECREF(result);
1192 result = NULL;
1193 PyErr_SetString(PyExc_EOFError,
1194 "EOF when reading a line");
1195 }
1196 else if (s[len-1] == '\n') {
1197 if (result->ob_refcnt == 1)
1198 _PyString_Resize(&result, len-1);
1199 else {
1200 PyObject *v;
1201 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001202 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001203 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001204 }
1205 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001206 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001207#ifdef Py_USING_UNICODE
1208 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1209 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1210 int len = PyUnicode_GET_SIZE(result);
1211 if (len == 0) {
1212 Py_DECREF(result);
1213 result = NULL;
1214 PyErr_SetString(PyExc_EOFError,
1215 "EOF when reading a line");
1216 }
1217 else if (s[len-1] == '\n') {
1218 if (result->ob_refcnt == 1)
1219 PyUnicode_Resize(&result, len-1);
1220 else {
1221 PyObject *v;
1222 v = PyUnicode_FromUnicode(s, len-1);
1223 Py_DECREF(result);
1224 result = v;
1225 }
1226 }
1227 }
1228#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001229 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001230}
1231
1232/* Python method */
1233
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001235file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001236{
Guido van Rossum789a1611997-05-10 22:33:55 +00001237 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001238
Guido van Rossumd7297e61992-07-06 14:19:26 +00001239 if (f->f_fp == NULL)
1240 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001241 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001242 return NULL;
1243 if (n == 0)
1244 return PyString_FromString("");
1245 if (n < 0)
1246 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001247 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001248}
1249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001251file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001252{
Guido van Rossum789a1611997-05-10 22:33:55 +00001253 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254 PyObject *list;
1255 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001256 char small_buffer[SMALLCHUNK];
1257 char *buffer = small_buffer;
1258 size_t buffersize = SMALLCHUNK;
1259 PyObject *big_buffer = NULL;
1260 size_t nfilled = 0;
1261 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001262 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001263 char *p, *q, *end;
1264 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001265 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001266
Guido van Rossumd7297e61992-07-06 14:19:26 +00001267 if (f->f_fp == NULL)
1268 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001269 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001270 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001272 return NULL;
1273 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001274 if (shortread)
1275 nread = 0;
1276 else {
1277 Py_BEGIN_ALLOW_THREADS
1278 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001279 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001280 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001281 Py_END_ALLOW_THREADS
1282 shortread = (nread < buffersize-nfilled);
1283 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001284 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001285 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001286 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001287 break;
1288 PyErr_SetFromErrno(PyExc_IOError);
1289 clearerr(f->f_fp);
1290 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001292 list = NULL;
1293 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001294 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001295 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001296 p = memchr(buffer+nfilled, '\n', nread);
1297 if (p == NULL) {
1298 /* Need a larger buffer to fit this line */
1299 nfilled += nread;
1300 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001301 if (buffersize > INT_MAX) {
1302 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001303 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001304 goto error;
1305 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001306 if (big_buffer == NULL) {
1307 /* Create the big buffer */
1308 big_buffer = PyString_FromStringAndSize(
1309 NULL, buffersize);
1310 if (big_buffer == NULL)
1311 goto error;
1312 buffer = PyString_AS_STRING(big_buffer);
1313 memcpy(buffer, small_buffer, nfilled);
1314 }
1315 else {
1316 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001317 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1318 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001319 buffer = PyString_AS_STRING(big_buffer);
1320 }
1321 continue;
1322 }
1323 end = buffer+nfilled+nread;
1324 q = buffer;
1325 do {
1326 /* Process complete lines */
1327 p++;
1328 line = PyString_FromStringAndSize(q, p-q);
1329 if (line == NULL)
1330 goto error;
1331 err = PyList_Append(list, line);
1332 Py_DECREF(line);
1333 if (err != 0)
1334 goto error;
1335 q = p;
1336 p = memchr(q, '\n', end-q);
1337 } while (p != NULL);
1338 /* Move the remaining incomplete line to the start */
1339 nfilled = end-q;
1340 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001341 if (sizehint > 0)
1342 if (totalread >= (size_t)sizehint)
1343 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001344 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001345 if (nfilled != 0) {
1346 /* Partial last line */
1347 line = PyString_FromStringAndSize(buffer, nfilled);
1348 if (line == NULL)
1349 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001350 if (sizehint > 0) {
1351 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001352 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001353 if (rest == NULL) {
1354 Py_DECREF(line);
1355 goto error;
1356 }
1357 PyString_Concat(&line, rest);
1358 Py_DECREF(rest);
1359 if (line == NULL)
1360 goto error;
1361 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001362 err = PyList_Append(list, line);
1363 Py_DECREF(line);
1364 if (err != 0)
1365 goto error;
1366 }
1367 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001368 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001369 return list;
1370}
1371
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001372static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001373file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001374{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001375 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001376 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001377 if (f->f_fp == NULL)
1378 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001379 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001380 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001381 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001384 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001386 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001388 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001389 return NULL;
1390 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001391 Py_INCREF(Py_None);
1392 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001393}
1394
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001396file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001397{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001398#define CHUNKSIZE 1000
1399 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001400 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001401 PyObject *result;
1402 int i, j, index, len, nwritten, islist;
1403
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001404 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001405 if (f->f_fp == NULL)
1406 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001407
1408 result = NULL;
1409 list = NULL;
1410 islist = PyList_Check(seq);
1411 if (islist)
1412 it = NULL;
1413 else {
1414 it = PyObject_GetIter(seq);
1415 if (it == NULL) {
1416 PyErr_SetString(PyExc_TypeError,
1417 "writelines() requires an iterable argument");
1418 return NULL;
1419 }
1420 /* From here on, fail by going to error, to reclaim "it". */
1421 list = PyList_New(CHUNKSIZE);
1422 if (list == NULL)
1423 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001424 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001425
1426 /* Strategy: slurp CHUNKSIZE lines into a private list,
1427 checking that they are all strings, then write that list
1428 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001429 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001430 if (islist) {
1431 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001432 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001433 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001434 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001435 j = PyList_GET_SIZE(list);
1436 }
1437 else {
1438 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001439 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001440 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001441 if (PyErr_Occurred())
1442 goto error;
1443 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001444 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001445 PyList_SetItem(list, j, line);
1446 }
1447 }
1448 if (j == 0)
1449 break;
1450
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001451 /* Check that all entries are indeed strings. If not,
1452 apply the same rules as for file.write() and
1453 convert the results to strings. This is slow, but
1454 seems to be the only way since all conversion APIs
1455 could potentially execute Python code. */
1456 for (i = 0; i < j; i++) {
1457 PyObject *v = PyList_GET_ITEM(list, i);
1458 if (!PyString_Check(v)) {
1459 const char *buffer;
1460 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001461 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001462 PyObject_AsReadBuffer(v,
1463 (const void**)&buffer,
1464 &len)) ||
1465 PyObject_AsCharBuffer(v,
1466 &buffer,
1467 &len))) {
1468 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001469 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001470 goto error;
1471 }
1472 line = PyString_FromStringAndSize(buffer,
1473 len);
1474 if (line == NULL)
1475 goto error;
1476 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001477 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001478 }
1479 }
1480
1481 /* Since we are releasing the global lock, the
1482 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001483 Py_BEGIN_ALLOW_THREADS
1484 f->f_softspace = 0;
1485 errno = 0;
1486 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001487 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001488 len = PyString_GET_SIZE(line);
1489 nwritten = fwrite(PyString_AS_STRING(line),
1490 1, len, f->f_fp);
1491 if (nwritten != len) {
1492 Py_BLOCK_THREADS
1493 PyErr_SetFromErrno(PyExc_IOError);
1494 clearerr(f->f_fp);
1495 goto error;
1496 }
1497 }
1498 Py_END_ALLOW_THREADS
1499
1500 if (j < CHUNKSIZE)
1501 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001502 }
1503
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001504 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001505 result = Py_None;
1506 error:
1507 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001508 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001509 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001510#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001511}
1512
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001513static PyObject *
1514file_getiter(PyFileObject *f)
1515{
1516 if (f->f_fp == NULL)
1517 return err_closed();
1518 Py_INCREF(f);
1519 return (PyObject *)f;
1520}
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001523"readline([size]) -> next line from the file, as a string.\n"
1524"\n"
1525"Retain newline. A non-negative size argument limits the maximum\n"
1526"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001529PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001530"read([size]) -> read at most size bytes, returned as a string.\n"
1531"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001532"If the size argument is negative or omitted, read until EOF is reached.\n"
1533"Notice that when in non-blocking mode, less data than what was requested\n"
1534"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001537"write(str) -> None. Write string str to file.\n"
1538"\n"
1539"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001540"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001542PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001543"fileno() -> integer \"file descriptor\".\n"
1544"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001548"seek(offset[, whence]) -> None. Move to new file position.\n"
1549"\n"
1550"Argument offset is a byte count. Optional argument whence defaults to\n"
1551"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1552"(move relative to current position, positive or negative), and 2 (move\n"
1553"relative to end of file, usually negative, although many platforms allow\n"
Martin v. Löwis849a9722003-10-18 09:38:01 +00001554"seeking beyond the end of a file). If the file is opened in text mode,\n"
1555"only offsets returned by tell() are legal. Use of other offsets causes\n"
1556"undefined behavior."
Tim Petersefc3a3a2001-09-20 07:55:22 +00001557"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001559
Guido van Rossumd7047b31995-01-02 19:07:15 +00001560#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001562"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1563"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001565#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567PyDoc_STRVAR(tell_doc,
1568"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(readinto_doc,
1571"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001573PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001574"readlines([size]) -> list of strings, each a line from the file.\n"
1575"\n"
1576"Call readline() repeatedly and return a list of the lines so read.\n"
1577"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001578"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001580PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001581"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001582"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001583"For backward compatibility. File objects now include the performance\n"
1584"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001587"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001588"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001589"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001590"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592PyDoc_STRVAR(flush_doc,
1593"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001596"close() -> None or (perhaps) an integer. Close the file.\n"
1597"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001598"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001599"further I/O operations. close() may be called more than once without\n"
1600"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603PyDoc_STRVAR(isatty_doc,
1604"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001605
1606static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001607 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1608 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1609 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1610 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1611 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001612#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001613 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001614#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001615 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1616 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1617 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1618 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1619 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1620 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1621 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1622 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1623 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001624};
1625
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001626#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001627
Guido van Rossum6f799372001-09-20 20:46:19 +00001628static PyMemberDef file_memberlist[] = {
1629 {"softspace", T_INT, OFF(f_softspace), 0,
1630 "flag indicating that a space needs to be printed; used by print"},
1631 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001632 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001633 {"name", T_OBJECT, OFF(f_name), RO,
1634 "file name"},
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001635 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1636 "file encoding"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001637 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001638 {NULL} /* Sentinel */
1639};
1640
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001641static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001642get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001643{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001644 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001645}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001646static PyObject *
1647get_newlines(PyFileObject *f, void *closure)
1648{
1649 switch (f->f_newlinetypes) {
1650 case NEWLINE_UNKNOWN:
1651 Py_INCREF(Py_None);
1652 return Py_None;
1653 case NEWLINE_CR:
1654 return PyString_FromString("\r");
1655 case NEWLINE_LF:
1656 return PyString_FromString("\n");
1657 case NEWLINE_CR|NEWLINE_LF:
1658 return Py_BuildValue("(ss)", "\r", "\n");
1659 case NEWLINE_CRLF:
1660 return PyString_FromString("\r\n");
1661 case NEWLINE_CR|NEWLINE_CRLF:
1662 return Py_BuildValue("(ss)", "\r", "\r\n");
1663 case NEWLINE_LF|NEWLINE_CRLF:
1664 return Py_BuildValue("(ss)", "\n", "\r\n");
1665 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1666 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1667 default:
Tim Petersf1827cf2003-09-07 03:30:18 +00001668 PyErr_Format(PyExc_SystemError,
1669 "Unknown newlines value 0x%x\n",
Jeremy Hylton8b735422002-08-14 21:01:41 +00001670 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001671 return NULL;
1672 }
1673}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001674
Guido van Rossum32d34c82001-09-20 21:45:26 +00001675static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001676 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Tim Petersf1827cf2003-09-07 03:30:18 +00001677 {"newlines", (getter)get_newlines, NULL,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001678 "end-of-line convention used in this file"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001679 {0},
1680};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001681
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001682static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001683drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001684{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001685 if (f->f_buf != NULL) {
1686 PyMem_Free(f->f_buf);
1687 f->f_buf = NULL;
1688 }
Guido van Rossum65967252001-04-21 13:20:18 +00001689}
1690
Tim Petersf1827cf2003-09-07 03:30:18 +00001691/* Make sure that file has a readahead buffer with at least one byte
1692 (unless at EOF) and no more than bufsize. Returns negative value on
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001693 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001694static int
1695readahead(PyFileObject *f, int bufsize)
1696{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001697 int chunksize;
1698
1699 if (f->f_buf != NULL) {
Tim Petersf1827cf2003-09-07 03:30:18 +00001700 if( (f->f_bufend - f->f_bufptr) >= 1)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001701 return 0;
1702 else
1703 drop_readahead(f);
1704 }
1705 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1706 return -1;
1707 }
1708 Py_BEGIN_ALLOW_THREADS
1709 errno = 0;
1710 chunksize = Py_UniversalNewlineFread(
1711 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1712 Py_END_ALLOW_THREADS
1713 if (chunksize == 0) {
1714 if (ferror(f->f_fp)) {
1715 PyErr_SetFromErrno(PyExc_IOError);
1716 clearerr(f->f_fp);
1717 drop_readahead(f);
1718 return -1;
1719 }
1720 }
1721 f->f_bufptr = f->f_buf;
1722 f->f_bufend = f->f_buf + chunksize;
1723 return 0;
1724}
1725
1726/* Used by file_iternext. The returned string will start with 'skip'
Tim Petersf1827cf2003-09-07 03:30:18 +00001727 uninitialized bytes followed by the remainder of the line. Don't be
1728 horrified by the recursive call: maximum recursion depth is limited by
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001729 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1730
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001731static PyStringObject *
1732readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1733{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001734 PyStringObject* s;
1735 char *bufptr;
1736 char *buf;
1737 int len;
1738
1739 if (f->f_buf == NULL)
Tim Petersf1827cf2003-09-07 03:30:18 +00001740 if (readahead(f, bufsize) < 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001741 return NULL;
1742
1743 len = f->f_bufend - f->f_bufptr;
Tim Petersf1827cf2003-09-07 03:30:18 +00001744 if (len == 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001745 return (PyStringObject *)
1746 PyString_FromStringAndSize(NULL, skip);
1747 bufptr = memchr(f->f_bufptr, '\n', len);
1748 if (bufptr != NULL) {
1749 bufptr++; /* Count the '\n' */
1750 len = bufptr - f->f_bufptr;
1751 s = (PyStringObject *)
1752 PyString_FromStringAndSize(NULL, skip+len);
Tim Petersf1827cf2003-09-07 03:30:18 +00001753 if (s == NULL)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001754 return NULL;
1755 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1756 f->f_bufptr = bufptr;
1757 if (bufptr == f->f_bufend)
1758 drop_readahead(f);
1759 } else {
1760 bufptr = f->f_bufptr;
1761 buf = f->f_buf;
1762 f->f_buf = NULL; /* Force new readahead buffer */
1763 s = readahead_get_line_skip(
1764 f, skip+len, bufsize + (bufsize>>2) );
1765 if (s == NULL) {
1766 PyMem_Free(buf);
1767 return NULL;
1768 }
1769 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1770 PyMem_Free(buf);
1771 }
1772 return s;
1773}
1774
1775/* A larger buffer size may actually decrease performance. */
1776#define READAHEAD_BUFSIZE 8192
1777
1778static PyObject *
1779file_iternext(PyFileObject *f)
1780{
1781 PyStringObject* l;
1782
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001783 if (f->f_fp == NULL)
1784 return err_closed();
1785
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001786 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1787 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1788 Py_XDECREF(l);
1789 return NULL;
1790 }
1791 return (PyObject *)l;
1792}
1793
1794
Tim Peters59c9a642001-09-13 05:38:56 +00001795static PyObject *
1796file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1797{
Tim Peters44410012001-09-14 03:26:08 +00001798 PyObject *self;
1799 static PyObject *not_yet_string;
1800
1801 assert(type != NULL && type->tp_alloc != NULL);
1802
1803 if (not_yet_string == NULL) {
1804 not_yet_string = PyString_FromString("<uninitialized file>");
1805 if (not_yet_string == NULL)
1806 return NULL;
1807 }
1808
1809 self = type->tp_alloc(type, 0);
1810 if (self != NULL) {
1811 /* Always fill in the name and mode, so that nobody else
1812 needs to special-case NULLs there. */
1813 Py_INCREF(not_yet_string);
1814 ((PyFileObject *)self)->f_name = not_yet_string;
1815 Py_INCREF(not_yet_string);
1816 ((PyFileObject *)self)->f_mode = not_yet_string;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001817 Py_INCREF(Py_None);
1818 ((PyFileObject *)self)->f_encoding = Py_None;
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001819 ((PyFileObject *)self)->weakreflist = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001820 }
1821 return self;
1822}
1823
1824static int
1825file_init(PyObject *self, PyObject *args, PyObject *kwds)
1826{
1827 PyFileObject *foself = (PyFileObject *)self;
1828 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001829 static char *kwlist[] = {"name", "mode", "buffering", 0};
1830 char *name = NULL;
1831 char *mode = "r";
1832 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001833 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001834
1835 assert(PyFile_Check(self));
1836 if (foself->f_fp != NULL) {
1837 /* Have to close the existing file first. */
1838 PyObject *closeresult = file_close(foself);
1839 if (closeresult == NULL)
1840 return -1;
1841 Py_DECREF(closeresult);
1842 }
Tim Peters59c9a642001-09-13 05:38:56 +00001843
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001844#ifdef Py_WIN_WIDE_FILENAMES
1845 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1846 PyObject *po;
1847 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1848 kwlist, &po, &mode, &bufsize)) {
1849 wideargument = 1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001850 if (fill_file_fields(foself, NULL, po, mode,
1851 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001852 goto Error;
1853 } else {
1854 /* Drop the argument parsing error as narrow
1855 strings are also valid. */
1856 PyErr_Clear();
1857 }
1858 }
1859#endif
1860
1861 if (!wideargument) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001862 PyObject *o_name;
1863
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001864 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1865 Py_FileSystemDefaultEncoding,
1866 &name,
1867 &mode, &bufsize))
1868 return -1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00001869
1870 /* We parse again to get the name as a PyObject */
1871 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", kwlist,
1872 &o_name, &mode, &bufsize))
1873 return -1;
1874
1875 if (fill_file_fields(foself, NULL, o_name, mode,
1876 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001877 goto Error;
1878 }
Tim Peters44410012001-09-14 03:26:08 +00001879 if (open_the_file(foself, name, mode) == NULL)
1880 goto Error;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +00001881 foself->f_setbuf = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001882 PyFile_SetBufSize(self, bufsize);
1883 goto Done;
1884
1885Error:
1886 ret = -1;
1887 /* fall through */
1888Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001889 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001890 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001891}
1892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001893PyDoc_VAR(file_doc) =
1894PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001895"file(name[, mode[, buffering]]) -> file object\n"
1896"\n"
1897"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1898"writing or appending. The file will be created if it doesn't exist\n"
1899"when opened for writing or appending; it will be truncated when\n"
1900"opened for writing. Add a 'b' to the mode for binary files.\n"
1901"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1902"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001903"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001906"Add a 'U' to mode to open the file for input with universal newline\n"
1907"support. Any line ending in the input file will be seen as a '\\n'\n"
1908"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1909"the value for this attribute is one of None (no newline read yet),\n"
1910"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1911"\n"
1912"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001913)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001915"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916"Note: open() is an alias for file()."
1917);
Tim Peters59c9a642001-09-13 05:38:56 +00001918
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001919PyTypeObject PyFile_Type = {
1920 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001921 0,
1922 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001923 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001924 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001925 (destructor)file_dealloc, /* tp_dealloc */
1926 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001927 0, /* tp_getattr */
1928 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001929 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001930 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001931 0, /* tp_as_number */
1932 0, /* tp_as_sequence */
1933 0, /* tp_as_mapping */
1934 0, /* tp_hash */
1935 0, /* tp_call */
1936 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001937 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00001938 /* softspace is writable: we must supply tp_setattro */
1939 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001940 0, /* tp_as_buffer */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001941 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001942 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001943 0, /* tp_traverse */
1944 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001945 0, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001946 offsetof(PyFileObject, weakreflist), /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001947 (getiterfunc)file_getiter, /* tp_iter */
1948 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001949 file_methods, /* tp_methods */
1950 file_memberlist, /* tp_members */
1951 file_getsetlist, /* tp_getset */
1952 0, /* tp_base */
1953 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001954 0, /* tp_descr_get */
1955 0, /* tp_descr_set */
1956 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001957 (initproc)file_init, /* tp_init */
1958 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001959 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001960 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001961};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001962
1963/* Interface for the 'soft space' between print items. */
1964
1965int
Fred Drakefd99de62000-07-09 05:02:18 +00001966PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001967{
1968 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001969 if (f == NULL) {
1970 /* Do nothing */
1971 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001972 else if (PyFile_Check(f)) {
1973 oldflag = ((PyFileObject *)f)->f_softspace;
1974 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001975 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001976 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001977 PyObject *v;
1978 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001979 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001980 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001981 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001982 if (PyInt_Check(v))
1983 oldflag = PyInt_AsLong(v);
1984 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001985 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001986 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001987 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001989 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001990 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1991 PyErr_Clear();
1992 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001993 }
1994 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00001995 return oldflag;
1996}
Guido van Rossum3165fe61992-09-25 21:59:05 +00001997
1998/* Interfaces to write objects/strings to file-like objects */
1999
2000int
Fred Drakefd99de62000-07-09 05:02:18 +00002001PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002002{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002003 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002004 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002006 return -1;
2007 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002008 else if (PyFile_Check(f)) {
2009 FILE *fp = PyFile_AsFile(f);
Fred Drake086a0f72004-03-19 15:22:36 +00002010#ifdef Py_USING_UNICODE
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002011 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2012 int result;
Fred Drake086a0f72004-03-19 15:22:36 +00002013#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002014 if (fp == NULL) {
2015 err_closed();
2016 return -1;
2017 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002018#ifdef Py_USING_UNICODE
Tim Petersf1827cf2003-09-07 03:30:18 +00002019 if ((flags & Py_PRINT_RAW) &&
Martin v. Löwis415da6e2003-05-18 12:56:25 +00002020 PyUnicode_Check(v) && enc != Py_None) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002021 char *cenc = PyString_AS_STRING(enc);
2022 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2023 if (value == NULL)
2024 return -1;
2025 } else {
2026 value = v;
2027 Py_INCREF(value);
2028 }
2029 result = PyObject_Print(value, fp, flags);
2030 Py_DECREF(value);
2031 return result;
2032#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002033 return PyObject_Print(v, fp, flags);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002034#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002035 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002036 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002037 if (writer == NULL)
2038 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002039 if (flags & Py_PRINT_RAW) {
2040 if (PyUnicode_Check(v)) {
2041 value = v;
2042 Py_INCREF(value);
2043 } else
2044 value = PyObject_Str(v);
2045 }
2046 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002048 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002049 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002050 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002051 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002052 args = PyTuple_Pack(1, value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002053 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002054 Py_DECREF(value);
2055 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002056 return -1;
2057 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002058 result = PyEval_CallObject(writer, args);
2059 Py_DECREF(args);
2060 Py_DECREF(value);
2061 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002062 if (result == NULL)
2063 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002064 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002065 return 0;
2066}
2067
Guido van Rossum27a60b11997-05-22 22:25:11 +00002068int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002069PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002070{
2071 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002072 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002073 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002074 PyErr_SetString(PyExc_SystemError,
2075 "null file for PyFile_WriteString");
2076 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002077 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002078 else if (PyFile_Check(f)) {
2079 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002080 if (fp == NULL) {
2081 err_closed();
2082 return -1;
2083 }
2084 fputs(s, fp);
2085 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002086 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002087 else if (!PyErr_Occurred()) {
2088 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002089 int err;
2090 if (v == NULL)
2091 return -1;
2092 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2093 Py_DECREF(v);
2094 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002095 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002096 else
2097 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002098}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002099
2100/* Try to get a file-descriptor from a Python object. If the object
2101 is an integer or long integer, its value is returned. If not, the
2102 object's fileno() method is called if it exists; the method must return
2103 an integer or long integer, which is returned as the file descriptor value.
2104 -1 is returned on failure.
2105*/
2106
2107int PyObject_AsFileDescriptor(PyObject *o)
2108{
2109 int fd;
2110 PyObject *meth;
2111
2112 if (PyInt_Check(o)) {
2113 fd = PyInt_AsLong(o);
2114 }
2115 else if (PyLong_Check(o)) {
2116 fd = PyLong_AsLong(o);
2117 }
2118 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2119 {
2120 PyObject *fno = PyEval_CallObject(meth, NULL);
2121 Py_DECREF(meth);
2122 if (fno == NULL)
2123 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002124
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002125 if (PyInt_Check(fno)) {
2126 fd = PyInt_AsLong(fno);
2127 Py_DECREF(fno);
2128 }
2129 else if (PyLong_Check(fno)) {
2130 fd = PyLong_AsLong(fno);
2131 Py_DECREF(fno);
2132 }
2133 else {
2134 PyErr_SetString(PyExc_TypeError,
2135 "fileno() returned a non-integer");
2136 Py_DECREF(fno);
2137 return -1;
2138 }
2139 }
2140 else {
2141 PyErr_SetString(PyExc_TypeError,
2142 "argument must be an int, or have a fileno() method.");
2143 return -1;
2144 }
2145
2146 if (fd < 0) {
2147 PyErr_Format(PyExc_ValueError,
2148 "file descriptor cannot be a negative integer (%i)",
2149 fd);
2150 return -1;
2151 }
2152 return fd;
2153}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002154
Jack Jansen7b8c7542002-04-14 20:12:41 +00002155/* From here on we need access to the real fgets and fread */
2156#undef fgets
2157#undef fread
2158
2159/*
2160** Py_UniversalNewlineFgets is an fgets variation that understands
2161** all of \r, \n and \r\n conventions.
2162** The stream should be opened in binary mode.
2163** If fobj is NULL the routine always does newline conversion, and
2164** it may peek one char ahead to gobble the second char in \r\n.
2165** If fobj is non-NULL it must be a PyFileObject. In this case there
2166** is no readahead but in stead a flag is used to skip a following
2167** \n on the next read. Also, if the file is open in binary mode
2168** the whole conversion is skipped. Finally, the routine keeps track of
2169** the different types of newlines seen.
2170** Note that we need no error handling: fgets() treats error and eof
2171** identically.
2172*/
2173char *
2174Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2175{
2176 char *p = buf;
2177 int c;
2178 int newlinetypes = 0;
2179 int skipnextlf = 0;
2180 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002181
Jack Jansen7b8c7542002-04-14 20:12:41 +00002182 if (fobj) {
2183 if (!PyFile_Check(fobj)) {
2184 errno = ENXIO; /* What can you do... */
2185 return NULL;
2186 }
2187 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2188 if ( !univ_newline )
2189 return fgets(buf, n, stream);
2190 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2191 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2192 }
2193 FLOCKFILE(stream);
2194 c = 'x'; /* Shut up gcc warning */
2195 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2196 if (skipnextlf ) {
2197 skipnextlf = 0;
2198 if (c == '\n') {
2199 /* Seeing a \n here with skipnextlf true
2200 ** means we saw a \r before.
2201 */
2202 newlinetypes |= NEWLINE_CRLF;
2203 c = GETC(stream);
2204 if (c == EOF) break;
2205 } else {
2206 /*
2207 ** Note that c == EOF also brings us here,
2208 ** so we're okay if the last char in the file
2209 ** is a CR.
2210 */
2211 newlinetypes |= NEWLINE_CR;
2212 }
2213 }
2214 if (c == '\r') {
2215 /* A \r is translated into a \n, and we skip
2216 ** an adjacent \n, if any. We don't set the
2217 ** newlinetypes flag until we've seen the next char.
2218 */
2219 skipnextlf = 1;
2220 c = '\n';
2221 } else if ( c == '\n') {
2222 newlinetypes |= NEWLINE_LF;
2223 }
2224 *p++ = c;
2225 if (c == '\n') break;
2226 }
2227 if ( c == EOF && skipnextlf )
2228 newlinetypes |= NEWLINE_CR;
2229 FUNLOCKFILE(stream);
2230 *p = '\0';
2231 if (fobj) {
2232 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2233 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2234 } else if ( skipnextlf ) {
2235 /* If we have no file object we cannot save the
2236 ** skipnextlf flag. We have to readahead, which
2237 ** will cause a pause if we're reading from an
2238 ** interactive stream, but that is very unlikely
2239 ** unless we're doing something silly like
2240 ** execfile("/dev/tty").
2241 */
2242 c = GETC(stream);
2243 if ( c != '\n' )
2244 ungetc(c, stream);
2245 }
2246 if (p == buf)
2247 return NULL;
2248 return buf;
2249}
2250
2251/*
2252** Py_UniversalNewlineFread is an fread variation that understands
2253** all of \r, \n and \r\n conventions.
2254** The stream should be opened in binary mode.
2255** fobj must be a PyFileObject. In this case there
2256** is no readahead but in stead a flag is used to skip a following
2257** \n on the next read. Also, if the file is open in binary mode
2258** the whole conversion is skipped. Finally, the routine keeps track of
2259** the different types of newlines seen.
2260*/
2261size_t
Tim Peters058b1412002-04-21 07:29:14 +00002262Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002263 FILE *stream, PyObject *fobj)
2264{
Tim Peters058b1412002-04-21 07:29:14 +00002265 char *dst = buf;
2266 PyFileObject *f = (PyFileObject *)fobj;
2267 int newlinetypes, skipnextlf;
2268
2269 assert(buf != NULL);
2270 assert(stream != NULL);
2271
Jack Jansen7b8c7542002-04-14 20:12:41 +00002272 if (!fobj || !PyFile_Check(fobj)) {
2273 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002274 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002275 }
Tim Peters058b1412002-04-21 07:29:14 +00002276 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002277 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002278 newlinetypes = f->f_newlinetypes;
2279 skipnextlf = f->f_skipnextlf;
2280 /* Invariant: n is the number of bytes remaining to be filled
2281 * in the buffer.
2282 */
2283 while (n) {
2284 size_t nread;
2285 int shortread;
2286 char *src = dst;
2287
2288 nread = fread(dst, 1, n, stream);
2289 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002290 if (nread == 0)
2291 break;
2292
Tim Peterse1682a82002-04-21 18:15:20 +00002293 n -= nread; /* assuming 1 byte out for each in; will adjust */
2294 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002295 while (nread--) {
2296 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002297 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002298 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002299 *dst++ = '\n';
2300 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002301 }
2302 else if (skipnextlf && c == '\n') {
2303 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002304 skipnextlf = 0;
2305 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002306 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002307 }
2308 else {
2309 /* Normal char to be stored in buffer. Also
2310 * update the newlinetypes flag if either this
2311 * is an LF or the previous char was a CR.
2312 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002313 if (c == '\n')
2314 newlinetypes |= NEWLINE_LF;
2315 else if (skipnextlf)
2316 newlinetypes |= NEWLINE_CR;
2317 *dst++ = c;
2318 skipnextlf = 0;
2319 }
2320 }
Tim Peters058b1412002-04-21 07:29:14 +00002321 if (shortread) {
2322 /* If this is EOF, update type flags. */
2323 if (skipnextlf && feof(stream))
2324 newlinetypes |= NEWLINE_CR;
2325 break;
2326 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002327 }
Tim Peters058b1412002-04-21 07:29:14 +00002328 f->f_newlinetypes = newlinetypes;
2329 f->f_skipnextlf = skipnextlf;
2330 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002331}