diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index a85e07cacf7..cb1bdce08e0 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -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) diff --git a/Objects/fileobject.c b/Objects/fileobject.c index ebccc845cc2..9284185be63 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -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;