mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Fix SF buf #476953: Bad more for opening file gives bad msg.
If fopen() fails with EINVAL it means that the mode argument is invalid. Return the mode in the error message instead of the filename.
This commit is contained in:
parent
20747fa167
commit
41c8321252
2 changed files with 17 additions and 2 deletions
|
|
@ -46,4 +46,15 @@ else:
|
||||||
print "writelines accepted sequence of non-string objects"
|
print "writelines accepted sequence of non-string objects"
|
||||||
f.close()
|
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)
|
os.unlink(TESTFN)
|
||||||
|
|
|
||||||
|
|
@ -121,13 +121,17 @@ open_the_file(PyFileObject *f, char *name, char *mode)
|
||||||
if (f->f_fp == NULL) {
|
if (f->f_fp == NULL) {
|
||||||
#ifdef NO_FOPEN_ERRNO
|
#ifdef NO_FOPEN_ERRNO
|
||||||
/* Metroworks only, not testable, so unchanged */
|
/* Metroworks only, not testable, so unchanged */
|
||||||
if ( errno == 0 ) {
|
if (errno == 0) {
|
||||||
PyErr_SetString(PyExc_IOError, "Cannot open file");
|
PyErr_SetString(PyExc_IOError, "Cannot open file");
|
||||||
Py_DECREF(f);
|
Py_DECREF(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#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;
|
f = NULL;
|
||||||
}
|
}
|
||||||
return (PyObject *)f;
|
return (PyObject *)f;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue