blob: c4d495d0d63550336029353049ccbe8bc39985a0 [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01002#include "osdefs.h"
Stefan Krah6c01e382014-01-20 15:31:08 +01003#include <locale.h>
4
Victor Stinnerb306d752010-10-07 22:09:40 +00005#ifdef MS_WINDOWS
Steve Dowerd81431f2015-03-06 14:47:02 -08006# include <malloc.h>
Victor Stinnerb306d752010-10-07 22:09:40 +00007# include <windows.h>
Steve Dower8fc89802015-04-12 00:26:27 -04008extern int winerror_to_errno(int);
Victor Stinnerb306d752010-10-07 22:09:40 +00009#endif
Victor Stinner4e314432010-10-07 21:45:39 +000010
Brett Cannonefb00c02012-02-29 18:31:31 -050011#ifdef HAVE_LANGINFO_H
12#include <langinfo.h>
13#endif
14
Victor Stinnerdaf45552013-08-28 00:53:59 +020015#ifdef HAVE_SYS_IOCTL_H
16#include <sys/ioctl.h>
17#endif
18
19#ifdef HAVE_FCNTL_H
20#include <fcntl.h>
21#endif /* HAVE_FCNTL_H */
22
Victor Stinner91106cd2017-12-13 12:29:09 +010023extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size,
24 size_t *p_wlen);
Victor 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*
184encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
185{
186 char *result = NULL, *out;
187 size_t len, i;
188 wchar_t ch;
189
190 if (error_pos != NULL)
191 *error_pos = (size_t)-1;
192
193 len = wcslen(text);
194
195 result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
196 if (result == NULL)
197 return NULL;
198
199 out = result;
200 for (i=0; i<len; i++) {
201 ch = text[i];
202
203 if (ch <= 0x7f) {
204 /* ASCII character */
205 *out++ = (char)ch;
206 }
207 else if (0xdc80 <= ch && ch <= 0xdcff) {
208 /* UTF-8b surrogate */
209 *out++ = (char)(ch - 0xdc00);
210 }
211 else {
212 if (error_pos != NULL)
213 *error_pos = i;
214 PyMem_Free(result);
215 return NULL;
216 }
217 }
218 *out = '\0';
219 return result;
220}
221#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
222
223#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
224static wchar_t*
225decode_ascii_surrogateescape(const char *arg, size_t *size)
226{
227 wchar_t *res;
228 unsigned char *in;
229 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600230 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100231
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600232 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
233 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600234 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100235 if (!res)
236 return NULL;
237
238 in = (unsigned char*)arg;
239 out = res;
240 while(*in)
241 if(*in < 128)
242 *out++ = *in++;
243 else
244 *out++ = 0xdc00 + *in++;
245 *out = 0;
246 if (size != NULL)
247 *size = out - res;
248 return res;
249}
250#endif
251
Victor Stinnerd2b02312017-12-15 23:06:17 +0100252#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100253static wchar_t*
254decode_locale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000255{
256 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100257 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000258 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200259#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000260 unsigned char *in;
261 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000262 mbstate_t mbs;
263#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100264
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100265#ifdef HAVE_BROKEN_MBSTOWCS
266 /* Some platforms have a broken implementation of
267 * mbstowcs which does not count the characters that
268 * would result from conversion. Use an upper bound.
269 */
270 argsize = strlen(arg);
271#else
272 argsize = mbstowcs(NULL, arg, 0);
273#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000274 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600275 if (argsize == PY_SSIZE_T_MAX)
276 goto oom;
277 argsize += 1;
278 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
279 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600280 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000281 if (!res)
282 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600283 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000284 if (count != (size_t)-1) {
285 wchar_t *tmp;
286 /* Only use the result if it contains no
287 surrogate characters. */
288 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100289 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000290 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000291 if (*tmp == 0) {
292 if (size != NULL)
293 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000294 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000295 }
Victor Stinner4e314432010-10-07 21:45:39 +0000296 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200297 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000298 }
299 /* Conversion failed. Fall back to escaping with surrogateescape. */
300#ifdef HAVE_MBRTOWC
301 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
302
303 /* Overallocate; as multi-byte characters are in the argument, the
304 actual output could use less memory. */
305 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600306 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
307 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200308 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000309 if (!res)
310 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000311 in = (unsigned char*)arg;
312 out = res;
313 memset(&mbs, 0, sizeof mbs);
314 while (argsize) {
315 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
316 if (converted == 0)
317 /* Reached end of string; null char stored. */
318 break;
319 if (converted == (size_t)-2) {
320 /* Incomplete character. This should never happen,
321 since we provide everything that we have -
322 unless there is a bug in the C library, or I
323 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200324 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100325 if (size != NULL)
326 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000327 return NULL;
328 }
329 if (converted == (size_t)-1) {
330 /* Conversion error. Escape as UTF-8b, and start over
331 in the initial shift state. */
332 *out++ = 0xdc00 + *in++;
333 argsize--;
334 memset(&mbs, 0, sizeof mbs);
335 continue;
336 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100337 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000338 /* Surrogate character. Escape the original
339 byte sequence with surrogateescape. */
340 argsize -= converted;
341 while (converted--)
342 *out++ = 0xdc00 + *in++;
343 continue;
344 }
345 /* successfully converted some bytes */
346 in += converted;
347 argsize -= converted;
348 out++;
349 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100350 if (size != NULL)
351 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100352#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000353 /* Cannot use C locale for escaping; manually escape as if charset
354 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
355 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100356 res = decode_ascii_surrogateescape(arg, size);
357 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100358 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100359#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000360 return res;
Victor Stinner91106cd2017-12-13 12:29:09 +0100361
Victor Stinner4e314432010-10-07 21:45:39 +0000362oom:
Victor Stinner91106cd2017-12-13 12:29:09 +0100363 if (size != NULL) {
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100364 *size = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100365 }
Victor Stinner4e314432010-10-07 21:45:39 +0000366 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100367}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100368#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100369
370
371/* Decode a byte string from the locale encoding with the
372 surrogateescape error handler: undecodable bytes are decoded as characters
373 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
374 character, escape the bytes using the surrogateescape error handler instead
375 of decoding them.
376
377 Return a pointer to a newly allocated wide character string, use
378 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
379 wide characters excluding the null character into *size
380
381 Return NULL on decoding error or memory allocation error. If *size* is not
382 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
383 decoding error.
384
385 Decoding errors should never happen, unless there is a bug in the C
386 library.
387
388 Use the Py_EncodeLocale() function to encode the character string back to a
389 byte string. */
390wchar_t*
391Py_DecodeLocale(const char* arg, size_t *size)
392{
393#if defined(__APPLE__) || defined(__ANDROID__)
394 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
395#else
Victor Stinner94540602017-12-16 04:54:22 +0100396 if (Py_UTF8Mode == 1) {
Victor Stinner91106cd2017-12-13 12:29:09 +0100397 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
398 }
399
400#ifndef MS_WINDOWS
401 if (force_ascii == -1)
402 force_ascii = check_force_ascii();
403
404 if (force_ascii) {
405 /* force ASCII encoding to workaround mbstowcs() issue */
406 wchar_t *wstr = decode_ascii_surrogateescape(arg, size);
407 if (wstr == NULL) {
408 if (size != NULL) {
409 *size = (size_t)-1;
410 }
411 return NULL;
412 }
413 return wstr;
414 }
415#endif
416
417 return decode_locale(arg, size);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100418#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000419}
420
Victor Stinner91106cd2017-12-13 12:29:09 +0100421static char*
422_Py_EncodeLocaleUTF8(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000423{
Victor Stinnere2623772012-11-12 23:04:02 +0100424 Py_ssize_t len;
425 PyObject *unicode, *bytes = NULL;
426 char *cpath;
427
428 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner91106cd2017-12-13 12:29:09 +0100429 if (unicode == NULL) {
Victor Stinnere2623772012-11-12 23:04:02 +0100430 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100431 }
Victor Stinnere2623772012-11-12 23:04:02 +0100432
433 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
434 Py_DECREF(unicode);
435 if (bytes == NULL) {
436 PyErr_Clear();
Victor Stinner91106cd2017-12-13 12:29:09 +0100437 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100438 *error_pos = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100439 }
Victor Stinnere2623772012-11-12 23:04:02 +0100440 return NULL;
441 }
442
443 len = PyBytes_GET_SIZE(bytes);
444 cpath = PyMem_Malloc(len+1);
445 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100446 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100447 Py_DECREF(bytes);
Victor Stinner91106cd2017-12-13 12:29:09 +0100448 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100449 *error_pos = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100450 }
Victor Stinnere2623772012-11-12 23:04:02 +0100451 return NULL;
452 }
453 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
454 Py_DECREF(bytes);
455 return cpath;
Victor Stinner91106cd2017-12-13 12:29:09 +0100456}
457
Victor Stinnerd2b02312017-12-15 23:06:17 +0100458#if !defined(__APPLE__) && !defined(__ANDROID__)
Victor Stinner91106cd2017-12-13 12:29:09 +0100459static char*
460encode_locale(const wchar_t *text, size_t *error_pos)
461{
Victor Stinner4e314432010-10-07 21:45:39 +0000462 const size_t len = wcslen(text);
463 char *result = NULL, *bytes = NULL;
464 size_t i, size, converted;
465 wchar_t c, buf[2];
466
467 /* The function works in two steps:
468 1. compute the length of the output buffer in bytes (size)
469 2. outputs the bytes */
470 size = 0;
471 buf[1] = 0;
472 while (1) {
473 for (i=0; i < len; i++) {
474 c = text[i];
475 if (c >= 0xdc80 && c <= 0xdcff) {
476 /* UTF-8b surrogate */
477 if (bytes != NULL) {
478 *bytes++ = c - 0xdc00;
479 size--;
480 }
481 else
482 size++;
483 continue;
484 }
485 else {
486 buf[0] = c;
487 if (bytes != NULL)
488 converted = wcstombs(bytes, buf, size);
489 else
490 converted = wcstombs(NULL, buf, 0);
491 if (converted == (size_t)-1) {
492 if (result != NULL)
493 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000494 if (error_pos != NULL)
495 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000496 return NULL;
497 }
498 if (bytes != NULL) {
499 bytes += converted;
500 size -= converted;
501 }
502 else
503 size += converted;
504 }
505 }
506 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100507 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000508 break;
509 }
510
511 size += 1; /* nul byte at the end */
512 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100513 if (result == NULL) {
514 if (error_pos != NULL)
515 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000516 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100517 }
Victor Stinner4e314432010-10-07 21:45:39 +0000518 bytes = result;
519 }
520 return result;
Victor Stinner91106cd2017-12-13 12:29:09 +0100521}
Victor Stinnerd2b02312017-12-15 23:06:17 +0100522#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100523
524/* Encode a wide character string to the locale encoding with the
525 surrogateescape error handler: surrogate characters in the range
526 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
527
528 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
529 the memory. Return NULL on encoding or memory allocation error.
530
531 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
532 to the index of the invalid character on encoding error.
533
534 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
535 character string. */
536char*
537Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
538{
539#if defined(__APPLE__) || defined(__ANDROID__)
540 return _Py_EncodeLocaleUTF8(text, error_pos);
541#else /* __APPLE__ */
Victor Stinner94540602017-12-16 04:54:22 +0100542 if (Py_UTF8Mode == 1) {
Victor Stinner91106cd2017-12-13 12:29:09 +0100543 return _Py_EncodeLocaleUTF8(text, error_pos);
544 }
545
546#ifndef MS_WINDOWS
547 if (force_ascii == -1)
548 force_ascii = check_force_ascii();
549
550 if (force_ascii)
551 return encode_ascii_surrogateescape(text, error_pos);
552#endif
553
554 return encode_locale(text, error_pos);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100555#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000556}
557
Victor Stinner6672d0c2010-10-07 22:53:43 +0000558
Steve Dowerf2f373f2015-02-21 08:44:05 -0800559#ifdef MS_WINDOWS
560static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
561
562static void
563FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
564{
565 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
566 /* Cannot simply cast and dereference in_ptr,
567 since it might not be aligned properly */
568 __int64 in;
569 memcpy(&in, in_ptr, sizeof(in));
570 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
571 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
572}
573
574void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800575_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800576{
577 /* XXX endianness */
578 __int64 out;
579 out = time_in + secs_between_epochs;
580 out = out * 10000000 + nsec_in / 100;
581 memcpy(out_ptr, &out, sizeof(out));
582}
583
584/* Below, we *know* that ugo+r is 0444 */
585#if _S_IREAD != 0400
586#error Unsupported C library
587#endif
588static int
589attributes_to_mode(DWORD attr)
590{
591 int m = 0;
592 if (attr & FILE_ATTRIBUTE_DIRECTORY)
593 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
594 else
595 m |= _S_IFREG;
596 if (attr & FILE_ATTRIBUTE_READONLY)
597 m |= 0444;
598 else
599 m |= 0666;
600 return m;
601}
602
Steve Dowerbf1f3762015-02-21 15:26:02 -0800603void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200604_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
605 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800606{
607 memset(result, 0, sizeof(*result));
608 result->st_mode = attributes_to_mode(info->dwFileAttributes);
609 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
610 result->st_dev = info->dwVolumeSerialNumber;
611 result->st_rdev = result->st_dev;
612 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
613 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
614 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
615 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100616 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800617 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
618 /* first clear the S_IFMT bits */
619 result->st_mode ^= (result->st_mode & S_IFMT);
620 /* now set the bits that make this a symlink */
621 result->st_mode |= S_IFLNK;
622 }
623 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800624}
625#endif
626
627/* Return information about a file.
628
629 On POSIX, use fstat().
630
631 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800632 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
633 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800634 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200635
636 On Windows, set the last Windows error and return nonzero on error. On
637 POSIX, set errno and return nonzero on error. Fill status and return 0 on
638 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800639int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200640_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800641{
642#ifdef MS_WINDOWS
643 BY_HANDLE_FILE_INFORMATION info;
644 HANDLE h;
645 int type;
646
Steve Dower940f33a2016-09-08 11:21:54 -0700647 _Py_BEGIN_SUPPRESS_IPH
648 h = (HANDLE)_get_osfhandle(fd);
649 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800650
651 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400652 /* errno is already set by _get_osfhandle, but we also set
653 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800654 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800655 return -1;
656 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200657 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800658
659 type = GetFileType(h);
660 if (type == FILE_TYPE_UNKNOWN) {
661 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400662 if (error != 0) {
663 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800664 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400665 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800666 /* else: valid but unknown file */
667 }
668
669 if (type != FILE_TYPE_DISK) {
670 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200671 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800672 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200673 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800674 return 0;
675 }
676
677 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400678 /* The Win32 error is already set, but we also set errno for
679 callers who expect it */
680 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800681 return -1;
682 }
683
Victor Stinnere134a7f2015-03-30 10:09:31 +0200684 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800685 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100686 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800687 return 0;
688#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200689 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800690#endif
691}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800692
Victor Stinnere134a7f2015-03-30 10:09:31 +0200693/* Return information about a file.
694
695 On POSIX, use fstat().
696
697 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800698 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
699 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200700 #23152.
701
702 Raise an exception and return -1 on error. On Windows, set the last Windows
703 error on error. On POSIX, set errno on error. Fill status and return 0 on
704 success.
705
Victor Stinner6f4fae82015-04-01 18:34:32 +0200706 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
707 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200708int
709_Py_fstat(int fd, struct _Py_stat_struct *status)
710{
711 int res;
712
Victor Stinner8a1be612016-03-14 22:07:55 +0100713 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100714
Victor Stinnere134a7f2015-03-30 10:09:31 +0200715 Py_BEGIN_ALLOW_THREADS
716 res = _Py_fstat_noraise(fd, status);
717 Py_END_ALLOW_THREADS
718
719 if (res != 0) {
720#ifdef MS_WINDOWS
721 PyErr_SetFromWindowsErr(0);
722#else
723 PyErr_SetFromErrno(PyExc_OSError);
724#endif
725 return -1;
726 }
727 return 0;
728}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800729
Victor Stinner6672d0c2010-10-07 22:53:43 +0000730/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
731 call stat() otherwise. Only fill st_mode attribute on Windows.
732
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100733 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
734 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000735
736int
Victor Stinnera4a75952010-10-07 22:23:10 +0000737_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000738{
739#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000740 int err;
741 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300742 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000743
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300744 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100745 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100746 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300747
Victor Stinneree587ea2011-11-17 00:51:38 +0100748 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000749 if (!err)
750 statbuf->st_mode = wstatbuf.st_mode;
751 return err;
752#else
753 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300754 PyObject *bytes;
755 char *cpath;
756
757 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000758 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100759 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300760
761 /* check for embedded null bytes */
762 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
763 Py_DECREF(bytes);
764 return -2;
765 }
766
767 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000768 Py_DECREF(bytes);
769 return ret;
770#endif
771}
772
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100773
Antoine Pitrou409b5382013-10-12 22:41:17 +0200774static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200775get_inheritable(int fd, int raise)
776{
777#ifdef MS_WINDOWS
778 HANDLE handle;
779 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000780
Steve Dower8fc89802015-04-12 00:26:27 -0400781 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200782 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400783 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200784 if (handle == INVALID_HANDLE_VALUE) {
785 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700786 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200787 return -1;
788 }
789
790 if (!GetHandleInformation(handle, &flags)) {
791 if (raise)
792 PyErr_SetFromWindowsErr(0);
793 return -1;
794 }
795
796 return (flags & HANDLE_FLAG_INHERIT);
797#else
798 int flags;
799
800 flags = fcntl(fd, F_GETFD, 0);
801 if (flags == -1) {
802 if (raise)
803 PyErr_SetFromErrno(PyExc_OSError);
804 return -1;
805 }
806 return !(flags & FD_CLOEXEC);
807#endif
808}
809
810/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200811 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200812 raise an exception and return -1 on error. */
813int
814_Py_get_inheritable(int fd)
815{
816 return get_inheritable(fd, 1);
817}
818
819static int
820set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
821{
822#ifdef MS_WINDOWS
823 HANDLE handle;
824 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200825#else
826#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
827 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200828 int request;
829 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200830#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200831 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200832 int res;
833#endif
834
835 /* atomic_flag_works can only be used to make the file descriptor
836 non-inheritable */
837 assert(!(atomic_flag_works != NULL && inheritable));
838
839 if (atomic_flag_works != NULL && !inheritable) {
840 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700841 int isInheritable = get_inheritable(fd, raise);
842 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200843 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700844 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200845 }
846
847 if (*atomic_flag_works)
848 return 0;
849 }
850
851#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -0400852 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200853 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400854 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200855 if (handle == INVALID_HANDLE_VALUE) {
856 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700857 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200858 return -1;
859 }
860
861 if (inheritable)
862 flags = HANDLE_FLAG_INHERIT;
863 else
864 flags = 0;
865 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
866 if (raise)
867 PyErr_SetFromWindowsErr(0);
868 return -1;
869 }
870 return 0;
871
Victor Stinnerdaf45552013-08-28 00:53:59 +0200872#else
Victor Stinner282124b2014-09-02 11:41:04 +0200873
874#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
875 if (ioctl_works != 0) {
876 /* fast-path: ioctl() only requires one syscall */
877 if (inheritable)
878 request = FIONCLEX;
879 else
880 request = FIOCLEX;
881 err = ioctl(fd, request, NULL);
882 if (!err) {
883 ioctl_works = 1;
884 return 0;
885 }
886
Victor Stinner3116cc42016-05-19 16:46:18 +0200887 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200888 if (raise)
889 PyErr_SetFromErrno(PyExc_OSError);
890 return -1;
891 }
892 else {
893 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
894 device". The ioctl is declared but not supported by the kernel.
895 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200896 Illumos-based OS for example.
897
898 Issue #27057: When SELinux policy disallows ioctl it will fail
899 with EACCES. While FIOCLEX is safe operation it may be
900 unavailable because ioctl was denied altogether.
901 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200902 ioctl_works = 0;
903 }
904 /* fallback to fcntl() if ioctl() does not work */
905 }
906#endif
907
908 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200909 flags = fcntl(fd, F_GETFD);
910 if (flags < 0) {
911 if (raise)
912 PyErr_SetFromErrno(PyExc_OSError);
913 return -1;
914 }
915
Victor Stinnera858bbd2016-04-17 16:51:52 +0200916 if (inheritable) {
917 new_flags = flags & ~FD_CLOEXEC;
918 }
919 else {
920 new_flags = flags | FD_CLOEXEC;
921 }
922
923 if (new_flags == flags) {
924 /* FD_CLOEXEC flag already set/cleared: nothing to do */
925 return 0;
926 }
927
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +0100928 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200929 if (res < 0) {
930 if (raise)
931 PyErr_SetFromErrno(PyExc_OSError);
932 return -1;
933 }
934 return 0;
935#endif
936}
937
938/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200939 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200940static int
941make_non_inheritable(int fd)
942{
943 return set_inheritable(fd, 0, 0, NULL);
944}
945
946/* Set the inheritable flag of the specified file descriptor.
947 On success: return 0, on error: raise an exception if raise is nonzero
948 and return -1.
949
950 If atomic_flag_works is not NULL:
951
952 * if *atomic_flag_works==-1, check if the inheritable is set on the file
953 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
954 set the inheritable flag
955 * if *atomic_flag_works==1: do nothing
956 * if *atomic_flag_works==0: set inheritable flag to False
957
958 Set atomic_flag_works to NULL if no atomic flag was used to create the
959 file descriptor.
960
961 atomic_flag_works can only be used to make a file descriptor
962 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
963int
964_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
965{
966 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
967}
968
Victor Stinnera555cfc2015-03-18 00:22:14 +0100969static int
970_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200971{
972 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100973 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100974#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200975 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100976#endif
977
978#ifdef MS_WINDOWS
979 flags |= O_NOINHERIT;
980#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200981 atomic_flag_works = &_Py_open_cloexec_works;
982 flags |= O_CLOEXEC;
983#else
984 atomic_flag_works = NULL;
985#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200986
Victor Stinnera555cfc2015-03-18 00:22:14 +0100987 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100988 do {
989 Py_BEGIN_ALLOW_THREADS
990 fd = open(pathname, flags);
991 Py_END_ALLOW_THREADS
992 } while (fd < 0
993 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
994 if (async_err)
995 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100996 if (fd < 0) {
997 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
998 return -1;
999 }
1000 }
1001 else {
1002 fd = open(pathname, flags);
1003 if (fd < 0)
1004 return -1;
1005 }
1006
1007#ifndef MS_WINDOWS
1008 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001009 close(fd);
1010 return -1;
1011 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001012#endif
1013
Victor Stinnerdaf45552013-08-28 00:53:59 +02001014 return fd;
1015}
1016
Victor Stinnera555cfc2015-03-18 00:22:14 +01001017/* Open a file with the specified flags (wrapper to open() function).
1018 Return a file descriptor on success. Raise an exception and return -1 on
1019 error.
1020
1021 The file descriptor is created non-inheritable.
1022
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001023 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1024 except if the Python signal handler raises an exception.
1025
Victor Stinner6f4fae82015-04-01 18:34:32 +02001026 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001027int
1028_Py_open(const char *pathname, int flags)
1029{
1030 /* _Py_open() must be called with the GIL held. */
1031 assert(PyGILState_Check());
1032 return _Py_open_impl(pathname, flags, 1);
1033}
1034
1035/* Open a file with the specified flags (wrapper to open() function).
1036 Return a file descriptor on success. Set errno and return -1 on error.
1037
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001038 The file descriptor is created non-inheritable.
1039
1040 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001041int
1042_Py_open_noraise(const char *pathname, int flags)
1043{
1044 return _Py_open_impl(pathname, flags, 0);
1045}
1046
Victor Stinnerdaf45552013-08-28 00:53:59 +02001047/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001048 encoding and use fopen() otherwise.
1049
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001050 The file descriptor is created non-inheritable.
1051
1052 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001053FILE *
1054_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1055{
Victor Stinner4e314432010-10-07 21:45:39 +00001056 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001057#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001058 char *cpath;
1059 char cmode[10];
1060 size_t r;
1061 r = wcstombs(cmode, mode, 10);
1062 if (r == (size_t)-1 || r >= 10) {
1063 errno = EINVAL;
1064 return NULL;
1065 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001066 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001067 if (cpath == NULL)
1068 return NULL;
1069 f = fopen(cpath, cmode);
1070 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001071#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001072 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001073#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001074 if (f == NULL)
1075 return NULL;
1076 if (make_non_inheritable(fileno(f)) < 0) {
1077 fclose(f);
1078 return NULL;
1079 }
1080 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001081}
1082
Victor Stinnere42ccd22015-03-18 01:39:23 +01001083/* Wrapper to fopen().
1084
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001085 The file descriptor is created non-inheritable.
1086
1087 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001088FILE*
1089_Py_fopen(const char *pathname, const char *mode)
1090{
1091 FILE *f = fopen(pathname, mode);
1092 if (f == NULL)
1093 return NULL;
1094 if (make_non_inheritable(fileno(f)) < 0) {
1095 fclose(f);
1096 return NULL;
1097 }
1098 return f;
1099}
1100
1101/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001102 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001103
Victor Stinnere42ccd22015-03-18 01:39:23 +01001104 Return the new file object on success. Raise an exception and return NULL
1105 on error.
1106
1107 The file descriptor is created non-inheritable.
1108
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001109 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1110 except if the Python signal handler raises an exception.
1111
Victor Stinner6f4fae82015-04-01 18:34:32 +02001112 Release the GIL to call _wfopen() or fopen(). The caller must hold
1113 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001114FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001115_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001116{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001117 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001118 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001119#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001120 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001121 wchar_t wmode[10];
1122 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001123
Victor Stinnere42ccd22015-03-18 01:39:23 +01001124 assert(PyGILState_Check());
1125
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001126 if (!PyUnicode_Check(path)) {
1127 PyErr_Format(PyExc_TypeError,
1128 "str file path expected under Windows, got %R",
1129 Py_TYPE(path));
1130 return NULL;
1131 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001132 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001133 if (wpath == NULL)
1134 return NULL;
1135
Victor Stinner4e314432010-10-07 21:45:39 +00001136 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001137 if (usize == 0) {
1138 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001139 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001140 }
Victor Stinner4e314432010-10-07 21:45:39 +00001141
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001142 do {
1143 Py_BEGIN_ALLOW_THREADS
1144 f = _wfopen(wpath, wmode);
1145 Py_END_ALLOW_THREADS
1146 } while (f == NULL
1147 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001148#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001149 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001150 char *path_bytes;
1151
1152 assert(PyGILState_Check());
1153
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001154 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001155 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001156 path_bytes = PyBytes_AS_STRING(bytes);
1157
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001158 do {
1159 Py_BEGIN_ALLOW_THREADS
1160 f = fopen(path_bytes, mode);
1161 Py_END_ALLOW_THREADS
1162 } while (f == NULL
1163 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001164
Victor Stinner4e314432010-10-07 21:45:39 +00001165 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001166#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001167 if (async_err)
1168 return NULL;
1169
Victor Stinnere42ccd22015-03-18 01:39:23 +01001170 if (f == NULL) {
1171 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001172 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001173 }
1174
1175 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001176 fclose(f);
1177 return NULL;
1178 }
1179 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001180}
1181
Victor Stinner66aab0c2015-03-19 22:53:20 +01001182/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001183
1184 On success, return the number of read bytes, it can be lower than count.
1185 If the current file offset is at or past the end of file, no bytes are read,
1186 and read() returns zero.
1187
1188 On error, raise an exception, set errno and return -1.
1189
1190 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1191 If the Python signal handler raises an exception, the function returns -1
1192 (the syscall is not retried).
1193
1194 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001195Py_ssize_t
1196_Py_read(int fd, void *buf, size_t count)
1197{
1198 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001199 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001200 int async_err = 0;
1201
Victor Stinner8a1be612016-03-14 22:07:55 +01001202 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001203
Victor Stinner66aab0c2015-03-19 22:53:20 +01001204 /* _Py_read() must not be called with an exception set, otherwise the
1205 * caller may think that read() was interrupted by a signal and the signal
1206 * handler raised an exception. */
1207 assert(!PyErr_Occurred());
1208
Victor Stinner66aab0c2015-03-19 22:53:20 +01001209#ifdef MS_WINDOWS
1210 if (count > INT_MAX) {
1211 /* On Windows, the count parameter of read() is an int */
1212 count = INT_MAX;
1213 }
1214#else
1215 if (count > PY_SSIZE_T_MAX) {
1216 /* if count is greater than PY_SSIZE_T_MAX,
1217 * read() result is undefined */
1218 count = PY_SSIZE_T_MAX;
1219 }
1220#endif
1221
Steve Dower8fc89802015-04-12 00:26:27 -04001222 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001223 do {
1224 Py_BEGIN_ALLOW_THREADS
1225 errno = 0;
1226#ifdef MS_WINDOWS
1227 n = read(fd, buf, (int)count);
1228#else
1229 n = read(fd, buf, count);
1230#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001231 /* save/restore errno because PyErr_CheckSignals()
1232 * and PyErr_SetFromErrno() can modify it */
1233 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001234 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001235 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001236 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001237 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001238
1239 if (async_err) {
1240 /* read() was interrupted by a signal (failed with EINTR)
1241 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001242 errno = err;
1243 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001244 return -1;
1245 }
1246 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001247 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001248 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001249 return -1;
1250 }
1251
1252 return n;
1253}
1254
Victor Stinner82c3e452015-04-01 18:34:45 +02001255static Py_ssize_t
1256_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001257{
1258 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001259 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001260 int async_err = 0;
1261
Steve Dower8fc89802015-04-12 00:26:27 -04001262 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001263#ifdef MS_WINDOWS
1264 if (count > 32767 && isatty(fd)) {
1265 /* Issue #11395: the Windows console returns an error (12: not
1266 enough space error) on writing into stdout if stdout mode is
1267 binary and the length is greater than 66,000 bytes (or less,
1268 depending on heap usage). */
1269 count = 32767;
1270 }
1271 else if (count > INT_MAX)
1272 count = INT_MAX;
1273#else
1274 if (count > PY_SSIZE_T_MAX) {
1275 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1276 * to do it ourself to have a portable behaviour. */
1277 count = PY_SSIZE_T_MAX;
1278 }
1279#endif
1280
Victor Stinner82c3e452015-04-01 18:34:45 +02001281 if (gil_held) {
1282 do {
1283 Py_BEGIN_ALLOW_THREADS
1284 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001285#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001286 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001287#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001288 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001289#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001290 /* save/restore errno because PyErr_CheckSignals()
1291 * and PyErr_SetFromErrno() can modify it */
1292 err = errno;
1293 Py_END_ALLOW_THREADS
1294 } while (n < 0 && err == EINTR &&
1295 !(async_err = PyErr_CheckSignals()));
1296 }
1297 else {
1298 do {
1299 errno = 0;
1300#ifdef MS_WINDOWS
1301 n = write(fd, buf, (int)count);
1302#else
1303 n = write(fd, buf, count);
1304#endif
1305 err = errno;
1306 } while (n < 0 && err == EINTR);
1307 }
Steve Dower8fc89802015-04-12 00:26:27 -04001308 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001309
1310 if (async_err) {
1311 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001312 and the Python signal handler raised an exception (if gil_held is
1313 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001314 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001315 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001316 return -1;
1317 }
1318 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001319 if (gil_held)
1320 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001321 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001322 return -1;
1323 }
1324
1325 return n;
1326}
1327
Victor Stinner82c3e452015-04-01 18:34:45 +02001328/* Write count bytes of buf into fd.
1329
1330 On success, return the number of written bytes, it can be lower than count
1331 including 0. On error, raise an exception, set errno and return -1.
1332
1333 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1334 If the Python signal handler raises an exception, the function returns -1
1335 (the syscall is not retried).
1336
1337 Release the GIL to call write(). The caller must hold the GIL. */
1338Py_ssize_t
1339_Py_write(int fd, const void *buf, size_t count)
1340{
Victor Stinner8a1be612016-03-14 22:07:55 +01001341 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001342
Victor Stinner82c3e452015-04-01 18:34:45 +02001343 /* _Py_write() must not be called with an exception set, otherwise the
1344 * caller may think that write() was interrupted by a signal and the signal
1345 * handler raised an exception. */
1346 assert(!PyErr_Occurred());
1347
1348 return _Py_write_impl(fd, buf, count, 1);
1349}
1350
1351/* Write count bytes of buf into fd.
1352 *
1353 * On success, return the number of written bytes, it can be lower than count
1354 * including 0. On error, set errno and return -1.
1355 *
1356 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1357 * without calling the Python signal handler. */
1358Py_ssize_t
1359_Py_write_noraise(int fd, const void *buf, size_t count)
1360{
1361 return _Py_write_impl(fd, buf, count, 0);
1362}
1363
Victor Stinner4e314432010-10-07 21:45:39 +00001364#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001365
1366/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001367 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001368
Victor Stinner4e314432010-10-07 21:45:39 +00001369int
1370_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1371{
1372 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001373 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001374 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001375 int res;
1376 size_t r1;
1377
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001378 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001379 if (cpath == NULL) {
1380 errno = EINVAL;
1381 return -1;
1382 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001383 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001384 PyMem_Free(cpath);
1385 if (res == -1)
1386 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001387 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001388 errno = EINVAL;
1389 return -1;
1390 }
1391 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001392 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001393 if (wbuf == NULL) {
1394 errno = EINVAL;
1395 return -1;
1396 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001397 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001398 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001399 errno = EINVAL;
1400 return -1;
1401 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001402 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001403 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001404 return (int)r1;
1405}
1406#endif
1407
1408#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001409
1410/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001411 encoding, decode the result from the locale encoding.
1412 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001413
Victor Stinner4e314432010-10-07 21:45:39 +00001414wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001415_Py_wrealpath(const wchar_t *path,
1416 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001417{
1418 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001419 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001420 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001421 char *res;
1422 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001423 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001424 if (cpath == NULL) {
1425 errno = EINVAL;
1426 return NULL;
1427 }
1428 res = realpath(cpath, cresolved_path);
1429 PyMem_Free(cpath);
1430 if (res == NULL)
1431 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001432
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001433 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001434 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001435 errno = EINVAL;
1436 return NULL;
1437 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001438 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001439 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001440 errno = EINVAL;
1441 return NULL;
1442 }
1443 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001444 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001445 return resolved_path;
1446}
1447#endif
1448
Victor Stinnerf4061da2010-10-14 12:37:19 +00001449/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001450 including the null character. Decode the path from the locale encoding.
1451 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001452
Victor Stinner4e314432010-10-07 21:45:39 +00001453wchar_t*
1454_Py_wgetcwd(wchar_t *buf, size_t size)
1455{
1456#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001457 int isize = (int)Py_MIN(size, INT_MAX);
1458 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001459#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001460 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001461 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001462 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001463
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001464 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001465 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001466 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001467 if (wname == NULL)
1468 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001469 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001470 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001471 return NULL;
1472 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001473 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001474 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001475 return buf;
1476#endif
1477}
1478
Victor Stinnerdaf45552013-08-28 00:53:59 +02001479/* Duplicate a file descriptor. The new file descriptor is created as
1480 non-inheritable. Return a new file descriptor on success, raise an OSError
1481 exception and return -1 on error.
1482
1483 The GIL is released to call dup(). The caller must hold the GIL. */
1484int
1485_Py_dup(int fd)
1486{
1487#ifdef MS_WINDOWS
1488 HANDLE handle;
1489 DWORD ftype;
1490#endif
1491
Victor Stinner8a1be612016-03-14 22:07:55 +01001492 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001493
Victor Stinnerdaf45552013-08-28 00:53:59 +02001494#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001495 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001496 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001497 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001498 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001499 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001500 return -1;
1501 }
1502
1503 /* get the file type, ignore the error if it failed */
1504 ftype = GetFileType(handle);
1505
1506 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001507 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001508 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001509 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001510 Py_END_ALLOW_THREADS
1511 if (fd < 0) {
1512 PyErr_SetFromErrno(PyExc_OSError);
1513 return -1;
1514 }
1515
1516 /* Character files like console cannot be make non-inheritable */
1517 if (ftype != FILE_TYPE_CHAR) {
1518 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001519 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001520 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001521 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001522 return -1;
1523 }
1524 }
1525#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1526 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001527 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001528 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001529 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001530 Py_END_ALLOW_THREADS
1531 if (fd < 0) {
1532 PyErr_SetFromErrno(PyExc_OSError);
1533 return -1;
1534 }
1535
1536#else
1537 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001538 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001539 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001540 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001541 Py_END_ALLOW_THREADS
1542 if (fd < 0) {
1543 PyErr_SetFromErrno(PyExc_OSError);
1544 return -1;
1545 }
1546
1547 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001548 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001549 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001550 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001551 return -1;
1552 }
1553#endif
1554 return fd;
1555}
1556
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001557#ifndef MS_WINDOWS
1558/* Get the blocking mode of the file descriptor.
1559 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1560 raise an exception and return -1 on error. */
1561int
1562_Py_get_blocking(int fd)
1563{
Steve Dower8fc89802015-04-12 00:26:27 -04001564 int flags;
1565 _Py_BEGIN_SUPPRESS_IPH
1566 flags = fcntl(fd, F_GETFL, 0);
1567 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001568 if (flags < 0) {
1569 PyErr_SetFromErrno(PyExc_OSError);
1570 return -1;
1571 }
1572
1573 return !(flags & O_NONBLOCK);
1574}
1575
1576/* Set the blocking mode of the specified file descriptor.
1577
1578 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1579 otherwise.
1580
1581 Return 0 on success, raise an exception and return -1 on error. */
1582int
1583_Py_set_blocking(int fd, int blocking)
1584{
1585#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1586 int arg = !blocking;
1587 if (ioctl(fd, FIONBIO, &arg) < 0)
1588 goto error;
1589#else
1590 int flags, res;
1591
Steve Dower8fc89802015-04-12 00:26:27 -04001592 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001593 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001594 if (flags >= 0) {
1595 if (blocking)
1596 flags = flags & (~O_NONBLOCK);
1597 else
1598 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001599
Steve Dower8fc89802015-04-12 00:26:27 -04001600 res = fcntl(fd, F_SETFL, flags);
1601 } else {
1602 res = -1;
1603 }
1604 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001605
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001606 if (res < 0)
1607 goto error;
1608#endif
1609 return 0;
1610
1611error:
1612 PyErr_SetFromErrno(PyExc_OSError);
1613 return -1;
1614}
1615#endif