Skip to content

Commit ed67e60

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

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
@@ -856,10 +856,12 @@ PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
856856
{
857857
PyObject *name = NULL;
858858
if (filename) {
859+
int i = errno;
859860
name = PyUnicode_DecodeFSDefault(filename);
860861
if (name == NULL) {
861862
return NULL;
862863
}
864+
errno = i;
863865
}
864866
PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
865867
Py_XDECREF(name);
@@ -959,6 +961,9 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
959961
{
960962
PyObject *name = NULL;
961963
if (filename) {
964+
if ((DWORD)ierr == 0) {
965+
ierr = (int)GetLastError();
966+
}
962967
name = PyUnicode_DecodeFSDefault(filename);
963968
if (name == NULL) {
964969
return NULL;
@@ -989,6 +994,9 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
989994
{
990995
PyObject *name = NULL;
991996
if (filename) {
997+
if ((DWORD)ierr == 0) {
998+
ierr = (int)GetLastError();
999+
}
9921000
name = PyUnicode_DecodeFSDefault(filename);
9931001
if (name == NULL) {
9941002
return NULL;

0 commit comments

Comments
 (0)