mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Instantiate the OS-related exception as soon as we raise it, so that "except"
works properly. PyErr_SetFromErrnoWithFilenameObject() was already fixed by the changeset 793c75177d28. This commit fixes PyErr_SetExcFromWindowsErrWithFilenameObject(), used on Windows.
This commit is contained in:
parent
ecd0207444
commit
9ea8e4c29d
2 changed files with 21 additions and 6 deletions
|
|
@ -80,12 +80,23 @@ class HierarchyTest(unittest.TestCase):
|
||||||
self.assertIs(type(e), SubOSError)
|
self.assertIs(type(e), SubOSError)
|
||||||
|
|
||||||
def test_try_except(self):
|
def test_try_except(self):
|
||||||
|
filename = "some_hopefully_non_existing_file"
|
||||||
|
|
||||||
# This checks that try .. except checks the concrete exception
|
# This checks that try .. except checks the concrete exception
|
||||||
# (FileNotFoundError) and not the base type specified when
|
# (FileNotFoundError) and not the base type specified when
|
||||||
# PyErr_SetFromErrnoWithFilenameObject was called.
|
# PyErr_SetFromErrnoWithFilenameObject was called.
|
||||||
# (it is therefore deliberate that it doesn't use assertRaises)
|
# (it is therefore deliberate that it doesn't use assertRaises)
|
||||||
try:
|
try:
|
||||||
open("some_hopefully_non_existing_file")
|
open(filename)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("should have raised a FileNotFoundError")
|
||||||
|
|
||||||
|
# Another test for PyErr_SetExcFromWindowsErrWithFilenameObject()
|
||||||
|
self.assertFalse(os.path.exists(filename))
|
||||||
|
try:
|
||||||
|
os.unlink(filename)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -468,7 +468,7 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
|
||||||
int len;
|
int len;
|
||||||
WCHAR *s_buf = NULL; /* Free via LocalFree */
|
WCHAR *s_buf = NULL; /* Free via LocalFree */
|
||||||
PyObject *message;
|
PyObject *message;
|
||||||
PyObject *v;
|
PyObject *args, *v;
|
||||||
DWORD err = (DWORD)ierr;
|
DWORD err = (DWORD)ierr;
|
||||||
if (err==0) err = GetLastError();
|
if (err==0) err = GetLastError();
|
||||||
len = FormatMessageW(
|
len = FormatMessageW(
|
||||||
|
|
@ -504,12 +504,16 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
|
||||||
filenameObject = Py_None;
|
filenameObject = Py_None;
|
||||||
/* This is the constructor signature for passing a Windows error code.
|
/* This is the constructor signature for passing a Windows error code.
|
||||||
The POSIX translation will be figured out by the constructor. */
|
The POSIX translation will be figured out by the constructor. */
|
||||||
v = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
|
args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
|
||||||
Py_DECREF(message);
|
Py_DECREF(message);
|
||||||
|
|
||||||
if (v != NULL) {
|
if (args != NULL) {
|
||||||
PyErr_SetObject(exc, v);
|
v = PyObject_Call(exc, args, NULL);
|
||||||
Py_DECREF(v);
|
Py_DECREF(args);
|
||||||
|
if (v != NULL) {
|
||||||
|
PyErr_SetObject((PyObject *) Py_TYPE(v), v);
|
||||||
|
Py_DECREF(v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LocalFree(s_buf);
|
LocalFree(s_buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue