blob: 9275494e8644419ee701488f686ccd92e7de88a1 [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*
Victor Stinner2cba6b82018-01-10 22:46:15 +0100266decode_current_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
Victor Stinner2cba6b82018-01-10 22:46:15 +0100383static wchar_t*
384decode_locale(const char* arg, size_t *size, int ignore_utf8_mode)
385{
386#if defined(__APPLE__) || defined(__ANDROID__)
387 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
388#else
389 if (!ignore_utf8_mode && Py_UTF8Mode == 1) {
390 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
391 }
392
393#ifndef MS_WINDOWS
394 if (force_ascii == -1)
395 force_ascii = check_force_ascii();
396
397 if (force_ascii) {
398 /* force ASCII encoding to workaround mbstowcs() issue */
399 wchar_t *wstr = decode_ascii_surrogateescape(arg, size);
400 if (wstr == NULL) {
401 if (size != NULL) {
402 *size = (size_t)-1;
403 }
404 return NULL;
405 }
406 return wstr;
407 }
408#endif
409
410 return decode_current_locale(arg, size);
411#endif /* __APPLE__ or __ANDROID__ */
412}
413
414
Victor Stinner91106cd2017-12-13 12:29:09 +0100415/* Decode a byte string from the locale encoding with the
416 surrogateescape error handler: undecodable bytes are decoded as characters
417 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
418 character, escape the bytes using the surrogateescape error handler instead
419 of decoding them.
420
421 Return a pointer to a newly allocated wide character string, use
422 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
423 wide characters excluding the null character into *size
424
425 Return NULL on decoding error or memory allocation error. If *size* is not
426 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
427 decoding error.
428
429 Decoding errors should never happen, unless there is a bug in the C
430 library.
431
432 Use the Py_EncodeLocale() function to encode the character string back to a
433 byte string. */
434wchar_t*
435Py_DecodeLocale(const char* arg, size_t *size)
436{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100437 return decode_locale(arg, size, 0);
438}
Victor Stinner91106cd2017-12-13 12:29:09 +0100439
Victor Stinner91106cd2017-12-13 12:29:09 +0100440
Victor Stinner2cba6b82018-01-10 22:46:15 +0100441/* Similar to Py_DecodeLocale() but ignore the UTF-8 mode */
442wchar_t*
443_Py_DecodeCurrentLocale(const char* arg, size_t *size)
444{
445 return decode_locale(arg, size, 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000446}
447
Victor Stinner91106cd2017-12-13 12:29:09 +0100448
Victor Stinnerd2b02312017-12-15 23:06:17 +0100449#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100450static char*
Victor Stinner9dd76202017-12-21 16:20:32 +0100451encode_current_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
Victor Stinner91106cd2017-12-13 12:29:09 +0100452{
Victor Stinner4e314432010-10-07 21:45:39 +0000453 const size_t len = wcslen(text);
454 char *result = NULL, *bytes = NULL;
455 size_t i, size, converted;
456 wchar_t c, buf[2];
457
458 /* The function works in two steps:
459 1. compute the length of the output buffer in bytes (size)
460 2. outputs the bytes */
461 size = 0;
462 buf[1] = 0;
463 while (1) {
464 for (i=0; i < len; i++) {
465 c = text[i];
466 if (c >= 0xdc80 && c <= 0xdcff) {
467 /* UTF-8b surrogate */
468 if (bytes != NULL) {
469 *bytes++ = c - 0xdc00;
470 size--;
471 }
472 else
473 size++;
474 continue;
475 }
476 else {
477 buf[0] = c;
478 if (bytes != NULL)
479 converted = wcstombs(bytes, buf, size);
480 else
481 converted = wcstombs(NULL, buf, 0);
482 if (converted == (size_t)-1) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100483 if (raw_malloc) {
484 PyMem_RawFree(result);
485 }
486 else {
487 PyMem_Free(result);
Victor Stinner9dd76202017-12-21 16:20:32 +0100488 }
Victor Stinner2f02a512010-11-08 22:43:46 +0000489 if (error_pos != NULL)
490 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000491 return NULL;
492 }
493 if (bytes != NULL) {
494 bytes += converted;
495 size -= converted;
496 }
497 else
498 size += converted;
499 }
500 }
501 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100502 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000503 break;
504 }
505
506 size += 1; /* nul byte at the end */
Victor Stinner9dd76202017-12-21 16:20:32 +0100507 if (raw_malloc) {
508 result = PyMem_RawMalloc(size);
509 }
510 else {
511 result = PyMem_Malloc(size);
512 }
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100513 if (result == NULL) {
Victor Stinner9dd76202017-12-21 16:20:32 +0100514 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100515 *error_pos = (size_t)-1;
Victor Stinner9dd76202017-12-21 16:20:32 +0100516 }
Victor Stinner4e314432010-10-07 21:45:39 +0000517 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100518 }
Victor Stinner4e314432010-10-07 21:45:39 +0000519 bytes = result;
520 }
521 return result;
Victor Stinner91106cd2017-12-13 12:29:09 +0100522}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100523#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100524
Victor Stinner9dd76202017-12-21 16:20:32 +0100525static char*
Victor Stinner2cba6b82018-01-10 22:46:15 +0100526encode_locale(const wchar_t *text, size_t *error_pos,
527 int raw_malloc, int ignore_utf8_mode)
Victor Stinner9dd76202017-12-21 16:20:32 +0100528{
529#if defined(__APPLE__) || defined(__ANDROID__)
530 return _Py_EncodeUTF8_surrogateescape(text, error_pos, raw_malloc);
531#else /* __APPLE__ */
Victor Stinner2cba6b82018-01-10 22:46:15 +0100532 if (!ignore_utf8_mode && Py_UTF8Mode == 1) {
Victor Stinner9dd76202017-12-21 16:20:32 +0100533 return _Py_EncodeUTF8_surrogateescape(text, error_pos, raw_malloc);
534 }
535
536#ifndef MS_WINDOWS
537 if (force_ascii == -1)
538 force_ascii = check_force_ascii();
539
540 if (force_ascii)
541 return encode_ascii_surrogateescape(text, error_pos, raw_malloc);
542#endif
543
544 return encode_current_locale(text, error_pos, raw_malloc);
545#endif /* __APPLE__ or __ANDROID__ */
546}
547
Victor Stinner91106cd2017-12-13 12:29:09 +0100548/* Encode a wide character string to the locale encoding with the
549 surrogateescape error handler: surrogate characters in the range
550 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
551
552 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
553 the memory. Return NULL on encoding or memory allocation error.
554
555 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
556 to the index of the invalid character on encoding error.
557
558 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
559 character string. */
560char*
561Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
562{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100563 return encode_locale(text, error_pos, 0, 0);
Victor Stinner9dd76202017-12-21 16:20:32 +0100564}
Victor Stinner91106cd2017-12-13 12:29:09 +0100565
Victor Stinner91106cd2017-12-13 12:29:09 +0100566
Victor Stinner9dd76202017-12-21 16:20:32 +0100567/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
568 instead of PyMem_Free(). */
569char*
570_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
571{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100572 return encode_locale(text, error_pos, 1, 0);
573}
574
575
576/* Similar to _Py_EncodeLocaleRaw() but ignore the UTF-8 Mode */
577char*
578_Py_EncodeCurrentLocale(const wchar_t *text, size_t *error_pos)
579{
580 return encode_locale(text, error_pos, 1, 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000581}
582
Victor Stinner6672d0c2010-10-07 22:53:43 +0000583
Steve Dowerf2f373f2015-02-21 08:44:05 -0800584#ifdef MS_WINDOWS
585static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
586
587static void
588FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
589{
590 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
591 /* Cannot simply cast and dereference in_ptr,
592 since it might not be aligned properly */
593 __int64 in;
594 memcpy(&in, in_ptr, sizeof(in));
595 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
596 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
597}
598
599void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800600_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800601{
602 /* XXX endianness */
603 __int64 out;
604 out = time_in + secs_between_epochs;
605 out = out * 10000000 + nsec_in / 100;
606 memcpy(out_ptr, &out, sizeof(out));
607}
608
609/* Below, we *know* that ugo+r is 0444 */
610#if _S_IREAD != 0400
611#error Unsupported C library
612#endif
613static int
614attributes_to_mode(DWORD attr)
615{
616 int m = 0;
617 if (attr & FILE_ATTRIBUTE_DIRECTORY)
618 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
619 else
620 m |= _S_IFREG;
621 if (attr & FILE_ATTRIBUTE_READONLY)
622 m |= 0444;
623 else
624 m |= 0666;
625 return m;
626}
627
Steve Dowerbf1f3762015-02-21 15:26:02 -0800628void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200629_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
630 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800631{
632 memset(result, 0, sizeof(*result));
633 result->st_mode = attributes_to_mode(info->dwFileAttributes);
634 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
635 result->st_dev = info->dwVolumeSerialNumber;
636 result->st_rdev = result->st_dev;
637 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
638 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
639 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
640 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100641 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800642 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
643 /* first clear the S_IFMT bits */
644 result->st_mode ^= (result->st_mode & S_IFMT);
645 /* now set the bits that make this a symlink */
646 result->st_mode |= S_IFLNK;
647 }
648 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800649}
650#endif
651
652/* Return information about a file.
653
654 On POSIX, use fstat().
655
656 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800657 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
658 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800659 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200660
661 On Windows, set the last Windows error and return nonzero on error. On
662 POSIX, set errno and return nonzero on error. Fill status and return 0 on
663 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800664int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200665_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800666{
667#ifdef MS_WINDOWS
668 BY_HANDLE_FILE_INFORMATION info;
669 HANDLE h;
670 int type;
671
Steve Dower940f33a2016-09-08 11:21:54 -0700672 _Py_BEGIN_SUPPRESS_IPH
673 h = (HANDLE)_get_osfhandle(fd);
674 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800675
676 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400677 /* errno is already set by _get_osfhandle, but we also set
678 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800679 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800680 return -1;
681 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200682 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800683
684 type = GetFileType(h);
685 if (type == FILE_TYPE_UNKNOWN) {
686 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400687 if (error != 0) {
688 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800689 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400690 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800691 /* else: valid but unknown file */
692 }
693
694 if (type != FILE_TYPE_DISK) {
695 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200696 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800697 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200698 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800699 return 0;
700 }
701
702 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400703 /* The Win32 error is already set, but we also set errno for
704 callers who expect it */
705 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800706 return -1;
707 }
708
Victor Stinnere134a7f2015-03-30 10:09:31 +0200709 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800710 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100711 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800712 return 0;
713#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200714 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800715#endif
716}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800717
Victor Stinnere134a7f2015-03-30 10:09:31 +0200718/* Return information about a file.
719
720 On POSIX, use fstat().
721
722 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800723 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
724 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200725 #23152.
726
727 Raise an exception and return -1 on error. On Windows, set the last Windows
728 error on error. On POSIX, set errno on error. Fill status and return 0 on
729 success.
730
Victor Stinner6f4fae82015-04-01 18:34:32 +0200731 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
732 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200733int
734_Py_fstat(int fd, struct _Py_stat_struct *status)
735{
736 int res;
737
Victor Stinner8a1be612016-03-14 22:07:55 +0100738 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100739
Victor Stinnere134a7f2015-03-30 10:09:31 +0200740 Py_BEGIN_ALLOW_THREADS
741 res = _Py_fstat_noraise(fd, status);
742 Py_END_ALLOW_THREADS
743
744 if (res != 0) {
745#ifdef MS_WINDOWS
746 PyErr_SetFromWindowsErr(0);
747#else
748 PyErr_SetFromErrno(PyExc_OSError);
749#endif
750 return -1;
751 }
752 return 0;
753}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800754
Victor Stinner6672d0c2010-10-07 22:53:43 +0000755/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
756 call stat() otherwise. Only fill st_mode attribute on Windows.
757
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100758 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
759 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000760
761int
Victor Stinnera4a75952010-10-07 22:23:10 +0000762_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000763{
764#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000765 int err;
766 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300767 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000768
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300769 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100770 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100771 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300772
Victor Stinneree587ea2011-11-17 00:51:38 +0100773 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000774 if (!err)
775 statbuf->st_mode = wstatbuf.st_mode;
776 return err;
777#else
778 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300779 PyObject *bytes;
780 char *cpath;
781
782 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000783 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100784 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300785
786 /* check for embedded null bytes */
787 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
788 Py_DECREF(bytes);
789 return -2;
790 }
791
792 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000793 Py_DECREF(bytes);
794 return ret;
795#endif
796}
797
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100798
Antoine Pitrou409b5382013-10-12 22:41:17 +0200799static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200800get_inheritable(int fd, int raise)
801{
802#ifdef MS_WINDOWS
803 HANDLE handle;
804 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000805
Steve Dower8fc89802015-04-12 00:26:27 -0400806 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200807 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400808 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200809 if (handle == INVALID_HANDLE_VALUE) {
810 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700811 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200812 return -1;
813 }
814
815 if (!GetHandleInformation(handle, &flags)) {
816 if (raise)
817 PyErr_SetFromWindowsErr(0);
818 return -1;
819 }
820
821 return (flags & HANDLE_FLAG_INHERIT);
822#else
823 int flags;
824
825 flags = fcntl(fd, F_GETFD, 0);
826 if (flags == -1) {
827 if (raise)
828 PyErr_SetFromErrno(PyExc_OSError);
829 return -1;
830 }
831 return !(flags & FD_CLOEXEC);
832#endif
833}
834
835/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200836 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200837 raise an exception and return -1 on error. */
838int
839_Py_get_inheritable(int fd)
840{
841 return get_inheritable(fd, 1);
842}
843
844static int
845set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
846{
847#ifdef MS_WINDOWS
848 HANDLE handle;
849 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200850#else
851#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
852 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200853 int request;
854 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200855#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200856 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200857 int res;
858#endif
859
860 /* atomic_flag_works can only be used to make the file descriptor
861 non-inheritable */
862 assert(!(atomic_flag_works != NULL && inheritable));
863
864 if (atomic_flag_works != NULL && !inheritable) {
865 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700866 int isInheritable = get_inheritable(fd, raise);
867 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200868 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700869 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200870 }
871
872 if (*atomic_flag_works)
873 return 0;
874 }
875
876#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -0400877 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200878 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400879 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200880 if (handle == INVALID_HANDLE_VALUE) {
881 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700882 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200883 return -1;
884 }
885
886 if (inheritable)
887 flags = HANDLE_FLAG_INHERIT;
888 else
889 flags = 0;
890 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
891 if (raise)
892 PyErr_SetFromWindowsErr(0);
893 return -1;
894 }
895 return 0;
896
Victor Stinnerdaf45552013-08-28 00:53:59 +0200897#else
Victor Stinner282124b2014-09-02 11:41:04 +0200898
899#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
900 if (ioctl_works != 0) {
901 /* fast-path: ioctl() only requires one syscall */
902 if (inheritable)
903 request = FIONCLEX;
904 else
905 request = FIOCLEX;
906 err = ioctl(fd, request, NULL);
907 if (!err) {
908 ioctl_works = 1;
909 return 0;
910 }
911
Victor Stinner3116cc42016-05-19 16:46:18 +0200912 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200913 if (raise)
914 PyErr_SetFromErrno(PyExc_OSError);
915 return -1;
916 }
917 else {
918 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
919 device". The ioctl is declared but not supported by the kernel.
920 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200921 Illumos-based OS for example.
922
923 Issue #27057: When SELinux policy disallows ioctl it will fail
924 with EACCES. While FIOCLEX is safe operation it may be
925 unavailable because ioctl was denied altogether.
926 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200927 ioctl_works = 0;
928 }
929 /* fallback to fcntl() if ioctl() does not work */
930 }
931#endif
932
933 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200934 flags = fcntl(fd, F_GETFD);
935 if (flags < 0) {
936 if (raise)
937 PyErr_SetFromErrno(PyExc_OSError);
938 return -1;
939 }
940
Victor Stinnera858bbd2016-04-17 16:51:52 +0200941 if (inheritable) {
942 new_flags = flags & ~FD_CLOEXEC;
943 }
944 else {
945 new_flags = flags | FD_CLOEXEC;
946 }
947
948 if (new_flags == flags) {
949 /* FD_CLOEXEC flag already set/cleared: nothing to do */
950 return 0;
951 }
952
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +0100953 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200954 if (res < 0) {
955 if (raise)
956 PyErr_SetFromErrno(PyExc_OSError);
957 return -1;
958 }
959 return 0;
960#endif
961}
962
963/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200964 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200965static int
966make_non_inheritable(int fd)
967{
968 return set_inheritable(fd, 0, 0, NULL);
969}
970
971/* Set the inheritable flag of the specified file descriptor.
972 On success: return 0, on error: raise an exception if raise is nonzero
973 and return -1.
974
975 If atomic_flag_works is not NULL:
976
977 * if *atomic_flag_works==-1, check if the inheritable is set on the file
978 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
979 set the inheritable flag
980 * if *atomic_flag_works==1: do nothing
981 * if *atomic_flag_works==0: set inheritable flag to False
982
983 Set atomic_flag_works to NULL if no atomic flag was used to create the
984 file descriptor.
985
986 atomic_flag_works can only be used to make a file descriptor
987 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
988int
989_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
990{
991 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
992}
993
Victor Stinnera555cfc2015-03-18 00:22:14 +0100994static int
995_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200996{
997 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100998 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100999#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +02001000 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001001#endif
1002
1003#ifdef MS_WINDOWS
1004 flags |= O_NOINHERIT;
1005#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001006 atomic_flag_works = &_Py_open_cloexec_works;
1007 flags |= O_CLOEXEC;
1008#else
1009 atomic_flag_works = NULL;
1010#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001011
Victor Stinnera555cfc2015-03-18 00:22:14 +01001012 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001013 do {
1014 Py_BEGIN_ALLOW_THREADS
1015 fd = open(pathname, flags);
1016 Py_END_ALLOW_THREADS
1017 } while (fd < 0
1018 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1019 if (async_err)
1020 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001021 if (fd < 0) {
1022 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
1023 return -1;
1024 }
1025 }
1026 else {
1027 fd = open(pathname, flags);
1028 if (fd < 0)
1029 return -1;
1030 }
1031
1032#ifndef MS_WINDOWS
1033 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001034 close(fd);
1035 return -1;
1036 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001037#endif
1038
Victor Stinnerdaf45552013-08-28 00:53:59 +02001039 return fd;
1040}
1041
Victor Stinnera555cfc2015-03-18 00:22:14 +01001042/* Open a file with the specified flags (wrapper to open() function).
1043 Return a file descriptor on success. Raise an exception and return -1 on
1044 error.
1045
1046 The file descriptor is created non-inheritable.
1047
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001048 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1049 except if the Python signal handler raises an exception.
1050
Victor Stinner6f4fae82015-04-01 18:34:32 +02001051 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001052int
1053_Py_open(const char *pathname, int flags)
1054{
1055 /* _Py_open() must be called with the GIL held. */
1056 assert(PyGILState_Check());
1057 return _Py_open_impl(pathname, flags, 1);
1058}
1059
1060/* Open a file with the specified flags (wrapper to open() function).
1061 Return a file descriptor on success. Set errno and return -1 on error.
1062
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001063 The file descriptor is created non-inheritable.
1064
1065 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001066int
1067_Py_open_noraise(const char *pathname, int flags)
1068{
1069 return _Py_open_impl(pathname, flags, 0);
1070}
1071
Victor Stinnerdaf45552013-08-28 00:53:59 +02001072/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001073 encoding and use fopen() otherwise.
1074
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001075 The file descriptor is created non-inheritable.
1076
1077 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001078FILE *
1079_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1080{
Victor Stinner4e314432010-10-07 21:45:39 +00001081 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001082#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001083 char *cpath;
1084 char cmode[10];
1085 size_t r;
1086 r = wcstombs(cmode, mode, 10);
1087 if (r == (size_t)-1 || r >= 10) {
1088 errno = EINVAL;
1089 return NULL;
1090 }
Victor Stinner9dd76202017-12-21 16:20:32 +01001091 cpath = _Py_EncodeLocaleRaw(path, NULL);
1092 if (cpath == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001093 return NULL;
Victor Stinner9dd76202017-12-21 16:20:32 +01001094 }
Victor Stinner4e314432010-10-07 21:45:39 +00001095 f = fopen(cpath, cmode);
Victor Stinner9dd76202017-12-21 16:20:32 +01001096 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001097#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001098 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001099#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001100 if (f == NULL)
1101 return NULL;
1102 if (make_non_inheritable(fileno(f)) < 0) {
1103 fclose(f);
1104 return NULL;
1105 }
1106 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001107}
1108
Victor Stinnere42ccd22015-03-18 01:39:23 +01001109/* Wrapper to fopen().
1110
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001111 The file descriptor is created non-inheritable.
1112
1113 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001114FILE*
1115_Py_fopen(const char *pathname, const char *mode)
1116{
1117 FILE *f = fopen(pathname, mode);
1118 if (f == NULL)
1119 return NULL;
1120 if (make_non_inheritable(fileno(f)) < 0) {
1121 fclose(f);
1122 return NULL;
1123 }
1124 return f;
1125}
1126
1127/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001128 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001129
Victor Stinnere42ccd22015-03-18 01:39:23 +01001130 Return the new file object on success. Raise an exception and return NULL
1131 on error.
1132
1133 The file descriptor is created non-inheritable.
1134
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001135 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1136 except if the Python signal handler raises an exception.
1137
Victor Stinner6f4fae82015-04-01 18:34:32 +02001138 Release the GIL to call _wfopen() or fopen(). The caller must hold
1139 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001140FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001141_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001142{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001143 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001144 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001145#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001146 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001147 wchar_t wmode[10];
1148 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001149
Victor Stinnere42ccd22015-03-18 01:39:23 +01001150 assert(PyGILState_Check());
1151
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001152 if (!PyUnicode_Check(path)) {
1153 PyErr_Format(PyExc_TypeError,
1154 "str file path expected under Windows, got %R",
1155 Py_TYPE(path));
1156 return NULL;
1157 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001158 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001159 if (wpath == NULL)
1160 return NULL;
1161
Victor Stinner4e314432010-10-07 21:45:39 +00001162 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001163 if (usize == 0) {
1164 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001165 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001166 }
Victor Stinner4e314432010-10-07 21:45:39 +00001167
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001168 do {
1169 Py_BEGIN_ALLOW_THREADS
1170 f = _wfopen(wpath, wmode);
1171 Py_END_ALLOW_THREADS
1172 } while (f == NULL
1173 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001174#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001175 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001176 char *path_bytes;
1177
1178 assert(PyGILState_Check());
1179
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001180 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001181 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001182 path_bytes = PyBytes_AS_STRING(bytes);
1183
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001184 do {
1185 Py_BEGIN_ALLOW_THREADS
1186 f = fopen(path_bytes, mode);
1187 Py_END_ALLOW_THREADS
1188 } while (f == NULL
1189 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001190
Victor Stinner4e314432010-10-07 21:45:39 +00001191 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001192#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001193 if (async_err)
1194 return NULL;
1195
Victor Stinnere42ccd22015-03-18 01:39:23 +01001196 if (f == NULL) {
1197 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001198 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001199 }
1200
1201 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001202 fclose(f);
1203 return NULL;
1204 }
1205 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001206}
1207
Victor Stinner66aab0c2015-03-19 22:53:20 +01001208/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001209
1210 On success, return the number of read bytes, it can be lower than count.
1211 If the current file offset is at or past the end of file, no bytes are read,
1212 and read() returns zero.
1213
1214 On error, raise an exception, set errno and return -1.
1215
1216 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1217 If the Python signal handler raises an exception, the function returns -1
1218 (the syscall is not retried).
1219
1220 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001221Py_ssize_t
1222_Py_read(int fd, void *buf, size_t count)
1223{
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
Victor Stinner8a1be612016-03-14 22:07:55 +01001228 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001229
Victor Stinner66aab0c2015-03-19 22:53:20 +01001230 /* _Py_read() must not be called with an exception set, otherwise the
1231 * caller may think that read() was interrupted by a signal and the signal
1232 * handler raised an exception. */
1233 assert(!PyErr_Occurred());
1234
Victor Stinner66aab0c2015-03-19 22:53:20 +01001235#ifdef MS_WINDOWS
1236 if (count > INT_MAX) {
1237 /* On Windows, the count parameter of read() is an int */
1238 count = INT_MAX;
1239 }
1240#else
1241 if (count > PY_SSIZE_T_MAX) {
1242 /* if count is greater than PY_SSIZE_T_MAX,
1243 * read() result is undefined */
1244 count = PY_SSIZE_T_MAX;
1245 }
1246#endif
1247
Steve Dower8fc89802015-04-12 00:26:27 -04001248 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001249 do {
1250 Py_BEGIN_ALLOW_THREADS
1251 errno = 0;
1252#ifdef MS_WINDOWS
1253 n = read(fd, buf, (int)count);
1254#else
1255 n = read(fd, buf, count);
1256#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001257 /* save/restore errno because PyErr_CheckSignals()
1258 * and PyErr_SetFromErrno() can modify it */
1259 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001260 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001261 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001262 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001263 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001264
1265 if (async_err) {
1266 /* read() was interrupted by a signal (failed with EINTR)
1267 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001268 errno = err;
1269 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001270 return -1;
1271 }
1272 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001273 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001274 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001275 return -1;
1276 }
1277
1278 return n;
1279}
1280
Victor Stinner82c3e452015-04-01 18:34:45 +02001281static Py_ssize_t
1282_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001283{
1284 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001285 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001286 int async_err = 0;
1287
Steve Dower8fc89802015-04-12 00:26:27 -04001288 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001289#ifdef MS_WINDOWS
1290 if (count > 32767 && isatty(fd)) {
1291 /* Issue #11395: the Windows console returns an error (12: not
1292 enough space error) on writing into stdout if stdout mode is
1293 binary and the length is greater than 66,000 bytes (or less,
1294 depending on heap usage). */
1295 count = 32767;
1296 }
1297 else if (count > INT_MAX)
1298 count = INT_MAX;
1299#else
1300 if (count > PY_SSIZE_T_MAX) {
1301 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1302 * to do it ourself to have a portable behaviour. */
1303 count = PY_SSIZE_T_MAX;
1304 }
1305#endif
1306
Victor Stinner82c3e452015-04-01 18:34:45 +02001307 if (gil_held) {
1308 do {
1309 Py_BEGIN_ALLOW_THREADS
1310 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001311#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001312 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001313#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001314 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001315#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001316 /* save/restore errno because PyErr_CheckSignals()
1317 * and PyErr_SetFromErrno() can modify it */
1318 err = errno;
1319 Py_END_ALLOW_THREADS
1320 } while (n < 0 && err == EINTR &&
1321 !(async_err = PyErr_CheckSignals()));
1322 }
1323 else {
1324 do {
1325 errno = 0;
1326#ifdef MS_WINDOWS
1327 n = write(fd, buf, (int)count);
1328#else
1329 n = write(fd, buf, count);
1330#endif
1331 err = errno;
1332 } while (n < 0 && err == EINTR);
1333 }
Steve Dower8fc89802015-04-12 00:26:27 -04001334 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001335
1336 if (async_err) {
1337 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001338 and the Python signal handler raised an exception (if gil_held is
1339 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001340 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001341 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001342 return -1;
1343 }
1344 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001345 if (gil_held)
1346 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001347 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001348 return -1;
1349 }
1350
1351 return n;
1352}
1353
Victor Stinner82c3e452015-04-01 18:34:45 +02001354/* Write count bytes of buf into fd.
1355
1356 On success, return the number of written bytes, it can be lower than count
1357 including 0. On error, raise an exception, set errno and return -1.
1358
1359 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1360 If the Python signal handler raises an exception, the function returns -1
1361 (the syscall is not retried).
1362
1363 Release the GIL to call write(). The caller must hold the GIL. */
1364Py_ssize_t
1365_Py_write(int fd, const void *buf, size_t count)
1366{
Victor Stinner8a1be612016-03-14 22:07:55 +01001367 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001368
Victor Stinner82c3e452015-04-01 18:34:45 +02001369 /* _Py_write() must not be called with an exception set, otherwise the
1370 * caller may think that write() was interrupted by a signal and the signal
1371 * handler raised an exception. */
1372 assert(!PyErr_Occurred());
1373
1374 return _Py_write_impl(fd, buf, count, 1);
1375}
1376
1377/* Write count bytes of buf into fd.
1378 *
1379 * On success, return the number of written bytes, it can be lower than count
1380 * including 0. On error, set errno and return -1.
1381 *
1382 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1383 * without calling the Python signal handler. */
1384Py_ssize_t
1385_Py_write_noraise(int fd, const void *buf, size_t count)
1386{
1387 return _Py_write_impl(fd, buf, count, 0);
1388}
1389
Victor Stinner4e314432010-10-07 21:45:39 +00001390#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001391
1392/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001393 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001394
Victor Stinner4e314432010-10-07 21:45:39 +00001395int
1396_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1397{
1398 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001399 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001400 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001401 int res;
1402 size_t r1;
1403
Victor Stinner9dd76202017-12-21 16:20:32 +01001404 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001405 if (cpath == NULL) {
1406 errno = EINVAL;
1407 return -1;
1408 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001409 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner9dd76202017-12-21 16:20:32 +01001410 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001411 if (res == -1)
1412 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001413 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001414 errno = EINVAL;
1415 return -1;
1416 }
1417 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001418 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001419 if (wbuf == NULL) {
1420 errno = EINVAL;
1421 return -1;
1422 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001423 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001424 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001425 errno = EINVAL;
1426 return -1;
1427 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001428 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001429 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001430 return (int)r1;
1431}
1432#endif
1433
1434#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001435
1436/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001437 encoding, decode the result from the locale encoding.
1438 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001439
Victor Stinner4e314432010-10-07 21:45:39 +00001440wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001441_Py_wrealpath(const wchar_t *path,
1442 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001443{
1444 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001445 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001446 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001447 char *res;
1448 size_t r;
Victor Stinner9dd76202017-12-21 16:20:32 +01001449 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001450 if (cpath == NULL) {
1451 errno = EINVAL;
1452 return NULL;
1453 }
1454 res = realpath(cpath, cresolved_path);
Victor Stinner9dd76202017-12-21 16:20:32 +01001455 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001456 if (res == NULL)
1457 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001458
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001459 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001460 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001461 errno = EINVAL;
1462 return NULL;
1463 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001464 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001465 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001466 errno = EINVAL;
1467 return NULL;
1468 }
1469 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001470 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001471 return resolved_path;
1472}
1473#endif
1474
Victor Stinnerf4061da2010-10-14 12:37:19 +00001475/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001476 including the null character. Decode the path from the locale encoding.
1477 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001478
Victor Stinner4e314432010-10-07 21:45:39 +00001479wchar_t*
1480_Py_wgetcwd(wchar_t *buf, size_t size)
1481{
1482#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001483 int isize = (int)Py_MIN(size, INT_MAX);
1484 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001485#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001486 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001487 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001488 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001489
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001490 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001491 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001492 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001493 if (wname == NULL)
1494 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001495 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001496 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001497 return NULL;
1498 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001499 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001500 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001501 return buf;
1502#endif
1503}
1504
Victor Stinnerdaf45552013-08-28 00:53:59 +02001505/* Duplicate a file descriptor. The new file descriptor is created as
1506 non-inheritable. Return a new file descriptor on success, raise an OSError
1507 exception and return -1 on error.
1508
1509 The GIL is released to call dup(). The caller must hold the GIL. */
1510int
1511_Py_dup(int fd)
1512{
1513#ifdef MS_WINDOWS
1514 HANDLE handle;
1515 DWORD ftype;
1516#endif
1517
Victor Stinner8a1be612016-03-14 22:07:55 +01001518 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001519
Victor Stinnerdaf45552013-08-28 00:53:59 +02001520#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001521 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001522 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001523 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001524 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001525 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001526 return -1;
1527 }
1528
1529 /* get the file type, ignore the error if it failed */
1530 ftype = GetFileType(handle);
1531
1532 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001533 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001534 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001535 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001536 Py_END_ALLOW_THREADS
1537 if (fd < 0) {
1538 PyErr_SetFromErrno(PyExc_OSError);
1539 return -1;
1540 }
1541
1542 /* Character files like console cannot be make non-inheritable */
1543 if (ftype != FILE_TYPE_CHAR) {
1544 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001545 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001546 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001547 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001548 return -1;
1549 }
1550 }
1551#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1552 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001553 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001554 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001555 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001556 Py_END_ALLOW_THREADS
1557 if (fd < 0) {
1558 PyErr_SetFromErrno(PyExc_OSError);
1559 return -1;
1560 }
1561
1562#else
1563 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001564 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001565 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001566 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001567 Py_END_ALLOW_THREADS
1568 if (fd < 0) {
1569 PyErr_SetFromErrno(PyExc_OSError);
1570 return -1;
1571 }
1572
1573 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001574 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001575 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001576 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001577 return -1;
1578 }
1579#endif
1580 return fd;
1581}
1582
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001583#ifndef MS_WINDOWS
1584/* Get the blocking mode of the file descriptor.
1585 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1586 raise an exception and return -1 on error. */
1587int
1588_Py_get_blocking(int fd)
1589{
Steve Dower8fc89802015-04-12 00:26:27 -04001590 int flags;
1591 _Py_BEGIN_SUPPRESS_IPH
1592 flags = fcntl(fd, F_GETFL, 0);
1593 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001594 if (flags < 0) {
1595 PyErr_SetFromErrno(PyExc_OSError);
1596 return -1;
1597 }
1598
1599 return !(flags & O_NONBLOCK);
1600}
1601
1602/* Set the blocking mode of the specified file descriptor.
1603
1604 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1605 otherwise.
1606
1607 Return 0 on success, raise an exception and return -1 on error. */
1608int
1609_Py_set_blocking(int fd, int blocking)
1610{
1611#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1612 int arg = !blocking;
1613 if (ioctl(fd, FIONBIO, &arg) < 0)
1614 goto error;
1615#else
1616 int flags, res;
1617
Steve Dower8fc89802015-04-12 00:26:27 -04001618 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001619 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001620 if (flags >= 0) {
1621 if (blocking)
1622 flags = flags & (~O_NONBLOCK);
1623 else
1624 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001625
Steve Dower8fc89802015-04-12 00:26:27 -04001626 res = fcntl(fd, F_SETFL, flags);
1627 } else {
1628 res = -1;
1629 }
1630 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001631
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001632 if (res < 0)
1633 goto error;
1634#endif
1635 return 0;
1636
1637error:
1638 PyErr_SetFromErrno(PyExc_OSError);
1639 return -1;
1640}
1641#endif