[Python-checkins] CVS: python/dist/src/Objects fileobject.c,2.149,2.150

Neil Schemenauer nascheme@users.sourceforge.net
Fri, 22 Mar 2002 18:06:52 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv18739

Modified Files:
	fileobject.c 
Log Message:
Check in (hopefully) corrected version of last change.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.149
retrieving revision 2.150
diff -C2 -d -r2.149 -r2.150
*** fileobject.c	22 Mar 2002 23:50:30 -0000	2.149
--- fileobject.c	23 Mar 2002 02:06:50 -0000	2.150
***************
*** 57,60 ****
--- 57,86 ----
  }
  
+ /* 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 *
***************
*** 78,81 ****
--- 104,108 ----
  		return NULL;
  	f->f_fp = fp;
+         f = dircheck(f);
  	return (PyObject *) f;
  }
***************
*** 131,134 ****
--- 158,163 ----
  		f = NULL;
  	}
+ 	if (f != NULL) 
+ 		f = dircheck(f);
  	return (PyObject *)f;
  }