]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWith...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 21 Aug 2023 11:16:31 +0000 (14:16 +0300)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 11:16:31 +0000 (14:16 +0300)
Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst [new file with mode: 0644]
Python/errors.c

diff --git a/Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst b/Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst
new file mode 100644 (file)
index 0000000..f1f1660
--- /dev/null
@@ -0,0 +1,4 @@
+C API functions :c:func:`PyErr_SetFromErrnoWithFilename`,
+:c:func:`PyErr_SetExcFromWindowsErrWithFilename` and
+:c:func:`PyErr_SetFromWindowsErrWithFilename` save now the error code before
+calling :c:func:`PyUnicode_DecodeFSDefault`.
index d9086c507251e6722b52989ba286c9d1202ddf48..fb5b3ff2c7ba517aaaae98dc26b75a6d717da949 100644 (file)
@@ -895,10 +895,12 @@ PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
 {
     PyObject *name = NULL;
     if (filename) {
+        int i = errno;
         name = PyUnicode_DecodeFSDefault(filename);
         if (name == NULL) {
             return NULL;
         }
+        errno = i;
     }
     PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
     Py_XDECREF(name);
@@ -998,6 +1000,9 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
 {
     PyObject *name = NULL;
     if (filename) {
+        if ((DWORD)ierr == 0) {
+            ierr = (int)GetLastError();
+        }
         name = PyUnicode_DecodeFSDefault(filename);
         if (name == NULL) {
             return NULL;
@@ -1028,6 +1033,9 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
 {
     PyObject *name = NULL;
     if (filename) {
+        if ((DWORD)ierr == 0) {
+            ierr = (int)GetLastError();
+        }
         name = PyUnicode_DecodeFSDefault(filename);
         if (name == NULL) {
             return NULL;