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