| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | #include "pycore_fileutils.h"     // fileutils definitions
 | 
					
						
							|  |  |  | #include "pycore_runtime.h"       // _PyRuntime
 | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  | #include "pycore_pystate.h"       // _Py_AssertHoldsTstate()
 | 
					
						
							| 
									
										
										
										
											2020-04-15 03:24:57 +02:00
										 |  |  | #include "osdefs.h"               // SEP
 | 
					
						
							| 
									
										
										
										
											2023-09-30 22:06:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 19:25:53 +02:00
										 |  |  | #include <stdlib.h>               // mbstowcs()
 | 
					
						
							| 
									
										
										
										
											2023-09-30 22:06:45 +02:00
										 |  |  | #ifdef HAVE_UNISTD_H
 | 
					
						
							|  |  |  | #  include <unistd.h>             // getcwd()
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-01-20 15:31:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:09:40 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2015-03-06 14:47:02 -08:00
										 |  |  | #  include <malloc.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:09:40 +00:00
										 |  |  | #  include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  | #  include <winioctl.h>             // FILE_DEVICE_* constants
 | 
					
						
							|  |  |  | #  include "pycore_fileutils_windows.h" // FILE_STAT_BASIC_INFORMATION
 | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | #  if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
 | 
					
						
							|  |  |  | #    define PATHCCH_ALLOW_LONG_PATHS 0x01
 | 
					
						
							|  |  |  | #  else
 | 
					
						
							|  |  |  | #    include <pathcch.h>            // PathCchCombineEx
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  | extern int winerror_to_errno(int); | 
					
						
							| 
									
										
										
										
											2010-10-07 22:09:40 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  | #ifdef HAVE_LANGINFO_H
 | 
					
						
							| 
									
										
										
										
											2023-09-30 22:06:45 +02:00
										 |  |  | #  include <langinfo.h>           // nl_langinfo(CODESET)
 | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | #ifdef HAVE_SYS_IOCTL_H
 | 
					
						
							|  |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 15:21:42 +02:00
										 |  |  | #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
 | 
					
						
							| 
									
										
										
										
											2023-09-30 22:06:45 +02:00
										 |  |  | #  include <iconv.h>              // iconv_open()
 | 
					
						
							| 
									
										
										
										
											2021-04-30 15:21:42 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | #ifdef HAVE_FCNTL_H
 | 
					
						
							| 
									
										
										
										
											2023-09-30 22:06:45 +02:00
										 |  |  | #  include <fcntl.h>              // fcntl(F_GETFD)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef O_CLOEXEC
 | 
					
						
							| 
									
										
										
										
											2013-09-07 10:36:04 +02:00
										 |  |  | /* Does open() support the O_CLOEXEC flag? Possible values:
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    -1: unknown | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |    The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO | 
					
						
							|  |  |  |    and os.open(). */ | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | int _Py_open_cloexec_works = -1; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  | // The value must be the same in unicodeobject.c.
 | 
					
						
							|  |  |  | #define MAX_UNICODE 0x10ffff
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mbstowcs() and mbrtowc() errors
 | 
					
						
							|  |  |  | static const size_t DECODE_ERROR = ((size_t)-1); | 
					
						
							|  |  |  | static const size_t INCOMPLETE_CHARACTER = (size_t)-2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | get_surrogateescape(_Py_error_handler errors, int *surrogateescape) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (errors) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     case _Py_ERROR_STRICT: | 
					
						
							|  |  |  |         *surrogateescape = 0; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     case _Py_ERROR_SURROGATEESCAPE: | 
					
						
							|  |  |  |         *surrogateescape = 1; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | _Py_device_encoding(int fd) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     int valid; | 
					
						
							| 
									
										
										
										
											2021-09-09 15:12:03 +02:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2016-09-08 11:21:54 -07:00
										 |  |  |     valid = isatty(fd); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2021-09-09 15:12:03 +02:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     if (!valid) | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | #ifdef HAVE_WINDOWS_CONSOLE_IO
 | 
					
						
							| 
									
										
										
										
											2020-11-04 11:20:10 +01:00
										 |  |  |     UINT cp; | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  |     if (fd == 0) | 
					
						
							|  |  |  |         cp = GetConsoleCP(); | 
					
						
							|  |  |  |     else if (fd == 1 || fd == 2) | 
					
						
							|  |  |  |         cp = GetConsoleOutputCP(); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         cp = 0; | 
					
						
							|  |  |  |     /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
 | 
					
						
							|  |  |  |        has no console */ | 
					
						
							| 
									
										
										
										
											2020-11-04 11:20:10 +01:00
										 |  |  |     if (cp == 0) { | 
					
						
							|  |  |  |         Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-04 11:20:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return PyUnicode_FromFormat("cp%u", (unsigned int)cp); | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | #endif /* HAVE_WINDOWS_CONSOLE_IO */
 | 
					
						
							| 
									
										
										
										
											2020-11-04 11:20:10 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  |     if (_PyRuntime.preconfig.utf8_mode) { | 
					
						
							|  |  |  |         _Py_DECLARE_STR(utf_8, "utf-8"); | 
					
						
							| 
									
										
										
										
											2023-08-04 16:24:50 -07:00
										 |  |  |         return &_Py_STR(utf_8); | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-04 11:20:10 +01:00
										 |  |  |     return _Py_GetLocaleEncodingObject(); | 
					
						
							| 
									
										
										
										
											2012-02-29 18:31:31 -05:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 18:36:20 +02:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  | is_valid_wide_char(wchar_t ch) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-30 15:21:42 +02:00
										 |  |  | #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
 | 
					
						
							|  |  |  |     /* Oracle Solaris doesn't use Unicode code points as wchar_t encoding
 | 
					
						
							|  |  |  |        for non-Unicode locales, which makes values higher than MAX_UNICODE | 
					
						
							|  |  |  |        possibly valid. */ | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |     if (Py_UNICODE_IS_SURROGATE(ch)) { | 
					
						
							|  |  |  |         // Reject lone surrogate characters
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ch > MAX_UNICODE) { | 
					
						
							|  |  |  |         // bpo-35883: Reject characters outside [U+0000; U+10ffff] range.
 | 
					
						
							|  |  |  |         // The glibc mbstowcs() UTF-8 decoder does not respect the RFC 3629,
 | 
					
						
							|  |  |  |         // it creates characters outside the [U+0000; U+10ffff] range:
 | 
					
						
							|  |  |  |         // https://sourceware.org/bugzilla/show_bug.cgi?id=2373
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static size_t | 
					
						
							|  |  |  | _Py_mbstowcs(wchar_t *dest, const char *src, size_t n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     size_t count = mbstowcs(dest, src, n); | 
					
						
							|  |  |  |     if (dest != NULL && count != DECODE_ERROR) { | 
					
						
							|  |  |  |         for (size_t i=0; i < count; i++) { | 
					
						
							|  |  |  |             wchar_t ch = dest[i]; | 
					
						
							|  |  |  |             if (!is_valid_wide_char(ch)) { | 
					
						
							|  |  |  |                 return DECODE_ERROR; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_MBRTOWC
 | 
					
						
							|  |  |  | static size_t | 
					
						
							|  |  |  | _Py_mbrtowc(wchar_t *pwc, const char *str, size_t len, mbstate_t *pmbs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(pwc != NULL); | 
					
						
							|  |  |  |     size_t count = mbrtowc(pwc, str, len, pmbs); | 
					
						
							|  |  |  |     if (count != 0 && count != DECODE_ERROR && count != INCOMPLETE_CHARACTER) { | 
					
						
							|  |  |  |         if (!is_valid_wide_char(*pwc)) { | 
					
						
							|  |  |  |             return DECODE_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS)
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define USE_FORCE_ASCII
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | extern int _Py_normalize_encoding(const char *, char *, size_t); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale
 | 
					
						
							|  |  |  |    and POSIX locale. nl_langinfo(CODESET) announces an alias of the | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |    ASCII encoding, whereas mbstowcs() and wcstombs() functions use the | 
					
						
							|  |  |  |    ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use | 
					
						
							|  |  |  |    locale.getpreferredencoding() codec. For example, if command line arguments | 
					
						
							|  |  |  |    are decoded by mbstowcs() and encoded back by os.fsencode(), we get a | 
					
						
							|  |  |  |    UnicodeEncodeError instead of retrieving the original byte string. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", | 
					
						
							|  |  |  |    nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least | 
					
						
							|  |  |  |    one byte in range 0x80-0xff can be decoded from the locale encoding. The | 
					
						
							|  |  |  |    workaround is also enabled on error, for example if getting the locale | 
					
						
							|  |  |  |    failed. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |    On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET) | 
					
						
							|  |  |  |    announces "roman8" but mbstowcs() uses Latin1 in practice. Force also the | 
					
						
							|  |  |  |    ASCII encoding in this case. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-15 13:24:12 -08:00
										 |  |  |    Values of force_ascii: | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-01 12:28:48 +02:00
										 |  |  |        1: the workaround is used: Py_EncodeLocale() uses | 
					
						
							|  |  |  |           encode_ascii_surrogateescape() and Py_DecodeLocale() uses | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |           decode_ascii() | 
					
						
							| 
									
										
										
										
											2014-08-01 12:28:48 +02:00
										 |  |  |        0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and | 
					
						
							|  |  |  |           Py_DecodeLocale() uses mbstowcs() | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |       -1: unknown, need to call check_force_ascii() to get the value | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2022-11-16 09:37:14 -07:00
										 |  |  | #define force_ascii (_PyRuntime.fileutils.force_ascii)
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | check_force_ascii(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |     char *loc = setlocale(LC_CTYPE, NULL); | 
					
						
							|  |  |  |     if (loc == NULL) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) { | 
					
						
							|  |  |  |         /* the LC_CTYPE locale is different than C and POSIX */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | #if defined(HAVE_LANGINFO_H) && defined(CODESET)
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |     const char *codeset = nl_langinfo(CODESET); | 
					
						
							|  |  |  |     if (!codeset || codeset[0] == '\0') { | 
					
						
							|  |  |  |         /* CODESET is not set or empty */ | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 23:11:52 -07:00
										 |  |  |     char encoding[20];   /* longest name: "iso_646.irv_1991\0" */ | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |     if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __hpux
 | 
					
						
							|  |  |  |     if (strcmp(encoding, "roman8") == 0) { | 
					
						
							|  |  |  |         unsigned char ch; | 
					
						
							|  |  |  |         wchar_t wch; | 
					
						
							|  |  |  |         size_t res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ch = (unsigned char)0xA7; | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         res = _Py_mbstowcs(&wch, (char*)&ch, 1); | 
					
						
							|  |  |  |         if (res != DECODE_ERROR && wch == L'\xA7') { | 
					
						
							| 
									
										
										
										
											2021-10-07 00:55:27 +02:00
										 |  |  |             /* On HP-UX with C locale or the POSIX locale,
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |                nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() uses | 
					
						
							|  |  |  |                Latin1 encoding in practice. Force ASCII in this case. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */ | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     const char* ascii_aliases[] = { | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         "ascii", | 
					
						
							| 
									
										
										
										
											2016-09-09 23:11:52 -07:00
										 |  |  |         /* Aliases from Lib/encodings/aliases.py */ | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         "646", | 
					
						
							| 
									
										
										
										
											2016-09-09 23:11:52 -07:00
										 |  |  |         "ansi_x3.4_1968", | 
					
						
							|  |  |  |         "ansi_x3.4_1986", | 
					
						
							|  |  |  |         "ansi_x3_4_1968", | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         "cp367", | 
					
						
							|  |  |  |         "csascii", | 
					
						
							|  |  |  |         "ibm367", | 
					
						
							| 
									
										
										
										
											2016-09-09 23:11:52 -07:00
										 |  |  |         "iso646_us", | 
					
						
							|  |  |  |         "iso_646.irv_1991", | 
					
						
							|  |  |  |         "iso_ir_6", | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         "us", | 
					
						
							| 
									
										
										
										
											2016-09-09 23:11:52 -07:00
										 |  |  |         "us_ascii", | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         NULL | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |     int is_ascii = 0; | 
					
						
							|  |  |  |     for (const char **alias=ascii_aliases; *alias != NULL; alias++) { | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         if (strcmp(encoding, *alias) == 0) { | 
					
						
							|  |  |  |             is_ascii = 1; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!is_ascii) { | 
					
						
							|  |  |  |         /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |     for (unsigned int i=0x80; i<=0xff; i++) { | 
					
						
							|  |  |  |         char ch[1]; | 
					
						
							|  |  |  |         wchar_t wch[1]; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         size_t res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  |         unsigned uch = (unsigned char)i; | 
					
						
							|  |  |  |         ch[0] = (char)uch; | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         res = _Py_mbstowcs(wch, ch, 1); | 
					
						
							|  |  |  |         if (res != DECODE_ERROR) { | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |             /* decoding a non-ASCII character from the locale encoding succeed:
 | 
					
						
							|  |  |  |                the locale encoding is not ASCII, force ASCII */ | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* None of the bytes in the range 0x80-0xff can be decoded from the locale
 | 
					
						
							|  |  |  |        encoding: the locale encoding is really ASCII */ | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  | #endif   /* !defined(__hpux) */
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     /* nl_langinfo(CODESET) is not available: always force ASCII */ | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  | #endif   /* defined(HAVE_LANGINFO_H) && defined(CODESET) */
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							| 
									
										
										
										
											2016-05-26 05:35:26 +00:00
										 |  |  |     /* if an error occurred, force the ASCII encoding */ | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_GetForceASCII(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (force_ascii == -1) { | 
					
						
							|  |  |  |         force_ascii = check_force_ascii(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return force_ascii; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 13:08:26 +01:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_ResetForceASCII(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     force_ascii = -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | encode_ascii(const wchar_t *text, char **str, | 
					
						
							|  |  |  |              size_t *error_pos, const char **reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |              int raw_malloc, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char *result = NULL, *out; | 
					
						
							|  |  |  |     size_t len, i; | 
					
						
							|  |  |  |     wchar_t ch; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     int surrogateescape; | 
					
						
							|  |  |  |     if (get_surrogateescape(errors, &surrogateescape) < 0) { | 
					
						
							|  |  |  |         return -3; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     len = wcslen(text); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 16:49:13 +01:00
										 |  |  |     /* +1 for NULL byte */ | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     if (raw_malloc) { | 
					
						
							|  |  |  |         result = PyMem_RawMalloc(len + 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         result = PyMem_Malloc(len + 1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (result == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     out = result; | 
					
						
							|  |  |  |     for (i=0; i<len; i++) { | 
					
						
							|  |  |  |         ch = text[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ch <= 0x7f) { | 
					
						
							|  |  |  |             /* ASCII character */ | 
					
						
							|  |  |  |             *out++ = (char)ch; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) { | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |             /* UTF-8b surrogate */ | 
					
						
							|  |  |  |             *out++ = (char)(ch - 0xdc00); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |             if (raw_malloc) { | 
					
						
							|  |  |  |                 PyMem_RawFree(result); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 PyMem_Free(result); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |             if (error_pos != NULL) { | 
					
						
							|  |  |  |                 *error_pos = i; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (reason) { | 
					
						
							|  |  |  |                 *reason = "encoding error"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return -2; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *out = '\0'; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     *str = result; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-28 17:27:36 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_GetForceASCII(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-23 13:08:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_ResetForceASCII(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* nothing to do */ | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #endif   /* !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS) */
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |              const char **reason, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     wchar_t *res; | 
					
						
							|  |  |  |     unsigned char *in; | 
					
						
							|  |  |  |     wchar_t *out; | 
					
						
							| 
									
										
										
										
											2015-01-04 16:03:17 -06:00
										 |  |  |     size_t argsize = strlen(arg) + 1; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     int surrogateescape; | 
					
						
							|  |  |  |     if (get_surrogateescape(errors, &surrogateescape) < 0) { | 
					
						
							|  |  |  |         return -3; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     res = PyMem_RawMalloc(argsize * sizeof(wchar_t)); | 
					
						
							|  |  |  |     if (!res) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     out = res; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     for (in = (unsigned char*)arg; *in; in++) { | 
					
						
							|  |  |  |         unsigned char ch = *in; | 
					
						
							|  |  |  |         if (ch < 128) { | 
					
						
							|  |  |  |             *out++ = ch; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             if (!surrogateescape) { | 
					
						
							|  |  |  |                 PyMem_RawFree(res); | 
					
						
							|  |  |  |                 if (wlen) { | 
					
						
							|  |  |  |                     *wlen = in - (unsigned char*)arg; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (reason) { | 
					
						
							|  |  |  |                     *reason = "decoding error"; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return -2; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             *out++ = 0xdc00 + ch; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     *out = 0; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (wlen != NULL) { | 
					
						
							|  |  |  |         *wlen = out - res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *wstr = res; | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | #endif   /* !HAVE_MBRTOWC */
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                       const char **reason, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     wchar_t *res; | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     size_t argsize; | 
					
						
							|  |  |  |     size_t count; | 
					
						
							| 
									
										
										
										
											2013-05-07 23:48:56 +02:00
										 |  |  | #ifdef HAVE_MBRTOWC
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     unsigned char *in; | 
					
						
							|  |  |  |     wchar_t *out; | 
					
						
							|  |  |  |     mbstate_t mbs; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     int surrogateescape; | 
					
						
							|  |  |  |     if (get_surrogateescape(errors, &surrogateescape) < 0) { | 
					
						
							|  |  |  |         return -3; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #ifdef HAVE_BROKEN_MBSTOWCS
 | 
					
						
							|  |  |  |     /* Some platforms have a broken implementation of
 | 
					
						
							|  |  |  |      * mbstowcs which does not count the characters that | 
					
						
							|  |  |  |      * would result from conversion.  Use an upper bound. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |     argsize = strlen(arg); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |     argsize = _Py_mbstowcs(NULL, arg, 0); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |     if (argsize != DECODE_ERROR) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t)); | 
					
						
							|  |  |  |         if (!res) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         count = _Py_mbstowcs(res, arg, argsize + 1); | 
					
						
							|  |  |  |         if (count != DECODE_ERROR) { | 
					
						
							|  |  |  |             *wstr = res; | 
					
						
							|  |  |  |             if (wlen != NULL) { | 
					
						
							|  |  |  |                 *wlen = count; | 
					
						
							| 
									
										
										
										
											2010-10-16 23:16:16 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |         PyMem_RawFree(res); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     /* Conversion failed. Fall back to escaping with surrogateescape. */ | 
					
						
							|  |  |  | #ifdef HAVE_MBRTOWC
 | 
					
						
							|  |  |  |     /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Overallocate; as multi-byte characters are in the argument, the
 | 
					
						
							|  |  |  |        actual output could use less memory. */ | 
					
						
							|  |  |  |     argsize = strlen(arg) + 1; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t)); | 
					
						
							|  |  |  |     if (!res) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     in = (unsigned char*)arg; | 
					
						
							|  |  |  |     out = res; | 
					
						
							|  |  |  |     memset(&mbs, 0, sizeof mbs); | 
					
						
							|  |  |  |     while (argsize) { | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         size_t converted = _Py_mbrtowc(out, (char*)in, argsize, &mbs); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         if (converted == 0) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |             /* Reached end of string; null char stored. */ | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         if (converted == INCOMPLETE_CHARACTER) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |             /* Incomplete character. This should never happen,
 | 
					
						
							|  |  |  |                since we provide everything that we have - | 
					
						
							|  |  |  |                unless there is a bug in the C library, or I | 
					
						
							|  |  |  |                misunderstood how mbrtowc works. */ | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |             goto decode_error; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         if (converted == DECODE_ERROR) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |             if (!surrogateescape) { | 
					
						
							|  |  |  |                 goto decode_error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |             /* Decoding error. Escape as UTF-8b, and start over in the initial
 | 
					
						
							|  |  |  |                shift state. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |             *out++ = 0xdc00 + *in++; | 
					
						
							|  |  |  |             argsize--; | 
					
						
							|  |  |  |             memset(&mbs, 0, sizeof mbs); | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |         // _Py_mbrtowc() reject lone surrogate characters
 | 
					
						
							|  |  |  |         assert(!Py_UNICODE_IS_SURROGATE(*out)); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         /* successfully converted some bytes */ | 
					
						
							|  |  |  |         in += converted; | 
					
						
							|  |  |  |         argsize -= converted; | 
					
						
							|  |  |  |         out++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (wlen != NULL) { | 
					
						
							|  |  |  |         *wlen = out - res; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *wstr = res; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | decode_error: | 
					
						
							|  |  |  |     PyMem_RawFree(res); | 
					
						
							|  |  |  |     if (wlen) { | 
					
						
							|  |  |  |         *wlen = in - (unsigned char*)arg; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (reason) { | 
					
						
							|  |  |  |         *reason = "decoding error"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -2; | 
					
						
							| 
									
										
										
										
											2012-11-12 23:04:02 +01:00
										 |  |  | #else   /* HAVE_MBRTOWC */
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     /* Cannot use C locale for escaping; manually escape as if charset
 | 
					
						
							|  |  |  |        is ASCII (i.e. escape all bytes > 128. This will still roundtrip | 
					
						
							|  |  |  |        correctly in the locale's charset, which must be an ASCII superset. */ | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     return decode_ascii(arg, wstr, wlen, reason, errors); | 
					
						
							| 
									
										
										
										
											2012-11-12 23:04:02 +01:00
										 |  |  | #endif   /* HAVE_MBRTOWC */
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | /* Decode a byte string from the locale encoding.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Use the strict error handler if 'surrogateescape' is zero.  Use the | 
					
						
							|  |  |  |    surrogateescape error handler if 'surrogateescape' is non-zero: 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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 23:08:45 +03:00
										 |  |  |    On success, return 0 and write the newly allocated wide character string into | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |    *wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write | 
					
						
							|  |  |  |    the number of wide characters excluding the null character into *wlen. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On memory allocation failure, return -1. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On decoding error, return -2. If wlen is not NULL, write the start of | 
					
						
							|  |  |  |    invalid byte sequence in the input string into *wlen. If reason is not NULL, | 
					
						
							|  |  |  |    write the decoding error message into *reason. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |    Return -3 if the error handler 'errors' is not supported. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |    Use the Py_EncodeLocaleEx() function to encode the character string back to | 
					
						
							|  |  |  |    a byte string. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen, | 
					
						
							|  |  |  |                    const char **reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                    int current_locale, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (current_locale) { | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #ifdef _Py_FORCE_UTF8_LOCALE
 | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  |         return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                 errors); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |         return decode_current_locale(arg, wstr, wlen, reason, errors); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #ifdef _Py_FORCE_UTF8_FS_ENCODING
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                             errors); | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-05-25 00:09:48 +02:00
										 |  |  |     int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1); | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2022-05-25 00:09:48 +02:00
										 |  |  |     use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0); | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     if (use_utf8) { | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |         return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, | 
					
						
							|  |  |  |                                 errors); | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | #ifdef USE_FORCE_ASCII
 | 
					
						
							|  |  |  |     if (force_ascii == -1) { | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  |         force_ascii = check_force_ascii(); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (force_ascii) { | 
					
						
							|  |  |  |         /* force ASCII encoding to workaround mbstowcs() issue */ | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |         return decode_ascii(arg, wstr, wlen, reason, errors); | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     return decode_current_locale(arg, wstr, wlen, reason, errors); | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #endif   /* !_Py_FORCE_UTF8_FS_ENCODING */
 | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  | /* 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 | 
					
						
							|  |  |  |    character, escape the bytes using the surrogateescape error handler instead | 
					
						
							|  |  |  |    of decoding them. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    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 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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    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* | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | Py_DecodeLocale(const char* arg, size_t *wlen) | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     wchar_t *wstr; | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     int res = _Py_DecodeLocaleEx(arg, &wstr, wlen, | 
					
						
							|  |  |  |                                  NULL, 0, | 
					
						
							|  |  |  |                                  _Py_ERROR_SURROGATEESCAPE); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (res != 0) { | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |         assert(res != -3); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         if (wlen != NULL) { | 
					
						
							|  |  |  |             *wlen = (size_t)res; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return wstr; | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | encode_current_locale(const wchar_t *text, char **str, | 
					
						
							|  |  |  |                       size_t *error_pos, const char **reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                       int raw_malloc, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     const size_t len = wcslen(text); | 
					
						
							|  |  |  |     char *result = NULL, *bytes = NULL; | 
					
						
							|  |  |  |     size_t i, size, converted; | 
					
						
							|  |  |  |     wchar_t c, buf[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |     int surrogateescape; | 
					
						
							|  |  |  |     if (get_surrogateescape(errors, &surrogateescape) < 0) { | 
					
						
							|  |  |  |         return -3; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     /* The function works in two steps:
 | 
					
						
							|  |  |  |        1. compute the length of the output buffer in bytes (size) | 
					
						
							|  |  |  |        2. outputs the bytes */ | 
					
						
							|  |  |  |     size = 0; | 
					
						
							|  |  |  |     buf[1] = 0; | 
					
						
							|  |  |  |     while (1) { | 
					
						
							|  |  |  |         for (i=0; i < len; i++) { | 
					
						
							|  |  |  |             c = text[i]; | 
					
						
							|  |  |  |             if (c >= 0xdc80 && c <= 0xdcff) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 if (!surrogateescape) { | 
					
						
							|  |  |  |                     goto encode_error; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                 /* UTF-8b surrogate */ | 
					
						
							|  |  |  |                 if (bytes != NULL) { | 
					
						
							|  |  |  |                     *bytes++ = c - 0xdc00; | 
					
						
							|  |  |  |                     size--; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                     size++; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 buf[0] = c; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 if (bytes != NULL) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                     converted = wcstombs(bytes, buf, size); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                     converted = wcstombs(NULL, buf, 0); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |                 if (converted == DECODE_ERROR) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                     goto encode_error; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (bytes != NULL) { | 
					
						
							|  |  |  |                     bytes += converted; | 
					
						
							|  |  |  |                     size -= converted; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |                     size += converted; | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (result != NULL) { | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  |             *bytes = '\0'; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         size += 1; /* nul byte at the end */ | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |         if (raw_malloc) { | 
					
						
							|  |  |  |             result = PyMem_RawMalloc(size); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             result = PyMem_Malloc(size); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-11-12 23:32:21 +01:00
										 |  |  |         if (result == NULL) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2012-11-12 23:32:21 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         bytes = result; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     *str = result; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | encode_error: | 
					
						
							|  |  |  |     if (raw_malloc) { | 
					
						
							|  |  |  |         PyMem_RawFree(result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyMem_Free(result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (error_pos != NULL) { | 
					
						
							|  |  |  |         *error_pos = i; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (reason) { | 
					
						
							|  |  |  |         *reason = "encoding error"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -2; | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Encode a string to the locale encoding.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Parameters: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * raw_malloc: if non-zero, allocate memory using PyMem_RawMalloc() instead | 
					
						
							|  |  |  |      of PyMem_Malloc(). | 
					
						
							|  |  |  |    * current_locale: if non-zero, use the current LC_CTYPE, otherwise use | 
					
						
							|  |  |  |      Python filesystem encoding. | 
					
						
							|  |  |  |    * errors: error handler like "strict" or "surrogateescape". | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return value: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     0: success, *str is set to a newly allocated decoded string. | 
					
						
							|  |  |  |    -1: memory allocation failure | 
					
						
							|  |  |  |    -2: encoding error, set *error_pos and *reason (if set). | 
					
						
							|  |  |  |    -3: the error handler 'errors' is not supported. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos, | 
					
						
							|  |  |  |                  const char **reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                  int raw_malloc, int current_locale, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (current_locale) { | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #ifdef _Py_FORCE_UTF8_LOCALE
 | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  |         return _Py_EncodeUTF8Ex(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                 raw_malloc, errors); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         return encode_current_locale(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                      raw_malloc, errors); | 
					
						
							| 
									
										
										
										
											2018-01-22 19:07:32 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #ifdef _Py_FORCE_UTF8_FS_ENCODING
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     return _Py_EncodeUTF8Ex(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                             raw_malloc, errors); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-05-25 00:09:48 +02:00
										 |  |  |     int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1); | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2022-05-25 00:09:48 +02:00
										 |  |  |     use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0); | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     if (use_utf8) { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |         return _Py_EncodeUTF8Ex(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                 raw_malloc, errors); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | #ifdef USE_FORCE_ASCII
 | 
					
						
							|  |  |  |     if (force_ascii == -1) { | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |         force_ascii = check_force_ascii(); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (force_ascii) { | 
					
						
							|  |  |  |         return encode_ascii(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                             raw_malloc, errors); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     return encode_current_locale(text, str, error_pos, reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                  raw_malloc, errors); | 
					
						
							| 
									
										
										
										
											2019-05-02 11:28:57 -04:00
										 |  |  | #endif   /* _Py_FORCE_UTF8_FS_ENCODING */
 | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | static char* | 
					
						
							|  |  |  | encode_locale(const wchar_t *text, size_t *error_pos, | 
					
						
							|  |  |  |               int raw_malloc, int current_locale) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char *str; | 
					
						
							|  |  |  |     int res = encode_locale_ex(text, &str, error_pos, NULL, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                                raw_malloc, current_locale, | 
					
						
							|  |  |  |                                _Py_ERROR_SURROGATEESCAPE); | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     if (res != -2 && error_pos) { | 
					
						
							|  |  |  |         *error_pos = (size_t)-1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (res != 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | /* 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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return a pointer to a newly allocated byte string, use PyMem_Free() to free | 
					
						
							|  |  |  |    the memory. Return NULL on encoding or memory allocation error. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set | 
					
						
							|  |  |  |    to the index of the invalid character on encoding error. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Use the Py_DecodeLocale() function to decode the bytes string back to a wide | 
					
						
							|  |  |  |    character string. */ | 
					
						
							|  |  |  | char* | 
					
						
							|  |  |  | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  |     return encode_locale(text, error_pos, 0, 0); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-13 12:29:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  | /* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
 | 
					
						
							|  |  |  |    instead of PyMem_Free(). */ | 
					
						
							|  |  |  | char* | 
					
						
							|  |  |  | _Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  |     return encode_locale(text, error_pos, 1, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_EncodeLocaleEx(const wchar_t *text, char **str, | 
					
						
							|  |  |  |                    size_t *error_pos, const char **reason, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                    int current_locale, _Py_error_handler errors) | 
					
						
							| 
									
										
										
										
											2018-01-10 22:46:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 10:45:49 +01:00
										 |  |  |     return encode_locale_ex(text, str, error_pos, reason, 1, | 
					
						
							| 
									
										
										
										
											2018-08-29 22:21:32 +02:00
										 |  |  |                             current_locale, errors); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | // Get the current locale encoding name:
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  | // - Return "utf-8" if _Py_FORCE_UTF8_LOCALE macro is defined (ex: on Android)
 | 
					
						
							|  |  |  | // - Return "utf-8" if the UTF-8 Mode is enabled
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | // - On Windows, return the ANSI code page (ex: "cp1250")
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  | // - Return "utf-8" if nl_langinfo(CODESET) returns an empty string.
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | // - Otherwise, return nl_langinfo(CODESET).
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  | // Return NULL on memory allocation failure.
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | // See also config_get_locale_encoding()
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | wchar_t* | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  | _Py_GetLocaleEncoding(void) | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef _Py_FORCE_UTF8_LOCALE
 | 
					
						
							|  |  |  |     // On Android langinfo.h and CODESET are missing,
 | 
					
						
							|  |  |  |     // and UTF-8 is always used in mbstowcs() and wcstombs().
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  |     return _PyMem_RawWcsdup(L"utf-8"); | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     wchar_t encoding[23]; | 
					
						
							|  |  |  |     unsigned int ansi_codepage = GetACP(); | 
					
						
							|  |  |  |     swprintf(encoding, Py_ARRAY_LENGTH(encoding), L"cp%u", ansi_codepage); | 
					
						
							|  |  |  |     encoding[Py_ARRAY_LENGTH(encoding) - 1] = 0; | 
					
						
							|  |  |  |     return _PyMem_RawWcsdup(encoding); | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     const char *encoding = nl_langinfo(CODESET); | 
					
						
							|  |  |  |     if (!encoding || encoding[0] == '\0') { | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  |         // Use UTF-8 if nl_langinfo() returns an empty string. It can happen on
 | 
					
						
							|  |  |  |         // macOS if the LC_CTYPE locale is not supported.
 | 
					
						
							| 
									
										
										
										
											2022-04-09 09:54:54 +09:00
										 |  |  |         return _PyMem_RawWcsdup(L"utf-8"); | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  |     wchar_t *wstr; | 
					
						
							|  |  |  |     int res = decode_current_locale(encoding, &wstr, NULL, | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  |                                     NULL, _Py_ERROR_SURROGATEESCAPE); | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  |     if (res < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return wstr; | 
					
						
							|  |  |  | #endif  // !MS_WINDOWS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // !_Py_FORCE_UTF8_LOCALE
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | _Py_GetLocaleEncodingObject(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  |     wchar_t *encoding = _Py_GetLocaleEncoding(); | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  |     if (encoding == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-01 23:07:23 +01:00
										 |  |  |         PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2020-11-01 20:59:35 +01:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *str = PyUnicode_FromWideChar(encoding, -1); | 
					
						
							|  |  |  |     PyMem_RawFree(encoding); | 
					
						
							|  |  |  |     return str; | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 15:21:42 +02:00
										 |  |  | #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Check whether current locale uses Unicode as internal wchar_t form. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_LocaleUsesNonUnicodeWchar(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Oracle Solaris uses non-Unicode internal wchar_t form for
 | 
					
						
							|  |  |  |        non-Unicode locales and hence needs conversion to UTF first. */ | 
					
						
							|  |  |  |     char* codeset = nl_langinfo(CODESET); | 
					
						
							|  |  |  |     if (!codeset) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* 646 refers to ISO/IEC 646 standard that corresponds to ASCII encoding */ | 
					
						
							|  |  |  |     return (strcmp(codeset, "UTF-8") != 0 && strcmp(codeset, "646") != 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static wchar_t * | 
					
						
							|  |  |  | _Py_ConvertWCharForm(const wchar_t *source, Py_ssize_t size, | 
					
						
							|  |  |  |                      const char *tocode, const char *fromcode) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-20 19:26:40 +02:00
										 |  |  |     static_assert(sizeof(wchar_t) == 4, "wchar_t must be 32-bit"); | 
					
						
							| 
									
										
										
										
											2021-04-30 15:21:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Ensure we won't overflow the size. */ | 
					
						
							|  |  |  |     if (size > (PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t))) { | 
					
						
							|  |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* the string doesn't have to be NULL terminated */ | 
					
						
							|  |  |  |     wchar_t* target = PyMem_Malloc(size * sizeof(wchar_t)); | 
					
						
							|  |  |  |     if (target == NULL) { | 
					
						
							|  |  |  |         PyErr_NoMemory(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     iconv_t cd = iconv_open(tocode, fromcode); | 
					
						
							|  |  |  |     if (cd == (iconv_t)-1) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_ValueError, "iconv_open() failed"); | 
					
						
							|  |  |  |         PyMem_Free(target); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     char *inbuf = (char *) source; | 
					
						
							|  |  |  |     char *outbuf = (char *) target; | 
					
						
							|  |  |  |     size_t inbytesleft = sizeof(wchar_t) * size; | 
					
						
							|  |  |  |     size_t outbytesleft = inbytesleft; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); | 
					
						
							|  |  |  |     if (ret == DECODE_ERROR) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_ValueError, "iconv() failed"); | 
					
						
							|  |  |  |         PyMem_Free(target); | 
					
						
							|  |  |  |         iconv_close(cd); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     iconv_close(cd); | 
					
						
							|  |  |  |     return target; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Convert a wide character string to the UCS-4 encoded string. This
 | 
					
						
							|  |  |  |    is necessary on systems where internal form of wchar_t are not Unicode | 
					
						
							|  |  |  |    code points (e.g. Oracle Solaris). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return a pointer to a newly allocated string, use PyMem_Free() to free | 
					
						
							|  |  |  |    the memory. Return NULL and raise exception on conversion or memory | 
					
						
							|  |  |  |    allocation error. */ | 
					
						
							|  |  |  | wchar_t * | 
					
						
							|  |  |  | _Py_DecodeNonUnicodeWchar(const wchar_t *native, Py_ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_ConvertWCharForm(native, size, "UCS-4-INTERNAL", "wchar_t"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Convert a UCS-4 encoded string to native wide character string. This
 | 
					
						
							|  |  |  |    is necessary on systems where internal form of wchar_t are not Unicode | 
					
						
							|  |  |  |    code points (e.g. Oracle Solaris). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The conversion is done in place. This can be done because both wchar_t | 
					
						
							|  |  |  |    and UCS-4 use 4-byte encoding, and one wchar_t symbol always correspond | 
					
						
							|  |  |  |    to a single UCS-4 symbol and vice versa. (This is true for Oracle Solaris, | 
					
						
							|  |  |  |    which is currently the only system using these functions; it doesn't have | 
					
						
							|  |  |  |    to be for other systems). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Return 0 on success. Return -1 and raise exception on conversion | 
					
						
							|  |  |  |    or memory allocation error. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_EncodeNonUnicodeWchar_InPlace(wchar_t *unicode, Py_ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     wchar_t* result = _Py_ConvertWCharForm(unicode, size, "wchar_t", "UCS-4-INTERNAL"); | 
					
						
							|  |  |  |     if (!result) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     memcpy(unicode, result, size * sizeof(wchar_t)); | 
					
						
							|  |  |  |     PyMem_Free(result); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /* HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION */
 | 
					
						
							| 
									
										
										
										
											2020-10-31 01:02:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | #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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | LARGE_INTEGER_to_time_t_nsec(LARGE_INTEGER *in_ptr, time_t *time_out, int* nsec_out) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     *nsec_out = (int)(in_ptr->QuadPart % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ | 
					
						
							|  |  |  |     *time_out = Py_SAFE_DOWNCAST((in_ptr->QuadPart / 10000000) - secs_between_epochs, __int64, time_t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2015-02-21 15:26:02 -08:00
										 |  |  | _Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     /* 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef union { | 
					
						
							|  |  |  |     FILE_ID_128 id; | 
					
						
							|  |  |  |     struct { | 
					
						
							|  |  |  |         uint64_t st_ino; | 
					
						
							|  |  |  |         uint64_t st_ino_high; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } id_128_to_ino; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 15:26:02 -08:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |                            FILE_BASIC_INFO *basic_info, FILE_ID_INFO *id_info, | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |                            struct _Py_stat_struct *result) | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     memset(result, 0, sizeof(*result)); | 
					
						
							|  |  |  |     result->st_mode = attributes_to_mode(info->dwFileAttributes); | 
					
						
							|  |  |  |     result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |     result->st_dev = id_info ? id_info->VolumeSerialNumber : info->dwVolumeSerialNumber; | 
					
						
							|  |  |  |     result->st_rdev = 0; | 
					
						
							|  |  |  |     /* st_ctime is deprecated, but we preserve the legacy value in our caller, not here */ | 
					
						
							|  |  |  |     if (basic_info) { | 
					
						
							|  |  |  |         LARGE_INTEGER_to_time_t_nsec(&basic_info->CreationTime, &result->st_birthtime, &result->st_birthtime_nsec); | 
					
						
							|  |  |  |         LARGE_INTEGER_to_time_t_nsec(&basic_info->ChangeTime, &result->st_ctime, &result->st_ctime_nsec); | 
					
						
							|  |  |  |         LARGE_INTEGER_to_time_t_nsec(&basic_info->LastWriteTime, &result->st_mtime, &result->st_mtime_nsec); | 
					
						
							|  |  |  |         LARGE_INTEGER_to_time_t_nsec(&basic_info->LastAccessTime, &result->st_atime, &result->st_atime_nsec); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_birthtime, &result->st_birthtime_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); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |     result->st_nlink = info->nNumberOfLinks; | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (id_info) { | 
					
						
							|  |  |  |         id_128_to_ino file_id; | 
					
						
							|  |  |  |         file_id.id = id_info->FileId; | 
					
						
							|  |  |  |         result->st_ino = file_id.st_ino; | 
					
						
							|  |  |  |         result->st_ino_high = file_id.st_ino_high; | 
					
						
							| 
									
										
										
										
											2023-05-25 00:16:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (!result->st_ino && !result->st_ino_high) { | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |         /* should only occur for DirEntry_from_find_data, in which case the
 | 
					
						
							|  |  |  |            index is likely to be zero anyway. */ | 
					
						
							|  |  |  |         result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 15:27:33 -07:00
										 |  |  |     /* bpo-37834: Only actual symlinks set the S_IFLNK flag. But lstat() will
 | 
					
						
							|  |  |  |        open other name surrogate reparse points without traversing them. To | 
					
						
							|  |  |  |        detect/handle these, check st_file_attributes and st_reparse_tag. */ | 
					
						
							|  |  |  |     result->st_reparse_tag = reparse_tag; | 
					
						
							|  |  |  |     if (info->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && | 
					
						
							|  |  |  |         reparse_tag == IO_REPARSE_TAG_SYMLINK) { | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |         /* set the bits that make this a symlink */ | 
					
						
							|  |  |  |         result->st_mode = (result->st_mode & ~S_IFMT) | S_IFLNK; | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     result->st_file_attributes = info->dwFileAttributes; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_stat_basic_info_to_stat(FILE_STAT_BASIC_INFORMATION *info, | 
					
						
							|  |  |  |                             struct _Py_stat_struct *result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     memset(result, 0, sizeof(*result)); | 
					
						
							|  |  |  |     result->st_mode = attributes_to_mode(info->FileAttributes); | 
					
						
							|  |  |  |     result->st_size = info->EndOfFile.QuadPart; | 
					
						
							|  |  |  |     LARGE_INTEGER_to_time_t_nsec(&info->CreationTime, &result->st_birthtime, &result->st_birthtime_nsec); | 
					
						
							|  |  |  |     LARGE_INTEGER_to_time_t_nsec(&info->ChangeTime, &result->st_ctime, &result->st_ctime_nsec); | 
					
						
							|  |  |  |     LARGE_INTEGER_to_time_t_nsec(&info->LastWriteTime, &result->st_mtime, &result->st_mtime_nsec); | 
					
						
							|  |  |  |     LARGE_INTEGER_to_time_t_nsec(&info->LastAccessTime, &result->st_atime, &result->st_atime_nsec); | 
					
						
							|  |  |  |     result->st_nlink = info->NumberOfLinks; | 
					
						
							|  |  |  |     result->st_dev = info->VolumeSerialNumber.QuadPart; | 
					
						
							|  |  |  |     /* File systems with less than 128-bits zero pad into this field */ | 
					
						
							|  |  |  |     id_128_to_ino file_id; | 
					
						
							|  |  |  |     file_id.id = info->FileId128; | 
					
						
							|  |  |  |     result->st_ino = file_id.st_ino; | 
					
						
							|  |  |  |     result->st_ino_high = file_id.st_ino_high; | 
					
						
							|  |  |  |     /* bpo-37834: Only actual symlinks set the S_IFLNK flag. But lstat() will
 | 
					
						
							|  |  |  |        open other name surrogate reparse points without traversing them. To | 
					
						
							|  |  |  |        detect/handle these, check st_file_attributes and st_reparse_tag. */ | 
					
						
							|  |  |  |     result->st_reparse_tag = info->ReparseTag; | 
					
						
							|  |  |  |     if (info->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && | 
					
						
							|  |  |  |         info->ReparseTag == IO_REPARSE_TAG_SYMLINK) { | 
					
						
							|  |  |  |         /* set the bits that make this a symlink */ | 
					
						
							|  |  |  |         result->st_mode = (result->st_mode & ~S_IFMT) | S_IFLNK; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     result->st_file_attributes = info->FileAttributes; | 
					
						
							|  |  |  |     switch (info->DeviceType) { | 
					
						
							|  |  |  |     case FILE_DEVICE_DISK: | 
					
						
							|  |  |  |     case FILE_DEVICE_VIRTUAL_DISK: | 
					
						
							|  |  |  |     case FILE_DEVICE_DFS: | 
					
						
							|  |  |  |     case FILE_DEVICE_CD_ROM: | 
					
						
							|  |  |  |     case FILE_DEVICE_CONTROLLER: | 
					
						
							|  |  |  |     case FILE_DEVICE_DATALINK: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case FILE_DEVICE_DISK_FILE_SYSTEM: | 
					
						
							|  |  |  |     case FILE_DEVICE_CD_ROM_FILE_SYSTEM: | 
					
						
							|  |  |  |     case FILE_DEVICE_NETWORK_FILE_SYSTEM: | 
					
						
							|  |  |  |         result->st_mode = (result->st_mode & ~S_IFMT) | 0x6000; /* _S_IFBLK */ | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case FILE_DEVICE_CONSOLE: | 
					
						
							|  |  |  |     case FILE_DEVICE_NULL: | 
					
						
							|  |  |  |     case FILE_DEVICE_KEYBOARD: | 
					
						
							|  |  |  |     case FILE_DEVICE_MODEM: | 
					
						
							|  |  |  |     case FILE_DEVICE_MOUSE: | 
					
						
							|  |  |  |     case FILE_DEVICE_PARALLEL_PORT: | 
					
						
							|  |  |  |     case FILE_DEVICE_PRINTER: | 
					
						
							|  |  |  |     case FILE_DEVICE_SCREEN: | 
					
						
							|  |  |  |     case FILE_DEVICE_SERIAL_PORT: | 
					
						
							|  |  |  |     case FILE_DEVICE_SOUND: | 
					
						
							|  |  |  |         result->st_mode = (result->st_mode & ~S_IFMT) | _S_IFCHR; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case FILE_DEVICE_NAMED_PIPE: | 
					
						
							|  |  |  |         result->st_mode = (result->st_mode & ~S_IFMT) | _S_IFIFO; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         if (info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | 
					
						
							|  |  |  |             result->st_mode = (result->st_mode & ~S_IFMT) | _S_IFDIR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return information about a file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On POSIX, use fstat(). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On Windows, use GetFileType() and GetFileInformationByHandle() which support | 
					
						
							| 
									
										
										
										
											2017-11-08 14:44:44 -08:00
										 |  |  |    files larger than 2 GiB.  fstat() may fail with EOVERFLOW on files larger | 
					
						
							|  |  |  |    than 2 GiB because the file size type is a signed 32-bit integer: see issue | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |    #23152.
 | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    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. */ | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  | _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     BY_HANDLE_FILE_INFORMATION info; | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |     FILE_BASIC_INFO basicInfo; | 
					
						
							|  |  |  |     FILE_ID_INFO idInfo; | 
					
						
							| 
									
										
										
										
											2023-11-14 00:10:06 +08:00
										 |  |  |     FILE_ID_INFO *pIdInfo = &idInfo; | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |     HANDLE h; | 
					
						
							|  |  |  |     int type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  |     h = _Py_get_osfhandle_noraise(fd); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (h == INVALID_HANDLE_VALUE) { | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         /* errno is already set by _get_osfhandle, but we also set
 | 
					
						
							|  |  |  |            the Win32 error for callers who expect that */ | 
					
						
							| 
									
										
										
										
											2015-03-07 18:14:07 -08:00
										 |  |  |         SetLastError(ERROR_INVALID_HANDLE); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |     memset(status, 0, sizeof(*status)); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     type = GetFileType(h); | 
					
						
							|  |  |  |     if (type == FILE_TYPE_UNKNOWN) { | 
					
						
							|  |  |  |         DWORD error = GetLastError(); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         if (error != 0) { | 
					
						
							|  |  |  |             errno = winerror_to_errno(error); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |         /* else: valid but unknown file */ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (type != FILE_TYPE_DISK) { | 
					
						
							|  |  |  |         if (type == FILE_TYPE_CHAR) | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |             status->st_mode = _S_IFCHR; | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |         else if (type == FILE_TYPE_PIPE) | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |             status->st_mode = _S_IFIFO; | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 17:27:21 +00:00
										 |  |  |     if (!GetFileInformationByHandle(h, &info) || | 
					
						
							| 
									
										
										
										
											2023-11-14 00:10:06 +08:00
										 |  |  |         !GetFileInformationByHandleEx(h, FileBasicInfo, &basicInfo, sizeof(basicInfo))) { | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         /* The Win32 error is already set, but we also set errno for
 | 
					
						
							|  |  |  |            callers who expect it */ | 
					
						
							|  |  |  |         errno = winerror_to_errno(GetLastError()); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-14 00:10:06 +08:00
										 |  |  |     if (!GetFileInformationByHandleEx(h, FileIdInfo, &idInfo, sizeof(idInfo))) { | 
					
						
							|  |  |  |         /* Failed to get FileIdInfo, so do not pass it along */ | 
					
						
							|  |  |  |         pIdInfo = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_attribute_data_to_stat(&info, 0, &basicInfo, pIdInfo, status); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |     return fstat(fd, status); | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  | /* Return information about a file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On POSIX, use fstat(). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On Windows, use GetFileType() and GetFileInformationByHandle() which support | 
					
						
							| 
									
										
										
										
											2017-11-08 14:44:44 -08:00
										 |  |  |    files larger than 2 GiB.  fstat() may fail with EOVERFLOW on files larger | 
					
						
							|  |  |  |    than 2 GiB because the file size type is a signed 32-bit integer: see issue | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |    #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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:32 +02:00
										 |  |  |    Release the GIL to call GetFileType() and GetFileInformationByHandle(), or | 
					
						
							|  |  |  |    to call fstat(). The caller must hold the GIL. */ | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_fstat(int fd, struct _Py_stat_struct *status) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2016-03-14 22:07:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 10:09:31 +02:00
										 |  |  |     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; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-21 08:44:05 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  | /* Like _Py_stat() but with a raw filename. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_wstat(const wchar_t* path, struct stat *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     struct _stat wstatbuf; | 
					
						
							|  |  |  |     err = _wstat(path, &wstatbuf); | 
					
						
							|  |  |  |     if (!err) { | 
					
						
							|  |  |  |         buf->st_mode = wstatbuf.st_mode; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     char *fname; | 
					
						
							|  |  |  |     fname = _Py_EncodeLocaleRaw(path, NULL); | 
					
						
							|  |  |  |     if (fname == NULL) { | 
					
						
							|  |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     err = stat(fname, buf); | 
					
						
							|  |  |  |     PyMem_RawFree(fname); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and
 | 
					
						
							|  |  |  |    call stat() otherwise. Only fill st_mode attribute on Windows. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-18 20:47:30 +01:00
										 |  |  |    Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was | 
					
						
							|  |  |  |    raised. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2010-10-07 22:23:10 +00:00
										 |  |  | _Py_stat(PyObject *path, struct stat *statbuf) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 23:26:06 +03:00
										 |  |  |     wchar_t *wpath = PyUnicode_AsWideCharString(path, NULL); | 
					
						
							| 
									
										
										
										
											2011-11-17 00:51:38 +01:00
										 |  |  |     if (wpath == NULL) | 
					
						
							| 
									
										
										
										
											2011-12-18 20:47:30 +01:00
										 |  |  |         return -2; | 
					
						
							| 
									
										
										
										
											2017-06-28 08:30:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  |     err = _Py_wstat(wpath, statbuf); | 
					
						
							| 
									
										
										
										
											2020-07-10 23:26:06 +03:00
										 |  |  |     PyMem_Free(wpath); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     return err; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2017-06-28 08:30:06 +03:00
										 |  |  |     PyObject *bytes; | 
					
						
							|  |  |  |     char *cpath; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bytes = PyUnicode_EncodeFSDefault(path); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     if (bytes == NULL) | 
					
						
							| 
									
										
										
										
											2011-12-18 20:47:30 +01:00
										 |  |  |         return -2; | 
					
						
							| 
									
										
										
										
											2017-06-28 08:30:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* check for embedded null bytes */ | 
					
						
							|  |  |  |     if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) { | 
					
						
							|  |  |  |         Py_DECREF(bytes); | 
					
						
							|  |  |  |         return -2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = stat(cpath, statbuf); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     Py_DECREF(bytes); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | // For some Windows API partitions, SetHandleInformation() is declared
 | 
					
						
							|  |  |  | // but none of the handle flags are defined.
 | 
					
						
							|  |  |  | #ifndef HANDLE_FLAG_INHERIT
 | 
					
						
							|  |  |  | #define HANDLE_FLAG_INHERIT 0x00000001
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-12-04 01:34:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 09:09:34 +03:00
										 |  |  | /* This function MUST be kept async-signal-safe on POSIX when raise=0. */ | 
					
						
							| 
									
										
										
										
											2013-10-12 22:41:17 +02:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | get_inheritable(int fd, int raise) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     HANDLE handle; | 
					
						
							|  |  |  |     DWORD flags; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  |     handle = _Py_get_osfhandle_noraise(fd); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     if (handle == INVALID_HANDLE_VALUE) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							| 
									
										
										
										
											2015-03-14 11:38:27 -07:00
										 |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!GetHandleInformation(handle, &flags)) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							|  |  |  |             PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return (flags & HANDLE_FLAG_INHERIT); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     int flags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     flags = fcntl(fd, F_GETFD, 0); | 
					
						
							|  |  |  |     if (flags == -1) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							|  |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return !(flags & FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Get the inheritable flag of the specified file descriptor.
 | 
					
						
							| 
									
										
										
										
											2013-09-07 10:36:04 +02:00
										 |  |  |    Return 1 if the file descriptor can be inherited, 0 if it cannot, | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |    raise an exception and return -1 on error. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_get_inheritable(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_inheritable(fd, 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 09:09:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* This function MUST be kept async-signal-safe on POSIX when raise=0. */ | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     HANDLE handle; | 
					
						
							|  |  |  |     DWORD flags; | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
 | 
					
						
							|  |  |  |     static int ioctl_works = -1; | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     int request; | 
					
						
							|  |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-04-17 16:51:52 +02:00
										 |  |  |     int flags, new_flags; | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     int res; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* atomic_flag_works can only be used to make the file descriptor
 | 
					
						
							|  |  |  |        non-inheritable */ | 
					
						
							|  |  |  |     assert(!(atomic_flag_works != NULL && inheritable)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (atomic_flag_works != NULL && !inheritable) { | 
					
						
							| 
									
										
										
										
											2024-12-31 19:10:06 +05:30
										 |  |  |         if (_Py_atomic_load_int_relaxed(atomic_flag_works) == -1) { | 
					
						
							| 
									
										
										
										
											2015-03-14 11:38:27 -07:00
										 |  |  |             int isInheritable = get_inheritable(fd, raise); | 
					
						
							|  |  |  |             if (isInheritable == -1) | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |                 return -1; | 
					
						
							| 
									
										
										
										
											2024-12-31 19:10:06 +05:30
										 |  |  |             _Py_atomic_store_int_relaxed(atomic_flag_works, !isInheritable); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-31 19:10:06 +05:30
										 |  |  |         if (_Py_atomic_load_int_relaxed(atomic_flag_works)) | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  |     handle = _Py_get_osfhandle_noraise(fd); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     if (handle == INVALID_HANDLE_VALUE) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							| 
									
										
										
										
											2015-03-14 11:38:27 -07:00
										 |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (inheritable) | 
					
						
							|  |  |  |         flags = HANDLE_FLAG_INHERIT; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         flags = 0; | 
					
						
							| 
									
										
										
										
											2019-08-23 12:38:41 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 01:31:21 +01:00
										 |  |  |     if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         if (raise) | 
					
						
							|  |  |  |             PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
 | 
					
						
							| 
									
										
										
										
											2024-06-17 13:23:40 -04:00
										 |  |  |     if (raise != 0 && _Py_atomic_load_int_relaxed(&ioctl_works) != 0) { | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |         /* fast-path: ioctl() only requires one syscall */ | 
					
						
							| 
									
										
										
										
											2018-02-06 09:09:34 +03:00
										 |  |  |         /* caveat: raise=0 is an indicator that we must be async-signal-safe
 | 
					
						
							|  |  |  |          * thus avoid using ioctl() so we skip the fast-path. */ | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |         if (inheritable) | 
					
						
							|  |  |  |             request = FIONCLEX; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             request = FIOCLEX; | 
					
						
							|  |  |  |         err = ioctl(fd, request, NULL); | 
					
						
							|  |  |  |         if (!err) { | 
					
						
							| 
									
										
										
										
											2024-06-17 13:23:40 -04:00
										 |  |  |             if (_Py_atomic_load_int_relaxed(&ioctl_works) == -1) { | 
					
						
							|  |  |  |                 _Py_atomic_store_int_relaxed(&ioctl_works, 1); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 15:15:10 +02:00
										 |  |  | #ifdef O_PATH
 | 
					
						
							| 
									
										
										
										
											2021-01-20 09:05:51 -05:00
										 |  |  |         if (errno == EBADF) { | 
					
						
							| 
									
										
										
										
											2021-08-06 15:15:10 +02:00
										 |  |  |             // bpo-44849: On Linux and FreeBSD, ioctl(FIOCLEX) fails with EBADF
 | 
					
						
							|  |  |  |             // on O_PATH file descriptors. Fall through to the fcntl()
 | 
					
						
							|  |  |  |             // implementation.
 | 
					
						
							| 
									
										
										
										
											2021-01-20 09:05:51 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-05-19 16:46:18 +02:00
										 |  |  |         if (errno != ENOTTY && errno != EACCES) { | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |             if (raise) | 
					
						
							|  |  |  |                 PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
 | 
					
						
							|  |  |  |                device". The ioctl is declared but not supported by the kernel. | 
					
						
							|  |  |  |                Remember that ioctl() doesn't work. It is the case on | 
					
						
							| 
									
										
										
										
											2016-05-19 16:46:18 +02:00
										 |  |  |                Illumos-based OS for example. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                Issue #27057: When SELinux policy disallows ioctl it will fail | 
					
						
							|  |  |  |                with EACCES. While FIOCLEX is safe operation it may be | 
					
						
							|  |  |  |                unavailable because ioctl was denied altogether. | 
					
						
							|  |  |  |                This can be the case on Android. */ | 
					
						
							| 
									
										
										
										
											2024-06-17 13:23:40 -04:00
										 |  |  |             _Py_atomic_store_int_relaxed(&ioctl_works, 0); | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         /* fallback to fcntl() if ioctl() does not work */ | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:41:04 +02:00
										 |  |  |     /* slow-path: fcntl() requires two syscalls */ | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     flags = fcntl(fd, F_GETFD); | 
					
						
							|  |  |  |     if (flags < 0) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							|  |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-17 16:51:52 +02:00
										 |  |  |     if (inheritable) { | 
					
						
							|  |  |  |         new_flags = flags & ~FD_CLOEXEC; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         new_flags = flags | FD_CLOEXEC; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (new_flags == flags) { | 
					
						
							|  |  |  |         /* FD_CLOEXEC flag already set/cleared: nothing to do */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 16:19:29 +01:00
										 |  |  |     res = fcntl(fd, F_SETFD, new_flags); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     if (res < 0) { | 
					
						
							|  |  |  |         if (raise) | 
					
						
							|  |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Make the file descriptor non-inheritable.
 | 
					
						
							| 
									
										
										
										
											2013-09-07 10:36:04 +02:00
										 |  |  |    Return 0 on success, set errno and return -1 on error. */ | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | make_non_inheritable(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return set_inheritable(fd, 0, 0, NULL); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | /* Set the inheritable flag of the specified file descriptor.
 | 
					
						
							| 
									
										
										
										
											2018-02-06 09:09:34 +03:00
										 |  |  |    On success: return 0, on error: raise an exception and return -1. | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    If atomic_flag_works is not NULL: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     * if *atomic_flag_works==-1, check if the inheritable is set on the file | 
					
						
							|  |  |  |       descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and | 
					
						
							|  |  |  |       set the inheritable flag | 
					
						
							|  |  |  |     * if *atomic_flag_works==1: do nothing | 
					
						
							|  |  |  |     * if *atomic_flag_works==0: set inheritable flag to False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Set atomic_flag_works to NULL if no atomic flag was used to create the | 
					
						
							|  |  |  |    file descriptor. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    atomic_flag_works can only be used to make a file descriptor | 
					
						
							|  |  |  |    non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return set_inheritable(fd, inheritable, 1, atomic_flag_works); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 09:09:34 +03:00
										 |  |  | /* Same as _Py_set_inheritable() but on error, set errno and
 | 
					
						
							|  |  |  |    don't raise an exception. | 
					
						
							|  |  |  |    This function is async-signal-safe. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return set_inheritable(fd, inheritable, 0, atomic_flag_works); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | _Py_open_impl(const char *pathname, int flags, int gil_held) | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     int fd; | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |     int async_err = 0; | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     int *atomic_flag_works; | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     flags |= O_NOINHERIT; | 
					
						
							|  |  |  | #elif defined(O_CLOEXEC)
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     atomic_flag_works = &_Py_open_cloexec_works; | 
					
						
							|  |  |  |     flags |= O_CLOEXEC; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     atomic_flag_works = NULL; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |     if (gil_held) { | 
					
						
							| 
									
										
										
										
											2020-06-24 08:46:05 +03:00
										 |  |  |         PyObject *pathname_obj = PyUnicode_DecodeFSDefault(pathname); | 
					
						
							|  |  |  |         if (pathname_obj == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PySys_Audit("open", "OOi", pathname_obj, Py_None, flags) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(pathname_obj); | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |         do { | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |             fd = open(pathname, flags); | 
					
						
							|  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |         } while (fd < 0 | 
					
						
							|  |  |  |                  && errno == EINTR && !(async_err = PyErr_CheckSignals())); | 
					
						
							| 
									
										
										
										
											2020-06-24 08:46:05 +03:00
										 |  |  |         if (async_err) { | 
					
						
							|  |  |  |             Py_DECREF(pathname_obj); | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2020-06-24 08:46:05 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |         if (fd < 0) { | 
					
						
							| 
									
										
										
										
											2020-06-24 08:46:05 +03:00
										 |  |  |             PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, pathname_obj, NULL); | 
					
						
							|  |  |  |             Py_DECREF(pathname_obj); | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 08:46:05 +03:00
										 |  |  |         Py_DECREF(pathname_obj); | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         fd = open(pathname, flags); | 
					
						
							|  |  |  |         if (fd < 0) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MS_WINDOWS
 | 
					
						
							|  |  |  |     if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         close(fd); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | /* 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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |    When interrupted by a signal (open() fails with EINTR), retry the syscall, | 
					
						
							|  |  |  |    except if the Python signal handler raises an exception. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:32 +02:00
										 |  |  |    Release the GIL to call open(). The caller must hold the GIL. */ | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_open(const char *pathname, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* _Py_open() must be called with the GIL held. */ | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  |     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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |    The file descriptor is created non-inheritable. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If interrupted by a signal, fail with EINTR. */ | 
					
						
							| 
									
										
										
										
											2015-03-18 00:22:14 +01:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_open_noraise(const char *pathname, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_open_impl(pathname, flags, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | /* Open a file. Use _wfopen() on Windows, encode the path to the locale
 | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |    encoding and use fopen() otherwise. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |    The file descriptor is created non-inheritable. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If interrupted by a signal, fail with EINTR. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | FILE * | 
					
						
							|  |  |  | _Py_wfopen(const wchar_t *path, const wchar_t *mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     FILE *f; | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     if (PySys_Audit("open", "uui", path, mode, 0) < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     char *cpath; | 
					
						
							|  |  |  |     char cmode[10]; | 
					
						
							|  |  |  |     size_t r; | 
					
						
							|  |  |  |     r = wcstombs(cmode, mode, 10); | 
					
						
							| 
									
										
										
										
											2021-03-17 21:46:53 +01:00
										 |  |  |     if (r == DECODE_ERROR || r >= 10) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     cpath = _Py_EncodeLocaleRaw(path, NULL); | 
					
						
							|  |  |  |     if (cpath == NULL) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     f = fopen(cpath, cmode); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     PyMem_RawFree(cpath); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     f = _wfopen(path, mode); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     if (f == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (make_non_inheritable(fileno(f)) < 0) { | 
					
						
							|  |  |  |         fclose(f); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return f; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  | /* Open a file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    On Windows, if 'path' is a Unicode string, call _wfopen(). Otherwise, encode | 
					
						
							|  |  |  |    the path to the filesystem encoding and call fopen(). | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Return the new file object on success. Raise an exception and return NULL | 
					
						
							|  |  |  |    on error. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The file descriptor is created non-inheritable. | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |    When interrupted by a signal (open() fails with EINTR), retry the syscall, | 
					
						
							|  |  |  |    except if the Python signal handler raises an exception. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:32 +02:00
										 |  |  |    Release the GIL to call _wfopen() or fopen(). The caller must hold | 
					
						
							|  |  |  |    the GIL. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | FILE* | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  | Py_fopen(PyObject *path, const char *mode) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     if (PySys_Audit("open", "Osi", path, mode, 0) < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     FILE *f; | 
					
						
							|  |  |  |     int async_err = 0; | 
					
						
							|  |  |  |     int saved_errno; | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     PyObject *unicode; | 
					
						
							|  |  |  |     if (!PyUnicode_FSDecoder(path, &unicode)) { | 
					
						
							| 
									
										
										
										
											2011-12-22 10:03:38 +01:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-12 14:48:38 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     wchar_t *wpath = PyUnicode_AsWideCharString(unicode, NULL); | 
					
						
							|  |  |  |     Py_DECREF(unicode); | 
					
						
							|  |  |  |     if (wpath == NULL) { | 
					
						
							| 
									
										
										
										
											2011-11-17 00:51:38 +01:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-17 00:51:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     wchar_t wmode[10]; | 
					
						
							|  |  |  |     int usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, | 
					
						
							|  |  |  |                                     wmode, Py_ARRAY_LENGTH(wmode)); | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |     if (usize == 0) { | 
					
						
							|  |  |  |         PyErr_SetFromWindowsErr(0); | 
					
						
							| 
									
										
										
										
											2020-07-10 23:26:06 +03:00
										 |  |  |         PyMem_Free(wpath); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |     do { | 
					
						
							|  |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |         _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |         f = _wfopen(wpath, wmode); | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |         _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |         Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } while (f == NULL | 
					
						
							|  |  |  |              && errno == EINTR && !(async_err = PyErr_CheckSignals())); | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     saved_errno = errno; | 
					
						
							| 
									
										
										
										
											2020-07-10 23:26:06 +03:00
										 |  |  |     PyMem_Free(wpath); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-12-19 18:19:06 +01:00
										 |  |  |     PyObject *bytes; | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     if (!PyUnicode_FSConverter(path, &bytes)) { | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     const char *path_bytes = PyBytes_AS_STRING(bytes); | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |     do { | 
					
						
							|  |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |         f = fopen(path_bytes, mode); | 
					
						
							|  |  |  |         Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } while (f == NULL | 
					
						
							|  |  |  |              && errno == EINTR && !(async_err = PyErr_CheckSignals())); | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     saved_errno = errno; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     Py_DECREF(bytes); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (async_err) { | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-18 09:52:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |     if (f == NULL) { | 
					
						
							| 
									
										
										
										
											2023-08-27 00:35:06 +03:00
										 |  |  |         errno = saved_errno; | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |         PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2015-03-18 01:39:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         fclose(f); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return f; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 13:43:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Call fclose().
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // On Windows, files opened by Py_fopen() in the Python DLL must be closed by
 | 
					
						
							|  |  |  | // the Python DLL to use the same C runtime version. Otherwise, calling
 | 
					
						
							|  |  |  | // fclose() directly can cause undefined behavior.
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Py_fclose(FILE *file) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return fclose(file); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | /* Read count bytes from fd into buf.
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    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. */ | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | Py_ssize_t | 
					
						
							|  |  |  | _Py_read(int fd, void *buf, size_t count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t n; | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     int async_err = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2016-03-14 22:07:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     /* _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()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 01:05:04 +02:00
										 |  |  |     if (count > _PY_READ_MAX) { | 
					
						
							|  |  |  |         count = _PY_READ_MAX; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     do { | 
					
						
							|  |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |         errno = 0; | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |         _doserrno = 0; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         n = read(fd, buf, (int)count); | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |         // read() on a non-blocking empty pipe fails with EINVAL, which is
 | 
					
						
							|  |  |  |         // mapped from the Windows error code ERROR_NO_DATA.
 | 
					
						
							|  |  |  |         if (n < 0 && errno == EINVAL) { | 
					
						
							|  |  |  |             if (_doserrno == ERROR_NO_DATA) { | 
					
						
							|  |  |  |                 errno = EAGAIN; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |         n = read(fd, buf, count); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |         /* save/restore errno because PyErr_CheckSignals()
 | 
					
						
							|  |  |  |          * and PyErr_SetFromErrno() can modify it */ | 
					
						
							|  |  |  |         err = errno; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |     } while (n < 0 && err == EINTR && | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |             !(async_err = PyErr_CheckSignals())); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (async_err) { | 
					
						
							|  |  |  |         /* read() was interrupted by a signal (failed with EINTR)
 | 
					
						
							|  |  |  |          * and the Python signal handler raised an exception */ | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |         errno = err; | 
					
						
							|  |  |  |         assert(errno == EINTR && PyErr_Occurred()); | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (n < 0) { | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |         errno = err; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return n; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t n; | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     int async_err = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2021-09-09 15:12:03 +02:00
										 |  |  |     if (count > 32767) { | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         /* 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). */ | 
					
						
							| 
									
										
										
										
											2021-09-09 15:12:03 +02:00
										 |  |  |         if (gil_held) { | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |             if (isatty(fd)) { | 
					
						
							|  |  |  |                 count = 32767; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (isatty(fd)) { | 
					
						
							|  |  |  |                 count = 32767; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-10-18 01:05:04 +02:00
										 |  |  |     if (count > _PY_WRITE_MAX) { | 
					
						
							|  |  |  |         count = _PY_WRITE_MAX; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |     if (gil_held) { | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |             errno = 0; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |             // write() on a non-blocking pipe fails with ENOSPC on Windows if
 | 
					
						
							|  |  |  |             // the pipe lacks available space for the entire buffer.
 | 
					
						
							|  |  |  |             int c = (int)count; | 
					
						
							|  |  |  |             do { | 
					
						
							|  |  |  |                 _doserrno = 0; | 
					
						
							|  |  |  |                 n = write(fd, buf, c); | 
					
						
							|  |  |  |                 if (n >= 0 || errno != ENOSPC || _doserrno != 0) { | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 errno = EAGAIN; | 
					
						
							|  |  |  |                 c /= 2; | 
					
						
							|  |  |  |             } while (c > 0); | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |             n = write(fd, buf, count); | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |             /* 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
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |             // write() on a non-blocking pipe fails with ENOSPC on Windows if
 | 
					
						
							|  |  |  |             // the pipe lacks available space for the entire buffer.
 | 
					
						
							|  |  |  |             int c = (int)count; | 
					
						
							|  |  |  |             do { | 
					
						
							|  |  |  |                 _doserrno = 0; | 
					
						
							|  |  |  |                 n = write(fd, buf, c); | 
					
						
							|  |  |  |                 if (n >= 0 || errno != ENOSPC || _doserrno != 0) { | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 errno = EAGAIN; | 
					
						
							|  |  |  |                 c /= 2; | 
					
						
							|  |  |  |             } while (c > 0); | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |             n = write(fd, buf, count); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |             err = errno; | 
					
						
							|  |  |  |         } while (n < 0 && err == EINTR); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (async_err) { | 
					
						
							|  |  |  |         /* write() was interrupted by a signal (failed with EINTR)
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |            and the Python signal handler raised an exception (if gil_held is | 
					
						
							|  |  |  |            nonzero). */ | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |         errno = err; | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |         assert(errno == EINTR && (!gil_held || PyErr_Occurred())); | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (n < 0) { | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |         if (gil_held) | 
					
						
							|  |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2015-03-20 11:58:18 +01:00
										 |  |  |         errno = err; | 
					
						
							| 
									
										
										
										
											2015-03-19 22:53:20 +01:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return n; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  | /* 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2016-03-14 22:07:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:34:45 +02:00
										 |  |  |     /* _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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #ifdef HAVE_READLINK
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Read value of symbolic link. Encode the path to the locale encoding, decode
 | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |    the result from the locale encoding. | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |    Return -1 on encoding error, on readlink() error, if the internal buffer is | 
					
						
							|  |  |  |    too short, on decoding error, or if 'buf' is too short. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t buflen) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     char *cpath; | 
					
						
							| 
									
										
										
										
											2013-11-15 18:14:11 +01:00
										 |  |  |     char cbuf[MAXPATHLEN]; | 
					
						
							| 
									
										
										
										
											2019-10-04 02:22:39 +02:00
										 |  |  |     size_t cbuf_len = Py_ARRAY_LENGTH(cbuf); | 
					
						
							| 
									
										
										
										
											2010-10-16 22:47:37 +00:00
										 |  |  |     wchar_t *wbuf; | 
					
						
							| 
									
										
										
										
											2019-10-04 02:22:39 +02:00
										 |  |  |     Py_ssize_t res; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     size_t r1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     cpath = _Py_EncodeLocaleRaw(path, NULL); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     if (cpath == NULL) { | 
					
						
							|  |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-04 02:22:39 +02:00
										 |  |  |     res = readlink(cpath, cbuf, cbuf_len); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     PyMem_RawFree(cpath); | 
					
						
							| 
									
										
										
										
											2019-10-04 02:22:39 +02:00
										 |  |  |     if (res == -1) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2019-10-04 02:22:39 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     if ((size_t)res == cbuf_len) { | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     cbuf[res] = '\0'; /* buf will be null terminated */ | 
					
						
							| 
									
										
										
										
											2014-08-01 12:28:48 +02:00
										 |  |  |     wbuf = Py_DecodeLocale(cbuf, &r1); | 
					
						
							| 
									
										
										
										
											2010-10-16 22:52:09 +00:00
										 |  |  |     if (wbuf == NULL) { | 
					
						
							|  |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     /* wbuf must have space to store the trailing NUL character */ | 
					
						
							|  |  |  |     if (buflen <= r1) { | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |         PyMem_RawFree(wbuf); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     wcsncpy(buf, wbuf, buflen); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |     PyMem_RawFree(wbuf); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     return (int)r1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_REALPATH
 | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Return the canonicalized absolute pathname. Encode path to the locale
 | 
					
						
							| 
									
										
										
										
											2011-12-16 23:56:01 +01:00
										 |  |  |    encoding, decode the result from the locale encoding. | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |    Return NULL on encoding error, realpath() error, decoding error | 
					
						
							|  |  |  |    or if 'resolved_path' is too short. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | wchar_t* | 
					
						
							| 
									
										
										
										
											2010-10-07 22:29:53 +00:00
										 |  |  | _Py_wrealpath(const wchar_t *path, | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |               wchar_t *resolved_path, size_t resolved_path_len) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     char *cpath; | 
					
						
							| 
									
										
										
										
											2013-11-15 18:14:11 +01:00
										 |  |  |     char cresolved_path[MAXPATHLEN]; | 
					
						
							| 
									
										
										
										
											2010-10-16 22:55:47 +00:00
										 |  |  |     wchar_t *wresolved_path; | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     char *res; | 
					
						
							|  |  |  |     size_t r; | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     cpath = _Py_EncodeLocaleRaw(path, NULL); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     if (cpath == NULL) { | 
					
						
							|  |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     res = realpath(cpath, cresolved_path); | 
					
						
							| 
									
										
										
										
											2017-12-21 16:20:32 +01:00
										 |  |  |     PyMem_RawFree(cpath); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     if (res == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-10-16 22:55:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-01 12:28:48 +02:00
										 |  |  |     wresolved_path = Py_DecodeLocale(cresolved_path, &r); | 
					
						
							| 
									
										
										
										
											2010-10-16 22:55:47 +00:00
										 |  |  |     if (wresolved_path == NULL) { | 
					
						
							|  |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     /* wresolved_path must have space to store the trailing NUL character */ | 
					
						
							|  |  |  |     if (resolved_path_len <= r) { | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |         PyMem_RawFree(wresolved_path); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         errno = EINVAL; | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     wcsncpy(resolved_path, wresolved_path, resolved_path_len); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |     PyMem_RawFree(wresolved_path); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     return resolved_path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_isabs(const wchar_t *path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     const wchar_t *tail; | 
					
						
							|  |  |  |     HRESULT hr = PathCchSkipRoot(path, &tail); | 
					
						
							|  |  |  |     if (FAILED(hr) || path == tail) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (tail == &path[1] && (path[0] == SEP || path[0] == ALTSEP)) { | 
					
						
							|  |  |  |         // Exclude paths with leading SEP
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (tail == &path[2] && path[1] == L':') { | 
					
						
							|  |  |  |         // Exclude drive-relative paths (e.g. C:filename.ext)
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  |     return (path[0] == SEP); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Get an absolute path.
 | 
					
						
							|  |  |  |    On error (ex: fail to get the current directory), return -1. | 
					
						
							|  |  |  |    On memory allocation failure, set *abspath_p to NULL and return 0. | 
					
						
							|  |  |  |    On success, return a newly allocated to *abspath_p to and return 0. | 
					
						
							|  |  |  |    The string must be freed by PyMem_RawFree(). */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_abspath(const wchar_t *path, wchar_t **abspath_p) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     if (path[0] == '\0' || !wcscmp(path, L".")) { | 
					
						
							|  |  |  |         wchar_t cwd[MAXPATHLEN + 1]; | 
					
						
							|  |  |  |         cwd[Py_ARRAY_LENGTH(cwd) - 1] = 0; | 
					
						
							|  |  |  |         if (!_Py_wgetcwd(cwd, Py_ARRAY_LENGTH(cwd) - 1)) { | 
					
						
							|  |  |  |             /* unable to get the current directory */ | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         *abspath_p = _PyMem_RawWcsdup(cwd); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_Py_isabs(path)) { | 
					
						
							|  |  |  |         *abspath_p = _PyMem_RawWcsdup(path); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2022-01-14 08:35:42 +09:00
										 |  |  |     return _PyOS_getfullpathname(path, abspath_p); | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     wchar_t cwd[MAXPATHLEN + 1]; | 
					
						
							|  |  |  |     cwd[Py_ARRAY_LENGTH(cwd) - 1] = 0; | 
					
						
							|  |  |  |     if (!_Py_wgetcwd(cwd, Py_ARRAY_LENGTH(cwd) - 1)) { | 
					
						
							|  |  |  |         /* unable to get the current directory */ | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t cwd_len = wcslen(cwd); | 
					
						
							|  |  |  |     size_t path_len = wcslen(path); | 
					
						
							|  |  |  |     size_t len = cwd_len + 1 + path_len + 1; | 
					
						
							|  |  |  |     if (len <= (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t)) { | 
					
						
							|  |  |  |         *abspath_p = PyMem_RawMalloc(len * sizeof(wchar_t)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         *abspath_p = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (*abspath_p == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     wchar_t *abspath = *abspath_p; | 
					
						
							|  |  |  |     memcpy(abspath, cwd, cwd_len * sizeof(wchar_t)); | 
					
						
							|  |  |  |     abspath += cwd_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *abspath = (wchar_t)SEP; | 
					
						
							|  |  |  |     abspath++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memcpy(abspath, path, path_len * sizeof(wchar_t)); | 
					
						
							|  |  |  |     abspath += path_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *abspath = 0; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | // The Windows Games API family implements the PathCch* APIs in the Xbox OS,
 | 
					
						
							|  |  |  | // but does not expose them yet. Load them dynamically until
 | 
					
						
							|  |  |  | // 1) they are officially exposed
 | 
					
						
							|  |  |  | // 2) we stop supporting older versions of the GDK which do not expose them
 | 
					
						
							|  |  |  | #if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
 | 
					
						
							|  |  |  | HRESULT | 
					
						
							|  |  |  | PathCchSkipRoot(const wchar_t *path, const wchar_t **rootEnd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static int initialized = 0; | 
					
						
							|  |  |  |     typedef HRESULT(__stdcall *PPathCchSkipRoot) (PCWSTR pszPath, | 
					
						
							|  |  |  |                                                   PCWSTR *ppszRootEnd); | 
					
						
							|  |  |  |     static PPathCchSkipRoot _PathCchSkipRoot; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (initialized == 0) { | 
					
						
							|  |  |  |         HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL, | 
					
						
							|  |  |  |                                          LOAD_LIBRARY_SEARCH_SYSTEM32); | 
					
						
							|  |  |  |         if (pathapi) { | 
					
						
							|  |  |  |             _PathCchSkipRoot = (PPathCchSkipRoot)GetProcAddress( | 
					
						
							|  |  |  |                 pathapi, "PathCchSkipRoot"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             _PathCchSkipRoot = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         initialized = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!_PathCchSkipRoot) { | 
					
						
							|  |  |  |         return E_NOINTERFACE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return _PathCchSkipRoot(path, rootEnd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static HRESULT | 
					
						
							|  |  |  | PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname, | 
					
						
							|  |  |  |                  const wchar_t *relfile, unsigned long flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static int initialized = 0; | 
					
						
							|  |  |  |     typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, | 
					
						
							|  |  |  |                                                    size_t cchPathOut, | 
					
						
							|  |  |  |                                                    PCWSTR pszPathIn, | 
					
						
							|  |  |  |                                                    PCWSTR pszMore, | 
					
						
							|  |  |  |                                                    unsigned long dwFlags); | 
					
						
							|  |  |  |     static PPathCchCombineEx _PathCchCombineEx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (initialized == 0) { | 
					
						
							|  |  |  |         HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL, | 
					
						
							|  |  |  |                                          LOAD_LIBRARY_SEARCH_SYSTEM32); | 
					
						
							|  |  |  |         if (pathapi) { | 
					
						
							|  |  |  |             _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress( | 
					
						
							|  |  |  |                 pathapi, "PathCchCombineEx"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             _PathCchCombineEx = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         initialized = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!_PathCchCombineEx) { | 
					
						
							|  |  |  |         return E_NOINTERFACE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return _PathCchCombineEx(buffer, bufsize, dirname, relfile, flags); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */
 | 
					
						
							| 
									
										
										
										
											2019-06-25 15:02:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_skiproot(const wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, | 
					
						
							|  |  |  |              Py_ssize_t *rootsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(drvsize); | 
					
						
							|  |  |  |     assert(rootsize); | 
					
						
							|  |  |  | #ifndef MS_WINDOWS
 | 
					
						
							|  |  |  | #define IS_SEP(x) (*(x) == SEP)
 | 
					
						
							|  |  |  |     *drvsize = 0; | 
					
						
							|  |  |  |     if (!IS_SEP(&path[0])) { | 
					
						
							|  |  |  |         // Relative path, e.g.: 'foo'
 | 
					
						
							|  |  |  |         *rootsize = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!IS_SEP(&path[1]) || IS_SEP(&path[2])) { | 
					
						
							|  |  |  |         // Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
 | 
					
						
							|  |  |  |         *rootsize = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
 | 
					
						
							|  |  |  |         // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
 | 
					
						
							|  |  |  |         *rootsize = 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #undef IS_SEP
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     const wchar_t *pEnd = size >= 0 ? &path[size] : NULL; | 
					
						
							|  |  |  | #define IS_END(x) (pEnd ? (x) == pEnd : !*(x))
 | 
					
						
							|  |  |  | #define IS_SEP(x) (*(x) == SEP || *(x) == ALTSEP)
 | 
					
						
							|  |  |  | #define SEP_OR_END(x) (IS_SEP(x) || IS_END(x))
 | 
					
						
							|  |  |  |     if (IS_SEP(&path[0])) { | 
					
						
							|  |  |  |         if (IS_SEP(&path[1])) { | 
					
						
							|  |  |  |             // Device drives, e.g. \\.\device or \\?\device
 | 
					
						
							|  |  |  |             // UNC drives, e.g. \\server\share or \\?\UNC\server\share
 | 
					
						
							|  |  |  |             Py_ssize_t idx; | 
					
						
							|  |  |  |             if (path[2] == L'?' && IS_SEP(&path[3]) && | 
					
						
							|  |  |  |                 (path[4] == L'U' || path[4] == L'u') && | 
					
						
							|  |  |  |                 (path[5] == L'N' || path[5] == L'n') && | 
					
						
							|  |  |  |                 (path[6] == L'C' || path[6] == L'c') && | 
					
						
							|  |  |  |                 IS_SEP(&path[7])) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 idx = 8; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 idx = 2; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             while (!SEP_OR_END(&path[idx])) { | 
					
						
							|  |  |  |                 idx++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (IS_END(&path[idx])) { | 
					
						
							|  |  |  |                 *drvsize = idx; | 
					
						
							|  |  |  |                 *rootsize = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 idx++; | 
					
						
							|  |  |  |                 while (!SEP_OR_END(&path[idx])) { | 
					
						
							|  |  |  |                     idx++; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 *drvsize = idx; | 
					
						
							|  |  |  |                 if (IS_END(&path[idx])) { | 
					
						
							|  |  |  |                     *rootsize = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     *rootsize = 1; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // Relative path with root, e.g. \Windows
 | 
					
						
							|  |  |  |             *drvsize = 0; | 
					
						
							|  |  |  |             *rootsize = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!IS_END(&path[0]) && path[1] == L':') { | 
					
						
							|  |  |  |         *drvsize = 2; | 
					
						
							|  |  |  |         if (IS_SEP(&path[2])) { | 
					
						
							|  |  |  |             // Absolute drive-letter path, e.g. X:\Windows
 | 
					
						
							|  |  |  |             *rootsize = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // Relative path with drive, e.g. X:Windows
 | 
					
						
							|  |  |  |             *rootsize = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // Relative path, e.g. Windows
 | 
					
						
							|  |  |  |         *drvsize = 0; | 
					
						
							|  |  |  |         *rootsize = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #undef SEP_OR_END
 | 
					
						
							|  |  |  | #undef IS_SEP
 | 
					
						
							|  |  |  | #undef IS_END
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  | // The caller must ensure "buffer" is big enough.
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | join_relfile(wchar_t *buffer, size_t bufsize, | 
					
						
							|  |  |  |              const wchar_t *dirname, const wchar_t *relfile) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2022-03-05 17:47:00 +02:00
										 |  |  |     if (FAILED(PathCchCombineEx(buffer, bufsize, dirname, relfile, | 
					
						
							| 
									
										
										
										
											2022-01-08 00:06:53 +00:00
										 |  |  |         PATHCCH_ALLOW_LONG_PATHS))) { | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     assert(!_Py_isabs(relfile)); | 
					
						
							|  |  |  |     size_t dirlen = wcslen(dirname); | 
					
						
							|  |  |  |     size_t rellen = wcslen(relfile); | 
					
						
							|  |  |  |     size_t maxlen = bufsize - 1; | 
					
						
							|  |  |  |     if (maxlen > MAXPATHLEN || dirlen >= maxlen || rellen >= maxlen - dirlen) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dirlen == 0) { | 
					
						
							|  |  |  |         // We do not add a leading separator.
 | 
					
						
							|  |  |  |         wcscpy(buffer, relfile); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         if (dirname != buffer) { | 
					
						
							|  |  |  |             wcscpy(buffer, dirname); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         size_t relstart = dirlen; | 
					
						
							|  |  |  |         if (dirlen > 1 && dirname[dirlen - 1] != SEP) { | 
					
						
							|  |  |  |             buffer[dirlen] = SEP; | 
					
						
							|  |  |  |             relstart += 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         wcscpy(&buffer[relstart], relfile); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Join the two paths together, like os.path.join().  Return NULL
 | 
					
						
							|  |  |  |    if memory could not be allocated.  The caller is responsible | 
					
						
							|  |  |  |    for calling PyMem_RawFree() on the result. */ | 
					
						
							|  |  |  | wchar_t * | 
					
						
							|  |  |  | _Py_join_relfile(const wchar_t *dirname, const wchar_t *relfile) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(dirname != NULL && relfile != NULL); | 
					
						
							| 
									
										
										
										
											2021-09-27 10:52:19 -06:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  |     assert(!_Py_isabs(relfile)); | 
					
						
							| 
									
										
										
										
											2021-09-27 10:52:19 -06:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  |     size_t maxlen = wcslen(dirname) + 1 + wcslen(relfile); | 
					
						
							|  |  |  |     size_t bufsize = maxlen + 1; | 
					
						
							|  |  |  |     wchar_t *filename = PyMem_RawMalloc(bufsize * sizeof(wchar_t)); | 
					
						
							|  |  |  |     if (filename == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(wcslen(dirname) < MAXPATHLEN); | 
					
						
							|  |  |  |     assert(wcslen(relfile) < MAXPATHLEN - wcslen(dirname)); | 
					
						
							| 
									
										
										
										
											2023-03-15 13:58:43 +01:00
										 |  |  |     if (join_relfile(filename, bufsize, dirname, relfile) < 0) { | 
					
						
							|  |  |  |         PyMem_RawFree(filename); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-27 10:00:32 -06:00
										 |  |  |     return filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Join the two paths together, like os.path.join().
 | 
					
						
							|  |  |  |      dirname: the target buffer with the dirname already in place, | 
					
						
							|  |  |  |               including trailing NUL | 
					
						
							|  |  |  |      relfile: this must be a relative path | 
					
						
							|  |  |  |      bufsize: total allocated size of the buffer | 
					
						
							|  |  |  |    Return -1 if anything is wrong with the path lengths. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_add_relfile(wchar_t *dirname, const wchar_t *relfile, size_t bufsize) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(dirname != NULL && relfile != NULL); | 
					
						
							|  |  |  |     assert(bufsize > 0); | 
					
						
							|  |  |  |     return join_relfile(dirname, bufsize, dirname, relfile); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 13:16:08 -06:00
										 |  |  | size_t | 
					
						
							|  |  |  | _Py_find_basename(const wchar_t *filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (size_t i = wcslen(filename); i > 0; --i) { | 
					
						
							|  |  |  |         if (filename[i] == SEP) { | 
					
						
							|  |  |  |             return i + 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | /* In-place path normalisation. Returns the start of the normalized
 | 
					
						
							|  |  |  |    path, which will be within the original buffer. Guaranteed to not | 
					
						
							|  |  |  |    make the path longer, and will not fail. 'size' is the length of | 
					
						
							|  |  |  |    the path, if known. If -1, the first null character will be assumed | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  |    to be the end of the path. 'normsize' will be set to contain the | 
					
						
							|  |  |  |    length of the resulting normalized path. */ | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | wchar_t * | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  | _Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t *normsize) | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-15 13:58:43 +01:00
										 |  |  |     assert(path != NULL); | 
					
						
							| 
									
										
										
										
											2023-08-15 18:02:32 +01:00
										 |  |  |     if ((size < 0 && !path[0]) || size == 0) { | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  |         *normsize = 0; | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |         return path; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     wchar_t *pEnd = size >= 0 ? &path[size] : NULL; | 
					
						
							| 
									
										
										
										
											2022-01-07 04:13:10 +09:00
										 |  |  |     wchar_t *p1 = path;     // sequentially scanned address in the path
 | 
					
						
							|  |  |  |     wchar_t *p2 = path;     // destination of a scanned character to be ljusted
 | 
					
						
							|  |  |  |     wchar_t *minP2 = path;  // the beginning of the destination range
 | 
					
						
							|  |  |  |     wchar_t lastC = L'\0';  // the last ljusted character, p2[-1] in most cases
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define IS_END(x) (pEnd ? (x) == pEnd : !*(x))
 | 
					
						
							|  |  |  | #ifdef ALTSEP
 | 
					
						
							|  |  |  | #define IS_SEP(x) (*(x) == SEP || *(x) == ALTSEP)
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define IS_SEP(x) (*(x) == SEP)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #define SEP_OR_END(x) (IS_SEP(x) || IS_END(x))
 | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |     Py_ssize_t drvsize, rootsize; | 
					
						
							|  |  |  |     _Py_skiproot(path, size, &drvsize, &rootsize); | 
					
						
							|  |  |  |     if (drvsize || rootsize) { | 
					
						
							|  |  |  |         // Skip past root and update minP2
 | 
					
						
							|  |  |  |         p1 = &path[drvsize + rootsize]; | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  | #ifndef ALTSEP
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         p2 = p1; | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         for (; p2 < p1; ++p2) { | 
					
						
							|  |  |  |             if (*p2 == ALTSEP) { | 
					
						
							|  |  |  |                 *p2 = SEP; | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         minP2 = p2 - 1; | 
					
						
							|  |  |  |         lastC = *minP2; | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         if (lastC != SEP) { | 
					
						
							|  |  |  |             minP2++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (p1[0] == L'.' && SEP_OR_END(&p1[1])) { | 
					
						
							|  |  |  |         // Skip leading '.\'
 | 
					
						
							|  |  |  |         lastC = *++p1; | 
					
						
							|  |  |  | #ifdef ALTSEP
 | 
					
						
							|  |  |  |         if (lastC == ALTSEP) { | 
					
						
							|  |  |  |             lastC = SEP; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-25 11:07:38 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-11-21 15:43:36 +01:00
										 |  |  |         while (IS_SEP(p1)) { | 
					
						
							|  |  |  |             p1++; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* if pEnd is specified, check that. Else, check for null terminator */ | 
					
						
							|  |  |  |     for (; !IS_END(p1); ++p1) { | 
					
						
							|  |  |  |         wchar_t c = *p1; | 
					
						
							|  |  |  | #ifdef ALTSEP
 | 
					
						
							|  |  |  |         if (c == ALTSEP) { | 
					
						
							|  |  |  |             c = SEP; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |         if (lastC == SEP) { | 
					
						
							|  |  |  |             if (c == L'.') { | 
					
						
							|  |  |  |                 int sep_at_1 = SEP_OR_END(&p1[1]); | 
					
						
							|  |  |  |                 int sep_at_2 = !sep_at_1 && SEP_OR_END(&p1[2]); | 
					
						
							|  |  |  |                 if (sep_at_2 && p1[1] == L'.') { | 
					
						
							|  |  |  |                     wchar_t *p3 = p2; | 
					
						
							|  |  |  |                     while (p3 != minP2 && *--p3 == SEP) { } | 
					
						
							|  |  |  |                     while (p3 != minP2 && *(p3 - 1) != SEP) { --p3; } | 
					
						
							| 
									
										
										
										
											2022-01-07 04:13:10 +09:00
										 |  |  |                     if (p2 == minP2 | 
					
						
							|  |  |  |                         || (p3[0] == L'.' && p3[1] == L'.' && IS_SEP(&p3[2]))) | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         // Previous segment is also ../, so append instead.
 | 
					
						
							|  |  |  |                         // Relative path does not absorb ../ at minP2 as well.
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                         *p2++ = L'.'; | 
					
						
							|  |  |  |                         *p2++ = L'.'; | 
					
						
							|  |  |  |                         lastC = L'.'; | 
					
						
							|  |  |  |                     } else if (p3[0] == SEP) { | 
					
						
							|  |  |  |                         // Absolute path, so absorb segment
 | 
					
						
							|  |  |  |                         p2 = p3 + 1; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         p2 = p3; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     p1 += 1; | 
					
						
							|  |  |  |                 } else if (sep_at_1) { | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     *p2++ = lastC = c; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else if (c == SEP) { | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 *p2++ = lastC = c; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             *p2++ = lastC = c; | 
					
						
							| 
									
										
										
										
											2022-01-07 04:13:10 +09:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     *p2 = L'\0'; | 
					
						
							|  |  |  |     if (p2 != minP2) { | 
					
						
							|  |  |  |         while (--p2 != minP2 && *p2 == SEP) { | 
					
						
							|  |  |  |             *p2 = L'\0'; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         --p2; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  |     *normsize = p2 - path + 1; | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | #undef SEP_OR_END
 | 
					
						
							|  |  |  | #undef IS_SEP
 | 
					
						
							|  |  |  | #undef IS_END
 | 
					
						
							|  |  |  |     return path; | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-15 08:33:00 -07:00
										 |  |  | /* In-place path normalisation. Returns the start of the normalized
 | 
					
						
							|  |  |  |    path, which will be within the original buffer. Guaranteed to not | 
					
						
							|  |  |  |    make the path longer, and will not fail. 'size' is the length of | 
					
						
							|  |  |  |    the path, if known. If -1, the first null character will be assumed | 
					
						
							|  |  |  |    to be the end of the path. */ | 
					
						
							|  |  |  | wchar_t * | 
					
						
							|  |  |  | _Py_normpath(wchar_t *path, Py_ssize_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t norm_length; | 
					
						
							|  |  |  |     return _Py_normpath_and_size(path, size, &norm_length); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-22 17:20:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 02:58:14 +01:00
										 |  |  | /* Get the current directory. buflen is the buffer size in wide characters
 | 
					
						
							| 
									
										
										
										
											2011-12-16 23:56:01 +01:00
										 |  |  |    including the null character. Decode the path from the locale encoding. | 
					
						
							| 
									
										
										
										
											2010-10-07 22:53:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |    Return NULL on getcwd() error, on decoding error, or if 'buf' is | 
					
						
							|  |  |  |    too short. */ | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | wchar_t* | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  | _Py_wgetcwd(wchar_t *buf, size_t buflen) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     int ibuflen = (int)Py_MIN(buflen, INT_MAX); | 
					
						
							|  |  |  |     return _wgetcwd(buf, ibuflen); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-11-15 18:14:11 +01:00
										 |  |  |     char fname[MAXPATHLEN]; | 
					
						
							| 
									
										
										
										
											2010-10-14 12:37:19 +00:00
										 |  |  |     wchar_t *wname; | 
					
						
							| 
									
										
										
										
											2010-10-16 23:16:16 +00:00
										 |  |  |     size_t len; | 
					
						
							| 
									
										
										
										
											2010-10-14 12:37:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-15 18:14:11 +01:00
										 |  |  |     if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2014-08-01 12:28:48 +02:00
										 |  |  |     wname = Py_DecodeLocale(fname, &len); | 
					
						
							| 
									
										
										
										
											2010-10-14 12:37:19 +00:00
										 |  |  |     if (wname == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     /* wname must have space to store the trailing NUL character */ | 
					
						
							|  |  |  |     if (buflen <= len) { | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |         PyMem_RawFree(wname); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 17:47:26 +01:00
										 |  |  |     wcsncpy(buf, wname, buflen); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:25:15 +02:00
										 |  |  |     PyMem_RawFree(wname); | 
					
						
							| 
									
										
										
										
											2010-10-07 21:45:39 +00:00
										 |  |  |     return buf; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | /* Duplicate a file descriptor. The new file descriptor is created as
 | 
					
						
							|  |  |  |    non-inheritable. Return a new file descriptor on success, raise an OSError | 
					
						
							|  |  |  |    exception and return -1 on error. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The GIL is released to call dup(). The caller must hold the GIL. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_dup(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  |     HANDLE handle; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2016-03-14 22:07:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  |     handle = _Py_get_osfhandle(fd); | 
					
						
							|  |  |  |     if (handle == INVALID_HANDLE_VALUE) | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     fd = dup(fd); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 01:17:14 -06:00
										 |  |  |     if (_Py_set_inheritable(fd, 0, NULL) < 0) { | 
					
						
							|  |  |  |         _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  |         close(fd); | 
					
						
							|  |  |  |         _Py_END_SUPPRESS_IPH | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
 | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 11:16:51 +02:00
										 |  |  | #elif HAVE_DUP
 | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     fd = dup(fd); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_Py_set_inheritable(fd, 0, NULL) < 0) { | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         close(fd); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-26 11:16:51 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     errno = ENOTSUP; | 
					
						
							|  |  |  |     PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2013-08-28 00:53:59 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  | #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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     int flags; | 
					
						
							|  |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  |     flags = fcntl(fd, F_GETFL, 0); | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  |     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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-08 04:41:12 +08:00
										 |  |  | /* bpo-41462: On VxWorks, ioctl(FIONBIO) only works on sockets.
 | 
					
						
							|  |  |  |    Use fcntl() instead. */ | 
					
						
							|  |  |  | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) && !defined(__VXWORKS__)
 | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  |     int arg = !blocking; | 
					
						
							|  |  |  |     if (ioctl(fd, FIONBIO, &arg) < 0) | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     int flags, res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  |     flags = fcntl(fd, F_GETFL, 0); | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |     if (flags >= 0) { | 
					
						
							|  |  |  |         if (blocking) | 
					
						
							|  |  |  |             flags = flags & (~O_NONBLOCK); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             flags = flags | O_NONBLOCK; | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 00:26:27 -04:00
										 |  |  |         res = fcntl(fd, F_SETFL, flags); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         res = -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							| 
									
										
										
										
											2014-07-29 22:32:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res < 0) | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  | #else   /* MS_WINDOWS */
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_get_blocking(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     HANDLE handle; | 
					
						
							|  |  |  |     DWORD mode; | 
					
						
							|  |  |  |     BOOL success; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handle = _Py_get_osfhandle(fd); | 
					
						
							|  |  |  |     if (handle == INVALID_HANDLE_VALUE) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     success = GetNamedPipeHandleStateW(handle, &mode, | 
					
						
							|  |  |  |                                        NULL, NULL, NULL, NULL, 0); | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |     if (!success) { | 
					
						
							|  |  |  |         PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-09 22:09:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 14:52:24 +00:00
										 |  |  |     return !(mode & PIPE_NOWAIT); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_set_blocking(int fd, int blocking) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     HANDLE handle; | 
					
						
							|  |  |  |     DWORD mode; | 
					
						
							|  |  |  |     BOOL success; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handle = _Py_get_osfhandle(fd); | 
					
						
							|  |  |  |     if (handle == INVALID_HANDLE_VALUE) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     success = GetNamedPipeHandleStateW(handle, &mode, | 
					
						
							|  |  |  |                                        NULL, NULL, NULL, NULL, 0); | 
					
						
							|  |  |  |     if (success) { | 
					
						
							|  |  |  |         if (blocking) { | 
					
						
							|  |  |  |             mode &= ~PIPE_NOWAIT; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             mode |= PIPE_NOWAIT; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         success = SetNamedPipeHandleState(handle, &mode, NULL, NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!success) { | 
					
						
							|  |  |  |         PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  | void* | 
					
						
							|  |  |  | _Py_get_osfhandle_noraise(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *handle; | 
					
						
							|  |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  |     handle = (void*)_get_osfhandle(fd); | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							|  |  |  |     return handle; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 01:00:27 +03:00
										 |  |  | void* | 
					
						
							|  |  |  | _Py_get_osfhandle(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *handle = _Py_get_osfhandle_noraise(fd); | 
					
						
							|  |  |  |     if (handle == INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return handle; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_open_osfhandle_noraise(void *handle, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd; | 
					
						
							|  |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  |     fd = _open_osfhandle((intptr_t)handle, flags); | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_open_osfhandle(void *handle, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int fd = _Py_open_osfhandle_noraise(handle, flags); | 
					
						
							|  |  |  |     if (fd == -1) | 
					
						
							|  |  |  |         PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return fd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif  /* MS_WINDOWS */
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  | _Py_GetLocaleconvNumeric(struct lconv *lc, | 
					
						
							|  |  |  |                          PyObject **decimal_point, PyObject **thousands_sep) | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     assert(decimal_point != NULL); | 
					
						
							|  |  |  |     assert(thousands_sep != NULL); | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |     int change_locale = 0; | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) { | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |         change_locale = 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     if ((strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127)) { | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |         change_locale = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Keep a copy of the LC_CTYPE locale */ | 
					
						
							|  |  |  |     char *oldloc = NULL, *loc = NULL; | 
					
						
							|  |  |  |     if (change_locale) { | 
					
						
							|  |  |  |         oldloc = setlocale(LC_CTYPE, NULL); | 
					
						
							|  |  |  |         if (!oldloc) { | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |             PyErr_SetString(PyExc_RuntimeWarning, | 
					
						
							|  |  |  |                             "failed to get LC_CTYPE locale"); | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         oldloc = _PyMem_Strdup(oldloc); | 
					
						
							|  |  |  |         if (!oldloc) { | 
					
						
							|  |  |  |             PyErr_NoMemory(); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         loc = setlocale(LC_NUMERIC, NULL); | 
					
						
							|  |  |  |         if (loc != NULL && strcmp(loc, oldloc) == 0) { | 
					
						
							|  |  |  |             loc = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (loc != NULL) { | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |             /* Only set the locale temporarily the LC_CTYPE locale
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |                if LC_NUMERIC locale is different than LC_CTYPE locale and | 
					
						
							|  |  |  |                decimal_point and/or thousands_sep are non-ASCII or longer than | 
					
						
							|  |  |  |                1 byte */ | 
					
						
							|  |  |  |             setlocale(LC_CTYPE, loc); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  | #define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL)
 | 
					
						
							|  |  |  | #else /* MS_WINDOWS */
 | 
					
						
							|  |  |  | /* Use _W_* fields of Windows strcut lconv */ | 
					
						
							|  |  |  | #define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1)
 | 
					
						
							|  |  |  | #endif /* MS_WINDOWS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     int res = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  |     *decimal_point = GET_LOCALE_STRING(decimal_point); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     if (*decimal_point == NULL) { | 
					
						
							|  |  |  |         goto done; | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  |     *thousands_sep = GET_LOCALE_STRING(thousands_sep); | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  |     if (*thousands_sep == NULL) { | 
					
						
							|  |  |  |         goto done; | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 16:20:16 +01:00
										 |  |  | done: | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  | #ifndef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |     if (loc != NULL) { | 
					
						
							|  |  |  |         setlocale(LC_CTYPE, oldloc); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyMem_Free(oldloc); | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2020-10-20 08:39:52 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #undef GET_LOCALE_STRING
 | 
					
						
							| 
									
										
										
										
											2018-01-15 15:58:02 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-13 15:04:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Our selection logic for which function to use is as follows:
 | 
					
						
							|  |  |  |  * 1. If close_range(2) is available, always prefer that; it's better for | 
					
						
							|  |  |  |  *    contiguous ranges like this than fdwalk(3) which entails iterating over | 
					
						
							|  |  |  |  *    the entire fd space and simply doing nothing for those outside the range. | 
					
						
							|  |  |  |  * 2. If closefrom(2) is available, we'll attempt to use that next if we're | 
					
						
							|  |  |  |  *    closing up to sysconf(_SC_OPEN_MAX). | 
					
						
							|  |  |  |  * 2a. Fallback to fdwalk(3) if we're not closing up to sysconf(_SC_OPEN_MAX), | 
					
						
							|  |  |  |  *    as that will be more performant if the range happens to have any chunk of | 
					
						
							|  |  |  |  *    non-opened fd in the middle. | 
					
						
							|  |  |  |  * 2b. If fdwalk(3) isn't available, just do a plain close(2) loop. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-12-12 10:25:27 +00:00
										 |  |  | #ifdef HAVE_CLOSEFROM
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:04:44 -05:00
										 |  |  | #  define USE_CLOSEFROM
 | 
					
						
							| 
									
										
										
										
											2023-12-12 10:25:27 +00:00
										 |  |  | #endif /* HAVE_CLOSEFROM */
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:04:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_FDWALK
 | 
					
						
							|  |  |  | #  define USE_FDWALK
 | 
					
						
							|  |  |  | #endif /* HAVE_FDWALK */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef USE_FDWALK
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _fdwalk_close_func(void *lohi, int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int lo = ((int *)lohi)[0]; | 
					
						
							|  |  |  |     int hi = ((int *)lohi)[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fd >= hi) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (fd >= lo) { | 
					
						
							|  |  |  |         /* Ignore errors */ | 
					
						
							|  |  |  |         (void)close(fd); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /* USE_FDWALK */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Closes all file descriptors in [first, last], ignoring errors. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_closerange(int first, int last) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     first = Py_MAX(first, 0); | 
					
						
							|  |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  | #ifdef HAVE_CLOSE_RANGE
 | 
					
						
							| 
									
										
										
										
											2022-04-08 20:40:39 +03:00
										 |  |  |     if (close_range(first, last, 0) == 0) { | 
					
						
							|  |  |  |         /* close_range() ignores errors when it closes file descriptors.
 | 
					
						
							|  |  |  |          * Possible reasons of an error return are lack of kernel support | 
					
						
							|  |  |  |          * or denial of the underlying syscall by a seccomp sandbox on Linux. | 
					
						
							|  |  |  |          * Fallback to other methods in case of any error. */ | 
					
						
							| 
									
										
										
										
											2020-10-13 15:04:44 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  | #endif /* HAVE_CLOSE_RANGE */
 | 
					
						
							|  |  |  | #ifdef USE_CLOSEFROM
 | 
					
						
							|  |  |  |     if (last >= sysconf(_SC_OPEN_MAX)) { | 
					
						
							|  |  |  |         /* Any errors encountered while closing file descriptors are ignored */ | 
					
						
							| 
									
										
										
										
											2023-12-12 10:25:27 +00:00
										 |  |  |         (void)closefrom(first); | 
					
						
							| 
									
										
										
										
											2020-10-13 15:04:44 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  | #endif /* USE_CLOSEFROM */
 | 
					
						
							|  |  |  | #ifdef USE_FDWALK
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int lohi[2]; | 
					
						
							|  |  |  |         lohi[0] = first; | 
					
						
							|  |  |  |         lohi[1] = last + 1; | 
					
						
							|  |  |  |         fdwalk(_fdwalk_close_func, lohi); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (int i = first; i <= last; i++) { | 
					
						
							|  |  |  |             /* Ignore errors */ | 
					
						
							|  |  |  |             (void)close(i); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif /* USE_FDWALK */
 | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-12-01 17:05:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MS_WINDOWS
 | 
					
						
							|  |  |  | // Ticks per second used by clock() and times() functions.
 | 
					
						
							|  |  |  | // See os.times() and time.process_time() implementations.
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_GetTicksPerSecond(long *ticks_per_second) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
 | 
					
						
							|  |  |  |     long value = sysconf(_SC_CLK_TCK); | 
					
						
							|  |  |  |     if (value < 1) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *ticks_per_second = value; | 
					
						
							|  |  |  | #elif defined(HZ)
 | 
					
						
							|  |  |  |     assert(HZ >= 1); | 
					
						
							|  |  |  |     *ticks_per_second = HZ; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // Magic fallback value; may be bogus
 | 
					
						
							|  |  |  |     *ticks_per_second = 60; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-04-30 22:32:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Check if a file descriptor is valid or not.
 | 
					
						
							|  |  |  |    Return 0 if the file descriptor is invalid, return non-zero otherwise. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _Py_IsValidFD(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | /* dup() is faster than fstat(): fstat() can require input/output operations,
 | 
					
						
							|  |  |  |    whereas dup() doesn't. There is a low risk of EMFILE/ENFILE at Python | 
					
						
							|  |  |  |    startup. Problem: dup() doesn't check if the file descriptor is valid on | 
					
						
							|  |  |  |    some platforms. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    fcntl(fd, F_GETFD) is even faster, because it only checks the process table. | 
					
						
							|  |  |  |    It is preferred over dup() when available, since it cannot fail with the | 
					
						
							|  |  |  |    "too many open files" error (EMFILE). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other | 
					
						
							|  |  |  |    side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with | 
					
						
							|  |  |  |    EBADF. FreeBSD has similar issue (bpo-32849). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Only use dup() on Linux where dup() is enough to detect invalid FD | 
					
						
							|  |  |  |    (bpo-32849). | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #if defined(F_GETFD) && ( \
 | 
					
						
							|  |  |  |         defined(__linux__) || \ | 
					
						
							|  |  |  |         defined(__APPLE__) || \ | 
					
						
							|  |  |  |         (defined(__wasm__) && !defined(__wasi__))) | 
					
						
							|  |  |  |     return fcntl(fd, F_GETFD) >= 0; | 
					
						
							|  |  |  | #elif defined(__linux__)
 | 
					
						
							|  |  |  |     int fd2 = dup(fd); | 
					
						
							|  |  |  |     if (fd2 >= 0) { | 
					
						
							|  |  |  |         close(fd2); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (fd2 >= 0); | 
					
						
							|  |  |  | #elif defined(MS_WINDOWS)
 | 
					
						
							|  |  |  |     HANDLE hfile; | 
					
						
							|  |  |  |     _Py_BEGIN_SUPPRESS_IPH | 
					
						
							|  |  |  |     hfile = (HANDLE)_get_osfhandle(fd); | 
					
						
							|  |  |  |     _Py_END_SUPPRESS_IPH | 
					
						
							|  |  |  |     return (hfile != INVALID_HANDLE_VALUE | 
					
						
							|  |  |  |             && GetFileType(hfile) != FILE_TYPE_UNKNOWN); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     struct stat st; | 
					
						
							|  |  |  |     return (fstat(fd, &st) == 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } |