1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* File object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1994-08-01 08:34:53 -03:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1999-08-27 17:39:37 -03:00
|
|
|
#ifndef DONT_HAVE_SYS_TYPES_H
|
1999-01-07 18:09:51 -04:00
|
|
|
#include <sys/types.h>
|
1999-08-27 17:39:37 -03:00
|
|
|
#endif /* DONT_HAVE_SYS_TYPES_H */
|
2000-06-28 17:57:07 -03:00
|
|
|
|
1997-05-06 12:23:24 -03:00
|
|
|
#ifdef MS_WIN32
|
|
|
|
#define fileno _fileno
|
2002-03-10 20:24:00 -04:00
|
|
|
/* can simulate truncate with Win32 API functions; see file_truncate */
|
1997-05-06 12:23:24 -03:00
|
|
|
#define HAVE_FTRUNCATE
|
2002-03-10 20:24:00 -04:00
|
|
|
#define WINDOWS_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
1997-05-06 12:23:24 -03:00
|
|
|
#endif
|
|
|
|
|
1998-04-28 13:05:59 -03:00
|
|
|
#ifdef macintosh
|
|
|
|
#ifdef USE_GUSI
|
|
|
|
#define HAVE_FTRUNCATE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1995-04-23 19:12:47 -03:00
|
|
|
#ifdef __MWERKS__
|
|
|
|
/* Mwerks fopen() doesn't always set errno */
|
|
|
|
#define NO_FOPEN_ERRNO
|
|
|
|
#endif
|
1995-02-19 11:55:19 -04:00
|
|
|
|
2002-02-26 07:36:35 -04:00
|
|
|
#if defined(PYOS_OS2) && defined(PYCC_GCC)
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
|
1991-03-06 09:06:18 -04:00
|
|
|
|
1999-08-27 17:39:37 -03:00
|
|
|
#ifndef DONT_HAVE_ERRNO_H
|
1993-07-05 07:31:29 -03:00
|
|
|
#include <errno.h>
|
1999-08-27 17:39:37 -03:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-04-14 17:12:41 -03: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)
|
|
|
|
#define FUNLOCKFILE(f)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
/* Bits in f_newlinetypes */
|
|
|
|
#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 */
|
|
|
|
#endif
|
2000-08-11 16:02:59 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
FILE *
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_AsFile(PyObject *f)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (f == NULL || !PyFile_Check(f))
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1992-09-25 18:59:05 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyFileObject *)f)->f_fp;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_Name(PyObject *f)
|
1993-10-18 14:06:59 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (f == NULL || !PyFile_Check(f))
|
1993-10-18 14:06:59 -03:00
|
|
|
return NULL;
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyFileObject *)f)->f_name;
|
1993-10-18 14:06:59 -03:00
|
|
|
}
|
|
|
|
|
2002-03-22 22:06:50 -04:00
|
|
|
/* On Unix, fopen will succeed for directories.
|
|
|
|
In Python, there should be no file objects referring to
|
|
|
|
directories, so we need a check. */
|
|
|
|
|
|
|
|
static PyFileObject*
|
|
|
|
dircheck(PyFileObject* f)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
|
|
|
|
struct stat buf;
|
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return f;
|
|
|
|
if (fstat(fileno(f->f_fp), &buf) == 0 &&
|
|
|
|
S_ISDIR(buf.st_mode)) {
|
|
|
|
#ifdef HAVE_STRERROR
|
|
|
|
char *msg = strerror(EISDIR);
|
|
|
|
#else
|
|
|
|
char *msg = "Is a directory";
|
|
|
|
#endif
|
|
|
|
PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)", EISDIR, msg);
|
|
|
|
PyErr_SetObject(PyExc_IOError, exc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2001-09-13 02:38:56 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
|
|
|
|
int (*close)(FILE *))
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-09-13 02:38:56 -03:00
|
|
|
assert(f != NULL);
|
|
|
|
assert(PyFile_Check(f));
|
2001-09-14 00:26:08 -03:00
|
|
|
assert(f->f_fp == NULL);
|
|
|
|
|
|
|
|
Py_DECREF(f->f_name);
|
|
|
|
Py_DECREF(f->f_mode);
|
1997-05-02 00:12:38 -03:00
|
|
|
f->f_name = PyString_FromString(name);
|
|
|
|
f->f_mode = PyString_FromString(mode);
|
2001-09-14 00:26:08 -03:00
|
|
|
|
1991-06-04 16:37:39 -03:00
|
|
|
f->f_close = close;
|
1991-04-04 06:44:06 -04:00
|
|
|
f->f_softspace = 0;
|
2001-09-13 02:38:56 -03:00
|
|
|
f->f_binary = strchr(mode,'b') != NULL;
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
f->f_univ_newline = (strchr(mode, 'U') != NULL);
|
|
|
|
f->f_newlinetypes = NEWLINE_UNKNOWN;
|
|
|
|
f->f_skipnextlf = 0;
|
|
|
|
#endif
|
2001-09-14 00:26:08 -03:00
|
|
|
|
2001-09-13 02:38:56 -03:00
|
|
|
if (f->f_name == NULL || f->f_mode == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
f->f_fp = fp;
|
2002-03-22 22:06:50 -04:00
|
|
|
f = dircheck(f);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) f;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2001-09-13 02:38:56 -03:00
|
|
|
static PyObject *
|
|
|
|
open_the_file(PyFileObject *f, char *name, char *mode)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-09-13 02:38:56 -03:00
|
|
|
assert(f != NULL);
|
|
|
|
assert(PyFile_Check(f));
|
|
|
|
assert(name != NULL);
|
|
|
|
assert(mode != NULL);
|
2001-09-14 00:26:08 -03:00
|
|
|
assert(f->f_fp == NULL);
|
2001-09-13 02:38:56 -03:00
|
|
|
|
2001-09-13 18:01:29 -03:00
|
|
|
/* rexec.py can't stop a user from getting the file() constructor --
|
|
|
|
all they have to do is get *any* file object f, and then do
|
|
|
|
type(f). Here we prevent them from doing damage with it. */
|
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_IOError,
|
|
|
|
"file() constructor not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-09 16:59:14 -04:00
|
|
|
errno = 0;
|
1995-01-02 15:07:15 -04:00
|
|
|
#ifdef HAVE_FOPENRF
|
1991-02-13 19:25:27 -04:00
|
|
|
if (*mode == '*') {
|
|
|
|
FILE *fopenRF();
|
|
|
|
f->f_fp = fopenRF(name, mode+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
1992-08-05 16:58:53 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
|
|
|
|
mode = "rb";
|
|
|
|
#else
|
|
|
|
/* Compatibility: specifying U in a Python without universal
|
|
|
|
** newlines is allowed, and the file is opened as a normal text
|
|
|
|
** file.
|
|
|
|
*/
|
|
|
|
if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
|
|
|
|
mode = "r";
|
|
|
|
#endif
|
1992-08-05 16:58:53 -03:00
|
|
|
f->f_fp = fopen(name, mode);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
}
|
1991-02-13 19:25:27 -04:00
|
|
|
if (f->f_fp == NULL) {
|
1995-04-23 19:12:47 -03:00
|
|
|
#ifdef NO_FOPEN_ERRNO
|
2001-11-30 10:16:36 -04:00
|
|
|
/* Metroworks only, wich does not always sets errno */
|
2001-11-09 12:17:24 -04:00
|
|
|
if (errno == 0) {
|
2001-11-30 10:16:36 -04:00
|
|
|
PyObject *v;
|
|
|
|
v = Py_BuildValue("(is)", 0, "Cannot open file");
|
|
|
|
if (v != NULL) {
|
|
|
|
PyErr_SetObject(PyExc_IOError, v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
}
|
1995-04-23 19:12:47 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-04-08 01:13:12 -03:00
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
|
|
|
|
* across all Windows flavors. When it sets EINVAL varies
|
|
|
|
* across Windows flavors, the exact conditions aren't
|
|
|
|
* documented, and the answer lies in the OS's implementation
|
|
|
|
* of Win32's CreateFile function (whose source is secret).
|
|
|
|
* Seems the best we can do is map EINVAL to ENOENT.
|
|
|
|
*/
|
|
|
|
if (errno == 0) /* bad mode string */
|
|
|
|
errno = EINVAL;
|
|
|
|
else if (errno == EINVAL) /* unknown, but not a mode string */
|
|
|
|
errno = ENOENT;
|
1995-04-23 19:12:47 -03:00
|
|
|
#endif
|
2001-11-09 12:17:24 -04:00
|
|
|
if (errno == EINVAL)
|
2002-04-08 01:13:12 -03:00
|
|
|
PyErr_Format(PyExc_IOError, "invalid mode: %s",
|
2001-11-09 12:17:24 -04:00
|
|
|
mode);
|
|
|
|
else
|
|
|
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
|
2001-09-13 02:38:56 -03:00
|
|
|
f = NULL;
|
|
|
|
}
|
2002-04-08 01:13:12 -03:00
|
|
|
if (f != NULL)
|
2002-03-22 22:06:50 -04:00
|
|
|
f = dircheck(f);
|
2001-09-13 02:38:56 -03:00
|
|
|
return (PyObject *)f;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
|
|
|
|
{
|
2001-09-14 00:26:08 -03:00
|
|
|
PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
|
|
|
|
NULL, NULL);
|
2001-09-13 02:38:56 -03:00
|
|
|
if (f != NULL) {
|
|
|
|
if (fill_file_fields(f, fp, name, mode, close) == NULL) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
f = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (PyObject *) f;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyFile_FromString(char *name, char *mode)
|
|
|
|
{
|
|
|
|
extern int fclose(FILE *);
|
|
|
|
PyFileObject *f;
|
|
|
|
|
|
|
|
f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
|
|
|
|
if (f != NULL) {
|
|
|
|
if (open_the_file(f, name, mode) == NULL) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
f = NULL;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)f;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-08-01 08:34:53 -03:00
|
|
|
void
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_SetBufSize(PyObject *f, int bufsize)
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
|
|
|
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 00:12:38 -03:00
|
|
|
setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
|
|
|
|
type, bufsize);
|
1998-03-06 11:32:40 -04:00
|
|
|
#else /* !HAVE_SETVBUF */
|
|
|
|
if (bufsize <= 1)
|
|
|
|
setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
|
|
|
|
#endif /* !HAVE_SETVBUF */
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
err_closed(void)
|
1992-07-06 11:19:26 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
|
1992-07-06 11:19:26 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
2000-07-09 02:02:18 -03:00
|
|
|
file_dealloc(PyFileObject *f)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-08-05 16:58:53 -03:00
|
|
|
if (f->f_fp != NULL && f->f_close != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-06-04 16:37:39 -03:00
|
|
|
(*f->f_close)(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
}
|
2001-09-14 00:26:08 -03:00
|
|
|
Py_XDECREF(f->f_name);
|
|
|
|
Py_XDECREF(f->f_mode);
|
2001-10-05 17:51:39 -03:00
|
|
|
f->ob_type->tp_free((PyObject *)f);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_repr(PyFileObject *f)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-08-24 15:34:26 -03:00
|
|
|
return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
|
|
|
|
f->f_fp == NULL ? "closed" : "open",
|
|
|
|
PyString_AsString(f->f_name),
|
|
|
|
PyString_AsString(f->f_mode),
|
|
|
|
f);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_close(PyFileObject *f)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-06-04 16:37:39 -03:00
|
|
|
int sts = 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
if (f->f_fp != NULL) {
|
1992-08-05 16:58:53 -03:00
|
|
|
if (f->f_close != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
errno = 0;
|
1991-06-04 16:37:39 -03:00
|
|
|
sts = (*f->f_close)(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
f->f_fp = NULL;
|
|
|
|
}
|
1992-03-04 12:39:24 -04:00
|
|
|
if (sts == EOF)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
1991-06-04 16:37:39 -03:00
|
|
|
if (sts != 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong((long)sts);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2000-08-11 16:02:59 -03:00
|
|
|
|
2001-09-05 11:58:11 -03:00
|
|
|
/* Our very own off_t-like type, 64-bit if possible */
|
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
typedef off_t Py_off_t;
|
|
|
|
#elif SIZEOF_OFF_T >= 8
|
|
|
|
typedef off_t Py_off_t;
|
|
|
|
#elif SIZEOF_FPOS_T >= 8
|
2001-03-01 14:26:53 -04:00
|
|
|
typedef fpos_t Py_off_t;
|
2000-08-11 16:02:59 -03:00
|
|
|
#else
|
2001-09-05 11:58:11 -03:00
|
|
|
#error "Large file support, but neither off_t nor fpos_t is large enough."
|
2000-08-11 16:02:59 -03:00
|
|
|
#endif
|
2001-03-01 14:26:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* a portable fseek() function
|
|
|
|
return 0 on success, non-zero on failure (with errno set) */
|
2001-04-14 14:55:09 -03:00
|
|
|
static int
|
2001-03-01 14:26:53 -04:00
|
|
|
_portable_fseek(FILE *fp, Py_off_t offset, int whence)
|
2000-08-11 16:02:59 -03:00
|
|
|
{
|
2001-09-05 11:58:11 -03:00
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
return fseek(fp, offset, whence);
|
|
|
|
#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
|
2000-08-11 16:02:59 -03:00
|
|
|
return fseeko(fp, offset, whence);
|
|
|
|
#elif defined(HAVE_FSEEK64)
|
|
|
|
return fseek64(fp, offset, whence);
|
2000-10-06 17:42:33 -03:00
|
|
|
#elif defined(__BEOS__)
|
|
|
|
return _fseek(fp, offset, whence);
|
2001-09-05 11:58:11 -03:00
|
|
|
#elif SIZEOF_FPOS_T >= 8
|
2001-01-16 16:53:31 -04:00
|
|
|
/* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
|
|
|
|
and fgetpos() to implement fseek()*/
|
2000-08-11 16:02:59 -03:00
|
|
|
fpos_t pos;
|
|
|
|
switch (whence) {
|
2001-01-16 16:53:31 -04:00
|
|
|
case SEEK_END:
|
_portable_fseek():
Subtlety on Windows: if we change test_largefile.py to use a file
> 4GB, it still fails. A debug session suggests this is because
fseek(fp, 0, 2) refuses to seek to the end of the file when the file
is > 4GB, because it uses the SetFilePointer() in 32-bit mode.
But it only fails when we seek relative to the end of the file,
because in the other seek modes only calls to fgetpos() and fsetpos()
are made, which use Get/SetFilePointer() in 64-bit mode. Solution:
#ifdef MS_WInDOWS, replace the call to fseek(fp, ...) with a call to
_lseeki64(fileno(fp), ...). Make sure to call fflush(fp) first.
(XXX Could also replace the entire branch with a call to _lseeki64().
Would that be more efficient? Certainly less generated code.)
(XXX This needs more testing. I can't actually test that it works for
files >4GB on my Win98 machine, because the filesystem here won't let
me create files >=4GB at all. Tim should test this on his Win2K
machine.)
2001-09-10 17:43:35 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
fflush(fp);
|
|
|
|
if (_lseeki64(fileno(fp), 0, 2) == -1)
|
|
|
|
return -1;
|
|
|
|
#else
|
2001-01-16 16:53:31 -04:00
|
|
|
if (fseek(fp, 0, SEEK_END) != 0)
|
|
|
|
return -1;
|
_portable_fseek():
Subtlety on Windows: if we change test_largefile.py to use a file
> 4GB, it still fails. A debug session suggests this is because
fseek(fp, 0, 2) refuses to seek to the end of the file when the file
is > 4GB, because it uses the SetFilePointer() in 32-bit mode.
But it only fails when we seek relative to the end of the file,
because in the other seek modes only calls to fgetpos() and fsetpos()
are made, which use Get/SetFilePointer() in 64-bit mode. Solution:
#ifdef MS_WInDOWS, replace the call to fseek(fp, ...) with a call to
_lseeki64(fileno(fp), ...). Make sure to call fflush(fp) first.
(XXX Could also replace the entire branch with a call to _lseeki64().
Would that be more efficient? Certainly less generated code.)
(XXX This needs more testing. I can't actually test that it works for
files >4GB on my Win98 machine, because the filesystem here won't let
me create files >=4GB at all. Tim should test this on his Win2K
machine.)
2001-09-10 17:43:35 -03:00
|
|
|
#endif
|
2001-01-16 16:53:31 -04:00
|
|
|
/* fall through */
|
|
|
|
case SEEK_CUR:
|
|
|
|
if (fgetpos(fp, &pos) != 0)
|
|
|
|
return -1;
|
|
|
|
offset += pos;
|
|
|
|
break;
|
|
|
|
/* case SEEK_SET: break; */
|
2000-08-11 16:02:59 -03:00
|
|
|
}
|
|
|
|
return fsetpos(fp, &offset);
|
|
|
|
#else
|
2001-09-05 11:58:11 -03:00
|
|
|
#error "Large file support, but no way to fseek."
|
2000-08-11 16:02:59 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* a portable ftell() function
|
|
|
|
Return -1 on failure with errno set appropriately, current file
|
|
|
|
position on success */
|
2001-04-14 14:55:09 -03:00
|
|
|
static Py_off_t
|
2000-08-31 02:18:54 -03:00
|
|
|
_portable_ftell(FILE* fp)
|
2000-08-11 16:02:59 -03:00
|
|
|
{
|
2001-09-05 11:58:11 -03:00
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
return ftell(fp);
|
|
|
|
#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
|
|
|
|
return ftello(fp);
|
|
|
|
#elif defined(HAVE_FTELL64)
|
|
|
|
return ftell64(fp);
|
|
|
|
#elif SIZEOF_FPOS_T >= 8
|
2000-08-11 16:02:59 -03:00
|
|
|
fpos_t pos;
|
|
|
|
if (fgetpos(fp, &pos) != 0)
|
|
|
|
return -1;
|
|
|
|
return pos;
|
|
|
|
#else
|
2001-09-05 11:58:11 -03:00
|
|
|
#error "Large file support, but no way to ftell."
|
2000-08-11 16:02:59 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_seek(PyFileObject *f, PyObject *args)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-07-06 11:19:26 -03:00
|
|
|
int whence;
|
1992-08-05 16:58:53 -03:00
|
|
|
int ret;
|
2001-03-01 14:26:53 -04:00
|
|
|
Py_off_t offset;
|
1999-01-06 14:51:17 -04:00
|
|
|
PyObject *offobj;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
whence = 0;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
|
1999-01-06 14:51:17 -04:00
|
|
|
return NULL;
|
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
offset = PyInt_AsLong(offobj);
|
|
|
|
#else
|
|
|
|
offset = PyLong_Check(offobj) ?
|
|
|
|
PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
|
|
|
|
#endif
|
|
|
|
if (PyErr_Occurred())
|
1999-01-04 13:22:18 -04:00
|
|
|
return NULL;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 09:06:18 -04:00
|
|
|
errno = 0;
|
2000-08-11 16:02:59 -03:00
|
|
|
ret = _portable_fseek(f->f_fp, offset, whence);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2000-08-11 16:02:59 -03:00
|
|
|
|
1992-08-05 16:58:53 -03:00
|
|
|
if (ret != 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 12:39:24 -04:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
f->f_skipnextlf = 0;
|
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
|
|
|
|
2000-08-11 16:02:59 -03:00
|
|
|
|
1995-01-02 15:07:15 -04:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_truncate(PyFileObject *f, PyObject *args)
|
1995-01-02 15:07:15 -04:00
|
|
|
{
|
|
|
|
int ret;
|
2001-03-01 14:26:53 -04:00
|
|
|
Py_off_t newsize;
|
1999-01-06 14:51:17 -04:00
|
|
|
PyObject *newsizeobj;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1995-01-02 15:07:15 -04:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1999-01-06 14:51:17 -04:00
|
|
|
newsizeobj = NULL;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
|
1999-01-04 13:22:18 -04:00
|
|
|
return NULL;
|
2002-03-10 20:24:00 -04:00
|
|
|
|
|
|
|
/* Set newsize to current postion if newsizeobj NULL, else to the
|
|
|
|
specified value. */
|
1999-01-06 14:51:17 -04:00
|
|
|
if (newsizeobj != NULL) {
|
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
|
|
|
newsize = PyInt_AsLong(newsizeobj);
|
|
|
|
#else
|
|
|
|
newsize = PyLong_Check(newsizeobj) ?
|
|
|
|
PyLong_AsLongLong(newsizeobj) :
|
|
|
|
PyInt_AsLong(newsizeobj);
|
|
|
|
#endif
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2002-03-10 20:24:00 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Default to current position. */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 15:07:15 -04:00
|
|
|
errno = 0;
|
2000-08-11 16:02:59 -03:00
|
|
|
newsize = _portable_ftell(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-03-10 20:24:00 -04:00
|
|
|
if (newsize == -1)
|
|
|
|
goto onioerror;
|
1995-01-02 15:07:15 -04:00
|
|
|
}
|
2002-03-10 20:24:00 -04:00
|
|
|
|
|
|
|
/* Flush the file. */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1995-01-02 15:07:15 -04:00
|
|
|
errno = 0;
|
|
|
|
ret = fflush(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-03-10 20:24:00 -04:00
|
|
|
if (ret != 0)
|
|
|
|
goto onioerror;
|
2000-08-11 16:02:59 -03:00
|
|
|
|
|
|
|
#ifdef MS_WIN32
|
2002-03-10 20:24:00 -04:00
|
|
|
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
|
2002-03-11 23:04:44 -04:00
|
|
|
so don't even try using it. */
|
2002-03-10 20:24:00 -04:00
|
|
|
{
|
2002-03-11 23:04:44 -04:00
|
|
|
Py_off_t current; /* current file position */
|
2002-03-10 20:24:00 -04:00
|
|
|
HANDLE hFile;
|
|
|
|
int error;
|
|
|
|
|
2002-03-11 23:04:44 -04:00
|
|
|
/* current <- current file postion. */
|
|
|
|
if (newsizeobj == NULL)
|
|
|
|
current = newsize;
|
|
|
|
else {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-03-10 20:24:00 -04:00
|
|
|
errno = 0;
|
2002-03-11 23:04:44 -04:00
|
|
|
current = _portable_ftell(f->f_fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (current == -1)
|
2002-03-10 20:24:00 -04:00
|
|
|
goto onioerror;
|
2002-03-11 23:04:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move to newsize. */
|
|
|
|
if (current != newsize) {
|
2002-03-10 20:24:00 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
2002-03-11 23:04:44 -04:00
|
|
|
error = _portable_fseek(f->f_fp, newsize, SEEK_SET)
|
|
|
|
!= 0;
|
2002-03-10 20:24:00 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (error)
|
|
|
|
goto onioerror;
|
|
|
|
}
|
|
|
|
|
2002-03-11 23:04:44 -04:00
|
|
|
/* Truncate. Note that this may grow the file! */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
|
|
|
|
error = hFile == (HANDLE)-1;
|
|
|
|
if (!error) {
|
|
|
|
error = SetEndOfFile(hFile) == 0;
|
|
|
|
if (error)
|
|
|
|
errno = EACCES;
|
|
|
|
}
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (error)
|
|
|
|
goto onioerror;
|
|
|
|
|
|
|
|
/* Restore original file position. */
|
|
|
|
if (current != newsize) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
error = _portable_fseek(f->f_fp, current, SEEK_SET)
|
|
|
|
!= 0;
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (error)
|
|
|
|
goto onioerror;
|
|
|
|
}
|
1995-01-02 15:07:15 -04:00
|
|
|
}
|
2000-08-11 16:02:59 -03:00
|
|
|
#else
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
ret = ftruncate(fileno(f->f_fp), newsize);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (ret != 0) goto onioerror;
|
|
|
|
#endif /* !MS_WIN32 */
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2000-08-11 16:02:59 -03:00
|
|
|
|
|
|
|
onioerror:
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1995-01-02 15:07:15 -04:00
|
|
|
}
|
|
|
|
#endif /* HAVE_FTRUNCATE */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_tell(PyFileObject *f)
|
1991-03-06 09:06:18 -04:00
|
|
|
{
|
2001-03-01 14:26:53 -04:00
|
|
|
Py_off_t pos;
|
2000-08-11 16:02:59 -03:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 09:06:18 -04:00
|
|
|
errno = 0;
|
2000-08-11 16:02:59 -03:00
|
|
|
pos = _portable_ftell(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2000-08-11 16:02:59 -03:00
|
|
|
if (pos == -1) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 12:39:24 -04:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
if (f->f_skipnextlf) {
|
|
|
|
int c;
|
|
|
|
c = GETC(f->f_fp);
|
|
|
|
if (c == '\n') {
|
|
|
|
pos++;
|
|
|
|
f->f_skipnextlf = 0;
|
|
|
|
} else if (c != EOF) ungetc(c, f->f_fp);
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-06 14:51:17 -04:00
|
|
|
#if !defined(HAVE_LARGEFILE_SUPPORT)
|
2000-08-11 16:02:59 -03:00
|
|
|
return PyInt_FromLong(pos);
|
1999-01-06 14:51:17 -04:00
|
|
|
#else
|
2000-08-11 16:02:59 -03:00
|
|
|
return PyLong_FromLongLong(pos);
|
1999-01-06 14:51:17 -04:00
|
|
|
#endif
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_fileno(PyFileObject *f)
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong((long) fileno(f->f_fp));
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_flush(PyFileObject *f)
|
1991-03-06 09:06:18 -04:00
|
|
|
{
|
1992-08-05 16:58:53 -03:00
|
|
|
int res;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1991-03-06 09:06:18 -04:00
|
|
|
errno = 0;
|
1992-08-05 16:58:53 -03:00
|
|
|
res = fflush(f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
if (res != 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 12:39:24 -04:00
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_isatty(PyFileObject *f)
|
1991-06-04 16:37:39 -03:00
|
|
|
{
|
1992-08-05 16:58:53 -03:00
|
|
|
long res;
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-05 16:58:53 -03:00
|
|
|
res = isatty((int)fileno(f->f_fp));
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-04-07 03:28:00 -03:00
|
|
|
return PyBool_FromLong(res);
|
1991-06-04 16:37:39 -03:00
|
|
|
}
|
|
|
|
|
1999-08-27 17:39:37 -03:00
|
|
|
|
1997-05-09 19:27:31 -03:00
|
|
|
#if BUFSIZ < 8192
|
|
|
|
#define SMALLCHUNK 8192
|
|
|
|
#else
|
|
|
|
#define SMALLCHUNK BUFSIZ
|
|
|
|
#endif
|
|
|
|
|
1999-01-14 15:00:14 -04:00
|
|
|
#if SIZEOF_INT < 4
|
|
|
|
#define BIGCHUNK (512 * 32)
|
|
|
|
#else
|
|
|
|
#define BIGCHUNK (512 * 1024)
|
|
|
|
#endif
|
1997-05-09 19:27:31 -03:00
|
|
|
|
|
|
|
static size_t
|
2000-07-09 02:02:18 -03:00
|
|
|
new_buffersize(PyFileObject *f, size_t currentsize)
|
1997-05-09 19:27:31 -03:00
|
|
|
{
|
|
|
|
#ifdef HAVE_FSTAT
|
2001-07-19 18:49:38 -03:00
|
|
|
off_t pos, end;
|
1997-05-09 19:27:31 -03:00
|
|
|
struct stat st;
|
|
|
|
if (fstat(fileno(f->f_fp), &st) == 0) {
|
|
|
|
end = st.st_size;
|
1998-12-11 16:44:56 -04:00
|
|
|
/* The following is not a bug: we really need to call lseek()
|
|
|
|
*and* ftell(). The reason is that some stdio libraries
|
|
|
|
mistakenly flush their buffer when ftell() is called and
|
|
|
|
the lseek() call it makes fails, thereby throwing away
|
|
|
|
data that cannot be recovered in any way. To avoid this,
|
|
|
|
we first test lseek(), and only call ftell() if lseek()
|
|
|
|
works. We can't use the lseek() value either, because we
|
|
|
|
need to take the amount of buffered data into account.
|
|
|
|
(Yet another reason why stdio stinks. :-) */
|
2001-10-10 19:03:27 -03:00
|
|
|
#ifdef USE_GUSI2
|
|
|
|
pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
|
|
|
|
pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
|
|
|
|
#else
|
1998-05-05 19:21:35 -03:00
|
|
|
pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
|
2001-10-10 19:03:27 -03:00
|
|
|
#endif
|
|
|
|
if (pos >= 0) {
|
1998-05-05 19:21:35 -03:00
|
|
|
pos = ftell(f->f_fp);
|
2001-10-10 19:03:27 -03:00
|
|
|
}
|
1998-04-27 16:01:08 -03:00
|
|
|
if (pos < 0)
|
|
|
|
clearerr(f->f_fp);
|
1997-05-09 19:27:31 -03:00
|
|
|
if (end > pos && pos >= 0)
|
1998-12-11 16:44:56 -04:00
|
|
|
return currentsize + end - pos + 1;
|
1998-03-03 18:36:10 -04:00
|
|
|
/* Add 1 so if the file were to grow we'd notice. */
|
1997-05-09 19:27:31 -03:00
|
|
|
}
|
|
|
|
#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 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_read(PyFileObject *f, PyObject *args)
|
1991-03-06 09:06:18 -04:00
|
|
|
{
|
1997-05-10 19:33:55 -03:00
|
|
|
long bytesrequested = -1;
|
1997-05-09 19:27:31 -03:00
|
|
|
size_t bytesread, buffersize, chunksize;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
|
1997-05-10 19:33:55 -03:00
|
|
|
return NULL;
|
1997-05-09 19:27:31 -03:00
|
|
|
if (bytesrequested < 0)
|
1999-04-10 12:48:23 -03:00
|
|
|
buffersize = new_buffersize(f, (size_t)0);
|
1997-05-09 19:27:31 -03:00
|
|
|
else
|
|
|
|
buffersize = bytesrequested;
|
2000-08-11 16:02:59 -03:00
|
|
|
if (buffersize > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"requested number of bytes is more than a Python string can hold");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-09 19:27:31 -03:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, buffersize);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1997-05-09 19:27:31 -03:00
|
|
|
bytesread = 0;
|
1991-03-06 09:06:18 -04:00
|
|
|
for (;;) {
|
1997-05-10 19:07:25 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
2002-04-14 17:12:41 -03:00
|
|
|
chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
|
|
|
|
buffersize - bytesread, f->f_fp, (PyObject *)f);
|
1997-05-10 19:07:25 -03: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 19:27:31 -03:00
|
|
|
bytesread += chunksize;
|
|
|
|
if (bytesread < buffersize)
|
1991-03-06 09:06:18 -04:00
|
|
|
break;
|
1997-05-09 19:27:31 -03:00
|
|
|
if (bytesrequested < 0) {
|
1998-12-11 16:44:56 -04:00
|
|
|
buffersize = new_buffersize(f, buffersize);
|
1997-05-09 19:27:31 -03:00
|
|
|
if (_PyString_Resize(&v, buffersize) < 0)
|
1991-03-06 09:06:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1997-05-09 19:27:31 -03:00
|
|
|
if (bytesread != buffersize)
|
|
|
|
_PyString_Resize(&v, bytesread);
|
1990-10-14 09:07:46 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-05-05 19:15:02 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_readinto(PyFileObject *f, PyObject *args)
|
1997-05-05 19:15:02 -03:00
|
|
|
{
|
|
|
|
char *ptr;
|
2001-10-23 18:25:24 -03:00
|
|
|
int ntodo;
|
|
|
|
size_t ndone, nnow;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
1997-05-05 19:15:02 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2002-03-31 20:09:00 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
|
1997-05-05 19:15:02 -03:00
|
|
|
return NULL;
|
|
|
|
ndone = 0;
|
1997-05-10 19:07:25 -03:00
|
|
|
while (ntodo > 0) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
2002-04-14 17:12:41 -03:00
|
|
|
nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp, (PyObject *)f);
|
1997-05-10 19:07:25 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nnow == 0) {
|
|
|
|
if (!ferror(f->f_fp))
|
|
|
|
break;
|
1997-05-05 19:15:02 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ndone += nnow;
|
|
|
|
ntodo -= nnow;
|
|
|
|
}
|
2000-08-11 16:02:59 -03:00
|
|
|
return PyInt_FromLong((long)ndone);
|
1997-05-05 19:15:02 -03:00
|
|
|
}
|
|
|
|
|
2001-01-07 17:19:34 -04:00
|
|
|
/**************************************************************************
|
2001-01-15 02:33:19 -04:00
|
|
|
Routine to get next line using platform fgets().
|
2001-01-07 17:19:34 -04:00
|
|
|
|
|
|
|
Under MSVC 6:
|
|
|
|
|
2001-01-08 00:02:07 -04:00
|
|
|
+ MS threadsafe getc is very slow (multiple layers of function calls before+
|
|
|
|
after each character, to lock+unlock the stream).
|
|
|
|
+ The stream-locking functions are MS-internal -- can't access them from user
|
|
|
|
code.
|
|
|
|
+ There's nothing Tim could find in the MS C or platform SDK libraries that
|
|
|
|
can worm around this.
|
2001-01-07 17:19:34 -04:00
|
|
|
+ MS fgets locks/unlocks only once per line; it's the only hook we have.
|
|
|
|
|
|
|
|
So we use fgets for speed(!), despite that it's painful.
|
|
|
|
|
|
|
|
MS realloc is also slow.
|
|
|
|
|
2001-01-15 02:33:19 -04:00
|
|
|
Reports from other platforms on this method vs getc_unlocked (which MS doesn't
|
|
|
|
have):
|
|
|
|
Linux a wash
|
|
|
|
Solaris a wash
|
|
|
|
Tru64 Unix getline_via_fgets significantly faster
|
|
|
|
|
|
|
|
CAUTION: The C std isn't clear about this: in those cases where fgets
|
|
|
|
writes something into the buffer, can it write into any position beyond the
|
|
|
|
required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
|
|
|
|
known on which it does; and it would be a strange way to code fgets. Still,
|
|
|
|
getline_via_fgets may not work correctly if it does. The std test
|
|
|
|
test_bufio.py should fail if platform fgets() routinely writes beyond the
|
|
|
|
trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
|
2001-01-07 17:19:34 -04:00
|
|
|
**************************************************************************/
|
|
|
|
|
2001-01-15 02:33:19 -04:00
|
|
|
/* Use this routine if told to, or by default on non-get_unlocked()
|
|
|
|
* platforms unless told not to. Yikes! Let's spell that out:
|
|
|
|
* On a platform with getc_unlocked():
|
|
|
|
* By default, use getc_unlocked().
|
|
|
|
* If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
|
|
|
|
* On a platform without getc_unlocked():
|
|
|
|
* By default, use fgets().
|
|
|
|
* If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
|
|
|
|
*/
|
|
|
|
#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
|
|
|
|
#define USE_FGETS_IN_GETLINE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
|
|
|
|
#undef USE_FGETS_IN_GETLINE
|
2001-01-07 17:19:34 -04:00
|
|
|
#endif
|
|
|
|
|
2001-01-15 02:33:19 -04:00
|
|
|
#ifdef USE_FGETS_IN_GETLINE
|
2001-01-07 17:19:34 -04:00
|
|
|
static PyObject*
|
2001-01-15 02:33:19 -04:00
|
|
|
getline_via_fgets(FILE *fp)
|
2001-01-07 17:19:34 -04:00
|
|
|
{
|
2001-01-07 20:53:12 -04:00
|
|
|
/* INITBUFSIZE is the maximum line length that lets us get away with the fast
|
2001-01-15 06:36:56 -04:00
|
|
|
* no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
|
|
|
|
* to fill this much of the buffer with a known value in order to figure out
|
|
|
|
* how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
|
|
|
|
* than "most" lines, we waste time filling unused buffer slots. 100 is
|
|
|
|
* surely adequate for most peoples' email archives, chewing over source code,
|
|
|
|
* etc -- "regular old text files".
|
|
|
|
* MAXBUFSIZE is the maximum line length that lets us get away with the less
|
|
|
|
* fast (but still zippy) no-realloc, two-fgets()-call path. See above for
|
|
|
|
* cautions about boosting that. 300 was chosen because the worst real-life
|
|
|
|
* text-crunching job reported on Python-Dev was a mail-log crawler where over
|
|
|
|
* half the lines were 254 chars.
|
2001-01-07 20:53:12 -04:00
|
|
|
*/
|
2001-01-15 06:36:56 -04:00
|
|
|
#define INITBUFSIZE 100
|
|
|
|
#define MAXBUFSIZE 300
|
|
|
|
char* p; /* temp */
|
|
|
|
char buf[MAXBUFSIZE];
|
2001-01-07 17:19:34 -04:00
|
|
|
PyObject* v; /* the string object result */
|
|
|
|
char* pvfree; /* address of next free slot */
|
|
|
|
char* pvend; /* address one beyond last free slot */
|
2001-01-15 06:36:56 -04:00
|
|
|
size_t nfree; /* # of free buffer slots; pvend-pvfree */
|
|
|
|
size_t total_v_size; /* total # of slots in buffer */
|
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
When WITH_PYMALLOC is defined, define PYMALLOC_DEBUG to enable the debug
allocator. This can be done independent of build type (release or debug).
A debug build automatically defines PYMALLOC_DEBUG when pymalloc is
enabled. It's a detected error to define PYMALLOC_DEBUG when pymalloc
isn't enabled.
Two debugging entry points defined only under PYMALLOC_DEBUG:
+ _PyMalloc_DebugCheckAddress(const void *p) can be used (e.g., from gdb)
to sanity-check a memory block obtained from pymalloc. It sprays
info to stderr (see next) and dies via Py_FatalError if the block is
detectably damaged.
+ _PyMalloc_DebugDumpAddress(const void *p) can be used to spray info
about a debug memory block to stderr.
A tiny start at implementing "API family" checks isn't good for
anything yet.
_PyMalloc_DebugRealloc() has been optimized to do little when the new
size is <= old size. However, if the new size is larger, it really
can't call the underlying realloc() routine without either violating its
contract, or knowing something non-trivial about how the underlying
realloc() works. A memcpy is always done in this case.
This was a disaster for (and only) one of the std tests: test_bufio
creates single text file lines up to a million characters long. On
Windows, fileobject.c's get_line() uses the horridly funky
getline_via_fgets(), which keeps growing and growing a string object
hoping to find a newline. It grew the string object 1000 bytes each
time, so for a million-character string it took approximately forever
(I gave up after a few minutes).
So, also:
fileobject.c, getline_via_fgets(): When a single line is outrageously
long, grow the string object at a mildly exponential rate, instead of
just 1000 bytes at a time.
That's enough so that a debug-build test_bufio finishes in about 5 seconds
on my Win98SE box. I'm curious to try this on Win2K, because it has very
different memory behavior than Win9X, and test_bufio always took a factor
of 10 longer to complete on Win2K. It *could* be that the endless
reallocs were simply killing it on Win2K even in the release build.
2002-03-23 06:03:50 -04:00
|
|
|
size_t increment; /* amount to increment the buffer */
|
2001-01-07 17:19:34 -04:00
|
|
|
|
2001-01-07 20:53:12 -04:00
|
|
|
/* Optimize for normal case: avoid _PyString_Resize if at all
|
2001-01-15 06:36:56 -04:00
|
|
|
* possible via first reading into stack buffer "buf".
|
2001-01-07 20:53:12 -04:00
|
|
|
*/
|
2001-01-15 06:36:56 -04:00
|
|
|
total_v_size = INITBUFSIZE; /* start small and pray */
|
|
|
|
pvfree = buf;
|
|
|
|
for (;;) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
pvend = buf + total_v_size;
|
|
|
|
nfree = pvend - pvfree;
|
|
|
|
memset(pvfree, '\n', nfree);
|
|
|
|
p = fgets(pvfree, nfree, fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
2001-01-07 20:53:12 -04:00
|
|
|
|
2001-01-15 06:36:56 -04:00
|
|
|
if (p == NULL) {
|
|
|
|
clearerr(fp);
|
|
|
|
if (PyErr_CheckSignals())
|
|
|
|
return NULL;
|
|
|
|
v = PyString_FromStringAndSize(buf, pvfree - buf);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
/* fgets read *something* */
|
|
|
|
p = memchr(pvfree, '\n', nfree);
|
|
|
|
if (p != NULL) {
|
|
|
|
/* Did the \n come from fgets or from us?
|
|
|
|
* Since fgets stops at the first \n, and then writes
|
|
|
|
* \0, if it's from fgets a \0 must be next. But if
|
|
|
|
* that's so, it could not have come from us, since
|
|
|
|
* the \n's we filled the buffer with have only more
|
|
|
|
* \n's to the right.
|
2001-01-07 17:19:34 -04:00
|
|
|
*/
|
2001-01-15 06:36:56 -04:00
|
|
|
if (p+1 < pvend && *(p+1) == '\0') {
|
|
|
|
/* It's from fgets: we win! In particular,
|
|
|
|
* we haven't done any mallocs yet, and can
|
|
|
|
* build the final result on the first try.
|
|
|
|
*/
|
|
|
|
++p; /* include \n from fgets */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Must be from us: fgets didn't fill the
|
|
|
|
* buffer and didn't find a newline, so it
|
|
|
|
* must be the last and newline-free line of
|
|
|
|
* the file.
|
|
|
|
*/
|
|
|
|
assert(p > pvfree && *(p-1) == '\0');
|
|
|
|
--p; /* don't include \0 from fgets */
|
|
|
|
}
|
|
|
|
v = PyString_FromStringAndSize(buf, p - buf);
|
2001-01-07 17:19:34 -04:00
|
|
|
return v;
|
|
|
|
}
|
2001-01-15 06:36:56 -04:00
|
|
|
/* yuck: fgets overwrote all the newlines, i.e. the entire
|
|
|
|
* buffer. So this line isn't over yet, or maybe it is but
|
|
|
|
* we're exactly at EOF. If we haven't already, try using the
|
|
|
|
* rest of the stack buffer.
|
2001-01-07 17:19:34 -04:00
|
|
|
*/
|
2001-01-15 06:36:56 -04:00
|
|
|
assert(*(pvend-1) == '\0');
|
|
|
|
if (pvfree == buf) {
|
|
|
|
pvfree = pvend - 1; /* overwrite trailing null */
|
|
|
|
total_v_size = MAXBUFSIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2001-01-07 17:19:34 -04:00
|
|
|
}
|
2001-01-15 06:36:56 -04:00
|
|
|
|
|
|
|
/* The stack buffer isn't big enough; malloc a string object and read
|
|
|
|
* into its buffer.
|
2001-01-07 20:53:12 -04:00
|
|
|
*/
|
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
When WITH_PYMALLOC is defined, define PYMALLOC_DEBUG to enable the debug
allocator. This can be done independent of build type (release or debug).
A debug build automatically defines PYMALLOC_DEBUG when pymalloc is
enabled. It's a detected error to define PYMALLOC_DEBUG when pymalloc
isn't enabled.
Two debugging entry points defined only under PYMALLOC_DEBUG:
+ _PyMalloc_DebugCheckAddress(const void *p) can be used (e.g., from gdb)
to sanity-check a memory block obtained from pymalloc. It sprays
info to stderr (see next) and dies via Py_FatalError if the block is
detectably damaged.
+ _PyMalloc_DebugDumpAddress(const void *p) can be used to spray info
about a debug memory block to stderr.
A tiny start at implementing "API family" checks isn't good for
anything yet.
_PyMalloc_DebugRealloc() has been optimized to do little when the new
size is <= old size. However, if the new size is larger, it really
can't call the underlying realloc() routine without either violating its
contract, or knowing something non-trivial about how the underlying
realloc() works. A memcpy is always done in this case.
This was a disaster for (and only) one of the std tests: test_bufio
creates single text file lines up to a million characters long. On
Windows, fileobject.c's get_line() uses the horridly funky
getline_via_fgets(), which keeps growing and growing a string object
hoping to find a newline. It grew the string object 1000 bytes each
time, so for a million-character string it took approximately forever
(I gave up after a few minutes).
So, also:
fileobject.c, getline_via_fgets(): When a single line is outrageously
long, grow the string object at a mildly exponential rate, instead of
just 1000 bytes at a time.
That's enough so that a debug-build test_bufio finishes in about 5 seconds
on my Win98SE box. I'm curious to try this on Win2K, because it has very
different memory behavior than Win9X, and test_bufio always took a factor
of 10 longer to complete on Win2K. It *could* be that the endless
reallocs were simply killing it on Win2K even in the release build.
2002-03-23 06:03:50 -04:00
|
|
|
total_v_size = MAXBUFSIZE << 1;
|
2001-01-08 00:02:07 -04:00
|
|
|
v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
|
2001-01-07 20:53:12 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return v;
|
|
|
|
/* copy over everything except the last null byte */
|
2001-01-15 06:36:56 -04:00
|
|
|
memcpy(BUF(v), buf, MAXBUFSIZE-1);
|
|
|
|
pvfree = BUF(v) + MAXBUFSIZE - 1;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
|
|
|
/* Keep reading stuff into v; if it ever ends successfully, break
|
2001-01-07 20:53:12 -04:00
|
|
|
* after setting p one beyond the end of the line. The code here is
|
|
|
|
* very much like the code above, except reads into v's buffer; see
|
|
|
|
* the code above for detailed comments about the logic.
|
2001-01-07 17:19:34 -04:00
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
pvend = BUF(v) + total_v_size;
|
|
|
|
nfree = pvend - pvfree;
|
|
|
|
memset(pvfree, '\n', nfree);
|
|
|
|
p = fgets(pvfree, nfree, fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
clearerr(fp);
|
|
|
|
if (PyErr_CheckSignals()) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = pvfree;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = memchr(pvfree, '\n', nfree);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (p+1 < pvend && *(p+1) == '\0') {
|
|
|
|
/* \n came from fgets */
|
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* \n came from us; last line of file, no newline */
|
|
|
|
assert(p > pvfree && *(p-1) == '\0');
|
|
|
|
--p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* expand buffer and try again */
|
|
|
|
assert(*(pvend-1) == '\0');
|
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
When WITH_PYMALLOC is defined, define PYMALLOC_DEBUG to enable the debug
allocator. This can be done independent of build type (release or debug).
A debug build automatically defines PYMALLOC_DEBUG when pymalloc is
enabled. It's a detected error to define PYMALLOC_DEBUG when pymalloc
isn't enabled.
Two debugging entry points defined only under PYMALLOC_DEBUG:
+ _PyMalloc_DebugCheckAddress(const void *p) can be used (e.g., from gdb)
to sanity-check a memory block obtained from pymalloc. It sprays
info to stderr (see next) and dies via Py_FatalError if the block is
detectably damaged.
+ _PyMalloc_DebugDumpAddress(const void *p) can be used to spray info
about a debug memory block to stderr.
A tiny start at implementing "API family" checks isn't good for
anything yet.
_PyMalloc_DebugRealloc() has been optimized to do little when the new
size is <= old size. However, if the new size is larger, it really
can't call the underlying realloc() routine without either violating its
contract, or knowing something non-trivial about how the underlying
realloc() works. A memcpy is always done in this case.
This was a disaster for (and only) one of the std tests: test_bufio
creates single text file lines up to a million characters long. On
Windows, fileobject.c's get_line() uses the horridly funky
getline_via_fgets(), which keeps growing and growing a string object
hoping to find a newline. It grew the string object 1000 bytes each
time, so for a million-character string it took approximately forever
(I gave up after a few minutes).
So, also:
fileobject.c, getline_via_fgets(): When a single line is outrageously
long, grow the string object at a mildly exponential rate, instead of
just 1000 bytes at a time.
That's enough so that a debug-build test_bufio finishes in about 5 seconds
on my Win98SE box. I'm curious to try this on Win2K, because it has very
different memory behavior than Win9X, and test_bufio always took a factor
of 10 longer to complete on Win2K. It *could* be that the endless
reallocs were simply killing it on Win2K even in the release build.
2002-03-23 06:03:50 -04:00
|
|
|
increment = total_v_size >> 2; /* mild exponential growth */
|
|
|
|
total_v_size += increment;
|
2001-01-07 17:19:34 -04:00
|
|
|
if (total_v_size > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"line is longer than a Python string can hold");
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (_PyString_Resize(&v, (int)total_v_size) < 0)
|
|
|
|
return NULL;
|
|
|
|
/* overwrite the trailing null byte */
|
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
When WITH_PYMALLOC is defined, define PYMALLOC_DEBUG to enable the debug
allocator. This can be done independent of build type (release or debug).
A debug build automatically defines PYMALLOC_DEBUG when pymalloc is
enabled. It's a detected error to define PYMALLOC_DEBUG when pymalloc
isn't enabled.
Two debugging entry points defined only under PYMALLOC_DEBUG:
+ _PyMalloc_DebugCheckAddress(const void *p) can be used (e.g., from gdb)
to sanity-check a memory block obtained from pymalloc. It sprays
info to stderr (see next) and dies via Py_FatalError if the block is
detectably damaged.
+ _PyMalloc_DebugDumpAddress(const void *p) can be used to spray info
about a debug memory block to stderr.
A tiny start at implementing "API family" checks isn't good for
anything yet.
_PyMalloc_DebugRealloc() has been optimized to do little when the new
size is <= old size. However, if the new size is larger, it really
can't call the underlying realloc() routine without either violating its
contract, or knowing something non-trivial about how the underlying
realloc() works. A memcpy is always done in this case.
This was a disaster for (and only) one of the std tests: test_bufio
creates single text file lines up to a million characters long. On
Windows, fileobject.c's get_line() uses the horridly funky
getline_via_fgets(), which keeps growing and growing a string object
hoping to find a newline. It grew the string object 1000 bytes each
time, so for a million-character string it took approximately forever
(I gave up after a few minutes).
So, also:
fileobject.c, getline_via_fgets(): When a single line is outrageously
long, grow the string object at a mildly exponential rate, instead of
just 1000 bytes at a time.
That's enough so that a debug-build test_bufio finishes in about 5 seconds
on my Win98SE box. I'm curious to try this on Win2K, because it has very
different memory behavior than Win9X, and test_bufio always took a factor
of 10 longer to complete on Win2K. It *could* be that the endless
reallocs were simply killing it on Win2K even in the release build.
2002-03-23 06:03:50 -04:00
|
|
|
pvfree = BUF(v) + (total_v_size - increment - 1);
|
2001-01-07 17:19:34 -04:00
|
|
|
}
|
|
|
|
if (BUF(v) + total_v_size != p)
|
|
|
|
_PyString_Resize(&v, p - BUF(v));
|
|
|
|
return v;
|
|
|
|
#undef INITBUFSIZE
|
2001-01-15 06:36:56 -04:00
|
|
|
#undef MAXBUFSIZE
|
2001-01-07 17:19:34 -04:00
|
|
|
}
|
2001-01-15 02:33:19 -04:00
|
|
|
#endif /* ifdef USE_FGETS_IN_GETLINE */
|
1997-05-05 19:15:02 -03:00
|
|
|
|
1991-04-04 11:21:57 -04:00
|
|
|
/* Internal routine to get a line.
|
|
|
|
Size argument interpretation:
|
|
|
|
> 0: max length;
|
2001-01-07 21:26:47 -04:00
|
|
|
<= 0: read arbitrary line
|
1991-03-06 09:06:18 -04:00
|
|
|
*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
get_line(PyFileObject *f, int n)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2001-01-05 10:43:05 -04:00
|
|
|
FILE *fp = f->f_fp;
|
|
|
|
int c;
|
2000-11-28 22:53:22 -04:00
|
|
|
char *buf, *end;
|
2002-03-23 15:41:34 -04:00
|
|
|
size_t total_v_size; /* total # of slots in buffer */
|
|
|
|
size_t used_v_size; /* # used slots in buffer */
|
|
|
|
size_t increment; /* amount to increment the buffer */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
int newlinetypes = f->f_newlinetypes;
|
|
|
|
int skipnextlf = f->f_skipnextlf;
|
|
|
|
int univ_newline = f->f_univ_newline;
|
|
|
|
#endif
|
1991-04-04 11:21:57 -04:00
|
|
|
|
2002-04-14 17:12:41 -03:00
|
|
|
#if defined(USE_FGETS_IN_GETLINE)
|
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
if (n <= 0 && !univ_newline )
|
|
|
|
#else
|
2001-01-07 21:26:47 -04:00
|
|
|
if (n <= 0)
|
2002-04-14 17:12:41 -03:00
|
|
|
#endif
|
2001-01-15 02:33:19 -04:00
|
|
|
return getline_via_fgets(fp);
|
2001-01-07 17:19:34 -04:00
|
|
|
#endif
|
2002-03-23 15:41:34 -04:00
|
|
|
total_v_size = n > 0 ? n : 100;
|
|
|
|
v = PyString_FromStringAndSize((char *)NULL, total_v_size);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1991-03-06 09:06:18 -04:00
|
|
|
buf = BUF(v);
|
2002-03-23 15:41:34 -04:00
|
|
|
end = buf + total_v_size;
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1991-03-06 09:06:18 -04:00
|
|
|
for (;;) {
|
2001-01-05 10:43:05 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
FLOCKFILE(fp);
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
if (univ_newline) {
|
|
|
|
c = 'x'; /* Shut up gcc warning */
|
|
|
|
while ( buf != end && (c = GETC(fp)) != 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(fp);
|
|
|
|
if (c == EOF) break;
|
|
|
|
} else {
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
skipnextlf = 1;
|
|
|
|
c = '\n';
|
|
|
|
} else if ( c == '\n')
|
|
|
|
newlinetypes |= NEWLINE_LF;
|
|
|
|
*buf++ = c;
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
if ( c == EOF && skipnextlf )
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
} else /* If not universal newlines use the normal loop */
|
|
|
|
#endif
|
2001-01-05 10:43:05 -04:00
|
|
|
while ((c = GETC(fp)) != EOF &&
|
|
|
|
(*buf++ = c) != '\n' &&
|
|
|
|
buf != end)
|
|
|
|
;
|
|
|
|
FUNLOCKFILE(fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
f->f_newlinetypes = newlinetypes;
|
|
|
|
f->f_skipnextlf = skipnextlf;
|
|
|
|
#endif
|
2001-01-05 10:43:05 -04:00
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
if (c == EOF) {
|
2001-08-09 15:14:59 -03:00
|
|
|
if (ferror(fp)) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(fp);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
1991-06-03 07:54:55 -03:00
|
|
|
clearerr(fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_CheckSignals()) {
|
|
|
|
Py_DECREF(v);
|
1991-04-04 11:21:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-03-06 09:06:18 -04:00
|
|
|
break;
|
1991-04-04 11:21:57 -04:00
|
|
|
}
|
2001-01-05 10:43:05 -04:00
|
|
|
/* Must be because buf == end */
|
|
|
|
if (n > 0)
|
1991-04-04 11:21:57 -04:00
|
|
|
break;
|
2002-03-23 15:41:34 -04:00
|
|
|
used_v_size = total_v_size;
|
|
|
|
increment = total_v_size >> 2; /* mild exponential growth */
|
|
|
|
total_v_size += increment;
|
|
|
|
if (total_v_size > INT_MAX) {
|
2001-01-05 10:43:05 -04:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"line is longer than a Python string can hold");
|
2001-01-07 17:19:34 -04:00
|
|
|
Py_DECREF(v);
|
2001-01-05 10:43:05 -04:00
|
|
|
return NULL;
|
1991-04-04 11:21:57 -04:00
|
|
|
}
|
2002-03-23 15:41:34 -04:00
|
|
|
if (_PyString_Resize(&v, total_v_size) < 0)
|
2001-01-05 10:43:05 -04:00
|
|
|
return NULL;
|
2002-03-23 15:41:34 -04:00
|
|
|
buf = BUF(v) + used_v_size;
|
|
|
|
end = BUF(v) + total_v_size;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2002-03-23 15:41:34 -04:00
|
|
|
used_v_size = buf - BUF(v);
|
|
|
|
if (used_v_size != total_v_size)
|
|
|
|
_PyString_Resize(&v, used_v_size);
|
1990-10-14 09:07:46 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1991-04-04 11:21:57 -04:00
|
|
|
/* External C interface */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_GetLine(PyObject *f, int n)
|
1991-04-04 11:21:57 -04:00
|
|
|
{
|
2001-01-07 16:51:39 -04:00
|
|
|
PyObject *result;
|
|
|
|
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1991-04-04 11:21:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
|
|
|
|
if (PyFile_Check(f)) {
|
|
|
|
if (((PyFileObject*)f)->f_fp == NULL)
|
|
|
|
return err_closed();
|
|
|
|
result = get_line((PyFileObject *)f, n);
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *reader;
|
|
|
|
PyObject *args;
|
2001-01-07 16:51:39 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
reader = PyObject_GetAttrString(f, "readline");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (reader == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (n <= 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("()");
|
1992-09-25 18:59:05 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(i)", n);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(reader);
|
1992-09-25 18:59:05 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
result = PyEval_CallObject(reader, args);
|
|
|
|
Py_DECREF(reader);
|
|
|
|
Py_DECREF(args);
|
|
|
|
if (result != NULL && !PyString_Check(result)) {
|
|
|
|
Py_DECREF(result);
|
1992-09-25 18:59:05 -03:00
|
|
|
result = NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-25 18:59:05 -03:00
|
|
|
"object.readline() returned non-string");
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n < 0 && result != NULL && PyString_Check(result)) {
|
|
|
|
char *s = PyString_AS_STRING(result);
|
|
|
|
int len = PyString_GET_SIZE(result);
|
|
|
|
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 00:12:38 -03:00
|
|
|
Py_DECREF(result);
|
2001-01-07 16:51:39 -04:00
|
|
|
result = v;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
return result;
|
1991-04-04 11:21:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Python method */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_readline(PyFileObject *f, PyObject *args)
|
1991-04-04 11:21:57 -04:00
|
|
|
{
|
1997-05-10 19:33:55 -03:00
|
|
|
int n = -1;
|
1991-04-04 11:21:57 -04:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|i:readline", &n))
|
1997-05-10 19:33:55 -03:00
|
|
|
return NULL;
|
|
|
|
if (n == 0)
|
|
|
|
return PyString_FromString("");
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
2000-07-05 12:32:40 -03:00
|
|
|
return get_line(f, n);
|
1991-04-04 11:21:57 -04:00
|
|
|
}
|
|
|
|
|
2001-01-09 17:50:24 -04:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
file_xreadlines(PyFileObject *f)
|
2001-01-09 17:50:24 -04:00
|
|
|
{
|
|
|
|
static PyObject* xreadlines_function = NULL;
|
2001-01-15 02:33:19 -04:00
|
|
|
|
2002-01-01 15:07:13 -04:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2001-01-09 17:50:24 -04:00
|
|
|
if (!xreadlines_function) {
|
|
|
|
PyObject *xreadlines_module =
|
|
|
|
PyImport_ImportModule("xreadlines");
|
|
|
|
if(!xreadlines_module)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
xreadlines_function = PyObject_GetAttrString(xreadlines_module,
|
|
|
|
"xreadlines");
|
|
|
|
Py_DECREF(xreadlines_module);
|
|
|
|
if(!xreadlines_function)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_CallFunction(xreadlines_function, "(O)", f);
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_readlines(PyFileObject *f, PyObject *args)
|
1991-03-06 09:06:18 -04:00
|
|
|
{
|
1997-05-10 19:33:55 -03:00
|
|
|
long sizehint = 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *list;
|
|
|
|
PyObject *line;
|
1997-05-10 19:07:25 -03: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 19:33:55 -03:00
|
|
|
size_t totalread = 0;
|
1997-05-10 19:07:25 -03:00
|
|
|
char *p, *q, *end;
|
|
|
|
int err;
|
2001-10-12 17:01:53 -03:00
|
|
|
int shortread = 0;
|
1991-04-04 11:21:57 -04:00
|
|
|
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
|
1991-04-04 11:21:57 -04:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((list = PyList_New(0)) == NULL)
|
1991-03-06 09:06:18 -04:00
|
|
|
return NULL;
|
|
|
|
for (;;) {
|
2001-10-12 17:01:53 -03:00
|
|
|
if (shortread)
|
|
|
|
nread = 0;
|
|
|
|
else {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
2002-04-21 04:29:14 -03:00
|
|
|
nread = Py_UniversalNewlineFread(buffer+nfilled,
|
2002-04-14 17:12:41 -03:00
|
|
|
buffersize-nfilled, f->f_fp, (PyObject *)f);
|
2001-10-12 17:01:53 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
shortread = (nread < buffersize-nfilled);
|
|
|
|
}
|
1997-05-10 19:07:25 -03:00
|
|
|
if (nread == 0) {
|
1997-05-10 19:33:55 -03:00
|
|
|
sizehint = 0;
|
1998-02-19 16:46:48 -04:00
|
|
|
if (!ferror(f->f_fp))
|
1997-05-10 19:07:25 -03:00
|
|
|
break;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
error:
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(list);
|
1997-05-10 19:07:25 -03:00
|
|
|
list = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
1997-05-10 19:33:55 -03:00
|
|
|
totalread += nread;
|
1997-05-10 19:07:25 -03:00
|
|
|
p = memchr(buffer+nfilled, '\n', nread);
|
|
|
|
if (p == NULL) {
|
|
|
|
/* Need a larger buffer to fit this line */
|
|
|
|
nfilled += nread;
|
|
|
|
buffersize *= 2;
|
2000-08-11 16:02:59 -03:00
|
|
|
if (buffersize > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
2001-01-09 17:50:24 -04:00
|
|
|
"line is longer than a Python string can hold");
|
2000-08-11 16:02:59 -03:00
|
|
|
goto error;
|
|
|
|
}
|
1997-05-10 19:07:25 -03:00
|
|
|
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 */
|
2002-04-14 17:12:41 -03:00
|
|
|
if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
|
|
|
|
goto error;
|
1997-05-10 19:07:25 -03:00
|
|
|
buffer = PyString_AS_STRING(big_buffer);
|
|
|
|
}
|
|
|
|
continue;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
1997-05-10 19:07:25 -03: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 19:33:55 -03:00
|
|
|
if (sizehint > 0)
|
|
|
|
if (totalread >= (size_t)sizehint)
|
|
|
|
break;
|
1997-05-10 19:07:25 -03:00
|
|
|
}
|
|
|
|
if (nfilled != 0) {
|
|
|
|
/* Partial last line */
|
|
|
|
line = PyString_FromStringAndSize(buffer, nfilled);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
1997-05-10 19:33:55 -03:00
|
|
|
if (sizehint > 0) {
|
|
|
|
/* Need to complete the last line */
|
2000-07-05 12:32:40 -03:00
|
|
|
PyObject *rest = get_line(f, 0);
|
1997-05-10 19:33:55 -03:00
|
|
|
if (rest == NULL) {
|
|
|
|
Py_DECREF(line);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyString_Concat(&line, rest);
|
|
|
|
Py_DECREF(rest);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
1997-05-10 19:07:25 -03:00
|
|
|
err = PyList_Append(list, line);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(line);
|
1997-05-10 19:07:25 -03:00
|
|
|
if (err != 0)
|
|
|
|
goto error;
|
1991-03-06 09:06:18 -04:00
|
|
|
}
|
1997-05-10 19:07:25 -03:00
|
|
|
cleanup:
|
2002-04-27 15:44:32 -03:00
|
|
|
Py_XDECREF(big_buffer);
|
1991-03-06 09:06:18 -04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
file_write(PyFileObject *f, PyObject *args)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-07-06 11:19:26 -03:00
|
|
|
char *s;
|
1990-10-14 09:07:46 -03:00
|
|
|
int n, n2;
|
1992-07-06 11:19:26 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2001-10-31 14:51:01 -04:00
|
|
|
if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1991-04-04 06:44:06 -04:00
|
|
|
f->f_softspace = 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1990-10-14 09:07:46 -03:00
|
|
|
errno = 0;
|
1992-07-06 11:19:26 -03:00
|
|
|
n2 = fwrite(s, 1, n, f->f_fp);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1990-10-14 09:07:46 -03:00
|
|
|
if (n2 != n) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
1992-03-04 12:39:24 -04:00
|
|
|
clearerr(f->f_fp);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-09-23 01:06:05 -03:00
|
|
|
file_writelines(PyFileObject *f, PyObject *seq)
|
1993-10-25 06:59:04 -03:00
|
|
|
{
|
2000-03-13 12:27:06 -04:00
|
|
|
#define CHUNKSIZE 1000
|
|
|
|
PyObject *list, *line;
|
2001-09-23 01:06:05 -03:00
|
|
|
PyObject *it; /* iter(seq) */
|
2000-03-13 12:27:06 -04:00
|
|
|
PyObject *result;
|
|
|
|
int i, j, index, len, nwritten, islist;
|
|
|
|
|
2001-09-23 01:06:05 -03:00
|
|
|
assert(seq != NULL);
|
1993-10-25 06:59:04 -03:00
|
|
|
if (f->f_fp == NULL)
|
|
|
|
return err_closed();
|
2000-03-13 12:27:06 -04:00
|
|
|
|
2001-09-23 01:06:05 -03:00
|
|
|
result = NULL;
|
|
|
|
list = NULL;
|
|
|
|
islist = PyList_Check(seq);
|
|
|
|
if (islist)
|
|
|
|
it = NULL;
|
2000-03-13 12:27:06 -04:00
|
|
|
else {
|
2001-09-23 01:06:05 -03:00
|
|
|
it = PyObject_GetIter(seq);
|
|
|
|
if (it == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"writelines() requires an iterable argument");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* From here on, fail by going to error, to reclaim "it". */
|
2000-03-13 12:27:06 -04:00
|
|
|
list = PyList_New(CHUNKSIZE);
|
|
|
|
if (list == NULL)
|
2001-09-23 01:06:05 -03:00
|
|
|
goto error;
|
2000-03-13 12:27:06 -04:00
|
|
|
}
|
|
|
|
|
2001-09-23 01:06:05 -03:00
|
|
|
/* Strategy: slurp CHUNKSIZE lines into a private list,
|
|
|
|
checking that they are all strings, then write that list
|
|
|
|
without holding the interpreter lock, then come back for more. */
|
|
|
|
for (index = 0; ; index += CHUNKSIZE) {
|
2000-03-13 12:27:06 -04:00
|
|
|
if (islist) {
|
|
|
|
Py_XDECREF(list);
|
2001-09-23 01:06:05 -03:00
|
|
|
list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
|
2000-03-13 12:27:06 -04:00
|
|
|
if (list == NULL)
|
2001-09-23 01:06:05 -03:00
|
|
|
goto error;
|
2000-03-13 12:27:06 -04:00
|
|
|
j = PyList_GET_SIZE(list);
|
1993-10-25 06:59:04 -03:00
|
|
|
}
|
2000-03-13 12:27:06 -04:00
|
|
|
else {
|
|
|
|
for (j = 0; j < CHUNKSIZE; j++) {
|
2001-09-23 01:06:05 -03:00
|
|
|
line = PyIter_Next(it);
|
2000-03-13 12:27:06 -04:00
|
|
|
if (line == NULL) {
|
2001-09-23 01:06:05 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto error;
|
|
|
|
break;
|
2000-03-13 12:27:06 -04:00
|
|
|
}
|
|
|
|
PyList_SetItem(list, j, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == 0)
|
|
|
|
break;
|
|
|
|
|
2000-08-25 19:39:50 -03:00
|
|
|
/* Check that all entries are indeed strings. If not,
|
|
|
|
apply the same rules as for file.write() and
|
|
|
|
convert the results to strings. This is slow, but
|
|
|
|
seems to be the only way since all conversion APIs
|
|
|
|
could potentially execute Python code. */
|
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
PyObject *v = PyList_GET_ITEM(list, i);
|
|
|
|
if (!PyString_Check(v)) {
|
|
|
|
const char *buffer;
|
|
|
|
int len;
|
2001-01-07 17:19:34 -04:00
|
|
|
if (((f->f_binary &&
|
2000-08-25 19:39:50 -03:00
|
|
|
PyObject_AsReadBuffer(v,
|
|
|
|
(const void**)&buffer,
|
|
|
|
&len)) ||
|
|
|
|
PyObject_AsCharBuffer(v,
|
|
|
|
&buffer,
|
|
|
|
&len))) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2000-10-24 16:57:45 -03:00
|
|
|
"writelines() argument must be a sequence of strings");
|
2000-08-25 19:39:50 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
line = PyString_FromStringAndSize(buffer,
|
|
|
|
len);
|
|
|
|
if (line == NULL)
|
|
|
|
goto error;
|
|
|
|
Py_DECREF(v);
|
2000-08-25 19:49:05 -03:00
|
|
|
PyList_SET_ITEM(list, i, line);
|
2000-08-25 19:39:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since we are releasing the global lock, the
|
|
|
|
following code may *not* execute Python code. */
|
2000-03-13 12:27:06 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
f->f_softspace = 0;
|
|
|
|
errno = 0;
|
|
|
|
for (i = 0; i < j; i++) {
|
2000-08-25 19:39:50 -03:00
|
|
|
line = PyList_GET_ITEM(list, i);
|
2000-03-13 12:27:06 -04:00
|
|
|
len = PyString_GET_SIZE(line);
|
|
|
|
nwritten = fwrite(PyString_AS_STRING(line),
|
|
|
|
1, len, f->f_fp);
|
|
|
|
if (nwritten != len) {
|
|
|
|
Py_BLOCK_THREADS
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
clearerr(f->f_fp);
|
|
|
|
goto error;
|
|
|
|
}
|
1993-10-25 06:59:04 -03:00
|
|
|
}
|
2000-03-13 12:27:06 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (j < CHUNKSIZE)
|
|
|
|
break;
|
1993-10-25 06:59:04 -03:00
|
|
|
}
|
2000-03-13 12:27:06 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
2000-03-13 12:27:06 -04:00
|
|
|
result = Py_None;
|
|
|
|
error:
|
|
|
|
Py_XDECREF(list);
|
2001-09-23 01:06:05 -03:00
|
|
|
Py_XDECREF(it);
|
2000-03-13 12:27:06 -04:00
|
|
|
return result;
|
2001-09-23 01:06:05 -03:00
|
|
|
#undef CHUNKSIZE
|
1993-10-25 06:59:04 -03:00
|
|
|
}
|
|
|
|
|
2001-09-20 04:55:22 -03:00
|
|
|
static char readline_doc[] =
|
|
|
|
"readline([size]) -> next line from the file, as a string.\n"
|
|
|
|
"\n"
|
|
|
|
"Retain newline. A non-negative size argument limits the maximum\n"
|
|
|
|
"number of bytes to return (an incomplete line may be returned then).\n"
|
|
|
|
"Return an empty string at EOF.";
|
|
|
|
|
|
|
|
static char read_doc[] =
|
|
|
|
"read([size]) -> read at most size bytes, returned as a string.\n"
|
|
|
|
"\n"
|
|
|
|
"If the size argument is negative or omitted, read until EOF is reached.";
|
|
|
|
|
|
|
|
static char write_doc[] =
|
|
|
|
"write(str) -> None. Write string str to file.\n"
|
|
|
|
"\n"
|
|
|
|
"Note that due to buffering, flush() or close() may be needed before\n"
|
|
|
|
"the file on disk reflects the data written.";
|
|
|
|
|
|
|
|
static char fileno_doc[] =
|
|
|
|
"fileno() -> integer \"file descriptor\".\n"
|
|
|
|
"\n"
|
|
|
|
"This is needed for lower-level file interfaces, such os.read().";
|
|
|
|
|
|
|
|
static char seek_doc[] =
|
|
|
|
"seek(offset[, whence]) -> None. Move to new file position.\n"
|
|
|
|
"\n"
|
|
|
|
"Argument offset is a byte count. Optional argument whence defaults to\n"
|
|
|
|
"0 (offset from start of file, offset should be >= 0); other values are 1\n"
|
|
|
|
"(move relative to current position, positive or negative), and 2 (move\n"
|
|
|
|
"relative to end of file, usually negative, although many platforms allow\n"
|
|
|
|
"seeking beyond the end of a file).\n"
|
|
|
|
"\n"
|
|
|
|
"Note that not all file objects are seekable.";
|
|
|
|
|
|
|
|
#ifdef HAVE_FTRUNCATE
|
|
|
|
static char truncate_doc[] =
|
|
|
|
"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
|
|
|
|
"\n"
|
|
|
|
"Size defaults to the current file position, as returned by tell().";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char tell_doc[] =
|
|
|
|
"tell() -> current file position, an integer (may be a long integer).";
|
|
|
|
|
|
|
|
static char readinto_doc[] =
|
|
|
|
"readinto() -> Undocumented. Don't use this; it may go away.";
|
|
|
|
|
|
|
|
static char readlines_doc[] =
|
|
|
|
"readlines([size]) -> list of strings, each a line from the file.\n"
|
|
|
|
"\n"
|
|
|
|
"Call readline() repeatedly and return a list of the lines so read.\n"
|
|
|
|
"The optional size argument, if given, is an approximate bound on the\n"
|
|
|
|
"total number of bytes in the lines returned.";
|
|
|
|
|
|
|
|
static char xreadlines_doc[] =
|
|
|
|
"xreadlines() -> next line from the file, as a string.\n"
|
|
|
|
"\n"
|
|
|
|
"Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n"
|
|
|
|
"often quicker, due to reading ahead internally.";
|
|
|
|
|
|
|
|
static char writelines_doc[] =
|
2001-09-23 01:06:05 -03:00
|
|
|
"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
|
2001-09-20 04:55:22 -03:00
|
|
|
"\n"
|
2001-09-23 01:06:05 -03:00
|
|
|
"Note that newlines are not added. The sequence can be any iterable object\n"
|
|
|
|
"producing strings. This is equivalent to calling write() for each string.";
|
2001-09-20 04:55:22 -03:00
|
|
|
|
|
|
|
static char flush_doc[] =
|
|
|
|
"flush() -> None. Flush the internal I/O buffer.";
|
|
|
|
|
|
|
|
static char close_doc[] =
|
|
|
|
"close() -> None or (perhaps) an integer. Close the file.\n"
|
|
|
|
"\n"
|
2002-04-03 18:41:51 -04:00
|
|
|
"Sets data attribute .closed to True. A closed file cannot be used for\n"
|
2001-09-20 04:55:22 -03:00
|
|
|
"further I/O operations. close() may be called more than once without\n"
|
|
|
|
"error. Some kinds of file objects (for example, opened by popen())\n"
|
|
|
|
"may return an exit status upon closing.";
|
|
|
|
|
|
|
|
static char isatty_doc[] =
|
|
|
|
"isatty() -> true or false. True if the file is connected to a tty device.";
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMethodDef file_methods[] = {
|
2001-09-20 04:55:22 -03:00
|
|
|
{"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
|
|
|
|
{"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
|
2001-10-31 14:51:01 -04:00
|
|
|
{"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
|
2001-09-20 04:55:22 -03:00
|
|
|
{"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
|
|
|
|
{"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
|
1995-01-02 15:07:15 -04:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
2001-09-20 04:55:22 -03:00
|
|
|
{"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
|
1995-01-02 15:07:15 -04:00
|
|
|
#endif
|
2001-09-20 04:55:22 -03:00
|
|
|
{"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
|
2002-03-31 20:09:00 -04:00
|
|
|
{"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
|
2001-09-20 04:55:22 -03:00
|
|
|
{"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
|
|
|
|
{"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
|
|
|
|
{"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc},
|
|
|
|
{"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
|
|
|
|
{"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
|
|
|
|
{"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
|
1990-10-14 09:07:46 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define OFF(x) offsetof(PyFileObject, x)
|
1994-08-01 08:34:53 -03:00
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
static PyMemberDef file_memberlist[] = {
|
|
|
|
{"softspace", T_INT, OFF(f_softspace), 0,
|
|
|
|
"flag indicating that a space needs to be printed; used by print"},
|
|
|
|
{"mode", T_OBJECT, OFF(f_mode), RO,
|
|
|
|
"file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"},
|
|
|
|
{"name", T_OBJECT, OFF(f_name), RO,
|
|
|
|
"file name"},
|
1994-08-01 08:34:53 -03:00
|
|
|
/* getattr(f, "closed") is implemented without this table */
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2001-08-02 01:15:00 -03:00
|
|
|
get_closed(PyFileObject *f, void *closure)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2002-04-03 18:41:51 -04:00
|
|
|
return PyBool_FromLong((long)(f->f_fp == 0));
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
static PyObject *
|
|
|
|
get_newlines(PyFileObject *f, void *closure)
|
|
|
|
{
|
|
|
|
switch (f->f_newlinetypes) {
|
|
|
|
case NEWLINE_UNKNOWN:
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
case NEWLINE_CR:
|
|
|
|
return PyString_FromString("\r");
|
|
|
|
case NEWLINE_LF:
|
|
|
|
return PyString_FromString("\n");
|
|
|
|
case NEWLINE_CR|NEWLINE_LF:
|
|
|
|
return Py_BuildValue("(ss)", "\r", "\n");
|
|
|
|
case NEWLINE_CRLF:
|
|
|
|
return PyString_FromString("\r\n");
|
|
|
|
case NEWLINE_CR|NEWLINE_CRLF:
|
|
|
|
return Py_BuildValue("(ss)", "\r", "\r\n");
|
|
|
|
case NEWLINE_LF|NEWLINE_CRLF:
|
|
|
|
return Py_BuildValue("(ss)", "\n", "\r\n");
|
|
|
|
case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
|
|
|
|
return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
|
|
|
|
default:
|
|
|
|
PyErr_Format(PyExc_SystemError, "Unknown newlines value 0x%x\n", f->f_newlinetypes);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1994-08-01 08:34:53 -03:00
|
|
|
|
2001-09-20 18:45:26 -03:00
|
|
|
static PyGetSetDef file_getsetlist[] = {
|
2002-04-03 18:41:51 -04:00
|
|
|
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
{"newlines", (getter)get_newlines, NULL, "end-of-line convention used in this file"},
|
|
|
|
#endif
|
2001-08-02 01:15:00 -03:00
|
|
|
{0},
|
|
|
|
};
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2001-04-21 10:20:18 -03:00
|
|
|
static PyObject *
|
2001-05-22 13:48:37 -03:00
|
|
|
file_getiter(PyObject *f)
|
2001-04-21 10:20:18 -03:00
|
|
|
{
|
2001-05-22 13:48:37 -03:00
|
|
|
return PyObject_CallMethod(f, "xreadlines", "");
|
2001-04-21 10:20:18 -03:00
|
|
|
}
|
|
|
|
|
2001-09-13 02:38:56 -03:00
|
|
|
static PyObject *
|
|
|
|
file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2001-09-14 00:26:08 -03:00
|
|
|
PyObject *self;
|
|
|
|
static PyObject *not_yet_string;
|
|
|
|
|
|
|
|
assert(type != NULL && type->tp_alloc != NULL);
|
|
|
|
|
|
|
|
if (not_yet_string == NULL) {
|
|
|
|
not_yet_string = PyString_FromString("<uninitialized file>");
|
|
|
|
if (not_yet_string == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = type->tp_alloc(type, 0);
|
|
|
|
if (self != NULL) {
|
|
|
|
/* Always fill in the name and mode, so that nobody else
|
|
|
|
needs to special-case NULLs there. */
|
|
|
|
Py_INCREF(not_yet_string);
|
|
|
|
((PyFileObject *)self)->f_name = not_yet_string;
|
|
|
|
Py_INCREF(not_yet_string);
|
|
|
|
((PyFileObject *)self)->f_mode = not_yet_string;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyFileObject *foself = (PyFileObject *)self;
|
|
|
|
int ret = 0;
|
2001-09-13 02:38:56 -03:00
|
|
|
static char *kwlist[] = {"name", "mode", "buffering", 0};
|
|
|
|
char *name = NULL;
|
|
|
|
char *mode = "r";
|
|
|
|
int bufsize = -1;
|
2001-09-14 00:26:08 -03:00
|
|
|
|
|
|
|
assert(PyFile_Check(self));
|
|
|
|
if (foself->f_fp != NULL) {
|
|
|
|
/* Have to close the existing file first. */
|
|
|
|
PyObject *closeresult = file_close(foself);
|
|
|
|
if (closeresult == NULL)
|
|
|
|
return -1;
|
|
|
|
Py_DECREF(closeresult);
|
|
|
|
}
|
2001-09-13 02:38:56 -03:00
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
|
|
|
|
Py_FileSystemDefaultEncoding, &name,
|
|
|
|
&mode, &bufsize))
|
2001-09-14 00:26:08 -03:00
|
|
|
return -1;
|
|
|
|
if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL)
|
|
|
|
goto Error;
|
|
|
|
if (open_the_file(foself, name, mode) == NULL)
|
|
|
|
goto Error;
|
|
|
|
PyFile_SetBufSize(self, bufsize);
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
ret = -1;
|
|
|
|
/* fall through */
|
|
|
|
Done:
|
2001-09-13 02:38:56 -03:00
|
|
|
PyMem_Free(name); /* free the encoded string */
|
2001-09-14 00:26:08 -03:00
|
|
|
return ret;
|
2001-09-13 02:38:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char file_doc[] =
|
|
|
|
"file(name[, mode[, buffering]]) -> file object\n"
|
|
|
|
"\n"
|
|
|
|
"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
|
|
|
|
"writing or appending. The file will be created if it doesn't exist\n"
|
|
|
|
"when opened for writing or appending; it will be truncated when\n"
|
|
|
|
"opened for writing. Add a 'b' to the mode for binary files.\n"
|
|
|
|
"Add a '+' to the mode to allow simultaneous reading and writing.\n"
|
|
|
|
"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
|
2001-09-13 18:49:44 -03:00
|
|
|
"buffered, and larger numbers specify the buffer size.\n"
|
|
|
|
"Note: open() is an alias for file().\n";
|
2001-09-13 02:38:56 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyFile_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
"file",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyFileObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-04-21 10:20:18 -03:00
|
|
|
(destructor)file_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2001-04-21 10:20:18 -03:00
|
|
|
0, /* tp_compare */
|
2001-08-02 01:15:00 -03:00
|
|
|
(reprfunc)file_repr, /* tp_repr */
|
2001-04-21 10:20:18 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
2001-04-21 10:20:18 -03:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-10-05 17:51:39 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
2001-09-13 02:38:56 -03:00
|
|
|
file_doc, /* tp_doc */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
2001-04-21 10:20:18 -03:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
2001-05-22 13:48:37 -03:00
|
|
|
file_getiter, /* tp_iter */
|
2001-04-23 11:08:49 -03:00
|
|
|
0, /* tp_iternext */
|
2001-08-02 01:15:00 -03:00
|
|
|
file_methods, /* tp_methods */
|
|
|
|
file_memberlist, /* tp_members */
|
|
|
|
file_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
2001-09-13 02:38:56 -03:00
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
2001-09-14 00:26:08 -03:00
|
|
|
(initproc)file_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
2001-09-13 02:38:56 -03:00
|
|
|
file_new, /* tp_new */
|
2002-04-11 23:44:10 -03:00
|
|
|
PyObject_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1991-04-04 06:44:06 -04:00
|
|
|
|
|
|
|
/* Interface for the 'soft space' between print items. */
|
|
|
|
|
|
|
|
int
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_SoftSpace(PyObject *f, int newflag)
|
1991-04-04 06:44:06 -04:00
|
|
|
{
|
|
|
|
int oldflag = 0;
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL) {
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (PyFile_Check(f)) {
|
|
|
|
oldflag = ((PyFileObject *)f)->f_softspace;
|
|
|
|
((PyFileObject *)f)->f_softspace = newflag;
|
1991-04-04 06:44:06 -04:00
|
|
|
}
|
1992-09-25 18:59:05 -03:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
|
|
|
v = PyObject_GetAttrString(f, "softspace");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1992-09-25 18:59:05 -03:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInt_Check(v))
|
|
|
|
oldflag = PyInt_AsLong(v);
|
|
|
|
Py_DECREF(v);
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
v = PyInt_FromLong((long)newflag);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (v == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1992-09-25 18:59:05 -03:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyObject_SetAttrString(f, "softspace", v) != 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(v);
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
|
|
|
}
|
1991-04-04 06:44:06 -04:00
|
|
|
return oldflag;
|
|
|
|
}
|
1992-09-25 18:59:05 -03:00
|
|
|
|
|
|
|
/* Interfaces to write objects/strings to file-like objects */
|
|
|
|
|
|
|
|
int
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
|
1992-09-25 18:59:05 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *writer, *value, *args, *result;
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
|
1992-09-25 18:59:05 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (PyFile_Check(f)) {
|
|
|
|
FILE *fp = PyFile_AsFile(f);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (fp == NULL) {
|
|
|
|
err_closed();
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyObject_Print(v, fp, flags);
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
writer = PyObject_GetAttrString(f, "write");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (writer == NULL)
|
|
|
|
return -1;
|
2001-09-19 10:47:32 -03:00
|
|
|
if (flags & Py_PRINT_RAW) {
|
|
|
|
if (PyUnicode_Check(v)) {
|
|
|
|
value = v;
|
|
|
|
Py_INCREF(value);
|
|
|
|
} else
|
|
|
|
value = PyObject_Str(v);
|
|
|
|
}
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
value = PyObject_Repr(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
if (value == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(writer);
|
1993-11-05 06:22:19 -04:00
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(O)", value);
|
1997-05-22 11:02:25 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1995-07-10 20:32:26 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
result = PyEval_CallObject(writer, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (result == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(result);
|
1992-09-25 18:59:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-22 19:25:11 -03:00
|
|
|
int
|
2001-11-28 18:13:25 -04:00
|
|
|
PyFile_WriteString(const char *s, PyObject *f)
|
1992-09-25 18:59:05 -03:00
|
|
|
{
|
|
|
|
if (f == NULL) {
|
1997-05-22 19:25:11 -03:00
|
|
|
/* Should be caused by a pre-existing error */
|
2000-07-09 02:02:18 -03:00
|
|
|
if (!PyErr_Occurred())
|
1997-05-22 19:25:11 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"null file for PyFile_WriteString");
|
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (PyFile_Check(f)) {
|
|
|
|
FILE *fp = PyFile_AsFile(f);
|
1997-05-22 19:25:11 -03:00
|
|
|
if (fp == NULL) {
|
|
|
|
err_closed();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fputs(s, fp);
|
|
|
|
return 0;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (!PyErr_Occurred()) {
|
|
|
|
PyObject *v = PyString_FromString(s);
|
1997-05-22 19:25:11 -03:00
|
|
|
int err;
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return err;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-07-13 00:56:50 -03:00
|
|
|
else
|
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
2000-07-13 20:56:54 -03: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int PyObject_AsFileDescriptor(PyObject *o)
|
|
|
|
{
|
|
|
|
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 17:19:34 -04:00
|
|
|
|
2000-07-13 20:56:54 -03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"file descriptor cannot be a negative integer (%i)",
|
|
|
|
fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
|
|
|
|
#ifdef WITH_UNIVERSAL_NEWLINES
|
|
|
|
/* From here on we need access to the real fgets and fread */
|
|
|
|
#undef fgets
|
|
|
|
#undef fread
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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;
|
|
|
|
int univ_newline = 1;
|
2002-04-21 04:29:14 -03:00
|
|
|
|
2002-04-14 17:12:41 -03:00
|
|
|
if (fobj) {
|
|
|
|
if (!PyFile_Check(fobj)) {
|
|
|
|
errno = ENXIO; /* What can you do... */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
|
|
|
|
if ( !univ_newline )
|
|
|
|
return fgets(buf, n, stream);
|
|
|
|
newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
|
|
|
|
skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
|
|
|
|
}
|
|
|
|
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';
|
|
|
|
if (fobj) {
|
|
|
|
((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
|
|
|
|
((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
|
|
|
|
} else if ( skipnextlf ) {
|
|
|
|
/* 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
|
|
|
|
** execfile("/dev/tty").
|
|
|
|
*/
|
|
|
|
c = GETC(stream);
|
|
|
|
if ( c != '\n' )
|
|
|
|
ungetc(c, stream);
|
|
|
|
}
|
|
|
|
if (p == buf)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Py_UniversalNewlineFread is an fread variation that understands
|
|
|
|
** all of \r, \n and \r\n conventions.
|
|
|
|
** The stream should be opened in binary mode.
|
|
|
|
** fobj 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.
|
|
|
|
*/
|
|
|
|
size_t
|
2002-04-21 04:29:14 -03:00
|
|
|
Py_UniversalNewlineFread(char *buf, size_t n,
|
2002-04-14 17:12:41 -03:00
|
|
|
FILE *stream, PyObject *fobj)
|
|
|
|
{
|
2002-04-21 04:29:14 -03:00
|
|
|
char *dst = buf;
|
|
|
|
PyFileObject *f = (PyFileObject *)fobj;
|
|
|
|
int newlinetypes, skipnextlf;
|
|
|
|
|
|
|
|
assert(buf != NULL);
|
|
|
|
assert(stream != NULL);
|
|
|
|
|
2002-04-14 17:12:41 -03:00
|
|
|
if (!fobj || !PyFile_Check(fobj)) {
|
|
|
|
errno = ENXIO; /* What can you do... */
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-21 04:29:14 -03:00
|
|
|
if (!f->f_univ_newline)
|
2002-04-14 17:12:41 -03:00
|
|
|
return fread(buf, 1, n, stream);
|
2002-04-21 04:29:14 -03:00
|
|
|
newlinetypes = f->f_newlinetypes;
|
|
|
|
skipnextlf = f->f_skipnextlf;
|
|
|
|
/* Invariant: n is the number of bytes remaining to be filled
|
|
|
|
* in the buffer.
|
|
|
|
*/
|
|
|
|
while (n) {
|
|
|
|
size_t nread;
|
|
|
|
int shortread;
|
|
|
|
char *src = dst;
|
|
|
|
|
|
|
|
nread = fread(dst, 1, n, stream);
|
|
|
|
assert(nread <= n);
|
2002-04-21 15:15:20 -03:00
|
|
|
n -= nread; /* assuming 1 byte out for each in; will adjust */
|
|
|
|
shortread = n != 0; /* true iff EOF or error */
|
2002-04-21 04:29:14 -03:00
|
|
|
while (nread--) {
|
|
|
|
char c = *src++;
|
2002-04-14 17:12:41 -03:00
|
|
|
if (c == '\r') {
|
2002-04-21 04:29:14 -03:00
|
|
|
/* Save as LF and set flag to skip next LF. */
|
2002-04-14 17:12:41 -03:00
|
|
|
*dst++ = '\n';
|
|
|
|
skipnextlf = 1;
|
2002-04-21 04:29:14 -03:00
|
|
|
}
|
|
|
|
else if (skipnextlf && c == '\n') {
|
|
|
|
/* Skip LF, and remember we saw CR LF. */
|
2002-04-14 17:12:41 -03:00
|
|
|
skipnextlf = 0;
|
|
|
|
newlinetypes |= NEWLINE_CRLF;
|
2002-04-21 15:15:20 -03:00
|
|
|
++n;
|
2002-04-21 04:29:14 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Normal char to be stored in buffer. Also
|
|
|
|
* update the newlinetypes flag if either this
|
|
|
|
* is an LF or the previous char was a CR.
|
|
|
|
*/
|
2002-04-14 17:12:41 -03:00
|
|
|
if (c == '\n')
|
|
|
|
newlinetypes |= NEWLINE_LF;
|
|
|
|
else if (skipnextlf)
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
*dst++ = c;
|
|
|
|
skipnextlf = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-04-21 04:29:14 -03:00
|
|
|
if (shortread) {
|
|
|
|
/* If this is EOF, update type flags. */
|
|
|
|
if (skipnextlf && feof(stream))
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
break;
|
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
}
|
2002-04-21 04:29:14 -03:00
|
|
|
f->f_newlinetypes = newlinetypes;
|
|
|
|
f->f_skipnextlf = skipnextlf;
|
|
|
|
return dst - buf;
|
2002-04-14 17:12:41 -03:00
|
|
|
}
|
|
|
|
#endif
|