blob: 03cc37958a0248fc31f01a799d6862837fefe961 [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 Stinner91106cd2017-12-13 12:29:09 +0100252static wchar_t*
253decode_locale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000254{
255 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100256 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000257 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200258#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000259 unsigned char *in;
260 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000261 mbstate_t mbs;
262#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100263
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100264#ifdef HAVE_BROKEN_MBSTOWCS
265 /* Some platforms have a broken implementation of
266 * mbstowcs which does not count the characters that
267 * would result from conversion. Use an upper bound.
268 */
269 argsize = strlen(arg);
270#else
271 argsize = mbstowcs(NULL, arg, 0);
272#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000273 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600274 if (argsize == PY_SSIZE_T_MAX)
275 goto oom;
276 argsize += 1;
277 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
278 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600279 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000280 if (!res)
281 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600282 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000283 if (count != (size_t)-1) {
284 wchar_t *tmp;
285 /* Only use the result if it contains no
286 surrogate characters. */
287 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100288 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000289 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000290 if (*tmp == 0) {
291 if (size != NULL)
292 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000293 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000294 }
Victor Stinner4e314432010-10-07 21:45:39 +0000295 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200296 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000297 }
298 /* Conversion failed. Fall back to escaping with surrogateescape. */
299#ifdef HAVE_MBRTOWC
300 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
301
302 /* Overallocate; as multi-byte characters are in the argument, the
303 actual output could use less memory. */
304 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600305 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
306 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200307 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000308 if (!res)
309 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000310 in = (unsigned char*)arg;
311 out = res;
312 memset(&mbs, 0, sizeof mbs);
313 while (argsize) {
314 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
315 if (converted == 0)
316 /* Reached end of string; null char stored. */
317 break;
318 if (converted == (size_t)-2) {
319 /* Incomplete character. This should never happen,
320 since we provide everything that we have -
321 unless there is a bug in the C library, or I
322 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200323 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100324 if (size != NULL)
325 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000326 return NULL;
327 }
328 if (converted == (size_t)-1) {
329 /* Conversion error. Escape as UTF-8b, and start over
330 in the initial shift state. */
331 *out++ = 0xdc00 + *in++;
332 argsize--;
333 memset(&mbs, 0, sizeof mbs);
334 continue;
335 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100336 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000337 /* Surrogate character. Escape the original
338 byte sequence with surrogateescape. */
339 argsize -= converted;
340 while (converted--)
341 *out++ = 0xdc00 + *in++;
342 continue;
343 }
344 /* successfully converted some bytes */
345 in += converted;
346 argsize -= converted;
347 out++;
348 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100349 if (size != NULL)
350 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100351#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000352 /* Cannot use C locale for escaping; manually escape as if charset
353 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
354 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100355 res = decode_ascii_surrogateescape(arg, size);
356 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100357 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100358#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000359 return res;
Victor Stinner91106cd2017-12-13 12:29:09 +0100360
Victor Stinner4e314432010-10-07 21:45:39 +0000361oom:
Victor Stinner91106cd2017-12-13 12:29:09 +0100362 if (size != NULL) {
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100363 *size = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100364 }
Victor Stinner4e314432010-10-07 21:45:39 +0000365 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100366}
367
368
369/* Decode a byte string from the locale encoding with the
370 surrogateescape error handler: undecodable bytes are decoded as characters
371 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
372 character, escape the bytes using the surrogateescape error handler instead
373 of decoding them.
374
375 Return a pointer to a newly allocated wide character string, use
376 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
377 wide characters excluding the null character into *size
378
379 Return NULL on decoding error or memory allocation error. If *size* is not
380 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
381 decoding error.
382
383 Decoding errors should never happen, unless there is a bug in the C
384 library.
385
386 Use the Py_EncodeLocale() function to encode the character string back to a
387 byte string. */
388wchar_t*
389Py_DecodeLocale(const char* arg, size_t *size)
390{
391#if defined(__APPLE__) || defined(__ANDROID__)
392 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
393#else
394 if (Py_UTF8Mode) {
395 return _Py_DecodeUTF8_surrogateescape(arg, strlen(arg), size);
396 }
397
398#ifndef MS_WINDOWS
399 if (force_ascii == -1)
400 force_ascii = check_force_ascii();
401
402 if (force_ascii) {
403 /* force ASCII encoding to workaround mbstowcs() issue */
404 wchar_t *wstr = decode_ascii_surrogateescape(arg, size);
405 if (wstr == NULL) {
406 if (size != NULL) {
407 *size = (size_t)-1;
408 }
409 return NULL;
410 }
411 return wstr;
412 }
413#endif
414
415 return decode_locale(arg, size);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100416#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000417}
418
Victor Stinner91106cd2017-12-13 12:29:09 +0100419static char*
420_Py_EncodeLocaleUTF8(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000421{
Victor Stinnere2623772012-11-12 23:04:02 +0100422 Py_ssize_t len;
423 PyObject *unicode, *bytes = NULL;
424 char *cpath;
425
426 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner91106cd2017-12-13 12:29:09 +0100427 if (unicode == NULL) {
Victor Stinnere2623772012-11-12 23:04:02 +0100428 return NULL;
Victor Stinner91106cd2017-12-13 12:29:09 +0100429 }
Victor Stinnere2623772012-11-12 23:04:02 +0100430
431 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
432 Py_DECREF(unicode);
433 if (bytes == NULL) {
434 PyErr_Clear();
Victor Stinner91106cd2017-12-13 12:29:09 +0100435 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100436 *error_pos = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100437 }
Victor Stinnere2623772012-11-12 23:04:02 +0100438 return NULL;
439 }
440
441 len = PyBytes_GET_SIZE(bytes);
442 cpath = PyMem_Malloc(len+1);
443 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100444 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100445 Py_DECREF(bytes);
Victor Stinner91106cd2017-12-13 12:29:09 +0100446 if (error_pos != NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100447 *error_pos = (size_t)-1;
Victor Stinner91106cd2017-12-13 12:29:09 +0100448 }
Victor Stinnere2623772012-11-12 23:04:02 +0100449 return NULL;
450 }
451 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
452 Py_DECREF(bytes);
453 return cpath;
Victor Stinner91106cd2017-12-13 12:29:09 +0100454}
455
456static char*
457encode_locale(const wchar_t *text, size_t *error_pos)
458{
Victor Stinner4e314432010-10-07 21:45:39 +0000459 const size_t len = wcslen(text);
460 char *result = NULL, *bytes = NULL;
461 size_t i, size, converted;
462 wchar_t c, buf[2];
463
464 /* The function works in two steps:
465 1. compute the length of the output buffer in bytes (size)
466 2. outputs the bytes */
467 size = 0;
468 buf[1] = 0;
469 while (1) {
470 for (i=0; i < len; i++) {
471 c = text[i];
472 if (c >= 0xdc80 && c <= 0xdcff) {
473 /* UTF-8b surrogate */
474 if (bytes != NULL) {
475 *bytes++ = c - 0xdc00;
476 size--;
477 }
478 else
479 size++;
480 continue;
481 }
482 else {
483 buf[0] = c;
484 if (bytes != NULL)
485 converted = wcstombs(bytes, buf, size);
486 else
487 converted = wcstombs(NULL, buf, 0);
488 if (converted == (size_t)-1) {
489 if (result != NULL)
490 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000491 if (error_pos != NULL)
492 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000493 return NULL;
494 }
495 if (bytes != NULL) {
496 bytes += converted;
497 size -= converted;
498 }
499 else
500 size += converted;
501 }
502 }
503 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100504 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000505 break;
506 }
507
508 size += 1; /* nul byte at the end */
509 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100510 if (result == NULL) {
511 if (error_pos != NULL)
512 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000513 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100514 }
Victor Stinner4e314432010-10-07 21:45:39 +0000515 bytes = result;
516 }
517 return result;
Victor Stinner91106cd2017-12-13 12:29:09 +0100518}
519
520/* Encode a wide character string to the locale encoding with the
521 surrogateescape error handler: surrogate characters in the range
522 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
523
524 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
525 the memory. Return NULL on encoding or memory allocation error.
526
527 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
528 to the index of the invalid character on encoding error.
529
530 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
531 character string. */
532char*
533Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
534{
535#if defined(__APPLE__) || defined(__ANDROID__)
536 return _Py_EncodeLocaleUTF8(text, error_pos);
537#else /* __APPLE__ */
538 if (Py_UTF8Mode) {
539 return _Py_EncodeLocaleUTF8(text, error_pos);
540 }
541
542#ifndef MS_WINDOWS
543 if (force_ascii == -1)
544 force_ascii = check_force_ascii();
545
546 if (force_ascii)
547 return encode_ascii_surrogateescape(text, error_pos);
548#endif
549
550 return encode_locale(text, error_pos);
Xavier de Gaye76febd02016-12-15 20:59:58 +0100551#endif /* __APPLE__ or __ANDROID__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000552}
553
Victor Stinner6672d0c2010-10-07 22:53:43 +0000554
Steve Dowerf2f373f2015-02-21 08:44:05 -0800555#ifdef MS_WINDOWS
556static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
557
558static void
559FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
560{
561 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
562 /* Cannot simply cast and dereference in_ptr,
563 since it might not be aligned properly */
564 __int64 in;
565 memcpy(&in, in_ptr, sizeof(in));
566 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
567 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
568}
569
570void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800571_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800572{
573 /* XXX endianness */
574 __int64 out;
575 out = time_in + secs_between_epochs;
576 out = out * 10000000 + nsec_in / 100;
577 memcpy(out_ptr, &out, sizeof(out));
578}
579
580/* Below, we *know* that ugo+r is 0444 */
581#if _S_IREAD != 0400
582#error Unsupported C library
583#endif
584static int
585attributes_to_mode(DWORD attr)
586{
587 int m = 0;
588 if (attr & FILE_ATTRIBUTE_DIRECTORY)
589 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
590 else
591 m |= _S_IFREG;
592 if (attr & FILE_ATTRIBUTE_READONLY)
593 m |= 0444;
594 else
595 m |= 0666;
596 return m;
597}
598
Steve Dowerbf1f3762015-02-21 15:26:02 -0800599void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200600_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
601 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800602{
603 memset(result, 0, sizeof(*result));
604 result->st_mode = attributes_to_mode(info->dwFileAttributes);
605 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
606 result->st_dev = info->dwVolumeSerialNumber;
607 result->st_rdev = result->st_dev;
608 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
609 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
610 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
611 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100612 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800613 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
614 /* first clear the S_IFMT bits */
615 result->st_mode ^= (result->st_mode & S_IFMT);
616 /* now set the bits that make this a symlink */
617 result->st_mode |= S_IFLNK;
618 }
619 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800620}
621#endif
622
623/* Return information about a file.
624
625 On POSIX, use fstat().
626
627 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800628 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
629 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800630 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200631
632 On Windows, set the last Windows error and return nonzero on error. On
633 POSIX, set errno and return nonzero on error. Fill status and return 0 on
634 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800635int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200636_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800637{
638#ifdef MS_WINDOWS
639 BY_HANDLE_FILE_INFORMATION info;
640 HANDLE h;
641 int type;
642
Steve Dower940f33a2016-09-08 11:21:54 -0700643 _Py_BEGIN_SUPPRESS_IPH
644 h = (HANDLE)_get_osfhandle(fd);
645 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800646
647 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400648 /* errno is already set by _get_osfhandle, but we also set
649 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800650 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800651 return -1;
652 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200653 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800654
655 type = GetFileType(h);
656 if (type == FILE_TYPE_UNKNOWN) {
657 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400658 if (error != 0) {
659 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800660 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400661 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800662 /* else: valid but unknown file */
663 }
664
665 if (type != FILE_TYPE_DISK) {
666 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200667 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800668 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200669 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800670 return 0;
671 }
672
673 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400674 /* The Win32 error is already set, but we also set errno for
675 callers who expect it */
676 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800677 return -1;
678 }
679
Victor Stinnere134a7f2015-03-30 10:09:31 +0200680 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800681 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100682 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800683 return 0;
684#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200685 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800686#endif
687}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800688
Victor Stinnere134a7f2015-03-30 10:09:31 +0200689/* Return information about a file.
690
691 On POSIX, use fstat().
692
693 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800694 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
695 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200696 #23152.
697
698 Raise an exception and return -1 on error. On Windows, set the last Windows
699 error on error. On POSIX, set errno on error. Fill status and return 0 on
700 success.
701
Victor Stinner6f4fae82015-04-01 18:34:32 +0200702 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
703 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200704int
705_Py_fstat(int fd, struct _Py_stat_struct *status)
706{
707 int res;
708
Victor Stinner8a1be612016-03-14 22:07:55 +0100709 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100710
Victor Stinnere134a7f2015-03-30 10:09:31 +0200711 Py_BEGIN_ALLOW_THREADS
712 res = _Py_fstat_noraise(fd, status);
713 Py_END_ALLOW_THREADS
714
715 if (res != 0) {
716#ifdef MS_WINDOWS
717 PyErr_SetFromWindowsErr(0);
718#else
719 PyErr_SetFromErrno(PyExc_OSError);
720#endif
721 return -1;
722 }
723 return 0;
724}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800725
Victor Stinner6672d0c2010-10-07 22:53:43 +0000726/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
727 call stat() otherwise. Only fill st_mode attribute on Windows.
728
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100729 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
730 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000731
732int
Victor Stinnera4a75952010-10-07 22:23:10 +0000733_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000734{
735#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000736 int err;
737 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300738 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000739
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300740 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100741 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100742 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300743
Victor Stinneree587ea2011-11-17 00:51:38 +0100744 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000745 if (!err)
746 statbuf->st_mode = wstatbuf.st_mode;
747 return err;
748#else
749 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300750 PyObject *bytes;
751 char *cpath;
752
753 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000754 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100755 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300756
757 /* check for embedded null bytes */
758 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
759 Py_DECREF(bytes);
760 return -2;
761 }
762
763 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000764 Py_DECREF(bytes);
765 return ret;
766#endif
767}
768
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100769
Antoine Pitrou409b5382013-10-12 22:41:17 +0200770static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200771get_inheritable(int fd, int raise)
772{
773#ifdef MS_WINDOWS
774 HANDLE handle;
775 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000776
Steve Dower8fc89802015-04-12 00:26:27 -0400777 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200778 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400779 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200780 if (handle == INVALID_HANDLE_VALUE) {
781 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700782 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200783 return -1;
784 }
785
786 if (!GetHandleInformation(handle, &flags)) {
787 if (raise)
788 PyErr_SetFromWindowsErr(0);
789 return -1;
790 }
791
792 return (flags & HANDLE_FLAG_INHERIT);
793#else
794 int flags;
795
796 flags = fcntl(fd, F_GETFD, 0);
797 if (flags == -1) {
798 if (raise)
799 PyErr_SetFromErrno(PyExc_OSError);
800 return -1;
801 }
802 return !(flags & FD_CLOEXEC);
803#endif
804}
805
806/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200807 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200808 raise an exception and return -1 on error. */
809int
810_Py_get_inheritable(int fd)
811{
812 return get_inheritable(fd, 1);
813}
814
815static int
816set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
817{
818#ifdef MS_WINDOWS
819 HANDLE handle;
820 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200821#else
822#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
823 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200824 int request;
825 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200826#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200827 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200828 int res;
829#endif
830
831 /* atomic_flag_works can only be used to make the file descriptor
832 non-inheritable */
833 assert(!(atomic_flag_works != NULL && inheritable));
834
835 if (atomic_flag_works != NULL && !inheritable) {
836 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700837 int isInheritable = get_inheritable(fd, raise);
838 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200839 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700840 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200841 }
842
843 if (*atomic_flag_works)
844 return 0;
845 }
846
847#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -0400848 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200849 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400850 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200851 if (handle == INVALID_HANDLE_VALUE) {
852 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700853 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200854 return -1;
855 }
856
857 if (inheritable)
858 flags = HANDLE_FLAG_INHERIT;
859 else
860 flags = 0;
861 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
862 if (raise)
863 PyErr_SetFromWindowsErr(0);
864 return -1;
865 }
866 return 0;
867
Victor Stinnerdaf45552013-08-28 00:53:59 +0200868#else
Victor Stinner282124b2014-09-02 11:41:04 +0200869
870#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
871 if (ioctl_works != 0) {
872 /* fast-path: ioctl() only requires one syscall */
873 if (inheritable)
874 request = FIONCLEX;
875 else
876 request = FIOCLEX;
877 err = ioctl(fd, request, NULL);
878 if (!err) {
879 ioctl_works = 1;
880 return 0;
881 }
882
Victor Stinner3116cc42016-05-19 16:46:18 +0200883 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200884 if (raise)
885 PyErr_SetFromErrno(PyExc_OSError);
886 return -1;
887 }
888 else {
889 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
890 device". The ioctl is declared but not supported by the kernel.
891 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200892 Illumos-based OS for example.
893
894 Issue #27057: When SELinux policy disallows ioctl it will fail
895 with EACCES. While FIOCLEX is safe operation it may be
896 unavailable because ioctl was denied altogether.
897 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200898 ioctl_works = 0;
899 }
900 /* fallback to fcntl() if ioctl() does not work */
901 }
902#endif
903
904 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200905 flags = fcntl(fd, F_GETFD);
906 if (flags < 0) {
907 if (raise)
908 PyErr_SetFromErrno(PyExc_OSError);
909 return -1;
910 }
911
Victor Stinnera858bbd2016-04-17 16:51:52 +0200912 if (inheritable) {
913 new_flags = flags & ~FD_CLOEXEC;
914 }
915 else {
916 new_flags = flags | FD_CLOEXEC;
917 }
918
919 if (new_flags == flags) {
920 /* FD_CLOEXEC flag already set/cleared: nothing to do */
921 return 0;
922 }
923
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +0100924 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200925 if (res < 0) {
926 if (raise)
927 PyErr_SetFromErrno(PyExc_OSError);
928 return -1;
929 }
930 return 0;
931#endif
932}
933
934/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200935 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200936static int
937make_non_inheritable(int fd)
938{
939 return set_inheritable(fd, 0, 0, NULL);
940}
941
942/* Set the inheritable flag of the specified file descriptor.
943 On success: return 0, on error: raise an exception if raise is nonzero
944 and return -1.
945
946 If atomic_flag_works is not NULL:
947
948 * if *atomic_flag_works==-1, check if the inheritable is set on the file
949 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
950 set the inheritable flag
951 * if *atomic_flag_works==1: do nothing
952 * if *atomic_flag_works==0: set inheritable flag to False
953
954 Set atomic_flag_works to NULL if no atomic flag was used to create the
955 file descriptor.
956
957 atomic_flag_works can only be used to make a file descriptor
958 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
959int
960_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
961{
962 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
963}
964
Victor Stinnera555cfc2015-03-18 00:22:14 +0100965static int
966_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200967{
968 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100969 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100970#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200971 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100972#endif
973
974#ifdef MS_WINDOWS
975 flags |= O_NOINHERIT;
976#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200977 atomic_flag_works = &_Py_open_cloexec_works;
978 flags |= O_CLOEXEC;
979#else
980 atomic_flag_works = NULL;
981#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200982
Victor Stinnera555cfc2015-03-18 00:22:14 +0100983 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100984 do {
985 Py_BEGIN_ALLOW_THREADS
986 fd = open(pathname, flags);
987 Py_END_ALLOW_THREADS
988 } while (fd < 0
989 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
990 if (async_err)
991 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100992 if (fd < 0) {
993 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
994 return -1;
995 }
996 }
997 else {
998 fd = open(pathname, flags);
999 if (fd < 0)
1000 return -1;
1001 }
1002
1003#ifndef MS_WINDOWS
1004 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001005 close(fd);
1006 return -1;
1007 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001008#endif
1009
Victor Stinnerdaf45552013-08-28 00:53:59 +02001010 return fd;
1011}
1012
Victor Stinnera555cfc2015-03-18 00:22:14 +01001013/* Open a file with the specified flags (wrapper to open() function).
1014 Return a file descriptor on success. Raise an exception and return -1 on
1015 error.
1016
1017 The file descriptor is created non-inheritable.
1018
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001019 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1020 except if the Python signal handler raises an exception.
1021
Victor Stinner6f4fae82015-04-01 18:34:32 +02001022 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001023int
1024_Py_open(const char *pathname, int flags)
1025{
1026 /* _Py_open() must be called with the GIL held. */
1027 assert(PyGILState_Check());
1028 return _Py_open_impl(pathname, flags, 1);
1029}
1030
1031/* Open a file with the specified flags (wrapper to open() function).
1032 Return a file descriptor on success. Set errno and return -1 on error.
1033
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001034 The file descriptor is created non-inheritable.
1035
1036 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001037int
1038_Py_open_noraise(const char *pathname, int flags)
1039{
1040 return _Py_open_impl(pathname, flags, 0);
1041}
1042
Victor Stinnerdaf45552013-08-28 00:53:59 +02001043/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001044 encoding and use fopen() otherwise.
1045
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001046 The file descriptor is created non-inheritable.
1047
1048 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001049FILE *
1050_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1051{
Victor Stinner4e314432010-10-07 21:45:39 +00001052 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001053#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001054 char *cpath;
1055 char cmode[10];
1056 size_t r;
1057 r = wcstombs(cmode, mode, 10);
1058 if (r == (size_t)-1 || r >= 10) {
1059 errno = EINVAL;
1060 return NULL;
1061 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001062 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001063 if (cpath == NULL)
1064 return NULL;
1065 f = fopen(cpath, cmode);
1066 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001067#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001068 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001069#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001070 if (f == NULL)
1071 return NULL;
1072 if (make_non_inheritable(fileno(f)) < 0) {
1073 fclose(f);
1074 return NULL;
1075 }
1076 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001077}
1078
Victor Stinnere42ccd22015-03-18 01:39:23 +01001079/* Wrapper to fopen().
1080
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001081 The file descriptor is created non-inheritable.
1082
1083 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001084FILE*
1085_Py_fopen(const char *pathname, const char *mode)
1086{
1087 FILE *f = fopen(pathname, mode);
1088 if (f == NULL)
1089 return NULL;
1090 if (make_non_inheritable(fileno(f)) < 0) {
1091 fclose(f);
1092 return NULL;
1093 }
1094 return f;
1095}
1096
1097/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001098 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001099
Victor Stinnere42ccd22015-03-18 01:39:23 +01001100 Return the new file object on success. Raise an exception and return NULL
1101 on error.
1102
1103 The file descriptor is created non-inheritable.
1104
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001105 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1106 except if the Python signal handler raises an exception.
1107
Victor Stinner6f4fae82015-04-01 18:34:32 +02001108 Release the GIL to call _wfopen() or fopen(). The caller must hold
1109 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001110FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001111_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001112{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001113 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001114 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001115#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001116 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001117 wchar_t wmode[10];
1118 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001119
Victor Stinnere42ccd22015-03-18 01:39:23 +01001120 assert(PyGILState_Check());
1121
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001122 if (!PyUnicode_Check(path)) {
1123 PyErr_Format(PyExc_TypeError,
1124 "str file path expected under Windows, got %R",
1125 Py_TYPE(path));
1126 return NULL;
1127 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001128 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001129 if (wpath == NULL)
1130 return NULL;
1131
Victor Stinner4e314432010-10-07 21:45:39 +00001132 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001133 if (usize == 0) {
1134 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001135 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001136 }
Victor Stinner4e314432010-10-07 21:45:39 +00001137
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001138 do {
1139 Py_BEGIN_ALLOW_THREADS
1140 f = _wfopen(wpath, wmode);
1141 Py_END_ALLOW_THREADS
1142 } while (f == NULL
1143 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001144#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001145 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001146 char *path_bytes;
1147
1148 assert(PyGILState_Check());
1149
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001150 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001151 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001152 path_bytes = PyBytes_AS_STRING(bytes);
1153
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001154 do {
1155 Py_BEGIN_ALLOW_THREADS
1156 f = fopen(path_bytes, mode);
1157 Py_END_ALLOW_THREADS
1158 } while (f == NULL
1159 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001160
Victor Stinner4e314432010-10-07 21:45:39 +00001161 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001162#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001163 if (async_err)
1164 return NULL;
1165
Victor Stinnere42ccd22015-03-18 01:39:23 +01001166 if (f == NULL) {
1167 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001168 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001169 }
1170
1171 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001172 fclose(f);
1173 return NULL;
1174 }
1175 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001176}
1177
Victor Stinner66aab0c2015-03-19 22:53:20 +01001178/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001179
1180 On success, return the number of read bytes, it can be lower than count.
1181 If the current file offset is at or past the end of file, no bytes are read,
1182 and read() returns zero.
1183
1184 On error, raise an exception, set errno and return -1.
1185
1186 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1187 If the Python signal handler raises an exception, the function returns -1
1188 (the syscall is not retried).
1189
1190 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001191Py_ssize_t
1192_Py_read(int fd, void *buf, size_t count)
1193{
1194 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001195 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001196 int async_err = 0;
1197
Victor Stinner8a1be612016-03-14 22:07:55 +01001198 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001199
Victor Stinner66aab0c2015-03-19 22:53:20 +01001200 /* _Py_read() must not be called with an exception set, otherwise the
1201 * caller may think that read() was interrupted by a signal and the signal
1202 * handler raised an exception. */
1203 assert(!PyErr_Occurred());
1204
Victor Stinner66aab0c2015-03-19 22:53:20 +01001205#ifdef MS_WINDOWS
1206 if (count > INT_MAX) {
1207 /* On Windows, the count parameter of read() is an int */
1208 count = INT_MAX;
1209 }
1210#else
1211 if (count > PY_SSIZE_T_MAX) {
1212 /* if count is greater than PY_SSIZE_T_MAX,
1213 * read() result is undefined */
1214 count = PY_SSIZE_T_MAX;
1215 }
1216#endif
1217
Steve Dower8fc89802015-04-12 00:26:27 -04001218 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001219 do {
1220 Py_BEGIN_ALLOW_THREADS
1221 errno = 0;
1222#ifdef MS_WINDOWS
1223 n = read(fd, buf, (int)count);
1224#else
1225 n = read(fd, buf, count);
1226#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001227 /* save/restore errno because PyErr_CheckSignals()
1228 * and PyErr_SetFromErrno() can modify it */
1229 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001230 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001231 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001232 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001233 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001234
1235 if (async_err) {
1236 /* read() was interrupted by a signal (failed with EINTR)
1237 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001238 errno = err;
1239 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001240 return -1;
1241 }
1242 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001243 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001244 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001245 return -1;
1246 }
1247
1248 return n;
1249}
1250
Victor Stinner82c3e452015-04-01 18:34:45 +02001251static Py_ssize_t
1252_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001253{
1254 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001255 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001256 int async_err = 0;
1257
Steve Dower8fc89802015-04-12 00:26:27 -04001258 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001259#ifdef MS_WINDOWS
1260 if (count > 32767 && isatty(fd)) {
1261 /* Issue #11395: the Windows console returns an error (12: not
1262 enough space error) on writing into stdout if stdout mode is
1263 binary and the length is greater than 66,000 bytes (or less,
1264 depending on heap usage). */
1265 count = 32767;
1266 }
1267 else if (count > INT_MAX)
1268 count = INT_MAX;
1269#else
1270 if (count > PY_SSIZE_T_MAX) {
1271 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1272 * to do it ourself to have a portable behaviour. */
1273 count = PY_SSIZE_T_MAX;
1274 }
1275#endif
1276
Victor Stinner82c3e452015-04-01 18:34:45 +02001277 if (gil_held) {
1278 do {
1279 Py_BEGIN_ALLOW_THREADS
1280 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001281#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001282 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001283#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001284 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001285#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001286 /* save/restore errno because PyErr_CheckSignals()
1287 * and PyErr_SetFromErrno() can modify it */
1288 err = errno;
1289 Py_END_ALLOW_THREADS
1290 } while (n < 0 && err == EINTR &&
1291 !(async_err = PyErr_CheckSignals()));
1292 }
1293 else {
1294 do {
1295 errno = 0;
1296#ifdef MS_WINDOWS
1297 n = write(fd, buf, (int)count);
1298#else
1299 n = write(fd, buf, count);
1300#endif
1301 err = errno;
1302 } while (n < 0 && err == EINTR);
1303 }
Steve Dower8fc89802015-04-12 00:26:27 -04001304 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001305
1306 if (async_err) {
1307 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001308 and the Python signal handler raised an exception (if gil_held is
1309 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001310 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001311 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001312 return -1;
1313 }
1314 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001315 if (gil_held)
1316 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001317 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001318 return -1;
1319 }
1320
1321 return n;
1322}
1323
Victor Stinner82c3e452015-04-01 18:34:45 +02001324/* Write count bytes of buf into fd.
1325
1326 On success, return the number of written bytes, it can be lower than count
1327 including 0. On error, raise an exception, set errno and return -1.
1328
1329 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1330 If the Python signal handler raises an exception, the function returns -1
1331 (the syscall is not retried).
1332
1333 Release the GIL to call write(). The caller must hold the GIL. */
1334Py_ssize_t
1335_Py_write(int fd, const void *buf, size_t count)
1336{
Victor Stinner8a1be612016-03-14 22:07:55 +01001337 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001338
Victor Stinner82c3e452015-04-01 18:34:45 +02001339 /* _Py_write() must not be called with an exception set, otherwise the
1340 * caller may think that write() was interrupted by a signal and the signal
1341 * handler raised an exception. */
1342 assert(!PyErr_Occurred());
1343
1344 return _Py_write_impl(fd, buf, count, 1);
1345}
1346
1347/* Write count bytes of buf into fd.
1348 *
1349 * On success, return the number of written bytes, it can be lower than count
1350 * including 0. On error, set errno and return -1.
1351 *
1352 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1353 * without calling the Python signal handler. */
1354Py_ssize_t
1355_Py_write_noraise(int fd, const void *buf, size_t count)
1356{
1357 return _Py_write_impl(fd, buf, count, 0);
1358}
1359
Victor Stinner4e314432010-10-07 21:45:39 +00001360#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001361
1362/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001363 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001364
Victor Stinner4e314432010-10-07 21:45:39 +00001365int
1366_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1367{
1368 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001369 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001370 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001371 int res;
1372 size_t r1;
1373
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001374 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001375 if (cpath == NULL) {
1376 errno = EINVAL;
1377 return -1;
1378 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001379 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001380 PyMem_Free(cpath);
1381 if (res == -1)
1382 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001383 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001384 errno = EINVAL;
1385 return -1;
1386 }
1387 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001388 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001389 if (wbuf == NULL) {
1390 errno = EINVAL;
1391 return -1;
1392 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001393 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001394 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001395 errno = EINVAL;
1396 return -1;
1397 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001398 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001399 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001400 return (int)r1;
1401}
1402#endif
1403
1404#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001405
1406/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001407 encoding, decode the result from the locale encoding.
1408 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001409
Victor Stinner4e314432010-10-07 21:45:39 +00001410wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001411_Py_wrealpath(const wchar_t *path,
1412 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001413{
1414 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001415 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001416 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001417 char *res;
1418 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001419 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001420 if (cpath == NULL) {
1421 errno = EINVAL;
1422 return NULL;
1423 }
1424 res = realpath(cpath, cresolved_path);
1425 PyMem_Free(cpath);
1426 if (res == NULL)
1427 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001428
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001429 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001430 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001431 errno = EINVAL;
1432 return NULL;
1433 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001434 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001435 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001436 errno = EINVAL;
1437 return NULL;
1438 }
1439 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001440 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001441 return resolved_path;
1442}
1443#endif
1444
Victor Stinnerf4061da2010-10-14 12:37:19 +00001445/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001446 including the null character. Decode the path from the locale encoding.
1447 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001448
Victor Stinner4e314432010-10-07 21:45:39 +00001449wchar_t*
1450_Py_wgetcwd(wchar_t *buf, size_t size)
1451{
1452#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001453 int isize = (int)Py_MIN(size, INT_MAX);
1454 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001455#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001456 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001457 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001458 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001459
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001460 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001461 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001462 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001463 if (wname == NULL)
1464 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001465 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001466 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001467 return NULL;
1468 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001469 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001470 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001471 return buf;
1472#endif
1473}
1474
Victor Stinnerdaf45552013-08-28 00:53:59 +02001475/* Duplicate a file descriptor. The new file descriptor is created as
1476 non-inheritable. Return a new file descriptor on success, raise an OSError
1477 exception and return -1 on error.
1478
1479 The GIL is released to call dup(). The caller must hold the GIL. */
1480int
1481_Py_dup(int fd)
1482{
1483#ifdef MS_WINDOWS
1484 HANDLE handle;
1485 DWORD ftype;
1486#endif
1487
Victor Stinner8a1be612016-03-14 22:07:55 +01001488 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001489
Victor Stinnerdaf45552013-08-28 00:53:59 +02001490#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001491 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001492 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001493 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001494 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001495 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001496 return -1;
1497 }
1498
1499 /* get the file type, ignore the error if it failed */
1500 ftype = GetFileType(handle);
1501
1502 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001503 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001504 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001505 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001506 Py_END_ALLOW_THREADS
1507 if (fd < 0) {
1508 PyErr_SetFromErrno(PyExc_OSError);
1509 return -1;
1510 }
1511
1512 /* Character files like console cannot be make non-inheritable */
1513 if (ftype != FILE_TYPE_CHAR) {
1514 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001515 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001516 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001517 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001518 return -1;
1519 }
1520 }
1521#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1522 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001523 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001524 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001525 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001526 Py_END_ALLOW_THREADS
1527 if (fd < 0) {
1528 PyErr_SetFromErrno(PyExc_OSError);
1529 return -1;
1530 }
1531
1532#else
1533 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001534 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001535 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001536 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001537 Py_END_ALLOW_THREADS
1538 if (fd < 0) {
1539 PyErr_SetFromErrno(PyExc_OSError);
1540 return -1;
1541 }
1542
1543 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001544 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001545 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001546 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001547 return -1;
1548 }
1549#endif
1550 return fd;
1551}
1552
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001553#ifndef MS_WINDOWS
1554/* Get the blocking mode of the file descriptor.
1555 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1556 raise an exception and return -1 on error. */
1557int
1558_Py_get_blocking(int fd)
1559{
Steve Dower8fc89802015-04-12 00:26:27 -04001560 int flags;
1561 _Py_BEGIN_SUPPRESS_IPH
1562 flags = fcntl(fd, F_GETFL, 0);
1563 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001564 if (flags < 0) {
1565 PyErr_SetFromErrno(PyExc_OSError);
1566 return -1;
1567 }
1568
1569 return !(flags & O_NONBLOCK);
1570}
1571
1572/* Set the blocking mode of the specified file descriptor.
1573
1574 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1575 otherwise.
1576
1577 Return 0 on success, raise an exception and return -1 on error. */
1578int
1579_Py_set_blocking(int fd, int blocking)
1580{
1581#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1582 int arg = !blocking;
1583 if (ioctl(fd, FIONBIO, &arg) < 0)
1584 goto error;
1585#else
1586 int flags, res;
1587
Steve Dower8fc89802015-04-12 00:26:27 -04001588 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001589 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001590 if (flags >= 0) {
1591 if (blocking)
1592 flags = flags & (~O_NONBLOCK);
1593 else
1594 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001595
Steve Dower8fc89802015-04-12 00:26:27 -04001596 res = fcntl(fd, F_SETFL, flags);
1597 } else {
1598 res = -1;
1599 }
1600 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001601
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001602 if (res < 0)
1603 goto error;
1604#endif
1605 return 0;
1606
1607error:
1608 PyErr_SetFromErrno(PyExc_OSError);
1609 return -1;
1610}
1611#endif