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 |
| 6 | # include <windows.h> |
| 7 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 8 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 9 | #ifdef HAVE_LANGINFO_H |
| 10 | #include <langinfo.h> |
| 11 | #endif |
| 12 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13 | #ifdef HAVE_SYS_IOCTL_H |
| 14 | #include <sys/ioctl.h> |
| 15 | #endif |
| 16 | |
| 17 | #ifdef HAVE_FCNTL_H |
| 18 | #include <fcntl.h> |
| 19 | #endif /* HAVE_FCNTL_H */ |
| 20 | |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 21 | #ifdef __APPLE__ |
| 22 | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
| 23 | #endif |
| 24 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 25 | #ifdef O_CLOEXEC |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 26 | /* Does open() support the O_CLOEXEC flag? Possible values: |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 27 | |
| 28 | -1: unknown |
| 29 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
| 30 | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
| 31 | |
| 32 | The flag is used by _Py_open(), io.FileIO and os.open() */ |
| 33 | int _Py_open_cloexec_works = -1; |
| 34 | #endif |
| 35 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 36 | PyObject * |
| 37 | _Py_device_encoding(int fd) |
| 38 | { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 39 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 40 | UINT cp; |
| 41 | #endif |
| 42 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 43 | Py_RETURN_NONE; |
| 44 | } |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 45 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 46 | if (fd == 0) |
| 47 | cp = GetConsoleCP(); |
| 48 | else if (fd == 1 || fd == 2) |
| 49 | cp = GetConsoleOutputCP(); |
| 50 | else |
| 51 | cp = 0; |
| 52 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 53 | has no console */ |
| 54 | if (cp != 0) |
| 55 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
| 56 | #elif defined(CODESET) |
| 57 | { |
| 58 | char *codeset = nl_langinfo(CODESET); |
| 59 | if (codeset != NULL && codeset[0] != 0) |
| 60 | return PyUnicode_FromString(codeset); |
| 61 | } |
| 62 | #endif |
| 63 | Py_RETURN_NONE; |
| 64 | } |
| 65 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 66 | #if !defined(__APPLE__) && !defined(MS_WINDOWS) |
| 67 | extern int _Py_normalize_encoding(const char *, char *, size_t); |
| 68 | |
| 69 | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale. |
| 70 | On these operating systems, nl_langinfo(CODESET) announces an alias of the |
| 71 | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
| 72 | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
| 73 | locale.getpreferredencoding() codec. For example, if command line arguments |
| 74 | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
| 75 | UnicodeEncodeError instead of retrieving the original byte string. |
| 76 | |
| 77 | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
| 78 | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
| 79 | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
| 80 | workaround is also enabled on error, for example if getting the locale |
| 81 | failed. |
| 82 | |
Philip Jenvey | 215c49a | 2013-01-15 13:24:12 -0800 | [diff] [blame] | 83 | Values of force_ascii: |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 84 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 85 | 1: the workaround is used: Py_EncodeLocale() uses |
| 86 | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 87 | decode_ascii_surrogateescape() |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 88 | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
| 89 | Py_DecodeLocale() uses mbstowcs() |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 90 | -1: unknown, need to call check_force_ascii() to get the value |
| 91 | */ |
| 92 | static int force_ascii = -1; |
| 93 | |
| 94 | static int |
| 95 | check_force_ascii(void) |
| 96 | { |
| 97 | char *loc; |
| 98 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 99 | char *codeset, **alias; |
| 100 | char encoding[100]; |
| 101 | int is_ascii; |
| 102 | unsigned int i; |
| 103 | char* ascii_aliases[] = { |
| 104 | "ascii", |
| 105 | "646", |
| 106 | "ansi-x3.4-1968", |
| 107 | "ansi-x3-4-1968", |
| 108 | "ansi-x3.4-1986", |
| 109 | "cp367", |
| 110 | "csascii", |
| 111 | "ibm367", |
| 112 | "iso646-us", |
| 113 | "iso-646.irv-1991", |
| 114 | "iso-ir-6", |
| 115 | "us", |
| 116 | "us-ascii", |
| 117 | NULL |
| 118 | }; |
| 119 | #endif |
| 120 | |
| 121 | loc = setlocale(LC_CTYPE, NULL); |
| 122 | if (loc == NULL) |
| 123 | goto error; |
| 124 | if (strcmp(loc, "C") != 0) { |
| 125 | /* the LC_CTYPE locale is different than C */ |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 130 | codeset = nl_langinfo(CODESET); |
| 131 | if (!codeset || codeset[0] == '\0') { |
| 132 | /* CODESET is not set or empty */ |
| 133 | goto error; |
| 134 | } |
| 135 | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) |
| 136 | goto error; |
| 137 | |
| 138 | is_ascii = 0; |
| 139 | for (alias=ascii_aliases; *alias != NULL; alias++) { |
| 140 | if (strcmp(encoding, *alias) == 0) { |
| 141 | is_ascii = 1; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (!is_ascii) { |
| 146 | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | for (i=0x80; i<0xff; i++) { |
| 151 | unsigned char ch; |
| 152 | wchar_t wch; |
| 153 | size_t res; |
| 154 | |
| 155 | ch = (unsigned char)i; |
| 156 | res = mbstowcs(&wch, (char*)&ch, 1); |
| 157 | if (res != (size_t)-1) { |
| 158 | /* decoding a non-ASCII character from the locale encoding succeed: |
| 159 | the locale encoding is not ASCII, force ASCII */ |
| 160 | return 1; |
| 161 | } |
| 162 | } |
| 163 | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
| 164 | encoding: the locale encoding is really ASCII */ |
| 165 | return 0; |
| 166 | #else |
| 167 | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
| 168 | return 1; |
| 169 | #endif |
| 170 | |
| 171 | error: |
| 172 | /* if an error occured, force the ASCII encoding */ |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | static char* |
| 177 | encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos) |
| 178 | { |
| 179 | char *result = NULL, *out; |
| 180 | size_t len, i; |
| 181 | wchar_t ch; |
| 182 | |
| 183 | if (error_pos != NULL) |
| 184 | *error_pos = (size_t)-1; |
| 185 | |
| 186 | len = wcslen(text); |
| 187 | |
| 188 | result = PyMem_Malloc(len + 1); /* +1 for NUL byte */ |
| 189 | if (result == NULL) |
| 190 | return NULL; |
| 191 | |
| 192 | out = result; |
| 193 | for (i=0; i<len; i++) { |
| 194 | ch = text[i]; |
| 195 | |
| 196 | if (ch <= 0x7f) { |
| 197 | /* ASCII character */ |
| 198 | *out++ = (char)ch; |
| 199 | } |
| 200 | else if (0xdc80 <= ch && ch <= 0xdcff) { |
| 201 | /* UTF-8b surrogate */ |
| 202 | *out++ = (char)(ch - 0xdc00); |
| 203 | } |
| 204 | else { |
| 205 | if (error_pos != NULL) |
| 206 | *error_pos = i; |
| 207 | PyMem_Free(result); |
| 208 | return NULL; |
| 209 | } |
| 210 | } |
| 211 | *out = '\0'; |
| 212 | return result; |
| 213 | } |
| 214 | #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */ |
| 215 | |
| 216 | #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC)) |
| 217 | static wchar_t* |
| 218 | decode_ascii_surrogateescape(const char *arg, size_t *size) |
| 219 | { |
| 220 | wchar_t *res; |
| 221 | unsigned char *in; |
| 222 | wchar_t *out; |
| 223 | |
Victor Stinner | 65bf9cf | 2013-07-07 16:35:54 +0200 | [diff] [blame] | 224 | res = PyMem_RawMalloc((strlen(arg)+1)*sizeof(wchar_t)); |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 225 | if (!res) |
| 226 | return NULL; |
| 227 | |
| 228 | in = (unsigned char*)arg; |
| 229 | out = res; |
| 230 | while(*in) |
| 231 | if(*in < 128) |
| 232 | *out++ = *in++; |
| 233 | else |
| 234 | *out++ = 0xdc00 + *in++; |
| 235 | *out = 0; |
| 236 | if (size != NULL) |
| 237 | *size = out - res; |
| 238 | return res; |
| 239 | } |
| 240 | #endif |
| 241 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 242 | |
| 243 | /* Decode a byte string from the locale encoding with the |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 244 | surrogateescape error handler: undecodable bytes are decoded as characters |
| 245 | 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] | 246 | character, escape the bytes using the surrogateescape error handler instead |
| 247 | of decoding them. |
| 248 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 249 | Return a pointer to a newly allocated wide character string, use |
| 250 | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
| 251 | wide characters excluding the null character into *size |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 252 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 253 | Return NULL on decoding error or memory allocation error. If *size* is not |
| 254 | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
| 255 | decoding error. |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 256 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 257 | Decoding errors should never happen, unless there is a bug in the C |
| 258 | library. |
| 259 | |
| 260 | Use the Py_EncodeLocale() function to encode the character string back to a |
| 261 | byte string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 262 | wchar_t* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 263 | Py_DecodeLocale(const char* arg, size_t *size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 264 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 265 | #ifdef __APPLE__ |
| 266 | wchar_t *wstr; |
| 267 | wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 268 | if (size != NULL) { |
| 269 | if (wstr != NULL) |
| 270 | *size = wcslen(wstr); |
| 271 | else |
| 272 | *size = (size_t)-1; |
| 273 | } |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 274 | return wstr; |
| 275 | #else |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 276 | wchar_t *res; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 277 | size_t argsize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 278 | size_t count; |
Victor Stinner | 313f10c | 2013-05-07 23:48:56 +0200 | [diff] [blame] | 279 | #ifdef HAVE_MBRTOWC |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 280 | unsigned char *in; |
| 281 | wchar_t *out; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 282 | mbstate_t mbs; |
| 283 | #endif |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 284 | |
| 285 | #ifndef MS_WINDOWS |
| 286 | if (force_ascii == -1) |
| 287 | force_ascii = check_force_ascii(); |
| 288 | |
| 289 | if (force_ascii) { |
| 290 | /* force ASCII encoding to workaround mbstowcs() issue */ |
| 291 | res = decode_ascii_surrogateescape(arg, size); |
| 292 | if (res == NULL) |
| 293 | goto oom; |
| 294 | return res; |
| 295 | } |
| 296 | #endif |
| 297 | |
| 298 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 299 | /* Some platforms have a broken implementation of |
| 300 | * mbstowcs which does not count the characters that |
| 301 | * would result from conversion. Use an upper bound. |
| 302 | */ |
| 303 | argsize = strlen(arg); |
| 304 | #else |
| 305 | argsize = mbstowcs(NULL, arg, 0); |
| 306 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 307 | if (argsize != (size_t)-1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 308 | res = (wchar_t *)PyMem_RawMalloc((argsize+1)*sizeof(wchar_t)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 309 | if (!res) |
| 310 | goto oom; |
| 311 | count = mbstowcs(res, arg, argsize+1); |
| 312 | if (count != (size_t)-1) { |
| 313 | wchar_t *tmp; |
| 314 | /* Only use the result if it contains no |
| 315 | surrogate characters. */ |
| 316 | for (tmp = res; *tmp != 0 && |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 317 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 318 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 319 | if (*tmp == 0) { |
| 320 | if (size != NULL) |
| 321 | *size = count; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 322 | return res; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 323 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 324 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 325 | PyMem_RawFree(res); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 326 | } |
| 327 | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 328 | #ifdef HAVE_MBRTOWC |
| 329 | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 330 | |
| 331 | /* Overallocate; as multi-byte characters are in the argument, the |
| 332 | actual output could use less memory. */ |
| 333 | argsize = strlen(arg) + 1; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 334 | res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 335 | if (!res) |
| 336 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 337 | in = (unsigned char*)arg; |
| 338 | out = res; |
| 339 | memset(&mbs, 0, sizeof mbs); |
| 340 | while (argsize) { |
| 341 | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
| 342 | if (converted == 0) |
| 343 | /* Reached end of string; null char stored. */ |
| 344 | break; |
| 345 | if (converted == (size_t)-2) { |
| 346 | /* Incomplete character. This should never happen, |
| 347 | since we provide everything that we have - |
| 348 | unless there is a bug in the C library, or I |
| 349 | misunderstood how mbrtowc works. */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 350 | PyMem_RawFree(res); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 351 | if (size != NULL) |
| 352 | *size = (size_t)-2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
| 355 | if (converted == (size_t)-1) { |
| 356 | /* Conversion error. Escape as UTF-8b, and start over |
| 357 | in the initial shift state. */ |
| 358 | *out++ = 0xdc00 + *in++; |
| 359 | argsize--; |
| 360 | memset(&mbs, 0, sizeof mbs); |
| 361 | continue; |
| 362 | } |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 363 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 364 | /* Surrogate character. Escape the original |
| 365 | byte sequence with surrogateescape. */ |
| 366 | argsize -= converted; |
| 367 | while (converted--) |
| 368 | *out++ = 0xdc00 + *in++; |
| 369 | continue; |
| 370 | } |
| 371 | /* successfully converted some bytes */ |
| 372 | in += converted; |
| 373 | argsize -= converted; |
| 374 | out++; |
| 375 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 376 | if (size != NULL) |
| 377 | *size = out - res; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 378 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 379 | /* Cannot use C locale for escaping; manually escape as if charset |
| 380 | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 381 | correctly in the locale's charset, which must be an ASCII superset. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 382 | res = decode_ascii_surrogateescape(arg, size); |
| 383 | if (res == NULL) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 384 | goto oom; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 385 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 386 | return res; |
| 387 | oom: |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 388 | if (size != NULL) |
| 389 | *size = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 390 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 391 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 394 | /* Encode a wide character string to the locale encoding with the |
| 395 | surrogateescape error handler: surrogate characters in the range |
| 396 | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 397 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 398 | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
| 399 | the memory. Return NULL on encoding or memory allocation error. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 400 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 401 | If error_pos is not NULL, *error_pos is set to the index of the invalid |
| 402 | character on encoding error, or set to (size_t)-1 otherwise. |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 403 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 404 | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
| 405 | character string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 406 | char* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 407 | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 408 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 409 | #ifdef __APPLE__ |
| 410 | Py_ssize_t len; |
| 411 | PyObject *unicode, *bytes = NULL; |
| 412 | char *cpath; |
| 413 | |
| 414 | unicode = PyUnicode_FromWideChar(text, wcslen(text)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 415 | if (unicode == NULL) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 416 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 417 | |
| 418 | bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
| 419 | Py_DECREF(unicode); |
| 420 | if (bytes == NULL) { |
| 421 | PyErr_Clear(); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 422 | if (error_pos != NULL) |
| 423 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | len = PyBytes_GET_SIZE(bytes); |
| 428 | cpath = PyMem_Malloc(len+1); |
| 429 | if (cpath == NULL) { |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 430 | PyErr_Clear(); |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 431 | Py_DECREF(bytes); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 432 | if (error_pos != NULL) |
| 433 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | memcpy(cpath, PyBytes_AsString(bytes), len + 1); |
| 437 | Py_DECREF(bytes); |
| 438 | return cpath; |
| 439 | #else /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 440 | const size_t len = wcslen(text); |
| 441 | char *result = NULL, *bytes = NULL; |
| 442 | size_t i, size, converted; |
| 443 | wchar_t c, buf[2]; |
| 444 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 445 | #ifndef MS_WINDOWS |
| 446 | if (force_ascii == -1) |
| 447 | force_ascii = check_force_ascii(); |
| 448 | |
| 449 | if (force_ascii) |
| 450 | return encode_ascii_surrogateescape(text, error_pos); |
| 451 | #endif |
| 452 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 453 | /* The function works in two steps: |
| 454 | 1. compute the length of the output buffer in bytes (size) |
| 455 | 2. outputs the bytes */ |
| 456 | size = 0; |
| 457 | buf[1] = 0; |
| 458 | while (1) { |
| 459 | for (i=0; i < len; i++) { |
| 460 | c = text[i]; |
| 461 | if (c >= 0xdc80 && c <= 0xdcff) { |
| 462 | /* UTF-8b surrogate */ |
| 463 | if (bytes != NULL) { |
| 464 | *bytes++ = c - 0xdc00; |
| 465 | size--; |
| 466 | } |
| 467 | else |
| 468 | size++; |
| 469 | continue; |
| 470 | } |
| 471 | else { |
| 472 | buf[0] = c; |
| 473 | if (bytes != NULL) |
| 474 | converted = wcstombs(bytes, buf, size); |
| 475 | else |
| 476 | converted = wcstombs(NULL, buf, 0); |
| 477 | if (converted == (size_t)-1) { |
| 478 | if (result != NULL) |
| 479 | PyMem_Free(result); |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 480 | if (error_pos != NULL) |
| 481 | *error_pos = i; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | if (bytes != NULL) { |
| 485 | bytes += converted; |
| 486 | size -= converted; |
| 487 | } |
| 488 | else |
| 489 | size += converted; |
| 490 | } |
| 491 | } |
| 492 | if (result != NULL) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 493 | *bytes = '\0'; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | |
| 497 | size += 1; /* nul byte at the end */ |
| 498 | result = PyMem_Malloc(size); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 499 | if (result == NULL) { |
| 500 | if (error_pos != NULL) |
| 501 | *error_pos = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 502 | return NULL; |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 503 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 504 | bytes = result; |
| 505 | } |
| 506 | return result; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 507 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 510 | /* In principle, this should use HAVE__WSTAT, and _wstat |
| 511 | should be detected by autoconf. However, no current |
| 512 | POSIX system provides that function, so testing for |
| 513 | it is pointless. |
| 514 | Not sure whether the MS_WINDOWS guards are necessary: |
| 515 | perhaps for cygwin/mingw builds? |
| 516 | */ |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 517 | #if defined(HAVE_STAT) && !defined(MS_WINDOWS) |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 518 | |
| 519 | /* Get file status. Encode the path to the locale encoding. */ |
| 520 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 521 | int |
| 522 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 523 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 524 | int err; |
| 525 | char *fname; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 526 | fname = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 527 | if (fname == NULL) { |
| 528 | errno = EINVAL; |
| 529 | return -1; |
| 530 | } |
| 531 | err = stat(fname, buf); |
| 532 | PyMem_Free(fname); |
| 533 | return err; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 534 | } |
| 535 | #endif |
| 536 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 537 | #ifdef HAVE_STAT |
| 538 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 539 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 540 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 541 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 542 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 543 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 544 | |
| 545 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 546 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 547 | { |
| 548 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 549 | int err; |
| 550 | struct _stat wstatbuf; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 551 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 552 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 553 | wpath = PyUnicode_AsUnicode(path); |
| 554 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 555 | return -2; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 556 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 557 | if (!err) |
| 558 | statbuf->st_mode = wstatbuf.st_mode; |
| 559 | return err; |
| 560 | #else |
| 561 | int ret; |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 562 | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 563 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 564 | return -2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 565 | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
| 566 | Py_DECREF(bytes); |
| 567 | return ret; |
| 568 | #endif |
| 569 | } |
| 570 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 571 | #endif |
| 572 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 573 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 574 | get_inheritable(int fd, int raise) |
| 575 | { |
| 576 | #ifdef MS_WINDOWS |
| 577 | HANDLE handle; |
| 578 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 579 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 580 | if (!_PyVerify_fd(fd)) { |
| 581 | if (raise) |
| 582 | PyErr_SetFromErrno(PyExc_OSError); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | handle = (HANDLE)_get_osfhandle(fd); |
| 587 | if (handle == INVALID_HANDLE_VALUE) { |
| 588 | if (raise) |
| 589 | PyErr_SetFromWindowsErr(0); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | if (!GetHandleInformation(handle, &flags)) { |
| 594 | if (raise) |
| 595 | PyErr_SetFromWindowsErr(0); |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | return (flags & HANDLE_FLAG_INHERIT); |
| 600 | #else |
| 601 | int flags; |
| 602 | |
| 603 | flags = fcntl(fd, F_GETFD, 0); |
| 604 | if (flags == -1) { |
| 605 | if (raise) |
| 606 | PyErr_SetFromErrno(PyExc_OSError); |
| 607 | return -1; |
| 608 | } |
| 609 | return !(flags & FD_CLOEXEC); |
| 610 | #endif |
| 611 | } |
| 612 | |
| 613 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 614 | 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] | 615 | raise an exception and return -1 on error. */ |
| 616 | int |
| 617 | _Py_get_inheritable(int fd) |
| 618 | { |
| 619 | return get_inheritable(fd, 1); |
| 620 | } |
| 621 | |
| 622 | static int |
| 623 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 624 | { |
| 625 | #ifdef MS_WINDOWS |
| 626 | HANDLE handle; |
| 627 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 628 | #else |
| 629 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 630 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 631 | int request; |
| 632 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 633 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 634 | int flags; |
| 635 | int res; |
| 636 | #endif |
| 637 | |
| 638 | /* atomic_flag_works can only be used to make the file descriptor |
| 639 | non-inheritable */ |
| 640 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 641 | |
| 642 | if (atomic_flag_works != NULL && !inheritable) { |
| 643 | if (*atomic_flag_works == -1) { |
| 644 | int inheritable = get_inheritable(fd, raise); |
| 645 | if (inheritable == -1) |
| 646 | return -1; |
| 647 | *atomic_flag_works = !inheritable; |
| 648 | } |
| 649 | |
| 650 | if (*atomic_flag_works) |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | #ifdef MS_WINDOWS |
| 655 | if (!_PyVerify_fd(fd)) { |
| 656 | if (raise) |
| 657 | PyErr_SetFromErrno(PyExc_OSError); |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | handle = (HANDLE)_get_osfhandle(fd); |
| 662 | if (handle == INVALID_HANDLE_VALUE) { |
| 663 | if (raise) |
| 664 | PyErr_SetFromWindowsErr(0); |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | if (inheritable) |
| 669 | flags = HANDLE_FLAG_INHERIT; |
| 670 | else |
| 671 | flags = 0; |
| 672 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 673 | if (raise) |
| 674 | PyErr_SetFromWindowsErr(0); |
| 675 | return -1; |
| 676 | } |
| 677 | return 0; |
| 678 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 679 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 680 | |
| 681 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 682 | if (ioctl_works != 0) { |
| 683 | /* fast-path: ioctl() only requires one syscall */ |
| 684 | if (inheritable) |
| 685 | request = FIONCLEX; |
| 686 | else |
| 687 | request = FIOCLEX; |
| 688 | err = ioctl(fd, request, NULL); |
| 689 | if (!err) { |
| 690 | ioctl_works = 1; |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | if (errno != ENOTTY) { |
| 695 | if (raise) |
| 696 | PyErr_SetFromErrno(PyExc_OSError); |
| 697 | return -1; |
| 698 | } |
| 699 | else { |
| 700 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 701 | device". The ioctl is declared but not supported by the kernel. |
| 702 | Remember that ioctl() doesn't work. It is the case on |
| 703 | Illumos-based OS for example. */ |
| 704 | ioctl_works = 0; |
| 705 | } |
| 706 | /* fallback to fcntl() if ioctl() does not work */ |
| 707 | } |
| 708 | #endif |
| 709 | |
| 710 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 711 | flags = fcntl(fd, F_GETFD); |
| 712 | if (flags < 0) { |
| 713 | if (raise) |
| 714 | PyErr_SetFromErrno(PyExc_OSError); |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | if (inheritable) |
| 719 | flags &= ~FD_CLOEXEC; |
| 720 | else |
| 721 | flags |= FD_CLOEXEC; |
| 722 | res = fcntl(fd, F_SETFD, flags); |
| 723 | if (res < 0) { |
| 724 | if (raise) |
| 725 | PyErr_SetFromErrno(PyExc_OSError); |
| 726 | return -1; |
| 727 | } |
| 728 | return 0; |
| 729 | #endif |
| 730 | } |
| 731 | |
| 732 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 733 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 734 | static int |
| 735 | make_non_inheritable(int fd) |
| 736 | { |
| 737 | return set_inheritable(fd, 0, 0, NULL); |
| 738 | } |
| 739 | |
| 740 | /* Set the inheritable flag of the specified file descriptor. |
| 741 | On success: return 0, on error: raise an exception if raise is nonzero |
| 742 | and return -1. |
| 743 | |
| 744 | If atomic_flag_works is not NULL: |
| 745 | |
| 746 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 747 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 748 | set the inheritable flag |
| 749 | * if *atomic_flag_works==1: do nothing |
| 750 | * if *atomic_flag_works==0: set inheritable flag to False |
| 751 | |
| 752 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 753 | file descriptor. |
| 754 | |
| 755 | atomic_flag_works can only be used to make a file descriptor |
| 756 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 757 | int |
| 758 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 759 | { |
| 760 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 761 | } |
| 762 | |
| 763 | /* Open a file with the specified flags (wrapper to open() function). |
| 764 | The file descriptor is created non-inheritable. */ |
| 765 | int |
| 766 | _Py_open(const char *pathname, int flags) |
| 767 | { |
| 768 | int fd; |
| 769 | #ifdef MS_WINDOWS |
| 770 | fd = open(pathname, flags | O_NOINHERIT); |
| 771 | if (fd < 0) |
| 772 | return fd; |
| 773 | #else |
| 774 | |
| 775 | int *atomic_flag_works; |
| 776 | #ifdef O_CLOEXEC |
| 777 | atomic_flag_works = &_Py_open_cloexec_works; |
| 778 | flags |= O_CLOEXEC; |
| 779 | #else |
| 780 | atomic_flag_works = NULL; |
| 781 | #endif |
| 782 | fd = open(pathname, flags); |
| 783 | if (fd < 0) |
| 784 | return fd; |
| 785 | |
| 786 | if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) { |
| 787 | close(fd); |
| 788 | return -1; |
| 789 | } |
| 790 | #endif /* !MS_WINDOWS */ |
| 791 | return fd; |
| 792 | } |
| 793 | |
| 794 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
| 795 | encoding and use fopen() otherwise. The file descriptor is created |
| 796 | non-inheritable. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 797 | FILE * |
| 798 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 799 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 800 | FILE *f; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 801 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 802 | char *cpath; |
| 803 | char cmode[10]; |
| 804 | size_t r; |
| 805 | r = wcstombs(cmode, mode, 10); |
| 806 | if (r == (size_t)-1 || r >= 10) { |
| 807 | errno = EINVAL; |
| 808 | return NULL; |
| 809 | } |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 810 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 811 | if (cpath == NULL) |
| 812 | return NULL; |
| 813 | f = fopen(cpath, cmode); |
| 814 | PyMem_Free(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 815 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 816 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 817 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 818 | if (f == NULL) |
| 819 | return NULL; |
| 820 | if (make_non_inheritable(fileno(f)) < 0) { |
| 821 | fclose(f); |
| 822 | return NULL; |
| 823 | } |
| 824 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 827 | /* Wrapper to fopen(). The file descriptor is created non-inheritable. */ |
| 828 | FILE* |
| 829 | _Py_fopen(const char *pathname, const char *mode) |
| 830 | { |
| 831 | FILE *f = fopen(pathname, mode); |
| 832 | if (f == NULL) |
| 833 | return NULL; |
| 834 | if (make_non_inheritable(fileno(f)) < 0) { |
| 835 | fclose(f); |
| 836 | return NULL; |
| 837 | } |
| 838 | return f; |
| 839 | } |
| 840 | |
| 841 | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
| 842 | encoding and call fopen() otherwise. The file descriptor is created |
| 843 | non-inheritable. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 844 | |
| 845 | Return the new file object on success, or NULL if the file cannot be open or |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 846 | (if PyErr_Occurred()) on unicode error. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 847 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 848 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 849 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 850 | FILE *f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 851 | #ifdef MS_WINDOWS |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 852 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 853 | wchar_t wmode[10]; |
| 854 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 856 | if (!PyUnicode_Check(path)) { |
| 857 | PyErr_Format(PyExc_TypeError, |
| 858 | "str file path expected under Windows, got %R", |
| 859 | Py_TYPE(path)); |
| 860 | return NULL; |
| 861 | } |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 862 | wpath = PyUnicode_AsUnicode(path); |
| 863 | if (wpath == NULL) |
| 864 | return NULL; |
| 865 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 866 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
| 867 | if (usize == 0) |
| 868 | return NULL; |
| 869 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 870 | f = _wfopen(wpath, wmode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 871 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 872 | PyObject *bytes; |
| 873 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 874 | return NULL; |
| 875 | f = fopen(PyBytes_AS_STRING(bytes), mode); |
| 876 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 877 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 878 | if (f == NULL) |
| 879 | return NULL; |
| 880 | if (make_non_inheritable(fileno(f)) < 0) { |
| 881 | fclose(f); |
| 882 | return NULL; |
| 883 | } |
| 884 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 888 | |
| 889 | /* 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] | 890 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 891 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 892 | int |
| 893 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 894 | { |
| 895 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 896 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 897 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 898 | int res; |
| 899 | size_t r1; |
| 900 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 901 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 902 | if (cpath == NULL) { |
| 903 | errno = EINVAL; |
| 904 | return -1; |
| 905 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 906 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 907 | PyMem_Free(cpath); |
| 908 | if (res == -1) |
| 909 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 910 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 911 | errno = EINVAL; |
| 912 | return -1; |
| 913 | } |
| 914 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 915 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 916 | if (wbuf == NULL) { |
| 917 | errno = EINVAL; |
| 918 | return -1; |
| 919 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 920 | if (bufsiz <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 921 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 922 | errno = EINVAL; |
| 923 | return -1; |
| 924 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 925 | wcsncpy(buf, wbuf, bufsiz); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 926 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 927 | return (int)r1; |
| 928 | } |
| 929 | #endif |
| 930 | |
| 931 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 932 | |
| 933 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 934 | encoding, decode the result from the locale encoding. |
| 935 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 936 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 937 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 938 | _Py_wrealpath(const wchar_t *path, |
| 939 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 940 | { |
| 941 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 942 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 943 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 944 | char *res; |
| 945 | size_t r; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 946 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 947 | if (cpath == NULL) { |
| 948 | errno = EINVAL; |
| 949 | return NULL; |
| 950 | } |
| 951 | res = realpath(cpath, cresolved_path); |
| 952 | PyMem_Free(cpath); |
| 953 | if (res == NULL) |
| 954 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 955 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 956 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 957 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 958 | errno = EINVAL; |
| 959 | return NULL; |
| 960 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 961 | if (resolved_path_size <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 962 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 963 | errno = EINVAL; |
| 964 | return NULL; |
| 965 | } |
| 966 | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 967 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 968 | return resolved_path; |
| 969 | } |
| 970 | #endif |
| 971 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 972 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 973 | including the null character. Decode the path from the locale encoding. |
| 974 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 975 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 976 | wchar_t* |
| 977 | _Py_wgetcwd(wchar_t *buf, size_t size) |
| 978 | { |
| 979 | #ifdef MS_WINDOWS |
Victor Stinner | 56785ea | 2013-06-05 00:46:29 +0200 | [diff] [blame] | 980 | int isize = (int)Py_MIN(size, INT_MAX); |
| 981 | return _wgetcwd(buf, isize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 982 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 983 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 984 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 985 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 986 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 987 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 988 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 989 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 990 | if (wname == NULL) |
| 991 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 992 | if (size <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 993 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 994 | return NULL; |
| 995 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 996 | wcsncpy(buf, wname, size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 997 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 998 | return buf; |
| 999 | #endif |
| 1000 | } |
| 1001 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1002 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1003 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1004 | exception and return -1 on error. |
| 1005 | |
| 1006 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1007 | int |
| 1008 | _Py_dup(int fd) |
| 1009 | { |
| 1010 | #ifdef MS_WINDOWS |
| 1011 | HANDLE handle; |
| 1012 | DWORD ftype; |
| 1013 | #endif |
| 1014 | |
| 1015 | if (!_PyVerify_fd(fd)) { |
| 1016 | PyErr_SetFromErrno(PyExc_OSError); |
| 1017 | return -1; |
| 1018 | } |
| 1019 | |
| 1020 | #ifdef MS_WINDOWS |
| 1021 | handle = (HANDLE)_get_osfhandle(fd); |
| 1022 | if (handle == INVALID_HANDLE_VALUE) { |
| 1023 | PyErr_SetFromWindowsErr(0); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
| 1027 | /* get the file type, ignore the error if it failed */ |
| 1028 | ftype = GetFileType(handle); |
| 1029 | |
| 1030 | Py_BEGIN_ALLOW_THREADS |
| 1031 | fd = dup(fd); |
| 1032 | Py_END_ALLOW_THREADS |
| 1033 | if (fd < 0) { |
| 1034 | PyErr_SetFromErrno(PyExc_OSError); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | |
| 1038 | /* Character files like console cannot be make non-inheritable */ |
| 1039 | if (ftype != FILE_TYPE_CHAR) { |
| 1040 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1041 | close(fd); |
| 1042 | return -1; |
| 1043 | } |
| 1044 | } |
| 1045 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1046 | Py_BEGIN_ALLOW_THREADS |
| 1047 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 1048 | Py_END_ALLOW_THREADS |
| 1049 | if (fd < 0) { |
| 1050 | PyErr_SetFromErrno(PyExc_OSError); |
| 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | #else |
| 1055 | Py_BEGIN_ALLOW_THREADS |
| 1056 | fd = dup(fd); |
| 1057 | Py_END_ALLOW_THREADS |
| 1058 | if (fd < 0) { |
| 1059 | PyErr_SetFromErrno(PyExc_OSError); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1064 | close(fd); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | #endif |
| 1068 | return fd; |
| 1069 | } |
| 1070 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1071 | #ifndef MS_WINDOWS |
| 1072 | /* Get the blocking mode of the file descriptor. |
| 1073 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1074 | raise an exception and return -1 on error. */ |
| 1075 | int |
| 1076 | _Py_get_blocking(int fd) |
| 1077 | { |
| 1078 | int flags = fcntl(fd, F_GETFL, 0); |
| 1079 | if (flags < 0) { |
| 1080 | PyErr_SetFromErrno(PyExc_OSError); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | |
| 1084 | return !(flags & O_NONBLOCK); |
| 1085 | } |
| 1086 | |
| 1087 | /* Set the blocking mode of the specified file descriptor. |
| 1088 | |
| 1089 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1090 | otherwise. |
| 1091 | |
| 1092 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1093 | int |
| 1094 | _Py_set_blocking(int fd, int blocking) |
| 1095 | { |
| 1096 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1097 | int arg = !blocking; |
| 1098 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1099 | goto error; |
| 1100 | #else |
| 1101 | int flags, res; |
| 1102 | |
| 1103 | flags = fcntl(fd, F_GETFL, 0); |
| 1104 | if (flags < 0) |
| 1105 | goto error; |
| 1106 | |
| 1107 | if (blocking) |
| 1108 | flags = flags & (~O_NONBLOCK); |
| 1109 | else |
| 1110 | flags = flags | O_NONBLOCK; |
| 1111 | |
| 1112 | res = fcntl(fd, F_SETFL, flags); |
| 1113 | if (res < 0) |
| 1114 | goto error; |
| 1115 | #endif |
| 1116 | return 0; |
| 1117 | |
| 1118 | error: |
| 1119 | PyErr_SetFromErrno(PyExc_OSError); |
| 1120 | return -1; |
| 1121 | } |
| 1122 | #endif |
| 1123 | |