]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-107916: Save the error code before decoding the filename in PyErr_SetFromEr...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 21 Aug 2023 12:33:46 +0000 (05:33 -0700)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2023 12:33:46 +0000 (14:33 +0200)
gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929)
(cherry picked from commit 80bdebdd8593f007a2232ec04a7729bba6ebf12c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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 f1d3007b9fdf8cd65b1676194bdf23d89abd7d47..6c46d1f21366548403c511ccd8234d5dbe3784e6 100644 (file)
@@ -919,10 +919,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);
@@ -1022,6 +1024,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;
@@ -1052,6 +1057,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;