Mercurial > cpython-lru_cache-weakref
annotate Objects/fileobject.c @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | e86875f069b1 |
| children | f939ce52a734 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
138
diff
changeset
|
1 |
| 6 | 2 /* File object implementation */ |
| 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 | 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 | 11 #ifdef HAVE_UNISTD_H |
| 12 #include <unistd.h> | |
| 13 #endif | |
| 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 | 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 | 39 typedef struct { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
40 PyObject_HEAD |
| 6 | 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 | 49 |
| 50 FILE * | |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
51 PyFile_AsFile(PyObject *f) |
| 6 | 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 | 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 | 57 } |
| 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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
68 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
69 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) |
| 6 | 70 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
71 PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type); |
| 6 | 72 if (f == NULL) |
| 73 return NULL; | |
| 74 f->f_fp = NULL; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
75 f->f_name = PyString_FromString(name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
76 f->f_mode = PyString_FromString(mode); |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
77 f->f_close = close; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
78 f->f_softspace = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
79 if (strchr(mode,'b') != NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
80 f->f_binary = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
81 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
82 f->f_binary = 0; |
| 6 | 83 if (f->f_name == NULL || f->f_mode == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
84 Py_DECREF(f); |
| 6 | 85 return NULL; |
| 86 } | |
| 87 f->f_fp = fp; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
88 return (PyObject *) f; |
| 6 | 89 } |
| 90 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
91 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
92 PyFile_FromString(char *name, char *mode) |
| 6 | 93 { |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13360
diff
changeset
|
94 extern int fclose(FILE *); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
95 PyFileObject *f; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
96 f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
| 6 | 97 if (f == NULL) |
| 98 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
|
99 #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
|
100 if (*mode == '*') { |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
101 FILE *fopenRF(); |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
102 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
|
103 } |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
104 else |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
105 #endif |
| 926 | 106 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
107 Py_BEGIN_ALLOW_THREADS |
| 926 | 108 f->f_fp = fopen(name, mode); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
109 Py_END_ALLOW_THREADS |
| 926 | 110 } |
|
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 (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
|
112 #ifdef NO_FOPEN_ERRNO |
|
8707
4edf3495c692
PyFile_FromString(): If an exception occurs, pass in the filename that
Barry Warsaw <barry@python.org>
parents:
8217
diff
changeset
|
113 /* Metroworks only, not testable, so unchanged */ |
|
2667
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
114 if ( errno == 0 ) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
115 PyErr_SetString(PyExc_IOError, "Cannot open file"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
116 Py_DECREF(f); |
|
2667
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
117 return NULL; |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
118 } |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
119 #endif |
|
8707
4edf3495c692
PyFile_FromString(): If an exception occurs, pass in the filename that
Barry Warsaw <barry@python.org>
parents:
8217
diff
changeset
|
120 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
121 Py_DECREF(f); |
| 6 | 122 return NULL; |
| 123 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
124 return (PyObject *)f; |
| 6 | 125 } |
| 126 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
127 void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
128 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
|
129 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
130 if (bufsize >= 0) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
131 #ifdef HAVE_SETVBUF |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
132 int type; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
133 switch (bufsize) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
134 case 0: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
135 type = _IONBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
136 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
137 case 1: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
138 type = _IOLBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
139 bufsize = BUFSIZ; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
140 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
141 default: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
142 type = _IOFBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
143 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
144 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
145 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
|
146 #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
|
147 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
|
148 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
|
149 #endif /* !HAVE_SETVBUF */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
150 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
151 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
152 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
153 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
154 err_closed(void) |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
155 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
156 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
|
157 return NULL; |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
158 } |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
159 |
| 6 | 160 /* Methods */ |
| 161 | |
| 162 static void | |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
163 file_dealloc(PyFileObject *f) |
| 6 | 164 { |
| 926 | 165 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
|
166 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
|
167 (*f->f_close)(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
168 Py_END_ALLOW_THREADS |
| 926 | 169 } |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
170 if (f->f_name != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
171 Py_DECREF(f->f_name); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
172 } |
|
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
173 if (f->f_mode != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
174 Py_DECREF(f->f_mode); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
175 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12000
diff
changeset
|
176 PyObject_DEL(f); |
| 6 | 177 } |
| 178 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
179 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
180 file_repr(PyFileObject *f) |
| 6 | 181 { |
| 182 char buf[300]; | |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13076
diff
changeset
|
183 sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>", |
| 6 | 184 f->f_fp == NULL ? "closed" : "open", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
185 PyString_AsString(f->f_name), |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
186 PyString_AsString(f->f_mode), |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
13076
diff
changeset
|
187 f); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
188 return PyString_FromString(buf); |
| 6 | 189 } |
| 190 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
191 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
192 file_close(PyFileObject *f, PyObject *args) |
| 6 | 193 { |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
194 int sts = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
195 if (!PyArg_NoArgs(args)) |
| 6 | 196 return NULL; |
| 197 if (f->f_fp != NULL) { | |
| 926 | 198 if (f->f_close != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
199 Py_BEGIN_ALLOW_THREADS |
| 926 | 200 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
|
201 sts = (*f->f_close)(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
202 Py_END_ALLOW_THREADS |
| 926 | 203 } |
| 6 | 204 f->f_fp = NULL; |
| 205 } | |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
206 if (sts == EOF) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
207 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
|
208 if (sts != 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
209 return PyInt_FromLong((long)sts); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
210 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
211 return Py_None; |
| 6 | 212 } |
| 213 | |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
214 |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
215 /* An 8-byte off_t-like type */ |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
216 #if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
217 typedef fpos_t Py_off_t; |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
218 #else |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
219 typedef off_t Py_off_t; |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
220 #endif |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
221 |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
222 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
223 /* 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
|
224 return 0 on success, non-zero on failure (with errno set) */ |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
225 int |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
226 _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
|
227 { |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
228 #if defined(HAVE_FSEEKO) |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
229 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
|
230 #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
|
231 return fseek64(fp, offset, whence); |
| 15068 | 232 #elif defined(__BEOS__) |
| 233 return _fseek(fp, offset, whence); | |
|
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
|
234 #elif defined(HAVE_LARGEFILE_SUPPORT) && 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
|
235 /* 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
|
236 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
|
237 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
|
238 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
|
239 case SEEK_END: |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
240 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
|
241 return -1; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
242 /* fall through */ |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
243 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
|
244 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
|
245 return -1; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
246 offset += pos; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
247 break; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
248 /* 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
|
249 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
250 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
|
251 #else |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
252 return fseek(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
|
253 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
254 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
255 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
256 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
257 /* 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
|
258 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
|
259 position on success */ |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
260 Py_off_t |
|
14277
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
14215
diff
changeset
|
261 _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
|
262 { |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
263 #if SIZEOF_FPOS_T >= 8 && 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
|
264 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
|
265 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
|
266 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
|
267 return pos; |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
268 #elif defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT) |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
269 return ftello(fp); |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
270 #elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT) |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
271 return ftell64(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
|
272 #else |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
273 return ftell(fp); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
274 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
275 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
276 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
277 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
278 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
279 file_seek(PyFileObject *f, PyObject *args) |
| 6 | 280 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
281 int whence; |
| 926 | 282 int ret; |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
283 Py_off_t offset; |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
284 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
|
285 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
286 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
287 return err_closed(); |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
288 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
|
289 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
|
290 return NULL; |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
291 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
292 offset = PyInt_AsLong(offobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
293 #else |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
294 offset = PyLong_Check(offobj) ? |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
295 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
|
296 #endif |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
297 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
|
298 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
|
299 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
300 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
|
301 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
|
302 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
|
303 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
|
304 |
| 926 | 305 if (ret != 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
306 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
307 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
308 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
309 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
310 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
311 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
312 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
313 |
|
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 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
315 #ifdef HAVE_FTRUNCATE |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
316 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
317 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
|
318 { |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
319 int ret; |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
320 Py_off_t newsize; |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
321 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
|
322 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
323 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
|
324 return err_closed(); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
325 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
|
326 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
|
327 return NULL; |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
328 if (newsizeobj != NULL) { |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
329 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
330 newsize = PyInt_AsLong(newsizeobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
331 #else |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
332 newsize = PyLong_Check(newsizeobj) ? |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
333 PyLong_AsLongLong(newsizeobj) : |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
334 PyInt_AsLong(newsizeobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
335 #endif |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
336 if (PyErr_Occurred()) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
337 return NULL; |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
338 } else { |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
339 /* Default to current position*/ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
340 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
|
341 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
|
342 newsize = _portable_ftell(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
343 Py_END_ALLOW_THREADS |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
344 if (newsize == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 } |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
349 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
350 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
|
351 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
|
352 ret = fflush(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
353 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
|
354 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
|
355 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
356 #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
|
357 /* 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
|
358 _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
|
359 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
|
360 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
|
361 "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
|
362 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
|
363 } else { |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
364 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
|
365 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
|
366 ret = _chsize(fileno(f->f_fp), newsize); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
367 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
|
368 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
|
369 } |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
370 #else |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 #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
|
377 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
378 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
379 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
|
380 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
381 onioerror: |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 } |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
386 #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
|
387 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
388 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
389 file_tell(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
|
390 { |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
391 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
|
392 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
393 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
394 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
395 if (!PyArg_NoArgs(args)) |
| 6 | 396 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
397 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
|
398 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
|
399 pos = _portable_ftell(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
400 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
|
401 if (pos == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
402 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
403 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
404 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
405 } |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
406 #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
|
407 return PyInt_FromLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
408 #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
|
409 return PyLong_FromLongLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
410 #endif |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
411 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
412 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
413 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
414 file_fileno(PyFileObject *f, PyObject *args) |
|
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
|
415 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
416 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
417 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
418 if (!PyArg_NoArgs(args)) |
|
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
|
419 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
420 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
|
421 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
422 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
423 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
424 file_flush(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
|
425 { |
| 926 | 426 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
|
427 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
428 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
429 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
430 if (!PyArg_NoArgs(args)) |
| 6 | 431 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
432 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
|
433 errno = 0; |
| 926 | 434 res = fflush(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
435 Py_END_ALLOW_THREADS |
| 926 | 436 if (res != 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
437 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
438 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
439 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
440 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
441 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
442 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
443 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
444 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
445 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
446 file_isatty(PyFileObject *f, PyObject *args) |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
447 { |
| 926 | 448 long res; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
449 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
450 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
451 if (!PyArg_NoArgs(args)) |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
452 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
453 Py_BEGIN_ALLOW_THREADS |
| 926 | 454 res = isatty((int)fileno(f->f_fp)); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
455 Py_END_ALLOW_THREADS |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
456 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
|
457 } |
|
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
458 |
|
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
|
459 |
|
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
|
460 #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
|
461 #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
|
462 #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
|
463 #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
|
464 #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
|
465 |
|
9883
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
466 #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
|
467 #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
|
468 #else |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
469 #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
|
470 #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
|
471 |
|
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
|
472 static size_t |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
473 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
|
474 { |
|
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
|
475 #ifdef HAVE_FSTAT |
|
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
|
476 long pos, end; |
|
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
|
477 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
|
478 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
|
479 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
|
480 /* 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
|
481 *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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 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
|
488 (Yet another reason why stdio stinks. :-) */ |
|
8217
a19661076529
Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents:
8180
diff
changeset
|
489 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
|
a19661076529
Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents:
8180
diff
changeset
|
490 if (pos >= 0) |
|
a19661076529
Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents:
8180
diff
changeset
|
491 pos = ftell(f->f_fp); |
|
8160
ce9fc64528a7
Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
492 if (pos < 0) |
|
ce9fc64528a7
Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
493 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
|
494 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
|
495 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
|
496 /* 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
|
497 } |
|
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
|
498 #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
|
499 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
|
500 /* 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
|
501 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
|
502 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
|
503 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
|
504 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
|
505 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
|
506 } |
|
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 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
|
508 } |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
510 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
511 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
|
512 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
513 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
|
514 size_t bytesread, buffersize, chunksize; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
515 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
|
516 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
517 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
518 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
|
519 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
|
520 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
|
521 if (bytesrequested < 0) |
|
10562
a42824cf0b18
casts for picky compilers.
Guido van Rossum <guido@python.org>
parents:
9883
diff
changeset
|
522 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
|
523 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
|
524 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
|
525 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
|
526 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
|
527 "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
|
528 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
|
529 } |
|
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
|
530 v = PyString_FromStringAndSize((char *)NULL, buffersize); |
| 86 | 531 if (v == NULL) |
| 6 | 532 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
|
533 bytesread = 0; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 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
|
543 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
|
544 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
|
545 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
|
546 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
|
547 } |
|
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
|
548 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
|
549 if (bytesread < buffersize) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
550 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
|
551 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
|
552 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
|
553 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
|
554 return NULL; |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
555 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
556 } |
|
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
|
557 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
|
558 _PyString_Resize(&v, bytesread); |
| 6 | 559 return v; |
| 560 } | |
| 561 | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
562 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
563 file_readinto(PyFileObject *f, PyObject *args) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
564 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
565 char *ptr; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
566 size_t ntodo, 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
|
567 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
568 if (f->f_fp == NULL) |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
569 return err_closed(); |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
570 if (!PyArg_Parse(args, "w#", &ptr, &ntodo)) |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
571 return NULL; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
572 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
|
573 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
|
574 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
|
575 errno = 0; |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 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
|
580 break; |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
581 PyErr_SetFromErrno(PyExc_IOError); |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
582 clearerr(f->f_fp); |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
583 return NULL; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
584 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
585 ndone += nnow; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
586 ntodo -= nnow; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
587 } |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
588 return PyInt_FromLong((long)ndone); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
589 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
590 |
|
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
|
591 /************************************************************************** |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
592 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
|
593 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
594 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
|
595 |
|
15758
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
596 + 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
|
597 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
|
598 + 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
|
599 code. |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
600 + 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
|
601 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
|
602 + 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
|
603 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
604 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
|
605 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
606 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
|
607 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
608 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
|
609 have): |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
610 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
|
611 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
|
612 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
|
613 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
614 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 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
|
620 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
|
621 **************************************************************************/ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
622 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
623 /* 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
|
624 * 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
|
625 * 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
|
626 * 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
|
627 * 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
|
628 * 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
|
629 * 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
|
630 * 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
|
631 */ |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
632 #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
|
633 #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
|
634 #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
|
635 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
636 #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
|
637 #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
|
638 #endif |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
639 |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
640 #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
|
641 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
|
642 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
|
643 { |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
644 /* 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
|
645 * 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
|
646 * 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
|
647 * 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
|
648 * 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
|
649 * 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
|
650 * 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
|
651 * 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
|
652 * 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
|
653 * 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
|
654 * 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
|
655 * 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
|
656 * 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
|
657 * 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
|
658 * 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
|
659 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
660 #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
|
661 #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
|
662 #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
|
663 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
|
664 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
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
671 /* 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
|
672 * 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
|
673 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
674 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
|
675 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
|
676 for (;;) { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
684 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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 } |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
691 /* 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
|
692 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
|
693 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
|
694 /* 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
|
695 * 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
|
696 * \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
|
697 * 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
|
698 * 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
|
699 * \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
|
700 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
701 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
|
702 /* 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
|
703 * 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
|
704 * 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
|
705 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
706 ++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
|
707 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
708 else { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
709 /* 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
|
710 * 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
|
711 * 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
|
712 * 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
|
713 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
714 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
|
715 --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
|
716 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
717 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
|
718 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
|
719 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
720 /* 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
|
721 * 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
|
722 * 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
|
723 * 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
|
724 */ |
|
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 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
|
726 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
|
727 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
|
728 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
|
729 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
730 else |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
731 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
|
732 } |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
733 |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
734 /* 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
|
735 * 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
|
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 total_v_size = MAXBUFSIZE + INCBUFSIZE; |
|
15758
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
738 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
|
739 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
|
740 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
|
741 /* 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
|
742 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
|
743 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
|
744 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
745 /* 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
|
746 * 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
|
747 * 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
|
748 * 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
|
749 */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
750 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
|
751 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
|
752 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
|
753 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
|
754 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
|
755 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
|
756 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
|
757 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
758 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
|
759 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
|
760 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
|
761 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
|
762 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
|
763 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
764 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
|
765 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
|
766 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
767 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
|
768 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
|
769 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
|
770 /* \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
|
771 ++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
|
772 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
|
773 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
774 /* \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
|
775 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
|
776 --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
|
777 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
|
778 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
779 /* 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
|
780 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
|
781 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
|
782 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
|
783 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
|
784 "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
|
785 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
|
786 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
|
787 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
788 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
|
789 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
|
790 /* 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
|
791 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
|
792 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
793 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
|
794 _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
|
795 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
|
796 #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
|
797 #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
|
798 #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
|
799 } |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
800 #endif /* ifdef USE_FGETS_IN_GETLINE */ |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
801 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
802 /* 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
|
803 Size argument interpretation: |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
804 > 0: max length; |
|
15757
a9e15bcab32c
Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents:
15755
diff
changeset
|
805 <= 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
|
806 */ |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
807 |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
808 #ifdef HAVE_GETC_UNLOCKED |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
809 #define GETC(f) getc_unlocked(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
810 #define FLOCKFILE(f) flockfile(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
811 #define FUNLOCKFILE(f) funlockfile(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
812 #else |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
813 #define GETC(f) getc(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
814 #define FLOCKFILE(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
815 #define FUNLOCKFILE(f) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
816 #endif |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
817 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
818 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
819 get_line(PyFileObject *f, int n) |
| 6 | 820 { |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
821 FILE *fp = f->f_fp; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
822 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
|
823 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
|
824 size_t n1, n2; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
825 PyObject *v; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
826 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
827 #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
|
828 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
|
829 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
|
830 #endif |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
831 n2 = n > 0 ? n : 100; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
832 v = PyString_FromStringAndSize((char *)NULL, n2); |
| 86 | 833 if (v == NULL) |
| 6 | 834 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
835 buf = BUF(v); |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
836 end = buf + n2; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
837 |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
838 for (;;) { |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
839 Py_BEGIN_ALLOW_THREADS |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
840 FLOCKFILE(fp); |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
841 while ((c = GETC(fp)) != EOF && |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
842 (*buf++ = c) != '\n' && |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
843 buf != end) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
844 ; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
845 FUNLOCKFILE(fp); |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
846 Py_END_ALLOW_THREADS |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
847 if (c == '\n') |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
848 break; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
849 if (c == EOF) { |
|
251
48d58b74a3a4
Call clearerr() after EOF seen.
Guido van Rossum <guido@python.org>
parents:
183
diff
changeset
|
850 clearerr(fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
851 if (PyErr_CheckSignals()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
852 Py_DECREF(v); |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
853 return NULL; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
854 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
855 break; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
856 } |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
857 /* Must be because buf == end */ |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
858 if (n > 0) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
859 break; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
860 n1 = n2; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
861 n2 += 1000; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
862 if (n2 > INT_MAX) { |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
863 PyErr_SetString(PyExc_OverflowError, |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
864 "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
|
865 Py_DECREF(v); |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
866 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
867 } |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
868 if (_PyString_Resize(&v, n2) < 0) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
869 return NULL; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
870 buf = BUF(v) + n1; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
871 end = BUF(v) + n2; |
| 6 | 872 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
873 |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
874 n1 = buf - BUF(v); |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
875 if (n1 != n2) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
876 _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
|
877 return v; |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
878 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
879 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
880 /* External C interface */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
881 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
882 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
883 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
|
884 { |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
885 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
|
886 |
|
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
|
887 if (f == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
888 PyErr_BadInternalCall(); |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
889 return NULL; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
890 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
891 |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
892 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
|
893 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
|
894 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
|
895 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
|
896 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
897 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
898 PyObject *reader; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
899 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
|
900 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
901 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
|
902 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
|
903 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
|
904 if (n <= 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
905 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
|
906 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
907 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
|
908 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
909 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
|
910 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
|
911 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
912 result = PyEval_CallObject(reader, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
913 Py_DECREF(reader); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
914 Py_DECREF(args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
915 if (result != NULL && !PyString_Check(result)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
916 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
|
917 result = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
918 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
|
919 "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
|
920 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
921 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
922 |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
923 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
|
924 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
|
925 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
|
926 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
|
927 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
|
928 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
|
929 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
|
930 "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
|
931 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
932 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
|
933 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
|
934 _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
|
935 else { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
936 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
|
937 v = PyString_FromStringAndSize(s, len-1); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
938 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
|
939 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
|
940 } |
|
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
|
941 } |
|
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
|
942 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
943 return result; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
944 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
945 |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
946 /* Python method */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
947 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
948 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
949 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
|
950 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
951 int n = -1; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
952 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
953 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
954 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
|
955 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
|
956 return NULL; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
957 if (n == 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
958 return PyString_FromString(""); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
959 if (n < 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
960 n = 0; |
| 13360 | 961 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
|
962 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
963 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
964 static PyObject * |
|
15780
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
965 file_xreadlines(PyFileObject *f, PyObject *args) |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
966 { |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
967 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
|
968 |
|
15780
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
969 if (!PyArg_ParseTuple(args, ":xreadlines")) |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
970 return 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
|
971 |
|
15780
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
972 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
|
973 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
|
974 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
|
975 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
|
976 return NULL; |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
977 |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
978 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
|
979 "xreadlines"); |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
980 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
|
981 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
|
982 return NULL; |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
983 } |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
984 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
|
985 } |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
986 |
|
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
987 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
988 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
|
989 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
990 long sizehint = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
991 PyObject *list; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
992 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
|
993 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
|
994 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
|
995 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
|
996 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
|
997 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
|
998 size_t nread; |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
999 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
|
1000 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
|
1001 int err; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1002 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1003 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1004 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
|
1005 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
|
1006 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1007 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
|
1008 return NULL; |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1009 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
|
1010 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
|
1011 errno = 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
|
1012 nread = fread(buffer+nfilled, 1, buffersize-nfilled, 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
|
1013 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
|
1014 if (nread == 0) { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1015 sizehint = 0; |
|
7398
7afc15f08d82
Check ferror(), not errno, for fread() error.
Guido van Rossum <guido@python.org>
parents:
6568
diff
changeset
|
1016 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
|
1017 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
|
1018 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
|
1019 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
|
1020 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
|
1021 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
|
1022 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
|
1023 goto cleanup; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1024 } |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1025 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
|
1026 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
|
1027 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
|
1028 /* 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
|
1029 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
|
1030 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
|
1031 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
|
1032 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
|
1033 "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
|
1034 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
|
1035 } |
|
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
|
1036 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
|
1037 /* 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
|
1038 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
|
1039 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
|
1040 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
|
1041 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
|
1042 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
|
1043 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
|
1044 } |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1045 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
|
1046 /* 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
|
1047 _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
|
1048 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
|
1049 } |
|
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 continue; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1051 } |
|
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
|
1052 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
|
1053 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
|
1054 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
|
1055 /* 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
|
1056 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
|
1057 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
|
1058 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
|
1059 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
|
1060 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
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 } 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
|
1067 /* 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
|
1068 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
|
1069 memmove(buffer, q, nfilled); |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1070 if (sizehint > 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1071 if (totalread >= (size_t)sizehint) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1072 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
|
1073 } |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1074 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
|
1075 /* 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
|
1076 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
|
1077 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
|
1078 goto error; |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1079 if (sizehint > 0) { |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1080 /* Need to complete the last line */ |
| 13360 | 1081 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
|
1082 if (rest == NULL) { |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1083 Py_DECREF(line); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1084 goto error; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1085 } |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1086 PyString_Concat(&line, rest); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1087 Py_DECREF(rest); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1088 if (line == NULL) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1089 goto error; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1090 } |
|
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
|
1091 err = PyList_Append(list, line); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1092 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
|
1093 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
|
1094 goto error; |
| 6 | 1095 } |
|
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
|
1096 cleanup: |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
1097 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
|
1098 Py_DECREF(big_buffer); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
7719
diff
changeset
|
1099 } |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1100 return list; |
| 6 | 1101 } |
| 1102 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1103 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1104 file_write(PyFileObject *f, PyObject *args) |
| 6 | 1105 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1106 char *s; |
| 6 | 1107 int n, n2; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1108 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1109 return err_closed(); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
1110 if (!PyArg_Parse(args, f->f_binary ? "s#" : "t#", &s, &n)) |
| 6 | 1111 return NULL; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1112 f->f_softspace = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1113 Py_BEGIN_ALLOW_THREADS |
| 6 | 1114 errno = 0; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1115 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
|
1116 Py_END_ALLOW_THREADS |
| 6 | 1117 if (n2 != n) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1118 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
1119 clearerr(f->f_fp); |
| 6 | 1120 return NULL; |
| 1121 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1122 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1123 return Py_None; |
| 6 | 1124 } |
| 1125 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1126 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1127 file_writelines(PyFileObject *f, PyObject *args) |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1128 { |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1129 #define CHUNKSIZE 1000 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1130 PyObject *list, *line; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1131 PyObject *result; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1132 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
|
1133 |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1134 if (f->f_fp == NULL) |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1135 return err_closed(); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1136 if (args == NULL || !PySequence_Check(args)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1137 PyErr_SetString(PyExc_TypeError, |
| 15318 | 1138 "writelines() argument must be a sequence of strings"); |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1139 return NULL; |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1140 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1141 islist = PyList_Check(args); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1142 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1143 /* 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
|
1144 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
|
1145 without holding the interpreter lock, then come back for more. */ |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1146 index = 0; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1147 if (islist) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1148 list = NULL; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1149 else { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1150 list = PyList_New(CHUNKSIZE); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1151 if (list == NULL) |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1152 return NULL; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1153 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1154 result = NULL; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1155 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1156 for (;;) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1157 if (islist) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1158 Py_XDECREF(list); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1159 list = PyList_GetSlice(args, index, index+CHUNKSIZE); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1160 if (list == NULL) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1161 return NULL; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1162 j = PyList_GET_SIZE(list); |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1163 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1164 else { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1165 for (j = 0; j < CHUNKSIZE; j++) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1166 line = PySequence_GetItem(args, index+j); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1167 if (line == NULL) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1168 if (PyErr_ExceptionMatches( |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1169 PyExc_IndexError)) { |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1170 PyErr_Clear(); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1171 break; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1172 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1173 /* Some other error occurred. |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1174 XXX We may lose some output. */ |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1175 goto error; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1176 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1177 PyList_SetItem(list, j, line); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1178 } |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1179 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1180 if (j == 0) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1181 break; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1182 |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1183 /* 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
|
1184 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
|
1185 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
|
1186 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
|
1187 could potentially execute Python code. */ |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1188 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
|
1189 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
|
1190 if (!PyString_Check(v)) { |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1191 const char *buffer; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1192 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
|
1193 if (((f->f_binary && |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1194 PyObject_AsReadBuffer(v, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1195 (const void**)&buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1196 &len)) || |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1197 PyObject_AsCharBuffer(v, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1198 &buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1199 &len))) { |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1200 PyErr_SetString(PyExc_TypeError, |
| 15318 | 1201 "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
|
1202 goto error; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1203 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1204 line = PyString_FromStringAndSize(buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1205 len); |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1206 if (line == NULL) |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1207 goto error; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1208 Py_DECREF(v); |
|
14215
0797fdb25a77
Fixed a serious typo.
Marc-André Lemburg <mal@egenix.com>
parents:
14213
diff
changeset
|
1209 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
|
1210 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1211 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1212 |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1213 /* 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
|
1214 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
|
1215 Py_BEGIN_ALLOW_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1216 f->f_softspace = 0; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1217 errno = 0; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1218 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
|
1219 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
|
1220 len = PyString_GET_SIZE(line); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1221 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
|
1222 1, len, f->f_fp); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1223 if (nwritten != len) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1224 Py_BLOCK_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1225 PyErr_SetFromErrno(PyExc_IOError); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1226 clearerr(f->f_fp); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1227 goto error; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1228 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1229 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1230 Py_END_ALLOW_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1231 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1232 if (j < CHUNKSIZE) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1233 break; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1234 index += CHUNKSIZE; |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1235 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1236 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1237 Py_INCREF(Py_None); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1238 result = Py_None; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1239 error: |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1240 Py_XDECREF(list); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1241 return result; |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1242 } |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1243 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1244 static PyMethodDef file_methods[] = { |
|
5783
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1245 {"readline", (PyCFunction)file_readline, 1}, |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1246 {"read", (PyCFunction)file_read, 1}, |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1247 {"write", (PyCFunction)file_write, 0}, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1248 {"fileno", (PyCFunction)file_fileno, 0}, |
|
9762
ed523ea073db
Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents:
9633
diff
changeset
|
1249 {"seek", (PyCFunction)file_seek, 1}, |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
1250 #ifdef HAVE_FTRUNCATE |
|
9762
ed523ea073db
Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents:
9633
diff
changeset
|
1251 {"truncate", (PyCFunction)file_truncate, 1}, |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
1252 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1253 {"tell", (PyCFunction)file_tell, 0}, |
|
5783
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1254 {"readinto", (PyCFunction)file_readinto, 0}, |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1255 {"readlines", (PyCFunction)file_readlines, 1}, |
|
15780
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
1256 {"xreadlines", (PyCFunction)file_xreadlines, 1}, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1257 {"writelines", (PyCFunction)file_writelines, 0}, |
|
5783
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1258 {"flush", (PyCFunction)file_flush, 0}, |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1259 {"close", (PyCFunction)file_close, 0}, |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1260 {"isatty", (PyCFunction)file_isatty, 0}, |
| 6 | 1261 {NULL, NULL} /* sentinel */ |
| 1262 }; | |
| 1263 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1264 #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
|
1265 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1266 static struct memberlist file_memberlist[] = { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1267 {"softspace", T_INT, OFF(f_softspace)}, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1268 {"mode", T_OBJECT, OFF(f_mode), RO}, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1269 {"name", T_OBJECT, OFF(f_name), RO}, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1270 /* 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
|
1271 {"closed", T_INT, 0, RO}, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1272 {NULL} /* Sentinel */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1273 }; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1274 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1275 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1276 file_getattr(PyFileObject *f, char *name) |
| 6 | 1277 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1278 PyObject *res; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1279 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1280 res = Py_FindMethod(file_methods, (PyObject *)f, name); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1281 if (res != NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1282 return res; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1283 PyErr_Clear(); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1284 if (strcmp(name, "closed") == 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1285 return PyInt_FromLong((long)(f->f_fp == 0)); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1286 return PyMember_Get((char *)f, file_memberlist, name); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1287 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1288 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1289 static int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1290 file_setattr(PyFileObject *f, char *name, PyObject *v) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1291 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1292 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1293 PyErr_SetString(PyExc_AttributeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1294 "can't delete file attributes"); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1295 return -1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1296 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1297 return PyMember_Set((char *)f, file_memberlist, name, v); |
| 6 | 1298 } |
| 1299 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1300 PyTypeObject PyFile_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1301 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 1302 0, |
| 1303 "file", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1304 sizeof(PyFileObject), |
| 6 | 1305 0, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1306 (destructor)file_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
1307 0, /*tp_print*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1308 (getattrfunc)file_getattr, /*tp_getattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1309 (setattrfunc)file_setattr, /*tp_setattr*/ |
| 6 | 1310 0, /*tp_compare*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1311 (reprfunc)file_repr, /*tp_repr*/ |
| 6 | 1312 }; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1313 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1314 /* 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
|
1315 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1316 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1317 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
|
1318 { |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1319 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
|
1320 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
|
1321 /* 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
|
1322 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1323 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1324 oldflag = ((PyFileObject *)f)->f_softspace; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1325 ((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
|
1326 } |
|
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
|
1327 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1328 PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1329 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
|
1330 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1331 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
|
1332 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1333 if (PyInt_Check(v)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1334 oldflag = PyInt_AsLong(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1335 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
|
1336 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1337 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
|
1338 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1339 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
|
1340 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1341 if (PyObject_SetAttrString(f, "softspace", v) != 0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1342 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1343 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
|
1344 } |
|
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
|
1345 } |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1346 return oldflag; |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1347 } |
|
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
|
1348 |
|
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
|
1349 /* 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
|
1350 |
|
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
|
1351 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1352 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
|
1353 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1354 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
|
1355 if (f == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1356 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
|
1357 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
|
1358 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1359 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1360 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
|
1361 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
|
1362 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
|
1363 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
|
1364 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1365 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
|
1366 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1367 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
|
1368 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
|
1369 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1370 if (flags & Py_PRINT_RAW) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1371 value = PyObject_Str(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1389
diff
changeset
|
1372 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1373 value = PyObject_Repr(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1389
diff
changeset
|
1374 if (value == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1375 Py_DECREF(writer); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1389
diff
changeset
|
1376 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
|
1377 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1378 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
|
1379 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1380 Py_DECREF(value); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1381 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
|
1382 return -1; |
|
bebb0ea7de20
fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents:
2667
diff
changeset
|
1383 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1384 result = PyEval_CallObject(writer, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1385 Py_DECREF(args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1386 Py_DECREF(value); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1387 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
|
1388 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
|
1389 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1390 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
|
1391 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
|
1392 } |
|
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
|
1393 |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1394 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1395 PyFile_WriteString(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
|
1396 { |
|
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
|
1397 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
|
1398 /* Should be caused by a pre-existing error */ |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1399 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
|
1400 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
|
1401 "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
|
1402 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
|
1403 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1404 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1405 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
|
1406 if (fp == NULL) { |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1407 err_closed(); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1408 return -1; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1409 } |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1410 fputs(s, fp); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1411 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
|
1412 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1413 else if (!PyErr_Occurred()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1414 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
|
1415 int err; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1416 if (v == NULL) |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1417 return -1; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1418 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
|
1419 Py_DECREF(v); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
1420 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
|
1421 } |
|
5783
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1422 else |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
1423 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
|
1424 } |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1425 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1426 /* 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
|
1427 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
|
1428 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
|
1429 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
|
1430 -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
|
1431 */ |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1432 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1433 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
|
1434 { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1435 int fd; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1436 PyObject *meth; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1437 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1438 if (PyInt_Check(o)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1439 fd = PyInt_AsLong(o); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1440 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1441 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
|
1442 fd = PyLong_AsLong(o); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1443 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1444 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
|
1445 { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1446 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
|
1447 Py_DECREF(meth); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1448 if (fno == NULL) |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1449 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
|
1450 |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1451 if (PyInt_Check(fno)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1452 fd = PyInt_AsLong(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1453 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1454 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1455 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
|
1456 fd = PyLong_AsLong(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1457 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1458 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1459 else { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1460 PyErr_SetString(PyExc_TypeError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1461 "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
|
1462 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1463 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1464 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1465 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1466 else { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1467 PyErr_SetString(PyExc_TypeError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1468 "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
|
1469 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1470 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1471 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1472 if (fd < 0) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1473 PyErr_Format(PyExc_ValueError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1474 "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
|
1475 fd); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1476 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1477 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1478 return fd; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1479 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1480 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1481 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
1482 |
