Mercurial > cpython-lru_cache-weakref
annotate Objects/fileobject.c @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | e60ef17561dc |
| children | 5181c3bf8970 |
| rev | line source |
|---|---|
| 6 | 1 /* File object implementation */ |
| 2 | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36485
diff
changeset
|
3 #define PY_SSIZE_T_CLEAN |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
4 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
5 #include "structmember.h" |
| 6 | 6 |
|
38716
42808526511c
Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
38670
diff
changeset
|
7 #ifdef HAVE_SYS_TYPES_H |
|
9809
93d70308be17
Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents:
9789
diff
changeset
|
8 #include <sys/types.h> |
|
38716
42808526511c
Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
38670
diff
changeset
|
9 #endif /* HAVE_SYS_TYPES_H */ |
|
9809
93d70308be17
Need to include <sys/types.h> for off_t.
Guido van Rossum <guido@python.org>
parents:
9789
diff
changeset
|
10 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
11 #ifdef MS_WINDOWS |
|
5414
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
12 #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
|
13 /* 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
|
14 #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
|
15 #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
|
16 #include <windows.h> |
|
5414
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
17 #endif |
|
013ff6951909
Fix by Mark Hammond to enable truncate() on Windows.
Guido van Rossum <guido@python.org>
parents:
5410
diff
changeset
|
18 |
|
22053
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
19 #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
|
20 #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
|
21 #endif |
|
49197fd76362
OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
21712
diff
changeset
|
22 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47413
diff
changeset
|
23 #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
|
24 |
|
59039
77d877d0bf06
#7706: DONT_HAVE_ERRNO_H is no longer defined by configure (after rev.46819).
Andrew M. Kuchling <amk@amk.ca>
parents:
58713
diff
changeset
|
25 #ifdef HAVE_ERRNO_H |
|
1319
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
26 #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
|
27 #endif |
| 6 | 28 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
29 #ifdef HAVE_GETC_UNLOCKED |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
30 #define GETC(f) getc_unlocked(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
31 #define FLOCKFILE(f) flockfile(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
32 #define FUNLOCKFILE(f) funlockfile(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
33 #else |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
34 #define GETC(f) getc(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
35 #define FLOCKFILE(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
36 #define FUNLOCKFILE(f) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
37 #endif |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
38 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
39 /* Bits in f_newlinetypes */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
40 #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
41 #define NEWLINE_CR 1 /* \r newline seen */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
42 #define NEWLINE_LF 2 /* \n newline seen */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
43 #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
44 |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
45 /* |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
46 * These macros release the GIL while preventing the f_close() function being |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
47 * called in the interval between them. For that purpose, a running total of |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
48 * the number of currently running unlocked code sections is kept in |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
49 * the unlocked_count field of the PyFileObject. The close() method raises |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
50 * an IOError if that field is non-zero. See issue #815646, #595601. |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
51 */ |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
52 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
53 #define FILE_BEGIN_ALLOW_THREADS(fobj) \ |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
54 { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
55 fobj->unlocked_count++; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
56 Py_BEGIN_ALLOW_THREADS |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
57 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
58 #define FILE_END_ALLOW_THREADS(fobj) \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
59 Py_END_ALLOW_THREADS \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
60 fobj->unlocked_count--; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
61 assert(fobj->unlocked_count >= 0); \ |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
62 } |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
63 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
64 #define FILE_ABORT_ALLOW_THREADS(fobj) \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
65 Py_BLOCK_THREADS \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
66 fobj->unlocked_count--; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
67 assert(fobj->unlocked_count >= 0); |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
68 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
69 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
70 extern "C" { |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
71 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
72 |
| 6 | 73 FILE * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
74 PyFile_AsFile(PyObject *f) |
| 6 | 75 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
76 if (f == NULL || !PyFile_Check(f)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
77 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
78 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
79 return ((PyFileObject *)f)->f_fp; |
| 6 | 80 } |
| 81 | |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
82 void PyFile_IncUseCount(PyFileObject *fobj) |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
83 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
84 fobj->unlocked_count++; |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
85 } |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
86 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
87 void PyFile_DecUseCount(PyFileObject *fobj) |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
88 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
89 fobj->unlocked_count--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
90 assert(fobj->unlocked_count >= 0); |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
91 } |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
92 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
93 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
94 PyFile_Name(PyObject *f) |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
95 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
96 if (f == NULL || !PyFile_Check(f)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
97 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
98 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
99 return ((PyFileObject *)f)->f_name; |
|
1354
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
100 } |
|
fccd415e2eb8
* bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
101 |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
102 /* This is a safe wrapper around PyObject_Print to print to the FILE |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
103 of a PyFileObject. PyObject_Print releases the GIL but knows nothing |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
104 about PyFileObject. */ |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
105 static int |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
106 file_PyObject_Print(PyObject *op, PyFileObject *f, int flags) |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
107 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
108 int result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
109 PyFile_IncUseCount(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
110 result = PyObject_Print(op, f->f_fp, flags); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
111 PyFile_DecUseCount(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
112 return result; |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
113 } |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
114 |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
115 /* 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
|
116 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
|
117 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
|
118 |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
119 static PyFileObject* |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
120 dircheck(PyFileObject* f) |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
121 { |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
122 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
123 struct stat buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
124 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
125 return f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
126 if (fstat(fileno(f->f_fp), &buf) == 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
127 S_ISDIR(buf.st_mode)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
128 char *msg = strerror(EISDIR); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
129 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
130 EISDIR, msg, f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
131 PyErr_SetObject(PyExc_IOError, exc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
132 Py_XDECREF(exc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
133 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
134 } |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
135 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
136 return f; |
|
22471
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
137 } |
|
acf5ecd806af
Check in (hopefully) corrected version of last change.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22467
diff
changeset
|
138 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
139 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
140 static PyObject * |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
141 fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
142 int (*close)(FILE *)) |
| 6 | 143 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
144 assert(name != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
145 assert(f != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
146 assert(PyFile_Check(f)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
147 assert(f->f_fp == NULL); |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
148 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
149 Py_DECREF(f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
150 Py_DECREF(f->f_mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
151 Py_DECREF(f->f_encoding); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
152 Py_DECREF(f->f_errors); |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
153 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
154 Py_INCREF(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
155 f->f_name = name; |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
156 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
157 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
|
158 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
159 f->f_close = close; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
160 f->f_softspace = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
161 f->f_binary = strchr(mode,'b') != NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
162 f->f_buf = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
163 f->f_univ_newline = (strchr(mode, 'U') != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
164 f->f_newlinetypes = NEWLINE_UNKNOWN; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
165 f->f_skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
166 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
167 f->f_encoding = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
168 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
169 f->f_errors = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
170 f->readable = f->writable = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
171 if (strchr(mode, 'r') != NULL || f->f_univ_newline) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
172 f->readable = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
173 if (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
174 f->writable = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
175 if (strchr(mode, '+') != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
176 f->readable = f->writable = 1; |
|
30364
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
177 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
178 if (f->f_mode == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
179 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
180 f->f_fp = fp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
181 f = dircheck(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
182 return (PyObject *) f; |
| 6 | 183 } |
| 184 | |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
185 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
186 #define Py_VERIFY_WINNT |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
187 /* The CRT on windows compiled with Visual Studio 2005 and higher may |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
188 * assert if given invalid mode strings. This is all fine and well |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
189 * in static languages like C where the mode string is typcially hard |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
190 * coded. But in Python, were we pass in the mode string from the user, |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
191 * we need to verify it first manually |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
192 */ |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
193 static int _PyVerify_Mode_WINNT(const char *mode) |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
194 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
195 /* See if mode string is valid on Windows to avoid hard assertions */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
196 /* remove leading spacese */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
197 int singles = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
198 int pairs = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
199 int encoding = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
200 const char *s, *c; |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
201 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
202 while(*mode == ' ') /* strip initial spaces */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
203 ++mode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
204 if (!strchr("rwa", *mode)) /* must start with one of these */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
205 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
206 while (*++mode) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
207 if (*mode == ' ' || *mode == 'N') /* ignore spaces and N */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
208 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
209 s = "+TD"; /* each of this can appear only once */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
210 c = strchr(s, *mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
211 if (c) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
212 ptrdiff_t idx = s-c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
213 if (singles & (1<<idx)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
214 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
215 singles |= (1<<idx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
216 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
217 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
218 s = "btcnSR"; /* only one of each letter in the pairs allowed */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
219 c = strchr(s, *mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
220 if (c) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
221 ptrdiff_t idx = (s-c)/2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
222 if (pairs & (1<<idx)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
223 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
224 pairs |= (1<<idx); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
225 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
226 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
227 if (*mode == ',') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
228 encoding = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
229 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
230 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
231 return 0; /* found an invalid char */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
232 } |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
233 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
234 if (encoding) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
235 char *e[] = {"UTF-8", "UTF-16LE", "UNICODE"}; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
236 while (*mode == ' ') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
237 ++mode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
238 /* find 'ccs =' */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
239 if (strncmp(mode, "ccs", 3)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
240 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
241 mode += 3; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
242 while (*mode == ' ') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
243 ++mode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
244 if (*mode != '=') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
245 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
246 while (*mode == ' ') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
247 ++mode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
248 for(encoding = 0; encoding<_countof(e); ++encoding) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
249 size_t l = strlen(e[encoding]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
250 if (!strncmp(mode, e[encoding], l)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
251 mode += l; /* found a valid encoding */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
252 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
253 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
254 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
255 if (encoding == _countof(e)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
256 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
257 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
258 /* skip trailing spaces */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
259 while (*mode == ' ') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
260 ++mode; |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
261 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
262 return *mode == '\0'; /* must be at the end of the string */ |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
263 } |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
264 #endif |
|
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
265 |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
266 /* check for known incorrect mode strings - problem is, platforms are |
|
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
267 free to accept any mode characters they like and are supposed to |
|
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
268 ignore stuff they don't understand... write or append mode with |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
269 universal newline support is expressly forbidden by PEP 278. |
|
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
270 Additionally, remove the 'U' from the mode string as platforms |
|
41546
d9a487c2fa96
Export function sanitize_the_mode from fileobject.c as _PyFile_SanitizeMode(). Use this function in posixmodule.c when implementing fdopen(). This fixes test_subprocess.py for a VisualStudio 2005 compile.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40699
diff
changeset
|
271 won't know what it is. Non-zero return signals an exception */ |
|
d9a487c2fa96
Export function sanitize_the_mode from fileobject.c as _PyFile_SanitizeMode(). Use this function in posixmodule.c when implementing fdopen(). This fixes test_subprocess.py for a VisualStudio 2005 compile.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40699
diff
changeset
|
272 int |
|
d9a487c2fa96
Export function sanitize_the_mode from fileobject.c as _PyFile_SanitizeMode(). Use this function in posixmodule.c when implementing fdopen(). This fixes test_subprocess.py for a VisualStudio 2005 compile.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40699
diff
changeset
|
273 _PyFile_SanitizeMode(char *mode) |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
274 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
275 char *upos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
276 size_t len = strlen(mode); |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
277 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
278 if (!len) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
279 PyErr_SetString(PyExc_ValueError, "empty mode string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
280 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
281 } |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
282 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
283 upos = strchr(mode, 'U'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
284 if (upos) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
285 memmove(upos, upos+1, len-(upos-mode)); /* incl null char */ |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
286 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
287 if (mode[0] == 'w' || mode[0] == 'a') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
288 PyErr_Format(PyExc_ValueError, "universal newline " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
289 "mode can only be used with modes " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
290 "starting with 'r'"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
291 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
292 } |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
293 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
294 if (mode[0] != 'r') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
295 memmove(mode+1, mode, strlen(mode)+1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
296 mode[0] = 'r'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
297 } |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
298 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
299 if (!strchr(mode, 'b')) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
300 memmove(mode+2, mode+1, strlen(mode)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
301 mode[1] = 'b'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
302 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
303 } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
304 PyErr_Format(PyExc_ValueError, "mode string must begin with " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
305 "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
306 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
307 } |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
308 #ifdef Py_VERIFY_WINNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
309 /* additional checks on NT with visual studio 2005 and higher */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
310 if (!_PyVerify_Mode_WINNT(mode)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
311 PyErr_Format(PyExc_ValueError, "Invalid mode ('%.50s')", mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
312 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
313 } |
|
51311
b586ca3a3062
issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50424
diff
changeset
|
314 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
315 return 0; |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
316 } |
|
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
317 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
318 static PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
319 open_the_file(PyFileObject *f, char *name, char *mode) |
| 6 | 320 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
321 char *newmode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
322 assert(f != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
323 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
|
324 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
325 /* windows ignores the passed name in order to support Unicode */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
326 assert(f->f_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
|
327 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
328 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
|
329 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
330 assert(mode != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
331 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
|
332 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
333 /* probably need to replace 'U' by 'rb' */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
334 newmode = PyMem_MALLOC(strlen(mode) + 3); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
335 if (!newmode) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
336 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
337 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
338 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
339 strcpy(newmode, mode); |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
340 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
341 if (_PyFile_SanitizeMode(newmode)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
342 f = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
343 goto cleanup; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
344 } |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
33927
diff
changeset
|
345 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
346 /* rexec.py can't stop a user from getting the file() constructor -- |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
347 all they have to do is get *any* file object f, and then do |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
348 type(f). Here we prevent them from doing damage with it. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
349 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
350 PyErr_SetString(PyExc_IOError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
351 "file() constructor not accessible in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
352 f = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
353 goto cleanup; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
354 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
355 errno = 0; |
|
32322
50f3a550b4fe
dump HAVE_FOPENRF stuff - obsolete
Skip Montanaro <skip@pobox.com>
parents:
32159
diff
changeset
|
356 |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
357 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
358 if (PyUnicode_Check(f->f_name)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
359 PyObject *wmode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
360 wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
361 if (f->f_name && wmode) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
362 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
363 /* PyUnicode_AS_UNICODE OK without thread |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
364 lock as it is a simple dereference. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
365 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
366 PyUnicode_AS_UNICODE(wmode)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
367 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
368 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
369 Py_XDECREF(wmode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
370 } |
|
32322
50f3a550b4fe
dump HAVE_FOPENRF stuff - obsolete
Skip Montanaro <skip@pobox.com>
parents:
32159
diff
changeset
|
371 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
372 if (NULL == f->f_fp && NULL != name) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
373 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
374 f->f_fp = fopen(name, newmode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
375 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
376 } |
|
32322
50f3a550b4fe
dump HAVE_FOPENRF stuff - obsolete
Skip Montanaro <skip@pobox.com>
parents:
32159
diff
changeset
|
377 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
378 if (f->f_fp == NULL) { |
|
38999
75bdb1eb0766
Fix build problems with the platform SDK on windows. It is not sufficient to test for the C compiler version when determining if we have the secure CRT from microsoft. Must test with an undocumented macro, __STDC_SECURE_LIB__ too.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
38779
diff
changeset
|
379 #if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__)) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
380 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
381 * across all Windows flavors. When it sets EINVAL varies |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
382 * across Windows flavors, the exact conditions aren't |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
383 * documented, and the answer lies in the OS's implementation |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
384 * of Win32's CreateFile function (whose source is secret). |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
385 * Seems the best we can do is map EINVAL to ENOENT. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
386 * Starting with Visual Studio .NET 2005, EINVAL is correctly |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
387 * set by our CRT error handler (set in exceptions.c.) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
388 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
389 if (errno == 0) /* bad mode string */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
390 errno = EINVAL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
391 else if (errno == EINVAL) /* unknown, but not a mode string */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
392 errno = ENOENT; |
|
22873
8d337b3b655a
SF bug 538827: Python open w/ MSVC6: bad error msgs.
Tim Peters <tim.peters@gmail.com>
parents:
22864
diff
changeset
|
393 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
394 /* EINVAL is returned when an invalid filename or |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
395 * an invalid mode is supplied. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
396 if (errno == EINVAL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
397 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
398 char message[100]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
399 PyOS_snprintf(message, 100, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
400 "invalid mode ('%.50s') or filename", mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
401 v = Py_BuildValue("(isO)", errno, message, f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
402 if (v != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
403 PyErr_SetObject(PyExc_IOError, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
404 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
405 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
406 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
407 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
408 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
409 f = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
410 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
411 if (f != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
412 f = dircheck(f); |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
413 |
|
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
414 cleanup: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
415 PyMem_FREE(newmode); |
|
38212
42d6bab88079
Bug #1462152: file() now checks more thoroughly for invalid mode
Georg Brandl <georg@python.org>
parents:
38084
diff
changeset
|
416 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
417 return (PyObject *)f; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
418 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
419 |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
420 static PyObject * |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
421 close_the_file(PyFileObject *f) |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
422 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
423 int sts = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
424 int (*local_close)(FILE *); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
425 FILE *local_fp = f->f_fp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
426 if (local_fp != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
427 local_close = f->f_close; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
428 if (local_close != NULL && f->unlocked_count > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
429 if (f->ob_refcnt > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
430 PyErr_SetString(PyExc_IOError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
431 "close() called during concurrent " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
432 "operation on the same file object."); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
433 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
434 /* This should not happen unless someone is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
435 * carelessly playing with the PyFileObject |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
436 * struct fields and/or its associated FILE |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
437 * pointer. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
438 PyErr_SetString(PyExc_SystemError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
439 "PyFileObject locking error in " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
440 "destructor (refcnt <= 0 at close)."); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
441 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
442 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
443 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
444 /* NULL out the FILE pointer before releasing the GIL, because |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
445 * it will not be valid anymore after the close() function is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
446 * called. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
447 f->f_fp = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
448 if (local_close != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
449 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
450 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
451 sts = (*local_close)(local_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
452 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
453 if (sts == EOF) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
454 return PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
455 if (sts != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
456 return PyInt_FromLong((long)sts); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
457 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
458 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
459 Py_RETURN_NONE; |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
460 } |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
461 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
462 PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
463 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
|
464 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
465 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
466 NULL, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
467 if (f != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
468 PyObject *o_name = PyString_FromString(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
469 if (o_name == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
470 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
471 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
472 Py_DECREF(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
473 f = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
474 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
475 Py_DECREF(o_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
476 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
477 return (PyObject *) f; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
478 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
479 |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
480 PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
481 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
|
482 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
483 extern int fclose(FILE *); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
484 PyFileObject *f; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
485 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
486 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
487 if (f != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
488 if (open_the_file(f, name, mode) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
489 Py_DECREF(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
490 f = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
491 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
492 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
493 return (PyObject *)f; |
| 6 | 494 } |
| 495 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
496 void |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
497 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
|
498 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
499 PyFileObject *file = (PyFileObject *)f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
500 if (bufsize >= 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
501 int type; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
502 switch (bufsize) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
503 case 0: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
504 type = _IONBF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
505 break; |
|
30320
521d026a8048
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Martin v. Löwis <martin@v.loewis.de>
parents:
30169
diff
changeset
|
506 #ifdef HAVE_SETVBUF |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
507 case 1: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
508 type = _IOLBF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
509 bufsize = BUFSIZ; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
510 break; |
|
30320
521d026a8048
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Martin v. Löwis <martin@v.loewis.de>
parents:
30169
diff
changeset
|
511 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
512 default: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
513 type = _IOFBF; |
|
30320
521d026a8048
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Martin v. Löwis <martin@v.loewis.de>
parents:
30169
diff
changeset
|
514 #ifndef HAVE_SETVBUF |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
515 bufsize = BUFSIZ; |
|
30320
521d026a8048
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Martin v. Löwis <martin@v.loewis.de>
parents:
30169
diff
changeset
|
516 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
517 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
518 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
519 fflush(file->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
520 if (type == _IONBF) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
521 PyMem_Free(file->f_setbuf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
522 file->f_setbuf = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
523 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
524 file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
525 bufsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
526 } |
|
30320
521d026a8048
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Martin v. Löwis <martin@v.loewis.de>
parents:
30169
diff
changeset
|
527 #ifdef HAVE_SETVBUF |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
528 setvbuf(file->f_fp, file->f_setbuf, 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
|
529 #else /* !HAVE_SETVBUF */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
530 setbuf(file->f_fp, file->f_setbuf); |
|
7572
c9f417124e0f
When we have no setvbuf(), make the file totally unbuffered using
Guido van Rossum <guido@python.org>
parents:
7536
diff
changeset
|
531 #endif /* !HAVE_SETVBUF */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
532 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
533 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
534 |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
535 /* Set the encoding used to output Unicode strings. |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
536 Return 1 on success, 0 on failure. */ |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
537 |
|
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
538 int |
|
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
539 PyFile_SetEncoding(PyObject *f, const char *enc) |
|
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
540 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
541 return PyFile_SetEncodingAndErrors(f, enc, NULL); |
|
47413
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
542 } |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
543 |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
544 int |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
545 PyFile_SetEncodingAndErrors(PyObject *f, const char *enc, char* errors) |
|
1062b7dec95b
New environment variable PYTHONIOENCODING.
Martin v. Löwis <martin@v.loewis.de>
parents:
47294
diff
changeset
|
546 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
547 PyFileObject *file = (PyFileObject*)f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
548 PyObject *str, *oerrors; |
|
40699
86ab2f20a333
SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize
Thomas Wouters <thomas@python.org>
parents:
40335
diff
changeset
|
549 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
550 assert(PyFile_Check(f)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
551 str = PyString_FromString(enc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
552 if (!str) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
553 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
554 if (errors) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
555 oerrors = PyString_FromString(errors); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
556 if (!oerrors) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
557 Py_DECREF(str); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
558 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
559 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
560 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
561 oerrors = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
562 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
563 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
564 Py_DECREF(file->f_encoding); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
565 file->f_encoding = str; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
566 Py_DECREF(file->f_errors); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
567 file->f_errors = oerrors; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
568 return 1; |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
569 } |
|
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
570 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
571 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
572 err_closed(void) |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
573 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
574 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
575 return NULL; |
|
908
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
576 } |
|
424f55e67d5d
Makefile: added instann rules and $(*DEST*) defines.
Guido van Rossum <guido@python.org>
parents:
902
diff
changeset
|
577 |
|
58713
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
54975
diff
changeset
|
578 static PyObject * |
|
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
54975
diff
changeset
|
579 err_mode(char *action) |
|
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
54975
diff
changeset
|
580 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
581 PyErr_Format(PyExc_IOError, "File not open for %s", action); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
582 return NULL; |
|
58713
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
54975
diff
changeset
|
583 } |
|
7c933683010d
Issue #5677: Explicitly forbid write operations on read-only file objects,
Antoine Pitrou <solipsis@pitrou.net>
parents:
54975
diff
changeset
|
584 |
|
36485
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
585 /* Refuse regular file I/O if there's data in the iteration-buffer. |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
586 * Mixing them would cause data to arrive out of order, as the read* |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
587 * methods don't use the iteration buffer. */ |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
588 static PyObject * |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
589 err_iterbuffered(void) |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
590 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
591 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
592 "Mixing iteration and read methods would lose data"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
593 return NULL; |
|
36485
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
594 } |
|
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36243
diff
changeset
|
595 |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
596 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
|
597 |
| 6 | 598 /* Methods */ |
| 599 | |
| 600 static void | |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
601 file_dealloc(PyFileObject *f) |
| 6 | 602 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
603 PyObject *ret; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
604 if (f->weakreflist != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
605 PyObject_ClearWeakRefs((PyObject *) f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
606 ret = close_the_file(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
607 if (!ret) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
608 PySys_WriteStderr("close failed in file object destructor:\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
609 PyErr_Print(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
610 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
611 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
612 Py_DECREF(ret); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
613 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
614 PyMem_Free(f->f_setbuf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
615 Py_XDECREF(f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
616 Py_XDECREF(f->f_mode); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
617 Py_XDECREF(f->f_encoding); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
618 Py_XDECREF(f->f_errors); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
619 drop_readahead(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
620 Py_TYPE(f)->tp_free((PyObject *)f); |
| 6 | 621 } |
| 622 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
623 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
624 file_repr(PyFileObject *f) |
| 6 | 625 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
626 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
|
627 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
628 PyObject *ret = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
629 PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
630 const char *name_str = name ? PyString_AsString(name) : "?"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
631 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
632 f->f_fp == NULL ? "closed" : "open", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
633 name_str, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
634 PyString_AsString(f->f_mode), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
635 f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
636 Py_XDECREF(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
637 return ret; |
|
26330
3086e111783a
Fix --disable-unicode compilation problems.
Martin v. Löwis <martin@v.loewis.de>
parents:
25535
diff
changeset
|
638 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
639 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
640 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
641 f->f_fp == NULL ? "closed" : "open", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
642 PyString_AsString(f->f_name), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
643 PyString_AsString(f->f_mode), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
644 f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
645 } |
| 6 | 646 } |
| 647 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
648 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
|
649 file_close(PyFileObject *f) |
| 6 | 650 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
651 PyObject *sts = close_the_file(f); |
|
61677
f0fcd69187cd
Issue #7079: Fix a possible crash when closing a file object while using
Antoine Pitrou <solipsis@pitrou.net>
parents:
61463
diff
changeset
|
652 if (sts) { |
|
f0fcd69187cd
Issue #7079: Fix a possible crash when closing a file object while using
Antoine Pitrou <solipsis@pitrou.net>
parents:
61463
diff
changeset
|
653 PyMem_Free(f->f_setbuf); |
|
f0fcd69187cd
Issue #7079: Fix a possible crash when closing a file object while using
Antoine Pitrou <solipsis@pitrou.net>
parents:
61463
diff
changeset
|
654 f->f_setbuf = NULL; |
|
f0fcd69187cd
Issue #7079: Fix a possible crash when closing a file object while using
Antoine Pitrou <solipsis@pitrou.net>
parents:
61463
diff
changeset
|
655 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
656 return sts; |
| 6 | 657 } |
| 658 | |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
659 |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
660 /* 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
|
661 #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
|
662 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
|
663 #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
|
664 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
|
665 #elif SIZEOF_FPOS_T >= 8 |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
666 typedef fpos_t Py_off_t; |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
667 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
668 #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
|
669 #endif |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
670 |
|
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
671 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
672 /* 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
|
673 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
|
674 static int |
|
16903
e86875f069b1
Two improvements to large file support:
Guido van Rossum <guido@python.org>
parents:
15978
diff
changeset
|
675 _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
|
676 { |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
677 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
678 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
|
679 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
680 return fseeko(fp, offset, 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
|
681 #elif defined(HAVE_FSEEK64) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
682 return fseek64(fp, offset, whence); |
| 15068 | 683 #elif defined(__BEOS__) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
684 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
|
685 #elif SIZEOF_FPOS_T >= 8 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
686 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
687 and fgetpos() to implement fseek()*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
688 fpos_t pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
689 switch (whence) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
690 case SEEK_END: |
| 19836 | 691 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
692 fflush(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
693 if (_lseeki64(fileno(fp), 0, 2) == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
694 return -1; |
| 19836 | 695 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
696 if (fseek(fp, 0, SEEK_END) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
697 return -1; |
| 19836 | 698 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
699 /* fall through */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
700 case SEEK_CUR: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
701 if (fgetpos(fp, &pos) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
702 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
703 offset += pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
704 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
705 /* case SEEK_SET: break; */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
706 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
707 return fsetpos(fp, &offset); |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
708 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
709 #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
|
710 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
711 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
712 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
713 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
714 /* 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
|
715 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
|
716 position on success */ |
|
17518
f939ce52a734
Make some private symbols static.
Guido van Rossum <guido@python.org>
parents:
16903
diff
changeset
|
717 static Py_off_t |
|
14277
984abd20eec2
Peter Schneider-Kamp <nowonder@nowonder.de>:
Fred Drake <fdrake@acm.org>
parents:
14215
diff
changeset
|
718 _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
|
719 { |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
720 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
721 return ftell(fp); |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
722 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
723 return ftello(fp); |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
724 #elif defined(HAVE_FTELL64) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
725 return ftell64(fp); |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
726 #elif SIZEOF_FPOS_T >= 8 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
727 fpos_t pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
728 if (fgetpos(fp, &pos) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
729 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
730 return 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
|
731 #else |
|
19732
b7dc5159448f
Changes to automatically enable large file support on some systems.
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
732 #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
|
733 #endif |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
734 } |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
735 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
736 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
737 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
738 file_seek(PyFileObject *f, PyObject *args) |
| 6 | 739 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
740 int whence; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
741 int ret; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
742 Py_off_t offset; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
743 PyObject *offobj, *off_index; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
744 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
745 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
746 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
747 drop_readahead(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
748 whence = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
749 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
750 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
751 off_index = PyNumber_Index(offobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
752 if (!off_index) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
753 if (!PyFloat_Check(offobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
754 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
755 /* Deprecated in 2.6 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
756 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
757 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
758 "integer argument expected, got float", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
759 1) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
760 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
761 off_index = offobj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
762 Py_INCREF(offobj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
763 } |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
764 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
765 offset = PyInt_AsLong(off_index); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
766 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
767 offset = PyLong_Check(off_index) ? |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
768 PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
769 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
770 Py_DECREF(off_index); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
771 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
772 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
|
773 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
774 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
775 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
776 ret = _portable_fseek(f->f_fp, offset, whence); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
777 FILE_END_ALLOW_THREADS(f) |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
778 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
779 if (ret != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
780 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
781 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
782 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
783 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
784 f->f_skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
785 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
786 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
787 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
788 |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
789 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
790 #ifdef HAVE_FTRUNCATE |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
791 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
792 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
|
793 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
794 Py_off_t newsize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
795 PyObject *newsizeobj = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
796 Py_off_t initialpos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
797 int ret; |
|
15754
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
798 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
799 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
800 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
801 if (!f->writable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
802 return err_mode("writing"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
803 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
804 return NULL; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
805 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
806 /* Get current file position. If the file happens to be open for |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
807 * update and the last operation was an input operation, C doesn't |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
808 * define what the later fflush() will do, but we promise truncate() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
809 * won't change the current position (and fflush() *does* change it |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
810 * then at least on Windows). The easiest thing is to capture |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
811 * current pos now and seek back to it at the end. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
812 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
813 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
814 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
815 initialpos = _portable_ftell(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
816 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
817 if (initialpos == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
818 goto onioerror; |
|
30364
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
819 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
820 /* Set newsize to current postion if newsizeobj NULL, else to the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
821 * specified value. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
822 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
823 if (newsizeobj != NULL) { |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
824 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
825 newsize = PyInt_AsLong(newsizeobj); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
826 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
827 newsize = PyLong_Check(newsizeobj) ? |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
828 PyLong_AsLongLong(newsizeobj) : |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
829 PyInt_AsLong(newsizeobj); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
830 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
831 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
832 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
833 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
834 else /* default to current position */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
835 newsize = initialpos; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
836 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
837 /* Flush the stream. We're mixing stream-level I/O with lower-level |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
838 * I/O, and a flush may be necessary to synch both platform views |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
839 * of the current file state. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
840 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
841 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
842 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
843 ret = fflush(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
844 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
845 if (ret != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
846 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
|
847 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
848 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
849 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
850 so don't even try using it. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
851 { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
852 HANDLE hFile; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
853 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
854 /* Have to move current pos to desired endpoint on Windows. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
855 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
856 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
857 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
858 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
859 if (ret) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
860 goto onioerror; |
|
22214
e24ba04d01b0
file_truncate(): provide full "large file" support on Windows, by
Tim Peters <tim.peters@gmail.com>
parents:
22053
diff
changeset
|
861 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
862 /* Truncate. Note that this may grow the file! */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
863 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
864 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
865 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
866 ret = hFile == (HANDLE)-1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
867 if (ret == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
868 ret = SetEndOfFile(hFile) == 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
869 if (ret) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
870 errno = EACCES; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
871 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
872 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
873 if (ret) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
874 goto onioerror; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
875 } |
|
13924
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
876 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
877 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
878 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
879 ret = ftruncate(fileno(f->f_fp), newsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
880 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
881 if (ret != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
882 goto onioerror; |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23832
diff
changeset
|
883 #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
|
884 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
885 /* Restore original file position. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
886 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
887 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
888 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
889 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
890 if (ret) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
891 goto onioerror; |
|
30364
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
892 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
893 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
894 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
|
895 |
|
ddf302d69211
Add largefile support for Linux64 and WIn64. Add test_largefile and some minor
Trent Mick <trentm@activestate.com>
parents:
13591
diff
changeset
|
896 onioerror: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
897 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
898 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
899 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
|
900 } |
|
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
901 #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
|
902 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
903 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
|
904 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
|
905 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
906 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
|
907 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
908 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
909 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
910 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
911 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
912 pos = _portable_ftell(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
913 FILE_END_ALLOW_THREADS(f) |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
914 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
915 if (pos == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
916 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
917 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
918 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
919 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
920 if (f->f_skipnextlf) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
921 int c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
922 c = GETC(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
923 if (c == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
924 f->f_newlinetypes |= NEWLINE_CRLF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
925 pos++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
926 f->f_skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
927 } else if (c != EOF) ungetc(c, f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
928 } |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
929 #if !defined(HAVE_LARGEFILE_SUPPORT) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
930 return PyInt_FromLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
931 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
932 return PyLong_FromLongLong(pos); |
|
9789
08dfbee0a32a
Changes for long file support by Steve Clift.
Guido van Rossum <guido@python.org>
parents:
9762
diff
changeset
|
933 #endif |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
934 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
935 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
936 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
|
937 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
|
938 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
939 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
940 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
941 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
|
942 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
943 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
944 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
|
945 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
|
946 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
947 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
|
948 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
949 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
950 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
951 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
952 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
953 res = fflush(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
954 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
955 if (res != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
956 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
957 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
958 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
959 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
960 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
961 return Py_None; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
962 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
963 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
964 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
|
965 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
|
966 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
967 long res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
968 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
969 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
970 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
971 res = isatty((int)fileno(f->f_fp)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
972 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
973 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
|
974 } |
|
f4a944e9835f
In support of popen(), file objects are now parametrized with a close
Guido van Rossum <guido@python.org>
parents:
251
diff
changeset
|
975 |
|
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
|
976 |
|
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
|
977 #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
|
978 #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
|
979 #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
|
980 #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
|
981 #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
|
982 |
|
9883
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
983 #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
|
984 #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
|
985 #else |
|
7195ec296b3d
Jim Ahlstrom patch: BIGCHUNK is too large for 16-bit int.
Guido van Rossum <guido@python.org>
parents:
9809
diff
changeset
|
986 #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
|
987 #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
|
988 |
|
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
|
989 static size_t |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
990 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
|
991 { |
|
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
|
992 #ifdef HAVE_FSTAT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
993 off_t pos, end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
994 struct stat st; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
995 if (fstat(fileno(f->f_fp), &st) == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
996 end = st.st_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
997 /* The following is not a bug: we really need to call lseek() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
998 *and* ftell(). The reason is that some stdio libraries |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
999 mistakenly flush their buffer when ftell() is called and |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1000 the lseek() call it makes fails, thereby throwing away |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1001 data that cannot be recovered in any way. To avoid this, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1002 we first test lseek(), and only call ftell() if lseek() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1003 works. We can't use the lseek() value either, because we |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1004 need to take the amount of buffered data into account. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1005 (Yet another reason why stdio stinks. :-) */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1006 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1007 if (pos >= 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1008 pos = ftell(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1009 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1010 if (pos < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1011 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1012 if (end > pos && pos >= 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1013 return currentsize + end - pos + 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1014 /* Add 1 so if the file were to grow we'd notice. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1015 } |
|
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
|
1016 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1017 if (currentsize > SMALLCHUNK) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1018 /* Keep doubling until we reach BIGCHUNK; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1019 then keep adding BIGCHUNK. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1020 if (currentsize <= BIGCHUNK) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1021 return currentsize + currentsize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1022 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1023 return currentsize + BIGCHUNK; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1024 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1025 return currentsize + SMALLCHUNK; |
|
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
|
1026 } |
|
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
|
1027 |
| 26672 | 1028 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
| 1029 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN) | |
| 1030 #else | |
| 1031 #ifdef EWOULDBLOCK | |
| 1032 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK) | |
| 1033 #else | |
| 1034 #ifdef EAGAIN | |
| 1035 #define BLOCKED_ERRNO(x) ((x) == EAGAIN) | |
| 1036 #else | |
| 1037 #define BLOCKED_ERRNO(x) 0 | |
| 1038 #endif | |
| 1039 #endif | |
| 1040 #endif | |
| 1041 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1042 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1043 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
|
1044 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1045 long bytesrequested = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1046 size_t bytesread, buffersize, chunksize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1047 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
|
1048 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1049 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1050 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1051 if (!f->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1052 return err_mode("reading"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1053 /* refuse to mix with f.next() */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1054 if (f->f_buf != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1055 (f->f_bufend - f->f_bufptr) > 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1056 f->f_buf[0] != '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1057 return err_iterbuffered(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1058 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1059 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1060 if (bytesrequested < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1061 buffersize = new_buffersize(f, (size_t)0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1062 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1063 buffersize = bytesrequested; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1064 if (buffersize > PY_SSIZE_T_MAX) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1065 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1066 "requested number of bytes is more than a Python string can hold"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1067 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1068 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1069 v = PyString_FromStringAndSize((char *)NULL, buffersize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1070 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1071 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1072 bytesread = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1073 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1074 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1075 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1076 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1077 buffersize - bytesread, f->f_fp, (PyObject *)f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1078 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1079 if (chunksize == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1080 if (!ferror(f->f_fp)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1081 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1082 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1083 /* When in non-blocking mode, data shouldn't |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1084 * be discarded if a blocking signal was |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1085 * received. That will also happen if |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1086 * chunksize != 0, but bytesread < buffersize. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1087 if (bytesread > 0 && BLOCKED_ERRNO(errno)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1088 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1089 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1090 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1091 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1092 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1093 bytesread += chunksize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1094 if (bytesread < buffersize) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1095 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1096 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1097 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1098 if (bytesrequested < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1099 buffersize = new_buffersize(f, buffersize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1100 if (_PyString_Resize(&v, buffersize) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1101 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1102 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1103 /* Got what was requested. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1104 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1105 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1106 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1107 if (bytesread != buffersize && _PyString_Resize(&v, bytesread)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1108 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1109 return v; |
| 6 | 1110 } |
| 1111 | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1112 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1113 file_readinto(PyFileObject *f, PyObject *args) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1114 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1115 char *ptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1116 Py_ssize_t ntodo; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1117 Py_ssize_t ndone, nnow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1118 Py_buffer pbuf; |
|
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
|
1119 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1120 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1121 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1122 if (!f->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1123 return err_mode("reading"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1124 /* refuse to mix with f.next() */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1125 if (f->f_buf != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1126 (f->f_bufend - f->f_bufptr) > 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1127 f->f_buf[0] != '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1128 return err_iterbuffered(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1129 if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1130 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1131 ptr = pbuf.buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1132 ntodo = pbuf.len; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1133 ndone = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1134 while (ntodo > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1135 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1136 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1137 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1138 (PyObject *)f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1139 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1140 if (nnow == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1141 if (!ferror(f->f_fp)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1142 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1143 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1144 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1145 PyBuffer_Release(&pbuf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1146 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1147 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1148 ndone += nnow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1149 ntodo -= nnow; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1150 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1151 PyBuffer_Release(&pbuf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1152 return PyInt_FromSsize_t(ndone); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1153 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1154 |
|
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
|
1155 /************************************************************************** |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1156 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
|
1157 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1158 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
|
1159 |
|
15758
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
1160 + 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
|
1161 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
|
1162 + 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
|
1163 code. |
|
a1d88ab023a0
A few reformats; no logic changes.
Tim Peters <tim.peters@gmail.com>
parents:
15757
diff
changeset
|
1164 + 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
|
1165 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
|
1166 + 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
|
1167 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1168 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
|
1169 |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1170 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
|
1171 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1172 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
|
1173 have): |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1174 Linux a wash |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1175 Solaris a wash |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1176 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
|
1177 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1178 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
|
1179 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
|
1180 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
|
1181 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
|
1182 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
|
1183 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
|
1184 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
|
1185 **************************************************************************/ |
|
7c8fac4ac31a
MS Win32 .readline() speedup, as discussed on Python-Dev. This is a tricky
Tim Peters <tim.peters@gmail.com>
parents:
15753
diff
changeset
|
1186 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1187 /* 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
|
1188 * 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
|
1189 * 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
|
1190 * 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
|
1191 * 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
|
1192 * 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
|
1193 * 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
|
1194 * 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
|
1195 */ |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1196 #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
|
1197 #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
|
1198 #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
|
1199 |
|
15875
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1200 #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
|
1201 #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
|
1202 #endif |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1203 |
|
195557a0f050
Use the "MS" getline hack (fgets()) by default on non-get_unlocked
Tim Peters <tim.peters@gmail.com>
parents:
15780
diff
changeset
|
1204 #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
|
1205 static PyObject* |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
1206 getline_via_fgets(PyFileObject *f, 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
|
1207 { |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
1208 /* 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
|
1209 * 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
|
1210 * 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
|
1211 * 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
|
1212 * 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
|
1213 * 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
|
1214 * 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
|
1215 * 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
|
1216 * 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
|
1217 * 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
|
1218 * 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
|
1219 * 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
|
1220 */ |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
1221 #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
|
1222 #define MAXBUFSIZE 300 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1223 char* p; /* temp */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1224 char buf[MAXBUFSIZE]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1225 PyObject* v; /* the string object result */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1226 char* pvfree; /* address of next free slot */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1227 char* pvend; /* address one beyond last free slot */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1228 size_t nfree; /* # of free buffer slots; pvend-pvfree */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1229 size_t total_v_size; /* total # of slots in buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1230 size_t increment; /* amount to increment the buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1231 size_t prev_v_size; |
|
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
|
1232 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1233 /* Optimize for normal case: avoid _PyString_Resize if at all |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1234 * possible via first reading into stack buffer "buf". |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1235 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1236 total_v_size = INITBUFSIZE; /* start small and pray */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1237 pvfree = buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1238 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1239 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1240 pvend = buf + total_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1241 nfree = pvend - pvfree; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1242 memset(pvfree, '\n', nfree); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1243 assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1244 p = fgets(pvfree, (int)nfree, fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1245 FILE_END_ALLOW_THREADS(f) |
|
15755
a43a8a1f46ee
Fiddled ms_getline_hack after talking w/ Guido: made clearer that the
Tim Peters <tim.peters@gmail.com>
parents:
15754
diff
changeset
|
1246 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1247 if (p == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1248 clearerr(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1249 if (PyErr_CheckSignals()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1250 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1251 v = PyString_FromStringAndSize(buf, pvfree - buf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1252 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1253 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1254 /* fgets read *something* */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1255 p = memchr(pvfree, '\n', nfree); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1256 if (p != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1257 /* Did the \n come from fgets or from us? |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1258 * Since fgets stops at the first \n, and then writes |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1259 * \0, if it's from fgets a \0 must be next. But if |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1260 * that's so, it could not have come from us, since |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1261 * the \n's we filled the buffer with have only more |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1262 * \n's to the right. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1263 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1264 if (p+1 < pvend && *(p+1) == '\0') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1265 /* It's from fgets: we win! In particular, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1266 * we haven't done any mallocs yet, and can |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1267 * build the final result on the first try. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1268 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1269 ++p; /* include \n from fgets */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1270 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1271 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1272 /* Must be from us: fgets didn't fill the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1273 * buffer and didn't find a newline, so it |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1274 * must be the last and newline-free line of |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1275 * the file. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1276 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1277 assert(p > pvfree && *(p-1) == '\0'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1278 --p; /* don't include \0 from fgets */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1279 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1280 v = PyString_FromStringAndSize(buf, p - buf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1281 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1282 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1283 /* yuck: fgets overwrote all the newlines, i.e. the entire |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1284 * buffer. So this line isn't over yet, or maybe it is but |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1285 * we're exactly at EOF. If we haven't already, try using the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1286 * rest of the stack buffer. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1287 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1288 assert(*(pvend-1) == '\0'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1289 if (pvfree == buf) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1290 pvfree = pvend - 1; /* overwrite trailing null */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1291 total_v_size = MAXBUFSIZE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1292 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1293 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1294 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1295 } |
|
15876
a33de23b3066
Speed getline_via_fgets(), by supplying two "fast paths", although one is
Tim Peters <tim.peters@gmail.com>
parents:
15875
diff
changeset
|
1296 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1297 /* The stack buffer isn't big enough; malloc a string object and read |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1298 * into its buffer. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1299 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1300 total_v_size = MAXBUFSIZE << 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1301 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1302 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1303 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1304 /* copy over everything except the last null byte */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1305 memcpy(BUF(v), buf, MAXBUFSIZE-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1306 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
|
1307 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1308 /* Keep reading stuff into v; if it ever ends successfully, break |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1309 * after setting p one beyond the end of the line. The code here is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1310 * very much like the code above, except reads into v's buffer; see |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1311 * the code above for detailed comments about the logic. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1312 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1313 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1314 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1315 pvend = BUF(v) + total_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1316 nfree = pvend - pvfree; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1317 memset(pvfree, '\n', nfree); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1318 assert(nfree < INT_MAX); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1319 p = fgets(pvfree, (int)nfree, fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1320 FILE_END_ALLOW_THREADS(f) |
|
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
|
1321 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1322 if (p == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1323 clearerr(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1324 if (PyErr_CheckSignals()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1325 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1326 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1327 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1328 p = pvfree; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1329 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1330 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1331 p = memchr(pvfree, '\n', nfree); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1332 if (p != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1333 if (p+1 < pvend && *(p+1) == '\0') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1334 /* \n came from fgets */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1335 ++p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1336 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1337 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1338 /* \n came from us; last line of file, no newline */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1339 assert(p > pvfree && *(p-1) == '\0'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1340 --p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1341 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1342 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1343 /* expand buffer and try again */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1344 assert(*(pvend-1) == '\0'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1345 increment = total_v_size >> 2; /* mild exponential growth */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1346 prev_v_size = total_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1347 total_v_size += increment; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1348 /* check for overflow */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1349 if (total_v_size <= prev_v_size || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1350 total_v_size > PY_SSIZE_T_MAX) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1351 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1352 "line is longer than a Python string can hold"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1353 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1354 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1355 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1356 if (_PyString_Resize(&v, (int)total_v_size) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1357 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1358 /* overwrite the trailing null byte */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1359 pvfree = BUF(v) + (prev_v_size - 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1360 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1361 if (BUF(v) + total_v_size != p && _PyString_Resize(&v, p - BUF(v))) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1362 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1363 return 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
|
1364 #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
|
1365 #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
|
1366 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1367 #endif /* ifdef USE_FGETS_IN_GETLINE */ |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1368 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1369 /* 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
|
1370 Size argument interpretation: |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1371 > 0: max length; |
|
15757
a9e15bcab32c
Let's hope that three time's a charm...
Guido van Rossum <guido@python.org>
parents:
15755
diff
changeset
|
1372 <= 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
|
1373 */ |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1374 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1375 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1376 get_line(PyFileObject *f, int n) |
| 6 | 1377 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1378 FILE *fp = f->f_fp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1379 int c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1380 char *buf, *end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1381 size_t total_v_size; /* total # of slots in buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1382 size_t used_v_size; /* # used slots in buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1383 size_t increment; /* amount to increment the buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1384 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1385 int newlinetypes = f->f_newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1386 int skipnextlf = f->f_skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1387 int univ_newline = f->f_univ_newline; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1388 |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
1389 #if defined(USE_FGETS_IN_GETLINE) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1390 if (n <= 0 && !univ_newline ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1391 return getline_via_fgets(f, 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
|
1392 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1393 total_v_size = n > 0 ? n : 100; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1394 v = PyString_FromStringAndSize((char *)NULL, total_v_size); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1395 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1396 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1397 buf = BUF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1398 end = buf + total_v_size; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
1399 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1400 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1401 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1402 FLOCKFILE(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1403 if (univ_newline) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1404 c = 'x'; /* Shut up gcc warning */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1405 while ( buf != end && (c = GETC(fp)) != EOF ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1406 if (skipnextlf ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1407 skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1408 if (c == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1409 /* Seeing a \n here with |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1410 * skipnextlf true means we |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1411 * saw a \r before. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1412 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1413 newlinetypes |= NEWLINE_CRLF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1414 c = GETC(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1415 if (c == EOF) break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1416 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1417 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1418 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1419 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1420 if (c == '\r') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1421 skipnextlf = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1422 c = '\n'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1423 } else if ( c == '\n') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1424 newlinetypes |= NEWLINE_LF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1425 *buf++ = c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1426 if (c == '\n') break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1427 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1428 if ( c == EOF && skipnextlf ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1429 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1430 } else /* If not universal newlines use the normal loop */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1431 while ((c = GETC(fp)) != EOF && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1432 (*buf++ = c) != '\n' && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1433 buf != end) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1434 ; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1435 FUNLOCKFILE(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1436 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1437 f->f_newlinetypes = newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1438 f->f_skipnextlf = skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1439 if (c == '\n') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1440 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1441 if (c == EOF) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1442 if (ferror(fp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1443 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1444 clearerr(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1445 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1446 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1447 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1448 clearerr(fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1449 if (PyErr_CheckSignals()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1450 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1451 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1452 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1453 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1454 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1455 /* Must be because buf == end */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1456 if (n > 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1457 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1458 used_v_size = total_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1459 increment = total_v_size >> 2; /* mild exponential growth */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1460 total_v_size += increment; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1461 if (total_v_size > PY_SSIZE_T_MAX) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1462 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1463 "line is longer than a Python string can hold"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1464 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1465 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1466 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1467 if (_PyString_Resize(&v, total_v_size) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1468 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1469 buf = BUF(v) + used_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1470 end = BUF(v) + total_v_size; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1471 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
916
diff
changeset
|
1472 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1473 used_v_size = buf - BUF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1474 if (used_v_size != total_v_size && _PyString_Resize(&v, used_v_size)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1475 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1476 return v; |
|
154
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1477 } |
|
f2efceaf73cc
Fixed read() and readline() to do arbitrarily long reads.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1478 |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1479 /* External C interface */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1480 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1481 PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1482 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
|
1483 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1484 PyObject *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
|
1485 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1486 if (f == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1487 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1488 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1489 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1490 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1491 if (PyFile_Check(f)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1492 PyFileObject *fo = (PyFileObject *)f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1493 if (fo->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1494 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1495 if (!fo->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1496 return err_mode("reading"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1497 /* refuse to mix with f.next() */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1498 if (fo->f_buf != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1499 (fo->f_bufend - fo->f_bufptr) > 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1500 fo->f_buf[0] != '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1501 return err_iterbuffered(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1502 result = get_line(fo, n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1503 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1504 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1505 PyObject *reader; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1506 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
|
1507 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1508 reader = PyObject_GetAttrString(f, "readline"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1509 if (reader == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1510 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1511 if (n <= 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1512 args = PyTuple_New(0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1513 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1514 args = Py_BuildValue("(i)", n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1515 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1516 Py_DECREF(reader); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1517 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1518 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1519 result = PyEval_CallObject(reader, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1520 Py_DECREF(reader); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1521 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1522 if (result != NULL && !PyString_Check(result) && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1523 !PyUnicode_Check(result)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1524 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1525 result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1526 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1527 "object.readline() returned non-string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1528 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1529 } |
|
15753
8d00f7555eec
Tim noticed that I had botched get_line_raw(). Looking again, I
Guido van Rossum <guido@python.org>
parents:
15745
diff
changeset
|
1530 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1531 if (n < 0 && result != NULL && PyString_Check(result)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1532 char *s = PyString_AS_STRING(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1533 Py_ssize_t len = PyString_GET_SIZE(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1534 if (len == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1535 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1536 result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1537 PyErr_SetString(PyExc_EOFError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1538 "EOF when reading a line"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1539 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1540 else if (s[len-1] == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1541 if (result->ob_refcnt == 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1542 if (_PyString_Resize(&result, len-1)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1543 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1544 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1545 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1546 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1547 v = PyString_FromStringAndSize(s, len-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1548 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1549 result = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1550 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1551 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1552 } |
|
27060
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1553 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1554 if (n < 0 && result != NULL && PyUnicode_Check(result)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1555 Py_UNICODE *s = PyUnicode_AS_UNICODE(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1556 Py_ssize_t len = PyUnicode_GET_SIZE(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1557 if (len == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1558 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1559 result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1560 PyErr_SetString(PyExc_EOFError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1561 "EOF when reading a line"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1562 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1563 else if (s[len-1] == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1564 if (result->ob_refcnt == 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1565 PyUnicode_Resize(&result, len-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1566 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1567 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1568 v = PyUnicode_FromUnicode(s, len-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1569 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1570 result = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1571 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1572 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1573 } |
|
27060
cf90309efeee
Allow PyFile_GetLine() to return Unicode objects. Fixes #660165.
Martin v. Löwis <martin@v.loewis.de>
parents:
26818
diff
changeset
|
1574 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1575 return result; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1576 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1577 |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1578 /* Python method */ |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1579 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1580 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1581 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
|
1582 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1583 int n = -1; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1584 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1585 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1586 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1587 if (!f->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1588 return err_mode("reading"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1589 /* refuse to mix with f.next() */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1590 if (f->f_buf != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1591 (f->f_bufend - f->f_bufptr) > 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1592 f->f_buf[0] != '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1593 return err_iterbuffered(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1594 if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1595 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1596 if (n == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1597 return PyString_FromString(""); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1598 if (n < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1599 n = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1600 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
|
1601 } |
|
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1602 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1603 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1604 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
|
1605 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1606 long sizehint = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1607 PyObject *list = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1608 PyObject *line; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1609 char small_buffer[SMALLCHUNK]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1610 char *buffer = small_buffer; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1611 size_t buffersize = SMALLCHUNK; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1612 PyObject *big_buffer = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1613 size_t nfilled = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1614 size_t nread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1615 size_t totalread = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1616 char *p, *q, *end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1617 int err; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1618 int shortread = 0; |
|
183
c9168afe23dd
Added external interface to readline, for raw_input().
Guido van Rossum <guido@python.org>
parents:
177
diff
changeset
|
1619 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1620 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1621 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1622 if (!f->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1623 return err_mode("reading"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1624 /* refuse to mix with f.next() */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1625 if (f->f_buf != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1626 (f->f_bufend - f->f_bufptr) > 0 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1627 f->f_buf[0] != '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1628 return err_iterbuffered(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1629 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1630 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1631 if ((list = PyList_New(0)) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1632 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1633 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1634 if (shortread) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1635 nread = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1636 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1637 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1638 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1639 nread = Py_UniversalNewlineFread(buffer+nfilled, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1640 buffersize-nfilled, f->f_fp, (PyObject *)f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1641 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1642 shortread = (nread < buffersize-nfilled); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1643 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1644 if (nread == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1645 sizehint = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1646 if (!ferror(f->f_fp)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1647 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1648 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1649 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1650 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1651 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1652 totalread += nread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1653 p = (char *)memchr(buffer+nfilled, '\n', nread); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1654 if (p == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1655 /* Need a larger buffer to fit this line */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1656 nfilled += nread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1657 buffersize *= 2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1658 if (buffersize > PY_SSIZE_T_MAX) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1659 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1660 "line is longer than a Python string can hold"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1661 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1662 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1663 if (big_buffer == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1664 /* Create the big buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1665 big_buffer = PyString_FromStringAndSize( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1666 NULL, buffersize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1667 if (big_buffer == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1668 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1669 buffer = PyString_AS_STRING(big_buffer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1670 memcpy(buffer, small_buffer, nfilled); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1671 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1672 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1673 /* Grow the big buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1674 if ( _PyString_Resize(&big_buffer, buffersize) < 0 ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1675 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1676 buffer = PyString_AS_STRING(big_buffer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1677 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1678 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1679 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1680 end = buffer+nfilled+nread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1681 q = buffer; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1682 do { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1683 /* Process complete lines */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1684 p++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1685 line = PyString_FromStringAndSize(q, p-q); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1686 if (line == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1687 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1688 err = PyList_Append(list, line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1689 Py_DECREF(line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1690 if (err != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1691 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1692 q = p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1693 p = (char *)memchr(q, '\n', end-q); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1694 } while (p != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1695 /* Move the remaining incomplete line to the start */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1696 nfilled = end-q; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1697 memmove(buffer, q, nfilled); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1698 if (sizehint > 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1699 if (totalread >= (size_t)sizehint) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1700 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1701 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1702 if (nfilled != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1703 /* Partial last line */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1704 line = PyString_FromStringAndSize(buffer, nfilled); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1705 if (line == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1706 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1707 if (sizehint > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1708 /* Need to complete the last line */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1709 PyObject *rest = get_line(f, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1710 if (rest == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1711 Py_DECREF(line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1712 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1713 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1714 PyString_Concat(&line, rest); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1715 Py_DECREF(rest); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1716 if (line == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1717 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1718 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1719 err = PyList_Append(list, line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1720 Py_DECREF(line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1721 if (err != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1722 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1723 } |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
1724 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
1725 cleanup: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1726 Py_XDECREF(big_buffer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1727 return list; |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
1728 |
|
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
1729 error: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1730 Py_CLEAR(list); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1731 goto cleanup; |
| 6 | 1732 } |
| 1733 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1734 static PyObject * |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1735 file_write(PyFileObject *f, PyObject *args) |
| 6 | 1736 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1737 Py_buffer pbuf; |
|
64556
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1738 const char *s; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1739 Py_ssize_t n, n2; |
|
64556
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1740 PyObject *encoded = NULL; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1741 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1742 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1743 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1744 if (!f->writable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1745 return err_mode("writing"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1746 if (f->f_binary) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1747 if (!PyArg_ParseTuple(args, "s*", &pbuf)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1748 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1749 s = pbuf.buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1750 n = pbuf.len; |
|
64556
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1751 } |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1752 else { |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1753 const char *encoding, *errors; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1754 PyObject *text; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1755 if (!PyArg_ParseTuple(args, "O", &text)) |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1756 return NULL; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1757 |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1758 if (PyString_Check(text)) { |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1759 s = PyString_AS_STRING(text); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1760 n = PyString_GET_SIZE(text); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1761 } else if (PyUnicode_Check(text)) { |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1762 if (f->f_encoding != Py_None) |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1763 encoding = PyString_AS_STRING(f->f_encoding); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1764 else |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1765 encoding = PyUnicode_GetDefaultEncoding(); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1766 if (f->f_errors != Py_None) |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1767 errors = PyString_AS_STRING(f->f_errors); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1768 else |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1769 errors = "strict"; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1770 encoded = PyUnicode_AsEncodedString(text, encoding, errors); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1771 if (encoded == NULL) |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1772 return NULL; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1773 s = PyString_AS_STRING(encoded); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1774 n = PyString_GET_SIZE(encoded); |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1775 } else { |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1776 if (PyObject_AsCharBuffer(text, &s, &n)) |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1777 return NULL; |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1778 } |
|
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1779 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1780 f->f_softspace = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1781 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1782 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1783 n2 = fwrite(s, 1, n, f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1784 FILE_END_ALLOW_THREADS(f) |
|
64556
e60ef17561dc
Issue #4947: The write() method of sys.stdout and sys.stderr uses their
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61677
diff
changeset
|
1785 Py_XDECREF(encoded); |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1786 if (f->f_binary) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1787 PyBuffer_Release(&pbuf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1788 if (n2 != n) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1789 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1790 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1791 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1792 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1793 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1794 return Py_None; |
| 6 | 1795 } |
| 1796 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
1797 static PyObject * |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1798 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
|
1799 { |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1800 #define CHUNKSIZE 1000 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1801 PyObject *list, *line; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1802 PyObject *it; /* iter(seq) */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1803 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1804 int index, islist; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1805 Py_ssize_t i, j, nwritten, len; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1806 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1807 assert(seq != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1808 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1809 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1810 if (!f->writable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1811 return err_mode("writing"); |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1812 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1813 result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1814 list = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1815 islist = PyList_Check(seq); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1816 if (islist) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1817 it = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1818 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1819 it = PyObject_GetIter(seq); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1820 if (it == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1821 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1822 "writelines() requires an iterable argument"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1823 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1824 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1825 /* From here on, fail by going to error, to reclaim "it". */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1826 list = PyList_New(CHUNKSIZE); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1827 if (list == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1828 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1829 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1830 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1831 /* Strategy: slurp CHUNKSIZE lines into a private list, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1832 checking that they are all strings, then write that list |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1833 without holding the interpreter lock, then come back for more. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1834 for (index = 0; ; index += CHUNKSIZE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1835 if (islist) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1836 Py_XDECREF(list); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1837 list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1838 if (list == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1839 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1840 j = PyList_GET_SIZE(list); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1841 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1842 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1843 for (j = 0; j < CHUNKSIZE; j++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1844 line = PyIter_Next(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1845 if (line == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1846 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1847 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1848 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1849 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1850 PyList_SetItem(list, j, line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1851 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1852 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1853 if (j == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1854 break; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1855 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1856 /* Check that all entries are indeed strings. If not, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1857 apply the same rules as for file.write() and |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1858 convert the results to strings. This is slow, but |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1859 seems to be the only way since all conversion APIs |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1860 could potentially execute Python code. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1861 for (i = 0; i < j; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1862 PyObject *v = PyList_GET_ITEM(list, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1863 if (!PyString_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1864 const char *buffer; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1865 if (((f->f_binary && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1866 PyObject_AsReadBuffer(v, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1867 (const void**)&buffer, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1868 &len)) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1869 PyObject_AsCharBuffer(v, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1870 &buffer, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1871 &len))) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1872 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1873 "writelines() argument must be a sequence of strings"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1874 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1875 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1876 line = PyString_FromStringAndSize(buffer, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1877 len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1878 if (line == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1879 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1880 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1881 PyList_SET_ITEM(list, i, line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1882 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1883 } |
|
14213
63ea2a2df06f
Fix to bug [ Bug #111860 ] file.writelines() crashes.
Marc-André Lemburg <mal@egenix.com>
parents:
14125
diff
changeset
|
1884 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1885 /* Since we are releasing the global lock, the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1886 following code may *not* execute Python code. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1887 f->f_softspace = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1888 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1889 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1890 for (i = 0; i < j; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1891 line = PyList_GET_ITEM(list, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1892 len = PyString_GET_SIZE(line); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1893 nwritten = fwrite(PyString_AS_STRING(line), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1894 1, len, f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1895 if (nwritten != len) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1896 FILE_ABORT_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1897 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1898 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1899 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1900 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1901 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1902 FILE_END_ALLOW_THREADS(f) |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1903 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1904 if (j < CHUNKSIZE) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1905 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1906 } |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1907 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1908 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1909 result = Py_None; |
|
12000
8651f2da7c4c
Checking in the new, improve file.writelines() code.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
1910 error: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1911 Py_XDECREF(list); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1912 Py_XDECREF(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1913 return result; |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
1914 #undef CHUNKSIZE |
|
1362
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1915 } |
|
560407ec632b
* filemodule.c: added writelines() -- analogous to readlines()
Guido van Rossum <guido@python.org>
parents:
1354
diff
changeset
|
1916 |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1917 static PyObject * |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1918 file_self(PyFileObject *f) |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1919 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1920 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1921 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1922 Py_INCREF(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1923 return (PyObject *)f; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1924 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
1925 |
|
38670
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
1926 static PyObject * |
|
47148
3d5cddc6e5e7
#2353: raise Py3k warning in file.xreadlines().
Georg Brandl <georg@python.org>
parents:
46578
diff
changeset
|
1927 file_xreadlines(PyFileObject *f) |
|
3d5cddc6e5e7
#2353: raise Py3k warning in file.xreadlines().
Georg Brandl <georg@python.org>
parents:
46578
diff
changeset
|
1928 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1929 if (PyErr_WarnPy3k("f.xreadlines() not supported in 3.x, " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1930 "try 'for line in f' instead", 1) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1931 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1932 return file_self(f); |
|
47148
3d5cddc6e5e7
#2353: raise Py3k warning in file.xreadlines().
Georg Brandl <georg@python.org>
parents:
46578
diff
changeset
|
1933 } |
|
3d5cddc6e5e7
#2353: raise Py3k warning in file.xreadlines().
Georg Brandl <georg@python.org>
parents:
46578
diff
changeset
|
1934 |
|
3d5cddc6e5e7
#2353: raise Py3k warning in file.xreadlines().
Georg Brandl <georg@python.org>
parents:
46578
diff
changeset
|
1935 static PyObject * |
|
45453
930b2802c037
#2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents:
45028
diff
changeset
|
1936 file_exit(PyObject *f, PyObject *args) |
|
38670
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
1937 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1938 PyObject *ret = PyObject_CallMethod(f, "close", NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1939 if (!ret) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1940 /* If error occurred, pass through */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1941 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1942 Py_DECREF(ret); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1943 /* We cannot return the result of close since a true |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1944 * value will be interpreted as "yes, swallow the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1945 * exception if one was raised inside the with block". */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
1946 Py_RETURN_NONE; |
|
38670
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
1947 } |
|
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
1948 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1949 PyDoc_STRVAR(readline_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1950 "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
|
1951 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1952 "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
|
1953 "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
|
1954 "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
|
1955 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1956 PyDoc_STRVAR(read_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1957 "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
|
1958 "\n" |
| 26672 | 1959 "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 1960 "Notice that when in non-blocking mode, less data than what was requested\n" | |
| 1961 "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
|
1962 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1963 PyDoc_STRVAR(write_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1964 "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
|
1965 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1966 "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
|
1967 "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
|
1968 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1969 PyDoc_STRVAR(fileno_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1970 "fileno() -> integer \"file descriptor\".\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1971 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1972 "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
|
1973 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1974 PyDoc_STRVAR(seek_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1975 "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
|
1976 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1977 "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
|
1978 "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
|
1979 "(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
|
1980 "relative to end of file, usually negative, although many platforms allow\n" |
|
30705
3ff03446f362
Patch #809535: Mention behaviour of seek on text files. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
1981 "seeking beyond the end of a file). If the file is opened in text mode,\n" |
|
3ff03446f362
Patch #809535: Mention behaviour of seek on text files. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
1982 "only offsets returned by tell() are legal. Use of other offsets causes\n" |
|
3ff03446f362
Patch #809535: Mention behaviour of seek on text files. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
1983 "undefined behavior." |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1984 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1985 "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
|
1986 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
1987 #ifdef HAVE_FTRUNCATE |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1988 PyDoc_STRVAR(truncate_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1989 "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
|
1990 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1991 "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
|
1992 #endif |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
1993 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1994 PyDoc_STRVAR(tell_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1995 "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
|
1996 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1997 PyDoc_STRVAR(readinto_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
1998 "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
|
1999 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2000 PyDoc_STRVAR(readlines_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
2001 "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
|
2002 "\n" |
|
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
2003 "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
|
2004 "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
|
2005 "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
|
2006 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2007 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
|
2008 "xreadlines() -> returns self.\n" |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
2009 "\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
|
2010 "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
|
2011 "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
|
2012 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2013 PyDoc_STRVAR(writelines_doc, |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
2014 "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
|
2015 "\n" |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20015
diff
changeset
|
2016 "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
|
2017 "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
|
2018 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2019 PyDoc_STRVAR(flush_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2020 "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
|
2021 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2022 PyDoc_STRVAR(close_doc, |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
2023 "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
|
2024 "\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
|
2025 "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
|
2026 "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
|
2027 "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
|
2028 "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
|
2029 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2030 PyDoc_STRVAR(isatty_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2031 "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
|
2032 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
2033 PyDoc_STRVAR(enter_doc, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2034 "__enter__() -> self."); |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
2035 |
|
38670
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
2036 PyDoc_STRVAR(exit_doc, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2037 "__exit__(*excinfo) -> None. Closes the file."); |
|
38670
24930e3b6061
Bug #1502805: don't alias file.__exit__ to file.close since the
Georg Brandl <georg@python.org>
parents:
38212
diff
changeset
|
2038 |
|
19996
646e24728852
SF bug [#463093] File methods need doc strings.
Tim Peters <tim.peters@gmail.com>
parents:
19985
diff
changeset
|
2039 static PyMethodDef file_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2040 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2041 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2042 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2043 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2044 {"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
|
2045 #ifdef HAVE_FTRUNCATE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2046 {"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
|
2047 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2048 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2049 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2050 {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2051 {"xreadlines",(PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2052 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2053 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2054 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2055 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2056 {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2057 {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2058 {NULL, NULL} /* sentinel */ |
| 6 | 2059 }; |
| 2060 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2061 #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
|
2062 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19996
diff
changeset
|
2063 static PyMemberDef file_memberlist[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2064 {"mode", T_OBJECT, OFF(f_mode), RO, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2065 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2066 {"name", T_OBJECT, OFF(f_name), RO, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2067 "file name"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2068 {"encoding", T_OBJECT, OFF(f_encoding), RO, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2069 "file encoding"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2070 {"errors", T_OBJECT, OFF(f_errors), RO, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2071 "Unicode error handler"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2072 /* getattr(f, "closed") is implemented without this table */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2073 {NULL} /* Sentinel */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
2074 }; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
2075 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2076 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
2077 get_closed(PyFileObject *f, void *closure) |
| 6 | 2078 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2079 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
|
2080 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2081 static PyObject * |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2082 get_newlines(PyFileObject *f, void *closure) |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2083 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2084 switch (f->f_newlinetypes) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2085 case NEWLINE_UNKNOWN: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2086 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2087 return Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2088 case NEWLINE_CR: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2089 return PyString_FromString("\r"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2090 case NEWLINE_LF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2091 return PyString_FromString("\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2092 case NEWLINE_CR|NEWLINE_LF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2093 return Py_BuildValue("(ss)", "\r", "\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2094 case NEWLINE_CRLF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2095 return PyString_FromString("\r\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2096 case NEWLINE_CR|NEWLINE_CRLF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2097 return Py_BuildValue("(ss)", "\r", "\r\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2098 case NEWLINE_LF|NEWLINE_CRLF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2099 return Py_BuildValue("(ss)", "\n", "\r\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2100 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2101 return Py_BuildValue("(sss)", "\r", "\n", "\r\n"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2102 default: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2103 PyErr_Format(PyExc_SystemError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2104 "Unknown newlines value 0x%x\n", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2105 f->f_newlinetypes); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2106 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2107 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2108 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1400
diff
changeset
|
2109 |
|
45985
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2110 static PyObject * |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2111 get_softspace(PyFileObject *f, void *closure) |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2112 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2113 if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2114 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2115 return PyInt_FromLong(f->f_softspace); |
|
45985
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2116 } |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2117 |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2118 static int |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2119 set_softspace(PyFileObject *f, PyObject *value) |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2120 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2121 int new; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2122 if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2123 return -1; |
|
45985
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2124 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2125 if (value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2126 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2127 "can't delete softspace attribute"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2128 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2129 } |
|
45985
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2130 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2131 new = PyInt_AsLong(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2132 if (new == -1 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2133 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2134 f->f_softspace = new; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2135 return 0; |
|
45985
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2136 } |
|
bf07bad1d72f
#2348: add py3k warning for file.softspace.
Georg Brandl <georg@python.org>
parents:
45854
diff
changeset
|
2137 |
|
20015
ca7af2a5d915
Add optional docstrings to getset descriptors. Fortunately, there's
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
2138 static PyGetSetDef file_getsetlist[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2139 {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2140 {"newlines", (getter)get_newlines, NULL, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2141 "end-of-line convention used in this file"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2142 {"softspace", (getter)get_softspace, (setter)set_softspace, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2143 "flag indicating that a space needs to be printed; used by print"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2144 {0}, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18755
diff
changeset
|
2145 }; |
| 6 | 2146 |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2147 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
|
2148 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
|
2149 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2150 if (f->f_buf != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2151 PyMem_Free(f->f_buf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2152 f->f_buf = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2153 } |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2154 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2155 |
|
30364
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
2156 /* Make sure that file has a readahead buffer with at least one byte |
|
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
2157 (unless at EOF) and no more than bufsize. Returns negative value on |
|
37354
016d3d23ff52
Bug #1177964: make file iterator raise MemoryError on too big files
Georg Brandl <georg@python.org>
parents:
37304
diff
changeset
|
2158 error, will set MemoryError if bufsize bytes cannot be allocated. */ |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2159 static int |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2160 readahead(PyFileObject *f, int bufsize) |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2161 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2162 Py_ssize_t chunksize; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2163 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2164 if (f->f_buf != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2165 if( (f->f_bufend - f->f_bufptr) >= 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2166 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2167 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2168 drop_readahead(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2169 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2170 if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2171 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2172 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2173 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2174 FILE_BEGIN_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2175 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2176 chunksize = Py_UniversalNewlineFread( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2177 f->f_buf, bufsize, f->f_fp, (PyObject *)f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2178 FILE_END_ALLOW_THREADS(f) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2179 if (chunksize == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2180 if (ferror(f->f_fp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2181 PyErr_SetFromErrno(PyExc_IOError); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2182 clearerr(f->f_fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2183 drop_readahead(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2184 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2185 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2186 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2187 f->f_bufptr = f->f_buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2188 f->f_bufend = f->f_buf + chunksize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2189 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
|
2190 } |
|
79f0cd3a71a1
Oops, forgot to merge this from the iter-branch to the trunk.
Guido van Rossum <guido@python.org>
parents:
17518
diff
changeset
|
2191 |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2192 /* Used by file_iternext. The returned string will start with 'skip' |
|
30364
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
2193 uninitialized bytes followed by the remainder of the line. Don't be |
|
80a4b8f6ae0e
SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents:
30320
diff
changeset
|
2194 horrified by the recursive call: maximum recursion depth is limited by |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2195 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
|
2196 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47413
diff
changeset
|
2197 static PyStringObject * |
|
24615
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2198 readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) |
|
8a1c086ad213
Make readahead functions static
Neal Norwitz <nnorwitz@gmail.com>
parents:
24594
diff
changeset
|
2199 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2200 PyStringObject* s; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2201 char *bufptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2202 char *buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2203 Py_ssize_t len; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2204 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2205 if (f->f_buf == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2206 if (readahead(f, bufsize) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2207 return 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
|
2208 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2209 len = f->f_bufend - f->f_bufptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2210 if (len == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2211 return (PyStringObject *) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2212 PyString_FromStringAndSize(NULL, skip); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2213 bufptr = (char *)memchr(f->f_bufptr, '\n', len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2214 if (bufptr != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2215 bufptr++; /* Count the '\n' */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2216 len = bufptr - f->f_bufptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2217 s = (PyStringObject *) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2218 PyString_FromStringAndSize(NULL, skip+len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2219 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2220 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2221 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2222 f->f_bufptr = bufptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2223 if (bufptr == f->f_bufend) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2224 drop_readahead(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2225 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2226 bufptr = f->f_bufptr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2227 buf = f->f_buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2228 f->f_buf = NULL; /* Force new readahead buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2229 assert(skip+len < INT_MAX); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2230 s = readahead_get_line_skip( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2231 f, (int)(skip+len), bufsize + (bufsize>>2) ); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2232 if (s == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2233 PyMem_Free(buf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2234 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2235 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2236 memcpy(PyString_AS_STRING(s)+skip, bufptr, len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2237 PyMem_Free(buf); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2238 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2239 return s; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2240 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2241 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2242 /* 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
|
2243 #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
|
2244 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2245 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
|
2246 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
|
2247 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2248 PyStringObject* l; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2249 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2250 if (f->f_fp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2251 return err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2252 if (!f->readable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2253 return err_mode("reading"); |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2254 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2255 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2256 if (l == NULL || PyString_GET_SIZE(l) == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2257 Py_XDECREF(l); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2258 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2259 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2260 return (PyObject *)l; |
|
24594
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2261 } |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2262 |
|
f928f7eea2d6
SF patch 580331 by Oren Tirosh: make file objects their own iterator.
Guido van Rossum <guido@python.org>
parents:
24233
diff
changeset
|
2263 |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2264 static PyObject * |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2265 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
|
2266 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2267 PyObject *self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2268 static PyObject *not_yet_string; |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2269 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2270 assert(type != NULL && type->tp_alloc != NULL); |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2271 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2272 if (not_yet_string == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2273 not_yet_string = PyString_InternFromString("<uninitialized file>"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2274 if (not_yet_string == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2275 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2276 } |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2277 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2278 self = type->tp_alloc(type, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2279 if (self != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2280 /* Always fill in the name and mode, so that nobody else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2281 needs to special-case NULLs there. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2282 Py_INCREF(not_yet_string); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2283 ((PyFileObject *)self)->f_name = not_yet_string; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2284 Py_INCREF(not_yet_string); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2285 ((PyFileObject *)self)->f_mode = not_yet_string; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2286 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2287 ((PyFileObject *)self)->f_encoding = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2288 Py_INCREF(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2289 ((PyFileObject *)self)->f_errors = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2290 ((PyFileObject *)self)->weakreflist = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2291 ((PyFileObject *)self)->unlocked_count = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2292 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2293 return self; |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2294 } |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2295 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2296 static int |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2297 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
|
2298 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2299 PyFileObject *foself = (PyFileObject *)self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2300 int ret = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2301 static char *kwlist[] = {"name", "mode", "buffering", 0}; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2302 char *name = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2303 char *mode = "r"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2304 int bufsize = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2305 int wideargument = 0; |
|
54975
70eda02c7a98
Issue #6368: Fixed unused variable warning on Unix.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
54895
diff
changeset
|
2306 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2307 PyObject *po; |
|
54975
70eda02c7a98
Issue #6368: Fixed unused variable warning on Unix.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
54895
diff
changeset
|
2308 #endif |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2309 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2310 assert(PyFile_Check(self)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2311 if (foself->f_fp != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2312 /* Have to close the existing file first. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2313 PyObject *closeresult = file_close(foself); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2314 if (closeresult == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2315 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2316 Py_DECREF(closeresult); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2317 } |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2318 |
|
54126
d64d47ee54a1
Issue #3527: Removed Py_WIN_WIDE_FILENAMES which is not used any more.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
51311
diff
changeset
|
2319 #ifdef MS_WINDOWS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2320 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2321 kwlist, &po, &mode, &bufsize)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2322 wideargument = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2323 if (fill_file_fields(foself, NULL, po, mode, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2324 fclose) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2325 goto Error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2326 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2327 /* Drop the argument parsing error as narrow |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2328 strings are also valid. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2329 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2330 } |
|
25535
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
2331 #endif |
|
861065256487
Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24836
diff
changeset
|
2332 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2333 if (!wideargument) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2334 PyObject *o_name; |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
2335 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2336 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2337 Py_FileSystemDefaultEncoding, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2338 &name, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2339 &mode, &bufsize)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2340 return -1; |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
2341 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2342 /* We parse again to get the name as a PyObject */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2343 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2344 kwlist, &o_name, &mode, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2345 &bufsize)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2346 goto Error; |
|
31753
2eb3175fc59b
Changed file.name to be the object passed as the 'name' argument to file()
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31691
diff
changeset
|
2347 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2348 if (fill_file_fields(foself, NULL, o_name, mode, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2349 fclose) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2350 goto Error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2351 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2352 if (open_the_file(foself, name, mode) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2353 goto Error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2354 foself->f_setbuf = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2355 PyFile_SetBufSize(self, bufsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2356 goto Done; |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2357 |
|
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2358 Error: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2359 ret = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2360 /* fall through */ |
|
19905
a7ba00e3b0de
The end of [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19903
diff
changeset
|
2361 Done: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2362 PyMem_Free(name); /* free the encoded string */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2363 return ret; |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2364 } |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2365 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2366 PyDoc_VAR(file_doc) = |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2367 PyDoc_STR( |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2368 "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
|
2369 "\n" |
|
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2370 "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
|
2371 "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
|
2372 "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
|
2373 "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
|
2374 "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
|
2375 "If the buffering argument is given, 0 means unbuffered, 1 means line\n" |
|
44222
9a976a6ee936
Note that open() is the preferred way to open files (issue 1510).
Skip Montanaro <skip@pobox.com>
parents:
43422
diff
changeset
|
2376 "buffered, and larger numbers specify the buffer size. The preferred way\n" |
|
9a976a6ee936
Note that open() is the preferred way to open files (issue 1510).
Skip Montanaro <skip@pobox.com>
parents:
43422
diff
changeset
|
2377 "to open a file is with the builtin open() function.\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2378 ) |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23514
diff
changeset
|
2379 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
|
2380 "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
|
2381 "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
|
2382 "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
|
2383 "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
|
2384 "'\\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
|
2385 "\n" |
|
9eea5edd857f
file_doc: Add some description of the U mode character, but only when
Barry Warsaw <barry@python.org>
parents:
23295
diff
changeset
|
2386 "'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
|
2387 ); |
|
19894
51cc82605d32
SF bug [#460467] file objects should be subclassable.
Tim Peters <tim.peters@gmail.com>
parents:
19836
diff
changeset
|
2388 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4585
diff
changeset
|
2389 PyTypeObject PyFile_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2390 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2391 "file", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2392 sizeof(PyFileObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2393 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2394 (destructor)file_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2395 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2396 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2397 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2398 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2399 (reprfunc)file_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2400 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2401 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2402 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2403 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2404 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2405 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2406 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2407 /* softspace is writable: we must supply tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2408 PyObject_GenericSetAttr, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2409 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2410 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2411 file_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2412 0, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2413 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2414 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2415 offsetof(PyFileObject, weakreflist), /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2416 (getiterfunc)file_self, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2417 (iternextfunc)file_iternext, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2418 file_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2419 file_memberlist, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2420 file_getsetlist, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2421 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2422 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2423 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2424 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2425 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2426 file_init, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2427 PyType_GenericAlloc, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2428 file_new, /* tp_new */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2429 PyObject_Del, /* tp_free */ |
| 6 | 2430 }; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2431 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2432 /* 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
|
2433 |
|
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2434 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2435 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
|
2436 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2437 long oldflag = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2438 if (f == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2439 /* Do nothing */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2440 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2441 else if (PyFile_Check(f)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2442 oldflag = ((PyFileObject *)f)->f_softspace; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2443 ((PyFileObject *)f)->f_softspace = newflag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2444 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2445 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2446 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2447 v = PyObject_GetAttrString(f, "softspace"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2448 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2449 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2450 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2451 if (PyInt_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2452 oldflag = PyInt_AsLong(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2453 assert(oldflag < INT_MAX); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2454 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2455 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2456 v = PyInt_FromLong((long)newflag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2457 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2458 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2459 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2460 if (PyObject_SetAttrString(f, "softspace", v) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2461 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2462 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2463 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2464 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2465 return (int)oldflag; |
|
177
bd49ca6c4add
Added 'softspace' interface to replace 'needspace' printing hack.
Guido van Rossum <guido@python.org>
parents:
154
diff
changeset
|
2466 } |
|
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
|
2467 |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2468 /* 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
|
2469 |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2470 int |
|
13454
fcc75a3748f5
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2471 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
|
2472 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2473 PyObject *writer, *value, *args, *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2474 if (f == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2475 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2476 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2477 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2478 else if (PyFile_Check(f)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2479 PyFileObject *fobj = (PyFileObject *) f; |
|
31691
5e8c6f38e15b
PyFile_WriteObject(): some of the local variables are only used when
Fred Drake <fdrake@acm.org>
parents:
31471
diff
changeset
|
2480 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2481 PyObject *enc = fobj->f_encoding; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2482 int result; |
|
31691
5e8c6f38e15b
PyFile_WriteObject(): some of the local variables are only used when
Fred Drake <fdrake@acm.org>
parents:
31471
diff
changeset
|
2483 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2484 if (fobj->f_fp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2485 err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2486 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2487 } |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
2488 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2489 if ((flags & Py_PRINT_RAW) && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2490 PyUnicode_Check(v) && enc != Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2491 char *cenc = PyString_AS_STRING(enc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2492 char *errors = fobj->f_errors == Py_None ? |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2493 "strict" : PyString_AS_STRING(fobj->f_errors); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2494 value = PyUnicode_AsEncodedString(v, cenc, errors); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2495 if (value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2496 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2497 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2498 value = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2499 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2500 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2501 result = file_PyObject_Print(value, fobj, flags); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2502 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2503 return result; |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
2504 #else |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2505 return file_PyObject_Print(v, fobj, flags); |
|
29065
bf1634b023d5
Patch #612627: Add encoding attribute to file objects, and determine
Martin v. Löwis <martin@v.loewis.de>
parents:
28967
diff
changeset
|
2506 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2507 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2508 writer = PyObject_GetAttrString(f, "write"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2509 if (writer == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2510 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2511 if (flags & Py_PRINT_RAW) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2512 if (PyUnicode_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2513 value = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2514 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2515 } else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2516 value = PyObject_Str(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2517 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2518 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2519 value = PyObject_Repr(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2520 if (value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2521 Py_DECREF(writer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2522 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2523 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2524 args = PyTuple_Pack(1, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2525 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2526 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2527 Py_DECREF(writer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2528 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2529 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2530 result = PyEval_CallObject(writer, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2531 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2532 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2533 Py_DECREF(writer); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2534 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2535 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2536 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2537 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
|
2538 } |
|
2c0432fcc270
Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2539 |
|
5605
89af5bf8f873
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5582
diff
changeset
|
2540 int |
|
21095
b1280cba79b5
PyFile_WriteString(): change prototype so that the string arg is
Tim Peters <tim.peters@gmail.com>
parents:
20942
diff
changeset
|
2541 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
|
2542 { |
|
46329
86da9298b908
Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45985
diff
changeset
|
2543 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2544 if (f == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2545 /* Should be caused by a pre-existing error */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2546 if (!PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2547 PyErr_SetString(PyExc_SystemError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2548 "null file for PyFile_WriteString"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2549 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2550 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2551 else if (PyFile_Check(f)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2552 PyFileObject *fobj = (PyFileObject *) f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2553 FILE *fp = PyFile_AsFile(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2554 if (fp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2555 err_closed(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2556 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2557 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2558 FILE_BEGIN_ALLOW_THREADS(fobj) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2559 fputs(s, fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2560 FILE_END_ALLOW_THREADS(fobj) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2561 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2562 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2563 else if (!PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2564 PyObject *v = PyString_FromString(s); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2565 int err; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2566 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2567 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2568 err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2569 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2570 return err; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2571 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2572 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2573 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
|
2574 } |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2575 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2576 /* 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
|
2577 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
|
2578 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
|
2579 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
|
2580 -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
|
2581 */ |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2582 |
|
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2583 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
|
2584 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2585 int fd; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2586 PyObject *meth; |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2587 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2588 if (PyInt_Check(o)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2589 fd = PyInt_AsLong(o); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2590 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2591 else if (PyLong_Check(o)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2592 fd = PyLong_AsLong(o); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2593 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2594 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2595 { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2596 PyObject *fno = PyEval_CallObject(meth, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2597 Py_DECREF(meth); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2598 if (fno == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2599 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
|
2600 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2601 if (PyInt_Check(fno)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2602 fd = PyInt_AsLong(fno); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2603 Py_DECREF(fno); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2604 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2605 else if (PyLong_Check(fno)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2606 fd = PyLong_AsLong(fno); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2607 Py_DECREF(fno); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2608 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2609 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2610 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2611 "fileno() returned a non-integer"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2612 Py_DECREF(fno); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2613 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2614 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2615 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2616 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2617 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2618 "argument must be an int, or have a fileno() method."); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2619 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2620 } |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2621 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2622 if (fd < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2623 PyErr_Format(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2624 "file descriptor cannot be a negative integer (%i)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2625 fd); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2626 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2627 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2628 return fd; |
|
13591
2077bf141b37
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
Andrew M. Kuchling <amk@amk.ca>
parents:
13454
diff
changeset
|
2629 } |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2630 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2631 /* 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
|
2632 #undef fgets |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2633 #undef fread |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2634 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2635 /* |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2636 ** 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
|
2637 ** 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
|
2638 ** 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
|
2639 ** 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
|
2640 ** 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
|
2641 ** 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
|
2642 ** 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
|
2643 ** \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
|
2644 ** 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
|
2645 ** the different types of newlines seen. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2646 ** 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
|
2647 ** identically. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2648 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2649 char * |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2650 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
|
2651 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2652 char *p = buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2653 int c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2654 int newlinetypes = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2655 int skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2656 int univ_newline = 1; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2657 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2658 if (fobj) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2659 if (!PyFile_Check(fobj)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2660 errno = ENXIO; /* What can you do... */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2661 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2662 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2663 univ_newline = ((PyFileObject *)fobj)->f_univ_newline; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2664 if ( !univ_newline ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2665 return fgets(buf, n, stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2666 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2667 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2668 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2669 FLOCKFILE(stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2670 c = 'x'; /* Shut up gcc warning */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2671 while (--n > 0 && (c = GETC(stream)) != EOF ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2672 if (skipnextlf ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2673 skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2674 if (c == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2675 /* Seeing a \n here with skipnextlf true |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2676 ** means we saw a \r before. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2677 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2678 newlinetypes |= NEWLINE_CRLF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2679 c = GETC(stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2680 if (c == EOF) break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2681 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2682 /* |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2683 ** Note that c == EOF also brings us here, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2684 ** so we're okay if the last char in the file |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2685 ** is a CR. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2686 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2687 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2688 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2689 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2690 if (c == '\r') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2691 /* A \r is translated into a \n, and we skip |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2692 ** an adjacent \n, if any. We don't set the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2693 ** newlinetypes flag until we've seen the next char. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2694 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2695 skipnextlf = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2696 c = '\n'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2697 } else if ( c == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2698 newlinetypes |= NEWLINE_LF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2699 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2700 *p++ = c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2701 if (c == '\n') break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2702 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2703 if ( c == EOF && skipnextlf ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2704 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2705 FUNLOCKFILE(stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2706 *p = '\0'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2707 if (fobj) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2708 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2709 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2710 } else if ( skipnextlf ) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2711 /* If we have no file object we cannot save the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2712 ** skipnextlf flag. We have to readahead, which |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2713 ** will cause a pause if we're reading from an |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2714 ** interactive stream, but that is very unlikely |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2715 ** unless we're doing something silly like |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2716 ** execfile("/dev/tty"). |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2717 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2718 c = GETC(stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2719 if ( c != '\n' ) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2720 ungetc(c, stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2721 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2722 if (p == buf) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2723 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2724 return buf; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2725 } |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2726 |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2727 /* |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2728 ** 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
|
2729 ** 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
|
2730 ** 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
|
2731 ** 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
|
2732 ** 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
|
2733 ** \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
|
2734 ** 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
|
2735 ** the different types of newlines seen. |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2736 */ |
|
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2737 size_t |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2738 Py_UniversalNewlineFread(char *buf, size_t n, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2739 FILE *stream, PyObject *fobj) |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2740 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2741 char *dst = buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2742 PyFileObject *f = (PyFileObject *)fobj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2743 int newlinetypes, skipnextlf; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2744 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2745 assert(buf != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2746 assert(stream != NULL); |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2747 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2748 if (!fobj || !PyFile_Check(fobj)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2749 errno = ENXIO; /* What can you do... */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2750 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2751 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2752 if (!f->f_univ_newline) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2753 return fread(buf, 1, n, stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2754 newlinetypes = f->f_newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2755 skipnextlf = f->f_skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2756 /* Invariant: n is the number of bytes remaining to be filled |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2757 * in the buffer. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2758 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2759 while (n) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2760 size_t nread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2761 int shortread; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2762 char *src = dst; |
|
23201
c408e8b2675a
Py_UniversalNewlineFread(): Many changes.
Tim Peters <tim.peters@gmail.com>
parents:
23029
diff
changeset
|
2763 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2764 nread = fread(dst, 1, n, stream); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2765 assert(nread <= n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2766 if (nread == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2767 break; |
|
27768
2d55be3a8ec3
SF patch #683187, fix universal newline problems on error
Neal Norwitz <nnorwitz@gmail.com>
parents:
27060
diff
changeset
|
2768 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2769 n -= nread; /* assuming 1 byte out for each in; will adjust */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2770 shortread = n != 0; /* true iff EOF or error */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2771 while (nread--) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2772 char c = *src++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2773 if (c == '\r') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2774 /* Save as LF and set flag to skip next LF. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2775 *dst++ = '\n'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2776 skipnextlf = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2777 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2778 else if (skipnextlf && c == '\n') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2779 /* Skip LF, and remember we saw CR LF. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2780 skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2781 newlinetypes |= NEWLINE_CRLF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2782 ++n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2783 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2784 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2785 /* Normal char to be stored in buffer. Also |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2786 * update the newlinetypes flag if either this |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2787 * is an LF or the previous char was a CR. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2788 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2789 if (c == '\n') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2790 newlinetypes |= NEWLINE_LF; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2791 else if (skipnextlf) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2792 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2793 *dst++ = c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2794 skipnextlf = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2795 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2796 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2797 if (shortread) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2798 /* If this is EOF, update type flags. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2799 if (skipnextlf && feof(stream)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2800 newlinetypes |= NEWLINE_CR; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2801 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2802 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2803 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2804 f->f_newlinetypes = newlinetypes; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2805 f->f_skipnextlf = skipnextlf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60203
diff
changeset
|
2806 return dst - buf; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22974
diff
changeset
|
2807 } |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
2808 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
2809 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
2810 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37589
diff
changeset
|
2811 #endif |
