blob: babaa05bdbc49e201014d265a40295cc11eb9712 [file] [log] [blame]
Guido van Rossumda5b8f22007-06-12 23:30:11 +00001/* File object implementation (what's left of it -- see io.py) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002
Martin v. Löwis18e16552006-02-15 17:27:45 +00003#define PY_SSIZE_T_CLEAN
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Gregory P. Smith3015fb82018-11-12 22:01:22 -08006#if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER)
Gregory P. Smithe6c77d82018-11-12 19:47:13 -08007/* clang MemorySanitizer doesn't yet understand getc_unlocked. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00008#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 Rossumda5b8f22007-06-12 23:30:11 +000017/* Newline flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018#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 Mickf29f47b2000-08-11 19:02:59 +000022
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000023#ifdef __cplusplus
24extern "C" {
25#endif
26
Guido van Rossumda5b8f22007-06-12 23:30:11 +000027/* External C interface */
Tim Peters59c9a642001-09-13 05:38:56 +000028
29PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +030030PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding,
31 const char *errors, const char *newline, int closefd)
Tim Peters59c9a642001-09-13 05:38:56 +000032{
Victor Stinner3603cc52010-08-13 13:34:52 +000033 PyObject *io, *stream;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020034 _Py_IDENTIFIER(open);
Guido van Rossum53970392007-06-12 00:28:30 +000035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 io = PyImport_ImportModule("io");
37 if (io == NULL)
38 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020039 stream = _PyObject_CallMethodId(io, &PyId_open, "isisssi", fd, mode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 buffering, encoding, errors,
41 newline, closefd);
42 Py_DECREF(io);
43 if (stream == NULL)
44 return NULL;
Victor Stinner3603cc52010-08-13 13:34:52 +000045 /* ignore name attribute because the name attribute of _BufferedIOMixin
46 and TextIOWrapper is read only */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 return stream;
Tim Peters59c9a642001-09-13 05:38:56 +000048}
49
50PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000051PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +000052{
INADA Naoki72dccde2017-02-16 09:26:01 +090053 _Py_IDENTIFIER(readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 PyObject *result;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 if (f == NULL) {
57 PyErr_BadInternalCall();
58 return NULL;
59 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000060
INADA Naoki72dccde2017-02-16 09:26:01 +090061 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 Pitrouf95a1b32010-05-09 15:52:27 +000073 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 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öwisd63a3b82011-09-28 07:41:54 +020096 Py_ssize_t len = PyUnicode_GET_LENGTH(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 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öwisd63a3b82011-09-28 07:41:54 +0200103 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 Pitrouf95a1b32010-05-09 15:52:27 +0000108 }
109 }
110 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000111}
112
Guido van Rossum3165fe61992-09-25 21:59:05 +0000113/* Interfaces to write objects/strings to file-like objects */
114
115int
Fred Drakefd99de62000-07-09 05:02:18 +0000116PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000117{
Victor Stinner9def0902016-08-20 00:44:04 +0200118 PyObject *writer, *value, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200119 _Py_IDENTIFIER(write);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 if (f == NULL) {
122 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
123 return -1;
124 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200125 writer = _PyObject_GetAttrId(f, &PyId_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 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 Stinner7bfb42d2016-12-05 17:04:32 +0100137 result = PyObject_CallFunctionObjArgs(writer, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 Py_DECREF(value);
139 Py_DECREF(writer);
140 if (result == NULL)
141 return -1;
142 Py_DECREF(result);
143 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +0000144}
145
Guido van Rossum27a60b11997-05-22 22:25:11 +0000146int
Tim Petersc1bbcb82001-11-28 22:13:25 +0000147PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 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 Rossum3165fe61992-09-25 21:59:05 +0000167}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000168
169/* Try to get a file-descriptor from a Python object. If the object
Serhiy Storchaka95949422013-08-27 19:40:23 +0300170 is an integer, its value is returned. If not, the
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000171 object's fileno() method is called if it exists; the method must return
Serhiy Storchaka95949422013-08-27 19:40:23 +0300172 an integer, which is returned as the file descriptor value.
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000173 -1 is returned on failure.
174*/
175
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000176int
177PyObject_AsFileDescriptor(PyObject *o)
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 int fd;
180 PyObject *meth;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200181 _Py_IDENTIFIER(fileno);
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 if (PyLong_Check(o)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200184 fd = _PyLong_AsInt(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200186 else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 {
INADA Naoki72dccde2017-02-16 09:26:01 +0900188 PyObject *fno = _PyObject_CallNoArg(meth);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 Py_DECREF(meth);
190 if (fno == NULL)
191 return -1;
Tim Peters86821b22001-01-07 21:19:34 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (PyLong_Check(fno)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200194 fd = _PyLong_AsInt(fno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 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. Kuchling06051ed2000-07-13 23:56:54 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 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. Kuchling06051ed2000-07-13 23:56:54 +0000219}
Jack Jansen7b8c7542002-04-14 20:12:41 +0000220
Jack Jansen7b8c7542002-04-14 20:12:41 +0000221/*
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*/
235char *
236Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 char *p = buf;
239 int c;
240 int newlinetypes = 0;
241 int skipnextlf = 0;
Tim Peters058b1412002-04-21 07:29:14 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 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 Cannonb94767f2011-02-22 20:15:44 +0000281 /* if ( c == EOF && skipnextlf )
282 newlinetypes |= NEWLINE_CR; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 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 Jansen7b8c7542002-04-14 20:12:41 +0000300}
301
Christian Heimesaf935e32007-11-12 16:05:45 +0000302/* **************************** 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 Rossum826d8972007-10-30 18:34:07 +0000306
307typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 PyObject_HEAD
309 int fd;
Guido van Rossum826d8972007-10-30 18:34:07 +0000310} PyStdPrinter_Object;
311
312static PyObject *
313stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossum826d8972007-10-30 18:34:07 +0000318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
320 if (self != NULL) {
321 self->fd = -1;
322 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 return (PyObject *) self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000325}
326
Christian Heimesaf935e32007-11-12 16:05:45 +0000327static int
Victor Stinnerdc555402011-01-04 13:15:39 +0000328stdprinter_init(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimesaf935e32007-11-12 16:05:45 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyErr_SetString(PyExc_TypeError,
331 "cannot create 'stderrprinter' instances");
332 return -1;
Christian Heimesaf935e32007-11-12 16:05:45 +0000333}
334
Guido van Rossum826d8972007-10-30 18:34:07 +0000335PyObject *
336PyFile_NewStdPrinter(int fd)
337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (fd != fileno(stdout) && fd != fileno(stderr)) {
341 /* not enough infrastructure for PyErr_BadInternalCall() */
342 return NULL;
343 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 self = PyObject_New(PyStdPrinter_Object,
346 &PyStdPrinter_Type);
347 if (self != NULL) {
348 self->fd = fd;
349 }
350 return (PyObject*)self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000351}
352
Martin v. Löwisff649b42008-06-13 07:24:48 +0000353static PyObject *
Guido van Rossum826d8972007-10-30 18:34:07 +0000354stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
355{
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300356 PyObject *unicode;
357 PyObject *bytes = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200358 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 Py_ssize_t n;
Victor Stinner89719e12015-09-30 15:01:34 +0200360 int err;
Guido van Rossum826d8972007-10-30 18:34:07 +0000361
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100362 /* The function can clear the current exception */
363 assert(!PyErr_Occurred());
364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 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 Rossum826d8972007-10-30 18:34:07 +0000372
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100373 if (!PyArg_ParseTuple(args, "U", &unicode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return NULL;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100375 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000376
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100377 /* Encode Unicode to UTF-8/surrogateescape */
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300378 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 Storchaka85b0f5b2016-11-20 10:16:47 +0200384 str = PyBytes_AS_STRING(bytes);
385 n = PyBytes_GET_SIZE(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000386 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000387
Serhiy Storchaka008fc772015-09-30 15:50:32 +0300388 n = _Py_write(self->fd, str, n);
Victor Stinner89719e12015-09-30 15:01:34 +0200389 /* save errno, it can be modified indirectly by Py_XDECREF() */
390 err = errno;
Victor Stinner89719e12015-09-30 15:01:34 +0200391
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300392 Py_XDECREF(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000393
Victor Stinner66aab0c2015-03-19 22:53:20 +0100394 if (n == -1) {
Victor Stinnerae86da92015-09-30 15:03:31 +0200395 if (err == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100396 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return NULL;
400 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return PyLong_FromSsize_t(n);
Christian Heimesaf935e32007-11-12 16:05:45 +0000403}
404
405static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530406stdprinter_fileno(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 return PyLong_FromLong((long) self->fd);
Christian Heimesaf935e32007-11-12 16:05:45 +0000409}
410
411static PyObject *
412stdprinter_repr(PyStdPrinter_Object *self)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
415 self->fd, self);
Christian Heimesaf935e32007-11-12 16:05:45 +0000416}
417
418static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530419stdprinter_noop(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000422}
423
424static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530425stdprinter_isatty(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 long res;
428 if (self->fd < 0) {
429 Py_RETURN_FALSE;
430 }
Christian Heimesaf935e32007-11-12 16:05:45 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_BEGIN_ALLOW_THREADS
433 res = isatty(self->fd);
434 Py_END_ALLOW_THREADS
Christian Heimesaf935e32007-11-12 16:05:45 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return PyBool_FromLong(res);
Guido van Rossum826d8972007-10-30 18:34:07 +0000437}
438
439static PyMethodDef stdprinter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 {"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 Heimesaf935e32007-11-12 16:05:45 +0000446};
447
448static PyObject *
449get_closed(PyStdPrinter_Object *self, void *closure)
450{
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200451 Py_RETURN_FALSE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000452}
453
454static PyObject *
455get_mode(PyStdPrinter_Object *self, void *closure)
456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return PyUnicode_FromString("w");
Christian Heimesaf935e32007-11-12 16:05:45 +0000458}
459
460static PyObject *
461get_encoding(PyStdPrinter_Object *self, void *closure)
462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000464}
465
466static PyGetSetDef stdprinter_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 {"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 Rossum826d8972007-10-30 18:34:07 +0000471};
472
473PyTypeObject PyStdPrinter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 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 Stinnerdc555402011-01-04 13:15:39 +0000510 stdprinter_init, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyType_GenericAlloc, /* tp_alloc */
512 stdprinter_new, /* tp_new */
513 PyObject_Del, /* tp_free */
Guido van Rossum826d8972007-10-30 18:34:07 +0000514};
515
516
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517#ifdef __cplusplus
518}
519#endif