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