This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.stat() does not work for NUL and CON
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, paul.moore, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2019-05-28 07:37 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg343745 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-28 07:37
>>> os.stat('nul')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 1] Incorrect function: 'nul'
>>> os.stat('con')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 87] The parameter is incorrect: 'con'

But os.open() and os.fstat() work.

>>> os.fstat(os.open('nul', os.O_RDONLY))
os.stat_result(st_mode=8192, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0, st_atime=0, st_mtime=0, st_ctime=0)
>>> os.fstat(os.open('con', os.O_RDONLY))
os.stat_result(st_mode=8192, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0, st_atime=0, st_mtime=0, st_ctime=0)
msg343755 - (view) Author: Michele Angrisano (mangrisano) * Date: 2019-05-28 09:57
I've tried to reproduce this behavior on my Mac with python3.8 and python 3.7 but I couldn't.

If 'nul' doesn't exist, it raises a FileNotFound exception as it should do.
If 'nul' exists, it shows me the right output.
Same behavior with 'con'.
msg343781 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-05-28 14:10
Opening "CON" (i.e. r"\\.\CON") fails with ERROR_INVALID_PARAMETER (87) because it has to be opened with either GENERIC_READ or GENERIC_WRITE data access in order to map it to either the console input buffer or active screen buffer. The CreateFileW call in stat() necessarily requests no data access.

Calling stat on "NUL" (among others such as "CONIN$", "//./C:", and "//./PhysicalDrive0") fails with ERROR_INVALID_FUNCTION (1) -- or possibly ERROR_INVALID_PARAMETER or ERROR_NOT_SUPPORTED (50), depending on the device. stat() calls GetFileInformationByHandle. This requests FileFsVolumeInformation and FileAllInformation, which commonly fail as unsupported or invalid requests for devices other than filesystem devices. Even volume and raw disk devices fail a FileAllInformation request. 

If we have a valid file handle, we can get the file type via GetFileType(hFile), as _Py_fstat_noraise does in Python/fileutils.c. If opening a handle fails with ERROR_INVALID_PARAMETER for a path that resolves to r"\\.\CON" or r"\\?\CON" via GetFullPathNameW, we can simply set st_mode to _S_IFCHR and return.

For example:

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD lastError = GetLastError();
        if (lastError == ERROR_INVALID_PARAMETER) {
            WCHAR fullPath[8];
            if (GetFullPathNameW(path, sizeof(fullPath),
                    fullPath, NULL) == 7 && (
                _wcsicmp(fullPath, L"\\\\.\\CON") == 0 ||
                _wcsicmp(fullPath, L"\\\\?\\CON") == 0)) {
                memset(result, 0, sizeof(*result));
                result->st_mode = _S_IFCHR;
                return 0;
            }
        }
        /*
            Existing error handling code.
        */
    } else {
        DWORD type = GetFileType(hFile);
        if (type != FILE_TYPE_DISK) {
            CloseHandle(hFile);
            if (type == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
                return -1;
            }
            memset(result, 0, sizeof(*result));
            if (type == FILE_TYPE_CHAR) { /* e.g. "//./NUL" */
                result->st_mode = _S_IFCHR;
            } else if (type == FILE_TYPE_PIPE) { /* e.g. "//./PIPE/Spam" */
                result->st_mode = _S_IFIFO;
            }
            return 0;
        } else if (!GetFileInformationByHandle(hFile, &info)) {
            DWORD lastError = GetLastError();
            CloseHandle(hFile);
            /* e.g. "//./C:" or "//./PhysicalDrive0" */
            if (lastError == ERROR_INVALID_FUNCTION ||
                lastError == ERROR_INVALID_PARAMETER ||
                lastError == ERROR_NOT_SUPPORTED) {
                memset(result, 0, sizeof(*result));
                result->st_mode = _S_IFREG;
                return 0;
            }
            return -1;
        }
    }
msg343783 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-05-28 14:31
> GetFullPathNameW(path, sizeof(fullPath),

That should be sizeof(fullPath) / sizeof(WCHAR), or use Py_ARRAY_LENGTH, or just hard code 8. That's probably not the only mistake. But this is just an example to discuss the details and alternatives.
msg345107 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-06-10 08:01
Do you mind to create a PR Eryk?
msg350158 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-22 05:40
Where it was fixed?
msg350166 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-08-22 06:57
> Where it was fixed?

It was addressed in issue 37834. PR 15231 includes a rewrite of win32_xstat_impl. The file type was needed, so the rewrite could also address the problem of character devices such as CON and NUL.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81255
2019-08-22 06:57:04eryksunsetmessages: + msg350166
2019-08-22 05:40:27serhiy.storchakasetmessages: + msg350158
2019-08-22 04:57:30eryksunsetstatus: open -> closed
resolution: out of date
stage: resolved
2019-06-10 08:01:11serhiy.storchakasetmessages: + msg345107
2019-06-02 22:04:04mangrisanosetnosy: - mangrisano
2019-05-28 14:31:50eryksunsetmessages: + msg343783
2019-05-28 14:10:09eryksunsetnosy: + eryksun
messages: + msg343781
2019-05-28 09:57:10mangrisanosetnosy: + mangrisano
messages: + msg343755
2019-05-28 07:37:41serhiy.storchakacreate