blob: 92cfa5b35a94d0185e1f2d7cb44d26b95f59ce82 [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
Guido van Rossumf2044e11998-04-28 16:05:59 +000024#ifdef macintosh
25#ifdef USE_GUSI
26#define HAVE_FTRUNCATE
27#endif
28#endif
29
Jack Jansene08dea191995-04-23 22:12:47 +000030#ifdef __MWERKS__
31/* Mwerks fopen() doesn't always set errno */
32#define NO_FOPEN_ERRNO
33#endif
Guido van Rossum295d1711995-02-19 15:55:19 +000034
Andrew MacIntyrec4874392002-02-26 11:36:35 +000035#if defined(PYOS_OS2) && defined(PYCC_GCC)
36#include <io.h>
37#endif
38
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Guido van Rossumce5ba841991-03-06 13:06:18 +000040
Guido van Rossumff7e83d1999-08-27 20:39:37 +000041#ifndef DONT_HAVE_ERRNO_H
Guido van Rossumf1dc5661993-07-05 10:31:29 +000042#include <errno.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000043#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Jack Jansen7b8c7542002-04-14 20:12:41 +000045#ifdef HAVE_GETC_UNLOCKED
46#define GETC(f) getc_unlocked(f)
47#define FLOCKFILE(f) flockfile(f)
48#define FUNLOCKFILE(f) funlockfile(f)
49#else
50#define GETC(f) getc(f)
51#define FLOCKFILE(f)
52#define FUNLOCKFILE(f)
53#endif
54
55#ifdef WITH_UNIVERSAL_NEWLINES
56/* Bits in f_newlinetypes */
57#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
58#define NEWLINE_CR 1 /* \r newline seen */
59#define NEWLINE_LF 2 /* \n newline seen */
60#define NEWLINE_CRLF 4 /* \r\n newline seen */
61#endif
Trent Mickf29f47b2000-08-11 19:02:59 +000062
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063FILE *
Fred Drakefd99de62000-07-09 05:02:18 +000064PyFile_AsFile(PyObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 if (f == NULL || !PyFile_Check(f))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 return NULL;
Guido van Rossum3165fe61992-09-25 21:59:05 +000068 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069 return ((PyFileObject *)f)->f_fp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000073PyFile_Name(PyObject *f)
Guido van Rossumdb3165e1993-10-18 17:06:59 +000074{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 if (f == NULL || !PyFile_Check(f))
Guido van Rossumdb3165e1993-10-18 17:06:59 +000076 return NULL;
77 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 return ((PyFileObject *)f)->f_name;
Guido van Rossumdb3165e1993-10-18 17:06:59 +000079}
80
Neil Schemenauered19b882002-03-23 02:06:50 +000081/* On Unix, fopen will succeed for directories.
82 In Python, there should be no file objects referring to
83 directories, so we need a check. */
84
85static PyFileObject*
86dircheck(PyFileObject* f)
87{
88#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
89 struct stat buf;
90 if (f->f_fp == NULL)
91 return f;
92 if (fstat(fileno(f->f_fp), &buf) == 0 &&
93 S_ISDIR(buf.st_mode)) {
94#ifdef HAVE_STRERROR
95 char *msg = strerror(EISDIR);
96#else
97 char *msg = "Is a directory";
98#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +000099 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
100 EISDIR, msg);
Neil Schemenauered19b882002-03-23 02:06:50 +0000101 PyErr_SetObject(PyExc_IOError, exc);
102 return NULL;
103 }
104#endif
105 return f;
106}
107
Tim Peters59c9a642001-09-13 05:38:56 +0000108
109static PyObject *
110fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000111 int (*close)(FILE *), PyObject *wname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Tim Peters59c9a642001-09-13 05:38:56 +0000113 assert(f != NULL);
114 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +0000115 assert(f->f_fp == NULL);
116
117 Py_DECREF(f->f_name);
118 Py_DECREF(f->f_mode);
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000119#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000120 if (wname)
121 f->f_name = PyUnicode_FromObject(wname);
122 else
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000123#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000124 f->f_name = PyString_FromString(name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +0000126
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000127 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +0000128 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +0000129 f->f_binary = strchr(mode,'b') != NULL;
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000130 f->f_buf = NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000131#ifdef WITH_UNIVERSAL_NEWLINES
132 f->f_univ_newline = (strchr(mode, 'U') != NULL);
133 f->f_newlinetypes = NEWLINE_UNKNOWN;
134 f->f_skipnextlf = 0;
135#endif
Tim Peters44410012001-09-14 03:26:08 +0000136
Tim Peters59c9a642001-09-13 05:38:56 +0000137 if (f->f_name == NULL || f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 f->f_fp = fp;
Neil Schemenauered19b882002-03-23 02:06:50 +0000140 f = dircheck(f);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
143
Tim Peters59c9a642001-09-13 05:38:56 +0000144static PyObject *
145open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
Tim Peters59c9a642001-09-13 05:38:56 +0000147 assert(f != NULL);
148 assert(PyFile_Check(f));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000149#ifdef MS_WINDOWS
150 /* windows ignores the passed name in order to support Unicode */
151 assert(f->f_name != NULL);
152#else
Tim Peters59c9a642001-09-13 05:38:56 +0000153 assert(name != NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000154#endif
Tim Peters59c9a642001-09-13 05:38:56 +0000155 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +0000156 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000157
Tim Peters8fa45672001-09-13 21:01:29 +0000158 /* rexec.py can't stop a user from getting the file() constructor --
159 all they have to do is get *any* file object f, and then do
160 type(f). Here we prevent them from doing damage with it. */
161 if (PyEval_GetRestricted()) {
162 PyErr_SetString(PyExc_IOError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000163 "file() constructor not accessible in restricted mode");
Tim Peters8fa45672001-09-13 21:01:29 +0000164 return NULL;
165 }
Tim Petersa27a1502001-11-09 20:59:14 +0000166 errno = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000167#ifdef HAVE_FOPENRF
Guido van Rossuma08095a1991-02-13 23:25:27 +0000168 if (*mode == '*') {
169 FILE *fopenRF();
170 f->f_fp = fopenRF(name, mode+1);
171 }
172 else
173#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000174 {
Jack Jansen7b8c7542002-04-14 20:12:41 +0000175#ifdef WITH_UNIVERSAL_NEWLINES
176 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
177 mode = "rb";
178#else
179 /* Compatibility: specifying U in a Python without universal
180 ** newlines is allowed, and the file is opened as a normal text
181 ** file.
182 */
183 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
184 mode = "r";
185#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000186#ifdef MS_WINDOWS
187 if (PyUnicode_Check(f->f_name)) {
188 PyObject *wmode;
189 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
190 if (f->f_name && wmode) {
191 Py_BEGIN_ALLOW_THREADS
192 /* PyUnicode_AS_UNICODE OK without thread
193 lock as it is a simple dereference. */
194 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
195 PyUnicode_AS_UNICODE(wmode));
196 Py_END_ALLOW_THREADS
197 }
198 Py_XDECREF(wmode);
199 }
200#endif
201 if (NULL == f->f_fp && NULL != name) {
202 Py_BEGIN_ALLOW_THREADS
203 f->f_fp = fopen(name, mode);
204 Py_END_ALLOW_THREADS
205 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206 }
Guido van Rossuma08095a1991-02-13 23:25:27 +0000207 if (f->f_fp == NULL) {
Jack Jansene08dea191995-04-23 22:12:47 +0000208#ifdef NO_FOPEN_ERRNO
Jack Jansenb3be2162001-11-30 14:16:36 +0000209 /* Metroworks only, wich does not always sets errno */
Jeremy Hylton41c83212001-11-09 16:17:24 +0000210 if (errno == 0) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000211 PyObject *v;
212 v = Py_BuildValue("(is)", 0, "Cannot open file");
213 if (v != NULL) {
214 PyErr_SetObject(PyExc_IOError, v);
215 Py_DECREF(v);
216 }
Jack Jansene08dea191995-04-23 22:12:47 +0000217 return NULL;
218 }
219#endif
Tim Peters2ea91112002-04-08 04:13:12 +0000220#ifdef _MSC_VER
221 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
222 * across all Windows flavors. When it sets EINVAL varies
223 * across Windows flavors, the exact conditions aren't
224 * documented, and the answer lies in the OS's implementation
225 * of Win32's CreateFile function (whose source is secret).
226 * Seems the best we can do is map EINVAL to ENOENT.
227 */
228 if (errno == 0) /* bad mode string */
229 errno = EINVAL;
230 else if (errno == EINVAL) /* unknown, but not a mode string */
231 errno = ENOENT;
232#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000233 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000234 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000235 mode);
236 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000237#ifdef MS_WINDOWS
238 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
239#else
Jeremy Hylton41c83212001-11-09 16:17:24 +0000240 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000241#endif /* MS_WINDOWS */
Tim Peters59c9a642001-09-13 05:38:56 +0000242 f = NULL;
243 }
Tim Peters2ea91112002-04-08 04:13:12 +0000244 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000245 f = dircheck(f);
Tim Peters59c9a642001-09-13 05:38:56 +0000246 return (PyObject *)f;
247}
248
249PyObject *
250PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
251{
Tim Peters44410012001-09-14 03:26:08 +0000252 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
253 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000254 if (f != NULL) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000255 if (fill_file_fields(f, fp, name, mode, close, NULL) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000256 Py_DECREF(f);
257 f = NULL;
258 }
259 }
260 return (PyObject *) f;
261}
262
263PyObject *
264PyFile_FromString(char *name, char *mode)
265{
266 extern int fclose(FILE *);
267 PyFileObject *f;
268
269 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
270 if (f != NULL) {
271 if (open_the_file(f, name, mode) == NULL) {
272 Py_DECREF(f);
273 f = NULL;
274 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279void
Fred Drakefd99de62000-07-09 05:02:18 +0000280PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281{
282 if (bufsize >= 0) {
283#ifdef HAVE_SETVBUF
284 int type;
285 switch (bufsize) {
286 case 0:
287 type = _IONBF;
288 break;
289 case 1:
290 type = _IOLBF;
291 bufsize = BUFSIZ;
292 break;
293 default:
294 type = _IOFBF;
295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
297 type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000298#else /* !HAVE_SETVBUF */
299 if (bufsize <= 1)
300 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
301#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302 }
303}
304
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000306err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000307{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000309 return NULL;
310}
311
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000312static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000313
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314/* Methods */
315
316static void
Fred Drakefd99de62000-07-09 05:02:18 +0000317file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000319 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000321 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000322 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000323 }
Tim Peters44410012001-09-14 03:26:08 +0000324 Py_XDECREF(f->f_name);
325 Py_XDECREF(f->f_mode);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000326 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000327 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328}
329
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000331file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000333 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000334#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000335 PyObject *ret = NULL;
336 PyObject *name;
337 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
338 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
339 f->f_fp == NULL ? "closed" : "open",
340 PyString_AsString(name),
341 PyString_AsString(f->f_mode),
342 f);
343 Py_XDECREF(name);
344 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000345#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000346 } else {
347 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000348 f->f_fp == NULL ? "closed" : "open",
349 PyString_AsString(f->f_name),
350 PyString_AsString(f->f_mode),
351 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000352 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353}
354
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000356file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000358 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000360 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000363 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000365 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 f->f_fp = NULL;
367 }
Guido van Rossumfebd5511992-03-04 16:39:24 +0000368 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000370 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371 return PyInt_FromLong((long)sts);
372 Py_INCREF(Py_None);
373 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
Trent Mickf29f47b2000-08-11 19:02:59 +0000376
Guido van Rossumb8552162001-09-05 14:58:11 +0000377/* Our very own off_t-like type, 64-bit if possible */
378#if !defined(HAVE_LARGEFILE_SUPPORT)
379typedef off_t Py_off_t;
380#elif SIZEOF_OFF_T >= 8
381typedef off_t Py_off_t;
382#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000383typedef fpos_t Py_off_t;
384#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000385#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000386#endif
387
388
Trent Mickf29f47b2000-08-11 19:02:59 +0000389/* a portable fseek() function
390 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000391static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000392_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000393{
Guido van Rossumb8552162001-09-05 14:58:11 +0000394#if !defined(HAVE_LARGEFILE_SUPPORT)
395 return fseek(fp, offset, whence);
396#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000397 return fseeko(fp, offset, whence);
398#elif defined(HAVE_FSEEK64)
399 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000400#elif defined(__BEOS__)
401 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000402#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000403 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
404 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000405 fpos_t pos;
406 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000407 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000408#ifdef MS_WINDOWS
409 fflush(fp);
410 if (_lseeki64(fileno(fp), 0, 2) == -1)
411 return -1;
412#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000413 if (fseek(fp, 0, SEEK_END) != 0)
414 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000415#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000416 /* fall through */
417 case SEEK_CUR:
418 if (fgetpos(fp, &pos) != 0)
419 return -1;
420 offset += pos;
421 break;
422 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000423 }
424 return fsetpos(fp, &offset);
425#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000426#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000427#endif
428}
429
430
431/* a portable ftell() function
432 Return -1 on failure with errno set appropriately, current file
433 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000434static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000435_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000436{
Guido van Rossumb8552162001-09-05 14:58:11 +0000437#if !defined(HAVE_LARGEFILE_SUPPORT)
438 return ftell(fp);
439#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
440 return ftello(fp);
441#elif defined(HAVE_FTELL64)
442 return ftell64(fp);
443#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000444 fpos_t pos;
445 if (fgetpos(fp, &pos) != 0)
446 return -1;
447 return pos;
448#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000449#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000450#endif
451}
452
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000455file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000457 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000458 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000459 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000460 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000461
Guido van Rossumd7297e61992-07-06 14:19:26 +0000462 if (f->f_fp == NULL)
463 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000464 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000465 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000466 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000467 return NULL;
468#if !defined(HAVE_LARGEFILE_SUPPORT)
469 offset = PyInt_AsLong(offobj);
470#else
471 offset = PyLong_Check(offobj) ?
472 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
473#endif
474 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000475 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000478 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000479 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000481
Guido van Rossumff4949e1992-08-05 19:58:53 +0000482 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000484 clearerr(f->f_fp);
485 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000486 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000487#ifdef WITH_UNIVERSAL_NEWLINES
488 f->f_skipnextlf = 0;
489#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 Py_INCREF(Py_None);
491 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000492}
493
Trent Mickf29f47b2000-08-11 19:02:59 +0000494
Guido van Rossumd7047b31995-01-02 19:07:15 +0000495#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000497file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000498{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000499 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000500 Py_off_t newsize;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000501 PyObject *newsizeobj;
Tim Peters86821b22001-01-07 21:19:34 +0000502
Guido van Rossumd7047b31995-01-02 19:07:15 +0000503 if (f->f_fp == NULL)
504 return err_closed();
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000505 newsizeobj = NULL;
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000506 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000507 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000508
509 /* Set newsize to current postion if newsizeobj NULL, else to the
510 specified value. */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000511 if (newsizeobj != NULL) {
512#if !defined(HAVE_LARGEFILE_SUPPORT)
513 newsize = PyInt_AsLong(newsizeobj);
514#else
515 newsize = PyLong_Check(newsizeobj) ?
516 PyLong_AsLongLong(newsizeobj) :
517 PyInt_AsLong(newsizeobj);
518#endif
519 if (PyErr_Occurred())
520 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000521 }
522 else {
523 /* Default to current position. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000525 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000526 newsize = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000528 if (newsize == -1)
529 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000530 }
Tim Petersfb05db22002-03-11 00:24:00 +0000531
532 /* Flush the file. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000534 errno = 0;
535 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000537 if (ret != 0)
538 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000539
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000540#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000541 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000542 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000543 {
Tim Peters8f01b682002-03-12 03:04:44 +0000544 Py_off_t current; /* current file position */
Tim Petersfb05db22002-03-11 00:24:00 +0000545 HANDLE hFile;
546 int error;
547
Tim Peters8f01b682002-03-12 03:04:44 +0000548 /* current <- current file postion. */
549 if (newsizeobj == NULL)
550 current = newsize;
551 else {
Tim Petersfb05db22002-03-11 00:24:00 +0000552 Py_BEGIN_ALLOW_THREADS
553 errno = 0;
Tim Peters8f01b682002-03-12 03:04:44 +0000554 current = _portable_ftell(f->f_fp);
555 Py_END_ALLOW_THREADS
556 if (current == -1)
557 goto onioerror;
558 }
559
560 /* Move to newsize. */
561 if (current != newsize) {
562 Py_BEGIN_ALLOW_THREADS
563 errno = 0;
564 error = _portable_fseek(f->f_fp, newsize, SEEK_SET)
565 != 0;
Tim Petersfb05db22002-03-11 00:24:00 +0000566 Py_END_ALLOW_THREADS
567 if (error)
568 goto onioerror;
569 }
570
Tim Peters8f01b682002-03-12 03:04:44 +0000571 /* Truncate. Note that this may grow the file! */
572 Py_BEGIN_ALLOW_THREADS
573 errno = 0;
574 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
575 error = hFile == (HANDLE)-1;
576 if (!error) {
577 error = SetEndOfFile(hFile) == 0;
578 if (error)
579 errno = EACCES;
580 }
581 Py_END_ALLOW_THREADS
582 if (error)
583 goto onioerror;
584
585 /* Restore original file position. */
586 if (current != newsize) {
587 Py_BEGIN_ALLOW_THREADS
588 errno = 0;
589 error = _portable_fseek(f->f_fp, current, SEEK_SET)
590 != 0;
591 Py_END_ALLOW_THREADS
592 if (error)
593 goto onioerror;
594 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000595 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000596#else
597 Py_BEGIN_ALLOW_THREADS
598 errno = 0;
599 ret = ftruncate(fileno(f->f_fp), newsize);
600 Py_END_ALLOW_THREADS
601 if (ret != 0) goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000602#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000603
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 Py_INCREF(Py_None);
605 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000606
607onioerror:
608 PyErr_SetFromErrno(PyExc_IOError);
609 clearerr(f->f_fp);
610 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000611}
612#endif /* HAVE_FTRUNCATE */
613
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000615file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000616{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000617 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000618
Guido van Rossumd7297e61992-07-06 14:19:26 +0000619 if (f->f_fp == NULL)
620 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000622 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000623 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000625 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000627 clearerr(f->f_fp);
628 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000629 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000630#ifdef WITH_UNIVERSAL_NEWLINES
631 if (f->f_skipnextlf) {
632 int c;
633 c = GETC(f->f_fp);
634 if (c == '\n') {
635 pos++;
636 f->f_skipnextlf = 0;
637 } else if (c != EOF) ungetc(c, f->f_fp);
638 }
639#endif
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000640#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000641 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000642#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000643 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000644#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000648file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000649{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000650 if (f->f_fp == NULL)
651 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000653}
654
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000656file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000657{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000658 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000659
Guido van Rossumd7297e61992-07-06 14:19:26 +0000660 if (f->f_fp == NULL)
661 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000663 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000664 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000666 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000668 clearerr(f->f_fp);
669 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000670 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_INCREF(Py_None);
672 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000673}
674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000676file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000677{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000678 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000679 if (f->f_fp == NULL)
680 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000682 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000684 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000685}
686
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000687
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000688#if BUFSIZ < 8192
689#define SMALLCHUNK 8192
690#else
691#define SMALLCHUNK BUFSIZ
692#endif
693
Guido van Rossum3c259041999-01-14 19:00:14 +0000694#if SIZEOF_INT < 4
695#define BIGCHUNK (512 * 32)
696#else
697#define BIGCHUNK (512 * 1024)
698#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000699
700static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000701new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000702{
703#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000704 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000705 struct stat st;
706 if (fstat(fileno(f->f_fp), &st) == 0) {
707 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000708 /* The following is not a bug: we really need to call lseek()
709 *and* ftell(). The reason is that some stdio libraries
710 mistakenly flush their buffer when ftell() is called and
711 the lseek() call it makes fails, thereby throwing away
712 data that cannot be recovered in any way. To avoid this,
713 we first test lseek(), and only call ftell() if lseek()
714 works. We can't use the lseek() value either, because we
715 need to take the amount of buffered data into account.
716 (Yet another reason why stdio stinks. :-) */
Jack Jansen2771b5b2001-10-10 22:03:27 +0000717#ifdef USE_GUSI2
718 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
719 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
720#else
Guido van Rossum91aaa921998-05-05 22:21:35 +0000721 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000722#endif
723 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000724 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000725 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000726 if (pos < 0)
727 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000728 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000729 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000730 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000731 }
732#endif
733 if (currentsize > SMALLCHUNK) {
734 /* Keep doubling until we reach BIGCHUNK;
735 then keep adding BIGCHUNK. */
736 if (currentsize <= BIGCHUNK)
737 return currentsize + currentsize;
738 else
739 return currentsize + BIGCHUNK;
740 }
741 return currentsize + SMALLCHUNK;
742}
743
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000744#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
745#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
746#else
747#ifdef EWOULDBLOCK
748#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
749#else
750#ifdef EAGAIN
751#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
752#else
753#define BLOCKED_ERRNO(x) 0
754#endif
755#endif
756#endif
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000759file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000760{
Guido van Rossum789a1611997-05-10 22:33:55 +0000761 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000762 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000764
Guido van Rossumd7297e61992-07-06 14:19:26 +0000765 if (f->f_fp == NULL)
766 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000767 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000768 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000769 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000770 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000771 else
772 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000773 if (buffersize > INT_MAX) {
774 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000775 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000776 return NULL;
777 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000778 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000779 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000781 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000782 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000783 Py_BEGIN_ALLOW_THREADS
784 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000785 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000786 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000787 Py_END_ALLOW_THREADS
788 if (chunksize == 0) {
789 if (!ferror(f->f_fp))
790 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000791 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000792 /* When in non-blocking mode, data shouldn't
793 * be discarded if a blocking signal was
794 * received. That will also happen if
795 * chunksize != 0, but bytesread < buffersize. */
796 if (bytesread > 0 && BLOCKED_ERRNO(errno))
797 break;
798 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000799 Py_DECREF(v);
800 return NULL;
801 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000802 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000803 if (bytesread < buffersize) {
804 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000805 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000806 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000807 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000808 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000809 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000810 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000811 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000812 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000813 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000814 }
815 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000816 if (bytesread != buffersize)
817 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return v;
819}
820
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000821static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000822file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000823{
824 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000825 int ntodo;
826 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000827
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000828 if (f->f_fp == NULL)
829 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000830 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000831 return NULL;
832 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000833 while (ntodo > 0) {
834 Py_BEGIN_ALLOW_THREADS
835 errno = 0;
Jeremy Hylton8b735422002-08-14 21:01:41 +0000836 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
837 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000838 Py_END_ALLOW_THREADS
839 if (nnow == 0) {
840 if (!ferror(f->f_fp))
841 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000842 PyErr_SetFromErrno(PyExc_IOError);
843 clearerr(f->f_fp);
844 return NULL;
845 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000846 ndone += nnow;
847 ntodo -= nnow;
848 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000849 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000850}
851
Tim Peters86821b22001-01-07 21:19:34 +0000852/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000853Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000854
855Under MSVC 6:
856
Tim Peters1c733232001-01-08 04:02:07 +0000857+ MS threadsafe getc is very slow (multiple layers of function calls before+
858 after each character, to lock+unlock the stream).
859+ The stream-locking functions are MS-internal -- can't access them from user
860 code.
861+ There's nothing Tim could find in the MS C or platform SDK libraries that
862 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000863+ MS fgets locks/unlocks only once per line; it's the only hook we have.
864
865So we use fgets for speed(!), despite that it's painful.
866
867MS realloc is also slow.
868
Tim Petersf29b64d2001-01-15 06:33:19 +0000869Reports from other platforms on this method vs getc_unlocked (which MS doesn't
870have):
871 Linux a wash
872 Solaris a wash
873 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000874
Tim Petersf29b64d2001-01-15 06:33:19 +0000875CAUTION: The C std isn't clear about this: in those cases where fgets
876writes something into the buffer, can it write into any position beyond the
877required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
878known on which it does; and it would be a strange way to code fgets. Still,
879getline_via_fgets may not work correctly if it does. The std test
880test_bufio.py should fail if platform fgets() routinely writes beyond the
881trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000882**************************************************************************/
883
Tim Petersf29b64d2001-01-15 06:33:19 +0000884/* Use this routine if told to, or by default on non-get_unlocked()
885 * platforms unless told not to. Yikes! Let's spell that out:
886 * On a platform with getc_unlocked():
887 * By default, use getc_unlocked().
888 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
889 * On a platform without getc_unlocked():
890 * By default, use fgets().
891 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
892 */
893#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
894#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000895#endif
896
Tim Petersf29b64d2001-01-15 06:33:19 +0000897#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
898#undef USE_FGETS_IN_GETLINE
899#endif
900
901#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000902static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000903getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000904{
Tim Peters15b83852001-01-08 00:53:12 +0000905/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000906 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
907 * to fill this much of the buffer with a known value in order to figure out
908 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
909 * than "most" lines, we waste time filling unused buffer slots. 100 is
910 * surely adequate for most peoples' email archives, chewing over source code,
911 * etc -- "regular old text files".
912 * MAXBUFSIZE is the maximum line length that lets us get away with the less
913 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
914 * cautions about boosting that. 300 was chosen because the worst real-life
915 * text-crunching job reported on Python-Dev was a mail-log crawler where over
916 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000917 */
Tim Peters142297a2001-01-15 10:36:56 +0000918#define INITBUFSIZE 100
919#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000920 char* p; /* temp */
921 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000922 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000923 char* pvfree; /* address of next free slot */
924 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000925 size_t nfree; /* # of free buffer slots; pvend-pvfree */
926 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000927 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000928
Tim Peters15b83852001-01-08 00:53:12 +0000929 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000930 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000931 */
Tim Peters142297a2001-01-15 10:36:56 +0000932 total_v_size = INITBUFSIZE; /* start small and pray */
933 pvfree = buf;
934 for (;;) {
935 Py_BEGIN_ALLOW_THREADS
936 pvend = buf + total_v_size;
937 nfree = pvend - pvfree;
938 memset(pvfree, '\n', nfree);
939 p = fgets(pvfree, nfree, fp);
940 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000941
Tim Peters142297a2001-01-15 10:36:56 +0000942 if (p == NULL) {
943 clearerr(fp);
944 if (PyErr_CheckSignals())
945 return NULL;
946 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000947 return v;
948 }
Tim Peters142297a2001-01-15 10:36:56 +0000949 /* fgets read *something* */
950 p = memchr(pvfree, '\n', nfree);
951 if (p != NULL) {
952 /* Did the \n come from fgets or from us?
953 * Since fgets stops at the first \n, and then writes
954 * \0, if it's from fgets a \0 must be next. But if
955 * that's so, it could not have come from us, since
956 * the \n's we filled the buffer with have only more
957 * \n's to the right.
958 */
959 if (p+1 < pvend && *(p+1) == '\0') {
960 /* It's from fgets: we win! In particular,
961 * we haven't done any mallocs yet, and can
962 * build the final result on the first try.
963 */
964 ++p; /* include \n from fgets */
965 }
966 else {
967 /* Must be from us: fgets didn't fill the
968 * buffer and didn't find a newline, so it
969 * must be the last and newline-free line of
970 * the file.
971 */
972 assert(p > pvfree && *(p-1) == '\0');
973 --p; /* don't include \0 from fgets */
974 }
975 v = PyString_FromStringAndSize(buf, p - buf);
976 return v;
977 }
978 /* yuck: fgets overwrote all the newlines, i.e. the entire
979 * buffer. So this line isn't over yet, or maybe it is but
980 * we're exactly at EOF. If we haven't already, try using the
981 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000982 */
Tim Peters142297a2001-01-15 10:36:56 +0000983 assert(*(pvend-1) == '\0');
984 if (pvfree == buf) {
985 pvfree = pvend - 1; /* overwrite trailing null */
986 total_v_size = MAXBUFSIZE;
987 }
988 else
989 break;
Tim Peters86821b22001-01-07 21:19:34 +0000990 }
Tim Peters142297a2001-01-15 10:36:56 +0000991
992 /* The stack buffer isn't big enough; malloc a string object and read
993 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000994 */
Tim Petersddea2082002-03-23 10:03:50 +0000995 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +0000996 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000997 if (v == NULL)
998 return v;
999 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +00001000 memcpy(BUF(v), buf, MAXBUFSIZE-1);
1001 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +00001002
1003 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +00001004 * after setting p one beyond the end of the line. The code here is
1005 * very much like the code above, except reads into v's buffer; see
1006 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +00001007 */
1008 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +00001009 Py_BEGIN_ALLOW_THREADS
1010 pvend = BUF(v) + total_v_size;
1011 nfree = pvend - pvfree;
1012 memset(pvfree, '\n', nfree);
1013 p = fgets(pvfree, nfree, fp);
1014 Py_END_ALLOW_THREADS
1015
1016 if (p == NULL) {
1017 clearerr(fp);
1018 if (PyErr_CheckSignals()) {
1019 Py_DECREF(v);
1020 return NULL;
1021 }
1022 p = pvfree;
1023 break;
1024 }
Tim Peters86821b22001-01-07 21:19:34 +00001025 p = memchr(pvfree, '\n', nfree);
1026 if (p != NULL) {
1027 if (p+1 < pvend && *(p+1) == '\0') {
1028 /* \n came from fgets */
1029 ++p;
1030 break;
1031 }
1032 /* \n came from us; last line of file, no newline */
1033 assert(p > pvfree && *(p-1) == '\0');
1034 --p;
1035 break;
1036 }
1037 /* expand buffer and try again */
1038 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001039 increment = total_v_size >> 2; /* mild exponential growth */
1040 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001041 if (total_v_size > INT_MAX) {
1042 PyErr_SetString(PyExc_OverflowError,
1043 "line is longer than a Python string can hold");
1044 Py_DECREF(v);
1045 return NULL;
1046 }
1047 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1048 return NULL;
1049 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001050 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001051 }
1052 if (BUF(v) + total_v_size != p)
1053 _PyString_Resize(&v, p - BUF(v));
1054 return v;
1055#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001056#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001057}
Tim Petersf29b64d2001-01-15 06:33:19 +00001058#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001059
Guido van Rossum0bd24411991-04-04 15:21:57 +00001060/* Internal routine to get a line.
1061 Size argument interpretation:
1062 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001063 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001064*/
1065
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001066static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001067get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001069 FILE *fp = f->f_fp;
1070 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001071 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001072 size_t total_v_size; /* total # of slots in buffer */
1073 size_t used_v_size; /* # used slots in buffer */
1074 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001076#ifdef WITH_UNIVERSAL_NEWLINES
1077 int newlinetypes = f->f_newlinetypes;
1078 int skipnextlf = f->f_skipnextlf;
1079 int univ_newline = f->f_univ_newline;
1080#endif
Guido van Rossum0bd24411991-04-04 15:21:57 +00001081
Jack Jansen7b8c7542002-04-14 20:12:41 +00001082#if defined(USE_FGETS_IN_GETLINE)
1083#ifdef WITH_UNIVERSAL_NEWLINES
1084 if (n <= 0 && !univ_newline )
1085#else
Guido van Rossum86282062001-01-08 01:26:47 +00001086 if (n <= 0)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001087#endif
Tim Petersf29b64d2001-01-15 06:33:19 +00001088 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001089#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001090 total_v_size = n > 0 ? n : 100;
1091 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001092 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001094 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001095 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001096
Guido van Rossumce5ba841991-03-06 13:06:18 +00001097 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001098 Py_BEGIN_ALLOW_THREADS
1099 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001100#ifdef WITH_UNIVERSAL_NEWLINES
1101 if (univ_newline) {
1102 c = 'x'; /* Shut up gcc warning */
1103 while ( buf != end && (c = GETC(fp)) != EOF ) {
1104 if (skipnextlf ) {
1105 skipnextlf = 0;
1106 if (c == '\n') {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001107 /* Seeing a \n here with
1108 * skipnextlf true means we
1109 * saw a \r before.
1110 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001111 newlinetypes |= NEWLINE_CRLF;
1112 c = GETC(fp);
1113 if (c == EOF) break;
1114 } else {
1115 newlinetypes |= NEWLINE_CR;
1116 }
1117 }
1118 if (c == '\r') {
1119 skipnextlf = 1;
1120 c = '\n';
1121 } else if ( c == '\n')
1122 newlinetypes |= NEWLINE_LF;
1123 *buf++ = c;
1124 if (c == '\n') break;
1125 }
1126 if ( c == EOF && skipnextlf )
1127 newlinetypes |= NEWLINE_CR;
1128 } else /* If not universal newlines use the normal loop */
1129#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001130 while ((c = GETC(fp)) != EOF &&
1131 (*buf++ = c) != '\n' &&
1132 buf != end)
1133 ;
1134 FUNLOCKFILE(fp);
1135 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001136#ifdef WITH_UNIVERSAL_NEWLINES
1137 f->f_newlinetypes = newlinetypes;
1138 f->f_skipnextlf = skipnextlf;
1139#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001140 if (c == '\n')
1141 break;
1142 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001143 if (ferror(fp)) {
1144 PyErr_SetFromErrno(PyExc_IOError);
1145 clearerr(fp);
1146 Py_DECREF(v);
1147 return NULL;
1148 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001149 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001152 return NULL;
1153 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001154 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001155 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001156 /* Must be because buf == end */
1157 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001158 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001159 used_v_size = total_v_size;
1160 increment = total_v_size >> 2; /* mild exponential growth */
1161 total_v_size += increment;
1162 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001163 PyErr_SetString(PyExc_OverflowError,
1164 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001165 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001166 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001167 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001168 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001169 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001170 buf = BUF(v) + used_v_size;
1171 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001173
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001174 used_v_size = buf - BUF(v);
1175 if (used_v_size != total_v_size)
1176 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177 return v;
1178}
1179
Guido van Rossum0bd24411991-04-04 15:21:57 +00001180/* External C interface */
1181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001183PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001184{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001185 PyObject *result;
1186
Guido van Rossum3165fe61992-09-25 21:59:05 +00001187 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001189 return NULL;
1190 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001191
1192 if (PyFile_Check(f)) {
1193 if (((PyFileObject*)f)->f_fp == NULL)
1194 return err_closed();
1195 result = get_line((PyFileObject *)f, n);
1196 }
1197 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 PyObject *reader;
1199 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001200
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001202 if (reader == NULL)
1203 return NULL;
1204 if (n <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001205 args = Py_BuildValue("()");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001206 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001208 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001210 return NULL;
1211 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001212 result = PyEval_CallObject(reader, args);
1213 Py_DECREF(reader);
1214 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001215 if (result != NULL && !PyString_Check(result) &&
1216 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001218 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001219 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001220 "object.readline() returned non-string");
1221 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001222 }
1223
1224 if (n < 0 && result != NULL && PyString_Check(result)) {
1225 char *s = PyString_AS_STRING(result);
1226 int len = PyString_GET_SIZE(result);
1227 if (len == 0) {
1228 Py_DECREF(result);
1229 result = NULL;
1230 PyErr_SetString(PyExc_EOFError,
1231 "EOF when reading a line");
1232 }
1233 else if (s[len-1] == '\n') {
1234 if (result->ob_refcnt == 1)
1235 _PyString_Resize(&result, len-1);
1236 else {
1237 PyObject *v;
1238 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001240 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001241 }
1242 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001243 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001244#ifdef Py_USING_UNICODE
1245 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1246 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1247 int len = PyUnicode_GET_SIZE(result);
1248 if (len == 0) {
1249 Py_DECREF(result);
1250 result = NULL;
1251 PyErr_SetString(PyExc_EOFError,
1252 "EOF when reading a line");
1253 }
1254 else if (s[len-1] == '\n') {
1255 if (result->ob_refcnt == 1)
1256 PyUnicode_Resize(&result, len-1);
1257 else {
1258 PyObject *v;
1259 v = PyUnicode_FromUnicode(s, len-1);
1260 Py_DECREF(result);
1261 result = v;
1262 }
1263 }
1264 }
1265#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001266 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001267}
1268
1269/* Python method */
1270
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001272file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001273{
Guido van Rossum789a1611997-05-10 22:33:55 +00001274 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001275
Guido van Rossumd7297e61992-07-06 14:19:26 +00001276 if (f->f_fp == NULL)
1277 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001278 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001279 return NULL;
1280 if (n == 0)
1281 return PyString_FromString("");
1282 if (n < 0)
1283 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001284 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001285}
1286
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001287static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001288file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001289{
Guido van Rossum789a1611997-05-10 22:33:55 +00001290 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291 PyObject *list;
1292 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001293 char small_buffer[SMALLCHUNK];
1294 char *buffer = small_buffer;
1295 size_t buffersize = SMALLCHUNK;
1296 PyObject *big_buffer = NULL;
1297 size_t nfilled = 0;
1298 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001299 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001300 char *p, *q, *end;
1301 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001302 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001303
Guido van Rossumd7297e61992-07-06 14:19:26 +00001304 if (f->f_fp == NULL)
1305 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001306 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001307 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001309 return NULL;
1310 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001311 if (shortread)
1312 nread = 0;
1313 else {
1314 Py_BEGIN_ALLOW_THREADS
1315 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001316 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001317 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001318 Py_END_ALLOW_THREADS
1319 shortread = (nread < buffersize-nfilled);
1320 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001321 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001322 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001323 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001324 break;
1325 PyErr_SetFromErrno(PyExc_IOError);
1326 clearerr(f->f_fp);
1327 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001328 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001329 list = NULL;
1330 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001331 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001332 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001333 p = memchr(buffer+nfilled, '\n', nread);
1334 if (p == NULL) {
1335 /* Need a larger buffer to fit this line */
1336 nfilled += nread;
1337 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001338 if (buffersize > INT_MAX) {
1339 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001340 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001341 goto error;
1342 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001343 if (big_buffer == NULL) {
1344 /* Create the big buffer */
1345 big_buffer = PyString_FromStringAndSize(
1346 NULL, buffersize);
1347 if (big_buffer == NULL)
1348 goto error;
1349 buffer = PyString_AS_STRING(big_buffer);
1350 memcpy(buffer, small_buffer, nfilled);
1351 }
1352 else {
1353 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001354 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1355 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001356 buffer = PyString_AS_STRING(big_buffer);
1357 }
1358 continue;
1359 }
1360 end = buffer+nfilled+nread;
1361 q = buffer;
1362 do {
1363 /* Process complete lines */
1364 p++;
1365 line = PyString_FromStringAndSize(q, p-q);
1366 if (line == NULL)
1367 goto error;
1368 err = PyList_Append(list, line);
1369 Py_DECREF(line);
1370 if (err != 0)
1371 goto error;
1372 q = p;
1373 p = memchr(q, '\n', end-q);
1374 } while (p != NULL);
1375 /* Move the remaining incomplete line to the start */
1376 nfilled = end-q;
1377 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001378 if (sizehint > 0)
1379 if (totalread >= (size_t)sizehint)
1380 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001381 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001382 if (nfilled != 0) {
1383 /* Partial last line */
1384 line = PyString_FromStringAndSize(buffer, nfilled);
1385 if (line == NULL)
1386 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001387 if (sizehint > 0) {
1388 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001389 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001390 if (rest == NULL) {
1391 Py_DECREF(line);
1392 goto error;
1393 }
1394 PyString_Concat(&line, rest);
1395 Py_DECREF(rest);
1396 if (line == NULL)
1397 goto error;
1398 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001399 err = PyList_Append(list, line);
1400 Py_DECREF(line);
1401 if (err != 0)
1402 goto error;
1403 }
1404 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001405 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001406 return list;
1407}
1408
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001410file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001412 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001414 if (f->f_fp == NULL)
1415 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001416 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001417 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001418 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001420 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001421 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001422 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001423 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001424 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001425 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001426 return NULL;
1427 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001428 Py_INCREF(Py_None);
1429 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001430}
1431
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001433file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001434{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001435#define CHUNKSIZE 1000
1436 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001437 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001438 PyObject *result;
1439 int i, j, index, len, nwritten, islist;
1440
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001441 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001442 if (f->f_fp == NULL)
1443 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001444
1445 result = NULL;
1446 list = NULL;
1447 islist = PyList_Check(seq);
1448 if (islist)
1449 it = NULL;
1450 else {
1451 it = PyObject_GetIter(seq);
1452 if (it == NULL) {
1453 PyErr_SetString(PyExc_TypeError,
1454 "writelines() requires an iterable argument");
1455 return NULL;
1456 }
1457 /* From here on, fail by going to error, to reclaim "it". */
1458 list = PyList_New(CHUNKSIZE);
1459 if (list == NULL)
1460 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001461 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001462
1463 /* Strategy: slurp CHUNKSIZE lines into a private list,
1464 checking that they are all strings, then write that list
1465 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001466 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001467 if (islist) {
1468 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001469 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001470 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001471 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001472 j = PyList_GET_SIZE(list);
1473 }
1474 else {
1475 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001476 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001477 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001478 if (PyErr_Occurred())
1479 goto error;
1480 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001481 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001482 PyList_SetItem(list, j, line);
1483 }
1484 }
1485 if (j == 0)
1486 break;
1487
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001488 /* Check that all entries are indeed strings. If not,
1489 apply the same rules as for file.write() and
1490 convert the results to strings. This is slow, but
1491 seems to be the only way since all conversion APIs
1492 could potentially execute Python code. */
1493 for (i = 0; i < j; i++) {
1494 PyObject *v = PyList_GET_ITEM(list, i);
1495 if (!PyString_Check(v)) {
1496 const char *buffer;
1497 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001498 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001499 PyObject_AsReadBuffer(v,
1500 (const void**)&buffer,
1501 &len)) ||
1502 PyObject_AsCharBuffer(v,
1503 &buffer,
1504 &len))) {
1505 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001506 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001507 goto error;
1508 }
1509 line = PyString_FromStringAndSize(buffer,
1510 len);
1511 if (line == NULL)
1512 goto error;
1513 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001514 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001515 }
1516 }
1517
1518 /* Since we are releasing the global lock, the
1519 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001520 Py_BEGIN_ALLOW_THREADS
1521 f->f_softspace = 0;
1522 errno = 0;
1523 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001524 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001525 len = PyString_GET_SIZE(line);
1526 nwritten = fwrite(PyString_AS_STRING(line),
1527 1, len, f->f_fp);
1528 if (nwritten != len) {
1529 Py_BLOCK_THREADS
1530 PyErr_SetFromErrno(PyExc_IOError);
1531 clearerr(f->f_fp);
1532 goto error;
1533 }
1534 }
1535 Py_END_ALLOW_THREADS
1536
1537 if (j < CHUNKSIZE)
1538 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001539 }
1540
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001541 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001542 result = Py_None;
1543 error:
1544 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001545 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001546 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001547#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001548}
1549
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001550static PyObject *
1551file_getiter(PyFileObject *f)
1552{
1553 if (f->f_fp == NULL)
1554 return err_closed();
1555 Py_INCREF(f);
1556 return (PyObject *)f;
1557}
1558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001560"readline([size]) -> next line from the file, as a string.\n"
1561"\n"
1562"Retain newline. A non-negative size argument limits the maximum\n"
1563"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001567"read([size]) -> read at most size bytes, returned as a string.\n"
1568"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001569"If the size argument is negative or omitted, read until EOF is reached.\n"
1570"Notice that when in non-blocking mode, less data than what was requested\n"
1571"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001573PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001574"write(str) -> None. Write string str to file.\n"
1575"\n"
1576"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001577"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001580"fileno() -> integer \"file descriptor\".\n"
1581"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001582"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001584PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001585"seek(offset[, whence]) -> None. Move to new file position.\n"
1586"\n"
1587"Argument offset is a byte count. Optional argument whence defaults to\n"
1588"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1589"(move relative to current position, positive or negative), and 2 (move\n"
1590"relative to end of file, usually negative, although many platforms allow\n"
1591"seeking beyond the end of a file).\n"
1592"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001594
Guido van Rossumd7047b31995-01-02 19:07:15 +00001595#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001596PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001597"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1598"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001600#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602PyDoc_STRVAR(tell_doc,
1603"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001605PyDoc_STRVAR(readinto_doc,
1606"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001609"readlines([size]) -> list of strings, each a line from the file.\n"
1610"\n"
1611"Call readline() repeatedly and return a list of the lines so read.\n"
1612"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001615PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001616"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001617"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001618"For backward compatibility. File objects now include the performance\n"
1619"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001622"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001623"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001624"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001625"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(flush_doc,
1628"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001631"close() -> None or (perhaps) an integer. Close the file.\n"
1632"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001633"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001634"further I/O operations. close() may be called more than once without\n"
1635"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638PyDoc_STRVAR(isatty_doc,
1639"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001640
1641static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001642 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1643 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1644 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1645 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1646 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001647#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001648 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001649#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001650 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1651 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1652 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1653 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1654 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1655 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1656 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1657 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1658 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659};
1660
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001661#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001662
Guido van Rossum6f799372001-09-20 20:46:19 +00001663static PyMemberDef file_memberlist[] = {
1664 {"softspace", T_INT, OFF(f_softspace), 0,
1665 "flag indicating that a space needs to be printed; used by print"},
1666 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001667 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001668 {"name", T_OBJECT, OFF(f_name), RO,
1669 "file name"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001670 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001671 {NULL} /* Sentinel */
1672};
1673
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001674static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001675get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001676{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001677 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001678}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001679#ifdef WITH_UNIVERSAL_NEWLINES
1680static PyObject *
1681get_newlines(PyFileObject *f, void *closure)
1682{
1683 switch (f->f_newlinetypes) {
1684 case NEWLINE_UNKNOWN:
1685 Py_INCREF(Py_None);
1686 return Py_None;
1687 case NEWLINE_CR:
1688 return PyString_FromString("\r");
1689 case NEWLINE_LF:
1690 return PyString_FromString("\n");
1691 case NEWLINE_CR|NEWLINE_LF:
1692 return Py_BuildValue("(ss)", "\r", "\n");
1693 case NEWLINE_CRLF:
1694 return PyString_FromString("\r\n");
1695 case NEWLINE_CR|NEWLINE_CRLF:
1696 return Py_BuildValue("(ss)", "\r", "\r\n");
1697 case NEWLINE_LF|NEWLINE_CRLF:
1698 return Py_BuildValue("(ss)", "\n", "\r\n");
1699 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1700 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1701 default:
Jeremy Hylton8b735422002-08-14 21:01:41 +00001702 PyErr_Format(PyExc_SystemError,
1703 "Unknown newlines value 0x%x\n",
1704 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001705 return NULL;
1706 }
1707}
1708#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001709
Guido van Rossum32d34c82001-09-20 21:45:26 +00001710static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001711 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001712#ifdef WITH_UNIVERSAL_NEWLINES
Jeremy Hylton8b735422002-08-14 21:01:41 +00001713 {"newlines", (getter)get_newlines, NULL,
1714 "end-of-line convention used in this file"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001715#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001716 {0},
1717};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001718
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001719static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001720drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001721{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001722 if (f->f_buf != NULL) {
1723 PyMem_Free(f->f_buf);
1724 f->f_buf = NULL;
1725 }
Guido van Rossum65967252001-04-21 13:20:18 +00001726}
1727
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001728/* Make sure that file has a readahead buffer with at least one byte
1729 (unless at EOF) and no more than bufsize. Returns negative value on
1730 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001731static int
1732readahead(PyFileObject *f, int bufsize)
1733{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001734 int chunksize;
1735
1736 if (f->f_buf != NULL) {
1737 if( (f->f_bufend - f->f_bufptr) >= 1)
1738 return 0;
1739 else
1740 drop_readahead(f);
1741 }
1742 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1743 return -1;
1744 }
1745 Py_BEGIN_ALLOW_THREADS
1746 errno = 0;
1747 chunksize = Py_UniversalNewlineFread(
1748 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1749 Py_END_ALLOW_THREADS
1750 if (chunksize == 0) {
1751 if (ferror(f->f_fp)) {
1752 PyErr_SetFromErrno(PyExc_IOError);
1753 clearerr(f->f_fp);
1754 drop_readahead(f);
1755 return -1;
1756 }
1757 }
1758 f->f_bufptr = f->f_buf;
1759 f->f_bufend = f->f_buf + chunksize;
1760 return 0;
1761}
1762
1763/* Used by file_iternext. The returned string will start with 'skip'
1764 uninitialized bytes followed by the remainder of the line. Don't be
1765 horrified by the recursive call: maximum recursion depth is limited by
1766 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1767
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001768static PyStringObject *
1769readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1770{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001771 PyStringObject* s;
1772 char *bufptr;
1773 char *buf;
1774 int len;
1775
1776 if (f->f_buf == NULL)
1777 if (readahead(f, bufsize) < 0)
1778 return NULL;
1779
1780 len = f->f_bufend - f->f_bufptr;
1781 if (len == 0)
1782 return (PyStringObject *)
1783 PyString_FromStringAndSize(NULL, skip);
1784 bufptr = memchr(f->f_bufptr, '\n', len);
1785 if (bufptr != NULL) {
1786 bufptr++; /* Count the '\n' */
1787 len = bufptr - f->f_bufptr;
1788 s = (PyStringObject *)
1789 PyString_FromStringAndSize(NULL, skip+len);
1790 if (s == NULL)
1791 return NULL;
1792 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1793 f->f_bufptr = bufptr;
1794 if (bufptr == f->f_bufend)
1795 drop_readahead(f);
1796 } else {
1797 bufptr = f->f_bufptr;
1798 buf = f->f_buf;
1799 f->f_buf = NULL; /* Force new readahead buffer */
1800 s = readahead_get_line_skip(
1801 f, skip+len, bufsize + (bufsize>>2) );
1802 if (s == NULL) {
1803 PyMem_Free(buf);
1804 return NULL;
1805 }
1806 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1807 PyMem_Free(buf);
1808 }
1809 return s;
1810}
1811
1812/* A larger buffer size may actually decrease performance. */
1813#define READAHEAD_BUFSIZE 8192
1814
1815static PyObject *
1816file_iternext(PyFileObject *f)
1817{
1818 PyStringObject* l;
1819
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001820 if (f->f_fp == NULL)
1821 return err_closed();
1822
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001823 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1824 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1825 Py_XDECREF(l);
1826 return NULL;
1827 }
1828 return (PyObject *)l;
1829}
1830
1831
Tim Peters59c9a642001-09-13 05:38:56 +00001832static PyObject *
1833file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1834{
Tim Peters44410012001-09-14 03:26:08 +00001835 PyObject *self;
1836 static PyObject *not_yet_string;
1837
1838 assert(type != NULL && type->tp_alloc != NULL);
1839
1840 if (not_yet_string == NULL) {
1841 not_yet_string = PyString_FromString("<uninitialized file>");
1842 if (not_yet_string == NULL)
1843 return NULL;
1844 }
1845
1846 self = type->tp_alloc(type, 0);
1847 if (self != NULL) {
1848 /* Always fill in the name and mode, so that nobody else
1849 needs to special-case NULLs there. */
1850 Py_INCREF(not_yet_string);
1851 ((PyFileObject *)self)->f_name = not_yet_string;
1852 Py_INCREF(not_yet_string);
1853 ((PyFileObject *)self)->f_mode = not_yet_string;
1854 }
1855 return self;
1856}
1857
1858static int
1859file_init(PyObject *self, PyObject *args, PyObject *kwds)
1860{
1861 PyFileObject *foself = (PyFileObject *)self;
1862 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001863 static char *kwlist[] = {"name", "mode", "buffering", 0};
1864 char *name = NULL;
1865 char *mode = "r";
1866 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001867 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001868
1869 assert(PyFile_Check(self));
1870 if (foself->f_fp != NULL) {
1871 /* Have to close the existing file first. */
1872 PyObject *closeresult = file_close(foself);
1873 if (closeresult == NULL)
1874 return -1;
1875 Py_DECREF(closeresult);
1876 }
Tim Peters59c9a642001-09-13 05:38:56 +00001877
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001878#ifdef Py_WIN_WIDE_FILENAMES
1879 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1880 PyObject *po;
1881 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1882 kwlist, &po, &mode, &bufsize)) {
1883 wideargument = 1;
1884 if (fill_file_fields(foself, NULL, name, mode,
1885 fclose, po) == NULL)
1886 goto Error;
1887 } else {
1888 /* Drop the argument parsing error as narrow
1889 strings are also valid. */
1890 PyErr_Clear();
1891 }
1892 }
1893#endif
1894
1895 if (!wideargument) {
1896 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1897 Py_FileSystemDefaultEncoding,
1898 &name,
1899 &mode, &bufsize))
1900 return -1;
1901 if (fill_file_fields(foself, NULL, name, mode,
1902 fclose, NULL) == NULL)
1903 goto Error;
1904 }
Tim Peters44410012001-09-14 03:26:08 +00001905 if (open_the_file(foself, name, mode) == NULL)
1906 goto Error;
1907 PyFile_SetBufSize(self, bufsize);
1908 goto Done;
1909
1910Error:
1911 ret = -1;
1912 /* fall through */
1913Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001914 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001915 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001916}
1917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918PyDoc_VAR(file_doc) =
1919PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001920"file(name[, mode[, buffering]]) -> file object\n"
1921"\n"
1922"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1923"writing or appending. The file will be created if it doesn't exist\n"
1924"when opened for writing or appending; it will be truncated when\n"
1925"opened for writing. Add a 'b' to the mode for binary files.\n"
1926"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1927"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001928"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001930#ifdef WITH_UNIVERSAL_NEWLINES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001931PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001932"Add a 'U' to mode to open the file for input with universal newline\n"
1933"support. Any line ending in the input file will be seen as a '\\n'\n"
1934"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1935"the value for this attribute is one of None (no newline read yet),\n"
1936"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1937"\n"
1938"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001939)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001940#endif /* WITH_UNIVERSAL_NEWLINES */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001941PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001942"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943"Note: open() is an alias for file()."
1944);
Tim Peters59c9a642001-09-13 05:38:56 +00001945
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001946PyTypeObject PyFile_Type = {
1947 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001948 0,
1949 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001950 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001951 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001952 (destructor)file_dealloc, /* tp_dealloc */
1953 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001954 0, /* tp_getattr */
1955 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001956 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001957 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001958 0, /* tp_as_number */
1959 0, /* tp_as_sequence */
1960 0, /* tp_as_mapping */
1961 0, /* tp_hash */
1962 0, /* tp_call */
1963 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001964 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00001965 /* softspace is writable: we must supply tp_setattro */
1966 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001967 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001969 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001970 0, /* tp_traverse */
1971 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001972 0, /* tp_richcompare */
1973 0, /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001974 (getiterfunc)file_getiter, /* tp_iter */
1975 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001976 file_methods, /* tp_methods */
1977 file_memberlist, /* tp_members */
1978 file_getsetlist, /* tp_getset */
1979 0, /* tp_base */
1980 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001981 0, /* tp_descr_get */
1982 0, /* tp_descr_set */
1983 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001984 (initproc)file_init, /* tp_init */
1985 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001986 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001987 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001988};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001989
1990/* Interface for the 'soft space' between print items. */
1991
1992int
Fred Drakefd99de62000-07-09 05:02:18 +00001993PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001994{
1995 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001996 if (f == NULL) {
1997 /* Do nothing */
1998 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001999 else if (PyFile_Check(f)) {
2000 oldflag = ((PyFileObject *)f)->f_softspace;
2001 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00002002 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00002003 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002004 PyObject *v;
2005 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002006 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002007 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002008 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002009 if (PyInt_Check(v))
2010 oldflag = PyInt_AsLong(v);
2011 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002012 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002013 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002014 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002015 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002016 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017 if (PyObject_SetAttrString(f, "softspace", v) != 0)
2018 PyErr_Clear();
2019 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002020 }
2021 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00002022 return oldflag;
2023}
Guido van Rossum3165fe61992-09-25 21:59:05 +00002024
2025/* Interfaces to write objects/strings to file-like objects */
2026
2027int
Fred Drakefd99de62000-07-09 05:02:18 +00002028PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002029{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002031 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002032 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002033 return -1;
2034 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002035 else if (PyFile_Check(f)) {
2036 FILE *fp = PyFile_AsFile(f);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002037 if (fp == NULL) {
2038 err_closed();
2039 return -1;
2040 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002041 return PyObject_Print(v, fp, flags);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002042 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002043 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002044 if (writer == NULL)
2045 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002046 if (flags & Py_PRINT_RAW) {
2047 if (PyUnicode_Check(v)) {
2048 value = v;
2049 Py_INCREF(value);
2050 } else
2051 value = PyObject_Str(v);
2052 }
2053 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002054 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002055 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002057 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002058 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002059 args = Py_BuildValue("(O)", value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002060 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061 Py_DECREF(value);
2062 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002063 return -1;
2064 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002065 result = PyEval_CallObject(writer, args);
2066 Py_DECREF(args);
2067 Py_DECREF(value);
2068 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002069 if (result == NULL)
2070 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002072 return 0;
2073}
2074
Guido van Rossum27a60b11997-05-22 22:25:11 +00002075int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002076PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002077{
2078 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002079 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002080 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002081 PyErr_SetString(PyExc_SystemError,
2082 "null file for PyFile_WriteString");
2083 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002084 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002085 else if (PyFile_Check(f)) {
2086 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002087 if (fp == NULL) {
2088 err_closed();
2089 return -1;
2090 }
2091 fputs(s, fp);
2092 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002093 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002094 else if (!PyErr_Occurred()) {
2095 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002096 int err;
2097 if (v == NULL)
2098 return -1;
2099 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2100 Py_DECREF(v);
2101 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002102 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002103 else
2104 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002105}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002106
2107/* Try to get a file-descriptor from a Python object. If the object
2108 is an integer or long integer, its value is returned. If not, the
2109 object's fileno() method is called if it exists; the method must return
2110 an integer or long integer, which is returned as the file descriptor value.
2111 -1 is returned on failure.
2112*/
2113
2114int PyObject_AsFileDescriptor(PyObject *o)
2115{
2116 int fd;
2117 PyObject *meth;
2118
2119 if (PyInt_Check(o)) {
2120 fd = PyInt_AsLong(o);
2121 }
2122 else if (PyLong_Check(o)) {
2123 fd = PyLong_AsLong(o);
2124 }
2125 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2126 {
2127 PyObject *fno = PyEval_CallObject(meth, NULL);
2128 Py_DECREF(meth);
2129 if (fno == NULL)
2130 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002131
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002132 if (PyInt_Check(fno)) {
2133 fd = PyInt_AsLong(fno);
2134 Py_DECREF(fno);
2135 }
2136 else if (PyLong_Check(fno)) {
2137 fd = PyLong_AsLong(fno);
2138 Py_DECREF(fno);
2139 }
2140 else {
2141 PyErr_SetString(PyExc_TypeError,
2142 "fileno() returned a non-integer");
2143 Py_DECREF(fno);
2144 return -1;
2145 }
2146 }
2147 else {
2148 PyErr_SetString(PyExc_TypeError,
2149 "argument must be an int, or have a fileno() method.");
2150 return -1;
2151 }
2152
2153 if (fd < 0) {
2154 PyErr_Format(PyExc_ValueError,
2155 "file descriptor cannot be a negative integer (%i)",
2156 fd);
2157 return -1;
2158 }
2159 return fd;
2160}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002161
2162#ifdef WITH_UNIVERSAL_NEWLINES
2163/* From here on we need access to the real fgets and fread */
2164#undef fgets
2165#undef fread
2166
2167/*
2168** Py_UniversalNewlineFgets is an fgets variation that understands
2169** all of \r, \n and \r\n conventions.
2170** The stream should be opened in binary mode.
2171** If fobj is NULL the routine always does newline conversion, and
2172** it may peek one char ahead to gobble the second char in \r\n.
2173** If fobj is non-NULL it must be a PyFileObject. In this case there
2174** is no readahead but in stead a flag is used to skip a following
2175** \n on the next read. Also, if the file is open in binary mode
2176** the whole conversion is skipped. Finally, the routine keeps track of
2177** the different types of newlines seen.
2178** Note that we need no error handling: fgets() treats error and eof
2179** identically.
2180*/
2181char *
2182Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2183{
2184 char *p = buf;
2185 int c;
2186 int newlinetypes = 0;
2187 int skipnextlf = 0;
2188 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002189
Jack Jansen7b8c7542002-04-14 20:12:41 +00002190 if (fobj) {
2191 if (!PyFile_Check(fobj)) {
2192 errno = ENXIO; /* What can you do... */
2193 return NULL;
2194 }
2195 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2196 if ( !univ_newline )
2197 return fgets(buf, n, stream);
2198 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2199 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2200 }
2201 FLOCKFILE(stream);
2202 c = 'x'; /* Shut up gcc warning */
2203 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2204 if (skipnextlf ) {
2205 skipnextlf = 0;
2206 if (c == '\n') {
2207 /* Seeing a \n here with skipnextlf true
2208 ** means we saw a \r before.
2209 */
2210 newlinetypes |= NEWLINE_CRLF;
2211 c = GETC(stream);
2212 if (c == EOF) break;
2213 } else {
2214 /*
2215 ** Note that c == EOF also brings us here,
2216 ** so we're okay if the last char in the file
2217 ** is a CR.
2218 */
2219 newlinetypes |= NEWLINE_CR;
2220 }
2221 }
2222 if (c == '\r') {
2223 /* A \r is translated into a \n, and we skip
2224 ** an adjacent \n, if any. We don't set the
2225 ** newlinetypes flag until we've seen the next char.
2226 */
2227 skipnextlf = 1;
2228 c = '\n';
2229 } else if ( c == '\n') {
2230 newlinetypes |= NEWLINE_LF;
2231 }
2232 *p++ = c;
2233 if (c == '\n') break;
2234 }
2235 if ( c == EOF && skipnextlf )
2236 newlinetypes |= NEWLINE_CR;
2237 FUNLOCKFILE(stream);
2238 *p = '\0';
2239 if (fobj) {
2240 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2241 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2242 } else if ( skipnextlf ) {
2243 /* If we have no file object we cannot save the
2244 ** skipnextlf flag. We have to readahead, which
2245 ** will cause a pause if we're reading from an
2246 ** interactive stream, but that is very unlikely
2247 ** unless we're doing something silly like
2248 ** execfile("/dev/tty").
2249 */
2250 c = GETC(stream);
2251 if ( c != '\n' )
2252 ungetc(c, stream);
2253 }
2254 if (p == buf)
2255 return NULL;
2256 return buf;
2257}
2258
2259/*
2260** Py_UniversalNewlineFread is an fread variation that understands
2261** all of \r, \n and \r\n conventions.
2262** The stream should be opened in binary mode.
2263** fobj must be a PyFileObject. In this case there
2264** is no readahead but in stead a flag is used to skip a following
2265** \n on the next read. Also, if the file is open in binary mode
2266** the whole conversion is skipped. Finally, the routine keeps track of
2267** the different types of newlines seen.
2268*/
2269size_t
Tim Peters058b1412002-04-21 07:29:14 +00002270Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002271 FILE *stream, PyObject *fobj)
2272{
Tim Peters058b1412002-04-21 07:29:14 +00002273 char *dst = buf;
2274 PyFileObject *f = (PyFileObject *)fobj;
2275 int newlinetypes, skipnextlf;
2276
2277 assert(buf != NULL);
2278 assert(stream != NULL);
2279
Jack Jansen7b8c7542002-04-14 20:12:41 +00002280 if (!fobj || !PyFile_Check(fobj)) {
2281 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002282 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002283 }
Tim Peters058b1412002-04-21 07:29:14 +00002284 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002285 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002286 newlinetypes = f->f_newlinetypes;
2287 skipnextlf = f->f_skipnextlf;
2288 /* Invariant: n is the number of bytes remaining to be filled
2289 * in the buffer.
2290 */
2291 while (n) {
2292 size_t nread;
2293 int shortread;
2294 char *src = dst;
2295
2296 nread = fread(dst, 1, n, stream);
2297 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002298 if (nread == 0)
2299 break;
2300
Tim Peterse1682a82002-04-21 18:15:20 +00002301 n -= nread; /* assuming 1 byte out for each in; will adjust */
2302 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002303 while (nread--) {
2304 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002305 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002306 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002307 *dst++ = '\n';
2308 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002309 }
2310 else if (skipnextlf && c == '\n') {
2311 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002312 skipnextlf = 0;
2313 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002314 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002315 }
2316 else {
2317 /* Normal char to be stored in buffer. Also
2318 * update the newlinetypes flag if either this
2319 * is an LF or the previous char was a CR.
2320 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002321 if (c == '\n')
2322 newlinetypes |= NEWLINE_LF;
2323 else if (skipnextlf)
2324 newlinetypes |= NEWLINE_CR;
2325 *dst++ = c;
2326 skipnextlf = 0;
2327 }
2328 }
Tim Peters058b1412002-04-21 07:29:14 +00002329 if (shortread) {
2330 /* If this is EOF, update type flags. */
2331 if (skipnextlf && feof(stream))
2332 newlinetypes |= NEWLINE_CR;
2333 break;
2334 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002335 }
Tim Peters058b1412002-04-21 07:29:14 +00002336 f->f_newlinetypes = newlinetypes;
2337 f->f_skipnextlf = skipnextlf;
2338 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002339}
2340#endif