| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | /* File object implementation (what's left of it -- see io.py) */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | #define PY_SSIZE_T_CLEAN
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2021-10-12 08:38:19 +02:00
										 |  |  | #include "pycore_call.h"          // _PyObject_CallNoArgs()
 | 
					
						
							|  |  |  | #include "pycore_runtime.h"       // _PyRuntime
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:01:22 -08:00
										 |  |  | #if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER)
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:47:13 -08:00
										 |  |  | /* clang MemorySanitizer doesn't yet understand getc_unlocked. */ | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | #define GETC(f) getc_unlocked(f)
 | 
					
						
							|  |  |  | #define FLOCKFILE(f) flockfile(f)
 | 
					
						
							|  |  |  | #define FUNLOCKFILE(f) funlockfile(f)
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define GETC(f) getc(f)
 | 
					
						
							|  |  |  | #define FLOCKFILE(f)
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | #define FUNLOCKFILE(f)
 | 
					
						
							| 
									
										
										
										
											2001-01-07 21:19:34 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | /* Newline flags */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define NEWLINE_UNKNOWN 0       /* No newline seen, yet */
 | 
					
						
							|  |  |  | #define NEWLINE_CR 1            /* \r newline seen */
 | 
					
						
							|  |  |  | #define NEWLINE_LF 2            /* \n newline seen */
 | 
					
						
							|  |  |  | #define NEWLINE_CRLF 4          /* \r\n newline seen */
 | 
					
						
							| 
									
										
										
										
											2001-01-07 21:19:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-05-05 22:15:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | /* External C interface */ | 
					
						
							| 
									
										
										
										
											1991-03-06 13:06:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2013-10-19 21:03:34 +03:00
										 |  |  | PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, | 
					
						
							|  |  |  |               const char *errors, const char *newline, int closefd) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |     PyObject *open, *stream; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     /* import _io in case we are being used to open io.py */ | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |     open = _PyImport_GetModuleAttrString("_io", "open"); | 
					
						
							|  |  |  |     if (open == NULL) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |     stream = PyObject_CallFunction(open, "isisssO", fd, mode, | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |                                   buffering, encoding, errors, | 
					
						
							|  |  |  |                                   newline, closefd ? Py_True : Py_False); | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |     Py_DECREF(open); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (stream == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-08-13 13:34:52 +00:00
										 |  |  |     /* ignore name attribute because the name attribute of _BufferedIOMixin
 | 
					
						
							|  |  |  |        and TextIOWrapper is read only */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return stream; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 05:02:18 +00:00
										 |  |  | PyFile_GetLine(PyObject *f, int n) | 
					
						
							| 
									
										
										
										
											1991-04-04 15:21:57 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (f == NULL) { | 
					
						
							|  |  |  |         PyErr_BadInternalCall(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 09:26:01 +09:00
										 |  |  |     if (n <= 0) { | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |         result = PyObject_CallMethodNoArgs(f, &_Py_ID(readline)); | 
					
						
							| 
									
										
										
										
											2017-02-16 09:26:01 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |         result = _PyObject_CallMethod(f, &_Py_ID(readline), "i", n); | 
					
						
							| 
									
										
										
										
											2017-02-16 09:26:01 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (result != NULL && !PyBytes_Check(result) && | 
					
						
							|  |  |  |         !PyUnicode_Check(result)) { | 
					
						
							| 
									
										
										
										
											2022-11-23 14:57:50 +01:00
										 |  |  |         Py_SETREF(result, NULL); | 
					
						
							| 
									
										
										
										
											2017-02-16 09:26:01 +09:00
										 |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                    "object.readline() returned non-string"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (n < 0 && result != NULL && PyBytes_Check(result)) { | 
					
						
							| 
									
										
										
										
											2020-04-12 14:58:27 +03:00
										 |  |  |         const char *s = PyBytes_AS_STRING(result); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_ssize_t len = PyBytes_GET_SIZE(result); | 
					
						
							|  |  |  |         if (len == 0) { | 
					
						
							| 
									
										
										
										
											2022-11-23 14:57:50 +01:00
										 |  |  |             Py_SETREF(result, NULL); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             PyErr_SetString(PyExc_EOFError, | 
					
						
							|  |  |  |                             "EOF when reading a line"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (s[len-1] == '\n') { | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |             if (Py_REFCNT(result) == 1) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 _PyBytes_Resize(&result, len-1); | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 PyObject *v; | 
					
						
							|  |  |  |                 v = PyBytes_FromStringAndSize(s, len-1); | 
					
						
							| 
									
										
										
										
											2022-11-22 13:39:11 +01:00
										 |  |  |                 Py_SETREF(result, v); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (n < 0 && result != NULL && PyUnicode_Check(result)) { | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |         Py_ssize_t len = PyUnicode_GET_LENGTH(result); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (len == 0) { | 
					
						
							| 
									
										
										
										
											2022-11-23 14:57:50 +01:00
										 |  |  |             Py_SETREF(result, NULL); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             PyErr_SetString(PyExc_EOFError, | 
					
						
							|  |  |  |                             "EOF when reading a line"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |         else if (PyUnicode_READ_CHAR(result, len-1) == '\n') { | 
					
						
							|  |  |  |             PyObject *v; | 
					
						
							|  |  |  |             v = PyUnicode_Substring(result, 0, len-1); | 
					
						
							| 
									
										
										
										
											2022-11-22 13:39:11 +01:00
										 |  |  |             Py_SETREF(result, v); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											1991-04-04 15:21:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | /* Interfaces to write objects/strings to file-like objects */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 05:02:18 +00:00
										 |  |  | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-20 00:44:04 +02:00
										 |  |  |     PyObject *writer, *value, *result; | 
					
						
							| 
									
										
										
										
											2011-10-10 18:11:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (f == NULL) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     writer = PyObject_GetAttr(f, &_Py_ID(write)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (writer == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     if (flags & Py_PRINT_RAW) { | 
					
						
							|  |  |  |         value = PyObject_Str(v); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         value = PyObject_Repr(v); | 
					
						
							|  |  |  |     if (value == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(writer); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-11 17:46:57 +01:00
										 |  |  |     result = PyObject_CallOneArg(writer, value); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(value); | 
					
						
							|  |  |  |     Py_DECREF(writer); | 
					
						
							|  |  |  |     if (result == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-22 22:25:11 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2001-11-28 22:13:25 +00:00
										 |  |  | PyFile_WriteString(const char *s, PyObject *f) | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (f == NULL) { | 
					
						
							|  |  |  |         /* Should be caused by a pre-existing error */ | 
					
						
							|  |  |  |         if (!PyErr_Occurred()) | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                             "null file for PyFile_WriteString"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |         PyObject *v = PyUnicode_FromString(s); | 
					
						
							|  |  |  |         int err; | 
					
						
							|  |  |  |         if (v == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         err = PyFile_WriteObject(v, f, Py_PRINT_RAW); | 
					
						
							|  |  |  |         Py_DECREF(v); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Try to get a file-descriptor from a Python object.  If the object
 | 
					
						
							| 
									
										
										
										
											2013-08-27 19:40:23 +03:00
										 |  |  |    is an integer, its value is returned.  If not, the | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  |    object's fileno() method is called if it exists; the method must return | 
					
						
							| 
									
										
										
										
											2013-08-27 19:40:23 +03:00
										 |  |  |    an integer, which is returned as the file descriptor value. | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  |    -1 is returned on failure. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_AsFileDescriptor(PyObject *o) | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int fd; | 
					
						
							|  |  |  |     PyObject *meth; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyLong_Check(o)) { | 
					
						
							| 
									
										
										
										
											2013-01-15 01:12:17 +02:00
										 |  |  |         fd = _PyLong_AsInt(o); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     else if (_PyObject_LookupAttr(o, &_Py_ID(fileno), &meth) < 0) { | 
					
						
							| 
									
										
										
										
											2019-09-01 12:03:39 +03:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (meth != NULL) { | 
					
						
							| 
									
										
										
										
											2021-10-12 00:42:23 +02:00
										 |  |  |         PyObject *fno = _PyObject_CallNoArgs(meth); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(meth); | 
					
						
							|  |  |  |         if (fno == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (PyLong_Check(fno)) { | 
					
						
							| 
									
										
										
										
											2013-01-15 01:12:17 +02:00
										 |  |  |             fd = _PyLong_AsInt(fno); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             Py_DECREF(fno); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                             "fileno() returned a non-integer"); | 
					
						
							|  |  |  |             Py_DECREF(fno); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                         "argument must be an int, or have a fileno() method."); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fd == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  |                      "file descriptor cannot be a negative integer (%i)", | 
					
						
							|  |  |  |                      fd); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return fd; | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:00:45 +03:00
										 |  |  | int | 
					
						
							|  |  |  | _PyLong_FileDescriptor_Converter(PyObject *o, void *ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd = PyObject_AsFileDescriptor(o); | 
					
						
							|  |  |  |     if (fd == -1) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *(int *)ptr = fd; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | char * | 
					
						
							| 
									
										
										
										
											2022-09-27 23:23:42 +01:00
										 |  |  | _Py_UniversalNewlineFgetsWithSize(char *buf, int n, FILE *stream, PyObject *fobj, size_t* size) | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *p = buf; | 
					
						
							|  |  |  |     int c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fobj) { | 
					
						
							|  |  |  |         errno = ENXIO;          /* What can you do... */ | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     FLOCKFILE(stream); | 
					
						
							|  |  |  |     while (--n > 0 && (c = GETC(stream)) != EOF ) { | 
					
						
							|  |  |  |         if (c == '\r') { | 
					
						
							| 
									
										
										
										
											2021-10-14 23:10:52 -07:00
										 |  |  |             // A \r is translated into a \n, and we skip an adjacent \n, if any.
 | 
					
						
							|  |  |  |             c = GETC(stream); | 
					
						
							|  |  |  |             if (c != '\n') { | 
					
						
							|  |  |  |                 ungetc(c, stream); | 
					
						
							|  |  |  |                 c = '\n'; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         *p++ = c; | 
					
						
							| 
									
										
										
										
											2021-10-14 23:10:52 -07:00
										 |  |  |         if (c == '\n') { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     FUNLOCKFILE(stream); | 
					
						
							|  |  |  |     *p = '\0'; | 
					
						
							| 
									
										
										
										
											2022-09-27 23:23:42 +01:00
										 |  |  |     if (p == buf) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-09-27 23:23:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     *size = p - buf; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return buf; | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 23:23:42 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  | ** Py_UniversalNewlineFgets is an fgets variation that understands | 
					
						
							|  |  |  | ** all of \r, \n and \r\n conventions. | 
					
						
							|  |  |  | ** The stream should be opened in binary mode. | 
					
						
							|  |  |  | ** The fobj parameter exists solely for legacy reasons and must be NULL. | 
					
						
							|  |  |  | ** Note that we need no error handling: fgets() treats error and eof | 
					
						
							|  |  |  | ** identically. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) { | 
					
						
							|  |  |  |     size_t size; | 
					
						
							|  |  |  |     return _Py_UniversalNewlineFgetsWithSize(buf, n, stream, fobj, &size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | /* **************************** std printer ****************************
 | 
					
						
							|  |  |  |  * The stdprinter is used during the boot strapping phase as a preliminary | 
					
						
							|  |  |  |  * file like object for sys.stderr. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     int fd; | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | } PyStdPrinter_Object; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyFile_NewStdPrinter(int fd) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyStdPrinter_Object *self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fd != fileno(stdout) && fd != fileno(stderr)) { | 
					
						
							|  |  |  |         /* not enough infrastructure for PyErr_BadInternalCall() */ | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self = PyObject_New(PyStdPrinter_Object, | 
					
						
							|  |  |  |                         &PyStdPrinter_Type); | 
					
						
							|  |  |  |     if (self != NULL) { | 
					
						
							|  |  |  |         self->fd = fd; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-13 07:24:48 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | stdprinter_write(PyStdPrinter_Object *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-30 15:46:53 +03:00
										 |  |  |     PyObject *unicode; | 
					
						
							|  |  |  |     PyObject *bytes = NULL; | 
					
						
							| 
									
										
										
										
											2016-11-20 10:16:47 +02:00
										 |  |  |     const char *str; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_ssize_t n; | 
					
						
							| 
									
										
										
										
											2015-09-30 15:01:34 +02:00
										 |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:18:05 +01:00
										 |  |  |     /* The function can clear the current exception */ | 
					
						
							|  |  |  |     assert(!PyErr_Occurred()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (self->fd < 0) { | 
					
						
							|  |  |  |         /* fd might be invalid on Windows
 | 
					
						
							|  |  |  |          * I can't raise an exception here. It may lead to an | 
					
						
							|  |  |  |          * unlimited recursion in the case stderr is invalid. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:18:05 +01:00
										 |  |  |     if (!PyArg_ParseTuple(args, "U", &unicode)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:18:05 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  |     /* Encode Unicode to UTF-8/backslashreplace */ | 
					
						
							| 
									
										
										
										
											2015-09-30 15:46:53 +03:00
										 |  |  |     str = PyUnicode_AsUTF8AndSize(unicode, &n); | 
					
						
							|  |  |  |     if (str == NULL) { | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							|  |  |  |         bytes = _PyUnicode_AsUTF8String(unicode, "backslashreplace"); | 
					
						
							|  |  |  |         if (bytes == NULL) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2016-11-20 10:16:47 +02:00
										 |  |  |         str = PyBytes_AS_STRING(bytes); | 
					
						
							|  |  |  |         n = PyBytes_GET_SIZE(bytes); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 15:50:32 +03:00
										 |  |  |     n = _Py_write(self->fd, str, n); | 
					
						
							| 
									
										
										
										
											2015-09-30 15:01:34 +02:00
										 |  |  |     /* save errno, it can be modified indirectly by Py_XDECREF() */ | 
					
						
							|  |  |  |     err = errno; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-30 15:46:53 +03:00
										 |  |  |     Py_XDECREF(bytes); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     if (n == -1) { | 
					
						
							| 
									
										
										
										
											2015-09-30 15:03:31 +02:00
										 |  |  |         if (err == EAGAIN) { | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |             PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return PyLong_FromSsize_t(n); | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  | stdprinter_fileno(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyLong_FromLong((long) self->fd); | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | stdprinter_repr(PyStdPrinter_Object *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-10 11:29:14 +01:00
										 |  |  |     return PyUnicode_FromFormat("<stdprinter(fd=%d) object at %p>", | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                 self->fd, self); | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  | stdprinter_noop(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  | stdprinter_isatty(PyStdPrinter_Object *self, PyObject *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     long res; | 
					
						
							|  |  |  |     if (self->fd < 0) { | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     res = isatty(self->fd); | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyBool_FromLong(res); | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef stdprinter_methods[] = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {"close",           (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, | 
					
						
							|  |  |  |     {"flush",           (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, | 
					
						
							|  |  |  |     {"fileno",          (PyCFunction)stdprinter_fileno, METH_NOARGS, ""}, | 
					
						
							|  |  |  |     {"isatty",          (PyCFunction)stdprinter_isatty, METH_NOARGS, ""}, | 
					
						
							|  |  |  |     {"write",           (PyCFunction)stdprinter_write, METH_VARARGS, ""}, | 
					
						
							|  |  |  |     {NULL,              NULL}  /*sentinel */ | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | get_closed(PyStdPrinter_Object *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-23 09:47:21 +02:00
										 |  |  |     Py_RETURN_FALSE; | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | get_mode(PyStdPrinter_Object *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyUnicode_FromString("w"); | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | get_encoding(PyStdPrinter_Object *self, void *closure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2007-11-12 16:05:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyGetSetDef stdprinter_getsetlist[] = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {"closed", (getter)get_closed, NULL, "True if the file is closed"}, | 
					
						
							|  |  |  |     {"encoding", (getter)get_encoding, NULL, "Encoding of the file"}, | 
					
						
							|  |  |  |     {"mode", (getter)get_mode, NULL, "String giving the file mode"}, | 
					
						
							|  |  |  |     {0}, | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyTypeObject PyStdPrinter_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "stderrprinter",                            /* tp_name */ | 
					
						
							|  |  |  |     sizeof(PyStdPrinter_Object),                /* tp_basicsize */ | 
					
						
							|  |  |  |     0,                                          /* tp_itemsize */ | 
					
						
							|  |  |  |     /* methods */ | 
					
						
							|  |  |  |     0,                                          /* tp_dealloc */ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                                          /* tp_vectorcall_offset */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                                          /* tp_getattr */ | 
					
						
							|  |  |  |     0,                                          /* tp_setattr */ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                                          /* tp_as_async */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     (reprfunc)stdprinter_repr,                  /* tp_repr */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_number */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_sequence */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_mapping */ | 
					
						
							|  |  |  |     0,                                          /* tp_hash */ | 
					
						
							|  |  |  |     0,                                          /* tp_call */ | 
					
						
							|  |  |  |     0,                                          /* tp_str */ | 
					
						
							|  |  |  |     PyObject_GenericGetAttr,                    /* tp_getattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_setattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_buffer */ | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, /* tp_flags */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                                          /* tp_doc */ | 
					
						
							|  |  |  |     0,                                          /* tp_traverse */ | 
					
						
							|  |  |  |     0,                                          /* tp_clear */ | 
					
						
							|  |  |  |     0,                                          /* tp_richcompare */ | 
					
						
							|  |  |  |     0,                                          /* tp_weaklistoffset */ | 
					
						
							|  |  |  |     0,                                          /* tp_iter */ | 
					
						
							|  |  |  |     0,                                          /* tp_iternext */ | 
					
						
							|  |  |  |     stdprinter_methods,                         /* tp_methods */ | 
					
						
							|  |  |  |     0,                                          /* tp_members */ | 
					
						
							|  |  |  |     stdprinter_getsetlist,                      /* tp_getset */ | 
					
						
							|  |  |  |     0,                                          /* tp_base */ | 
					
						
							|  |  |  |     0,                                          /* tp_dict */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_get */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_set */ | 
					
						
							|  |  |  |     0,                                          /* tp_dictoffset */ | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  |     0,                                          /* tp_init */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyType_GenericAlloc,                        /* tp_alloc */ | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  |     0,                                          /* tp_new */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_Del,                               /* tp_free */ | 
					
						
							| 
									
										
										
										
											2007-10-30 18:34:07 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  | /* ************************** open_code hook ***************************
 | 
					
						
							|  |  |  |  * The open_code hook allows embedders to override the method used to | 
					
						
							|  |  |  |  * open files that are going to be used by the runtime to execute code | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction hook, void *userData) { | 
					
						
							|  |  |  |     if (Py_IsInitialized() && | 
					
						
							|  |  |  |         PySys_Audit("setopencodehook", NULL) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyRuntime.open_code_hook) { | 
					
						
							|  |  |  |         if (Py_IsInitialized()) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                 "failed to change existing open_code hook"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyRuntime.open_code_hook = hook; | 
					
						
							|  |  |  |     _PyRuntime.open_code_userdata = userData; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyFile_OpenCodeObject(PyObject *path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |     PyObject *f = NULL; | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(path)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, "'path' must be 'str', not '%.200s'", | 
					
						
							|  |  |  |                      Py_TYPE(path)->tp_name); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_OpenCodeHookFunction hook = _PyRuntime.open_code_hook; | 
					
						
							|  |  |  |     if (hook) { | 
					
						
							|  |  |  |         f = hook(path, _PyRuntime.open_code_userdata); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-06-14 07:15:26 +03:00
										 |  |  |         PyObject *open = _PyImport_GetModuleAttrString("_io", "open"); | 
					
						
							|  |  |  |         if (open) { | 
					
						
							|  |  |  |             f = PyObject_CallFunction(open, "Os", path, "rb"); | 
					
						
							|  |  |  |             Py_DECREF(open); | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyFile_OpenCode(const char *utf8path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *pathobj = PyUnicode_FromString(utf8path); | 
					
						
							|  |  |  |     PyObject *f; | 
					
						
							|  |  |  |     if (!pathobj) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     f = PyFile_OpenCodeObject(pathobj); | 
					
						
							|  |  |  |     Py_DECREF(pathobj); | 
					
						
							|  |  |  |     return f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |