]> granicus.if.org Git - python/commitdiff
Fix SF buf #476953: Bad more for opening file gives bad msg.
authorJeremy Hylton <jeremy@alum.mit.edu>
Fri, 9 Nov 2001 16:17:24 +0000 (16:17 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Fri, 9 Nov 2001 16:17:24 +0000 (16:17 +0000)
If fopen() fails with EINVAL it means that the mode argument is
invalid.  Return the mode in the error message instead of the
filename.

Lib/test/test_file.py
Objects/fileobject.c

index a85e07cacf7a4982783aba0dd0c1abdfc0d73e07..cb1bdce08e0d8b35d210b5a6cab3ac97d5a0760e 100644 (file)
@@ -46,4 +46,15 @@ else:
     print "writelines accepted sequence of non-string objects"
 f.close()
 
+# verify that we get a sensible error message for bad made argument
+bad_mode = "qwerty"
+try:
+    open(TESTFN, bad_mode)
+except IOError, msg:
+    s = str(msg)
+    if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
+        print "bad error message for invalid mode: %s" % s
+else:
+    print "no error for invalid mode: %s" % bad_mode
+
 os.unlink(TESTFN)
index ebccc845cc25c86fe9d5a3467b733ca545906a06..9284185be637cb306d1ddafcddc30480dc3236f6 100644 (file)
@@ -121,13 +121,17 @@ open_the_file(PyFileObject *f, char *name, char *mode)
        if (f->f_fp == NULL) {
 #ifdef NO_FOPEN_ERRNO
                /* Metroworks only, not testable, so unchanged */
-               if ( errno == 0 ) {
+               if (errno == 0) {
                        PyErr_SetString(PyExc_IOError, "Cannot open file");
                        Py_DECREF(f);
                        return NULL;
                }
 #endif
-               PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
+               if (errno == EINVAL)
+                       PyErr_Format(PyExc_IOError, "invalid argument: %s",
+                                    mode);
+               else
+                       PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
                f = NULL;
        }
        return (PyObject *)f;