Index: fileobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v retrieving revision 2.141 diff -c -p -r2.141 fileobject.c *** fileobject.c 2001/11/30 14:16:36 2.141 --- fileobject.c 2001/12/03 08:58:31 *************** PyFile_Name(PyObject *f) *** 65,71 **** --- 65,97 ---- return ((PyFileObject *)f)->f_name; } + /* 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; + } + + static PyObject * fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode, int (*close)(FILE *)) *************** fill_file_fields(PyFileObject *f, FILE * *** 86,91 **** --- 112,118 ---- if (f->f_name == NULL || f->f_mode == NULL) return NULL; f->f_fp = fp; + f = dircheck(f); return (PyObject *) f; } *************** open_the_file(PyFileObject *f, char *nam *** 139,144 **** --- 166,172 ---- PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); f = NULL; } + f = dircheck(f); return (PyObject *)f; }