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