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