annotate Objects/fileobject.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 9cfbc9bb6423
children 8542b8903989
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2825
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* File object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
34 #include "Python.h"
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
35 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
4585
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 #ifdef HAVE_UNISTD_H
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
38 #include <unistd.h>
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
39 #endif
4a8165b3b660 Make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
40
5414
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
41 #ifdef MS_WIN32
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
42 #define ftruncate _chsize
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
43 #define fileno _fileno
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
44 #define HAVE_FTRUNCATE
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
45 #endif
013ff6951909 Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
46
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
47 #ifdef THINK_C
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
48 #define HAVE_FOPENRF
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
49 #endif
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
50 #ifdef __MWERKS__
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
51 /* 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
52 #define NO_FOPEN_ERRNO
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
53 #endif
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
54
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
55 #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
56
1319
cd397266dc1c * Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
57 #include <errno.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
60 PyObject_HEAD
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 FILE *f_fp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
62 PyObject *f_name;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
63 PyObject *f_mode;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
64 int (*f_close) Py_PROTO((FILE *));
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
65 int f_softspace; /* Flag used by 'print' command */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
66 } PyFileObject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 FILE *
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
69 PyFile_AsFile(f)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
70 PyObject *f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
72 if (f == NULL || !PyFile_Check(f))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
75 return ((PyFileObject *)f)->f_fp;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
78 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
79 PyFile_Name(f)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
80 PyObject *f;
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
81 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
82 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
83 return NULL;
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
84 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
85 return ((PyFileObject *)f)->f_name;
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
86 }
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
87
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
88 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
89 PyFile_FromFile(fp, name, mode, close)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 char *mode;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
93 int (*close) Py_FPROTO((FILE *));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
95 PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if (f == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 f->f_fp = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
99 f->f_name = PyString_FromString(name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
100 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
101 f->f_close = close;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
102 f->f_softspace = 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 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
104 Py_DECREF(f);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 f->f_fp = fp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
108 return (PyObject *) f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
111 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
112 PyFile_FromString(name, mode)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 char *name, *mode;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
115 extern int fclose Py_PROTO((FILE *));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
116 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
117 f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (f == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 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
120 #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
121 if (*mode == '*') {
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
122 FILE *fopenRF();
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
123 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
124 }
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
125 else
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
126 #endif
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
127 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
128 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
129 f->f_fp = fopen(name, mode);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
130 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
131 }
138
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
132 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
133 #ifdef NO_FOPEN_ERRNO
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
134 if ( errno == 0 ) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
135 PyErr_SetString(PyExc_IOError, "Cannot open file");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
136 Py_DECREF(f);
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
137 return NULL;
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
138 }
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
139 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
140 PyErr_SetFromErrno(PyExc_IOError);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
141 Py_DECREF(f);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
144 return (PyObject *)f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
147 void
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
148 PyFile_SetBufSize(f, bufsize)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
149 PyObject *f;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
150 int bufsize;
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 if (bufsize >= 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
153 #ifdef HAVE_SETVBUF
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
154 int type;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
155 switch (bufsize) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
156 case 0:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
157 type = _IONBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
158 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
159 case 1:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
160 type = _IOLBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
161 bufsize = BUFSIZ;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
162 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
163 default:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
164 type = _IOFBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
165 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
166 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
167 type, bufsize);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
168 #endif /* HAVE_SETVBUF */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
169 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
170 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
171
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
172 static PyObject *
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
173 err_closed()
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
174 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
175 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
176 return NULL;
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
177 }
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
178
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 static void
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
182 file_dealloc(f)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
183 PyFileObject *f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
185 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
186 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
187 (*f->f_close)(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
188 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
189 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 if (f->f_name != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
191 Py_DECREF(f->f_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if (f->f_mode != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
193 Py_DECREF(f->f_mode);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 free((char *)f);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
197 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
198 file_repr(f)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
199 PyFileObject *f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 char buf[300];
1182
64cf191d21d0 Checking in last-minute changes that are already part of release 0.9.8
Guido van Rossum <guido@python.org>
parents: 1166
diff changeset
202 sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 f->f_fp == NULL ? "closed" : "open",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
204 PyString_AsString(f->f_name),
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
205 PyString_AsString(f->f_mode),
1182
64cf191d21d0 Checking in last-minute changes that are already part of release 0.9.8
Guido van Rossum <guido@python.org>
parents: 1166
diff changeset
206 (long)f);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
207 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
210 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
211 file_close(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
212 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
213 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 {
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
215 int sts = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
216 if (!PyArg_NoArgs(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 if (f->f_fp != NULL) {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
219 if (f->f_close != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
220 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
221 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
222 sts = (*f->f_close)(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
223 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
224 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 f->f_fp = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 }
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
227 if (sts == EOF)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
228 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
229 if (sts != 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
230 return PyInt_FromLong((long)sts);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
231 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
232 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
235 static PyObject *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
236 file_seek(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
237 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
238 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
240 long offset;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
241 int whence;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
242 int ret;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
243
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
244 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
245 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
246 whence = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
247 if (!PyArg_Parse(args, "l", &offset)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
248 PyErr_Clear();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
249 if (!PyArg_Parse(args, "(li)", &offset, &whence))
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
250 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
251 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
252 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
253 errno = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
254 ret = fseek(f->f_fp, offset, whence);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
255 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
256 if (ret != 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
257 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
258 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
259 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
260 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
261 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
262 return Py_None;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
263 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
264
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
265 #ifdef HAVE_FTRUNCATE
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
266 static PyObject *
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
267 file_truncate(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
268 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
269 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
270 {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
271 long newsize;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
272 int ret;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
273
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
274 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
275 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
276 if (!PyArg_Parse(args, "l", &newsize)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
277 PyErr_Clear();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
278 if (!PyArg_NoArgs(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
279 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
280 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
281 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
282 newsize = ftell(f->f_fp); /* default to current position*/
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
283 Py_END_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
284 if (newsize == -1L) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
285 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
286 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
287 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
288 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
289 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
290 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
291 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
292 ret = fflush(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
293 Py_END_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
294 if (ret == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
295 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
296 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
297 ret = ftruncate(fileno(f->f_fp), newsize);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
298 Py_END_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
299 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
300 if (ret != 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
301 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
302 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
303 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
304 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
305 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
306 return Py_None;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
307 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
308 #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
309
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
310 static PyObject *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
311 file_tell(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
312 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
313 PyObject *args;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
314 {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
315 long offset;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
316 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
317 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
318 if (!PyArg_NoArgs(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
320 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
321 errno = 0;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
322 offset = ftell(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
323 Py_END_ALLOW_THREADS
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
324 if (offset == -1L) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
325 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
326 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
327 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
328 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
329 return PyInt_FromLong(offset);
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
330 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
331
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
332 static PyObject *
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
333 file_fileno(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
334 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
335 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
336 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
337 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
338 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
339 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
340 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
341 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
342 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
343
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
344 static PyObject *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
345 file_flush(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
346 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
347 PyObject *args;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
348 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
349 int res;
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
350
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
351 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
352 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
353 if (!PyArg_NoArgs(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
355 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
356 errno = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
357 res = fflush(f->f_fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
358 Py_END_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
359 if (res != 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
360 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
361 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
362 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
363 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
364 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
365 return Py_None;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
366 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
367
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
368 static PyObject *
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
369 file_isatty(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
370 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
371 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
372 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
373 long res;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
374 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
375 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
376 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
377 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
378 Py_BEGIN_ALLOW_THREADS
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
379 res = isatty((int)fileno(f->f_fp));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
380 Py_END_ALLOW_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
381 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
382 }
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
383
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
384 /* We expect that fstat exists on most systems.
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
385 It's confirmed on Unix, Mac and Windows.
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
386 If you don't have it, add #define DONT_HAVE_FSTAT to your config.h. */
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
387 #ifndef DONT_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
388 #define 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
389
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
390 #include <sys/types.h>
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
391 #include <sys/stat.h>
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
392
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
393 #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
394
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
395 #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
396 #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
397 #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
398 #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
399 #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
400
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
401 #define BIGCHUNK (512*1024)
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
402
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
403 static size_t
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
404 new_buffersize(f, 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
405 PyFileObject *f;
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
406 size_t 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
407 {
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
408 #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
409 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
410 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
411 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
412 end = st.st_size;
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
413 pos = ftell(f->f_fp);
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
414 if (end > pos && pos >= 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
415 return end - pos + 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
416 }
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
417 #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
418 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
419 /* 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
420 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
421 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
422 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
423 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
424 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
425 }
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
426 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
427 }
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
428
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
429 static PyObject *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
430 file_read(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
431 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
432 PyObject *args;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
433 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
434 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
435 size_t bytesread, buffersize, chunksize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
436 PyObject *v;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
437
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
438 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
439 return err_closed();
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
440 if (!PyArg_ParseTuple(args, "|l", &bytesrequested))
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
441 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
442 if (bytesrequested < 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
443 buffersize = new_buffersize(f, 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
444 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
445 buffersize = bytesrequested;
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
446 v = PyString_FromStringAndSize((char *)NULL, buffersize);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
447 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 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
449 bytesread = 0;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
450 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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458 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
459 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
460 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
461 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
462 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
463 }
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
464 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
465 if (bytesread < buffersize)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
466 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
467 if (bytesrequested < 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
468 buffersize = new_buffersize(f, 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
469 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
470 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
471 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
472 }
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
473 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
474 _PyString_Resize(&v, bytesread);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 return v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
478 static PyObject *
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
479 file_readinto(f, args)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
480 PyFileObject *f;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
481 PyObject *args;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
482 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
483 char *ptr;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
484 int ntodo, ndone, nnow;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
485
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
486 if (f->f_fp == NULL)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
487 return err_closed();
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
488 if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
489 return NULL;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
490 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
491 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
492 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
493 errno = 0;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
494 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
495 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
496 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
497 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
498 break;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
499 PyErr_SetFromErrno(PyExc_IOError);
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
500 clearerr(f->f_fp);
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
501 return NULL;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
502 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
503 ndone += nnow;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
504 ntodo -= nnow;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
505 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
506 return PyInt_FromLong(ndone);
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
507 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
508
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
509
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
510 /* 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
511 Size argument interpretation:
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
512 > 0: max length;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
513 = 0: read arbitrary line;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
514 < 0: strip trailing '\n', raise EOFError if EOF reached immediately
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
515 */
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
516
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
517 static PyObject *
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
518 getline(f, n)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
519 PyFileObject *f;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
520 int n;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 {
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
522 register FILE *fp;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
523 register int c;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
524 register char *buf, *end;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
525 int n1, n2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
526 PyObject *v;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
527
916
fec18e244d16 fileobject.c: fix fatal bug in getline() (forgot to initialize fp now)
Guido van Rossum <guido@python.org>
parents: 908
diff changeset
528 fp = f->f_fp;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
529 n2 = n > 0 ? n : 100;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
530 v = PyString_FromStringAndSize((char *)NULL, n2);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
531 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
533 buf = BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
534 end = buf + n2;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
535
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
536 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
537 for (;;) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
538 if ((c = getc(fp)) == EOF) {
251
48d58b74a3a4 Call clearerr() after EOF seen.
Guido van Rossum <guido@python.org>
parents: 183
diff changeset
539 clearerr(fp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
540 if (PyErr_CheckSignals()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
541 Py_BLOCK_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
542 Py_DECREF(v);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
543 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
544 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
545 if (n < 0 && buf == BUF(v)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
546 Py_BLOCK_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
547 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
548 PyErr_SetString(PyExc_EOFError,
509
5b5f86ee4f79 Added message to EOFError.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
549 "EOF when reading a line");
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
550 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
551 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
552 break;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
553 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
554 if ((*buf++ = c) == '\n') {
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
555 if (n < 0)
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
556 buf--;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
557 break;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
558 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
559 if (buf == end) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
560 if (n > 0)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
561 break;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
562 n1 = n2;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
563 n2 += 1000;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
564 Py_BLOCK_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
565 if (_PyString_Resize(&v, n2) < 0)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
566 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
567 Py_UNBLOCK_THREADS
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
568 buf = BUF(v) + n1;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
569 end = BUF(v) + n2;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
570 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
572 Py_END_ALLOW_THREADS
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
573
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
574 n1 = buf - BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
575 if (n1 != n2)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
576 _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
577 return v;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
578 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
579
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
580 /* External C interface */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
581
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
582 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
583 PyFile_GetLine(f, n)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
584 PyObject *f;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
585 int n;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
586 {
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
587 if (f == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
588 PyErr_BadInternalCall();
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
589 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
590 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
591 if (!PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
592 PyObject *reader;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
593 PyObject *args;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
594 PyObject *result;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
595 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
596 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
597 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
598 if (n <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
599 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
600 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
601 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
602 if (args == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
603 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
604 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
605 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
606 result = PyEval_CallObject(reader, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
607 Py_DECREF(reader);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
608 Py_DECREF(args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
609 if (result != NULL && !PyString_Check(result)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
610 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
611 result = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
612 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
613 "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
614 }
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
615 if (n < 0 && result != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
616 char *s = PyString_AsString(result);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
617 int len = PyString_Size(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
618 if (len == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
619 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
620 result = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
621 PyErr_SetString(PyExc_EOFError,
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
622 "EOF when reading a line");
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
623 }
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
624 else if (s[len-1] == '\n') {
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
625 if (result->ob_refcnt == 1)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
626 _PyString_Resize(&result, len-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
627 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
628 PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
629 v = PyString_FromStringAndSize(s,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
630 len-1);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
631 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
632 result = v;
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
633 }
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
634 }
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
635 }
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
636 return result;
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
637 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
638 if (((PyFileObject*)f)->f_fp == NULL)
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
639 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
640 return getline((PyFileObject *)f, n);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
641 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
642
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
643 /* Python method */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
644
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
645 static PyObject *
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
646 file_readline(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
647 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
648 PyObject *args;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
649 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
650 int n = -1;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
651
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
652 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
653 return err_closed();
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
654 if (!PyArg_ParseTuple(args, "|i", &n))
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
655 return NULL;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
656 if (n == 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
657 return PyString_FromString("");
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
658 if (n < 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
659 n = 0;
734
cf5cabfb3f71 Quiet lint
Guido van Rossum <guido@python.org>
parents: 689
diff changeset
660 return getline(f, n);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
661 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
662
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
663 static PyObject *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
664 file_readlines(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
665 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
666 PyObject *args;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
667 {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
668 long sizehint = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
669 PyObject *list;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
670 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
671 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
672 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
673 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
674 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
675 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
676 size_t nread;
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
677 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
678 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
679 int err;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
680
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
681 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
682 return err_closed();
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
683 if (!PyArg_ParseTuple(args, "|l", &sizehint))
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
684 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
685 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
686 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
687 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
688 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
689 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
690 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
691 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
692 if (nread == 0) {
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
693 sizehint = 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
694 if (nread == 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
695 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
696 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
697 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
698 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
699 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
700 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
701 goto cleanup;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
702 }
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
703 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
704 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
705 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
706 /* 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
707 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
708 buffersize *= 2;
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
709 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
710 /* 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
711 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
712 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
713 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
714 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
715 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
716 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
717 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
718 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
719 /* 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
720 _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
721 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
722 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
723 continue;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
724 }
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
725 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
726 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
727 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
728 /* 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
729 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
730 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
731 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
732 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
733 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
734 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
735 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
736 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
737 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
738 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
739 } 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
740 /* 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
741 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
742 memmove(buffer, q, nfilled);
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
743 if (sizehint > 0)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
744 if (totalread >= (size_t)sizehint)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
745 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
746 }
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
747 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
748 /* 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
749 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
750 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
751 goto error;
5479
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
752 if (sizehint > 0) {
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
753 /* Need to complete the last line */
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
754 PyObject *rest = getline(f, 0);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
755 if (rest == NULL) {
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
756 Py_DECREF(line);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
757 goto error;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
758 }
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
759 PyString_Concat(&line, rest);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
760 Py_DECREF(rest);
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
761 if (line == NULL)
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
762 goto error;
6d53ebaec021 Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents: 5478
diff changeset
763 }
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
764 err = PyList_Append(list, line);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
765 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
766 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
767 goto error;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 }
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
769 cleanup:
266c9aa8c127 Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents: 5477
diff changeset
770 if (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
771 Py_DECREF(big_buffer);
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
772 return list;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
774
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
775 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
776 file_write(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
777 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
778 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
780 char *s;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 int n, n2;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
782 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
783 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
784 if (!PyArg_Parse(args, "s#", &s, &n))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 return NULL;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
786 f->f_softspace = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
787 Py_BEGIN_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 errno = 0;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
789 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
790 Py_END_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 if (n2 != n) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
792 PyErr_SetFromErrno(PyExc_IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
793 clearerr(f->f_fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
796 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
797 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
800 static PyObject *
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
801 file_writelines(f, args)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
802 PyFileObject *f;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
803 PyObject *args;
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
804 {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
805 int i, n;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
806 if (f->f_fp == NULL)
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
807 return err_closed();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
808 if (args == NULL || !PyList_Check(args)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
809 PyErr_SetString(PyExc_TypeError,
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
810 "writelines() requires list of strings");
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
811 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
812 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
813 n = PyList_Size(args);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
814 f->f_softspace = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
815 Py_BEGIN_ALLOW_THREADS
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
816 errno = 0;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
817 for (i = 0; i < n; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
818 PyObject *line = PyList_GetItem(args, i);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
819 int len;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
820 int nwritten;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
821 if (!PyString_Check(line)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
822 Py_BLOCK_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
823 PyErr_SetString(PyExc_TypeError,
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
824 "writelines() requires list of strings");
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
825 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
826 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
827 len = PyString_Size(line);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
828 nwritten = fwrite(PyString_AsString(line), 1, len, f->f_fp);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
829 if (nwritten != len) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
830 Py_BLOCK_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
831 PyErr_SetFromErrno(PyExc_IOError);
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
832 clearerr(f->f_fp);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
833 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
834 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
835 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
836 Py_END_ALLOW_THREADS
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
837 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
838 return Py_None;
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
839 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
840
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
841 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
842 {"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
843 {"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
844 {"write", (PyCFunction)file_write, 0},
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
845 {"fileno", (PyCFunction)file_fileno, 0},
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
846 {"seek", (PyCFunction)file_seek, 0},
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
847 #ifdef HAVE_FTRUNCATE
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
848 {"truncate", (PyCFunction)file_truncate, 0},
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
849 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
850 {"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
851 {"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
852 {"readlines", (PyCFunction)file_readlines, 1},
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
853 {"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
854 {"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
855 {"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
856 {"isatty", (PyCFunction)file_isatty, 0},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
859
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
860 #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
861
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
862 static struct memberlist file_memberlist[] = {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
863 {"softspace", T_INT, OFF(f_softspace)},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
864 {"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
865 {"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
866 /* 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
867 {"closed", T_INT, 0, RO},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
868 {NULL} /* Sentinel */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
869 };
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
870
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
871 static PyObject *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
872 file_getattr(f, name)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
873 PyFileObject *f;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
876 PyObject *res;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
877
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
878 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
879 if (res != NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
880 return res;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
881 PyErr_Clear();
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
882 if (strcmp(name, "closed") == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
883 return PyInt_FromLong((long)(f->f_fp == 0));
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
884 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
885 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
886
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
887 static int
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
888 file_setattr(f, name, v)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
889 PyFileObject *f;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
890 char *name;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
891 PyObject *v;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
892 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
893 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
894 PyErr_SetString(PyExc_AttributeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
895 "can't delete file attributes");
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
896 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
897 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
898 return PyMember_Set((char *)f, file_memberlist, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
900
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
901 PyTypeObject PyFile_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
902 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
904 "file",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
905 sizeof(PyFileObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
906 0,
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
907 (destructor)file_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
908 0, /*tp_print*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
909 (getattrfunc)file_getattr, /*tp_getattr*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
910 (setattrfunc)file_setattr, /*tp_setattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 0, /*tp_compare*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
912 (reprfunc)file_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 };
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
914
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
915 /* 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
916
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
917 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
918 PyFile_SoftSpace(f, newflag)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
919 PyObject *f;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
920 int newflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
921 {
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
922 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
923 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
924 /* 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
925 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
926 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
927 oldflag = ((PyFileObject *)f)->f_softspace;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
928 ((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
929 }
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
930 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
931 PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
932 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
933 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
934 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
935 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
936 if (PyInt_Check(v))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
937 oldflag = PyInt_AsLong(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
938 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
939 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
940 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
941 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
942 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
943 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
944 if (PyObject_SetAttrString(f, "softspace", v) != 0)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
945 PyErr_Clear();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
946 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
947 }
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
948 }
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
949 return oldflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
950 }
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
951
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
952 /* 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
953
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
954 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
955 PyFile_WriteObject(v, f, flags)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
956 PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
957 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
958 int flags;
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
959 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
960 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
961 if (f == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
962 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
963 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
964 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
965 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
966 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
967 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
968 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
969 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
970 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
971 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
972 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
973 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
974 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
975 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
976 if (flags & Py_PRINT_RAW)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
977 value = PyObject_Str(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
978 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
979 value = PyObject_Repr(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
980 if (value == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
981 Py_DECREF(writer);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
982 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
983 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
984 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
985 if (args == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
986 Py_DECREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
987 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
988 return -1;
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
989 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
990 result = PyEval_CallObject(writer, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
991 Py_DECREF(args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
992 Py_DECREF(value);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
993 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
994 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
995 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
996 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
997 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
998 }
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
999
5605
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1000 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1001 PyFile_WriteString(s, 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
1002 char *s;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1003 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
1004 {
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
1005 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
1006 /* Should be caused by a pre-existing error */
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1007 if(!PyErr_Occurred())
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1008 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
1009 "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
1010 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
1011 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1012 else if (PyFile_Check(f)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1013 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
1014 if (fp == NULL) {
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1015 err_closed();
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1016 return -1;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1017 }
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1018 fputs(s, fp);
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1019 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
1020 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1021 else if (!PyErr_Occurred()) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4585
diff changeset
1022 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
1023 int err;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1024 if (v == NULL)
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1025 return -1;
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1026 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
1027 Py_DECREF(v);
89af5bf8f873 PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5582
diff changeset
1028 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
1029 }
5783
9cfbc9bb6423 Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5605
diff changeset
1030 else
9cfbc9bb6423 Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents: 5605
diff changeset
1031 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
1032 }