GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/fileutils.c at main · python/cpython Expand file tree/ Copy pathfileutils.cMore file actions 3398 lines (3014 loc) · 96.3 KB / Copy pathfileutils.cFile metadata and controls3398 lines (3014 loc) · 96.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include "Python.h" #include "pycore_fileutils.h" // fileutils definitions #include "pycore_runtime.h" // _PyRuntime #include "pycore_pystate.h" // _Py_AssertHoldsTstate() #include "pycore_unicodeobject.h" // _Py_MAX_UNICODE #include "osdefs.h" // SEP #include <stdlib.h> // mbstowcs() #ifdef HAVE_UNISTD_H # include <unistd.h> // getcwd() #endif #ifdef MS_WINDOWS # include <malloc.h> # include <windows.h> # include <winioctl.h> // FILE_DEVICE_* constants # include "pycore_fileutils_windows.h" // FILE_STAT_BASIC_INFORMATION # if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) # define PATHCCH_ALLOW_LONG_PATHS 0x01 # else # include <pathcch.h> // PathCchCombineEx # endif extern int winerror_to_errno(int); #endif #ifdef HAVE_LANGINFO_H # include <langinfo.h> // nl_langinfo(CODESET) #endif #ifdef HAVE_SYS_IOCTL_H #include <sys/ioctl.h> #endif #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION # include <iconv.h> // iconv_open() #endif #ifdef HAVE_FCNTL_H # include <fcntl.h> // fcntl(F_GETFD) #endif #ifdef O_CLOEXEC /* Does open() support the O_CLOEXEC flag? Possible values: -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 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO and os.open(). */ int _Py_open_cloexec_works = -1; #endif // wcstombs() error static const size_t ENCODE_ERROR = (size_t)-1; // mbstowcs() and mbrtowc() errors static const size_t DECODE_ERROR = (size_t)-1; #ifdef HAVE_MBRTOWC static const size_t INCOMPLETE_CHARACTER = (size_t)-2; #endif // Get the error handler as 'int surrogateescape'. // Set '*surrogateescape' and return 0 on success. // Return -1 if the error handler is not supported: other than "strict" and // "surrogateescape". 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; } } PyObject * _Py_device_encoding(int fd) { #if defined(MS_WINDOWS) && defined(HAVE_WINDOWS_CONSOLE_IO) HANDLE handle; DWORD temp; UINT cp = 0; _Py_BEGIN_SUPPRESS_IPH handle = (HANDLE)_get_osfhandle(fd); _Py_END_SUPPRESS_IPH if (handle == INVALID_HANDLE_VALUE) { Py_RETURN_NONE; } Py_BEGIN_ALLOW_THREADS if (GetFileType(handle) == FILE_TYPE_CHAR) { /* GetConsoleMode() only succeeds for a console handle. */ if (!GetConsoleMode(handle, &temp)) { /* Assume that access denied implies an output handle. */ if (GetLastError() == ERROR_ACCESS_DENIED) { cp = GetConsoleOutputCP(); } } else if (GetNumberOfConsoleInputEvents(handle, &temp)) { cp = GetConsoleCP(); } else { cp = GetConsoleOutputCP(); } } Py_END_ALLOW_THREADS /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application has no console */ if (cp == CP_UTF8) { _Py_DECLARE_STR(utf_8, "utf-8"); return &_Py_STR(utf_8); } if (cp == 0) { Py_RETURN_NONE; } return PyUnicode_FromFormat("cp%u", (unsigned int)cp); #else int valid; Py_BEGIN_ALLOW_THREADS _Py_BEGIN_SUPPRESS_IPH valid = isatty(fd); _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS if (!valid) { Py_RETURN_NONE; } #ifdef MS_WINDOWS Py_RETURN_NONE; #else if (_PyRuntime.preconfig.utf8_mode) { _Py_DECLARE_STR(utf_8, "utf-8"); return &_Py_STR(utf_8); } return _Py_GetLocaleEncodingObject(); #endif #endif /* MS_WINDOWS && HAVE_WINDOWS_CONSOLE_IO */ } static int is_valid_wide_char(wchar_t ch) { #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 _Py_MAX_UNICODE possibly valid. */ return 1; #endif if (Py_UNICODE_IS_SURROGATE(ch)) { // Reject lone surrogate characters return 0; } #if SIZEOF_WCHAR_T > 2 if (ch > _Py_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; } #endif 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 #if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS) #define USE_FORCE_ASCII extern int _Py_normalize_encoding(const char *, char *, size_t, int); /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale and POSIX locale. nl_langinfo(CODESET) announces an alias of the 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. 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. Values of force_ascii: 1: the workaround is used: Py_EncodeLocale() uses encode_ascii() and Py_DecodeLocale() uses decode_ascii() 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and Py_DecodeLocale() uses mbstowcs() -1: unknown, need to call check_force_ascii() to get the value */ #define force_ascii (_PyRuntime.fileutils.force_ascii) static int check_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_1991\0" */ if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding), 1)) { goto error; } #ifdef __hpux if (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 with C locale or the POSIX locale, 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[] = { "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 locale encoding: 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; } // Encode a wide string to the ASCII encoding. If errors is // _Py_ERROR_SURROGATEESCAPE, handle non-ASCII character using the // surrogateescape error handler. In that case, the output is non-ASCII. // // Set *str to a newly allocated string on success. // Return a negative number on error: _Py_CODEC_MEMORY_ERROR, // _Py_CODEC_ENCODE_ERROR or _Py_CODEC_UNSUPPORTED_ERROR_HANDLER. // // str and output_length must not be NULL static int encode_ascii(const wchar_t *text, char **str, size_t *output_length, size_t *error_pos, int raw_malloc, _Py_error_handler errors) { assert(str != NULL); assert(output_length != NULL); int surrogateescape; if (get_surrogateescape(errors, &surrogateescape) < 0) { // Only support "strict" and "surrogateescape" return _Py_CODEC_UNSUPPORTED_ERROR_HANDLER; } size_t len = wcslen(text); *output_length = len; /* +1 for NULL byte */ char *result; result = raw_malloc ? PyMem_RawMalloc(len + 1) : PyMem_Malloc(len + 1); if (result == NULL) { return _Py_CODEC_MEMORY_ERROR; } char *out = result; for (size_t i=0; i < len; i++) { wchar_t 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; } return _Py_CODEC_ENCODE_ERROR; } } *out++ = '\0'; assert((size_t)(out - result) == (*output_length + 1)); *str = result; return 0; } #else int _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) // Decode a bytes string from the ASCII encoding. If 'errors' is // "surrogateescape", decode non-ASCII bytes with "surrogateescape" error // handler and so the output is non-ASCII. // // On success, set *wstr and *wlen (if set) and return 0. // On error, return a negative number: _Py_CODEC_MEMORY_ERROR, // _Py_CODEC_DECODE_ERROR or _Py_CODEC_UNSUPPORTED_ERROR_HANDLER. // On decode, set *wlen (if set). static int decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen, _Py_error_handler errors) { assert(arg != NULL); assert(wstr != NULL); wchar_t *res; unsigned char *in; wchar_t *out; size_t argsize = strlen(arg) + 1; int surrogateescape; if (get_surrogateescape(errors, &surrogateescape) < 0) { // Only support "strict" and "surrogateescape" return _Py_CODEC_UNSUPPORTED_ERROR_HANDLER; } if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { return _Py_CODEC_MEMORY_ERROR; } res = PyMem_RawMalloc(argsize * sizeof(wchar_t)); if (!res) { return _Py_CODEC_MEMORY_ERROR; } out = res; for (in = (unsigned char*)arg; *in; in++) { unsigned char ch = *in; if (ch <= 127) { *out++ = ch; } else { if (!surrogateescape) { PyMem_RawFree(res); if (wlen) { *wlen = in - (unsigned char*)arg; } return _Py_CODEC_DECODE_ERROR; } *out++ = 0xdc00 + ch; } } *out = 0; if (wlen != NULL) { *wlen = out - res; } *wstr = res; return 0; } #endif /* !HAVE_MBRTOWC */ // Decode a bytes string from the current locale encoding. // On success, set *wstr and *wlen (if set) and return 0. // On error, return a negative number: _Py_CODEC_MEMORY_ERROR, // _Py_CODEC_DECODE_ERROR or _Py_CODEC_UNSUPPORTED_ERROR_HANDLER. // On decode, set *wlen (if set). static int decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen, _Py_error_handler errors) { assert(arg != NULL); assert(wstr != NULL); int surrogateescape; if (get_surrogateescape(errors, &surrogateescape) < 0) { // Only support "strict" and "surrogateescape" return _Py_CODEC_UNSUPPORTED_ERROR_HANDLER; } #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. */ size_t argsize = strlen(arg); #else size_t argsize = _Py_mbstowcs(NULL, arg, 0); #endif wchar_t *res; if (argsize != DECODE_ERROR) { if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { return _Py_CODEC_MEMORY_ERROR; } res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t)); if (!res) { return _Py_CODEC_MEMORY_ERROR; } // +1 to write also the trailing NUL character size_t count = _Py_mbstowcs(res, arg, argsize + 1); if (count != DECODE_ERROR) { // String decoded successfully. // gh-158893: This assertion can fail if the input string was // mutated during this function call. For example, the assertion // fails on decoding strerror() result if another thread mutated // the string in-place by calling strerror() in parallel. assert(count == argsize); *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, the actual output could use less memory. */ argsize = strlen(arg) + 1; if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { return _Py_CODEC_MEMORY_ERROR; } res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t)); if (!res) { return _Py_CODEC_MEMORY_ERROR; } unsigned char *in = (unsigned char*)arg; wchar_t *out = res; mbstate_t mbs; 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 == DECODE_ERROR || converted == INCOMPLETE_CHARACTER) { if (!surrogateescape) { goto decode_error; } /* Decoding error. Escape as UTF-8b, and start over in the initial shift state. */ *out++ = 0xdc00 + *in++; argsize--; memset(&mbs, 0, sizeof mbs); continue; } // _Py_mbrtowc() reject lone surrogate characters assert(!Py_UNICODE_IS_SURROGATE(*out)); /* successfully converted some bytes */ in += converted; argsize -= converted; out++; } if (wlen != NULL) { *wlen = out - res; assert(res[*wlen] == 0); } *wstr = res; return 0; decode_error: PyMem_RawFree(res); if (wlen) { *wlen = in - (unsigned char*)arg; } return _Py_CODEC_DECODE_ERROR; #else /* HAVE_MBRTOWC */ /* 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. */ return decode_ascii(arg, wstr, wlen, errors); #endif /* HAVE_MBRTOWC */ } static int decode_locale_impl(const char* arg, wchar_t **wstr, size_t *wlen, int current_locale, _Py_error_handler errors) { if (current_locale) { #ifdef _Py_FORCE_UTF8_LOCALE return _Py_DecodeUTF8(arg, strlen(arg), wstr, wlen, errors); #else return decode_current_locale(arg, wstr, wlen, errors); #endif } #ifdef _Py_FORCE_UTF8_FS_ENCODING return _Py_DecodeUTF8(arg, strlen(arg), wstr, wlen, errors); #else int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1); #ifdef MS_WINDOWS use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0); #endif if (use_utf8) { return _Py_DecodeUTF8(arg, strlen(arg), wstr, wlen, errors); } #ifdef USE_FORCE_ASCII if (force_ascii == -1) { force_ascii = check_force_ascii(); } if (force_ascii) { /* force ASCII encoding to workaround mbstowcs() issue */ return decode_ascii(arg, wstr, wlen, errors); } #endif return decode_current_locale(arg, wstr, wlen, errors); #endif /* !_Py_FORCE_UTF8_FS_ENCODING */ } // Decode a byte string from the locale encoding. // // Supported error handlers are _Py_ERROR_STRICT and _Py_ERROR_SURROGATEESCAPE. // The UTF-8 decoder also supports _Py_ERROR_SURROGATEPASS. // // If errors is _Py_ERROR_SURROGATEESCAPE, 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. // // 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, write // the number of wide characters excluding the null character into *wlen. // // On error, return a negative number. // // On memory allocation failure, return _Py_CODEC_MEMORY_ERROR. // // On decoding error, return _Py_CODEC_DECODE_ERROR. 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. // // Return _Py_CODEC_UNSUPPORTED_ERROR_HANDLER if the 'errors' error // handler is not supported: other than "strict" and "surrogateescape". // // Use the _Py_EncodeLocale() function to encode the character string back to // a byte string. // // arg and wstr must not be NULL. int _Py_DecodeLocale(const char* arg, wchar_t **wstr, size_t *wlen, int current_locale, _Py_error_handler errors) { assert(arg != NULL); assert(wstr != NULL); #ifdef Py_DEBUG size_t arglen = strlen(arg); size_t wlen_canary = (size_t)-2; if (wlen) { *wlen = wlen_canary; } #endif int res = decode_locale_impl(arg, wstr, wlen, current_locale, errors); if (res < 0) { // Error *wstr = NULL; if (res == _Py_CODEC_DECODE_ERROR) { #ifdef Py_DEBUG assert(wlen == NULL || *wlen != wlen_canary); #endif } else { if (wlen) { *wlen = 0; } } } else { // Success assert(*wstr != NULL); #ifdef Py_DEBUG // gh-158893: Detect if the input string was mutated during the // function call. For example, the assertion fails on decoding // strerror() result if another thread mutated the string in-place by // calling strerror() in parallel. assert(strlen(arg) == arglen); if (wlen != NULL) { assert(*wlen == wcslen(*wstr)); } #endif } return res; } // 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. // // On memory allocation failure, set *size to (size_t)-1 and return NULL. // // On decode error, set *size to (size_t)-2 and return NULL. // Decoding errors should never happen, unless there is a bug in the C library. // // Use the Py_EncodeLocale() function to encode the character string back to a // byte string. wchar_t* Py_DecodeLocale(const char* arg, size_t *size) { wchar_t *wstr; int res = _Py_DecodeLocale(arg, &wstr, size, 0, _Py_ERROR_SURROGATEESCAPE); if (res != 0) { assert(res == _Py_CODEC_MEMORY_ERROR || res == _Py_CODEC_DECODE_ERROR); if (size != NULL) { *size = (size_t)res; } return NULL; } return wstr; } // If bytes is NULL, compute the length in 'bytes' of the encoded string. // Otherwise, encode the wide string into 'bytes', decrements 'size', and // return 0 on success. // On encoding error, set 'error_pos' (if set) and return ENCODE_ERROR. static size_t encode_current_locale_impl(const wchar_t *text, const size_t len, int surrogateescape, char *bytes, size_t size, size_t *error_pos) { wchar_t buf[2]; // The second character is always the NUL character buf[1] = 0; for (size_t i=0; i < len; i++) { wchar_t c = text[i]; if (c >= 0xdc80 && c <= 0xdcff) { if (!surrogateescape) { if (error_pos != NULL) { *error_pos = i; } return ENCODE_ERROR; } /* UTF-8b surrogate */ if (bytes != NULL) { *bytes++ = c - 0xdc00; size--; } else { size++; } } else { // Encode a single character using wcstombs() buf[0] = c; size_t converted; if (bytes != NULL) { converted = wcstombs(bytes, buf, size); } else { converted = wcstombs(NULL, buf, 0); } if (converted == ENCODE_ERROR) { if (error_pos != NULL) { *error_pos = i; } return ENCODE_ERROR; } if (bytes != NULL) { bytes += converted; size -= converted; } else { size += converted; } } } if (bytes) { *bytes = '\0'; } // Sanity check, it cannot happen in practice assert(size != ENCODE_ERROR); return size; } // Encode a wide string to the current locale encoding. // Set *str to a newly allocated string on success. // Return a negative number on error: _Py_CODEC_MEMORY_ERROR, // _Py_CODEC_ENCODE_ERROR or _Py_CODEC_UNSUPPORTED_ERROR_HANDLER. // // str and output_length must not be NULL static int encode_current_locale(const wchar_t *text, char **str, size_t *output_length, size_t *error_pos, int raw_malloc, _Py_error_handler errors) { assert(str != NULL); assert(output_length != NULL); int surrogateescape; if (get_surrogateescape(errors, &surrogateescape) < 0) { // Only support "strict" and "surrogateescape" return _Py_CODEC_UNSUPPORTED_ERROR_HANDLER; } // First, compute the output length const size_t len = wcslen(text); // Sanity check, it cannot happen in practice assert(len != ENCODE_ERROR); size_t size = encode_current_locale_impl(text, len, surrogateescape, NULL, 0, error_pos); if (size == ENCODE_ERROR) { return _Py_CODEC_ENCODE_ERROR; } *output_length = size; char *result; result = raw_malloc ? PyMem_RawMalloc(size + 1) : PyMem_Malloc(size + 1); if (result == NULL) { return _Py_CODEC_MEMORY_ERROR; } // Second, encode characters size = encode_current_locale_impl(text, len, surrogateescape, result, size, error_pos); if (size == ENCODE_ERROR) { if (raw_malloc) { PyMem_RawFree(result); } else { PyMem_Free(result); } return _Py_CODEC_ENCODE_ERROR; } assert(size == 0); *str = result; return 0; } static int encode_locale_inner(const wchar_t *text, char **str, size_t *output_length, size_t *error_pos, int raw_malloc, int current_locale, _Py_error_handler errors) { if (current_locale) { #ifdef _Py_FORCE_UTF8_LOCALE return _Py_EncodeUTF8(text, str, output_length, error_pos, raw_malloc, errors); #else return encode_current_locale(text, str, output_length, error_pos, raw_malloc, errors); #endif } #ifdef _Py_FORCE_UTF8_FS_ENCODING return _Py_EncodeUTF8(text, str, output_length, error_pos, raw_malloc, errors); #else int use_utf8 = (_PyRuntime.preconfig.utf8_mode >= 1); #ifdef MS_WINDOWS use_utf8 |= (_PyRuntime.preconfig.legacy_windows_fs_encoding == 0); #endif if (use_utf8) { return _Py_EncodeUTF8(text, str, output_length, error_pos, raw_malloc, errors); } #ifdef USE_FORCE_ASCII if (force_ascii == -1) { force_ascii = check_force_ascii(); } if (force_ascii) { return encode_ascii(text, str, output_length, error_pos, raw_malloc, errors); } #endif return encode_current_locale(text, str, output_length, error_pos, raw_malloc, errors); #endif /* _Py_FORCE_UTF8_FS_ENCODING */ } // Encode a wide 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: supported error handlers are _Py_ERROR_STRICT and // _Py_ERROR_SURROGATEESCAPE. The UTF-8 encoder also supports // _Py_ERROR_SURROGATEPASS. // // Set *str to a newly allocated decoded string and return 0 on success. // Return a negative result on error: // // * _Py_CODEC_MEMORY_ERROR: memory allocation failure // * _Py_CODEC_ENCODE_ERROR: encoding error, set *error_pos. // * _Py_CODEC_UNSUPPORTED_ERROR_HANDLER: the 'errors' error handler // is not supported. // // text, str and output_length must not be NULL. static int encode_locale_impl(const wchar_t *text, char **str, size_t *output_length, size_t *error_pos, int raw_malloc, int current_locale, _Py_error_handler errors) { assert(text != NULL); assert(str != NULL); assert(output_length != NULL); int res = encode_locale_inner(text, str, output_length, error_pos, raw_malloc, current_locale, errors); if (res < 0) { // Error *str = NULL; *output_length = 0; if (res != _Py_CODEC_ENCODE_ERROR) { if (error_pos) { *error_pos = 0; } } } else { assert(*str != NULL); assert(*output_length == strlen(*str)); // *error_pos is left unchanged } return res; } static char* encode_locale(const wchar_t *text, size_t *error_pos, int raw_malloc, int current_locale) { char *str; size_t unused_output_length; int res = encode_locale_impl(text, &str, &unused_output_length, |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |