From c3f54dd5d927667235986b636624f5690e067a27 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 29 Jul 2014 22:32:47 +0200 Subject: Issue #22054: Add os.get_blocking() and os.set_blocking() functions to get and set the blocking mode of a file descriptor (False if the O_NONBLOCK flag is set, True otherwise). These functions are not available on Windows. --- Python/fileutils.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index a55064ffdc..065d3fd974 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1045,3 +1045,56 @@ _Py_dup(int fd) return fd; } +#ifndef MS_WINDOWS +/* Get the blocking mode of the file descriptor. + Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, + raise an exception and return -1 on error. */ +int +_Py_get_blocking(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + + return !(flags & O_NONBLOCK); +} + +/* Set the blocking mode of the specified file descriptor. + + Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag + otherwise. + + Return 0 on success, raise an exception and return -1 on error. */ +int +_Py_set_blocking(int fd, int blocking) +{ +#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) + int arg = !blocking; + if (ioctl(fd, FIONBIO, &arg) < 0) + goto error; +#else + int flags, res; + + flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + goto error; + + if (blocking) + flags = flags & (~O_NONBLOCK); + else + flags = flags | O_NONBLOCK; + + res = fcntl(fd, F_SETFL, flags); + if (res < 0) + goto error; +#endif + return 0; + +error: + PyErr_SetFromErrno(PyExc_OSError); + return -1; +} +#endif + -- cgit v1.2.1 From db4ad80ec7958ad28530e990b75c8aff03e2bd38 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 1 Aug 2014 12:28:48 +0200 Subject: Issue #18395: Rename ``_Py_char2wchar()`` to :c:func:`Py_DecodeLocale`, rename ``_Py_wchar2char()`` to :c:func:`Py_EncodeLocale`, and document these functions. --- Python/fileutils.c | 67 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 065d3fd974..227e92a79e 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -82,11 +82,11 @@ extern int _Py_normalize_encoding(const char *, char *, size_t); Values of force_ascii: - 1: the workaround is used: _Py_wchar2char() uses - encode_ascii_surrogateescape() and _Py_char2wchar() uses + 1: the workaround is used: Py_EncodeLocale() uses + encode_ascii_surrogateescape() and Py_DecodeLocale() uses decode_ascii_surrogateescape() - 0: the workaround is not used: _Py_wchar2char() uses wcstombs() and - _Py_char2wchar() uses mbstowcs() + 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and + Py_DecodeLocale() uses mbstowcs() -1: unknown, need to call check_force_ascii() to get the value */ static int force_ascii = -1; @@ -241,24 +241,26 @@ decode_ascii_surrogateescape(const char *arg, size_t *size) /* Decode a byte string from the locale encoding with the - surrogateescape error handler (undecodable bytes are decoded as characters - in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate + surrogateescape error handler: undecodable bytes are decoded as characters + in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate character, escape the bytes using the surrogateescape error handler instead of decoding them. - Use _Py_wchar2char() to encode the character string back to a byte string. + Return a pointer to a newly allocated wide character string, use + PyMem_RawFree() to free the memory. If size is not NULL, write the number of + wide characters excluding the null character into *size - Return a pointer to a newly allocated wide character string (use - PyMem_RawFree() to free the memory) and write the number of written wide - characters excluding the null character into *size if size is not NULL, or - NULL on error (decoding or memory allocation error). If size is not NULL, - *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding - error. + Return NULL on decoding error or memory allocation error. If *size* is not + NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on + decoding error. - Conversion errors should never happen, unless there is a bug in the C - library. */ + Decoding errors should never happen, unless there is a bug in the C + library. + + Use the Py_EncodeLocale() function to encode the character string back to a + byte string. */ wchar_t* -_Py_char2wchar(const char* arg, size_t *size) +Py_DecodeLocale(const char* arg, size_t *size) { #ifdef __APPLE__ wchar_t *wstr; @@ -389,19 +391,20 @@ oom: #endif /* __APPLE__ */ } -/* Encode a (wide) character string to the locale encoding with the - surrogateescape error handler (characters in range U+DC80..U+DCFF are - converted to bytes 0x80..0xFF). +/* Encode a wide character string to the locale encoding with the + surrogateescape error handler: surrogate characters in the range + U+DC80..U+DCFF are converted to bytes 0x80..0xFF. - This function is the reverse of _Py_char2wchar(). + Return a pointer to a newly allocated byte string, use PyMem_Free() to free + the memory. Return NULL on encoding or memory allocation error. - Return a pointer to a newly allocated byte string (use PyMem_Free() to free - the memory), or NULL on encoding or memory allocation error. + If error_pos is not NULL, *error_pos is set to the index of the invalid + character on encoding error, or set to (size_t)-1 otherwise. - If error_pos is not NULL: *error_pos is the index of the invalid character - on encoding error, or (size_t)-1 otherwise. */ + Use the Py_DecodeLocale() function to decode the bytes string back to a wide + character string. */ char* -_Py_wchar2char(const wchar_t *text, size_t *error_pos) +Py_EncodeLocale(const wchar_t *text, size_t *error_pos) { #ifdef __APPLE__ Py_ssize_t len; @@ -520,7 +523,7 @@ _Py_wstat(const wchar_t* path, struct stat *buf) { int err; char *fname; - fname = _Py_wchar2char(path, NULL); + fname = Py_EncodeLocale(path, NULL); if (fname == NULL) { errno = EINVAL; return -1; @@ -784,7 +787,7 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode) errno = EINVAL; return NULL; } - cpath = _Py_wchar2char(path, NULL); + cpath = Py_EncodeLocale(path, NULL); if (cpath == NULL) return NULL; f = fopen(cpath, cmode); @@ -875,7 +878,7 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) int res; size_t r1; - cpath = _Py_wchar2char(path, NULL); + cpath = Py_EncodeLocale(path, NULL); if (cpath == NULL) { errno = EINVAL; return -1; @@ -889,7 +892,7 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) return -1; } cbuf[res] = '\0'; /* buf will be null terminated */ - wbuf = _Py_char2wchar(cbuf, &r1); + wbuf = Py_DecodeLocale(cbuf, &r1); if (wbuf == NULL) { errno = EINVAL; return -1; @@ -920,7 +923,7 @@ _Py_wrealpath(const wchar_t *path, wchar_t *wresolved_path; char *res; size_t r; - cpath = _Py_wchar2char(path, NULL); + cpath = Py_EncodeLocale(path, NULL); if (cpath == NULL) { errno = EINVAL; return NULL; @@ -930,7 +933,7 @@ _Py_wrealpath(const wchar_t *path, if (res == NULL) return NULL; - wresolved_path = _Py_char2wchar(cresolved_path, &r); + wresolved_path = Py_DecodeLocale(cresolved_path, &r); if (wresolved_path == NULL) { errno = EINVAL; return NULL; @@ -963,7 +966,7 @@ _Py_wgetcwd(wchar_t *buf, size_t size) if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) return NULL; - wname = _Py_char2wchar(fname, &len); + wname = Py_DecodeLocale(fname, &len); if (wname == NULL) return NULL; if (size <= len) { -- cgit v1.2.1 From 4b5c8edd294f02afef7407d9fd5236789bd4bc34 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 21 Feb 2015 08:44:05 -0800 Subject: Issue #23152: Implement _Py_fstat() to support files larger than 2 GB on Windows. fstat() may fail with EOVERFLOW on files larger than 2 GB because the file size type is an signed 32-bit integer. --- Python/fileutils.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 2 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index c5c8d4e990..8f3fdca755 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -544,8 +544,145 @@ _Py_wstat(const wchar_t* path, struct stat *buf) } #endif -#ifdef HAVE_STAT +#if defined(HAVE_FSTAT) || defined(MS_WINDOWS) + +#ifdef MS_WINDOWS +static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ + +static void +FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) +{ + /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ + /* Cannot simply cast and dereference in_ptr, + since it might not be aligned properly */ + __int64 in; + memcpy(&in, in_ptr, sizeof(in)); + *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ + *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); +} + +void +time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) +{ + /* XXX endianness */ + __int64 out; + out = time_in + secs_between_epochs; + out = out * 10000000 + nsec_in / 100; + memcpy(out_ptr, &out, sizeof(out)); +} + +/* Below, we *know* that ugo+r is 0444 */ +#if _S_IREAD != 0400 +#error Unsupported C library +#endif +static int +attributes_to_mode(DWORD attr) +{ + int m = 0; + if (attr & FILE_ATTRIBUTE_DIRECTORY) + m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ + else + m |= _S_IFREG; + if (attr & FILE_ATTRIBUTE_READONLY) + m |= 0444; + else + m |= 0666; + return m; +} + +int +attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) +{ + memset(result, 0, sizeof(*result)); + result->st_mode = attributes_to_mode(info->dwFileAttributes); + result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; + result->st_dev = info->dwVolumeSerialNumber; + result->st_rdev = result->st_dev; + FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); + FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); + FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); + result->st_nlink = info->nNumberOfLinks; + result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; + if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { + /* first clear the S_IFMT bits */ + result->st_mode ^= (result->st_mode & S_IFMT); + /* now set the bits that make this a symlink */ + result->st_mode |= S_IFLNK; + } + result->st_file_attributes = info->dwFileAttributes; + + return 0; +} +#endif + +/* Return information about a file. + + On POSIX, use fstat(). + + On Windows, use GetFileType() and GetFileInformationByHandle() which support + files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger + than 2 GB because the file size type is an signed 32-bit integer: see issue + #23152. + */ +int +_Py_fstat(int fd, struct _Py_stat_struct *result) +{ +#ifdef MS_WINDOWS + BY_HANDLE_FILE_INFORMATION info; + HANDLE h; + int type; + + if (!_PyVerify_fd(fd)) + h = INVALID_HANDLE_VALUE; + else + h = (HANDLE)_get_osfhandle(fd); + + /* Protocol violation: we explicitly clear errno, instead of + setting it to a POSIX error. Callers should use GetLastError. */ + errno = 0; + + if (h == INVALID_HANDLE_VALUE) { + /* This is really a C library error (invalid file handle). + We set the Win32 error to the closes one matching. */ + SetLastError(ERROR_INVALID_HANDLE); + return -1; + } + memset(result, 0, sizeof(*result)); + + type = GetFileType(h); + if (type == FILE_TYPE_UNKNOWN) { + DWORD error = GetLastError(); + if (error != 0) { + return -1; + } + /* else: valid but unknown file */ + } + + if (type != FILE_TYPE_DISK) { + if (type == FILE_TYPE_CHAR) + result->st_mode = _S_IFCHR; + else if (type == FILE_TYPE_PIPE) + result->st_mode = _S_IFIFO; + return 0; + } + + if (!GetFileInformationByHandle(h, &info)) { + return -1; + } + + attribute_data_to_stat(&info, 0, result); + /* specific to fstat() */ + result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; + return 0; +#else + return fstat(fd, result); +#endif +} +#endif /* HAVE_FSTAT || MS_WINDOWS */ + + +#ifdef HAVE_STAT /* Call _wstat() on Windows, or encode the path to the filesystem encoding and call stat() otherwise. Only fill st_mode attribute on Windows. @@ -578,7 +715,8 @@ _Py_stat(PyObject *path, struct stat *statbuf) #endif } -#endif +#endif /* HAVE_STAT */ + static int get_inheritable(int fd, int raise) -- cgit v1.2.1 From 89363ff86bc7a9bdce45baf22b2c89695efc50d0 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 21 Feb 2015 10:04:10 -0800 Subject: Issue #23152: Renames attribute_data_to_stat to _Py_attribute_data_to_stat --- Python/fileutils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 8f3fdca755..bf99e2d6bc 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -592,7 +592,7 @@ attributes_to_mode(DWORD attr) } int -attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) +_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) { memset(result, 0, sizeof(*result)); result->st_mode = attributes_to_mode(info->dwFileAttributes); @@ -671,7 +671,7 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) return -1; } - attribute_data_to_stat(&info, 0, result); + _Py_attribute_data_to_stat(&info, 0, result); /* specific to fstat() */ result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; return 0; -- cgit v1.2.1 From 0683c38701f8d9dcbee329c01eb099d8ce65f957 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 21 Feb 2015 15:26:02 -0800 Subject: Issue #23152: Renames time_t_to_FILE_TIME to _Py_time_t_to_FILE_TIME, removes unused struct win32_stat and return value --- Python/fileutils.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index bf99e2d6bc..e7111c1431 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -563,7 +563,7 @@ FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) } void -time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) +_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) { /* XXX endianness */ __int64 out; @@ -591,7 +591,7 @@ attributes_to_mode(DWORD attr) return m; } -int +void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) { memset(result, 0, sizeof(*result)); @@ -611,8 +611,6 @@ _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, result->st_mode |= S_IFLNK; } result->st_file_attributes = info->dwFileAttributes; - - return 0; } #endif -- cgit v1.2.1 From 6139bc8c6c02db0e084b495d1fe5ac9707635ecb Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 6 Mar 2015 14:47:02 -0800 Subject: Issue #23524: Replace _PyVerify_fd function with calling _set_thread_local_invalid_parameter_handler on every thread. --- Python/fileutils.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 5 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index e7111c1431..c0dbc86a93 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -3,6 +3,7 @@ #include #ifdef MS_WINDOWS +# include # include #endif @@ -636,14 +637,10 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) else h = (HANDLE)_get_osfhandle(fd); - /* Protocol violation: we explicitly clear errno, instead of - setting it to a POSIX error. Callers should use GetLastError. */ errno = 0; if (h == INVALID_HANDLE_VALUE) { - /* This is really a C library error (invalid file handle). - We set the Win32 error to the closes one matching. */ - SetLastError(ERROR_INVALID_HANDLE); + errno = EBADF; return -1; } memset(result, 0, sizeof(*result)); @@ -652,6 +649,7 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); if (error != 0) { + errno = EINVAL; return -1; } /* else: valid but unknown file */ @@ -666,6 +664,7 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) } if (!GetFileInformationByHandle(h, &info)) { + errno = EINVAL; return -1; } @@ -1267,3 +1266,102 @@ error: } #endif +#ifdef _MSC_VER +#if _MSC_VER >= 1900 + +/* This function lets the Windows CRT validate the file handle without + terminating the process if it's invalid. */ +int +_PyVerify_fd(int fd) +{ + intptr_t osh; + /* Fast check for the only condition we know */ + if (fd < 0) { + _set_errno(EBADF); + return 0; + } + osh = _get_osfhandle(fd); + return osh != (intptr_t)-1; +} + +#elif _MSC_VER >= 1400 +/* Legacy implementation of _PyVerify_fd while transitioning to + * MSVC 14.0. This should eventually be removed. (issue23524) + */ + +/* Microsoft CRT in VS2005 and higher will verify that a filehandle is + * valid and raise an assertion if it isn't. + * Normally, an invalid fd is likely to be a C program error and therefore + * an assertion can be useful, but it does contradict the POSIX standard + * which for write(2) states: + * "Otherwise, -1 shall be returned and errno set to indicate the error." + * "[EBADF] The fildes argument is not a valid file descriptor open for + * writing." + * Furthermore, python allows the user to enter any old integer + * as a fd and should merely raise a python exception on error. + * The Microsoft CRT doesn't provide an official way to check for the + * validity of a file descriptor, but we can emulate its internal behaviour + * by using the exported __pinfo data member and knowledge of the + * internal structures involved. + * The structures below must be updated for each version of visual studio + * according to the file internal.h in the CRT source, until MS comes + * up with a less hacky way to do this. + * (all of this is to avoid globally modifying the CRT behaviour using + * _set_invalid_parameter_handler() and _CrtSetReportMode()) + */ +/* The actual size of the structure is determined at runtime. + * Only the first items must be present. + */ +typedef struct { + intptr_t osfhnd; + char osfile; +} my_ioinfo; + +extern __declspec(dllimport) char * __pioinfo[]; +#define IOINFO_L2E 5 +#define IOINFO_ARRAYS 64 +#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) +#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) +#define FOPEN 0x01 +#define _NO_CONSOLE_FILENO (intptr_t)-2 + +/* This function emulates what the windows CRT does to validate file handles */ +int +_PyVerify_fd(int fd) +{ + const int i1 = fd >> IOINFO_L2E; + const int i2 = fd & ((1 << IOINFO_L2E) - 1); + + static size_t sizeof_ioinfo = 0; + + /* Determine the actual size of the ioinfo structure, + * as used by the CRT loaded in memory + */ + if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { + sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; + } + if (sizeof_ioinfo == 0) { + /* This should not happen... */ + goto fail; + } + + /* See that it isn't a special CLEAR fileno */ + if (fd != _NO_CONSOLE_FILENO) { + /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead + * we check pointer validity and other info + */ + if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { + /* finally, check that the file is open */ + my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); + if (info->osfile & FOPEN) { + return 1; + } + } + } + fail: + errno = EBADF; + return 0; +} + +#endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */ +#endif /* defined _MSC_VER */ -- cgit v1.2.1 From 2d72ea4b9806468c66ac0562db71d5b4faeaea58 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sat, 7 Mar 2015 18:14:07 -0800 Subject: Issue #23524: Change back to using Windows errors for _Py_fstat instead of the errno shim. --- Python/fileutils.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index c0dbc86a93..6502823535 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -637,10 +637,14 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) else h = (HANDLE)_get_osfhandle(fd); + /* Protocol violation: we explicitly clear errno, instead of + setting it to a POSIX error. Callers should use GetLastError. */ errno = 0; if (h == INVALID_HANDLE_VALUE) { - errno = EBADF; + /* This is really a C library error (invalid file handle). + We set the Win32 error to the closes one matching. */ + SetLastError(ERROR_INVALID_HANDLE); return -1; } memset(result, 0, sizeof(*result)); @@ -649,7 +653,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); if (error != 0) { - errno = EINVAL; return -1; } /* else: valid but unknown file */ @@ -664,7 +667,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) } if (!GetFileInformationByHandle(h, &info)) { - errno = EINVAL; return -1; } -- cgit v1.2.1 From b8d0bcab60af86bb0d982ef0483eec7930a0a9d0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Mar 2015 00:22:14 +0100 Subject: Issue #23694: Enhance _Py_open(), it now raises exceptions * _Py_open() now raises exceptions on error. If open() fails, it raises an OSError with the filename. * _Py_open() now releases the GIL while calling open() * Add _Py_open_noraise() when _Py_open() cannot be used because the GIL is not held --- Python/fileutils.c | 72 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 17 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index c6cdb19fbe..76860406b2 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -30,7 +30,8 @@ extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 1: open() supports O_CLOEXEC flag, close-on-exec is set - The flag is used by _Py_open(), io.FileIO and os.open() */ + The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO + and os.open(). */ int _Py_open_cloexec_works = -1; #endif @@ -907,37 +908,74 @@ _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) return set_inheritable(fd, inheritable, 1, atomic_flag_works); } -/* Open a file with the specified flags (wrapper to open() function). - The file descriptor is created non-inheritable. */ -int -_Py_open(const char *pathname, int flags) +static int +_Py_open_impl(const char *pathname, int flags, int gil_held) { int fd; -#ifdef MS_WINDOWS - fd = open(pathname, flags | O_NOINHERIT); - if (fd < 0) - return fd; -#else - +#ifndef MS_WINDOWS int *atomic_flag_works; -#ifdef O_CLOEXEC +#endif + +#ifdef MS_WINDOWS + flags |= O_NOINHERIT; +#elif defined(O_CLOEXEC) atomic_flag_works = &_Py_open_cloexec_works; flags |= O_CLOEXEC; #else atomic_flag_works = NULL; #endif - fd = open(pathname, flags); - if (fd < 0) - return fd; - if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) { + if (gil_held) { + Py_BEGIN_ALLOW_THREADS + fd = open(pathname, flags); + Py_END_ALLOW_THREADS + + if (fd < 0) { + PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); + return -1; + } + } + else { + fd = open(pathname, flags); + if (fd < 0) + return -1; + } + +#ifndef MS_WINDOWS + if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { close(fd); return -1; } -#endif /* !MS_WINDOWS */ +#endif + return fd; } +/* Open a file with the specified flags (wrapper to open() function). + Return a file descriptor on success. Raise an exception and return -1 on + error. + + The file descriptor is created non-inheritable. + + The GIL must be held. Use _Py_open_noraise() if the GIL cannot be held. */ +int +_Py_open(const char *pathname, int flags) +{ + /* _Py_open() must be called with the GIL held. */ + assert(PyGILState_Check()); + return _Py_open_impl(pathname, flags, 1); +} + +/* Open a file with the specified flags (wrapper to open() function). + Return a file descriptor on success. Set errno and return -1 on error. + + The file descriptor is created non-inheritable. */ +int +_Py_open_noraise(const char *pathname, int flags) +{ + return _Py_open_impl(pathname, flags, 0); +} + /* Open a file. Use _wfopen() on Windows, encode the path to the locale encoding and use fopen() otherwise. The file descriptor is created non-inheritable. */ -- cgit v1.2.1 From b76e79ce344cf60aeeff69c4ca5c96302890ea3c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Mar 2015 01:39:23 +0100 Subject: Issue #23694: Enhance _Py_fopen(), it now raises an exception on error * If fopen() fails, OSError is raised with the original filename object. * The GIL is now released while calling fopen() --- Python/fileutils.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 76860406b2..a198625afa 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -957,7 +957,7 @@ _Py_open_impl(const char *pathname, int flags, int gil_held) The file descriptor is created non-inheritable. - The GIL must be held. Use _Py_open_noraise() if the GIL cannot be held. */ + The GIL must be held. */ int _Py_open(const char *pathname, int flags) { @@ -977,8 +977,9 @@ _Py_open_noraise(const char *pathname, int flags) } /* Open a file. Use _wfopen() on Windows, encode the path to the locale - encoding and use fopen() otherwise. The file descriptor is created - non-inheritable. */ + encoding and use fopen() otherwise. + + The file descriptor is created non-inheritable). */ FILE * _Py_wfopen(const wchar_t *path, const wchar_t *mode) { @@ -1009,7 +1010,9 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode) return f; } -/* Wrapper to fopen(). The file descriptor is created non-inheritable. */ +/* Wrapper to fopen(). + + The file descriptor is created non-inheritable). */ FILE* _Py_fopen(const char *pathname, const char *mode) { @@ -1024,11 +1027,14 @@ _Py_fopen(const char *pathname, const char *mode) } /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem - encoding and call fopen() otherwise. The file descriptor is created - non-inheritable. + encoding and call fopen() otherwise. + + Return the new file object on success. Raise an exception and return NULL + on error. - Return the new file object on success, or NULL if the file cannot be open or - (if PyErr_Occurred()) on unicode error. */ + The file descriptor is created non-inheritable. + + The GIL must be held. */ FILE* _Py_fopen_obj(PyObject *path, const char *mode) { @@ -1038,6 +1044,8 @@ _Py_fopen_obj(PyObject *path, const char *mode) wchar_t wmode[10]; int usize; + assert(PyGILState_Check()); + if (!PyUnicode_Check(path)) { PyErr_Format(PyExc_TypeError, "str file path expected under Windows, got %R", @@ -1049,20 +1057,36 @@ _Py_fopen_obj(PyObject *path, const char *mode) return NULL; usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); - if (usize == 0) + if (usize == 0) { + PyErr_SetFromWindowsErr(0); return NULL; + } + Py_BEGIN_ALLOW_THREADS f = _wfopen(wpath, wmode); + Py_END_ALLOW_THREADS #else PyObject *bytes; + char *path_bytes; + + assert(PyGILState_Check()); + if (!PyUnicode_FSConverter(path, &bytes)) return NULL; - f = fopen(PyBytes_AS_STRING(bytes), mode); + path_bytes = PyBytes_AS_STRING(bytes); + + Py_BEGIN_ALLOW_THREADS + f = fopen(path_bytes, mode); + Py_END_ALLOW_THREADS + Py_DECREF(bytes); #endif - if (f == NULL) + if (f == NULL) { + PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); return NULL; - if (make_non_inheritable(fileno(f)) < 0) { + } + + if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { fclose(f); return NULL; } -- cgit v1.2.1 From e3ce25cf8744ef9862bb811a87e077493836d2fe Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Mar 2015 09:52:54 +0100 Subject: Issue #23694: Handle EINTR in _Py_open() and _Py_fopen_obj() Retry open()/fopen() if it fails with EINTR and the Python signal handler doesn't raise an exception. --- Python/fileutils.c | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index a198625afa..fa481ae772 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -912,6 +912,7 @@ static int _Py_open_impl(const char *pathname, int flags, int gil_held) { int fd; + int async_err = 0; #ifndef MS_WINDOWS int *atomic_flag_works; #endif @@ -926,10 +927,14 @@ _Py_open_impl(const char *pathname, int flags, int gil_held) #endif if (gil_held) { - Py_BEGIN_ALLOW_THREADS - fd = open(pathname, flags); - Py_END_ALLOW_THREADS - + do { + Py_BEGIN_ALLOW_THREADS + fd = open(pathname, flags); + Py_END_ALLOW_THREADS + } while (fd < 0 + && errno == EINTR && !(async_err = PyErr_CheckSignals())); + if (async_err) + return -1; if (fd < 0) { PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); return -1; @@ -957,6 +962,9 @@ _Py_open_impl(const char *pathname, int flags, int gil_held) The file descriptor is created non-inheritable. + When interrupted by a signal (open() fails with EINTR), retry the syscall, + except if the Python signal handler raises an exception. + The GIL must be held. */ int _Py_open(const char *pathname, int flags) @@ -969,7 +977,9 @@ _Py_open(const char *pathname, int flags) /* Open a file with the specified flags (wrapper to open() function). Return a file descriptor on success. Set errno and return -1 on error. - The file descriptor is created non-inheritable. */ + The file descriptor is created non-inheritable. + + If interrupted by a signal, fail with EINTR. */ int _Py_open_noraise(const char *pathname, int flags) { @@ -979,7 +989,9 @@ _Py_open_noraise(const char *pathname, int flags) /* Open a file. Use _wfopen() on Windows, encode the path to the locale encoding and use fopen() otherwise. - The file descriptor is created non-inheritable). */ + The file descriptor is created non-inheritable. + + If interrupted by a signal, fail with EINTR. */ FILE * _Py_wfopen(const wchar_t *path, const wchar_t *mode) { @@ -1012,7 +1024,9 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode) /* Wrapper to fopen(). - The file descriptor is created non-inheritable). */ + The file descriptor is created non-inheritable. + + If interrupted by a signal, fail with EINTR. */ FILE* _Py_fopen(const char *pathname, const char *mode) { @@ -1034,11 +1048,15 @@ _Py_fopen(const char *pathname, const char *mode) The file descriptor is created non-inheritable. + When interrupted by a signal (open() fails with EINTR), retry the syscall, + except if the Python signal handler raises an exception. + The GIL must be held. */ FILE* _Py_fopen_obj(PyObject *path, const char *mode) { FILE *f; + int async_err = 0; #ifdef MS_WINDOWS wchar_t *wpath; wchar_t wmode[10]; @@ -1062,9 +1080,12 @@ _Py_fopen_obj(PyObject *path, const char *mode) return NULL; } - Py_BEGIN_ALLOW_THREADS - f = _wfopen(wpath, wmode); - Py_END_ALLOW_THREADS + do { + Py_BEGIN_ALLOW_THREADS + f = _wfopen(wpath, wmode); + Py_END_ALLOW_THREADS + } while (f == NULL + && errno == EINTR && !(async_err = PyErr_CheckSignals())); #else PyObject *bytes; char *path_bytes; @@ -1075,12 +1096,18 @@ _Py_fopen_obj(PyObject *path, const char *mode) return NULL; path_bytes = PyBytes_AS_STRING(bytes); - Py_BEGIN_ALLOW_THREADS - f = fopen(path_bytes, mode); - Py_END_ALLOW_THREADS + do { + Py_BEGIN_ALLOW_THREADS + f = fopen(path_bytes, mode); + Py_END_ALLOW_THREADS + } while (f == NULL + && errno == EINTR && !(async_err = PyErr_CheckSignals())); Py_DECREF(bytes); #endif + if (async_err) + return NULL; + if (f == NULL) { PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); return NULL; -- cgit v1.2.1 From 00471d560c71a73624b8983caf1707ed1610e84d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Mar 2015 22:53:20 +0100 Subject: Issue #23708: Add _Py_read() and _Py_write() functions to factorize code handle EINTR error and special cases for Windows. These functions now truncate the length to PY_SSIZE_T_MAX to have a portable and reliable behaviour. For example, read() result is undefined if counter is greater than PY_SSIZE_T_MAX on Linux. --- Python/fileutils.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index fa481ae772..0948781a62 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1120,6 +1120,155 @@ _Py_fopen_obj(PyObject *path, const char *mode) return f; } +/* Read count bytes from fd into buf. + * + * On success, return the number of read bytes, it can be lower than count. + * If the current file offset is at or past the end of file, no bytes are read, + * and read() returns zero. + * + * On error, raise an exception, set errno and return -1. + * + * When interrupted by a signal (read() fails with EINTR), retry the syscall. + * If the Python signal handler raises an exception, the function returns -1 + * (the syscall is not retried). + * + * The GIL must be held. */ +Py_ssize_t +_Py_read(int fd, void *buf, size_t count) +{ + Py_ssize_t n; + int async_err = 0; + + /* _Py_read() must not be called with an exception set, otherwise the + * caller may think that read() was interrupted by a signal and the signal + * handler raised an exception. */ + assert(!PyErr_Occurred()); + + if (!_PyVerify_fd(self->fd)) { + PyErr_SetFromErrno(PyExc_OSError); + assert(errno == EBADF); + return -1; + } + +#ifdef MS_WINDOWS + if (count > INT_MAX) { + /* On Windows, the count parameter of read() is an int */ + count = INT_MAX; + } +#else + if (count > PY_SSIZE_T_MAX) { + /* if count is greater than PY_SSIZE_T_MAX, + * read() result is undefined */ + count = PY_SSIZE_T_MAX; + } +#endif + + do { + Py_BEGIN_ALLOW_THREADS + errno = 0; +#ifdef MS_WINDOWS + n = read(fd, buf, (int)count); +#else + n = read(fd, buf, count); +#endif + Py_END_ALLOW_THREADS + } while (n < 0 && errno == EINTR && + !(async_err = PyErr_CheckSignals())); + + if (async_err) { + /* read() was interrupted by a signal (failed with EINTR) + * and the Python signal handler raised an exception */ + assert(errno == EINTR && PyErr_Occurred()); + return -1; + } + if (n < 0) { +#ifndef NDEBUG + int err = errno; +#endif + PyErr_SetFromErrno(PyExc_OSError); + assert(errno == err); + return -1; + } + + return n; +} + +/* Write count bytes of buf into fd. + * + * -On success, return the number of written bytes, it can be lower than count + * including 0 + * - On error, raise an exception, set errno and return -1. + * + * When interrupted by a signal (write() fails with EINTR), retry the syscall. + * If the Python signal handler raises an exception, the function returns -1 + * (the syscall is not retried). + * + * The GIL must be held. */ +Py_ssize_t +_Py_write(int fd, const void *buf, size_t count) +{ + Py_ssize_t n; + int async_err = 0; + + /* _Py_write() must not be called with an exception set, otherwise the + * caller may think that write() was interrupted by a signal and the signal + * handler raised an exception. */ + assert(!PyErr_Occurred()); + + if (!_PyVerify_fd(fd)) { + PyErr_SetFromErrno(PyExc_OSError); + assert(errno == EBADF); + return -1; + } + +#ifdef MS_WINDOWS + if (count > 32767 && isatty(fd)) { + /* Issue #11395: the Windows console returns an error (12: not + enough space error) on writing into stdout if stdout mode is + binary and the length is greater than 66,000 bytes (or less, + depending on heap usage). */ + count = 32767; + } + else if (count > INT_MAX) + count = INT_MAX; +#else + if (count > PY_SSIZE_T_MAX) { + /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer + * to do it ourself to have a portable behaviour. */ + count = PY_SSIZE_T_MAX; + } +#endif + + do { + Py_BEGIN_ALLOW_THREADS + errno = 0; +#ifdef MS_WINDOWS + n = write(fd, buf, (int)count); +#else + n = write(fd, buf, count); +#endif + Py_END_ALLOW_THREADS + } while (n < 0 && errno == EINTR && + !(async_err = PyErr_CheckSignals())); + + if (async_err) { + /* write() was interrupted by a signal (failed with EINTR) + * and the Python signal handler raised an exception */ + assert(errno == EINTR && PyErr_Occurred()); + return -1; + } + if (n < 0) { +#ifndef NDEBUG + int err = errno; +#endif + PyErr_SetFromErrno(PyExc_OSError); + assert(errno == err); + return -1; + } + + return n; +} + #ifdef HAVE_READLINK /* Read value of symbolic link. Encode the path to the locale encoding, decode -- cgit v1.2.1 From 3def5a0f2d8e2e37c5a257e05bef327d2407033d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Mar 2015 23:53:04 +0100 Subject: Issue #23708: Fix _Py_read() compilation error on Windows Fix typo: self->fd => fd --- Python/fileutils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 0948781a62..702e25df61 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1144,7 +1144,7 @@ _Py_read(int fd, void *buf, size_t count) * handler raised an exception. */ assert(!PyErr_Occurred()); - if (!_PyVerify_fd(self->fd)) { + if (!_PyVerify_fd(fd)) { PyErr_SetFromErrno(PyExc_OSError); assert(errno == EBADF); return -1; -- cgit v1.2.1 From 4172928875e061f5d12e7ae75ea493183f639a8b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 20 Mar 2015 11:21:41 +0100 Subject: Issue #23708: Split assertion expression in two assertions in _Py_read() and _Py_write() to know which test failed on the buildbot "AMD64 Snow Leop 3.x". --- Python/fileutils.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 702e25df61..cc6582c7aa 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1178,7 +1178,8 @@ _Py_read(int fd, void *buf, size_t count) if (async_err) { /* read() was interrupted by a signal (failed with EINTR) * and the Python signal handler raised an exception */ - assert(errno == EINTR && PyErr_Occurred()); + assert(errno == EINTR); + assert(PyErr_Occurred()); return -1; } if (n < 0) { @@ -1254,7 +1255,8 @@ _Py_write(int fd, const void *buf, size_t count) if (async_err) { /* write() was interrupted by a signal (failed with EINTR) * and the Python signal handler raised an exception */ - assert(errno == EINTR && PyErr_Occurred()); + assert(errno == EINTR); + assert(PyErr_Occurred()); return -1; } if (n < 0) { -- cgit v1.2.1 From 71140156b03fca7e8fd50f1164d4fbbd4df400f8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 20 Mar 2015 11:58:18 +0100 Subject: Issue #23708: Save/restore errno in _Py_read() and _Py_write() Save and then restore errno because PyErr_CheckSignals() and PyErr_SetFromErrno() can modify it. --- Python/fileutils.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index cc6582c7aa..e9c902b7f9 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1137,6 +1137,7 @@ Py_ssize_t _Py_read(int fd, void *buf, size_t count) { Py_ssize_t n; + int err; int async_err = 0; /* _Py_read() must not be called with an exception set, otherwise the @@ -1145,8 +1146,10 @@ _Py_read(int fd, void *buf, size_t count) assert(!PyErr_Occurred()); if (!_PyVerify_fd(fd)) { + /* save/restore errno because PyErr_SetFromErrno() can modify it */ + err = errno; PyErr_SetFromErrno(PyExc_OSError); - assert(errno == EBADF); + errno = err; return -1; } @@ -1171,23 +1174,23 @@ _Py_read(int fd, void *buf, size_t count) #else n = read(fd, buf, count); #endif + /* save/restore errno because PyErr_CheckSignals() + * and PyErr_SetFromErrno() can modify it */ + err = errno; Py_END_ALLOW_THREADS - } while (n < 0 && errno == EINTR && + } while (n < 0 && err == EINTR && !(async_err = PyErr_CheckSignals())); if (async_err) { /* read() was interrupted by a signal (failed with EINTR) * and the Python signal handler raised an exception */ - assert(errno == EINTR); - assert(PyErr_Occurred()); + errno = err; + assert(errno == EINTR && PyErr_Occurred()); return -1; } if (n < 0) { -#ifndef NDEBUG - int err = errno; -#endif PyErr_SetFromErrno(PyExc_OSError); - assert(errno == err); + errno = err; return -1; } @@ -1209,6 +1212,7 @@ Py_ssize_t _Py_write(int fd, const void *buf, size_t count) { Py_ssize_t n; + int err; int async_err = 0; /* _Py_write() must not be called with an exception set, otherwise the @@ -1217,8 +1221,10 @@ _Py_write(int fd, const void *buf, size_t count) assert(!PyErr_Occurred()); if (!_PyVerify_fd(fd)) { + /* save/restore errno because PyErr_SetFromErrno() can modify it */ + err = errno; PyErr_SetFromErrno(PyExc_OSError); - assert(errno == EBADF); + errno = err; return -1; } @@ -1248,6 +1254,9 @@ _Py_write(int fd, const void *buf, size_t count) #else n = write(fd, buf, count); #endif + /* save/restore errno because PyErr_CheckSignals() + * and PyErr_SetFromErrno() can modify it */ + err = errno; Py_END_ALLOW_THREADS } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); @@ -1255,16 +1264,13 @@ _Py_write(int fd, const void *buf, size_t count) if (async_err) { /* write() was interrupted by a signal (failed with EINTR) * and the Python signal handler raised an exception */ - assert(errno == EINTR); - assert(PyErr_Occurred()); + errno = err; + assert(errno == EINTR && PyErr_Occurred()); return -1; } if (n < 0) { -#ifndef NDEBUG - int err = errno; -#endif PyErr_SetFromErrno(PyExc_OSError); - assert(errno == err); + errno = err; return -1; } -- cgit v1.2.1 From eaf93d5dbe0a159d1d0908fab4f627934abbe6b1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 24 Mar 2015 10:27:50 +0100 Subject: Issue #23753: Python doesn't support anymore platforms without stat() or fstat(), these functions are always required. Remove HAVE_STAT and HAVE_FSTAT defines, and stop supporting DONT_HAVE_STAT and DONT_HAVE_FSTAT. --- Python/fileutils.c | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index e9c902b7f9..63c2571492 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -519,17 +519,8 @@ Py_EncodeLocale(const wchar_t *text, size_t *error_pos) #endif /* __APPLE__ */ } -/* In principle, this should use HAVE__WSTAT, and _wstat - should be detected by autoconf. However, no current - POSIX system provides that function, so testing for - it is pointless. - Not sure whether the MS_WINDOWS guards are necessary: - perhaps for cygwin/mingw builds? -*/ -#if defined(HAVE_STAT) && !defined(MS_WINDOWS) /* Get file status. Encode the path to the locale encoding. */ - int _Py_wstat(const wchar_t* path, struct stat *buf) { @@ -544,11 +535,8 @@ _Py_wstat(const wchar_t* path, struct stat *buf) PyMem_Free(fname); return err; } -#endif -#if defined(HAVE_FSTAT) || defined(MS_WINDOWS) - #ifdef MS_WINDOWS static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ @@ -679,10 +667,8 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) return fstat(fd, result); #endif } -#endif /* HAVE_FSTAT || MS_WINDOWS */ -#ifdef HAVE_STAT /* Call _wstat() on Windows, or encode the path to the filesystem encoding and call stat() otherwise. Only fill st_mode attribute on Windows. @@ -715,8 +701,6 @@ _Py_stat(PyObject *path, struct stat *statbuf) #endif } -#endif /* HAVE_STAT */ - static int get_inheritable(int fd, int raise) -- cgit v1.2.1 From 7f8ff41ec0ff818773f2fa5d01c21dc5cdb9d19a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 24 Mar 2015 12:16:28 +0100 Subject: Issue #23753: Move _Py_wstat() from Python/fileutils.c to Modules/getpath.c I expected more users of _Py_wstat(), but in practice it's only used by Modules/getpath.c. Move the function because it's not needed on Windows. Windows uses PC/getpathp.c which uses the Win32 API (ex: GetFileAttributesW()) not the POSIX API. --- Python/fileutils.c | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 63c2571492..e6d3154490 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -520,23 +520,6 @@ Py_EncodeLocale(const wchar_t *text, size_t *error_pos) } -/* Get file status. Encode the path to the locale encoding. */ -int -_Py_wstat(const wchar_t* path, struct stat *buf) -{ - int err; - char *fname; - fname = Py_EncodeLocale(path, NULL); - if (fname == NULL) { - errno = EINVAL; - return -1; - } - err = stat(fname, buf); - PyMem_Free(fname); - return err; -} - - #ifdef MS_WINDOWS static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ -- cgit v1.2.1 From 0600ebc4d9a0fbb0f16689fb2c1d6a09482594cc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 30 Mar 2015 10:09:31 +0200 Subject: Issue #23752: _Py_fstat() is now responsible to raise the Python exception Add _Py_fstat_noraise() function when a Python exception is not welcome. --- Python/fileutils.c | 58 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index e6d3154490..daaad2a3d5 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -565,7 +565,8 @@ attributes_to_mode(DWORD attr) } void -_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) +_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, + struct _Py_stat_struct *result) { memset(result, 0, sizeof(*result)); result->st_mode = attributes_to_mode(info->dwFileAttributes); @@ -595,9 +596,12 @@ _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger than 2 GB because the file size type is an signed 32-bit integer: see issue #23152. - */ + + On Windows, set the last Windows error and return nonzero on error. On + POSIX, set errno and return nonzero on error. Fill status and return 0 on + success. */ int -_Py_fstat(int fd, struct _Py_stat_struct *result) +_Py_fstat_noraise(int fd, struct _Py_stat_struct *status) { #ifdef MS_WINDOWS BY_HANDLE_FILE_INFORMATION info; @@ -619,22 +623,21 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) SetLastError(ERROR_INVALID_HANDLE); return -1; } - memset(result, 0, sizeof(*result)); + memset(status, 0, sizeof(*status)); type = GetFileType(h); if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); - if (error != 0) { + if (error != 0) return -1; - } /* else: valid but unknown file */ } if (type != FILE_TYPE_DISK) { if (type == FILE_TYPE_CHAR) - result->st_mode = _S_IFCHR; + status->st_mode = _S_IFCHR; else if (type == FILE_TYPE_PIPE) - result->st_mode = _S_IFIFO; + status->st_mode = _S_IFIFO; return 0; } @@ -642,15 +645,48 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) return -1; } - _Py_attribute_data_to_stat(&info, 0, result); + _Py_attribute_data_to_stat(&info, 0, status); /* specific to fstat() */ - result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; + status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; return 0; #else - return fstat(fd, result); + return fstat(fd, status); #endif } +/* Return information about a file. + + On POSIX, use fstat(). + + On Windows, use GetFileType() and GetFileInformationByHandle() which support + files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger + than 2 GB because the file size type is an signed 32-bit integer: see issue + #23152. + + Raise an exception and return -1 on error. On Windows, set the last Windows + error on error. On POSIX, set errno on error. Fill status and return 0 on + success. + + The GIL must be held. */ +int +_Py_fstat(int fd, struct _Py_stat_struct *status) +{ + int res; + + Py_BEGIN_ALLOW_THREADS + res = _Py_fstat_noraise(fd, status); + Py_END_ALLOW_THREADS + + if (res != 0) { +#ifdef MS_WINDOWS + PyErr_SetFromWindowsErr(0); +#else + PyErr_SetFromErrno(PyExc_OSError); +#endif + return -1; + } + return 0; +} /* Call _wstat() on Windows, or encode the path to the filesystem encoding and call stat() otherwise. Only fill st_mode attribute on Windows. -- cgit v1.2.1 From 97e2c69c5ad505aa26bb31aa586e39961792187c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 1 Apr 2015 18:34:45 +0200 Subject: Issue #23836: Add _Py_write_noraise() function Helper to write() which retries write() if it is interrupted by a signal (fails with EINTR). --- Python/fileutils.c | 131 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 83 insertions(+), 48 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index daaad2a3d5..e4c524184b 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -1124,18 +1124,18 @@ _Py_fopen_obj(PyObject *path, const char *mode) } /* Read count bytes from fd into buf. - * - * On success, return the number of read bytes, it can be lower than count. - * If the current file offset is at or past the end of file, no bytes are read, - * and read() returns zero. - * - * On error, raise an exception, set errno and return -1. - * - * When interrupted by a signal (read() fails with EINTR), retry the syscall. - * If the Python signal handler raises an exception, the function returns -1 - * (the syscall is not retried). - * - * The GIL must be held. */ + + On success, return the number of read bytes, it can be lower than count. + If the current file offset is at or past the end of file, no bytes are read, + and read() returns zero. + + On error, raise an exception, set errno and return -1. + + When interrupted by a signal (read() fails with EINTR), retry the syscall. + If the Python signal handler raises an exception, the function returns -1 + (the syscall is not retried). + + Release the GIL to call read(). The caller must hold the GIL. */ Py_ssize_t _Py_read(int fd, void *buf, size_t count) { @@ -1200,34 +1200,20 @@ _Py_read(int fd, void *buf, size_t count) return n; } -/* Write count bytes of buf into fd. - * - * -On success, return the number of written bytes, it can be lower than count - * including 0 - * - On error, raise an exception, set errno and return -1. - * - * When interrupted by a signal (write() fails with EINTR), retry the syscall. - * If the Python signal handler raises an exception, the function returns -1 - * (the syscall is not retried). - * - * The GIL must be held. */ -Py_ssize_t -_Py_write(int fd, const void *buf, size_t count) +static Py_ssize_t +_Py_write_impl(int fd, const void *buf, size_t count, int gil_held) { Py_ssize_t n; int err; int async_err = 0; - /* _Py_write() must not be called with an exception set, otherwise the - * caller may think that write() was interrupted by a signal and the signal - * handler raised an exception. */ - assert(!PyErr_Occurred()); - if (!_PyVerify_fd(fd)) { - /* save/restore errno because PyErr_SetFromErrno() can modify it */ - err = errno; - PyErr_SetFromErrno(PyExc_OSError); - errno = err; + if (gil_held) { + /* save/restore errno because PyErr_SetFromErrno() can modify it */ + err = errno; + PyErr_SetFromErrno(PyExc_OSError); + errno = err; + } return -1; } @@ -1249,30 +1235,45 @@ _Py_write(int fd, const void *buf, size_t count) } #endif - do { - Py_BEGIN_ALLOW_THREADS - errno = 0; + if (gil_held) { + do { + Py_BEGIN_ALLOW_THREADS + errno = 0; #ifdef MS_WINDOWS - n = write(fd, buf, (int)count); + n = write(fd, buf, (int)count); #else - n = write(fd, buf, count); + n = write(fd, buf, count); #endif - /* save/restore errno because PyErr_CheckSignals() - * and PyErr_SetFromErrno() can modify it */ - err = errno; - Py_END_ALLOW_THREADS - } while (n < 0 && errno == EINTR && - !(async_err = PyErr_CheckSignals())); + /* save/restore errno because PyErr_CheckSignals() + * and PyErr_SetFromErrno() can modify it */ + err = errno; + Py_END_ALLOW_THREADS + } while (n < 0 && err == EINTR && + !(async_err = PyErr_CheckSignals())); + } + else { + do { + errno = 0; +#ifdef MS_WINDOWS + n = write(fd, buf, (int)count); +#else + n = write(fd, buf, count); +#endif + err = errno; + } while (n < 0 && err == EINTR); + } if (async_err) { /* write() was interrupted by a signal (failed with EINTR) - * and the Python signal handler raised an exception */ + and the Python signal handler raised an exception (if gil_held is + nonzero). */ errno = err; - assert(errno == EINTR && PyErr_Occurred()); + assert(errno == EINTR && (!gil_held || PyErr_Occurred())); return -1; } if (n < 0) { - PyErr_SetFromErrno(PyExc_OSError); + if (gil_held) + PyErr_SetFromErrno(PyExc_OSError); errno = err; return -1; } @@ -1280,6 +1281,40 @@ _Py_write(int fd, const void *buf, size_t count) return n; } +/* Write count bytes of buf into fd. + + On success, return the number of written bytes, it can be lower than count + including 0. On error, raise an exception, set errno and return -1. + + When interrupted by a signal (write() fails with EINTR), retry the syscall. + If the Python signal handler raises an exception, the function returns -1 + (the syscall is not retried). + + Release the GIL to call write(). The caller must hold the GIL. */ +Py_ssize_t +_Py_write(int fd, const void *buf, size_t count) +{ + /* _Py_write() must not be called with an exception set, otherwise the + * caller may think that write() was interrupted by a signal and the signal + * handler raised an exception. */ + assert(!PyErr_Occurred()); + + return _Py_write_impl(fd, buf, count, 1); +} + +/* Write count bytes of buf into fd. + * + * On success, return the number of written bytes, it can be lower than count + * including 0. On error, set errno and return -1. + * + * When interrupted by a signal (write() fails with EINTR), retry the syscall + * without calling the Python signal handler. */ +Py_ssize_t +_Py_write_noraise(int fd, const void *buf, size_t count) +{ + return _Py_write_impl(fd, buf, count, 0); +} + #ifdef HAVE_READLINK /* Read value of symbolic link. Encode the path to the locale encoding, decode -- cgit v1.2.1 From a3341153748f96d87959c3f2b726c3a73d6aa5a4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 1 Apr 2015 18:34:32 +0200 Subject: Issue #23836: Document functions releasing the GIL in fileutils.c --- Python/fileutils.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index e4c524184b..64368f356f 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -667,7 +667,8 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) error on error. On POSIX, set errno on error. Fill status and return 0 on success. - The GIL must be held. */ + Release the GIL to call GetFileType() and GetFileInformationByHandle(), or + to call fstat(). The caller must hold the GIL. */ int _Py_fstat(int fd, struct _Py_stat_struct *status) { @@ -968,7 +969,7 @@ _Py_open_impl(const char *pathname, int flags, int gil_held) When interrupted by a signal (open() fails with EINTR), retry the syscall, except if the Python signal handler raises an exception. - The GIL must be held. */ + Release the GIL to call open(). The caller must hold the GIL. */ int _Py_open(const char *pathname, int flags) { @@ -1054,7 +1055,8 @@ _Py_fopen(const char *pathname, const char *mode) When interrupted by a signal (open() fails with EINTR), retry the syscall, except if the Python signal handler raises an exception. - The GIL must be held. */ + Release the GIL to call _wfopen() or fopen(). The caller must hold + the GIL. */ FILE* _Py_fopen_obj(PyObject *path, const char *mode) { -- cgit v1.2.1 From e07a52f272f00b6707b40f557e481749b73dd0fd Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Sun, 12 Apr 2015 00:26:27 -0400 Subject: Issue #23524: Replace _PyVerify_fd function with calls to _set_thread_local_invalid_parameter_handler. --- Python/fileutils.c | 95 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 39 deletions(-) (limited to 'Python/fileutils.c') diff --git a/Python/fileutils.c b/Python/fileutils.c index 64368f356f..bccd32145c 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -5,6 +5,7 @@ #ifdef MS_WINDOWS # include # include +extern int winerror_to_errno(int); #endif #ifdef HAVE_LANGINFO_H @@ -41,9 +42,13 @@ _Py_device_encoding(int fd) #if defined(MS_WINDOWS) UINT cp; #endif - if (!_PyVerify_fd(fd) || !isatty(fd)) { + int valid; + _Py_BEGIN_SUPPRESS_IPH + valid = _PyVerify_fd(fd) && isatty(fd); + _Py_END_SUPPRESS_IPH + if (!valid) Py_RETURN_NONE; - } + #if defined(MS_WINDOWS) if (fd == 0) cp = GetConsoleCP(); @@ -610,16 +615,15 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) if (!_PyVerify_fd(fd)) h = INVALID_HANDLE_VALUE; - else + else { + _Py_BEGIN_SUPPRESS_IPH h = (HANDLE)_get_osfhandle(fd); - - /* Protocol violation: we explicitly clear errno, instead of - setting it to a POSIX error. Callers should use GetLastError. */ - errno = 0; + _Py_END_SUPPRESS_IPH + } if (h == INVALID_HANDLE_VALUE) { - /* This is really a C library error (invalid file handle). - We set the Win32 error to the closes one matching. */ + /* errno is already set by _get_osfhandle, but we also set + the Win32 error for callers who expect that */ SetLastError(ERROR_INVALID_HANDLE); return -1; } @@ -628,8 +632,10 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) type = GetFileType(h); if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); - if (error != 0) + if (error != 0) { + errno = winerror_to_errno(error); return -1; + } /* else: valid but unknown file */ } @@ -642,6 +648,9 @@ _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) } if (!GetFileInformationByHandle(h, &info)) { + /* The Win32 error is already set, but we also set errno for + callers who expect it */ + errno = winerror_to_errno(GetLastError()); return -1; } @@ -735,7 +744,9 @@ get_inheritable(int fd, int raise) return -1; } + _Py_BEGIN_SUPPRESS_IPH handle = (HANDLE)_get_osfhandle(fd); + _Py_END_SUPPRESS_IPH if (handle == INVALID_HANDLE_VALUE) { if (raise) PyErr_SetFromErrno(PyExc_OSError); @@ -810,7 +821,9 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) return -1; } + _Py_BEGIN_SUPPRESS_IPH handle = (HANDLE)_get_osfhandle(fd); + _Py_END_SUPPRESS_IPH if (handle == INVALID_HANDLE_VALUE) { if (raise) PyErr_SetFromErrno(PyExc_OSError); @@ -1171,6 +1184,7 @@ _Py_read(int fd, void *buf, size_t count) } #endif + _Py_BEGIN_SUPPRESS_IPH do { Py_BEGIN_ALLOW_THREADS errno = 0; @@ -1185,6 +1199,7 @@ _Py_read(int fd, void *buf, size_t count) Py_END_ALLOW_THREADS } while (n < 0 && err == EINTR && !(async_err = PyErr_CheckSignals())); + _Py_END_SUPPRESS_IPH if (async_err) { /* read() was interrupted by a signal (failed with EINTR) @@ -1219,6 +1234,7 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) return -1; } + _Py_BEGIN_SUPPRESS_IPH #ifdef MS_WINDOWS if (count > 32767 && isatty(fd)) { /* Issue #11395: the Windows console returns an error (12: not @@ -1264,6 +1280,7 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) err = errno; } while (n < 0 && err == EINTR); } + _Py_END_SUPPRESS_IPH if (async_err) { /* write() was interrupted by a signal (failed with EINTR) @@ -1451,7 +1468,9 @@ _Py_dup(int fd) } #ifdef MS_WINDOWS + _Py_BEGIN_SUPPRESS_IPH handle = (HANDLE)_get_osfhandle(fd); + _Py_END_SUPPRESS_IPH if (handle == INVALID_HANDLE_VALUE) { PyErr_SetFromErrno(PyExc_OSError); return -1; @@ -1461,7 +1480,9 @@ _Py_dup(int fd) ftype = GetFileType(handle); Py_BEGIN_ALLOW_THREADS + _Py_BEGIN_SUPPRESS_IPH fd = dup(fd); + _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS if (fd < 0) { PyErr_SetFromErrno(PyExc_OSError); @@ -1471,13 +1492,17 @@ _Py_dup(int fd) /* Character files like console cannot be make non-inheritable */ if (ftype != FILE_TYPE_CHAR) { if (_Py_set_inheritable(fd, 0, NULL) < 0) { + _Py_BEGIN_SUPPRESS_IPH close(fd); + _Py_END_SUPPRESS_IPH return -1; } } #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) Py_BEGIN_ALLOW_THREADS + _Py_BEGIN_SUPPRESS_IPH fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); + _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS if (fd < 0) { PyErr_SetFromErrno(PyExc_OSError); @@ -1486,7 +1511,9 @@ _Py_dup(int fd) #else Py_BEGIN_ALLOW_THREADS + _Py_BEGIN_SUPPRESS_IPH fd = dup(fd); + _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS if (fd < 0) { PyErr_SetFromErrno(PyExc_OSError); @@ -1494,7 +1521,9 @@ _Py_dup(int fd) } if (_Py_set_inheritable(fd, 0, NULL) < 0) { + _Py_BEGIN_SUPPRESS_IPH close(fd); + _Py_END_SUPPRESS_IPH return -1; } #endif @@ -1508,7 +1537,10 @@ _Py_dup(int fd) int _Py_get_blocking(int fd) { - int flags = fcntl(fd, F_GETFL, 0); + int flags; + _Py_BEGIN_SUPPRESS_IPH + flags = fcntl(fd, F_GETFL, 0); + _Py_END_SUPPRESS_IPH if (flags < 0) { PyErr_SetFromErrno(PyExc_OSError); return -1; @@ -1533,16 +1565,20 @@ _Py_set_blocking(int fd, int blocking) #else int flags, res; + _Py_BEGIN_SUPPRESS_IPH flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) - goto error; + if (flags >= 0) { + if (blocking) + flags = flags & (~O_NONBLOCK); + else + flags = flags | O_NONBLOCK; - if (blocking) - flags = flags & (~O_NONBLOCK); - else - flags = flags | O_NONBLOCK; + res = fcntl(fd, F_SETFL, flags); + } else { + res = -1; + } + _Py_END_SUPPRESS_IPH - res = fcntl(fd, F_SETFL, flags); if (res < 0) goto error; #endif @@ -1554,25 +1590,7 @@ error: } #endif -#ifdef _MSC_VER -#if _MSC_VER >= 1900 - -/* This function lets the Windows CRT validate the file handle without - terminating the process if it's invalid. */ -int -_PyVerify_fd(int fd) -{ - intptr_t osh; - /* Fast check for the only condition we know */ - if (fd < 0) { - _set_errno(EBADF); - return 0; - } - osh = _get_osfhandle(fd); - return osh != (intptr_t)-1; -} - -#elif _MSC_VER >= 1400 +#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 /* Legacy implementation of _PyVerify_fd while transitioning to * MSVC 14.0. This should eventually be removed. (issue23524) */ @@ -1651,5 +1669,4 @@ _PyVerify_fd(int fd) return 0; } -#endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */ -#endif /* defined _MSC_VER */ +#endif /* defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 */ -- cgit v1.2.1