Mercurial > cpython-lru_cache-weakref
annotate Objects/fileobject.c @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 2d55be3a8ec3 |
| children | 9959d8fa4ddf |
| rev | line source |
|---|---|
| 6 | 1 /* File object implementation */ |
| 2 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
3 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
4 #include "structmember.h" |
| 6 | 5 |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
10562
diff
changeset
|
6 #ifndef DONT_HAVE_SYS_TYPES_H |
|
9809
93d70308be17
Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents:
9789
diff
changeset
|
7 #include <sys/types.h> |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
10562
diff
changeset
|
8 #endif /* DONT_HAVE_SYS_TYPES_H */ |
|
9809
93d70308be17
Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents:
9789
diff
changeset
|
9 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
10 #ifdef MS_WINDOWS |
|
5414
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
11 #define fileno _fileno |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
12 /* can simulate truncate with Win32 API functions; see file_truncate */ |
|
5414
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
13 #define HAVE_FTRUNCATE |
|
24233
11c4253698dc
WINDOWS_LEAN_AND_MEAN: There is no such symbol, although a very few
Tim Peters <tim.peters@gmail.com>
parents:
24071
diff
changeset
|
14 #define WIN32_LEAN_AND_MEAN |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
15 #include <windows.h> |
|
5414
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
16 #endif |
|
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
17 |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
18 #ifdef _MSC_VER |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
19 /* Need GetVersion to see if on NT so safe to use _wfopen */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
20 #define WIN32_LEAN_AND_MEAN |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
21 #include <windows.h> |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
22 #endif /* _MSC_VER */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
23 |
|
8180
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
24 #ifdef macintosh |
|
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
25 #ifdef USE_GUSI |
|
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
26 #define HAVE_FTRUNCATE |
|
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
27 #endif |
|
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
28 #endif |
|
68ee8abd9782
Enable ftruncate() on the Mac.
Guido van Rossum <guido@python.org>
parents:
8160
diff
changeset
|
29 |
|
2667
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
30 #ifdef __MWERKS__ |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
31 /* 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
|
32 #define NO_FOPEN_ERRNO |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
33 #endif |
|
2397
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
34 |
|
22053
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
35 #if defined(PYOS_OS2) && defined(PYCC_GCC) |
|
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
36 #include <io.h> |
|
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
37 #endif |
|
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
38 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
39 #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
40 |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
10562
diff
changeset
|
41 #ifndef DONT_HAVE_ERRNO_H |
|
1319
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
42 #include <errno.h> |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
10562
diff
changeset
|
43 #endif |
| 6 | 44 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
45 #ifdef HAVE_GETC_UNLOCKED |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
46 #define GETC(f) getc_unlocked(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
47 #define FLOCKFILE(f) flockfile(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
48 #define FUNLOCKFILE(f) funlockfile(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
49 #else |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
50 #define GETC(f) getc(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
51 #define FLOCKFILE(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
52 #define FUNLOCKFILE(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
53 #endif |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
54 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
55 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
56 /* Bits in f_newlinetypes */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
57 #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
58 #define NEWLINE_CR 1 /* \r newline seen */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
59 #define NEWLINE_LF 2 /* \n newline seen */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
60 #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
61 #endif |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
62 |
| 6 | 63 FILE * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
64 PyFile_AsFile(PyObject *f) |
| 6 | 65 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
66 if (f == NULL || !PyFile_Check(f)) |
| 6 | 67 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
|
68 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
69 return ((PyFileObject *)f)->f_fp; |
| 6 | 70 } |
| 71 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
72 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
73 PyFile_Name(PyObject *f) |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
74 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
75 if (f == NULL || !PyFile_Check(f)) |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
76 return NULL; |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
77 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
78 return ((PyFileObject *)f)->f_name; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
79 } |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
80 |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
81 /* On Unix, fopen will succeed for directories. |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
82 In Python, there should be no file objects referring to |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
83 directories, so we need a check. */ |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
84 |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
85 static PyFileObject* |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
86 dircheck(PyFileObject* f) |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
87 { |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
88 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
89 struct stat buf; |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
90 if (f->f_fp == NULL) |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
91 return f; |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
92 if (fstat(fileno(f->f_fp), &buf) == 0 && |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
93 S_ISDIR(buf.st_mode)) { |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
94 #ifdef HAVE_STRERROR |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
95 char *msg = strerror(EISDIR); |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
96 #else |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
97 char *msg = "Is a directory"; |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
98 #endif |
| 24836 | 99 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)", |
| 100 EISDIR, msg); | |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
101 PyErr_SetObject(PyExc_IOError, exc); |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
102 return NULL; |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
103 } |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
104 #endif |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
105 return f; |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
106 } |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
107 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
108 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
109 static PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
110 fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode, |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
111 int (*close)(FILE *), PyObject *wname) |
| 6 | 112 { |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
113 assert(f != NULL); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
114 assert(PyFile_Check(f)); |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
115 assert(f->f_fp == NULL); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
116 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
117 Py_DECREF(f->f_name); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
118 Py_DECREF(f->f_mode); |
|
26330
3086e111783a
Fix --disable-unicode compilation problems.
Martin v. Löwis <martin@v.loewis.de>
parents:
25535
diff
changeset
|
119 #ifdef Py_USING_UNICODE |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
120 if (wname) |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
121 f->f_name = PyUnicode_FromObject(wname); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
122 else |
|
26330
3086e111783a
Fix --disable-unicode compilation problems.
Martin v. Löwis <martin@v.loewis.de>
parents:
25535
diff
changeset
|
123 #endif |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
124 f->f_name = PyString_FromString(name); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
125 f->f_mode = PyString_FromString(mode); |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
126 |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
127 f->f_close = close; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
128 f->f_softspace = 0; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
129 f->f_binary = strchr(mode,'b') != NULL; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
130 f->f_buf = NULL; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
131 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
132 f->f_univ_newline = (strchr(mode, 'U') != NULL); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
133 f->f_newlinetypes = NEWLINE_UNKNOWN; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
134 f->f_skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
135 #endif |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
136 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
137 if (f->f_name == NULL || f->f_mode == NULL) |
| 6 | 138 return NULL; |
| 139 f->f_fp = fp; | |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
140 f = dircheck(f); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
141 return (PyObject *) f; |
| 6 | 142 } |
| 143 | |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
144 static PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
145 open_the_file(PyFileObject *f, char *name, char *mode) |
| 6 | 146 { |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
147 assert(f != NULL); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
148 assert(PyFile_Check(f)); |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
149 #ifdef MS_WINDOWS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
150 /* windows ignores the passed name in order to support Unicode */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
151 assert(f->f_name != NULL); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
152 #else |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
153 assert(name != NULL); |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
154 #endif |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
155 assert(mode != NULL); |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
156 assert(f->f_fp == NULL); |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
157 |
|
19901
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
158 /* rexec.py can't stop a user from getting the file() constructor -- |
|
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
159 all they have to do is get *any* file object f, and then do |
|
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
160 type(f). Here we prevent them from doing damage with it. */ |
|
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
161 if (PyEval_GetRestricted()) { |
|
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
162 PyErr_SetString(PyExc_IOError, |
| 24836 | 163 "file() constructor not accessible in restricted mode"); |
|
19901
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
164 return NULL; |
|
07818cbcd302
Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents:
19894
diff
changeset
|
165 } |
|
20942
d3dd0c2a1771
open_the_file(): Explicitly set errno to 0 before calling fopen().
Tim Peters <tim.peters@gmail.com>
parents:
20935
diff
changeset
|
166 errno = 0; |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
167 #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
|
168 if (*mode == '*') { |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
169 FILE *fopenRF(); |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
170 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
|
171 } |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
172 else |
|
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
173 #endif |
| 926 | 174 { |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
175 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
176 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
177 mode = "rb"; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
178 #else |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
179 /* Compatibility: specifying U in a Python without universal |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
180 ** newlines is allowed, and the file is opened as a normal text |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
181 ** file. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
182 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
183 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
184 mode = "r"; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
185 #endif |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
186 #ifdef MS_WINDOWS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
187 if (PyUnicode_Check(f->f_name)) { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
188 PyObject *wmode; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
189 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
190 if (f->f_name && wmode) { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
191 Py_BEGIN_ALLOW_THREADS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
192 /* PyUnicode_AS_UNICODE OK without thread |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
193 lock as it is a simple dereference. */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
194 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name), |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
195 PyUnicode_AS_UNICODE(wmode)); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
196 Py_END_ALLOW_THREADS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
197 } |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
198 Py_XDECREF(wmode); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
199 } |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
200 #endif |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
201 if (NULL == f->f_fp && NULL != name) { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
202 Py_BEGIN_ALLOW_THREADS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
203 f->f_fp = fopen(name, mode); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
204 Py_END_ALLOW_THREADS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
205 } |
| 926 | 206 } |
|
138
bb24948957e3
Hack to open resource fork on the Mac: open(filename, '*rb').
Guido van Rossum <guido@python.org>
parents:
109
diff
changeset
|
207 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
|
208 #ifdef NO_FOPEN_ERRNO |
|
21113
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
209 /* Metroworks only, wich does not always sets errno */ |
|
20924
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20793
diff
changeset
|
210 if (errno == 0) { |
|
21113
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
211 PyObject *v; |
|
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
212 v = Py_BuildValue("(is)", 0, "Cannot open file"); |
|
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
213 if (v != NULL) { |
|
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
214 PyErr_SetObject(PyExc_IOError, v); |
|
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
215 Py_DECREF(v); |
|
ed3dce17fc79
Merged changes made on r22b2-branch between r22b2 and r22b2-mac (the
Jack Jansen <jack.jansen@cwi.nl>
parents:
21095
diff
changeset
|
216 } |
|
2667
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
217 return NULL; |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
218 } |
|
0b7e14bb54ae
MW does not always set errno on failing fopen()
Jack Jansen <jack.jansen@cwi.nl>
parents:
2397
diff
changeset
|
219 #endif |
|
22873
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
220 #ifdef _MSC_VER |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
221 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings, |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
222 * across all Windows flavors. When it sets EINVAL varies |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
223 * across Windows flavors, the exact conditions aren't |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
224 * documented, and the answer lies in the OS's implementation |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
225 * of Win32's CreateFile function (whose source is secret). |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
226 * Seems the best we can do is map EINVAL to ENOENT. |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
227 */ |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
228 if (errno == 0) /* bad mode string */ |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
229 errno = EINVAL; |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
230 else if (errno == EINVAL) /* unknown, but not a mode string */ |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
231 errno = ENOENT; |
|
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
232 #endif |
|
20924
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20793
diff
changeset
|
233 if (errno == EINVAL) |
|
22873
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
234 PyErr_Format(PyExc_IOError, "invalid mode: %s", |
|
20924
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20793
diff
changeset
|
235 mode); |
|
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20793
diff
changeset
|
236 else |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
237 #ifdef MS_WINDOWS |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
238 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
239 #else |
|
20924
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20793
diff
changeset
|
240 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
241 #endif /* MS_WINDOWS */ |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
242 f = NULL; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
243 } |
|
22873
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
244 if (f != NULL) |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
245 f = dircheck(f); |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
246 return (PyObject *)f; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
247 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
248 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
249 PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
250 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
251 { |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
252 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
253 NULL, NULL); |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
254 if (f != NULL) { |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
255 if (fill_file_fields(f, fp, name, mode, close, NULL) == NULL) { |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
256 Py_DECREF(f); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
257 f = NULL; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
258 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
259 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
260 return (PyObject *) f; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
261 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
262 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
263 PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
264 PyFile_FromString(char *name, char *mode) |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
265 { |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
266 extern int fclose(FILE *); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
267 PyFileObject *f; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
268 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
269 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
270 if (f != NULL) { |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
271 if (open_the_file(f, name, mode) == NULL) { |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
272 Py_DECREF(f); |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
273 f = NULL; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
274 } |
| 6 | 275 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
276 return (PyObject *)f; |
| 6 | 277 } |
| 278 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
279 void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
280 PyFile_SetBufSize(PyObject *f, int bufsize) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
281 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
282 if (bufsize >= 0) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
283 #ifdef HAVE_SETVBUF |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
284 int type; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
285 switch (bufsize) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
286 case 0: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
287 type = _IONBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
288 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
289 case 1: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
290 type = _IOLBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
291 bufsize = BUFSIZ; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
292 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
293 default: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
294 type = _IOFBF; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
295 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
296 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
297 type, bufsize); |
|
7572
c9f417124e0f
When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents:
7536
diff
changeset
|
298 #else /* !HAVE_SETVBUF */ |
|
c9f417124e0f
When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents:
7536
diff
changeset
|
299 if (bufsize <= 1) |
|
c9f417124e0f
When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents:
7536
diff
changeset
|
300 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL); |
|
c9f417124e0f
When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents:
7536
diff
changeset
|
301 #endif /* !HAVE_SETVBUF */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
302 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
303 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
304 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
305 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
306 err_closed(void) |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
307 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
308 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
309 return NULL; |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
310 } |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
311 |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
312 static void drop_readahead(PyFileObject *); |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
313 |
| 6 | 314 /* Methods */ |
| 315 | |
| 316 static void | |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
317 file_dealloc(PyFileObject *f) |
| 6 | 318 { |
| 926 | 319 if (f->f_fp != NULL && f->f_close != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
320 Py_BEGIN_ALLOW_THREADS |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
321 (*f->f_close)(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
322 Py_END_ALLOW_THREADS |
| 926 | 323 } |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
324 Py_XDECREF(f->f_name); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
325 Py_XDECREF(f->f_mode); |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
326 drop_readahead(f); |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20045
diff
changeset
|
327 f->ob_type->tp_free((PyObject *)f); |
| 6 | 328 } |
| 329 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
330 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
331 file_repr(PyFileObject *f) |
| 6 | 332 { |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
333 if (PyUnicode_Check(f->f_name)) { |
|
26330
3086e111783a
Fix --disable-unicode compilation problems.
Martin v. Löwis <martin@v.loewis.de>
parents:
25535
diff
changeset
|
334 #ifdef Py_USING_UNICODE |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
335 PyObject *ret = NULL; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
336 PyObject *name; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
337 name = PyUnicode_AsUnicodeEscapeString(f->f_name); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
338 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>", |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
339 f->f_fp == NULL ? "closed" : "open", |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
340 PyString_AsString(name), |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
341 PyString_AsString(f->f_mode), |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
342 f); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
343 Py_XDECREF(name); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
344 return ret; |
|
26330
3086e111783a
Fix --disable-unicode compilation problems.
Martin v. Löwis <martin@v.loewis.de>
parents:
25535
diff
changeset
|
345 #endif |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
346 } else { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
347 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
348 f->f_fp == NULL ? "closed" : "open", |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
349 PyString_AsString(f->f_name), |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
350 PyString_AsString(f->f_mode), |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
351 f); |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
352 } |
| 6 | 353 } |
| 354 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
355 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19105
diff
changeset
|
356 file_close(PyFileObject *f) |
| 6 | 357 { |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
358 int sts = 0; |
| 6 | 359 if (f->f_fp != NULL) { |
| 926 | 360 if (f->f_close != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
361 Py_BEGIN_ALLOW_THREADS |
| 926 | 362 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
|
363 sts = (*f->f_close)(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
364 Py_END_ALLOW_THREADS |
| 926 | 365 } |
| 6 | 366 f->f_fp = NULL; |
| 367 } | |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
368 if (sts == EOF) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
369 return PyErr_SetFromErrno(PyExc_IOError); |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
370 if (sts != 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
371 return PyInt_FromLong((long)sts); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
372 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
373 return Py_None; |
| 6 | 374 } |
| 375 | |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
376 |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
377 /* Our very own off_t-like type, 64-bit if possible */ |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
378 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
379 typedef off_t Py_off_t; |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
380 #elif SIZEOF_OFF_T >= 8 |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
381 typedef off_t Py_off_t; |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
382 #elif SIZEOF_FPOS_T >= 8 |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
383 typedef fpos_t Py_off_t; |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
384 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
385 #error "Large file support, but neither off_t nor fpos_t is large enough." |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
386 #endif |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
387 |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
388 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
389 /* a portable fseek() function |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
390 return 0 on success, non-zero on failure (with errno set) */ |
|
17518
f939ce52a734
Make some private symbols static.
Guido van Rossum <guido@python.org>
parents:
16903
diff
changeset
|
391 static int |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
392 _portable_fseek(FILE *fp, Py_off_t offset, int whence) |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
393 { |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
394 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
395 return fseek(fp, offset, whence); |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
396 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
397 return fseeko(fp, offset, whence); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
398 #elif defined(HAVE_FSEEK64) |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
399 return fseek64(fp, offset, whence); |
| 15068 | 400 #elif defined(__BEOS__) |
| 401 return _fseek(fp, offset, whence); | |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
402 #elif SIZEOF_FPOS_T >= 8 |
|
15925
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
403 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos() |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
404 and fgetpos() to implement fseek()*/ |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
405 fpos_t pos; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
406 switch (whence) { |
|
15925
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
407 case SEEK_END: |
| 19836 | 408 #ifdef MS_WINDOWS |
| 409 fflush(fp); | |
| 410 if (_lseeki64(fileno(fp), 0, 2) == -1) | |
| 411 return -1; | |
| 412 #else | |
|
15925
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
413 if (fseek(fp, 0, SEEK_END) != 0) |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
414 return -1; |
| 19836 | 415 #endif |
|
15925
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
416 /* fall through */ |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
417 case SEEK_CUR: |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
418 if (fgetpos(fp, &pos) != 0) |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
419 return -1; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
420 offset += pos; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
421 break; |
|
9d5beed0b93f
Rationalizing the fallback code for portable fseek -- this is all much
Guido van Rossum <guido@python.org>
parents:
15876
diff
changeset
|
422 /* case SEEK_SET: break; */ |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
423 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
424 return fsetpos(fp, &offset); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
425 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
426 #error "Large file support, but no way to fseek." |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
427 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
428 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
429 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
430 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
431 /* a portable ftell() function |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
432 Return -1 on failure with errno set appropriately, current file |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
433 position on success */ |
|
17518
f939ce52a734
Make some private symbols static.
Guido van Rossum <guido@python.org>
parents:
16903
diff
changeset
|
434 static Py_off_t |
|
14277
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
14215
diff
changeset
|
435 _portable_ftell(FILE* fp) |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
436 { |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
437 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
438 return ftell(fp); |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
439 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8 |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
440 return ftello(fp); |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
441 #elif defined(HAVE_FTELL64) |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
442 return ftell64(fp); |
|
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
443 #elif SIZEOF_FPOS_T >= 8 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
444 fpos_t pos; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
445 if (fgetpos(fp, &pos) != 0) |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
446 return -1; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
447 return pos; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
448 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
449 #error "Large file support, but no way to ftell." |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
450 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
451 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
452 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
453 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
454 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
455 file_seek(PyFileObject *f, PyObject *args) |
| 6 | 456 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
457 int whence; |
| 926 | 458 int ret; |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
459 Py_off_t offset; |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
460 PyObject *offobj; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
461 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
462 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
463 return err_closed(); |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
464 drop_readahead(f); |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
465 whence = 0; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11243
diff
changeset
|
466 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
467 return NULL; |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
468 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
469 offset = PyInt_AsLong(offobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
470 #else |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
471 offset = PyLong_Check(offobj) ? |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
472 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
473 #endif |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
474 if (PyErr_Occurred()) |
|
9762
ed523ea073db
Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents:
9633
diff
changeset
|
475 return NULL; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
476 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
477 Py_BEGIN_ALLOW_THREADS |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
478 errno = 0; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
479 ret = _portable_fseek(f->f_fp, offset, whence); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
480 Py_END_ALLOW_THREADS |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
481 |
| 926 | 482 if (ret != 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
483 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
484 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
485 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
486 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
487 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
488 f->f_skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
489 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
490 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
491 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
492 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
493 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
494 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
495 #ifdef HAVE_FTRUNCATE |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
496 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
497 file_truncate(PyFileObject *f, PyObject *args) |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
498 { |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
499 int ret; |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
500 Py_off_t newsize; |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
501 PyObject *newsizeobj; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
502 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
503 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
|
504 return err_closed(); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
505 newsizeobj = NULL; |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26688
diff
changeset
|
506 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj)) |
|
9762
ed523ea073db
Fix two places (seek and truncate) where a cascade of PyArg_Parse
Guido van Rossum <guido@python.org>
parents:
9633
diff
changeset
|
507 return NULL; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
508 |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
509 /* Set newsize to current postion if newsizeobj NULL, else to the |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
510 specified value. */ |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
511 if (newsizeobj != NULL) { |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
512 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
513 newsize = PyInt_AsLong(newsizeobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
514 #else |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
515 newsize = PyLong_Check(newsizeobj) ? |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
516 PyLong_AsLongLong(newsizeobj) : |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
517 PyInt_AsLong(newsizeobj); |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
518 #endif |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
519 if (PyErr_Occurred()) |
|
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
520 return NULL; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
521 } |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
522 else { |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
523 /* Default to current position. */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
524 Py_BEGIN_ALLOW_THREADS |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
525 errno = 0; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
526 newsize = _portable_ftell(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
527 Py_END_ALLOW_THREADS |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
528 if (newsize == -1) |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
529 goto onioerror; |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
530 } |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
531 |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
532 /* Flush the file. */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
533 Py_BEGIN_ALLOW_THREADS |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
534 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
|
535 ret = fflush(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
536 Py_END_ALLOW_THREADS |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
537 if (ret != 0) |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
538 goto onioerror; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
539 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
540 #ifdef MS_WINDOWS |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
541 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
|
22241
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
542 so don't even try using it. */ |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
543 { |
|
22241
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
544 Py_off_t current; /* current file position */ |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
545 HANDLE hFile; |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
546 int error; |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
547 |
|
22241
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
548 /* current <- current file postion. */ |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
549 if (newsizeobj == NULL) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
550 current = newsize; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
551 else { |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
552 Py_BEGIN_ALLOW_THREADS |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
553 errno = 0; |
|
22241
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
554 current = _portable_ftell(f->f_fp); |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
555 Py_END_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
556 if (current == -1) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
557 goto onioerror; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
558 } |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
559 |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
560 /* Move to newsize. */ |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
561 if (current != newsize) { |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
562 Py_BEGIN_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
563 errno = 0; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
564 error = _portable_fseek(f->f_fp, newsize, SEEK_SET) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
565 != 0; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
566 Py_END_ALLOW_THREADS |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
567 if (error) |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
568 goto onioerror; |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
569 } |
|
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
570 |
|
22241
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
571 /* Truncate. Note that this may grow the file! */ |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
572 Py_BEGIN_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
573 errno = 0; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
574 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp)); |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
575 error = hFile == (HANDLE)-1; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
576 if (!error) { |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
577 error = SetEndOfFile(hFile) == 0; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
578 if (error) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
579 errno = EACCES; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
580 } |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
581 Py_END_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
582 if (error) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
583 goto onioerror; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
584 |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
585 /* Restore original file position. */ |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
586 if (current != newsize) { |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
587 Py_BEGIN_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
588 errno = 0; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
589 error = _portable_fseek(f->f_fp, current, SEEK_SET) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
590 != 0; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
591 Py_END_ALLOW_THREADS |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
592 if (error) |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
593 goto onioerror; |
|
7584b6f1afde
Change Windows file.truncate() to (a) restore the original file position,
Tim Peters <tim.peters@gmail.com>
parents:
22214
diff
changeset
|
594 } |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
595 } |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
596 #else |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
597 Py_BEGIN_ALLOW_THREADS |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
598 errno = 0; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
599 ret = ftruncate(fileno(f->f_fp), newsize); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
600 Py_END_ALLOW_THREADS |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
601 if (ret != 0) goto onioerror; |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
602 #endif /* !MS_WINDOWS */ |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
603 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
604 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
605 return Py_None; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
606 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
607 onioerror: |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
608 PyErr_SetFromErrno(PyExc_IOError); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
609 clearerr(f->f_fp); |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
610 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
|
611 } |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
612 #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
|
613 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
614 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19105
diff
changeset
|
615 file_tell(PyFileObject *f) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
616 { |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
617 Py_off_t pos; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
618 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
619 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
620 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
621 Py_BEGIN_ALLOW_THREADS |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
622 errno = 0; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
623 pos = _portable_ftell(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
624 Py_END_ALLOW_THREADS |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
625 if (pos == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
626 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
627 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
628 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
629 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
630 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
631 if (f->f_skipnextlf) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
632 int c; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
633 c = GETC(f->f_fp); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
634 if (c == '\n') { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
635 pos++; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
636 f->f_skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
637 } else if (c != EOF) ungetc(c, f->f_fp); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
638 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
639 #endif |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
640 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
641 return PyInt_FromLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
642 #else |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
643 return PyLong_FromLongLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
644 #endif |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
645 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
646 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
647 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19105
diff
changeset
|
648 file_fileno(PyFileObject *f) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
649 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
650 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
651 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
652 return PyInt_FromLong((long) fileno(f->f_fp)); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
653 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
654 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
655 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19105
diff
changeset
|
656 file_flush(PyFileObject *f) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
657 { |
| 926 | 658 int res; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
659 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
660 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
661 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
662 Py_BEGIN_ALLOW_THREADS |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
663 errno = 0; |
| 926 | 664 res = fflush(f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
665 Py_END_ALLOW_THREADS |
| 926 | 666 if (res != 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
667 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
668 clearerr(f->f_fp); |
|
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
669 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
670 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
671 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
672 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
673 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
674 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
675 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19105
diff
changeset
|
676 file_isatty(PyFileObject *f) |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
677 { |
| 926 | 678 long res; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
679 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
680 return err_closed(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
681 Py_BEGIN_ALLOW_THREADS |
| 926 | 682 res = isatty((int)fileno(f->f_fp)); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
683 Py_END_ALLOW_THREADS |
|
22864
6804e48a0033
isatty() should return a bool.
Guido van Rossum <guido@python.org>
parents:
22785
diff
changeset
|
684 return PyBool_FromLong(res); |
|
261
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
685 } |
|
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
686 |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
687 |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
688 #if BUFSIZ < 8192 |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
689 #define SMALLCHUNK 8192 |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
690 #else |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
691 #define SMALLCHUNK BUFSIZ |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
692 #endif |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
693 |
|
9883
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
694 #if SIZEOF_INT < 4 |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
695 #define BIGCHUNK (512 * 32) |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
696 #else |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
697 #define BIGCHUNK (512 * 1024) |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
698 #endif |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
699 |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
700 static size_t |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
701 new_buffersize(PyFileObject *f, size_t currentsize) |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
702 { |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
703 #ifdef HAVE_FSTAT |
|
18755
f8bd14aef4d6
Kill more warnings from the SGI compiler.
Fred Drake <fdrake@acm.org>
parents:
17911
diff
changeset
|
704 off_t pos, end; |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
705 struct stat st; |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
706 if (fstat(fileno(f->f_fp), &st) == 0) { |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
707 end = st.st_size; |
|
9633
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
708 /* The following is not a bug: we really need to call lseek() |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
709 *and* ftell(). The reason is that some stdio libraries |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
710 mistakenly flush their buffer when ftell() is called and |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
711 the lseek() call it makes fails, thereby throwing away |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
712 data that cannot be recovered in any way. To avoid this, |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
713 we first test lseek(), and only call ftell() if lseek() |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
714 works. We can't use the lseek() value either, because we |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
715 need to take the amount of buffered data into account. |
|
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
716 (Yet another reason why stdio stinks. :-) */ |
|
20365
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
717 #ifdef USE_GUSI2 |
|
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
718 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR); |
|
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
719 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR); |
|
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
720 #else |
|
8217
a19661076529
Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents:
8180
diff
changeset
|
721 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
|
20365
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
722 #endif |
|
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
723 if (pos >= 0) { |
|
8217
a19661076529
Ugly band-aid to work around a bug in Linux ftell().
Guido van Rossum <guido@python.org>
parents:
8180
diff
changeset
|
724 pos = ftell(f->f_fp); |
|
20365
e46f69f5798b
Rather gross workaround for a bug in the mac GUSI I/O library:
Jack Jansen <jack.jansen@cwi.nl>
parents:
20277
diff
changeset
|
725 } |
|
8160
ce9fc64528a7
Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
726 if (pos < 0) |
|
ce9fc64528a7
Clear the error condition set by ftell().
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
727 clearerr(f->f_fp); |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
728 if (end > pos && pos >= 0) |
|
9633
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
729 return currentsize + end - pos + 1; |
|
7536
eedac50d7744
Of course, I shouldn't have used lseek() to find out the file's
Guido van Rossum <guido@python.org>
parents:
7398
diff
changeset
|
730 /* Add 1 so if the file were to grow we'd notice. */ |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
731 } |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
732 #endif |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
733 if (currentsize > SMALLCHUNK) { |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
734 /* Keep doubling until we reach BIGCHUNK; |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
735 then keep adding BIGCHUNK. */ |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
736 if (currentsize <= BIGCHUNK) |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
737 return currentsize + currentsize; |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
738 else |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
739 return currentsize + BIGCHUNK; |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
740 } |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
741 return currentsize + SMALLCHUNK; |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
742 } |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
743 |
| 26672 | 744 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
| 745 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN) | |
| 746 #else | |
| 747 #ifdef EWOULDBLOCK | |
| 748 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK) | |
| 749 #else | |
| 750 #ifdef EAGAIN | |
| 751 #define BLOCKED_ERRNO(x) ((x) == EAGAIN) | |
| 752 #else | |
| 753 #define BLOCKED_ERRNO(x) 0 | |
| 754 #endif | |
| 755 #endif | |
| 756 #endif | |
| 757 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
758 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
759 file_read(PyFileObject *f, PyObject *args) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
760 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
761 long bytesrequested = -1; |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
762 size_t bytesread, buffersize, chunksize; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
763 PyObject *v; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
764 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
765 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
766 return err_closed(); |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11243
diff
changeset
|
767 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
768 return NULL; |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
769 if (bytesrequested < 0) |
|
10562
a42824cf0b18
casts for picky compilers.
Guido van Rossum <guido@python.org>
parents:
9883
diff
changeset
|
770 buffersize = new_buffersize(f, (size_t)0); |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
771 else |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
772 buffersize = bytesrequested; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
773 if (buffersize > INT_MAX) { |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
774 PyErr_SetString(PyExc_OverflowError, |
| 24836 | 775 "requested number of bytes is more than a Python string can hold"); |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
776 return NULL; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
777 } |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
778 v = PyString_FromStringAndSize((char *)NULL, buffersize); |
| 86 | 779 if (v == NULL) |
| 6 | 780 return NULL; |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
781 bytesread = 0; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
782 for (;;) { |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
783 Py_BEGIN_ALLOW_THREADS |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
784 errno = 0; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
785 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread, |
| 24836 | 786 buffersize - bytesread, f->f_fp, (PyObject *)f); |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
787 Py_END_ALLOW_THREADS |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
788 if (chunksize == 0) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
789 if (!ferror(f->f_fp)) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
790 break; |
| 26672 | 791 clearerr(f->f_fp); |
| 792 /* When in non-blocking mode, data shouldn't | |
| 793 * be discarded if a blocking signal was | |
| 794 * received. That will also happen if | |
| 795 * chunksize != 0, but bytesread < buffersize. */ | |
| 796 if (bytesread > 0 && BLOCKED_ERRNO(errno)) | |
| 797 break; | |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
798 PyErr_SetFromErrno(PyExc_IOError); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
799 Py_DECREF(v); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
800 return NULL; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
801 } |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
802 bytesread += chunksize; |
| 26672 | 803 if (bytesread < buffersize) { |
| 804 clearerr(f->f_fp); | |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
805 break; |
| 26672 | 806 } |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
807 if (bytesrequested < 0) { |
|
9633
e2dc3cb08c60
As noted by Per Cederqvist, new_buffersize() sometimes returns the
Guido van Rossum <guido@python.org>
parents:
8707
diff
changeset
|
808 buffersize = new_buffersize(f, buffersize); |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
809 if (_PyString_Resize(&v, buffersize) < 0) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
810 return NULL; |
| 26672 | 811 } else { |
|
26688
c74b69ab4742
* Objects/fileobject.c
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
26672
diff
changeset
|
812 /* Got what was requested. */ |
| 26672 | 813 break; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
814 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
815 } |
|
5477
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
816 if (bytesread != buffersize) |
|
f44a56e697fb
Speed up read() (i.e. read till EOF) considerably by doing a stat() to
Guido van Rossum <guido@python.org>
parents:
5414
diff
changeset
|
817 _PyString_Resize(&v, bytesread); |
| 6 | 818 return v; |
| 819 } | |
| 820 | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
821 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
822 file_readinto(PyFileObject *f, PyObject *args) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
823 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
824 char *ptr; |
|
20635
04f2801d9977
SF patch #474175 (Jay T Miller): file.readinto arg parsing bug
Guido van Rossum <guido@python.org>
parents:
20392
diff
changeset
|
825 int ntodo; |
|
04f2801d9977
SF patch #474175 (Jay T Miller): file.readinto arg parsing bug
Guido van Rossum <guido@python.org>
parents:
20392
diff
changeset
|
826 size_t ndone, nnow; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
827 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
828 if (f->f_fp == NULL) |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
829 return err_closed(); |
|
22718
c3d637308954
Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22480
diff
changeset
|
830 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo)) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
831 return NULL; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
832 ndone = 0; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
833 while (ntodo > 0) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
834 Py_BEGIN_ALLOW_THREADS |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
835 errno = 0; |
| 24836 | 836 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp, |
| 837 (PyObject *)f); | |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
838 Py_END_ALLOW_THREADS |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
839 if (nnow == 0) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
840 if (!ferror(f->f_fp)) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
841 break; |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
842 PyErr_SetFromErrno(PyExc_IOError); |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
843 clearerr(f->f_fp); |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
844 return NULL; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
845 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
846 ndone += nnow; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
847 ntodo -= nnow; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
848 } |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
849 return PyInt_FromLong((long)ndone); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
850 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
851 |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
852 /************************************************************************** |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
853 Routine to get next line using platform fgets(). |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
854 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
855 Under MSVC 6: |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
856 |
|
15758
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
857 + MS threadsafe getc is very slow (multiple layers of function calls before+ |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
858 after each character, to lock+unlock the stream). |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
859 + The stream-locking functions are MS-internal -- can't access them from user |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
860 code. |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
861 + There's nothing Tim could find in the MS C or platform SDK libraries that |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
862 can worm around this. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
863 + MS fgets locks/unlocks only once per line; it's the only hook we have. |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
864 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
865 So we use fgets for speed(!), despite that it's painful. |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
866 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
867 MS realloc is also slow. |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
868 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
869 Reports from other platforms on this method vs getc_unlocked (which MS doesn't |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
870 have): |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
871 Linux a wash |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
872 Solaris a wash |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
873 Tru64 Unix getline_via_fgets significantly faster |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
874 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
875 CAUTION: The C std isn't clear about this: in those cases where fgets |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
876 writes something into the buffer, can it write into any position beyond the |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
877 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet) |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
878 known on which it does; and it would be a strange way to code fgets. Still, |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
879 getline_via_fgets may not work correctly if it does. The std test |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
880 test_bufio.py should fail if platform fgets() routinely writes beyond the |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
881 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
882 **************************************************************************/ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
883 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
884 /* Use this routine if told to, or by default on non-get_unlocked() |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
885 * platforms unless told not to. Yikes! Let's spell that out: |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
886 * On a platform with getc_unlocked(): |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
887 * By default, use getc_unlocked(). |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
888 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE. |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
889 * On a platform without getc_unlocked(): |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
890 * By default, use fgets(). |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
891 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE. |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
892 */ |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
893 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED) |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
894 #define USE_FGETS_IN_GETLINE |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
895 #endif |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
896 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
897 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE) |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
898 #undef USE_FGETS_IN_GETLINE |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
899 #endif |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
900 |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
901 #ifdef USE_FGETS_IN_GETLINE |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
902 static PyObject* |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
903 getline_via_fgets(FILE *fp) |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
904 { |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
905 /* INITBUFSIZE is the maximum line length that lets us get away with the fast |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
906 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
907 * to fill this much of the buffer with a known value in order to figure out |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
908 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
909 * than "most" lines, we waste time filling unused buffer slots. 100 is |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
910 * surely adequate for most peoples' email archives, chewing over source code, |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
911 * etc -- "regular old text files". |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
912 * MAXBUFSIZE is the maximum line length that lets us get away with the less |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
913 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
914 * cautions about boosting that. 300 was chosen because the worst real-life |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
915 * text-crunching job reported on Python-Dev was a mail-log crawler where over |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
916 * half the lines were 254 chars. |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
917 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
918 #define INITBUFSIZE 100 |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
919 #define MAXBUFSIZE 300 |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
920 char* p; /* temp */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
921 char buf[MAXBUFSIZE]; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
922 PyObject* v; /* the string object result */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
923 char* pvfree; /* address of next free slot */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
924 char* pvend; /* address one beyond last free slot */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
925 size_t nfree; /* # of free buffer slots; pvend-pvfree */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
926 size_t total_v_size; /* total # of slots in buffer */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22471
diff
changeset
|
927 size_t increment; /* amount to increment the buffer */ |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
928 |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
929 /* Optimize for normal case: avoid _PyString_Resize if at all |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
930 * possible via first reading into stack buffer "buf". |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
931 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
932 total_v_size = INITBUFSIZE; /* start small and pray */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
933 pvfree = buf; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
934 for (;;) { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
935 Py_BEGIN_ALLOW_THREADS |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
936 pvend = buf + total_v_size; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
937 nfree = pvend - pvfree; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
938 memset(pvfree, '\n', nfree); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
939 p = fgets(pvfree, nfree, fp); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
940 Py_END_ALLOW_THREADS |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
941 |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
942 if (p == NULL) { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
943 clearerr(fp); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
944 if (PyErr_CheckSignals()) |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
945 return NULL; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
946 v = PyString_FromStringAndSize(buf, pvfree - buf); |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
947 return v; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
948 } |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
949 /* fgets read *something* */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
950 p = memchr(pvfree, '\n', nfree); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
951 if (p != NULL) { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
952 /* Did the \n come from fgets or from us? |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
953 * Since fgets stops at the first \n, and then writes |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
954 * \0, if it's from fgets a \0 must be next. But if |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
955 * that's so, it could not have come from us, since |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
956 * the \n's we filled the buffer with have only more |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
957 * \n's to the right. |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
958 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
959 if (p+1 < pvend && *(p+1) == '\0') { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
960 /* It's from fgets: we win! In particular, |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
961 * we haven't done any mallocs yet, and can |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
962 * build the final result on the first try. |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
963 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
964 ++p; /* include \n from fgets */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
965 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
966 else { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
967 /* Must be from us: fgets didn't fill the |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
968 * buffer and didn't find a newline, so it |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
969 * must be the last and newline-free line of |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
970 * the file. |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
971 */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
972 assert(p > pvfree && *(p-1) == '\0'); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
973 --p; /* don't include \0 from fgets */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
974 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
975 v = PyString_FromStringAndSize(buf, p - buf); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
976 return v; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
977 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
978 /* yuck: fgets overwrote all the newlines, i.e. the entire |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
979 * buffer. So this line isn't over yet, or maybe it is but |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
980 * we're exactly at EOF. If we haven't already, try using the |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
981 * rest of the stack buffer. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
982 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
983 assert(*(pvend-1) == '\0'); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
984 if (pvfree == buf) { |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
985 pvfree = pvend - 1; /* overwrite trailing null */ |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
986 total_v_size = MAXBUFSIZE; |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
987 } |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
988 else |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
989 break; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
990 } |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
991 |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
992 /* The stack buffer isn't big enough; malloc a string object and read |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
993 * into its buffer. |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
994 */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22471
diff
changeset
|
995 total_v_size = MAXBUFSIZE << 1; |
|
15758
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
996 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size); |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
997 if (v == NULL) |
|
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
998 return v; |
|
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
999 /* copy over everything except the last null byte */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
1000 memcpy(BUF(v), buf, MAXBUFSIZE-1); |
|
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
1001 pvfree = BUF(v) + MAXBUFSIZE - 1; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1002 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1003 /* Keep reading stuff into v; if it ever ends successfully, break |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
1004 * after setting p one beyond the end of the line. The code here is |
|
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
1005 * very much like the code above, except reads into v's buffer; see |
|
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
1006 * the code above for detailed comments about the logic. |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1007 */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1008 for (;;) { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1009 Py_BEGIN_ALLOW_THREADS |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1010 pvend = BUF(v) + total_v_size; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1011 nfree = pvend - pvfree; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1012 memset(pvfree, '\n', nfree); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1013 p = fgets(pvfree, nfree, fp); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1014 Py_END_ALLOW_THREADS |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1015 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1016 if (p == NULL) { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1017 clearerr(fp); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1018 if (PyErr_CheckSignals()) { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1019 Py_DECREF(v); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1020 return NULL; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1021 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1022 p = pvfree; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1023 break; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1024 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1025 p = memchr(pvfree, '\n', nfree); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1026 if (p != NULL) { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1027 if (p+1 < pvend && *(p+1) == '\0') { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1028 /* \n came from fgets */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1029 ++p; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1030 break; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1031 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1032 /* \n came from us; last line of file, no newline */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1033 assert(p > pvfree && *(p-1) == '\0'); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1034 --p; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1035 break; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1036 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1037 /* expand buffer and try again */ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1038 assert(*(pvend-1) == '\0'); |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22471
diff
changeset
|
1039 increment = total_v_size >> 2; /* mild exponential growth */ |
|
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22471
diff
changeset
|
1040 total_v_size += increment; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1041 if (total_v_size > INT_MAX) { |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1042 PyErr_SetString(PyExc_OverflowError, |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1043 "line is longer than a Python string can hold"); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1044 Py_DECREF(v); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1045 return NULL; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1046 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1047 if (_PyString_Resize(&v, (int)total_v_size) < 0) |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1048 return NULL; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1049 /* overwrite the trailing null byte */ |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22471
diff
changeset
|
1050 pvfree = BUF(v) + (total_v_size - increment - 1); |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1051 } |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1052 if (BUF(v) + total_v_size != p) |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1053 _PyString_Resize(&v, p - BUF(v)); |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1054 return v; |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1055 #undef INITBUFSIZE |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
1056 #undef MAXBUFSIZE |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1057 } |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1058 #endif /* ifdef USE_FGETS_IN_GETLINE */ |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1059 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1060 /* 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
|
1061 Size argument interpretation: |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1062 > 0: max length; |
|
15757
a9e15bcab32c
Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents:
15755
diff
changeset
|
1063 <= 0: read arbitrary line |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1064 */ |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1065 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1066 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1067 get_line(PyFileObject *f, int n) |
| 6 | 1068 { |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1069 FILE *fp = f->f_fp; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1070 int c; |
|
15446
f813ede134bf
Patch #102469: Use glibc's getline() extension when reading unbounded lines
Andrew M. Kuchling <amk@amk.ca>
parents:
15404
diff
changeset
|
1071 char *buf, *end; |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1072 size_t total_v_size; /* total # of slots in buffer */ |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1073 size_t used_v_size; /* # used slots in buffer */ |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1074 size_t increment; /* amount to increment the buffer */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1075 PyObject *v; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1076 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1077 int newlinetypes = f->f_newlinetypes; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1078 int skipnextlf = f->f_skipnextlf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1079 int univ_newline = f->f_univ_newline; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1080 #endif |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1081 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1082 #if defined(USE_FGETS_IN_GETLINE) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1083 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1084 if (n <= 0 && !univ_newline ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1085 #else |
|
15757
a9e15bcab32c
Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents:
15755
diff
changeset
|
1086 if (n <= 0) |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1087 #endif |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1088 return getline_via_fgets(fp); |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1089 #endif |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1090 total_v_size = n > 0 ? n : 100; |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1091 v = PyString_FromStringAndSize((char *)NULL, total_v_size); |
| 86 | 1092 if (v == NULL) |
| 6 | 1093 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1094 buf = BUF(v); |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1095 end = buf + total_v_size; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
1096 |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1097 for (;;) { |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1098 Py_BEGIN_ALLOW_THREADS |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1099 FLOCKFILE(fp); |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1100 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1101 if (univ_newline) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1102 c = 'x'; /* Shut up gcc warning */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1103 while ( buf != end && (c = GETC(fp)) != EOF ) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1104 if (skipnextlf ) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1105 skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1106 if (c == '\n') { |
| 24836 | 1107 /* Seeing a \n here with |
| 1108 * skipnextlf true means we | |
| 1109 * saw a \r before. | |
| 1110 */ | |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1111 newlinetypes |= NEWLINE_CRLF; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1112 c = GETC(fp); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1113 if (c == EOF) break; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1114 } else { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1115 newlinetypes |= NEWLINE_CR; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1116 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1117 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1118 if (c == '\r') { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1119 skipnextlf = 1; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1120 c = '\n'; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1121 } else if ( c == '\n') |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1122 newlinetypes |= NEWLINE_LF; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1123 *buf++ = c; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1124 if (c == '\n') break; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1125 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1126 if ( c == EOF && skipnextlf ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1127 newlinetypes |= NEWLINE_CR; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1128 } else /* If not universal newlines use the normal loop */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1129 #endif |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1130 while ((c = GETC(fp)) != EOF && |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1131 (*buf++ = c) != '\n' && |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1132 buf != end) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1133 ; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1134 FUNLOCKFILE(fp); |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1135 Py_END_ALLOW_THREADS |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1136 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1137 f->f_newlinetypes = newlinetypes; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1138 f->f_skipnextlf = skipnextlf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1139 #endif |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1140 if (c == '\n') |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1141 break; |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1142 if (c == EOF) { |
|
19105
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1143 if (ferror(fp)) { |
|
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1144 PyErr_SetFromErrno(PyExc_IOError); |
|
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1145 clearerr(fp); |
|
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1146 Py_DECREF(v); |
|
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1147 return NULL; |
|
eea8dfbbee86
Apply anonymous SF patch #441229.
Guido van Rossum <guido@python.org>
parents:
19015
diff
changeset
|
1148 } |
|
251
48d58b74a3a4
Call clearerr() after EOF seen.
Guido van Rossum <guido@python.org>
parents:
183
diff
changeset
|
1149 clearerr(fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1150 if (PyErr_CheckSignals()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1151 Py_DECREF(v); |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1152 return NULL; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1153 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1154 break; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1155 } |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1156 /* Must be because buf == end */ |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1157 if (n > 0) |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1158 break; |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1159 used_v_size = total_v_size; |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1160 increment = total_v_size >> 2; /* mild exponential growth */ |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1161 total_v_size += increment; |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1162 if (total_v_size > INT_MAX) { |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1163 PyErr_SetString(PyExc_OverflowError, |
|
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1164 "line is longer than a Python string can hold"); |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1165 Py_DECREF(v); |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1166 return NULL; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1167 } |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1168 if (_PyString_Resize(&v, total_v_size) < 0) |
|
15745
f5c04134f4e2
Restructured get_line() for clarity and speed.
Guido van Rossum <guido@python.org>
parents:
15633
diff
changeset
|
1169 return NULL; |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1170 buf = BUF(v) + used_v_size; |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1171 end = BUF(v) + total_v_size; |
| 6 | 1172 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
1173 |
|
22480
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1174 used_v_size = buf - BUF(v); |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1175 if (used_v_size != total_v_size) |
|
15702e1b961e
Grow the string buffer at a mildly exponential rate for the getc version
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22479
diff
changeset
|
1176 _PyString_Resize(&v, used_v_size); |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1177 return v; |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1178 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1179 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1180 /* External C interface */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1181 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1182 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1183 PyFile_GetLine(PyObject *f, int n) |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1184 { |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1185 PyObject *result; |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1186 |
|
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
|
1187 if (f == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1188 PyErr_BadInternalCall(); |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1189 return NULL; |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1190 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1191 |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1192 if (PyFile_Check(f)) { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1193 if (((PyFileObject*)f)->f_fp == NULL) |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1194 return err_closed(); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1195 result = get_line((PyFileObject *)f, n); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1196 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1197 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1198 PyObject *reader; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1199 PyObject *args; |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1200 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1201 reader = PyObject_GetAttrString(f, "readline"); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1202 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
|
1203 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
|
1204 if (n <= 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1205 args = Py_BuildValue("()"); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1206 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1207 args = Py_BuildValue("(i)", n); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1208 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1209 Py_DECREF(reader); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1210 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
|
1211 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1212 result = PyEval_CallObject(reader, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1213 Py_DECREF(reader); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1214 Py_DECREF(args); |
|
27060
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1215 if (result != NULL && !PyString_Check(result) && |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1216 !PyUnicode_Check(result)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1217 Py_DECREF(result); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1218 result = NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1219 PyErr_SetString(PyExc_TypeError, |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1220 "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
|
1221 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1222 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1223 |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1224 if (n < 0 && result != NULL && PyString_Check(result)) { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1225 char *s = PyString_AS_STRING(result); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1226 int len = PyString_GET_SIZE(result); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1227 if (len == 0) { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1228 Py_DECREF(result); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1229 result = NULL; |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1230 PyErr_SetString(PyExc_EOFError, |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1231 "EOF when reading a line"); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1232 } |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1233 else if (s[len-1] == '\n') { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1234 if (result->ob_refcnt == 1) |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1235 _PyString_Resize(&result, len-1); |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1236 else { |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1237 PyObject *v; |
|
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1238 v = PyString_FromStringAndSize(s, len-1); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1239 Py_DECREF(result); |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1240 result = v; |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1241 } |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1242 } |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1243 } |
|
27060
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1244 #ifdef Py_USING_UNICODE |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1245 if (n < 0 && result != NULL && PyUnicode_Check(result)) { |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1246 Py_UNICODE *s = PyUnicode_AS_UNICODE(result); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1247 int len = PyUnicode_GET_SIZE(result); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1248 if (len == 0) { |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1249 Py_DECREF(result); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1250 result = NULL; |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1251 PyErr_SetString(PyExc_EOFError, |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1252 "EOF when reading a line"); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1253 } |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1254 else if (s[len-1] == '\n') { |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1255 if (result->ob_refcnt == 1) |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1256 PyUnicode_Resize(&result, len-1); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1257 else { |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1258 PyObject *v; |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1259 v = PyUnicode_FromUnicode(s, len-1); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1260 Py_DECREF(result); |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1261 result = v; |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1262 } |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1263 } |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1264 } |
|
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1265 #endif |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1266 return result; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1267 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1268 |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1269 /* Python method */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1270 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1271 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1272 file_readline(PyFileObject *f, PyObject *args) |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1273 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1274 int n = -1; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1275 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1276 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1277 return err_closed(); |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11243
diff
changeset
|
1278 if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1279 return NULL; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1280 if (n == 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1281 return PyString_FromString(""); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1282 if (n < 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1283 n = 0; |
| 13360 | 1284 return get_line(f, n); |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1285 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1286 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1287 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1288 file_readlines(PyFileObject *f, PyObject *args) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1289 { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1290 long sizehint = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1291 PyObject *list; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1292 PyObject *line; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1293 char small_buffer[SMALLCHUNK]; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1294 char *buffer = small_buffer; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1295 size_t buffersize = SMALLCHUNK; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1296 PyObject *big_buffer = NULL; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1297 size_t nfilled = 0; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1298 size_t nread; |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1299 size_t totalread = 0; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1300 char *p, *q, *end; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1301 int err; |
|
20392
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1302 int shortread = 0; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1303 |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1304 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1305 return err_closed(); |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11243
diff
changeset
|
1306 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1307 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1308 if ((list = PyList_New(0)) == NULL) |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1309 return NULL; |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1310 for (;;) { |
|
20392
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1311 if (shortread) |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1312 nread = 0; |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1313 else { |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1314 Py_BEGIN_ALLOW_THREADS |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1315 errno = 0; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
1316 nread = Py_UniversalNewlineFread(buffer+nfilled, |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1317 buffersize-nfilled, f->f_fp, (PyObject *)f); |
|
20392
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1318 Py_END_ALLOW_THREADS |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1319 shortread = (nread < buffersize-nfilled); |
|
853c268bf247
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
Guido van Rossum <guido@python.org>
parents:
20365
diff
changeset
|
1320 } |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1321 if (nread == 0) { |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1322 sizehint = 0; |
|
7398
7afc15f08d82
Check ferror(), not errno, for fread() error.
Guido van Rossum <guido@python.org>
parents:
6568
diff
changeset
|
1323 if (!ferror(f->f_fp)) |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1324 break; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1325 PyErr_SetFromErrno(PyExc_IOError); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1326 clearerr(f->f_fp); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1327 error: |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1328 Py_DECREF(list); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1329 list = NULL; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1330 goto cleanup; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1331 } |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1332 totalread += nread; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1333 p = memchr(buffer+nfilled, '\n', nread); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1334 if (p == NULL) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1335 /* Need a larger buffer to fit this line */ |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1336 nfilled += nread; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1337 buffersize *= 2; |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
1338 if (buffersize > INT_MAX) { |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
1339 PyErr_SetString(PyExc_OverflowError, |
|
15780
d646d6068924
Jeff Epler's patch adding an xreadlines() method. (It just imports
Guido van Rossum <guido@python.org>
parents:
15767
diff
changeset
|
1340 "line is longer than a Python string can hold"); |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
1341 goto error; |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
1342 } |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1343 if (big_buffer == NULL) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1344 /* Create the big buffer */ |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1345 big_buffer = PyString_FromStringAndSize( |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1346 NULL, buffersize); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1347 if (big_buffer == NULL) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1348 goto error; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1349 buffer = PyString_AS_STRING(big_buffer); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1350 memcpy(buffer, small_buffer, nfilled); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1351 } |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1352 else { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1353 /* Grow the big buffer */ |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1354 if ( _PyString_Resize(&big_buffer, buffersize) < 0 ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1355 goto error; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1356 buffer = PyString_AS_STRING(big_buffer); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1357 } |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1358 continue; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1359 } |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1360 end = buffer+nfilled+nread; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1361 q = buffer; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1362 do { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1363 /* Process complete lines */ |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1364 p++; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1365 line = PyString_FromStringAndSize(q, p-q); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1366 if (line == NULL) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1367 goto error; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1368 err = PyList_Append(list, line); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1369 Py_DECREF(line); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1370 if (err != 0) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1371 goto error; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1372 q = p; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1373 p = memchr(q, '\n', end-q); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1374 } while (p != NULL); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1375 /* Move the remaining incomplete line to the start */ |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1376 nfilled = end-q; |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1377 memmove(buffer, q, nfilled); |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1378 if (sizehint > 0) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1379 if (totalread >= (size_t)sizehint) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1380 break; |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1381 } |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1382 if (nfilled != 0) { |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1383 /* Partial last line */ |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1384 line = PyString_FromStringAndSize(buffer, nfilled); |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1385 if (line == NULL) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1386 goto error; |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1387 if (sizehint > 0) { |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1388 /* Need to complete the last line */ |
| 13360 | 1389 PyObject *rest = get_line(f, 0); |
|
5479
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1390 if (rest == NULL) { |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1391 Py_DECREF(line); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1392 goto error; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1393 } |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1394 PyString_Concat(&line, rest); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1395 Py_DECREF(rest); |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1396 if (line == NULL) |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1397 goto error; |
|
6d53ebaec021
Add optional 'sizehint' argument to readlines(). After approximately
Guido van Rossum <guido@python.org>
parents:
5478
diff
changeset
|
1398 } |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1399 err = PyList_Append(list, line); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1400 Py_DECREF(line); |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1401 if (err != 0) |
|
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1402 goto error; |
| 6 | 1403 } |
|
5478
266c9aa8c127
Rewrite readlines() to speed it up -- about a factor of 2 on my
Guido van Rossum <guido@python.org>
parents:
5477
diff
changeset
|
1404 cleanup: |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23206
diff
changeset
|
1405 Py_XDECREF(big_buffer); |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1406 return list; |
| 6 | 1407 } |
| 1408 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1409 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1410 file_write(PyFileObject *f, PyObject *args) |
| 6 | 1411 { |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1412 char *s; |
| 6 | 1413 int n, n2; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1414 if (f->f_fp == NULL) |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1415 return err_closed(); |
| 20793 | 1416 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) |
| 6 | 1417 return NULL; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1418 f->f_softspace = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1419 Py_BEGIN_ALLOW_THREADS |
| 6 | 1420 errno = 0; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
1421 n2 = fwrite(s, 1, n, f->f_fp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1422 Py_END_ALLOW_THREADS |
| 6 | 1423 if (n2 != n) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1424 PyErr_SetFromErrno(PyExc_IOError); |
|
689
90ea50786095
Change error handling. Call clearerr() more often.
Guido van Rossum <guido@python.org>
parents:
645
diff
changeset
|
1425 clearerr(f->f_fp); |
| 6 | 1426 return NULL; |
| 1427 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1428 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1429 return Py_None; |
| 6 | 1430 } |
| 1431 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1432 static PyObject * |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1433 file_writelines(PyFileObject *f, PyObject *seq) |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1434 { |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1435 #define CHUNKSIZE 1000 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1436 PyObject *list, *line; |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1437 PyObject *it; /* iter(seq) */ |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1438 PyObject *result; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1439 int i, j, index, len, nwritten, islist; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1440 |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1441 assert(seq != NULL); |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1442 if (f->f_fp == NULL) |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1443 return err_closed(); |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1444 |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1445 result = NULL; |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1446 list = NULL; |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1447 islist = PyList_Check(seq); |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1448 if (islist) |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1449 it = NULL; |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1450 else { |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1451 it = PyObject_GetIter(seq); |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1452 if (it == NULL) { |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1453 PyErr_SetString(PyExc_TypeError, |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1454 "writelines() requires an iterable argument"); |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1455 return NULL; |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1456 } |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1457 /* From here on, fail by going to error, to reclaim "it". */ |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1458 list = PyList_New(CHUNKSIZE); |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1459 if (list == NULL) |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1460 goto error; |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1461 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1462 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1463 /* Strategy: slurp CHUNKSIZE lines into a private list, |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1464 checking that they are all strings, then write that list |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1465 without holding the interpreter lock, then come back for more. */ |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1466 for (index = 0; ; index += CHUNKSIZE) { |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1467 if (islist) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1468 Py_XDECREF(list); |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1469 list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1470 if (list == NULL) |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1471 goto error; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1472 j = PyList_GET_SIZE(list); |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1473 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1474 else { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1475 for (j = 0; j < CHUNKSIZE; j++) { |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1476 line = PyIter_Next(it); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1477 if (line == NULL) { |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1478 if (PyErr_Occurred()) |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1479 goto error; |
|
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1480 break; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1481 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1482 PyList_SetItem(list, j, line); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1483 } |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1484 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1485 if (j == 0) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1486 break; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1487 |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1488 /* Check that all entries are indeed strings. If not, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1489 apply the same rules as for file.write() and |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1490 convert the results to strings. This is slow, but |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1491 seems to be the only way since all conversion APIs |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1492 could potentially execute Python code. */ |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1493 for (i = 0; i < j; i++) { |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1494 PyObject *v = PyList_GET_ITEM(list, i); |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1495 if (!PyString_Check(v)) { |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1496 const char *buffer; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1497 int len; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1498 if (((f->f_binary && |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1499 PyObject_AsReadBuffer(v, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1500 (const void**)&buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1501 &len)) || |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1502 PyObject_AsCharBuffer(v, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1503 &buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1504 &len))) { |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1505 PyErr_SetString(PyExc_TypeError, |
| 24836 | 1506 "writelines() argument must be a sequence of strings"); |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1507 goto error; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1508 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1509 line = PyString_FromStringAndSize(buffer, |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1510 len); |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1511 if (line == NULL) |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1512 goto error; |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1513 Py_DECREF(v); |
|
14215
0797fdb25a77
Fixed a serious typo.
Marc-André Lemburg <mal@egenix.com>
parents:
14213
diff
changeset
|
1514 PyList_SET_ITEM(list, i, line); |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1515 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1516 } |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1517 |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1518 /* Since we are releasing the global lock, the |
|
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1519 following code may *not* execute Python code. */ |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1520 Py_BEGIN_ALLOW_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1521 f->f_softspace = 0; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1522 errno = 0; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1523 for (i = 0; i < j; i++) { |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1524 line = PyList_GET_ITEM(list, i); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1525 len = PyString_GET_SIZE(line); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1526 nwritten = fwrite(PyString_AS_STRING(line), |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1527 1, len, f->f_fp); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1528 if (nwritten != len) { |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1529 Py_BLOCK_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1530 PyErr_SetFromErrno(PyExc_IOError); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1531 clearerr(f->f_fp); |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1532 goto error; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1533 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1534 } |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1535 Py_END_ALLOW_THREADS |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1536 |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1537 if (j < CHUNKSIZE) |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1538 break; |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1539 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1540 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1541 Py_INCREF(Py_None); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1542 result = Py_None; |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1543 error: |
|
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1544 Py_XDECREF(list); |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1545 Py_XDECREF(it); |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1546 return result; |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1547 #undef CHUNKSIZE |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1548 } |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1549 |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1550 static PyObject * |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1551 file_getiter(PyFileObject *f) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1552 { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1553 if (f->f_fp == NULL) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1554 return err_closed(); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1555 Py_INCREF(f); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1556 return (PyObject *)f; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1557 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1558 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1559 PyDoc_STRVAR(readline_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1560 "readline([size]) -> next line from the file, as a string.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1561 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1562 "Retain newline. A non-negative size argument limits the maximum\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1563 "number of bytes to return (an incomplete line may be returned then).\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1564 "Return an empty string at EOF."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1565 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1566 PyDoc_STRVAR(read_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1567 "read([size]) -> read at most size bytes, returned as a string.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1568 "\n" |
| 26672 | 1569 "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 1570 "Notice that when in non-blocking mode, less data than what was requested\n" | |
| 1571 "may be returned, even if no size parameter was given."); | |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1572 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1573 PyDoc_STRVAR(write_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1574 "write(str) -> None. Write string str to file.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1575 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1576 "Note that due to buffering, flush() or close() may be needed before\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1577 "the file on disk reflects the data written."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1578 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1579 PyDoc_STRVAR(fileno_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1580 "fileno() -> integer \"file descriptor\".\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1581 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1582 "This is needed for lower-level file interfaces, such os.read()."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1583 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1584 PyDoc_STRVAR(seek_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1585 "seek(offset[, whence]) -> None. Move to new file position.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1586 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1587 "Argument offset is a byte count. Optional argument whence defaults to\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1588 "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1589 "(move relative to current position, positive or negative), and 2 (move\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1590 "relative to end of file, usually negative, although many platforms allow\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1591 "seeking beyond the end of a file).\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1592 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1593 "Note that not all file objects are seekable."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1594 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
1595 #ifdef HAVE_FTRUNCATE |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1596 PyDoc_STRVAR(truncate_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1597 "truncate([size]) -> None. Truncate the file to at most size bytes.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1598 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1599 "Size defaults to the current file position, as returned by tell()."); |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
1600 #endif |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1601 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1602 PyDoc_STRVAR(tell_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1603 "tell() -> current file position, an integer (may be a long integer)."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1604 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1605 PyDoc_STRVAR(readinto_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1606 "readinto() -> Undocumented. Don't use this; it may go away."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1607 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1608 PyDoc_STRVAR(readlines_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1609 "readlines([size]) -> list of strings, each a line from the file.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1610 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1611 "Call readline() repeatedly and return a list of the lines so read.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1612 "The optional size argument, if given, is an approximate bound on the\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1613 "total number of bytes in the lines returned."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1614 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1615 PyDoc_STRVAR(xreadlines_doc, |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1616 "xreadlines() -> returns self.\n" |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1617 "\n" |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1618 "For backward compatibility. File objects now include the performance\n" |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1619 "optimizations previously implemented in the xreadlines module."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1620 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1621 PyDoc_STRVAR(writelines_doc, |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1622 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1623 "\n" |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1624 "Note that newlines are not added. The sequence can be any iterable object\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1625 "producing strings. This is equivalent to calling write() for each string."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1626 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1627 PyDoc_STRVAR(flush_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1628 "flush() -> None. Flush the internal I/O buffer."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1629 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1630 PyDoc_STRVAR(close_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1631 "close() -> None or (perhaps) an integer. Close the file.\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1632 "\n" |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22718
diff
changeset
|
1633 "Sets data attribute .closed to True. A closed file cannot be used for\n" |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1634 "further I/O operations. close() may be called more than once without\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1635 "error. Some kinds of file objects (for example, opened by popen())\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1636 "may return an exit status upon closing."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1637 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1638 PyDoc_STRVAR(isatty_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1639 "isatty() -> true or false. True if the file is connected to a tty device."); |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1640 |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1641 static PyMethodDef file_methods[] = { |
| 24836 | 1642 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
| 1643 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, | |
| 1644 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, | |
| 1645 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, | |
| 1646 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, | |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1647 #ifdef HAVE_FTRUNCATE |
| 24836 | 1648 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1649 #endif |
| 24836 | 1650 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
| 1651 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, | |
| 1652 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc}, | |
| 1653 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc}, | |
| 1654 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc}, | |
| 1655 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, | |
| 1656 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, | |
| 1657 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, | |
| 1658 {NULL, NULL} /* sentinel */ | |
| 6 | 1659 }; |
| 1660 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1661 #define OFF(x) offsetof(PyFileObject, x) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1662 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1663 static PyMemberDef file_memberlist[] = { |
|
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1664 {"softspace", T_INT, OFF(f_softspace), 0, |
|
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1665 "flag indicating that a space needs to be printed; used by print"}, |
|
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1666 {"mode", T_OBJECT, OFF(f_mode), RO, |
|
26596
3084bf8bd11c
Patch #650834: Document 'U' in file mode, remove stale variables.
Martin v. Löwis <martin@v.loewis.de>
parents:
26330
diff
changeset
|
1667 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1668 {"name", T_OBJECT, OFF(f_name), RO, |
|
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
1669 "file name"}, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1670 /* 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
|
1671 {NULL} /* Sentinel */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1672 }; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1673 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1674 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1675 get_closed(PyFileObject *f, void *closure) |
| 6 | 1676 { |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22718
diff
changeset
|
1677 return PyBool_FromLong((long)(f->f_fp == 0)); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1678 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1679 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1680 static PyObject * |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1681 get_newlines(PyFileObject *f, void *closure) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1682 { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1683 switch (f->f_newlinetypes) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1684 case NEWLINE_UNKNOWN: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1685 Py_INCREF(Py_None); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1686 return Py_None; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1687 case NEWLINE_CR: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1688 return PyString_FromString("\r"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1689 case NEWLINE_LF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1690 return PyString_FromString("\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1691 case NEWLINE_CR|NEWLINE_LF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1692 return Py_BuildValue("(ss)", "\r", "\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1693 case NEWLINE_CRLF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1694 return PyString_FromString("\r\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1695 case NEWLINE_CR|NEWLINE_CRLF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1696 return Py_BuildValue("(ss)", "\r", "\r\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1697 case NEWLINE_LF|NEWLINE_CRLF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1698 return Py_BuildValue("(ss)", "\n", "\r\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1699 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF: |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1700 return Py_BuildValue("(sss)", "\r", "\n", "\r\n"); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1701 default: |
| 24836 | 1702 PyErr_Format(PyExc_SystemError, |
| 1703 "Unknown newlines value 0x%x\n", | |
| 1704 f->f_newlinetypes); | |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1705 return NULL; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1706 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1707 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1708 #endif |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
1709 |
|
20015
ca7af2a5d915
Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
1710 static PyGetSetDef file_getsetlist[] = { |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22718
diff
changeset
|
1711 {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1712 #ifdef WITH_UNIVERSAL_NEWLINES |
| 24836 | 1713 {"newlines", (getter)get_newlines, NULL, |
| 1714 "end-of-line convention used in this file"}, | |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1715 #endif |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1716 {0}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1717 }; |
| 6 | 1718 |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1719 static void |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1720 drop_readahead(PyFileObject *f) |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1721 { |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1722 if (f->f_buf != NULL) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1723 PyMem_Free(f->f_buf); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1724 f->f_buf = NULL; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1725 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1726 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1727 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1728 /* Make sure that file has a readahead buffer with at least one byte |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1729 (unless at EOF) and no more than bufsize. Returns negative value on |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1730 error */ |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1731 static int |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1732 readahead(PyFileObject *f, int bufsize) |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1733 { |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1734 int chunksize; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1735 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1736 if (f->f_buf != NULL) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1737 if( (f->f_bufend - f->f_bufptr) >= 1) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1738 return 0; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1739 else |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1740 drop_readahead(f); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1741 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1742 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1743 return -1; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1744 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1745 Py_BEGIN_ALLOW_THREADS |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1746 errno = 0; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1747 chunksize = Py_UniversalNewlineFread( |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1748 f->f_buf, bufsize, f->f_fp, (PyObject *)f); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1749 Py_END_ALLOW_THREADS |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1750 if (chunksize == 0) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1751 if (ferror(f->f_fp)) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1752 PyErr_SetFromErrno(PyExc_IOError); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1753 clearerr(f->f_fp); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1754 drop_readahead(f); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1755 return -1; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1756 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1757 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1758 f->f_bufptr = f->f_buf; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1759 f->f_bufend = f->f_buf + chunksize; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1760 return 0; |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1761 } |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1762 |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1763 /* Used by file_iternext. The returned string will start with 'skip' |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1764 uninitialized bytes followed by the remainder of the line. Don't be |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1765 horrified by the recursive call: maximum recursion depth is limited by |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1766 logarithmic buffer growth to about 50 even when reading a 1gb line. */ |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1767 |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1768 static PyStringObject * |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1769 readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
1770 { |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1771 PyStringObject* s; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1772 char *bufptr; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1773 char *buf; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1774 int len; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1775 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1776 if (f->f_buf == NULL) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1777 if (readahead(f, bufsize) < 0) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1778 return NULL; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1779 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1780 len = f->f_bufend - f->f_bufptr; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1781 if (len == 0) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1782 return (PyStringObject *) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1783 PyString_FromStringAndSize(NULL, skip); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1784 bufptr = memchr(f->f_bufptr, '\n', len); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1785 if (bufptr != NULL) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1786 bufptr++; /* Count the '\n' */ |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1787 len = bufptr - f->f_bufptr; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1788 s = (PyStringObject *) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1789 PyString_FromStringAndSize(NULL, skip+len); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1790 if (s == NULL) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1791 return NULL; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1792 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1793 f->f_bufptr = bufptr; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1794 if (bufptr == f->f_bufend) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1795 drop_readahead(f); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1796 } else { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1797 bufptr = f->f_bufptr; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1798 buf = f->f_buf; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1799 f->f_buf = NULL; /* Force new readahead buffer */ |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1800 s = readahead_get_line_skip( |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1801 f, skip+len, bufsize + (bufsize>>2) ); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1802 if (s == NULL) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1803 PyMem_Free(buf); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1804 return NULL; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1805 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1806 memcpy(PyString_AS_STRING(s)+skip, bufptr, len); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1807 PyMem_Free(buf); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1808 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1809 return s; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1810 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1811 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1812 /* A larger buffer size may actually decrease performance. */ |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1813 #define READAHEAD_BUFSIZE 8192 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1814 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1815 static PyObject * |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1816 file_iternext(PyFileObject *f) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1817 { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1818 PyStringObject* l; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1819 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1820 if (f->f_fp == NULL) |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1821 return err_closed(); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1822 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1823 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1824 if (l == NULL || PyString_GET_SIZE(l) == 0) { |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1825 Py_XDECREF(l); |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1826 return NULL; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1827 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1828 return (PyObject *)l; |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1829 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1830 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1831 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1832 static PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1833 file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1834 { |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1835 PyObject *self; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1836 static PyObject *not_yet_string; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1837 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1838 assert(type != NULL && type->tp_alloc != NULL); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1839 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1840 if (not_yet_string == NULL) { |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1841 not_yet_string = PyString_FromString("<uninitialized file>"); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1842 if (not_yet_string == NULL) |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1843 return NULL; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1844 } |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1845 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1846 self = type->tp_alloc(type, 0); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1847 if (self != NULL) { |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1848 /* Always fill in the name and mode, so that nobody else |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1849 needs to special-case NULLs there. */ |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1850 Py_INCREF(not_yet_string); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1851 ((PyFileObject *)self)->f_name = not_yet_string; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1852 Py_INCREF(not_yet_string); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1853 ((PyFileObject *)self)->f_mode = not_yet_string; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1854 } |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1855 return self; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1856 } |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1857 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1858 static int |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1859 file_init(PyObject *self, PyObject *args, PyObject *kwds) |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1860 { |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1861 PyFileObject *foself = (PyFileObject *)self; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1862 int ret = 0; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1863 static char *kwlist[] = {"name", "mode", "buffering", 0}; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1864 char *name = NULL; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1865 char *mode = "r"; |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1866 int bufsize = -1; |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1867 int wideargument = 0; |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1868 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1869 assert(PyFile_Check(self)); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1870 if (foself->f_fp != NULL) { |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1871 /* Have to close the existing file first. */ |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1872 PyObject *closeresult = file_close(foself); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1873 if (closeresult == NULL) |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1874 return -1; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1875 Py_DECREF(closeresult); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1876 } |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1877 |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1878 #ifdef Py_WIN_WIDE_FILENAMES |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1879 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1880 PyObject *po; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1881 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file", |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1882 kwlist, &po, &mode, &bufsize)) { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1883 wideargument = 1; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1884 if (fill_file_fields(foself, NULL, name, mode, |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1885 fclose, po) == NULL) |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1886 goto Error; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1887 } else { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1888 /* Drop the argument parsing error as narrow |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1889 strings are also valid. */ |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1890 PyErr_Clear(); |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1891 } |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1892 } |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1893 #endif |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1894 |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1895 if (!wideargument) { |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1896 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1897 Py_FileSystemDefaultEncoding, |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1898 &name, |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1899 &mode, &bufsize)) |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1900 return -1; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1901 if (fill_file_fields(foself, NULL, name, mode, |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1902 fclose, NULL) == NULL) |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1903 goto Error; |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
1904 } |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1905 if (open_the_file(foself, name, mode) == NULL) |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1906 goto Error; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1907 PyFile_SetBufSize(self, bufsize); |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1908 goto Done; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1909 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1910 Error: |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1911 ret = -1; |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1912 /* fall through */ |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1913 Done: |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1914 PyMem_Free(name); /* free the encoded string */ |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1915 return ret; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1916 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1917 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1918 PyDoc_VAR(file_doc) = |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1919 PyDoc_STR( |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1920 "file(name[, mode[, buffering]]) -> file object\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1921 "\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1922 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1923 "writing or appending. The file will be created if it doesn't exist\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1924 "when opened for writing or appending; it will be truncated when\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1925 "opened for writing. Add a 'b' to the mode for binary files.\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1926 "Add a '+' to the mode to allow simultaneous reading and writing.\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1927 "If the buffering argument is given, 0 means unbuffered, 1 means line\n" |
|
19903
c082971823ce
Get rid of builtin_open() entirely (the C code and docstring, not the
Tim Peters <tim.peters@gmail.com>
parents:
19901
diff
changeset
|
1928 "buffered, and larger numbers specify the buffer size.\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1929 ) |
|
23514
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1930 #ifdef WITH_UNIVERSAL_NEWLINES |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1931 PyDoc_STR( |
|
23514
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1932 "Add a 'U' to mode to open the file for input with universal newline\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1933 "support. Any line ending in the input file will be seen as a '\\n'\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1934 "in Python. Also, a file so opened gains the attribute 'newlines';\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1935 "the value for this attribute is one of None (no newline read yet),\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1936 "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1937 "\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1938 "'U' cannot be combined with 'w' or '+' mode.\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1939 ) |
|
23514
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1940 #endif /* WITH_UNIVERSAL_NEWLINES */ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1941 PyDoc_STR( |
|
23514
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
1942 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1943 "Note: open() is an alias for file()." |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1944 ); |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1945 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1946 PyTypeObject PyFile_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1947 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 1948 0, |
| 1949 "file", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1950 sizeof(PyFileObject), |
| 6 | 1951 0, |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1952 (destructor)file_dealloc, /* tp_dealloc */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1953 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1954 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1955 0, /* tp_setattr */ |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1956 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1957 (reprfunc)file_repr, /* tp_repr */ |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1958 0, /* tp_as_number */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1959 0, /* tp_as_sequence */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1960 0, /* tp_as_mapping */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1961 0, /* tp_hash */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1962 0, /* tp_call */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1963 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1964 PyObject_GenericGetAttr, /* tp_getattro */ |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1965 0, /* tp_setattro */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1966 0, /* tp_as_buffer */ |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20045
diff
changeset
|
1967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1968 file_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1969 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1970 0, /* tp_clear */ |
|
17609
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1971 0, /* tp_richcompare */ |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
1972 0, /* tp_weaklistoffset */ |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1973 (getiterfunc)file_getiter, /* tp_iter */ |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1974 (iternextfunc)file_iternext, /* tp_iternext */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1975 file_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1976 file_memberlist, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1977 file_getsetlist, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1978 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
1979 0, /* tp_dict */ |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1980 0, /* tp_descr_get */ |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1981 0, /* tp_descr_set */ |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1982 0, /* tp_dictoffset */ |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1983 (initproc)file_init, /* tp_init */ |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
1984 PyType_GenericAlloc, /* tp_alloc */ |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
1985 file_new, /* tp_new */ |
|
22974
72778ec94dec
PyObject_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22873
diff
changeset
|
1986 PyObject_Del, /* tp_free */ |
| 6 | 1987 }; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1988 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1989 /* 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
|
1990 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1991 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1992 PyFile_SoftSpace(PyObject *f, int newflag) |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1993 { |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
1994 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
|
1995 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
|
1996 /* 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
|
1997 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1998 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1999 oldflag = ((PyFileObject *)f)->f_softspace; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2000 ((PyFileObject *)f)->f_softspace = newflag; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2001 } |
|
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
|
2002 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2003 PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2004 v = PyObject_GetAttrString(f, "softspace"); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2005 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2006 PyErr_Clear(); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2007 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2008 if (PyInt_Check(v)) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2009 oldflag = PyInt_AsLong(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2010 Py_DECREF(v); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2011 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2012 v = PyInt_FromLong((long)newflag); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2013 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2014 PyErr_Clear(); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2015 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2016 if (PyObject_SetAttrString(f, "softspace", v) != 0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2017 PyErr_Clear(); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2018 Py_DECREF(v); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2019 } |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2020 } |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2021 return oldflag; |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2022 } |
|
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
|
2023 |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2024 /* 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
|
2025 |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2026 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2027 PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2028 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2029 PyObject *writer, *value, *args, *result; |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2030 if (f == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2031 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2032 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
|
2033 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2034 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2035 FILE *fp = PyFile_AsFile(f); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2036 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
|
2037 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
|
2038 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
|
2039 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2040 return PyObject_Print(v, fp, flags); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2041 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2042 writer = PyObject_GetAttrString(f, "write"); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2043 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
|
2044 return -1; |
|
19985
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2045 if (flags & Py_PRINT_RAW) { |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2046 if (PyUnicode_Check(v)) { |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2047 value = v; |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2048 Py_INCREF(value); |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2049 } else |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2050 value = PyObject_Str(v); |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2051 } |
|
712afdae1123
Patch #462849: Pass Unicode objects to file's .write method.
Martin v. Löwis <martin@v.loewis.de>
parents:
19905
diff
changeset
|
2052 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2053 value = PyObject_Repr(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1389
diff
changeset
|
2054 if (value == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2055 Py_DECREF(writer); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1389
diff
changeset
|
2056 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
|
2057 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2058 args = Py_BuildValue("(O)", value); |
|
5582
87d5d7fa8d07
Fix typo in error checking spotted by Just...
Guido van Rossum <guido@python.org>
parents:
5479
diff
changeset
|
2059 if (args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2060 Py_DECREF(value); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2061 Py_DECREF(writer); |
|
2825
bebb0ea7de20
fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents:
2667
diff
changeset
|
2062 return -1; |
|
bebb0ea7de20
fix read(0), readline(0); make tuple for call_object args
Guido van Rossum <guido@python.org>
parents:
2667
diff
changeset
|
2063 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2064 result = PyEval_CallObject(writer, args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2065 Py_DECREF(args); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2066 Py_DECREF(value); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2067 Py_DECREF(writer); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2068 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
|
2069 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2070 Py_DECREF(result); |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2071 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
|
2072 } |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2073 |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2074 int |
|
21095
b1280cba79b5
PyFile_WriteString(): change prototype so that the string arg is
Tim Peters <tim.peters@gmail.com>
parents:
20942
diff
changeset
|
2075 PyFile_WriteString(const char *s, PyObject *f) |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2076 { |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2077 if (f == NULL) { |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2078 /* Should be caused by a pre-existing error */ |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2079 if (!PyErr_Occurred()) |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2080 PyErr_SetString(PyExc_SystemError, |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2081 "null file for PyFile_WriteString"); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2082 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
|
2083 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2084 else if (PyFile_Check(f)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2085 FILE *fp = PyFile_AsFile(f); |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2086 if (fp == NULL) { |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2087 err_closed(); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2088 return -1; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2089 } |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2090 fputs(s, fp); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2091 return 0; |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2092 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2093 else if (!PyErr_Occurred()) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2094 PyObject *v = PyString_FromString(s); |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2095 int err; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2096 if (v == NULL) |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2097 return -1; |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2098 err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2099 Py_DECREF(v); |
|
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2100 return err; |
|
1073
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2101 } |
|
5783
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
2102 else |
|
9cfbc9bb6423
Reordered list of methods to hopefully put the most frequently used
Guido van Rossum <guido@python.org>
parents:
5605
diff
changeset
|
2103 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
|
2104 } |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2105 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2106 /* Try to get a file-descriptor from a Python object. If the object |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2107 is an integer or long integer, its value is returned. If not, the |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2108 object's fileno() method is called if it exists; the method must return |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2109 an integer or long integer, which is returned as the file descriptor value. |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2110 -1 is returned on failure. |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2111 */ |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2112 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2113 int PyObject_AsFileDescriptor(PyObject *o) |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2114 { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2115 int fd; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2116 PyObject *meth; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2117 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2118 if (PyInt_Check(o)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2119 fd = PyInt_AsLong(o); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2120 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2121 else if (PyLong_Check(o)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2122 fd = PyLong_AsLong(o); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2123 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2124 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2125 { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2126 PyObject *fno = PyEval_CallObject(meth, NULL); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2127 Py_DECREF(meth); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2128 if (fno == NULL) |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2129 return -1; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
2130 |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2131 if (PyInt_Check(fno)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2132 fd = PyInt_AsLong(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2133 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2134 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2135 else if (PyLong_Check(fno)) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2136 fd = PyLong_AsLong(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2137 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2138 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2139 else { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2140 PyErr_SetString(PyExc_TypeError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2141 "fileno() returned a non-integer"); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2142 Py_DECREF(fno); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2143 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2144 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2145 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2146 else { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2147 PyErr_SetString(PyExc_TypeError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2148 "argument must be an int, or have a fileno() method."); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2149 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2150 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2151 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2152 if (fd < 0) { |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2153 PyErr_Format(PyExc_ValueError, |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2154 "file descriptor cannot be a negative integer (%i)", |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2155 fd); |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2156 return -1; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2157 } |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2158 return fd; |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2159 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2160 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2161 #ifdef WITH_UNIVERSAL_NEWLINES |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2162 /* From here on we need access to the real fgets and fread */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2163 #undef fgets |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2164 #undef fread |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2165 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2166 /* |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2167 ** Py_UniversalNewlineFgets is an fgets variation that understands |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2168 ** all of \r, \n and \r\n conventions. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2169 ** The stream should be opened in binary mode. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2170 ** If fobj is NULL the routine always does newline conversion, and |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2171 ** it may peek one char ahead to gobble the second char in \r\n. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2172 ** If fobj is non-NULL it must be a PyFileObject. In this case there |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2173 ** is no readahead but in stead a flag is used to skip a following |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2174 ** \n on the next read. Also, if the file is open in binary mode |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2175 ** the whole conversion is skipped. Finally, the routine keeps track of |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2176 ** the different types of newlines seen. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2177 ** Note that we need no error handling: fgets() treats error and eof |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2178 ** identically. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2179 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2180 char * |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2181 Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2182 { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2183 char *p = buf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2184 int c; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2185 int newlinetypes = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2186 int skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2187 int univ_newline = 1; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2188 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2189 if (fobj) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2190 if (!PyFile_Check(fobj)) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2191 errno = ENXIO; /* What can you do... */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2192 return NULL; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2193 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2194 univ_newline = ((PyFileObject *)fobj)->f_univ_newline; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2195 if ( !univ_newline ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2196 return fgets(buf, n, stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2197 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2198 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2199 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2200 FLOCKFILE(stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2201 c = 'x'; /* Shut up gcc warning */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2202 while (--n > 0 && (c = GETC(stream)) != EOF ) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2203 if (skipnextlf ) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2204 skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2205 if (c == '\n') { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2206 /* Seeing a \n here with skipnextlf true |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2207 ** means we saw a \r before. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2208 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2209 newlinetypes |= NEWLINE_CRLF; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2210 c = GETC(stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2211 if (c == EOF) break; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2212 } else { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2213 /* |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2214 ** Note that c == EOF also brings us here, |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2215 ** so we're okay if the last char in the file |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2216 ** is a CR. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2217 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2218 newlinetypes |= NEWLINE_CR; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2219 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2220 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2221 if (c == '\r') { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2222 /* A \r is translated into a \n, and we skip |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2223 ** an adjacent \n, if any. We don't set the |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2224 ** newlinetypes flag until we've seen the next char. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2225 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2226 skipnextlf = 1; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2227 c = '\n'; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2228 } else if ( c == '\n') { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2229 newlinetypes |= NEWLINE_LF; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2230 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2231 *p++ = c; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2232 if (c == '\n') break; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2233 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2234 if ( c == EOF && skipnextlf ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2235 newlinetypes |= NEWLINE_CR; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2236 FUNLOCKFILE(stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2237 *p = '\0'; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2238 if (fobj) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2239 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2240 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2241 } else if ( skipnextlf ) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2242 /* If we have no file object we cannot save the |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2243 ** skipnextlf flag. We have to readahead, which |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2244 ** will cause a pause if we're reading from an |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2245 ** interactive stream, but that is very unlikely |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2246 ** unless we're doing something silly like |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2247 ** execfile("/dev/tty"). |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2248 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2249 c = GETC(stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2250 if ( c != '\n' ) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2251 ungetc(c, stream); |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2252 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2253 if (p == buf) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2254 return NULL; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2255 return buf; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2256 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2257 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2258 /* |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2259 ** Py_UniversalNewlineFread is an fread variation that understands |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2260 ** all of \r, \n and \r\n conventions. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2261 ** The stream should be opened in binary mode. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2262 ** fobj must be a PyFileObject. In this case there |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2263 ** is no readahead but in stead a flag is used to skip a following |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2264 ** \n on the next read. Also, if the file is open in binary mode |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2265 ** the whole conversion is skipped. Finally, the routine keeps track of |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2266 ** the different types of newlines seen. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2267 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2268 size_t |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2269 Py_UniversalNewlineFread(char *buf, size_t n, |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2270 FILE *stream, PyObject *fobj) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2271 { |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2272 char *dst = buf; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2273 PyFileObject *f = (PyFileObject *)fobj; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2274 int newlinetypes, skipnextlf; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2275 |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2276 assert(buf != NULL); |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2277 assert(stream != NULL); |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2278 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2279 if (!fobj || !PyFile_Check(fobj)) { |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2280 errno = ENXIO; /* What can you do... */ |
|
27768
2d55be3a8ec3
SF patch #683187, fix universal newline problems on error
Neal Norwitz <nnorwitz@gmail.com>
parents:
27060
diff
changeset
|
2281 return 0; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2282 } |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2283 if (!f->f_univ_newline) |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2284 return fread(buf, 1, n, stream); |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2285 newlinetypes = f->f_newlinetypes; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2286 skipnextlf = f->f_skipnextlf; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2287 /* Invariant: n is the number of bytes remaining to be filled |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2288 * in the buffer. |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2289 */ |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2290 while (n) { |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2291 size_t nread; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2292 int shortread; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2293 char *src = dst; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2294 |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2295 nread = fread(dst, 1, n, stream); |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2296 assert(nread <= n); |
|
27768
2d55be3a8ec3
SF patch #683187, fix universal newline problems on error
Neal Norwitz <nnorwitz@gmail.com>
parents:
27060
diff
changeset
|
2297 if (nread == 0) |
|
2d55be3a8ec3
SF patch #683187, fix universal newline problems on error
Neal Norwitz <nnorwitz@gmail.com>
parents:
27060
diff
changeset
|
2298 break; |
|
2d55be3a8ec3
SF patch #683187, fix universal newline problems on error
Neal Norwitz <nnorwitz@gmail.com>
parents:
27060
diff
changeset
|
2299 |
|
23206
fd609d2c1669
Py_UniversalNewlineFread(): small speed boost on non-Windows boxes.
Tim Peters <tim.peters@gmail.com>
parents:
23201
diff
changeset
|
2300 n -= nread; /* assuming 1 byte out for each in; will adjust */ |
|
fd609d2c1669
Py_UniversalNewlineFread(): small speed boost on non-Windows boxes.
Tim Peters <tim.peters@gmail.com>
parents:
23201
diff
changeset
|
2301 shortread = n != 0; /* true iff EOF or error */ |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2302 while (nread--) { |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2303 char c = *src++; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2304 if (c == '\r') { |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2305 /* Save as LF and set flag to skip next LF. */ |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2306 *dst++ = '\n'; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2307 skipnextlf = 1; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2308 } |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2309 else if (skipnextlf && c == '\n') { |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2310 /* Skip LF, and remember we saw CR LF. */ |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2311 skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2312 newlinetypes |= NEWLINE_CRLF; |
|
23206
fd609d2c1669
Py_UniversalNewlineFread(): small speed boost on non-Windows boxes.
Tim Peters <tim.peters@gmail.com>
parents:
23201
diff
changeset
|
2313 ++n; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2314 } |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2315 else { |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2316 /* Normal char to be stored in buffer. Also |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2317 * update the newlinetypes flag if either this |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2318 * is an LF or the previous char was a CR. |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2319 */ |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2320 if (c == '\n') |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2321 newlinetypes |= NEWLINE_LF; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2322 else if (skipnextlf) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2323 newlinetypes |= NEWLINE_CR; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2324 *dst++ = c; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2325 skipnextlf = 0; |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2326 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2327 } |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2328 if (shortread) { |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2329 /* If this is EOF, update type flags. */ |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2330 if (skipnextlf && feof(stream)) |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2331 newlinetypes |= NEWLINE_CR; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2332 break; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2333 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2334 } |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2335 f->f_newlinetypes = newlinetypes; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2336 f->f_skipnextlf = skipnextlf; |
|
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2337 return dst - buf; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2338 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2339 #endif |
