| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | #ifdef HAVE_GETC_UNLOCKED
 | 
					
						
							|  |  |  | #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 */ | 
					
						
							|  |  |  | #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 * | 
					
						
							|  |  |  | PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 	PyObject *io, *stream, *nameobj; | 
					
						
							| 
									
										
										
										
											1991-04-04 15:21:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 	io = PyImport_ImportModule("io"); | 
					
						
							|  |  |  | 	if (io == NULL) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 	stream = PyObject_CallMethod(io, "open", "is", fileno(fp), mode); | 
					
						
							|  |  |  |         Py_DECREF(io); | 
					
						
							|  |  |  | 	if (stream == NULL) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  |         nameobj = PyUnicode_FromString(name); | 
					
						
							|  |  |  |         if (nameobj == NULL) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		if (PyObject_SetAttrString(stream, "name", nameobj) < 0) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		Py_DECREF(nameobj); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 	PyObject *result; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	if (f == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		PyErr_BadInternalCall(); | 
					
						
							| 
									
										
										
										
											1991-04-04 15:21:57 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		PyObject *reader; | 
					
						
							|  |  |  | 		PyObject *args; | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		reader = PyObject_GetAttrString(f, "readline"); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 		if (reader == NULL) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		if (n <= 0) | 
					
						
							| 
									
										
										
										
											2003-10-12 19:09:37 +00:00
										 |  |  | 			args = PyTuple_New(0); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 			args = Py_BuildValue("(i)", n); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 		if (args == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 			Py_DECREF(reader); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		result = PyEval_CallObject(reader, args); | 
					
						
							|  |  |  | 		Py_DECREF(reader); | 
					
						
							|  |  |  | 		Py_DECREF(args); | 
					
						
							| 
									
										
										
										
											2003-01-03 19:16:14 +00:00
										 |  |  | 		if (result != NULL && !PyString_Check(result) && | 
					
						
							|  |  |  | 		    !PyUnicode_Check(result)) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 			Py_DECREF(result); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 			result = NULL; | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 			PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 				   "object.readline() returned non-string"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (n < 0 && result != NULL && PyString_Check(result)) { | 
					
						
							|  |  |  | 		char *s = PyString_AS_STRING(result); | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 		Py_ssize_t len = PyString_GET_SIZE(result); | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 		if (len == 0) { | 
					
						
							|  |  |  | 			Py_DECREF(result); | 
					
						
							|  |  |  | 			result = NULL; | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_EOFError, | 
					
						
							|  |  |  | 					"EOF when reading a line"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (s[len-1] == '\n') { | 
					
						
							|  |  |  | 			if (result->ob_refcnt == 1) | 
					
						
							|  |  |  | 				_PyString_Resize(&result, len-1); | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				PyObject *v; | 
					
						
							|  |  |  | 				v = PyString_FromStringAndSize(s, len-1); | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 				Py_DECREF(result); | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +00:00
										 |  |  | 				result = v; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-01-03 19:16:14 +00:00
										 |  |  | 	if (n < 0 && result != NULL && PyUnicode_Check(result)) { | 
					
						
							|  |  |  | 		Py_UNICODE *s = PyUnicode_AS_UNICODE(result); | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 		Py_ssize_t len = PyUnicode_GET_SIZE(result); | 
					
						
							| 
									
										
										
										
											2003-01-03 19:16:14 +00:00
										 |  |  | 		if (len == 0) { | 
					
						
							|  |  |  | 			Py_DECREF(result); | 
					
						
							|  |  |  | 			result = NULL; | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_EOFError, | 
					
						
							|  |  |  | 					"EOF when reading a line"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (s[len-1] == '\n') { | 
					
						
							|  |  |  | 			if (result->ob_refcnt == 1) | 
					
						
							|  |  |  | 				PyUnicode_Resize(&result, len-1); | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				PyObject *v; | 
					
						
							|  |  |  | 				v = PyUnicode_FromUnicode(s, len-1); | 
					
						
							|  |  |  | 				Py_DECREF(result); | 
					
						
							|  |  |  | 				result = v; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-01-07 20:51:39 +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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	PyObject *writer, *value, *args, *result; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	if (f == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	writer = PyObject_GetAttrString(f, "write"); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	if (writer == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2001-09-19 13:47:32 +00:00
										 |  |  | 	if (flags & Py_PRINT_RAW) { | 
					
						
							| 
									
										
										
										
											2007-07-11 09:28:11 +00:00
										 |  |  | 		value = _PyObject_Str(v); | 
					
						
							| 
									
										
										
										
											2001-09-19 13:47:32 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-07-11 09:28:11 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2007-05-18 17:15:44 +00:00
										 |  |  | 		value = PyObject_ReprStr8(v); | 
					
						
							| 
									
										
										
										
											1993-11-05 10:22:19 +00:00
										 |  |  | 	if (value == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		Py_DECREF(writer); | 
					
						
							| 
									
										
										
										
											1993-11-05 10:22:19 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-10-12 19:09:37 +00:00
										 |  |  | 	args = PyTuple_Pack(1, value); | 
					
						
							| 
									
										
										
										
											1997-05-22 14:02:25 +00:00
										 |  |  | 	if (args == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		Py_DECREF(value); | 
					
						
							|  |  |  | 		Py_DECREF(writer); | 
					
						
							| 
									
										
										
										
											1995-07-10 23:32:26 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	result = PyEval_CallObject(writer, args); | 
					
						
							|  |  |  | 	Py_DECREF(args); | 
					
						
							|  |  |  | 	Py_DECREF(value); | 
					
						
							|  |  |  | 	Py_DECREF(writer); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	if (result == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	Py_DECREF(result); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  | 	if (f == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-22 22:25:11 +00:00
										 |  |  | 		/* Should be caused by a pre-existing error */ | 
					
						
							| 
									
										
										
										
											2000-07-09 05:02:18 +00:00
										 |  |  | 		if (!PyErr_Occurred()) | 
					
						
							| 
									
										
										
										
											1997-05-22 22:25:11 +00:00
										 |  |  | 			PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  | 					"null file for PyFile_WriteString"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	else if (!PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2007-08-09 22:58:05 +00:00
										 |  |  | 		PyObject *v = PyUnicode_FromString(s); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:25:11 +00:00
										 |  |  | 		int err; | 
					
						
							|  |  |  | 		if (v == NULL) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		err = PyFile_WriteObject(v, f, Py_PRINT_RAW); | 
					
						
							|  |  |  | 		Py_DECREF(v); | 
					
						
							|  |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-07-13 03:56:50 +00:00
										 |  |  | 	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
 | 
					
						
							|  |  |  |    is an integer or long integer, its value is returned.  If not, the | 
					
						
							|  |  |  |    object's fileno() method is called if it exists; the method must return | 
					
						
							|  |  |  |    an integer or long integer, which is returned as the file descriptor value. | 
					
						
							|  |  |  |    -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
										 |  |  | { | 
					
						
							|  |  |  | 	int fd; | 
					
						
							|  |  |  | 	PyObject *meth; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (PyInt_Check(o)) { | 
					
						
							|  |  |  | 		fd = PyInt_AsLong(o); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (PyLong_Check(o)) { | 
					
						
							|  |  |  | 		fd = PyLong_AsLong(o); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyObject *fno = PyEval_CallObject(meth, NULL); | 
					
						
							|  |  |  | 		Py_DECREF(meth); | 
					
						
							|  |  |  | 		if (fno == NULL) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2001-01-07 21:19:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  | 		if (PyInt_Check(fno)) { | 
					
						
							|  |  |  | 			fd = PyInt_AsLong(fno); | 
					
						
							|  |  |  | 			Py_DECREF(fno); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (PyLong_Check(fno)) { | 
					
						
							|  |  |  | 			fd = PyLong_AsLong(fno); | 
					
						
							|  |  |  | 			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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 03:31:43 +00:00
										 |  |  | 	if (fd == -1 && PyErr_Occurred()) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2000-07-13 23:56:54 +00:00
										 |  |  | 	if (fd < 0) { | 
					
						
							|  |  |  | 		PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  | 			     "file descriptor cannot be a negative integer (%i)", | 
					
						
							|  |  |  | 			     fd); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fd; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00: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. | 
					
						
							|  |  |  | ** If fobj is NULL the routine always does newline conversion, and | 
					
						
							|  |  |  | ** it may peek one char ahead to gobble the second char in \r\n. | 
					
						
							|  |  |  | ** If fobj is non-NULL it must be a PyFileObject. In this case there | 
					
						
							|  |  |  | ** is no readahead but in stead a flag is used to skip a following | 
					
						
							|  |  |  | ** \n on the next read. Also, if the file is open in binary mode | 
					
						
							|  |  |  | ** the whole conversion is skipped. Finally, the routine keeps track of | 
					
						
							|  |  |  | ** the different types of newlines seen. | 
					
						
							|  |  |  | ** Note that we need no error handling: fgets() treats error and eof | 
					
						
							|  |  |  | ** identically. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *p = buf; | 
					
						
							|  |  |  | 	int c; | 
					
						
							|  |  |  | 	int newlinetypes = 0; | 
					
						
							|  |  |  | 	int skipnextlf = 0; | 
					
						
							| 
									
										
										
										
											2002-04-21 07:29:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | 	if (fobj) { | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 		errno = ENXIO;	/* What can you do... */ | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	FLOCKFILE(stream); | 
					
						
							|  |  |  | 	c = 'x'; /* Shut up gcc warning */ | 
					
						
							|  |  |  | 	while (--n > 0 && (c = GETC(stream)) != EOF ) { | 
					
						
							|  |  |  | 		if (skipnextlf ) { | 
					
						
							|  |  |  | 			skipnextlf = 0; | 
					
						
							|  |  |  | 			if (c == '\n') { | 
					
						
							|  |  |  | 				/* Seeing a \n here with skipnextlf true
 | 
					
						
							|  |  |  | 				** means we saw a \r before. | 
					
						
							|  |  |  | 				*/ | 
					
						
							|  |  |  | 				newlinetypes |= NEWLINE_CRLF; | 
					
						
							|  |  |  | 				c = GETC(stream); | 
					
						
							|  |  |  | 				if (c == EOF) break; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				/*
 | 
					
						
							|  |  |  | 				** Note that c == EOF also brings us here, | 
					
						
							|  |  |  | 				** so we're okay if the last char in the file | 
					
						
							|  |  |  | 				** is a CR. | 
					
						
							|  |  |  | 				*/ | 
					
						
							|  |  |  | 				newlinetypes |= NEWLINE_CR; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (c == '\r') { | 
					
						
							|  |  |  | 			/* A \r is translated into a \n, and we skip
 | 
					
						
							|  |  |  | 			** an adjacent \n, if any. We don't set the | 
					
						
							|  |  |  | 			** newlinetypes flag until we've seen the next char. | 
					
						
							|  |  |  | 			*/ | 
					
						
							|  |  |  | 			skipnextlf = 1; | 
					
						
							|  |  |  | 			c = '\n'; | 
					
						
							|  |  |  | 		} else if ( c == '\n') { | 
					
						
							|  |  |  | 			newlinetypes |= NEWLINE_LF; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		*p++ = c; | 
					
						
							|  |  |  | 		if (c == '\n') break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ( c == EOF && skipnextlf ) | 
					
						
							|  |  |  | 		newlinetypes |= NEWLINE_CR; | 
					
						
							|  |  |  | 	FUNLOCKFILE(stream); | 
					
						
							|  |  |  | 	*p = '\0'; | 
					
						
							| 
									
										
										
										
											2007-06-12 23:30:11 +00:00
										 |  |  | 	if ( skipnextlf ) { | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | 		/* If we have no file object we cannot save the
 | 
					
						
							|  |  |  | 		** skipnextlf flag. We have to readahead, which | 
					
						
							|  |  |  | 		** will cause a pause if we're reading from an | 
					
						
							|  |  |  | 		** interactive stream, but that is very unlikely | 
					
						
							|  |  |  | 		** unless we're doing something silly like | 
					
						
							| 
									
										
										
										
											2007-08-12 00:43:29 +00:00
										 |  |  | 		** exec(open("/dev/tty").read()). | 
					
						
							| 
									
										
										
										
											2002-04-14 20:12:41 +00:00
										 |  |  | 		*/ | 
					
						
							|  |  |  | 		c = GETC(stream); | 
					
						
							|  |  |  | 		if ( c != '\n' ) | 
					
						
							|  |  |  | 			ungetc(c, stream); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (p == buf) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	return buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |