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