summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2003-09-04 19:01:46 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2003-09-04 19:01:46 (GMT)
commit1e3bdf6c459a2e324730dda2db53ad845ad39532 (patch)
treed9402a5bd98c5efe7bca7779f48e9632e608541f /Objects
parentfa3bdea018c511dbfabec8c2860b36523448b217 (diff)
downloadcpython-1e3bdf6c459a2e324730dda2db53ad845ad39532.zip
cpython-1e3bdf6c459a2e324730dda2db53ad845ad39532.tar.gz
cpython-1e3bdf6c459a2e324730dda2db53ad845ad39532.tar.bz2
Patch #788249: Pass an explicit buffer to setvbuf in PyFile_SetBufSize().
Fixes #603724. Will backport to 2.3.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/fileobject.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 13354c8..bddd81e 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -283,25 +283,37 @@ PyFile_FromString(char *name, char *mode)
void
PyFile_SetBufSize(PyObject *f, int bufsize)
{
+ PyFileObject *file = (PyFileObject *)f;
if (bufsize >= 0) {
-#ifdef HAVE_SETVBUF
int type;
switch (bufsize) {
case 0:
type = _IONBF;
break;
+#ifdef HAVE_SETVBUF
case 1:
type = _IOLBF;
bufsize = BUFSIZ;
break;
+#endif
default:
type = _IOFBF;
+#ifndef HAVE_SETVBUF
+ bufsize = BUFSIZ;
+#endif
+ break;
+ }
+ fflush(file->f_fp);
+ if (type == _IONBF) {
+ PyMem_Free(file->f_setbuf);
+ file->f_setbuf = NULL;
+ } else {
+ file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
}
- setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
- type, bufsize);
+#ifdef HAVE_SETVBUF
+ setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
#else /* !HAVE_SETVBUF */
- if (bufsize <= 1)
- setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
+ setbuf(file->f_fp, file->f_setbuf);
#endif /* !HAVE_SETVBUF */
}
}
@@ -376,6 +388,7 @@ static PyObject *
file_close(PyFileObject *f)
{
int sts = 0;
+ PyMem_Free(f->f_setbuf);
if (f->f_fp != NULL) {
if (f->f_close != NULL) {
Py_BEGIN_ALLOW_THREADS
@@ -1928,6 +1941,7 @@ file_init(PyObject *self, PyObject *args, PyObject *kwds)
}
if (open_the_file(foself, name, mode) == NULL)
goto Error;
+ foself->f_setbuf = NULL;
PyFile_SetBufSize(self, bufsize);
goto Done;