Skip to content

Commit 238c8d2

Browse files
[3.12] gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929) (#108205)
gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929) (cherry picked from commit 80bdebd) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1ce8b92 commit 238c8d2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
C API functions :c:func:`PyErr_SetFromErrnoWithFilename`,
2+
:c:func:`PyErr_SetExcFromWindowsErrWithFilename` and
3+
:c:func:`PyErr_SetFromWindowsErrWithFilename` save now the error code before
4+
calling :c:func:`PyUnicode_DecodeFSDefault`.

Python/errors.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,10 +919,12 @@ PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
919919
{
920920
PyObject *name = NULL;
921921
if (filename) {
922+
int i = errno;
922923
name = PyUnicode_DecodeFSDefault(filename);
923924
if (name == NULL) {
924925
return NULL;
925926
}
927+
errno = i;
926928
}
927929
PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
928930
Py_XDECREF(name);
@@ -1022,6 +1024,9 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
10221024
{
10231025
PyObject *name = NULL;
10241026
if (filename) {
1027+
if ((DWORD)ierr == 0) {
1028+
ierr = (int)GetLastError();
1029+
}
10251030
name = PyUnicode_DecodeFSDefault(filename);
10261031
if (name == NULL) {
10271032
return NULL;
@@ -1052,6 +1057,9 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
10521057
{
10531058
PyObject *name = NULL;
10541059
if (filename) {
1060+
if ((DWORD)ierr == 0) {
1061+
ierr = (int)GetLastError();
1062+
}
10551063
name = PyUnicode_DecodeFSDefault(filename);
10561064
if (name == NULL) {
10571065
return NULL;

0 commit comments

Comments
 (0)