同步操作将从 OpenHarmony-SIG/python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "Python.h"#include "pycore_fileutils.h"#include "osdefs.h"#include <locale.h>#ifdef MS_WINDOWS# include <malloc.h># include <windows.h>extern int winerror_to_errno(int);#endif#ifdef HAVE_LANGINFO_H#include <langinfo.h>#endif#ifdef HAVE_SYS_IOCTL_H#include <sys/ioctl.h>#endif#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif /* HAVE_FCNTL_H */#ifdef O_CLOEXEC/* Does open() support the O_CLOEXEC flag? Possible values:-1: unknown0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.231: open() supports O_CLOEXEC flag, close-on-exec is setThe flag is used by _Py_open(), _Py_open_noraise(), io.FileIOand os.open(). */int _Py_open_cloexec_works = -1;#endif// The value must be the same in unicodeobject.c.#define MAX_UNICODE 0x10ffff// mbstowcs() and mbrtowc() errorsstatic const size_t DECODE_ERROR = ((size_t)-1);static const size_t INCOMPLETE_CHARACTER = (size_t)-2;static intget_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;}}PyObject *_Py_device_encoding(int fd){#if defined(MS_WINDOWS)UINT cp;#endifint valid;_Py_BEGIN_SUPPRESS_IPHvalid = isatty(fd);_Py_END_SUPPRESS_IPHif (!valid)Py_RETURN_NONE;#if defined(MS_WINDOWS)if (fd == 0)cp = GetConsoleCP();else if (fd == 1 || fd == 2)cp = GetConsoleOutputCP();elsecp = 0;/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the applicationhas no console */if (cp != 0)return PyUnicode_FromFormat("cp%u", (unsigned int)cp);#elif defined(CODESET){char *codeset = nl_langinfo(CODESET);if (codeset != NULL && codeset[0] != 0)return PyUnicode_FromString(codeset);}#endifPy_RETURN_NONE;}static size_tis_valid_wide_char(wchar_t ch){if (Py_UNICODE_IS_SURROGATE(ch)) {// Reject lone surrogate charactersreturn 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=2373return 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_MBRTOWCstatic 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#if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS)#define USE_FORCE_ASCIIextern int _Py_normalize_encoding(const char *, char *, size_t);/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C localeand POSIX locale. nl_langinfo(CODESET) announces an alias of theASCII encoding, whereas mbstowcs() and wcstombs() functions use theISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() uselocale.getpreferredencoding() codec. For example, if command line argumentsare decoded by mbstowcs() and encoded back by os.fsencode(), we get aUnicodeEncodeError 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 leastone byte in range 0x80-0xff can be decoded from the locale encoding. Theworkaround is also enabled on error, for example if getting the localefailed.On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET)announces "roman8" but mbstowcs() uses Latin1 in practice. Force also theASCII encoding in this case.Values of force_ascii:1: the workaround is used: Py_EncodeLocale() usesencode_ascii_surrogateescape() and Py_DecodeLocale() usesdecode_ascii()0: the workaround is not used: Py_EncodeLocale() uses wcstombs() andPy_DecodeLocale() uses mbstowcs()-1: unknown, need to call check_force_ascii() to get the value*/static int force_ascii = -1;static intcheck_force_ascii(void){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;}#if defined(HAVE_LANGINFO_H) && defined(CODESET)const char *codeset = nl_langinfo(CODESET);if (!codeset || codeset[0] == '0円') {/* CODESET is not set or empty */goto error;}char encoding[20]; /* longest name: "iso_646.irv_19910円" */if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) {goto error;}#ifdef __hpuxif (strcmp(encoding, "roman8") == 0) {unsigned char ch;wchar_t wch;size_t res;ch = (unsigned char)0xA7;res = _Py_mbstowcs(&wch, (char*)&ch, 1);if (res != DECODE_ERROR && wch == L'\xA7') {/* On HP-UX withe C locale or the POSIX locale,nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() usesLatin1 encoding in practice. Force ASCII in this case.Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */return 1;}}#elseconst char* ascii_aliases[] = {"ascii",/* Aliases from Lib/encodings/aliases.py */"646","ansi_x3.4_1968","ansi_x3.4_1986","ansi_x3_4_1968","cp367","csascii","ibm367","iso646_us","iso_646.irv_1991","iso_ir_6","us","us_ascii",NULL};int is_ascii = 0;for (const char **alias=ascii_aliases; *alias != NULL; alias++) {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;}for (unsigned int i=0x80; i<=0xff; i++) {char ch[1];wchar_t wch[1];size_t res;unsigned uch = (unsigned char)i;ch[0] = (char)uch;res = _Py_mbstowcs(wch, ch, 1);if (res != DECODE_ERROR) {/* 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 localeencoding: the locale encoding is really ASCII */#endif /* !defined(__hpux) */return 0;#else/* nl_langinfo(CODESET) is not available: always force ASCII */return 1;#endif /* defined(HAVE_LANGINFO_H) && defined(CODESET) */error:/* if an error occurred, force the ASCII encoding */return 1;}int_Py_GetForceASCII(void){if (force_ascii == -1) {force_ascii = check_force_ascii();}return force_ascii;}void_Py_ResetForceASCII(void){force_ascii = -1;}static intencode_ascii(const wchar_t *text, char **str,size_t *error_pos, const char **reason,int raw_malloc, _Py_error_handler errors){char *result = NULL, *out;size_t len, i;wchar_t ch;int surrogateescape;if (get_surrogateescape(errors, &surrogateescape) < 0) {return -3;}len = wcslen(text);/* +1 for NULL byte */if (raw_malloc) {result = PyMem_RawMalloc(len + 1);}else {result = PyMem_Malloc(len + 1);}if (result == NULL) {return -1;}out = result;for (i=0; i<len; i++) {ch = text[i];if (ch <= 0x7f) {/* ASCII character */*out++ = (char)ch;}else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) {/* UTF-8b surrogate */*out++ = (char)(ch - 0xdc00);}else {if (raw_malloc) {PyMem_RawFree(result);}else {PyMem_Free(result);}if (error_pos != NULL) {*error_pos = i;}if (reason) {*reason = "encoding error";}return -2;}}*out = '0円';*str = result;return 0;}#elseint_Py_GetForceASCII(void){return 0;}void_Py_ResetForceASCII(void){/* nothing to do */}#endif /* !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS) */#if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)static intdecode_ascii(const char *arg, wchar_t **wstr, size_t *wlen,const char **reason, _Py_error_handler errors){wchar_t *res;unsigned char *in;wchar_t *out;size_t argsize = strlen(arg) + 1;int surrogateescape;if (get_surrogateescape(errors, &surrogateescape) < 0) {return -3;}if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {return -1;}res = PyMem_RawMalloc(argsize * sizeof(wchar_t));if (!res) {return -1;}out = res;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;}}*out = 0;if (wlen != NULL) {*wlen = out - res;}*wstr = res;return 0;}#endif /* !HAVE_MBRTOWC */static intdecode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen,const char **reason, _Py_error_handler errors){wchar_t *res;size_t argsize;size_t count;#ifdef HAVE_MBRTOWCunsigned char *in;wchar_t *out;mbstate_t mbs;#endifint surrogateescape;if (get_surrogateescape(errors, &surrogateescape) < 0) {return -3;}#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.*/argsize = strlen(arg);#elseargsize = _Py_mbstowcs(NULL, arg, 0);#endifif (argsize != DECODE_ERROR) {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;}count = _Py_mbstowcs(res, arg, argsize + 1);if (count != DECODE_ERROR) {*wstr = res;if (wlen != NULL) {*wlen = count;}return 0;}PyMem_RawFree(res);}/* 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, theactual output could use less memory. */argsize = strlen(arg) + 1;if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {return -1;}res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t));if (!res) {return -1;}in = (unsigned char*)arg;out = res;memset(&mbs, 0, sizeof mbs);while (argsize) {size_t converted = _Py_mbrtowc(out, (char*)in, argsize, &mbs);if (converted == 0) {/* Reached end of string; null char stored. */break;}if (converted == INCOMPLETE_CHARACTER) {/* Incomplete character. This should never happen,since we provide everything that we have -unless there is a bug in the C library, or Imisunderstood how mbrtowc works. */goto decode_error;}if (converted == DECODE_ERROR) {if (!surrogateescape) {goto decode_error;}/* Decoding error. Escape as UTF-8b, and start over in the initialshift state. */*out++ = 0xdc00 + *in++;argsize--;memset(&mbs, 0, sizeof mbs);continue;}// _Py_mbrtowc() reject lone surrogate charactersassert(!Py_UNICODE_IS_SURROGATE(*out));/* successfully converted some bytes */in += converted;argsize -= converted;out++;}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;#else /* HAVE_MBRTOWC *//* Cannot use C locale for escaping; manually escape as if charsetis ASCII (i.e. escape all bytes > 128. This will still roundtripcorrectly in the locale's charset, which must be an ASCII superset. */return decode_ascii(arg, wstr, wlen, reason, errors);#endif /* HAVE_MBRTOWC */}/* Decode a byte string from the locale encoding.Use the strict error handler if 'surrogateescape' is zero. Use thesurrogateescape error handler if 'surrogateescape' is non-zero: undecodablebytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequencecan be decoded as a surrogate character, escape the bytes using thesurrogateescape error handler instead of decoding them.On success, return 0 and write the newly allocated wide character string into*wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, writethe 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 ofinvalid byte sequence in the input string into *wlen. If reason is not NULL,write the decoding error message into *reason.Return -3 if the error handler 'errors' is not supported.Use the Py_EncodeLocaleEx() function to encode the character string back toa byte string. */int_Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,const char **reason,int current_locale, _Py_error_handler errors){if (current_locale) {#ifdef _Py_FORCE_UTF8_LOCALEreturn _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,errors);#elsereturn decode_current_locale(arg, wstr, wlen, reason, errors);#endif}#ifdef _Py_FORCE_UTF8_FS_ENCODINGreturn _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,errors);#elseint use_utf8 = (Py_UTF8Mode == 1);#ifdef MS_WINDOWSuse_utf8 |= !Py_LegacyWindowsFSEncodingFlag;#endifif (use_utf8) {return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,errors);}#ifdef USE_FORCE_ASCIIif (force_ascii == -1) {force_ascii = check_force_ascii();}if (force_ascii) {/* force ASCII encoding to workaround mbstowcs() issue */return decode_ascii(arg, wstr, wlen, reason, errors);}#endifreturn decode_current_locale(arg, wstr, wlen, reason, errors);#endif /* !_Py_FORCE_UTF8_FS_ENCODING */}/* Decode a byte string from the locale encoding with thesurrogateescape error handler: undecodable bytes are decoded as charactersin range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogatecharacter, escape the bytes using the surrogateescape error handler insteadof decoding them.Return a pointer to a newly allocated wide character string, usePyMem_RawFree() to free the memory. If size is not NULL, write the number ofwide characters excluding the null character into *sizeReturn NULL on decoding error or memory allocation error. If *size* is notNULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 ondecoding error.Decoding errors should never happen, unless there is a bug in the Clibrary.Use the Py_EncodeLocale() function to encode the character string back to abyte string. */wchar_t*Py_DecodeLocale(const char* arg, size_t *wlen){wchar_t *wstr;int res = _Py_DecodeLocaleEx(arg, &wstr, wlen,NULL, 0,_Py_ERROR_SURROGATEESCAPE);if (res != 0) {assert(res != -3);if (wlen != NULL) {*wlen = (size_t)res;}return NULL;}return wstr;}static intencode_current_locale(const wchar_t *text, char **str,size_t *error_pos, const char **reason,int raw_malloc, _Py_error_handler errors){const size_t len = wcslen(text);char *result = NULL, *bytes = NULL;size_t i, size, converted;wchar_t c, buf[2];int surrogateescape;if (get_surrogateescape(errors, &surrogateescape) < 0) {return -3;}/* 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) {if (!surrogateescape) {goto encode_error;}/* UTF-8b surrogate */if (bytes != NULL) {*bytes++ = c - 0xdc00;size--;}else {size++;}continue;}else {buf[0] = c;if (bytes != NULL) {converted = wcstombs(bytes, buf, size);}else {converted = wcstombs(NULL, buf, 0);}if (converted == DECODE_ERROR) {goto encode_error;}if (bytes != NULL) {bytes += converted;size -= converted;}else {size += converted;}}}if (result != NULL) {*bytes = '0円';break;}size += 1; /* nul byte at the end */if (raw_malloc) {result = PyMem_RawMalloc(size);}else {result = PyMem_Malloc(size);}if (result == NULL) {return -1;}bytes = result;}*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;}/* Encode a string to the locale encoding.Parameters:* raw_malloc: if non-zero, allocate memory using PyMem_RawMalloc() insteadof PyMem_Malloc().* current_locale: if non-zero, use the current LC_CTYPE, otherwise usePython 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.*/static intencode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,const char **reason,int raw_malloc, int current_locale, _Py_error_handler errors){if (current_locale) {#ifdef _Py_FORCE_UTF8_LOCALEreturn _Py_EncodeUTF8Ex(text, str, error_pos, reason,raw_malloc, errors);#elsereturn encode_current_locale(text, str, error_pos, reason,raw_malloc, errors);#endif}#ifdef _Py_FORCE_UTF8_FS_ENCODINGreturn _Py_EncodeUTF8Ex(text, str, error_pos, reason,raw_malloc, errors);#elseint use_utf8 = (Py_UTF8Mode == 1);#ifdef MS_WINDOWSuse_utf8 |= !Py_LegacyWindowsFSEncodingFlag;#endifif (use_utf8) {return _Py_EncodeUTF8Ex(text, str, error_pos, reason,raw_malloc, errors);}#ifdef USE_FORCE_ASCIIif (force_ascii == -1) {force_ascii = check_force_ascii();}if (force_ascii) {return encode_ascii(text, str, error_pos, reason,raw_malloc, errors);}#endifreturn encode_current_locale(text, str, error_pos, reason,raw_malloc, errors);#endif /* _Py_FORCE_UTF8_FS_ENCODING */}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,raw_malloc, current_locale,_Py_ERROR_SURROGATEESCAPE);if (res != -2 && error_pos) {*error_pos = (size_t)-1;}if (res != 0) {return NULL;}return str;}/* Encode a wide character string to the locale encoding with thesurrogateescape error handler: surrogate characters in the rangeU+DC80..U+DCFF are converted to bytes 0x80..0xFF.Return a pointer to a newly allocated byte string, use PyMem_Free() to freethe 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 setto the index of the invalid character on encoding error.Use the Py_DecodeLocale() function to decode the bytes string back to a widecharacter string. */char*Py_EncodeLocale(const wchar_t *text, size_t *error_pos){return encode_locale(text, error_pos, 0, 0);}/* 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){return encode_locale(text, error_pos, 1, 0);}int_Py_EncodeLocaleEx(const wchar_t *text, char **str,size_t *error_pos, const char **reason,int current_locale, _Py_error_handler errors){return encode_locale_ex(text, str, error_pos, reason, 1,current_locale, errors);}#ifdef MS_WINDOWSstatic __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */static voidFILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out){/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian *//* Cannot simply cast and dereference in_ptr,since it might not be aligned properly */__int64 in;memcpy(&in, in_ptr, sizeof(in));*nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */*time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);}void_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr){/* XXX endianness */__int64 out;out = time_in + secs_between_epochs;out = out * 10000000 + nsec_in / 100;memcpy(out_ptr, &out, sizeof(out));}/* Below, we *know* that ugo+r is 0444 */#if _S_IREAD != 0400#error Unsupported C library#endifstatic intattributes_to_mode(DWORD attr){int m = 0;if (attr & FILE_ATTRIBUTE_DIRECTORY)m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */elsem |= _S_IFREG;if (attr & FILE_ATTRIBUTE_READONLY)m |= 0444;elsem |= 0666;return m;}void_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,struct _Py_stat_struct *result){memset(result, 0, sizeof(*result));result->st_mode = attributes_to_mode(info->dwFileAttributes);result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;result->st_dev = info->dwVolumeSerialNumber;result->st_rdev = result->st_dev;FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);result->st_nlink = info->nNumberOfLinks;result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;/* bpo-37834: Only actual symlinks set the S_IFLNK flag. But lstat() willopen other name surrogate reparse points without traversing them. Todetect/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) {/* first clear the S_IFMT bits */result->st_mode ^= (result->st_mode & S_IFMT);/* now set the bits that make this a symlink */result->st_mode |= S_IFLNK;}result->st_file_attributes = info->dwFileAttributes;}#endif/* Return information about a file.On POSIX, use fstat().On Windows, use GetFileType() and GetFileInformationByHandle() which supportfiles larger than 2 GiB. fstat() may fail with EOVERFLOW on files largerthan 2 GiB because the file size type is a signed 32-bit integer: see issue#23152.On Windows, set the last Windows error and return nonzero on error. OnPOSIX, set errno and return nonzero on error. Fill status and return 0 onsuccess. */int_Py_fstat_noraise(int fd, struct _Py_stat_struct *status){#ifdef MS_WINDOWSBY_HANDLE_FILE_INFORMATION info;HANDLE h;int type;_Py_BEGIN_SUPPRESS_IPHh = (HANDLE)_get_osfhandle(fd);_Py_END_SUPPRESS_IPHif (h == INVALID_HANDLE_VALUE) {/* errno is already set by _get_osfhandle, but we also setthe Win32 error for callers who expect that */SetLastError(ERROR_INVALID_HANDLE);return -1;}memset(status, 0, sizeof(*status));type = GetFileType(h);if (type == FILE_TYPE_UNKNOWN) {DWORD error = GetLastError();if (error != 0) {errno = winerror_to_errno(error);return -1;}/* else: valid but unknown file */}if (type != FILE_TYPE_DISK) {if (type == FILE_TYPE_CHAR)status->st_mode = _S_IFCHR;else if (type == FILE_TYPE_PIPE)status->st_mode = _S_IFIFO;return 0;}if (!GetFileInformationByHandle(h, &info)) {/* The Win32 error is already set, but we also set errno forcallers who expect it */errno = winerror_to_errno(GetLastError());return -1;}_Py_attribute_data_to_stat(&info, 0, status);/* specific to fstat() */status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;return 0;#elsereturn fstat(fd, status);#endif}/* Return information about a file.On POSIX, use fstat().On Windows, use GetFileType() and GetFileInformationByHandle() which supportfiles larger than 2 GiB. fstat() may fail with EOVERFLOW on files largerthan 2 GiB because the file size type is a signed 32-bit integer: see issue#23152.Raise an exception and return -1 on error. On Windows, set the last Windowserror on error. On POSIX, set errno on error. Fill status and return 0 onsuccess.Release the GIL to call GetFileType() and GetFileInformationByHandle(), orto call fstat(). The caller must hold the GIL. */int_Py_fstat(int fd, struct _Py_stat_struct *status){int res;assert(PyGILState_Check());Py_BEGIN_ALLOW_THREADSres = _Py_fstat_noraise(fd, status);Py_END_ALLOW_THREADSif (res != 0) {#ifdef MS_WINDOWSPyErr_SetFromWindowsErr(0);#elsePyErr_SetFromErrno(PyExc_OSError);#endifreturn -1;}return 0;}/* Call _wstat() on Windows, or encode the path to the filesystem encoding andcall stat() otherwise. Only fill st_mode attribute on Windows.Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception wasraised. */int_Py_stat(PyObject *path, struct stat *statbuf){#ifdef MS_WINDOWSint err;struct _stat wstatbuf;const wchar_t *wpath;wpath = _PyUnicode_AsUnicode(path);if (wpath == NULL)return -2;err = _wstat(wpath, &wstatbuf);if (!err)statbuf->st_mode = wstatbuf.st_mode;return err;#elseint ret;PyObject *bytes;char *cpath;bytes = PyUnicode_EncodeFSDefault(path);if (bytes == NULL)return -2;/* check for embedded null bytes */if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {Py_DECREF(bytes);return -2;}ret = stat(cpath, statbuf);Py_DECREF(bytes);return ret;#endif}/* This function MUST be kept async-signal-safe on POSIX when raise=0. */static intget_inheritable(int fd, int raise){#ifdef MS_WINDOWSHANDLE handle;DWORD flags;_Py_BEGIN_SUPPRESS_IPHhandle = (HANDLE)_get_osfhandle(fd);_Py_END_SUPPRESS_IPHif (handle == INVALID_HANDLE_VALUE) {if (raise)PyErr_SetFromErrno(PyExc_OSError);return -1;}if (!GetHandleInformation(handle, &flags)) {if (raise)PyErr_SetFromWindowsErr(0);return -1;}return (flags & HANDLE_FLAG_INHERIT);#elseint 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.Return 1 if the file descriptor can be inherited, 0 if it cannot,raise an exception and return -1 on error. */int_Py_get_inheritable(int fd){return get_inheritable(fd, 1);}/* This function MUST be kept async-signal-safe on POSIX when raise=0. */static intset_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works){#ifdef MS_WINDOWSHANDLE handle;DWORD flags;#else#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)static int ioctl_works = -1;int request;int err;#endifint flags, new_flags;int res;#endif/* atomic_flag_works can only be used to make the file descriptornon-inheritable */assert(!(atomic_flag_works != NULL && inheritable));if (atomic_flag_works != NULL && !inheritable) {if (*atomic_flag_works == -1) {int isInheritable = get_inheritable(fd, raise);if (isInheritable == -1)return -1;*atomic_flag_works = !isInheritable;}if (*atomic_flag_works)return 0;}#ifdef MS_WINDOWS_Py_BEGIN_SUPPRESS_IPHhandle = (HANDLE)_get_osfhandle(fd);_Py_END_SUPPRESS_IPHif (handle == INVALID_HANDLE_VALUE) {if (raise)PyErr_SetFromErrno(PyExc_OSError);return -1;}if (inheritable)flags = HANDLE_FLAG_INHERIT;elseflags = 0;/* This check can be removed once support for Windows 7 ends. */#define CONSOLE_PSEUDOHANDLE(handle) (((ULONG_PTR)(handle) & 0x3) == 0x3 && \GetFileType(handle) == FILE_TYPE_CHAR)if (!CONSOLE_PSEUDOHANDLE(handle) &&!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {if (raise)PyErr_SetFromWindowsErr(0);return -1;}#undef CONSOLE_PSEUDOHANDLEreturn 0;#else#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)if (ioctl_works != 0 && raise != 0) {/* fast-path: ioctl() only requires one syscall *//* caveat: raise=0 is an indicator that we must be async-signal-safe* thus avoid using ioctl() so we skip the fast-path. */if (inheritable)request = FIONCLEX;elserequest = FIOCLEX;err = ioctl(fd, request, NULL);if (!err) {ioctl_works = 1;return 0;}#ifdef __linux__if (errno == EBADF) {// On Linux, ioctl(FIOCLEX) will fail with EBADF for O_PATH file descriptors// Fall through to the fcntl() path}else#endifif (errno != ENOTTY && errno != EACCES) {if (raise)PyErr_SetFromErrno(PyExc_OSError);return -1;}else {/* Issue #22258: Here, ENOTTY means "Inappropriate ioctl fordevice". The ioctl is declared but not supported by the kernel.Remember that ioctl() doesn't work. It is the case onIllumos-based OS for example.Issue #27057: When SELinux policy disallows ioctl it will failwith EACCES. While FIOCLEX is safe operation it may beunavailable because ioctl was denied altogether.This can be the case on Android. */ioctl_works = 0;}/* fallback to fcntl() if ioctl() does not work */}#endif/* slow-path: fcntl() requires two syscalls */flags = fcntl(fd, F_GETFD);if (flags < 0) {if (raise)PyErr_SetFromErrno(PyExc_OSError);return -1;}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;}res = fcntl(fd, F_SETFD, new_flags);if (res < 0) {if (raise)PyErr_SetFromErrno(PyExc_OSError);return -1;}return 0;#endif}/* Make the file descriptor non-inheritable.Return 0 on success, set errno and return -1 on error. */static intmake_non_inheritable(int fd){return set_inheritable(fd, 0, 0, NULL);}/* Set the inheritable flag of the specified file descriptor.On success: return 0, on error: raise an exception and return -1.If atomic_flag_works is not NULL:* if *atomic_flag_works==-1, check if the inheritable is set on the filedescriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 andset the inheritable flag* if *atomic_flag_works==1: do nothing* if *atomic_flag_works==0: set inheritable flag to FalseSet atomic_flag_works to NULL if no atomic flag was used to create thefile descriptor.atomic_flag_works can only be used to make a file descriptornon-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);}/* Same as _Py_set_inheritable() but on error, set errno anddon'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);}static int_Py_open_impl(const char *pathname, int flags, int gil_held){int fd;int async_err = 0;#ifndef MS_WINDOWSint *atomic_flag_works;#endif#ifdef MS_WINDOWSflags |= O_NOINHERIT;#elif defined(O_CLOEXEC)atomic_flag_works = &_Py_open_cloexec_works;flags |= O_CLOEXEC;#elseatomic_flag_works = NULL;#endifif (gil_held) {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);return -1;}do {Py_BEGIN_ALLOW_THREADSfd = open(pathname, flags);Py_END_ALLOW_THREADS} while (fd < 0&& errno == EINTR && !(async_err = PyErr_CheckSignals()));if (async_err) {Py_DECREF(pathname_obj);return -1;}if (fd < 0) {PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, pathname_obj, NULL);Py_DECREF(pathname_obj);return -1;}Py_DECREF(pathname_obj);}else {fd = open(pathname, flags);if (fd < 0)return -1;}#ifndef MS_WINDOWSif (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {close(fd);return -1;}#endifreturn fd;}/* Open a file with the specified flags (wrapper to open() function).Return a file descriptor on success. Raise an exception and return -1 onerror.The file descriptor is created non-inheritable.When interrupted by a signal (open() fails with EINTR), retry the syscall,except if the Python signal handler raises an exception.Release the GIL to call open(). The caller must hold the GIL. */int_Py_open(const char *pathname, int flags){/* _Py_open() must be called with the GIL held. */assert(PyGILState_Check());return _Py_open_impl(pathname, flags, 1);}/* Open a file with the specified flags (wrapper to open() function).Return a file descriptor on success. Set errno and return -1 on error.The file descriptor is created non-inheritable.If interrupted by a signal, fail with EINTR. */int_Py_open_noraise(const char *pathname, int flags){return _Py_open_impl(pathname, flags, 0);}/* Open a file. Use _wfopen() on Windows, encode the path to the localeencoding and use fopen() otherwise.The file descriptor is created non-inheritable.If interrupted by a signal, fail with EINTR. */FILE *_Py_wfopen(const wchar_t *path, const wchar_t *mode){FILE *f;if (PySys_Audit("open", "uui", path, mode, 0) < 0) {return NULL;}#ifndef MS_WINDOWSchar *cpath;char cmode[10];size_t r;r = wcstombs(cmode, mode, 10);if (r == DECODE_ERROR || r >= 10) {errno = EINVAL;return NULL;}cpath = _Py_EncodeLocaleRaw(path, NULL);if (cpath == NULL) {return NULL;}f = fopen(cpath, cmode);PyMem_RawFree(cpath);#elsef = _wfopen(path, mode);#endifif (f == NULL)return NULL;if (make_non_inheritable(fileno(f)) < 0) {fclose(f);return NULL;}return f;}/* Wrapper to fopen().The file descriptor is created non-inheritable.If interrupted by a signal, fail with EINTR. */FILE*_Py_fopen(const char *pathname, const char *mode){PyObject *pathname_obj = PyUnicode_DecodeFSDefault(pathname);if (pathname_obj == NULL) {return NULL;}if (PySys_Audit("open", "Osi", pathname_obj, mode, 0) < 0) {Py_DECREF(pathname_obj);return NULL;}Py_DECREF(pathname_obj);FILE *f = fopen(pathname, mode);if (f == NULL)return NULL;if (make_non_inheritable(fileno(f)) < 0) {fclose(f);return NULL;}return f;}/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystemencoding and call fopen() otherwise.Return the new file object on success. Raise an exception and return NULLon error.The file descriptor is created non-inheritable.When interrupted by a signal (open() fails with EINTR), retry the syscall,except if the Python signal handler raises an exception.Release the GIL to call _wfopen() or fopen(). The caller must holdthe GIL. */FILE*_Py_fopen_obj(PyObject *path, const char *mode){FILE *f;int async_err = 0;#ifdef MS_WINDOWSconst wchar_t *wpath;wchar_t wmode[10];int usize;assert(PyGILState_Check());if (PySys_Audit("open", "Osi", path, mode, 0) < 0) {return NULL;}if (!PyUnicode_Check(path)) {PyErr_Format(PyExc_TypeError,"str file path expected under Windows, got %R",Py_TYPE(path));return NULL;}wpath = _PyUnicode_AsUnicode(path);if (wpath == NULL)return NULL;usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,wmode, Py_ARRAY_LENGTH(wmode));if (usize == 0) {PyErr_SetFromWindowsErr(0);return NULL;}do {Py_BEGIN_ALLOW_THREADSf = _wfopen(wpath, wmode);Py_END_ALLOW_THREADS} while (f == NULL&& errno == EINTR && !(async_err = PyErr_CheckSignals()));#elsePyObject *bytes;char *path_bytes;assert(PyGILState_Check());if (!PyUnicode_FSConverter(path, &bytes))return NULL;path_bytes = PyBytes_AS_STRING(bytes);if (PySys_Audit("open", "Osi", path, mode, 0) < 0) {Py_DECREF(bytes);return NULL;}do {Py_BEGIN_ALLOW_THREADSf = fopen(path_bytes, mode);Py_END_ALLOW_THREADS} while (f == NULL&& errno == EINTR && !(async_err = PyErr_CheckSignals()));Py_DECREF(bytes);#endifif (async_err)return NULL;if (f == NULL) {PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);return NULL;}if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {fclose(f);return NULL;}return f;}/* Read count bytes from fd into buf.On success, return the number of read bytes, it can be lower than count.If the current file offset is at or past the end of file, no bytes are read,and read() returns zero.On error, raise an exception, set errno and return -1.When interrupted by a signal (read() fails with EINTR), retry the syscall.If the Python signal handler raises an exception, the function returns -1(the syscall is not retried).Release the GIL to call read(). The caller must hold the GIL. */Py_ssize_t_Py_read(int fd, void *buf, size_t count){Py_ssize_t n;int err;int async_err = 0;assert(PyGILState_Check());/* _Py_read() must not be called with an exception set, otherwise the* caller may think that read() was interrupted by a signal and the signal* handler raised an exception. */assert(!PyErr_Occurred());if (count > _PY_READ_MAX) {count = _PY_READ_MAX;}_Py_BEGIN_SUPPRESS_IPHdo {Py_BEGIN_ALLOW_THREADSerrno = 0;#ifdef MS_WINDOWSn = read(fd, buf, (int)count);#elsen = read(fd, buf, count);#endif/* save/restore errno because PyErr_CheckSignals()* and PyErr_SetFromErrno() can modify it */err = errno;Py_END_ALLOW_THREADS} while (n < 0 && err == EINTR &&!(async_err = PyErr_CheckSignals()));_Py_END_SUPPRESS_IPHif (async_err) {/* read() was interrupted by a signal (failed with EINTR)* and the Python signal handler raised an exception */errno = err;assert(errno == EINTR && PyErr_Occurred());return -1;}if (n < 0) {PyErr_SetFromErrno(PyExc_OSError);errno = err;return -1;}return n;}static Py_ssize_t_Py_write_impl(int fd, const void *buf, size_t count, int gil_held){Py_ssize_t n;int err;int async_err = 0;_Py_BEGIN_SUPPRESS_IPH#ifdef MS_WINDOWSif (count > 32767 && isatty(fd)) {/* Issue #11395: the Windows console returns an error (12: notenough space error) on writing into stdout if stdout mode isbinary and the length is greater than 66,000 bytes (or less,depending on heap usage). */count = 32767;}#endifif (count > _PY_WRITE_MAX) {count = _PY_WRITE_MAX;}if (gil_held) {do {Py_BEGIN_ALLOW_THREADSerrno = 0;#ifdef MS_WINDOWSn = write(fd, buf, (int)count);#elsen = write(fd, buf, count);#endif/* save/restore errno because PyErr_CheckSignals()* and PyErr_SetFromErrno() can modify it */err = errno;Py_END_ALLOW_THREADS} while (n < 0 && err == EINTR &&!(async_err = PyErr_CheckSignals()));}else {do {errno = 0;#ifdef MS_WINDOWSn = write(fd, buf, (int)count);#elsen = write(fd, buf, count);#endiferr = errno;} while (n < 0 && err == EINTR);}_Py_END_SUPPRESS_IPHif (async_err) {/* write() was interrupted by a signal (failed with EINTR)and the Python signal handler raised an exception (if gil_held isnonzero). */errno = err;assert(errno == EINTR && (!gil_held || PyErr_Occurred()));return -1;}if (n < 0) {if (gil_held)PyErr_SetFromErrno(PyExc_OSError);errno = err;return -1;}return n;}/* Write count bytes of buf into fd.On success, return the number of written bytes, it can be lower than countincluding 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){assert(PyGILState_Check());/* _Py_write() must not be called with an exception set, otherwise the* caller may think that write() was interrupted by a signal and the signal* handler raised an exception. */assert(!PyErr_Occurred());return _Py_write_impl(fd, buf, count, 1);}/* Write count bytes of buf into fd.** On success, return the number of written bytes, it can be lower than count* including 0. On error, set errno and return -1.** When interrupted by a signal (write() fails with EINTR), retry the syscall* without calling the Python signal handler. */Py_ssize_t_Py_write_noraise(int fd, const void *buf, size_t count){return _Py_write_impl(fd, buf, count, 0);}#ifdef HAVE_READLINK/* Read value of symbolic link. Encode the path to the locale encoding, decodethe result from the locale encoding.Return -1 on encoding error, on readlink() error, if the internal buffer istoo short, on decoding error, or if 'buf' is too short. */int_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t buflen){char *cpath;char cbuf[MAXPATHLEN];wchar_t *wbuf;int res;size_t r1;cpath = _Py_EncodeLocaleRaw(path, NULL);if (cpath == NULL) {errno = EINVAL;return -1;}res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));PyMem_RawFree(cpath);if (res == -1)return -1;if (res == Py_ARRAY_LENGTH(cbuf)) {errno = EINVAL;return -1;}cbuf[res] = '0円'; /* buf will be null terminated */wbuf = Py_DecodeLocale(cbuf, &r1);if (wbuf == NULL) {errno = EINVAL;return -1;}/* wbuf must have space to store the trailing NUL character */if (buflen <= r1) {PyMem_RawFree(wbuf);errno = EINVAL;return -1;}wcsncpy(buf, wbuf, buflen);PyMem_RawFree(wbuf);return (int)r1;}#endif#ifdef HAVE_REALPATH/* Return the canonicalized absolute pathname. Encode path to the localeencoding, decode the result from the locale encoding.Return NULL on encoding error, realpath() error, decoding erroror if 'resolved_path' is too short. */wchar_t*_Py_wrealpath(const wchar_t *path,wchar_t *resolved_path, size_t resolved_path_len){char *cpath;char cresolved_path[MAXPATHLEN];wchar_t *wresolved_path;char *res;size_t r;cpath = _Py_EncodeLocaleRaw(path, NULL);if (cpath == NULL) {errno = EINVAL;return NULL;}res = realpath(cpath, cresolved_path);PyMem_RawFree(cpath);if (res == NULL)return NULL;wresolved_path = Py_DecodeLocale(cresolved_path, &r);if (wresolved_path == NULL) {errno = EINVAL;return NULL;}/* wresolved_path must have space to store the trailing NUL character */if (resolved_path_len <= r) {PyMem_RawFree(wresolved_path);errno = EINVAL;return NULL;}wcsncpy(resolved_path, wresolved_path, resolved_path_len);PyMem_RawFree(wresolved_path);return resolved_path;}#endif/* Get the current directory. buflen is the buffer size in wide charactersincluding the null character. Decode the path from the locale encoding.Return NULL on getcwd() error, on decoding error, or if 'buf' istoo short. */wchar_t*_Py_wgetcwd(wchar_t *buf, size_t buflen){#ifdef MS_WINDOWSint ibuflen = (int)Py_MIN(buflen, INT_MAX);return _wgetcwd(buf, ibuflen);#elsechar fname[MAXPATHLEN];wchar_t *wname;size_t len;if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)return NULL;wname = Py_DecodeLocale(fname, &len);if (wname == NULL)return NULL;/* wname must have space to store the trailing NUL character */if (buflen <= len) {PyMem_RawFree(wname);return NULL;}wcsncpy(buf, wname, buflen);PyMem_RawFree(wname);return buf;#endif}/* Duplicate a file descriptor. The new file descriptor is created asnon-inheritable. Return a new file descriptor on success, raise an OSErrorexception 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_WINDOWSHANDLE handle;#endifassert(PyGILState_Check());#ifdef MS_WINDOWS_Py_BEGIN_SUPPRESS_IPHhandle = (HANDLE)_get_osfhandle(fd);_Py_END_SUPPRESS_IPHif (handle == INVALID_HANDLE_VALUE) {PyErr_SetFromErrno(PyExc_OSError);return -1;}Py_BEGIN_ALLOW_THREADS_Py_BEGIN_SUPPRESS_IPHfd = dup(fd);_Py_END_SUPPRESS_IPHPy_END_ALLOW_THREADSif (fd < 0) {PyErr_SetFromErrno(PyExc_OSError);return -1;}if (_Py_set_inheritable(fd, 0, NULL) < 0) {_Py_BEGIN_SUPPRESS_IPHclose(fd);_Py_END_SUPPRESS_IPHreturn -1;}#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)Py_BEGIN_ALLOW_THREADS_Py_BEGIN_SUPPRESS_IPHfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);_Py_END_SUPPRESS_IPHPy_END_ALLOW_THREADSif (fd < 0) {PyErr_SetFromErrno(PyExc_OSError);return -1;}#elsePy_BEGIN_ALLOW_THREADS_Py_BEGIN_SUPPRESS_IPHfd = dup(fd);_Py_END_SUPPRESS_IPHPy_END_ALLOW_THREADSif (fd < 0) {PyErr_SetFromErrno(PyExc_OSError);return -1;}if (_Py_set_inheritable(fd, 0, NULL) < 0) {_Py_BEGIN_SUPPRESS_IPHclose(fd);_Py_END_SUPPRESS_IPHreturn -1;}#endifreturn fd;}#ifndef MS_WINDOWS/* Get the blocking mode of the file descriptor.Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,raise an exception and return -1 on error. */int_Py_get_blocking(int fd){int flags;_Py_BEGIN_SUPPRESS_IPHflags = fcntl(fd, F_GETFL, 0);_Py_END_SUPPRESS_IPHif (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 flagotherwise.Return 0 on success, raise an exception and return -1 on error. */int_Py_set_blocking(int fd, int blocking){#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)int arg = !blocking;if (ioctl(fd, FIONBIO, &arg) < 0)goto error;#elseint flags, res;_Py_BEGIN_SUPPRESS_IPHflags = fcntl(fd, F_GETFL, 0);if (flags >= 0) {if (blocking)flags = flags & (~O_NONBLOCK);elseflags = flags | O_NONBLOCK;res = fcntl(fd, F_SETFL, flags);} else {res = -1;}_Py_END_SUPPRESS_IPHif (res < 0)goto error;#endifreturn 0;error:PyErr_SetFromErrno(PyExc_OSError);return -1;}#endifint_Py_GetLocaleconvNumeric(struct lconv *lc,PyObject **decimal_point, PyObject **thousands_sep){assert(decimal_point != NULL);assert(thousands_sep != NULL);#ifndef MS_WINDOWSint change_locale = 0;if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) {change_locale = 1;}if ((strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127)) {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) {PyErr_SetString(PyExc_RuntimeWarning,"failed to get LC_CTYPE locale");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) {/* Only set the locale temporarily the LC_CTYPE localeif LC_NUMERIC locale is different than LC_CTYPE locale anddecimal_point and/or thousands_sep are non-ASCII or longer than1 byte */setlocale(LC_CTYPE, loc);}}#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 */int res = -1;*decimal_point = GET_LOCALE_STRING(decimal_point);if (*decimal_point == NULL) {goto done;}*thousands_sep = GET_LOCALE_STRING(thousands_sep);if (*thousands_sep == NULL) {goto done;}res = 0;done:#ifndef MS_WINDOWSif (loc != NULL) {setlocale(LC_CTYPE, oldloc);}PyMem_Free(oldloc);#endifreturn res;#undef GET_LOCALE_STRING}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。