Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 1 | /* File object implementation (what's left of it -- see io.py) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 6 | #ifdef HAVE_GETC_UNLOCKED |
| 7 | #define GETC(f) getc_unlocked(f) |
| 8 | #define FLOCKFILE(f) flockfile(f) |
| 9 | #define FUNLOCKFILE(f) funlockfile(f) |
| 10 | #else |
| 11 | #define GETC(f) getc(f) |
| 12 | #define FLOCKFILE(f) |
| 13 | #define FUNLOCKFILE(f) |
| 14 | #endif |
| 15 | |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 16 | /* Newline flags */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 17 | #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
| 18 | #define NEWLINE_CR 1 /* \r newline seen */ |
| 19 | #define NEWLINE_LF 2 /* \n newline seen */ |
| 20 | #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 21 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 26 | /* External C interface */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 27 | |
| 28 | PyObject * |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 29 | PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 30 | char *errors, char *newline, int closefd) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 31 | { |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 32 | PyObject *io, *stream, *nameobj = NULL; |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 33 | |
| 34 | io = PyImport_ImportModule("io"); |
| 35 | if (io == NULL) |
| 36 | return NULL; |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 37 | stream = PyObject_CallMethod(io, "open", "isisssi", fd, mode, |
| 38 | buffering, encoding, errors, |
| 39 | newline, closefd); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 40 | Py_DECREF(io); |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 41 | if (stream == NULL) |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 42 | return NULL; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 43 | if (name != NULL) { |
| 44 | nameobj = PyUnicode_FromString(name); |
| 45 | if (nameobj == NULL) |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 46 | PyErr_Clear(); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 47 | else { |
| 48 | if (PyObject_SetAttrString(stream, "name", nameobj) < 0) |
| 49 | PyErr_Clear(); |
| 50 | Py_DECREF(nameobj); |
| 51 | } |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | 5397039 | 2007-06-12 00:28:30 +0000 | [diff] [blame] | 53 | return stream; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 57 | PyFile_GetLine(PyObject *f, int n) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 58 | { |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 59 | PyObject *result; |
| 60 | |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 61 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyErr_BadInternalCall(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 66 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | PyObject *reader; |
| 68 | PyObject *args; |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 70 | reader = PyObject_GetAttrString(f, "readline"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 71 | if (reader == NULL) |
| 72 | return NULL; |
| 73 | if (n <= 0) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 74 | args = PyTuple_New(0); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 75 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | args = Py_BuildValue("(i)", n); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 77 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | Py_DECREF(reader); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | result = PyEval_CallObject(reader, args); |
| 82 | Py_DECREF(reader); |
| 83 | Py_DECREF(args); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 84 | if (result != NULL && !PyBytes_Check(result) && |
Martin v. Löwis | af6a27a | 2003-01-03 19:16:14 +0000 | [diff] [blame] | 85 | !PyUnicode_Check(result)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 87 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 89 | "object.readline() returned non-string"); |
| 90 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 93 | if (n < 0 && result != NULL && PyBytes_Check(result)) { |
| 94 | char *s = PyBytes_AS_STRING(result); |
| 95 | Py_ssize_t len = PyBytes_GET_SIZE(result); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 96 | if (len == 0) { |
| 97 | Py_DECREF(result); |
| 98 | result = NULL; |
| 99 | PyErr_SetString(PyExc_EOFError, |
| 100 | "EOF when reading a line"); |
| 101 | } |
| 102 | else if (s[len-1] == '\n') { |
| 103 | if (result->ob_refcnt == 1) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 104 | _PyBytes_Resize(&result, len-1); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 105 | else { |
| 106 | PyObject *v; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 107 | v = PyBytes_FromStringAndSize(s, len-1); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | Py_DECREF(result); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 109 | result = v; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 112 | } |
Martin v. Löwis | af6a27a | 2003-01-03 19:16:14 +0000 | [diff] [blame] | 113 | if (n < 0 && result != NULL && PyUnicode_Check(result)) { |
| 114 | Py_UNICODE *s = PyUnicode_AS_UNICODE(result); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 115 | Py_ssize_t len = PyUnicode_GET_SIZE(result); |
Martin v. Löwis | af6a27a | 2003-01-03 19:16:14 +0000 | [diff] [blame] | 116 | if (len == 0) { |
| 117 | Py_DECREF(result); |
| 118 | result = NULL; |
| 119 | PyErr_SetString(PyExc_EOFError, |
| 120 | "EOF when reading a line"); |
| 121 | } |
| 122 | else if (s[len-1] == '\n') { |
| 123 | if (result->ob_refcnt == 1) |
| 124 | PyUnicode_Resize(&result, len-1); |
| 125 | else { |
| 126 | PyObject *v; |
| 127 | v = PyUnicode_FromUnicode(s, len-1); |
| 128 | Py_DECREF(result); |
| 129 | result = v; |
| 130 | } |
| 131 | } |
| 132 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 133 | return result; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 136 | /* Interfaces to write objects/strings to file-like objects */ |
| 137 | |
| 138 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 139 | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 140 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 141 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 142 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 144 | return -1; |
| 145 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 147 | if (writer == NULL) |
| 148 | return -1; |
Martin v. Löwis | 2777c02 | 2001-09-19 13:47:32 +0000 | [diff] [blame] | 149 | if (flags & Py_PRINT_RAW) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 150 | value = PyObject_Str(v); |
Martin v. Löwis | 2777c02 | 2001-09-19 13:47:32 +0000 | [diff] [blame] | 151 | } |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 152 | else |
Guido van Rossum | e518bf3 | 2007-10-09 21:55:58 +0000 | [diff] [blame] | 153 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 154 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 156 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 157 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 158 | args = PyTuple_Pack(1, value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 159 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 160 | Py_DECREF(value); |
| 161 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 164 | result = PyEval_CallObject(writer, args); |
| 165 | Py_DECREF(args); |
| 166 | Py_DECREF(value); |
| 167 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 168 | if (result == NULL) |
| 169 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 174 | int |
Tim Peters | c1bbcb8 | 2001-11-28 22:13:25 +0000 | [diff] [blame] | 175 | PyFile_WriteString(const char *s, PyObject *f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 176 | { |
| 177 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 178 | /* Should be caused by a pre-existing error */ |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 179 | if (!PyErr_Occurred()) |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 180 | PyErr_SetString(PyExc_SystemError, |
| 181 | "null file for PyFile_WriteString"); |
| 182 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 183 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | else if (!PyErr_Occurred()) { |
Guido van Rossum | 1101980 | 2007-08-09 22:58:05 +0000 | [diff] [blame] | 185 | PyObject *v = PyUnicode_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 186 | int err; |
| 187 | if (v == NULL) |
| 188 | return -1; |
| 189 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 190 | Py_DECREF(v); |
| 191 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 192 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 193 | else |
| 194 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 195 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 196 | |
| 197 | /* Try to get a file-descriptor from a Python object. If the object |
| 198 | is an integer or long integer, its value is returned. If not, the |
| 199 | object's fileno() method is called if it exists; the method must return |
| 200 | an integer or long integer, which is returned as the file descriptor value. |
| 201 | -1 is returned on failure. |
| 202 | */ |
| 203 | |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 204 | int |
| 205 | PyObject_AsFileDescriptor(PyObject *o) |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 206 | { |
| 207 | int fd; |
| 208 | PyObject *meth; |
| 209 | |
Georg Brandl | b6538a8 | 2007-10-23 18:25:20 +0000 | [diff] [blame] | 210 | if (PyLong_Check(o)) { |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 211 | fd = PyLong_AsLong(o); |
| 212 | } |
| 213 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 214 | { |
| 215 | PyObject *fno = PyEval_CallObject(meth, NULL); |
| 216 | Py_DECREF(meth); |
| 217 | if (fno == NULL) |
| 218 | return -1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 219 | |
Georg Brandl | b6538a8 | 2007-10-23 18:25:20 +0000 | [diff] [blame] | 220 | if (PyLong_Check(fno)) { |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 221 | fd = PyLong_AsLong(fno); |
| 222 | Py_DECREF(fno); |
| 223 | } |
| 224 | else { |
| 225 | PyErr_SetString(PyExc_TypeError, |
| 226 | "fileno() returned a non-integer"); |
| 227 | Py_DECREF(fno); |
| 228 | return -1; |
| 229 | } |
| 230 | } |
| 231 | else { |
| 232 | PyErr_SetString(PyExc_TypeError, |
| 233 | "argument must be an int, or have a fileno() method."); |
| 234 | return -1; |
| 235 | } |
| 236 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 237 | if (fd == -1 && PyErr_Occurred()) |
| 238 | return -1; |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 239 | if (fd < 0) { |
| 240 | PyErr_Format(PyExc_ValueError, |
| 241 | "file descriptor cannot be a negative integer (%i)", |
| 242 | fd); |
| 243 | return -1; |
| 244 | } |
| 245 | return fd; |
| 246 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 247 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 248 | /* |
| 249 | ** Py_UniversalNewlineFgets is an fgets variation that understands |
| 250 | ** all of \r, \n and \r\n conventions. |
| 251 | ** The stream should be opened in binary mode. |
| 252 | ** If fobj is NULL the routine always does newline conversion, and |
| 253 | ** it may peek one char ahead to gobble the second char in \r\n. |
| 254 | ** If fobj is non-NULL it must be a PyFileObject. In this case there |
| 255 | ** is no readahead but in stead a flag is used to skip a following |
| 256 | ** \n on the next read. Also, if the file is open in binary mode |
| 257 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 258 | ** the different types of newlines seen. |
| 259 | ** Note that we need no error handling: fgets() treats error and eof |
| 260 | ** identically. |
| 261 | */ |
| 262 | char * |
| 263 | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
| 264 | { |
| 265 | char *p = buf; |
| 266 | int c; |
| 267 | int newlinetypes = 0; |
| 268 | int skipnextlf = 0; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 269 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 270 | if (fobj) { |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 271 | errno = ENXIO; /* What can you do... */ |
| 272 | return NULL; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 273 | } |
| 274 | FLOCKFILE(stream); |
| 275 | c = 'x'; /* Shut up gcc warning */ |
| 276 | while (--n > 0 && (c = GETC(stream)) != EOF ) { |
| 277 | if (skipnextlf ) { |
| 278 | skipnextlf = 0; |
| 279 | if (c == '\n') { |
| 280 | /* Seeing a \n here with skipnextlf true |
| 281 | ** means we saw a \r before. |
| 282 | */ |
| 283 | newlinetypes |= NEWLINE_CRLF; |
| 284 | c = GETC(stream); |
| 285 | if (c == EOF) break; |
| 286 | } else { |
| 287 | /* |
| 288 | ** Note that c == EOF also brings us here, |
| 289 | ** so we're okay if the last char in the file |
| 290 | ** is a CR. |
| 291 | */ |
| 292 | newlinetypes |= NEWLINE_CR; |
| 293 | } |
| 294 | } |
| 295 | if (c == '\r') { |
| 296 | /* A \r is translated into a \n, and we skip |
| 297 | ** an adjacent \n, if any. We don't set the |
| 298 | ** newlinetypes flag until we've seen the next char. |
| 299 | */ |
| 300 | skipnextlf = 1; |
| 301 | c = '\n'; |
| 302 | } else if ( c == '\n') { |
| 303 | newlinetypes |= NEWLINE_LF; |
| 304 | } |
| 305 | *p++ = c; |
| 306 | if (c == '\n') break; |
| 307 | } |
| 308 | if ( c == EOF && skipnextlf ) |
| 309 | newlinetypes |= NEWLINE_CR; |
| 310 | FUNLOCKFILE(stream); |
| 311 | *p = '\0'; |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 312 | if ( skipnextlf ) { |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 313 | /* If we have no file object we cannot save the |
| 314 | ** skipnextlf flag. We have to readahead, which |
| 315 | ** will cause a pause if we're reading from an |
| 316 | ** interactive stream, but that is very unlikely |
| 317 | ** unless we're doing something silly like |
Neal Norwitz | 0168802 | 2007-08-12 00:43:29 +0000 | [diff] [blame] | 318 | ** exec(open("/dev/tty").read()). |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 319 | */ |
| 320 | c = GETC(stream); |
| 321 | if ( c != '\n' ) |
| 322 | ungetc(c, stream); |
| 323 | } |
| 324 | if (p == buf) |
| 325 | return NULL; |
| 326 | return buf; |
| 327 | } |
| 328 | |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 329 | /* **************************** std printer **************************** |
| 330 | * The stdprinter is used during the boot strapping phase as a preliminary |
| 331 | * file like object for sys.stderr. |
| 332 | */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 333 | |
| 334 | typedef struct { |
| 335 | PyObject_HEAD |
| 336 | int fd; |
| 337 | } PyStdPrinter_Object; |
| 338 | |
| 339 | static PyObject * |
| 340 | stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
| 341 | { |
| 342 | PyStdPrinter_Object *self; |
| 343 | |
| 344 | assert(type != NULL && type->tp_alloc != NULL); |
| 345 | |
| 346 | self = (PyStdPrinter_Object *) type->tp_alloc(type, 0); |
| 347 | if (self != NULL) { |
| 348 | self->fd = -1; |
| 349 | } |
| 350 | |
| 351 | return (PyObject *) self; |
| 352 | } |
| 353 | |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 354 | static int |
| 355 | fileio_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 356 | { |
| 357 | PyErr_SetString(PyExc_TypeError, |
| 358 | "cannot create 'stderrprinter' instances"); |
| 359 | return -1; |
| 360 | } |
| 361 | |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 362 | PyObject * |
| 363 | PyFile_NewStdPrinter(int fd) |
| 364 | { |
| 365 | PyStdPrinter_Object *self; |
| 366 | |
Christian Heimes | 58cb1b8 | 2007-11-13 02:19:40 +0000 | [diff] [blame] | 367 | if (fd != fileno(stdout) && fd != fileno(stderr)) { |
Christian Heimes | e018b30 | 2007-11-10 00:30:14 +0000 | [diff] [blame] | 368 | /* not enough infrastructure for PyErr_BadInternalCall() */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | self = PyObject_New(PyStdPrinter_Object, |
| 373 | &PyStdPrinter_Type); |
Guido van Rossum | c6ecfcd | 2007-10-30 18:36:44 +0000 | [diff] [blame] | 374 | if (self != NULL) { |
| 375 | self->fd = fd; |
| 376 | } |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 377 | return (PyObject*)self; |
| 378 | } |
| 379 | |
Martin v. Löwis | ff649b4 | 2008-06-13 07:24:48 +0000 | [diff] [blame] | 380 | static PyObject * |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 381 | stdprinter_write(PyStdPrinter_Object *self, PyObject *args) |
| 382 | { |
| 383 | char *c; |
| 384 | Py_ssize_t n; |
| 385 | |
| 386 | if (self->fd < 0) { |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 387 | /* fd might be invalid on Windows |
| 388 | * I can't raise an exception here. It may lead to an |
| 389 | * unlimited recursion in the case stderr is invalid. |
| 390 | */ |
Christian Heimes | 1607278 | 2007-11-12 17:02:51 +0000 | [diff] [blame] | 391 | Py_RETURN_NONE; |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 394 | if (!PyArg_ParseTuple(args, "s", &c)) { |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 395 | return NULL; |
| 396 | } |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 397 | n = strlen(c); |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 398 | |
| 399 | Py_BEGIN_ALLOW_THREADS |
| 400 | errno = 0; |
| 401 | n = write(self->fd, c, n); |
| 402 | Py_END_ALLOW_THREADS |
| 403 | |
| 404 | if (n < 0) { |
| 405 | if (errno == EAGAIN) |
| 406 | Py_RETURN_NONE; |
| 407 | PyErr_SetFromErrno(PyExc_IOError); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 411 | return PyLong_FromSsize_t(n); |
| 412 | } |
| 413 | |
| 414 | static PyObject * |
| 415 | stdprinter_fileno(PyStdPrinter_Object *self) |
| 416 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 417 | return PyLong_FromLong((long) self->fd); |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static PyObject * |
| 421 | stdprinter_repr(PyStdPrinter_Object *self) |
| 422 | { |
| 423 | return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>", |
| 424 | self->fd, self); |
| 425 | } |
| 426 | |
| 427 | static PyObject * |
| 428 | stdprinter_noop(PyStdPrinter_Object *self) |
| 429 | { |
| 430 | Py_RETURN_NONE; |
| 431 | } |
| 432 | |
| 433 | static PyObject * |
| 434 | stdprinter_isatty(PyStdPrinter_Object *self) |
| 435 | { |
| 436 | long res; |
| 437 | if (self->fd < 0) { |
Christian Heimes | 1607278 | 2007-11-12 17:02:51 +0000 | [diff] [blame] | 438 | Py_RETURN_FALSE; |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | Py_BEGIN_ALLOW_THREADS |
| 442 | res = isatty(self->fd); |
| 443 | Py_END_ALLOW_THREADS |
| 444 | |
| 445 | return PyBool_FromLong(res); |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static PyMethodDef stdprinter_methods[] = { |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 449 | {"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, |
| 450 | {"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, |
| 451 | {"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""}, |
| 452 | {"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""}, |
| 453 | {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""}, |
| 454 | {NULL, NULL} /*sentinel */ |
| 455 | }; |
| 456 | |
| 457 | static PyObject * |
| 458 | get_closed(PyStdPrinter_Object *self, void *closure) |
| 459 | { |
| 460 | Py_INCREF(Py_False); |
| 461 | return Py_False; |
| 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | get_mode(PyStdPrinter_Object *self, void *closure) |
| 466 | { |
| 467 | return PyUnicode_FromString("w"); |
| 468 | } |
| 469 | |
| 470 | static PyObject * |
| 471 | get_encoding(PyStdPrinter_Object *self, void *closure) |
| 472 | { |
| 473 | Py_RETURN_NONE; |
| 474 | } |
| 475 | |
| 476 | static PyGetSetDef stdprinter_getsetlist[] = { |
| 477 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 478 | {"encoding", (getter)get_encoding, NULL, "Encoding of the file"}, |
| 479 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
| 480 | {0}, |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | PyTypeObject PyStdPrinter_Type = { |
| 484 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 485 | "stderrprinter", /* tp_name */ |
| 486 | sizeof(PyStdPrinter_Object), /* tp_basicsize */ |
| 487 | 0, /* tp_itemsize */ |
| 488 | /* methods */ |
| 489 | 0, /* tp_dealloc */ |
| 490 | 0, /* tp_print */ |
| 491 | 0, /* tp_getattr */ |
| 492 | 0, /* tp_setattr */ |
| 493 | 0, /* tp_compare */ |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 494 | (reprfunc)stdprinter_repr, /* tp_repr */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 495 | 0, /* tp_as_number */ |
| 496 | 0, /* tp_as_sequence */ |
| 497 | 0, /* tp_as_mapping */ |
| 498 | 0, /* tp_hash */ |
| 499 | 0, /* tp_call */ |
| 500 | 0, /* tp_str */ |
| 501 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 502 | 0, /* tp_setattro */ |
| 503 | 0, /* tp_as_buffer */ |
| 504 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 505 | 0, /* tp_doc */ |
| 506 | 0, /* tp_traverse */ |
| 507 | 0, /* tp_clear */ |
| 508 | 0, /* tp_richcompare */ |
| 509 | 0, /* tp_weaklistoffset */ |
| 510 | 0, /* tp_iter */ |
| 511 | 0, /* tp_iternext */ |
| 512 | stdprinter_methods, /* tp_methods */ |
| 513 | 0, /* tp_members */ |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 514 | stdprinter_getsetlist, /* tp_getset */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 515 | 0, /* tp_base */ |
| 516 | 0, /* tp_dict */ |
| 517 | 0, /* tp_descr_get */ |
| 518 | 0, /* tp_descr_set */ |
| 519 | 0, /* tp_dictoffset */ |
Christian Heimes | af935e3 | 2007-11-12 16:05:45 +0000 | [diff] [blame] | 520 | fileio_init, /* tp_init */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 521 | PyType_GenericAlloc, /* tp_alloc */ |
| 522 | stdprinter_new, /* tp_new */ |
| 523 | PyObject_Del, /* tp_free */ |
| 524 | }; |
| 525 | |
| 526 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 527 | #ifdef __cplusplus |
| 528 | } |
| 529 | #endif |