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