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