Skip to content

Commit 80bdebd

Browse files
gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929)
1 parent acbd3f9 commit 80bdebd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
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

+8
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,12 @@ PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
895895
{
896896
PyObject *name = NULL;
897897
if (filename) {
898+
int i = errno;
898899
name = PyUnicode_DecodeFSDefault(filename);
899900
if (name == NULL) {
900901
return NULL;
901902
}
903+
errno = i;
902904
}
903905
PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
904906
Py_XDECREF(name);
@@ -998,6 +1000,9 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
9981000
{
9991001
PyObject *name = NULL;
10001002
if (filename) {
1003+
if ((DWORD)ierr == 0) {
1004+
ierr = (int)GetLastError();
1005+
}
10011006
name = PyUnicode_DecodeFSDefault(filename);
10021007
if (name == NULL) {
10031008
return NULL;
@@ -1028,6 +1033,9 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
10281033
{
10291034
PyObject *name = NULL;
10301035
if (filename) {
1036+
if ((DWORD)ierr == 0) {
1037+
ierr = (int)GetLastError();
1038+
}
10311039
name = PyUnicode_DecodeFSDefault(filename);
10321040
if (name == NULL) {
10331041
return NULL;

0 commit comments

Comments
 (0)