Index: Objects/fileobject.c =================================================================== --- Objects/fileobject.c (revision 82848) +++ Objects/fileobject.c (working copy) @@ -234,10 +234,36 @@ return 0; } +#if 1 /* DEBUG */ + +#define DEBUG_ENTER(func_) \ + const char *func = func_; \ + const int thread = GetCurrentThreadId(); \ + printf("thread(%4d) func(%s): enter....\n", GetCurrentThreadId(), func); \ + fflush(stdout); + +#define DEBUG(s, file) \ + assert(file); \ + printf("thread(%4d) func(%s): file(%p) address(%p): " ## s ## "\n", GetCurrentThreadId(), func, file, file->f_setbuf); \ + fflush(stdout); + +#define DEBUG_LEAVE \ + printf("thread(%4d) func(%s): leave....\n", GetCurrentThreadId(), func); \ + fflush(stdout); + +#else /* DEBUG */ + +#define DEBUG_ENTER(func_) +#define DEBUG(s, file) +#define DEBUG_LEAVE + +#endif + static PyObject * open_the_file(PyFileObject *f, char *name, char *mode) { char *newmode; + DEBUG_ENTER("open_the_file") assert(f != NULL); assert(PyFile_Check(f)); #ifdef MS_WINDOWS @@ -253,6 +279,7 @@ newmode = PyMem_MALLOC(strlen(mode) + 3); if (!newmode) { PyErr_NoMemory(); + DEBUG_LEAVE return NULL; } strcpy(newmode, mode); @@ -284,6 +311,7 @@ f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name), PyUnicode_AS_UNICODE(wmode)); FILE_END_ALLOW_THREADS(f) + DEBUG("_wfopen", f); } Py_XDECREF(wmode); } @@ -292,6 +320,7 @@ FILE_BEGIN_ALLOW_THREADS(f) f->f_fp = fopen(name, newmode); FILE_END_ALLOW_THREADS(f) + DEBUG("fopen", f); } if (f->f_fp == NULL) { @@ -342,6 +371,7 @@ int sts = 0; int (*local_close)(FILE *); FILE *local_fp = f->f_fp; + DEBUG_ENTER("close_the_file") if (local_fp != NULL) { local_close = f->f_close; if (local_close != NULL && f->unlocked_count > 0) { @@ -358,6 +388,7 @@ "PyFileObject locking error in " "destructor (refcnt <= 0 at close)."); } + DEBUG_LEAVE return NULL; } /* NULL out the FILE pointer before releasing the GIL, because @@ -365,16 +396,23 @@ * called. */ f->f_fp = NULL; if (local_close != NULL) { + DEBUG("local_close enter...", f); Py_BEGIN_ALLOW_THREADS errno = 0; sts = (*local_close)(local_fp); Py_END_ALLOW_THREADS - if (sts == EOF) + DEBUG("local_close leave...", f); + if (sts == EOF) { + DEBUG_LEAVE return PyErr_SetFromErrno(PyExc_IOError); - if (sts != 0) + } + if (sts != 0) { + DEBUG_LEAVE return PyInt_FromLong((long)sts); + } } } + DEBUG_LEAVE Py_RETURN_NONE; } @@ -416,6 +454,7 @@ PyFile_SetBufSize(PyObject *f, int bufsize) { PyFileObject *file = (PyFileObject *)f; + DEBUG_ENTER("PyFile_SetBufSize") if (bufsize >= 0) { int type; switch (bufsize) { @@ -437,11 +476,14 @@ } fflush(file->f_fp); if (type == _IONBF) { + DEBUG("PyMem_Free", file); PyMem_Free(file->f_setbuf); file->f_setbuf = NULL; } else { + DEBUG("PyMem_Realloc enter", file); file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf, bufsize); + DEBUG("PyMem_Realloc leave", file); } #ifdef HAVE_SETVBUF setvbuf(file->f_fp, file->f_setbuf, type, bufsize); @@ -449,6 +491,7 @@ setbuf(file->f_fp, file->f_setbuf); #endif /* !HAVE_SETVBUF */ } + DEBUG_LEAVE } /* Set the encoding used to output Unicode strings. @@ -520,6 +563,7 @@ file_dealloc(PyFileObject *f) { PyObject *ret; + DEBUG_ENTER("file_dealloc") if (f->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) f); ret = close_the_file(f); @@ -530,6 +574,7 @@ else { Py_DECREF(ret); } + DEBUG("PyMem_Free", f); PyMem_Free(f->f_setbuf); Py_XDECREF(f->f_name); Py_XDECREF(f->f_mode); @@ -537,6 +582,7 @@ Py_XDECREF(f->f_errors); drop_readahead(f); Py_TYPE(f)->tp_free((PyObject *)f); + DEBUG_LEAVE } static PyObject * @@ -567,11 +613,15 @@ static PyObject * file_close(PyFileObject *f) { - PyObject *sts = close_the_file(f); + PyObject *sts; + DEBUG_ENTER("file_close") + sts = close_the_file(f); if (sts) { + DEBUG("PyMem_Free", f); PyMem_Free(f->f_setbuf); f->f_setbuf = NULL; } + DEBUG_LEAVE return sts; }