Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8acde7d

Browse files
committed
Issue #23524: Change back to using Windows errors for _Py_fstat instead of the errno shim.
1 parent 35a97c0 commit 8acde7d

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

Modules/_io/fileio.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ check_fd(int fd)
182182
{
183183
#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
184184
struct _Py_stat_struct buf;
185-
if (_Py_fstat(fd, &buf) < 0 && errno == EBADF) {
185+
if (_Py_fstat(fd, &buf) < 0 &&
186+
#ifdef MS_WINDOWS
187+
GetLastError() == ERROR_INVALID_HANDLE
188+
#else
189+
errno == EBADF
190+
#endif
191+
) {
186192
PyObject *exc;
187193
char *msg = strerror(EBADF);
188194
exc = PyObject_CallFunction(PyExc_OSError, "(is)",

Modules/signalmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
560560
}
561561

562562
if (_Py_fstat(fd, &st) != 0) {
563-
PyErr_SetFromErrno(PyExc_OSError);
563+
PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError());
564564
return NULL;
565565
}
566566

Python/fileutils.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,14 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
637637
else
638638
h = (HANDLE)_get_osfhandle(fd);
639639

640+
/* Protocol violation: we explicitly clear errno, instead of
641+
setting it to a POSIX error. Callers should use GetLastError. */
640642
errno = 0;
641643

642644
if (h == INVALID_HANDLE_VALUE) {
643-
errno = EBADF;
645+
/* This is really a C library error (invalid file handle).
646+
We set the Win32 error to the closes one matching. */
647+
SetLastError(ERROR_INVALID_HANDLE);
644648
return -1;
645649
}
646650
memset(result, 0, sizeof(*result));
@@ -649,7 +653,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
649653
if (type == FILE_TYPE_UNKNOWN) {
650654
DWORD error = GetLastError();
651655
if (error != 0) {
652-
errno = EINVAL;
653656
return -1;
654657
}
655658
/* else: valid but unknown file */
@@ -664,7 +667,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
664667
}
665668

666669
if (!GetFileInformationByHandle(h, &info)) {
667-
errno = EINVAL;
668670
return -1;
669671
}
670672

0 commit comments

Comments
 (0)