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