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