1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:07:38 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 12:39:46 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 12:39:46 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* File object implementation */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#include "Python.h"
|
1994-08-01 11:34:53 +00:00
|
|
|
#include "structmember.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1996-12-05 21:54:17 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1997-05-06 15:23:24 +00:00
|
|
|
#ifdef MS_WIN32
|
|
|
|
#define ftruncate _chsize
|
|
|
|
#define fileno _fileno
|
|
|
|
#define HAVE_FTRUNCATE
|
|
|
|
#endif
|
|
|
|
|
1995-02-19 15:55:19 +00:00
|
|
|
#ifdef THINK_C
|
|
|
|
#define HAVE_FOPENRF
|
|
|
|
#endif
|
1995-04-23 22:12:47 +00:00
|
|
|
#ifdef __MWERKS__
|
|
|
|
/* Mwerks fopen() doesn't always set errno */
|
|
|
|
#define NO_FOPEN_ERRNO
|
|
|
|
#endif
|
1995-02-19 15:55:19 +00:00
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
|
1991-03-06 13:06:18 +00:00
|
|
|
|
1993-07-05 10:31:29 +00:00
|
|
|
#include <errno.h>
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
typedef struct {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject_HEAD
|
1990-10-14 12:07:46 +00:00
|
|
|
FILE *f_fp;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *f_name;
|
|
|
|
PyObject *f_mode;
|
|
|
|
int (*f_close) Py_PROTO((FILE *));
|
1991-04-04 10:44:06 +00:00
|
|
|
int f_softspace; /* Flag used by 'print' command */
|
1997-05-02 03:12:38 +00:00
|
|
|
} PyFileObject;
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
FILE *
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFile_AsFile(f)
|
|
|
|
PyObject *f;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (f == NULL || !PyFile_Check(f))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1992-09-25 21:59:05 +00:00
|
|
|
else
|
1997-05-02 03:12:38 +00:00
|
|
|
return ((PyFileObject *)f)->f_fp;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFile_Name(f)
|
|
|
|
PyObject *f;
|
1993-10-18 17:06:59 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
if (f == NULL || !PyFile_Check(f))
|
1993-10-18 17:06:59 +00:00
|
|
|
return NULL;
|
|
|
|
else
|
1997-05-02 03:12:38 +00:00
|
|
|
return ((PyFileObject *)f)->f_name;
|
1993-10-18 17:06:59 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFile_FromFile(fp, name, mode, close)
|
1990-10-14 12:07:46 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *name;
|
|
|
|
char *mode;
|
1997-05-02 03:12:38 +00:00
|
|
|
int (*close) Py_FPROTO((FILE *));
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
f->f_fp = NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
f->f_name = PyString_FromString(name);
|
|
|
|
f->f_mode = PyString_FromString(mode);
|
1991-06-04 19:37:39 +00:00
|
|
|
f->f_close = close;
|
1991-04-04 10:44:06 +00:00
|
|
|
f->f_softspace = 0;
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f->f_name == NULL || f->f_mode == NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(f);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
f->f_fp = fp;
|
1997-05-02 03:12:38 +00:00
|
|
|
return (PyObject *) f;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFile_FromString(name, mode)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name, *mode;
|
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
extern int fclose Py_PROTO((FILE *));
|
|
|
|
PyFileObject *f;
|
|
|
|
f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FOPENRF
|
1991-02-13 23:25:27 +00:00
|
|
|
if (*mode == '*') {
|
|
|
|
FILE *fopenRF();
|
|
|
|
f->f_fp = fopenRF(name, mode+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
1992-08-05 19:58:53 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
f->f_fp = fopen(name, mode);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
}
|
1991-02-13 23:25:27 +00:00
|
|
|
if (f->f_fp == NULL) {
|
1995-04-23 22:12:47 +00:00
|
|
|
#ifdef NO_FOPEN_ERRNO
|
|
|
|
if ( errno == 0 ) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetString(PyExc_IOError, "Cannot open file");
|
|
|
|
Py_DECREF(f);
|
1995-04-23 22:12:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
Py_DECREF(f);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return (PyObject *)f;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
void
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFile_SetBufSize(f, bufsize)
|
|
|
|
PyObject *f;
|
1994-08-01 11:34:53 +00:00
|
|
|
int bufsize;
|
|
|
|
{
|
|
|
|
if (bufsize >= 0) {
|
|
|
|
#ifdef HAVE_SETVBUF
|
|
|
|
int type;
|
|
|
|
switch (bufsize) {
|
|
|
|
case 0:
|
|
|
|
type = _IONBF;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
type = _IOLBF;
|
|
|
|
bufsize = BUFSIZ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = _IOFBF;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
|
|
|
|
type, bufsize);
|
1994-08-01 11:34:53 +00:00
|
|
|
#endif /* HAVE_SETVBUF */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1992-07-06 14:19:26 +00:00
|
|
|
err_closed()
|
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
|
1992-07-06 14:19:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
1990-12-20 15:06:42 +00:00
|
|
|
file_dealloc(f)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
if (f->f_fp != NULL && f->f_close != NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-06-04 19:37:39 +00:00
|
|
|
(*f->f_close)(f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f->f_name != NULL)
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(f->f_name);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (f->f_mode != NULL)
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(f->f_mode);
|
1990-10-14 12:07:46 +00:00
|
|
|
free((char *)f);
|
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_repr(f)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
|
|
|
char buf[300];
|
1993-01-10 18:33:56 +00:00
|
|
|
sprintf(buf, "<%s file '%.256s', mode '%.10s' at %lx>",
|
1990-10-14 12:07:46 +00:00
|
|
|
f->f_fp == NULL ? "closed" : "open",
|
1997-05-02 03:12:38 +00:00
|
|
|
PyString_AsString(f->f_name),
|
|
|
|
PyString_AsString(f->f_mode),
|
1993-01-10 18:33:56 +00:00
|
|
|
(long)f);
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyString_FromString(buf);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_close(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-06-04 19:37:39 +00:00
|
|
|
int sts = 0;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
if (f->f_fp != NULL) {
|
1992-08-05 19:58:53 +00:00
|
|
|
if (f->f_close != NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
errno = 0;
|
1991-06-04 19:37:39 +00:00
|
|
|
sts = (*f->f_close)(f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
f->f_fp = NULL;
|
|
|
|
}
|
1992-03-04 16:39:24 +00:00
|
|
|
if (sts == EOF)
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
1991-06-04 19:37:39 +00:00
|
|
|
if (sts != 0)
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyInt_FromLong((long)sts);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_seek(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-03-06 13:06:18 +00:00
|
|
|
long offset;
|
1992-07-06 14:19:26 +00:00
|
|
|
int whence;
|
1992-08-05 19:58:53 +00:00
|
|
|
int ret;
|
1991-03-06 13:06:18 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
whence = 0;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_Parse(args, "l", &offset)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "(li)", &offset, &whence))
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
ret = fseek(f->f_fp, offset, whence);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
if (ret != 0) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 16:39:24 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1995-01-02 19:07:15 +00:00
|
|
|
file_truncate(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1995-01-02 19:07:15 +00:00
|
|
|
{
|
|
|
|
long newsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_Parse(args, "l", &newsize)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_NoArgs(args))
|
1995-01-02 19:07:15 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
errno = 0;
|
|
|
|
newsize = ftell(f->f_fp); /* default to current position*/
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
if (newsize == -1L) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1995-01-02 19:07:15 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
errno = 0;
|
|
|
|
ret = fflush(f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
if (ret == 0) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
errno = 0;
|
|
|
|
ret = ftruncate(fileno(f->f_fp), newsize);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1995-01-02 19:07:15 +00:00
|
|
|
}
|
|
|
|
if (ret != 0) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1995-01-02 19:07:15 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-01-02 19:07:15 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_FTRUNCATE */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_tell(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-03-06 13:06:18 +00:00
|
|
|
{
|
|
|
|
long offset;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
|
|
|
offset = ftell(f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
if (offset == -1L) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 16:39:24 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyInt_FromLong(offset);
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1992-06-23 09:07:03 +00:00
|
|
|
file_fileno(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1992-06-23 09:07:03 +00:00
|
|
|
{
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1992-06-23 09:07:03 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyInt_FromLong((long) fileno(f->f_fp));
|
1992-06-23 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_flush(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-03-06 13:06:18 +00:00
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
int res;
|
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
errno = 0;
|
1992-08-05 19:58:53 +00:00
|
|
|
res = fflush(f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
if (res != 0) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 16:39:24 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-06-04 19:37:39 +00:00
|
|
|
file_isatty(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-06-04 19:37:39 +00:00
|
|
|
{
|
1992-08-05 19:58:53 +00:00
|
|
|
long res;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
1991-06-04 19:37:39 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-05 19:58:53 +00:00
|
|
|
res = isatty((int)fileno(f->f_fp));
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
return PyInt_FromLong(res);
|
1991-06-04 19:37:39 +00:00
|
|
|
}
|
|
|
|
|
1997-05-09 22:27:31 +00:00
|
|
|
/* We expect that fstat exists on most systems.
|
|
|
|
It's confirmed on Unix, Mac and Windows.
|
|
|
|
If you don't have it, add #define DONT_HAVE_FSTAT to your config.h. */
|
|
|
|
#ifndef DONT_HAVE_FSTAT
|
|
|
|
#define HAVE_FSTAT
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if BUFSIZ < 8192
|
|
|
|
#define SMALLCHUNK 8192
|
|
|
|
#else
|
|
|
|
#define SMALLCHUNK BUFSIZ
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BIGCHUNK (512*1024)
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
new_buffersize(f, currentsize)
|
|
|
|
PyFileObject *f;
|
|
|
|
size_t currentsize;
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FSTAT
|
|
|
|
long pos, end;
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fileno(f->f_fp), &st) == 0) {
|
|
|
|
end = st.st_size;
|
|
|
|
pos = ftell(f->f_fp);
|
|
|
|
if (end > pos && pos >= 0)
|
|
|
|
return end - pos + 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (currentsize > SMALLCHUNK) {
|
|
|
|
/* Keep doubling until we reach BIGCHUNK;
|
|
|
|
then keep adding BIGCHUNK. */
|
|
|
|
if (currentsize <= BIGCHUNK)
|
|
|
|
return currentsize + currentsize;
|
|
|
|
else
|
|
|
|
return currentsize + BIGCHUNK;
|
|
|
|
}
|
|
|
|
return currentsize + SMALLCHUNK;
|
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_read(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-03-06 13:06:18 +00:00
|
|
|
{
|
1997-05-10 22:33:55 +00:00
|
|
|
long bytesrequested = -1;
|
1997-05-09 22:27:31 +00:00
|
|
|
size_t bytesread, buffersize, chunksize;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *v;
|
1991-03-06 13:06:18 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-10 22:33:55 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "|l", &bytesrequested))
|
|
|
|
return NULL;
|
1997-05-09 22:27:31 +00:00
|
|
|
if (bytesrequested < 0)
|
|
|
|
buffersize = new_buffersize(f, 0);
|
|
|
|
else
|
|
|
|
buffersize = bytesrequested;
|
|
|
|
v = PyString_FromStringAndSize((char *)NULL, buffersize);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1997-05-09 22:27:31 +00:00
|
|
|
bytesread = 0;
|
1991-03-06 13:06:18 +00:00
|
|
|
for (;;) {
|
1997-05-10 22:07:25 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
1997-05-09 22:27:31 +00:00
|
|
|
chunksize = fread(BUF(v) + bytesread, 1,
|
|
|
|
buffersize - bytesread, f->f_fp);
|
1997-05-10 22:07:25 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (chunksize == 0) {
|
|
|
|
if (!ferror(f->f_fp))
|
|
|
|
break;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-09 22:27:31 +00:00
|
|
|
bytesread += chunksize;
|
|
|
|
if (bytesread < buffersize)
|
1991-03-06 13:06:18 +00:00
|
|
|
break;
|
1997-05-09 22:27:31 +00:00
|
|
|
if (bytesrequested < 0) {
|
|
|
|
buffersize = new_buffersize(f, buffersize);
|
|
|
|
if (_PyString_Resize(&v, buffersize) < 0)
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-05-09 22:27:31 +00:00
|
|
|
if (bytesread != buffersize)
|
|
|
|
_PyString_Resize(&v, bytesread);
|
1990-10-14 12:07:46 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-05-05 22:15:02 +00:00
|
|
|
static PyObject *
|
|
|
|
file_readinto(f, args)
|
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
int ntodo, ndone, nnow;
|
|
|
|
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
|
|
|
|
return NULL;
|
|
|
|
ndone = 0;
|
1997-05-10 22:07:25 +00:00
|
|
|
while (ntodo > 0) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
1997-05-05 22:15:02 +00:00
|
|
|
nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
|
1997-05-10 22:07:25 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nnow == 0) {
|
|
|
|
if (!ferror(f->f_fp))
|
|
|
|
break;
|
1997-05-05 22:15:02 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ndone += nnow;
|
|
|
|
ntodo -= nnow;
|
|
|
|
}
|
|
|
|
return PyInt_FromLong(ndone);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-04-04 15:21:57 +00:00
|
|
|
/* Internal routine to get a line.
|
|
|
|
Size argument interpretation:
|
|
|
|
> 0: max length;
|
|
|
|
= 0: read arbitrary line;
|
|
|
|
< 0: strip trailing '\n', raise EOFError if EOF reached immediately
|
1991-03-06 13:06:18 +00:00
|
|
|
*/
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-04-04 15:21:57 +00:00
|
|
|
getline(f, n)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
1991-04-04 15:21:57 +00:00
|
|
|
int n;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-03-06 13:06:18 +00:00
|
|
|
register FILE *fp;
|
|
|
|
register int c;
|
|
|
|
register char *buf, *end;
|
1991-04-04 15:21:57 +00:00
|
|
|
int n1, n2;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *v;
|
1991-04-04 15:21:57 +00:00
|
|
|
|
1992-07-31 12:42:38 +00:00
|
|
|
fp = f->f_fp;
|
1991-04-04 15:21:57 +00:00
|
|
|
n2 = n > 0 ? n : 100;
|
1997-05-02 03:12:38 +00:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, n2);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-03-06 13:06:18 +00:00
|
|
|
buf = BUF(v);
|
|
|
|
end = buf + n2;
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
for (;;) {
|
1991-04-04 15:21:57 +00:00
|
|
|
if ((c = getc(fp)) == EOF) {
|
1991-06-03 10:54:55 +00:00
|
|
|
clearerr(fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
if (PyErr_CheckSignals()) {
|
|
|
|
Py_BLOCK_THREADS
|
|
|
|
Py_DECREF(v);
|
1991-04-04 15:21:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (n < 0 && buf == BUF(v)) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BLOCK_THREADS
|
|
|
|
Py_DECREF(v);
|
|
|
|
PyErr_SetString(PyExc_EOFError,
|
1991-12-24 13:26:41 +00:00
|
|
|
"EOF when reading a line");
|
1991-04-04 15:21:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-03-06 13:06:18 +00:00
|
|
|
break;
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
if ((*buf++ = c) == '\n') {
|
|
|
|
if (n < 0)
|
|
|
|
buf--;
|
|
|
|
break;
|
|
|
|
}
|
1991-03-06 13:06:18 +00:00
|
|
|
if (buf == end) {
|
1991-04-04 15:21:57 +00:00
|
|
|
if (n > 0)
|
1991-03-06 13:06:18 +00:00
|
|
|
break;
|
|
|
|
n1 = n2;
|
|
|
|
n2 += 1000;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BLOCK_THREADS
|
|
|
|
if (_PyString_Resize(&v, n2) < 0)
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_UNBLOCK_THREADS
|
1991-03-06 13:06:18 +00:00
|
|
|
buf = BUF(v) + n1;
|
|
|
|
end = BUF(v) + n2;
|
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1991-03-06 13:06:18 +00:00
|
|
|
n1 = buf - BUF(v);
|
|
|
|
if (n1 != n2)
|
1997-05-02 03:12:38 +00:00
|
|
|
_PyString_Resize(&v, n1);
|
1990-10-14 12:07:46 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1991-04-04 15:21:57 +00:00
|
|
|
/* External C interface */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
|
|
|
PyFile_GetLine(f, n)
|
|
|
|
PyObject *f;
|
1991-04-04 15:21:57 +00:00
|
|
|
int n;
|
|
|
|
{
|
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;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyFile_Check(f)) {
|
|
|
|
PyObject *reader;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *result;
|
|
|
|
reader = PyObject_GetAttrString(f, "readline");
|
1992-09-25 21:59:05 +00:00
|
|
|
if (reader == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (n <= 0)
|
1997-05-02 03:12:38 +00:00
|
|
|
args = Py_BuildValue("()");
|
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);
|
|
|
|
if (result != NULL && !PyString_Check(result)) {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
if (n < 0 && result != NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
char *s = PyString_AsString(result);
|
|
|
|
int len = PyString_Size(result);
|
1992-09-25 21:59:05 +00:00
|
|
|
if (len == 0) {
|
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_EOFError,
|
1992-09-25 21:59:05 +00:00
|
|
|
"EOF when reading a line");
|
|
|
|
}
|
|
|
|
else if (s[len-1] == '\n') {
|
|
|
|
if (result->ob_refcnt == 1)
|
1997-05-02 03:12:38 +00:00
|
|
|
_PyString_Resize(&result, len-1);
|
1992-09-25 21:59:05 +00:00
|
|
|
else {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *v;
|
|
|
|
v = PyString_FromStringAndSize(s,
|
|
|
|
len-1);
|
|
|
|
Py_DECREF(result);
|
1992-09-25 21:59:05 +00:00
|
|
|
result = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
if (((PyFileObject*)f)->f_fp == NULL)
|
1992-07-06 14:19:26 +00:00
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
return getline((PyFileObject *)f, n);
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Python method */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-04-04 15:21:57 +00:00
|
|
|
file_readline(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-04-04 15:21:57 +00:00
|
|
|
{
|
1997-05-10 22:33:55 +00:00
|
|
|
int n = -1;
|
1991-04-04 15:21:57 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-10 22:33:55 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "|i", &n))
|
|
|
|
return NULL;
|
|
|
|
if (n == 0)
|
|
|
|
return PyString_FromString("");
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
1992-03-27 17:23:38 +00:00
|
|
|
return getline(f, n);
|
1991-04-04 15:21:57 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1991-03-06 13:06:18 +00:00
|
|
|
file_readlines(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1991-03-06 13:06:18 +00:00
|
|
|
{
|
1997-05-10 22:33:55 +00:00
|
|
|
long sizehint = 0;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *list;
|
|
|
|
PyObject *line;
|
1997-05-10 22:07:25 +00:00
|
|
|
char small_buffer[SMALLCHUNK];
|
|
|
|
char *buffer = small_buffer;
|
|
|
|
size_t buffersize = SMALLCHUNK;
|
|
|
|
PyObject *big_buffer = NULL;
|
|
|
|
size_t nfilled = 0;
|
|
|
|
size_t nread;
|
1997-05-10 22:33:55 +00:00
|
|
|
size_t totalread = 0;
|
1997-05-10 22:07:25 +00:00
|
|
|
char *p, *q, *end;
|
|
|
|
int err;
|
1991-04-04 15:21:57 +00:00
|
|
|
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-10 22:33:55 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "|l", &sizehint))
|
1991-04-04 15:21:57 +00:00
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
if ((list = PyList_New(0)) == NULL)
|
1991-03-06 13:06:18 +00:00
|
|
|
return NULL;
|
|
|
|
for (;;) {
|
1997-05-10 22:07:25 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nread == 0) {
|
1997-05-10 22:33:55 +00:00
|
|
|
sizehint = 0;
|
1997-05-10 22:07:25 +00:00
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
error:
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(list);
|
1997-05-10 22:07:25 +00:00
|
|
|
list = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
1997-05-10 22:33:55 +00:00
|
|
|
totalread += nread;
|
1997-05-10 22:07:25 +00:00
|
|
|
p = memchr(buffer+nfilled, '\n', nread);
|
|
|
|
if (p == NULL) {
|
|
|
|
/* Need a larger buffer to fit this line */
|
|
|
|
nfilled += nread;
|
|
|
|
buffersize *= 2;
|
|
|
|
if (big_buffer == NULL) {
|
|
|
|
/* Create the big buffer */
|
|
|
|
big_buffer = PyString_FromStringAndSize(
|
|
|
|
NULL, buffersize);
|
|
|
|
if (big_buffer == NULL)
|
|
|
|
goto error;
|
|
|
|
buffer = PyString_AS_STRING(big_buffer);
|
|
|
|
memcpy(buffer, small_buffer, nfilled);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Grow the big buffer */
|
|
|
|
_PyString_Resize(&big_buffer, buffersize);
|
|
|
|
buffer = PyString_AS_STRING(big_buffer);
|
|
|
|
}
|
|
|
|
continue;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1997-05-10 22:07:25 +00:00
|
|
|
end = buffer+nfilled+nread;
|
|
|
|
q = buffer;
|
|
|
|
do {
|
|
|
|
/* Process complete lines */
|
|
|
|
p++;
|
|
|
|
line = PyString_FromStringAndSize(q, p-q);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
|
|
|
err = PyList_Append(list, line);
|
|
|
|
Py_DECREF(line);
|
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
|
|
|
q = p;
|
|
|
|
p = memchr(q, '\n', end-q);
|
|
|
|
} while (p != NULL);
|
|
|
|
/* Move the remaining incomplete line to the start */
|
|
|
|
nfilled = end-q;
|
|
|
|
memmove(buffer, q, nfilled);
|
1997-05-10 22:33:55 +00:00
|
|
|
if (sizehint > 0)
|
|
|
|
if (totalread >= (size_t)sizehint)
|
|
|
|
break;
|
1997-05-10 22:07:25 +00:00
|
|
|
}
|
|
|
|
if (nfilled != 0) {
|
|
|
|
/* Partial last line */
|
|
|
|
line = PyString_FromStringAndSize(buffer, nfilled);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
1997-05-10 22:33:55 +00:00
|
|
|
if (sizehint > 0) {
|
|
|
|
/* Need to complete the last line */
|
|
|
|
PyObject *rest = getline(f, 0);
|
|
|
|
if (rest == NULL) {
|
|
|
|
Py_DECREF(line);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyString_Concat(&line, rest);
|
|
|
|
Py_DECREF(rest);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
1997-05-10 22:07:25 +00:00
|
|
|
err = PyList_Append(list, line);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(line);
|
1997-05-10 22:07:25 +00:00
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
1991-03-06 13:06:18 +00:00
|
|
|
}
|
1997-05-10 22:07:25 +00:00
|
|
|
cleanup:
|
|
|
|
if (big_buffer)
|
|
|
|
Py_DECREF(big_buffer);
|
1991-03-06 13:06:18 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_write(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1992-07-06 14:19:26 +00:00
|
|
|
char *s;
|
1990-10-14 12:07:46 +00:00
|
|
|
int n, n2;
|
1992-07-06 14:19:26 +00:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyArg_Parse(args, "s#", &s, &n))
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-04-04 10:44:06 +00:00
|
|
|
f->f_softspace = 0;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1990-10-14 12:07:46 +00:00
|
|
|
errno = 0;
|
1992-07-06 14:19:26 +00:00
|
|
|
n2 = fwrite(s, 1, n, f->f_fp);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1990-10-14 12:07:46 +00:00
|
|
|
if (n2 != n) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 16:39:24 +00:00
|
|
|
clearerr(f->f_fp);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1993-10-25 09:59:04 +00:00
|
|
|
file_writelines(f, args)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
|
|
|
PyObject *args;
|
1993-10-25 09:59:04 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 03:12:38 +00:00
|
|
|
if (args == NULL || !PyList_Check(args)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1993-10-25 09:59:04 +00:00
|
|
|
"writelines() requires list of strings");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
n = PyList_Size(args);
|
1993-10-25 09:59:04 +00:00
|
|
|
f->f_softspace = 0;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1993-10-25 09:59:04 +00:00
|
|
|
errno = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *line = PyList_GetItem(args, i);
|
1993-10-25 09:59:04 +00:00
|
|
|
int len;
|
|
|
|
int nwritten;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyString_Check(line)) {
|
|
|
|
Py_BLOCK_THREADS
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1993-10-25 09:59:04 +00:00
|
|
|
"writelines() requires list of strings");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
len = PyString_Size(line);
|
|
|
|
nwritten = fwrite(PyString_AsString(line), 1, len, f->f_fp);
|
1993-10-25 09:59:04 +00:00
|
|
|
if (nwritten != len) {
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_BLOCK_THREADS
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1993-10-25 09:59:04 +00:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1993-10-25 09:59:04 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyMethodDef file_methods[] = {
|
|
|
|
{"close", (PyCFunction)file_close, 0},
|
|
|
|
{"flush", (PyCFunction)file_flush, 0},
|
|
|
|
{"fileno", (PyCFunction)file_fileno, 0},
|
|
|
|
{"isatty", (PyCFunction)file_isatty, 0},
|
1997-05-10 22:33:55 +00:00
|
|
|
{"read", (PyCFunction)file_read, 1},
|
|
|
|
{"readline", (PyCFunction)file_readline, 1},
|
|
|
|
{"readlines", (PyCFunction)file_readlines, 1},
|
1997-05-02 03:12:38 +00:00
|
|
|
{"seek", (PyCFunction)file_seek, 0},
|
1995-01-02 19:07:15 +00:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
1997-05-02 03:12:38 +00:00
|
|
|
{"truncate", (PyCFunction)file_truncate, 0},
|
1995-01-02 19:07:15 +00:00
|
|
|
#endif
|
1997-05-02 03:12:38 +00:00
|
|
|
{"tell", (PyCFunction)file_tell, 0},
|
|
|
|
{"write", (PyCFunction)file_write, 0},
|
|
|
|
{"writelines", (PyCFunction)file_writelines, 0},
|
1997-05-05 22:15:02 +00:00
|
|
|
{"readinto", (PyCFunction)file_readinto, 0},
|
1990-10-14 12:07:46 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#define OFF(x) offsetof(PyFileObject, x)
|
1994-08-01 11:34:53 +00:00
|
|
|
|
|
|
|
static struct memberlist file_memberlist[] = {
|
|
|
|
{"softspace", T_INT, OFF(f_softspace)},
|
|
|
|
{"mode", T_OBJECT, OFF(f_mode), RO},
|
|
|
|
{"name", T_OBJECT, OFF(f_name), RO},
|
|
|
|
/* getattr(f, "closed") is implemented without this table */
|
|
|
|
{"closed", T_INT, 0, RO},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
file_getattr(f, name)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *res;
|
1994-08-01 11:34:53 +00:00
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
res = Py_FindMethod(file_methods, (PyObject *)f, name);
|
1994-08-01 11:34:53 +00:00
|
|
|
if (res != NULL)
|
|
|
|
return res;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_Clear();
|
1994-08-01 11:34:53 +00:00
|
|
|
if (strcmp(name, "closed") == 0)
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyInt_FromLong((long)(f->f_fp == 0));
|
|
|
|
return PyMember_Get((char *)f, file_memberlist, name);
|
1994-08-01 11:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_setattr(f, name, v)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFileObject *f;
|
1994-08-01 11:34:53 +00:00
|
|
|
char *name;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *v;
|
1994-08-01 11:34:53 +00:00
|
|
|
{
|
|
|
|
if (v == NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
"can't delete file attributes");
|
1994-08-01 11:34:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyMember_Set((char *)f, file_memberlist, name, v);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyTypeObject PyFile_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
|
|
|
"file",
|
1997-05-02 03:12:38 +00:00
|
|
|
sizeof(PyFileObject),
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
1994-08-01 11:34:53 +00:00
|
|
|
(destructor)file_dealloc, /*tp_dealloc*/
|
1992-09-17 17:54:56 +00:00
|
|
|
0, /*tp_print*/
|
1994-08-01 11:34:53 +00:00
|
|
|
(getattrfunc)file_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)file_setattr, /*tp_setattr*/
|
1990-10-14 12:07:46 +00:00
|
|
|
0, /*tp_compare*/
|
1994-08-01 11:34:53 +00:00
|
|
|
(reprfunc)file_repr, /*tp_repr*/
|
1990-10-14 12:07:46 +00:00
|
|
|
};
|
1991-04-04 10:44:06 +00:00
|
|
|
|
|
|
|
/* Interface for the 'soft space' between print items. */
|
|
|
|
|
|
|
|
int
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFile_SoftSpace(f, newflag)
|
|
|
|
PyObject *f;
|
1991-04-04 10:44:06 +00:00
|
|
|
int newflag;
|
|
|
|
{
|
|
|
|
int oldflag = 0;
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
else if (PyFile_Check(f)) {
|
|
|
|
oldflag = ((PyFileObject *)f)->f_softspace;
|
|
|
|
((PyFileObject *)f)->f_softspace = newflag;
|
1991-04-04 10:44:06 +00:00
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
else {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *v;
|
|
|
|
v = PyObject_GetAttrString(f, "softspace");
|
1992-09-25 21:59:05 +00:00
|
|
|
if (v == NULL)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_Clear();
|
1992-09-25 21:59:05 +00:00
|
|
|
else {
|
1997-05-02 03:12:38 +00:00
|
|
|
if (PyInt_Check(v))
|
|
|
|
oldflag = PyInt_AsLong(v);
|
|
|
|
Py_DECREF(v);
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
v = PyInt_FromLong((long)newflag);
|
1992-09-25 21:59:05 +00:00
|
|
|
if (v == NULL)
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_Clear();
|
1992-09-25 21:59:05 +00:00
|
|
|
else {
|
1997-05-02 03:12:38 +00:00
|
|
|
if (PyObject_SetAttrString(f, "softspace", v) != 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(v);
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
1991-04-04 10:44:06 +00:00
|
|
|
return oldflag;
|
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
|
|
|
|
/* Interfaces to write objects/strings to file-like objects */
|
|
|
|
|
|
|
|
int
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFile_WriteObject(v, f, flags)
|
|
|
|
PyObject *v;
|
|
|
|
PyObject *f;
|
1992-09-25 21:59:05 +00:00
|
|
|
int flags;
|
|
|
|
{
|
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
|
|
|
else if (PyFile_Check(f)) {
|
|
|
|
FILE *fp = PyFile_AsFile(f);
|
1992-09-25 21:59:05 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
err_closed();
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyObject_Print(v, fp, flags);
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
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;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (flags & Py_PRINT_RAW)
|
|
|
|
value = PyObject_Str(v);
|
1993-11-05 10:22:19 +00:00
|
|
|
else
|
1997-05-02 03:12:38 +00:00
|
|
|
value = PyObject_Repr(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
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
args = Py_BuildValue("(O)", 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
|
1997-05-02 03:12:38 +00:00
|
|
|
PyFile_WriteString(s, f)
|
1992-09-25 21:59:05 +00:00
|
|
|
char *s;
|
1997-05-02 03:12:38 +00:00
|
|
|
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 */
|
|
|
|
if(!PyErr_Occurred())
|
|
|
|
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 (PyFile_Check(f)) {
|
|
|
|
FILE *fp = PyFile_AsFile(f);
|
1997-05-22 22:25:11 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
err_closed();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fputs(s, fp);
|
|
|
|
return 0;
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
else if (!PyErr_Occurred()) {
|
|
|
|
PyObject *v = PyString_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
|
|
|
}
|
|
|
|
}
|