2007-06-13 07:30:11 +08:00
|
|
|
/* File object implementation (what's left of it -- see io.py) */
|
1990-10-14 20:07:46 +08:00
|
|
|
|
2006-02-16 01:27:45 +08:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
1997-05-02 11:12:38 +08:00
|
|
|
#include "Python.h"
|
1990-10-14 20:07:46 +08:00
|
|
|
|
2002-04-15 04:12:41 +08: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-13 07:30:11 +08:00
|
|
|
#define FUNLOCKFILE(f)
|
2001-01-08 05:19:34 +08:00
|
|
|
#endif
|
|
|
|
|
2007-06-13 07:30:11 +08: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-08 05:19:34 +08:00
|
|
|
|
2007-06-13 07:30:11 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
1997-05-06 06:15:02 +08:00
|
|
|
|
2007-06-13 07:30:11 +08:00
|
|
|
/* External C interface */
|
1991-03-06 21:06:18 +08:00
|
|
|
|
2007-06-13 07:30:11 +08:00
|
|
|
PyObject *
|
2007-10-22 08:09:51 +08:00
|
|
|
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
|
2007-10-31 01:27:30 +08:00
|
|
|
char *newline, int closefd)
|
1990-10-14 20:07:46 +08:00
|
|
|
{
|
2007-10-22 08:09:51 +08:00
|
|
|
PyObject *io, *stream, *nameobj = NULL;
|
1991-04-04 23:21:57 +08:00
|
|
|
|
2007-06-13 07:30:11 +08:00
|
|
|
io = PyImport_ImportModule("io");
|
|
|
|
if (io == NULL)
|
1990-10-14 20:07:46 +08:00
|
|
|
return NULL;
|
2007-10-31 01:27:30 +08:00
|
|
|
stream = PyObject_CallMethod(io, "open", "isissi", fd, mode,
|
|
|
|
buffering, encoding, newline, closefd);
|
2007-10-20 07:16:50 +08:00
|
|
|
Py_DECREF(io);
|
2007-06-13 07:30:11 +08:00
|
|
|
if (stream == NULL)
|
|
|
|
return NULL;
|
2007-10-20 07:16:50 +08:00
|
|
|
if (name != NULL) {
|
|
|
|
nameobj = PyUnicode_FromString(name);
|
|
|
|
if (nameobj == NULL)
|
2007-06-13 07:30:11 +08:00
|
|
|
PyErr_Clear();
|
2007-10-20 07:16:50 +08:00
|
|
|
else {
|
|
|
|
if (PyObject_SetAttrString(stream, "name", nameobj) < 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(nameobj);
|
|
|
|
}
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
2007-06-13 07:30:11 +08:00
|
|
|
return stream;
|
1990-10-14 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *
|
2000-07-09 13:02:18 +08:00
|
|
|
PyFile_GetLine(PyObject *f, int n)
|
1991-04-04 23:21:57 +08:00
|
|
|
{
|
2001-01-08 04:51:39 +08:00
|
|
|
PyObject *result;
|
|
|
|
|
1992-09-26 05:59:05 +08:00
|
|
|
if (f == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_BadInternalCall();
|
1991-04-04 23:21:57 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-08 04:51:39 +08:00
|
|
|
|
2007-06-13 07:30:11 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *reader;
|
|
|
|
PyObject *args;
|
2001-01-08 04:51:39 +08:00
|
|
|
|
1997-05-02 11:12:38 +08:00
|
|
|
reader = PyObject_GetAttrString(f, "readline");
|
1992-09-26 05:59:05 +08:00
|
|
|
if (reader == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (n <= 0)
|
2003-10-13 03:09:37 +08:00
|
|
|
args = PyTuple_New(0);
|
1992-09-26 05:59:05 +08:00
|
|
|
else
|
1997-05-02 11:12:38 +08:00
|
|
|
args = Py_BuildValue("(i)", n);
|
1992-09-26 05:59:05 +08:00
|
|
|
if (args == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(reader);
|
1992-09-26 05:59:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
result = PyEval_CallObject(reader, args);
|
|
|
|
Py_DECREF(reader);
|
|
|
|
Py_DECREF(args);
|
2003-01-04 03:16:14 +08:00
|
|
|
if (result != NULL && !PyString_Check(result) &&
|
|
|
|
!PyUnicode_Check(result)) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(result);
|
1992-09-26 05:59:05 +08:00
|
|
|
result = NULL;
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-26 05:59:05 +08:00
|
|
|
"object.readline() returned non-string");
|
|
|
|
}
|
2001-01-08 04:51:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n < 0 && result != NULL && PyString_Check(result)) {
|
|
|
|
char *s = PyString_AS_STRING(result);
|
2006-02-16 01:27:45 +08:00
|
|
|
Py_ssize_t len = PyString_GET_SIZE(result);
|
2001-01-08 04:51:39 +08: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 11:12:38 +08:00
|
|
|
Py_DECREF(result);
|
2001-01-08 04:51:39 +08:00
|
|
|
result = v;
|
1992-09-26 05:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-04 03:16:14 +08:00
|
|
|
if (n < 0 && result != NULL && PyUnicode_Check(result)) {
|
|
|
|
Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
|
2006-02-16 01:27:45 +08:00
|
|
|
Py_ssize_t len = PyUnicode_GET_SIZE(result);
|
2003-01-04 03:16:14 +08: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-08 04:51:39 +08:00
|
|
|
return result;
|
1991-04-04 23:21:57 +08:00
|
|
|
}
|
|
|
|
|
1992-09-26 05:59:05 +08:00
|
|
|
/* Interfaces to write objects/strings to file-like objects */
|
|
|
|
|
|
|
|
int
|
2000-07-09 13:02:18 +08:00
|
|
|
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
|
1992-09-26 05:59:05 +08:00
|
|
|
{
|
1997-05-02 11:12:38 +08:00
|
|
|
PyObject *writer, *value, *args, *result;
|
1992-09-26 05:59:05 +08:00
|
|
|
if (f == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
|
1992-09-26 05:59:05 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
writer = PyObject_GetAttrString(f, "write");
|
1992-09-26 05:59:05 +08:00
|
|
|
if (writer == NULL)
|
|
|
|
return -1;
|
2001-09-19 21:47:32 +08:00
|
|
|
if (flags & Py_PRINT_RAW) {
|
2007-07-11 17:28:11 +08:00
|
|
|
value = _PyObject_Str(v);
|
2001-09-19 21:47:32 +08:00
|
|
|
}
|
2007-07-11 17:28:11 +08:00
|
|
|
else
|
2007-10-10 05:55:58 +08:00
|
|
|
value = PyObject_Repr(v);
|
1993-11-05 18:22:19 +08:00
|
|
|
if (value == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(writer);
|
1993-11-05 18:22:19 +08:00
|
|
|
return -1;
|
1992-09-26 05:59:05 +08:00
|
|
|
}
|
2003-10-13 03:09:37 +08:00
|
|
|
args = PyTuple_Pack(1, value);
|
1997-05-22 22:02:25 +08:00
|
|
|
if (args == NULL) {
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1995-07-11 07:32:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
result = PyEval_CallObject(writer, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1992-09-26 05:59:05 +08:00
|
|
|
if (result == NULL)
|
|
|
|
return -1;
|
1997-05-02 11:12:38 +08:00
|
|
|
Py_DECREF(result);
|
1992-09-26 05:59:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-23 06:25:11 +08:00
|
|
|
int
|
2001-11-29 06:13:25 +08:00
|
|
|
PyFile_WriteString(const char *s, PyObject *f)
|
1992-09-26 05:59:05 +08:00
|
|
|
{
|
|
|
|
if (f == NULL) {
|
1997-05-23 06:25:11 +08:00
|
|
|
/* Should be caused by a pre-existing error */
|
2000-07-09 13:02:18 +08:00
|
|
|
if (!PyErr_Occurred())
|
1997-05-23 06:25:11 +08:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"null file for PyFile_WriteString");
|
|
|
|
return -1;
|
1992-09-26 05:59:05 +08:00
|
|
|
}
|
1997-05-02 11:12:38 +08:00
|
|
|
else if (!PyErr_Occurred()) {
|
2007-08-10 06:58:05 +08:00
|
|
|
PyObject *v = PyUnicode_FromString(s);
|
1997-05-23 06:25:11 +08:00
|
|
|
int err;
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return err;
|
1992-09-26 05:59:05 +08:00
|
|
|
}
|
1997-07-13 11:56:50 +08:00
|
|
|
else
|
|
|
|
return -1;
|
1992-09-26 05:59:05 +08:00
|
|
|
}
|
2000-07-14 07:56:54 +08: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-13 07:30:11 +08:00
|
|
|
int
|
|
|
|
PyObject_AsFileDescriptor(PyObject *o)
|
2000-07-14 07:56:54 +08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
PyObject *meth;
|
|
|
|
|
2007-10-24 02:25:20 +08:00
|
|
|
if (PyLong_Check(o)) {
|
2000-07-14 07:56:54 +08:00
|
|
|
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-08 05:19:34 +08:00
|
|
|
|
2007-10-24 02:25:20 +08:00
|
|
|
if (PyLong_Check(fno)) {
|
2000-07-14 07:56:54 +08:00
|
|
|
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 11:31:43 +08:00
|
|
|
if (fd == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
2000-07-14 07:56:54 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"file descriptor cannot be a negative integer (%i)",
|
|
|
|
fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2002-04-15 04:12:41 +08: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 15:29:14 +08:00
|
|
|
|
2002-04-15 04:12:41 +08:00
|
|
|
if (fobj) {
|
2007-06-13 07:30:11 +08:00
|
|
|
errno = ENXIO; /* What can you do... */
|
|
|
|
return NULL;
|
2002-04-15 04:12:41 +08: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-13 07:30:11 +08:00
|
|
|
if ( skipnextlf ) {
|
2002-04-15 04:12:41 +08: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 08:43:29 +08:00
|
|
|
** exec(open("/dev/tty").read()).
|
2002-04-15 04:12:41 +08:00
|
|
|
*/
|
|
|
|
c = GETC(stream);
|
|
|
|
if ( c != '\n' )
|
|
|
|
ungetc(c, stream);
|
|
|
|
}
|
|
|
|
if (p == buf)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2006-04-21 18:40:58 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|