static PyObject *file_write(PyFileObject *f, PyObject *args){ char *s; Py_ssize_t n, n2; if (f->f_fp == NULL) return err_closed(); if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) return NULL; f->f_softspace = 0; Py_BEGIN_ALLOW_THREADS errno = 0; n2 = fwrite(s, 1, n, f->f_fp); Py_END_ALLOW_THREADS if (n2 != n) { PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } Py_INCREF(Py_None); return Py_None;} static PyObject *file_writelines(PyFileObject *f, PyObject *seq){#define CHUNKSIZE 1000 PyObject *list, *line; PyObject *it; /* iter(seq) */ PyObject *result; int index, islist; Py_ssize_t i, j, nwritten, len; assert(seq != NULL); if (f->f_fp == NULL) return err_closed(); result = NULL; list = NULL; islist = PyList_Check(seq); if (islist) it = NULL; else { 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". */ list = PyList_New(CHUNKSIZE); if (list == NULL) goto error; } /* 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) { if (islist) { Py_XDECREF(list); list = PyList_GetSlice(seq, index, index+CHUNKSIZE); if (list == NULL) goto error; j = PyList_GET_SIZE(list); } else { for (j = 0; j < CHUNKSIZE; j++) { line = PyIter_Next(it); if (line == NULL) { if (PyErr_Occurred()) goto error; break; } PyList_SetItem(list, j, line); } } if (j == 0) break; /* 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; if (((f->f_binary && PyObject_AsReadBuffer(v, (const void**)&buffer, &len)) || PyObject_AsCharBuffer(v, &buffer, &len))) { PyErr_SetString(PyExc_TypeError, "writelines() argument must be a sequence of strings"); goto error; } line = PyString_FromStringAndSize(buffer, len); if (line == NULL) goto error; Py_DECREF(v); PyList_SET_ITEM(list, i, line); } } /* Since we are releasing the global lock, the following code may *not* execute Python code. */ Py_BEGIN_ALLOW_THREADS f->f_softspace = 0; errno = 0; for (i = 0; i < j; i++) { line = PyList_GET_ITEM(list, i); 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; } } Py_END_ALLOW_THREADS if (j < CHUNKSIZE) break; } Py_INCREF(Py_None); result = Py_None; error: Py_XDECREF(list); Py_XDECREF(it); return result;#undef CHUNKSIZE} static PyObject *file_self(PyFileObject *f){ if (f->f_fp == NULL) return err_closed(); Py_INCREF(f); return (PyObject *)f;} static PyObject *file_exit(PyFileObject *f, PyObject *args){ PyObject *ret = file_close(f); if (!ret) /* If error occurred, pass through */ return NULL; Py_DECREF(ret); /* We cannot return the result of close since a true * value will be interpreted as "yes, swallow the * exception if one was raised inside the with block". */ Py_RETURN_NONE;} PyDoc_STRVAR(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."); PyDoc_STRVAR(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.\n""Notice that when in non-blocking mode, less data than what was requested\n""may be returned, even if no size parameter was given."); PyDoc_STRVAR(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."); PyDoc_STRVAR(fileno_doc,"fileno() -> integer \"file descriptor\".\n""\n""This is needed for lower-level file interfaces, such os.read()."); PyDoc_STRVAR(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). If the file is opened in text mode,\n""only offsets returned by tell() are legal. Use of other offsets causes\n""undefined behavior.""\n""Note that not all file objects are seekable."); #ifdef HAVE_FTRUNCATEPyDoc_STRVAR(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 PyDoc_STRVAR(tell_doc,"tell() -> current file position, an integer (may be a long integer)."); PyDoc_STRVAR(readinto_doc,"readinto() -> Undocumented. Don't use this; it may go away."); PyDoc_STRVAR(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."); PyDoc_STRVAR(xreadlines_doc,"xreadlines() -> returns self.\n""\n""For backward compatibility. File objects now include the performance\n""optimizations previously implemented in the xreadlines module."); PyDoc_STRVAR(writelines_doc,"writelines(sequence_of_strings) -> None. Write the strings to the file.\n""\n""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."); PyDoc_STRVAR(flush_doc,"flush() -> None. Flush the internal I/O buffer."); PyDoc_STRVAR(close_doc,"close() -> None or (perhaps) an integer. Close the file.\n""\n""Sets data attribute .closed to True. A closed file cannot be used for\n""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."); PyDoc_STRVAR(isatty_doc,"isatty() -> true or false. True if the file is connected to a tty device."); PyDoc_STRVAR(enter_doc, "__enter__() -> self."); PyDoc_STRVAR(exit_doc, "__exit__(*excinfo) -> None. Closes the file."); static PyMethodDef file_methods[] = { {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},#ifdef HAVE_FTRUNCATE {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},#endif {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc}, {"xreadlines",(PyCFunction)file_self, 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}, {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc}, {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc}, {NULL, NULL} /* sentinel */}; #define OFF(x) offsetof(PyFileObject, x) 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', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, {"name", T_OBJECT, OFF(f_name), RO, "file name"}, {"encoding", T_OBJECT, OFF(f_encoding), RO, "file encoding"}, /* getattr(f, "closed") is implemented without this table */ {NULL} /* Sentinel */}; static PyObject *get_closed(PyFileObject *f, void *closure){ return PyBool_FromLong((long)(f->f_fp == 0));}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; }} static PyGetSetDef file_getsetlist[] = { {"closed", (getter)get_closed, NULL, "True if the file is closed"}, {"newlines", (getter)get_newlines, NULL, "end-of-line convention used in this file"}, {0},}; static voiddrop_readahead(PyFileObject *f){ if (f->f_buf != NULL) { PyMem_Free(f->f_buf); f->f_buf = NULL; }} /* Make sure that file has a readahead buffer with at least one byte (unless at EOF) and no more than bufsize. Returns negative value on error, will set MemoryError if bufsize bytes cannot be allocated. */static intreadahead(PyFileObject *f, int bufsize){ Py_ssize_t chunksize; if (f->f_buf != NULL) { if( (f->f_bufend - f->f_bufptr) >= 1) return 0; else drop_readahead(f); } if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) { PyErr_NoMemory(); return -1; } Py_BEGIN_ALLOW_THREADS errno = 0; chunksize = Py_UniversalNewlineFread( f->f_buf, bufsize, f->f_fp, (PyObject *)f); Py_END_ALLOW_THREADS if (chunksize == 0) { if (ferror(f->f_fp)) { PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); drop_readahead(f); return -1; } } f->f_bufptr = f->f_buf; f->f_bufend = f->f_buf + chunksize; return 0;} /* Used by file_iternext. The returned string will start with 'skip' uninitialized bytes followed by the remainder of the line. Don't be horrified by the recursive call: maximum recursion depth is limited by logarithmic buffer growth to about 50 even when reading a 1gb line. */ static PyStringObject *readahead_get_line_skip(PyFileObject *f, int skip, int bufsize){ PyStringObject* s; char *bufptr; char *buf; Py_ssize_t len; if (f->f_buf == NULL) if (readahead(f, bufsize) < 0) return NULL; len = f->f_bufend - f->f_bufptr; if (len == 0) return (PyStringObject *) PyString_FromStringAndSize(NULL, skip); bufptr = (char *)memchr(f->f_bufptr, '\n', len); if (bufptr != NULL) { bufptr++; /* Count the '\n' */ len = bufptr - f->f_bufptr; s = (PyStringObject *) PyString_FromStringAndSize(NULL, skip+len); if (s == NULL) return NULL; memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); f->f_bufptr = bufptr; if (bufptr == f->f_bufend) drop_readahead(f); } else { bufptr = f->f_bufptr; buf = f->f_buf; f->f_buf = NULL; /* Force new readahead buffer */ assert(skip+len < INT_MAX); s = readahead_get_line_skip( f, (int)(skip+len), bufsize + (bufsize>>2) ); if (s == NULL) { PyMem_Free(buf); return NULL; } memcpy(PyString_AS_STRING(s)+skip, bufptr, len); PyMem_Free(buf); } return s;} /* A larger buffer size may actually decrease performance. */#define READAHEAD_BUFSIZE 8192 static PyObject *file_iternext(PyFileObject *f){ PyStringObject* l; if (f->f_fp == NULL) return err_closed(); l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); if (l == NULL || PyString_GET_SIZE(l) == 0) { Py_XDECREF(l); return NULL; } return (PyObject *)l;} static PyObject *file_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ 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; Py_INCREF(Py_None); ((PyFileObject *)self)->f_encoding = Py_None; ((PyFileObject *)self)->weakreflist = NULL; } return self;} static intfile_init(PyObject *self, PyObject *args, PyObject *kwds){ PyFileObject *foself = (PyFileObject *)self; int ret = 0; static char *kwlist[] = {"name", "mode", "buffering", 0}; char *name = NULL; char *mode = "r"; int bufsize = -1; int wideargument = 0; 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); } #ifdef Py_WIN_WIDE_FILENAMES if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ PyObject *po;Lines 1501–2000 of 2487. Previous lines Load more
static PyObject * file_write(PyFileObject *f, PyObject *args) { char *s; Py_ssize_t n, n2; if (f->f_fp == NULL) return err_closed(); if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) return NULL; f->f_softspace = 0; Py_BEGIN_ALLOW_THREADS errno = 0; n2 = fwrite(s, 1, n, f->f_fp); Py_END_ALLOW_THREADS if (n2 != n) { PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); return NULL; } Py_INCREF(Py_None); return Py_None; } static PyObject * file_writelines(PyFileObject *f, PyObject *seq) { #define CHUNKSIZE 1000 PyObject *list, *line; PyObject *it; /* iter(seq) */ PyObject *result; int index, islist; Py_ssize_t i, j, nwritten, len; assert(seq != NULL); if (f->f_fp == NULL) return err_closed(); result = NULL; list = NULL; islist = PyList_Check(seq); if (islist) it = NULL; else { 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". */ list = PyList_New(CHUNKSIZE); if (list == NULL) goto error; } /* 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) { if (islist) { Py_XDECREF(list); list = PyList_GetSlice(seq, index, index+CHUNKSIZE); if (list == NULL) goto error; j = PyList_GET_SIZE(list); } else { for (j = 0; j < CHUNKSIZE; j++) { line = PyIter_Next(it); if (line == NULL) { if (PyErr_Occurred()) goto error; break; } PyList_SetItem(list, j, line); } } if (j == 0) break; /* 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; if (((f->f_binary && PyObject_AsReadBuffer(v, (const void**)&buffer, &len)) || PyObject_AsCharBuffer(v, &buffer, &len))) { PyErr_SetString(PyExc_TypeError, "writelines() argument must be a sequence of strings"); goto error; } line = PyString_FromStringAndSize(buffer, len); if (line == NULL) goto error; Py_DECREF(v); PyList_SET_ITEM(list, i, line); } } /* Since we are releasing the global lock, the following code may *not* execute Python code. */ Py_BEGIN_ALLOW_THREADS f->f_softspace = 0; errno = 0; for (i = 0; i < j; i++) { line = PyList_GET_ITEM(list, i); 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; } } Py_END_ALLOW_THREADS if (j < CHUNKSIZE) break; } Py_INCREF(Py_None); result = Py_None; error: Py_XDECREF(list); Py_XDECREF(it); return result; #undef CHUNKSIZE } static PyObject * file_self(PyFileObject *f) { if (f->f_fp == NULL) return err_closed(); Py_INCREF(f); return (PyObject *)f; } static PyObject * file_exit(PyFileObject *f, PyObject *args) { PyObject *ret = file_close(f); if (!ret) /* If error occurred, pass through */ return NULL; Py_DECREF(ret); /* We cannot return the result of close since a true * value will be interpreted as "yes, swallow the * exception if one was raised inside the with block". */ Py_RETURN_NONE; } PyDoc_STRVAR(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."); PyDoc_STRVAR(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.\n" "Notice that when in non-blocking mode, less data than what was requested\n" "may be returned, even if no size parameter was given."); PyDoc_STRVAR(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."); PyDoc_STRVAR(fileno_doc, "fileno() -> integer \"file descriptor\".\n" "\n" "This is needed for lower-level file interfaces, such os.read()."); PyDoc_STRVAR(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). If the file is opened in text mode,\n" "only offsets returned by tell() are legal. Use of other offsets causes\n" "undefined behavior." "\n" "Note that not all file objects are seekable."); #ifdef HAVE_FTRUNCATE PyDoc_STRVAR(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 PyDoc_STRVAR(tell_doc, "tell() -> current file position, an integer (may be a long integer)."); PyDoc_STRVAR(readinto_doc, "readinto() -> Undocumented. Don't use this; it may go away."); PyDoc_STRVAR(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."); PyDoc_STRVAR(xreadlines_doc, "xreadlines() -> returns self.\n" "\n" "For backward compatibility. File objects now include the performance\n" "optimizations previously implemented in the xreadlines module."); PyDoc_STRVAR(writelines_doc, "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" "\n" "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."); PyDoc_STRVAR(flush_doc, "flush() -> None. Flush the internal I/O buffer."); PyDoc_STRVAR(close_doc, "close() -> None or (perhaps) an integer. Close the file.\n" "\n" "Sets data attribute .closed to True. A closed file cannot be used for\n" "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."); PyDoc_STRVAR(isatty_doc, "isatty() -> true or false. True if the file is connected to a tty device."); PyDoc_STRVAR(enter_doc, "__enter__() -> self."); PyDoc_STRVAR(exit_doc, "__exit__(*excinfo) -> None. Closes the file."); static PyMethodDef file_methods[] = { {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, #ifdef HAVE_FTRUNCATE {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, #endif {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc}, {"xreadlines",(PyCFunction)file_self, 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}, {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc}, {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc}, {NULL, NULL} /* sentinel */ }; #define OFF(x) offsetof(PyFileObject, x) 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', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, {"name", T_OBJECT, OFF(f_name), RO, "file name"}, {"encoding", T_OBJECT, OFF(f_encoding), RO, "file encoding"}, /* getattr(f, "closed") is implemented without this table */ {NULL} /* Sentinel */ }; static PyObject * get_closed(PyFileObject *f, void *closure) { return PyBool_FromLong((long)(f->f_fp == 0)); } 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; } } static PyGetSetDef file_getsetlist[] = { {"closed", (getter)get_closed, NULL, "True if the file is closed"}, {"newlines", (getter)get_newlines, NULL, "end-of-line convention used in this file"}, {0}, }; static void drop_readahead(PyFileObject *f) { if (f->f_buf != NULL) { PyMem_Free(f->f_buf); f->f_buf = NULL; } } /* Make sure that file has a readahead buffer with at least one byte (unless at EOF) and no more than bufsize. Returns negative value on error, will set MemoryError if bufsize bytes cannot be allocated. */ static int readahead(PyFileObject *f, int bufsize) { Py_ssize_t chunksize; if (f->f_buf != NULL) { if( (f->f_bufend - f->f_bufptr) >= 1) return 0; else drop_readahead(f); } if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) { PyErr_NoMemory(); return -1; } Py_BEGIN_ALLOW_THREADS errno = 0; chunksize = Py_UniversalNewlineFread( f->f_buf, bufsize, f->f_fp, (PyObject *)f); Py_END_ALLOW_THREADS if (chunksize == 0) { if (ferror(f->f_fp)) { PyErr_SetFromErrno(PyExc_IOError); clearerr(f->f_fp); drop_readahead(f); return -1; } } f->f_bufptr = f->f_buf; f->f_bufend = f->f_buf + chunksize; return 0; } /* Used by file_iternext. The returned string will start with 'skip' uninitialized bytes followed by the remainder of the line. Don't be horrified by the recursive call: maximum recursion depth is limited by logarithmic buffer growth to about 50 even when reading a 1gb line. */ static PyStringObject * readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) { PyStringObject* s; char *bufptr; char *buf; Py_ssize_t len; if (f->f_buf == NULL) if (readahead(f, bufsize) < 0) return NULL; len = f->f_bufend - f->f_bufptr; if (len == 0) return (PyStringObject *) PyString_FromStringAndSize(NULL, skip); bufptr = (char *)memchr(f->f_bufptr, '\n', len); if (bufptr != NULL) { bufptr++; /* Count the '\n' */ len = bufptr - f->f_bufptr; s = (PyStringObject *) PyString_FromStringAndSize(NULL, skip+len); if (s == NULL) return NULL; memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); f->f_bufptr = bufptr; if (bufptr == f->f_bufend) drop_readahead(f); } else { bufptr = f->f_bufptr; buf = f->f_buf; f->f_buf = NULL; /* Force new readahead buffer */ assert(skip+len < INT_MAX); s = readahead_get_line_skip( f, (int)(skip+len), bufsize + (bufsize>>2) ); if (s == NULL) { PyMem_Free(buf); return NULL; } memcpy(PyString_AS_STRING(s)+skip, bufptr, len); PyMem_Free(buf); } return s; } /* A larger buffer size may actually decrease performance. */ #define READAHEAD_BUFSIZE 8192 static PyObject * file_iternext(PyFileObject *f) { PyStringObject* l; if (f->f_fp == NULL) return err_closed(); l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); if (l == NULL || PyString_GET_SIZE(l) == 0) { Py_XDECREF(l); return NULL; } return (PyObject *)l; } static PyObject * file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { 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; Py_INCREF(Py_None); ((PyFileObject *)self)->f_encoding = Py_None; ((PyFileObject *)self)->weakreflist = NULL; } return self; } static int file_init(PyObject *self, PyObject *args, PyObject *kwds) { PyFileObject *foself = (PyFileObject *)self; int ret = 0; static char *kwlist[] = {"name", "mode", "buffering", 0}; char *name = NULL; char *mode = "r"; int bufsize = -1; int wideargument = 0; 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); } #ifdef Py_WIN_WIDE_FILENAMES if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ PyObject *po;