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

Tim Peters tim_one@users.sourceforge.net
Sun, 07 Apr 2002 21:13:15 -0700


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

Modified Files:
	fileobject.c 
Log Message:
SF bug 538827:  Python open w/ MSVC6: bad error msgs.
open_the_file:  Some (not all) flavors of Windows set errno to EINVAL
when passed a syntactically invalid filename.  Python turned that into an
incomprehensible complaint about the mode string.  Fixed by special-casing
MSVC.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.155
retrieving revision 2.156
diff -C2 -d -r2.155 -r2.156
*** fileobject.c	7 Apr 2002 06:28:00 -0000	2.155
--- fileobject.c	8 Apr 2002 04:13:12 -0000	2.156
***************
*** 151,156 ****
  		}
  #endif
  		if (errno == EINVAL)
! 			PyErr_Format(PyExc_IOError, "invalid argument: %s",
  				     mode);
  		else
--- 151,169 ----
  		}
  #endif
+ #ifdef _MSC_VER
+ 		/* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
+ 		 * across all Windows flavors.  When it sets EINVAL varies
+ 		 * across Windows flavors, the exact conditions aren't
+ 		 * documented, and the answer lies in the OS's implementation
+ 		 * of Win32's CreateFile function (whose source is secret).
+ 		 * Seems the best we can do is map EINVAL to ENOENT.
+ 		 */
+ 		if (errno == 0)	/* bad mode string */
+ 			errno = EINVAL;
+ 		else if (errno == EINVAL) /* unknown, but not a mode string */
+ 			errno = ENOENT;
+ #endif
  		if (errno == EINVAL)
! 			PyErr_Format(PyExc_IOError, "invalid mode: %s",
  				     mode);
  		else
***************
*** 158,162 ****
  		f = NULL;
  	}
! 	if (f != NULL) 
  		f = dircheck(f);
  	return (PyObject *)f;
--- 171,175 ----
  		f = NULL;
  	}
! 	if (f != NULL)
  		f = dircheck(f);
  	return (PyObject *)f;