annotate Objects/fileobject.c @ 3720:b341def4eef8 legacy-trunk

Ignore latex intermediate products.
author Guido van Rossum <guido@python.org>
date Wed, 26 Jun 1996 19:30:01 +0000
parents bebb0ea7de20
children 65d2d8e67da3
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
8 documentation for any purpose and without fee is hereby granted,
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
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 138
diff changeset
24
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* File object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
27 #include "allobjects.h"
645
c0e7b0b9b750 Include modsupport.h for getargs().
Guido van Rossum <guido@python.org>
parents: 509
diff changeset
28 #include "modsupport.h"
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
29 #include "structmember.h"
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
30 #include "ceval.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
32 #ifdef THINK_C
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
33 #define HAVE_FOPENRF
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
34 #endif
2667
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
35 #ifdef __MWERKS__
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
36 /* 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
37 #define NO_FOPEN_ERRNO
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
38 #endif
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
39
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
40 #define BUF(v) GETSTRINGVALUE((stringobject *)v)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
41
1319
cd397266dc1c * Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
42 #include <errno.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 typedef struct {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 OB_HEAD
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 FILE *f_fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 object *f_name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 object *f_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
49 int (*f_close) PROTO((FILE *));
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
50 int f_softspace; /* Flag used by 'print' command */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 } fileobject;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 FILE *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 getfilefile(f)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 object *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 {
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
57 if (f == NULL || !is_fileobject(f))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 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
59 else
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
60 return ((fileobject *)f)->f_fp;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 object *
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
64 getfilename(f)
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
65 object *f;
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
66 {
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
67 if (f == NULL || !is_fileobject(f))
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
68 return NULL;
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
69 else
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
70 return ((fileobject *)f)->f_name;
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
71 }
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
72
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
73 object *
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
74 newopenfileobject(fp, name, mode, close)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 char *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
78 int (*close) FPROTO((FILE *));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 fileobject *f = NEWOBJ(fileobject, &Filetype);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if (f == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 f->f_fp = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 f->f_name = newstringobject(name);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 f->f_mode = newstringobject(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
86 f->f_close = close;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
87 f->f_softspace = 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if (f->f_name == NULL || f->f_mode == NULL) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 DECREF(f);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 f->f_fp = fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 return (object *) f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 newfileobject(name, mode)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 char *name, *mode;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 {
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
100 extern int fclose PROTO((FILE *));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 fileobject *f;
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
102 f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if (f == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 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
105 #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
106 if (*mode == '*') {
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
107 FILE *fopenRF();
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
108 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
109 }
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
110 else
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
111 #endif
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
112 {
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
113 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
114 f->f_fp = fopen(name, mode);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
115 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
116 }
138
bb24948957e3 Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents: 109
diff changeset
117 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
118 #ifdef NO_FOPEN_ERRNO
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
119 if ( errno == 0 ) {
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
120 err_setstr(IOError, "Cannot open file");
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
121 DECREF(f);
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
122 return NULL;
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
123 }
0b7e14bb54ae MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents: 2397
diff changeset
124 #endif
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 436
diff changeset
125 err_errno(IOError);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 DECREF(f);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return (object *)f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
132 void
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
133 setfilebufsize(f, bufsize)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
134 object *f;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
135 int bufsize;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
136 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
137 if (bufsize >= 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
138 #ifdef HAVE_SETVBUF
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
139 int type;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
140 switch (bufsize) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
141 case 0:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
142 type = _IONBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
143 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
144 case 1:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
145 type = _IOLBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
146 bufsize = BUFSIZ;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
147 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
148 default:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
149 type = _IOFBF;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
150 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
151 setvbuf(((fileobject *)f)->f_fp, (char *)NULL, type, bufsize);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
152 #endif /* HAVE_SETVBUF */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
153 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
154 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
155
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
156 static object *
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
157 err_closed()
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
158 {
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
159 err_setstr(ValueError, "I/O operation on closed file");
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
160 return NULL;
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
161 }
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
162
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 static void
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
166 file_dealloc(f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
169 if (f->f_fp != NULL && f->f_close != NULL) {
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
170 BGN_SAVE
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
171 (*f->f_close)(f->f_fp);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
172 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
173 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 if (f->f_name != NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 DECREF(f->f_name);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 if (f->f_mode != NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 DECREF(f->f_mode);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 free((char *)f);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 }
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 object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
182 file_repr(f)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 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
186 sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 f->f_fp == NULL ? "closed" : "open",
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 getstringvalue(f->f_name),
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
189 getstringvalue(f->f_mode),
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
190 (long)f);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 return newstringobject(buf);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
195 file_close(f, args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 object *args;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 {
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
199 int sts = 0;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
200 if (!getnoarg(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 if (f->f_fp != NULL) {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
203 if (f->f_close != NULL) {
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
204 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
205 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
206 sts = (*f->f_close)(f->f_fp);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
207 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
208 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 f->f_fp = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
211 if (sts == EOF)
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 436
diff changeset
212 return err_errno(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
213 if (sts != 0)
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
214 return newintobject((long)sts);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 INCREF(None);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 return None;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 static object *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
220 file_seek(f, args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 object *args;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 {
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
224 long offset;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
225 int whence;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
226 int ret;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
227
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
228 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
229 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
230 whence = 0;
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
231 if (!getargs(args, "l", &offset)) {
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
232 err_clear();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
233 if (!getargs(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
234 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
235 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
236 BGN_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
237 errno = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
238 ret = fseek(f->f_fp, offset, whence);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
239 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
240 if (ret != 0) {
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
241 err_errno(IOError);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
242 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
243 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
244 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
245 INCREF(None);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
246 return None;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
247 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
248
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
249 #ifdef 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
250 static object *
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
251 file_truncate(f, args)
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
252 fileobject *f;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
253 object *args;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
254 {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
255 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
256 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
257
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
258 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
259 return err_closed();
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
260 if (!getargs(args, "l", &newsize)) {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
261 err_clear();
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
262 if (!getnoarg(args))
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
263 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
264 BGN_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
265 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
266 newsize = ftell(f->f_fp); /* default to current position*/
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
267 END_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
268 if (newsize == -1L) {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
269 err_errno(IOError);
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
270 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
271 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
272 }
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 BGN_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
275 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
276 ret = fflush(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
277 END_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
278 if (ret == 0) {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
279 BGN_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
280 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
281 ret = ftruncate(fileno(f->f_fp), newsize);
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
282 END_SAVE
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
283 }
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 (ret != 0) {
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
285 err_errno(IOError);
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 INCREF(None);
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
290 return None;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
291 }
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
292 #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
293
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
294 static object *
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
295 file_tell(f, args)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
296 fileobject *f;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
297 object *args;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
298 {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
299 long offset;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
300 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
301 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
302 if (!getnoarg(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
304 BGN_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
305 errno = 0;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
306 offset = ftell(f->f_fp);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
307 END_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
308 if (offset == -1L) {
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
309 err_errno(IOError);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
310 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
311 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
312 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
313 return newintobject(offset);
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
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
316 static object *
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
317 file_fileno(f, args)
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
318 fileobject *f;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
319 object *args;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
320 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
321 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
322 return err_closed();
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
323 if (!getnoarg(args))
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
324 return NULL;
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
325 return newintobject((long) fileno(f->f_fp));
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
326 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
327
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
328 static object *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
329 file_flush(f, args)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
330 fileobject *f;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
331 object *args;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
332 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
333 int res;
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
334
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
335 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
336 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
337 if (!getnoarg(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
339 BGN_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
340 errno = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
341 res = fflush(f->f_fp);
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
342 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
343 if (res != 0) {
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
344 err_errno(IOError);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
345 clearerr(f->f_fp);
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
346 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
347 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
348 INCREF(None);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
349 return None;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
350 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
351
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
352 static object *
261
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
353 file_isatty(f, args)
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
354 fileobject *f;
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
355 object *args;
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
356 {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
357 long res;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
358 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
359 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
360 if (!getnoarg(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
361 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
362 BGN_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
363 res = isatty((int)fileno(f->f_fp));
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
364 END_SAVE
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
365 return newintobject(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
366 }
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
367
f4a944e9835f In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents: 251
diff changeset
368 static object *
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
369 file_read(f, args)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
370 fileobject *f;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
371 object *args;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
372 {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
373 int n, n1, n2, n3;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
374 object *v;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
375
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
376 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
377 return err_closed();
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
378 if (args == NULL)
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
379 n = -1;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
380 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
381 if (!getargs(args, "i", &n))
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
382 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
383 }
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
384 n2 = n >= 0 ? n : BUFSIZ;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
385 v = newsizedstringobject((char *)NULL, n2);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
386 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
388 n1 = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
389 BGN_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
390 for (;;) {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
391 n3 = fread(BUF(v)+n1, 1, n2-n1, f->f_fp);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
392 /* XXX Error check? */
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
393 if (n3 == 0)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
394 break;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
395 n1 += n3;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
396 if (n1 == n)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
397 break;
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
398 if (n < 0) {
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
399 n2 = n1 + BUFSIZ;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
400 RET_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
401 if (resizestring(&v, n2) < 0)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
402 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
403 RES_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
404 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
405 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
406 END_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
407 if (n1 != n2)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
408 resizestring(&v, n1);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 return v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
412 /* 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
413 Size argument interpretation:
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
414 > 0: max length;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
415 = 0: read arbitrary line;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
416 < 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
417 */
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
418
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
419 static object *
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
420 getline(f, n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 fileobject *f;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
422 int n;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 {
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
424 register FILE *fp;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
425 register int c;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
426 register char *buf, *end;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
427 int n1, n2;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 object *v;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
429
916
fec18e244d16 fileobject.c: fix fatal bug in getline() (forgot to initialize fp now)
Guido van Rossum <guido@python.org>
parents: 908
diff changeset
430 fp = f->f_fp;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
431 n2 = n > 0 ? n : 100;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
432 v = newsizedstringobject((char *)NULL, n2);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
433 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
435 buf = BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
436 end = buf + n2;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
437
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
438 BGN_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
439 for (;;) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
440 if ((c = getc(fp)) == EOF) {
251
48d58b74a3a4 Call clearerr() after EOF seen.
Guido van Rossum <guido@python.org>
parents: 183
diff changeset
441 clearerr(fp);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
442 if (sigcheck()) {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
443 RET_SAVE
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
444 DECREF(v);
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
445 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
446 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
447 if (n < 0 && buf == BUF(v)) {
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
448 RET_SAVE
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
449 DECREF(v);
509
5b5f86ee4f79 Added message to EOFError.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
450 err_setstr(EOFError,
5b5f86ee4f79 Added message to EOFError.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
451 "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
452 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
453 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
454 break;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
455 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
456 if ((*buf++ = c) == '\n') {
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
457 if (n < 0)
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
458 buf--;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
459 break;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
460 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
461 if (buf == end) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
462 if (n > 0)
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
463 break;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
464 n1 = n2;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
465 n2 += 1000;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
466 RET_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
467 if (resizestring(&v, n2) < 0)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
468 return NULL;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
469 RES_SAVE
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
470 buf = BUF(v) + n1;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
471 end = BUF(v) + n2;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
472 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
474 END_SAVE
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 916
diff changeset
475
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
476 n1 = buf - BUF(v);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
477 if (n1 != n2)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
478 resizestring(&v, n1);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
479 return v;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
480 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
481
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
482 /* External C interface */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
483
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
484 object *
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
485 filegetline(f, n)
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
486 object *f;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
487 int n;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
488 {
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
489 if (f == NULL) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
490 err_badcall();
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
491 return NULL;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
492 }
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
493 if (!is_fileobject(f)) {
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
494 object *reader;
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
495 object *args;
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
496 object *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
497 reader = getattr(f, "readline");
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
498 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
499 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
500 if (n <= 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
501 args = mkvalue("()");
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
502 else
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
503 args = mkvalue("(i)", 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
504 if (args == 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
505 DECREF(reader);
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
506 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
507 }
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
508 result = call_object(reader, args);
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
509 DECREF(reader);
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
510 DECREF(args);
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
511 if (result != NULL && !is_stringobject(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
512 DECREF(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
513 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
514 err_setstr(TypeError,
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
515 "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
516 }
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
517 if (n < 0 && 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
518 char *s = getstringvalue(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
519 int len = getstringsize(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
520 if (len == 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
521 DECREF(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
522 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
523 err_setstr(EOFError,
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
524 "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
525 }
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
526 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
527 if (result->ob_refcnt == 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
528 resizestring(&result, len-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
529 else {
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
530 object *v;
1166
a7cf20c845a3 fileobject.c: fix nasty bug; Makefile; turn on STROP and change lint flags.
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
531 v = newsizedstringobject(s, 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
532 DECREF(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
533 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
534 }
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
535 }
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
536 }
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
537 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
538 }
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
539 if (((fileobject*)f)->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
540 return err_closed();
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
541 return getline((fileobject *)f, n);
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
542 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
543
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
544 /* Python method */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
545
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
546 static object *
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
547 file_readline(f, args)
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
548 fileobject *f;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
549 object *args;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
550 {
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
551 int n;
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
552
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
553 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
554 return err_closed();
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
555 if (args == NULL)
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
556 n = 0; /* Unlimited */
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
557 else {
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
558 if (!getintarg(args, &n))
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
559 return NULL;
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
560 if (n == 0)
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
561 return newstringobject("");
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
562 if (n < 0)
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
563 n = 0;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
564 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
565
734
cf5cabfb3f71 Quiet lint
Guido van Rossum <guido@python.org>
parents: 689
diff changeset
566 return getline(f, n);
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
567 }
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
568
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
569 static object *
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
570 file_readlines(f, args)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
571 fileobject *f;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
572 object *args;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
573 {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
574 object *list;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
575 object *line;
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
576
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
577 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
578 return err_closed();
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
579 if (!getnoarg(args))
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
580 return NULL;
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
581 if ((list = newlistobject(0)) == NULL)
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
582 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
583 for (;;) {
183
c9168afe23dd Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents: 177
diff changeset
584 line = getline(f, 0);
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
585 if (line != NULL && getstringsize(line) == 0) {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
586 DECREF(line);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
587 break;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
588 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
589 if (line == NULL || addlistitem(list, line) != 0) {
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
590 DECREF(list);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
591 XDECREF(line);
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
592 return NULL;
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
593 }
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
594 DECREF(line);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 }
154
f2efceaf73cc Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
596 return list;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
600 file_write(f, args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 object *args;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 {
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
604 char *s;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 int n, n2;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
606 if (f->f_fp == NULL)
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
607 return err_closed();
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
608 if (!getargs(args, "s#", &s, &n))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 return NULL;
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
610 f->f_softspace = 0;
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
611 BGN_SAVE
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 errno = 0;
908
424f55e67d5d Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents: 902
diff changeset
613 n2 = fwrite(s, 1, n, f->f_fp);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
614 END_SAVE
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 if (n2 != n) {
475
e2c3463c4d91 Use new exceptions.
Guido van Rossum <guido@python.org>
parents: 436
diff changeset
616 err_errno(IOError);
689
90ea50786095 Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents: 645
diff changeset
617 clearerr(f->f_fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 INCREF(None);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 return None;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623
1362
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
624 static object *
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
625 file_writelines(f, args)
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
626 fileobject *f;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
627 object *args;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
628 {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
629 int i, n;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
630 if (f->f_fp == NULL)
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
631 return err_closed();
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
632 if (args == NULL || !is_listobject(args)) {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
633 err_setstr(TypeError,
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
634 "writelines() requires list of strings");
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
635 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
636 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
637 n = getlistsize(args);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
638 f->f_softspace = 0;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
639 BGN_SAVE
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
640 errno = 0;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
641 for (i = 0; i < n; i++) {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
642 object *line = getlistitem(args, i);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
643 int len;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
644 int nwritten;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
645 if (!is_stringobject(line)) {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
646 RET_SAVE
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
647 err_setstr(TypeError,
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
648 "writelines() requires list of strings");
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
649 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
650 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
651 len = getstringsize(line);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
652 nwritten = fwrite(getstringvalue(line), 1, len, f->f_fp);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
653 if (nwritten != len) {
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
654 RET_SAVE
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
655 err_errno(IOError);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
656 clearerr(f->f_fp);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
657 return NULL;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
658 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
659 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
660 END_SAVE
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
661 INCREF(None);
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
662 return None;
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
663 }
560407ec632b * filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
664
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
665 static struct methodlist file_methods[] = {
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
666 {"close", (method)file_close, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
667 {"flush", (method)file_flush, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
668 {"fileno", (method)file_fileno, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
669 {"isatty", (method)file_isatty, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
670 {"read", (method)file_read, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
671 {"readline", (method)file_readline, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
672 {"readlines", (method)file_readlines, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
673 {"seek", (method)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
674 #ifdef HAVE_FTRUNCATE
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
675 {"truncate", (method)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
676 #endif
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
677 {"tell", (method)file_tell, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
678 {"write", (method)file_write, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
679 {"writelines", (method)file_writelines, 0},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
682
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
683 #define OFF(x) offsetof(fileobject, x)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
684
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
685 static struct memberlist file_memberlist[] = {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
686 {"softspace", T_INT, OFF(f_softspace)},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
687 {"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
688 {"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
689 /* 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
690 {"closed", T_INT, 0, RO},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
691 {NULL} /* Sentinel */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
692 };
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
693
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 38
diff changeset
695 file_getattr(f, name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 fileobject *f;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
699 object *res;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
700
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
701 res = findmethod(file_methods, (object *)f, name);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
702 if (res != NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
703 return res;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
704 err_clear();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
705 if (strcmp(name, "closed") == 0)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
706 return newintobject((long)(f->f_fp == 0));
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
707 return getmember((char *)f, file_memberlist, name);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
708 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
709
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
710 static int
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
711 file_setattr(f, name, v)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
712 fileobject *f;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
713 char *name;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
714 object *v;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
715 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
716 if (v == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
717 err_setstr(AttributeError, "can't delete file attributes");
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
718 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
719 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
720 return setmember((char *)f, file_memberlist, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 typeobject Filetype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 "file",
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 sizeof(fileobject),
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 0,
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
729 (destructor)file_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
730 0, /*tp_print*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
731 (getattrfunc)file_getattr, /*tp_getattr*/
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
732 (setattrfunc)file_setattr, /*tp_setattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 0, /*tp_compare*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
734 (reprfunc)file_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 };
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
736
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
737 /* 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
738
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
739 int
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
740 softspace(f, newflag)
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
741 object *f;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
742 int newflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
743 {
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
744 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
745 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
746 /* 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
747 }
1389
e99c22490bfb * fileobject.c (softspace): fix bug if called with NULL file.
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
748 else if (is_fileobject(f)) {
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
749 oldflag = ((fileobject *)f)->f_softspace;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
750 ((fileobject *)f)->f_softspace = newflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
751 }
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
752 else {
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
753 object *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
754 v = getattr(f, "softspace");
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
755 if (v == 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
756 err_clear();
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
757 else {
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
758 if (is_intobject(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
759 oldflag = getintvalue(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
760 DECREF(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
761 }
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
762 v = newintobject((long)newflag);
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
763 if (v == 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
764 err_clear();
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
765 else {
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
766 if (setattr(f, "softspace", v) != 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
767 err_clear();
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
768 DECREF(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
769 }
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
770 }
177
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
771 return oldflag;
bd49ca6c4add Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents: 154
diff changeset
772 }
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
773
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
774 /* 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
775
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
776 int
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
777 writeobject(v, f, 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
778 object *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
779 object *f;
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
780 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
781 {
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
782 object *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
783 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
784 err_setstr(TypeError, "writeobject with NULL file");
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
785 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
786 }
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
787 else if (is_fileobject(f)) {
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
788 FILE *fp = getfilefile(f);
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
789 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
790 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
791 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
792 }
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
793 return printobject(v, fp, 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
794 }
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
795 writer = getattr(f, "write");
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
796 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
797 return -1;
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
798 if (flags & PRINT_RAW)
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
799 value = strobject(v);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
800 else
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
801 value = reprobject(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
802 if (value == NULL) {
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
803 DECREF(writer);
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1389
diff changeset
804 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
805 }
2825
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
806 args = mkvalue("(O)", value);
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
807 if (value == NULL) {
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
808 DECREF(value);
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
809 DECREF(writer);
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
810 return -1;
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
811 }
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
812 result = call_object(writer, args);
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
813 DECREF(args);
bebb0ea7de20 fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents: 2667
diff changeset
814 DECREF(value);
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
815 DECREF(writer);
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
816 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
817 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
818 DECREF(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
819 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
820 }
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
821
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
822 void
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
823 writestring(s, f)
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
824 char *s;
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
825 object *f;
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
826 {
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
827 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
828 /* 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
829 }
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
830 else if (is_fileobject(f)) {
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
831 FILE *fp = getfilefile(f);
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
832 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
833 fputs(s, fp);
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
834 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
835 else if (!err_occurred()) {
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
836 object *v = newstringobject(s);
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
837 if (v == 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
838 err_clear();
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
839 }
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
840 else {
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1182
diff changeset
841 if (writeobject(v, f, PRINT_RAW) != 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
842 err_clear();
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
843 DECREF(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
844 }
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
845 }
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
846 }