Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 9fc57a3 | 2018-11-07 00:44:03 +0100 | [diff] [blame] | 2 | #include "pycore_fileutils.h" |
Stefan Krah | 6df5cae | 2012-11-12 20:14:36 +0100 | [diff] [blame] | 3 | #include "osdefs.h" |
Stefan Krah | 6c01e38 | 2014-01-20 15:31:08 +0100 | [diff] [blame] | 4 | #include <locale.h> |
| 5 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 6 | #ifdef MS_WINDOWS |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 7 | # include <malloc.h> |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 8 | # include <windows.h> |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9 | extern int winerror_to_errno(int); |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 10 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 11 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 12 | #ifdef HAVE_LANGINFO_H |
| 13 | #include <langinfo.h> |
| 14 | #endif |
| 15 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16 | #ifdef HAVE_SYS_IOCTL_H |
| 17 | #include <sys/ioctl.h> |
| 18 | #endif |
| 19 | |
| 20 | #ifdef HAVE_FCNTL_H |
| 21 | #include <fcntl.h> |
| 22 | #endif /* HAVE_FCNTL_H */ |
| 23 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 24 | #ifdef O_CLOEXEC |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 25 | /* Does open() support the O_CLOEXEC flag? Possible values: |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 26 | |
| 27 | -1: unknown |
| 28 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
| 29 | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
| 30 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 31 | The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO |
| 32 | and os.open(). */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 33 | int _Py_open_cloexec_works = -1; |
| 34 | #endif |
| 35 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 36 | |
| 37 | static int |
| 38 | get_surrogateescape(_Py_error_handler errors, int *surrogateescape) |
| 39 | { |
| 40 | switch (errors) |
| 41 | { |
| 42 | case _Py_ERROR_STRICT: |
| 43 | *surrogateescape = 0; |
| 44 | return 0; |
| 45 | case _Py_ERROR_SURROGATEESCAPE: |
| 46 | *surrogateescape = 1; |
| 47 | return 0; |
| 48 | default: |
| 49 | return -1; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 54 | PyObject * |
| 55 | _Py_device_encoding(int fd) |
| 56 | { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 57 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 58 | UINT cp; |
| 59 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 60 | int valid; |
| 61 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 62 | valid = isatty(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 63 | _Py_END_SUPPRESS_IPH |
| 64 | if (!valid) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 65 | Py_RETURN_NONE; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 66 | |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 67 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 68 | if (fd == 0) |
| 69 | cp = GetConsoleCP(); |
| 70 | else if (fd == 1 || fd == 2) |
| 71 | cp = GetConsoleOutputCP(); |
| 72 | else |
| 73 | cp = 0; |
| 74 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 75 | has no console */ |
| 76 | if (cp != 0) |
| 77 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
| 78 | #elif defined(CODESET) |
| 79 | { |
| 80 | char *codeset = nl_langinfo(CODESET); |
| 81 | if (codeset != NULL && codeset[0] != 0) |
| 82 | return PyUnicode_FromString(codeset); |
| 83 | } |
| 84 | #endif |
| 85 | Py_RETURN_NONE; |
| 86 | } |
| 87 | |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 88 | #if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS) |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 89 | |
| 90 | #define USE_FORCE_ASCII |
| 91 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 92 | extern int _Py_normalize_encoding(const char *, char *, size_t); |
| 93 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 94 | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale |
| 95 | and POSIX locale. nl_langinfo(CODESET) announces an alias of the |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 96 | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
| 97 | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
| 98 | locale.getpreferredencoding() codec. For example, if command line arguments |
| 99 | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
| 100 | UnicodeEncodeError instead of retrieving the original byte string. |
| 101 | |
| 102 | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
| 103 | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
| 104 | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
| 105 | workaround is also enabled on error, for example if getting the locale |
| 106 | failed. |
| 107 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 108 | On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET) |
| 109 | announces "roman8" but mbstowcs() uses Latin1 in practice. Force also the |
| 110 | ASCII encoding in this case. |
| 111 | |
Philip Jenvey | 215c49a | 2013-01-15 13:24:12 -0800 | [diff] [blame] | 112 | Values of force_ascii: |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 113 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 114 | 1: the workaround is used: Py_EncodeLocale() uses |
| 115 | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 116 | decode_ascii() |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 117 | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
| 118 | Py_DecodeLocale() uses mbstowcs() |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 119 | -1: unknown, need to call check_force_ascii() to get the value |
| 120 | */ |
| 121 | static int force_ascii = -1; |
| 122 | |
| 123 | static int |
| 124 | check_force_ascii(void) |
| 125 | { |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 126 | char *loc = setlocale(LC_CTYPE, NULL); |
| 127 | if (loc == NULL) { |
| 128 | goto error; |
| 129 | } |
| 130 | if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) { |
| 131 | /* the LC_CTYPE locale is different than C and POSIX */ |
| 132 | return 0; |
| 133 | } |
| 134 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 135 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 136 | const char *codeset = nl_langinfo(CODESET); |
| 137 | if (!codeset || codeset[0] == '\0') { |
| 138 | /* CODESET is not set or empty */ |
| 139 | goto error; |
| 140 | } |
| 141 | |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 142 | char encoding[20]; /* longest name: "iso_646.irv_1991\0" */ |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 143 | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) { |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | #ifdef __hpux |
| 148 | if (strcmp(encoding, "roman8") == 0) { |
| 149 | unsigned char ch; |
| 150 | wchar_t wch; |
| 151 | size_t res; |
| 152 | |
| 153 | ch = (unsigned char)0xA7; |
| 154 | res = mbstowcs(&wch, (char*)&ch, 1); |
| 155 | if (res != (size_t)-1 && wch == L'\xA7') { |
| 156 | /* On HP-UX withe C locale or the POSIX locale, |
| 157 | nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() uses |
| 158 | Latin1 encoding in practice. Force ASCII in this case. |
| 159 | |
| 160 | Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */ |
| 161 | return 1; |
| 162 | } |
| 163 | } |
| 164 | #else |
| 165 | const char* ascii_aliases[] = { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 166 | "ascii", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 167 | /* Aliases from Lib/encodings/aliases.py */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 168 | "646", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 169 | "ansi_x3.4_1968", |
| 170 | "ansi_x3.4_1986", |
| 171 | "ansi_x3_4_1968", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 172 | "cp367", |
| 173 | "csascii", |
| 174 | "ibm367", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 175 | "iso646_us", |
| 176 | "iso_646.irv_1991", |
| 177 | "iso_ir_6", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 178 | "us", |
Victor Stinner | 54de2b1 | 2016-09-09 23:11:52 -0700 | [diff] [blame] | 179 | "us_ascii", |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 180 | NULL |
| 181 | }; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 182 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 183 | int is_ascii = 0; |
| 184 | for (const char **alias=ascii_aliases; *alias != NULL; alias++) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 185 | if (strcmp(encoding, *alias) == 0) { |
| 186 | is_ascii = 1; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | if (!is_ascii) { |
| 191 | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
| 192 | return 0; |
| 193 | } |
| 194 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 195 | for (unsigned int i=0x80; i<=0xff; i++) { |
| 196 | char ch[1]; |
| 197 | wchar_t wch[1]; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 198 | size_t res; |
| 199 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 200 | unsigned uch = (unsigned char)i; |
| 201 | ch[0] = (char)uch; |
| 202 | res = mbstowcs(wch, ch, 1); |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 203 | if (res != (size_t)-1) { |
| 204 | /* decoding a non-ASCII character from the locale encoding succeed: |
| 205 | the locale encoding is not ASCII, force ASCII */ |
| 206 | return 1; |
| 207 | } |
| 208 | } |
| 209 | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
| 210 | encoding: the locale encoding is really ASCII */ |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 211 | #endif /* !defined(__hpux) */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 212 | return 0; |
| 213 | #else |
| 214 | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
| 215 | return 1; |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 216 | #endif /* defined(HAVE_LANGINFO_H) && defined(CODESET) */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 217 | |
| 218 | error: |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 219 | /* if an error occurred, force the ASCII encoding */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 220 | return 1; |
| 221 | } |
| 222 | |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 223 | |
| 224 | int |
| 225 | _Py_GetForceASCII(void) |
| 226 | { |
| 227 | if (force_ascii == -1) { |
| 228 | force_ascii = check_force_ascii(); |
| 229 | } |
| 230 | return force_ascii; |
| 231 | } |
| 232 | |
| 233 | |
Victor Stinner | 353933e | 2018-11-23 13:08:26 +0100 | [diff] [blame] | 234 | void |
| 235 | _Py_ResetForceASCII(void) |
| 236 | { |
| 237 | force_ascii = -1; |
| 238 | } |
| 239 | |
| 240 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 241 | static int |
| 242 | encode_ascii(const wchar_t *text, char **str, |
| 243 | size_t *error_pos, const char **reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 244 | int raw_malloc, _Py_error_handler errors) |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 245 | { |
| 246 | char *result = NULL, *out; |
| 247 | size_t len, i; |
| 248 | wchar_t ch; |
| 249 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 250 | int surrogateescape; |
| 251 | if (get_surrogateescape(errors, &surrogateescape) < 0) { |
| 252 | return -3; |
| 253 | } |
| 254 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 255 | len = wcslen(text); |
| 256 | |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 257 | /* +1 for NULL byte */ |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 258 | if (raw_malloc) { |
| 259 | result = PyMem_RawMalloc(len + 1); |
| 260 | } |
| 261 | else { |
| 262 | result = PyMem_Malloc(len + 1); |
| 263 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 264 | if (result == NULL) { |
| 265 | return -1; |
| 266 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 267 | |
| 268 | out = result; |
| 269 | for (i=0; i<len; i++) { |
| 270 | ch = text[i]; |
| 271 | |
| 272 | if (ch <= 0x7f) { |
| 273 | /* ASCII character */ |
| 274 | *out++ = (char)ch; |
| 275 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 276 | else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 277 | /* UTF-8b surrogate */ |
| 278 | *out++ = (char)(ch - 0xdc00); |
| 279 | } |
| 280 | else { |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 281 | if (raw_malloc) { |
| 282 | PyMem_RawFree(result); |
| 283 | } |
| 284 | else { |
| 285 | PyMem_Free(result); |
| 286 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 287 | if (error_pos != NULL) { |
| 288 | *error_pos = i; |
| 289 | } |
| 290 | if (reason) { |
| 291 | *reason = "encoding error"; |
| 292 | } |
| 293 | return -2; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | *out = '\0'; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 297 | *str = result; |
| 298 | return 0; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 299 | } |
Victor Stinner | d500e53 | 2018-08-28 17:27:36 +0200 | [diff] [blame] | 300 | #else |
| 301 | int |
| 302 | _Py_GetForceASCII(void) |
| 303 | { |
| 304 | return 0; |
| 305 | } |
Victor Stinner | 353933e | 2018-11-23 13:08:26 +0100 | [diff] [blame] | 306 | |
| 307 | void |
| 308 | _Py_ResetForceASCII(void) |
| 309 | { |
| 310 | /* nothing to do */ |
| 311 | } |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 312 | #endif /* !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS) */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 313 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 314 | |
| 315 | #if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII) |
| 316 | static int |
| 317 | decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 318 | const char **reason, _Py_error_handler errors) |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 319 | { |
| 320 | wchar_t *res; |
| 321 | unsigned char *in; |
| 322 | wchar_t *out; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 323 | size_t argsize = strlen(arg) + 1; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 324 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 325 | int surrogateescape; |
| 326 | if (get_surrogateescape(errors, &surrogateescape) < 0) { |
| 327 | return -3; |
| 328 | } |
| 329 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 330 | if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
| 331 | return -1; |
| 332 | } |
| 333 | res = PyMem_RawMalloc(argsize * sizeof(wchar_t)); |
| 334 | if (!res) { |
| 335 | return -1; |
| 336 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 337 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 338 | out = res; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 339 | for (in = (unsigned char*)arg; *in; in++) { |
| 340 | unsigned char ch = *in; |
| 341 | if (ch < 128) { |
| 342 | *out++ = ch; |
| 343 | } |
| 344 | else { |
| 345 | if (!surrogateescape) { |
| 346 | PyMem_RawFree(res); |
| 347 | if (wlen) { |
| 348 | *wlen = in - (unsigned char*)arg; |
| 349 | } |
| 350 | if (reason) { |
| 351 | *reason = "decoding error"; |
| 352 | } |
| 353 | return -2; |
| 354 | } |
| 355 | *out++ = 0xdc00 + ch; |
| 356 | } |
| 357 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 358 | *out = 0; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 359 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 360 | if (wlen != NULL) { |
| 361 | *wlen = out - res; |
| 362 | } |
| 363 | *wstr = res; |
| 364 | return 0; |
| 365 | } |
| 366 | #endif /* !HAVE_MBRTOWC */ |
| 367 | |
| 368 | static int |
| 369 | decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 370 | const char **reason, _Py_error_handler errors) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 371 | { |
| 372 | wchar_t *res; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 373 | size_t argsize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 374 | size_t count; |
Victor Stinner | 313f10c | 2013-05-07 23:48:56 +0200 | [diff] [blame] | 375 | #ifdef HAVE_MBRTOWC |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 376 | unsigned char *in; |
| 377 | wchar_t *out; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 378 | mbstate_t mbs; |
| 379 | #endif |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 380 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 381 | int surrogateescape; |
| 382 | if (get_surrogateescape(errors, &surrogateescape) < 0) { |
| 383 | return -3; |
| 384 | } |
| 385 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 386 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 387 | /* Some platforms have a broken implementation of |
| 388 | * mbstowcs which does not count the characters that |
| 389 | * would result from conversion. Use an upper bound. |
| 390 | */ |
| 391 | argsize = strlen(arg); |
| 392 | #else |
| 393 | argsize = mbstowcs(NULL, arg, 0); |
| 394 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 395 | if (argsize != (size_t)-1) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 396 | if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 397 | return -1; |
| 398 | } |
| 399 | res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t)); |
| 400 | if (!res) { |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | count = mbstowcs(res, arg, argsize + 1); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 405 | if (count != (size_t)-1) { |
| 406 | wchar_t *tmp; |
| 407 | /* Only use the result if it contains no |
| 408 | surrogate characters. */ |
| 409 | for (tmp = res; *tmp != 0 && |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 410 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 411 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 412 | if (*tmp == 0) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 413 | if (wlen != NULL) { |
| 414 | *wlen = count; |
| 415 | } |
| 416 | *wstr = res; |
| 417 | return 0; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 418 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 419 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 420 | PyMem_RawFree(res); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 421 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 422 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 423 | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 424 | #ifdef HAVE_MBRTOWC |
| 425 | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 426 | |
| 427 | /* Overallocate; as multi-byte characters are in the argument, the |
| 428 | actual output could use less memory. */ |
| 429 | argsize = strlen(arg) + 1; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 430 | if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
| 431 | return -1; |
| 432 | } |
| 433 | res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t)); |
| 434 | if (!res) { |
| 435 | return -1; |
| 436 | } |
| 437 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 438 | in = (unsigned char*)arg; |
| 439 | out = res; |
| 440 | memset(&mbs, 0, sizeof mbs); |
| 441 | while (argsize) { |
| 442 | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 443 | if (converted == 0) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 444 | /* Reached end of string; null char stored. */ |
| 445 | break; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 448 | if (converted == (size_t)-2) { |
| 449 | /* Incomplete character. This should never happen, |
| 450 | since we provide everything that we have - |
| 451 | unless there is a bug in the C library, or I |
| 452 | misunderstood how mbrtowc works. */ |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 453 | goto decode_error; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 454 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 455 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 456 | if (converted == (size_t)-1) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 457 | if (!surrogateescape) { |
| 458 | goto decode_error; |
| 459 | } |
| 460 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 461 | /* Conversion error. Escape as UTF-8b, and start over |
| 462 | in the initial shift state. */ |
| 463 | *out++ = 0xdc00 + *in++; |
| 464 | argsize--; |
| 465 | memset(&mbs, 0, sizeof mbs); |
| 466 | continue; |
| 467 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 468 | |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 469 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 470 | if (!surrogateescape) { |
| 471 | goto decode_error; |
| 472 | } |
| 473 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 474 | /* Surrogate character. Escape the original |
| 475 | byte sequence with surrogateescape. */ |
| 476 | argsize -= converted; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 477 | while (converted--) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 478 | *out++ = 0xdc00 + *in++; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 479 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 480 | continue; |
| 481 | } |
| 482 | /* successfully converted some bytes */ |
| 483 | in += converted; |
| 484 | argsize -= converted; |
| 485 | out++; |
| 486 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 487 | if (wlen != NULL) { |
| 488 | *wlen = out - res; |
| 489 | } |
| 490 | *wstr = res; |
| 491 | return 0; |
| 492 | |
| 493 | decode_error: |
| 494 | PyMem_RawFree(res); |
| 495 | if (wlen) { |
| 496 | *wlen = in - (unsigned char*)arg; |
| 497 | } |
| 498 | if (reason) { |
| 499 | *reason = "decoding error"; |
| 500 | } |
| 501 | return -2; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 502 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 503 | /* Cannot use C locale for escaping; manually escape as if charset |
| 504 | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 505 | correctly in the locale's charset, which must be an ASCII superset. */ |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 506 | return decode_ascii(arg, wstr, wlen, reason, errors); |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 507 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 511 | /* Decode a byte string from the locale encoding. |
| 512 | |
| 513 | Use the strict error handler if 'surrogateescape' is zero. Use the |
| 514 | surrogateescape error handler if 'surrogateescape' is non-zero: undecodable |
| 515 | bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence |
| 516 | can be decoded as a surrogate character, escape the bytes using the |
| 517 | surrogateescape error handler instead of decoding them. |
| 518 | |
Ville Skyttä | 61f82e0 | 2018-04-20 23:08:45 +0300 | [diff] [blame] | 519 | On success, return 0 and write the newly allocated wide character string into |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 520 | *wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write |
| 521 | the number of wide characters excluding the null character into *wlen. |
| 522 | |
| 523 | On memory allocation failure, return -1. |
| 524 | |
| 525 | On decoding error, return -2. If wlen is not NULL, write the start of |
| 526 | invalid byte sequence in the input string into *wlen. If reason is not NULL, |
| 527 | write the decoding error message into *reason. |
| 528 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 529 | Return -3 if the error handler 'errors' is not supported. |
| 530 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 531 | Use the Py_EncodeLocaleEx() function to encode the character string back to |
| 532 | a byte string. */ |
| 533 | int |
| 534 | _Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen, |
| 535 | const char **reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 536 | int current_locale, _Py_error_handler errors) |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 537 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 538 | if (current_locale) { |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 539 | #ifdef _Py_FORCE_UTF8_LOCALE |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 540 | return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 541 | errors); |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 542 | #else |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 543 | return decode_current_locale(arg, wstr, wlen, reason, errors); |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 544 | #endif |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 547 | #ifdef _Py_FORCE_UTF8_FS_ENCODING |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 548 | return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 549 | errors); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 550 | #else |
Victor Stinner | c5989cd | 2018-08-29 19:32:47 +0200 | [diff] [blame] | 551 | int use_utf8 = (Py_UTF8Mode == 1); |
| 552 | #ifdef MS_WINDOWS |
| 553 | use_utf8 |= !Py_LegacyWindowsFSEncodingFlag; |
| 554 | #endif |
| 555 | if (use_utf8) { |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 556 | return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason, |
| 557 | errors); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | #ifdef USE_FORCE_ASCII |
| 561 | if (force_ascii == -1) { |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 562 | force_ascii = check_force_ascii(); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 563 | } |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 564 | |
| 565 | if (force_ascii) { |
| 566 | /* force ASCII encoding to workaround mbstowcs() issue */ |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 567 | return decode_ascii(arg, wstr, wlen, reason, errors); |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 568 | } |
| 569 | #endif |
| 570 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 571 | return decode_current_locale(arg, wstr, wlen, reason, errors); |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 572 | #endif /* !_Py_FORCE_UTF8_FS_ENCODING */ |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 576 | /* Decode a byte string from the locale encoding with the |
| 577 | surrogateescape error handler: undecodable bytes are decoded as characters |
| 578 | in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate |
| 579 | character, escape the bytes using the surrogateescape error handler instead |
| 580 | of decoding them. |
| 581 | |
| 582 | Return a pointer to a newly allocated wide character string, use |
| 583 | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
| 584 | wide characters excluding the null character into *size |
| 585 | |
| 586 | Return NULL on decoding error or memory allocation error. If *size* is not |
| 587 | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
| 588 | decoding error. |
| 589 | |
| 590 | Decoding errors should never happen, unless there is a bug in the C |
| 591 | library. |
| 592 | |
| 593 | Use the Py_EncodeLocale() function to encode the character string back to a |
| 594 | byte string. */ |
| 595 | wchar_t* |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 596 | Py_DecodeLocale(const char* arg, size_t *wlen) |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 597 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 598 | wchar_t *wstr; |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 599 | int res = _Py_DecodeLocaleEx(arg, &wstr, wlen, |
| 600 | NULL, 0, |
| 601 | _Py_ERROR_SURROGATEESCAPE); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 602 | if (res != 0) { |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 603 | assert(res != -3); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 604 | if (wlen != NULL) { |
| 605 | *wlen = (size_t)res; |
| 606 | } |
| 607 | return NULL; |
| 608 | } |
| 609 | return wstr; |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 610 | } |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 611 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 612 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 613 | static int |
| 614 | encode_current_locale(const wchar_t *text, char **str, |
| 615 | size_t *error_pos, const char **reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 616 | int raw_malloc, _Py_error_handler errors) |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 617 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 618 | const size_t len = wcslen(text); |
| 619 | char *result = NULL, *bytes = NULL; |
| 620 | size_t i, size, converted; |
| 621 | wchar_t c, buf[2]; |
| 622 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 623 | int surrogateescape; |
| 624 | if (get_surrogateescape(errors, &surrogateescape) < 0) { |
| 625 | return -3; |
| 626 | } |
| 627 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 628 | /* The function works in two steps: |
| 629 | 1. compute the length of the output buffer in bytes (size) |
| 630 | 2. outputs the bytes */ |
| 631 | size = 0; |
| 632 | buf[1] = 0; |
| 633 | while (1) { |
| 634 | for (i=0; i < len; i++) { |
| 635 | c = text[i]; |
| 636 | if (c >= 0xdc80 && c <= 0xdcff) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 637 | if (!surrogateescape) { |
| 638 | goto encode_error; |
| 639 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 640 | /* UTF-8b surrogate */ |
| 641 | if (bytes != NULL) { |
| 642 | *bytes++ = c - 0xdc00; |
| 643 | size--; |
| 644 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 645 | else { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 646 | size++; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 647 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 648 | continue; |
| 649 | } |
| 650 | else { |
| 651 | buf[0] = c; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 652 | if (bytes != NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 653 | converted = wcstombs(bytes, buf, size); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 654 | } |
| 655 | else { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 656 | converted = wcstombs(NULL, buf, 0); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 657 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 658 | if (converted == (size_t)-1) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 659 | goto encode_error; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 660 | } |
| 661 | if (bytes != NULL) { |
| 662 | bytes += converted; |
| 663 | size -= converted; |
| 664 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 665 | else { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 666 | size += converted; |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 667 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | if (result != NULL) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 671 | *bytes = '\0'; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 672 | break; |
| 673 | } |
| 674 | |
| 675 | size += 1; /* nul byte at the end */ |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 676 | if (raw_malloc) { |
| 677 | result = PyMem_RawMalloc(size); |
| 678 | } |
| 679 | else { |
| 680 | result = PyMem_Malloc(size); |
| 681 | } |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 682 | if (result == NULL) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 683 | return -1; |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 684 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 685 | bytes = result; |
| 686 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 687 | *str = result; |
| 688 | return 0; |
| 689 | |
| 690 | encode_error: |
| 691 | if (raw_malloc) { |
| 692 | PyMem_RawFree(result); |
| 693 | } |
| 694 | else { |
| 695 | PyMem_Free(result); |
| 696 | } |
| 697 | if (error_pos != NULL) { |
| 698 | *error_pos = i; |
| 699 | } |
| 700 | if (reason) { |
| 701 | *reason = "encoding error"; |
| 702 | } |
| 703 | return -2; |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 704 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 705 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 706 | |
| 707 | /* Encode a string to the locale encoding. |
| 708 | |
| 709 | Parameters: |
| 710 | |
| 711 | * raw_malloc: if non-zero, allocate memory using PyMem_RawMalloc() instead |
| 712 | of PyMem_Malloc(). |
| 713 | * current_locale: if non-zero, use the current LC_CTYPE, otherwise use |
| 714 | Python filesystem encoding. |
| 715 | * errors: error handler like "strict" or "surrogateescape". |
| 716 | |
| 717 | Return value: |
| 718 | |
| 719 | 0: success, *str is set to a newly allocated decoded string. |
| 720 | -1: memory allocation failure |
| 721 | -2: encoding error, set *error_pos and *reason (if set). |
| 722 | -3: the error handler 'errors' is not supported. |
| 723 | */ |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 724 | static int |
| 725 | encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos, |
| 726 | const char **reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 727 | int raw_malloc, int current_locale, _Py_error_handler errors) |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 728 | { |
| 729 | if (current_locale) { |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 730 | #ifdef _Py_FORCE_UTF8_LOCALE |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 731 | return _Py_EncodeUTF8Ex(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 732 | raw_malloc, errors); |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 733 | #else |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 734 | return encode_current_locale(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 735 | raw_malloc, errors); |
Victor Stinner | 9089a26 | 2018-01-22 19:07:32 +0100 | [diff] [blame] | 736 | #endif |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 739 | #ifdef _Py_FORCE_UTF8_FS_ENCODING |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 740 | return _Py_EncodeUTF8Ex(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 741 | raw_malloc, errors); |
| 742 | #else |
Victor Stinner | c5989cd | 2018-08-29 19:32:47 +0200 | [diff] [blame] | 743 | int use_utf8 = (Py_UTF8Mode == 1); |
| 744 | #ifdef MS_WINDOWS |
| 745 | use_utf8 |= !Py_LegacyWindowsFSEncodingFlag; |
| 746 | #endif |
| 747 | if (use_utf8) { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 748 | return _Py_EncodeUTF8Ex(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 749 | raw_malloc, errors); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | #ifdef USE_FORCE_ASCII |
| 753 | if (force_ascii == -1) { |
| 754 | force_ascii = check_force_ascii(); |
| 755 | } |
| 756 | |
| 757 | if (force_ascii) { |
| 758 | return encode_ascii(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 759 | raw_malloc, errors); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 760 | } |
Victor Stinner | d2b0231 | 2017-12-15 23:06:17 +0100 | [diff] [blame] | 761 | #endif |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 762 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 763 | return encode_current_locale(text, str, error_pos, reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 764 | raw_malloc, errors); |
Victor Stinner | e251095 | 2019-05-02 11:28:57 -0400 | [diff] [blame] | 765 | #endif /* _Py_FORCE_UTF8_FS_ENCODING */ |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 768 | static char* |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 769 | encode_locale(const wchar_t *text, size_t *error_pos, |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 770 | int raw_malloc, int current_locale) |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 771 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 772 | char *str; |
| 773 | int res = encode_locale_ex(text, &str, error_pos, NULL, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 774 | raw_malloc, current_locale, |
| 775 | _Py_ERROR_SURROGATEESCAPE); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 776 | if (res != -2 && error_pos) { |
| 777 | *error_pos = (size_t)-1; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 778 | } |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 779 | if (res != 0) { |
| 780 | return NULL; |
| 781 | } |
| 782 | return str; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 785 | /* Encode a wide character string to the locale encoding with the |
| 786 | surrogateescape error handler: surrogate characters in the range |
| 787 | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
| 788 | |
| 789 | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
| 790 | the memory. Return NULL on encoding or memory allocation error. |
| 791 | |
| 792 | If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set |
| 793 | to the index of the invalid character on encoding error. |
| 794 | |
| 795 | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
| 796 | character string. */ |
| 797 | char* |
| 798 | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
| 799 | { |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 800 | return encode_locale(text, error_pos, 0, 0); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 801 | } |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 802 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 803 | |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 804 | /* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree() |
| 805 | instead of PyMem_Free(). */ |
| 806 | char* |
| 807 | _Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos) |
| 808 | { |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 809 | return encode_locale(text, error_pos, 1, 0); |
| 810 | } |
| 811 | |
| 812 | |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 813 | int |
| 814 | _Py_EncodeLocaleEx(const wchar_t *text, char **str, |
| 815 | size_t *error_pos, const char **reason, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 816 | int current_locale, _Py_error_handler errors) |
Victor Stinner | 2cba6b8 | 2018-01-10 22:46:15 +0100 | [diff] [blame] | 817 | { |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 818 | return encode_locale_ex(text, str, error_pos, reason, 1, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 819 | current_locale, errors); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 822 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 823 | #ifdef MS_WINDOWS |
| 824 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 825 | |
| 826 | static void |
| 827 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
| 828 | { |
| 829 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 830 | /* Cannot simply cast and dereference in_ptr, |
| 831 | since it might not be aligned properly */ |
| 832 | __int64 in; |
| 833 | memcpy(&in, in_ptr, sizeof(in)); |
| 834 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 835 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
| 836 | } |
| 837 | |
| 838 | void |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 839 | _Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 840 | { |
| 841 | /* XXX endianness */ |
| 842 | __int64 out; |
| 843 | out = time_in + secs_between_epochs; |
| 844 | out = out * 10000000 + nsec_in / 100; |
| 845 | memcpy(out_ptr, &out, sizeof(out)); |
| 846 | } |
| 847 | |
| 848 | /* Below, we *know* that ugo+r is 0444 */ |
| 849 | #if _S_IREAD != 0400 |
| 850 | #error Unsupported C library |
| 851 | #endif |
| 852 | static int |
| 853 | attributes_to_mode(DWORD attr) |
| 854 | { |
| 855 | int m = 0; |
| 856 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 857 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 858 | else |
| 859 | m |= _S_IFREG; |
| 860 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 861 | m |= 0444; |
| 862 | else |
| 863 | m |= 0666; |
| 864 | return m; |
| 865 | } |
| 866 | |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 867 | void |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 868 | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, |
| 869 | struct _Py_stat_struct *result) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 870 | { |
| 871 | memset(result, 0, sizeof(*result)); |
| 872 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 873 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 874 | result->st_dev = info->dwVolumeSerialNumber; |
| 875 | result->st_rdev = result->st_dev; |
| 876 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 877 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 878 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 879 | result->st_nlink = info->nNumberOfLinks; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 880 | result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 881 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 882 | /* first clear the S_IFMT bits */ |
| 883 | result->st_mode ^= (result->st_mode & S_IFMT); |
| 884 | /* now set the bits that make this a symlink */ |
| 885 | result->st_mode |= S_IFLNK; |
| 886 | } |
| 887 | result->st_file_attributes = info->dwFileAttributes; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 888 | } |
| 889 | #endif |
| 890 | |
| 891 | /* Return information about a file. |
| 892 | |
| 893 | On POSIX, use fstat(). |
| 894 | |
| 895 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 896 | files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger |
| 897 | than 2 GiB because the file size type is a signed 32-bit integer: see issue |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 898 | #23152. |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 899 | |
| 900 | On Windows, set the last Windows error and return nonzero on error. On |
| 901 | POSIX, set errno and return nonzero on error. Fill status and return 0 on |
| 902 | success. */ |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 903 | int |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 904 | _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 905 | { |
| 906 | #ifdef MS_WINDOWS |
| 907 | BY_HANDLE_FILE_INFORMATION info; |
| 908 | HANDLE h; |
| 909 | int type; |
| 910 | |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 911 | _Py_BEGIN_SUPPRESS_IPH |
| 912 | h = (HANDLE)_get_osfhandle(fd); |
| 913 | _Py_END_SUPPRESS_IPH |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 914 | |
| 915 | if (h == INVALID_HANDLE_VALUE) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 916 | /* errno is already set by _get_osfhandle, but we also set |
| 917 | the Win32 error for callers who expect that */ |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 918 | SetLastError(ERROR_INVALID_HANDLE); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 919 | return -1; |
| 920 | } |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 921 | memset(status, 0, sizeof(*status)); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 922 | |
| 923 | type = GetFileType(h); |
| 924 | if (type == FILE_TYPE_UNKNOWN) { |
| 925 | DWORD error = GetLastError(); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 926 | if (error != 0) { |
| 927 | errno = winerror_to_errno(error); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 928 | return -1; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 929 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 930 | /* else: valid but unknown file */ |
| 931 | } |
| 932 | |
| 933 | if (type != FILE_TYPE_DISK) { |
| 934 | if (type == FILE_TYPE_CHAR) |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 935 | status->st_mode = _S_IFCHR; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 936 | else if (type == FILE_TYPE_PIPE) |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 937 | status->st_mode = _S_IFIFO; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | if (!GetFileInformationByHandle(h, &info)) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 942 | /* The Win32 error is already set, but we also set errno for |
| 943 | callers who expect it */ |
| 944 | errno = winerror_to_errno(GetLastError()); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 948 | _Py_attribute_data_to_stat(&info, 0, status); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 949 | /* specific to fstat() */ |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 950 | status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 951 | return 0; |
| 952 | #else |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 953 | return fstat(fd, status); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 954 | #endif |
| 955 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 956 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 957 | /* Return information about a file. |
| 958 | |
| 959 | On POSIX, use fstat(). |
| 960 | |
| 961 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 962 | files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger |
| 963 | than 2 GiB because the file size type is a signed 32-bit integer: see issue |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 964 | #23152. |
| 965 | |
| 966 | Raise an exception and return -1 on error. On Windows, set the last Windows |
| 967 | error on error. On POSIX, set errno on error. Fill status and return 0 on |
| 968 | success. |
| 969 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 970 | Release the GIL to call GetFileType() and GetFileInformationByHandle(), or |
| 971 | to call fstat(). The caller must hold the GIL. */ |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 972 | int |
| 973 | _Py_fstat(int fd, struct _Py_stat_struct *status) |
| 974 | { |
| 975 | int res; |
| 976 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 977 | assert(PyGILState_Check()); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 978 | |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 979 | Py_BEGIN_ALLOW_THREADS |
| 980 | res = _Py_fstat_noraise(fd, status); |
| 981 | Py_END_ALLOW_THREADS |
| 982 | |
| 983 | if (res != 0) { |
| 984 | #ifdef MS_WINDOWS |
| 985 | PyErr_SetFromWindowsErr(0); |
| 986 | #else |
| 987 | PyErr_SetFromErrno(PyExc_OSError); |
| 988 | #endif |
| 989 | return -1; |
| 990 | } |
| 991 | return 0; |
| 992 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 993 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 994 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 995 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 996 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 997 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 998 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 999 | |
| 1000 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 1001 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1002 | { |
| 1003 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1004 | int err; |
| 1005 | struct _stat wstatbuf; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1006 | const wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1007 | |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1008 | wpath = _PyUnicode_AsUnicode(path); |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1009 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1010 | return -2; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1011 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1012 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1013 | if (!err) |
| 1014 | statbuf->st_mode = wstatbuf.st_mode; |
| 1015 | return err; |
| 1016 | #else |
| 1017 | int ret; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1018 | PyObject *bytes; |
| 1019 | char *cpath; |
| 1020 | |
| 1021 | bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1022 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 1023 | return -2; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1024 | |
| 1025 | /* check for embedded null bytes */ |
| 1026 | if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) { |
| 1027 | Py_DECREF(bytes); |
| 1028 | return -2; |
| 1029 | } |
| 1030 | |
| 1031 | ret = stat(cpath, statbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1032 | Py_DECREF(bytes); |
| 1033 | return ret; |
| 1034 | #endif |
| 1035 | } |
| 1036 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 1037 | |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1038 | /* This function MUST be kept async-signal-safe on POSIX when raise=0. */ |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 1039 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1040 | get_inheritable(int fd, int raise) |
| 1041 | { |
| 1042 | #ifdef MS_WINDOWS |
| 1043 | HANDLE handle; |
| 1044 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1045 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1046 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1047 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1048 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1049 | if (handle == INVALID_HANDLE_VALUE) { |
| 1050 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1051 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | if (!GetHandleInformation(handle, &flags)) { |
| 1056 | if (raise) |
| 1057 | PyErr_SetFromWindowsErr(0); |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | return (flags & HANDLE_FLAG_INHERIT); |
| 1062 | #else |
| 1063 | int flags; |
| 1064 | |
| 1065 | flags = fcntl(fd, F_GETFD, 0); |
| 1066 | if (flags == -1) { |
| 1067 | if (raise) |
| 1068 | PyErr_SetFromErrno(PyExc_OSError); |
| 1069 | return -1; |
| 1070 | } |
| 1071 | return !(flags & FD_CLOEXEC); |
| 1072 | #endif |
| 1073 | } |
| 1074 | |
| 1075 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 1076 | Return 1 if the file descriptor can be inherited, 0 if it cannot, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1077 | raise an exception and return -1 on error. */ |
| 1078 | int |
| 1079 | _Py_get_inheritable(int fd) |
| 1080 | { |
| 1081 | return get_inheritable(fd, 1); |
| 1082 | } |
| 1083 | |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1084 | |
| 1085 | /* This function MUST be kept async-signal-safe on POSIX when raise=0. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1086 | static int |
| 1087 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 1088 | { |
| 1089 | #ifdef MS_WINDOWS |
| 1090 | HANDLE handle; |
| 1091 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1092 | #else |
| 1093 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 1094 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1095 | int request; |
| 1096 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1097 | #endif |
Victor Stinner | a858bbd | 2016-04-17 16:51:52 +0200 | [diff] [blame] | 1098 | int flags, new_flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1099 | int res; |
| 1100 | #endif |
| 1101 | |
| 1102 | /* atomic_flag_works can only be used to make the file descriptor |
| 1103 | non-inheritable */ |
| 1104 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 1105 | |
| 1106 | if (atomic_flag_works != NULL && !inheritable) { |
| 1107 | if (*atomic_flag_works == -1) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1108 | int isInheritable = get_inheritable(fd, raise); |
| 1109 | if (isInheritable == -1) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1110 | return -1; |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1111 | *atomic_flag_works = !isInheritable; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (*atomic_flag_works) |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | #ifdef MS_WINDOWS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1119 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1120 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1121 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1122 | if (handle == INVALID_HANDLE_VALUE) { |
| 1123 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1124 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1125 | return -1; |
| 1126 | } |
| 1127 | |
| 1128 | if (inheritable) |
| 1129 | flags = HANDLE_FLAG_INHERIT; |
| 1130 | else |
| 1131 | flags = 0; |
| 1132 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 1133 | if (raise) |
| 1134 | PyErr_SetFromWindowsErr(0); |
| 1135 | return -1; |
| 1136 | } |
| 1137 | return 0; |
| 1138 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1139 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1140 | |
| 1141 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1142 | if (ioctl_works != 0 && raise != 0) { |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1143 | /* fast-path: ioctl() only requires one syscall */ |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1144 | /* caveat: raise=0 is an indicator that we must be async-signal-safe |
| 1145 | * thus avoid using ioctl() so we skip the fast-path. */ |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1146 | if (inheritable) |
| 1147 | request = FIONCLEX; |
| 1148 | else |
| 1149 | request = FIOCLEX; |
| 1150 | err = ioctl(fd, request, NULL); |
| 1151 | if (!err) { |
| 1152 | ioctl_works = 1; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Victor Stinner | 3116cc4 | 2016-05-19 16:46:18 +0200 | [diff] [blame] | 1156 | if (errno != ENOTTY && errno != EACCES) { |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1157 | if (raise) |
| 1158 | PyErr_SetFromErrno(PyExc_OSError); |
| 1159 | return -1; |
| 1160 | } |
| 1161 | else { |
| 1162 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 1163 | device". The ioctl is declared but not supported by the kernel. |
| 1164 | Remember that ioctl() doesn't work. It is the case on |
Victor Stinner | 3116cc4 | 2016-05-19 16:46:18 +0200 | [diff] [blame] | 1165 | Illumos-based OS for example. |
| 1166 | |
| 1167 | Issue #27057: When SELinux policy disallows ioctl it will fail |
| 1168 | with EACCES. While FIOCLEX is safe operation it may be |
| 1169 | unavailable because ioctl was denied altogether. |
| 1170 | This can be the case on Android. */ |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 1171 | ioctl_works = 0; |
| 1172 | } |
| 1173 | /* fallback to fcntl() if ioctl() does not work */ |
| 1174 | } |
| 1175 | #endif |
| 1176 | |
| 1177 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1178 | flags = fcntl(fd, F_GETFD); |
| 1179 | if (flags < 0) { |
| 1180 | if (raise) |
| 1181 | PyErr_SetFromErrno(PyExc_OSError); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | |
Victor Stinner | a858bbd | 2016-04-17 16:51:52 +0200 | [diff] [blame] | 1185 | if (inheritable) { |
| 1186 | new_flags = flags & ~FD_CLOEXEC; |
| 1187 | } |
| 1188 | else { |
| 1189 | new_flags = flags | FD_CLOEXEC; |
| 1190 | } |
| 1191 | |
| 1192 | if (new_flags == flags) { |
| 1193 | /* FD_CLOEXEC flag already set/cleared: nothing to do */ |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Xavier de Gaye | ec5d3cd | 2016-11-19 16:19:29 +0100 | [diff] [blame] | 1197 | res = fcntl(fd, F_SETFD, new_flags); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1198 | if (res < 0) { |
| 1199 | if (raise) |
| 1200 | PyErr_SetFromErrno(PyExc_OSError); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | return 0; |
| 1204 | #endif |
| 1205 | } |
| 1206 | |
| 1207 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 1208 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1209 | static int |
| 1210 | make_non_inheritable(int fd) |
| 1211 | { |
| 1212 | return set_inheritable(fd, 0, 0, NULL); |
| 1213 | } |
| 1214 | |
| 1215 | /* Set the inheritable flag of the specified file descriptor. |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1216 | On success: return 0, on error: raise an exception and return -1. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1217 | |
| 1218 | If atomic_flag_works is not NULL: |
| 1219 | |
| 1220 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 1221 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 1222 | set the inheritable flag |
| 1223 | * if *atomic_flag_works==1: do nothing |
| 1224 | * if *atomic_flag_works==0: set inheritable flag to False |
| 1225 | |
| 1226 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 1227 | file descriptor. |
| 1228 | |
| 1229 | atomic_flag_works can only be used to make a file descriptor |
| 1230 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 1231 | int |
| 1232 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 1233 | { |
| 1234 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 1235 | } |
| 1236 | |
Alexey Izbyshev | c1e46e9 | 2018-02-06 09:09:34 +0300 | [diff] [blame] | 1237 | /* Same as _Py_set_inheritable() but on error, set errno and |
| 1238 | don't raise an exception. |
| 1239 | This function is async-signal-safe. */ |
| 1240 | int |
| 1241 | _Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works) |
| 1242 | { |
| 1243 | return set_inheritable(fd, inheritable, 0, atomic_flag_works); |
| 1244 | } |
| 1245 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1246 | static int |
| 1247 | _Py_open_impl(const char *pathname, int flags, int gil_held) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1248 | { |
| 1249 | int fd; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1250 | int async_err = 0; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1251 | #ifndef MS_WINDOWS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1252 | int *atomic_flag_works; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1253 | #endif |
| 1254 | |
| 1255 | #ifdef MS_WINDOWS |
| 1256 | flags |= O_NOINHERIT; |
| 1257 | #elif defined(O_CLOEXEC) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1258 | atomic_flag_works = &_Py_open_cloexec_works; |
| 1259 | flags |= O_CLOEXEC; |
| 1260 | #else |
| 1261 | atomic_flag_works = NULL; |
| 1262 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1263 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1264 | if (gil_held) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1265 | if (PySys_Audit("open", "sOi", pathname, Py_None, flags) < 0) { |
| 1266 | return -1; |
| 1267 | } |
| 1268 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1269 | do { |
| 1270 | Py_BEGIN_ALLOW_THREADS |
| 1271 | fd = open(pathname, flags); |
| 1272 | Py_END_ALLOW_THREADS |
| 1273 | } while (fd < 0 |
| 1274 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1275 | if (async_err) |
| 1276 | return -1; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1277 | if (fd < 0) { |
| 1278 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); |
| 1279 | return -1; |
| 1280 | } |
| 1281 | } |
| 1282 | else { |
| 1283 | fd = open(pathname, flags); |
| 1284 | if (fd < 0) |
| 1285 | return -1; |
| 1286 | } |
| 1287 | |
| 1288 | #ifndef MS_WINDOWS |
| 1289 | if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1290 | close(fd); |
| 1291 | return -1; |
| 1292 | } |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1293 | #endif |
| 1294 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1295 | return fd; |
| 1296 | } |
| 1297 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1298 | /* Open a file with the specified flags (wrapper to open() function). |
| 1299 | Return a file descriptor on success. Raise an exception and return -1 on |
| 1300 | error. |
| 1301 | |
| 1302 | The file descriptor is created non-inheritable. |
| 1303 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1304 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 1305 | except if the Python signal handler raises an exception. |
| 1306 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 1307 | Release the GIL to call open(). The caller must hold the GIL. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1308 | int |
| 1309 | _Py_open(const char *pathname, int flags) |
| 1310 | { |
| 1311 | /* _Py_open() must be called with the GIL held. */ |
| 1312 | assert(PyGILState_Check()); |
| 1313 | return _Py_open_impl(pathname, flags, 1); |
| 1314 | } |
| 1315 | |
| 1316 | /* Open a file with the specified flags (wrapper to open() function). |
| 1317 | Return a file descriptor on success. Set errno and return -1 on error. |
| 1318 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1319 | The file descriptor is created non-inheritable. |
| 1320 | |
| 1321 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1322 | int |
| 1323 | _Py_open_noraise(const char *pathname, int flags) |
| 1324 | { |
| 1325 | return _Py_open_impl(pathname, flags, 0); |
| 1326 | } |
| 1327 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1328 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1329 | encoding and use fopen() otherwise. |
| 1330 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1331 | The file descriptor is created non-inheritable. |
| 1332 | |
| 1333 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1334 | FILE * |
| 1335 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 1336 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1337 | FILE *f; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1338 | if (PySys_Audit("open", "uui", path, mode, 0) < 0) { |
| 1339 | return NULL; |
| 1340 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1341 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1342 | char *cpath; |
| 1343 | char cmode[10]; |
| 1344 | size_t r; |
| 1345 | r = wcstombs(cmode, mode, 10); |
| 1346 | if (r == (size_t)-1 || r >= 10) { |
| 1347 | errno = EINVAL; |
| 1348 | return NULL; |
| 1349 | } |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1350 | cpath = _Py_EncodeLocaleRaw(path, NULL); |
| 1351 | if (cpath == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1352 | return NULL; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1353 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1354 | f = fopen(cpath, cmode); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1355 | PyMem_RawFree(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1356 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1357 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1358 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1359 | if (f == NULL) |
| 1360 | return NULL; |
| 1361 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1362 | fclose(f); |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1368 | /* Wrapper to fopen(). |
| 1369 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1370 | The file descriptor is created non-inheritable. |
| 1371 | |
| 1372 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1373 | FILE* |
| 1374 | _Py_fopen(const char *pathname, const char *mode) |
| 1375 | { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1376 | if (PySys_Audit("open", "ssi", pathname, mode, 0) < 0) { |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1380 | FILE *f = fopen(pathname, mode); |
| 1381 | if (f == NULL) |
| 1382 | return NULL; |
| 1383 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1384 | fclose(f); |
| 1385 | return NULL; |
| 1386 | } |
| 1387 | return f; |
| 1388 | } |
| 1389 | |
| 1390 | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1391 | encoding and call fopen() otherwise. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1392 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1393 | Return the new file object on success. Raise an exception and return NULL |
| 1394 | on error. |
| 1395 | |
| 1396 | The file descriptor is created non-inheritable. |
| 1397 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1398 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 1399 | except if the Python signal handler raises an exception. |
| 1400 | |
Victor Stinner | 6f4fae8 | 2015-04-01 18:34:32 +0200 | [diff] [blame] | 1401 | Release the GIL to call _wfopen() or fopen(). The caller must hold |
| 1402 | the GIL. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1403 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1404 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1405 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1406 | FILE *f; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1407 | int async_err = 0; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1408 | #ifdef MS_WINDOWS |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1409 | const wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1410 | wchar_t wmode[10]; |
| 1411 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1412 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1413 | assert(PyGILState_Check()); |
| 1414 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1415 | if (PySys_Audit("open", "Osi", path, mode, 0) < 0) { |
| 1416 | return NULL; |
| 1417 | } |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1418 | if (!PyUnicode_Check(path)) { |
| 1419 | PyErr_Format(PyExc_TypeError, |
| 1420 | "str file path expected under Windows, got %R", |
| 1421 | Py_TYPE(path)); |
| 1422 | return NULL; |
| 1423 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 1424 | wpath = _PyUnicode_AsUnicode(path); |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1425 | if (wpath == NULL) |
| 1426 | return NULL; |
| 1427 | |
Alexey Izbyshev | b3b4a9d | 2018-02-18 20:57:24 +0300 | [diff] [blame] | 1428 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, |
| 1429 | wmode, Py_ARRAY_LENGTH(wmode)); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1430 | if (usize == 0) { |
| 1431 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1432 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1433 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1434 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1435 | do { |
| 1436 | Py_BEGIN_ALLOW_THREADS |
| 1437 | f = _wfopen(wpath, wmode); |
| 1438 | Py_END_ALLOW_THREADS |
| 1439 | } while (f == NULL |
| 1440 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1441 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1442 | PyObject *bytes; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1443 | char *path_bytes; |
| 1444 | |
| 1445 | assert(PyGILState_Check()); |
| 1446 | |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1447 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1448 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1449 | path_bytes = PyBytes_AS_STRING(bytes); |
| 1450 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 1451 | if (PySys_Audit("open", "Osi", path, mode, 0) < 0) { |
| 1452 | return NULL; |
| 1453 | } |
| 1454 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1455 | do { |
| 1456 | Py_BEGIN_ALLOW_THREADS |
| 1457 | f = fopen(path_bytes, mode); |
| 1458 | Py_END_ALLOW_THREADS |
| 1459 | } while (f == NULL |
| 1460 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1461 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1462 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1463 | #endif |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1464 | if (async_err) |
| 1465 | return NULL; |
| 1466 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1467 | if (f == NULL) { |
| 1468 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1469 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1473 | fclose(f); |
| 1474 | return NULL; |
| 1475 | } |
| 1476 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1479 | /* Read count bytes from fd into buf. |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1480 | |
| 1481 | On success, return the number of read bytes, it can be lower than count. |
| 1482 | If the current file offset is at or past the end of file, no bytes are read, |
| 1483 | and read() returns zero. |
| 1484 | |
| 1485 | On error, raise an exception, set errno and return -1. |
| 1486 | |
| 1487 | When interrupted by a signal (read() fails with EINTR), retry the syscall. |
| 1488 | If the Python signal handler raises an exception, the function returns -1 |
| 1489 | (the syscall is not retried). |
| 1490 | |
| 1491 | Release the GIL to call read(). The caller must hold the GIL. */ |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1492 | Py_ssize_t |
| 1493 | _Py_read(int fd, void *buf, size_t count) |
| 1494 | { |
| 1495 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1496 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1497 | int async_err = 0; |
| 1498 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1499 | assert(PyGILState_Check()); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1500 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1501 | /* _Py_read() must not be called with an exception set, otherwise the |
| 1502 | * caller may think that read() was interrupted by a signal and the signal |
| 1503 | * handler raised an exception. */ |
| 1504 | assert(!PyErr_Occurred()); |
| 1505 | |
Stéphane Wirtel | 74a8b6e | 2018-10-18 01:05:04 +0200 | [diff] [blame] | 1506 | if (count > _PY_READ_MAX) { |
| 1507 | count = _PY_READ_MAX; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1508 | } |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1509 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1510 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1511 | do { |
| 1512 | Py_BEGIN_ALLOW_THREADS |
| 1513 | errno = 0; |
| 1514 | #ifdef MS_WINDOWS |
| 1515 | n = read(fd, buf, (int)count); |
| 1516 | #else |
| 1517 | n = read(fd, buf, count); |
| 1518 | #endif |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1519 | /* save/restore errno because PyErr_CheckSignals() |
| 1520 | * and PyErr_SetFromErrno() can modify it */ |
| 1521 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1522 | Py_END_ALLOW_THREADS |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1523 | } while (n < 0 && err == EINTR && |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1524 | !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1525 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1526 | |
| 1527 | if (async_err) { |
| 1528 | /* read() was interrupted by a signal (failed with EINTR) |
| 1529 | * and the Python signal handler raised an exception */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1530 | errno = err; |
| 1531 | assert(errno == EINTR && PyErr_Occurred()); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1532 | return -1; |
| 1533 | } |
| 1534 | if (n < 0) { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1535 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1536 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1537 | return -1; |
| 1538 | } |
| 1539 | |
| 1540 | return n; |
| 1541 | } |
| 1542 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1543 | static Py_ssize_t |
| 1544 | _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1545 | { |
| 1546 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1547 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1548 | int async_err = 0; |
| 1549 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1550 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1551 | #ifdef MS_WINDOWS |
| 1552 | if (count > 32767 && isatty(fd)) { |
| 1553 | /* Issue #11395: the Windows console returns an error (12: not |
| 1554 | enough space error) on writing into stdout if stdout mode is |
| 1555 | binary and the length is greater than 66,000 bytes (or less, |
| 1556 | depending on heap usage). */ |
| 1557 | count = 32767; |
| 1558 | } |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1559 | #endif |
Stéphane Wirtel | 74a8b6e | 2018-10-18 01:05:04 +0200 | [diff] [blame] | 1560 | if (count > _PY_WRITE_MAX) { |
| 1561 | count = _PY_WRITE_MAX; |
| 1562 | } |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1563 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1564 | if (gil_held) { |
| 1565 | do { |
| 1566 | Py_BEGIN_ALLOW_THREADS |
| 1567 | errno = 0; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1568 | #ifdef MS_WINDOWS |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1569 | n = write(fd, buf, (int)count); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1570 | #else |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1571 | n = write(fd, buf, count); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1572 | #endif |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1573 | /* save/restore errno because PyErr_CheckSignals() |
| 1574 | * and PyErr_SetFromErrno() can modify it */ |
| 1575 | err = errno; |
| 1576 | Py_END_ALLOW_THREADS |
| 1577 | } while (n < 0 && err == EINTR && |
| 1578 | !(async_err = PyErr_CheckSignals())); |
| 1579 | } |
| 1580 | else { |
| 1581 | do { |
| 1582 | errno = 0; |
| 1583 | #ifdef MS_WINDOWS |
| 1584 | n = write(fd, buf, (int)count); |
| 1585 | #else |
| 1586 | n = write(fd, buf, count); |
| 1587 | #endif |
| 1588 | err = errno; |
| 1589 | } while (n < 0 && err == EINTR); |
| 1590 | } |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1591 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1592 | |
| 1593 | if (async_err) { |
| 1594 | /* write() was interrupted by a signal (failed with EINTR) |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1595 | and the Python signal handler raised an exception (if gil_held is |
| 1596 | nonzero). */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1597 | errno = err; |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1598 | assert(errno == EINTR && (!gil_held || PyErr_Occurred())); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1599 | return -1; |
| 1600 | } |
| 1601 | if (n < 0) { |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1602 | if (gil_held) |
| 1603 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1604 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1605 | return -1; |
| 1606 | } |
| 1607 | |
| 1608 | return n; |
| 1609 | } |
| 1610 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1611 | /* Write count bytes of buf into fd. |
| 1612 | |
| 1613 | On success, return the number of written bytes, it can be lower than count |
| 1614 | including 0. On error, raise an exception, set errno and return -1. |
| 1615 | |
| 1616 | When interrupted by a signal (write() fails with EINTR), retry the syscall. |
| 1617 | If the Python signal handler raises an exception, the function returns -1 |
| 1618 | (the syscall is not retried). |
| 1619 | |
| 1620 | Release the GIL to call write(). The caller must hold the GIL. */ |
| 1621 | Py_ssize_t |
| 1622 | _Py_write(int fd, const void *buf, size_t count) |
| 1623 | { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1624 | assert(PyGILState_Check()); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1625 | |
Victor Stinner | 82c3e45 | 2015-04-01 18:34:45 +0200 | [diff] [blame] | 1626 | /* _Py_write() must not be called with an exception set, otherwise the |
| 1627 | * caller may think that write() was interrupted by a signal and the signal |
| 1628 | * handler raised an exception. */ |
| 1629 | assert(!PyErr_Occurred()); |
| 1630 | |
| 1631 | return _Py_write_impl(fd, buf, count, 1); |
| 1632 | } |
| 1633 | |
| 1634 | /* Write count bytes of buf into fd. |
| 1635 | * |
| 1636 | * On success, return the number of written bytes, it can be lower than count |
| 1637 | * including 0. On error, set errno and return -1. |
| 1638 | * |
| 1639 | * When interrupted by a signal (write() fails with EINTR), retry the syscall |
| 1640 | * without calling the Python signal handler. */ |
| 1641 | Py_ssize_t |
| 1642 | _Py_write_noraise(int fd, const void *buf, size_t count) |
| 1643 | { |
| 1644 | return _Py_write_impl(fd, buf, count, 0); |
| 1645 | } |
| 1646 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1647 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1648 | |
| 1649 | /* Read value of symbolic link. Encode the path to the locale encoding, decode |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1650 | the result from the locale encoding. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1651 | |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1652 | Return -1 on encoding error, on readlink() error, if the internal buffer is |
| 1653 | too short, on decoding error, or if 'buf' is too short. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1654 | int |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1655 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t buflen) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1656 | { |
| 1657 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1658 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1659 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1660 | int res; |
| 1661 | size_t r1; |
| 1662 | |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1663 | cpath = _Py_EncodeLocaleRaw(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1664 | if (cpath == NULL) { |
| 1665 | errno = EINVAL; |
| 1666 | return -1; |
| 1667 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1668 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1669 | PyMem_RawFree(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1670 | if (res == -1) |
| 1671 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1672 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1673 | errno = EINVAL; |
| 1674 | return -1; |
| 1675 | } |
| 1676 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1677 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 1678 | if (wbuf == NULL) { |
| 1679 | errno = EINVAL; |
| 1680 | return -1; |
| 1681 | } |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1682 | /* wbuf must have space to store the trailing NUL character */ |
| 1683 | if (buflen <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1684 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1685 | errno = EINVAL; |
| 1686 | return -1; |
| 1687 | } |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1688 | wcsncpy(buf, wbuf, buflen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1689 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1690 | return (int)r1; |
| 1691 | } |
| 1692 | #endif |
| 1693 | |
| 1694 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1695 | |
| 1696 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1697 | encoding, decode the result from the locale encoding. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1698 | |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1699 | Return NULL on encoding error, realpath() error, decoding error |
| 1700 | or if 'resolved_path' is too short. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1701 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1702 | _Py_wrealpath(const wchar_t *path, |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1703 | wchar_t *resolved_path, size_t resolved_path_len) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1704 | { |
| 1705 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1706 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1707 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1708 | char *res; |
| 1709 | size_t r; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1710 | cpath = _Py_EncodeLocaleRaw(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1711 | if (cpath == NULL) { |
| 1712 | errno = EINVAL; |
| 1713 | return NULL; |
| 1714 | } |
| 1715 | res = realpath(cpath, cresolved_path); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 1716 | PyMem_RawFree(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1717 | if (res == NULL) |
| 1718 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1719 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1720 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1721 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1722 | errno = EINVAL; |
| 1723 | return NULL; |
| 1724 | } |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1725 | /* wresolved_path must have space to store the trailing NUL character */ |
| 1726 | if (resolved_path_len <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1727 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1728 | errno = EINVAL; |
| 1729 | return NULL; |
| 1730 | } |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1731 | wcsncpy(resolved_path, wresolved_path, resolved_path_len); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1732 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1733 | return resolved_path; |
| 1734 | } |
| 1735 | #endif |
| 1736 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 1737 | /* Get the current directory. buflen is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1738 | including the null character. Decode the path from the locale encoding. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1739 | |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1740 | Return NULL on getcwd() error, on decoding error, or if 'buf' is |
| 1741 | too short. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1742 | wchar_t* |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1743 | _Py_wgetcwd(wchar_t *buf, size_t buflen) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1744 | { |
| 1745 | #ifdef MS_WINDOWS |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1746 | int ibuflen = (int)Py_MIN(buflen, INT_MAX); |
| 1747 | return _wgetcwd(buf, ibuflen); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1748 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1749 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1750 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1751 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1752 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1753 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1754 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1755 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1756 | if (wname == NULL) |
| 1757 | return NULL; |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1758 | /* wname must have space to store the trailing NUL character */ |
| 1759 | if (buflen <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1760 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1761 | return NULL; |
| 1762 | } |
Victor Stinner | 1be0d11 | 2019-03-18 17:47:26 +0100 | [diff] [blame] | 1763 | wcsncpy(buf, wname, buflen); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1764 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1765 | return buf; |
| 1766 | #endif |
| 1767 | } |
| 1768 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1769 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1770 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1771 | exception and return -1 on error. |
| 1772 | |
| 1773 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1774 | int |
| 1775 | _Py_dup(int fd) |
| 1776 | { |
| 1777 | #ifdef MS_WINDOWS |
| 1778 | HANDLE handle; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1779 | #endif |
| 1780 | |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1781 | assert(PyGILState_Check()); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 1782 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1783 | #ifdef MS_WINDOWS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1784 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1785 | handle = (HANDLE)_get_osfhandle(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1786 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1787 | if (handle == INVALID_HANDLE_VALUE) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1788 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1789 | return -1; |
| 1790 | } |
| 1791 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1792 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1793 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1794 | fd = dup(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1795 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1796 | Py_END_ALLOW_THREADS |
| 1797 | if (fd < 0) { |
| 1798 | PyErr_SetFromErrno(PyExc_OSError); |
| 1799 | return -1; |
| 1800 | } |
| 1801 | |
Miss Islington (bot) | 693945d | 2019-06-17 01:45:26 -0700 | [diff] [blame] | 1802 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1803 | _Py_BEGIN_SUPPRESS_IPH |
| 1804 | close(fd); |
| 1805 | _Py_END_SUPPRESS_IPH |
| 1806 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1807 | } |
| 1808 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1809 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1810 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1811 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1812 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1813 | Py_END_ALLOW_THREADS |
| 1814 | if (fd < 0) { |
| 1815 | PyErr_SetFromErrno(PyExc_OSError); |
| 1816 | return -1; |
| 1817 | } |
| 1818 | |
| 1819 | #else |
| 1820 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1821 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1822 | fd = dup(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1823 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1824 | Py_END_ALLOW_THREADS |
| 1825 | if (fd < 0) { |
| 1826 | PyErr_SetFromErrno(PyExc_OSError); |
| 1827 | return -1; |
| 1828 | } |
| 1829 | |
| 1830 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1831 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1832 | close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1833 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1834 | return -1; |
| 1835 | } |
| 1836 | #endif |
| 1837 | return fd; |
| 1838 | } |
| 1839 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1840 | #ifndef MS_WINDOWS |
| 1841 | /* Get the blocking mode of the file descriptor. |
| 1842 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1843 | raise an exception and return -1 on error. */ |
| 1844 | int |
| 1845 | _Py_get_blocking(int fd) |
| 1846 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1847 | int flags; |
| 1848 | _Py_BEGIN_SUPPRESS_IPH |
| 1849 | flags = fcntl(fd, F_GETFL, 0); |
| 1850 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1851 | if (flags < 0) { |
| 1852 | PyErr_SetFromErrno(PyExc_OSError); |
| 1853 | return -1; |
| 1854 | } |
| 1855 | |
| 1856 | return !(flags & O_NONBLOCK); |
| 1857 | } |
| 1858 | |
| 1859 | /* Set the blocking mode of the specified file descriptor. |
| 1860 | |
| 1861 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1862 | otherwise. |
| 1863 | |
| 1864 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1865 | int |
| 1866 | _Py_set_blocking(int fd, int blocking) |
| 1867 | { |
| 1868 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1869 | int arg = !blocking; |
| 1870 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1871 | goto error; |
| 1872 | #else |
| 1873 | int flags, res; |
| 1874 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1875 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1876 | flags = fcntl(fd, F_GETFL, 0); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1877 | if (flags >= 0) { |
| 1878 | if (blocking) |
| 1879 | flags = flags & (~O_NONBLOCK); |
| 1880 | else |
| 1881 | flags = flags | O_NONBLOCK; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1882 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1883 | res = fcntl(fd, F_SETFL, flags); |
| 1884 | } else { |
| 1885 | res = -1; |
| 1886 | } |
| 1887 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1888 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1889 | if (res < 0) |
| 1890 | goto error; |
| 1891 | #endif |
| 1892 | return 0; |
| 1893 | |
| 1894 | error: |
| 1895 | PyErr_SetFromErrno(PyExc_OSError); |
| 1896 | return -1; |
| 1897 | } |
| 1898 | #endif |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1899 | |
| 1900 | |
| 1901 | int |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1902 | _Py_GetLocaleconvNumeric(struct lconv *lc, |
| 1903 | PyObject **decimal_point, PyObject **thousands_sep) |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1904 | { |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1905 | assert(decimal_point != NULL); |
| 1906 | assert(thousands_sep != NULL); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1907 | |
| 1908 | int change_locale = 0; |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1909 | if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) { |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1910 | change_locale = 1; |
| 1911 | } |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1912 | if ((strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127)) { |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1913 | change_locale = 1; |
| 1914 | } |
| 1915 | |
| 1916 | /* Keep a copy of the LC_CTYPE locale */ |
| 1917 | char *oldloc = NULL, *loc = NULL; |
| 1918 | if (change_locale) { |
| 1919 | oldloc = setlocale(LC_CTYPE, NULL); |
| 1920 | if (!oldloc) { |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1921 | PyErr_SetString(PyExc_RuntimeWarning, |
| 1922 | "failed to get LC_CTYPE locale"); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1923 | return -1; |
| 1924 | } |
| 1925 | |
| 1926 | oldloc = _PyMem_Strdup(oldloc); |
| 1927 | if (!oldloc) { |
| 1928 | PyErr_NoMemory(); |
| 1929 | return -1; |
| 1930 | } |
| 1931 | |
| 1932 | loc = setlocale(LC_NUMERIC, NULL); |
| 1933 | if (loc != NULL && strcmp(loc, oldloc) == 0) { |
| 1934 | loc = NULL; |
| 1935 | } |
| 1936 | |
| 1937 | if (loc != NULL) { |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1938 | /* Only set the locale temporarily the LC_CTYPE locale |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1939 | if LC_NUMERIC locale is different than LC_CTYPE locale and |
| 1940 | decimal_point and/or thousands_sep are non-ASCII or longer than |
| 1941 | 1 byte */ |
| 1942 | setlocale(LC_CTYPE, loc); |
| 1943 | } |
| 1944 | } |
| 1945 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1946 | int res = -1; |
| 1947 | |
| 1948 | *decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL); |
| 1949 | if (*decimal_point == NULL) { |
| 1950 | goto done; |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1951 | } |
| 1952 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1953 | *thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL); |
| 1954 | if (*thousands_sep == NULL) { |
| 1955 | goto done; |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | res = 0; |
| 1959 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 1960 | done: |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 1961 | if (loc != NULL) { |
| 1962 | setlocale(LC_CTYPE, oldloc); |
| 1963 | } |
| 1964 | PyMem_Free(oldloc); |
| 1965 | return res; |
| 1966 | } |