diff options
author | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-23 02:06:50 (GMT) |
---|---|---|
committer | Neil Schemenauer <nascheme@enme.ucalgary.ca> | 2002-03-23 02:06:50 (GMT) |
commit | ed19b88f0b203bea32c9193009143e71f40f355f (patch) | |
tree | 450c3f0ea1924bf40fc4a577e8bb008627ed695a /Objects/fileobject.c | |
parent | ce7fb9b5151f7238bdbaeb16c3041c9990ca8e9d (diff) | |
download | cpython-ed19b88f0b203bea32c9193009143e71f40f355f.zip cpython-ed19b88f0b203bea32c9193009143e71f40f355f.tar.gz cpython-ed19b88f0b203bea32c9193009143e71f40f355f.tar.bz2 |
Check in (hopefully) corrected version of last change.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index c8fb214..47e6b17 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -56,6 +56,32 @@ PyFile_Name(PyObject *f) 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, @@ -77,6 +103,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode, if (f->f_name == NULL || f->f_mode == NULL) return NULL; f->f_fp = fp; + f = dircheck(f); return (PyObject *) f; } @@ -130,6 +157,8 @@ open_the_file(PyFileObject *f, char *name, char *mode) PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); f = NULL; } + if (f != NULL) + f = dircheck(f); return (PyObject *)f; } |