blob: 0f71944d526ec8c8a164e2ca346fa05276845727 [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
Jack Jansen7b8c7542002-04-14 20:12:41 +00006#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 Rossumda5b8f22007-06-12 23:30:11 +000016/* Newline flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017#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 Mickf29f47b2000-08-11 19:02:59 +000021
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000022#ifdef __cplusplus
23extern "C" {
24#endif
25
Guido van Rossumda5b8f22007-06-12 23:30:11 +000026/* External C interface */
Tim Peters59c9a642001-09-13 05:38:56 +000027
28PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +030029PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding,
30 const char *errors, const char *newline, int closefd)
Tim Peters59c9a642001-09-13 05:38:56 +000031{
Victor Stinner3603cc52010-08-13 13:34:52 +000032 PyObject *io, *stream;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020033 _Py_IDENTIFIER(open);
Guido van Rossum53970392007-06-12 00:28:30 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 io = PyImport_ImportModule("io");
36 if (io == NULL)
37 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020038 stream = _PyObject_CallMethodId(io, &PyId_open, "isisssi", fd, mode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 buffering, encoding, errors,
40 newline, closefd);
41 Py_DECREF(io);
42 if (stream == NULL)
43 return NULL;
Victor Stinner3603cc52010-08-13 13:34:52 +000044 /* ignore name attribute because the name attribute of _BufferedIOMixin
45 and TextIOWrapper is read only */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 return stream;
Tim Peters59c9a642001-09-13 05:38:56 +000047}
48
49PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000050PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +000051{
INADA Naoki72dccde2017-02-16 09:26:01 +090052 _Py_IDENTIFIER(readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 PyObject *result;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 if (f == NULL) {
56 PyErr_BadInternalCall();
57 return NULL;
58 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000059
INADA Naoki72dccde2017-02-16 09:26:01 +090060 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 Pitrouf95a1b32010-05-09 15:52:27 +000072 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 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öwisd63a3b82011-09-28 07:41:54 +020095 Py_ssize_t len = PyUnicode_GET_LENGTH(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 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öwisd63a3b82011-09-28 07:41:54 +0200102 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 Pitrouf95a1b32010-05-09 15:52:27 +0000107 }
108 }
109 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000110}
111
Guido van Rossum3165fe61992-09-25 21:59:05 +0000112/* Interfaces to write objects/strings to file-like objects */
113
114int
Fred Drakefd99de62000-07-09 05:02:18 +0000115PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000116{
Victor Stinner9def0902016-08-20 00:44:04 +0200117 PyObject *writer, *value, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200118 _Py_IDENTIFIER(write);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (f == NULL) {
121 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
122 return -1;
123 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200124 writer = _PyObject_GetAttrId(f, &PyId_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 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 Stinner7bfb42d2016-12-05 17:04:32 +0100136 result = PyObject_CallFunctionObjArgs(writer, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_DECREF(value);
138 Py_DECREF(writer);
139 if (result == NULL)
140 return -1;
141 Py_DECREF(result);
142 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +0000143}
144
Guido van Rossum27a60b11997-05-22 22:25:11 +0000145int
Tim Petersc1bbcb82001-11-28 22:13:25 +0000146PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 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 Rossum3165fe61992-09-25 21:59:05 +0000166}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000167
168/* Try to get a file-descriptor from a Python object. If the object
Serhiy Storchaka95949422013-08-27 19:40:23 +0300169 is an integer, its value is returned. If not, the
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000170 object's fileno() method is called if it exists; the method must return
Serhiy Storchaka95949422013-08-27 19:40:23 +0300171 an integer, which is returned as the file descriptor value.
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000172 -1 is returned on failure.
173*/
174
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000175int
176PyObject_AsFileDescriptor(PyObject *o)
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 int fd;
179 PyObject *meth;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200180 _Py_IDENTIFIER(fileno);
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 if (PyLong_Check(o)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200183 fd = _PyLong_AsInt(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200185 else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 {
INADA Naoki72dccde2017-02-16 09:26:01 +0900187 PyObject *fno = _PyObject_CallNoArg(meth);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 Py_DECREF(meth);
189 if (fno == NULL)
190 return -1;
Tim Peters86821b22001-01-07 21:19:34 +0000191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (PyLong_Check(fno)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200193 fd = _PyLong_AsInt(fno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 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. Kuchling06051ed2000-07-13 23:56:54 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 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. Kuchling06051ed2000-07-13 23:56:54 +0000218}
Jack Jansen7b8c7542002-04-14 20:12:41 +0000219
Jack Jansen7b8c7542002-04-14 20:12:41 +0000220/*
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*/
234char *
235Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 char *p = buf;
238 int c;
239 int newlinetypes = 0;
240 int skipnextlf = 0;
Tim Peters058b1412002-04-21 07:29:14 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 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 Cannonb94767f2011-02-22 20:15:44 +0000280 /* if ( c == EOF && skipnextlf )
281 newlinetypes |= NEWLINE_CR; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 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 Jansen7b8c7542002-04-14 20:12:41 +0000299}
300
Christian Heimesaf935e32007-11-12 16:05:45 +0000301/* **************************** 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 Rossum826d8972007-10-30 18:34:07 +0000305
306typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 PyObject_HEAD
308 int fd;
Guido van Rossum826d8972007-10-30 18:34:07 +0000309} PyStdPrinter_Object;
310
311static PyObject *
312stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossum826d8972007-10-30 18:34:07 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
319 if (self != NULL) {
320 self->fd = -1;
321 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return (PyObject *) self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000324}
325
Christian Heimesaf935e32007-11-12 16:05:45 +0000326static int
Victor Stinnerdc555402011-01-04 13:15:39 +0000327stdprinter_init(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimesaf935e32007-11-12 16:05:45 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyErr_SetString(PyExc_TypeError,
330 "cannot create 'stderrprinter' instances");
331 return -1;
Christian Heimesaf935e32007-11-12 16:05:45 +0000332}
333
Guido van Rossum826d8972007-10-30 18:34:07 +0000334PyObject *
335PyFile_NewStdPrinter(int fd)
336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (fd != fileno(stdout) && fd != fileno(stderr)) {
340 /* not enough infrastructure for PyErr_BadInternalCall() */
341 return NULL;
342 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 self = PyObject_New(PyStdPrinter_Object,
345 &PyStdPrinter_Type);
346 if (self != NULL) {
347 self->fd = fd;
348 }
349 return (PyObject*)self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000350}
351
Martin v. Löwisff649b42008-06-13 07:24:48 +0000352static PyObject *
Guido van Rossum826d8972007-10-30 18:34:07 +0000353stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
354{
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300355 PyObject *unicode;
356 PyObject *bytes = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200357 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 Py_ssize_t n;
Victor Stinner89719e12015-09-30 15:01:34 +0200359 int err;
Guido van Rossum826d8972007-10-30 18:34:07 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 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 Rossum826d8972007-10-30 18:34:07 +0000368
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300369 if (!PyArg_ParseTuple(args, "U", &unicode))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return NULL;
Guido van Rossum826d8972007-10-30 18:34:07 +0000371
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300372 /* 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 Storchaka85b0f5b2016-11-20 10:16:47 +0200379 str = PyBytes_AS_STRING(bytes);
380 n = PyBytes_GET_SIZE(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000381 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000382
Serhiy Storchaka008fc772015-09-30 15:50:32 +0300383 n = _Py_write(self->fd, str, n);
Victor Stinner89719e12015-09-30 15:01:34 +0200384 /* save errno, it can be modified indirectly by Py_XDECREF() */
385 err = errno;
Victor Stinner89719e12015-09-30 15:01:34 +0200386
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300387 Py_XDECREF(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000388
Victor Stinner66aab0c2015-03-19 22:53:20 +0100389 if (n == -1) {
Victor Stinnerae86da92015-09-30 15:03:31 +0200390 if (err == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100391 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 return NULL;
395 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return PyLong_FromSsize_t(n);
Christian Heimesaf935e32007-11-12 16:05:45 +0000398}
399
400static PyObject *
401stdprinter_fileno(PyStdPrinter_Object *self)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return PyLong_FromLong((long) self->fd);
Christian Heimesaf935e32007-11-12 16:05:45 +0000404}
405
406static PyObject *
407stdprinter_repr(PyStdPrinter_Object *self)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
410 self->fd, self);
Christian Heimesaf935e32007-11-12 16:05:45 +0000411}
412
413static PyObject *
414stdprinter_noop(PyStdPrinter_Object *self)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000417}
418
419static PyObject *
420stdprinter_isatty(PyStdPrinter_Object *self)
421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 long res;
423 if (self->fd < 0) {
424 Py_RETURN_FALSE;
425 }
Christian Heimesaf935e32007-11-12 16:05:45 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 Py_BEGIN_ALLOW_THREADS
428 res = isatty(self->fd);
429 Py_END_ALLOW_THREADS
Christian Heimesaf935e32007-11-12 16:05:45 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return PyBool_FromLong(res);
Guido van Rossum826d8972007-10-30 18:34:07 +0000432}
433
434static PyMethodDef stdprinter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 {"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 Heimesaf935e32007-11-12 16:05:45 +0000441};
442
443static PyObject *
444get_closed(PyStdPrinter_Object *self, void *closure)
445{
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200446 Py_RETURN_FALSE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000447}
448
449static PyObject *
450get_mode(PyStdPrinter_Object *self, void *closure)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return PyUnicode_FromString("w");
Christian Heimesaf935e32007-11-12 16:05:45 +0000453}
454
455static PyObject *
456get_encoding(PyStdPrinter_Object *self, void *closure)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000459}
460
461static PyGetSetDef stdprinter_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 {"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 Rossum826d8972007-10-30 18:34:07 +0000466};
467
468PyTypeObject PyStdPrinter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 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 Stinnerdc555402011-01-04 13:15:39 +0000505 stdprinter_init, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyType_GenericAlloc, /* tp_alloc */
507 stdprinter_new, /* tp_new */
508 PyObject_Del, /* tp_free */
Guido van Rossum826d8972007-10-30 18:34:07 +0000509};
510
511
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512#ifdef __cplusplus
513}
514#endif