blob: 645a1793664a1cbee9cab0b8102c1f63316a20ec [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 Stinnere47e6982017-12-21 15:45:16 +010023extern char* _Py_EncodeUTF8_surrogateescape(const wchar_t *text,
Victor Stinner9dd76202017-12-21 16:20:32 +010024 size_t *error_pos, int raw_malloc);
Victor Stinnere2623772012-11-12 23:04:02 +010025
Victor Stinnerdaf45552013-08-28 00:53:59 +020026#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020027/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020028
29 -1: unknown
30 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
31 1: open() supports O_CLOEXEC flag, close-on-exec is set
32
Victor Stinnera555cfc2015-03-18 00:22:14 +010033 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
34 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020035int _Py_open_cloexec_works = -1;
36#endif
37
Brett Cannonefb00c02012-02-29 18:31:31 -050038PyObject *
39_Py_device_encoding(int fd)
40{
Victor Stinner14b9b112013-06-25 00:37:25 +020041#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050042 UINT cp;
43#endif
Steve Dower8fc89802015-04-12 00:26:27 -040044 int valid;
45 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -070046 valid = isatty(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040047 _Py_END_SUPPRESS_IPH
48 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050049 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040050
Victor Stinner14b9b112013-06-25 00:37:25 +020051#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050052 if (fd == 0)
53 cp = GetConsoleCP();
54 else if (fd == 1 || fd == 2)
55 cp = GetConsoleOutputCP();
56 else
57 cp = 0;
58 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
59 has no console */
60 if (cp != 0)
61 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
62#elif defined(CODESET)
63 {
64 char *codeset = nl_langinfo(CODESET);
65 if (codeset != NULL && codeset[0] != 0)
66 return PyUnicode_FromString(codeset);
67 }
68#endif
69 Py_RETURN_NONE;
70}
71
Victor Stinnerd45c7f82012-12-04 01:34:47 +010072#if !defined(__APPLE__) && !defined(MS_WINDOWS)
73extern int _Py_normalize_encoding(const char *, char *, size_t);
74
75/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
76 On these operating systems, nl_langinfo(CODESET) announces an alias of the
77 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
78 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
79 locale.getpreferredencoding() codec. For example, if command line arguments
80 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
81 UnicodeEncodeError instead of retrieving the original byte string.
82
83 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
84 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
85 one byte in range 0x80-0xff can be decoded from the locale encoding. The
86 workaround is also enabled on error, for example if getting the locale
87 failed.
88
Philip Jenvey215c49a2013-01-15 13:24:12 -080089 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010090
Victor Stinnerf6a271a2014-08-01 12:28:48 +020091 1: the workaround is used: Py_EncodeLocale() uses
92 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010093 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020094 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
95 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010096 -1: unknown, need to call check_force_ascii() to get the value
97*/
98static int force_ascii = -1;
99
100static int
101check_force_ascii(void)
102{
103 char *loc;
104#if defined(HAVE_LANGINFO_H) && defined(CODESET)
105 char *codeset, **alias;
Victor Stinner54de2b12016-09-09 23:11:52 -0700106 char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100107 int is_ascii;
108 unsigned int i;
109 char* ascii_aliases[] = {
110 "ascii",
Victor Stinner54de2b12016-09-09 23:11:52 -0700111 /* Aliases from Lib/encodings/aliases.py */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100112 "646",
Victor Stinner54de2b12016-09-09 23:11:52 -0700113 "ansi_x3.4_1968",
114 "ansi_x3.4_1986",
115 "ansi_x3_4_1968",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100116 "cp367",
117 "csascii",
118 "ibm367",
Victor Stinner54de2b12016-09-09 23:11:52 -0700119 "iso646_us",
120 "iso_646.irv_1991",
121 "iso_ir_6",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100122 "us",
Victor Stinner54de2b12016-09-09 23:11:52 -0700123 "us_ascii",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100124 NULL
125 };
126#endif
127
128 loc = setlocale(LC_CTYPE, NULL);
129 if (loc == NULL)
130 goto error;
131 if (strcmp(loc, "C") != 0) {
132 /* the LC_CTYPE locale is different than C */
133 return 0;
134 }
135
136#if defined(HAVE_LANGINFO_H) && defined(CODESET)
137 codeset = nl_langinfo(CODESET);
138 if (!codeset || codeset[0] == '\0') {
139 /* CODESET is not set or empty */
140 goto error;
141 }
142 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
143 goto error;
144
145 is_ascii = 0;
146 for (alias=ascii_aliases; *alias != NULL; alias++) {
147 if (strcmp(encoding, *alias) == 0) {
148 is_ascii = 1;
149 break;
150 }
151 }
152 if (!is_ascii) {
153 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
154 return 0;
155 }
156
157 for (i=0x80; i<0xff; i++) {
158 unsigned char ch;
159 wchar_t wch;
160 size_t res;
161
162 ch = (unsigned char)i;
163 res = mbstowcs(&wch, (char*)&ch, 1);
164 if (res != (size_t)-1) {
165 /* decoding a non-ASCII character from the locale encoding succeed:
166 the locale encoding is not ASCII, force ASCII */
167 return 1;
168 }
169 }
170 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
171 encoding: the locale encoding is really ASCII */
172 return 0;
173#else
174 /* nl_langinfo(CODESET) is not available: always force ASCII */
175 return 1;
176#endif
177
178error:
Martin Panter46f50722016-05-26 05:35:26 +0000179 /* if an error occurred, force the ASCII encoding */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100180 return 1;
181}
182
183static char*
Victor Stinner9dd76202017-12-21 16:20:32 +0100184encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos, int raw_malloc)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100185{
186 char *result = NULL, *out;
187 size_t len, i;
188 wchar_t ch;
189
190 if (error_pos != NULL)
191 *error_pos = (size_t)-1;
192
193 len = wcslen(text);
194
Victor Stinner9bee3292017-12-21 16:49:13 +0100195 /* +1 for NULL byte */
Victor Stinner9dd76202017-12-21 16:20:32 +0100196 if (raw_malloc) {
197 result = PyMem_RawMalloc(len + 1);
198 }
199 else {
200 result = PyMem_Malloc(len + 1);
201 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100202 if (result == NULL)
203 return NULL;
204
205 out = result;
206 for (i=0; i<len; i++) {
207 ch = text[i];
208
209 if (ch <= 0x7f) {
210 /* ASCII character */
211 *out++ = (char)ch;
212 }
213 else if (0xdc80 <= ch && ch <= 0xdcff) {
214 /* UTF-8b surrogate */
215 *out++ = (char)(ch - 0xdc00);
216 }
217 else {
Victor Stinner9dd76202017-12-21 16:20:32 +0100218 if (error_pos != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100219 *error_pos = i;
Victor Stinner9dd76202017-12-21 16:20:32 +0100220 }
221 if (raw_malloc) {
222 PyMem_RawFree(result);
223 }
224 else {
225 PyMem_Free(result);
226 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100227 return NULL;
228 }
229 }
230 *out = '\0';
231 return result;
232}
233#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
234
235#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
236static wchar_t*
237decode_ascii_surrogateescape(const char *arg, size_t *size)
238{
239 wchar_t *res;
240 unsigned char *in;
241 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600242 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100243
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600244 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
245 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600246 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100247 if (!res)
248 return NULL;
249
250 in = (unsigned char*)arg;
251 out = res;
252 while(*in)
253 if(*in < 128)
254 *out++ = *in++;
255 else
256 *out++ = 0xdc00 + *in++;
257 *out = 0;
258 if (size != NULL)
259 *size = out - res;
260 return res;
261}
262#endif
263
Victor Stinnerd2b02312017-12-15 23:06:17 +0100264#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100265static wchar_t*
266decode_locale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000267{
268 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100269 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000270 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200271#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000272 unsigned char *in;
273 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000274 mbstate_t mbs;
275#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100276
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100277#ifdef HAVE_BROKEN_MBSTOWCS
278 /* Some platforms have a broken implementation of
279 * mbstowcs which does not count the characters that
280 * would result from conversion. Use an upper bound.
281 */
282 argsize = strlen(arg);
283#else
284 argsize = mbstowcs(NULL, arg, 0);
285#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000286 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600287 if (argsize == PY_SSIZE_T_MAX)
288 goto oom;
289 argsize += 1;
290 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
291 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600292 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000293 if (!res)
294 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600295 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000296 if (count != (size_t)-1) {
297 wchar_t *tmp;
298 /* Only use the result if it contains no
299 surrogate characters. */
300 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100301 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000302 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000303 if (*tmp == 0) {
304 if (size != NULL)
305 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000306 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000307 }
Victor Stinner4e314432010-10-07 21:45:39 +0000308 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200309 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000310 }
311 /* Conversion failed. Fall back to escaping with surrogateescape. */
312#ifdef HAVE_MBRTOWC
313 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
314
315 /* Overallocate; as multi-byte characters are in the argument, the
316 actual output could use less memory. */
317 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600318 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
319 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200320 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000321 if (!res)
322 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000323 in = (unsigned char*)arg;
324 out = res;
325 memset(&mbs, 0, sizeof mbs);
326 while (argsize) {
327 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
328 if (converted == 0)
329 /* Reached end of string; null char stored. */
330 break;
331 if (converted == (size_t)-2) {
332 /* Incomplete character. This should never happen,
333 since we provide everything that we have -
334 unless there is a bug in the C library, or I
335 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200336 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100337 if (size != NULL)
338 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000339 return NULL;
340 }
341 if (converted == (size_t)-1) {
342 /* Conversion error. Escape as UTF-8b, and start over
343 in the initial shift state. */
344 *out++ = 0xdc00 + *in++;
345 argsize--;
346 memset(&mbs, 0, sizeof mbs);
347 continue;
348 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100349 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000350 /* Surrogate character. Escape the original
351 byte sequence with surrogateescape. */
352 argsize -= converted;
353 while (converted--)
354 *out++ = 0xdc00 + *in++;
355 continue;
356 }
357 /* successfully converted some bytes */
358 in += converted;
359 argsize -= converted;
360 out++;
361 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100362 if (size != NULL)
363 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100364#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000365 /* Cannot use C locale for escaping; manually escape as if charset
366 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
367 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100368 res = decode_ascii_surrogateescape(arg, size);
369 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100370 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100371#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000372 return res;
Victor Stinner91106cd2017-12-13 12:29:09 +0100373
Victor Stinner4e314432010-10-07 21:45:39 +0000374oom:
Victor Stinner91106cd2017-12-13 12:29:09 +0100375 if (size != NULL) {
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100376 *size = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100377 }
Victor Stinner4e314432010-10-07 21:45:39 +0000378 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100379}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100380#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100381
382
383/* Decode a byte string from the locale encoding with the
384 surrogateescape error handler: undecodable bytes are decoded as characters
385 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
386 character, escape the bytes using the surrogateescape error handler instead
387 of decoding them.
388
389 Return a pointer to a newly allocated wide character string, use
390 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
391 wide characters excluding the null character into *size
392
393 Return NULL on decoding error or memory allocation error. If *size* is not
394 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
395 decoding error.
396
397 Decoding errors should never happen, unless there is a bug in the C
398 library.
399
400 Use the Py_EncodeLocale() function to encode the character string back to a
401 byte string. */
402wchar_t*
403Py_DecodeLocale(const char* arg, size_t *size)
404{
405#if defined(__APPLE__) || defined(__ANDROID__)
406 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
407#else
Victor Stinner94540602017-12-16 04:54:22 +0100408 if (Py_UTF8Mode == 1) {
Victor Stinner91106cd2017-12-13 12:29:09 +0100409 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
410 }
411
412#ifndef MS_WINDOWS
413 if (force_ascii == -1)
414 force_ascii = check_force_ascii();
415
416 if (force_ascii) {
417 /* force ASCII encoding to workaround mbstowcs() issue */
418 wchar_t *wstr = decode_ascii_surrogateescape(arg, size);
419 if (wstr == NULL) {
420 if (size != NULL) {
421 *size = (size_t)-1;
422 }
423 return NULL;
424 }
425 return wstr;
426 }
427#endif
428
429 return decode_locale(arg, size);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100430#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000431}
432
Victor Stinner91106cd2017-12-13 12:29:09 +0100433
Victor Stinnerd2b02312017-12-15 23:06:17 +0100434#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100435static char*
Victor Stinner9dd76202017-12-21 16:20:32 +0100436encode_current_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
Victor Stinner91106cd2017-12-13 12:29:09 +0100437{
Victor Stinner4e314432010-10-07 21:45:39 +0000438 const size_t len = wcslen(text);
439 char *result = NULL, *bytes = NULL;
440 size_t i, size, converted;
441 wchar_t c, buf[2];
442
443 /* The function works in two steps:
444 1. compute the length of the output buffer in bytes (size)
445 2. outputs the bytes */
446 size = 0;
447 buf[1] = 0;
448 while (1) {
449 for (i=0; i < len; i++) {
450 c = text[i];
451 if (c >= 0xdc80 && c <= 0xdcff) {
452 /* UTF-8b surrogate */
453 if (bytes != NULL) {
454 *bytes++ = c - 0xdc00;
455 size--;
456 }
457 else
458 size++;
459 continue;
460 }
461 else {
462 buf[0] = c;
463 if (bytes != NULL)
464 converted = wcstombs(bytes, buf, size);
465 else
466 converted = wcstombs(NULL, buf, 0);
467 if (converted == (size_t)-1) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100468 if (raw_malloc) {
469 PyMem_RawFree(result);
470 }
471 else {
472 PyMem_Free(result);
Victor Stinner9dd76202017-12-21 16:20:32 +0100473 }
Victor Stinner2f02a512010-11-08 22:43:46 +0000474 if (error_pos != NULL)
475 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000476 return NULL;
477 }
478 if (bytes != NULL) {
479 bytes += converted;
480 size -= converted;
481 }
482 else
483 size += converted;
484 }
485 }
486 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100487 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000488 break;
489 }
490
491 size += 1; /* nul byte at the end */
Victor Stinner9dd76202017-12-21 16:20:32 +0100492 if (raw_malloc) {
493 result = PyMem_RawMalloc(size);
494 }
495 else {
496 result = PyMem_Malloc(size);
497 }
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100498 if (result == NULL) {
Victor Stinner9dd76202017-12-21 16:20:32 +0100499 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100500 *error_pos = (size_t)-1;
Victor Stinner9dd76202017-12-21 16:20:32 +0100501 }
Victor Stinner4e314432010-10-07 21:45:39 +0000502 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100503 }
Victor Stinner4e314432010-10-07 21:45:39 +0000504 bytes = result;
505 }
506 return result;
Victor Stinner91106cd2017-12-13 12:29:09 +0100507}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100508#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100509
Victor Stinner9dd76202017-12-21 16:20:32 +0100510static char*
511encode_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
512{
513#if defined(__APPLE__) || defined(__ANDROID__)
514 return _Py_EncodeUTF8_surrogateescape(text, error_pos, raw_malloc);
515#else /* __APPLE__ */
516 if (Py_UTF8Mode == 1) {
517 return _Py_EncodeUTF8_surrogateescape(text, error_pos, raw_malloc);
518 }
519
520#ifndef MS_WINDOWS
521 if (force_ascii == -1)
522 force_ascii = check_force_ascii();
523
524 if (force_ascii)
525 return encode_ascii_surrogateescape(text, error_pos, raw_malloc);
526#endif
527
528 return encode_current_locale(text, error_pos, raw_malloc);
529#endif /* __APPLE__ or __ANDROID__ */
530}
531
Victor Stinner91106cd2017-12-13 12:29:09 +0100532/* Encode a wide character string to the locale encoding with the
533 surrogateescape error handler: surrogate characters in the range
534 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
535
536 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
537 the memory. Return NULL on encoding or memory allocation error.
538
539 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
540 to the index of the invalid character on encoding error.
541
542 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
543 character string. */
544char*
545Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
546{
Victor Stinner9dd76202017-12-21 16:20:32 +0100547 return encode_locale(text, error_pos, 0);
548}
Victor Stinner91106cd2017-12-13 12:29:09 +0100549
Victor Stinner91106cd2017-12-13 12:29:09 +0100550
Victor Stinner9dd76202017-12-21 16:20:32 +0100551/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
552 instead of PyMem_Free(). */
553char*
554_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
555{
556 return encode_locale(text, error_pos, 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000557}
558
Victor Stinner6672d0c2010-10-07 22:53:43 +0000559
Steve Dowerf2f373f2015-02-21 08:44:05 -0800560#ifdef MS_WINDOWS
561static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
562
563static void
564FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
565{
566 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
567 /* Cannot simply cast and dereference in_ptr,
568 since it might not be aligned properly */
569 __int64 in;
570 memcpy(&in, in_ptr, sizeof(in));
571 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
572 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
573}
574
575void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800576_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800577{
578 /* XXX endianness */
579 __int64 out;
580 out = time_in + secs_between_epochs;
581 out = out * 10000000 + nsec_in / 100;
582 memcpy(out_ptr, &out, sizeof(out));
583}
584
585/* Below, we *know* that ugo+r is 0444 */
586#if _S_IREAD != 0400
587#error Unsupported C library
588#endif
589static int
590attributes_to_mode(DWORD attr)
591{
592 int m = 0;
593 if (attr & FILE_ATTRIBUTE_DIRECTORY)
594 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
595 else
596 m |= _S_IFREG;
597 if (attr & FILE_ATTRIBUTE_READONLY)
598 m |= 0444;
599 else
600 m |= 0666;
601 return m;
602}
603
Steve Dowerbf1f3762015-02-21 15:26:02 -0800604void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200605_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
606 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800607{
608 memset(result, 0, sizeof(*result));
609 result->st_mode = attributes_to_mode(info->dwFileAttributes);
610 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
611 result->st_dev = info->dwVolumeSerialNumber;
612 result->st_rdev = result->st_dev;
613 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
614 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
615 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
616 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100617 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800618 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
619 /* first clear the S_IFMT bits */
620 result->st_mode ^= (result->st_mode & S_IFMT);
621 /* now set the bits that make this a symlink */
622 result->st_mode |= S_IFLNK;
623 }
624 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800625}
626#endif
627
628/* Return information about a file.
629
630 On POSIX, use fstat().
631
632 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800633 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
634 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800635 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200636
637 On Windows, set the last Windows error and return nonzero on error. On
638 POSIX, set errno and return nonzero on error. Fill status and return 0 on
639 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800640int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200641_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800642{
643#ifdef MS_WINDOWS
644 BY_HANDLE_FILE_INFORMATION info;
645 HANDLE h;
646 int type;
647
Steve Dower940f33a2016-09-08 11:21:54 -0700648 _Py_BEGIN_SUPPRESS_IPH
649 h = (HANDLE)_get_osfhandle(fd);
650 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800651
652 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400653 /* errno is already set by _get_osfhandle, but we also set
654 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800655 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800656 return -1;
657 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200658 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800659
660 type = GetFileType(h);
661 if (type == FILE_TYPE_UNKNOWN) {
662 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400663 if (error != 0) {
664 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800665 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400666 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800667 /* else: valid but unknown file */
668 }
669
670 if (type != FILE_TYPE_DISK) {
671 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200672 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800673 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200674 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800675 return 0;
676 }
677
678 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400679 /* The Win32 error is already set, but we also set errno for
680 callers who expect it */
681 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800682 return -1;
683 }
684
Victor Stinnere134a7f2015-03-30 10:09:31 +0200685 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800686 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100687 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800688 return 0;
689#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200690 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800691#endif
692}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800693
Victor Stinnere134a7f2015-03-30 10:09:31 +0200694/* Return information about a file.
695
696 On POSIX, use fstat().
697
698 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800699 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
700 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200701 #23152.
702
703 Raise an exception and return -1 on error. On Windows, set the last Windows
704 error on error. On POSIX, set errno on error. Fill status and return 0 on
705 success.
706
Victor Stinner6f4fae82015-04-01 18:34:32 +0200707 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
708 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200709int
710_Py_fstat(int fd, struct _Py_stat_struct *status)
711{
712 int res;
713
Victor Stinner8a1be612016-03-14 22:07:55 +0100714 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100715
Victor Stinnere134a7f2015-03-30 10:09:31 +0200716 Py_BEGIN_ALLOW_THREADS
717 res = _Py_fstat_noraise(fd, status);
718 Py_END_ALLOW_THREADS
719
720 if (res != 0) {
721#ifdef MS_WINDOWS
722 PyErr_SetFromWindowsErr(0);
723#else
724 PyErr_SetFromErrno(PyExc_OSError);
725#endif
726 return -1;
727 }
728 return 0;
729}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800730
Victor Stinner6672d0c2010-10-07 22:53:43 +0000731/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
732 call stat() otherwise. Only fill st_mode attribute on Windows.
733
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100734 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
735 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000736
737int
Victor Stinnera4a75952010-10-07 22:23:10 +0000738_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000739{
740#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000741 int err;
742 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300743 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000744
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300745 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100746 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100747 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300748
Victor Stinneree587ea2011-11-17 00:51:38 +0100749 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000750 if (!err)
751 statbuf->st_mode = wstatbuf.st_mode;
752 return err;
753#else
754 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300755 PyObject *bytes;
756 char *cpath;
757
758 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000759 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100760 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300761
762 /* check for embedded null bytes */
763 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
764 Py_DECREF(bytes);
765 return -2;
766 }
767
768 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000769 Py_DECREF(bytes);
770 return ret;
771#endif
772}
773
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100774
Antoine Pitrou409b5382013-10-12 22:41:17 +0200775static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200776get_inheritable(int fd, int raise)
777{
778#ifdef MS_WINDOWS
779 HANDLE handle;
780 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000781
Steve Dower8fc89802015-04-12 00:26:27 -0400782 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200783 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400784 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200785 if (handle == INVALID_HANDLE_VALUE) {
786 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700787 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200788 return -1;
789 }
790
791 if (!GetHandleInformation(handle, &flags)) {
792 if (raise)
793 PyErr_SetFromWindowsErr(0);
794 return -1;
795 }
796
797 return (flags & HANDLE_FLAG_INHERIT);
798#else
799 int flags;
800
801 flags = fcntl(fd, F_GETFD, 0);
802 if (flags == -1) {
803 if (raise)
804 PyErr_SetFromErrno(PyExc_OSError);
805 return -1;
806 }
807 return !(flags & FD_CLOEXEC);
808#endif
809}
810
811/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200812 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200813 raise an exception and return -1 on error. */
814int
815_Py_get_inheritable(int fd)
816{
817 return get_inheritable(fd, 1);
818}
819
820static int
821set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
822{
823#ifdef MS_WINDOWS
824 HANDLE handle;
825 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200826#else
827#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
828 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200829 int request;
830 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200831#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200832 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200833 int res;
834#endif
835
836 /* atomic_flag_works can only be used to make the file descriptor
837 non-inheritable */
838 assert(!(atomic_flag_works != NULL && inheritable));
839
840 if (atomic_flag_works != NULL && !inheritable) {
841 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700842 int isInheritable = get_inheritable(fd, raise);
843 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200844 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700845 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200846 }
847
848 if (*atomic_flag_works)
849 return 0;
850 }
851
852#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -0400853 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200854 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400855 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200856 if (handle == INVALID_HANDLE_VALUE) {
857 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700858 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200859 return -1;
860 }
861
862 if (inheritable)
863 flags = HANDLE_FLAG_INHERIT;
864 else
865 flags = 0;
866 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
867 if (raise)
868 PyErr_SetFromWindowsErr(0);
869 return -1;
870 }
871 return 0;
872
Victor Stinnerdaf45552013-08-28 00:53:59 +0200873#else
Victor Stinner282124b2014-09-02 11:41:04 +0200874
875#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
876 if (ioctl_works != 0) {
877 /* fast-path: ioctl() only requires one syscall */
878 if (inheritable)
879 request = FIONCLEX;
880 else
881 request = FIOCLEX;
882 err = ioctl(fd, request, NULL);
883 if (!err) {
884 ioctl_works = 1;
885 return 0;
886 }
887
Victor Stinner3116cc42016-05-19 16:46:18 +0200888 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200889 if (raise)
890 PyErr_SetFromErrno(PyExc_OSError);
891 return -1;
892 }
893 else {
894 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
895 device". The ioctl is declared but not supported by the kernel.
896 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200897 Illumos-based OS for example.
898
899 Issue #27057: When SELinux policy disallows ioctl it will fail
900 with EACCES. While FIOCLEX is safe operation it may be
901 unavailable because ioctl was denied altogether.
902 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200903 ioctl_works = 0;
904 }
905 /* fallback to fcntl() if ioctl() does not work */
906 }
907#endif
908
909 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200910 flags = fcntl(fd, F_GETFD);
911 if (flags < 0) {
912 if (raise)
913 PyErr_SetFromErrno(PyExc_OSError);
914 return -1;
915 }
916
Victor Stinnera858bbd2016-04-17 16:51:52 +0200917 if (inheritable) {
918 new_flags = flags & ~FD_CLOEXEC;
919 }
920 else {
921 new_flags = flags | FD_CLOEXEC;
922 }
923
924 if (new_flags == flags) {
925 /* FD_CLOEXEC flag already set/cleared: nothing to do */
926 return 0;
927 }
928
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +0100929 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200930 if (res < 0) {
931 if (raise)
932 PyErr_SetFromErrno(PyExc_OSError);
933 return -1;
934 }
935 return 0;
936#endif
937}
938
939/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200940 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200941static int
942make_non_inheritable(int fd)
943{
944 return set_inheritable(fd, 0, 0, NULL);
945}
946
947/* Set the inheritable flag of the specified file descriptor.
948 On success: return 0, on error: raise an exception if raise is nonzero
949 and return -1.
950
951 If atomic_flag_works is not NULL:
952
953 * if *atomic_flag_works==-1, check if the inheritable is set on the file
954 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
955 set the inheritable flag
956 * if *atomic_flag_works==1: do nothing
957 * if *atomic_flag_works==0: set inheritable flag to False
958
959 Set atomic_flag_works to NULL if no atomic flag was used to create the
960 file descriptor.
961
962 atomic_flag_works can only be used to make a file descriptor
963 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
964int
965_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
966{
967 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
968}
969
Victor Stinnera555cfc2015-03-18 00:22:14 +0100970static int
971_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200972{
973 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100974 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100975#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200976 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100977#endif
978
979#ifdef MS_WINDOWS
980 flags |= O_NOINHERIT;
981#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200982 atomic_flag_works = &_Py_open_cloexec_works;
983 flags |= O_CLOEXEC;
984#else
985 atomic_flag_works = NULL;
986#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200987
Victor Stinnera555cfc2015-03-18 00:22:14 +0100988 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100989 do {
990 Py_BEGIN_ALLOW_THREADS
991 fd = open(pathname, flags);
992 Py_END_ALLOW_THREADS
993 } while (fd < 0
994 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
995 if (async_err)
996 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100997 if (fd < 0) {
998 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
999 return -1;
1000 }
1001 }
1002 else {
1003 fd = open(pathname, flags);
1004 if (fd < 0)
1005 return -1;
1006 }
1007
1008#ifndef MS_WINDOWS
1009 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001010 close(fd);
1011 return -1;
1012 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001013#endif
1014
Victor Stinnerdaf45552013-08-28 00:53:59 +02001015 return fd;
1016}
1017
Victor Stinnera555cfc2015-03-18 00:22:14 +01001018/* Open a file with the specified flags (wrapper to open() function).
1019 Return a file descriptor on success. Raise an exception and return -1 on
1020 error.
1021
1022 The file descriptor is created non-inheritable.
1023
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001024 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1025 except if the Python signal handler raises an exception.
1026
Victor Stinner6f4fae82015-04-01 18:34:32 +02001027 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001028int
1029_Py_open(const char *pathname, int flags)
1030{
1031 /* _Py_open() must be called with the GIL held. */
1032 assert(PyGILState_Check());
1033 return _Py_open_impl(pathname, flags, 1);
1034}
1035
1036/* Open a file with the specified flags (wrapper to open() function).
1037 Return a file descriptor on success. Set errno and return -1 on error.
1038
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001039 The file descriptor is created non-inheritable.
1040
1041 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001042int
1043_Py_open_noraise(const char *pathname, int flags)
1044{
1045 return _Py_open_impl(pathname, flags, 0);
1046}
1047
Victor Stinnerdaf45552013-08-28 00:53:59 +02001048/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001049 encoding and use fopen() otherwise.
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 Stinner4e314432010-10-07 21:45:39 +00001054FILE *
1055_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1056{
Victor Stinner4e314432010-10-07 21:45:39 +00001057 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001058#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001059 char *cpath;
1060 char cmode[10];
1061 size_t r;
1062 r = wcstombs(cmode, mode, 10);
1063 if (r == (size_t)-1 || r >= 10) {
1064 errno = EINVAL;
1065 return NULL;
1066 }
Victor Stinner9dd76202017-12-21 16:20:32 +01001067 cpath = _Py_EncodeLocaleRaw(path, NULL);
1068 if (cpath == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001069 return NULL;
Victor Stinner9dd76202017-12-21 16:20:32 +01001070 }
Victor Stinner4e314432010-10-07 21:45:39 +00001071 f = fopen(cpath, cmode);
Victor Stinner9dd76202017-12-21 16:20:32 +01001072 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001073#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001074 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001075#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001076 if (f == NULL)
1077 return NULL;
1078 if (make_non_inheritable(fileno(f)) < 0) {
1079 fclose(f);
1080 return NULL;
1081 }
1082 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001083}
1084
Victor Stinnere42ccd22015-03-18 01:39:23 +01001085/* Wrapper to fopen().
1086
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001087 The file descriptor is created non-inheritable.
1088
1089 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001090FILE*
1091_Py_fopen(const char *pathname, const char *mode)
1092{
1093 FILE *f = fopen(pathname, mode);
1094 if (f == NULL)
1095 return NULL;
1096 if (make_non_inheritable(fileno(f)) < 0) {
1097 fclose(f);
1098 return NULL;
1099 }
1100 return f;
1101}
1102
1103/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001104 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001105
Victor Stinnere42ccd22015-03-18 01:39:23 +01001106 Return the new file object on success. Raise an exception and return NULL
1107 on error.
1108
1109 The file descriptor is created non-inheritable.
1110
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001111 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1112 except if the Python signal handler raises an exception.
1113
Victor Stinner6f4fae82015-04-01 18:34:32 +02001114 Release the GIL to call _wfopen() or fopen(). The caller must hold
1115 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001116FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001117_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001118{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001119 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001120 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001121#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001122 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001123 wchar_t wmode[10];
1124 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001125
Victor Stinnere42ccd22015-03-18 01:39:23 +01001126 assert(PyGILState_Check());
1127
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001128 if (!PyUnicode_Check(path)) {
1129 PyErr_Format(PyExc_TypeError,
1130 "str file path expected under Windows, got %R",
1131 Py_TYPE(path));
1132 return NULL;
1133 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001134 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001135 if (wpath == NULL)
1136 return NULL;
1137
Victor Stinner4e314432010-10-07 21:45:39 +00001138 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001139 if (usize == 0) {
1140 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001141 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001142 }
Victor Stinner4e314432010-10-07 21:45:39 +00001143
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001144 do {
1145 Py_BEGIN_ALLOW_THREADS
1146 f = _wfopen(wpath, wmode);
1147 Py_END_ALLOW_THREADS
1148 } while (f == NULL
1149 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001150#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001151 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001152 char *path_bytes;
1153
1154 assert(PyGILState_Check());
1155
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001156 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001157 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001158 path_bytes = PyBytes_AS_STRING(bytes);
1159
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001160 do {
1161 Py_BEGIN_ALLOW_THREADS
1162 f = fopen(path_bytes, mode);
1163 Py_END_ALLOW_THREADS
1164 } while (f == NULL
1165 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001166
Victor Stinner4e314432010-10-07 21:45:39 +00001167 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001168#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001169 if (async_err)
1170 return NULL;
1171
Victor Stinnere42ccd22015-03-18 01:39:23 +01001172 if (f == NULL) {
1173 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001174 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001175 }
1176
1177 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001178 fclose(f);
1179 return NULL;
1180 }
1181 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001182}
1183
Victor Stinner66aab0c2015-03-19 22:53:20 +01001184/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001185
1186 On success, return the number of read bytes, it can be lower than count.
1187 If the current file offset is at or past the end of file, no bytes are read,
1188 and read() returns zero.
1189
1190 On error, raise an exception, set errno and return -1.
1191
1192 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1193 If the Python signal handler raises an exception, the function returns -1
1194 (the syscall is not retried).
1195
1196 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001197Py_ssize_t
1198_Py_read(int fd, void *buf, size_t count)
1199{
1200 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001201 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001202 int async_err = 0;
1203
Victor Stinner8a1be612016-03-14 22:07:55 +01001204 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001205
Victor Stinner66aab0c2015-03-19 22:53:20 +01001206 /* _Py_read() must not be called with an exception set, otherwise the
1207 * caller may think that read() was interrupted by a signal and the signal
1208 * handler raised an exception. */
1209 assert(!PyErr_Occurred());
1210
Victor Stinner66aab0c2015-03-19 22:53:20 +01001211#ifdef MS_WINDOWS
1212 if (count > INT_MAX) {
1213 /* On Windows, the count parameter of read() is an int */
1214 count = INT_MAX;
1215 }
1216#else
1217 if (count > PY_SSIZE_T_MAX) {
1218 /* if count is greater than PY_SSIZE_T_MAX,
1219 * read() result is undefined */
1220 count = PY_SSIZE_T_MAX;
1221 }
1222#endif
1223
Steve Dower8fc89802015-04-12 00:26:27 -04001224 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001225 do {
1226 Py_BEGIN_ALLOW_THREADS
1227 errno = 0;
1228#ifdef MS_WINDOWS
1229 n = read(fd, buf, (int)count);
1230#else
1231 n = read(fd, buf, count);
1232#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001233 /* save/restore errno because PyErr_CheckSignals()
1234 * and PyErr_SetFromErrno() can modify it */
1235 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001236 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001237 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001238 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001239 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001240
1241 if (async_err) {
1242 /* read() was interrupted by a signal (failed with EINTR)
1243 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001244 errno = err;
1245 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001246 return -1;
1247 }
1248 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001249 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001250 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001251 return -1;
1252 }
1253
1254 return n;
1255}
1256
Victor Stinner82c3e452015-04-01 18:34:45 +02001257static Py_ssize_t
1258_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001259{
1260 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001261 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001262 int async_err = 0;
1263
Steve Dower8fc89802015-04-12 00:26:27 -04001264 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001265#ifdef MS_WINDOWS
1266 if (count > 32767 && isatty(fd)) {
1267 /* Issue #11395: the Windows console returns an error (12: not
1268 enough space error) on writing into stdout if stdout mode is
1269 binary and the length is greater than 66,000 bytes (or less,
1270 depending on heap usage). */
1271 count = 32767;
1272 }
1273 else if (count > INT_MAX)
1274 count = INT_MAX;
1275#else
1276 if (count > PY_SSIZE_T_MAX) {
1277 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1278 * to do it ourself to have a portable behaviour. */
1279 count = PY_SSIZE_T_MAX;
1280 }
1281#endif
1282
Victor Stinner82c3e452015-04-01 18:34:45 +02001283 if (gil_held) {
1284 do {
1285 Py_BEGIN_ALLOW_THREADS
1286 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001287#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001288 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001289#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001290 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001291#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001292 /* save/restore errno because PyErr_CheckSignals()
1293 * and PyErr_SetFromErrno() can modify it */
1294 err = errno;
1295 Py_END_ALLOW_THREADS
1296 } while (n < 0 && err == EINTR &&
1297 !(async_err = PyErr_CheckSignals()));
1298 }
1299 else {
1300 do {
1301 errno = 0;
1302#ifdef MS_WINDOWS
1303 n = write(fd, buf, (int)count);
1304#else
1305 n = write(fd, buf, count);
1306#endif
1307 err = errno;
1308 } while (n < 0 && err == EINTR);
1309 }
Steve Dower8fc89802015-04-12 00:26:27 -04001310 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001311
1312 if (async_err) {
1313 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001314 and the Python signal handler raised an exception (if gil_held is
1315 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001316 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001317 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001318 return -1;
1319 }
1320 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001321 if (gil_held)
1322 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001323 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001324 return -1;
1325 }
1326
1327 return n;
1328}
1329
Victor Stinner82c3e452015-04-01 18:34:45 +02001330/* Write count bytes of buf into fd.
1331
1332 On success, return the number of written bytes, it can be lower than count
1333 including 0. On error, raise an exception, set errno and return -1.
1334
1335 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1336 If the Python signal handler raises an exception, the function returns -1
1337 (the syscall is not retried).
1338
1339 Release the GIL to call write(). The caller must hold the GIL. */
1340Py_ssize_t
1341_Py_write(int fd, const void *buf, size_t count)
1342{
Victor Stinner8a1be612016-03-14 22:07:55 +01001343 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001344
Victor Stinner82c3e452015-04-01 18:34:45 +02001345 /* _Py_write() must not be called with an exception set, otherwise the
1346 * caller may think that write() was interrupted by a signal and the signal
1347 * handler raised an exception. */
1348 assert(!PyErr_Occurred());
1349
1350 return _Py_write_impl(fd, buf, count, 1);
1351}
1352
1353/* Write count bytes of buf into fd.
1354 *
1355 * On success, return the number of written bytes, it can be lower than count
1356 * including 0. On error, set errno and return -1.
1357 *
1358 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1359 * without calling the Python signal handler. */
1360Py_ssize_t
1361_Py_write_noraise(int fd, const void *buf, size_t count)
1362{
1363 return _Py_write_impl(fd, buf, count, 0);
1364}
1365
Victor Stinner4e314432010-10-07 21:45:39 +00001366#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001367
1368/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001369 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001370
Victor Stinner4e314432010-10-07 21:45:39 +00001371int
1372_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1373{
1374 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001375 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001376 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001377 int res;
1378 size_t r1;
1379
Victor Stinner9dd76202017-12-21 16:20:32 +01001380 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001381 if (cpath == NULL) {
1382 errno = EINVAL;
1383 return -1;
1384 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001385 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner9dd76202017-12-21 16:20:32 +01001386 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001387 if (res == -1)
1388 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001389 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001390 errno = EINVAL;
1391 return -1;
1392 }
1393 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001394 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001395 if (wbuf == NULL) {
1396 errno = EINVAL;
1397 return -1;
1398 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001399 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001400 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001401 errno = EINVAL;
1402 return -1;
1403 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001404 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001405 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001406 return (int)r1;
1407}
1408#endif
1409
1410#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001411
1412/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001413 encoding, decode the result from the locale encoding.
1414 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001415
Victor Stinner4e314432010-10-07 21:45:39 +00001416wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001417_Py_wrealpath(const wchar_t *path,
1418 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001419{
1420 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001421 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001422 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001423 char *res;
1424 size_t r;
Victor Stinner9dd76202017-12-21 16:20:32 +01001425 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001426 if (cpath == NULL) {
1427 errno = EINVAL;
1428 return NULL;
1429 }
1430 res = realpath(cpath, cresolved_path);
Victor Stinner9dd76202017-12-21 16:20:32 +01001431 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001432 if (res == NULL)
1433 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001434
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001435 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001436 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001437 errno = EINVAL;
1438 return NULL;
1439 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001440 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001441 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001442 errno = EINVAL;
1443 return NULL;
1444 }
1445 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001446 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001447 return resolved_path;
1448}
1449#endif
1450
Victor Stinnerf4061da2010-10-14 12:37:19 +00001451/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001452 including the null character. Decode the path from the locale encoding.
1453 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001454
Victor Stinner4e314432010-10-07 21:45:39 +00001455wchar_t*
1456_Py_wgetcwd(wchar_t *buf, size_t size)
1457{
1458#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001459 int isize = (int)Py_MIN(size, INT_MAX);
1460 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001461#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001462 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001463 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001464 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001465
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001466 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001467 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001468 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001469 if (wname == NULL)
1470 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001471 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001472 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001473 return NULL;
1474 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001475 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001476 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001477 return buf;
1478#endif
1479}
1480
Victor Stinnerdaf45552013-08-28 00:53:59 +02001481/* Duplicate a file descriptor. The new file descriptor is created as
1482 non-inheritable. Return a new file descriptor on success, raise an OSError
1483 exception and return -1 on error.
1484
1485 The GIL is released to call dup(). The caller must hold the GIL. */
1486int
1487_Py_dup(int fd)
1488{
1489#ifdef MS_WINDOWS
1490 HANDLE handle;
1491 DWORD ftype;
1492#endif
1493
Victor Stinner8a1be612016-03-14 22:07:55 +01001494 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001495
Victor Stinnerdaf45552013-08-28 00:53:59 +02001496#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001497 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001498 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001499 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001500 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001501 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001502 return -1;
1503 }
1504
1505 /* get the file type, ignore the error if it failed */
1506 ftype = GetFileType(handle);
1507
1508 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001509 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001510 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001511 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001512 Py_END_ALLOW_THREADS
1513 if (fd < 0) {
1514 PyErr_SetFromErrno(PyExc_OSError);
1515 return -1;
1516 }
1517
1518 /* Character files like console cannot be make non-inheritable */
1519 if (ftype != FILE_TYPE_CHAR) {
1520 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001521 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001522 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001523 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001524 return -1;
1525 }
1526 }
1527#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1528 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001529 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001530 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001531 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001532 Py_END_ALLOW_THREADS
1533 if (fd < 0) {
1534 PyErr_SetFromErrno(PyExc_OSError);
1535 return -1;
1536 }
1537
1538#else
1539 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001540 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001541 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001542 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001543 Py_END_ALLOW_THREADS
1544 if (fd < 0) {
1545 PyErr_SetFromErrno(PyExc_OSError);
1546 return -1;
1547 }
1548
1549 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001550 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001551 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001552 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001553 return -1;
1554 }
1555#endif
1556 return fd;
1557}
1558
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001559#ifndef MS_WINDOWS
1560/* Get the blocking mode of the file descriptor.
1561 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1562 raise an exception and return -1 on error. */
1563int
1564_Py_get_blocking(int fd)
1565{
Steve Dower8fc89802015-04-12 00:26:27 -04001566 int flags;
1567 _Py_BEGIN_SUPPRESS_IPH
1568 flags = fcntl(fd, F_GETFL, 0);
1569 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001570 if (flags < 0) {
1571 PyErr_SetFromErrno(PyExc_OSError);
1572 return -1;
1573 }
1574
1575 return !(flags & O_NONBLOCK);
1576}
1577
1578/* Set the blocking mode of the specified file descriptor.
1579
1580 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1581 otherwise.
1582
1583 Return 0 on success, raise an exception and return -1 on error. */
1584int
1585_Py_set_blocking(int fd, int blocking)
1586{
1587#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1588 int arg = !blocking;
1589 if (ioctl(fd, FIONBIO, &arg) < 0)
1590 goto error;
1591#else
1592 int flags, res;
1593
Steve Dower8fc89802015-04-12 00:26:27 -04001594 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001595 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001596 if (flags >= 0) {
1597 if (blocking)
1598 flags = flags & (~O_NONBLOCK);
1599 else
1600 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001601
Steve Dower8fc89802015-04-12 00:26:27 -04001602 res = fcntl(fd, F_SETFL, flags);
1603 } else {
1604 res = -1;
1605 }
1606 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001607
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001608 if (res < 0)
1609 goto error;
1610#endif
1611 return 0;
1612
1613error:
1614 PyErr_SetFromErrno(PyExc_OSError);
1615 return -1;
1616}
1617#endif