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