blob: dd42d516898a5903de09a50dd6f5210938456536 [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"
Steve Dowerb82e17e2019-05-23 08:45:22 -07005#include "pycore_pystate.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Gregory P. Smith3015fb82018-11-12 22:01:22 -08007#if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER)
Gregory P. Smithe6c77d82018-11-12 19:47:13 -08008/* clang MemorySanitizer doesn't yet understand getc_unlocked. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00009#define GETC(f) getc_unlocked(f)
10#define FLOCKFILE(f) flockfile(f)
11#define FUNLOCKFILE(f) funlockfile(f)
12#else
13#define GETC(f) getc(f)
14#define FLOCKFILE(f)
15#define FUNLOCKFILE(f)
16#endif
17
Guido van Rossumda5b8f22007-06-12 23:30:11 +000018/* Newline flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
20#define NEWLINE_CR 1 /* \r newline seen */
21#define NEWLINE_LF 2 /* \n newline seen */
22#define NEWLINE_CRLF 4 /* \r\n newline seen */
Trent Mickf29f47b2000-08-11 19:02:59 +000023
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024#ifdef __cplusplus
25extern "C" {
26#endif
27
Guido van Rossumda5b8f22007-06-12 23:30:11 +000028/* External C interface */
Tim Peters59c9a642001-09-13 05:38:56 +000029
30PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +030031PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding,
32 const char *errors, const char *newline, int closefd)
Tim Peters59c9a642001-09-13 05:38:56 +000033{
Victor Stinner3603cc52010-08-13 13:34:52 +000034 PyObject *io, *stream;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020035 _Py_IDENTIFIER(open);
Guido van Rossum53970392007-06-12 00:28:30 +000036
Steve Dowerb82e17e2019-05-23 08:45:22 -070037 /* import _io in case we are being used to open io.py */
38 io = PyImport_ImportModule("_io");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 if (io == NULL)
40 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020041 stream = _PyObject_CallMethodId(io, &PyId_open, "isisssi", fd, mode,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 buffering, encoding, errors,
43 newline, closefd);
44 Py_DECREF(io);
45 if (stream == NULL)
46 return NULL;
Victor Stinner3603cc52010-08-13 13:34:52 +000047 /* ignore name attribute because the name attribute of _BufferedIOMixin
48 and TextIOWrapper is read only */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return stream;
Tim Peters59c9a642001-09-13 05:38:56 +000050}
51
52PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000053PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +000054{
INADA Naoki72dccde2017-02-16 09:26:01 +090055 _Py_IDENTIFIER(readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 PyObject *result;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 if (f == NULL) {
59 PyErr_BadInternalCall();
60 return NULL;
61 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000062
INADA Naoki72dccde2017-02-16 09:26:01 +090063 if (n <= 0) {
64 result = _PyObject_CallMethodIdObjArgs(f, &PyId_readline, NULL);
65 }
66 else {
67 result = _PyObject_CallMethodId(f, &PyId_readline, "i", n);
68 }
69 if (result != NULL && !PyBytes_Check(result) &&
70 !PyUnicode_Check(result)) {
71 Py_DECREF(result);
72 result = NULL;
73 PyErr_SetString(PyExc_TypeError,
74 "object.readline() returned non-string");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (n < 0 && result != NULL && PyBytes_Check(result)) {
78 char *s = PyBytes_AS_STRING(result);
79 Py_ssize_t len = PyBytes_GET_SIZE(result);
80 if (len == 0) {
81 Py_DECREF(result);
82 result = NULL;
83 PyErr_SetString(PyExc_EOFError,
84 "EOF when reading a line");
85 }
86 else if (s[len-1] == '\n') {
87 if (result->ob_refcnt == 1)
88 _PyBytes_Resize(&result, len-1);
89 else {
90 PyObject *v;
91 v = PyBytes_FromStringAndSize(s, len-1);
92 Py_DECREF(result);
93 result = v;
94 }
95 }
96 }
97 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020098 Py_ssize_t len = PyUnicode_GET_LENGTH(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (len == 0) {
100 Py_DECREF(result);
101 result = NULL;
102 PyErr_SetString(PyExc_EOFError,
103 "EOF when reading a line");
104 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200105 else if (PyUnicode_READ_CHAR(result, len-1) == '\n') {
106 PyObject *v;
107 v = PyUnicode_Substring(result, 0, len-1);
108 Py_DECREF(result);
109 result = v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 }
111 }
112 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +0000113}
114
Guido van Rossum3165fe61992-09-25 21:59:05 +0000115/* Interfaces to write objects/strings to file-like objects */
116
117int
Fred Drakefd99de62000-07-09 05:02:18 +0000118PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000119{
Victor Stinner9def0902016-08-20 00:44:04 +0200120 PyObject *writer, *value, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200121 _Py_IDENTIFIER(write);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (f == NULL) {
124 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
125 return -1;
126 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200127 writer = _PyObject_GetAttrId(f, &PyId_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (writer == NULL)
129 return -1;
130 if (flags & Py_PRINT_RAW) {
131 value = PyObject_Str(v);
132 }
133 else
134 value = PyObject_Repr(v);
135 if (value == NULL) {
136 Py_DECREF(writer);
137 return -1;
138 }
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100139 result = PyObject_CallFunctionObjArgs(writer, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 Py_DECREF(value);
141 Py_DECREF(writer);
142 if (result == NULL)
143 return -1;
144 Py_DECREF(result);
145 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +0000146}
147
Guido van Rossum27a60b11997-05-22 22:25:11 +0000148int
Tim Petersc1bbcb82001-11-28 22:13:25 +0000149PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (f == NULL) {
152 /* Should be caused by a pre-existing error */
153 if (!PyErr_Occurred())
154 PyErr_SetString(PyExc_SystemError,
155 "null file for PyFile_WriteString");
156 return -1;
157 }
158 else if (!PyErr_Occurred()) {
159 PyObject *v = PyUnicode_FromString(s);
160 int err;
161 if (v == NULL)
162 return -1;
163 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
164 Py_DECREF(v);
165 return err;
166 }
167 else
168 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +0000169}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000170
171/* Try to get a file-descriptor from a Python object. If the object
Serhiy Storchaka95949422013-08-27 19:40:23 +0300172 is an integer, its value is returned. If not, the
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000173 object's fileno() method is called if it exists; the method must return
Serhiy Storchaka95949422013-08-27 19:40:23 +0300174 an integer, which is returned as the file descriptor value.
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000175 -1 is returned on failure.
176*/
177
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000178int
179PyObject_AsFileDescriptor(PyObject *o)
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 int fd;
182 PyObject *meth;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200183 _Py_IDENTIFIER(fileno);
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (PyLong_Check(o)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200186 fd = _PyLong_AsInt(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 }
Serhiy Storchaka353053d2019-09-01 14:01:05 +0300188 else if (_PyObject_LookupAttrId(o, &PyId_fileno, &meth) < 0) {
189 return -1;
190 }
191 else if (meth != NULL) {
INADA Naoki72dccde2017-02-16 09:26:01 +0900192 PyObject *fno = _PyObject_CallNoArg(meth);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 Py_DECREF(meth);
194 if (fno == NULL)
195 return -1;
Tim Peters86821b22001-01-07 21:19:34 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (PyLong_Check(fno)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200198 fd = _PyLong_AsInt(fno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 Py_DECREF(fno);
200 }
201 else {
202 PyErr_SetString(PyExc_TypeError,
203 "fileno() returned a non-integer");
204 Py_DECREF(fno);
205 return -1;
206 }
207 }
208 else {
209 PyErr_SetString(PyExc_TypeError,
210 "argument must be an int, or have a fileno() method.");
211 return -1;
212 }
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (fd == -1 && PyErr_Occurred())
215 return -1;
216 if (fd < 0) {
217 PyErr_Format(PyExc_ValueError,
218 "file descriptor cannot be a negative integer (%i)",
219 fd);
220 return -1;
221 }
222 return fd;
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +0000223}
Jack Jansen7b8c7542002-04-14 20:12:41 +0000224
Jack Jansen7b8c7542002-04-14 20:12:41 +0000225/*
226** Py_UniversalNewlineFgets is an fgets variation that understands
227** all of \r, \n and \r\n conventions.
228** The stream should be opened in binary mode.
229** If fobj is NULL the routine always does newline conversion, and
230** it may peek one char ahead to gobble the second char in \r\n.
231** If fobj is non-NULL it must be a PyFileObject. In this case there
232** is no readahead but in stead a flag is used to skip a following
233** \n on the next read. Also, if the file is open in binary mode
234** the whole conversion is skipped. Finally, the routine keeps track of
235** the different types of newlines seen.
236** Note that we need no error handling: fgets() treats error and eof
237** identically.
238*/
239char *
240Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 char *p = buf;
243 int c;
244 int newlinetypes = 0;
245 int skipnextlf = 0;
Tim Peters058b1412002-04-21 07:29:14 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (fobj) {
248 errno = ENXIO; /* What can you do... */
249 return NULL;
250 }
251 FLOCKFILE(stream);
252 c = 'x'; /* Shut up gcc warning */
253 while (--n > 0 && (c = GETC(stream)) != EOF ) {
254 if (skipnextlf ) {
255 skipnextlf = 0;
256 if (c == '\n') {
257 /* Seeing a \n here with skipnextlf true
258 ** means we saw a \r before.
259 */
260 newlinetypes |= NEWLINE_CRLF;
261 c = GETC(stream);
262 if (c == EOF) break;
263 } else {
264 /*
265 ** Note that c == EOF also brings us here,
266 ** so we're okay if the last char in the file
267 ** is a CR.
268 */
269 newlinetypes |= NEWLINE_CR;
270 }
271 }
272 if (c == '\r') {
273 /* A \r is translated into a \n, and we skip
274 ** an adjacent \n, if any. We don't set the
275 ** newlinetypes flag until we've seen the next char.
276 */
277 skipnextlf = 1;
278 c = '\n';
279 } else if ( c == '\n') {
280 newlinetypes |= NEWLINE_LF;
281 }
282 *p++ = c;
283 if (c == '\n') break;
284 }
Brett Cannonb94767f2011-02-22 20:15:44 +0000285 /* if ( c == EOF && skipnextlf )
286 newlinetypes |= NEWLINE_CR; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 FUNLOCKFILE(stream);
288 *p = '\0';
289 if ( skipnextlf ) {
290 /* If we have no file object we cannot save the
291 ** skipnextlf flag. We have to readahead, which
292 ** will cause a pause if we're reading from an
293 ** interactive stream, but that is very unlikely
294 ** unless we're doing something silly like
295 ** exec(open("/dev/tty").read()).
296 */
297 c = GETC(stream);
298 if ( c != '\n' )
299 ungetc(c, stream);
300 }
301 if (p == buf)
302 return NULL;
303 return buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000304}
305
Christian Heimesaf935e32007-11-12 16:05:45 +0000306/* **************************** std printer ****************************
307 * The stdprinter is used during the boot strapping phase as a preliminary
308 * file like object for sys.stderr.
309 */
Guido van Rossum826d8972007-10-30 18:34:07 +0000310
311typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyObject_HEAD
313 int fd;
Guido van Rossum826d8972007-10-30 18:34:07 +0000314} PyStdPrinter_Object;
315
316static PyObject *
317stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 assert(type != NULL && type->tp_alloc != NULL);
Guido van Rossum826d8972007-10-30 18:34:07 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
324 if (self != NULL) {
325 self->fd = -1;
326 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return (PyObject *) self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000329}
330
Christian Heimesaf935e32007-11-12 16:05:45 +0000331static int
Victor Stinnerdc555402011-01-04 13:15:39 +0000332stdprinter_init(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimesaf935e32007-11-12 16:05:45 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 PyErr_SetString(PyExc_TypeError,
335 "cannot create 'stderrprinter' instances");
336 return -1;
Christian Heimesaf935e32007-11-12 16:05:45 +0000337}
338
Guido van Rossum826d8972007-10-30 18:34:07 +0000339PyObject *
340PyFile_NewStdPrinter(int fd)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyStdPrinter_Object *self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (fd != fileno(stdout) && fd != fileno(stderr)) {
345 /* not enough infrastructure for PyErr_BadInternalCall() */
346 return NULL;
347 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 self = PyObject_New(PyStdPrinter_Object,
350 &PyStdPrinter_Type);
351 if (self != NULL) {
352 self->fd = fd;
353 }
354 return (PyObject*)self;
Guido van Rossum826d8972007-10-30 18:34:07 +0000355}
356
Martin v. Löwisff649b42008-06-13 07:24:48 +0000357static PyObject *
Guido van Rossum826d8972007-10-30 18:34:07 +0000358stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
359{
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300360 PyObject *unicode;
361 PyObject *bytes = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200362 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_ssize_t n;
Victor Stinner89719e12015-09-30 15:01:34 +0200364 int err;
Guido van Rossum826d8972007-10-30 18:34:07 +0000365
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100366 /* The function can clear the current exception */
367 assert(!PyErr_Occurred());
368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (self->fd < 0) {
370 /* fd might be invalid on Windows
371 * I can't raise an exception here. It may lead to an
372 * unlimited recursion in the case stderr is invalid.
373 */
374 Py_RETURN_NONE;
375 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000376
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100377 if (!PyArg_ParseTuple(args, "U", &unicode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return NULL;
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100379 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000380
Victor Stinner6d43f6f2019-01-22 21:18:05 +0100381 /* Encode Unicode to UTF-8/surrogateescape */
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300382 str = PyUnicode_AsUTF8AndSize(unicode, &n);
383 if (str == NULL) {
384 PyErr_Clear();
385 bytes = _PyUnicode_AsUTF8String(unicode, "backslashreplace");
386 if (bytes == NULL)
387 return NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200388 str = PyBytes_AS_STRING(bytes);
389 n = PyBytes_GET_SIZE(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000390 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000391
Serhiy Storchaka008fc772015-09-30 15:50:32 +0300392 n = _Py_write(self->fd, str, n);
Victor Stinner89719e12015-09-30 15:01:34 +0200393 /* save errno, it can be modified indirectly by Py_XDECREF() */
394 err = errno;
Victor Stinner89719e12015-09-30 15:01:34 +0200395
Serhiy Storchakaa59018c2015-09-30 15:46:53 +0300396 Py_XDECREF(bytes);
Guido van Rossum826d8972007-10-30 18:34:07 +0000397
Victor Stinner66aab0c2015-03-19 22:53:20 +0100398 if (n == -1) {
Victor Stinnerae86da92015-09-30 15:03:31 +0200399 if (err == EAGAIN) {
Victor Stinner66aab0c2015-03-19 22:53:20 +0100400 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_RETURN_NONE;
Victor Stinner66aab0c2015-03-19 22:53:20 +0100402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return NULL;
404 }
Guido van Rossum826d8972007-10-30 18:34:07 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return PyLong_FromSsize_t(n);
Christian Heimesaf935e32007-11-12 16:05:45 +0000407}
408
409static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530410stdprinter_fileno(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return PyLong_FromLong((long) self->fd);
Christian Heimesaf935e32007-11-12 16:05:45 +0000413}
414
415static PyObject *
416stdprinter_repr(PyStdPrinter_Object *self)
417{
sth8b91eda2019-03-10 11:29:14 +0100418 return PyUnicode_FromFormat("<stdprinter(fd=%d) object at %p>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 self->fd, self);
Christian Heimesaf935e32007-11-12 16:05:45 +0000420}
421
422static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530423stdprinter_noop(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000426}
427
428static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530429stdprinter_isatty(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored))
Christian Heimesaf935e32007-11-12 16:05:45 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 long res;
432 if (self->fd < 0) {
433 Py_RETURN_FALSE;
434 }
Christian Heimesaf935e32007-11-12 16:05:45 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 Py_BEGIN_ALLOW_THREADS
437 res = isatty(self->fd);
438 Py_END_ALLOW_THREADS
Christian Heimesaf935e32007-11-12 16:05:45 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return PyBool_FromLong(res);
Guido van Rossum826d8972007-10-30 18:34:07 +0000441}
442
443static PyMethodDef stdprinter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
445 {"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
446 {"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""},
447 {"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""},
448 {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""},
449 {NULL, NULL} /*sentinel */
Christian Heimesaf935e32007-11-12 16:05:45 +0000450};
451
452static PyObject *
453get_closed(PyStdPrinter_Object *self, void *closure)
454{
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200455 Py_RETURN_FALSE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000456}
457
458static PyObject *
459get_mode(PyStdPrinter_Object *self, void *closure)
460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return PyUnicode_FromString("w");
Christian Heimesaf935e32007-11-12 16:05:45 +0000462}
463
464static PyObject *
465get_encoding(PyStdPrinter_Object *self, void *closure)
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_RETURN_NONE;
Christian Heimesaf935e32007-11-12 16:05:45 +0000468}
469
470static PyGetSetDef stdprinter_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
472 {"encoding", (getter)get_encoding, NULL, "Encoding of the file"},
473 {"mode", (getter)get_mode, NULL, "String giving the file mode"},
474 {0},
Guido van Rossum826d8972007-10-30 18:34:07 +0000475};
476
477PyTypeObject PyStdPrinter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyVarObject_HEAD_INIT(&PyType_Type, 0)
479 "stderrprinter", /* tp_name */
480 sizeof(PyStdPrinter_Object), /* tp_basicsize */
481 0, /* tp_itemsize */
482 /* methods */
483 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200484 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 0, /* tp_getattr */
486 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200487 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 (reprfunc)stdprinter_repr, /* tp_repr */
489 0, /* tp_as_number */
490 0, /* tp_as_sequence */
491 0, /* tp_as_mapping */
492 0, /* tp_hash */
493 0, /* tp_call */
494 0, /* tp_str */
495 PyObject_GenericGetAttr, /* tp_getattro */
496 0, /* tp_setattro */
497 0, /* tp_as_buffer */
498 Py_TPFLAGS_DEFAULT, /* tp_flags */
499 0, /* tp_doc */
500 0, /* tp_traverse */
501 0, /* tp_clear */
502 0, /* tp_richcompare */
503 0, /* tp_weaklistoffset */
504 0, /* tp_iter */
505 0, /* tp_iternext */
506 stdprinter_methods, /* tp_methods */
507 0, /* tp_members */
508 stdprinter_getsetlist, /* tp_getset */
509 0, /* tp_base */
510 0, /* tp_dict */
511 0, /* tp_descr_get */
512 0, /* tp_descr_set */
513 0, /* tp_dictoffset */
Victor Stinnerdc555402011-01-04 13:15:39 +0000514 stdprinter_init, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 PyType_GenericAlloc, /* tp_alloc */
516 stdprinter_new, /* tp_new */
517 PyObject_Del, /* tp_free */
Guido van Rossum826d8972007-10-30 18:34:07 +0000518};
519
520
Steve Dowerb82e17e2019-05-23 08:45:22 -0700521/* ************************** open_code hook ***************************
522 * The open_code hook allows embedders to override the method used to
523 * open files that are going to be used by the runtime to execute code
524 */
525
526int
527PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction hook, void *userData) {
528 if (Py_IsInitialized() &&
529 PySys_Audit("setopencodehook", NULL) < 0) {
530 return -1;
531 }
532
533 if (_PyRuntime.open_code_hook) {
534 if (Py_IsInitialized()) {
535 PyErr_SetString(PyExc_SystemError,
536 "failed to change existing open_code hook");
537 }
538 return -1;
539 }
540
541 _PyRuntime.open_code_hook = hook;
542 _PyRuntime.open_code_userdata = userData;
543 return 0;
544}
545
546PyObject *
547PyFile_OpenCodeObject(PyObject *path)
548{
549 PyObject *iomod, *f = NULL;
550 _Py_IDENTIFIER(open);
551
552 if (!PyUnicode_Check(path)) {
553 PyErr_Format(PyExc_TypeError, "'path' must be 'str', not '%.200s'",
554 Py_TYPE(path)->tp_name);
555 return NULL;
556 }
557
558 Py_OpenCodeHookFunction hook = _PyRuntime.open_code_hook;
559 if (hook) {
560 f = hook(path, _PyRuntime.open_code_userdata);
561 } else {
562 iomod = PyImport_ImportModule("_io");
563 if (iomod) {
564 f = _PyObject_CallMethodId(iomod, &PyId_open, "Os",
565 path, "rb");
566 Py_DECREF(iomod);
567 }
568 }
569
570 return f;
571}
572
573PyObject *
574PyFile_OpenCode(const char *utf8path)
575{
576 PyObject *pathobj = PyUnicode_FromString(utf8path);
577 PyObject *f;
578 if (!pathobj) {
579 return NULL;
580 }
581 f = PyFile_OpenCodeObject(pathobj);
582 Py_DECREF(pathobj);
583 return f;
584}
585
586
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587#ifdef __cplusplus
588}
589#endif