mirror of
https://github.com/python/cpython.git
synced 2025-10-07 15:42:02 +00:00
Instantiate the OS-related exception as soon as we raise it, so that
"except" works properly.
This commit is contained in:
parent
1e4fe702f6
commit
5d6fbe8207
2 changed files with 22 additions and 6 deletions
|
@ -79,6 +79,18 @@ class HierarchyTest(unittest.TestCase):
|
||||||
e = SubOSError(EEXIST, "Bad file descriptor")
|
e = SubOSError(EEXIST, "Bad file descriptor")
|
||||||
self.assertIs(type(e), SubOSError)
|
self.assertIs(type(e), SubOSError)
|
||||||
|
|
||||||
|
def test_try_except(self):
|
||||||
|
# This checks that try .. except checks the concrete exception
|
||||||
|
# (FileNotFoundError) and not the base type specified when
|
||||||
|
# PyErr_SetFromErrnoWithFilenameObject was called.
|
||||||
|
# (it is therefore deliberate that it doesn't use assertRaises)
|
||||||
|
try:
|
||||||
|
open("some_hopefully_non_existing_file")
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("should have raised a FileNotFoundError")
|
||||||
|
|
||||||
|
|
||||||
class AttributesTest(unittest.TestCase):
|
class AttributesTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -341,7 +341,7 @@ PyObject *
|
||||||
PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
|
PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
|
||||||
{
|
{
|
||||||
PyObject *message;
|
PyObject *message;
|
||||||
PyObject *v;
|
PyObject *v, *args;
|
||||||
int i = errno;
|
int i = errno;
|
||||||
#ifndef MS_WINDOWS
|
#ifndef MS_WINDOWS
|
||||||
char *s;
|
char *s;
|
||||||
|
@ -410,15 +410,19 @@ PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filenameObject != NULL)
|
if (filenameObject != NULL)
|
||||||
v = Py_BuildValue("(iOO)", i, message, filenameObject);
|
args = Py_BuildValue("(iOO)", i, message, filenameObject);
|
||||||
else
|
else
|
||||||
v = Py_BuildValue("(iO)", i, message);
|
args = Py_BuildValue("(iO)", i, message);
|
||||||
Py_DECREF(message);
|
Py_DECREF(message);
|
||||||
|
|
||||||
|
if (args != NULL) {
|
||||||
|
v = PyObject_Call(exc, args, NULL);
|
||||||
|
Py_DECREF(args);
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
PyErr_SetObject(exc, v);
|
PyErr_SetObject((PyObject *) Py_TYPE(v), v);
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
LocalFree(s_buf);
|
LocalFree(s_buf);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue