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