blob: eeb5f2e89d251c6f0f91d70185553c0853d77029 [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01002#include "osdefs.h"
Stefan Krah6c01e382014-01-20 15:31:08 +01003#include <locale.h>
4
Victor Stinnerb306d752010-10-07 22:09:40 +00005#ifdef MS_WINDOWS
Steve Dowerd81431f2015-03-06 14:47:02 -08006# include <malloc.h>
Victor Stinnerb306d752010-10-07 22:09:40 +00007# include <windows.h>
Steve Dower8fc89802015-04-12 00:26:27 -04008extern int winerror_to_errno(int);
Victor Stinnerb306d752010-10-07 22:09:40 +00009#endif
Victor Stinner4e314432010-10-07 21:45:39 +000010
Brett Cannonefb00c02012-02-29 18:31:31 -050011#ifdef HAVE_LANGINFO_H
12#include <langinfo.h>
13#endif
14
Victor Stinnerdaf45552013-08-28 00:53:59 +020015#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 Stinner91106cd2017-12-13 12:29:09 +010023extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size,
24 size_t *p_wlen);
Victor Stinnere47e6982017-12-21 15:45:16 +010025extern char* _Py_EncodeUTF8_surrogateescape(const wchar_t *text,
26 size_t *error_pos);
Victor Stinnere2623772012-11-12 23:04:02 +010027
Victor Stinnerdaf45552013-08-28 00:53:59 +020028#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020029/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020030
31 -1: unknown
32 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
33 1: open() supports O_CLOEXEC flag, close-on-exec is set
34
Victor Stinnera555cfc2015-03-18 00:22:14 +010035 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
36 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020037int _Py_open_cloexec_works = -1;
38#endif
39
Brett Cannonefb00c02012-02-29 18:31:31 -050040PyObject *
41_Py_device_encoding(int fd)
42{
Victor Stinner14b9b112013-06-25 00:37:25 +020043#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050044 UINT cp;
45#endif
Steve Dower8fc89802015-04-12 00:26:27 -040046 int valid;
47 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -070048 valid = isatty(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040049 _Py_END_SUPPRESS_IPH
50 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050051 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040052
Victor Stinner14b9b112013-06-25 00:37:25 +020053#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050054 if (fd == 0)
55 cp = GetConsoleCP();
56 else if (fd == 1 || fd == 2)
57 cp = GetConsoleOutputCP();
58 else
59 cp = 0;
60 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
61 has no console */
62 if (cp != 0)
63 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
64#elif defined(CODESET)
65 {
66 char *codeset = nl_langinfo(CODESET);
67 if (codeset != NULL && codeset[0] != 0)
68 return PyUnicode_FromString(codeset);
69 }
70#endif
71 Py_RETURN_NONE;
72}
73
Victor Stinnerd45c7f82012-12-04 01:34:47 +010074#if !defined(__APPLE__) && !defined(MS_WINDOWS)
75extern int _Py_normalize_encoding(const char *, char *, size_t);
76
77/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
78 On these operating systems, nl_langinfo(CODESET) announces an alias of the
79 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
80 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
81 locale.getpreferredencoding() codec. For example, if command line arguments
82 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
83 UnicodeEncodeError instead of retrieving the original byte string.
84
85 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
86 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
87 one byte in range 0x80-0xff can be decoded from the locale encoding. The
88 workaround is also enabled on error, for example if getting the locale
89 failed.
90
Philip Jenvey215c49a2013-01-15 13:24:12 -080091 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010092
Victor Stinnerf6a271a2014-08-01 12:28:48 +020093 1: the workaround is used: Py_EncodeLocale() uses
94 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010095 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020096 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
97 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010098 -1: unknown, need to call check_force_ascii() to get the value
99*/
100static int force_ascii = -1;
101
102static int
103check_force_ascii(void)
104{
105 char *loc;
106#if defined(HAVE_LANGINFO_H) && defined(CODESET)
107 char *codeset, **alias;
Victor Stinner54de2b12016-09-09 23:11:52 -0700108 char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100109 int is_ascii;
110 unsigned int i;
111 char* ascii_aliases[] = {
112 "ascii",
Victor Stinner54de2b12016-09-09 23:11:52 -0700113 /* Aliases from Lib/encodings/aliases.py */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100114 "646",
Victor Stinner54de2b12016-09-09 23:11:52 -0700115 "ansi_x3.4_1968",
116 "ansi_x3.4_1986",
117 "ansi_x3_4_1968",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100118 "cp367",
119 "csascii",
120 "ibm367",
Victor Stinner54de2b12016-09-09 23:11:52 -0700121 "iso646_us",
122 "iso_646.irv_1991",
123 "iso_ir_6",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100124 "us",
Victor Stinner54de2b12016-09-09 23:11:52 -0700125 "us_ascii",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100126 NULL
127 };
128#endif
129
130 loc = setlocale(LC_CTYPE, NULL);
131 if (loc == NULL)
132 goto error;
133 if (strcmp(loc, "C") != 0) {
134 /* the LC_CTYPE locale is different than C */
135 return 0;
136 }
137
138#if defined(HAVE_LANGINFO_H) && defined(CODESET)
139 codeset = nl_langinfo(CODESET);
140 if (!codeset || codeset[0] == '\0') {
141 /* CODESET is not set or empty */
142 goto error;
143 }
144 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
145 goto error;
146
147 is_ascii = 0;
148 for (alias=ascii_aliases; *alias != NULL; alias++) {
149 if (strcmp(encoding, *alias) == 0) {
150 is_ascii = 1;
151 break;
152 }
153 }
154 if (!is_ascii) {
155 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
156 return 0;
157 }
158
159 for (i=0x80; i<0xff; i++) {
160 unsigned char ch;
161 wchar_t wch;
162 size_t res;
163
164 ch = (unsigned char)i;
165 res = mbstowcs(&wch, (char*)&ch, 1);
166 if (res != (size_t)-1) {
167 /* decoding a non-ASCII character from the locale encoding succeed:
168 the locale encoding is not ASCII, force ASCII */
169 return 1;
170 }
171 }
172 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
173 encoding: the locale encoding is really ASCII */
174 return 0;
175#else
176 /* nl_langinfo(CODESET) is not available: always force ASCII */
177 return 1;
178#endif
179
180error:
Martin Panter46f50722016-05-26 05:35:26 +0000181 /* if an error occurred, force the ASCII encoding */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100182 return 1;
183}
184
185static char*
186encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
187{
188 char *result = NULL, *out;
189 size_t len, i;
190 wchar_t ch;
191
192 if (error_pos != NULL)
193 *error_pos = (size_t)-1;
194
195 len = wcslen(text);
196
197 result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
198 if (result == NULL)
199 return NULL;
200
201 out = result;
202 for (i=0; i<len; i++) {
203 ch = text[i];
204
205 if (ch <= 0x7f) {
206 /* ASCII character */
207 *out++ = (char)ch;
208 }
209 else if (0xdc80 <= ch && ch <= 0xdcff) {
210 /* UTF-8b surrogate */
211 *out++ = (char)(ch - 0xdc00);
212 }
213 else {
214 if (error_pos != NULL)
215 *error_pos = i;
216 PyMem_Free(result);
217 return NULL;
218 }
219 }
220 *out = '\0';
221 return result;
222}
223#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
224
225#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
226static wchar_t*
227decode_ascii_surrogateescape(const char *arg, size_t *size)
228{
229 wchar_t *res;
230 unsigned char *in;
231 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600232 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100233
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600234 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
235 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600236 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100237 if (!res)
238 return NULL;
239
240 in = (unsigned char*)arg;
241 out = res;
242 while(*in)
243 if(*in < 128)
244 *out++ = *in++;
245 else
246 *out++ = 0xdc00 + *in++;
247 *out = 0;
248 if (size != NULL)
249 *size = out - res;
250 return res;
251}
252#endif
253
Victor Stinnerd2b02312017-12-15 23:06:17 +0100254#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100255static wchar_t*
256decode_locale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000257{
258 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100259 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000260 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200261#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000262 unsigned char *in;
263 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000264 mbstate_t mbs;
265#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100266
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100267#ifdef HAVE_BROKEN_MBSTOWCS
268 /* Some platforms have a broken implementation of
269 * mbstowcs which does not count the characters that
270 * would result from conversion. Use an upper bound.
271 */
272 argsize = strlen(arg);
273#else
274 argsize = mbstowcs(NULL, arg, 0);
275#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000276 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600277 if (argsize == PY_SSIZE_T_MAX)
278 goto oom;
279 argsize += 1;
280 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
281 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600282 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000283 if (!res)
284 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600285 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000286 if (count != (size_t)-1) {
287 wchar_t *tmp;
288 /* Only use the result if it contains no
289 surrogate characters. */
290 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100291 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000292 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000293 if (*tmp == 0) {
294 if (size != NULL)
295 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000296 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000297 }
Victor Stinner4e314432010-10-07 21:45:39 +0000298 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200299 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000300 }
301 /* Conversion failed. Fall back to escaping with surrogateescape. */
302#ifdef HAVE_MBRTOWC
303 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
304
305 /* Overallocate; as multi-byte characters are in the argument, the
306 actual output could use less memory. */
307 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600308 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
309 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200310 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000311 if (!res)
312 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000313 in = (unsigned char*)arg;
314 out = res;
315 memset(&mbs, 0, sizeof mbs);
316 while (argsize) {
317 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
318 if (converted == 0)
319 /* Reached end of string; null char stored. */
320 break;
321 if (converted == (size_t)-2) {
322 /* Incomplete character. This should never happen,
323 since we provide everything that we have -
324 unless there is a bug in the C library, or I
325 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200326 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100327 if (size != NULL)
328 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000329 return NULL;
330 }
331 if (converted == (size_t)-1) {
332 /* Conversion error. Escape as UTF-8b, and start over
333 in the initial shift state. */
334 *out++ = 0xdc00 + *in++;
335 argsize--;
336 memset(&mbs, 0, sizeof mbs);
337 continue;
338 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100339 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000340 /* Surrogate character. Escape the original
341 byte sequence with surrogateescape. */
342 argsize -= converted;
343 while (converted--)
344 *out++ = 0xdc00 + *in++;
345 continue;
346 }
347 /* successfully converted some bytes */
348 in += converted;
349 argsize -= converted;
350 out++;
351 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100352 if (size != NULL)
353 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100354#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000355 /* Cannot use C locale for escaping; manually escape as if charset
356 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
357 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100358 res = decode_ascii_surrogateescape(arg, size);
359 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100360 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100361#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000362 return res;
Victor Stinner91106cd2017-12-13 12:29:09 +0100363
Victor Stinner4e314432010-10-07 21:45:39 +0000364oom:
Victor Stinner91106cd2017-12-13 12:29:09 +0100365 if (size != NULL) {
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100366 *size = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100367 }
Victor Stinner4e314432010-10-07 21:45:39 +0000368 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100369}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100370#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100371
372
373/* Decode a byte string from the locale encoding with the
374 surrogateescape error handler: undecodable bytes are decoded as characters
375 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
376 character, escape the bytes using the surrogateescape error handler instead
377 of decoding them.
378
379 Return a pointer to a newly allocated wide character string, use
380 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
381 wide characters excluding the null character into *size
382
383 Return NULL on decoding error or memory allocation error. If *size* is not
384 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
385 decoding error.
386
387 Decoding errors should never happen, unless there is a bug in the C
388 library.
389
390 Use the Py_EncodeLocale() function to encode the character string back to a
391 byte string. */
392wchar_t*
393Py_DecodeLocale(const char* arg, size_t *size)
394{
395#if defined(__APPLE__) || defined(__ANDROID__)
396 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
397#else
Victor Stinner94540602017-12-16 04:54:22 +0100398 if (Py_UTF8Mode == 1) {
Victor Stinner91106cd2017-12-13 12:29:09 +0100399 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
400 }
401
402#ifndef MS_WINDOWS
403 if (force_ascii == -1)
404 force_ascii = check_force_ascii();
405
406 if (force_ascii) {
407 /* force ASCII encoding to workaround mbstowcs() issue */
408 wchar_t *wstr = decode_ascii_surrogateescape(arg, size);
409 if (wstr == NULL) {
410 if (size != NULL) {
411 *size = (size_t)-1;
412 }
413 return NULL;
414 }
415 return wstr;
416 }
417#endif
418
419 return decode_locale(arg, size);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100420#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000421}
422
Victor Stinner91106cd2017-12-13 12:29:09 +0100423
Victor Stinnerd2b02312017-12-15 23:06:17 +0100424#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100425static char*
426encode_locale(const wchar_t *text, size_t *error_pos)
427{
Victor Stinner4e314432010-10-07 21:45:39 +0000428 const size_t len = wcslen(text);
429 char *result = NULL, *bytes = NULL;
430 size_t i, size, converted;
431 wchar_t c, buf[2];
432
433 /* The function works in two steps:
434 1. compute the length of the output buffer in bytes (size)
435 2. outputs the bytes */
436 size = 0;
437 buf[1] = 0;
438 while (1) {
439 for (i=0; i < len; i++) {
440 c = text[i];
441 if (c >= 0xdc80 && c <= 0xdcff) {
442 /* UTF-8b surrogate */
443 if (bytes != NULL) {
444 *bytes++ = c - 0xdc00;
445 size--;
446 }
447 else
448 size++;
449 continue;
450 }
451 else {
452 buf[0] = c;
453 if (bytes != NULL)
454 converted = wcstombs(bytes, buf, size);
455 else
456 converted = wcstombs(NULL, buf, 0);
457 if (converted == (size_t)-1) {
458 if (result != NULL)
459 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000460 if (error_pos != NULL)
461 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000462 return NULL;
463 }
464 if (bytes != NULL) {
465 bytes += converted;
466 size -= converted;
467 }
468 else
469 size += converted;
470 }
471 }
472 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100473 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000474 break;
475 }
476
477 size += 1; /* nul byte at the end */
478 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100479 if (result == NULL) {
480 if (error_pos != NULL)
481 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000482 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100483 }
Victor Stinner4e314432010-10-07 21:45:39 +0000484 bytes = result;
485 }
486 return result;
Victor Stinner91106cd2017-12-13 12:29:09 +0100487}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100488#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100489
490/* Encode a wide character string to the locale encoding with the
491 surrogateescape error handler: surrogate characters in the range
492 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
493
494 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
495 the memory. Return NULL on encoding or memory allocation error.
496
497 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
498 to the index of the invalid character on encoding error.
499
500 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
501 character string. */
502char*
503Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
504{
505#if defined(__APPLE__) || defined(__ANDROID__)
Victor Stinnere47e6982017-12-21 15:45:16 +0100506 return _Py_EncodeUTF8_surrogateescape(text, error_pos);
Victor Stinner91106cd2017-12-13 12:29:09 +0100507#else /* __APPLE__ */
Victor Stinner94540602017-12-16 04:54:22 +0100508 if (Py_UTF8Mode == 1) {
Victor Stinnere47e6982017-12-21 15:45:16 +0100509 return _Py_EncodeUTF8_surrogateescape(text, error_pos);
Victor Stinner91106cd2017-12-13 12:29:09 +0100510 }
511
512#ifndef MS_WINDOWS
513 if (force_ascii == -1)
514 force_ascii = check_force_ascii();
515
516 if (force_ascii)
517 return encode_ascii_surrogateescape(text, error_pos);
518#endif
519
520 return encode_locale(text, error_pos);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100521#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000522}
523
Victor Stinner6672d0c2010-10-07 22:53:43 +0000524
Steve Dowerf2f373f2015-02-21 08:44:05 -0800525#ifdef MS_WINDOWS
526static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
527
528static void
529FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
530{
531 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
532 /* Cannot simply cast and dereference in_ptr,
533 since it might not be aligned properly */
534 __int64 in;
535 memcpy(&in, in_ptr, sizeof(in));
536 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
537 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
538}
539
540void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800541_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800542{
543 /* XXX endianness */
544 __int64 out;
545 out = time_in + secs_between_epochs;
546 out = out * 10000000 + nsec_in / 100;
547 memcpy(out_ptr, &out, sizeof(out));
548}
549
550/* Below, we *know* that ugo+r is 0444 */
551#if _S_IREAD != 0400
552#error Unsupported C library
553#endif
554static int
555attributes_to_mode(DWORD attr)
556{
557 int m = 0;
558 if (attr & FILE_ATTRIBUTE_DIRECTORY)
559 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
560 else
561 m |= _S_IFREG;
562 if (attr & FILE_ATTRIBUTE_READONLY)
563 m |= 0444;
564 else
565 m |= 0666;
566 return m;
567}
568
Steve Dowerbf1f3762015-02-21 15:26:02 -0800569void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200570_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
571 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800572{
573 memset(result, 0, sizeof(*result));
574 result->st_mode = attributes_to_mode(info->dwFileAttributes);
575 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
576 result->st_dev = info->dwVolumeSerialNumber;
577 result->st_rdev = result->st_dev;
578 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
579 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
580 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
581 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100582 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800583 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
584 /* first clear the S_IFMT bits */
585 result->st_mode ^= (result->st_mode & S_IFMT);
586 /* now set the bits that make this a symlink */
587 result->st_mode |= S_IFLNK;
588 }
589 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800590}
591#endif
592
593/* Return information about a file.
594
595 On POSIX, use fstat().
596
597 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800598 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
599 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800600 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200601
602 On Windows, set the last Windows error and return nonzero on error. On
603 POSIX, set errno and return nonzero on error. Fill status and return 0 on
604 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800605int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200606_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800607{
608#ifdef MS_WINDOWS
609 BY_HANDLE_FILE_INFORMATION info;
610 HANDLE h;
611 int type;
612
Steve Dower940f33a2016-09-08 11:21:54 -0700613 _Py_BEGIN_SUPPRESS_IPH
614 h = (HANDLE)_get_osfhandle(fd);
615 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800616
617 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400618 /* errno is already set by _get_osfhandle, but we also set
619 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800620 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800621 return -1;
622 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200623 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800624
625 type = GetFileType(h);
626 if (type == FILE_TYPE_UNKNOWN) {
627 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400628 if (error != 0) {
629 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800630 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400631 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800632 /* else: valid but unknown file */
633 }
634
635 if (type != FILE_TYPE_DISK) {
636 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200637 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800638 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200639 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800640 return 0;
641 }
642
643 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400644 /* The Win32 error is already set, but we also set errno for
645 callers who expect it */
646 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800647 return -1;
648 }
649
Victor Stinnere134a7f2015-03-30 10:09:31 +0200650 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800651 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100652 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800653 return 0;
654#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200655 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800656#endif
657}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800658
Victor Stinnere134a7f2015-03-30 10:09:31 +0200659/* Return information about a file.
660
661 On POSIX, use fstat().
662
663 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800664 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
665 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200666 #23152.
667
668 Raise an exception and return -1 on error. On Windows, set the last Windows
669 error on error. On POSIX, set errno on error. Fill status and return 0 on
670 success.
671
Victor Stinner6f4fae82015-04-01 18:34:32 +0200672 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
673 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200674int
675_Py_fstat(int fd, struct _Py_stat_struct *status)
676{
677 int res;
678
Victor Stinner8a1be612016-03-14 22:07:55 +0100679 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100680
Victor Stinnere134a7f2015-03-30 10:09:31 +0200681 Py_BEGIN_ALLOW_THREADS
682 res = _Py_fstat_noraise(fd, status);
683 Py_END_ALLOW_THREADS
684
685 if (res != 0) {
686#ifdef MS_WINDOWS
687 PyErr_SetFromWindowsErr(0);
688#else
689 PyErr_SetFromErrno(PyExc_OSError);
690#endif
691 return -1;
692 }
693 return 0;
694}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800695
Victor Stinner6672d0c2010-10-07 22:53:43 +0000696/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
697 call stat() otherwise. Only fill st_mode attribute on Windows.
698
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100699 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
700 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000701
702int
Victor Stinnera4a75952010-10-07 22:23:10 +0000703_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000704{
705#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000706 int err;
707 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300708 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000709
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300710 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100711 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100712 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300713
Victor Stinneree587ea2011-11-17 00:51:38 +0100714 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000715 if (!err)
716 statbuf->st_mode = wstatbuf.st_mode;
717 return err;
718#else
719 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300720 PyObject *bytes;
721 char *cpath;
722
723 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000724 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100725 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300726
727 /* check for embedded null bytes */
728 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
729 Py_DECREF(bytes);
730 return -2;
731 }
732
733 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000734 Py_DECREF(bytes);
735 return ret;
736#endif
737}
738
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100739
Antoine Pitrou409b5382013-10-12 22:41:17 +0200740static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200741get_inheritable(int fd, int raise)
742{
743#ifdef MS_WINDOWS
744 HANDLE handle;
745 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000746
Steve Dower8fc89802015-04-12 00:26:27 -0400747 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200748 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400749 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200750 if (handle == INVALID_HANDLE_VALUE) {
751 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700752 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200753 return -1;
754 }
755
756 if (!GetHandleInformation(handle, &flags)) {
757 if (raise)
758 PyErr_SetFromWindowsErr(0);
759 return -1;
760 }
761
762 return (flags & HANDLE_FLAG_INHERIT);
763#else
764 int flags;
765
766 flags = fcntl(fd, F_GETFD, 0);
767 if (flags == -1) {
768 if (raise)
769 PyErr_SetFromErrno(PyExc_OSError);
770 return -1;
771 }
772 return !(flags & FD_CLOEXEC);
773#endif
774}
775
776/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200777 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200778 raise an exception and return -1 on error. */
779int
780_Py_get_inheritable(int fd)
781{
782 return get_inheritable(fd, 1);
783}
784
785static int
786set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
787{
788#ifdef MS_WINDOWS
789 HANDLE handle;
790 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200791#else
792#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
793 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200794 int request;
795 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200796#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200797 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200798 int res;
799#endif
800
801 /* atomic_flag_works can only be used to make the file descriptor
802 non-inheritable */
803 assert(!(atomic_flag_works != NULL && inheritable));
804
805 if (atomic_flag_works != NULL && !inheritable) {
806 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700807 int isInheritable = get_inheritable(fd, raise);
808 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200809 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700810 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200811 }
812
813 if (*atomic_flag_works)
814 return 0;
815 }
816
817#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -0400818 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200819 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400820 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200821 if (handle == INVALID_HANDLE_VALUE) {
822 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700823 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200824 return -1;
825 }
826
827 if (inheritable)
828 flags = HANDLE_FLAG_INHERIT;
829 else
830 flags = 0;
831 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
832 if (raise)
833 PyErr_SetFromWindowsErr(0);
834 return -1;
835 }
836 return 0;
837
Victor Stinnerdaf45552013-08-28 00:53:59 +0200838#else
Victor Stinner282124b2014-09-02 11:41:04 +0200839
840#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
841 if (ioctl_works != 0) {
842 /* fast-path: ioctl() only requires one syscall */
843 if (inheritable)
844 request = FIONCLEX;
845 else
846 request = FIOCLEX;
847 err = ioctl(fd, request, NULL);
848 if (!err) {
849 ioctl_works = 1;
850 return 0;
851 }
852
Victor Stinner3116cc42016-05-19 16:46:18 +0200853 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200854 if (raise)
855 PyErr_SetFromErrno(PyExc_OSError);
856 return -1;
857 }
858 else {
859 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
860 device". The ioctl is declared but not supported by the kernel.
861 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200862 Illumos-based OS for example.
863
864 Issue #27057: When SELinux policy disallows ioctl it will fail
865 with EACCES. While FIOCLEX is safe operation it may be
866 unavailable because ioctl was denied altogether.
867 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200868 ioctl_works = 0;
869 }
870 /* fallback to fcntl() if ioctl() does not work */
871 }
872#endif
873
874 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200875 flags = fcntl(fd, F_GETFD);
876 if (flags < 0) {
877 if (raise)
878 PyErr_SetFromErrno(PyExc_OSError);
879 return -1;
880 }
881
Victor Stinnera858bbd2016-04-17 16:51:52 +0200882 if (inheritable) {
883 new_flags = flags & ~FD_CLOEXEC;
884 }
885 else {
886 new_flags = flags | FD_CLOEXEC;
887 }
888
889 if (new_flags == flags) {
890 /* FD_CLOEXEC flag already set/cleared: nothing to do */
891 return 0;
892 }
893
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +0100894 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200895 if (res < 0) {
896 if (raise)
897 PyErr_SetFromErrno(PyExc_OSError);
898 return -1;
899 }
900 return 0;
901#endif
902}
903
904/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200905 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200906static int
907make_non_inheritable(int fd)
908{
909 return set_inheritable(fd, 0, 0, NULL);
910}
911
912/* Set the inheritable flag of the specified file descriptor.
913 On success: return 0, on error: raise an exception if raise is nonzero
914 and return -1.
915
916 If atomic_flag_works is not NULL:
917
918 * if *atomic_flag_works==-1, check if the inheritable is set on the file
919 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
920 set the inheritable flag
921 * if *atomic_flag_works==1: do nothing
922 * if *atomic_flag_works==0: set inheritable flag to False
923
924 Set atomic_flag_works to NULL if no atomic flag was used to create the
925 file descriptor.
926
927 atomic_flag_works can only be used to make a file descriptor
928 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
929int
930_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
931{
932 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
933}
934
Victor Stinnera555cfc2015-03-18 00:22:14 +0100935static int
936_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200937{
938 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100939 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100940#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200941 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100942#endif
943
944#ifdef MS_WINDOWS
945 flags |= O_NOINHERIT;
946#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200947 atomic_flag_works = &_Py_open_cloexec_works;
948 flags |= O_CLOEXEC;
949#else
950 atomic_flag_works = NULL;
951#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200952
Victor Stinnera555cfc2015-03-18 00:22:14 +0100953 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100954 do {
955 Py_BEGIN_ALLOW_THREADS
956 fd = open(pathname, flags);
957 Py_END_ALLOW_THREADS
958 } while (fd < 0
959 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
960 if (async_err)
961 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100962 if (fd < 0) {
963 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
964 return -1;
965 }
966 }
967 else {
968 fd = open(pathname, flags);
969 if (fd < 0)
970 return -1;
971 }
972
973#ifndef MS_WINDOWS
974 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200975 close(fd);
976 return -1;
977 }
Victor Stinnera555cfc2015-03-18 00:22:14 +0100978#endif
979
Victor Stinnerdaf45552013-08-28 00:53:59 +0200980 return fd;
981}
982
Victor Stinnera555cfc2015-03-18 00:22:14 +0100983/* Open a file with the specified flags (wrapper to open() function).
984 Return a file descriptor on success. Raise an exception and return -1 on
985 error.
986
987 The file descriptor is created non-inheritable.
988
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100989 When interrupted by a signal (open() fails with EINTR), retry the syscall,
990 except if the Python signal handler raises an exception.
991
Victor Stinner6f4fae82015-04-01 18:34:32 +0200992 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100993int
994_Py_open(const char *pathname, int flags)
995{
996 /* _Py_open() must be called with the GIL held. */
997 assert(PyGILState_Check());
998 return _Py_open_impl(pathname, flags, 1);
999}
1000
1001/* Open a file with the specified flags (wrapper to open() function).
1002 Return a file descriptor on success. Set errno and return -1 on error.
1003
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001004 The file descriptor is created non-inheritable.
1005
1006 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001007int
1008_Py_open_noraise(const char *pathname, int flags)
1009{
1010 return _Py_open_impl(pathname, flags, 0);
1011}
1012
Victor Stinnerdaf45552013-08-28 00:53:59 +02001013/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001014 encoding and use fopen() otherwise.
1015
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001016 The file descriptor is created non-inheritable.
1017
1018 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001019FILE *
1020_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1021{
Victor Stinner4e314432010-10-07 21:45:39 +00001022 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001023#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001024 char *cpath;
1025 char cmode[10];
1026 size_t r;
1027 r = wcstombs(cmode, mode, 10);
1028 if (r == (size_t)-1 || r >= 10) {
1029 errno = EINVAL;
1030 return NULL;
1031 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001032 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001033 if (cpath == NULL)
1034 return NULL;
1035 f = fopen(cpath, cmode);
1036 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001037#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001038 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001039#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001040 if (f == NULL)
1041 return NULL;
1042 if (make_non_inheritable(fileno(f)) < 0) {
1043 fclose(f);
1044 return NULL;
1045 }
1046 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001047}
1048
Victor Stinnere42ccd22015-03-18 01:39:23 +01001049/* Wrapper to fopen().
1050
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001051 The file descriptor is created non-inheritable.
1052
1053 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001054FILE*
1055_Py_fopen(const char *pathname, const char *mode)
1056{
1057 FILE *f = fopen(pathname, mode);
1058 if (f == NULL)
1059 return NULL;
1060 if (make_non_inheritable(fileno(f)) < 0) {
1061 fclose(f);
1062 return NULL;
1063 }
1064 return f;
1065}
1066
1067/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001068 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001069
Victor Stinnere42ccd22015-03-18 01:39:23 +01001070 Return the new file object on success. Raise an exception and return NULL
1071 on error.
1072
1073 The file descriptor is created non-inheritable.
1074
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001075 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1076 except if the Python signal handler raises an exception.
1077
Victor Stinner6f4fae82015-04-01 18:34:32 +02001078 Release the GIL to call _wfopen() or fopen(). The caller must hold
1079 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001080FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001081_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001082{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001083 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001084 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001085#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001086 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001087 wchar_t wmode[10];
1088 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001089
Victor Stinnere42ccd22015-03-18 01:39:23 +01001090 assert(PyGILState_Check());
1091
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001092 if (!PyUnicode_Check(path)) {
1093 PyErr_Format(PyExc_TypeError,
1094 "str file path expected under Windows, got %R",
1095 Py_TYPE(path));
1096 return NULL;
1097 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001098 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001099 if (wpath == NULL)
1100 return NULL;
1101
Victor Stinner4e314432010-10-07 21:45:39 +00001102 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001103 if (usize == 0) {
1104 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001105 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001106 }
Victor Stinner4e314432010-10-07 21:45:39 +00001107
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001108 do {
1109 Py_BEGIN_ALLOW_THREADS
1110 f = _wfopen(wpath, wmode);
1111 Py_END_ALLOW_THREADS
1112 } while (f == NULL
1113 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001114#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001115 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001116 char *path_bytes;
1117
1118 assert(PyGILState_Check());
1119
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001120 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001121 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001122 path_bytes = PyBytes_AS_STRING(bytes);
1123
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001124 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 Stinnere42ccd22015-03-18 01:39:23 +01001130
Victor Stinner4e314432010-10-07 21:45:39 +00001131 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001132#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001133 if (async_err)
1134 return NULL;
1135
Victor Stinnere42ccd22015-03-18 01:39:23 +01001136 if (f == NULL) {
1137 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001138 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001139 }
1140
1141 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001142 fclose(f);
1143 return NULL;
1144 }
1145 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001146}
1147
Victor Stinner66aab0c2015-03-19 22:53:20 +01001148/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001149
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 Stinner66aab0c2015-03-19 22:53:20 +01001161Py_ssize_t
1162_Py_read(int fd, void *buf, size_t count)
1163{
1164 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001165 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001166 int async_err = 0;
1167
Victor Stinner8a1be612016-03-14 22:07:55 +01001168 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001169
Victor Stinner66aab0c2015-03-19 22:53:20 +01001170 /* _Py_read() must not be called with an exception set, otherwise the
1171 * caller may think that read() was interrupted by a signal and the signal
1172 * handler raised an exception. */
1173 assert(!PyErr_Occurred());
1174
Victor Stinner66aab0c2015-03-19 22:53:20 +01001175#ifdef MS_WINDOWS
1176 if (count > INT_MAX) {
1177 /* On Windows, the count parameter of read() is an int */
1178 count = INT_MAX;
1179 }
1180#else
1181 if (count > PY_SSIZE_T_MAX) {
1182 /* if count is greater than PY_SSIZE_T_MAX,
1183 * read() result is undefined */
1184 count = PY_SSIZE_T_MAX;
1185 }
1186#endif
1187
Steve Dower8fc89802015-04-12 00:26:27 -04001188 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001189 do {
1190 Py_BEGIN_ALLOW_THREADS
1191 errno = 0;
1192#ifdef MS_WINDOWS
1193 n = read(fd, buf, (int)count);
1194#else
1195 n = read(fd, buf, count);
1196#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001197 /* save/restore errno because PyErr_CheckSignals()
1198 * and PyErr_SetFromErrno() can modify it */
1199 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001200 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001201 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001202 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001203 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001204
1205 if (async_err) {
1206 /* read() was interrupted by a signal (failed with EINTR)
1207 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001208 errno = err;
1209 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001210 return -1;
1211 }
1212 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001213 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001214 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001215 return -1;
1216 }
1217
1218 return n;
1219}
1220
Victor Stinner82c3e452015-04-01 18:34:45 +02001221static Py_ssize_t
1222_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001223{
1224 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001225 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001226 int async_err = 0;
1227
Steve Dower8fc89802015-04-12 00:26:27 -04001228 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001229#ifdef MS_WINDOWS
1230 if (count > 32767 && isatty(fd)) {
1231 /* Issue #11395: the Windows console returns an error (12: not
1232 enough space error) on writing into stdout if stdout mode is
1233 binary and the length is greater than 66,000 bytes (or less,
1234 depending on heap usage). */
1235 count = 32767;
1236 }
1237 else if (count > INT_MAX)
1238 count = INT_MAX;
1239#else
1240 if (count > PY_SSIZE_T_MAX) {
1241 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1242 * to do it ourself to have a portable behaviour. */
1243 count = PY_SSIZE_T_MAX;
1244 }
1245#endif
1246
Victor Stinner82c3e452015-04-01 18:34:45 +02001247 if (gil_held) {
1248 do {
1249 Py_BEGIN_ALLOW_THREADS
1250 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001251#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001252 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001253#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001254 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001255#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001256 /* save/restore errno because PyErr_CheckSignals()
1257 * and PyErr_SetFromErrno() can modify it */
1258 err = errno;
1259 Py_END_ALLOW_THREADS
1260 } while (n < 0 && err == EINTR &&
1261 !(async_err = PyErr_CheckSignals()));
1262 }
1263 else {
1264 do {
1265 errno = 0;
1266#ifdef MS_WINDOWS
1267 n = write(fd, buf, (int)count);
1268#else
1269 n = write(fd, buf, count);
1270#endif
1271 err = errno;
1272 } while (n < 0 && err == EINTR);
1273 }
Steve Dower8fc89802015-04-12 00:26:27 -04001274 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001275
1276 if (async_err) {
1277 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001278 and the Python signal handler raised an exception (if gil_held is
1279 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001280 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001281 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001282 return -1;
1283 }
1284 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001285 if (gil_held)
1286 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001287 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001288 return -1;
1289 }
1290
1291 return n;
1292}
1293
Victor Stinner82c3e452015-04-01 18:34:45 +02001294/* Write count bytes of buf into fd.
1295
1296 On success, return the number of written bytes, it can be lower than count
1297 including 0. On error, raise an exception, set errno and return -1.
1298
1299 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1300 If the Python signal handler raises an exception, the function returns -1
1301 (the syscall is not retried).
1302
1303 Release the GIL to call write(). The caller must hold the GIL. */
1304Py_ssize_t
1305_Py_write(int fd, const void *buf, size_t count)
1306{
Victor Stinner8a1be612016-03-14 22:07:55 +01001307 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001308
Victor Stinner82c3e452015-04-01 18:34:45 +02001309 /* _Py_write() must not be called with an exception set, otherwise the
1310 * caller may think that write() was interrupted by a signal and the signal
1311 * handler raised an exception. */
1312 assert(!PyErr_Occurred());
1313
1314 return _Py_write_impl(fd, buf, count, 1);
1315}
1316
1317/* Write count bytes of buf into fd.
1318 *
1319 * On success, return the number of written bytes, it can be lower than count
1320 * including 0. On error, set errno and return -1.
1321 *
1322 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1323 * without calling the Python signal handler. */
1324Py_ssize_t
1325_Py_write_noraise(int fd, const void *buf, size_t count)
1326{
1327 return _Py_write_impl(fd, buf, count, 0);
1328}
1329
Victor Stinner4e314432010-10-07 21:45:39 +00001330#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001331
1332/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001333 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001334
Victor Stinner4e314432010-10-07 21:45:39 +00001335int
1336_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1337{
1338 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001339 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001340 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001341 int res;
1342 size_t r1;
1343
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001344 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001345 if (cpath == NULL) {
1346 errno = EINVAL;
1347 return -1;
1348 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001349 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001350 PyMem_Free(cpath);
1351 if (res == -1)
1352 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001353 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001354 errno = EINVAL;
1355 return -1;
1356 }
1357 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001358 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001359 if (wbuf == NULL) {
1360 errno = EINVAL;
1361 return -1;
1362 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001363 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001364 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001365 errno = EINVAL;
1366 return -1;
1367 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001368 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001369 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001370 return (int)r1;
1371}
1372#endif
1373
1374#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001375
1376/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001377 encoding, decode the result from the locale encoding.
1378 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001379
Victor Stinner4e314432010-10-07 21:45:39 +00001380wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001381_Py_wrealpath(const wchar_t *path,
1382 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001383{
1384 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001385 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001386 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001387 char *res;
1388 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001389 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001390 if (cpath == NULL) {
1391 errno = EINVAL;
1392 return NULL;
1393 }
1394 res = realpath(cpath, cresolved_path);
1395 PyMem_Free(cpath);
1396 if (res == NULL)
1397 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001398
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001399 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001400 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001401 errno = EINVAL;
1402 return NULL;
1403 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001404 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001405 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001406 errno = EINVAL;
1407 return NULL;
1408 }
1409 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001410 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001411 return resolved_path;
1412}
1413#endif
1414
Victor Stinnerf4061da2010-10-14 12:37:19 +00001415/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001416 including the null character. Decode the path from the locale encoding.
1417 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001418
Victor Stinner4e314432010-10-07 21:45:39 +00001419wchar_t*
1420_Py_wgetcwd(wchar_t *buf, size_t size)
1421{
1422#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001423 int isize = (int)Py_MIN(size, INT_MAX);
1424 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001425#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001426 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001427 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001428 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001429
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001430 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001431 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001432 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001433 if (wname == NULL)
1434 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001435 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001436 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001437 return NULL;
1438 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001439 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001440 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001441 return buf;
1442#endif
1443}
1444
Victor Stinnerdaf45552013-08-28 00:53:59 +02001445/* Duplicate a file descriptor. The new file descriptor is created as
1446 non-inheritable. Return a new file descriptor on success, raise an OSError
1447 exception and return -1 on error.
1448
1449 The GIL is released to call dup(). The caller must hold the GIL. */
1450int
1451_Py_dup(int fd)
1452{
1453#ifdef MS_WINDOWS
1454 HANDLE handle;
1455 DWORD ftype;
1456#endif
1457
Victor Stinner8a1be612016-03-14 22:07:55 +01001458 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001459
Victor Stinnerdaf45552013-08-28 00:53:59 +02001460#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001461 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001462 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001463 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001464 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001465 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001466 return -1;
1467 }
1468
1469 /* get the file type, ignore the error if it failed */
1470 ftype = GetFileType(handle);
1471
1472 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001473 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001474 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001475 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001476 Py_END_ALLOW_THREADS
1477 if (fd < 0) {
1478 PyErr_SetFromErrno(PyExc_OSError);
1479 return -1;
1480 }
1481
1482 /* Character files like console cannot be make non-inheritable */
1483 if (ftype != FILE_TYPE_CHAR) {
1484 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001485 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001486 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001487 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001488 return -1;
1489 }
1490 }
1491#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1492 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001493 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001494 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001495 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001496 Py_END_ALLOW_THREADS
1497 if (fd < 0) {
1498 PyErr_SetFromErrno(PyExc_OSError);
1499 return -1;
1500 }
1501
1502#else
1503 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001504 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001505 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001506 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001507 Py_END_ALLOW_THREADS
1508 if (fd < 0) {
1509 PyErr_SetFromErrno(PyExc_OSError);
1510 return -1;
1511 }
1512
1513 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001514 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001515 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001516 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001517 return -1;
1518 }
1519#endif
1520 return fd;
1521}
1522
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001523#ifndef MS_WINDOWS
1524/* Get the blocking mode of the file descriptor.
1525 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1526 raise an exception and return -1 on error. */
1527int
1528_Py_get_blocking(int fd)
1529{
Steve Dower8fc89802015-04-12 00:26:27 -04001530 int flags;
1531 _Py_BEGIN_SUPPRESS_IPH
1532 flags = fcntl(fd, F_GETFL, 0);
1533 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001534 if (flags < 0) {
1535 PyErr_SetFromErrno(PyExc_OSError);
1536 return -1;
1537 }
1538
1539 return !(flags & O_NONBLOCK);
1540}
1541
1542/* Set the blocking mode of the specified file descriptor.
1543
1544 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1545 otherwise.
1546
1547 Return 0 on success, raise an exception and return -1 on error. */
1548int
1549_Py_set_blocking(int fd, int blocking)
1550{
1551#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1552 int arg = !blocking;
1553 if (ioctl(fd, FIONBIO, &arg) < 0)
1554 goto error;
1555#else
1556 int flags, res;
1557
Steve Dower8fc89802015-04-12 00:26:27 -04001558 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001559 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001560 if (flags >= 0) {
1561 if (blocking)
1562 flags = flags & (~O_NONBLOCK);
1563 else
1564 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001565
Steve Dower8fc89802015-04-12 00:26:27 -04001566 res = fcntl(fd, F_SETFL, flags);
1567 } else {
1568 res = -1;
1569 }
1570 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001571
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001572 if (res < 0)
1573 goto error;
1574#endif
1575 return 0;
1576
1577error:
1578 PyErr_SetFromErrno(PyExc_OSError);
1579 return -1;
1580}
1581#endif