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 | |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 328 | /* **************************** std printer **************************** */ |
| 329 | |
| 330 | typedef struct { |
| 331 | PyObject_HEAD |
| 332 | int fd; |
| 333 | } PyStdPrinter_Object; |
| 334 | |
| 335 | static PyObject * |
| 336 | stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
| 337 | { |
| 338 | PyStdPrinter_Object *self; |
| 339 | |
| 340 | assert(type != NULL && type->tp_alloc != NULL); |
| 341 | |
| 342 | self = (PyStdPrinter_Object *) type->tp_alloc(type, 0); |
| 343 | if (self != NULL) { |
| 344 | self->fd = -1; |
| 345 | } |
| 346 | |
| 347 | return (PyObject *) self; |
| 348 | } |
| 349 | |
| 350 | PyObject * |
| 351 | PyFile_NewStdPrinter(int fd) |
| 352 | { |
| 353 | PyStdPrinter_Object *self; |
| 354 | |
Christian Heimes | e018b30 | 2007-11-10 00:30:14 +0000 | [diff] [blame^] | 355 | if ((fd != fileno(stdout) && fd != fileno(stderr)) || fd < 0) { |
| 356 | /* not enough infrastructure for PyErr_BadInternalCall() */ |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | self = PyObject_New(PyStdPrinter_Object, |
| 361 | &PyStdPrinter_Type); |
Guido van Rossum | c6ecfcd | 2007-10-30 18:36:44 +0000 | [diff] [blame] | 362 | if (self != NULL) { |
| 363 | self->fd = fd; |
| 364 | } |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 365 | return (PyObject*)self; |
| 366 | } |
| 367 | |
| 368 | PyObject * |
| 369 | stdprinter_write(PyStdPrinter_Object *self, PyObject *args) |
| 370 | { |
| 371 | char *c; |
| 372 | Py_ssize_t n; |
| 373 | |
| 374 | if (self->fd < 0) { |
| 375 | PyErr_SetString(PyExc_ValueError, |
| 376 | "I/O operation on closed file"); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | if (!PyArg_ParseTuple(args, "s#", &c, &n)) { |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | Py_BEGIN_ALLOW_THREADS |
| 385 | errno = 0; |
| 386 | n = write(self->fd, c, n); |
| 387 | Py_END_ALLOW_THREADS |
| 388 | |
| 389 | if (n < 0) { |
| 390 | if (errno == EAGAIN) |
| 391 | Py_RETURN_NONE; |
| 392 | PyErr_SetFromErrno(PyExc_IOError); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | return PyInt_FromSsize_t(n); |
| 397 | } |
| 398 | |
| 399 | static PyMethodDef stdprinter_methods[] = { |
| 400 | {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""}, |
| 401 | {NULL, NULL} /* sentinel */ |
| 402 | }; |
| 403 | |
| 404 | PyTypeObject PyStdPrinter_Type = { |
| 405 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 406 | "stderrprinter", /* tp_name */ |
| 407 | sizeof(PyStdPrinter_Object), /* tp_basicsize */ |
| 408 | 0, /* tp_itemsize */ |
| 409 | /* methods */ |
| 410 | 0, /* tp_dealloc */ |
| 411 | 0, /* tp_print */ |
| 412 | 0, /* tp_getattr */ |
| 413 | 0, /* tp_setattr */ |
| 414 | 0, /* tp_compare */ |
| 415 | 0, /* tp_repr */ |
| 416 | 0, /* tp_as_number */ |
| 417 | 0, /* tp_as_sequence */ |
| 418 | 0, /* tp_as_mapping */ |
| 419 | 0, /* tp_hash */ |
| 420 | 0, /* tp_call */ |
| 421 | 0, /* tp_str */ |
| 422 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 423 | 0, /* tp_setattro */ |
| 424 | 0, /* tp_as_buffer */ |
| 425 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 426 | 0, /* tp_doc */ |
| 427 | 0, /* tp_traverse */ |
| 428 | 0, /* tp_clear */ |
| 429 | 0, /* tp_richcompare */ |
| 430 | 0, /* tp_weaklistoffset */ |
| 431 | 0, /* tp_iter */ |
| 432 | 0, /* tp_iternext */ |
| 433 | stdprinter_methods, /* tp_methods */ |
| 434 | 0, /* tp_members */ |
| 435 | 0, /* tp_getset */ |
| 436 | 0, /* tp_base */ |
| 437 | 0, /* tp_dict */ |
| 438 | 0, /* tp_descr_get */ |
| 439 | 0, /* tp_descr_set */ |
| 440 | 0, /* tp_dictoffset */ |
| 441 | 0, /* tp_init */ |
| 442 | PyType_GenericAlloc, /* tp_alloc */ |
| 443 | stdprinter_new, /* tp_new */ |
| 444 | PyObject_Del, /* tp_free */ |
| 445 | }; |
| 446 | |
| 447 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 448 | #ifdef __cplusplus |
| 449 | } |
| 450 | #endif |