annotate Objects/fileobject.c @ 21283:d4907cdcc299 legacy-trunk

Use an immutable tuple for __slots instead of a mutable list Bump version number
author Andrew M. Kuchling <amk@amk.ca>
date Fri, 07 Dec 2001 14:22:13 +0000
parents ed3dce17fc79
children e223397b26fa be213f1cd462
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* File object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
4 #include "Python.h"
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
5 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10562
diff changeset
7 #ifndef DONT_HAVE_SYS_TYPES_H
9809
93d70308be17 Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents: 9789
diff changeset
8 #include <sys/types.h>
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10562
diff changeset
9 #endif /* DONT_HAVE_SYS_TYPES_H */
9809
93d70308be17 Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents: 9789
diff changeset
10
4585
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
11 #ifdef HAVE_UNISTD_H
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
12 #include <unistd.h>
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
13 #endif
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
14
5414
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
15 #ifdef MS_WIN32
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
16 #define fileno _fileno
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
17 /* can (almost fully) duplicate with _chsize, see file_truncate */
5414
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
18 #define HAVE_FTRUNCATE
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
19 #endif
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
20
8180
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
21 #ifdef macintosh
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
22 #ifdef USE_GUSI
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
23 #define HAVE_FTRUNCATE
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
24 #endif
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
25 #endif
68ee8abd9782 Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents: 8160
diff changeset
26
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
27 #ifdef __MWERKS__
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
28 /* Mwerks fopen() doesn't always set errno */
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
29 #define NO_FOPEN_ERRNO
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
30 #endif
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
31
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
32 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
33
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10562
diff changeset
34 #ifndef DONT_HAVE_ERRNO_H
1319
cd397266dc1c * Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
35 #include <errno.h>
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 10562
diff changeset
36 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
38
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
40 PyObject_HEAD
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 FILE *f_fp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
42 PyObject *f_name;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
43 PyObject *f_mode;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13360
diff changeset
44 int (*f_close)(FILE *);
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
45 int f_softspace; /* Flag used by 'print' command */
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
46 int f_binary; /* Flag which indicates whether the file is open
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
47 open in binary (1) or test (0) mode */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
48 } PyFileObject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 FILE *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
51 PyFile_AsFile(PyObject *f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
53 if (f == NULL || !PyFile_Check(f))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 return NULL;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
55 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
56 return ((PyFileObject *)f)->f_fp;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
59 PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
60 PyFile_Name(PyObject *f)
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
61 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
62 if (f == NULL || !PyFile_Check(f))
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
63 return NULL;
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
64 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
65 return ((PyFileObject *)f)->f_name;
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
66 }
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
67
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
68
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
69 static PyObject *
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
70 fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
71 int (*close)(FILE *))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 {
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
73 assert(f != NULL);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
74 assert(PyFile_Check(f));
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
75 assert(f->f_fp == NULL);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
76
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
77 Py_DECREF(f->f_name);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
78 Py_DECREF(f->f_mode);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
79 f->f_name = PyString_FromString(name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
80 f->f_mode = PyString_FromString(mode);
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
81
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
82 f->f_close = close;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
83 f->f_softspace = 0;
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
84 f->f_binary = strchr(mode,'b') != NULL;
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
85
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
86 if (f->f_name == NULL || f->f_mode == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 f->f_fp = fp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
89 return (PyObject *) f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
92 static PyObject *
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
93 open_the_file(PyFileObject *f, char *name, char *mode)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 {
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
95 assert(f != NULL);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
96 assert(PyFile_Check(f));
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
97 assert(name != NULL);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
98 assert(mode != NULL);
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
99 assert(f->f_fp == NULL);
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
100
19901
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
101 /* rexec.py can't stop a user from getting the file() constructor --
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
102 all they have to do is get *any* file object f, and then do
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
103 type(f). Here we prevent them from doing damage with it. */
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
104 if (PyEval_GetRestricted()) {
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
105 PyErr_SetString(PyExc_IOError,
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
106 "file() constructor not accessible in restricted mode");
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
107 return NULL;
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
108 }
20942
d3dd0c2a1771 open_the_file(): Explicitly set errno to 0 before calling fopen().
Tim Peters <tim.peters@gmail.com>
parents: 20935
diff changeset
109 errno = 0;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
110 #ifdef HAVE_FOPENRF
138
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
111 if (*mode == '*') {
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
112 FILE *fopenRF();
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
113 f->f_fp = fopenRF(name, mode+1);
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
114 }
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
115 else
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
116 #endif
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
117 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
118 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
119 f->f_fp = fopen(name, mode);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
120 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
121 }
138
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
122 if (f->f_fp == NULL) {
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
123 #ifdef NO_FOPEN_ERRNO
21113
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
124 /* Metroworks only, wich does not always sets errno */
20924
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
125 if (errno == 0) {
21113
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
126 PyObject *v;
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
127 v = Py_BuildValue("(is)", 0, "Cannot open file");
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
128 if (v != NULL) {
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
129 PyErr_SetObject(PyExc_IOError, v);
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
130 Py_DECREF(v);
ed3dce17fc79 Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents: 21095
diff changeset
131 }
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
132 return NULL;
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
133 }
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
134 #endif
20924
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
135 if (errno == EINVAL)
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
136 PyErr_Format(PyExc_IOError, "invalid argument: %s",
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
137 mode);
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
138 else
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20793
diff changeset
139 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
140 f = NULL;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
141 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
142 return (PyObject *)f;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
143 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
144
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
145 PyObject *
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
146 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
147 {
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
148 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
149 NULL, NULL);
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
150 if (f != NULL) {
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
151 if (fill_file_fields(f, fp, name, mode, close) == NULL) {
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
152 Py_DECREF(f);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
153 f = NULL;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
154 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
155 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
156 return (PyObject *) f;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
157 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
158
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
159 PyObject *
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
160 PyFile_FromString(char *name, char *mode)
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
161 {
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
162 extern int fclose(FILE *);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
163 PyFileObject *f;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
164
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
165 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
166 if (f != NULL) {
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
167 if (open_the_file(f, name, mode) == NULL) {
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
168 Py_DECREF(f);
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
169 f = NULL;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
170 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
172 return (PyObject *)f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
175 void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
176 PyFile_SetBufSize(PyObject *f, int bufsize)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
177 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
178 if (bufsize >= 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
179 #ifdef HAVE_SETVBUF
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
180 int type;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
181 switch (bufsize) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
182 case 0:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
183 type = _IONBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
184 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
185 case 1:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
186 type = _IOLBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
187 bufsize = BUFSIZ;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
188 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
189 default:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
190 type = _IOFBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
191 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
192 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
193 type, bufsize);
7572
c9f417124e0f When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents: 7536
diff changeset
194 #else /* !HAVE_SETVBUF */
c9f417124e0f When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents: 7536
diff changeset
195 if (bufsize <= 1)
c9f417124e0f When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents: 7536
diff changeset
196 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
c9f417124e0f When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents: 7536
diff changeset
197 #endif /* !HAVE_SETVBUF */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
198 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
199 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
200
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
201 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
202 err_closed(void)
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
203 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
204 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
205 return NULL;
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
206 }
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
207
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 static void
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
211 file_dealloc(PyFileObject *f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
213 if (f->f_fp != NULL && f->f_close != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
214 Py_BEGIN_ALLOW_THREADS
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
215 (*f->f_close)(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
216 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
217 }
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
218 Py_XDECREF(f->f_name);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
219 Py_XDECREF(f->f_mode);
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20045
diff changeset
220 f->ob_type->tp_free((PyObject *)f);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
223 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
224 file_repr(PyFileObject *f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
226 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
227 f->f_fp == NULL ? "closed" : "open",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
228 PyString_AsString(f->f_name),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
229 PyString_AsString(f->f_mode),
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
230 f);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
233 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
234 file_close(PyFileObject *f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 {
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
236 int sts = 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 if (f->f_fp != NULL) {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
238 if (f->f_close != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
239 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
240 errno = 0;
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
241 sts = (*f->f_close)(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
242 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
243 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 f->f_fp = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 }
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
246 if (sts == EOF)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
247 return PyErr_SetFromErrno(PyExc_IOError);
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
248 if (sts != 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
249 return PyInt_FromLong((long)sts);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
250 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
251 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
254
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
255 /* Our very own off_t-like type, 64-bit if possible */
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
256 #if !defined(HAVE_LARGEFILE_SUPPORT)
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
257 typedef off_t Py_off_t;
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
258 #elif SIZEOF_OFF_T >= 8
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
259 typedef off_t Py_off_t;
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
260 #elif SIZEOF_FPOS_T >= 8
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
261 typedef fpos_t Py_off_t;
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
262 #else
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
263 #error "Large file support, but neither off_t nor fpos_t is large enough."
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
264 #endif
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
265
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
266
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
267 /* a portable fseek() function
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
268 return 0 on success, non-zero on failure (with errno set) */
17518
f939ce52a734 Make some private symbols static.
Guido van Rossum <guido@python.org>
parents: 16903
diff changeset
269 static int
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
270 _portable_fseek(FILE *fp, Py_off_t offset, int whence)
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
271 {
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
272 #if !defined(HAVE_LARGEFILE_SUPPORT)
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
273 return fseek(fp, offset, whence);
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
274 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
275 return fseeko(fp, offset, whence);
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
276 #elif defined(HAVE_FSEEK64)
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
277 return fseek64(fp, offset, whence);
15068
a53f16b20f1d Donn Cave <donn@oz.net>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
278 #elif defined(__BEOS__)
a53f16b20f1d Donn Cave <donn@oz.net>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
279 return _fseek(fp, offset, whence);
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
280 #elif SIZEOF_FPOS_T >= 8
15925
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
281 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
282 and fgetpos() to implement fseek()*/
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
283 fpos_t pos;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
284 switch (whence) {
15925
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
285 case SEEK_END:
19836
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
286 #ifdef MS_WINDOWS
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
287 fflush(fp);
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
288 if (_lseeki64(fileno(fp), 0, 2) == -1)
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
289 return -1;
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
290 #else
15925
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
291 if (fseek(fp, 0, SEEK_END) != 0)
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
292 return -1;
19836
5420e7d348a4 _portable_fseek():
Guido van Rossum <guido@python.org>
parents: 19754
diff changeset
293 #endif
15925
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
294 /* fall through */
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
295 case SEEK_CUR:
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
296 if (fgetpos(fp, &pos) != 0)
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
297 return -1;
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
298 offset += pos;
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
299 break;
9d5beed0b93f Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents: 15876
diff changeset
300 /* case SEEK_SET: break; */
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
301 }
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
302 return fsetpos(fp, &offset);
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
303 #else
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
304 #error "Large file support, but no way to fseek."
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
305 #endif
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
306 }
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
307
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
308
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
309 /* a portable ftell() function
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
310 Return -1 on failure with errno set appropriately, current file
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
311 position on success */
17518
f939ce52a734 Make some private symbols static.
Guido van Rossum <guido@python.org>
parents: 16903
diff changeset
312 static Py_off_t
14277
984abd20eec2 Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents: 14215
diff changeset
313 _portable_ftell(FILE* fp)
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
314 {
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
315 #if !defined(HAVE_LARGEFILE_SUPPORT)
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
316 return ftell(fp);
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
317 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
318 return ftello(fp);
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
319 #elif defined(HAVE_FTELL64)
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
320 return ftell64(fp);
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
321 #elif SIZEOF_FPOS_T >= 8
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
322 fpos_t pos;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
323 if (fgetpos(fp, &pos) != 0)
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
324 return -1;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
325 return pos;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
326 #else
19732
b7dc5159448f Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
327 #error "Large file support, but no way to ftell."
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
328 #endif
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
329 }
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
330
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
331
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
332 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
333 file_seek(PyFileObject *f, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
335 int whence;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
336 int ret;
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
337 Py_off_t offset;
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
338 PyObject *offobj;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
339
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
340 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
341 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
342 whence = 0;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
343 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
344 return NULL;
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
345 #if !defined(HAVE_LARGEFILE_SUPPORT)
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
346 offset = PyInt_AsLong(offobj);
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
347 #else
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
348 offset = PyLong_Check(offobj) ?
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
349 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
350 #endif
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
351 if (PyErr_Occurred())
9762
ed523ea073db Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents: 9633
diff changeset
352 return NULL;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
353
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
354 Py_BEGIN_ALLOW_THREADS
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
355 errno = 0;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
356 ret = _portable_fseek(f->f_fp, offset, whence);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
357 Py_END_ALLOW_THREADS
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
358
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
359 if (ret != 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
360 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
361 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
362 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
363 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
364 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
365 return Py_None;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
366 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
367
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
368
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
369 #ifdef HAVE_FTRUNCATE
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
370 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
371 file_truncate(PyFileObject *f, PyObject *args)
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
372 {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
373 int ret;
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
374 Py_off_t newsize;
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
375 PyObject *newsizeobj;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
376
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
377 if (f->f_fp == NULL)
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
378 return err_closed();
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
379 newsizeobj = NULL;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
380 if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
9762
ed523ea073db Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents: 9633
diff changeset
381 return NULL;
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
382 if (newsizeobj != NULL) {
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
383 #if !defined(HAVE_LARGEFILE_SUPPORT)
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
384 newsize = PyInt_AsLong(newsizeobj);
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
385 #else
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
386 newsize = PyLong_Check(newsizeobj) ?
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
387 PyLong_AsLongLong(newsizeobj) :
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
388 PyInt_AsLong(newsizeobj);
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
389 #endif
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
390 if (PyErr_Occurred())
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
391 return NULL;
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
392 } else {
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
393 /* Default to current position*/
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
394 Py_BEGIN_ALLOW_THREADS
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
395 errno = 0;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
396 newsize = _portable_ftell(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
397 Py_END_ALLOW_THREADS
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
398 if (newsize == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
399 PyErr_SetFromErrno(PyExc_IOError);
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
400 clearerr(f->f_fp);
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
401 return NULL;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
402 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
403 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
404 Py_BEGIN_ALLOW_THREADS
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
405 errno = 0;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
406 ret = fflush(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
407 Py_END_ALLOW_THREADS
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
408 if (ret != 0) goto onioerror;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
409
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
410 #ifdef MS_WIN32
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
411 /* can use _chsize; if, however, the newsize overflows 32-bits then
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
412 _chsize is *not* adequate; in this case, an OverflowError is raised */
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
413 if (newsize > LONG_MAX) {
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
414 PyErr_SetString(PyExc_OverflowError,
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
415 "the new size is too long for _chsize (it is limited to 32-bit values)");
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
416 return NULL;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
417 } else {
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
418 Py_BEGIN_ALLOW_THREADS
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
419 errno = 0;
19754
e1f1e9ebf050 Enable large file support on Win32 systems.
Tim Peters <tim.peters@gmail.com>
parents: 19732
diff changeset
420 ret = _chsize(fileno(f->f_fp), (long)newsize);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
421 Py_END_ALLOW_THREADS
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
422 if (ret != 0) goto onioerror;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
423 }
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
424 #else
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
425 Py_BEGIN_ALLOW_THREADS
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
426 errno = 0;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
427 ret = ftruncate(fileno(f->f_fp), newsize);
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
428 Py_END_ALLOW_THREADS
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
429 if (ret != 0) goto onioerror;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
430 #endif /* !MS_WIN32 */
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
431
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
432 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
433 return Py_None;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
434
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
435 onioerror:
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
436 PyErr_SetFromErrno(PyExc_IOError);
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
437 clearerr(f->f_fp);
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
438 return NULL;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
439 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
440 #endif /* HAVE_FTRUNCATE */
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
441
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
442 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
443 file_tell(PyFileObject *f)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
444 {
16903
e86875f069b1 Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents: 15978
diff changeset
445 Py_off_t pos;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
446
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
447 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
448 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
449 Py_BEGIN_ALLOW_THREADS
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
450 errno = 0;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
451 pos = _portable_ftell(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
452 Py_END_ALLOW_THREADS
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
453 if (pos == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
454 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
455 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
456 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
457 }
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
458 #if !defined(HAVE_LARGEFILE_SUPPORT)
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
459 return PyInt_FromLong(pos);
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
460 #else
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
461 return PyLong_FromLongLong(pos);
9789
08dfbee0a32a Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents: 9762
diff changeset
462 #endif
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
463 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
464
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
465 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
466 file_fileno(PyFileObject *f)
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
467 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
468 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
469 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
470 return PyInt_FromLong((long) fileno(f->f_fp));
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
471 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
472
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
473 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
474 file_flush(PyFileObject *f)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
475 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
476 int res;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
477
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
478 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
479 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
480 Py_BEGIN_ALLOW_THREADS
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
481 errno = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
482 res = fflush(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
483 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
484 if (res != 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
485 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
486 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
487 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
488 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
489 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
490 return Py_None;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
491 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
492
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
493 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
494 file_isatty(PyFileObject *f)
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
495 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
496 long res;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
497 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
498 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
499 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
500 res = isatty((int)fileno(f->f_fp));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
501 Py_END_ALLOW_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
502 return PyInt_FromLong(res);
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
503 }
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
504
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
505
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
506 #if BUFSIZ < 8192
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
507 #define SMALLCHUNK 8192
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
508 #else
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
509 #define SMALLCHUNK BUFSIZ
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
510 #endif
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
511
9883
7195ec296b3d Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents: 9809
diff changeset
512 #if SIZEOF_INT < 4
7195ec296b3d Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents: 9809
diff changeset
513 #define BIGCHUNK (512 * 32)
7195ec296b3d Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents: 9809
diff changeset
514 #else
7195ec296b3d Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents: 9809
diff changeset
515 #define BIGCHUNK (512 * 1024)
7195ec296b3d Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents: 9809
diff changeset
516 #endif
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
517
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
518 static size_t
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
519 new_buffersize(PyFileObject *f, size_t currentsize)
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
520 {
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
521 #ifdef HAVE_FSTAT
18755
f8bd14aef4d6 Kill more warnings from the SGI compiler.
Fred Drake <fdrake@acm.org>
parents: 17911
diff changeset
522 off_t pos, end;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
523 struct stat st;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
524 if (fstat(fileno(f->f_fp), &st) == 0) {
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
525 end = st.st_size;
9633
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
526 /* The following is not a bug: we really need to call lseek()
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
527 *and* ftell(). The reason is that some stdio libraries
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
528 mistakenly flush their buffer when ftell() is called and
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
529 the lseek() call it makes fails, thereby throwing away
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
530 data that cannot be recovered in any way. To avoid this,
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
531 we first test lseek(), and only call ftell() if lseek()
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
532 works. We can't use the lseek() value either, because we
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
533 need to take the amount of buffered data into account.
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
534 (Yet another reason why stdio stinks. :-) */
20365
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
535 #ifdef USE_GUSI2
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
536 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
537 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
538 #else
8217
a19661076529 Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents: 8180
diff changeset
539 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
20365
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
540 #endif
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
541 if (pos >= 0) {
8217
a19661076529 Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents: 8180
diff changeset
542 pos = ftell(f->f_fp);
20365
e46f69f5798b Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents: 20277
diff changeset
543 }
8160
ce9fc64528a7 Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
544 if (pos < 0)
ce9fc64528a7 Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
545 clearerr(f->f_fp);
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
546 if (end > pos && pos >= 0)
9633
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
547 return currentsize + end - pos + 1;
7536
eedac50d7744 Of course, I shouldn't have used lseek() to find out the file's
Guido van Rossum <guido@python.org>
parents: 7398
diff changeset
548 /* Add 1 so if the file were to grow we'd notice. */
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
549 }
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
550 #endif
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
551 if (currentsize > SMALLCHUNK) {
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
552 /* Keep doubling until we reach BIGCHUNK;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
553 then keep adding BIGCHUNK. */
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
554 if (currentsize <= BIGCHUNK)
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
555 return currentsize + currentsize;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
556 else
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
557 return currentsize + BIGCHUNK;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
558 }
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
559 return currentsize + SMALLCHUNK;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
560 }
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
561
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
562 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
563 file_read(PyFileObject *f, PyObject *args)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
564 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
565 long bytesrequested = -1;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
566 size_t bytesread, buffersize, chunksize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
567 PyObject *v;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
568
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
569 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
570 return err_closed();
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
571 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
572 return NULL;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
573 if (bytesrequested < 0)
10562
a42824cf0b18 casts for picky compilers.
Guido van Rossum <guido@python.org>
parents: 9883
diff changeset
574 buffersize = new_buffersize(f, (size_t)0);
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
575 else
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
576 buffersize = bytesrequested;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
577 if (buffersize > INT_MAX) {
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
578 PyErr_SetString(PyExc_OverflowError,
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
579 "requested number of bytes is more than a Python string can hold");
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
580 return NULL;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
581 }
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
582 v = PyString_FromStringAndSize((char *)NULL, buffersize);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
583 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 return NULL;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
585 bytesread = 0;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
586 for (;;) {
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
587 Py_BEGIN_ALLOW_THREADS
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
588 errno = 0;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
589 chunksize = fread(BUF(v) + bytesread, 1,
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
590 buffersize - bytesread, f->f_fp);
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
591 Py_END_ALLOW_THREADS
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
592 if (chunksize == 0) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
593 if (!ferror(f->f_fp))
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
594 break;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
595 PyErr_SetFromErrno(PyExc_IOError);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
596 clearerr(f->f_fp);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
597 Py_DECREF(v);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
598 return NULL;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
599 }
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
600 bytesread += chunksize;
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
601 if (bytesread < buffersize)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
602 break;
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
603 if (bytesrequested < 0) {
9633
e2dc3cb08c60 As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents: 8707
diff changeset
604 buffersize = new_buffersize(f, buffersize);
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
605 if (_PyString_Resize(&v, buffersize) < 0)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
606 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
607 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
608 }
5477
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
609 if (bytesread != buffersize)
f44a56e697fb Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents: 5414
diff changeset
610 _PyString_Resize(&v, bytesread);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 return v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
614 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
615 file_readinto(PyFileObject *f, PyObject *args)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
616 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
617 char *ptr;
20635
04f2801d9977 SF patch #474175 (Jay T Miller): file.readinto arg parsing bug
Guido van Rossum <guido@python.org>
parents: 20392
diff changeset
618 int ntodo;
04f2801d9977 SF patch #474175 (Jay T Miller): file.readinto arg parsing bug
Guido van Rossum <guido@python.org>
parents: 20392
diff changeset
619 size_t ndone, nnow;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
620
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
621 if (f->f_fp == NULL)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
622 return err_closed();
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
623 if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
624 return NULL;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
625 ndone = 0;
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
626 while (ntodo > 0) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
627 Py_BEGIN_ALLOW_THREADS
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
628 errno = 0;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
629 nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
630 Py_END_ALLOW_THREADS
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
631 if (nnow == 0) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
632 if (!ferror(f->f_fp))
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
633 break;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
634 PyErr_SetFromErrno(PyExc_IOError);
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
635 clearerr(f->f_fp);
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
636 return NULL;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
637 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
638 ndone += nnow;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
639 ntodo -= nnow;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
640 }
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
641 return PyInt_FromLong((long)ndone);
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
642 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
643
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
644 /**************************************************************************
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
645 Routine to get next line using platform fgets().
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
646
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
647 Under MSVC 6:
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
648
15758
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
649 + MS threadsafe getc is very slow (multiple layers of function calls before+
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
650 after each character, to lock+unlock the stream).
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
651 + The stream-locking functions are MS-internal -- can't access them from user
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
652 code.
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
653 + There's nothing Tim could find in the MS C or platform SDK libraries that
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
654 can worm around this.
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
655 + MS fgets locks/unlocks only once per line; it's the only hook we have.
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
656
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
657 So we use fgets for speed(!), despite that it's painful.
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
658
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
659 MS realloc is also slow.
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
660
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
661 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
662 have):
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
663 Linux a wash
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
664 Solaris a wash
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
665 Tru64 Unix getline_via_fgets significantly faster
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
666
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
667 CAUTION: The C std isn't clear about this: in those cases where fgets
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
668 writes something into the buffer, can it write into any position beyond the
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
669 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
670 known on which it does; and it would be a strange way to code fgets. Still,
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
671 getline_via_fgets may not work correctly if it does. The std test
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
672 test_bufio.py should fail if platform fgets() routinely writes beyond the
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
673 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
674 **************************************************************************/
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
675
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
676 /* Use this routine if told to, or by default on non-get_unlocked()
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
677 * platforms unless told not to. Yikes! Let's spell that out:
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
678 * On a platform with getc_unlocked():
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
679 * By default, use getc_unlocked().
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
680 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
681 * On a platform without getc_unlocked():
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
682 * By default, use fgets().
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
683 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
684 */
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
685 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
686 #define USE_FGETS_IN_GETLINE
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
687 #endif
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
688
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
689 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
690 #undef USE_FGETS_IN_GETLINE
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
691 #endif
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
692
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
693 #ifdef USE_FGETS_IN_GETLINE
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
694 static PyObject*
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
695 getline_via_fgets(FILE *fp)
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
696 {
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
697 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
698 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
699 * to fill this much of the buffer with a known value in order to figure out
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
700 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
701 * than "most" lines, we waste time filling unused buffer slots. 100 is
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
702 * surely adequate for most peoples' email archives, chewing over source code,
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
703 * etc -- "regular old text files".
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
704 * MAXBUFSIZE is the maximum line length that lets us get away with the less
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
705 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
706 * cautions about boosting that. 300 was chosen because the worst real-life
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
707 * text-crunching job reported on Python-Dev was a mail-log crawler where over
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
708 * half the lines were 254 chars.
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
709 * INCBUFSIZE is the amount by which we grow the buffer, if MAXBUFSIZE isn't
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
710 * enough. It doesn't much matter what this is set to: we only get here for
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
711 * absurdly long lines anyway.
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
712 */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
713 #define INITBUFSIZE 100
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
714 #define MAXBUFSIZE 300
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
715 #define INCBUFSIZE 1000
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
716 char* p; /* temp */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
717 char buf[MAXBUFSIZE];
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
718 PyObject* v; /* the string object result */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
719 char* pvfree; /* address of next free slot */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
720 char* pvend; /* address one beyond last free slot */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
721 size_t nfree; /* # of free buffer slots; pvend-pvfree */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
722 size_t total_v_size; /* total # of slots in buffer */
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
723
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
724 /* Optimize for normal case: avoid _PyString_Resize if at all
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
725 * possible via first reading into stack buffer "buf".
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
726 */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
727 total_v_size = INITBUFSIZE; /* start small and pray */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
728 pvfree = buf;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
729 for (;;) {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
730 Py_BEGIN_ALLOW_THREADS
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
731 pvend = buf + total_v_size;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
732 nfree = pvend - pvfree;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
733 memset(pvfree, '\n', nfree);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
734 p = fgets(pvfree, nfree, fp);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
735 Py_END_ALLOW_THREADS
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
736
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
737 if (p == NULL) {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
738 clearerr(fp);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
739 if (PyErr_CheckSignals())
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
740 return NULL;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
741 v = PyString_FromStringAndSize(buf, pvfree - buf);
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
742 return v;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
743 }
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
744 /* fgets read *something* */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
745 p = memchr(pvfree, '\n', nfree);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
746 if (p != NULL) {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
747 /* Did the \n come from fgets or from us?
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
748 * Since fgets stops at the first \n, and then writes
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
749 * \0, if it's from fgets a \0 must be next. But if
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
750 * that's so, it could not have come from us, since
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
751 * the \n's we filled the buffer with have only more
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
752 * \n's to the right.
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
753 */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
754 if (p+1 < pvend && *(p+1) == '\0') {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
755 /* It's from fgets: we win! In particular,
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
756 * we haven't done any mallocs yet, and can
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
757 * build the final result on the first try.
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
758 */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
759 ++p; /* include \n from fgets */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
760 }
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
761 else {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
762 /* Must be from us: fgets didn't fill the
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
763 * buffer and didn't find a newline, so it
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
764 * must be the last and newline-free line of
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
765 * the file.
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
766 */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
767 assert(p > pvfree && *(p-1) == '\0');
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
768 --p; /* don't include \0 from fgets */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
769 }
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
770 v = PyString_FromStringAndSize(buf, p - buf);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
771 return v;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
772 }
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
773 /* yuck: fgets overwrote all the newlines, i.e. the entire
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
774 * buffer. So this line isn't over yet, or maybe it is but
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
775 * we're exactly at EOF. If we haven't already, try using the
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
776 * rest of the stack buffer.
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
777 */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
778 assert(*(pvend-1) == '\0');
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
779 if (pvfree == buf) {
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
780 pvfree = pvend - 1; /* overwrite trailing null */
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
781 total_v_size = MAXBUFSIZE;
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
782 }
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
783 else
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
784 break;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
785 }
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
786
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
787 /* The stack buffer isn't big enough; malloc a string object and read
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
788 * into its buffer.
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
789 */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
790 total_v_size = MAXBUFSIZE + INCBUFSIZE;
15758
a1d88ab023a0 A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents: 15757
diff changeset
791 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
792 if (v == NULL)
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
793 return v;
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
794 /* copy over everything except the last null byte */
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
795 memcpy(BUF(v), buf, MAXBUFSIZE-1);
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
796 pvfree = BUF(v) + MAXBUFSIZE - 1;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
797
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
798 /* Keep reading stuff into v; if it ever ends successfully, break
15755
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
799 * after setting p one beyond the end of the line. The code here is
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
800 * very much like the code above, except reads into v's buffer; see
a43a8a1f46ee Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents: 15754
diff changeset
801 * the code above for detailed comments about the logic.
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
802 */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
803 for (;;) {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
804 Py_BEGIN_ALLOW_THREADS
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
805 pvend = BUF(v) + total_v_size;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
806 nfree = pvend - pvfree;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
807 memset(pvfree, '\n', nfree);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
808 p = fgets(pvfree, nfree, fp);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
809 Py_END_ALLOW_THREADS
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
810
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
811 if (p == NULL) {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
812 clearerr(fp);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
813 if (PyErr_CheckSignals()) {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
814 Py_DECREF(v);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
815 return NULL;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
816 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
817 p = pvfree;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
818 break;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
819 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
820 p = memchr(pvfree, '\n', nfree);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
821 if (p != NULL) {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
822 if (p+1 < pvend && *(p+1) == '\0') {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
823 /* \n came from fgets */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
824 ++p;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
825 break;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
826 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
827 /* \n came from us; last line of file, no newline */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
828 assert(p > pvfree && *(p-1) == '\0');
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
829 --p;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
830 break;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
831 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
832 /* expand buffer and try again */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
833 assert(*(pvend-1) == '\0');
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
834 total_v_size += INCBUFSIZE;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
835 if (total_v_size > INT_MAX) {
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
836 PyErr_SetString(PyExc_OverflowError,
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
837 "line is longer than a Python string can hold");
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
838 Py_DECREF(v);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
839 return NULL;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
840 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
841 if (_PyString_Resize(&v, (int)total_v_size) < 0)
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
842 return NULL;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
843 /* overwrite the trailing null byte */
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
844 pvfree = BUF(v) + (total_v_size - INCBUFSIZE - 1);
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
845 }
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
846 if (BUF(v) + total_v_size != p)
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
847 _PyString_Resize(&v, p - BUF(v));
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
848 return v;
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
849 #undef INITBUFSIZE
15876
a33de23b3066 Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents: 15875
diff changeset
850 #undef MAXBUFSIZE
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
851 #undef INCBUFSIZE
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
852 }
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
853 #endif /* ifdef USE_FGETS_IN_GETLINE */
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
854
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
855 /* Internal routine to get a line.
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
856 Size argument interpretation:
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
857 > 0: max length;
15757
a9e15bcab32c Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents: 15755
diff changeset
858 <= 0: read arbitrary line
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
859 */
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
860
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
861 #ifdef HAVE_GETC_UNLOCKED
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
862 #define GETC(f) getc_unlocked(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
863 #define FLOCKFILE(f) flockfile(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
864 #define FUNLOCKFILE(f) funlockfile(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
865 #else
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
866 #define GETC(f) getc(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
867 #define FLOCKFILE(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
868 #define FUNLOCKFILE(f)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
869 #endif
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
870
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
871 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
872 get_line(PyFileObject *f, int n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 {
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
874 FILE *fp = f->f_fp;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
875 int c;
15446
f813ede134bf Patch #102469: Use glibc's getline() extension when reading unbounded lines
Andrew M. Kuchling <amk@amk.ca>
parents: 15404
diff changeset
876 char *buf, *end;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
877 size_t n1, n2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
878 PyObject *v;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
879
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
880 #ifdef USE_FGETS_IN_GETLINE
15757
a9e15bcab32c Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents: 15755
diff changeset
881 if (n <= 0)
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
882 return getline_via_fgets(fp);
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
883 #endif
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
884 n2 = n > 0 ? n : 100;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
885 v = PyString_FromStringAndSize((char *)NULL, n2);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
886 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
888 buf = BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
889 end = buf + n2;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
890
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
891 for (;;) {
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
892 Py_BEGIN_ALLOW_THREADS
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
893 FLOCKFILE(fp);
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
894 while ((c = GETC(fp)) != EOF &&
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
895 (*buf++ = c) != '\n' &&
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
896 buf != end)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
897 ;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
898 FUNLOCKFILE(fp);
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
899 Py_END_ALLOW_THREADS
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
900 if (c == '\n')
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
901 break;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
902 if (c == EOF) {
19105
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
903 if (ferror(fp)) {
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
904 PyErr_SetFromErrno(PyExc_IOError);
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
905 clearerr(fp);
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
906 Py_DECREF(v);
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
907 return NULL;
eea8dfbbee86 Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents: 19015
diff changeset
908 }
251
48d58b74a3a4 Call clearerr() after EOF seen.
Guido van Rossum <guido@python.org>
parents: 183
diff changeset
909 clearerr(fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
910 if (PyErr_CheckSignals()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
911 Py_DECREF(v);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
912 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
913 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
914 break;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
915 }
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
916 /* Must be because buf == end */
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
917 if (n > 0)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
918 break;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
919 n1 = n2;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
920 n2 += 1000;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
921 if (n2 > INT_MAX) {
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
922 PyErr_SetString(PyExc_OverflowError,
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
923 "line is longer than a Python string can hold");
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
924 Py_DECREF(v);
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
925 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
926 }
15745
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
927 if (_PyString_Resize(&v, n2) < 0)
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
928 return NULL;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
929 buf = BUF(v) + n1;
f5c04134f4e2 Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents: 15633
diff changeset
930 end = BUF(v) + n2;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
932
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
933 n1 = buf - BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
934 if (n1 != n2)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
935 _PyString_Resize(&v, n1);
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
936 return v;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
937 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
938
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
939 /* External C interface */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
940
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
941 PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
942 PyFile_GetLine(PyObject *f, int n)
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
943 {
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
944 PyObject *result;
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
945
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
946 if (f == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
947 PyErr_BadInternalCall();
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
948 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
949 }
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
950
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
951 if (PyFile_Check(f)) {
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
952 if (((PyFileObject*)f)->f_fp == NULL)
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
953 return err_closed();
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
954 result = get_line((PyFileObject *)f, n);
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
955 }
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
956 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
957 PyObject *reader;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
958 PyObject *args;
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
959
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
960 reader = PyObject_GetAttrString(f, "readline");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
961 if (reader == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
962 return NULL;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
963 if (n <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
964 args = Py_BuildValue("()");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
965 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
966 args = Py_BuildValue("(i)", n);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
967 if (args == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
968 Py_DECREF(reader);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
969 return NULL;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
970 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
971 result = PyEval_CallObject(reader, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
972 Py_DECREF(reader);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
973 Py_DECREF(args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
974 if (result != NULL && !PyString_Check(result)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
975 Py_DECREF(result);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
976 result = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
977 PyErr_SetString(PyExc_TypeError,
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
978 "object.readline() returned non-string");
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
979 }
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
980 }
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
981
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
982 if (n < 0 && result != NULL && PyString_Check(result)) {
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
983 char *s = PyString_AS_STRING(result);
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
984 int len = PyString_GET_SIZE(result);
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
985 if (len == 0) {
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
986 Py_DECREF(result);
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
987 result = NULL;
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
988 PyErr_SetString(PyExc_EOFError,
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
989 "EOF when reading a line");
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
990 }
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
991 else if (s[len-1] == '\n') {
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
992 if (result->ob_refcnt == 1)
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
993 _PyString_Resize(&result, len-1);
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
994 else {
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
995 PyObject *v;
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
996 v = PyString_FromStringAndSize(s, len-1);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
997 Py_DECREF(result);
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
998 result = v;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
999 }
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1000 }
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1001 }
15753
8d00f7555eec Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents: 15745
diff changeset
1002 return result;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1003 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1004
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1005 /* Python method */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1006
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1007 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1008 file_readline(PyFileObject *f, PyObject *args)
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1009 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1010 int n = -1;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1011
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1012 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1013 return err_closed();
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
1014 if (!PyArg_ParseTuple(args, "|i:readline", &n))
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1015 return NULL;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1016 if (n == 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1017 return PyString_FromString("");
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1018 if (n < 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1019 n = 0;
13360
484bfde70dcc Fix to bug #389:
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
1020 return get_line(f, n);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1021 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1022
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1023 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19105
diff changeset
1024 file_xreadlines(PyFileObject *f)
15780
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1025 {
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1026 static PyObject* xreadlines_function = NULL;
15875
195557a0f050 Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents: 15780
diff changeset
1027
15780
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1028 if (!xreadlines_function) {
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1029 PyObject *xreadlines_module =
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1030 PyImport_ImportModule("xreadlines");
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1031 if(!xreadlines_module)
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1032 return NULL;
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1033
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1034 xreadlines_function = PyObject_GetAttrString(xreadlines_module,
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1035 "xreadlines");
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1036 Py_DECREF(xreadlines_module);
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1037 if(!xreadlines_function)
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1038 return NULL;
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1039 }
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1040 return PyObject_CallFunction(xreadlines_function, "(O)", f);
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1041 }
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1042
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1043 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1044 file_readlines(PyFileObject *f, PyObject *args)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1045 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1046 long sizehint = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1047 PyObject *list;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1048 PyObject *line;
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1049 char small_buffer[SMALLCHUNK];
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1050 char *buffer = small_buffer;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1051 size_t buffersize = SMALLCHUNK;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1052 PyObject *big_buffer = NULL;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1053 size_t nfilled = 0;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1054 size_t nread;
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1055 size_t totalread = 0;
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1056 char *p, *q, *end;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1057 int err;
20392
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1058 int shortread = 0;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1059
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1060 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1061 return err_closed();
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
1062 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
1063 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1064 if ((list = PyList_New(0)) == NULL)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1065 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1066 for (;;) {
20392
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1067 if (shortread)
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1068 nread = 0;
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1069 else {
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1070 Py_BEGIN_ALLOW_THREADS
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1071 errno = 0;
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1072 nread = fread(buffer+nfilled, 1,
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1073 buffersize-nfilled, f->f_fp);
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1074 Py_END_ALLOW_THREADS
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1075 shortread = (nread < buffersize-nfilled);
853c268bf247 Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents: 20365
diff changeset
1076 }
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1077 if (nread == 0) {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1078 sizehint = 0;
7398
7afc15f08d82 Check ferror(), not errno, for fread() error.
Guido van Rossum <guido@python.org>
parents: 6568
diff changeset
1079 if (!ferror(f->f_fp))
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1080 break;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1081 PyErr_SetFromErrno(PyExc_IOError);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1082 clearerr(f->f_fp);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1083 error:
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1084 Py_DECREF(list);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1085 list = NULL;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1086 goto cleanup;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1087 }
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1088 totalread += nread;
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1089 p = memchr(buffer+nfilled, '\n', nread);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1090 if (p == NULL) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1091 /* Need a larger buffer to fit this line */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1092 nfilled += nread;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1093 buffersize *= 2;
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
1094 if (buffersize > INT_MAX) {
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
1095 PyErr_SetString(PyExc_OverflowError,
15780
d646d6068924 Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents: 15767
diff changeset
1096 "line is longer than a Python string can hold");
13924
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
1097 goto error;
ddf302d69211 Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents: 13591
diff changeset
1098 }
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1099 if (big_buffer == NULL) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1100 /* Create the big buffer */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1101 big_buffer = PyString_FromStringAndSize(
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1102 NULL, buffersize);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1103 if (big_buffer == NULL)
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1104 goto error;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1105 buffer = PyString_AS_STRING(big_buffer);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1106 memcpy(buffer, small_buffer, nfilled);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1107 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1108 else {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1109 /* Grow the big buffer */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1110 _PyString_Resize(&big_buffer, buffersize);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1111 buffer = PyString_AS_STRING(big_buffer);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1112 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1113 continue;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1114 }
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1115 end = buffer+nfilled+nread;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1116 q = buffer;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1117 do {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1118 /* Process complete lines */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1119 p++;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1120 line = PyString_FromStringAndSize(q, p-q);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1121 if (line == NULL)
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1122 goto error;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1123 err = PyList_Append(list, line);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1124 Py_DECREF(line);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1125 if (err != 0)
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1126 goto error;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1127 q = p;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1128 p = memchr(q, '\n', end-q);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1129 } while (p != NULL);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1130 /* Move the remaining incomplete line to the start */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1131 nfilled = end-q;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1132 memmove(buffer, q, nfilled);
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1133 if (sizehint > 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1134 if (totalread >= (size_t)sizehint)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1135 break;
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1136 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1137 if (nfilled != 0) {
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1138 /* Partial last line */
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1139 line = PyString_FromStringAndSize(buffer, nfilled);
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1140 if (line == NULL)
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1141 goto error;
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1142 if (sizehint > 0) {
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1143 /* Need to complete the last line */
13360
484bfde70dcc Fix to bug #389:
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
1144 PyObject *rest = get_line(f, 0);
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1145 if (rest == NULL) {
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1146 Py_DECREF(line);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1147 goto error;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1148 }
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1149 PyString_Concat(&line, rest);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1150 Py_DECREF(rest);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1151 if (line == NULL)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1152 goto error;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
1153 }
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1154 err = PyList_Append(list, line);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1155 Py_DECREF(line);
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1156 if (err != 0)
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1157 goto error;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 }
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1159 cleanup:
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7719
diff changeset
1160 if (big_buffer) {
5478
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
1161 Py_DECREF(big_buffer);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7719
diff changeset
1162 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1163 return list;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1166 static PyObject *
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1167 file_write(PyFileObject *f, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1169 char *s;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 int n, n2;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1171 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1172 return err_closed();
20793
79fcbc31a03e fix for
Michael W. Hudson <mwh@python.net>
parents: 20635
diff changeset
1173 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 return NULL;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1175 f->f_softspace = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1176 Py_BEGIN_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 errno = 0;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
1178 n2 = fwrite(s, 1, n, f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1179 Py_END_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 if (n2 != n) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1181 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
1182 clearerr(f->f_fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1185 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1186 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1187 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1189 static PyObject *
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1190 file_writelines(PyFileObject *f, PyObject *seq)
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1191 {
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1192 #define CHUNKSIZE 1000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1193 PyObject *list, *line;
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1194 PyObject *it; /* iter(seq) */
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1195 PyObject *result;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1196 int i, j, index, len, nwritten, islist;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1197
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1198 assert(seq != NULL);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1199 if (f->f_fp == NULL)
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1200 return err_closed();
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1201
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1202 result = NULL;
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1203 list = NULL;
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1204 islist = PyList_Check(seq);
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1205 if (islist)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1206 it = NULL;
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1207 else {
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1208 it = PyObject_GetIter(seq);
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1209 if (it == NULL) {
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1210 PyErr_SetString(PyExc_TypeError,
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1211 "writelines() requires an iterable argument");
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1212 return NULL;
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1213 }
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1214 /* From here on, fail by going to error, to reclaim "it". */
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1215 list = PyList_New(CHUNKSIZE);
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1216 if (list == NULL)
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1217 goto error;
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1218 }
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1219
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1220 /* Strategy: slurp CHUNKSIZE lines into a private list,
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1221 checking that they are all strings, then write that list
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1222 without holding the interpreter lock, then come back for more. */
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1223 for (index = 0; ; index += CHUNKSIZE) {
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1224 if (islist) {
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1225 Py_XDECREF(list);
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1226 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1227 if (list == NULL)
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1228 goto error;
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1229 j = PyList_GET_SIZE(list);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1230 }
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1231 else {
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1232 for (j = 0; j < CHUNKSIZE; j++) {
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1233 line = PyIter_Next(it);
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1234 if (line == NULL) {
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1235 if (PyErr_Occurred())
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1236 goto error;
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1237 break;
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1238 }
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1239 PyList_SetItem(list, j, line);
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1240 }
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1241 }
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1242 if (j == 0)
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1243 break;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1244
14213
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1245 /* Check that all entries are indeed strings. If not,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1246 apply the same rules as for file.write() and
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1247 convert the results to strings. This is slow, but
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1248 seems to be the only way since all conversion APIs
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1249 could potentially execute Python code. */
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1250 for (i = 0; i < j; i++) {
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1251 PyObject *v = PyList_GET_ITEM(list, i);
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1252 if (!PyString_Check(v)) {
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1253 const char *buffer;
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1254 int len;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
1255 if (((f->f_binary &&
14213
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1256 PyObject_AsReadBuffer(v,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1257 (const void**)&buffer,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1258 &len)) ||
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1259 PyObject_AsCharBuffer(v,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1260 &buffer,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1261 &len))) {
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1262 PyErr_SetString(PyExc_TypeError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15068
diff changeset
1263 "writelines() argument must be a sequence of strings");
14213
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1264 goto error;
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1265 }
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1266 line = PyString_FromStringAndSize(buffer,
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1267 len);
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1268 if (line == NULL)
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1269 goto error;
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1270 Py_DECREF(v);
14215
0797fdb25a77 Fixed a serious typo.
Marc-André Lemburg <mal@egenix.com>
parents: 14213
diff changeset
1271 PyList_SET_ITEM(list, i, line);
14213
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1272 }
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1273 }
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1274
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1275 /* Since we are releasing the global lock, the
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1276 following code may *not* execute Python code. */
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1277 Py_BEGIN_ALLOW_THREADS
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1278 f->f_softspace = 0;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1279 errno = 0;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1280 for (i = 0; i < j; i++) {
14213
63ea2a2df06f Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents: 14125
diff changeset
1281 line = PyList_GET_ITEM(list, i);
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1282 len = PyString_GET_SIZE(line);
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1283 nwritten = fwrite(PyString_AS_STRING(line),
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1284 1, len, f->f_fp);
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1285 if (nwritten != len) {
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1286 Py_BLOCK_THREADS
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1287 PyErr_SetFromErrno(PyExc_IOError);
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1288 clearerr(f->f_fp);
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1289 goto error;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1290 }
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1291 }
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1292 Py_END_ALLOW_THREADS
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1293
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1294 if (j < CHUNKSIZE)
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1295 break;
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1296 }
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1297
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1298 Py_INCREF(Py_None);
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1299 result = Py_None;
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1300 error:
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1301 Py_XDECREF(list);
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1302 Py_XDECREF(it);
12000
8651f2da7c4c Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1303 return result;
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1304 #undef CHUNKSIZE
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1305 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1306
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1307 static char readline_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1308 "readline([size]) -> next line from the file, as a string.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1309 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1310 "Retain newline. A non-negative size argument limits the maximum\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1311 "number of bytes to return (an incomplete line may be returned then).\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1312 "Return an empty string at EOF.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1313
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1314 static char read_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1315 "read([size]) -> read at most size bytes, returned as a string.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1316 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1317 "If the size argument is negative or omitted, read until EOF is reached.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1318
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1319 static char write_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1320 "write(str) -> None. Write string str to file.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1321 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1322 "Note that due to buffering, flush() or close() may be needed before\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1323 "the file on disk reflects the data written.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1324
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1325 static char fileno_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1326 "fileno() -> integer \"file descriptor\".\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1327 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1328 "This is needed for lower-level file interfaces, such os.read().";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1329
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1330 static char seek_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1331 "seek(offset[, whence]) -> None. Move to new file position.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1332 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1333 "Argument offset is a byte count. Optional argument whence defaults to\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1334 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1335 "(move relative to current position, positive or negative), and 2 (move\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1336 "relative to end of file, usually negative, although many platforms allow\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1337 "seeking beyond the end of a file).\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1338 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1339 "Note that not all file objects are seekable.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1340
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
1341 #ifdef HAVE_FTRUNCATE
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1342 static char truncate_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1343 "truncate([size]) -> None. Truncate the file to at most size bytes.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1344 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1345 "Size defaults to the current file position, as returned by tell().";
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
1346 #endif
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1347
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1348 static char tell_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1349 "tell() -> current file position, an integer (may be a long integer).";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1350
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1351 static char readinto_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1352 "readinto() -> Undocumented. Don't use this; it may go away.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1353
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1354 static char readlines_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1355 "readlines([size]) -> list of strings, each a line from the file.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1356 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1357 "Call readline() repeatedly and return a list of the lines so read.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1358 "The optional size argument, if given, is an approximate bound on the\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1359 "total number of bytes in the lines returned.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1360
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1361 static char xreadlines_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1362 "xreadlines() -> next line from the file, as a string.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1363 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1364 "Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1365 "often quicker, due to reading ahead internally.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1366
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1367 static char writelines_doc[] =
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1368 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1369 "\n"
20045
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1370 "Note that newlines are not added. The sequence can be any iterable object\n"
7b8c11739714 Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents: 20015
diff changeset
1371 "producing strings. This is equivalent to calling write() for each string.";
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1372
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1373 static char flush_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1374 "flush() -> None. Flush the internal I/O buffer.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1375
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1376 static char close_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1377 "close() -> None or (perhaps) an integer. Close the file.\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1378 "\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1379 "Sets data attribute .closed to true. A closed file cannot be used for\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1380 "further I/O operations. close() may be called more than once without\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1381 "error. Some kinds of file objects (for example, opened by popen())\n"
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1382 "may return an exit status upon closing.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1383
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1384 static char isatty_doc[] =
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1385 "isatty() -> true or false. True if the file is connected to a tty device.";
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1386
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1387 static PyMethodDef file_methods[] = {
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1388 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1389 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
20793
79fcbc31a03e fix for
Michael W. Hudson <mwh@python.net>
parents: 20635
diff changeset
1390 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
19996
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1391 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1392 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1393 #ifdef HAVE_FTRUNCATE
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1394 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1395 #endif
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1396 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1397 {"readinto", (PyCFunction)file_readinto, METH_OLDARGS, readinto_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1398 {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1399 {"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1400 {"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1401 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1402 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
646e24728852 SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents: 19985
diff changeset
1403 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1407 #define OFF(x) offsetof(PyFileObject, x)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1408
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1409 static PyMemberDef file_memberlist[] = {
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1410 {"softspace", T_INT, OFF(f_softspace), 0,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1411 "flag indicating that a space needs to be printed; used by print"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1412 {"mode", T_OBJECT, OFF(f_mode), RO,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1413 "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"},
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1414 {"name", T_OBJECT, OFF(f_name), RO,
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19996
diff changeset
1415 "file name"},
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1416 /* getattr(f, "closed") is implemented without this table */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1417 {NULL} /* Sentinel */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1418 };
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1419
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1420 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1421 get_closed(PyFileObject *f, void *closure)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1423 return PyInt_FromLong((long)(f->f_fp == 0));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1424 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1425
20015
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
1426 static PyGetSetDef file_getsetlist[] = {
ca7af2a5d915 Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
1427 {"closed", (getter)get_closed, NULL, "flag set if the file is closed"},
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1428 {0},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1429 };
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1430
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1431 static PyObject *
17911
fbffb6de25dd file_getiter(): make iter(file) be equivalent to file.xreadlines().
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
1432 file_getiter(PyObject *f)
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1433 {
17911
fbffb6de25dd file_getiter(): make iter(file) be equivalent to file.xreadlines().
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
1434 return PyObject_CallMethod(f, "xreadlines", "");
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1435 }
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1436
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1437 static PyObject *
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1438 file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1439 {
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1440 PyObject *self;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1441 static PyObject *not_yet_string;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1442
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1443 assert(type != NULL && type->tp_alloc != NULL);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1444
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1445 if (not_yet_string == NULL) {
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1446 not_yet_string = PyString_FromString("<uninitialized file>");
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1447 if (not_yet_string == NULL)
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1448 return NULL;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1449 }
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1450
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1451 self = type->tp_alloc(type, 0);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1452 if (self != NULL) {
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1453 /* Always fill in the name and mode, so that nobody else
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1454 needs to special-case NULLs there. */
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1455 Py_INCREF(not_yet_string);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1456 ((PyFileObject *)self)->f_name = not_yet_string;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1457 Py_INCREF(not_yet_string);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1458 ((PyFileObject *)self)->f_mode = not_yet_string;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1459 }
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1460 return self;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1461 }
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1462
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1463 static int
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1464 file_init(PyObject *self, PyObject *args, PyObject *kwds)
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1465 {
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1466 PyFileObject *foself = (PyFileObject *)self;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1467 int ret = 0;
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1468 static char *kwlist[] = {"name", "mode", "buffering", 0};
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1469 char *name = NULL;
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1470 char *mode = "r";
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1471 int bufsize = -1;
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1472
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1473 assert(PyFile_Check(self));
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1474 if (foself->f_fp != NULL) {
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1475 /* Have to close the existing file first. */
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1476 PyObject *closeresult = file_close(foself);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1477 if (closeresult == NULL)
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1478 return -1;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1479 Py_DECREF(closeresult);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1480 }
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1481
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1482 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1483 Py_FileSystemDefaultEncoding, &name,
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1484 &mode, &bufsize))
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1485 return -1;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1486 if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL)
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1487 goto Error;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1488 if (open_the_file(foself, name, mode) == NULL)
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1489 goto Error;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1490 PyFile_SetBufSize(self, bufsize);
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1491 goto Done;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1492
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1493 Error:
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1494 ret = -1;
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1495 /* fall through */
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1496 Done:
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1497 PyMem_Free(name); /* free the encoded string */
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1498 return ret;
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1499 }
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1500
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1501 static char file_doc[] =
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1502 "file(name[, mode[, buffering]]) -> file object\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1503 "\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1504 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1505 "writing or appending. The file will be created if it doesn't exist\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1506 "when opened for writing or appending; it will be truncated when\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1507 "opened for writing. Add a 'b' to the mode for binary files.\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1508 "Add a '+' to the mode to allow simultaneous reading and writing.\n"
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1509 "If the buffering argument is given, 0 means unbuffered, 1 means line\n"
19903
c082971823ce Get rid of builtin_open() entirely (the C code and docstring, not the
Tim Peters <tim.peters@gmail.com>
parents: 19901
diff changeset
1510 "buffered, and larger numbers specify the buffer size.\n"
c082971823ce Get rid of builtin_open() entirely (the C code and docstring, not the
Tim Peters <tim.peters@gmail.com>
parents: 19901
diff changeset
1511 "Note: open() is an alias for file().\n";
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1512
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1513 PyTypeObject PyFile_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1514 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1516 "file",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1517 sizeof(PyFileObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1518 0,
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1519 (destructor)file_dealloc, /* tp_dealloc */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1520 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1521 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1522 0, /* tp_setattr */
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1523 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1524 (reprfunc)file_repr, /* tp_repr */
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1525 0, /* tp_as_number */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1526 0, /* tp_as_sequence */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1527 0, /* tp_as_mapping */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1528 0, /* tp_hash */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1529 0, /* tp_call */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1530 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1531 PyObject_GenericGetAttr, /* tp_getattro */
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1532 0, /* tp_setattro */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1533 0, /* tp_as_buffer */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20045
diff changeset
1534 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1535 file_doc, /* tp_doc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1536 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1537 0, /* tp_clear */
17609
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1538 0, /* tp_richcompare */
79f0cd3a71a1 Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
1539 0, /* tp_weaklistoffset */
17911
fbffb6de25dd file_getiter(): make iter(file) be equivalent to file.xreadlines().
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
1540 file_getiter, /* tp_iter */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17609
diff changeset
1541 0, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1542 file_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1543 file_memberlist, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1544 file_getsetlist, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1545 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1546 0, /* tp_dict */
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1547 0, /* tp_descr_get */
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1548 0, /* tp_descr_set */
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1549 0, /* tp_dictoffset */
19905
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1550 (initproc)file_init, /* tp_init */
a7ba00e3b0de The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19903
diff changeset
1551 PyType_GenericAlloc, /* tp_alloc */
19894
51cc82605d32 SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents: 19836
diff changeset
1552 file_new, /* tp_new */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20045
diff changeset
1553 _PyObject_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1554 };
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1555
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1556 /* Interface for the 'soft space' between print items. */
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1557
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1558 int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1559 PyFile_SoftSpace(PyObject *f, int newflag)
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1560 {
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1561 int oldflag = 0;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1562 if (f == NULL) {
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1563 /* Do nothing */
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1564 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1565 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1566 oldflag = ((PyFileObject *)f)->f_softspace;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1567 ((PyFileObject *)f)->f_softspace = newflag;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1568 }
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1569 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1570 PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1571 v = PyObject_GetAttrString(f, "softspace");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1572 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1573 PyErr_Clear();
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1574 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1575 if (PyInt_Check(v))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1576 oldflag = PyInt_AsLong(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1577 Py_DECREF(v);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1578 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1579 v = PyInt_FromLong((long)newflag);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1580 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1581 PyErr_Clear();
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1582 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1583 if (PyObject_SetAttrString(f, "softspace", v) != 0)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1584 PyErr_Clear();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1585 Py_DECREF(v);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1586 }
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1587 }
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1588 return oldflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
1589 }
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1590
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1591 /* Interfaces to write objects/strings to file-like objects */
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1592
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1593 int
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1594 PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1595 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1596 PyObject *writer, *value, *args, *result;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1597 if (f == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1598 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1599 return -1;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1600 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1601 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1602 FILE *fp = PyFile_AsFile(f);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1603 if (fp == NULL) {
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1604 err_closed();
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1605 return -1;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1606 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1607 return PyObject_Print(v, fp, flags);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1608 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1609 writer = PyObject_GetAttrString(f, "write");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1610 if (writer == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1611 return -1;
19985
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1612 if (flags & Py_PRINT_RAW) {
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1613 if (PyUnicode_Check(v)) {
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1614 value = v;
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1615 Py_INCREF(value);
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1616 } else
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1617 value = PyObject_Str(v);
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1618 }
712afdae1123 Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents: 19905
diff changeset
1619 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1620 value = PyObject_Repr(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
1621 if (value == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1622 Py_DECREF(writer);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
1623 return -1;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1624 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1625 args = Py_BuildValue("(O)", value);
5582
87d5d7fa8d07 Fix typo in error checking spotted by Just...
Guido van Rossum <guido@python.org>
parents: 5479
diff changeset
1626 if (args == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1627 Py_DECREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1628 Py_DECREF(writer);
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
1629 return -1;
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
1630 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1631 result = PyEval_CallObject(writer, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1632 Py_DECREF(args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1633 Py_DECREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1634 Py_DECREF(writer);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1635 if (result == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1636 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1637 Py_DECREF(result);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1638 return 0;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1639 }
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1640
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1641 int
21095
b1280cba79b5 PyFile_WriteString(): change prototype so that the string arg is
Tim Peters <tim.peters@gmail.com>
parents: 20942
diff changeset
1642 PyFile_WriteString(const char *s, PyObject *f)
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1643 {
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1644 if (f == NULL) {
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1645 /* Should be caused by a pre-existing error */
13454
fcc75a3748f5 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1646 if (!PyErr_Occurred())
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1647 PyErr_SetString(PyExc_SystemError,
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1648 "null file for PyFile_WriteString");
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1649 return -1;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1650 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1651 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1652 FILE *fp = PyFile_AsFile(f);
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1653 if (fp == NULL) {
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1654 err_closed();
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1655 return -1;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1656 }
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1657 fputs(s, fp);
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1658 return 0;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1659 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1660 else if (!PyErr_Occurred()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1661 PyObject *v = PyString_FromString(s);
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1662 int err;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1663 if (v == NULL)
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1664 return -1;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1665 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1666 Py_DECREF(v);
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1667 return err;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1668 }
5783
9cfbc9bb6423 Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5605
diff changeset
1669 else
9cfbc9bb6423 Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5605
diff changeset
1670 return -1;
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1671 }
13591
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1672
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1673 /* Try to get a file-descriptor from a Python object. If the object
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1674 is an integer or long integer, its value is returned. If not, the
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1675 object's fileno() method is called if it exists; the method must return
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1676 an integer or long integer, which is returned as the file descriptor value.
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1677 -1 is returned on failure.
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1678 */
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1679
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1680 int PyObject_AsFileDescriptor(PyObject *o)
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1681 {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1682 int fd;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1683 PyObject *meth;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1684
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1685 if (PyInt_Check(o)) {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1686 fd = PyInt_AsLong(o);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1687 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1688 else if (PyLong_Check(o)) {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1689 fd = PyLong_AsLong(o);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1690 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1691 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1692 {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1693 PyObject *fno = PyEval_CallObject(meth, NULL);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1694 Py_DECREF(meth);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1695 if (fno == NULL)
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1696 return -1;
15754
7c8fac4ac31a MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents: 15753
diff changeset
1697
13591
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1698 if (PyInt_Check(fno)) {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1699 fd = PyInt_AsLong(fno);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1700 Py_DECREF(fno);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1701 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1702 else if (PyLong_Check(fno)) {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1703 fd = PyLong_AsLong(fno);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1704 Py_DECREF(fno);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1705 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1706 else {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1707 PyErr_SetString(PyExc_TypeError,
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1708 "fileno() returned a non-integer");
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1709 Py_DECREF(fno);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1710 return -1;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1711 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1712 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1713 else {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1714 PyErr_SetString(PyExc_TypeError,
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1715 "argument must be an int, or have a fileno() method.");
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1716 return -1;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1717 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1718
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1719 if (fd < 0) {
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1720 PyErr_Format(PyExc_ValueError,
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1721 "file descriptor cannot be a negative integer (%i)",
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1722 fd);
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1723 return -1;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1724 }
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1725 return fd;
2077bf141b37 Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents: 13454
diff changeset
1726 }