blob: 06531d97260f9a4b631a98ee368368214bd6e6e0 [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 Stinnere2623772012-11-12 23:04:02 +010023#ifdef __APPLE__
24extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
25#endif
26
Victor Stinnerdaf45552013-08-28 00:53:59 +020027#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020028/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020029
30 -1: unknown
31 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
32 1: open() supports O_CLOEXEC flag, close-on-exec is set
33
Victor Stinnera555cfc2015-03-18 00:22:14 +010034 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
35 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020036int _Py_open_cloexec_works = -1;
37#endif
38
Brett Cannonefb00c02012-02-29 18:31:31 -050039PyObject *
40_Py_device_encoding(int fd)
41{
Victor Stinner14b9b112013-06-25 00:37:25 +020042#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050043 UINT cp;
44#endif
Steve Dower8fc89802015-04-12 00:26:27 -040045 int valid;
46 _Py_BEGIN_SUPPRESS_IPH
47 valid = _PyVerify_fd(fd) && isatty(fd);
48 _Py_END_SUPPRESS_IPH
49 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050050 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040051
Victor Stinner14b9b112013-06-25 00:37:25 +020052#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050053 if (fd == 0)
54 cp = GetConsoleCP();
55 else if (fd == 1 || fd == 2)
56 cp = GetConsoleOutputCP();
57 else
58 cp = 0;
59 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
60 has no console */
61 if (cp != 0)
62 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
63#elif defined(CODESET)
64 {
65 char *codeset = nl_langinfo(CODESET);
66 if (codeset != NULL && codeset[0] != 0)
67 return PyUnicode_FromString(codeset);
68 }
69#endif
70 Py_RETURN_NONE;
71}
72
Victor Stinnerd45c7f82012-12-04 01:34:47 +010073#if !defined(__APPLE__) && !defined(MS_WINDOWS)
74extern int _Py_normalize_encoding(const char *, char *, size_t);
75
76/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
77 On these operating systems, nl_langinfo(CODESET) announces an alias of the
78 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
79 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
80 locale.getpreferredencoding() codec. For example, if command line arguments
81 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
82 UnicodeEncodeError instead of retrieving the original byte string.
83
84 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
85 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
86 one byte in range 0x80-0xff can be decoded from the locale encoding. The
87 workaround is also enabled on error, for example if getting the locale
88 failed.
89
Philip Jenvey215c49a2013-01-15 13:24:12 -080090 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010091
Victor Stinnerf6a271a2014-08-01 12:28:48 +020092 1: the workaround is used: Py_EncodeLocale() uses
93 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010094 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020095 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
96 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010097 -1: unknown, need to call check_force_ascii() to get the value
98*/
99static int force_ascii = -1;
100
101static int
102check_force_ascii(void)
103{
104 char *loc;
105#if defined(HAVE_LANGINFO_H) && defined(CODESET)
106 char *codeset, **alias;
107 char encoding[100];
108 int is_ascii;
109 unsigned int i;
110 char* ascii_aliases[] = {
111 "ascii",
112 "646",
113 "ansi-x3.4-1968",
114 "ansi-x3-4-1968",
115 "ansi-x3.4-1986",
116 "cp367",
117 "csascii",
118 "ibm367",
119 "iso646-us",
120 "iso-646.irv-1991",
121 "iso-ir-6",
122 "us",
123 "us-ascii",
124 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 Stinner4e314432010-10-07 21:45:39 +0000252
253/* Decode a byte string from the locale encoding with the
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200254 surrogateescape error handler: undecodable bytes are decoded as characters
255 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
Victor Stinner4e314432010-10-07 21:45:39 +0000256 character, escape the bytes using the surrogateescape error handler instead
257 of decoding them.
258
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200259 Return a pointer to a newly allocated wide character string, use
260 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
261 wide characters excluding the null character into *size
Victor Stinner4e314432010-10-07 21:45:39 +0000262
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200263 Return NULL on decoding error or memory allocation error. If *size* is not
264 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
265 decoding error.
Victor Stinner19de4c32010-11-08 23:30:46 +0000266
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200267 Decoding errors should never happen, unless there is a bug in the C
268 library.
269
270 Use the Py_EncodeLocale() function to encode the character string back to a
271 byte string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000272wchar_t*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200273Py_DecodeLocale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000274{
Victor Stinnere2623772012-11-12 23:04:02 +0100275#ifdef __APPLE__
276 wchar_t *wstr;
277 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100278 if (size != NULL) {
279 if (wstr != NULL)
280 *size = wcslen(wstr);
281 else
282 *size = (size_t)-1;
283 }
Victor Stinnere2623772012-11-12 23:04:02 +0100284 return wstr;
285#else
Victor Stinner4e314432010-10-07 21:45:39 +0000286 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100287 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000288 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200289#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000290 unsigned char *in;
291 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000292 mbstate_t mbs;
293#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100294
295#ifndef MS_WINDOWS
296 if (force_ascii == -1)
297 force_ascii = check_force_ascii();
298
299 if (force_ascii) {
300 /* force ASCII encoding to workaround mbstowcs() issue */
301 res = decode_ascii_surrogateescape(arg, size);
302 if (res == NULL)
303 goto oom;
304 return res;
305 }
306#endif
307
308#ifdef HAVE_BROKEN_MBSTOWCS
309 /* Some platforms have a broken implementation of
310 * mbstowcs which does not count the characters that
311 * would result from conversion. Use an upper bound.
312 */
313 argsize = strlen(arg);
314#else
315 argsize = mbstowcs(NULL, arg, 0);
316#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000317 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600318 if (argsize == PY_SSIZE_T_MAX)
319 goto oom;
320 argsize += 1;
321 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
322 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600323 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000324 if (!res)
325 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600326 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000327 if (count != (size_t)-1) {
328 wchar_t *tmp;
329 /* Only use the result if it contains no
330 surrogate characters. */
331 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100332 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000333 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000334 if (*tmp == 0) {
335 if (size != NULL)
336 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000337 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000338 }
Victor Stinner4e314432010-10-07 21:45:39 +0000339 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200340 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000341 }
342 /* Conversion failed. Fall back to escaping with surrogateescape. */
343#ifdef HAVE_MBRTOWC
344 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
345
346 /* Overallocate; as multi-byte characters are in the argument, the
347 actual output could use less memory. */
348 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600349 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
350 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200351 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000352 if (!res)
353 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000354 in = (unsigned char*)arg;
355 out = res;
356 memset(&mbs, 0, sizeof mbs);
357 while (argsize) {
358 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
359 if (converted == 0)
360 /* Reached end of string; null char stored. */
361 break;
362 if (converted == (size_t)-2) {
363 /* Incomplete character. This should never happen,
364 since we provide everything that we have -
365 unless there is a bug in the C library, or I
366 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200367 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100368 if (size != NULL)
369 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000370 return NULL;
371 }
372 if (converted == (size_t)-1) {
373 /* Conversion error. Escape as UTF-8b, and start over
374 in the initial shift state. */
375 *out++ = 0xdc00 + *in++;
376 argsize--;
377 memset(&mbs, 0, sizeof mbs);
378 continue;
379 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100380 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000381 /* Surrogate character. Escape the original
382 byte sequence with surrogateescape. */
383 argsize -= converted;
384 while (converted--)
385 *out++ = 0xdc00 + *in++;
386 continue;
387 }
388 /* successfully converted some bytes */
389 in += converted;
390 argsize -= converted;
391 out++;
392 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100393 if (size != NULL)
394 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100395#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000396 /* Cannot use C locale for escaping; manually escape as if charset
397 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
398 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100399 res = decode_ascii_surrogateescape(arg, size);
400 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100401 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100402#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000403 return res;
404oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100405 if (size != NULL)
406 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000407 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100408#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000409}
410
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200411/* Encode a wide character string to the locale encoding with the
412 surrogateescape error handler: surrogate characters in the range
413 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
Victor Stinner4e314432010-10-07 21:45:39 +0000414
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200415 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
416 the memory. Return NULL on encoding or memory allocation error.
Victor Stinner4e314432010-10-07 21:45:39 +0000417
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200418 If error_pos is not NULL, *error_pos is set to the index of the invalid
419 character on encoding error, or set to (size_t)-1 otherwise.
Victor Stinner2f02a512010-11-08 22:43:46 +0000420
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200421 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
422 character string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000423char*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200424Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000425{
Victor Stinnere2623772012-11-12 23:04:02 +0100426#ifdef __APPLE__
427 Py_ssize_t len;
428 PyObject *unicode, *bytes = NULL;
429 char *cpath;
430
431 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100432 if (unicode == NULL)
Victor Stinnere2623772012-11-12 23:04:02 +0100433 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100434
435 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
436 Py_DECREF(unicode);
437 if (bytes == NULL) {
438 PyErr_Clear();
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100439 if (error_pos != NULL)
440 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100441 return NULL;
442 }
443
444 len = PyBytes_GET_SIZE(bytes);
445 cpath = PyMem_Malloc(len+1);
446 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100447 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100448 Py_DECREF(bytes);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100449 if (error_pos != NULL)
450 *error_pos = (size_t)-1;
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;
456#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000457 const size_t len = wcslen(text);
458 char *result = NULL, *bytes = NULL;
459 size_t i, size, converted;
460 wchar_t c, buf[2];
461
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100462#ifndef MS_WINDOWS
463 if (force_ascii == -1)
464 force_ascii = check_force_ascii();
465
466 if (force_ascii)
467 return encode_ascii_surrogateescape(text, error_pos);
468#endif
469
Victor Stinner4e314432010-10-07 21:45:39 +0000470 /* The function works in two steps:
471 1. compute the length of the output buffer in bytes (size)
472 2. outputs the bytes */
473 size = 0;
474 buf[1] = 0;
475 while (1) {
476 for (i=0; i < len; i++) {
477 c = text[i];
478 if (c >= 0xdc80 && c <= 0xdcff) {
479 /* UTF-8b surrogate */
480 if (bytes != NULL) {
481 *bytes++ = c - 0xdc00;
482 size--;
483 }
484 else
485 size++;
486 continue;
487 }
488 else {
489 buf[0] = c;
490 if (bytes != NULL)
491 converted = wcstombs(bytes, buf, size);
492 else
493 converted = wcstombs(NULL, buf, 0);
494 if (converted == (size_t)-1) {
495 if (result != NULL)
496 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000497 if (error_pos != NULL)
498 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000499 return NULL;
500 }
501 if (bytes != NULL) {
502 bytes += converted;
503 size -= converted;
504 }
505 else
506 size += converted;
507 }
508 }
509 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100510 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000511 break;
512 }
513
514 size += 1; /* nul byte at the end */
515 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100516 if (result == NULL) {
517 if (error_pos != NULL)
518 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000519 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100520 }
Victor Stinner4e314432010-10-07 21:45:39 +0000521 bytes = result;
522 }
523 return result;
Victor Stinnere2623772012-11-12 23:04:02 +0100524#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000525}
526
Victor Stinner6672d0c2010-10-07 22:53:43 +0000527
Steve Dowerf2f373f2015-02-21 08:44:05 -0800528#ifdef MS_WINDOWS
529static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
530
531static void
532FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
533{
534 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
535 /* Cannot simply cast and dereference in_ptr,
536 since it might not be aligned properly */
537 __int64 in;
538 memcpy(&in, in_ptr, sizeof(in));
539 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
540 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
541}
542
543void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800544_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800545{
546 /* XXX endianness */
547 __int64 out;
548 out = time_in + secs_between_epochs;
549 out = out * 10000000 + nsec_in / 100;
550 memcpy(out_ptr, &out, sizeof(out));
551}
552
553/* Below, we *know* that ugo+r is 0444 */
554#if _S_IREAD != 0400
555#error Unsupported C library
556#endif
557static int
558attributes_to_mode(DWORD attr)
559{
560 int m = 0;
561 if (attr & FILE_ATTRIBUTE_DIRECTORY)
562 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
563 else
564 m |= _S_IFREG;
565 if (attr & FILE_ATTRIBUTE_READONLY)
566 m |= 0444;
567 else
568 m |= 0666;
569 return m;
570}
571
Steve Dowerbf1f3762015-02-21 15:26:02 -0800572void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200573_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
574 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800575{
576 memset(result, 0, sizeof(*result));
577 result->st_mode = attributes_to_mode(info->dwFileAttributes);
578 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
579 result->st_dev = info->dwVolumeSerialNumber;
580 result->st_rdev = result->st_dev;
581 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
582 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
583 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
584 result->st_nlink = info->nNumberOfLinks;
585 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
586 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
587 /* first clear the S_IFMT bits */
588 result->st_mode ^= (result->st_mode & S_IFMT);
589 /* now set the bits that make this a symlink */
590 result->st_mode |= S_IFLNK;
591 }
592 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800593}
594#endif
595
596/* Return information about a file.
597
598 On POSIX, use fstat().
599
600 On Windows, use GetFileType() and GetFileInformationByHandle() which support
601 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
Martin Panter6f9b0102015-12-17 10:18:28 +0000602 than 2 GB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800603 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200604
605 On Windows, set the last Windows error and return nonzero on error. On
606 POSIX, set errno and return nonzero on error. Fill status and return 0 on
607 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800608int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200609_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800610{
611#ifdef MS_WINDOWS
612 BY_HANDLE_FILE_INFORMATION info;
613 HANDLE h;
614 int type;
615
616 if (!_PyVerify_fd(fd))
617 h = INVALID_HANDLE_VALUE;
Steve Dower8fc89802015-04-12 00:26:27 -0400618 else {
619 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800620 h = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400621 _Py_END_SUPPRESS_IPH
622 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800623
624 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400625 /* errno is already set by _get_osfhandle, but we also set
626 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800627 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800628 return -1;
629 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200630 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800631
632 type = GetFileType(h);
633 if (type == FILE_TYPE_UNKNOWN) {
634 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400635 if (error != 0) {
636 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800637 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400638 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800639 /* else: valid but unknown file */
640 }
641
642 if (type != FILE_TYPE_DISK) {
643 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200644 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800645 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200646 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800647 return 0;
648 }
649
650 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400651 /* The Win32 error is already set, but we also set errno for
652 callers who expect it */
653 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800654 return -1;
655 }
656
Victor Stinnere134a7f2015-03-30 10:09:31 +0200657 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800658 /* specific to fstat() */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200659 status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800660 return 0;
661#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200662 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800663#endif
664}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800665
Victor Stinnere134a7f2015-03-30 10:09:31 +0200666/* Return information about a file.
667
668 On POSIX, use fstat().
669
670 On Windows, use GetFileType() and GetFileInformationByHandle() which support
671 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
Martin Panter6f9b0102015-12-17 10:18:28 +0000672 than 2 GB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200673 #23152.
674
675 Raise an exception and return -1 on error. On Windows, set the last Windows
676 error on error. On POSIX, set errno on error. Fill status and return 0 on
677 success.
678
Victor Stinner6f4fae82015-04-01 18:34:32 +0200679 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
680 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200681int
682_Py_fstat(int fd, struct _Py_stat_struct *status)
683{
684 int res;
685
Victor Stinner8a1be612016-03-14 22:07:55 +0100686#ifdef WITH_THREAD
687 assert(PyGILState_Check());
688#endif
689
Victor Stinnere134a7f2015-03-30 10:09:31 +0200690 Py_BEGIN_ALLOW_THREADS
691 res = _Py_fstat_noraise(fd, status);
692 Py_END_ALLOW_THREADS
693
694 if (res != 0) {
695#ifdef MS_WINDOWS
696 PyErr_SetFromWindowsErr(0);
697#else
698 PyErr_SetFromErrno(PyExc_OSError);
699#endif
700 return -1;
701 }
702 return 0;
703}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800704
Victor Stinner6672d0c2010-10-07 22:53:43 +0000705/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
706 call stat() otherwise. Only fill st_mode attribute on Windows.
707
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100708 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
709 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000710
711int
Victor Stinnera4a75952010-10-07 22:23:10 +0000712_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000713{
714#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000715 int err;
716 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100717 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000718
Victor Stinneree587ea2011-11-17 00:51:38 +0100719 wpath = PyUnicode_AsUnicode(path);
720 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100721 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100722 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000723 if (!err)
724 statbuf->st_mode = wstatbuf.st_mode;
725 return err;
726#else
727 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000728 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000729 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100730 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000731 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
732 Py_DECREF(bytes);
733 return ret;
734#endif
735}
736
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100737
Antoine Pitrou409b5382013-10-12 22:41:17 +0200738static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200739get_inheritable(int fd, int raise)
740{
741#ifdef MS_WINDOWS
742 HANDLE handle;
743 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000744
Victor Stinnerdaf45552013-08-28 00:53:59 +0200745 if (!_PyVerify_fd(fd)) {
746 if (raise)
747 PyErr_SetFromErrno(PyExc_OSError);
748 return -1;
749 }
750
Steve Dower8fc89802015-04-12 00:26:27 -0400751 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200752 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400753 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200754 if (handle == INVALID_HANDLE_VALUE) {
755 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700756 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200757 return -1;
758 }
759
760 if (!GetHandleInformation(handle, &flags)) {
761 if (raise)
762 PyErr_SetFromWindowsErr(0);
763 return -1;
764 }
765
766 return (flags & HANDLE_FLAG_INHERIT);
767#else
768 int flags;
769
770 flags = fcntl(fd, F_GETFD, 0);
771 if (flags == -1) {
772 if (raise)
773 PyErr_SetFromErrno(PyExc_OSError);
774 return -1;
775 }
776 return !(flags & FD_CLOEXEC);
777#endif
778}
779
780/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200781 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200782 raise an exception and return -1 on error. */
783int
784_Py_get_inheritable(int fd)
785{
786 return get_inheritable(fd, 1);
787}
788
789static int
790set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
791{
792#ifdef MS_WINDOWS
793 HANDLE handle;
794 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200795#else
796#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
797 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200798 int request;
799 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200800#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +0200801 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200802 int res;
803#endif
804
805 /* atomic_flag_works can only be used to make the file descriptor
806 non-inheritable */
807 assert(!(atomic_flag_works != NULL && inheritable));
808
809 if (atomic_flag_works != NULL && !inheritable) {
810 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700811 int isInheritable = get_inheritable(fd, raise);
812 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200813 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700814 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200815 }
816
817 if (*atomic_flag_works)
818 return 0;
819 }
820
821#ifdef MS_WINDOWS
822 if (!_PyVerify_fd(fd)) {
823 if (raise)
824 PyErr_SetFromErrno(PyExc_OSError);
825 return -1;
826 }
827
Steve Dower8fc89802015-04-12 00:26:27 -0400828 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200829 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400830 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200831 if (handle == INVALID_HANDLE_VALUE) {
832 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700833 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200834 return -1;
835 }
836
837 if (inheritable)
838 flags = HANDLE_FLAG_INHERIT;
839 else
840 flags = 0;
841 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
842 if (raise)
843 PyErr_SetFromWindowsErr(0);
844 return -1;
845 }
846 return 0;
847
Victor Stinnerdaf45552013-08-28 00:53:59 +0200848#else
Victor Stinner282124b2014-09-02 11:41:04 +0200849
850#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
851 if (ioctl_works != 0) {
852 /* fast-path: ioctl() only requires one syscall */
853 if (inheritable)
854 request = FIONCLEX;
855 else
856 request = FIOCLEX;
857 err = ioctl(fd, request, NULL);
858 if (!err) {
859 ioctl_works = 1;
860 return 0;
861 }
862
Victor Stinner3116cc42016-05-19 16:46:18 +0200863 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +0200864 if (raise)
865 PyErr_SetFromErrno(PyExc_OSError);
866 return -1;
867 }
868 else {
869 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
870 device". The ioctl is declared but not supported by the kernel.
871 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +0200872 Illumos-based OS for example.
873
874 Issue #27057: When SELinux policy disallows ioctl it will fail
875 with EACCES. While FIOCLEX is safe operation it may be
876 unavailable because ioctl was denied altogether.
877 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +0200878 ioctl_works = 0;
879 }
880 /* fallback to fcntl() if ioctl() does not work */
881 }
882#endif
883
884 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200885 flags = fcntl(fd, F_GETFD);
886 if (flags < 0) {
887 if (raise)
888 PyErr_SetFromErrno(PyExc_OSError);
889 return -1;
890 }
891
Victor Stinnera858bbd2016-04-17 16:51:52 +0200892 if (inheritable) {
893 new_flags = flags & ~FD_CLOEXEC;
894 }
895 else {
896 new_flags = flags | FD_CLOEXEC;
897 }
898
899 if (new_flags == flags) {
900 /* FD_CLOEXEC flag already set/cleared: nothing to do */
901 return 0;
902 }
903
Victor Stinnerdaf45552013-08-28 00:53:59 +0200904 res = fcntl(fd, F_SETFD, flags);
905 if (res < 0) {
906 if (raise)
907 PyErr_SetFromErrno(PyExc_OSError);
908 return -1;
909 }
910 return 0;
911#endif
912}
913
914/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200915 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200916static int
917make_non_inheritable(int fd)
918{
919 return set_inheritable(fd, 0, 0, NULL);
920}
921
922/* Set the inheritable flag of the specified file descriptor.
923 On success: return 0, on error: raise an exception if raise is nonzero
924 and return -1.
925
926 If atomic_flag_works is not NULL:
927
928 * if *atomic_flag_works==-1, check if the inheritable is set on the file
929 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
930 set the inheritable flag
931 * if *atomic_flag_works==1: do nothing
932 * if *atomic_flag_works==0: set inheritable flag to False
933
934 Set atomic_flag_works to NULL if no atomic flag was used to create the
935 file descriptor.
936
937 atomic_flag_works can only be used to make a file descriptor
938 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
939int
940_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
941{
942 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
943}
944
Victor Stinnera555cfc2015-03-18 00:22:14 +0100945static int
946_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200947{
948 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100949 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100950#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200951 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100952#endif
953
954#ifdef MS_WINDOWS
955 flags |= O_NOINHERIT;
956#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200957 atomic_flag_works = &_Py_open_cloexec_works;
958 flags |= O_CLOEXEC;
959#else
960 atomic_flag_works = NULL;
961#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200962
Victor Stinnera555cfc2015-03-18 00:22:14 +0100963 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100964 do {
965 Py_BEGIN_ALLOW_THREADS
966 fd = open(pathname, flags);
967 Py_END_ALLOW_THREADS
968 } while (fd < 0
969 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
970 if (async_err)
971 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100972 if (fd < 0) {
973 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
974 return -1;
975 }
976 }
977 else {
978 fd = open(pathname, flags);
979 if (fd < 0)
980 return -1;
981 }
982
983#ifndef MS_WINDOWS
984 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200985 close(fd);
986 return -1;
987 }
Victor Stinnera555cfc2015-03-18 00:22:14 +0100988#endif
989
Victor Stinnerdaf45552013-08-28 00:53:59 +0200990 return fd;
991}
992
Victor Stinnera555cfc2015-03-18 00:22:14 +0100993/* Open a file with the specified flags (wrapper to open() function).
994 Return a file descriptor on success. Raise an exception and return -1 on
995 error.
996
997 The file descriptor is created non-inheritable.
998
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100999 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1000 except if the Python signal handler raises an exception.
1001
Victor Stinner6f4fae82015-04-01 18:34:32 +02001002 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001003int
1004_Py_open(const char *pathname, int flags)
1005{
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001006#ifdef WITH_THREAD
Victor Stinnera555cfc2015-03-18 00:22:14 +01001007 /* _Py_open() must be called with the GIL held. */
1008 assert(PyGILState_Check());
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001009#endif
Victor Stinnera555cfc2015-03-18 00:22:14 +01001010 return _Py_open_impl(pathname, flags, 1);
1011}
1012
1013/* Open a file with the specified flags (wrapper to open() function).
1014 Return a file descriptor on success. Set errno and return -1 on error.
1015
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001016 The file descriptor is created non-inheritable.
1017
1018 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001019int
1020_Py_open_noraise(const char *pathname, int flags)
1021{
1022 return _Py_open_impl(pathname, flags, 0);
1023}
1024
Victor Stinnerdaf45552013-08-28 00:53:59 +02001025/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001026 encoding and use fopen() otherwise.
1027
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001028 The file descriptor is created non-inheritable.
1029
1030 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001031FILE *
1032_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1033{
Victor Stinner4e314432010-10-07 21:45:39 +00001034 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001035#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001036 char *cpath;
1037 char cmode[10];
1038 size_t r;
1039 r = wcstombs(cmode, mode, 10);
1040 if (r == (size_t)-1 || r >= 10) {
1041 errno = EINVAL;
1042 return NULL;
1043 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001044 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001045 if (cpath == NULL)
1046 return NULL;
1047 f = fopen(cpath, cmode);
1048 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001049#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001050 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001051#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001052 if (f == NULL)
1053 return NULL;
1054 if (make_non_inheritable(fileno(f)) < 0) {
1055 fclose(f);
1056 return NULL;
1057 }
1058 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001059}
1060
Victor Stinnere42ccd22015-03-18 01:39:23 +01001061/* Wrapper to fopen().
1062
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001063 The file descriptor is created non-inheritable.
1064
1065 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001066FILE*
1067_Py_fopen(const char *pathname, const char *mode)
1068{
1069 FILE *f = fopen(pathname, mode);
1070 if (f == NULL)
1071 return NULL;
1072 if (make_non_inheritable(fileno(f)) < 0) {
1073 fclose(f);
1074 return NULL;
1075 }
1076 return f;
1077}
1078
1079/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001080 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001081
Victor Stinnere42ccd22015-03-18 01:39:23 +01001082 Return the new file object on success. Raise an exception and return NULL
1083 on error.
1084
1085 The file descriptor is created non-inheritable.
1086
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001087 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1088 except if the Python signal handler raises an exception.
1089
Victor Stinner6f4fae82015-04-01 18:34:32 +02001090 Release the GIL to call _wfopen() or fopen(). The caller must hold
1091 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001092FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001093_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001094{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001095 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001096 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001097#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +01001098 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001099 wchar_t wmode[10];
1100 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001101
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001102#ifdef WITH_THREAD
Victor Stinnere42ccd22015-03-18 01:39:23 +01001103 assert(PyGILState_Check());
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001104#endif
Victor Stinnere42ccd22015-03-18 01:39:23 +01001105
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001106 if (!PyUnicode_Check(path)) {
1107 PyErr_Format(PyExc_TypeError,
1108 "str file path expected under Windows, got %R",
1109 Py_TYPE(path));
1110 return NULL;
1111 }
Victor Stinneree587ea2011-11-17 00:51:38 +01001112 wpath = PyUnicode_AsUnicode(path);
1113 if (wpath == NULL)
1114 return NULL;
1115
Victor Stinner4e314432010-10-07 21:45:39 +00001116 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001117 if (usize == 0) {
1118 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001119 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001120 }
Victor Stinner4e314432010-10-07 21:45:39 +00001121
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001122 do {
1123 Py_BEGIN_ALLOW_THREADS
1124 f = _wfopen(wpath, wmode);
1125 Py_END_ALLOW_THREADS
1126 } while (f == NULL
1127 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001128#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001129 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001130 char *path_bytes;
1131
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001132#ifdef WITH_THREAD
Victor Stinnere42ccd22015-03-18 01:39:23 +01001133 assert(PyGILState_Check());
Victor Stinnerbc5b80b2015-10-11 09:54:42 +02001134#endif
Victor Stinnere42ccd22015-03-18 01:39:23 +01001135
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001136 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001137 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001138 path_bytes = PyBytes_AS_STRING(bytes);
1139
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001140 do {
1141 Py_BEGIN_ALLOW_THREADS
1142 f = fopen(path_bytes, mode);
1143 Py_END_ALLOW_THREADS
1144 } while (f == NULL
1145 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001146
Victor Stinner4e314432010-10-07 21:45:39 +00001147 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001148#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001149 if (async_err)
1150 return NULL;
1151
Victor Stinnere42ccd22015-03-18 01:39:23 +01001152 if (f == NULL) {
1153 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001154 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001155 }
1156
1157 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001158 fclose(f);
1159 return NULL;
1160 }
1161 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001162}
1163
Victor Stinner66aab0c2015-03-19 22:53:20 +01001164/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001165
1166 On success, return the number of read bytes, it can be lower than count.
1167 If the current file offset is at or past the end of file, no bytes are read,
1168 and read() returns zero.
1169
1170 On error, raise an exception, set errno and return -1.
1171
1172 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1173 If the Python signal handler raises an exception, the function returns -1
1174 (the syscall is not retried).
1175
1176 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001177Py_ssize_t
1178_Py_read(int fd, void *buf, size_t count)
1179{
1180 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001181 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001182 int async_err = 0;
1183
Victor Stinner8a1be612016-03-14 22:07:55 +01001184#ifdef WITH_THREAD
1185 assert(PyGILState_Check());
1186#endif
1187
Victor Stinner66aab0c2015-03-19 22:53:20 +01001188 /* _Py_read() must not be called with an exception set, otherwise the
1189 * caller may think that read() was interrupted by a signal and the signal
1190 * handler raised an exception. */
1191 assert(!PyErr_Occurred());
1192
Victor Stinnerc1cf4f72015-03-19 23:53:04 +01001193 if (!_PyVerify_fd(fd)) {
Victor Stinnera3c02022015-03-20 11:58:18 +01001194 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1195 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001196 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001197 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001198 return -1;
1199 }
1200
1201#ifdef MS_WINDOWS
1202 if (count > INT_MAX) {
1203 /* On Windows, the count parameter of read() is an int */
1204 count = INT_MAX;
1205 }
1206#else
1207 if (count > PY_SSIZE_T_MAX) {
1208 /* if count is greater than PY_SSIZE_T_MAX,
1209 * read() result is undefined */
1210 count = PY_SSIZE_T_MAX;
1211 }
1212#endif
1213
Steve Dower8fc89802015-04-12 00:26:27 -04001214 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001215 do {
1216 Py_BEGIN_ALLOW_THREADS
1217 errno = 0;
1218#ifdef MS_WINDOWS
1219 n = read(fd, buf, (int)count);
1220#else
1221 n = read(fd, buf, count);
1222#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001223 /* save/restore errno because PyErr_CheckSignals()
1224 * and PyErr_SetFromErrno() can modify it */
1225 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001226 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001227 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001228 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001229 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001230
1231 if (async_err) {
1232 /* read() was interrupted by a signal (failed with EINTR)
1233 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001234 errno = err;
1235 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001236 return -1;
1237 }
1238 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001239 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001240 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001241 return -1;
1242 }
1243
1244 return n;
1245}
1246
Victor Stinner82c3e452015-04-01 18:34:45 +02001247static Py_ssize_t
1248_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001249{
1250 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001251 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001252 int async_err = 0;
1253
Victor Stinner66aab0c2015-03-19 22:53:20 +01001254 if (!_PyVerify_fd(fd)) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001255 if (gil_held) {
1256 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1257 err = errno;
1258 PyErr_SetFromErrno(PyExc_OSError);
1259 errno = err;
1260 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001261 return -1;
1262 }
1263
Steve Dower8fc89802015-04-12 00:26:27 -04001264 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001265#ifdef MS_WINDOWS
1266 if (count > 32767 && isatty(fd)) {
1267 /* Issue #11395: the Windows console returns an error (12: not
1268 enough space error) on writing into stdout if stdout mode is
1269 binary and the length is greater than 66,000 bytes (or less,
1270 depending on heap usage). */
1271 count = 32767;
1272 }
1273 else if (count > INT_MAX)
1274 count = INT_MAX;
1275#else
1276 if (count > PY_SSIZE_T_MAX) {
1277 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1278 * to do it ourself to have a portable behaviour. */
1279 count = PY_SSIZE_T_MAX;
1280 }
1281#endif
1282
Victor Stinner82c3e452015-04-01 18:34:45 +02001283 if (gil_held) {
1284 do {
1285 Py_BEGIN_ALLOW_THREADS
1286 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001287#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001288 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001289#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001290 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001291#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001292 /* save/restore errno because PyErr_CheckSignals()
1293 * and PyErr_SetFromErrno() can modify it */
1294 err = errno;
1295 Py_END_ALLOW_THREADS
1296 } while (n < 0 && err == EINTR &&
1297 !(async_err = PyErr_CheckSignals()));
1298 }
1299 else {
1300 do {
1301 errno = 0;
1302#ifdef MS_WINDOWS
1303 n = write(fd, buf, (int)count);
1304#else
1305 n = write(fd, buf, count);
1306#endif
1307 err = errno;
1308 } while (n < 0 && err == EINTR);
1309 }
Steve Dower8fc89802015-04-12 00:26:27 -04001310 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001311
1312 if (async_err) {
1313 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001314 and the Python signal handler raised an exception (if gil_held is
1315 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001316 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001317 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001318 return -1;
1319 }
1320 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001321 if (gil_held)
1322 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001323 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001324 return -1;
1325 }
1326
1327 return n;
1328}
1329
Victor Stinner82c3e452015-04-01 18:34:45 +02001330/* Write count bytes of buf into fd.
1331
1332 On success, return the number of written bytes, it can be lower than count
1333 including 0. On error, raise an exception, set errno and return -1.
1334
1335 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1336 If the Python signal handler raises an exception, the function returns -1
1337 (the syscall is not retried).
1338
1339 Release the GIL to call write(). The caller must hold the GIL. */
1340Py_ssize_t
1341_Py_write(int fd, const void *buf, size_t count)
1342{
Victor Stinner8a1be612016-03-14 22:07:55 +01001343#ifdef WITH_THREAD
1344 assert(PyGILState_Check());
1345#endif
1346
Victor Stinner82c3e452015-04-01 18:34:45 +02001347 /* _Py_write() must not be called with an exception set, otherwise the
1348 * caller may think that write() was interrupted by a signal and the signal
1349 * handler raised an exception. */
1350 assert(!PyErr_Occurred());
1351
1352 return _Py_write_impl(fd, buf, count, 1);
1353}
1354
1355/* Write count bytes of buf into fd.
1356 *
1357 * On success, return the number of written bytes, it can be lower than count
1358 * including 0. On error, set errno and return -1.
1359 *
1360 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1361 * without calling the Python signal handler. */
1362Py_ssize_t
1363_Py_write_noraise(int fd, const void *buf, size_t count)
1364{
1365 return _Py_write_impl(fd, buf, count, 0);
1366}
1367
Victor Stinner4e314432010-10-07 21:45:39 +00001368#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001369
1370/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001371 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001372
Victor Stinner4e314432010-10-07 21:45:39 +00001373int
1374_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1375{
1376 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001377 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001378 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001379 int res;
1380 size_t r1;
1381
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001382 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001383 if (cpath == NULL) {
1384 errno = EINVAL;
1385 return -1;
1386 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001387 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001388 PyMem_Free(cpath);
1389 if (res == -1)
1390 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001391 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001392 errno = EINVAL;
1393 return -1;
1394 }
1395 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001396 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001397 if (wbuf == NULL) {
1398 errno = EINVAL;
1399 return -1;
1400 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001401 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001402 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001403 errno = EINVAL;
1404 return -1;
1405 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001406 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001407 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001408 return (int)r1;
1409}
1410#endif
1411
1412#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001413
1414/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001415 encoding, decode the result from the locale encoding.
1416 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001417
Victor Stinner4e314432010-10-07 21:45:39 +00001418wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001419_Py_wrealpath(const wchar_t *path,
1420 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001421{
1422 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001423 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001424 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001425 char *res;
1426 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001427 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001428 if (cpath == NULL) {
1429 errno = EINVAL;
1430 return NULL;
1431 }
1432 res = realpath(cpath, cresolved_path);
1433 PyMem_Free(cpath);
1434 if (res == NULL)
1435 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001436
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001437 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001438 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001439 errno = EINVAL;
1440 return NULL;
1441 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001442 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001443 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001444 errno = EINVAL;
1445 return NULL;
1446 }
1447 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001448 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001449 return resolved_path;
1450}
1451#endif
1452
Victor Stinnerf4061da2010-10-14 12:37:19 +00001453/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001454 including the null character. Decode the path from the locale encoding.
1455 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001456
Victor Stinner4e314432010-10-07 21:45:39 +00001457wchar_t*
1458_Py_wgetcwd(wchar_t *buf, size_t size)
1459{
1460#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001461 int isize = (int)Py_MIN(size, INT_MAX);
1462 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001463#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001464 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001465 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001466 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001467
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001468 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001469 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001470 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001471 if (wname == NULL)
1472 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001473 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001474 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001475 return NULL;
1476 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001477 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001478 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001479 return buf;
1480#endif
1481}
1482
Victor Stinnerdaf45552013-08-28 00:53:59 +02001483/* Duplicate a file descriptor. The new file descriptor is created as
1484 non-inheritable. Return a new file descriptor on success, raise an OSError
1485 exception and return -1 on error.
1486
1487 The GIL is released to call dup(). The caller must hold the GIL. */
1488int
1489_Py_dup(int fd)
1490{
1491#ifdef MS_WINDOWS
1492 HANDLE handle;
1493 DWORD ftype;
1494#endif
1495
Victor Stinner8a1be612016-03-14 22:07:55 +01001496#ifdef WITH_THREAD
1497 assert(PyGILState_Check());
1498#endif
1499
Victor Stinnerdaf45552013-08-28 00:53:59 +02001500 if (!_PyVerify_fd(fd)) {
1501 PyErr_SetFromErrno(PyExc_OSError);
1502 return -1;
1503 }
1504
1505#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001506 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001507 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001508 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001509 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001510 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001511 return -1;
1512 }
1513
1514 /* get the file type, ignore the error if it failed */
1515 ftype = GetFileType(handle);
1516
1517 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001518 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001519 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001520 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001521 Py_END_ALLOW_THREADS
1522 if (fd < 0) {
1523 PyErr_SetFromErrno(PyExc_OSError);
1524 return -1;
1525 }
1526
1527 /* Character files like console cannot be make non-inheritable */
1528 if (ftype != FILE_TYPE_CHAR) {
1529 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001530 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001531 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001532 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001533 return -1;
1534 }
1535 }
1536#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
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 = fcntl(fd, F_DUPFD_CLOEXEC, 0);
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#else
1548 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001549 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001550 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001551 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001552 Py_END_ALLOW_THREADS
1553 if (fd < 0) {
1554 PyErr_SetFromErrno(PyExc_OSError);
1555 return -1;
1556 }
1557
1558 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001559 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001560 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001561 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001562 return -1;
1563 }
1564#endif
1565 return fd;
1566}
1567
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001568#ifndef MS_WINDOWS
1569/* Get the blocking mode of the file descriptor.
1570 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1571 raise an exception and return -1 on error. */
1572int
1573_Py_get_blocking(int fd)
1574{
Steve Dower8fc89802015-04-12 00:26:27 -04001575 int flags;
1576 _Py_BEGIN_SUPPRESS_IPH
1577 flags = fcntl(fd, F_GETFL, 0);
1578 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001579 if (flags < 0) {
1580 PyErr_SetFromErrno(PyExc_OSError);
1581 return -1;
1582 }
1583
1584 return !(flags & O_NONBLOCK);
1585}
1586
1587/* Set the blocking mode of the specified file descriptor.
1588
1589 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1590 otherwise.
1591
1592 Return 0 on success, raise an exception and return -1 on error. */
1593int
1594_Py_set_blocking(int fd, int blocking)
1595{
1596#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1597 int arg = !blocking;
1598 if (ioctl(fd, FIONBIO, &arg) < 0)
1599 goto error;
1600#else
1601 int flags, res;
1602
Steve Dower8fc89802015-04-12 00:26:27 -04001603 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001604 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001605 if (flags >= 0) {
1606 if (blocking)
1607 flags = flags & (~O_NONBLOCK);
1608 else
1609 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001610
Steve Dower8fc89802015-04-12 00:26:27 -04001611 res = fcntl(fd, F_SETFL, flags);
1612 } else {
1613 res = -1;
1614 }
1615 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001616
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001617 if (res < 0)
1618 goto error;
1619#endif
1620 return 0;
1621
1622error:
1623 PyErr_SetFromErrno(PyExc_OSError);
1624 return -1;
1625}
1626#endif
1627
Steve Dower8fc89802015-04-12 00:26:27 -04001628#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
Steve Dowerd81431f2015-03-06 14:47:02 -08001629/* Legacy implementation of _PyVerify_fd while transitioning to
1630 * MSVC 14.0. This should eventually be removed. (issue23524)
1631 */
1632
1633/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
1634 * valid and raise an assertion if it isn't.
1635 * Normally, an invalid fd is likely to be a C program error and therefore
1636 * an assertion can be useful, but it does contradict the POSIX standard
1637 * which for write(2) states:
1638 * "Otherwise, -1 shall be returned and errno set to indicate the error."
1639 * "[EBADF] The fildes argument is not a valid file descriptor open for
1640 * writing."
1641 * Furthermore, python allows the user to enter any old integer
1642 * as a fd and should merely raise a python exception on error.
1643 * The Microsoft CRT doesn't provide an official way to check for the
1644 * validity of a file descriptor, but we can emulate its internal behaviour
1645 * by using the exported __pinfo data member and knowledge of the
1646 * internal structures involved.
1647 * The structures below must be updated for each version of visual studio
1648 * according to the file internal.h in the CRT source, until MS comes
1649 * up with a less hacky way to do this.
1650 * (all of this is to avoid globally modifying the CRT behaviour using
1651 * _set_invalid_parameter_handler() and _CrtSetReportMode())
1652 */
1653/* The actual size of the structure is determined at runtime.
1654 * Only the first items must be present.
1655 */
1656typedef struct {
1657 intptr_t osfhnd;
1658 char osfile;
1659} my_ioinfo;
1660
1661extern __declspec(dllimport) char * __pioinfo[];
1662#define IOINFO_L2E 5
1663#define IOINFO_ARRAYS 64
1664#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1665#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
1666#define FOPEN 0x01
1667#define _NO_CONSOLE_FILENO (intptr_t)-2
1668
1669/* This function emulates what the windows CRT does to validate file handles */
1670int
1671_PyVerify_fd(int fd)
1672{
1673 const int i1 = fd >> IOINFO_L2E;
1674 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
1675
1676 static size_t sizeof_ioinfo = 0;
1677
1678 /* Determine the actual size of the ioinfo structure,
1679 * as used by the CRT loaded in memory
1680 */
1681 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
1682 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
1683 }
1684 if (sizeof_ioinfo == 0) {
1685 /* This should not happen... */
1686 goto fail;
1687 }
1688
1689 /* See that it isn't a special CLEAR fileno */
1690 if (fd != _NO_CONSOLE_FILENO) {
1691 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
1692 * we check pointer validity and other info
1693 */
1694 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
1695 /* finally, check that the file is open */
1696 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
1697 if (info->osfile & FOPEN) {
1698 return 1;
1699 }
1700 }
1701 }
1702 fail:
1703 errno = EBADF;
1704 return 0;
1705}
1706
Steve Dower8fc89802015-04-12 00:26:27 -04001707#endif /* defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 */